+redef class AParam
+
+ private init make(v: nullable Variable, t: nullable AType)
+ do
+ _n_id = new TId
+ _variable = v
+ _n_type = t
+ end
+
+ redef fun clone: SELF
+ do
+ var ntype = n_type
+ if ntype != null then ntype = n_type.clone
+ return new AParam.make(variable, ntype)
+ end
+end
+
+redef class ABlockExpr
+ private init make(t: nullable MType)
+ do
+ if t != null then
+ _mtype = t
+ _is_typed = true
+ end
+ end
+
+ redef fun add(expr)
+ do
+ n_expr.add expr
+ expr.parent = self
+ end
+
+ fun add_all(exprs: Array[AExpr])
+ do
+ for expr in exprs do
+ add(expr)
+ end
+ end
+
+ redef fun clone: SELF
+ do
+ var clone = new ABlockExpr.make(mtype)
+ for expr in self.n_expr do
+ clone.add(expr.clone)
+ end
+ return clone
+ end
+end
+
+redef class AQclassid
+ redef fun clone: SELF
+ do
+ return new AQclassid.init_aqclassid(n_qualified.clone, n_id)
+ end
+end
+
+redef class AQualified
+ redef fun clone: SELF
+ do
+ return new AQualified.init_aqualified(n_id.clone, n_classid)
+ end
+end
+
+redef class AQid
+ redef fun clone: SELF
+ do
+ var clone_n_qualified = n_qualified
+ if n_qualified != null then clone_n_qualified = n_qualified.clone
+ return new AQid.init_aqid(clone_n_qualified, n_id.clone)
+ end
+end
+
+redef class TId
+ redef fun clone: SELF
+ do
+ return new TId.init_tk(location)
+ end
+end
+
+redef class AParExpr
+ private init make(expr: AExpr, annotations: nullable AAnnotations)
+ do
+ self.init_aparexpr(new TOpar, expr, new TCpar, annotations)
+ 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 ANodes
+ super Cloneable
+
+ redef fun clone: SELF
+ do
+ var clone_anodes = new ANodes[E](self.parent)
+ for node in self do
+ clone_anodes.add(node.clone)
+ end
+ return clone_anodes
+ end
+end
+
+redef class ANode
+ super Cloneable
+
+ redef fun clone: SELF
+ do
+ # By default the clone abort to avoid surprises
+ print "The clone method is not implemented for the `{self.class_name}` class"
+ abort
+ end
+ # 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
+
+ private init make(n_args : ANodes[AExpr])
+ do
+ _n_visibility = new APublicVisibility
+ _n_args = n_args
+ end
+end