metamodel: MMLocalClass::init takes the MMModule
[nit.git] / src / syntax / syntax_base.nit
index 4e4bd01..259cf9a 100644 (file)
@@ -68,9 +68,9 @@ special MMConcreteClass
        # Concrete NIT source properties by name
        readable attr _src_local_properties: Map[Symbol, MMLocalProperty] 
 
-       init(n: Symbol, cla: PClassdef, a: Int)
+       init(mod: MMSrcModule, n: Symbol, cla: PClassdef, a: Int)
        do
-               super(n, a)
+               super(mod, n, a)
                _nodes = [cla]
                _src_local_properties = new HashMap[Symbol, MMLocalProperty]
        end
@@ -207,8 +207,8 @@ abstract class Variable
 
        init(n: Symbol, d: PNode)
        do
-               assert n != null
-               assert d != null
+               #assert n != null
+               #assert d != null
                _name = n
                _decl = d
        end
@@ -235,11 +235,33 @@ special Variable
        init(n: Symbol, d: PNode) do super
 end
 
+# False variable corresponding to closures declared in signatures
+# Lives in the same namespace than variables
+class ClosureVariable
+special Variable
+       redef meth kind do return once "closure"
+
+       # The signature of the closure
+       readable attr _closure: MMClosure
+
+       init(n: Symbol, d: PNode, c: MMClosure)
+       do
+               super(n, d)
+               _closure = c
+       end
+end
+
 ###############################################################################
 
 # Visitor used during the syntax analysis
 class AbsSyntaxVisitor
 special Visitor
+       # The root type Object
+       meth type_object: MMType
+       do
+               return _module.class_by_name(once ("Object".to_symbol)).get_type
+       end
+
        # The primitive type Bool
        meth type_bool: MMType
        do
@@ -345,7 +367,58 @@ special Visitor
                error(n, "Type error: expected {stype}, got {subtype}")
                return false
        end
+       
+       # Check that an expression has a static type and that 
+       # Display an error and return false if n is a statement
+       # Require that the static type of n is known
+       meth check_expr(n: PExpr): Bool
+       do
+               # FIXME: The tc.error_count is a workaround since currently there is no way
+               # to distingate statements from buggy expressions: both have a null stype
+               if tc.error_count == 0 and n.stype == null then
+                       error(n, "Type error: expected expression.")
+                       return false
+               end
+               return true
+       end
 
+       # Combine check_conform and check_expr
+       meth check_conform_expr(n: PExpr, stype: MMType): Bool
+       do
+               if check_expr(n) then return check_conform(n, n.stype, stype) else return false
+       end
+
+       # Check conformance between multiple expressions and a static type
+       # Conformance is granted if among them there is a most general type
+       # Return the most general type if a conformance is found
+       # Display an error and return null if no conformance is found
+       # @param stype is a possible additional type (without node)
+       # Examples:
+       #   Int, Int, Object => return Object
+       #   Int, Float => display error, return null
+       meth check_conform_multiexpr(stype: MMType, nodes: Collection[PExpr]): MMType
+       do
+               var node: PExpr = null # candidate node
+               for n in nodes do
+                       if not check_expr(n) then return null
+                       var ntype = n.stype
+                       if stype == null or (ntype != null and stype < ntype) then
+                               stype = ntype
+                               node = n
+                       end
+               end
+               for n in nodes do
+                       if not n.stype < stype then
+                               if node == null then
+                                       error(n, "Type error: no most general type. Got {n.stype} and {stype}.")
+                               else
+                                       error(n, "Type error: no most general type. Got {n.stype} and {stype} at {node.locate}.")
+                               end
+                               return null
+                       end
+               end
+               return stype
+       end
 
        protected init(tc: ToolContext, module: MMSrcModule)
        do
@@ -395,6 +468,9 @@ end
 redef class AMethPropdef
        # Associated method (MM entity)
        meth method: MMMethSrcMethod is abstract
+
+       # Associated 'self' variable
+       meth self_var: ParamVariable is abstract
 end
 
 redef class ATypePropdef
@@ -410,6 +486,11 @@ redef class PParam
        meth variable: ParamVariable is abstract 
 end
 
+redef class PClosureDecl
+       # Associated closure variable
+       meth variable: ClosureVariable is abstract
+end
+
 redef class PType
        # Retrieve the local class corresponding to the type.
        # Display an error and return null if there is no class
@@ -434,7 +515,7 @@ end
 
 redef class AType
        attr _stype_cache: MMType
-       attr _stype_cached: Bool
+       attr _stype_cached: Bool = false
 
        redef meth get_local_class(v)
        do
@@ -553,13 +634,30 @@ redef class AVardeclExpr
        readable writable attr _variable: VarVariable
 end
 
-redef class AForVardeclExpr
+redef class AForExpr
        # Associated automatic local variable
        readable writable attr _variable: AutoVariable
 end
 
+redef class ASelfExpr
+       # Associated local variable
+       readable writable attr _variable: ParamVariable 
+end
+
 redef class AVarFormExpr
        # Associated local variable
        readable writable attr _variable: Variable 
 end
 
+redef class AClosureCallExpr
+       # Associated closure variable
+       readable writable attr _variable: ClosureVariable
+end
+
+redef class PClosureDef
+       # Associated closure
+       readable writable attr _closure: MMClosure
+
+       # Automatic variables
+       readable writable attr _variables: Array[AutoVariable]
+end