nitc: fix calling extern constructors from extern code in separate compiler
[nit.git] / src / compiler / abstract_compiler.nit
index 6329dac..07a8ede 100644 (file)
@@ -120,20 +120,16 @@ redef class ModelBuilder
        # Simple indirection to `Toolchain::write_and_make`
        protected fun write_and_make(compiler: AbstractCompiler)
        do
-               var platform = compiler.mainmodule.target_platform
-               var toolchain
-               if platform == null then
-                       toolchain = new MakefileToolchain(toolcontext)
-               else
-                       toolchain = platform.toolchain(toolcontext)
-               end
+               var platform = compiler.target_platform
+               var toolchain = platform.toolchain(toolcontext)
                compile_dir = toolchain.compile_dir
                toolchain.write_and_make compiler
        end
 end
 
 redef class Platform
-       fun toolchain(toolcontext: ToolContext): Toolchain is abstract
+       # The specific tool-chain associated to the platform
+       fun toolchain(toolcontext: ToolContext): Toolchain do return new MakefileToolchain(toolcontext)
 end
 
 class Toolchain
@@ -189,10 +185,10 @@ class MakefileToolchain
 
        fun write_files(compiler: AbstractCompiler, compile_dir: String, cfiles: Array[String])
        do
-               var platform = compiler.mainmodule.target_platform
-               if self.toolcontext.opt_stacktrace.value == "nitstack" and (platform == null or platform.supports_libunwind) then compiler.build_c_to_nit_bindings
+               var platform = compiler.target_platform
+               if self.toolcontext.opt_stacktrace.value == "nitstack" and platform.supports_libunwind then compiler.build_c_to_nit_bindings
                var cc_opt_with_libgc = "-DWITH_LIBGC"
-               if platform != null and not platform.supports_libgc then cc_opt_with_libgc = ""
+               if not platform.supports_libgc then cc_opt_with_libgc = ""
 
                # Add gc_choser.h to aditionnal bodies
                var gc_chooser = new ExternCFile("gc_chooser.c", cc_opt_with_libgc)
@@ -311,7 +307,7 @@ class MakefileToolchain
        fun write_makefile(compiler: AbstractCompiler, compile_dir: String, cfiles: Array[String])
        do
                var mainmodule = compiler.mainmodule
-               var platform = compiler.mainmodule.target_platform
+               var platform = compiler.target_platform
 
                var outname = outfile(mainmodule)
 
@@ -333,10 +329,10 @@ class MakefileToolchain
                        if libs != null then linker_options.add_all(libs)
                end
 
-               makefile.write("CC = ccache cc\nCXX = ccache c++\nCFLAGS = -g -O2 -Wno-unused-value -Wno-switch\nCINCL =\nLDFLAGS ?= \nLDLIBS  ?= -lm {linker_options.join(" ")}\n\n")
+               makefile.write("CC = ccache cc\nCXX = ccache c++\nCFLAGS = -g -O2 -Wno-unused-value -Wno-switch -Wno-attributes\nCINCL =\nLDFLAGS ?= \nLDLIBS  ?= -lm {linker_options.join(" ")}\n\n")
 
                var ost = toolcontext.opt_stacktrace.value
-               if (ost == "libunwind" or ost == "nitstack") and (platform == null or platform.supports_libunwind) then makefile.write("NEED_LIBUNWIND := YesPlease\n")
+               if (ost == "libunwind" or ost == "nitstack") and platform.supports_libunwind then makefile.write("NEED_LIBUNWIND := YesPlease\n")
 
                # Dynamic adaptations
                # While `platform` enable complex toolchains, they are statically applied
@@ -371,6 +367,18 @@ class MakefileToolchain
                        dep_rules.add(o)
                end
 
+               # Generate linker script, if any
+               if not compiler.linker_script.is_empty then
+                       var linker_script_path = "{compile_dir}/linker_script"
+                       ofiles.add "linker_script"
+                       var f = new OFStream.open(linker_script_path)
+                       for l in compiler.linker_script do
+                               f.write l
+                               f.write "\n"
+                       end
+                       f.close
+               end
+
                var java_files = new Array[ExternFile]
 
                var pkgconfigs = new Array[String]
@@ -476,11 +484,19 @@ abstract class AbstractCompiler
        # Is hardening asked? (see --hardening)
        fun hardening: Bool do return self.modelbuilder.toolcontext.opt_hardening.value
 
+       # The targeted specific platform
+       var target_platform: Platform is noinit
+
        init
        do
                self.realmainmodule = mainmodule
