rename `NativeString` to `CString`
[nit.git] / src / compiler / separate_erasure_compiler.nit
index 004d361..244a9ed 100644 (file)
@@ -199,8 +199,6 @@ class SeparateErasureCompiler
                var mtype = mclass.intro.bound_mtype
                var c_name = mclass.c_name
 
-               var vft = self.method_tables[mclass]
-               var attrs = self.attr_tables[mclass]
                var class_table = self.class_tables[mclass]
                var v = self.new_visitor
 
@@ -230,7 +228,8 @@ class SeparateErasureCompiler
                        end
                        v.add_decl("&type_table_{c_name},")
                        v.add_decl("\{")
-                       for i in [0 .. vft.length[ do
+                       var vft = self.method_tables.get_or_null(mclass)
+                       if vft != null then for i in [0 .. vft.length[ do
                                var mpropdef = vft[i]
                                if mpropdef == null then
                                        v.add_decl("NULL, /* empty */")
@@ -274,7 +273,7 @@ class SeparateErasureCompiler
                        #Build BOX
                        self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype_extern});")
                        v.add_decl("/* allocate {mtype} */")
-                       v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
+                       v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype_extern} value) \{")
                        v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
                        v.require_declaration("class_{c_name}")
                        v.add("res->class = &class_{c_name};")
@@ -323,7 +322,7 @@ class SeparateErasureCompiler
                        v.add("return (val*){res};")
                        v.add("\}")
                        return
-               else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" then
+               else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "CString" then
                        var pointer_type = mainmodule.pointer_type
 
                        self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
@@ -355,11 +354,18 @@ class SeparateErasureCompiler
 
                        var res = v.new_named_var(mtype, "self")
                        res.is_exact = true
-                       v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
+                       var attrs = self.attr_tables.get_or_null(mclass)
+                       if attrs == null then
+                               v.add("{res} = nit_alloc(sizeof(struct instance));")
+                       else
+                               v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
+                       end
                        v.require_declaration("class_{c_name}")
                        v.add("{res}->class = &class_{c_name};")
-                       self.generate_init_attr(v, res, mtype)
-                       v.set_finalizer res
+                       if attrs != null then
+                               self.generate_init_attr(v, res, mtype)
+                               v.set_finalizer res
+                       end
                        v.add("return {res};")
                end
                v.add("\}")
@@ -597,7 +603,7 @@ class SeparateErasureCompilerVisitor
        redef fun unbox_extern(value, mtype)
        do
                if mtype isa MClassType and mtype.mclass.kind == extern_kind and
-                  mtype.mclass.name != "NativeString" then
+                  mtype.mclass.name != "CString" then
                        var pointer_type = compiler.mainmodule.pointer_type
                        var res = self.new_var_extern(mtype)
                        self.add "{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
@@ -610,12 +616,12 @@ class SeparateErasureCompilerVisitor
        redef fun box_extern(value, mtype)
        do
                if mtype isa MClassType and mtype.mclass.kind == extern_kind and
-                  mtype.mclass.name != "NativeString" then
+                  mtype.mclass.name != "CString" then
                        var valtype = compiler.mainmodule.pointer_type
                        var res = self.new_var(mtype)
                        if compiler.runtime_type_analysis != null and not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
                                self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
-                               self.add("PRINT_ERROR(\"Dead code executed!\\n\"); show_backtrace(1);")
+                               self.add("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
                                return res
                        end
                        self.require_declaration("BOX_{valtype.c_name}")
@@ -643,7 +649,7 @@ class SeparateErasureCompilerVisitor
 
        redef fun native_array_instance(elttype, length)
        do
-               var nclass = self.get_class("NativeArray")
+               var nclass = mmodule.native_array_class
                var mtype = nclass.get_mtype([elttype])
                var res = self.new_var(mtype)
                res.is_exact = true
@@ -651,11 +657,4 @@ class SeparateErasureCompilerVisitor
                self.add("{res} = NEW_{nclass.c_name}({length});")
                return res
        end
-
-       redef fun calloc_array(ret_type, arguments)
-       do
-               var ret = ret_type.as(MClassType)
-               self.require_declaration("NEW_{ret.mclass.c_name}")
-               self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
-       end
 end