ni_nitdoc: added fast copy past utility to signatures.
[nit.git] / src / ffi / ffi.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-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 # This module implements the FFI with different languages
18 module ffi
19
20 import c
21
22 redef class MMSrcModule
23 redef fun compile_separate_module(cprogram: CProgram)
24 do
25 super
26
27 if is_extern_hybrid then
28 var visitor = new FFIVisitor( cprogram.program.tc, self )
29 # TODO use cprogram to add generated files?
30
31 # actually compile stub
32 accept_ffi_visitor( visitor )
33
34 # write to file
35 if uses_ffi then
36 visitor.compile
37 end
38 end
39 end
40 end
41
42 redef class MMLocalClass
43 super FFIVisited
44 end
45
46 redef class FFIVisitor
47 fun compile
48 do
49 var compdir = tc.compdir.as(not null)
50 var base_name = "{mmmodule.cname}._ffi"
51 var c_file = "{base_name}.c"
52 var h_file = "{base_name}.h"
53
54 # header comments
55 var module_info = "/*\n\tExtern implementation of Nit module {mmmodule.name}\n*/\n"
56
57 # header file guard
58 var guard = "{mmmodule.cname.to_s.to_upper}_NIT_H"
59
60 # .h
61 var stream = new OFStream.open( "{compdir}/{h_file}" )
62 stream.write( module_info )
63 stream.write( "#include <{mmmodule.name}._nitni.h>\n\n" )
64 stream.write( "#ifndef {guard}\n" )
65 stream.write( "#define {guard}\n\n" )
66 compilation_unit.header_c_base.write_to_stream( stream )
67 compilation_unit.header_custom.write_to_stream( stream )
68 compilation_unit.header_c_types.write_to_stream( stream )
69 compilation_unit.header_decl.write_to_stream( stream )
70 stream.write( "#endif\n" )
71 stream.close
72
73 # .c
74 stream = new OFStream.open( "{compdir}/{c_file}" )
75 stream.write( module_info )
76 stream.write( "#include \"{h_file}\"\n" )
77 compilation_unit.body_decl.write_to_stream( stream )
78 compilation_unit.body_custom.write_to_stream( stream )
79 compilation_unit.body_impl.write_to_stream( stream )
80 stream.close
81 end
82 end