Add default values for some primitive type attributes.
[nit.git] / src / syntax / syntax_base.nit
index d6ebe97..b5ac33b 100644 (file)
@@ -190,8 +190,8 @@ special MMMethSrcMethod
        end
 end
 
-# Local variable and method parameter
-class Variable
+# Local variables
+abstract class Variable
        # Name of the variable
        readable attr _name: Symbol 
 
@@ -203,15 +203,38 @@ class Variable
 
        redef meth to_s do return _name.to_s
 
+       meth kind: String is abstract
+
        init(n: Symbol, d: PNode)
        do
-               assert n != null
-               assert d != null
+               #assert n != null
+               #assert d != null
                _name = n
                _decl = d
        end
 end
 
+# Variable declared with 'var'
+class VarVariable
+special Variable
+       redef meth kind do return once "variable"
+       init(n: Symbol, d: PNode) do super
+end
+
+# Parameter of method (declared in signature)
+class ParamVariable
+special Variable
+       redef meth kind do return once "parameter"
+       init(n: Symbol, d: PNode) do super
+end
+
+# Automatic variable (like in the 'for' statement)
+class AutoVariable
+special Variable
+       redef meth kind do return once "automatic variable"
+       init(n: Symbol, d: PNode) do super
+end
+
 ###############################################################################
 
 # Visitor used during the syntax analysis
@@ -322,6 +345,26 @@ 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
 
 
        protected init(tc: ToolContext, module: MMSrcModule)
@@ -372,6 +415,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
@@ -384,7 +430,7 @@ redef class PParam
        meth position: Int is abstract
 
        # Associated local variable
-       meth variable: Variable is abstract 
+       meth variable: ParamVariable is abstract 
 end
 
 redef class PType
@@ -411,7 +457,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
@@ -527,12 +573,17 @@ end
 
 redef class AVardeclExpr
        # Assiociated local variable
-       readable writable attr _variable: Variable
+       readable writable attr _variable: VarVariable
 end
 
 redef class AForVardeclExpr
        # Associated automatic local variable
-       readable writable attr _variable: Variable
+       readable writable attr _variable: AutoVariable
+end
+
+redef class ASelfExpr
+       # Associated local variable
+       readable writable attr _variable: ParamVariable 
 end
 
 redef class AVarFormExpr