ffi: move a lot of code and services from AModule to MModule
authorJean Privat <jean@pryen.org>
Wed, 2 Apr 2014 18:35:33 +0000 (14:35 -0400)
committerJean Privat <jean@pryen.org>
Wed, 2 Apr 2014 18:35:33 +0000 (14:35 -0400)
AModule is not the right class to do or store model related things.
Migrating these services simplify the code.

Signed-off-by: Jean Privat <jean@pryen.org>

src/abstract_compiler.nit
src/common_ffi/c.nit
src/common_ffi/c_compiler_options.nit
src/common_ffi/common_ffi.nit
src/common_ffi/cpp.nit
src/common_ffi/pkgconfig.nit
src/compiler_ffi.nit
src/separate_compiler.nit

index af4a343..aab3918 100644 (file)
@@ -199,9 +199,8 @@ class MakefileToolchain
 
                # FFI
                var m2m = toolcontext.modelbuilder.mmodule2nmodule
-               for m in compiler.mainmodule.in_importation.greaters do if m2m.keys.has(m) then
-                       var amodule = m2m[m]
-                       compiler.finalize_ffi_for_module(amodule)
+               for m in compiler.mainmodule.in_importation.greaters do
+                       compiler.finalize_ffi_for_module(m)
                end
 
                # Copy original .[ch] files to compile_dir
@@ -300,9 +299,8 @@ class MakefileToolchain
 
                var linker_options = new HashSet[String]
                var m2m = toolcontext.modelbuilder.mmodule2nmodule
-               for m in mainmodule.in_importation.greaters do if m2m.keys.has(m) then
-                       var amod = m2m[m]
-                       var libs = amod.collect_linker_libs
+               for m in mainmodule.in_importation.greaters do
+                       var libs = m.collect_linker_libs
                        if libs != null then linker_options.add_all(libs)
                end
 
@@ -706,11 +704,11 @@ abstract class AbstractCompiler
                return ((a*10000/b).to_f / 100.0).to_precision(2)
        end
 
-       fun finalize_ffi_for_module(nmodule: AModule)
+       fun finalize_ffi_for_module(mmodule: MModule)
        do
                var visitor = new_visitor
-               nmodule.finalize_ffi(visitor, modelbuilder)
-               nmodule.finalize_nitni(visitor)
+               mmodule.finalize_ffi(visitor, modelbuilder)
+               mmodule.finalize_nitni(visitor)
        end
 end
 
@@ -2612,9 +2610,7 @@ redef class MModule
                return properties_cache[mclass]
        end
        private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
-end
 
-redef class AModule
        # Write FFI results to file
        fun finalize_ffi(v: AbstractCompilerVisitor, modelbuilder: ModelBuilder) do end
 
index 2535fc9..17241bd 100644 (file)
@@ -71,7 +71,7 @@ redef class Location
        fun as_line_pragma: String do return "#line {line_start} \"{file.filename}\"\n"
 end
 
-redef class AModule
+redef class MModule
        var c_compiler_options writable = ""
        var c_linker_options writable = ""
 end
index b596a8a..ca64759 100644 (file)
@@ -134,33 +134,33 @@ private class CCompilerOptionsPhase
                end
 
                # retreive module
-               var nmodule = nmoduledecl.parent.as(AModule)
+               var mmodule = nmoduledecl.parent.as(AModule).mmodule.as(not null)
 
                for opt in simplified_options do
                        var cmd = opt.option
                        if annotation_name == compiler_annotation_name then
-                               process_c_compiler_annotation(nmodule, cmd)
+                               process_c_compiler_annotation(mmodule, cmd)
                        else if annotation_name == linker_annotation_name then
-                               process_c_linker_annotation(nmodule, cmd)
+                               process_c_linker_annotation(mmodule, cmd)
                        else if annotation_name == cpp_compiler_annotation_name then
-                               process_cpp_compiler_annotation(nmodule, cmd)
+                               process_cpp_compiler_annotation(mmodule, cmd)
                        else abort
                end
        end
 
-       fun process_c_compiler_annotation(nmodule: AModule, opt: String)
+       fun process_c_compiler_annotation(mmodule: MModule, opt: String)
        do
-               nmodule.c_compiler_options = "{nmodule.c_compiler_options} {opt}"
+               mmodule.c_compiler_options = "{mmodule.c_compiler_options} {opt}"
        end
 
