Moving the astvalidation module in astbuilder
[nit.git] / src / astbuilder.nit
index cc7cd88..6340ad2 100644 (file)
 module astbuilder
 
 intrude import semantize::typing
-intrude import semantize::literal
-intrude import semantize::parser
+intrude import literal
+intrude import parser
 intrude import semantize::scope
 
 # General factory to build semantic nodes in the AST of expressions
 class ASTBuilder
        # The module used as reference for the building
-       # It is used to gather types and other stufs
+       # It is used to gather types and other stuff
        var mmodule: MModule
 
        # The anchor used for some mechanism relying on types
@@ -35,7 +35,7 @@ class ASTBuilder
                return new AIntegerExpr.make(value, mmodule.int_type)
        end
 
-       # Make a new instatiation
+       # Make a new instantiation
        fun make_new(callsite: CallSite, args: nullable Array[AExpr]): ANewExpr
        do
                return new ANewExpr.make(callsite, args)
@@ -96,7 +96,7 @@ class ASTBuilder
                return new ABreakExpr.make(escapemark)
        end
 
-       # Make a new condinionnal
+       # Make a new conditional
        # `mtype` is the return type of the whole if, in case of a ternary operator.
        fun make_if(condition: AExpr, mtype: nullable MType): AIfExpr
        do
@@ -128,9 +128,9 @@ redef class AExpr
        private var variable_cache: nullable Variable
 
        # The `detach` method completely remove the node in the parent.
-       # Owever, sometime, it is useful to keep the emplacement of the removed child.
+       # However, sometime, it is useful to keep the emplacement of the removed child.
        #
-       # The standard usecase is the insertion of a node beetwen a parent `p` and a child `p.c`.
+       # The standard use case is the insertion of a node between a parent `p` and a child `p.c`.
        # To create the new node `n`, we need to attach the child to it.
        # But, to put `n` where `c` was in `p`, the place has to be remembered.
        #
@@ -159,10 +159,18 @@ redef class AExpr
                print "add not implemented in {inspect}"
                abort
        end
+
+       redef fun accept_ast_validation(v)
+       do
+               super
+               if mtype == null and not is_typed then
+                       #debug "TYPING: untyped expression"
+               end
+       end
 end
 
 # A placeholder for a `AExpr` node
-# Instances are transiantly used to mark some specific emplacments in the AST
+# Instances are transiantly used to mark some specific emplacements in the AST
 # during complex transformations.
 #
 # Their must not appear in a valid AST
@@ -173,6 +181,12 @@ class APlaceholderExpr
        private init make
        do
        end
+
+       redef fun accept_ast_validation(v)
+       do
+               super
+               debug "PARENT: remaining placeholder"
+       end
 end
 
 redef class ABlockExpr
@@ -181,7 +195,7 @@ redef class ABlockExpr
                self.is_typed = true
        end
 
-       redef fun add(expr: AExpr)
+       redef fun add(expr)
        do
                n_expr.add expr
        end
@@ -196,7 +210,7 @@ redef class ALoopExpr
                n_block.is_typed = true
        end
 
-       redef fun add(expr: AExpr)
+       redef fun add(expr)
        do
                n_block.add expr
        end
@@ -222,7 +236,7 @@ redef class ADoExpr
                return new ABreakExpr.make(escapemark)
        end
 
-       redef fun add(expr: AExpr)
+       redef fun add(expr)
        do
                n_block.add expr
        end
@@ -256,7 +270,10 @@ end
 redef class AType
        private init make
        do
-               _n_id = new TClassid
+               var n_id = new TClassid
+               var n_qid = new AQclassid
+               n_qid.n_id = n_id
+               _n_qid = n_qid
        end
 end
 
@@ -351,3 +368,66 @@ redef class AVarAssignExpr
        end
 end
 
+# Check the consitency of AST
+class ASTValidationVisitor
+       super Visitor
+       redef fun visit(node)
+       do
+               node.accept_ast_validation(self)
+       end
+       private var path = new CircularArray[ANode]
+       private var seen = new HashSet[ANode]
+end
+
+redef class ANode
+       # Recursively validate a AST node.
+       # This ensure that location and parenting are defined and coherent.
+       #
+       # After complex low-level AST manipulation and construction,
+       # it is recommended to call it.
+       #
+       # Note: this just instantiate and run an `ASTValidationVisitor`.
+       fun validate
+       do
+               (new ASTValidationVisitor).enter_visit(self)
+       end
+
+       private fun accept_ast_validation(v: ASTValidationVisitor)
+       do
+               var parent = self.parent
+               var path = v.path
+
+               if path.length > 0 then
+                       var path_parent = v.path.first
+                       if parent == null then
+                               self.parent = path_parent
+                               #debug "PARENT: expected parent: {path_parent}"
+                               v.seen.add(self)
+                       else if parent != path_parent then
+                               self.parent = path_parent
+                               if v.seen.has(self) then
+                                       debug "DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
+                               else
+                                       v.seen.add(self)
+                                       debug "PARENT: expected parent: {path_parent}, got {parent}"
+                               end
+                       end
+               end
+
+               if not isset _location then
+                       #debug "LOCATION: unlocated node {v.path.join(", ")}"
+                       _location = self.parent.location
+               end
+
+               path.unshift(self)
+               visit_all(v)
+               path.shift
+       end
+end
+
+redef class AAnnotation
+       redef fun accept_ast_validation(v)
+       do
+               # Do not enter in annotations
+       end
+end