C FFI: fix line offset in gcc errors on custom header code
[nit.git] / src / ffi / c.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2014 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 # Support for nesting C code within a Nit program using its FFI
18 module c
19
20 import ffi_base
21
22 redef class FFILanguageAssignationPhase
23 var c_language: FFILanguage = new CLanguage(self)
24 end
25
26 class CLanguage
27 super FFILanguage
28
29 redef fun identify_language(n) do return n.is_c
30
31 redef fun compile_module_block(block, ecc, mmodule)
32 do
33 if block.is_c_header then
34 ecc.header_custom.add block.location.as_line_pragma
35 ecc.header_custom.add "\n"
36 ecc.header_custom.add block.code
37 else if block.is_c_body then
38 ecc.body_impl.add block.location.as_line_pragma
39 ecc.body_impl.add "\n"
40 ecc.body_impl.add block.code
41 end
42 end
43
44 redef fun compile_extern_method(block, m, ecc, mmodule)
45 do
46 var fc = new ExternCFunction(m, mmodule)
47 fc.decls.add( block.location.as_line_pragma )
48 fc.exprs.add( block.code )
49 ecc.add_exported_function( fc )
50 end
51
52 redef fun compile_extern_class(block, m, ecc, mmodule) do end
53
54 redef fun get_ftype(block, m) do return new ForeignCType(block.code)
55
56 redef fun compile_callback(callback, mmodule, mainmodule, ecc)
57 do
58 callback.compile_callback_to_c(mainmodule, ecc)
59 end
60 end
61
62 redef class AExternCodeBlock
63 fun is_c: Bool do return language_name == null or
64 language_name_lowered == "c" or language_name_lowered.has_prefix( "c " )
65
66 fun is_c_body: Bool do return language_name == null or
67 language_name_lowered == "c" or language_name_lowered == "c body"
68
69 fun is_c_header: Bool do return language_name_lowered == "c header"
70 end
71
72 redef class Location
73 fun as_line_pragma: String do return "#line {line_start-1} \"{file.filename}\"\n"
74 end
75
76 redef class MModule
77 var cflags = "" is writable
78 var ldflags = "" is writable
79
80 # Additional libraries needed for the compilation
81 # Will be used with pkg-config
82 var pkgconfigs = new Array[String]
83 end
84
85 class ForeignCType
86 super ForeignType
87
88 redef var ctype: String
89 end
90
91 redef class NitniCallback
92 fun compile_callback_to_c(mmodule: MModule, ffi_ccu: CCompilationUnit) do end
93 end
94
95 redef class Object
96 # Context when calling user C code from generated code
97 fun to_c_call_context: ToCCallContext do return once new ToCCallContext
98
99 # Context when calling generated code from user C code
100 fun from_c_call_context: FromCCallContext do return once new FromCCallContext
101 end
102
103 redef class MExplicitCall
104 redef fun compile_callback_to_c(mmodule, ffi_ccu)
105 do
106 var mproperty = mproperty.as(MMethod)
107
108 var full_cname = mproperty.build_cname(recv_mtype, mmodule, null, long_signature)
109 var friendly_cname = mproperty.build_cname(recv_mtype, mmodule, null, short_signature)
110 ffi_ccu.body_decl.add("#define {friendly_cname} {full_cname}\n")
111 end
112 end
113
114 # Context when calling user C code from generated code
115 class ToCCallContext
116 super CallContext
117
118 # TODO: private init because singleton instance (see `to_c_call_context`)
119
120 redef fun name_mtype(mtype)
121 do
122 if mtype isa MClassType and mtype.mclass.kind == extern_kind then return "void *"
123 return mtype.cname
124 end
125 end
126
127 # Context when calling generated code from user C code
128 class FromCCallContext
129 super CallContext
130
131 # TODO: private init because singleton instance (see `from_c_call_context`)
132
133 redef fun name_mtype(mtype) do return mtype.cname
134 end
135
136 class ExternCFunction
137 super CFunction
138
139 var method: AMethPropdef
140
141 init (method: AMethPropdef, mmodule: MModule)
142 do
143 self.method = method
144
145 var recv_mtype = method.mpropdef.mclassdef.bound_mtype
146 var csignature = method.mpropdef.mproperty.build_csignature(recv_mtype, mmodule, "___impl", long_signature, from_c_call_context)
147
148 super( csignature )
149 end
150 end