Add default values for some primitive type attributes.
[nit.git] / src / syntax / syntax_base.nit
index 8a48e25..b5ac33b 100644 (file)
@@ -66,13 +66,13 @@ special MMConcreteClass
        readable writable attr _formal_dict: Map[Symbol, MMTypeFormalParameter]
 
        # Concrete NIT source properties by name
-       readable attr _src_local_properties: Map[Symbol, MMConcreteProperty] 
+       readable attr _src_local_properties: Map[Symbol, MMLocalProperty] 
 
        init(n: Symbol, cla: PClassdef, a: Int)
        do
                super(n, a)
                _nodes = [cla]
-               _src_local_properties = new HashMap[Symbol, MMConcreteProperty]
+               _src_local_properties = new HashMap[Symbol, MMLocalProperty]
        end
 end
 
@@ -99,26 +99,27 @@ redef class MMGlobalProperty
        end
 end
 
-redef class MMConcreteProperty
+redef class MMLocalProperty
        # The attached node (if any)
        meth node: PNode do return null
+
+       # Is the concrete method defined as init
+       meth is_init: Bool do return false
 end
 
 # Concrete NIT source attribute
 class MMSrcAttribute
-special MMConcreteProperty
 special MMAttribute
        redef readable attr _node: AAttrPropdef
        init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
        do
-               super(name, cla, self)
+               super(name, cla)
                _node = n
        end
 end
 
 # Concrete NIT source method
 class MMSrcMethod
-special MMConcreteProperty
 special MMMethod
 end
 
@@ -126,56 +127,71 @@ end
 class MMAttrImplementationMethod
 special MMSrcMethod
        redef readable attr _node: AAttrPropdef
+       init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
+       do
+               super(name, cla)
+               _node = n
+       end
 end
 
 # Concrete NIT source method for an automatic read accesor
 class MMReadImplementationMethod
 special MMAttrImplementationMethod
-
        init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
        do
-               super(name, cla, self)
-               _node = n
+               super(name, cla, n)
        end
 end
 
 # Concrete NIT source method for an automatic write accesor
 class MMWriteImplementationMethod
 special MMAttrImplementationMethod
-
        init(name: Symbol, cla: MMLocalClass, n: AAttrPropdef)
        do
-               super(name, cla, self)
-               _node = n
+               super(name, cla, n)
        end
 end
 
 # Concrete NIT source method for an explicit method
 class MMMethSrcMethod
 special MMSrcMethod
+       redef meth is_init do return _node isa AConcreteInitPropdef
        redef readable attr _node: AMethPropdef
        init(name: Symbol, cla: MMLocalClass, n: AMethPropdef)
        do
-               super(name, cla, self)
+               super(name, cla)
                _node = n
        end
 end
 
 # Concrete NIT source virtual type
 class MMSrcTypeProperty
-special MMConcreteProperty
+special MMLocalProperty
 special MMTypeProperty
        redef readable attr _node: ATypePropdef
        init(name: Symbol, cla: MMLocalClass, n: ATypePropdef)
        do
-               super(name, cla, self)
+               super(name, cla)
                _node = n
        end
 end
 
+# Concrete NIT implicit constructor
+class MMImplicitInit
+special MMMethSrcMethod
+       redef meth is_init do return true
+       readable attr _unassigned_attributes: Array[MMSrcAttribute]
+       readable attr _super_inits: Array[MMLocalProperty]
+       init(cla: MMLocalClass, unassigned_attributes: Array[MMSrcAttribute], super_inits: Array[MMLocalProperty])
+       do
+               super(once "init".to_symbol, cla, null)
+               _unassigned_attributes = unassigned_attributes
+               _super_inits = super_inits
+       end
+end
 
-# Local variable and method parameter
-class Variable
+# Local variables
+abstract class Variable
        # Name of the variable
        readable attr _name: Symbol 
 
@@ -187,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
@@ -268,7 +307,7 @@ special Visitor
        readable writable attr _local_class: MMSrcLocalClass 
 
        # The current property
-       readable writable attr _local_property: MMConcreteProperty
+       readable writable attr _local_property: MMLocalProperty
 
        # The current tool configuration/status
        readable attr _tc: ToolContext 
@@ -276,13 +315,20 @@ special Visitor
        # Display an error for a given syntax node
        meth error(n: PNode, s: String)
        do
-               _tc.error("{n.locate}: {s}")
+               _tc.error("{locate(n)}: {s}")
        end
 
        # Display a warning for a given syntax node
        meth warning(n: PNode, s: String)
        do
-               _tc.warning("{n.locate}: {s}")
+               _tc.warning("{locate(n)}: {s}")
+       end
+
+       #
+       meth locate(n: PNode): String
+       do
+               if n != null then return n.locate
+               return _module.filename
        end
 
        # Check conformity and display error
@@ -294,9 +340,31 @@ special Visitor
                if subtype < stype then
                        return true
                end
+               #error(n, "Type error: expected {stype}'{stype.module}, got {subtype}'{subtype.module}")
+               #abort
                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)
@@ -347,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
@@ -359,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
@@ -386,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
@@ -435,7 +506,7 @@ redef class AType
                                v.error(self, "Type error: formal type {name} cannot have formal parameters.")
                                return null
                        end
-                       var t = cla.get_type.select_virtual_type(name).stype
+                       var t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
                        if t == null then
                                v.error(self, "Type error: circular definition in formal type {name}.")
                                return null
@@ -502,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