+               target_platform = mainmodule.target_platform or else new Platform
        end
 
+       # Do the full code generation of the program `mainmodule`
+       # It is the main method usually called after the instantiation
+       fun do_compilation is abstract
+
        # Force the creation of a new file
        # The point is to avoid contamination between must-be-compiled-separately files
        fun new_file(name: String): CodeFile
@@ -507,6 +523,10 @@ abstract class AbstractCompiler
        # Where global declaration are stored (the main .h)
        var header: CodeWriter is writable, noinit
 
+       # Additionnal linker script for `ld`.
+       # Mainly used to do specific link-time symbol resolution
+       var linker_script = new Array[String]
+
        # Provide a declaration that can be requested (before or latter) by a visitor
        fun provide_declaration(key: String, s: String)
        do
@@ -677,11 +697,11 @@ extern void nitni_global_ref_decr( struct nitni_ref *ref );
                var v = self.new_visitor
                v.add_decl("#include <signal.h>")
                var ost = modelbuilder.toolcontext.opt_stacktrace.value
-               var platform = mainmodule.target_platform
+               var platform = target_platform
 
-               if platform != null and not platform.supports_libunwind then ost = "none"
+               if not platform.supports_libunwind then ost = "none"
 
-               var no_main = (platform != null and platform.no_main) or modelbuilder.toolcontext.opt_no_main.value
+               var no_main = platform.no_main or modelbuilder.toolcontext.opt_no_main.value
 
                if ost == "nitstack" or ost == "libunwind" then
                        v.add_decl("#define UNW_LOCAL_ONLY")
@@ -1036,8 +1056,8 @@ abstract class AbstractCompilerVisitor
        # The current visited AST node
        var current_node: nullable ANode = null is writable
 
-       # The current `Frame`
-       var frame: nullable Frame = null is writable
+       # The current `StaticFrame`
+       var frame: nullable StaticFrame = null is writable
 
        # Alias for self.compiler.mainmodule.object_type
        fun object_type: MClassType do return self.compiler.mainmodule.object_type
@@ -1279,11 +1299,11 @@ abstract class AbstractCompilerVisitor
        fun escapemark_name(e: nullable EscapeMark): String
        do
                assert e != null
-               if escapemark_names.has_key(e) then return escapemark_names[e]
+               if frame.escapemark_names.has_key(e) then return frame.escapemark_names[e]
                var name = e.name
                if name == null then name = "label"
                name = get_name(name)
-               escapemark_names[e] = name
+               frame.escapemark_names[e] = name
                return name
        end
 
@@ -1295,8 +1315,6 @@ abstract class AbstractCompilerVisitor
                add("BREAK_{escapemark_name(e)}: (void)0;")
        end
 
-       private var escapemark_names = new HashMap[EscapeMark, String]
-
        # Return a "const char*" variable associated to the classname of the dynamic type of an object
        # NOTE: we do not return a `RuntimeVariable` "NativeString" as the class may not exist in the module/program
        fun class_name_string(value: RuntimeVariable): String is abstract
@@ -1363,6 +1381,24 @@ abstract class AbstractCompilerVisitor
        # Generate a alloc-instance + init-attributes
        fun init_instance(mtype: MClassType): RuntimeVariable is abstract
 
+       # Allocate and init attributes of an instance of a standard or extern class
+       #
+       # Does not support universals and the pseudo-internal `NativeArray` class.
+       fun init_instance_or_extern(mtype: MClassType): RuntimeVariable
+       do
+               var recv
+               var ctype = mtype.ctype
+               assert mtype.mclass.name != "NativeArray"
+               if ctype == "val*" then
+                       recv = init_instance(mtype)
+               else if ctype == "char*" then
+                       recv = new_expr("NULL/*special!*/", mtype)
+               else
+                       recv = new_expr("({ctype})0/*special!*/", mtype)
+               end
+               return recv
+       end
+
        # Set a GC finalizer on `recv`, only if `recv` isa Finalizable
        fun set_finalizer(recv: RuntimeVariable)
        do
@@ -1460,10 +1496,11 @@ abstract class AbstractCompilerVisitor
                self.require_declaration(s)
        end
 
-       # look for a needed .h and .c file for a given .nit source-file
-       # FIXME: bad API, parameter should be a `MModule`, not its source-file
-       fun add_extern(file: String)
+       # Look for a needed .h and .c file for a given module
+       # This is used for the legacy FFI
+       fun add_extern(mmodule: MModule)
        do
