src: use `as_notnullable` in code
[nit.git] / src / separate_compiler.nit
index f7b8ee8..273451f 100644 (file)
@@ -105,6 +105,7 @@ redef class ModelBuilder
 
                # The main function of the C
                compiler.new_file("{mainmodule.name}.main")
+               compiler.compile_nitni_global_ref_functions
                compiler.compile_main_function
 
                # compile methods
@@ -263,8 +264,8 @@ class SeparateCompiler
                var rta = runtime_type_analysis
 
                # Layouts
-               var mclasses = new HashSet[MClass].from(modelbuilder.model.mclasses)
                var poset = mainmodule.flatten_mclass_hierarchy
+               var mclasses = new HashSet[MClass].from(poset)
                var colorer = new POSetColorer[MClass]
                colorer.colorize(poset)
 
@@ -345,45 +346,12 @@ class SeparateCompiler
                var tables = new HashMap[MClass, Array[nullable MPropDef]]
                for mclass in mclasses do
                        var table = new Array[nullable MPropDef]
-                       var supercalls = new List[MMethodDef]
+                       tables[mclass] = table
 
-                       # first, fill table from parents by reverse linearization order
-                       var parents = new Array[MClass]
-                       if mainmodule.flatten_mclass_hierarchy.has(mclass) then
-                               parents = mclass.in_hierarchy(mainmodule).greaters.to_a
-                               self.mainmodule.linearize_mclasses(parents)
-                       end
+                       var mproperties = self.mainmodule.properties(mclass)
+                       var mtype = mclass.intro.bound_mtype
 
