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
24 redef class ToolContext
25 readable writable var _global
: Bool = false
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 _ext_prefix
: String = ""
35 # Class used to generate files.
36 # Note that in fact it is not a visitor.
37 # Note also that this class is unefficient and poorly designed thus requires love.
39 # Add a line in the current declaration block
40 fun add_decl
(s
: String...)
42 add_line_to
(_ctx
.decls
, s
)
45 # Add a line in the current instr block
46 fun add_instr
(s
: String...)
48 add_line_to
(_ctx
.instrs
, s
)
51 fun add_line_to
(a
: Array[String], s
: Array[String])
53 if _indent_level
>= 8 then
56 for i
in [0.._indent_level
[ do
66 # Add a assignment between a variable and an expression
67 fun add_assignment
(v
: String, s
: String)
70 add_instr
(v
, " = ", s
, ";")
74 # C outputs written outside the current C function.
75 readable writable var _out_contexts
: Array[CContext] = new Array[CContext]
77 # Return a unique new number for the instance
84 # next number for new_number
85 var _number_cpt
: Int = 0
87 # Add an indent level.
88 # New decl and instr will be indented.
89 fun indent
do _indent_level
+= 1
91 # Remove an indent level.
95 if _indent_level
< 0 then _indent_level
= 0
98 # Return a big string containing all decl and instr
101 var out
= new Array[String]
102 out
.append
(_ctx
.decls
)
103 out
.append
(_ctx
.instrs
)
107 # The processed module
108 readable var _module
: MMModule
110 # Where instr and decl are stored
111 readable writable var _ctx
: CContext = new CContext
113 # The current indent lever
114 readable writable var _indent_level
: Int = 0
116 # The ToolContext info
117 readable var _tc
: ToolContext
119 # Create a new CompilerVisitor based on a module
120 init(module: MMModule, tc
: ToolContext)
127 # Where instr and decl are stored for a module
128 # Note that this class is as badly designed as CompilerVisitor
130 readable var _decls
: Array[String] = new Array[String]
131 readable var _instrs
: Array[String] = new Array[String]
133 fun append
(c
: CContext)
135 _instrs
.append
(c
.decls
)
136 _instrs
.append
(c
.instrs
)
139 fun merge
(c
: CContext)
141 _decls
.append
(c
.decls
)
142 _instrs
.append
(c
.instrs
)
148 redef class MMGlobalProperty
149 # C symbol refering a method inocation
150 fun meth_call
: String
152 return "CALL_{intro.cname}"
155 # C symbol refering an attribure access
156 fun attr_access
: String
158 return "ATTR_{intro.cname}"
162 redef class MMGlobalClass
163 # C symbol refering the identifier of the class
166 return "ID_{intro.name}"
169 # C symbol refering the color of the class (for subtype tests)
172 return "COLOR_{intro.name}"
175 # C symbol refering the init table position of the class (for constructor linearization)
176 fun init_table_pos_id
: String
178 return "INIT_TABLE_POS_{intro.name}"
182 redef class MMLocalProperty
183 # Cacher result of cname
184 var _cname_cache
: nullable String
186 # The mangled name of the method
189 var cname
= _cname_cache
190 if cname
== null then
191 cname
= cmangle
(module.name
, local_class
.name
, name
)
197 # C macro used to get the function for the call of a super property
198 fun super_meth_call
: String
200 return "CALL_SUPER_{cname}"