+               var file = mmodule.location.file.filename
                file = file.strip_extension(".nit")
                var tryfile = file + ".nit.h"
                if tryfile.file_exists then
@@ -1536,6 +1573,15 @@ abstract class AbstractCompilerVisitor
        fun stmt(nexpr: nullable AExpr)
        do
                if nexpr == null then return
+
+               var narray = nexpr.comprehension
+               if narray != null then
+                       var recv = frame.comprehension.as(not null)
+                       var val = expr(nexpr, narray.element_mtype)
+                       compile_callsite(narray.push_callsite.as(not null), [recv, val])
+                       return
+               end
+
                var old = self.current_node
                self.current_node = nexpr
                nexpr.stmt(self)
@@ -1658,8 +1704,8 @@ class RuntimeVariable
        end
 end
 
-# A frame correspond to a visited property in a `GlobalCompilerVisitor`
-class Frame
+# The static context of a visited property in a `AbstractCompilerVisitor`
+class StaticFrame
 
        type VISITOR: AbstractCompilerVisitor
 
@@ -1681,6 +1727,13 @@ class Frame
 
        # The label at the end of the property
        var returnlabel: nullable String = null is writable
+
+       # Labels associated to a each escapemarks.
+       # Because of inlinings, escape-marks must be associated to their context (the frame)
+       private var escapemark_names = new HashMap[EscapeMark, String]
+
+       # The array comprehension currently filled, if any
+       private var comprehension: nullable RuntimeVariable = null
 end
 
 redef class MType
@@ -1692,21 +1745,9 @@ redef class MType
 
        # Short name of the `ctype` to use in unions
        fun ctypename: String do return "val"
-
-       # Return the name of the C structure associated to a Nit live type
-       fun c_name: String is abstract
-       protected var c_name_cache: nullable String is protected writable
 end
 
 redef class MClassType
-       redef fun c_name
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{mclass.intro_mmodule.c_name}__{mclass.name.to_cmangle}"
-               self.c_name_cache = res
-               return res
-       end
 
        redef fun ctype: String
        do
@@ -1757,90 +1798,8 @@ redef class MClassType
        end
 end
 
-redef class MGenericType
-       redef fun c_name
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = super
-               for t in self.arguments do
-                       res = res + t.c_name
-               end
-               self.c_name_cache = res
-               return res
-       end
-end
-
-redef class MParameterType
-       redef fun c_name
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{self.mclass.c_name}_FT{self.rank}"
-               self.c_name_cache = res
-               return res
-       end
-end
-
-redef class MVirtualType
-       redef fun c_name
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
-               self.c_name_cache = res
-               return res
-       end
-end
-
-redef class MNullableType
-       redef fun c_name
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "nullable_{self.mtype.c_name}"
-               self.c_name_cache = res
-               return res
-       end
-end
-
-redef class MClass
-       # Return the name of the C structure associated to a Nit class
-       fun c_name: String do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{intro_mmodule.c_name}__{name.to_cmangle}"
-               self.c_name_cache = res
-               return res
-       end
-       private var c_name_cache: nullable String
-end
-
-redef class MProperty
-       fun c_name: String do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{self.intro.c_name}"
-               self.c_name_cache = res
-               return res
-       end
-       private var c_name_cache: nullable String
-end
-
 redef class MPropDef
        type VISITOR: AbstractCompilerVisitor
-
-       private var c_name_cache: nullable String
-
-       # The mangled name associated to the property
-       fun c_name: String
-       do
-               var res = self.c_name_cache
-               if res != null then return res
-               res = "{self.mclassdef.mmodule.c_name}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
-               self.c_name_cache = res
-               return res
-       end
 end
 
 redef class MMethodDef
@@ -2231,16 +2190,13 @@ redef class AMethPropdef
        do
                var externname
                var at = self.get_single_annotation("extern", v.compiler.modelbuilder)
-               if at != null then
+               if at != null and at.n_args.length == 1 then
                        externname = at.arg_as_string(v.compiler.modelbuilder)
                        if externname == null then return false
                else
                        return false
                end
-               if location.file != null then
-                       var file = location.file.filename
-                       v.add_extern(file)
-               end
+               v.add_extern(mpropdef.mclassdef.mmodule)
                var res: nullable RuntimeVariable = null
                var ret = mpropdef.msignature.return_mtype
                if ret != null then
@@ -2272,10 +2228,7 @@ redef class AMethPropdef
                else
                        return false
                end
