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