compile: add module compiling_writer to replace CContext
[nit.git] / src / compiling / compiling_base.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Common things for NIT compilation and C generation
18 package compiling_base
19
20 import mmloader
21 private import utils
22 import primitive_info
23 import program
24 import compiling_writer
25
26 redef class ToolContext
27 readable writable var _compdir: nullable String = null
28 readable writable var _clibdir: nullable String = null
29 readable writable var _bindir: nullable String = null
30 readable writable var _output_file: nullable String = null
31 readable writable var _boost: Bool = false
32 readable writable var _no_cc: Bool = false
33 readable writable var _cc_link: Bool = false
34 readable writable var _cc_libs: Array[String] = new Array[String]
35 readable writable var _cc_lib_paths: Array[String] = new Array[String]
36 readable writable var _cc_include_paths: Array[String] = new Array[String]
37 readable writable var _ext_prefix: String = ""
38 end
39
40 # Class used to generate files.
41 # Note that in fact it is not a visitor.
42 # Note also that this class is unefficient and poorly designed thus requires love.
43 class CompilerVisitor
44 # Add a line in the current declaration block
45 fun add_decl(s: String)
46 do
47 add_line_to(_decl_writer, s)
48 end
49
50 # Add a line in the current instr block
51 fun add_instr(s: String)
52 do
53 add_line_to(_writer, s)
54 end
55
56
57 fun add_indent(w: Writer)
58 do
59 if _indent_level >= 8 then
60 w.add("\t\t")
61 else
62 for i in [0.._indent_level[ do
63 w.add(" ")
64 end
65 end
66 end
67
68 fun add_line_to(w: Writer, s: String)
69 do
70 add_indent(w)
71 w.add(s)
72 w.add("\n")
73 end
74
75 # Add a assignment between a variable and an expression
76 fun add_assignment(v: String, s: String)
77 do
78 if v != s then
79 var w = _writer
80 add_indent(w)
81 w.add(v)
82 w.add(" = ")
83 w.add(s)
84 w.add(";\n")
85 end
86 end
87
88 # Return a unique new number for the instance
89 fun new_number: Int
90 do
91 var res = _number_cpt
92 _number_cpt = res + 1
93 return res
94 end
95 # next number for new_number
96 var _number_cpt: Int = 0
97
98 # Add an indent level.
99 # New decl and instr will be indented.
100 fun indent do _indent_level += 1
101
102 # Remove an indent level.
103 fun unindent
104 do
105 _indent_level -= 1
106 if _indent_level < 0 then _indent_level = 0
107 end
108
109 # The processed module
110 readable var _module: MMModule
111
112 # Where header decl are stored (public stuff)
113 readable writable var _header_writer: Writer
114
115 # Where current instr are stored (current function declaration)
116 readable writable var _writer: Writer
117
118 # Where current decl are stored (current function instructions)
119 readable writable var _decl_writer: Writer
120
121 # Where body instr are stored (C functions body)
122 readable writable var _top_writer: Writer
123
124 # Where body decl are stored (private C function proptypes and typedefs)
125 readable writable var _top_decl_writer: Writer
126
127 # The current indent lever
128 readable writable var _indent_level: Int = 0
129
130 # The program we are compiling
131 readable var _program: Program
132
133 # Create a new CompilerVisitor based on a module
134 init(module: MMModule, p: Program)
135 do
136 _module = module
137 _program = p
138
139 var w = new Writer
140 _header_writer = w
141 _decl_writer = w
142 w = new Writer
143 _writer = w
144 _top_writer = w
145 _top_decl_writer = w.sub
146 end
147 end
148
149 redef class MMGlobalProperty
150 # C symbol refering a method inocation
151 fun meth_call: String
152 do
153 return "CALL_{intro.cname}"
154 end
155
156 # C symbol refering an attribure access
157 fun attr_access: String
158 do
159 return "ATTR_{intro.cname}"
160 end
161 end
162
163 redef class MMGlobalClass
164 # C symbol refering the identifier of the class
165 fun id_id: String
166 do
167 return "ID_{intro.name}"
168 end
169
170 # C symbol refering the color of the class (for subtype tests)
171 fun color_id: String
172 do
173 return "COLOR_{intro.name}"
174 end
175
176 # C symbol refering the init table position of the class (for constructor linearization)
177 fun init_table_pos_id: String
178 do
179 return "INIT_TABLE_POS_{intro.name}"
180 end
181 end
182
183 redef class MMLocalProperty
184 # Cacher result of cname
185 var _cname_cache: nullable String
186
187 # The mangled name of the method
188 fun cname: String
189 do
190 var cname = _cname_cache
191 if cname == null then
192 cname = cmangle(module.name, local_class.name, name)
193 _cname_cache = cname
194 end
195 return cname
196 end
197
198 # C macro used to get the function for the call of a super property
199 fun super_meth_call: String
200 do
201 return "CALL_SUPER_{cname}"
202 end
203 end
204