Remove now useless variables and attributes in compile_global
[nit.git] / src / compiling / compiling_global.nit
index bf5c74b..bbce5fa 100644 (file)
@@ -23,7 +23,7 @@ private import syntax
 
 # Something that store color of table elements
 class ColorContext
-       attr _colors: HashMap[TableElt, Int]
+       attr _colors: HashMap[TableElt, Int] = null
 
        # The color of a table element.
        meth color(e: TableElt): Int
@@ -98,18 +98,9 @@ special ColorContext
        init(c: MMLocalClass) do _local_class = c
 end
 
-redef class MMGlobalProperty
-       # The position of the property in the local class table part
-       # FIXME: It's ugly. store this somewhere else please
-       readable writable attr _pos_of: Int 
-end
-
 redef class MMSrcLocalClass
-       # The table element of the attribute position (for accessor simulation)
-       readable attr _base_attr_pos: TableEltBaseAttrPos 
-
        # The table element of the subtype check
-       readable attr _class_color_pos: TableEltClassColorPos
+       readable attr _class_color_pos: TableEltClassColor
 
        # The proper local class table part (nor superclasses nor refinments)
        readable attr _class_layout: Array[LocalTableElt] 
@@ -120,43 +111,31 @@ redef class MMSrcLocalClass
        # Build the local layout of the class and feed the module table
        meth build_layout_in(tc: ToolContext, module_table: Array[LocalTableElt])
        do
-               var intro_methods = new Array[MMGlobalProperty] # FIXME: Remove this
-               var intro_attributes = new Array[MMGlobalProperty] # FIXME: Remove this
                var clt = new Array[LocalTableElt]
                _class_layout = clt
                var ilt = new Array[LocalTableElt]
                _instance_layout = ilt
 
                if global.intro == self then
-                       module_table.add(new TableEltClassIdPos(self))
-                       _class_color_pos = new TableEltClassColorPos(self)
+                       module_table.add(new TableEltClassId(self))
+                       _class_color_pos = new TableEltClassColor(self)
                        module_table.add(_class_color_pos)
-                       #clt.add(_class_color_pos)
-                       clt.add(new TableEltClassInitTablePos(self))
+                       clt.add(new TableEltClassInitTable(self))
                end
                for p in src_local_properties do
                        var pg = p.global
                        if pg.intro == p then
                                if p isa MMSrcAttribute then
-                                       pg.pos_of = intro_attributes.length
-                                       intro_attributes.add(pg)
-                                       ilt.add(new TableEltAttrPos(p))
+                                       ilt.add(new TableEltAttr(p))
                                else if p isa MMSrcMethod then
-                                       pg.pos_of = intro_methods.length
-                                       intro_methods.add(pg)
-                                       clt.add(new TableEltMethPos(p))
+                                       clt.add(new TableEltMeth(p))
                                end
                        end
                        if p isa MMSrcMethod and p.need_super then
-                               clt.add(new TableEltSuperPos(p))
+                               clt.add(new TableEltSuper(p))
                        end
                end
-               if tc.attr_sim and not intro_attributes.is_empty then
-                       _base_attr_pos = new TableEltBaseAttrPos(self)
-                       clt.add(_base_attr_pos)
-               else
-                       module_table.append(ilt)
-               end
+               module_table.append(ilt)
                module_table.append(clt)
        end
 end
@@ -288,7 +267,7 @@ redef class MMSrcModule
                                if cte.length > 0 then
                                        ctab.add(cte)
                                end
-                               if not cctx.attr_sim and ite.length > 0 then
+                               if ite.length > 0 then
                                        itab.add(ite)
                                end
                        end
@@ -322,14 +301,6 @@ redef class MMSrcModule
                                cc.instance_table = scc.instance_table.to_a
                                append_to_table(ga, cc.instance_table, cc.instance_layout)
                        end
-
-                       if cctx.attr_sim then
-                               cc.instance_table = build_tables(ga, c, itab)
-                               for sc in c.cshe.greaters_and_self do
-                                       var scc = ga.compiled_classes[sc.global]
-                                       append_to_table(cc, cc.instance_table, scc.instance_layout)
-                               end
-                       end
                end
 
                return ga
@@ -473,7 +444,7 @@ redef class MMSrcModule
                if not has_global_class_named(sysname) then
                        print("No main")
                else
-                       var sys = class_by_name(sysname).get_type
+                       var sys = class_by_name(sysname)
                        # var initm = sys.select_method(once "init".to_symbol)
                        var mainm = sys.select_method(once "main".to_symbol)
                        if mainm == null then
@@ -492,34 +463,35 @@ redef class MMSrcModule
        # Compile sep files 
        meth compile_mod_to_c(v: CompilerVisitor)
        do
-               v.add_decl("#define LOCATE_{name} \"{filename}\"")
+               v.add_decl("extern const char *LOCATE_{name};")
                if not v.tc.global then
                        v.add_decl("extern const int SFT_{name}[];")
                end
                var i = 0
                for e in _local_table do
