compile: Check correct object construction
[nit.git] / src / compiling / compiling_global.nit
index 27f4b5e..5ff14cd 100644 (file)
@@ -54,46 +54,51 @@ end
 class GlobalAnalysis
 special ColorContext
        # Associate global classes to compiled classes
-       readable attr _compiled_classes: HashMap[MMGlobalClass, CompiledClass] 
+       readable attr _compiled_classes: HashMap[MMGlobalClass, CompiledClass] = new HashMap[MMGlobalClass, CompiledClass]
 
        # The main module of the program globally analysed
-       readable attr _module: MMModule 
+       readable attr _module: MMModule
 
        # FIXME: do something better.
        readable writable attr _max_class_table_length: Int
 
        init(module: MMSrcModule)
        do
-               _compiled_classes = new HashMap[MMGlobalClass, CompiledClass]
                _module = module
        end
 end
 
-redef class CompilerVisitor
-       # The global analysis result, if any
-       readable writable attr _global_analysis: GlobalAnalysis 
+class GlobalCompilerVisitor
+special CompilerVisitor
+       # The global analysis result
+       readable attr _global_analysis: GlobalAnalysis
+       init(m: MMSrcModule, tc: ToolContext, ga: GlobalAnalysis)
+       do
+               super(m, tc)
+               _global_analysis = ga
+       end
 end
 
 # A compiled class is a class in a program
 class CompiledClass
 special ColorContext
        # The corresponding local class in the main module of the prgram
-       readable attr _local_class: MMLocalClass 
+       readable attr _local_class: MMLocalClass
 
        # The identifier of the class
-       readable writable attr _id: Int 
+       readable writable attr _id: Int
 
        # The full class table of the class
-       readable writable attr _class_table: Array[TableElt] 
+       readable attr _class_table: Array[TableElt] = new Array[TableElt]
 
        # The full instance table of the class
-       readable writable attr _instance_table: Array[TableElt] 
+       readable attr _instance_table: Array[TableElt] = new Array[TableElt]
 
        # The proper class table part (no superclasses but all refinements)
-       readable writable attr _class_layout: TableEltComposite 
+       readable attr _class_layout: TableEltComposite = new TableEltComposite(self)
 
        # The proper instance table part (no superclasses but all refinements)
-       readable writable attr _instance_layout: TableEltComposite 
+       readable attr _instance_layout: TableEltComposite = new TableEltComposite(self)
 
        init(c: MMLocalClass) do _local_class = c
 end
@@ -103,18 +108,16 @@ redef class MMSrcLocalClass
        readable attr _class_color_pos: TableEltClassColor
 
        # The proper local class table part (nor superclasses nor refinments)
-       readable attr _class_layout: Array[TableElt] 
+       readable attr _class_layout: Array[TableElt] = new Array[TableElt]
 
        # The proper local instance table part (nor superclasses nor refinments)
-       readable attr _instance_layout: Array[TableElt] 
+       readable attr _instance_layout: Array[TableElt] = new Array[TableElt]
 
        # Build the local layout of the class and feed the module table
-       meth build_layout_in(tc: ToolContext, module_table: Array[LocalTableElt])
+       meth build_layout_in(tc: ToolContext, module_table: Array[ModuleTableElt])
        do
-               var clt = new Array[TableElt]
-               _class_layout = clt
-               var ilt = new Array[TableElt]
-               _instance_layout = ilt
+               var clt = _class_layout
+               var ilt = _instance_layout
 
                if global.intro == self then
                        module_table.add(new TableEltClassId(self))
@@ -137,13 +140,13 @@ redef class MMSrcLocalClass
                end
 
                if not ilt.is_empty then
-                       var teg = new LocalTableEltGroup
+                       var teg = new ModuleTableEltGroup
                        teg.elements.append(ilt)
                        module_table.add(teg)
                end
 
                if not clt.is_empty then
-                       var teg = new LocalTableEltGroup
+                       var teg = new ModuleTableEltGroup
                        teg.elements.append(clt)
                        module_table.add(teg)
                end
@@ -152,15 +155,13 @@ end
 
 redef class MMSrcModule
        # The local table of the module (refers things introduced in the module)
-       attr _local_table: Array[LocalTableElt]
+       attr _local_table: Array[ModuleTableElt] = new Array[ModuleTableElt]
 
        # Builds the local tables and local classes layouts
        meth local_analysis(tc: ToolContext)
        do
