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