+                       var value: String
                        if v.tc.global then
-                               v.add_decl("#define {e.symbol} {e.value(v.global_analysis)}")
+                               value = "{e.value(v.global_analysis)}"
                        else
-                               v.add_decl("#define {e.symbol} SFT_{name}[{i}]")
+                               value = "SFT_{name}[{i}]"
                                i = i + 1
                        end
+                       e.compile_macros(v, value)
                end
                for c in src_local_classes do
-                       for p in c.src_local_properties do
-                               var pg = p.global
-                               if pg.intro == p and p isa MMAttribute then
-                                       if v.tc.attr_sim then
-                                               var bc = pg.local_class
-                                               assert bc isa MMSrcLocalClass
-                                               var s = bc.base_attr_pos.symbol
-                                               v.add_decl("#define {pg.attr_access}(recv) ATTRS(recv, {s}, {pg.pos_of})")
-                                       else
-                                               v.add_decl("#define {pg.attr_access}(recv) ATTR(recv, {pg.color_id})")
+                       for pg in c.global_properties do
+                               var p = c[pg]
+                               if p.local_class == c then
+                                       p.compile_property_to_c(v)
+                               end
+                               if pg.is_init_for(c) then
+                                       # Declare constructors
+                                       var params = new Array[String]
+                                       for i in [0..p.signature.arity[ do
+                                               params.add("val_t p{i}")
                                        end
+                                       v.add_decl("val_t NEW_{c}_{p.global.intro.cname}({params.join(", ")});")
                                end
-                               assert p isa MMConcreteProperty
-                               p.compile_property_to_c(v)
                        end
                end
        end
@@ -527,6 +499,8 @@ redef class MMSrcModule
        # Compile module file for the current module
        meth compile_local_table_to_c(v: CompilerVisitor)
        do
+               v.add_instr("const char *LOCATE_{name} = \"{filename}\";")
+
                if v.tc.global or _local_table.is_empty then
                        return
                end
@@ -541,33 +515,39 @@ redef class MMSrcModule
        end
 end
 
-class TableElt
+abstract class TableElt
        meth is_related_to(c: MMLocalClass): Bool is abstract
        meth length: Int do return 1
        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
 end
 
-class LocalTableElt
+abstract class LocalTableElt
 special TableElt
-       meth symbol: String is abstract
        meth value(ga: GlobalAnalysis): String is abstract
 end
 
-class TableEltPropPos
+abstract class TableEltProp
 special LocalTableElt
-       attr _property: MMConcreteProperty
-       redef meth symbol do return _property.global.color_id
+       attr _property: MMLocalProperty
+
        redef meth value(ga) do return "{ga.color(self)} /* Property {_property} */"
 
-       init(p: MMConcreteProperty)
+       init(p: MMLocalProperty)
        do
                _property = p
        end
 end
 
-class TableEltMethPos
-special TableEltPropPos
+class TableEltMeth
+special TableEltProp
+       redef meth compile_macros(v, value)
+       do
+               var pg = _property.global
+               v.add_decl("#define {pg.meth_call}(recv) (({pg.intro.cname}_t)CALL((recv), ({value})))")
+       end
+
        redef meth compile_to_c(v, c)
        do
                var p = c[_property.global]
@@ -576,9 +556,14 @@ special TableEltPropPos
        init(p) do super 
 end
 
-class TableEltSuperPos
-special TableEltPropPos
-       redef meth symbol do return _property.color_id_for_super
+class TableEltSuper
+special TableEltProp
+       redef meth compile_macros(v, value)
+       do
+               var p = _property
+               v.add_decl("#define {p.super_meth_call}(recv) (({p.cname}_t)CALL((recv), ({value})))")
+       end
+
        redef meth compile_to_c(v, c)
        do
                var pc = _property.local_class
@@ -591,7 +576,7 @@ special TableEltPropPos
                                found = true
                        else if found and c.che < s then
                                var p = s[g]
-                               if p != null and p isa MMConcreteProperty then
+                               if p != null then
                                        #print "found {s.module}::{s}::{p}"
                                        return p.cname
                                end
@@ -604,8 +589,14 @@ special TableEltPropPos
        init(p) do super 
 end
 
-class TableEltAttrPos
-special TableEltPropPos
+class TableEltAttr
+special TableEltProp
+       redef meth compile_macros(v, value)
+       do
+               var pg = _property.global
+               v.add_decl("#define {pg.attr_access}(recv) ATTR(recv, ({value}))")
+       end
+
        redef meth compile_to_c(v, c)
        do
                var ga = v.global_analysis
@@ -616,7 +607,7 @@ special TableEltPropPos
        init(p) do super 
 end
 
-class TableEltClassPos
+class TableEltClass
 special LocalTableElt
        attr _local_class: MMLocalClass
        redef meth is_related_to(c)
@@ -629,11 +620,19 @@ special LocalTableElt
        do
                _local_class = c
        end
+
+       meth symbol: String is abstract
+
+       redef meth compile_macros(v, value)
+       do
+               v.add_decl("#define {symbol} ({value})")
+       end
 end
 
-class TableEltClassIdPos
-special TableEltClassPos
+class TableEltClassId
+special TableEltClass
        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} */"
@@ -642,13 +641,15 @@ special TableEltClassPos
        init(c) do super
 end
 
-class TableEltClassInitTablePos
-special TableEltClassPos
+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
@@ -664,9 +665,10 @@ special TableEltClassPos
        init(c) do super
 end
 
-class TableEltClassColorPos
-special TableEltClassPos
+class TableEltClassColor
+special TableEltClass
        redef meth symbol do return _local_class.global.color_id
+
        redef meth value(ga)
        do
                return "{ga.color(self)} /* Color of {_local_class} */"
@@ -681,28 +683,6 @@ special TableEltClassPos
        init(c) do super
 end
 
-class TableEltBaseAttrPos
-special LocalTableElt
-       attr _local_class: MMSrcLocalClass
-       redef meth symbol do return "COLOR_{_local_class.module}_{_local_class.name}_BASEATTR"
-       redef meth value(ga)
-       do
-               return "{ga.color(self)} /* BaseAttrPos of {_local_class} */"
-       end
-
-       redef meth compile_to_c(v, c)
-       do
-               var ga = v.global_analysis
-               var cc = ga.compiled_classes[c.global]
-               return "{cc.color(_local_class.instance_layout.first)} /* {ga.color(self)}: Base attribut offset of {_local_class.module}::{_local_class} in {c} */"
-       end
-
-       init(c: MMSrcLocalClass)
-       do
-               _local_class = c
-       end
-end
-
 class TableEltComposite
 special TableElt
        attr _table: Array[LocalTableElt]
@@ -817,7 +797,7 @@ redef class MMLocalClass
                        if e == null then
                                v.add_instr("\{0} /* Class Hole :( */,")
                        else
-                               v.add_instr("\{(int) {e.compile_to_c(v, self)}},")
+                               v.add_instr("\{(bigint) {e.compile_to_c(v, self)}},")
                        end
                end
                if clen > ctab.length then
@@ -836,26 +816,30 @@ redef class MMLocalClass
 
                var pi = primitive_info
                if pi == null then
-                       v.clear
+                       v.cfc = new CFunctionContext(v)
+                       v.nmc = new NitMethodContext(null)
                        var s = "val_t NEW_{name}(void)"
                        v.add_instr(s + " \{")
                        v.indent
                        var ctx_old = v.ctx
                        v.ctx = new CContext
 
-                       v.method_params = ["OBJ2VAL(obj)"]
+                       var self_var = new ParamVariable(null, null)
+                       var self_var_cname = v.cfc.register_variable(self_var)
+                       v.nmc.method_params = [self_var]
 
                        v.add_instr("obj_t obj;")
                        v.add_instr("obj = alloc(sizeof(val_t) * {itab.length});")
                        v.add_instr("obj->vft = (classtable_elt_t*)VFT_{name};")
+                       v.add_assignment(self_var_cname, "OBJ2VAL(obj)")
+
                        for g in global_properties do
                                var p = self[g]
                                var t = p.signature.return_type
                                if p isa MMAttribute and t != null then
                                        # FIXME: Not compatible with sep compilation
-                                       var pi = p.concrete_property
-                                       assert pi isa MMSrcAttribute
-                                       var np = pi.node
+                                       assert p isa MMSrcAttribute
+                                       var np = p.node
                                        assert np isa AAttrPropdef
                                        var ne = np.n_expr
                                        if ne != null then
@@ -871,6 +855,7 @@ redef class MMLocalClass
                                end
                        end
                        v.add_instr("return OBJ2VAL(obj);")
+                       v.cfc.generate_var_decls
                        ctx_old.append(v.ctx)
                        v.ctx = ctx_old
                        v.unindent
@@ -881,8 +866,8 @@ redef class MMLocalClass
 
                        for g in global_properties do
                                var p = self[g]
-                               if not p.global.is_init or p.global.intro.local_class.global != global then continue
-                               v.clear
+                               # FIXME skip invisible constructors
+                               if not p.global.is_init_for(self) then continue
                                var params = new Array[String]
                                var args = ["self"]
                                for i in [0..p.signature.arity[ do
@@ -890,12 +875,12 @@ redef class MMLocalClass
                                        args.add("p{i}")
                                end
                                args.add("init_table")
-                               var s = "val_t NEW_{p.global.intro.cname}({params.join(", ")})"
-                               v.add_instr(s + " \{")
+                               var s = "val_t NEW_{self}_{p.global.intro.cname}({params.join(", ")}) \{"
+                               v.add_instr(s)
                                v.indent
                                v.add_instr(init_table_decl)
                                v.add_instr("val_t self = NEW_{name}();")
-                               v.add_instr("{p.concrete_property.cname}({args.join(", ")});")
+                               v.add_instr("{p.cname}({args.join(", ")});")
                                v.add_instr("return self;")
                                v.unindent
                                v.add_instr("}")