Suppress C warnings related to NEW_ functions
[nit.git] / src / compiling / compiling_global.nit
index f51aede..8a70439 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
@@ -473,7 +473,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,7 +492,7 @@ 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
@@ -506,20 +506,29 @@ redef class MMSrcModule
                        end
                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
+                                       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})")
+                                               end
+                                       end
+                                       p.compile_property_to_c(v)
+                               end
+                               if pg.is_init 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 MMSrcLocalProperty
-                               p.compile_property_to_c(v)
                        end
                end
        end
@@ -527,6 +536,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
@@ -556,11 +567,11 @@ end
 
 class TableEltPropPos
 special LocalTableElt
-       attr _property: MMSrcLocalProperty
+       attr _property: MMLocalProperty
        redef meth symbol do return _property.global.color_id
        redef meth value(ga) do return "{ga.color(self)} /* Property {_property} */"
 
-       init(p: MMSrcLocalProperty)
+       init(p: MMLocalProperty)
        do
                _property = p
        end
@@ -591,7 +602,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
@@ -817,7 +828,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 +847,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 +886,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 +897,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 then continue
                                var params = new Array[String]
                                var args = ["self"]
                                for i in [0..p.signature.arity[ do
@@ -890,12 +906,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("}")