Merge: Refactor FFI, the framework is now based on MModules
authorJean Privat <jean@pryen.org>
Tue, 13 May 2014 19:22:37 +0000 (15:22 -0400)
committerJean Privat <jean@pryen.org>
Tue, 13 May 2014 19:22:37 +0000 (15:22 -0400)
* Should fix #437 by supporting the use of the Java FFI across many modules in global compilation.
* Should also work (but we need an example for it): `nitg cross_platform_prog.nit -m android`

Pull-Request: #456
Reviewed-by: Jean Privat <jean@pryen.org>

1  2 
src/android_platform.nit
src/common_ffi/c.nit
src/common_ffi/common_ffi.nit
src/common_ffi/ffi_base.nit
src/common_ffi/java.nit
src/compiler_ffi.nit

diff --combined src/android_platform.nit
@@@ -65,7 -65,7 +65,7 @@@ en
  class AndroidToolchain
        super MakefileToolchain
  
 -      var android_project_root: String
 +      var android_project_root: nullable String = null
  
        redef fun compile_dir
        do
@@@ -76,7 -76,6 +76,7 @@@
  
        redef fun write_files(compiler, compile_dir, cfiles)
        do
 +              var android_project_root = android_project_root.as(not null)
                var project = toolcontext.modelbuilder.android_project_for(compiler.mainmodule)
                var short_project_name = compiler.mainmodule.name
  
