1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
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.
29 import header_dependency
31 import c_compiler_options
37 # Does this module uses the FFI?
38 var uses_ffi
: Bool = false
40 # C compilation unit for the FFI files
41 private var ffi_ccu
: nullable CCompilationUnit = null
43 # Foreign language used in this AModule
44 private var present_languages
= new HashSet[FFILanguage]
46 # Complete the compilation of the FFI code
47 fun finalize_ffi_wrapper
(compdir
: String, mainmodule
: MModule)
49 for language
in ffi_callbacks
.keys
do
50 for callback
in ffi_callbacks
[language
] do
51 language
.compile_callback
(callback
, self, mainmodule
, ffi_ccu
.as(not null))
54 language
.compile_to_files
(self, compdir
)
57 # include dependancies FFI
58 for mod
in header_dependencies
do
59 if mod
.uses_ffi
then ffi_ccu
.header_custom
.add
("#include \"{mod.name}._ffi
.h\
"\n")
62 ffi_ccu
.write_as_impl
(self, compdir
)
63 for filename
in ffi_ccu
.files
do ffi_files
.add
(new ExternCFile(filename
, c_compiler_options
))
69 # Ensures all of the general foreign code of the module has been analyzed.
70 # Manages header blocks, extern class types and foreign dependancies between modules
71 fun ensure_compile_ffi_wrapper
74 if mmodule
== null or mmodule
.ffi_ccu
!= null then return
76 # ready extern code compiler
77 var ffi_ccu
= new CCompilationUnit
78 mmodule
.ffi_ccu
= ffi_ccu
81 for block
in n_extern_code_blocks
do
82 var language
= block
.language
83 assert language
!= null
84 mmodule
.present_languages
.add
(language
)
85 language
.compile_module_block
(block
, ffi_ccu
, mmodule
)
88 ffi_ccu
.header_c_base
.add
( "#include \"{mmodule.name}._nitni
.h\
"\n" )
90 ffi_ccu
.body_decl
.add
("#ifdef ANDROID\n")
91 ffi_ccu
.body_decl
.add
(" #include <android/log.h>\n")
92 ffi_ccu
.body_decl
.add
(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\
", __VA_ARGS__)\n")
93 ffi_ccu
.body_decl
.add
("#else\n")
94 ffi_ccu
.body_decl
.add
(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
95 ffi_ccu
.body_decl
.add
("#endif\n")
97 for nclassdef
in n_classdefs
do
98 # Does it declares an extern type?
99 if nclassdef
isa AStdClassdef and nclassdef
.n_extern_code_block
!= null then
100 mmodule
.uses_ffi
= true
101 var language
= nclassdef
.n_extern_code_block
.language
102 assert language
!= null
103 mmodule
.present_languages
.add
(language
)
104 nclassdef
.n_extern_code_block
.language
.compile_extern_class
(
105 nclassdef
.n_extern_code_block
.as(not null), nclassdef
, ffi_ccu
, mmodule
)
111 redef class AMethPropdef
112 private var ffi_has_been_compiled
= false
114 # Compile the necessary wrapper around this extern method or constructor
115 fun compile_ffi_method
(mmodule
: MModule)
117 assert n_extern_code_block
!= null
119 if ffi_has_been_compiled
then return
120 ffi_has_been_compiled
= true
122 var language
= n_extern_code_block
.language
123 assert language
!= null
124 mmodule
.present_languages
.add
(language
)
125 n_extern_code_block
.language
.compile_extern_method
(
126 n_extern_code_block
.as(not null), self, mmodule
.ffi_ccu
.as(not null), mmodule
)
130 redef class VerifyNitniCallbacksPhase
131 redef fun process_npropdef
(npropdef
)
135 if not npropdef
isa AExternPropdef then return
137 var code_block
= npropdef
.n_extern_code_block
138 if code_block
== null then return
140 var lang
= code_block
.language
143 # Associate callbacks used by an extern method to its foreign language
144 for callback
in npropdef
.foreign_callbacks
.all
do
145 var map
= npropdef
.mpropdef
.mclassdef
.mmodule
.ffi_callbacks
146 if not map
.keys
.has
(lang
) then map
[lang
] = new HashSet[NitniCallback]
147 map
[lang
].add
(callback
)