-                       for parent in parents do
-                               if parent == mclass then continue
-                               for mproperty in self.mainmodule.properties(parent) do
-                                       if not mproperty isa MMethod then continue
-                                       if not method_colors.has_key(mproperty) then continue
-                                       var color = method_colors[mproperty]
-                                       if table.length <= color then
-                                               for i in [table.length .. color[ do
-                                                       table[i] = null
-                                               end
-                                       end
-                                       for mpropdef in mproperty.mpropdefs do
-                                               if mpropdef.mclassdef.mclass == parent then
-                                                       table[color] = mpropdef
-                                               end
-                                       end
-                               end
-
-                               # lookup for super calls in super classes
-                               for mmethoddef in super_calls do
-                                       for mclassdef in parent.mclassdefs do
-                                               if mclassdef.mpropdefs.has(mmethoddef) then
-                                                       supercalls.add(mmethoddef)
-                                               end
-                                       end
-                               end
-                       end
-
-                       # then override with local properties
-                       for mproperty in self.mainmodule.properties(mclass) do
+                       for mproperty in mproperties do
                                if not mproperty isa MMethod then continue
                                if not method_colors.has_key(mproperty) then continue
                                var color = method_colors[mproperty]
@@ -392,33 +360,22 @@ class SeparateCompiler
                                                table[i] = null
                                        end
                                end
-                               for mpropdef in mproperty.mpropdefs do
-                                       if mpropdef.mclassdef.mclass == mclass then
-                                               table[color] = mpropdef
-                                       end
-                               end
+                               table[color] = mproperty.lookup_first_definition(mainmodule, mtype)
                        end
 
-                       # lookup for super calls in local class
-                       for mmethoddef in super_calls do
-                               for mclassdef in mclass.mclassdefs do
-                                       if mclassdef.mpropdefs.has(mmethoddef) then
-                                               supercalls.add(mmethoddef)
-                                       end
-                               end
-                       end
-                       # insert super calls in table according to receiver
-                       for supercall in supercalls do
+                       for supercall in super_calls do
+                               if not mtype.collect_mclassdefs(mainmodule).has(supercall.mclassdef) then continue
+
                                var color = method_colors[supercall]
                                if table.length <= color then
                                        for i in [table.length .. color[ do
                                                table[i] = null
                                        end
                                end
-                               var mmethoddef = supercall.lookup_next_definition(self.mainmodule, mclass.intro.bound_mtype)
+                               var mmethoddef = supercall.lookup_next_definition(mainmodule, mtype)
                                table[color] = mmethoddef
                        end
-                       tables[mclass] = table
+
                end
                return tables
        end
@@ -427,46 +384,22 @@ class SeparateCompiler
                var tables = new HashMap[MClass, Array[nullable MPropDef]]
                for mclass in mclasses do
                        var table = new Array[nullable MPropDef]
-                       # first, fill table from parents by reverse linearization order
-                       var parents = new Array[MClass]
-                       if mainmodule.flatten_mclass_hierarchy.has(mclass) then
-                               parents = mclass.in_hierarchy(mainmodule).greaters.to_a
-                               self.mainmodule.linearize_mclasses(parents)
-                       end
-                       for parent in parents do
-                               if parent == mclass then continue
-                               for mproperty in self.mainmodule.properties(parent) do
-                                       if not mproperty isa MAttribute then continue
-                                       var color = attr_colors[mproperty]
-                                       if table.length <= color then
-                                               for i in [table.length .. color[ do
-                                                       table[i] = null
-                                               end
-                                       end
-                                       for mpropdef in mproperty.mpropdefs do
-                                               if mpropdef.mclassdef.mclass == parent then
-                                                       table[color] = mpropdef
-                                               end
-                                       end
-                               end
-                       end
+                       tables[mclass] = table
 
-                       # then override with local properties
-                       for mproperty in self.mainmodule.properties(mclass) do
+                       var mproperties = self.mainmodule.properties(mclass)
+                       var mtype = mclass.intro.bound_mtype
+
+                       for mproperty in mproperties do
                                if not mproperty isa MAttribute then continue
+                               if not attr_colors.has_key(mproperty) then continue
                                var color = attr_colors[mproperty]
                                if table.length <= color then
                                        for i in [table.length .. color[ do
                                                table[i] = null
                                        end
                                end
-                               for mpropdef in mproperty.mpropdefs do
-                                       if mpropdef.mclassdef.mclass == mclass then
-                                               table[color] = mpropdef
-                                       end
-                               end
+                               table[color] = mproperty.lookup_first_definition(mainmodule, mtype)
                        end
-                       tables[mclass] = table
                end
                return tables
        end
@@ -657,8 +590,7 @@ class SeparateCompiler
 
                # resolution table (for receiver)
                if is_live then
-                       var mclass_type = mtype
-                       if mclass_type isa MNullableType then mclass_type = mclass_type.mtype
+                       var mclass_type = mtype.as_notnullable
                        assert mclass_type isa MClassType
                        if resolution_tables[mclass_type].is_empty then
                                v.add_decl("NULL, /*NO RESOLUTIONS*/")
@@ -691,12 +623,7 @@ class SeparateCompiler
 
        fun compile_type_resolution_table(mtype: MType) do
 
-               var mclass_type: MClassType
-               if mtype isa MNullableType then
-                       mclass_type = mtype.mtype.as(MClassType)
-               else
-                       mclass_type = mtype.as(MClassType)
-               end
+               var mclass_type = mtype.as_notnullable.as(MClassType)
 
                # extern const struct resolution_table_X resolution_table_X
                self.provide_declaration("resolution_table_{mtype.c_name}", "extern const struct types resolution_table_{mtype.c_name};")
@@ -938,9 +865,17 @@ class SeparateCompiler
 
        redef fun compile_nitni_structs
        do
-               self.header.add_decl("struct nitni_instance \{struct instance *value;\};")
+               self.header.add_decl """
+struct nitni_instance \{
+       struct nitni_instance *next,
+               *prev; /* adjacent global references in global list */
+       int count; /* number of time this global reference has been marked */
+       struct instance *value;
+\};
+"""
+               super
        end
-       
+
        redef fun finalize_ffi_for_module(mmodule)
        do
                var old_module = self.mainmodule
@@ -1644,8 +1579,7 @@ class SeparateCompilerVisitor
 
        fun can_be_primitive(value: RuntimeVariable): Bool
        do
-               var t = value.mcasttype
-               if t isa MNullableType then t = t.mtype
+               var t = value.mcasttype.as_notnullable
                if not t isa MClassType then return false
                var k = t.mclass.kind
                return k == interface_kind or t.ctype != "val*"