-       fun process_c_linker_annotation(nmodule: AModule, opt: String)
+       fun process_c_linker_annotation(mmodule: MModule, opt: String)
        do
-               nmodule.c_linker_options = "{nmodule.c_linker_options} {opt}"
+               mmodule.c_linker_options = "{mmodule.c_linker_options} {opt}"
        end
 
-       fun process_cpp_compiler_annotation(nmodule: AModule, opt: String)
+       fun process_cpp_compiler_annotation(mmodule: MModule, opt: String)
        do
-               nmodule.cpp_compiler_options = "{nmodule.cpp_compiler_options} {opt}"
+               mmodule.cpp_compiler_options = "{mmodule.cpp_compiler_options} {opt}"
        end
 end
 
index d9e3045..44179db 100644 (file)
@@ -99,7 +99,7 @@ redef class AModule
                end
 
                ffi_ccu.write_as_impl(self, compdir)
-               for filename in ffi_ccu.files do ffi_files.add(new ExternCFile(filename, self.c_compiler_options))
+               for filename in ffi_ccu.files do ffi_files.add(new ExternCFile(filename, mmodule.c_compiler_options))
        end
 end
 
index 3af6d1a..be4d374 100644 (file)
@@ -26,6 +26,9 @@ end
 
 redef class AModule
        private var cpp_file: nullable CPPCompilationUnit = null
+end
+
+redef class MModule
        var cpp_compiler_options writable = ""
 end
 
@@ -133,7 +136,7 @@ class CPPLanguage
                nmodule.ffi_files.add(file)
 
                # add linked option to support C++
-               nmodule.c_linker_options = "{nmodule.c_linker_options} -lstdc++"
+               nmodule.mmodule.c_linker_options = "{nmodule.mmodule.c_linker_options} -lstdc++"
        end
 
        redef fun compile_callback(callback, nmodule, mmodule, ecc)
@@ -158,7 +161,8 @@ class CPPCompilationUnit
 
        fun write_to_files(amodule: AModule, compdir: String): ExternCppFile
        do
-               var base_name = "{amodule.mmodule.name}._ffi"
+               var mmodule = amodule.mmodule.as(not null)
+               var base_name = "{mmodule.name}._ffi"
 
                var h_file = "{base_name}.hpp"
                var guard = "{amodule.cname.to_s.to_upper}_NIT_HPP"
@@ -170,22 +174,22 @@ class CPPCompilationUnit
 
                files.add("{compdir}/{c_file}")
 
-               return new ExternCppFile("{compdir}/{c_file}", amodule)
+               return new ExternCppFile("{compdir}/{c_file}", mmodule)
        end
 end
 
 class ExternCppFile
        super ExternFile
 
-       var amodule: AModule
-       init(path: String, amodule: AModule)
+       var mmodule: MModule
+       init(path: String, mmodule: MModule)
        do
                super
-               self.amodule = amodule
+               self.mmodule = mmodule
        end
 
        redef fun makefile_rule_name do return "{filename.basename("")}.o"
-       redef fun makefile_rule_content do return "g++ {amodule.cpp_compiler_options} -c {filename.basename("")} -o {filename.basename("")}.o"
+       redef fun makefile_rule_content do return "g++ {mmodule.cpp_compiler_options} -c {filename.basename("")} -o {filename.basename("")}.o"
 end
 
 class ForeignCppType
index 3addbb5..b263834 100644 (file)
@@ -67,6 +67,7 @@ class PkgconfigPhase
 
                # retreive module
                var nmodule = nmoduledecl.parent.as(AModule)
+               var mmodule = nmodule.mmodule.as(not null)
 
                # check availability of pkg-config
                var proc_which = new IProcess("which", "pkg-config")
@@ -92,12 +93,12 @@ class PkgconfigPhase
                        # compiler
                        var proc = new IProcess("pkg-config", "--cflags", pkg)
                        var compiler_opts = proc.read_all
-                       nmodule.c_compiler_options = "{nmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
+                       mmodule.c_compiler_options = "{mmodule.c_compiler_options} {compiler_opts.replace("\n", " ")}"
 
                        # linker
                        proc = new IProcess("pkg-config", "--libs", pkg)
                        var linker_opts = proc.read_all
