1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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 # Tools and utilities for language targetted FFI implementations
25 redef class ToolContext
26 var ffi_language_assignation_phase
: Phase = new FFILanguageAssignationPhase(self, null)
29 class FFILanguageAssignationPhase
32 # All supported languages
33 var languages
= new Array[FFILanguage]
35 redef fun process_nmodule
(nmodule
)
37 for block
in nmodule
.n_extern_code_blocks
do
38 verify_foreign_code_on_node
( block
)
42 redef fun process_npropdef
(npropdef
)
44 if npropdef
isa AMethPropdef then
45 var code_block
= npropdef
.n_extern_code_block
46 if code_block
!= null then
47 verify_foreign_code_on_node
( code_block
)
52 redef fun process_nclassdef
(nclassdef
)
54 if nclassdef
isa AStdClassdef and nclassdef
.n_extern_code_block
!= null then
55 verify_foreign_code_on_node
( nclassdef
.n_extern_code_block
.as(not null) )
59 private fun verify_foreign_code_on_node
(n
: AExternCodeBlock)
63 var identified
= v
.identify_language
(n
)
65 if found
and identified
then
66 toolcontext
.error
(n
.location
, "Two languages identified as possible handlers.")
73 if not found
then toolcontext
.error
(n
.location
, "Unsupported language.")
78 var ffi_files
= new Array[ExternFile]
80 # Callbacks used by this module, classified by language
81 var ffi_callbacks
= new HashMap[FFILanguage, Set[NitniCallback]]
84 redef class AExternCodeBlock
85 fun language_name
: nullable String do
86 if n_in_language
== null then return null
87 return n_in_language
.n_string
.without_quotes
89 fun language_name_lowered
: nullable String do
90 if language_name
== null then return null
91 return language_name
.to_lower
94 fun code
: String do return n_extern_code_segment
.without_guard
96 var language
: nullable FFILanguage = null
99 # Visitor for a specific languages. Works kinda like a `Phase` and is executed
102 var ffi_language_assignation_phase
: FFILanguageAssignationPhase
106 ffi_language_assignation_phase
.languages
.add
(self)
109 # Is this `block` written in this language?
110 fun identify_language
(block
: AExternCodeBlock ): Bool is abstract
112 # Generate wrapper code for this module/header code block
113 fun compile_module_block
(block
: AExternCodeBlock, ecc
: CCompilationUnit, mmodule
: MModule) is abstract
115 # Generate wrapper code for this extern method
116 fun compile_extern_method
(block
: AExternCodeBlock, m
: AMethPropdef,
117 ecc
: CCompilationUnit, nmodule
: MModule) is abstract
119 # Generate wrapper code for this extern class
120 fun compile_extern_class
(block
: AExternCodeBlock, m
: AClassdef,
121 ecc
: CCompilationUnit, mmodule
: MModule) is abstract
123 # Get the foreign type of this extern class definition
124 fun get_ftype
(block
: AExternCodeBlock, m
: AClassdef): ForeignType is abstract
126 # Generate the code to offer this callback if foreign code
127 fun compile_callback
(callback
: NitniCallback, mmodule
: MModule,
128 mainmmodule
: MModule, ecc
: CCompilationUnit) is abstract
130 # Complete compilation of generated code
131 fun compile_to_files
(mmodule
: MModule, directory
: String) do end
135 # Returns the content of this node without both of the surrounding "
136 fun without_quotes
: String
138 assert text
.length
>= 2
139 return text
.substring
(1, text
.length-2
)
143 redef class TExternCodeSegment
144 # Returns the content of this node without the surrounding `{ and `}
145 fun without_guard
: String
147 assert text
.length
>= 4
148 return text
.substring
(2, text
.length-4
)
152 redef class CCompilationUnit
153 fun write_as_impl
(mmodule
: MModule, compdir
: String)
155 var base_name
= "{mmodule.name}._ffi"
157 var h_file
= "{base_name}.h"
158 var guard
= "{mmodule.cname.to_s.to_upper}_NIT_H"
159 write_header_to_file
(mmodule
, "{compdir}/{h_file}", new Array[String], guard
)
161 var c_file
= "{base_name}.c"
162 write_body_to_file
(mmodule
, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "\"{h_file}\
""])
164 files
.add
( "{compdir}/{c_file}" )
167 fun write_header_to_file
(mmodule
: MModule, file
: String, includes
: Array[String], guard
: String)
169 var stream
= new OFStream.open
( file
)
172 var module_info
= "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"
174 stream
.write
( module_info
)
176 stream
.write
( "#ifndef {guard}\n" )
177 stream
.write
( "#define {guard}\n\n" )
179 for incl
in includes
do stream
.write
( "#include {incl}\n" )
181 compile_header_core
( stream
)
183 # header file guard close
184 stream
.write
( "#endif\n" )
188 fun write_body_to_file
(mmodule
: MModule, file
: String, includes
: Array[String])
190 var stream
= new OFStream.open
(file
)
192 var module_info
= "/*\n\tExtern implementation of Nit module {mmodule.name}\n*/\n"
194 stream
.write
( module_info
)
195 for incl
in includes
do stream
.write
( "#include {incl}\n" )
197 compile_body_core
( stream
)
204 fun ctype
: String do return "void*"