nitc: FFI and nitni use MModule::c_name
[nit.git] / src / ffi / 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 ffi
21
22 import modelbuilder
23
24 import nitni
25
26 intrude import ffi_base
27 import extern_classes
28 import header_dependency
29 import pkgconfig
30 import c_compiler_options
31 import c
32 import cpp
33 import java
34 import extra_java_files
35 import objc
36
37 redef class MModule
38 # Does this module uses the FFI?
39 var uses_ffi: Bool = false
40
41 # C compilation unit for the FFI files
42 private var ffi_ccu: nullable CCompilationUnit = null
43
44 # Foreign language used in this AModule
45 private var present_languages = new HashSet[FFILanguage]
46
47 # Complete the compilation of the FFI code
48 fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
49 do
50 for language in ffi_callbacks.keys do
51 for callback in ffi_callbacks[language] do
52 language.compile_callback(callback, self, mainmodule, ffi_ccu.as(not null))
53 end
54
55 language.compile_to_files(self, compdir)
56 end
57
58 # include dependancies FFI
59 for mod in header_dependencies do
60 if mod.uses_ffi then ffi_ccu.header_custom.add("#include \"{mod.c_name}._ffi.h\"\n")
61 end
62
63 ffi_ccu.write_as_impl(self, compdir)
64 for filename in ffi_ccu.files do
65 var f = new ExternCFile(filename, c_compiler_options)
66 f.pkgconfigs.add_all pkgconfigs
67 ffi_files.add(f)
68 end
69 end
70
71 # Avoid the compile a ffi propdef more than once
72 # See `AMethPropdef::compile_ffi_method`
73 # FIXME find a better way
74 private var compiled_ffi_methods = new HashSet[AMethPropdef]
75 end
76
77 redef class AModule
78
79 # Ensures all of the general foreign code of the module has been analyzed.
80 # Manages header blocks, extern class types and foreign dependancies between modules
81 fun ensure_compile_ffi_wrapper
82 do
83 var mmodule = mmodule
84 if mmodule == null or mmodule.ffi_ccu != null then return
85
86 # ready extern code compiler
87 var ffi_ccu = new CCompilationUnit
88 mmodule.ffi_ccu = ffi_ccu
89
90 # generate code
91 for block in n_extern_code_blocks do
92 var language = block.language
93 assert language != null
94 mmodule.present_languages.add(language)
95 language.compile_module_block(block, ffi_ccu, mmodule)
96 end
97
98 ffi_ccu.header_c_base.add( "#include \"{mmodule.c_name}._nitni.h\"\n" )
99
100 ffi_ccu.body_decl.add("#ifdef ANDROID\n")
101 ffi_ccu.body_decl.add(" #include <android/log.h>\n")
102 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)\n")
103 ffi_ccu.body_decl.add("#else\n")
104 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
105 ffi_ccu.body_decl.add("#endif\n")
106
107 for nclassdef in n_classdefs do
108 # Does it declares an extern type?
109 if nclassdef isa AStdClassdef and nclassdef.n_extern_code_block != null then
110 mmodule.uses_ffi = true
111 var language = nclassdef.n_extern_code_block.language
112 assert language != null
113 mmodule.present_languages.add(language)
114 nclassdef.n_extern_code_block.language.compile_extern_class(
115 nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, mmodule)
116 end
117 end
118 end
119 end
120
121 redef class AMethPropdef
122 # Compile the necessary wrapper around this extern method or constructor
123 fun compile_ffi_method(mmodule: MModule)
124 do
125 assert n_extern_code_block != null
126
127 if mmodule.compiled_ffi_methods.has(self) then return
128 mmodule.compiled_ffi_methods.add self
129
130 var language = n_extern_code_block.language
131 assert language != null
132 mmodule.present_languages.add(language)
133 n_extern_code_block.language.compile_extern_method(
134 n_extern_code_block.as(not null), self, mmodule.ffi_ccu.as(not null), mmodule)
135 end
136 end
137
138 redef class VerifyNitniCallbacksPhase
139 redef fun process_npropdef(npropdef)
140 do
141 super
142
143 if not npropdef isa AMethPropdef then return
144
145 var code_block = npropdef.n_extern_code_block
146 if code_block == null then return
147
148 var lang = code_block.language
149 assert lang != null
150
151 # Associate callbacks used by an extern method to its foreign language
152 for callback in npropdef.foreign_callbacks.all do
153 var map = npropdef.mpropdef.mclassdef.mmodule.ffi_callbacks
154 if not map.keys.has(lang) then map[lang] = new HashSet[NitniCallback]
155 map[lang].add(callback)
156 end
157 end
158 end