model: remove a lot of casts to MGenericType
authorJean Privat <jean@pryen.org>
Tue, 6 Nov 2012 05:26:42 +0000 (00:26 -0500)
committerJean Privat <jean@pryen.org>
Tue, 6 Nov 2012 05:26:42 +0000 (00:26 -0500)
Since the generalization of MGenericType::arguments,
MGenericType do not introduce specific methods anymore.
Therefore, there is no point to require specific type or casts in clients.

This simplify the code since now a MClass, a MClassDef and a MClassType
can all be used to designate an entity, generic or not.

Signed-off-by: Jean Privat <jean@pryen.org>

src/global_compiler.nit
src/interpretor_type_test.nit
src/model/model.nit
src/modelbuilder.nit
src/naive_interpreter.nit
src/rapid_type_analysis.nit
src/typing.nit

index df8be17..58b9395 100644 (file)
@@ -516,7 +516,6 @@ redef class MClassType
                else if mclass.name == "NativeString" then
                        return "char*"
                else if mclass.name == "NativeArray" then
-                       assert self isa MGenericType
                        return "{self.arguments.first.ctype}*"
                else if mclass.kind == extern_kind then
                        return "void*"
@@ -1818,7 +1817,7 @@ redef class AInternMethPropdef
                        v.ret(v.new_expr("(char*)GC_MALLOC({arguments[1]})", ret.as(not null)))
                        return
                else if pname == "calloc_array" then
-                       var elttype = arguments.first.mtype.supertype_to(v.compiler.mainmodule,arguments.first.mtype.as(MClassType),v.get_class("ArrayCapable")).as(MGenericType).arguments.first
+                       var elttype = arguments.first.mtype.supertype_to(v.compiler.mainmodule,arguments.first.mtype.as(MClassType),v.get_class("ArrayCapable")).arguments.first
                        v.ret(v.new_expr("({elttype.ctype}*)GC_MALLOC({arguments[1]} * sizeof({elttype.ctype}))", ret.as(not null)))
                        return
                else if pname == "object_id" then
@@ -2281,7 +2280,7 @@ end
 redef class AArrayExpr
        redef fun expr(v)
        do
-               var mtype = self.mtype.as(MGenericType).arguments.first
+               var mtype = self.mtype.as(MClassType).arguments.first
                var array = new Array[RuntimeVariable]
                for nexpr in self.n_exprs.n_exprs do
                        var i = v.expr(nexpr, mtype)
index 4e14dec..b023130 100644 (file)
@@ -468,13 +468,13 @@ private abstract class Type
                                else if p.mtype.mclass == sup.mtype.mclass then
                                        # found the same class (ie. p.mclass = B[B#0] = sup.mclass)
                                        # compare formal types arguments
