1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-2014 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 # Support for nesting C code within a Nit program using its FFI
22 redef class FFILanguageAssignationPhase
23 var c_language
: FFILanguage = new CLanguage(self)
29 redef fun identify_language
(n
) do return n
.is_c
31 redef fun compile_module_block
(block
, ecc
, mmodule
)
33 if block
.is_c_header
then
34 ecc
.header_custom
.add
( block
.location
.as_line_pragma
)
35 ecc
.header_custom
.add
( block
.code
)
36 else if block
.is_c_body
then
37 ecc
.body_custom
.add
( block
.location
.as_line_pragma
)
38 ecc
.body_impl
.add
( block
.code
)
42 redef fun compile_extern_method
(block
, m
, ecc
, mmodule
)
44 var fc
= new ExternCFunction(m
, mmodule
)
45 fc
.decls
.add
( block
.location
.as_line_pragma
)
46 fc
.exprs
.add
( block
.code
)
47 ecc
.add_exported_function
( fc
)
50 redef fun compile_extern_class
(block
, m
, ecc
, mmodule
) do end
52 redef fun get_ftype
(block
, m
) do return new ForeignCType(block
.code
)
54 redef fun compile_callback
(callback
, mmodule
, mainmodule
, ecc
)
56 callback
.compile_callback_to_c
(mainmodule
, ecc
)
60 redef class AExternCodeBlock
61 fun is_c
: Bool do return language_name
== null or
62 language_name_lowered
== "c" or language_name_lowered
.has_prefix
( "c " )
64 fun is_c_body
: Bool do return language_name
== null or
65 language_name_lowered
== "c" or language_name_lowered
== "c body"
67 fun is_c_header
: Bool do return language_name_lowered
== "c header"
71 fun as_line_pragma
: String do return "#line {line_start-1} \"{file.filename}\
"\n"
75 var c_compiler_options
= "" is writable
76 var c_linker_options
= "" is writable
78 # Additional libraries needed for the compilation
79 # Will be used with pkg-config
80 var pkgconfigs
= new Array[String]
86 redef var ctype
: String
89 redef class NitniCallback
90 fun compile_callback_to_c
(mmodule
: MModule, ffi_ccu
: CCompilationUnit) do end
94 # Context when calling user C code from generated code
95 fun to_c_call_context
: ToCCallContext do return once
new ToCCallContext
97 # Context when calling generated code from user C code
98 fun from_c_call_context
: FromCCallContext do return once
new FromCCallContext
101 redef class MExplicitCall
102 redef fun compile_callback_to_c
(mmodule
, ffi_ccu
)
104 var mproperty
= mproperty
.as(MMethod)
106 var full_cname
= mproperty
.build_cname
(recv_mtype
, mmodule
, null, long_signature
)
107 var friendly_cname
= mproperty
.build_cname
(recv_mtype
, mmodule
, null, short_signature
)
108 ffi_ccu
.body_decl
.add
("#define {friendly_cname} {full_cname}\n")
112 # Context when calling user C code from generated code
116 # TODO: private init because singleton instance (see `to_c_call_context`)
118 redef fun name_mtype
(mtype
)
120 if mtype
isa MClassType and mtype
.mclass
.kind
== extern_kind
then return "void *"
125 # Context when calling generated code from user C code
126 class FromCCallContext
129 # TODO: private init because singleton instance (see `from_c_call_context`)
131 redef fun name_mtype
(mtype
) do return mtype
.cname
134 class ExternCFunction
137 var method
: AMethPropdef
139 init (method
: AMethPropdef, mmodule
: MModule)
143 var recv_mtype
= method
.mpropdef
.mclassdef
.bound_mtype
144 var csignature
= method
.mpropdef
.mproperty
.build_csignature
(recv_mtype
, mmodule
, "___impl", long_signature
, from_c_call_context
)