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