src/ffi: restrict importations in extern_classes
[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 intrude import light_ffi
37
38 redef class MModule
39 # Complete the compilation of the FFI code
40 redef fun finalize_ffi_wrapper(compdir, mainmodule)
41 do
42 for language in ffi_callbacks.keys do
43 for callback in ffi_callbacks[language] do
44 language.compile_callback(callback, self, mainmodule, ffi_ccu.as(not null))
45 end
46
47 language.compile_to_files(self, compdir)
48 end
49
50 # include dependancies FFI
51 for mod in header_dependencies do
52 if mod.uses_ffi then ffi_ccu.header_custom.add("#include \"{mod.c_name}._ffi.h\"\n")
53 end
54
55 super
56 end
57 end
58
59
60 redef class VerifyNitniCallbacksPhase
61 redef fun process_npropdef(npropdef)
62 do
63 super
64
65 if not npropdef isa AMethPropdef then return
66
67 var code_block = npropdef.n_extern_code_block
68 if code_block == null then return
69
70 var lang = code_block.language
71 assert lang != null
72
73 # Associate callbacks used by an extern method to its foreign language
74 for callback in npropdef.foreign_callbacks.all do
75 var map = npropdef.mpropdef.mclassdef.mmodule.ffi_callbacks
76 if not map.keys.has(lang) then map[lang] = new HashSet[NitniCallback]
77 map[lang].add(callback)
78 end
79 end
80 end