-               var lt = new Array[LocalTableElt]
-               _local_table = lt
                for c in src_local_classes do
-                       c.build_layout_in(tc, lt)
+                       c.build_layout_in(tc, _local_table)
                end
        end
 
@@ -176,7 +177,7 @@ redef class MMSrcModule
 
                ctab.add(new TableEltClassSelfId)
                itab.add(new TableEltVftPointer)
-               
+
                var pclassid = -1
                var classid = 3
 
@@ -222,7 +223,7 @@ redef class MMSrcModule
 
                # Compute core and crown classes for colorization
                var crown_classes = new HashSet[MMLocalClass]
-               var core_classes = new HashSet[MMLocalClass] 
+               var core_classes = new HashSet[MMLocalClass]
                for c in smallest_classes do
                        while c.cshe.direct_greaters.length == 1 do
                                c = c.cshe.direct_greaters.first
@@ -241,13 +242,14 @@ redef class MMSrcModule
                        var cc = ga.compiled_classes[c.global]
                        if core_classes.has(c) then
                                # For core classes, just build the table
-                               cc.class_table = build_tables(ga, c, ctab)
+                               build_tables_in(cc.class_table, ga, c, ctab)
                                if maxcolor < cc.class_table.length then maxcolor = cc.class_table.length
                        else
                                # For other classes, it's easier: just append to the parent tables
                                var sc = c.cshe.direct_greaters.first
                                var scc = ga.compiled_classes[sc.global]
-                               cc.class_table = scc.class_table.to_a
+                               assert cc.class_table.is_empty
+                               cc.class_table.add_all(scc.class_table)
                                var bc = c.global.intro
                                assert bc isa MMSrcLocalClass
                                var colpos = bc.class_color_pos
@@ -262,15 +264,13 @@ redef class MMSrcModule
                # Fill class table and instance tables pools
                for c in classes do
                        var cc = ga.compiled_classes[c.global]
-                       var cte = new TableEltComposite(cc)
-                       var ite = new TableEltComposite(cc)
+                       var cte = cc.class_layout
+                       var ite = cc.instance_layout
                        for sc in c.crhe.greaters_and_self do
                                if sc isa MMSrcLocalClass then
                                        cte.add(sc, sc.class_layout)
                                        ite.add(sc, sc.instance_layout)
                                end
-                               cc.class_layout = cte
-                               cc.instance_layout = ite
                        end
 
                        if core_classes.has(c) then
@@ -288,18 +288,19 @@ redef class MMSrcModule
                colorize(ga, itab, crown_classes, 0)
 
                # Build class and instance tables now things are colored
-               ga.max_class_table_length = 0 
+               ga.max_class_table_length = 0
                for c in classes do
                        var cc = ga.compiled_classes[c.global]
                        if core_classes.has(c) then
                                # For core classes, just build the table
-                               cc.class_table = build_tables(ga, c, ctab)
-                               cc.instance_table = build_tables(ga, c, itab)
+                               build_tables_in(cc.class_table, ga, c, ctab)
+                               build_tables_in(cc.instance_table, ga, c, itab)
                        else
                                # For other classes, it's easier: just append to the parent tables
                                var sc = c.cshe.direct_greaters.first
                                var scc = ga.compiled_classes[sc.global]
-                               cc.class_table = scc.class_table.to_a
+                               cc.class_table.clear
+                               cc.class_table.add_all(scc.class_table)
                                var bc = c.global.intro
                                assert bc isa MMSrcLocalClass
                                var colpos = bc.class_color_pos
@@ -308,7 +309,8 @@ redef class MMSrcModule
                                        cc.class_table.add(null)
                                end
                                append_to_table(ga, cc.class_table, cc.class_layout)
-                               cc.instance_table = scc.instance_table.to_a
+                               assert cc.instance_table.is_empty
+                               cc.instance_table.add_all(scc.instance_table)
                                append_to_table(ga, cc.instance_table, cc.instance_layout)
                        end
                end
@@ -325,7 +327,7 @@ redef class MMSrcModule
                end
        end
 