@@@ -217,9 -216,14 +217,14 @@@ $(call import-module,android/native_app
  
                ### Link to assets (for mnit and others)
                # This will be accessed from `android_project_root`
-               var mainmodule_dir = compiler.mainmodule.location.file.filename.dirname
-               var assets_dir = "{mainmodule_dir}/../assets"
-               if not assets_dir.file_exists then assets_dir = "{mainmodule_dir}/assets"
+               var assets_dir
+               if compiler.mainmodule.location.file != null then
+                       # it is a real file, use "{file}/../assets"
+                       assets_dir = "{compiler.mainmodule.location.file.filename.dirname}/../assets"
+               else
+                       # probably used -m, use "."
+                       assets_dir = "assets"
+               end
                if assets_dir.file_exists then
                        assets_dir = assets_dir.realpath
                        var target_assets_dir = "{android_project_root}/assets"
  
        redef fun compile_c_code(compiler, compile_dir)
        do
 +              var android_project_root = android_project_root.as(not null)
                # Compile C code (and thus Nit)
                toolcontext.exec_and_check(["ndk-build", "-s", "-j", "4", "-C", android_project_root])
  
diff --combined src/common_ffi/c.nit
@@@ -28,7 -28,7 +28,7 @@@ class CLanguag
  
        redef fun identify_language(n) do return n.is_c
  
-       redef fun compile_module_block(block, ecc, nmodule)
+       redef fun compile_module_block(block, ecc, mmodule)
        do
                if block.is_c_header then
                        ecc.header_custom.add( block.location.as_line_pragma )
                end
        end
  
-       redef fun compile_extern_method(block, m, ecc, nmodule)
+       redef fun compile_extern_method(block, m, ecc, mmodule)
        do
-               var fc = new ExternCFunction(m, nmodule.mmodule.as(not null))
+               var fc = new ExternCFunction(m, mmodule.as(not null))
                fc.decls.add( block.location.as_line_pragma )
                fc.exprs.add( block.code )
                ecc.add_exported_function( fc )
        end
  
-       redef fun compile_extern_class(block, m, ecc, nmodule) do end
+       redef fun compile_extern_class(block, m, ecc, mmodule) do end
  
        redef fun get_ftype(block, m) do return new ForeignCType(block.code)
  
-       redef fun compile_callback(callback, nmodule, mmodule, ecc)
+       redef fun compile_callback(callback, mmodule, mainmodule, ecc)
        do
-               callback.compile_callback_to_c(mmodule, ecc)
+               callback.compile_callback_to_c(mainmodule, ecc)
        end
  end
  
@@@ -88,7 -88,7 +88,7 @@@ class ForeignCTyp
  end
  
  redef class NitniCallback
-       fun compile_callback_to_c(nmodule: MModule, ffi_ccu: CCompilationUnit) do end
+       fun compile_callback_to_c(mmodule: MModule, ffi_ccu: CCompilationUnit) do end
  end
  
  redef class Object
@@@ -135,9 -135,9 +135,9 @@@ en
  class ExternCFunction
        super CFunction
  
 -      var method: AExternPropdef
 +      var method: AMethPropdef
  
 -      init (method: AExternPropdef, mmodule: MModule)
 +      init (method: AMethPropdef, mmodule: MModule)
        do
                self.method = method
  
@@@ -36,31 -36,53 +36,53 @@@ import jav
  redef class MModule
        # Does this module uses the FFI?
        var uses_ffi: Bool = false
- end
  
- redef class AModule
        # C compilation unit for the FFI files
        private var ffi_ccu: nullable CCompilationUnit = null
  
        # Foreign language used in this AModule
        private var present_languages = new HashSet[FFILanguage]
  
+       # Complete the compilation of the FFI code
+       fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
+       do
+               for language in ffi_callbacks.keys do
+                       for callback in ffi_callbacks[language] do
+                               language.compile_callback(callback, self, mainmodule, ffi_ccu.as(not null))
+                       end
+                       language.compile_to_files(self, compdir)
+               end
+               # include dependancies FFI
+               for mod in header_dependencies do
+                       if mod.uses_ffi then ffi_ccu.header_custom.add("#include \"{mod.name}._ffi.h\"\n")
+               end
+               ffi_ccu.write_as_impl(self, compdir)
+               for filename in ffi_ccu.files do ffi_files.add(new ExternCFile(filename, c_compiler_options))
+       end
+ end
+ redef class AModule
        # Ensures all of the general foreign code of the module has been analyzed.
        # Manages header blocks, extern class types and foreign dependancies between modules
        fun ensure_compile_ffi_wrapper
        do
-               if ffi_ccu != null then return
+               var mmodule = mmodule
+               if mmodule == null or mmodule.ffi_ccu != null then return
  
                # ready extern code compiler
                var ffi_ccu = new CCompilationUnit
-               self.ffi_ccu = ffi_ccu
+               mmodule.ffi_ccu = ffi_ccu
  
                # generate code
                for block in n_extern_code_blocks do
                        var language = block.language
                        assert language != null
-                       present_languages.add(language)
-                       language.compile_module_block(block, ffi_ccu, self)
+                       mmodule.present_languages.add(language)
+                       language.compile_module_block(block, ffi_ccu, mmodule)
                end
  
                ffi_ccu.header_c_base.add( "#include \"{mmodule.name}._nitni.h\"\n" )
                                mmodule.uses_ffi = true
                                var language = nclassdef.n_extern_code_block.language
                                assert language != null
-                               present_languages.add(language)
+                               mmodule.present_languages.add(language)
                                nclassdef.n_extern_code_block.language.compile_extern_class(
-                                       nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, self)
-                       end
-               end
-       end
-       # Complete the compilation of the FFI code
-       fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
-       do
-               ensure_compile_ffi_wrapper
-               for language in present_languages do if ffi_callbacks.keys.has(language) then
-                       for callback in ffi_callbacks[language] do
-                               language.compile_callback(callback, self, mainmodule, ffi_ccu.as(not null))
+                                       nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, mmodule)
                        end
-                       language.compile_to_files(self, compdir)
-               end
-               # include dependancies FFI
-               for mod in mmodule.header_dependencies do
-                       if mod.uses_ffi then ffi_ccu.header_custom.add("#include \"{mod.name}._ffi.h\"\n")
                end
        end
  end
  
 -redef class AExternPropdef
 +redef class AMethPropdef
        private var ffi_has_been_compiled = false
  
        # Compile the necessary wrapper around this extern method or constructor
-       fun compile_ffi_method(amodule: AModule)
+       fun compile_ffi_method(mmodule: MModule)
        do
                assert n_extern_code_block != null
  
                if ffi_has_been_compiled then return
                ffi_has_been_compiled = true
  
-               amodule.ensure_compile_ffi_wrapper
                var language = n_extern_code_block.language
                assert language != null
