1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
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 # Common things for NIT compilation and C generation
18 package compiling_base
25 redef class ToolContext
26 readable writable var _compdir
: nullable String = null
27 readable writable var _clibdir
: nullable String = null
28 readable writable var _bindir
: nullable String = null
29 readable writable var _output_file
: nullable String = null
30 readable writable var _boost
: Bool = false
31 readable writable var _no_cc
: Bool = false
32 readable writable var _cc_link
: Bool = false
33 readable writable var _cc_libs
: Array[String] = new Array[String]
34 readable writable var _cc_lib_paths
: Array[String] = new Array[String]
35 readable writable var _cc_include_paths
: Array[String] = new Array[String]
36 readable writable var _ext_prefix
: String = ""
39 # Class used to generate files.
40 # Note that in fact it is not a visitor.
41 # Note also that this class is unefficient and poorly designed thus requires love.
43 # Add a line in the current declaration block
44 fun add_decl
(s
: String...)
46 add_line_to
(_ctx
.decls
, s
)
49 # Add a line in the current instr block
50 fun add_instr
(s
: String...)
52 add_line_to
(_ctx
.instrs
, s
)
55 fun add_line_to
(a
: Array[String], s
: Array[String])
57 if _indent_level
>= 8 then
60 for i
in [0.._indent_level
[ do
70 # Add a assignment between a variable and an expression
71 fun add_assignment
(v
: String, s
: String)
74 add_instr
(v
, " = ", s
, ";")
78 # C outputs written outside the current C function.
79 readable writable var _out_contexts
: Array[CContext] = new Array[CContext]
81 # Return a unique new number for the instance
88 # next number for new_number
89 var _number_cpt
: Int = 0
91 # Add an indent level.
92 # New decl and instr will be indented.
93 fun indent
do _indent_level
+= 1
95 # Remove an indent level.
99 if _indent_level
< 0 then _indent_level
= 0
102 # Return a big string containing all decl and instr
105 var out
= new Array[String]
106 out
.append
(_ctx
.decls
)
107 out
.append
(_ctx
.instrs
)
111 # The processed module
112 readable var _module
: MMModule
114 # Where instr and decl are stored
115 readable writable var _ctx
: CContext = new CContext
117 # The current indent lever
118 readable writable var _indent_level
: Int = 0
120 # The program we are compiling
121 readable var _program
: Program
123 # Create a new CompilerVisitor based on a module
124 init(module: MMModule, p
: Program)
131 # Where instr and decl are stored for a module
132 # Note that this class is as badly designed as CompilerVisitor
134 readable var _decls
: Array[String] = new Array[String]
135 readable var _instrs
: Array[String] = new Array[String]
137 fun append
(c
: CContext)
139 _instrs
.append
(c
.decls
)
140 _instrs
.append
(c
.instrs
)
143 fun merge
(c
: CContext)
145 _decls
.append
(c
.decls
)
146 _instrs
.append
(c
.instrs
)
152 redef class MMGlobalProperty
153 # C symbol refering a method inocation
154 fun meth_call
: String
156 return "CALL_{intro.cname}"
159 # C symbol refering an attribure access
160 fun attr_access
: String
162 return "ATTR_{intro.cname}"
166 redef class MMGlobalClass
167 # C symbol refering the identifier of the class
170 return "ID_{intro.name}"
173 # C symbol refering the color of the class (for subtype tests)
176 return "COLOR_{intro.name}"
179 # C symbol refering the init table position of the class (for constructor linearization)
180 fun init_table_pos_id
: String
182 return "INIT_TABLE_POS_{intro.name}"
186 redef class MMLocalProperty
187 # Cacher result of cname
188 var _cname_cache
: nullable String
190 # The mangled name of the method
193 var cname
= _cname_cache
194 if cname
== null then
195 cname
= cmangle
(module.name
, local_class
.name
, name
)
201 # C macro used to get the function for the call of a super property
202 fun super_meth_call
: String
204 return "CALL_SUPER_{cname}"