rta: do not enter in annotations
[nit.git] / src / typing.nit
index 8b91bab..c1720c1 100644 (file)
@@ -140,6 +140,9 @@ private class TypeVisitor
                if sup == null then return null # Forward error
 
                var res = check_subtype(nexpr, sub, sup)
+               if res != sub then
+                       nexpr.implicit_cast_to = res
+               end
                return res
        end
 
@@ -206,11 +209,15 @@ private class TypeVisitor
                return mclass.mclass_type
        end
 
-       fun get_method(node: ANode, recvtype: MType, name: String, recv_is_self: Bool): nullable MMethodDef
+       fun get_method(node: ANode, recvtype: MType, name: String, recv_is_self: Bool): nullable CallSite
        do
                var unsafe_type = self.anchor_to(recvtype)
 
                #debug("recv: {recvtype} (aka {unsafe_type})")
+               if recvtype isa MNullType then
+                       self.error(node, "Error: Method '{name}' call on 'null'.")
+                       return null
+               end
 
                var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
                if mproperty == null then
@@ -223,23 +230,41 @@ private class TypeVisitor
                        return null
                end
 
+               assert mproperty isa MMethod
                if mproperty.visibility == protected_visibility and not recv_is_self and self.mmodule.visibility_for(mproperty.intro_mclassdef.mmodule) < intrude_visibility then
                        self.modelbuilder.error(node, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}")
                        return null
                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 msignature = self.resolve_signature_for(mpropdef, recvtype, recv_is_self)
+
+               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 propdef = propdefs.first
-               assert propdef isa MMethodDef
-               return propdef
+               var callsite = new CallSite(node, recvtype, recv_is_self, mproperty, mpropdef, msignature, erasure_cast)
+               return callsite
        end
 
        # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
@@ -345,6 +370,38 @@ private class TypeVisitor
        end
 end
 
+# A specific method call site with its associated informations.
+class CallSite
+       # The assiciated node for location
+       var node: ANode
+
+       # The statis type of the receiver
+       var recv: MType
+
+       # Is the receiver self?
+       # If "for_self", virtual types of the signature are keeped
+       # If "not_for_self", virtual type are erased
+       var recv_is_self: Bool
+
+       # The designated method
+       var mproperty: MMethod
+
+       # The statically designated method definition
+       # The most specif one, it is.
+       var mpropdef: MMethodDef
+
+       # 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)
+       end
+end
+
 redef class Variable
        # The declared type of the variable
        var declared_type: nullable MType
@@ -469,6 +526,12 @@ redef class AExpr
        # Used to distinguish errors and statements when `mtype' == null
        var is_typed: Bool = false
 
+       # 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`
+       var implicit_cast_to: nullable MType = null
+
        # Return the variable read (if any)
        # Used to perform adaptive typing
        fun its_variable: nullable Variable do return null
@@ -512,11 +575,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}")
@@ -558,8 +623,11 @@ 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
 
@@ -586,13 +654,11 @@ redef class AReassignFormExpr
                        return null
                end
 
-               var mpropdef = v.get_method(self, readtype, reassign_name, false)
-               if mpropdef == null then return null # Skip error
-
-               self.reassign_property = mpropdef
-
-               var msignature = v.resolve_signature_for(mpropdef, readtype, false)
+               var callsite = v.get_method(self, readtype, reassign_name, false)
+               if callsite == null then return null # Skip error
+               self.reassign_callsite = callsite
 
+               var msignature = callsite.msignature
                var rettype = msignature.return_mtype
                assert msignature.arity == 1 and rettype != null
 
@@ -785,7 +851,7 @@ redef class AForExpr
                        self.method_iterator = itdef.mproperty
 
                        # get iterator type
-                       var ittype = v.resolve_signature_for(itdef, mtype, false).return_mtype
+                       var ittype = itdef.msignature.return_mtype
                        if ittype == null then
                                v.error(self, "Type Error: Expected method 'iterator' to return an Iterator type")
                                return
@@ -1085,8 +1151,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
@@ -1099,18 +1168,17 @@ redef class ASendExpr
                        return
                end
 
-               var mpropdef = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
-               if mpropdef == null then return
-               var mproperty = mpropdef.mproperty
-               self.mproperty = mproperty
-               var msignature = v.resolve_signature_for(mpropdef, recvtype, self.n_expr isa ASelfExpr)
+               var callsite = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
+               if callsite == null then return
+               self.callsite = callsite
+               var msignature = callsite.msignature
 
                var args = compute_raw_arguments
                self.raw_arguments = args
 
-               v.check_signature(self, args, name, msignature)
+               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")
@@ -1254,8 +1322,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
@@ -1269,36 +1340,32 @@ redef class ASendReassignFormExpr
                end
 
                var for_self = self.n_expr isa ASelfExpr
-               var mpropdef = v.get_method(self, recvtype, name, for_self)
+               var callsite = v.get_method(self, recvtype, name, for_self)
 
-               if mpropdef == null then return
-               var mproperty = mpropdef.mproperty
-               self.mproperty = mproperty
-               var msignature = v.resolve_signature_for(mpropdef, recvtype, for_self)
+               if callsite == null then return
+               self.callsite = callsite
 
                var args = compute_raw_arguments
                self.raw_arguments = args
 
-               v.check_signature(self, args, name, msignature)
+               callsite.check_signature(v, args)
 
-               var readtype = msignature.return_mtype
+               var readtype = callsite.msignature.return_mtype
                if readtype == null then
                        v.error(self, "Error: {name} is not a function")
                        return
                end
 
-               var wpropdef = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
-               if wpropdef == null then return
-               var wmproperty = wpropdef.mproperty
-               self.write_mproperty = wmproperty
-               var wmsignature = v.resolve_signature_for(wpropdef, recvtype, for_self)
+               var wcallsite = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
+               if wcallsite == null then return
+               self.write_callsite = wcallsite
 
-               var wtype = self.resolve_reassignment(v, readtype, wmsignature.mparameters.last.mtype)
+               var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
                if wtype == null then return
 
                args = args.to_a # duplicate so raw_arguments keeps only the getter args
                args.add(self.n_value)
-               v.check_signature(self, args, name + "=", wmsignature)
+               wcallsite.check_signature(v, args)
 
                self.is_typed = true
        end
@@ -1346,10 +1413,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
 
@@ -1410,8 +1475,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
@@ -1436,20 +1504,18 @@ redef class ANewExpr
                else
                        name = "init"
                end
-               var propdef = v.get_method(self, recvtype, name, false)
-               if propdef == null then return
+               var callsite = v.get_method(self, recvtype, name, false)
+               if callsite == null then return
 
-               self.mproperty = propdef.mproperty
+               self.callsite = callsite
 
-               if not propdef.mproperty.is_init_for(recvtype.mclass) then
+               if not callsite.mproperty.is_init_for(recvtype.mclass) then
                        v.error(self, "Error: {name} is not a constructor.")
                        return
                end
 
-               var msignature = v.resolve_signature_for(propdef, recvtype, false)
-
                var args = n_args.to_a
-               v.check_signature(self, args, name, msignature)
+               callsite.check_signature(v, args)
        end
 end