-               amodule.present_languages.add(language)
+               mmodule.present_languages.add(language)
                n_extern_code_block.language.compile_extern_method(
-                       n_extern_code_block.as(not null), self, amodule.ffi_ccu.as(not null), amodule)
+                       n_extern_code_block.as(not null), self, mmodule.ffi_ccu.as(not null), mmodule)
        end
  end
  
@@@ -137,7 -135,7 +135,7 @@@ redef class VerifyNitniCallbacksPhas
  
                # Associate callbacks used by an extern method to its foreign language
                for callback in npropdef.foreign_callbacks.all do
-                       var map = npropdef.parent.parent.as(AModule).ffi_callbacks
+                       var map = npropdef.mpropdef.mclassdef.mmodule.ffi_callbacks
                        if not map.keys.has(lang) then map[lang] = new HashSet[NitniCallback]
                        map[lang].add(callback)
                end
@@@ -41,7 -41,7 +41,7 @@@ class FFILanguageAssignationPhas
  
        redef fun process_npropdef(npropdef)
        do
 -              if npropdef isa AExternPropdef then
 +              if npropdef isa AMethPropdef then
                        var code_block = npropdef.n_extern_code_block
                        if code_block != null then
                                verify_foreign_code_on_node( code_block )
@@@ -74,7 -74,7 +74,7 @@@
        end
  end
  
- redef class AModule
+ redef class MModule
        var ffi_files = new Array[ExternFile]
  
        # Callbacks used by this module, classified by language
@@@ -108,25 -108,25 +108,25 @@@ class FFILanguag
        fun identify_language(block: AExternCodeBlock ): Bool is abstract
  
        # Generate wrapper code for this module/header code block
-       fun compile_module_block(block: AExternCodeBlock, ecc: CCompilationUnit, nmodule: AModule) is abstract
+       fun compile_module_block(block: AExternCodeBlock, ecc: CCompilationUnit, mmodule: MModule) is abstract
  
        # Generate wrapper code for this extern method
 -      fun compile_extern_method(block: AExternCodeBlock, m: AExternPropdef,
 -              ecc: CCompilationUnit, mmodule: MModule) is abstract
 +      fun compile_extern_method(block: AExternCodeBlock, m: AMethPropdef,
-               ecc: CCompilationUnit, nmodule: AModule) is abstract
++              ecc: CCompilationUnit, nmodule: MModule) is abstract
  
        # Generate wrapper code for this extern class
        fun compile_extern_class(block: AExternCodeBlock, m: AClassdef,
-               ecc: CCompilationUnit, nmodule: AModule) is abstract
+               ecc: CCompilationUnit, mmodule: MModule) is abstract
  
        # Get the foreign type of this extern class definition
        fun get_ftype(block: AExternCodeBlock, m: AClassdef): ForeignType is abstract
  
        # Generate the code to offer this callback if foreign code
-       fun compile_callback(callback: NitniCallback, nmodule: AModule,
+       fun compile_callback(callback: NitniCallback, mmodule: MModule,
                mainmmodule: MModule, ecc: CCompilationUnit) is abstract
  
        # Complete compilation of generated code
-       fun compile_to_files(amodule: AModule, directory: String) do end
+       fun compile_to_files(mmodule: MModule, directory: String) do end
  end
  
  redef class TString
@@@ -148,26 -148,26 +148,26 @@@ redef class TExternCodeSegmen
  end
  
  redef class CCompilationUnit
-       fun write_as_impl( amodule: AModule, compdir: String )
+       fun write_as_impl(mmodule: MModule, compdir: String)
        do
-               var base_name = "{amodule.mmodule.name}._ffi"
+               var base_name = "{mmodule.name}._ffi"
  
                var h_file = "{base_name}.h"
-               var guard = "{amodule.cname.to_s.to_upper}_NIT_H"
-               write_header_to_file( amodule, "{compdir}/{h_file}", new Array[String], guard)
+               var guard = "{mmodule.cname.to_s.to_upper}_NIT_H"
+               write_header_to_file(mmodule, "{compdir}/{h_file}", new Array[String], guard)
  
                var c_file = "{base_name}.c"
-               write_body_to_file( amodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "\"{h_file}\""] )
+               write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "\"{h_file}\""])
  
                files.add( "{compdir}/{c_file}" )
        end
  