-       private meth build_tables(ga: GlobalAnalysis, c: MMLocalClass, elts: Array[TableElt]): Array[TableElt]
+       private meth build_tables_in(table: Array[TableElt], ga: GlobalAnalysis, c: MMLocalClass, elts: Array[TableElt])
        do
                var tab = new HashMap[Int, TableElt]
                var len = 0
@@ -339,21 +341,19 @@ redef class MMSrcModule
                                end
                        end
                end
-               var res = new Array[TableElt]
                var i = 0
                while i < len do
                        if tab.has_key(i) then
                                var e = tab[i]
                                for j in [0..e.length[ do
-                                       res[i] = e.item(j)
+                                       table[i] = e.item(j)
                                        i = i + 1
                                end
                        else
-                               res[i] = null
+                               table[i] = null
                                i = i + 1
                        end
                end
-               return res
        end
 
        # Perform coloring
@@ -377,7 +377,7 @@ redef class MMSrcModule
                                while trycolor != color do
                                        color = trycolor
                                        for c in rel_classes do
-                                               var idx = 0 
+                                               var idx = 0
                                                while idx < len do
                                                        if colors.has_key(trycolor + idx) and not free_color(colors[trycolor + idx], c) then
                                                                trycolor = trycolor + idx + 1
@@ -411,7 +411,7 @@ redef class MMSrcModule
        end
 
        # Compile module and class tables
-       meth compile_tables_to_c(v: CompilerVisitor)
+       meth compile_tables_to_c(v: GlobalCompilerVisitor)
        do
                for m in mhe.greaters_and_self do
                        assert m isa MMSrcModule
@@ -421,7 +421,7 @@ redef class MMSrcModule
                for c in local_classes do
                        c.compile_tables_to_c(v)
                end
-               var s = "classtable_t TAG2VFT[4] = \{NULL"
+               var s = new Buffer.from("classtable_t TAG2VFT[4] = \{NULL")
                for t in ["Int","Char","Bool"] do
                        if has_global_class_named(t.to_symbol) then
                                s.append(", (const classtable_t)VFT_{t}")
@@ -430,11 +430,11 @@ redef class MMSrcModule
                        end
                end
                s.append("};")
-               v.add_instr(s)
+               v.add_instr(s.to_s)
        end
 
        # Declare class table (for _sep.h)
-       meth declare_class_tables_to_c(v: CompilerVisitor)
+       meth declare_class_tables_to_c(v: GlobalCompilerVisitor)
        do
                for c in local_classes do
                        if c.global.module == self then
@@ -444,7 +444,7 @@ redef class MMSrcModule
        end
 
        # Compile main part (for _table.c)
-       meth compile_main_part(v: CompilerVisitor)
+       meth compile_main_part(v: GlobalCompilerVisitor)
        do
                v.add_instr("int main(int argc, char **argv) \{")
                v.indent
@@ -469,9 +469,9 @@ redef class MMSrcModule
                v.unindent
                v.add_instr("}")
        end
-       
-       # Compile sep files 
-       meth compile_mod_to_c(v: CompilerVisitor)
+
+       # Compile sep files
+       meth compile_mod_to_c(v: GlobalCompilerVisitor)
        do
                v.add_decl("extern const char *LOCATE_{name};")
                if not v.tc.global then
@@ -507,7 +507,7 @@ redef class MMSrcModule
        end
 
        # Compile module file for the current module
-       meth compile_local_table_to_c(v: CompilerVisitor)
+       meth compile_local_table_to_c(v: GlobalCompilerVisitor)
        do
                v.add_instr("const char *LOCATE_{name} = \"{filename}\";")
 
@@ -525,25 +525,44 @@ redef class MMSrcModule
        end
 end
 
+###############################################################################
+
+# An element of a class, an instance or a module table
+abstract class AbsTableElt
+       # Compile the macro needed to use the element and other related elements
+       meth compile_macros(v: GlobalCompilerVisitor, value: String) is abstract
+end
+
+# An element of a class or an instance table
+# Such an elements represent method function pointers, attribute values, etc.
 abstract class TableElt
+special AbsTableElt
+       # Is the element conflict to class `c' (used for coloring)
        meth is_related_to(c: MMLocalClass): Bool is abstract
+
+       # Number of sub-elements. 1 if none
        meth length: Int do return 1
+
+       # Access the ith subelement.
        meth item(i: Int): TableElt do return self
-       meth compile_macros(v: CompilerVisitor, value: String) is abstract
-       meth compile_to_c(v: CompilerVisitor, c: MMLocalClass): String is abstract
+
+       # Return the value of the element for a given class
+       meth compile_to_c(v: GlobalCompilerVisitor, c: MMLocalClass): String is abstract
 end
 
-abstract class LocalTableElt
-special TableElt
+# An element of a module table
+# Such an elements represent colors or identifiers
+abstract class ModuleTableElt
+special AbsTableElt
+       # Return the value of the element once the global analisys is performed
        meth value(ga: GlobalAnalysis): String is abstract
 end
 
-class LocalTableEltGroup
-special LocalTableElt
+# An element of a module table that represents a group of TableElt defined in the same local class
+class ModuleTableEltGroup
+special ModuleTableElt
        readable attr _elements: Array[TableElt] = new Array[TableElt]
 
-       redef meth length do return _elements.length
-       redef meth item(i) do return _elements[i]
        redef meth value(ga) do return "{ga.color(_elements.first)} /* Group of ? */"
        redef meth compile_macros(v, value)
        do
@@ -555,6 +574,7 @@ special LocalTableElt
        end
 end
 
+# An element that represents a class property
 abstract class TableEltProp
 special TableElt
        attr _property: MMLocalProperty
@@ -565,6 +585,7 @@ special TableElt
        end
 end
 
+# An element that represents a function pointer to a global method
 class TableEltMeth
 special TableEltProp
        redef meth compile_macros(v, value)
@@ -578,9 +599,9 @@ special TableEltProp
                var p = c[_property.global]
                return p.cname
        end
-       init(p) do super 
 end
 
+# An element that represents a function pointer to the super method of a local method
 class TableEltSuper
 special TableEltProp
        redef meth compile_macros(v, value)
@@ -600,20 +621,18 @@ special TableEltProp
                        if s == pc then
                                found = true
                        else if found and c.che < s then
-                               var p = s[g]
-                               if p != null then
+                               if s.has_global_property(g) then
                                        #print "found {s.module}::{s}::{p}"
-                                       return p.cname
+                                       return s[g].cname
                                end
                        end
                end
                assert false
                return null
        end
-
-       init(p) do super 
 end
 
+# An element that represents the value stored for a global attribute
 class TableEltAttr
 special TableEltProp
        redef meth compile_macros(v, value)
@@ -628,24 +647,20 @@ special TableEltProp
                var p = c[_property.global]
                return "/* {ga.color(self)}: Attribute {c}::{p} */"
        end
-
-       init(p) do super 
 end
 
-class TableEltClass
-special LocalTableElt
+# An element representing a class information
+class AbsTableEltClass
+special AbsTableElt
+       # The local class where the information comes from
        attr _local_class: MMLocalClass
-       redef meth is_related_to(c)
-       do
-               var bc = c.module[_local_class.global]
-               return c.cshe <= bc
-       end
 
        init(c: MMLocalClass)
        do
                _local_class = c
        end
 
+       # The C macro name refering the value
        meth symbol: String is abstract
 
        redef meth compile_macros(v, value)
@@ -654,27 +669,34 @@ special LocalTableElt
        end
 end
 
+# An element of a class table representing a class information
+class TableEltClass
+special TableElt
+special AbsTableEltClass
+       redef meth is_related_to(c)
+       do
+               var bc = c.module[_local_class.global]
+               return c.cshe <= bc
+       end
+end
+
+# An element representing the id of a class in a module table
 class TableEltClassId
-special TableEltClass
+special ModuleTableElt
+special AbsTableEltClass
        redef meth symbol do return _local_class.global.id_id
 
        redef meth value(ga)
        do
                return "{ga.compiled_classes[_local_class.global].id} /* Id of {_local_class} */"
        end
-
-       init(c) do super
 end
 
+# An element representing the constructor marker position in a class table
 class TableEltClassInitTable
 special TableEltClass
        redef meth symbol do return _local_class.global.init_table_pos_id
 
-       redef meth value(ga)
-       do
-               return "{ga.color(self)} /* Color of {_local_class} */"
-       end
-
        redef meth compile_to_c(v, c)
        do
                var ga = v.global_analysis
@@ -686,28 +708,31 @@ special TableEltClass
                end
                return "{i} /* {ga.color(self)}: {c} < {cc.local_class}: superclass init_table position */"
        end
-
-       init(c) do super
 end
 
+# An element used for a cast
+# Note: this element is both a TableElt and a ModuleTableElt.
+# At the TableElt offset, there is the id of the super-class
+# At the ModuleTableElt offset, there is the TableElt offset (ie. the color of the super-class).
 class TableEltClassColor
 special TableEltClass
+special ModuleTableElt
        redef meth symbol do return _local_class.global.color_id
 
        redef meth value(ga)
        do
                return "{ga.color(self)} /* Color of {_local_class} */"
        end
+
        redef meth compile_to_c(v, c)
        do
                var ga = v.global_analysis
                var cc = ga.compiled_classes[_local_class.global]
                return "{cc.id} /* {ga.color(self)}: {c} < {cc.local_class}: superclass typecheck marker */"
        end
-
-       init(c) do super
 end
 
+# A Group of elements introduced in the same global-class that are colored together
 class TableEltComposite
 special TableElt
        attr _table: Array[TableElt]
@@ -734,6 +759,7 @@ special TableElt
        end
 end
 
+# The element that represent the class id
 class TableEltClassSelfId
 special TableElt
        redef meth is_related_to(c) do return true
@@ -742,10 +768,9 @@ special TableElt
                var ga = v.global_analysis
                return "{v.global_analysis.compiled_classes[c.global].id} /* {ga.color(self)}: Identity */"
        end
-
-       init do end
 end
 
+# The element that
 class TableEltVftPointer
 special TableElt
        redef meth is_related_to(c) do return true
@@ -754,10 +779,10 @@ special TableElt
                var ga = v.global_analysis
                return "/* {ga.color(self)}: Pointer to the classtable */"
        end
-
-       init do end
 end
 
+###############################################################################
+
 # Used to sort local class in a deterministic total order
 # The total order superset the class refinement and the class specialisation relations
 class ClassSorter
@@ -790,7 +815,7 @@ redef class MMLocalClass
        end
 
        # Declaration and macros related to the class table
-       meth declare_tables_to_c(v: CompilerVisitor)
+       meth declare_tables_to_c(v: GlobalCompilerVisitor)
        do
                v.add_decl("")
                var pi = primitive_info
@@ -807,10 +832,10 @@ redef class MMLocalClass
        end
 
        # Compilation of table and new (or box)
-       meth compile_tables_to_c(v: CompilerVisitor)
+       meth compile_tables_to_c(v: GlobalCompilerVisitor)
        do
                var cc = v.global_analysis.compiled_classes[self.global]
-               var ctab =  cc.class_table
+               var ctab = cc.class_table
                var clen = ctab.length
                if v.global_analysis.max_class_table_length > ctab.length then
                        clen = v.global_analysis.max_class_table_length
@@ -886,6 +911,24 @@ redef class MMLocalClass
                        v.unindent
                        v.add_instr("}")
 
+                       # Compile CHECKNAME
+                       var s = "void CHECKNEW_{name}(val_t self, char *from)"
+                       v.add_instr(s + " \{")
+                       v.indent
+                       var ctx_old = v.ctx
+                       v.ctx = new CContext
+                       for g in global_properties do
+                               var p = self[g]
+                               var t = p.signature.return_type
+                               if p isa MMAttribute and t != null and not t.is_nullable and v.tc.opt_warn.value > 0 then
+                                       v.add_instr("if ({p.global.attr_access}(self) == NIT_NULL) fprintf(stderr, \"Uninitialized attribute %s at %s.\\n\", \"{p.full_name}\", from);")
+                               end
+                       end
+                       ctx_old.append(v.ctx)
+                       v.ctx = ctx_old
+                       v.unindent
+                       v.add_instr("}")
+
                        var init_table_size = cshe.greaters.length + 1
                        var init_table_decl = "int init_table[{init_table_size}] = \{0{", 0" * (init_table_size-1)}};"
 
@@ -906,6 +949,7 @@ redef class MMLocalClass
                                v.add_instr(init_table_decl)
                                v.add_instr("val_t self = NEW_{name}();")
                                v.add_instr("{p.cname}({args.join(", ")});")
+                               v.add_instr("CHECKNEW_{name}(self, \"{p.full_name} for {self}\");")
                                v.add_instr("return self;")
                                v.unindent
                                v.add_instr("}")