1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Separate compilation of a Nit program
16 module separate_compiler
19 import global_compiler
# TODO better separation of concerns
22 redef class ToolContext
24 var opt_separate
: OptionBool = new OptionBool("Use separate compilation", "--separate")
27 var opt_no_inline_intern
: OptionBool = new OptionBool("Do not inline call to intern methods", "--no-inline-intern")
29 # --no-union-attribute
30 var opt_no_union_attribute
: OptionBool = new OptionBool("Put primitive attibutes in a box instead of an union", "--no-union-attribute")
32 # --no-shortcut-equate
33 var opt_no_shortcut_equate
: OptionBool = new OptionBool("Always call == in a polymorphic way", "--no-shortcut-equal")
35 # --inline-coloring-numbers
36 var opt_inline_coloring_numbers
: OptionBool = new OptionBool("Inline colors and ids", "--inline-coloring-numbers")
38 # --use-naive-coloring
39 var opt_bm_typing
: OptionBool = new OptionBool("Colorize items incrementaly, used to simulate binary matrix typing", "--bm-typing")
41 # --use-mod-perfect-hashing
42 var opt_phmod_typing
: OptionBool = new OptionBool("Replace coloration by perfect hashing (with mod operator)", "--phmod-typing")
44 # --use-and-perfect-hashing
45 var opt_phand_typing
: OptionBool = new OptionBool("Replace coloration by perfect hashing (with and operator)", "--phand-typing")
47 # --generic-resolution-tree
48 var opt_generic_tree
: OptionBool = new OptionBool("Use tree representation for live generic types instead of flattened representation", "--generic-resolution-tree")
53 self.option_context
.add_option
(self.opt_separate
)
54 self.option_context
.add_option
(self.opt_no_inline_intern
)
55 self.option_context
.add_option
(self.opt_no_union_attribute
)
56 self.option_context
.add_option
(self.opt_no_shortcut_equate
)
57 self.option_context
.add_option
(self.opt_inline_coloring_numbers
)
58 self.option_context
.add_option
(self.opt_bm_typing
)
59 self.option_context
.add_option
(self.opt_phmod_typing
)
60 self.option_context
.add_option
(self.opt_phand_typing
)
61 self.option_context
.add_option
(self.opt_generic_tree
)
65 redef class ModelBuilder
66 fun run_separate_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
69 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
71 var compiler
= new SeparateCompiler(mainmodule
, runtime_type_analysis
, self)
72 compiler
.compile_header
74 # compile class structures
75 for m
in mainmodule
.in_importation
.greaters
do
76 for mclass
in m
.intro_mclasses
do
77 compiler
.compile_class_to_c
(mclass
)
81 # The main function of the C
83 compiler
.compile_main_function
86 for m
in mainmodule
.in_importation
.greaters
do
88 compiler
.compile_module_to_c
(m
)
91 # compile live & cast type structures
93 var mtypes
= compiler
.do_type_coloring
95 compiler
.compile_type_to_c
(t
)
98 if self.toolcontext
.opt_generic_tree
.value
then
99 # compile live generic types selection structures
100 for mclass
in model
.mclasses
do
101 compiler
.compile_live_gentype_to_c
(mclass
)
105 compiler
.display_stats
107 write_and_make
(compiler
)
111 # Singleton that store the knowledge about the separate compilation process
112 class SeparateCompiler
113 super GlobalCompiler # TODO better separation of concerns
115 private var undead_types
: Set[MType] = new HashSet[MType]
116 private var partial_types
: Set[MType] = new HashSet[MType]
117 protected var typeids
: HashMap[MType, Int] protected writable = new HashMap[MType, Int]
119 private var type_colors
: Map[MType, Int] = typeids
120 private var type_tables
: nullable Map[MType, Array[nullable MType]] = null
122 private var livetypes_colors
: nullable Map[MType, Int]
123 private var livetypes_tables
: nullable Map[MClass, Array[nullable Object]]
124 private var livetypes_tables_sizes
: nullable Map[MClass, Array[Int]]
125 private var live_unanchored_types
: Map[MClassDef, Set[MType]] = new HashMap[MClassDef, HashSet[MType]]
127 private var unanchored_types_colors
: nullable Map[MType, Int]
128 private var unanchored_types_tables
: nullable Map[MClassType, Array[nullable MType]]
129 private var unanchored_types_masks
: nullable Map[MClassType, Int]
131 protected var class_coloring
: ClassColoring
133 protected var method_colors
: Map[MMethod, Int]
134 protected var method_tables
: Map[MClass, Array[nullable MMethodDef]]
136 protected var attr_colors
: Map[MAttribute, Int]
137 protected var attr_tables
: Map[MClass, Array[nullable MAttributeDef]]
139 protected var vt_colors
: Map[MVirtualTypeProp, Int]
140 protected var vt_tables
: Map[MClass, Array[nullable MVirtualTypeDef]]
141 protected var vt_masks
: nullable Map[MClass, Int]
143 private var ft_colors
: nullable Map[MParameterType, Int]
144 private var ft_tables
: nullable Map[MClass, Array[nullable MParameterType]]
145 private var ft_masks
: nullable Map[MClass, Int]
147 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, mmbuilder
: ModelBuilder) do
148 self.do_property_coloring
149 self.compile_box_kinds
152 redef fun compile_header_structs
do
153 self.header
.add_decl
("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
154 self.compile_header_attribute_structs
155 self.header
.add_decl
("struct class \{ int box_kind; nitmethod_t vft[1]; \}; /* general C type representing a Nit class. */")
157 if modelbuilder
.toolcontext
.opt_generic_tree
.value
then
158 # With generic_tree, only ft and vt resolution is stored in the type
159 self.header
.add_decl
("struct type \{ int id; const char *name; int color; short int is_nullable; int livecolor; struct types *vts_table; struct types *fts_table; int table_size; int type_table[1]; \}; /* general C type representing a Nit type. */")
161 # With unanchored_table, all live type resolution are stored in a big table: unanchored_table
162 self.header
.add_decl
("struct type \{ int id; const char *name; int color; short int is_nullable; struct types *unanchored_table; int table_size; int type_table[1]; \}; /* general C type representing a Nit type. */")
165 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
166 self.header
.add_decl
("struct types \{ int mask; struct type *types[1]; \}; /* a list types (used for vts, fts and unanchored lists). */")
168 self.header
.add_decl
("struct types \{ struct type *types[1]; \}; /* a list types (used for vts, fts and unanchored lists). */")
172 self.header
.add_decl
("typedef struct \{ struct type *type; struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
175 fun compile_header_attribute_structs
177 if modelbuilder
.toolcontext
.opt_no_union_attribute
.value
then
178 self.header
.add_decl
("typedef void* nitattribute_t; /* general C type representing a Nit attribute. */")
180 self.header
.add_decl
("typedef union \{")
181 self.header
.add_decl
("void* val;")
182 for c
, v
in self.box_kinds
do
183 var t
= c
.mclass_type
184 self.header
.add_decl
("{t.ctype} {t.ctypename};")
186 self.header
.add_decl
("\} nitattribute_t; /* general C type representing a Nit attribute. */")
190 redef fun compile_class_names
do
191 abort # There is no class name compilation since the name is stored in the type structure
194 fun compile_box_kinds
196 # Collect all bas box class
197 # FIXME: this is not completely fine with a separate compilation scheme
198 for classname
in ["Int", "Bool", "Char", "Float", "NativeString", "Pointer"] do
199 var classes
= self.mainmodule
.model
.get_mclasses_by_name
(classname
)
200 if classes
== null then continue
201 assert classes
.length
== 1 else print classes
.join
(", ")
202 self.box_kinds
[classes
.first
] = self.box_kinds
.length
+ 1
206 var box_kinds
= new HashMap[MClass, Int]
208 fun box_kind_of
(mclass
: MClass): Int
210 if mclass
.mclass_type
.ctype
== "val*" then
212 else if mclass
.kind
== extern_kind
then
213 return self.box_kinds
[self.mainmodule
.get_primitive_class
("Pointer")]
215 return self.box_kinds
[mclass
]
220 fun compile_color_consts
(colors
: Map[Object, Int]) do
221 for m
, c
in colors
do
222 if m
isa MProperty then
223 if modelbuilder
.toolcontext
.opt_inline_coloring_numbers
.value
then
224 self.header
.add_decl
("#define {m.const_color} {c}")
226 self.header
.add_decl
("extern const int {m.const_color};")
227 self.header
.add
("const int {m.const_color} = {c};")
229 else if m
isa MType then
230 if modelbuilder
.toolcontext
.opt_inline_coloring_numbers
.value
then
231 self.header
.add_decl
("#define {m.const_color} {c}")
233 self.header
.add_decl
("extern const int {m.const_color};")
234 self.header
.add
("const int {m.const_color} = {c};")
240 # colorize classe properties
241 fun do_property_coloring
do
244 self.class_coloring
= new ClassColoring(mainmodule
)
245 class_coloring
.colorize
(modelbuilder
.model
.mclasses
)
248 var method_coloring
= new MethodColoring(self.class_coloring
)
249 self.method_colors
= method_coloring
.colorize
250 self.method_tables
= method_coloring
.build_property_tables
251 self.compile_color_consts
(self.method_colors
)
253 # attributes coloration
254 var attribute_coloring
= new AttributeColoring(self.class_coloring
)
255 self.attr_colors
= attribute_coloring
.colorize
256 self.attr_tables
= attribute_coloring
.build_property_tables
257 self.compile_color_consts
(self.attr_colors
)
259 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
260 self.class_coloring
= new NaiveClassColoring(mainmodule
)
261 self.class_coloring
.colorize
(modelbuilder
.model
.mclasses
)
265 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
266 var vt_coloring
= new NaiveVTColoring(self.class_coloring
)
267 self.vt_colors
= vt_coloring
.colorize
268 self.vt_tables
= vt_coloring
.build_property_tables
269 else if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
270 var vt_coloring
= new VTModPerfectHashing(self.class_coloring
)
271 self.vt_colors
= vt_coloring
.colorize
272 self.vt_masks
= vt_coloring
.compute_masks
273 self.vt_tables
= vt_coloring
.build_property_tables
274 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
275 var vt_coloring
= new VTAndPerfectHashing(self.class_coloring
)
276 self.vt_colors
= vt_coloring
.colorize
277 self.vt_masks
= vt_coloring
.compute_masks
278 self.vt_tables
= vt_coloring
.build_property_tables
280 var vt_coloring
= new VTColoring(self.class_coloring
)
281 self.vt_colors
= vt_coloring
.colorize
282 self.vt_tables
= vt_coloring
.build_property_tables
284 self.compile_color_consts
(self.vt_colors
)
287 # colorize live types of the program
288 private fun do_type_coloring
: Set[MType] do
289 var mtypes
= new HashSet[MType]
290 mtypes
.add_all
(self.runtime_type_analysis
.live_types
)
291 mtypes
.add_all
(self.runtime_type_analysis
.live_cast_types
)
292 mtypes
.add_all
(self.undead_types
)
293 for c
in self.box_kinds
.keys
do
294 mtypes
.add
(c
.mclass_type
)
297 for mtype
in mtypes
do
298 retieve_live_partial_types
(mtype
)
300 mtypes
.add_all
(self.partial_types
)
303 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
304 var sorted_mtypes
= new OrderedSet[MType].from
(mtypes
)
305 sorted_mtypes
.linearize
(new ReverseTypeSorter(self.mainmodule
))
306 for mtype
in sorted_mtypes
do
307 self.typeids
[mtype
] = self.typeids
.length
+ 1
310 for mtype
in mtypes
do
311 self.typeids
[mtype
] = self.typeids
.length
315 if modelbuilder
.toolcontext
.opt_generic_tree
.value
then
316 # fts coloration for non-erased compilation
317 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
318 var ft_coloring
= new NaiveFTColoring(self.class_coloring
)
319 self.ft_colors
= ft_coloring
.colorize
320 self.ft_tables
= ft_coloring
.build_ft_tables
321 else if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
322 var ft_coloring
= new FTModPerfectHashing(self.class_coloring
)
323 self.ft_colors
= ft_coloring
.colorize
324 self.ft_masks
= ft_coloring
.compute_masks
325 self.ft_tables
= ft_coloring
.build_ft_tables
326 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
327 var ft_coloring
= new FTAndPerfectHashing(self.class_coloring
)
328 self.ft_colors
= ft_coloring
.colorize
329 self.ft_masks
= ft_coloring
.compute_masks
330 self.ft_tables
= ft_coloring
.build_ft_tables
332 var ft_coloring
= new FTColoring(self.class_coloring
)
333 self.ft_colors
= ft_coloring
.colorize
334 self.ft_tables
= ft_coloring
.build_ft_tables
336 self.compile_color_consts
(self.ft_colors
.as(not null))
338 # colorize live entries
340 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
341 entries_coloring
= new NaiveLiveEntryColoring
343 entries_coloring
= new LiveEntryColoring
345 self.livetypes_colors
= entries_coloring
.colorize
(mtypes
)
346 self.livetypes_tables
= entries_coloring
.build_livetype_tables
(mtypes
)
347 self.livetypes_tables_sizes
= entries_coloring
.livetypes_tables_sizes
349 # VT and FT are stored with other unresolved types in the big unanchored_tables
350 self.compile_unanchored_tables
(mtypes
)
354 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
355 var type_coloring
= new NaiveTypeColoring(self.mainmodule
, mtypes
)
356 self.type_colors
= type_coloring
.colorize
(mtypes
)
357 self.type_tables
= type_coloring
.build_type_tables
(mtypes
, type_colors
)
358 else if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
359 var type_coloring
= new TypeModPerfectHashing(self.mainmodule
, mtypes
)
360 self.type_colors
= type_coloring
.compute_masks
(mtypes
, typeids
)
361 self.type_tables
= type_coloring
.hash_type_tables
(mtypes
, typeids
, type_colors
)
362 self.header
.add_decl
("#define HASH(mask, id) ((mask)%(id))")
363 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
364 var type_coloring
= new TypeAndPerfectHashing(self.mainmodule
, mtypes
)
365 self.type_colors
= type_coloring
.compute_masks
(mtypes
, typeids
)
366 self.type_tables
= type_coloring
.hash_type_tables
(mtypes
, typeids
, type_colors
)
367 self.header
.add_decl
("#define HASH(mask, id) ((mask)&(id))")
369 var type_coloring
= new TypeColoring(self.mainmodule
, mtypes
)
370 self.type_colors
= type_coloring
.colorize
(mtypes
)
371 self.type_tables
= type_coloring
.build_type_tables
(mtypes
, type_colors
)
378 protected fun compile_unanchored_tables
(mtypes
: Set[MType]) do
379 # Unanchored_tables is used to perform a type resolution at runtime in O(1)
381 # During the visit of the body of classes, live_unanchored_types are collected
383 # Collect all live_unanchored_types (visited in the body of classes)
385 # Determinate fo each livetype what are its possible requested anchored types
386 var mtype2unanchored
= new HashMap[MClassType, Set[MType]]
387 for mtype
in self.runtime_type_analysis
.live_types
do
388 var set
= new HashSet[MType]
389 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
390 if self.live_unanchored_types
.has_key
(cd
) then
391 set
.add_all
(self.live_unanchored_types
[cd
])
394 mtype2unanchored
[mtype
] = set
397 # Compute the table layout with the prefered method
398 if modelbuilder
.toolcontext
.opt_bm_typing
.value
then
399 var unanchored_type_coloring
= new NaiveUnanchoredTypeColoring
400 self.unanchored_types_colors
= unanchored_type_coloring
.colorize
(mtype2unanchored
)
401 self.unanchored_types_tables
= unanchored_type_coloring
.build_tables
(mtype2unanchored
)
402 else if modelbuilder
.toolcontext
.opt_phmod_typing
.value
then
403 var unanchored_type_coloring
= new UnanchoredTypeModPerfectHashing
404 self.unanchored_types_colors
= unanchored_type_coloring
.colorize
(mtype2unanchored
)
405 self.unanchored_types_masks
= unanchored_type_coloring
.compute_masks
(mtype2unanchored
)
406 self.unanchored_types_tables
= unanchored_type_coloring
.build_tables
(mtype2unanchored
)
407 else if modelbuilder
.toolcontext
.opt_phand_typing
.value
then
408 var unanchored_type_coloring
= new UnanchoredTypeAndPerfectHashing
409 self.unanchored_types_colors
= unanchored_type_coloring
.colorize
(mtype2unanchored
)
410 self.unanchored_types_masks
= unanchored_type_coloring
.compute_masks
(mtype2unanchored
)
411 self.unanchored_types_tables
= unanchored_type_coloring
.build_tables
(mtype2unanchored
)
413 var unanchored_type_coloring
= new UnanchoredTypeColoring
414 self.unanchored_types_colors
= unanchored_type_coloring
.colorize
(mtype2unanchored
)
415 self.unanchored_types_tables
= unanchored_type_coloring
.build_tables
(mtype2unanchored
)
418 # Compile a C constant for each collected unanchored type.
419 # Either to a color, or to -1 if the unanchored type is dead (no live receiver can require it)
420 var all_unanchored
= new HashSet[MType]
421 for t
in self.live_unanchored_types
.values
do
422 all_unanchored
.add_all
(t
)
424 var all_unanchored_types_colors
= new HashMap[MType, Int]
425 for t
in all_unanchored
do
426 if unanchored_types_colors
.has_key
(t
) then
427 all_unanchored_types_colors
[t
] = unanchored_types_colors
[t
]
429 all_unanchored_types_colors
[t
] = -1
432 self.compile_color_consts
(all_unanchored_types_colors
)
435 #for k, v in unanchored_types_tables.as(not null) do
436 # print "{k}: {v.join(", ")}"
441 fun retieve_live_partial_types
(mtype
: MType) do
442 # add formal types arguments to mtypes
443 if mtype
isa MGenericType then
444 for ft
in mtype
.arguments
do
445 if ft
.need_anchor
then
446 print
("Why do we need anchor here ?")
449 self.partial_types
.add
(ft
)
450 retieve_live_partial_types
(ft
)
453 var mclass_type
: MClassType
454 if mtype
isa MNullableType then
455 mclass_type
= mtype
.mtype
.as(MClassType)
457 mclass_type
= mtype
.as(MClassType)
460 # add virtual types to mtypes
461 for vt
in self.vt_tables
[mclass_type
.mclass
] do
463 var anchored
= vt
.bound
.anchor_to
(self.mainmodule
, mclass_type
)
464 self.partial_types
.add
(anchored
)
469 # declare live generic types tables selection
470 private fun compile_live_gentype_to_c
(mclass
: MClass) do
471 if mclass
.arity
> 0 then
472 if self.livetypes_tables
.has_key
(mclass
) then
473 var table
= self.livetypes_tables
[mclass
]
474 var sign
= self.livetypes_tables_sizes
[mclass
]
475 var table_buffer
= new Buffer.from
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}] = \{\n")
476 compile_livetype_table
(table
, table_buffer
, 1, mclass
.arity
)
477 table_buffer
.append
("\};")
479 var v
= new SeparateCompilerVisitor(self)
480 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
481 v
.add_decl
(table_buffer
.to_s
)
483 var sign
= new Array[Int].filled_with
(0, mclass
.arity
)
484 var v
= new SeparateCompilerVisitor(self)
485 self.header
.add_decl
("extern const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
486 v
.add_decl
("const struct type *livetypes_{mclass.c_name}[{sign.join("][")}];")
491 private fun compile_livetype_table
(table
: Array[nullable Object], buffer
: Buffer, depth
: Int, max
: Int) do
495 buffer
.append
("NULL,\n")
497 buffer
.append
("\{\},\n")
499 else if obj
isa MClassType then
500 buffer
.append
("(struct type*) &type_{obj.c_name}, /* {obj} */\n")
501 else if obj
isa Array[nullable Object] then
502 buffer
.append
("\{\n")
503 compile_livetype_table
(obj
, buffer
, depth
+ 1, max
)
504 buffer
.append
("\},\n")
509 # Separately compile all the method definitions of the module
510 fun compile_module_to_c
(mmodule
: MModule)
512 var old_module
= self.mainmodule
513 self.mainmodule
= mmodule
514 for cd
in mmodule
.mclassdefs
do
515 for pd
in cd
.mpropdefs
do
516 if not pd
isa MMethodDef then continue
517 #print "compile {pd} @ {cd} @ {mmodule}"
518 var r
= new SeparateRuntimeFunction(pd
)
520 if true or cd
.bound_mtype
.ctype
!= "val*" then
521 var r2
= new VirtualRuntimeFunction(pd
)
522 r2
.compile_to_c
(self)
526 self.mainmodule
= old_module
529 # Globaly compile the type structure of a live type
530 fun compile_type_to_c
(mtype
: MType)
532 var c_name
= mtype
.c_name
533 var v
= new SeparateCompilerVisitor(self)
534 v
.add_decl
("/* runtime type {mtype} */")
536 # extern const struct type_X
537 self.header
.add_decl
("extern const struct type_{c_name} type_{c_name};")
538 self.header
.add_decl
("struct type_{c_name} \{")
539 self.header
.add_decl
("int id;")
540 self.header
.add_decl
("const char *name;")
541 self.header
.add_decl
("int color;")
542 self.header
.add_decl
("short int is_nullable;")
543 if modelbuilder
.toolcontext
.opt_generic_tree
.value
then
544 self.header
.add_decl
("int livecolor;")
545 self.header
.add_decl
("const struct vts_table_{c_name} *vts_table;")
546 self.header
.add_decl
("const struct fts_table_{c_name} *fts_table;")
548 self.header
.add_decl
("const struct types *unanchored_table;")
550 self.header
.add_decl
("int table_size;")
551 self.header
.add_decl
("int type_table[{self.type_tables[mtype].length}];")
552 self.header
.add_decl
("\};")
554 # const struct type_X
555 v
.add_decl
("const struct type_{c_name} type_{c_name} = \{")
556 v
.add_decl
("{self.typeids[mtype]},")
557 v
.add_decl
("\"{mtype}\
", /* class_name_string */")
558 v
.add_decl
("{self.type_colors[mtype]},")
559 if mtype
isa MNullableType then
564 if modelbuilder
.toolcontext
.opt_generic_tree
.value
then
565 v
.add_decl
("{self.livetypes_colors[mtype]},")
566 if compile_type_vts_table
(mtype
) then
567 v
.add_decl
("&vts_table_{c_name},")
571 if compile_type_fts_table
(mtype
) then
572 v
.add_decl
("&fts_table_{c_name},")
577 if compile_type_unanchored_table
(mtype
) then
578 v
.add_decl
("(struct types*) &unanchored_table_{c_name},")
583 v
.add_decl
("{self.type_tables[mtype].length},")
585 for stype
in self.type_tables
[mtype
] do
586 if stype
== null then
587 v
.add_decl
("-1, /* empty */")
589 v
.add_decl
("{self.typeids[stype]}, /* {stype} */")
596 protected fun compile_type_fts_table
(mtype
: MType): Bool do
598 var mclass_type
: MClassType
599 if mtype
isa MNullableType then
600 mclass_type
= mtype
.mtype
.as(MClassType)
602 mclass_type
= mtype
.as(MClassType)
604 if self.ft_tables
[mclass_type
.mclass
].is_empty
then return false
606 # extern const struct fst_table_X fst_table_X
607 self.header
.add_decl
("extern const struct fts_table_{mtype.c_name} fts_table_{mtype.c_name};")
608 self.header
.add_decl
("struct fts_table_{mtype.c_name} \{")
609 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
610 self.header
.add_decl
("int mask;")
612 self.header
.add_decl
("struct type *types[{self.ft_tables[mclass_type.mclass].length}];")
613 self.header
.add_decl
("\};")
615 # const struct fts_table_X fts_table_X
617 v
.add_decl
("const struct fts_table_{mtype.c_name} fts_table_{mtype.c_name} = \{")
618 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
619 v
.add_decl
("{self.ft_masks[mclass_type.mclass]},")
622 for ft
in self.ft_tables
[mclass_type
.mclass
] do
624 v
.add_decl
("NULL, /* empty */")
627 if ft
.mclass
== mclass_type
.mclass
then
628 ntype
= mclass_type
.arguments
[ft
.rank
]
630 ntype
= ft
.anchor_to
(self.mainmodule
, mclass_type
)
632 if self.typeids
.has_key
(ntype
) then
633 v
.add_decl
("(struct type*)&type_{ntype.c_name}, /* {ft} ({ntype}) */")
635 v
.add_decl
("NULL, /* empty ({ft} not a live type) */")
644 protected fun compile_type_vts_table
(mtype
: MType): Bool do
646 var mclass_type
: MClassType
647 if mtype
isa MNullableType then
648 mclass_type
= mtype
.mtype
.as(MClassType)
650 mclass_type
= mtype
.as(MClassType)
652 if self.vt_tables
[mclass_type
.mclass
].is_empty
then return false
654 # extern const struct vts_table_X vts_table_X
655 self.header
.add_decl
("extern const struct vts_table_{mtype.c_name} vts_table_{mtype.c_name};")
656 self.header
.add_decl
("struct vts_table_{mtype.c_name} \{")
657 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
658 self.header
.add_decl
("int mask;")
660 self.header
.add_decl
("struct type *types[{self.vt_tables[mclass_type.mclass].length}];")
661 self.header
.add_decl
("\};")
663 # const struct vts_table_X vts_table_X
665 v
.add_decl
("const struct vts_table_{mtype.c_name} vts_table_{mtype.c_name} = \{")
666 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
667 v
.add_decl
("{vt_masks[mclass_type.mclass]},")
671 for vt
in self.vt_tables
[mclass_type
.mclass
] do
673 v
.add_decl
("NULL, /* empty */")
676 if bound
== null then
677 #FIXME how can a bound be null here ?
678 print
"No bound found for virtual type {vt} ?"
682 if bound
isa MNullableType then
684 is_nullable
= "nullable_"
686 if bound
isa MVirtualType then
687 bound
= bound
.anchor_to
(self.mainmodule
, mclass_type
)
688 else if bound
isa MParameterType then
689 bound
= bound
.anchor_to
(self.mainmodule
, mclass_type
)
690 else if bound
isa MGenericType and bound
.need_anchor
then
691 bound
= bound
.anchor_to
(self.mainmodule
, mclass_type
)
692 else if bound
isa MClassType then
694 print
"NOT YET IMPLEMENTED: mtype_to_livetype with type: {bound}"
698 if self.typeids
.has_key
(bound
) then
699 v
.add_decl
("(struct type*)&type_{is_nullable}{bound.c_name}, /* {bound} */")
701 v
.add_decl
("NULL, /* dead type {bound} */")
711 fun compile_type_unanchored_table
(mtype
: MType): Bool do
713 var mclass_type
: MClassType
714 if mtype
isa MNullableType then
715 mclass_type
= mtype
.mtype
.as(MClassType)
717 mclass_type
= mtype
.as(MClassType)
719 if not self.unanchored_types_tables
.has_key
(mclass_type
) then return false
721 # extern const struct unanchored_table_X unanchored_table_X
722 self.header
.add_decl
("extern const struct unanchored_table_{mtype.c_name} unanchored_table_{mtype.c_name};")
724 self.header
.add_decl
("struct unanchored_table_{mtype.c_name} \{")
725 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
726 self.header
.add_decl
("int mask;")
728 self.header
.add_decl
("struct type *types[{self.unanchored_types_tables[mclass_type].length}];")
729 self.header
.add_decl
("\};")
731 # const struct fts_table_X fts_table_X
733 v
.add_decl
("const struct unanchored_table_{mtype.c_name} unanchored_table_{mtype.c_name} = \{")
734 if modelbuilder
.toolcontext
.opt_phmod_typing
.value
or modelbuilder
.toolcontext
.opt_phand_typing
.value
then
735 v
.add_decl
("{self.unanchored_types_masks[mclass_type]},")
738 for t
in self.unanchored_types_tables
[mclass_type
] do
740 v
.add_decl
("NULL, /* empty */")
742 # The table stores the result of the type resolution
743 # Therefore, for a receiver `mclass_type`, and a unresolved type `t`
744 # the value stored is tv.
745 var tv
= t
.resolve_for
(mclass_type
, mclass_type
, self.mainmodule
, true)
746 # FIXME: What typeids means here? How can a tv not be live?
747 if self.typeids
.has_key
(tv
) then
748 v
.add_decl
("(struct type*)&type_{tv.c_name}, /* {t}: {tv} */")
750 v
.add_decl
("NULL, /* empty ({t}: {tv} not a live type) */")
759 # Globally compile the table of the class mclass
760 # In a link-time optimisation compiler, tables are globally computed
761 # In a true separate compiler (a with dynamic loading) you cannot do this unfortnally
762 fun compile_class_to_c
(mclass
: MClass)
764 var mtype
= mclass
.intro
.bound_mtype
765 var c_name
= mclass
.c_name
767 var vft
= self.method_tables
[mclass
]
768 var attrs
= self.attr_tables
[mclass
]
771 v
.add_decl
("/* runtime class {c_name} */")
772 var idnum
= classids
.length
773 var idname
= "ID_" + c_name
774 self.classids
[mtype
] = idname
775 #self.header.add_decl("#define {idname} {idnum} /* {c_name} */")
777 self.header
.add_decl
("struct class_{c_name} \{")
778 self.header
.add_decl
("int box_kind;")
779 self.header
.add_decl
("nitmethod_t vft[{vft.length}];")
780 self.header
.add_decl
("\};")
783 self.header
.add_decl
("extern const struct class_{c_name} class_{c_name};")
784 v
.add_decl
("const struct class_{c_name} class_{c_name} = \{")
785 v
.add_decl
("{self.box_kind_of(mclass)}, /* box_kind */")
787 for i
in [0 .. vft
.length
[ do
788 var mpropdef
= vft
[i
]
789 if mpropdef
== null then
790 v
.add_decl
("NULL, /* empty */")
792 if true or mpropdef
.mclassdef
.bound_mtype
.ctype
!= "val*" then
793 v
.add_decl
("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
795 v
.add_decl
("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
802 if mtype
.ctype
!= "val*" then
803 #Build instance struct
804 self.header
.add_decl
("struct instance_{c_name} \{")
805 self.header
.add_decl
("const struct type *type;")
806 self.header
.add_decl
("const struct class *class;")
807 self.header
.add_decl
("{mtype.ctype} value;")
808 self.header
.add_decl
("\};")
810 if not self.runtime_type_analysis
.live_types
.has
(mtype
) then return
812 self.header
.add_decl
("val* BOX_{c_name}({mtype.ctype});")
813 v
.add_decl
("/* allocate {mtype} */")
814 v
.add_decl
("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
815 v
.add
("struct instance_{c_name}*res = GC_MALLOC(sizeof(struct instance_{c_name}));")
816 v
.add
("res->type = (struct type*) &type_{c_name};")
817 v
.add
("res->class = (struct class*) &class_{c_name};")
818 v
.add
("res->value = value;")
819 v
.add
("return (val*)res;")
824 var is_native_array
= mclass
.name
== "NativeArray"
827 if is_native_array
then
828 sig
= "int length, struct type* type"
830 sig
= "struct type* type"
833 #Build instance struct
834 #extern const struct instance_array__NativeArray instance_array__NativeArray;
835 self.header
.add_decl
("struct instance_{c_name} \{")
836 self.header
.add_decl
("const struct type *type;")
837 self.header
.add_decl
("const struct class *class;")
838 self.header
.add_decl
("nitattribute_t attrs[{attrs.length}];")
839 if is_native_array
then
840 # NativeArrays are just a instance header followed by an array of values
841 self.header
.add_decl
("val* values[0];")
843 self.header
.add_decl
("\};")
846 self.header
.add_decl
("{mtype.ctype} NEW_{c_name}({sig});")
847 v
.add_decl
("/* allocate {mtype} */")
848 v
.add_decl
("{mtype.ctype} NEW_{c_name}({sig}) \{")
849 var res
= v
.new_named_var
(mtype
, "self")
851 if is_native_array
then
852 var mtype_elt
= mtype
.arguments
.first
853 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
855 v
.add
("{res} = GC_MALLOC(sizeof(struct instance_{c_name}));")
857 v
.add
("{res}->type = type;")
858 if v
.compiler
.modelbuilder
.toolcontext
.opt_hardening
.value
then
859 v
.add
("if(type == NULL) \{")
860 v
.add_abort
("type null")
862 if not v
.compiler
.modelbuilder
.toolcontext
.opt_generic_tree
.value
then
863 v
.add
("if(type->unanchored_table == NULL) \{")
864 v
.add
("fprintf(stderr, \"Insantiation of a dead
type: %s\\n\
", type->name);")
865 v
.add_abort
("type dead")
869 v
.add
("{res}->class = (struct class*) &class_{c_name};")
871 self.generate_init_attr
(v
, res
, mtype
)
872 v
.add
("return {res};")
875 generate_check_init_instance
(mtype
)
878 redef fun generate_check_init_instance
(mtype
)
880 if self.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
882 var v
= self.new_visitor
883 var c_name
= mtype
.mclass
.c_name
884 var res
= new RuntimeVariable("self", mtype
, mtype
)
885 self.header
.add_decl
("void CHECK_NEW_{c_name}({mtype.ctype});")
886 v
.add_decl
("/* allocate {mtype} */")
887 v
.add_decl
("void CHECK_NEW_{c_name}({mtype.ctype} {res}) \{")
888 self.generate_check_attr
(v
, res
, mtype
)
892 redef fun new_visitor
do return new SeparateCompilerVisitor(self)
895 # The C function associated to a methoddef separately compiled
896 class SeparateRuntimeFunction
897 super AbstractRuntimeFunction
899 redef fun build_c_name
: String
901 return "{mmethoddef.c_name}"
904 redef fun to_s
do return self.mmethoddef
.to_s
906 redef fun compile_to_c
(compiler
)
908 var mmethoddef
= self.mmethoddef
910 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
911 var v
= compiler
.new_visitor
912 var selfvar
= new RuntimeVariable("self", recv
, recv
)
913 var arguments
= new Array[RuntimeVariable]
914 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
917 var msignature
= mmethoddef
.msignature
.resolve_for
(mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.mmodule
, true)
920 var comment
= new Buffer
921 var ret
= msignature
.return_mtype
923 sig
.append
("{ret.ctype} ")
924 else if mmethoddef
.mproperty
.is_new
then
926 sig
.append
("{ret.ctype} ")
930 sig
.append
(self.c_name
)
931 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
932 comment
.append
("(self: {selfvar}")
933 arguments
.add
(selfvar
)
934 for i
in [0..msignature
.arity
[ do
935 var mtype
= msignature
.mparameters
[i
].mtype
936 if i
== msignature
.vararg_rank
then
937 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
939 comment
.append
(", {mtype}")
940 sig
.append
(", {mtype.ctype} p{i}")
941 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
942 arguments
.add
(argvar
)
947 comment
.append
(": {ret}")
949 compiler
.header
.add_decl
("{sig};")
951 v
.add_decl
("/* method {self} for {comment} */")
952 v
.add_decl
("{sig} \{")
954 frame
.returnvar
= v
.new_var
(ret
)
956 frame
.returnlabel
= v
.get_name
("RET_LABEL")
958 if recv
!= arguments
.first
.mtype
then
959 #print "{self} {recv} {arguments.first}"
961 mmethoddef
.compile_inside_to_c
(v
, arguments
)
963 v
.add
("{frame.returnlabel.as(not null)}:;")
965 v
.add
("return {frame.returnvar.as(not null)};")
971 # The C function associated to a methoddef on a primitive type, stored into a VFT of a class
972 # The first parameter (the reciever) is always typed by val* in order to accept an object value
973 class VirtualRuntimeFunction
974 super AbstractRuntimeFunction
976 redef fun build_c_name
: String
978 return "VIRTUAL_{mmethoddef.c_name}"
981 redef fun to_s
do return self.mmethoddef
.to_s
983 redef fun compile_to_c
(compiler
)
985 var mmethoddef
= self.mmethoddef
987 var recv
= self.mmethoddef
.mclassdef
.bound_mtype
988 var v
= compiler
.new_visitor
989 var selfvar
= new RuntimeVariable("self", v
.object_type
, recv
)
990 var arguments
= new Array[RuntimeVariable]
991 var frame
= new Frame(v
, mmethoddef
, recv
, arguments
)
995 var comment
= new Buffer
997 # Because the function is virtual, the signature must match the one of the original class
998 var intromclassdef
= self.mmethoddef
.mproperty
.intro
.mclassdef
999 var msignature
= mmethoddef
.mproperty
.intro
.msignature
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
1000 var ret
= msignature
.return_mtype
1002 sig
.append
("{ret.ctype} ")
1003 else if mmethoddef
.mproperty
.is_new
then
1005 sig
.append
("{ret.ctype} ")
1009 sig
.append
(self.c_name
)
1010 sig
.append
("({selfvar.mtype.ctype} {selfvar}")
1011 comment
.append
("(self: {selfvar}")
1012 arguments
.add
(selfvar
)
1013 for i
in [0..msignature
.arity
[ do
1014 var mtype
= msignature
.mparameters
[i
].mtype
1015 if i
== msignature
.vararg_rank
then
1016 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
1018 comment
.append
(", {mtype}")
1019 sig
.append
(", {mtype.ctype} p{i}")
1020 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
1021 arguments
.add
(argvar
)
1026 comment
.append
(": {ret}")
1028 compiler
.header
.add_decl
("{sig};")
1030 v
.add_decl
("/* method {self} for {comment} */")
1031 v
.add_decl
("{sig} \{")
1033 frame
.returnvar
= v
.new_var
(ret
)
1035 frame
.returnlabel
= v
.get_name
("RET_LABEL")
1037 if recv
!= arguments
.first
.mtype
then
1038 #print "{self} {recv} {arguments.first}"
1040 mmethoddef
.compile_inside_to_c
(v
, arguments
)
1042 v
.add
("{frame.returnlabel.as(not null)}:;")
1044 v
.add
("return {frame.returnvar.as(not null)};")
1049 redef fun call
(v
, arguments
)
1056 # A visitor on the AST of property definition that generate the C code of a separate compilation process.
1057 class SeparateCompilerVisitor
1058 super GlobalCompilerVisitor # TODO better separation of concerns
1060 redef fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
1062 var msignature
= m
.msignature
.resolve_for
(m
.mclassdef
.bound_mtype
, m
.mclassdef
.bound_mtype
, m
.mclassdef
.mmodule
, true)
1063 var recv
= args
.first
1064 if recv
.mtype
.ctype
!= m
.mclassdef
.mclass
.mclass_type
.ctype
then
1065 args
.first
= self.autobox
(args
.first
, m
.mclassdef
.mclass
.mclass_type
)
1067 for i
in [0..msignature
.arity
[ do
1068 var t
= msignature
.mparameters
[i
].mtype
1069 if i
== msignature
.vararg_rank
then
1072 args
[i
+1] = self.autobox
(args
[i
+1], t
)
1076 # Box or unbox a value to another type iff a C type conversion is needed
1077 # ENSURE: result.mtype.ctype == mtype.ctype
1078 redef fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1080 if value
.mtype
== mtype
then
1082 else if value
.mtype
.ctype
== "val*" and mtype
.ctype
== "val*" then
1084 else if value
.mtype
.ctype
== "val*" then
1085 return self.new_expr
("((struct instance_{mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype
)
1086 else if mtype
.ctype
== "val*" then
1087 var valtype
= value
.mtype
.as(MClassType)
1088 var res
= self.new_var
(mtype
)
1089 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
1090 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
1091 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
1094 self.add
("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
1097 # Bad things will appen!
1098 var res
= self.new_var
(mtype
)
1099 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
1100 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
1105 # Return a C expression returning the runtime type structure of the value
1106 # The point of the method is to works also with primitives types.
1107 fun type_info
(value
: RuntimeVariable): String
1109 if value
.mtype
.ctype
== "val*" then
1110 return "{value}->type"
1112 return "(&type_{value.mtype.c_name})"
1116 redef fun send
(mmethod
, arguments
)
1118 if arguments
.first
.mcasttype
.ctype
!= "val*" then
1119 return self.monomorphic_send
(mmethod
, arguments
.first
.mcasttype
, arguments
)
1122 var res
: nullable RuntimeVariable
1123 var msignature
= mmethod
.intro
.msignature
.resolve_for
(mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.bound_mtype
, mmethod
.intro
.mclassdef
.mmodule
, true)
1124 var ret
= msignature
.return_mtype
1125 if mmethod
.is_new
then
1126 ret
= arguments
.first
.mtype
1127 res
= self.new_var
(ret
)
1128 else if ret
== null then
1131 res
= self.new_var
(ret
)
1137 var recv
= arguments
.first
1140 self.varargize
(mmethod
.intro
, mmethod
.intro
.msignature
.as(not null), arguments
)
1141 for i
in [0..msignature
.arity
[ do
1142 var a
= arguments
[i
+1]
1143 var t
= msignature
.mparameters
[i
].mtype
1144 if i
== msignature
.vararg_rank
then
1145 t
= arguments
[i
+1].mcasttype
1147 s
.append
(", {t.ctype}")
1148 a
= self.autobox
(a
, t
)
1152 var consider_null
= not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
or mmethod
.name
== "==" or mmethod
.name
== "!="
1153 var maybenull
= recv
.mcasttype
isa MNullableType and consider_null
1155 self.add
("if ({recv} == NULL) \{")
1156 if mmethod
.name
== "==" then
1158 var arg
= arguments
[1]
1159 if arg
.mcasttype
isa MNullableType then
1160 self.add
("{res} = ({arg} == NULL);")
1161 else if arg
.mcasttype
isa MNullType then
1162 self.add
("{res} = 1; /* is null */")
1164 self.add
("{res} = 0; /* {arg.inspect} cannot be null */")
1166 else if mmethod
.name
== "!=" then
1168 var arg
= arguments
[1]
1169 if arg
.mcasttype
isa MNullableType then
1170 self.add
("{res} = ({arg} != NULL);")
1171 else if arg
.mcasttype
isa MNullType then
1172 self.add
("{res} = 0; /* is null */")
1174 self.add
("{res} = 1; /* {arg.inspect} cannot be null */")
1177 self.add_abort
("Reciever is null")
1179 self.add
("\} else \{")
1181 if not self.compiler
.modelbuilder
.toolcontext
.opt_no_shortcut_equate
.value
and (mmethod
.name
== "==" or mmethod
.name
== "!=") then
1183 # Recv is not null, thus is arg is, it is easy to conclude (and respect the invariants)
1184 var arg
= arguments
[1]
1185 if arg
.mcasttype
isa MNullType then
1186 if mmethod
.name
== "==" then
1187 self.add
("{res} = 0; /* arg is null but recv is not */")
1189 self.add
("{res} = 1; /* arg is null and recv is not */")
1199 if ret
== null then r
= "void" else r
= ret
.ctype
1200 var call
= "(({r} (*)({s}))({arguments.first}->class->vft[{mmethod.const_color}]))({ss}) /* {mmethod} on {arguments.first.inspect}*/"
1203 self.add
("{res} = {call};")
1215 redef fun call
(mmethoddef
, recvtype
, arguments
)
1217 var res
: nullable RuntimeVariable
1218 var ret
= mmethoddef
.msignature
.return_mtype
1219 if mmethoddef
.mproperty
.is_new
then
1220 ret
= arguments
.first
.mtype
1221 res
= self.new_var
(ret
)
1222 else if ret
== null then
1225 ret
= ret
.resolve_for
(mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.bound_mtype
, mmethoddef
.mclassdef
.mmodule
, true)
1226 res
= self.new_var
(ret
)
1229 if self.compiler
.modelbuilder
.mpropdef2npropdef
.has_key
(mmethoddef
) and
1230 self.compiler
.modelbuilder
.mpropdef2npropdef
[mmethoddef
] isa AInternMethPropdef and
1231 not compiler
.modelbuilder
.toolcontext
.opt_no_inline_intern
.value
then
1232 var frame
= new Frame(self, mmethoddef
, recvtype
, arguments
)
1233 frame
.returnlabel
= self.get_name
("RET_LABEL")
1234 frame
.returnvar
= res
1235 var old_frame
= self.frame
1237 self.add
("\{ /* Inline {mmethoddef} ({arguments.join(",")}) */")
1238 mmethoddef
.compile_inside_to_c
(self, arguments
)
1239 self.add
("{frame.returnlabel.as(not null)}:(void)0;")
1241 self.frame
= old_frame
1246 self.adapt_signature
(mmethoddef
, arguments
)
1249 self.add
("{mmethoddef.c_name}({arguments.join(", ")});")
1252 self.add
("{res} = {mmethoddef.c_name}({arguments.join(", ")});")
1258 redef fun vararg_instance
(mpropdef
, recv
, varargs
, elttype
)
1260 # A vararg must be stored into an new array
1261 # The trick is that the dymaic type of the array may depends on the receiver
1262 # of the method (ie recv) if the static type is unresolved
1263 # This is more complex than usual because the unanchored type must not be resolved
1264 # with the current receiver (ie self).
1265 # Therefore to isolate the resolution from self, a local Frame is created.
1266 # One can see this implementation as an inlined method of the receiver whose only
1267 # job is to allocate the array
1268 var old_frame
= self.frame
1269 var frame
= new Frame(self, mpropdef
, mpropdef
.mclassdef
.bound_mtype
, [recv
])
1271 #print "required Array[{elttype}] for recv {recv.inspect}. bound=Array[{self.resolve_for(elttype, recv)}]. selfvar={frame.arguments.first.inspect}"
1272 var res
= self.array_instance
(varargs
, elttype
)
1273 self.frame
= old_frame
1277 redef fun isset_attribute
(a
, recv
)
1279 self.check_recv_notnull
(recv
)
1280 var res
= self.new_var
(bool_type
)
1282 # What is the declared type of the attribute?
1283 var mtype
= a
.intro
.static_mtype
.as(not null)
1284 var intromclassdef
= a
.intro
.mclassdef
1285 mtype
= mtype
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
1287 if mtype
isa MNullableType then
1288 self.add
("{res} = 1; /* easy isset: {a} on {recv.inspect} */")
1292 if self.compiler
.modelbuilder
.toolcontext
.opt_no_union_attribute
.value
then
1293 self.add
("{res} = {recv}->attrs[{a.const_color}] != NULL; /* {a} on {recv.inspect}*/")
1296 if mtype
.ctype
== "val*" then
1297 self.add
("{res} = {recv}->attrs[{a.const_color}].val != NULL; /* {a} on {recv.inspect} */")
1299 self.add
("{res} = 1; /* NOT YET IMPLEMENTED: isset of primitives: {a} on {recv.inspect} */")
1305 redef fun read_attribute
(a
, recv
)
1307 self.check_recv_notnull
(recv
)
1309 # What is the declared type of the attribute?
1310 var ret
= a
.intro
.static_mtype
.as(not null)
1311 var intromclassdef
= a
.intro
.mclassdef
1312 ret
= ret
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
1314 if self.compiler
.modelbuilder
.toolcontext
.opt_no_union_attribute
.value
then
1315 # Get the attribute or a box (ie. always a val*)
1316 var cret
= self.object_type
.as_nullable
1317 var res
= self.new_var
(cret
)
1320 self.add
("{res} = {recv}->attrs[{a.const_color}]; /* {a} on {recv.inspect} */")
1322 # Check for Uninitialized attribute
1323 if not ret
isa MNullableType and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then
1324 self.add
("if ({res} == NULL) \{")
1325 self.add_abort
("Uninitialized attribute {a.name}")
1329 # Return the attribute or its unboxed version
1330 # Note: it is mandatory since we reuse the box on write, we do not whant that the box escapes
1331 return self.autobox
(res
, ret
)
1333 var res
= self.new_var
(ret
)
1334 self.add
("{res} = {recv}->attrs[{a.const_color}].{ret.ctypename}; /* {a} on {recv.inspect} */")
1336 # Check for Uninitialized attribute
1337 if ret
.ctype
== "val*" and not ret
isa MNullableType and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then
1338 self.add
("if ({res} == NULL) \{")
1339 self.add_abort
("Uninitialized attribute {a.name}")
1347 redef fun write_attribute
(a
, recv
, value
)
1349 self.check_recv_notnull
(recv
)
1351 # What is the declared type of the attribute?
1352 var mtype
= a
.intro
.static_mtype
.as(not null)
1353 var intromclassdef
= a
.intro
.mclassdef
1354 mtype
= mtype
.resolve_for
(intromclassdef
.bound_mtype
, intromclassdef
.bound_mtype
, intromclassdef
.mmodule
, true)
1356 # Adapt the value to the declared type
1357 value
= self.autobox
(value
, mtype
)
1359 if self.compiler
.modelbuilder
.toolcontext
.opt_no_union_attribute
.value
then
1360 var attr
= "{recv}->attrs[{a.const_color}]"
1361 if mtype
.ctype
!= "val*" then
1362 assert mtype
isa MClassType
1363 # The attribute is primitive, thus we store it in a box
1364 # The trick is to create the box the first time then resuse the box
1365 self.add
("if ({attr} != NULL) \{")
1366 self.add
("((struct instance_{mtype.c_name}*){attr})->value = {value}; /* {a} on {recv.inspect} */")
1367 self.add
("\} else \{")
1368 value
= self.autobox
(value
, self.object_type
.as_nullable
)
1369 self.add
("{attr} = {value}; /* {a} on {recv.inspect} */")
1372 # The attribute is not primitive, thus store it direclty
1373 self.add
("{attr} = {value}; /* {a} on {recv.inspect} */")
1376 self.add
("{recv}->attrs[{a.const_color}].{mtype.ctypename} = {value}; /* {a} on {recv.inspect} */")
1380 # Build livetype structure retrieving
1381 #ENSURE: mtype.need_anchor
1382 fun retrieve_anchored_livetype
(mtype
: MGenericType, buffer
: Buffer) do
1383 assert mtype
.need_anchor
1385 var compiler
= self.compiler
.as(SeparateCompiler)
1386 for ft
in mtype
.arguments
do
1390 if ntype
isa MNullableType then
1394 var recv
= self.frame
.arguments
.first
1395 var recv_type_info
= self.type_info
(recv
)
1396 if ntype
isa MParameterType then
1397 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1398 buffer
.append
("[{recv_type_info}->fts_table->types[HASH({recv_type_info}->fts_table->mask, {ntype.const_color})]->livecolor]")
1400 buffer
.append
("[{recv_type_info}->fts_table->types[{ntype.const_color}]->livecolor]")
1402 else if ntype
isa MVirtualType then
1403 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1404 buffer
.append
("[{recv_type_info}->vts_table->types[HASH({recv_type_info}->vts_table->mask, {ntype.mproperty.const_color})]->livecolor]")
1406 buffer
.append
("[{recv_type_info}->vts_table->types[{ntype.mproperty.const_color}]->livecolor]")
1408 else if ntype
isa MGenericType and ntype
.need_anchor
then
1409 var bbuff
= new Buffer
1410 retrieve_anchored_livetype
(ntype
, bbuff
)
1411 buffer
.append
("[livetypes_{ntype.mclass.c_name}{bbuff.to_s}->livecolor]")
1412 else if ntype
isa MClassType then
1413 compiler
.undead_types
.add
(ft
)
1414 buffer
.append
("[type_{ft.c_name}.livecolor]")
1416 self.add
("printf(\"NOT YET IMPLEMENTED: init_instance
(%s
, {mtype}).\\n\
", \"{ft}\
"); exit(1);")
1421 redef fun init_instance
(mtype
)
1423 var compiler
= self.compiler
.as(SeparateCompiler)
1424 if mtype
isa MGenericType and mtype
.need_anchor
then
1425 if compiler
.modelbuilder
.toolcontext
.opt_generic_tree
.value
then
1426 var buff
= new Buffer
1427 retrieve_anchored_livetype
(mtype
, buff
)
1428 mtype
= self.anchor
(mtype
).as(MClassType)
1429 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) livetypes_{mtype.mclass.c_name}{buff.to_s})", mtype
)
1431 link_unanchored_type
(self.frame
.mpropdef
.mclassdef
, mtype
)
1432 var recv
= self.frame
.arguments
.first
1433 var recv_type_info
= self.type_info
(recv
)
1434 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1435 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {mtype.const_color})])", mtype
)
1437 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) {recv_type_info}->unanchored_table->types[{mtype.const_color}])", mtype
)
1441 compiler
.undead_types
.add
(mtype
)
1442 return self.new_expr
("NEW_{mtype.mclass.c_name}((struct type *) &type_{mtype.c_name})", mtype
)
1445 redef fun check_init_instance
(value
, mtype
)
1447 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_initialization
.value
then return
1448 self.add
("CHECK_NEW_{mtype.mclass.c_name}({value});")
1452 redef fun type_test
(value
, mtype
, tag
)
1454 self.add
("/* {value.inspect} isa {mtype} */")
1455 var compiler
= self.compiler
.as(SeparateCompiler)
1457 var recv
= self.frame
.arguments
.first
1458 var recv_type_info
= self.type_info
(recv
)
1460 var res
= self.new_var
(bool_type
)
1462 var cltype
= self.get_name
("cltype")
1463 self.add_decl
("int {cltype};")
1464 var idtype
= self.get_name
("idtype")
1465 self.add_decl
("int {idtype};")
1467 var maybe_null
= self.maybe_null
(value
)
1468 var accept_null
= "0"
1470 if ntype
isa MNullableType then
1475 if value
.mcasttype
.is_subtype
(self.frame
.mpropdef
.mclassdef
.mmodule
, self.frame
.mpropdef
.mclassdef
.bound_mtype
, mtype
) then
1476 self.add
("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
1477 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1478 self.compiler
.count_type_test_skipped
[tag
] += 1
1479 self.add
("count_type_test_skipped_{tag}++;")
1484 if ntype
.need_anchor
then
1485 var type_struct
= self.get_name
("type_struct")
1486 self.add_decl
("struct type* {type_struct};")
1488 # For unresolved types, there is two implementations
1489 if compiler
.modelbuilder
.toolcontext
.opt_generic_tree
.value
then
1490 # Either with the generic_tree and the construction of a type
1491 if ntype
isa MParameterType then
1492 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1493 self.add
("{type_struct} = {recv_type_info}->fts_table->types[HASH({recv_type_info}->fts_table->mask, {ntype.const_color})];")
1495 self.add
("{type_struct} = {recv_type_info}->fts_table->types[{ntype.const_color}];")
1497 else if ntype
isa MVirtualType then
1498 var vtcolor
= ntype
.mproperty
.const_color
1499 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1500 self.add
("{type_struct} = {recv_type_info}->vts_table->types[HASH({recv_type_info}->vts_table->mask, {vtcolor})];")
1502 self.add
("{type_struct} = {recv_type_info}->vts_table->types[{vtcolor}];")
1504 else if ntype
isa MGenericType then
1505 var buff
= new Buffer
1506 retrieve_anchored_livetype
(ntype
, buff
)
1507 self.add
("{type_struct} = (struct type*)livetypes_{ntype.mclass.c_name}{buff.to_s};")
1510 # Either with unanchored_table with a direct resolution
1511 link_unanchored_type
(self.frame
.mpropdef
.mclassdef
, ntype
)
1512 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1513 self.add
("{type_struct} = {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {ntype.const_color})];")
1515 self.add
("{type_struct} = {recv_type_info}->unanchored_table->types[{ntype.const_color}];")
1517 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1518 self.compiler
.count_type_test_unresolved
[tag
] += 1
1519 self.add
("count_type_test_unresolved_{tag}++;")
1522 self.add
("{cltype} = {type_struct}->color;")
1523 self.add
("{idtype} = {type_struct}->id;")
1524 if maybe_null
and accept_null
== "0" then
1525 var is_nullable
= self.get_name
("is_nullable")
1526 self.add_decl
("short int {is_nullable};")
1527 self.add
("{is_nullable} = {type_struct}->is_nullable;")
1528 accept_null
= is_nullable
.to_s
1530 else if ntype
isa MClassType then
1531 compiler
.undead_types
.add
(mtype
)
1532 self.add
("{cltype} = type_{mtype.c_name}.color;")
1533 self.add
("{idtype} = type_{mtype.c_name}.id;")
1534 if compiler
.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
1535 self.compiler
.count_type_test_resolved
[tag
] += 1
1536 self.add
("count_type_test_resolved_{tag}++;")
1539 self.add
("printf(\"NOT YET IMPLEMENTED: type_test
(%s
, {mtype}).\\n\
", \"{value.inspect}\
"); exit(1);")
1542 # check color is in table
1544 self.add
("if({value} == NULL) \{")
1545 self.add
("{res} = {accept_null};")
1546 self.add
("\} else \{")
1548 var value_type_info
= self.type_info
(value
)
1549 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1550 self.add
("{cltype} = HASH({value_type_info}->color, {idtype});")
1552 self.add
("if({cltype} >= {value_type_info}->table_size) \{")
1553 self.add
("{res} = 0;")
1554 self.add
("\} else \{")
1555 self.add
("{res} = {value_type_info}->type_table[{cltype}] == {idtype};")
1564 redef fun is_same_type_test
(value1
, value2
)
1566 var res
= self.new_var
(bool_type
)
1567 # Swap values to be symetric
1568 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1573 if value1
.mtype
.ctype
!= "val*" then
1574 if value2
.mtype
== value1
.mtype
then
1575 self.add
("{res} = 1; /* is_same_type_test: compatible types {value1.mtype} vs. {value2.mtype} */")
1576 else if value2
.mtype
.ctype
!= "val*" then
1577 self.add
("{res} = 0; /* is_same_type_test: incompatible types {value1.mtype} vs. {value2.mtype}*/")
1579 var mtype1
= value1
.mtype
.as(MClassType)
1580 self.add
("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name}); /* is_same_type_test */")
1583 self.add
("{res} = ({value1} == {value2}) || ({value1} != NULL && {value2} != NULL && {value1}->class == {value2}->class); /* is_same_type_test */")
1588 redef fun class_name_string
(value
)
1590 var res
= self.get_name
("var_class_name")
1591 self.add_decl
("const char* {res};")
1592 if value
.mtype
.ctype
== "val*" then
1593 self.add
"{res} = {value} == NULL ? \"null\
" : {value}->type->name;"
1595 self.add
"{res} = type_{value.mtype.c_name}.name;"
1600 redef fun equal_test
(value1
, value2
)
1602 var res
= self.new_var
(bool_type
)
1603 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1608 if value1
.mtype
.ctype
!= "val*" then
1609 if value2
.mtype
== value1
.mtype
then
1610 self.add
("{res} = {value1} == {value2};")
1611 else if value2
.mtype
.ctype
!= "val*" then
1612 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1614 var mtype1
= value1
.mtype
.as(MClassType)
1615 self.add
("{res} = ({value2} != NULL) && ({value2}->class == (struct class*) &class_{mtype1.c_name});")
1616 self.add
("if ({res}) \{")
1617 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1622 var maybe_null
= true
1623 var test
= new Array[String]
1624 var t1
= value1
.mcasttype
1625 if t1
isa MNullableType then
1626 test
.add
("{value1} != NULL")
1631 var t2
= value2
.mcasttype
1632 if t2
isa MNullableType then
1633 test
.add
("{value2} != NULL")
1639 var incompatible
= false
1641 if t1
.ctype
!= "val*" then
1644 # No need to compare class
1645 else if t2
.ctype
!= "val*" then
1647 else if can_be_primitive
(value2
) then
1648 test
.add
("{value1}->class == {value2}->class")
1652 else if t2
.ctype
!= "val*" then
1654 if can_be_primitive
(value1
) then
1655 test
.add
("{value1}->class == {value2}->class")
1663 if incompatible
then
1665 self.add
("{res} = {value1} == {value2}; /* incompatible types {t1} vs. {t2}; but may be NULL*/")
1668 self.add
("{res} = 0; /* incompatible types {t1} vs. {t2}; cannot be NULL */")
1672 if primitive
!= null then
1673 test
.add
("((struct instance_{primitive.c_name}*){value1})->value == ((struct instance_{primitive.c_name}*){value2})->value")
1674 else if can_be_primitive
(value1
) and can_be_primitive
(value2
) then
1675 test
.add
("{value1}->class == {value2}->class")
1676 var s
= new Array[String]
1677 for t
, v
in self.compiler
.as(SeparateCompiler).box_kinds
do
1678 s
.add
"({value1}->class->box_kind == {v} && ((struct instance_{t.c_name}*){value1})->value == ((struct instance_{t.c_name}*){value2})->value)"
1680 test
.add
("({s.join(" || ")})")
1682 self.add
("{res} = {value1} == {value2};")
1685 self.add
("{res} = {value1} == {value2} || ({test.join(" && ")});")
1689 fun can_be_primitive
(value
: RuntimeVariable): Bool
1691 var t
= value
.mcasttype
1692 if t
isa MNullableType then t
= t
.mtype
1693 if not t
isa MClassType then return false
1694 var k
= t
.mclass
.kind
1695 return k
== interface_kind
or t
.ctype
!= "val*"
1698 fun maybe_null
(value
: RuntimeVariable): Bool
1700 var t
= value
.mcasttype
1701 return t
isa MNullableType or t
isa MNullType
1704 redef fun array_instance
(array
, elttype
)
1706 var nclass
= self.get_class
("NativeArray")
1707 var arrayclass
= self.get_class
("Array")
1708 var arraytype
= arrayclass
.get_mtype
([elttype
])
1709 var res
= self.init_instance
(arraytype
)
1710 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1711 var length
= self.int_instance
(array
.length
)
1712 var nat
= native_array_instance
(elttype
, length
)
1713 for i
in [0..array
.length
[ do
1714 var r
= self.autobox
(array
[i
], self.object_type
)
1715 self.add
("((struct instance_{nclass.c_name}*){nat})->values[{i}] = (val*) {r};")
1717 self.send
(self.get_property
("with_native", arrayclass
.intro
.bound_mtype
), [res
, nat
, length
])
1718 self.check_init_instance
(res
, arraytype
)
1723 fun native_array_instance
(elttype
: MType, length
: RuntimeVariable): RuntimeVariable
1725 var mtype
= self.get_class
("NativeArray").get_mtype
([elttype
])
1726 assert mtype
isa MGenericType
1727 var compiler
= self.compiler
.as(SeparateCompiler)
1728 if mtype
.need_anchor
then
1729 if compiler
.modelbuilder
.toolcontext
.opt_generic_tree
.value
then
1730 var buff
= new Buffer
1731 retrieve_anchored_livetype
(mtype
, buff
)
1732 mtype
= self.anchor
(mtype
).as(MClassType)
1733 return self.new_expr
("NEW_{mtype.mclass.c_name}({length}, (struct type *) livetypes_{mtype.mclass.c_name}{buff.to_s})", mtype
)
1735 link_unanchored_type
(self.frame
.mpropdef
.mclassdef
, mtype
)
1736 var recv
= self.frame
.arguments
.first
1737 var recv_type_info
= self.type_info
(recv
)
1738 if compiler
.modelbuilder
.toolcontext
.opt_phmod_typing
.value
or compiler
.modelbuilder
.toolcontext
.opt_phand_typing
.value
then
1739 return self.new_expr
("NEW_{mtype.mclass.c_name}({length}, (struct type *) {recv_type_info}->unanchored_table->types[HASH({recv_type_info}->unanchored_table->mask, {mtype.const_color})])", mtype
)
1741 return self.new_expr
("NEW_{mtype.mclass.c_name}({length}, (struct type *) {recv_type_info}->unanchored_table->types[{mtype.const_color}])", mtype
)
1745 compiler
.undead_types
.add
(mtype
)
1746 return self.new_expr
("NEW_{mtype.mclass.c_name}({length}, (struct type *) &type_{mtype.c_name})", mtype
)
1749 redef fun native_array_def
(pname
, ret_type
, arguments
)
1751 var elttype
= arguments
.first
.mtype
1752 var nclass
= self.get_class
("NativeArray")
1753 var recv
= "((struct instance_{nclass.c_name}*){arguments[0]})->values"
1754 if pname
== "[]" then
1755 self.ret
(self.new_expr
("{recv}[{arguments[1]}]", ret_type
.as(not null)))
1757 else if pname
== "[]=" then
1758 self.add
("{recv}[{arguments[1]}]={arguments[2]};")
1760 else if pname
== "copy_to" then
1761 var recv1
= "((struct instance_{nclass.c_name}*){arguments[1]})->values"
1762 self.add
("memcpy({recv1}, {recv}, {arguments[2]}*sizeof({elttype.ctype}));")
1767 redef fun calloc_array
(ret_type
, arguments
)
1769 var mclass
= self.get_class
("ArrayCapable")
1770 var ft
= mclass
.mclass_type
.arguments
.first
.as(MParameterType)
1771 var res
= self.native_array_instance
(ft
, arguments
[1])
1775 fun link_unanchored_type
(mclassdef
: MClassDef, mtype
: MType) do
1776 assert mtype
.need_anchor
1777 var compiler
= self.compiler
.as(SeparateCompiler)
1778 if not compiler
.live_unanchored_types
.has_key
(self.frame
.mpropdef
.mclassdef
) then
1779 compiler
.live_unanchored_types
[self.frame
.mpropdef
.mclassdef
] = new HashSet[MType]
1781 compiler
.live_unanchored_types
[self.frame
.mpropdef
.mclassdef
].add
(mtype
)
1786 # Return the name of the C structure associated to a Nit class
1787 fun c_name
: String do
1788 var res
= self.c_name_cache
1789 if res
!= null then return res
1790 res
= "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1791 self.c_name_cache
= res
1794 private var c_name_cache
: nullable String
1798 fun const_color
: String do return "COLOR_{c_name}"
1801 redef class MParameterType
1804 var res
= self.c_name_cache
1805 if res
!= null then return res
1806 res
= "{self.mclass.c_name}_FT{self.rank}"
1807 self.c_name_cache
= res
1812 redef class MVirtualType
1815 var res
= self.c_name_cache
1816 if res
!= null then return res
1817 res
= "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1818 self.c_name_cache
= res
1823 redef class MNullableType
1826 var res
= self.c_name_cache
1827 if res
!= null then return res
1828 res
= "nullable_{self.mtype.c_name}"
1829 self.c_name_cache
= res
1834 redef class MProperty
1835 fun c_name
: String do
1836 var res
= self.c_name_cache
1837 if res
!= null then return res
1838 res
= "{self.intro.c_name}"
1839 self.c_name_cache
= res
1842 private var c_name_cache
: nullable String
1844 fun const_color
: String do return "COLOR_{c_name}"