markdown: add a toolcontext attribute
[nit.git] / src / typing.nit
index fb169f8..3c11789 100644 (file)
 module typing
 
 import flow
-import modelbuilder
+import modelize_property
+import phase
+import local_var_init
+
+redef class ToolContext
+       var typing_phase: Phase = new TypingPhase(self, [flow_phase, modelize_property_phase, local_var_init_phase])
+end
+
+private class TypingPhase
+       super Phase
+       redef fun process_npropdef(npropdef) do npropdef.do_typing(toolcontext.modelbuilder)
+end
 
 private class TypeVisitor
        var modelbuilder:  ModelBuilder
@@ -68,7 +79,7 @@ private class TypeVisitor
                return res
        end
 
-       # Retrieve the signature of a MMethodDef resolved for a specific call.
+       # Retrieve the signature of a `MMethodDef` resolved for a specific call.
        # This method is an helper to symplify the query on the model.
        #
        # Note: `for_self` indicates if the reciever is self or not.
@@ -81,7 +92,7 @@ private class TypeVisitor
        # Check that `sub` is a subtype of `sup`.
        # If `sub` is not a valud suptype, then display an error on `node` an return null.
        # If `sub` is a safe subtype of `sup` then return `sub`.
-       # If `sun` is an insafe subtype (ie an imlicit cast is required), then return `sup`.
+       # If `sub` is an insafe subtype (ie an imlicit cast is required), then return `sup`.
        #
        # The point of the return type is to determinate the usable type on an expression:
        # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
@@ -109,7 +120,7 @@ private class TypeVisitor
        # Return the type of the expression
        # Display an error and return null if:
        #  * the type cannot be determined or
-       #  * `nexpr' is a statement
+       #  * `nexpr` is a statement
        fun visit_expr(nexpr: AExpr): nullable MType
        do
                nexpr.accept_typing(self)
@@ -127,11 +138,11 @@ private class TypeVisitor
                return null
        end
 
-       # Visit an expression and expect its static type is a least a `sup'
-       # Return the type of the expression
+       # Visit an expression and expect its static type is a least a `sup`
+       # Return the type of the expression or null if
        #  * the type cannot be determined or
-       #  * `nexpr' is a statement
-       #  * `nexpt' is not a `sup'
+       #  * `nexpr` is a statement or
+       #  * `nexpr` is not a `sup`
        fun visit_expr_subtype(nexpr: AExpr, sup: nullable MType): nullable MType
        do
                var sub = visit_expr(nexpr)
@@ -146,11 +157,11 @@ private class TypeVisitor
                return res
        end
 
-       # Visit an expression and expect its static type is a bool
-       # Return the type of the expression
+       # Visit an expression and expect its static type is a `Bool`
+       # Return the type of the expression or null if
        #  * the type cannot be determined or
-       #  * `nexpr' is a statement
-       #  * `nexpt' is not a `sup'
+       #  * `nexpr` is a statement or
+       #  * `nexpr` is not a `Bool`
        fun visit_expr_bool(nexpr: AExpr): nullable MType
        do
                return self.visit_expr_subtype(nexpr, self.type_bool(nexpr))
@@ -237,19 +248,33 @@ private class TypeVisitor
                end
 
                var propdefs = mproperty.lookup_definitions(self.mmodule, unsafe_type)
+               var mpropdef
                if propdefs.length == 0 then
                        self.modelbuilder.error(node, "Type error: no definition found for property {name} in {unsafe_type}")
                        return null
