Rename REAMDE to README.md
[nit.git] / src / ffi / light_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 # Light FFI support, independent of the compiler
18 #
19 # The light FFI offers only basic FFI features:
20 #
21 # * **Extern methods** implemented in C, nested within the Nit code.
22 # The body of these method is copied directly to the generated C files for compilation.
23 # Also supports extern `new` factories.
24 # * Module level **C code blocks**, both "C Body" (the default) and "C Header".
25 # They will be copied to the beginning of the generated C files.
26 # * Automatic transformation of Nit **primitive types** from/to their equivalent in C.
27 # * **Extern classes** to create a Nit class around a C pointer.
28 # Allows to specify the equivalent C type of the Nit extern class.
29 #
30 # These limited features should be easy to implement in new/alternative engines
31 # to quickly achieve a bootstrap. For this reason, core features of the Nit
32 # standard library should be limited to use the light FFI.
33 module light_ffi
34
35 import modelbuilder
36
37 import nitni::nitni_utilities
38
39 intrude import light_ffi_base
40 import extern_classes
41 import light_c
42
43 redef class MModule
44 # Does this module uses the FFI?
45 var uses_ffi: Bool = false
46
47 # C compilation unit for the FFI files
48 private var ffi_ccu: nullable CCompilationUnit = null
49
50 # Foreign language used in this AModule
51 private var present_languages = new HashSet[FFILanguage]
52
53 # Complete the compilation of the FFI code
54 fun finalize_ffi_wrapper(compdir: String, mainmodule: MModule)
55 do
56 var cflags = self.cflags[""].join(" ")
57
58 ffi_ccu.write_as_impl(self, compdir)
59 for filename in ffi_ccu.files do
60 var f = new ExternCFile(filename, cflags)
61 f.pkgconfigs.add_all pkgconfigs
62 ffi_files.add(f)
63 end
64 end
65
66 # Avoid the compile a ffi propdef more than once
67 # See `AMethPropdef::compile_ffi_method`
68 # FIXME find a better way
69 private var compiled_ffi_methods = new HashSet[AMethPropdef]
70 end
71
72 redef class AModule
73
74 # Ensures all of the general foreign code of the module has been analyzed.
75 # Manages header blocks, extern class types and foreign dependancies between modules
76 fun ensure_compile_ffi_wrapper
77 do
78 var mmodule = mmodule
79 if mmodule == null or mmodule.ffi_ccu != null then return
80
81 # ready extern code compiler
82 var ffi_ccu = new CCompilationUnit
83 mmodule.ffi_ccu = ffi_ccu
84
85 # generate code
86 for block in n_extern_code_blocks do
87 var language = block.language
88 assert language != null
89 mmodule.present_languages.add(language)
90 language.compile_module_block(block, ffi_ccu, mmodule)
91 end
92
93 ffi_ccu.header_c_base.add( "#include \"{mmodule.c_name}._nitni.h\"\n" )
94
95 ffi_ccu.body_decl.add("#ifdef ANDROID\n")
96 ffi_ccu.body_decl.add(" #include <android/log.h>\n")
97 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, \"Nit\", __VA_ARGS__)\n")
98 ffi_ccu.body_decl.add("#else\n")
99 ffi_ccu.body_decl.add(" #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)\n")
100 ffi_ccu.body_decl.add("#endif\n")
101
102 for nclassdef in n_classdefs do
103 # Does it declares an extern type?
104 if nclassdef isa AStdClassdef and nclassdef.n_extern_code_block != null then
105 mmodule.uses_ffi = true
106 var language = nclassdef.n_extern_code_block.language
107 assert language != null
108 mmodule.present_languages.add(language)
109 nclassdef.n_extern_code_block.language.compile_extern_class(
110 nclassdef.n_extern_code_block.as(not null), nclassdef, ffi_ccu, mmodule)
111 end
112 end
113 end
114 end
115
116 redef class AMethPropdef
117 # Compile the necessary wrapper around this extern method or constructor
118 fun compile_ffi_method(mmodule: MModule)
119 do
120 assert n_extern_code_block != null
121
122 if mmodule.compiled_ffi_methods.has(self) then return
123 mmodule.compiled_ffi_methods.add self
124
125 var language = n_extern_code_block.language
126 assert language != null
127 mmodule.present_languages.add(language)
128 n_extern_code_block.language.compile_extern_method(
129 n_extern_code_block.as(not null), self, mmodule.ffi_ccu.as(not null), mmodule)
130 end
131 end