329d9a36e18f760b9451880c898578bf11dd54d5
[nit.git] / src / ffi / c.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 # Handles C inline code
18 # Wraps all code in a thin wrapper.
19 module c
20
21 import ffi_base
22
23 redef class ExternCode
24 fun is_c : Bool do return language == null or
25 language_lowered == "c" or language_lowered.has_prefix( "c " )
26
27 fun is_c_body : Bool do return language == null or
28 language_lowered == "c" or language_lowered == "c body"
29
30 fun is_c_header : Bool do return language_lowered == "c header"
31
32 redef fun accept_ffi_visitor( v )
33 do
34 if is_c_header then
35 v.compilation_unit.header_custom.add( code )
36 else if is_c_body then
37 v.compilation_unit.body_custom.add( code )
38 end
39 end
40 end
41
42 redef class MMMethod
43 redef fun accept_ffi_visitor( v )
44 do
45 if extern_implementation.is_c then
46 var fc = new CFunction( impl_csignature )
47 fc.decls.add( extern_implementation.location.as_line_pragma )
48 fc.exprs.add( extern_implementation.code )
49 v.compilation_unit.add_exported_function( fc )
50 else
51 super
52 end
53 end
54 end
55
56 redef class Location
57 fun as_line_pragma : String
58 do
59 return "#line {line_start} \"{file.filename}\"\n"
60 end
61 end