-       fun write_header_to_file(amodule: AModule, file: String, includes: Array[String], guard: String)
+       fun write_header_to_file(mmodule: MModule, file: String, includes: Array[String], guard: String)
        do
                var stream = new OFStream.open( file )
  
                # header comments
-               var module_info = "/*\n\tExtern implementation of Nit module {amodule.mmodule.name}\n*/\n"
+               var module_info = "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"
  
                stream.write( module_info )
  
                stream.close
        end
  
-       fun write_body_to_file(amodule: AModule, file: String, includes: Array[String])
+       fun write_body_to_file(mmodule: MModule, file: String, includes: Array[String])
        do
                var stream = new OFStream.open(file)
  
-               var module_info = "/*\n\tExtern implementation of Nit module {amodule.mmodule.name}\n*/\n"
+               var module_info = "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"
  
                stream.write( module_info )
                for incl in includes do stream.write( "#include {incl}\n" )
diff --combined src/common_ffi/java.nit
@@@ -32,23 -32,22 +32,22 @@@ class JavaLanguag
  
        redef fun identify_language(n) do return n.is_java
  
-       redef fun compile_module_block(block, ccu, nmodule)
+       redef fun compile_module_block(block, ccu, mmodule)
        do
-               nmodule.ensure_java_files
-               var java_file = nmodule.java_file
+               mmodule.ensure_java_files
+               var java_file = mmodule.java_file
                assert java_file != null
  
                java_file.header.add(block.code)
        end
  
-       redef fun compile_extern_method(block, m, ccu, nmodule)
+       redef fun compile_extern_method(block, m, ccu, mmodule)
        do
                ffi_ccu = ccu
-               nmodule.ensure_java_files
-               var java_file = nmodule.java_file
+               mmodule.ensure_java_files
+               var java_file = mmodule.java_file
                assert java_file != null
  
-               var mmodule = nmodule.mmodule.as(not null)
                var mclass_type = m.parent.as(AClassdef).mclass.mclass_type
                var mmethodef = m.mpropdef
                var mproperty = m.mpropdef.mproperty
        jmethodID java_meth_id;
  
        // retrieve the current JVM
-       Sys sys = {{{mmodule.name}}}___Pointer_sys(NULL);
-       JNIEnv *nit_ffi_jni_env = {{{mmodule.name}}}___Sys_jni_env(sys);
+       Sys sys = Pointer_sys(NULL);
+       JNIEnv *nit_ffi_jni_env = Sys_jni_env(sys);
  
        // retrieve the implementation Java class