-               else if propdefs.length > 1 then
-                       self.modelbuilder.error(node, "Error: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
-                       return null
+               else if propdefs.length == 1 then
+                       mpropdef = propdefs.first
+               else
+                       self.modelbuilder.warning(node, "Warning: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
+                       mpropdef = mproperty.intro
                end
 
-               var mpropdef = propdefs.first
 
                var msignature = self.resolve_signature_for(mpropdef, recvtype, recv_is_self)
 
-               var callsite = new CallSite(node, recvtype, recv_is_self, mproperty, mpropdef, msignature)
+               var erasure_cast = false
+               var rettype = mpropdef.msignature.return_mtype
+               if not recv_is_self and rettype != null then
+                       if rettype isa MNullableType then rettype = rettype.mtype
+                       if rettype isa MParameterType then
+                               var erased_rettype = msignature.return_mtype
+                               assert erased_rettype != null
+                               #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
+                               erasure_cast = true
+                       end
+               end
+
+               var callsite = new CallSite(node, recvtype, recv_is_self, mproperty, mpropdef, msignature, erasure_cast)
                return callsite
        end
 
@@ -379,6 +404,9 @@ class CallSite
        # The resolved signature for the receiver
        var msignature: MSignature
 
+       # Is a implicit cast required on erasure typing policy?
+       var erasure_cast: Bool
+
        private fun check_signature(v: TypeVisitor, args: Array[AExpr]): Bool
        do
                return v.check_signature(self.node, args, self.mproperty.name, self.msignature)
@@ -506,10 +534,10 @@ redef class AExpr
        var mtype: nullable MType = null
 
        # Is the statement correctly typed?
-       # Used to distinguish errors and statements when `mtype' == null
+       # Used to distinguish errors and statements when `mtype == null`
        var is_typed: Bool = false
 
-       # If required, the following implicit cast ".as(XXX)"
+       # If required, the following implicit cast `.as(XXX)`
        # Such a cast may by required after evaluating the expression when
        # a unsafe operation is detected (silently accepted by the Nit language).
        # The attribute is computed by `check_subtype`
@@ -531,6 +559,13 @@ redef class ABlockExpr
                for e in self.n_expr do v.visit_stmt(e)
                self.is_typed = true
        end
+
+       # The type of a blockexpr is the one of the last expression (or null if empty)
+       redef fun mtype
+       do
+               if self.n_expr.is_empty then return null
+               return self.n_expr.last.mtype
+       end
 end
 
 redef class AVardeclExpr
@@ -558,11 +593,13 @@ redef class AVardeclExpr
                        end
                end
 
-               if mtype == null then
-                       mtype = v.get_mclass(self, "Object").mclass_type.as_nullable
+               var decltype = mtype
+               if mtype == null or mtype isa MNullType then
+                       decltype = v.get_mclass(self, "Object").mclass_type.as_nullable
+                       if mtype == null then mtype = decltype
                end
 
-               variable.declared_type = mtype
+               variable.declared_type = decltype
                v.set_variable(self, variable, mtype)
 
                #debug("var {variable}: {mtype}")
@@ -604,15 +641,18 @@ redef class AVarAssignExpr
 end
 
 redef class AReassignFormExpr
+       # @depreciated use `reassign_callsite`
+       fun reassign_property: nullable MMethodDef do return self.reassign_callsite.mpropdef
+
        # The method designed by the reassign operator.
-       var reassign_property: nullable MMethodDef = null
+       var reassign_callsite: nullable CallSite
 
        var read_type: nullable MType = null
 
-       # Determine the `reassign_property'
-       # `readtype' is the type of the reading of the left value.
-       # `writetype' is the type of the writing of the left value.
-       # (Because of ACallReassignExpr, both can be different.
+       # Determine the `reassign_property`
+       # `readtype` is the type of the reading of the left value.
+       # `writetype` is the type of the writing of the left value.
+       # (Because of `ACallReassignExpr`, both can be different.
        # Return the static type of the value to store.
        private fun resolve_reassignment(v: TypeVisitor, readtype, writetype: MType): nullable MType
        do
@@ -634,9 +674,7 @@ redef class AReassignFormExpr
 
                var callsite = v.get_method(self, readtype, reassign_name, false)
                if callsite == null then return null # Skip error
-               var mpropdef = callsite.mpropdef
-
-               self.reassign_property = mpropdef
+               self.reassign_callsite = callsite
 
                var msignature = callsite.msignature
                var rettype = msignature.return_mtype
@@ -1067,7 +1105,7 @@ end
 
 redef class AIsaExpr
        # The static type to cast to.
-       # (different from the static type of the expression that is Bool).
+       # (different from the static type of the expression that is `Bool`).
        var cast_type: nullable MType
        redef fun accept_typing(v)
        do
@@ -1131,8 +1169,11 @@ end
 ## MESSAGE SENDING AND PROPERTY
 
 redef class ASendExpr
+       # @depreciated: use `callsite`
+       fun mproperty: nullable MMethod do return callsite.mproperty
+
        # The property invoked by the send.
-       var mproperty: nullable MMethod
+       var callsite: nullable CallSite
 
        redef fun accept_typing(v)
        do
@@ -1147,8 +1188,7 @@ redef class ASendExpr
 
                var callsite = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
                if callsite == null then return
-               var mproperty = callsite.mproperty
-               self.mproperty = mproperty
+               self.callsite = callsite
                var msignature = callsite.msignature
 
                var args = compute_raw_arguments
@@ -1156,7 +1196,7 @@ redef class ASendExpr
 
                callsite.check_signature(v, args)
 
-               if mproperty.is_init then
+               if callsite.mproperty.is_init then
                        var vmpropdef = v.mpropdef
                        if not (vmpropdef isa MMethodDef and vmpropdef.mproperty.is_init) then
                                v.error(self, "Can call a init only in another init")
@@ -1300,8 +1340,11 @@ redef class ABraAssignExpr
 end
 
 redef class ASendReassignFormExpr
+       # @depreciated use `write_callsite`
+       fun write_mproperty: nullable MMethod do return write_callsite.mproperty
+
        # The property invoked for the writing
-       var write_mproperty: nullable MMethod = null
+       var write_callsite: nullable CallSite
 
        redef fun accept_typing(v)
        do
@@ -1318,8 +1361,7 @@ redef class ASendReassignFormExpr
                var callsite = v.get_method(self, recvtype, name, for_self)
 
                if callsite == null then return
-               var mproperty = callsite.mproperty
-               self.mproperty = mproperty
+               self.callsite = callsite
 
                var args = compute_raw_arguments
                self.raw_arguments = args
@@ -1334,8 +1376,7 @@ redef class ASendReassignFormExpr
 
                var wcallsite = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
                if wcallsite == null then return
-               var wmproperty = wcallsite.mproperty
-               self.write_mproperty = wmproperty
+               self.write_callsite = wcallsite
 
                var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
                if wtype == null then return
@@ -1390,10 +1431,8 @@ redef class ASuperExpr
                        end
                        v.error(self, "Error: No super method to call for {mproperty}.")
                        return
-               else if superprops.length > 1 then
-                       v.modelbuilder.warning(self, "Warning: NOT YET IMPLEMENTED: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
-                       return
                end
+               # FIXME: covariance of return type in linear extension?
                var superprop = superprops.first
                assert superprop isa MMethodDef
 
@@ -1403,6 +1442,7 @@ redef class ASuperExpr
                        v.check_signature(self, args, mproperty.name, msignature)
                end
                self.mtype = msignature.return_mtype
+               self.is_typed = true
        end
 
        private fun process_superinit(v: TypeVisitor)
@@ -1454,8 +1494,11 @@ end
 ####
 
 redef class ANewExpr
+       # @depreciated use `callsite`
+       fun mproperty: nullable MMethod do return self.callsite.mproperty
+
        # The constructor invoked by the new.
-       var mproperty: nullable MMethod
+       var callsite: nullable CallSite
 
        redef fun accept_typing(v)
        do
@@ -1483,7 +1526,7 @@ redef class ANewExpr
                var callsite = v.get_method(self, recvtype, name, false)
                if callsite == null then return
 
-               self.mproperty = callsite.mproperty
+               self.callsite = callsite
 
                if not callsite.mproperty.is_init_for(recvtype.mclass) then
                        v.error(self, "Error: {name} is not a constructor.")
@@ -1640,5 +1683,6 @@ redef class ADebugTypeExpr
                        var umtype = v.anchor_to(mtype)
                        v.modelbuilder.warning(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
                end
+               self.is_typed = true
        end
 end