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 # provides tools to write C .c and .h files
20 import compiling_writer
22 # Accumulates all C code for a compilation unit
23 class CCompilationUnit
25 # comments and native interface imports
26 var header_c_base
: Writer = new Writer
28 # custom C header code or generated for other languages
29 var header_custom
: Writer = new Writer
31 # types of extern classes and friendly types
32 var header_c_types
: Writer = new Writer
34 # implementation declaration for extern methods
35 var header_decl
: Writer = new Writer
38 # comments, imports, etc
39 var body_decl
: Writer = new Writer
41 # custom code and generated for ffi
42 var body_custom
: Writer = new Writer
44 # implementation body of extern methods
45 var body_impl
: Writer = new Writer
47 # files to compile TODO check is appropriate
48 #var files = new Array[String]
50 fun add_local_function
( efc
: CFunction )
52 body_decl
.add
( "{efc.signature};\n" )
54 body_impl
.append
( efc
.to_writer
)
57 fun add_exported_function
( efc
: CFunction )
59 header_decl
.add
( "{efc.signature};\n" )
61 body_impl
.append
( efc
.to_writer
)
64 fun compile_header_core
( stream
: OStream )
66 header_c_base
.write_to_stream
( stream
)
67 header_custom
.write_to_stream
( stream
)
68 header_c_types
.write_to_stream
( stream
)
69 header_decl
.write_to_stream
( stream
)
72 fun compile_body_core
( stream
: OStream )
74 body_decl
.write_to_stream
( stream
)
75 body_custom
.write_to_stream
( stream
)
76 body_impl
.write_to_stream
( stream
)
80 # Accumulates C code related to a specific function
82 var signature
: String
84 var decls
: Writer = new Writer
85 var exprs
: Writer = new Writer
87 fun to_writer
: Writer
91 w
.add
( "{signature}\n\{\n" )