src/ffi: restrict importations in extern_classes
[nit.git] / src / ffi / light_ffi.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # FFI concers common between the compilers and the interpreter.
18 # Offers services to compile modules using foreign code. Mainly allows
19 # to wrap foreign code in Nit methods.
20 module light_ffi
21
22 import modelbuilder
23
24 import nitni::nitni_utilities
25
26 intrude import light_ffi_base
27 import extern_classes
28 import light_c
29
30 redef class MModule
31 # Does this module uses the FFI?
32 var uses_ffi: Bool = false
33
34 # C compilation unit for the FFI files
35 private var ffi_ccu: nullable CCompilationUnit = null
36
37 # Foreign language used in this AModule
38 private var present_languages = new HashSet[FFILanguage]
39
40 # Complete the compilation of the FFI code
41 fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
42 do
43 var cflags = self.cflags[""].join(" ")
44
45 ffi_ccu.write_as_impl(self, compdir)
46 for filename in ffi_ccu.files do
47 var f = new ExternCFile(filename, cflags)
48 f.pkgconfigs.add_all pkgconfigs
49 ffi_files.add(f)
50 end
51 end
52
53 # Avoid the compile a ffi propdef more than once
54 # See `AMethPropdef::compile_ffi_method`
55 # FIXME find a better way
56 private var compiled_ffi_methods = new HashSet[AMethPropdef]
57 end
58
59 redef class AModule
60
61 # Ensures all of the general foreign code of the module has been analyzed.
62 # Manages header blocks, extern class types and foreign dependancies between modules
63 fun ensure_compile_ffi_wrapper
64 do
65 var mmodule = mmodule
66 if mmodule == null or mmodule.ffi_ccu != null then return
67
68 # ready extern code compiler
69 var ffi_ccu = new CCompilationUnit
70 mmodule.ffi_ccu = ffi_ccu
71
72 # generate code
73 for block in n_extern_code_blocks do
74 var language = block.language
75 assert language != null
76 mmodule.present_languages.add(language)
77 language.compile_module_block(block, ffi_ccu, mmodule)
78 end
79
80 ffi_ccu.header_c_base.add( "#include \"{mmodule.c_name}._nitni.h\"\n" )
81
82 ffi_ccu.body_decl.add("#ifdef ANDROID\n")
83 ffi_ccu.body_decl.add(" #include <android/log.h>\n")
84 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)\n")
85 ffi_ccu.body_decl.add("#else\n")
86 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
87 ffi_ccu.body_decl.add("#endif\n")
88
89 for nclassdef in n_classdefs do
90 # Does it declares an extern type?
91 if nclassdef isa AStdClassdef and nclassdef.n_extern_code_block != null then
92 mmodule.uses_ffi = true
93 var language = nclassdef.n_extern_code_block.language
94 assert language != null
95 mmodule.present_languages.add(language)
96 nclassdef.n_extern_code_block.language.compile_extern_class(
97 nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, mmodule)
98 end
99 end
100 end
101 end
102
103 redef class AMethPropdef
104 # Compile the necessary wrapper around this extern method or constructor
105 fun compile_ffi_method(mmodule: MModule)
106 do
107 assert n_extern_code_block != null
108
109 if mmodule.compiled_ffi_methods.has(self) then return
110 mmodule.compiled_ffi_methods.add self
111
112 var language = n_extern_code_block.language
113 assert language != null
114 mmodule.present_languages.add(language)
115 n_extern_code_block.language.compile_extern_method(
116 n_extern_code_block.as(not null), self, mmodule.ffi_ccu.as(not null), mmodule)
117 end
118 end