-       java_class = {{{mmodule.name}}}___Sys_load_jclass(sys, "{{{mmodule.impl_java_class_name}}}");
+       java_class = Sys_load_jclass(sys, "{{{mmodule.impl_java_class_name}}}");
        if (java_class == NULL) {
                fprintf(stderr, "Nit FFI with Java error: failed to load class.\\n");
                (*nit_ffi_jni_env)->ExceptionDescribe(nit_ffi_jni_env);
  
                # Java implementation function in Java
                var java_csig = mproperty.build_csignature(mclass_type, mmodule, "___java_impl", long_signature, java_call_context)
-               nmodule.java_file.class_content.add """
+               mmodule.java_file.class_content.add """
        public static {{{java_csig}}} {
                // from Nit FII at: {{{block.location}}}
                {{{block.code}}}
  """
        end
  
-       redef fun compile_extern_class(block, m, ccu, nmodule) do end
+       redef fun compile_extern_class(block, m, ccu, mmodule) do end
  
        redef fun get_ftype(block, m) do return new ForeignJavaType(block.code)
  
-       redef fun compile_to_files(nmodule, compdir)
+       redef fun compile_to_files(mmodule, compdir)
        do
                # Make sure we have a .java file
-               nmodule.ensure_java_files
+               mmodule.ensure_java_files
  
                # Needed compiler and linker options
-               nmodule.insert_compiler_options
+               mmodule.insert_compiler_options
  
                # Enable linking C callbacks to java native methods
-               nmodule.ensure_linking_callback_methods(ffi_ccu.as(not null), nmodule.ffi_callbacks[self])
 -              mmodule.ensure_linking_callback_methods(ffi_ccu, mmodule.ffi_callbacks[self])
++              mmodule.ensure_linking_callback_methods(ffi_ccu.as(not null), mmodule.ffi_callbacks[self])
  
                # Java implementation code
-               var java_file = nmodule.java_file
+               var java_file = mmodule.java_file
                assert java_file != null
                var extern_java_file = java_file.write_to_files(compdir)
-               nmodule.ffi_files.add(extern_java_file)
+               mmodule.ffi_files.add(extern_java_file)
        end
  
 -      var ffi_ccu: CCompilationUnit # HACK
 +      var ffi_ccu: nullable CCompilationUnit = null # HACK
  
-       redef fun compile_callback(callback, nmodule, mainmodule, ccu)
+       redef fun compile_callback(callback, mmodule, mainmodule, ccu)
        do
                ffi_ccu = ccu
-               callback.compile_callback_to_java(nmodule, ccu)
+               callback.compile_callback_to_java(mmodule, mainmodule, ccu)
        end
  end
  
- redef class AModule
+ redef class MModule
  
        # Pure java class source file
        private var java_file: nullable JavaClassTemplate = null
                if java_file != null then return
  
                # Java implementation code
-               java_file = new JavaClassTemplate(mmodule.impl_java_class_name)
+               java_file = new JavaClassTemplate(impl_java_class_name)
        end
  
        # Compile C code to call JNI and link C callbacks implementations to Java extern methods
  
                var jni_methods = new Array[String]
                for cb in callbacks do
-                       jni_methods.add_all(cb.jni_methods_declaration(mmodule.as(not null)))
+                       jni_methods.add_all(cb.jni_methods_declaration(self))
                end
  
                var cf = new CFunction("static void nit_ffi_with_java_register_natives(JNIEnv* env, jclass jclazz)")
        # Tell the C compiler where to find jni.h and how to link with libjvm
        private fun insert_compiler_options
        do
-               mmodule.c_compiler_options = "{mmodule.c_compiler_options} -I $(JAVA_HOME)/include/"
-               mmodule.c_linker_options = "{mmodule.c_linker_options} -L $(JNI_LIB_PATH) -ljvm"
+               c_compiler_options = "{c_compiler_options} -I $(JAVA_HOME)/include/"
+               c_linker_options = "{c_linker_options} -L $(JNI_LIB_PATH) -ljvm"
        end
- end
  
- redef class MModule
        # Name of the generated Java class where to store all implementation methods of this module
        # as well as generated callbacks.
        private fun impl_java_class_name: String do return "Nit_{name}"
@@@ -273,6 -270,12 +270,12 @@@ redef class AExternPropde
                var modelbuilder = toolcontext.modelbuilder
                var mmodule = mpropdef.mclassdef.mmodule
  
+               # We use callbacks from the C FFI since they will be called from generated C
+               var c_language_visitor = toolcontext.ffi_language_assignation_phase.as(FFILanguageAssignationPhase).c_language
+               if not mmodule.ffi_callbacks.keys.has(c_language_visitor) then
+                       mmodule.ffi_callbacks[c_language_visitor] = new HashSet[NitniCallback]
+               end
                # Pointer::sys
                var pointer_class = modelbuilder.try_get_mclass_by_name(self, mmodule, "Pointer")
                assert pointer_class != null
  
                var explicit_call = new MExplicitCall(pointer_class.mclass_type, pointer_sys_meth, mmodule)
                fcc.callbacks.add(explicit_call)
-               explicit_call.fill_type_for(fcc, mmodule)
+               mmodule.ffi_callbacks[c_language_visitor].add(explicit_call)
  
                # Sys::jni_env
                var sys_class = modelbuilder.try_get_mclass_by_name(self, mmodule, "Sys")
  
                explicit_call = new MExplicitCall(sys_class.mclass_type, sys_jni_env_meth, mmodule)
                fcc.callbacks.add(explicit_call)
-               explicit_call.fill_type_for(fcc, mmodule)
+               mmodule.ffi_callbacks[c_language_visitor].add(explicit_call)
  
                # Sys::load_jclass
                var sys_jni_load_jclass_meth = modelbuilder.try_get_mproperty_by_name2(self, mmodule, sys_class.mclass_type, "load_jclass")
  
                explicit_call = new MExplicitCall(sys_class.mclass_type, sys_jni_load_jclass_meth, mmodule)
                fcc.callbacks.add(explicit_call)
+               mmodule.ffi_callbacks[c_language_visitor].add(explicit_call)
                explicit_call.fill_type_for(fcc, mmodule)
        end
  end
@@@ -384,33 -388,32 +388,32 @@@ en
  
  redef class NitniCallback
        # Compile C and Java code to implement this callback
-       fun compile_callback_to_java(nmodule: AModule, ccu: CCompilationUnit) do end
+       fun compile_callback_to_java(mmodule: MModule, mainmodule: MModule, ccu: CCompilationUnit) do end
  
        # Returns the list of C functions to link with extern Java methods, as required
        # to enable this callback from Java code.
        # 
-       # Return used by `AModule::ensure_linking_callback_methods`
+       # Return used by `MModule::ensure_linking_callback_methods`
        #
        # TODO we return an Array to support cast and other features like that
        fun jni_methods_declaration(from_module: MModule): Array[String] do return new Array[String]
  end
  
  redef class MExplicitCall
-       redef fun compile_callback_to_java(nmodule, ccu)
+       redef fun compile_callback_to_java(mmodule, mainmodule, ccu)
        do
                var mproperty = mproperty
                assert mproperty isa MMethod
-               var mmodule = nmodule.mmodule.as(not null)
  
                # In C, indirection implementing the Java extern methods
                var csignature = mproperty.build_c_implementation_signature(recv_mtype, mmodule, "___indirect", long_signature, from_java_call_context)
                var cf = new CFunction("JNIEXPORT {csignature}")
-               cf.exprs.add "\t{mproperty.build_ccall(recv_mtype, mmodule, null, long_signature, from_java_call_context, null)}\n"
+               cf.exprs.add "\t{mproperty.build_ccall(recv_mtype, mainmodule, null, long_signature, from_java_call_context, null)}\n"
                ccu.add_local_function cf
  
                # In Java, declare the extern method as a private static local method
-               var java_signature = mproperty.build_csignature(recv_mtype, mmodule, null, short_signature, java_call_context)
-               nmodule.java_file.class_content.add "private native static {java_signature};\n"
+               var java_signature = mproperty.build_csignature(recv_mtype, mainmodule, null, short_signature, java_call_context)
+               mmodule.java_file.class_content.add "private native static {java_signature};\n"
        end
  
        redef fun jni_methods_declaration(from_mmodule)
diff --combined src/compiler_ffi.nit
@@@ -38,21 -38,22 +38,22 @@@ redef class MModul
                var v = compiler.new_visitor
                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)
+               n.ensure_compile_ffi_wrapper
+               finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
+               for file in ffi_files do v.compiler.extern_bodies.add(file)
  
                ensure_compile_nitni_base(v)
  
                nitni_ccu.header_c_types.add("#include \"{name}._ffi.h\"\n")
  
-               nitni_ccu.write_as_nitni(n, v.compiler.modelbuilder.compile_dir)
+               nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir)
  
                for file in nitni_ccu.files do
                        v.compiler.extern_bodies.add(new ExternCFile(file, c_compiler_options))
                end
        end
  
-       fun ensure_compile_nitni_base(v: AbstractCompilerVisitor)
+       private fun ensure_compile_nitni_base(v: AbstractCompilerVisitor)
        do
                if nitni_ccu != null then return
  
                return res
        end
  
-       var compiled_callbacks: Array[NitniCallback] = new Array[NitniCallback]
+       private var compiled_callbacks: Array[NitniCallback] = new Array[NitniCallback]
  
        # Returns true if callbacks has yet to be generated and register it as being generated
-       fun check_callback_compilation(cb: NitniCallback): Bool
+       private fun check_callback_compilation(cb: NitniCallback): Bool
        do
                var compiled = compiled_callbacks.has(cb)
                if not compiled then compiled_callbacks.add(cb)
@@@ -79,8 -80,8 +80,8 @@@
        end
  end
  
 -redef class AExternPropdef
 +redef class AMethPropdef
-       fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
+       private fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
        do
                var mmodule = mpropdef.mclassdef.mmodule
                var mainmodule = v.compiler.mainmodule
                v.declare_once("{csignature};")
  
                # FFI part
-               compile_ffi_method(amodule)
+               amodule.ensure_compile_ffi_wrapper
+               compile_ffi_method(mmodule)
  
 +              assert self isa AExternPropdef
 +
                # nitni - Compile missing callbacks
                mmodule.ensure_compile_nitni_base(v)
                var ccu = mmodule.nitni_ccu.as(not null)
                # manage nitni callback set
                mmodule.foreign_callbacks.join(foreign_callbacks)
        end
 -end
  
 -redef class AExternMethPropdef
 -      redef fun compile_to_c(v, mpropdef, arguments)
 +      redef fun compile_externmeth_to_c(v, mpropdef, arguments)
        do
                var mmodule = mpropdef.mclassdef.mmodule
-               var amodule = v.compiler.modelbuilder.mmodule2nmodule[mmodule]
  
                # if using the old native interface fallback on previous implementation
                var nextern = self.n_extern
                        return
                end
  
-               amodule.mmodule.uses_ffi = true
+               mmodule.uses_ffi = true
  
                var mclass_type = mpropdef.mclassdef.bound_mtype
  
  
                compile_ffi_support_to_c(v)
        end
 -end
  
 -redef class AExternInitPropdef
 -      redef fun compile_to_c(v, mpropdef, arguments)
 +      redef fun compile_externinit_to_c(v, mpropdef, arguments)
        do
                var mmodule = mpropdef.mclassdef.mmodule
-               var amodule = v.compiler.modelbuilder.mmodule2nmodule[mmodule]
  
                # if using the old native interface fallback on previous implementation
                var nextern = self.n_extern
                        return
                end
  
-               amodule.mmodule.uses_ffi = true
+               mmodule.uses_ffi = true
                var mclass_type = mpropdef.mclassdef.bound_mtype
  
                var externname = mpropdef.mproperty.build_cname(mpropdef.mclassdef.bound_mtype, mmodule, "___impl", long_signature)
  end
  
  redef class CCompilationUnit
-       fun write_as_nitni(amodule: AModule, compdir: String)
+       fun write_as_nitni(mmodule: MModule, compdir: String)
        do
-               var base_name = "{amodule.mmodule.name}._nitni"
+               var base_name = "{mmodule.name}._nitni"
  
                var h_file = "{base_name}.h"
-               write_header_to_file( amodule, "{compdir}/{h_file}", new Array[String],
-                       "{amodule.cname.to_s.to_upper}_NITG_NITNI_H")
+               write_header_to_file( mmodule, "{compdir}/{h_file}", new Array[String],
+                       "{mmodule.cname.to_s.to_upper}_NITG_NITNI_H")
  
                var c_file = "{base_name}.c"
-               write_body_to_file( amodule, "{compdir}/{c_file}", ["\"{h_file}\""] )
+               write_body_to_file( mmodule, "{compdir}/{c_file}", ["\"{h_file}\""] )
  
                files.add( "{compdir}/{c_file}" )
        end
@@@ -286,7 -289,7 +287,7 @@@ redef class AbstractCompilerVisito
  end
  
  redef class MType
-       fun compile_extern_type(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
+       private fun compile_extern_type(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
        do
                assert not is_cprimitive
  
                ccu.header_c_types.add("#endif\n")
        end
  
-       fun compile_extern_helper_functions(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
+       private fun compile_extern_helper_functions(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
        do
                # actually, we do not need to do anything when using the bohem garbage collector
  
@@@ -348,7 -351,7 +349,7 @@@ redef class MNullableTyp
  end
  
  redef class MExplicitCall
-       fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
+       private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
        do
                var mproperty = mproperty
                assert mproperty isa MMethod
  end
  
  redef class MExplicitSuper
-       fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
+       private fun compile_extern_callback(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
        do
                var mproperty = from.mproperty
                assert mproperty isa MMethod
  end
  
  redef class MExplicitCast
-       fun compile_extern_callbacks(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
+       private fun compile_extern_callbacks(v: AbstractCompilerVisitor, ccu: CCompilationUnit)
        do
                var from = from
                var to = to