+ _variable = v
+ _mtype = value.mtype
+ if _mtype != null then _is_typed = true
+ end
+end
+
+redef class ASignature
+
+ init make_from_msignature(msignature: MSignature)
+ do
+ var nparams = new Array[AParam]
+ for mparam in msignature.mparameters do
+ var variable = new Variable(mparam.name)
+ variable.declared_type = mparam.mtype
+ n_params.add(new AParam.make(variable, new AType.make(mparam.mtype)))
+ end
+ var return_type = null
+ if msignature.return_mtype != null then return_type = new AType.make(msignature.return_mtype)
+ init_asignature(null, nparams, null, return_type)
+ end
+
+ redef fun clone: SELF
+ do
+ var ntype = n_type
+ if ntype != null then ntype = n_type.clone
+ return new ASignature.init_asignature(null, n_params.clone, null, ntype)
+ end
+end
+
+redef class AParam
+
+ private init make(v: nullable Variable, t: nullable AType)
+ do
+ _n_id = new TId
+ if v != null then _n_id.text = v.name
+ _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 AFormaldef
+
+ private init make(mparameter: MParameterType, t: AType)
+ do
+ _n_id = new TClassid
+ _n_id.text = mparameter.name
+ _n_type = t
+ _mtype = mparameter
+ 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
+
+redef class MEntity
+ # Build a ANode from `self`
+ #
+ # Allows the creation of an AST node from a model entity.
+ fun create_ast_representation(astbuilder: nullable ASTBuilder): ANode is abstract
+end
+
+redef class MPropDef
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): APropdef is abstract
+end
+
+redef class MClassDef
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AStdClassdef do
+ if astbuilder == null then astbuilder = new ASTBuilder(mmodule)
+ var n_propdefs = new Array[APropdef]
+ for mpropdef in self.mpropdefs do
+ n_propdefs.add(mpropdef.create_ast_representation(astbuilder))
+ end
+ var n_formaldefs = new Array[AFormaldef]
+ for mparameter in self.mclass.mparameters do n_formaldefs.add(mparameter.create_ast_representation(astbuilder))
+
+ return astbuilder.make_class(self, visibility.create_ast_representation(astbuilder), n_formaldefs, null, n_propdefs, null)
+ end
+end
+
+redef class MAttributeDef
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AAttrPropdef do
+ if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
+ var ntype = null
+ if self.static_mtype != null then ntype = static_mtype.create_ast_representation(astbuilder)
+ return astbuilder.make_attribute("_" + self.name, ntype, self.visibility.create_ast_representation(astbuilder), null, null, self, null, null)
+ end
+end
+
+redef class MMethodDef
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AMethPropdef do
+ if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
+ var tk_redef = null
+ if self.mproperty.intro != self then tk_redef = new TKwredef
+ var n_signature = if self.msignature == null then new ASignature else self.msignature.create_ast_representation(astbuilder)
+ return astbuilder.make_method(visibility.create_ast_representation(astbuilder), tk_redef, self, n_signature)
+ end
+end
+
+redef class MVisibility
+ fun create_ast_representation(astbuilder: nullable ASTBuilder): AVisibility do
+ if self.to_s == "public" then
+ return new APublicVisibility
+ else if self.to_s == "private" then
+ return new APrivateVisibility
+ else if self.to_s == "protected" then
+ return new AProtectedVisibility
+ else
+ return new AIntrudeVisibility
+ end
+ end
+end
+
+redef class MSignature
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): ASignature do
+ var nparams = new Array[AParam]
+ for mparam in mparameters do nparams.add(mparam.create_ast_representation(astbuilder))
+ var return_type = null
+ if self.return_mtype != null then return_type = self.return_mtype.create_ast_representation(astbuilder)
+ return new ASignature.init_asignature(null, nparams, null, return_type)
+ end
+end
+
+redef class MParameter
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AParam do
+ var variable = new Variable(self.name)
+ variable.declared_type = self.mtype
+ return new AParam.make(variable, self.mtype.create_ast_representation(astbuilder))
+ end
+end
+
+redef class MParameterType
+ redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AFormaldef do
+ var n_type = super
+ return new AFormaldef.make(self, n_type)