abstract_compiler: attach compiler and toolchain
[nit.git] / src / compiler / compiler_ffi / light.nit
index 345377a..01579b0 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# FFI support for the compilers
+# Light FFI support for the compiler
 module light
 
 intrude import abstract_compiler
 intrude import ffi::light_ffi
 
 redef class MModule
+       # `CCompilationUnit` used for nitni signatures and code specific to the compiler
        var nitni_ccu: nullable CCompilationUnit = null
 
        private fun nmodule(v: AbstractCompilerVisitor): nullable AModule
@@ -36,7 +37,7 @@ redef class MModule
                var n = nmodule(v)
                if n == null then return
                n.ensure_compile_ffi_wrapper
-               finalize_ffi_wrapper(v.compiler.modelbuilder.compile_dir, v.compiler.mainmodule)
+               finalize_ffi_wrapper(v.compiler.toolchain.compile_dir, v.compiler.mainmodule)
                for file in ffi_files do v.compiler.extern_bodies.add(file)
 
                ensure_compile_nitni_base(v)
@@ -48,7 +49,7 @@ extern void nitni_global_ref_decr(void*);
 """
 
                var cflags = self.cflags[""].join(" ")
-               nitni_ccu.write_as_nitni(self, v.compiler.modelbuilder.compile_dir)
+               nitni_ccu.write_as_nitni(self, v.compiler.toolchain.compile_dir)
 
                for file in nitni_ccu.files do
                        var f = new ExternCFile(file, cflags)
@@ -82,7 +83,6 @@ redef class AMethPropdef
        private fun compile_ffi_support_to_c(v: AbstractCompilerVisitor)
        do
                var mmodule = mpropdef.mclassdef.mmodule
-               var mainmodule = v.compiler.mainmodule
                var amodule = v.compiler.modelbuilder.mmodule2node(mmodule)
                var mclass_type = mpropdef.mclassdef.bound_mtype
 
@@ -98,11 +98,18 @@ redef class AMethPropdef
                mmodule.ensure_compile_nitni_base(v)
        end
 
+       # Should we compile the extern method `self`?
+       #
+       # Returns false when restricting to the light FFI on methods using callbacks.
+       fun accept_externmeth: Bool do return true
+
        redef fun compile_externmeth_to_c(v, mpropdef, arguments)
        do
                # if using the old native interface fallback on previous implementation
                if n_extern_code_block == null then return super
 
+               if not accept_externmeth then return false
+
                var mmodule = mpropdef.mclassdef.mmodule
                mmodule.uses_ffi = true
 
@@ -164,6 +171,8 @@ redef class AMethPropdef
                # if using the old native interface fallback on previous implementation
                if n_extern_code_block == null then return super
 
+               if not accept_externmeth then return false
+
                var mmodule = mpropdef.mclassdef.mmodule
                mmodule.uses_ffi = true
 
@@ -211,6 +220,7 @@ redef class AMethPropdef
 end
 
 redef class CCompilationUnit
+       # Compile a `_nitni` files, used to implement nitni features for the compiler
        fun write_as_nitni(mmodule: MModule, compdir: String)
        do
                var base_name = "{mmodule.c_name}._nitni"