typing: add `ARangeExpr::init_callsite` and use it everywhere
[nit.git] / src / typing.nit
index 2aabcad..c001f3a 100644 (file)
@@ -524,7 +524,7 @@ end
 
 redef class AExpr
        # The static type of the expression.
-       # null if self is a statement of in case of error
+       # null if self is a statement or in case of error
        var mtype: nullable MType = null
 
        # Is the statement correctly typed?
@@ -810,11 +810,11 @@ end
 redef class AForExpr
        var coltype: nullable MClassType
 
-       var method_iterator: nullable MMethod
-       var method_is_ok: nullable MMethod
-       var method_item: nullable MMethod
-       var method_next: nullable MMethod
-       var method_key: nullable MMethod
+       var method_iterator: nullable CallSite
+       var method_is_ok: nullable CallSite
+       var method_item: nullable CallSite
+       var method_next: nullable CallSite
+       var method_key: nullable CallSite
 
        private fun do_type_iterator(v: TypeVisitor, mtype: MType)
        do
@@ -828,22 +828,12 @@ redef class AForExpr
                if objcla == null then return
 
                # check iterator method
-               var unsafe_type = v.anchor_to(mtype)
-               if v.try_get_mproperty_by_name2(self, unsafe_type, "iterator") == null then
-                       if v.try_get_mproperty_by_name2(self, unsafe_type, "iterate") == null then
-                               v.error(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
-                       else
-                               v.modelbuilder.error(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
-                       end
-                       return
-               end
-
                var itdef = v.get_method(self, mtype, "iterator", true)
                if itdef == null then
                        v.error(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
                        return
                end
-               self.method_iterator = itdef.mproperty
+               self.method_iterator = itdef
 
                # check that iterator return something
                var ittype = itdef.msignature.return_mtype
@@ -900,21 +890,21 @@ redef class AForExpr
                        v.error(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.")
                        return
                end
-               self.method_is_ok = ikdef.mproperty
+               self.method_is_ok = ikdef
 
                var itemdef = v.get_method(self, ittype, "item", false)
                if itemdef == null then
                        v.error(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.")
                        return
                end
-               self.method_item = itemdef.mproperty
+               self.method_item = itemdef
 
                var nextdef = v.get_method(self, ittype, "next", false)
                if nextdef == null then
                        v.error(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.")
                        return
                end
-               self.method_next = nextdef.mproperty
+               self.method_next = nextdef
 
                if is_map then
                        var keydef = v.get_method(self, ittype, "key", false)
@@ -922,7 +912,7 @@ redef class AForExpr
                                v.error(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.")
                                return
                        end
-                       self.method_key = keydef.mproperty
+                       self.method_key = keydef
                end
        end
 
@@ -1095,6 +1085,8 @@ redef class AArrayExpr
 end
 
 redef class ARangeExpr
+       var init_callsite: nullable CallSite
+
        redef fun accept_typing(v)
        do
                var discrete_class = v.get_mclass(self, "Discrete")
@@ -1105,13 +1097,28 @@ redef class ARangeExpr
                if t1 == null or t2 == null then return
                var mclass = v.get_mclass(self, "Range")
                if mclass == null then return # Forward error
+               var mtype
                if v.is_subtype(t1, t2) then
-                       self.mtype = mclass.get_mtype([t2])
+                       mtype = mclass.get_mtype([t2])
                else if v.is_subtype(t2, t1) then
-                       self.mtype = mclass.get_mtype([t1])
+                       mtype = mclass.get_mtype([t1])
                else
                        v.error(self, "Type Error: Cannot create range: {t1} vs {t2}")
+                       return
                end
+
+               self.mtype = mtype
+
+               # get the constructor
+               var callsite
+               if self isa ACrangeExpr then
+                       callsite = v.get_method(self, mtype, "init", false)
+               else if self isa AOrangeExpr then
+                       callsite = v.get_method(self, mtype, "without_last", false)
+               else
+                       abort
+               end
+               init_callsite = callsite
        end
 end
 
@@ -1420,6 +1427,10 @@ redef class ASuperExpr
        # Note: if the super is a normal call-next-method, then this attribute is null
        var callsite: nullable CallSite
 
+       # The method to call is the super is a standard `call-next-method` super-call
+       # Note: if the super is a special super-init-call, then this attribute is null
+       var mpropdef: nullable MMethodDef
+
        redef fun accept_typing(v)
        do
                var recvtype = v.nclassdef.mclassdef.bound_mtype
@@ -1448,6 +1459,7 @@ redef class ASuperExpr
                self.mtype = msignature.return_mtype
                self.is_typed = true
                v.mpropdef.has_supercall = true
+               mpropdef = v.mpropdef.as(MMethodDef)
        end
 
        private fun process_superinit(v: TypeVisitor)