-               if location.file != null then
-                       var file = location.file.filename
-                       v.add_extern(file)
-               end
+               v.add_extern(mpropdef.mclassdef.mmodule)
                v.adapt_signature(mpropdef, arguments)
                v.unbox_signature_extern(mpropdef, arguments)
                var ret = arguments.first.mtype
@@ -2291,6 +2244,8 @@ redef class AMethPropdef
 end
 
 redef class AAttrPropdef
+       redef fun can_inline: Bool do return not is_lazy
+
        redef fun compile_to_c(v, mpropdef, arguments)
        do
                if mpropdef == mreadpropdef then
@@ -2349,7 +2304,7 @@ redef class AAttrPropdef
                var oldnode = v.current_node
                v.current_node = self
                var old_frame = v.frame
-               var frame = new Frame(v, self.mpropdef.as(not null), recv.mcasttype.as(MClassType), [recv])
+               var frame = new StaticFrame(v, self.mpropdef.as(not null), recv.mcasttype.as_notnullable.as(MClassType), [recv])
                v.frame = frame
 
                var value
@@ -2388,7 +2343,7 @@ redef class AAttrPropdef
                var oldnode = v.current_node
                v.current_node = self
                var old_frame = v.frame
-               var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
+               var frame = new StaticFrame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
                v.frame = frame
                # Force read to check the initialization
                v.read_attribute(self.mpropdef.mproperty, recv)
@@ -2742,13 +2697,18 @@ end
 redef class AArrayExpr
        redef fun expr(v)
        do
-               var mtype = self.mtype.as(MClassType).arguments.first
+               var mtype = self.element_mtype.as(not null)
                var array = new Array[RuntimeVariable]
-               for nexpr in self.n_exprs.n_exprs do
-                       var i = v.expr(nexpr, mtype)
-                       array.add(i)
+               var res = v.array_instance(array, mtype)
+
+               var old_comprehension = v.frame.comprehension
+               v.frame.comprehension = res
+               for nexpr in self.n_exprs do
+                       v.stmt(nexpr)
                end
-               return v.array_instance(array, mtype)
+               v.frame.comprehension = old_comprehension
+
+               return res
        end
 end
 
@@ -2932,22 +2892,17 @@ redef class ANewExpr
        do
                var mtype = self.recvtype
                assert mtype != null
-               var recv
-               var ctype = mtype.ctype
+
                if mtype.mclass.name == "NativeArray" then
                        assert self.n_args.n_exprs.length == 1
                        var l = v.expr(self.n_args.n_exprs.first, null)
                        assert mtype isa MGenericType
                        var elttype = mtype.arguments.first
                        return v.native_array_instance(elttype, l)
-               else if ctype == "val*" then
-                       recv = v.init_instance(mtype)
-               else if ctype == "char*" then
-                       recv = v.new_expr("NULL/*special!*/", mtype)
-               else
-                       recv = v.new_expr("({ctype})0/*special!*/", mtype)
                end
 
+               var recv = v.init_instance_or_extern(mtype)
+
                var callsite = self.callsite.as(not null)
                var args = v.varargize(callsite.mpropdef, recv, self.n_args.n_exprs)
                var res2 = v.compile_callsite(callsite, args)
@@ -3020,19 +2975,6 @@ redef class Array[E]
 end
 
 redef class MModule
-       # Return the name of the global C identifier associated to `self`.
-       # This name is used to prefix files and other C identifiers associated with `self`.
-       var c_name: String is lazy do
-               var g = mgroup
-               var res
-               if g != null and g.mproject.name != name then
-                       res = g.mproject.name.to_cmangle + "__" + name.to_cmangle
-               else
-                       res = name.to_cmangle
-               end
-               return res
-       end
-
        # All `MProperty` associated to all `MClassDef` of `mclass`
        fun properties(mclass: MClass): Set[MProperty] do
                if not self.properties_cache.has_key(mclass) then
@@ -3061,13 +3003,13 @@ redef class MModule
 
        # Give requided addinional system libraries (as given to LD_LIBS)
        # Note: can return null instead of an empty set
-       fun collect_linker_libs: nullable Set[String] do return null
+       fun collect_linker_libs: nullable Array[String] do return null
 end
 
 # Create a tool context to handle options and paths
 var toolcontext = new ToolContext
 
-toolcontext.tooldescription = "Usage: nitg [OPTION]... file.nit...\nCompiles Nit programs."
+toolcontext.tooldescription = "Usage: nitc [OPTION]... file.nit...\nCompiles Nit programs."
 
 # We do not add other options, so process them now!
 toolcontext.process_options(args)