nitg-s: retrieve recursively all partial generic types
authorAlexandre Terrasa <alexandre@moz-code.org>
Sun, 9 Dec 2012 01:25:43 +0000 (20:25 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Sun, 9 Dec 2012 01:25:43 +0000 (20:25 -0500)
for new G[H[nullable T], Object] and T: X
nitg-s will now find :
G[H[nullable X], Object]
H[nullable X]
nullable X
Object
as type to generate.

H[nullable X] and nullable X are considered as partial type because they are only used as part of a generic livetype.

Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/separate_compiler.nit
tests/base_isa_vt_gen4.nit [new file with mode: 0644]
tests/sav/base_isa_vt_gen4.res [new file with mode: 0644]

index 06b95bc..55570b1 100644 (file)
@@ -93,6 +93,7 @@ class SeparateCompiler
        super GlobalCompiler # TODO better separation of concerns
 
        private var undead_types: Set[MType] = new HashSet[MType]
+       private var partial_types: Set[MType] = new HashSet[MType]
        protected var typeids: HashMap[MType, Int] protected writable = new HashMap[MType, Int]
 
        private var type_colors: Map[MType, Int] = typeids
@@ -266,34 +267,10 @@ class SeparateCompiler
                mtypes.add_all(self.runtime_type_analysis.live_cast_types)
                mtypes.add_all(self.undead_types)
 
-               self.undead_types.clear
                for mtype in mtypes do
-                       # add formal types arguments to mtypes
-                       if mtype isa MGenericType then
-                               for ft in mtype.arguments do
-                                       if ft.need_anchor then
-                                               print("Why do we need anchor here ?")
-                                               abort
-                                       end
-                                       self.undead_types.add(ft)
-                               end
-                       end
-                       var mclass_type: MClassType
-                       if mtype isa MNullableType then
-                               mclass_type = mtype.mtype.as(MClassType)
-                       else
-                               mclass_type = mtype.as(MClassType)
-                       end
-
-                       # add virtual types to mtypes
-                       for vt in self.vt_tables[mclass_type.mclass] do
-                               if vt != null then
-                                       var anchored = vt.bound.anchor_to(self.mainmodule, mclass_type)
-                                       self.undead_types.add(anchored)
-                               end
-                       end
+                       retieve_live_partial_types(mtype)
                end
-               mtypes.add_all(self.undead_types)
+               mtypes.add_all(self.partial_types)
 
                # set type unique id
                if modelbuilder.toolcontext.opt_phmod_typing.value or modelbuilder.toolcontext.opt_phand_typing.value then
@@ -379,6 +356,34 @@ class SeparateCompiler
                return mtypes
        end
 
+       fun retieve_live_partial_types(mtype: MType) do
+               # add formal types arguments to mtypes
+               if mtype isa MGenericType then
+                       for ft in mtype.arguments do
+                               if ft.need_anchor then
+                                       print("Why do we need anchor here ?")
+                                       abort
+                               end
+                               self.partial_types.add(ft)
+                               retieve_live_partial_types(ft)
+                       end
+               end
+               var mclass_type: MClassType
+               if mtype isa MNullableType then
+                       mclass_type = mtype.mtype.as(MClassType)
+               else
+                       mclass_type = mtype.as(MClassType)
+               end
+
+               # add virtual types to mtypes
+               for vt in self.vt_tables[mclass_type.mclass] do
+                       if vt != null then
+                               var anchored = vt.bound.anchor_to(self.mainmodule, mclass_type)
+                               self.partial_types.add(anchored)
+                       end
+               end
+       end
+
        # declare live generic types tables selection
        private fun compile_live_gentype_to_c(mclass: MClass) do
                if mclass.arity > 0 then
diff --git a/tests/base_isa_vt_gen4.nit b/tests/base_isa_vt_gen4.nit
new file mode 100644 (file)
index 0000000..c3edf4b
--- /dev/null
@@ -0,0 +1,26 @@
+import kernel
+
+class G[U,V]
+end
+
+class H[W]
+end
+
+class A
+       type T: X
+       fun foo: Object do return new G[H[nullable T], Object]
+end
+
+class B
+       super A
+       redef type T: Y
+end
+
+class X
+end
+class Y
+       super X
+end
+
+var b = new B
+var bt = b.foo
diff --git a/tests/sav/base_isa_vt_gen4.res b/tests/sav/base_isa_vt_gen4.res
new file mode 100644 (file)
index 0000000..e69de29