nitc: renamed --no-check-initialization in --no-check-attr-isset to follow the new...
[nit.git] / src / separate_compiler.nit
index 92cd821..4bbdd2d 100644 (file)
@@ -18,7 +18,6 @@ module separate_compiler
 import abstract_compiler
 import layout_builders
 import rapid_type_analysis
-import compiler_ffi
 
 # Add separate compiler specific options
 redef class ToolContext
@@ -807,7 +806,8 @@ class SeparateCompiler
                        self.header.add_decl("struct instance_{c_instance_name} \{")
                        self.header.add_decl("const struct type *type;")
                        self.header.add_decl("const struct class *class;")
-                       # NativeArrays are just a instance header followed by an array of values
+                       # NativeArrays are just a instance header followed by a length and an array of values
+                       self.header.add_decl("int length;")
                        self.header.add_decl("val* values[0];")
                        self.header.add_decl("\};")
 
@@ -815,15 +815,16 @@ class SeparateCompiler
                        self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length, const struct type* type);")
                        v.add_decl("/* allocate {mtype} */")
                        v.add_decl("{mtype.ctype} NEW_{c_name}(int length, const struct type* type) \{")
-                       var res = v.new_named_var(mtype, "self")
-                       res.is_exact = true
+                       var res = v.get_name("self")
+                       v.add_decl("struct instance_{c_instance_name} *{res};")
                        var mtype_elt = mtype.arguments.first
                        v.add("{res} = nit_alloc(sizeof(struct instance_{c_instance_name}) + length*sizeof({mtype_elt.ctype}));")
                        v.add("{res}->type = type;")
                        hardening_live_type(v, "type")
                        v.require_declaration("class_{c_name}")
                        v.add("{res}->class = &class_{c_name};")
-                       v.add("return {res};")
+                       v.add("{res}->length = length;")
+                       v.add("return (val*){res};")
                        v.add("\}")
                        return
                end
@@ -934,10 +935,10 @@ class SeparateCompiler
                self.header.add_decl("struct nitni_instance \{struct instance *value;\};")
        end
        
-       redef fun finalize_ffi_for_module(nmodule)
+       redef fun finalize_ffi_for_module(mmodule)
        do
                var old_module = self.mainmodule
-               self.mainmodule = nmodule.mmodule.as(not null)
+               self.mainmodule = mmodule
                super
                self.mainmodule = old_module
        end
@@ -984,7 +985,7 @@ class SeparateCompilerVisitor
                        self.require_declaration("BOX_{valtype.c_name}")
                        self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
                        return res
-               else if value.mtype.cname_blind == "void*" and mtype.cname_blind == "void*" then
+               else if value.mtype.ctype == "void*" and mtype.ctype == "void*" then
                        return value
                else
                        # Bad things will appen!
@@ -1302,7 +1303,7 @@ class SeparateCompilerVisitor
                        self.add("{res} = {recv}->attrs[{a.const_color}]; /* {a} on {recv.inspect} */")
 
                        # Check for Uninitialized attribute
-                       if not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
+                       if not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
                                self.add("if (unlikely({res} == NULL)) \{")
                                self.add_abort("Uninitialized attribute {a.name}")
                                self.add("\}")
@@ -1316,7 +1317,7 @@ class SeparateCompilerVisitor
                        self.add("{res} = {recv}->attrs[{a.const_color}].{ret.ctypename}; /* {a} on {recv.inspect} */")
 
                        # Check for Uninitialized attribute
-                       if ret.ctype == "val*" and not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then
+                       if ret.ctype == "val*" and not ret isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
                                self.add("if (unlikely({res} == NULL)) \{")
                                self.add_abort("Uninitialized attribute {a.name}")
                                self.add("\}")
@@ -1697,6 +1698,9 @@ class SeparateCompilerVisitor
                else if pname == "[]=" then
                        self.add("{recv}[{arguments[1]}]={arguments[2]};")
                        return
+               else if pname == "length" then
+                       self.ret(self.new_expr("((struct instance_{nclass.c_instance_name}*){arguments[0]})->length", ret_type.as(not null)))
+                       return
                else if pname == "copy_to" then
                        var recv1 = "((struct instance_{nclass.c_instance_name}*){arguments[1]})->values"
                        self.add("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")