-                                       for i in [0..p.mtype.as(MGenericType).arguments.length[ do
+                                       for i in [0..p.mtype.arguments.length[ do
                                                # erase nullable annotation of p arg
-                                               var sarg = p.mtype.as(MGenericType).arguments[i]
+                                               var sarg = p.mtype.arguments[i]
                                                if sarg isa MNullableType then sarg = sarg.mtype
                                                var sft = typing.load_type(sarg.as(MClassType))
                                                # erase nullable annotation of super arg
-                                               var suparg = sup.mtype.as(MGenericType).arguments[i]
+                                               var suparg = sup.mtype.arguments[i]
                                                if suparg isa MNullableType then suparg = suparg.mtype
                                                var pft = typing.load_type(suparg.as(MClassType))
                                                if not sft.is_subtype_fallback(pft) then
index 46a8ff4..dbd16c7 100644 (file)
@@ -550,7 +550,6 @@ abstract class MType
                if not sup isa MGenericType then return true
                var sub2 = sub.supertype_to(mmodule, anchor, sup.mclass)
                assert sub2.mclass == sup.mclass
-               assert sub2 isa MGenericType
                for i in [0..sup.mclass.arity[ do
                        var sub_arg = sub2.arguments[i]
                        var sup_arg = sup.arguments[i]
@@ -969,7 +968,6 @@ class MParameterType
                        if t.mclass == goalclass then
                                # Yeah! c specialize goalclass with a "super `t'". So the question is what is the argument of f
                                # FIXME: Here, we stop on the first goal. Should we check others and detect inconsistencies?
-                               assert t isa MGenericType
                                var res = t.arguments[self.rank]
                                return res
                        end
@@ -993,7 +991,7 @@ class MParameterType
                if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
                if resolved_receiver isa MParameterType then
                        assert resolved_receiver.mclass == anchor.mclass
-                       resolved_receiver = anchor.as(MGenericType).arguments[resolved_receiver.rank]
+                       resolved_receiver = anchor.arguments[resolved_receiver.rank]
                        if resolved_receiver isa MNullableType then resolved_receiver = resolved_receiver.mtype
                end
                assert resolved_receiver isa MClassType else print "{class_name}: {self}/{mtype}/{anchor}? {resolved_receiver}"
@@ -1001,7 +999,6 @@ class MParameterType
                # Eh! The parameter is in the current class.
                # So we return the corresponding argument, no mater what!
                if resolved_receiver.mclass == self.mclass then
-                       assert resolved_receiver isa MGenericType
                        var res = resolved_receiver.arguments[self.rank]
                        #print "{class_name}: {self}/{mtype}/{anchor} -> direct {res}"
                        return res
index e2649de..1f0ff0e 100644 (file)
@@ -513,7 +513,7 @@ class ModelBuilder
                                        bounds.add(objectclass.mclass_type.as_nullable)
                                else
                                        # Inherit the bound
-                                       bounds.add(mclass.mclassdefs.first.bound_mtype.as(MGenericType).arguments[i])
+                                       bounds.add(mclass.mclassdefs.first.bound_mtype.arguments[i])
                                end
                        end
                end
@@ -773,7 +773,7 @@ class ModelBuilder
                        end
                        for i in [0..mclassdef.parameter_names.length[ do
                                if mclassdef.parameter_names[i] == name then
-                                       res = mclassdef.mclass.mclass_type.as(MGenericType).arguments[i]
+                                       res = mclassdef.mclass.mclass_type.arguments[i]
                                        if ntype.n_kwnullable != null then res = res.as_nullable
                                        return res
                                end
index 95a66a4..48b517d 100644 (file)
@@ -798,8 +798,9 @@ redef class AInternMethPropdef
                        end
                else if pname == "calloc_array" then
                        var recvtype = args.first.mtype.as(MClassType)
-                       var mtype: MType = recvtype.supertype_to(v.mainmodule, recvtype, v.mainmodule.get_primitive_class("ArrayCapable"))
-                       mtype = mtype.as(MGenericType).arguments.first
+                       var mtype: MType
+                       mtype = recvtype.supertype_to(v.mainmodule, recvtype, v.mainmodule.get_primitive_class("ArrayCapable"))
+                       mtype = mtype.arguments.first
                        var val = new Array[Instance].filled_with(v.null_instance, args[1].to_i)
                        return new PrimitiveInstance[Array[Instance]](v.mainmodule.get_primitive_class("NativeArray").get_mtype([mtype]), val)
                end
@@ -1304,7 +1305,7 @@ redef class AArrayExpr
                        if i == null then return null
                        val.add(i)
                end
-               var mtype = v.unanchor_type(self.mtype.as(not null)).as(MGenericType)
+               var mtype = v.unanchor_type(self.mtype.as(not null)).as(MClassType)
                var elttype = mtype.arguments.first
                return v.array_instance(val, elttype)
        end
index ed0c6fc..67bd3f6 100644 (file)
@@ -419,9 +419,9 @@ end
 redef class AArrayExpr
        redef fun accept_rapid_type_vistor(v)
        do
-               var mtype = self.mtype.as(not null)
+               var mtype = self.mtype.as(MClassType)
                v.add_type(mtype)
-               var native = v.get_class("NativeArray").get_mtype([mtype.as(MGenericType).arguments.first])
+               var native = v.get_class("NativeArray").get_mtype([mtype.arguments.first])
                v.add_type(native)
                var prop = v.get_method(mtype, "with_native")
                v.add_monomorphic_send(mtype, prop)
index 6687413..179597c 100644 (file)
@@ -722,7 +722,7 @@ redef class ALoopExpr
 end
 
 redef class AForExpr
-       var coltype: nullable MGenericType
+       var coltype: nullable MClassType
 
        private fun do_type_iterator(v: TypeVisitor, mtype: MType)
        do
@@ -732,7 +732,6 @@ redef class AForExpr
                var colcla = v.try_get_mclass(self, "Collection")
                if colcla != null and v.is_subtype(mtype, colcla.get_mtype([objcla.mclass_type.as_nullable])) then
                        var coltype = mtype.supertype_to(v.mmodule, v.anchor, colcla)
-                       assert coltype isa MGenericType
                        self.coltype = coltype
                        var variables =  self.variables
                        if variables.length != 1 then
@@ -746,7 +745,6 @@ redef class AForExpr
                var mapcla = v.try_get_mclass(self, "Map")
                if mapcla != null and v.is_subtype(mtype, mapcla.get_mtype([objcla.mclass_type.as_nullable, objcla.mclass_type.as_nullable])) then
                        var coltype = mtype.supertype_to(v.mmodule, v.anchor, mapcla)
-                       assert coltype isa MGenericType
                        self.coltype = coltype
                        var variables = self.variables
                        if variables.length != 2 then