-                       nmodule.c_linker_options = "{nmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
+                       mmodule.c_linker_options = "{mmodule.c_linker_options} {linker_opts.replace("\n", " ")}"
                end
 
        end
index 244cc2c..6334be5 100644 (file)
@@ -21,14 +21,22 @@ intrude import abstract_compiler
 intrude import common_ffi
 import nitni
 
-redef class AModule
+redef class MModule
        private var foreign_callbacks = new ForeignCallbackSet
        var nitni_ccu: nullable CCompilationUnit = null
 
+       private fun nmodule(v: AbstractCompilerVisitor): nullable AModule
+       do
+               var m2n = v.compiler.modelbuilder.mmodule2nmodule
+               return m2n.get_or_null(self)
+       end
+
        redef fun finalize_ffi(v: AbstractCompilerVisitor, modelbuilder: ModelBuilder)
        do
-               finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
-               for file in ffi_files do v.compiler.extern_bodies.add(file)
+               var n = nmodule(v)
+               if n == null then return
+               n.finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
+               for file in n.ffi_files do v.compiler.extern_bodies.add(file)
        end
 
        fun ensure_compile_nitni_base(v: AbstractCompilerVisitor)
@@ -40,11 +48,14 @@ redef class AModule
 
        redef fun finalize_nitni(v: AbstractCompilerVisitor)
        do
+               var n = nmodule(v)
+               if n == null then return
+
                ensure_compile_nitni_base(v)
 
-               nitni_ccu.header_c_types.add("#include \"{mmodule.name}._ffi.h\"\n")
+               nitni_ccu.header_c_types.add("#include \"{name}._ffi.h\"\n")
 
-               nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir)
+               nitni_ccu.write_as_nitni(n, v.compiler.modelbuilder.compile_dir)
 
                for file in nitni_ccu.files do
                        v.compiler.extern_bodies.add(new ExternCFile(file, c_compiler_options))
@@ -75,7 +86,8 @@ redef class AExternPropdef
        fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
        do
                var mmodule = mpropdef.mclassdef.mmodule
-               var amainmodule = v.compiler.modelbuilder.mmodule2nmodule[v.compiler.mainmodule]
+               var mainmodule = v.compiler.mainmodule
+               var amainmodule = v.compiler.modelbuilder.mmodule2nmodule[mainmodule]
                var amodule = v.compiler.modelbuilder.mmodule2nmodule[mmodule]
                var mclass_type = mpropdef.mclassdef.bound_mtype
 
@@ -87,33 +99,33 @@ redef class AExternPropdef
                compile_ffi_method(amodule)
 
                # nitni - Compile missing callbacks
-               amodule.ensure_compile_nitni_base(v)
-               var ccu = amodule.nitni_ccu.as(not null)
+               mmodule.ensure_compile_nitni_base(v)
+               var ccu = mmodule.nitni_ccu.as(not null)
 
                for mtype in foreign_callbacks.types do
                        if not mtype.is_cprimitive then
                                mtype.compile_extern_type(v, ccu)
 
                                # has callbacks already been compiled? (this may very well happen with global compilation)
-                               if amodule.check_callback_compilation(mtype) then mtype.compile_extern_helper_functions(v, ccu)
+                               if mmodule.check_callback_compilation(mtype) then mtype.compile_extern_helper_functions(v, ccu)
                        end
                end
 
                # compile callbacks
-               for cb in foreign_callbacks.callbacks do if amainmodule.check_callback_compilation(cb) then
+               for cb in foreign_callbacks.callbacks do if mainmodule.check_callback_compilation(cb) then
                        cb.compile_extern_callback(v, ccu)
                end
 
-               for cb in foreign_callbacks.supers do if amainmodule.check_callback_compilation(cb) then
+               for cb in foreign_callbacks.supers do if mainmodule.check_callback_compilation(cb) then
                        cb.compile_extern_callback(v, ccu)
                end
 
-               for cb in foreign_callbacks.casts do if amainmodule.check_callback_compilation(cb) then
+               for cb in foreign_callbacks.casts do if mainmodule.check_callback_compilation(cb) then
                        cb.compile_extern_callbacks(v, ccu)
                end
 
                # manage nitni callback set
-               amodule.foreign_callbacks.join(foreign_callbacks)
+               mmodule.foreign_callbacks.join(foreign_callbacks)
        end
 end
 
index 5c46235..4d44800 100644 (file)
@@ -914,10 +914,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