typing: allow `new` on interface and abstract classes that have a `new`-factory
[nit.git] / src / compiler / separate_erasure_compiler.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Separate compilation of a Nit program with generic type erasure
16 module separate_erasure_compiler
17
18 intrude import separate_compiler
19
20 # Add separate erased compiler specific options
21 redef class ToolContext
22 # --erasure
23 var opt_erasure = new OptionBool("Erase generic types", "--erasure")
24 # --rta
25 var opt_rta = new OptionBool("Activate RTA (implicit with --global and --separate)", "--rta")
26 # --no-check-erasure-cast
27 var opt_no_check_erasure_cast = new OptionBool("Disable implicit casts on unsafe return with erasure-typing policy (dangerous)", "--no-check-erasure-cast")
28
29 redef init
30 do
31 super
32 self.option_context.add_option(self.opt_erasure, self.opt_no_check_erasure_cast, opt_rta)
33 end
34
35 redef fun process_options(args)
36 do
37 super
38
39 if opt_no_check_all.value then
40 opt_no_check_erasure_cast.value = true
41 end
42 end
43
44 var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
45 end
46
47 class ErasureCompilerPhase
48 super Phase
49 redef fun process_mainmodule(mainmodule, given_mmodules) do
50 if not toolcontext.opt_erasure.value then return
51
52 var modelbuilder = toolcontext.modelbuilder
53 var analysis = null
54 if toolcontext.opt_rta.value then
55 analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
56 end
57 modelbuilder.run_separate_erasure_compiler(mainmodule, analysis)
58 end
59 end
60
61 redef class ModelBuilder
62 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: nullable RapidTypeAnalysis)
63 do
64 var time0 = get_time
65 self.toolcontext.info("*** GENERATING C ***", 1)
66
67 var compiler = new SeparateErasureCompiler(mainmodule, self, runtime_type_analysis)
68 compiler.compile_header
69
70 # compile class structures
71 self.toolcontext.info("Property coloring", 2)
72 compiler.new_file("{mainmodule.name}.tables")
73 compiler.do_property_coloring
74 for m in mainmodule.in_importation.greaters do
75 for mclass in m.intro_mclasses do
76 compiler.compile_class_to_c(mclass)
77 end
78 end
79 compiler.compile_color_consts(compiler.vt_colors)
80
81 # The main function of the C
82 compiler.new_file("{mainmodule.name}.main")
83 compiler.compile_nitni_global_ref_functions
84 compiler.compile_main_function
85
86 # compile methods
87 for m in mainmodule.in_importation.greaters do
88 self.toolcontext.info("Generate C for module {m}", 2)
89 compiler.new_file("{m.name}.sep")
90 compiler.compile_module_to_c(m)
91 end
92
93 compiler.display_stats
94
95 var time1 = get_time
96 self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
97 write_and_make(compiler)
98 end
99 end
100
101 class SeparateErasureCompiler
102 super SeparateCompiler
103
104 private var class_ids: Map[MClass, Int]
105 private var class_colors: Map[MClass, Int]
106 protected var vt_colors: Map[MVirtualTypeProp, Int]
107
108 init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: nullable RapidTypeAnalysis) do
109 super
110
111 # Class coloring
112 var poset = mainmodule.flatten_mclass_hierarchy
113 var mclasses = new HashSet[MClass].from(poset)
114 var colorer = new POSetColorer[MClass]
115 colorer.colorize(poset)
116 class_ids = colorer.ids
117 class_colors = colorer.colors
118 class_tables = self.build_class_typing_tables(mclasses)
119
120 # lookup vt to build layout with
121 var vts = new HashMap[MClass, Set[MVirtualTypeProp]]
122 for mclass in mclasses do
123 vts[mclass] = new HashSet[MVirtualTypeProp]
124 for mprop in self.mainmodule.properties(mclass) do
125 if mprop isa MVirtualTypeProp then
126 vts[mclass].add(mprop)
127 end
128 end
129 end
130
131 # vt coloration
132 var vt_colorer = new POSetBucketsColorer[MClass, MVirtualTypeProp](poset, colorer.conflicts)
133 vt_colors = vt_colorer.colorize(vts)
134 vt_tables = build_vt_tables(mclasses)
135 end
136
137 fun build_vt_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MPropDef]] do
138 var tables = new HashMap[MClass, Array[nullable MPropDef]]
139 for mclass in mclasses do
140 var table = new Array[nullable MPropDef]
141 # first, fill table from parents by reverse linearization order
142 var parents = new Array[MClass]
143 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
144 parents = mclass.in_hierarchy(mainmodule).greaters.to_a
145 self.mainmodule.linearize_mclasses(parents)
146 end
147 for parent in parents do
148 if parent == mclass then continue
149 for mproperty in self.mainmodule.properties(parent) do
150 if not mproperty isa MVirtualTypeProp then continue
151 var color = vt_colors[mproperty]
152 if table.length <= color then
153 for i in [table.length .. color[ do
154 table[i] = null
155 end
156 end
157 for mpropdef in mproperty.mpropdefs do
158 if mpropdef.mclassdef.mclass == parent then
159 table[color] = mpropdef
160 end
161 end
162 end
163 end
164
165 # then override with local properties
166 for mproperty in self.mainmodule.properties(mclass) do
167 if not mproperty isa MVirtualTypeProp then continue
168 var color = vt_colors[mproperty]
169 if table.length <= color then
170 for i in [table.length .. color[ do
171 table[i] = null
172 end
173 end
174 for mpropdef in mproperty.mpropdefs do
175 if mpropdef.mclassdef.mclass == mclass then
176 table[color] = mpropdef
177 end
178 end
179 end
180 tables[mclass] = table
181 end
182 return tables
183 end
184
185 # Build class tables
186 fun build_class_typing_tables(mclasses: Set[MClass]): Map[MClass, Array[nullable MClass]] do
187 var tables = new HashMap[MClass, Array[nullable MClass]]
188 for mclass in mclasses do
189 var table = new Array[nullable MClass]
190 var supers = new Array[MClass]
191 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
192 supers = mclass.in_hierarchy(mainmodule).greaters.to_a
193 end
194 for sup in supers do
195 var color = class_colors[sup]
196 if table.length <= color then
197 for i in [table.length .. color[ do
198 table[i] = null
199 end
200 end
201 table[color] = sup
202 end
203 tables[mclass] = table
204 end
205 return tables
206 end
207
208 redef fun compile_header_structs do
209 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
210 self.compile_header_attribute_structs
211 self.header.add_decl("struct class \{ int id; const char *name; int box_kind; int color; const struct vts_table *vts_table; const struct type_table *type_table; nitmethod_t vft[]; \}; /* general C type representing a Nit class. */")
212 self.header.add_decl("struct type_table \{ int size; int table[]; \}; /* colorized type table. */")
213 self.header.add_decl("struct vts_entry \{ short int is_nullable; const struct class *class; \}; /* link (nullable or not) between the vts and is bound. */")
214 self.header.add_decl("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
215 self.header.add_decl("typedef struct instance \{ const struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
216 end
217
218 redef fun compile_class_to_c(mclass: MClass)
219 do
220 var mtype = mclass.intro.bound_mtype
221 var c_name = mclass.c_name
222
223 var vft = self.method_tables[mclass]
224 var attrs = self.attr_tables[mclass]
225 var class_table = self.class_tables[mclass]
226 var v = self.new_visitor
227
228 var rta = runtime_type_analysis
229 var is_dead = false # mclass.kind == abstract_kind or mclass.kind == interface_kind
230 if not is_dead and rta != null and not rta.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray" then
231 is_dead = true
232 end
233
234 v.add_decl("/* runtime class {c_name} */")
235
236 self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
237 v.add_decl("extern const struct type_table type_table_{c_name};")
238
239 # Build class vft
240 v.add_decl("const struct class class_{c_name} = \{")
241 v.add_decl("{class_ids[mclass]},")
242 v.add_decl("\"{mclass.name}\", /* class_name_string */")
243 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
244 v.add_decl("{class_colors[mclass]},")
245 if not is_dead then
246 if build_class_vts_table(mclass) then
247 v.require_declaration("vts_table_{c_name}")
248 v.add_decl("&vts_table_{c_name},")
249 else
250 v.add_decl("NULL,")
251 end
252 v.add_decl("&type_table_{c_name},")
253 v.add_decl("\{")
254 for i in [0 .. vft.length[ do
255 var mpropdef = vft[i]
256 if mpropdef == null then
257 v.add_decl("NULL, /* empty */")
258 else
259 assert mpropdef isa MMethodDef
260 if rta != null and not rta.live_methoddefs.has(mpropdef) then
261 v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
262 continue
263 end
264 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
265 v.require_declaration("VIRTUAL_{mpropdef.c_name}")
266 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
267 else
268 v.require_declaration("{mpropdef.c_name}")
269 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
270 end
271 end
272 end
273 v.add_decl("\}")
274 end
275 v.add_decl("\};")
276
277 # Build class type table
278
279 v.add_decl("const struct type_table type_table_{c_name} = \{")
280 v.add_decl("{class_table.length},")
281 v.add_decl("\{")
282 for msuper in class_table do
283 if msuper == null then
284 v.add_decl("-1, /* empty */")
285 else
286 v.add_decl("{self.class_ids[msuper]}, /* {msuper} */")
287 end
288 end
289 v.add_decl("\}")
290 v.add_decl("\};")
291
292 if mtype.ctype != "val*" or mtype.mclass.name == "Pointer" then
293 #Build instance struct
294 self.header.add_decl("struct instance_{c_name} \{")
295 self.header.add_decl("const struct class *class;")
296 self.header.add_decl("{mtype.ctype} value;")
297 self.header.add_decl("\};")
298
299 #Build BOX
300 self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
301 v.add_decl("/* allocate {mtype} */")
302 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
303 v.add("struct instance_{c_name}*res = nit_alloc(sizeof(struct instance_{c_name}));")
304 v.require_declaration("class_{c_name}")
305 v.add("res->class = &class_{c_name};")
306 v.add("res->value = value;")
307 v.add("return (val*)res;")
308 v.add("\}")
309
310 if mtype.mclass.name != "Pointer" then return
311
312 v = new_visitor
313 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
314 v.add_decl("/* allocate {mtype} */")
315 v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
316 if is_dead then
317 v.add_abort("{mclass} is DEAD")
318 else
319 var res = v.new_named_var(mtype, "self")
320 res.is_exact = true
321 v.add("{res} = nit_alloc(sizeof(struct instance_{mtype.c_name}));")
322 v.require_declaration("class_{c_name}")
323 v.add("{res}->class = &class_{c_name};")
324 v.add("((struct instance_{mtype.c_name}*){res})->value = NULL;")
325 v.add("return {res};")
326 end
327 v.add("\}")
328 return
329 else if mclass.name == "NativeArray" then
330 #Build instance struct
331 self.header.add_decl("struct instance_{c_name} \{")
332 self.header.add_decl("const struct class *class;")
333 self.header.add_decl("int length;")
334 self.header.add_decl("val* values[];")
335 self.header.add_decl("\};")
336
337 #Build NEW
338 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length);")
339 v.add_decl("/* allocate {mtype} */")
340 v.add_decl("{mtype.ctype} NEW_{c_name}(int length) \{")
341 var res = v.get_name("self")
342 v.add_decl("struct instance_{c_name} *{res};")
343 var mtype_elt = mtype.arguments.first
344 v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
345 v.require_declaration("class_{c_name}")
346 v.add("{res}->class = &class_{c_name};")
347 v.add("{res}->length = length;")
348 v.add("return (val*){res};")
349 v.add("\}")
350 return
351 else if mtype.mclass.kind == extern_kind and mtype.mclass.name != "NativeString" then
352 var pointer_type = mainmodule.pointer_type
353
354 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}();")
355 v.add_decl("/* allocate {mtype} */")
356 v.add_decl("{mtype.ctype} NEW_{c_name}() \{")
357 if is_dead then
358 v.add_abort("{mclass} is DEAD")
359 else
360 var res = v.new_named_var(mtype, "self")
361 res.is_exact = true
362 v.add("{res} = nit_alloc(sizeof(struct instance_{pointer_type.c_name}));")
363 #v.add("{res}->type = type;")
364 v.require_declaration("class_{c_name}")
365 v.add("{res}->class = &class_{c_name};")
366 v.add("((struct instance_{pointer_type.c_name}*){res})->value = NULL;")
367 v.add("return {res};")
368 end
369 v.add("\}")
370 return
371 end
372
373 #Build NEW
374 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(void);")
375 v.add_decl("/* allocate {mtype} */")
376 v.add_decl("{mtype.ctype} NEW_{c_name}(void) \{")
377 if is_dead then
378 v.add_abort("{mclass} is DEAD")
379 else
380
381 var res = v.new_named_var(mtype, "self")
382 res.is_exact = true
383 v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
384 v.require_declaration("class_{c_name}")
385 v.add("{res}->class = &class_{c_name};")
386 self.generate_init_attr(v, res, mtype)
387 v.set_finalizer res
388 v.add("return {res};")
389 end
390 v.add("\}")
391 end
392
393 private fun build_class_vts_table(mclass: MClass): Bool do
394 if self.vt_tables[mclass].is_empty then return false
395
396 self.provide_declaration("vts_table_{mclass.c_name}", "extern const struct vts_table vts_table_{mclass.c_name};")
397
398 var v = new_visitor
399 v.add_decl("const struct vts_table vts_table_{mclass.c_name} = \{")
400 v.add_decl("0, /* dummy */")
401 v.add_decl("\{")
402
403 for vt in self.vt_tables[mclass] do
404 if vt == null then
405 v.add_decl("\{-1, NULL\}, /* empty */")
406 else
407 var is_null = 0
408 var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.as(MVirtualTypeDef).bound)
409 while bound isa MNullableType do
410 bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
411 is_null = 1
412 end
413 var vtclass = bound.as(MClassType).mclass
414 v.require_declaration("class_{vtclass.c_name}")
415 v.add_decl("\{{is_null}, &class_{vtclass.c_name}\}, /* {vt} */")
416 end
417 end
418 v.add_decl("\},")
419 v.add_decl("\};")
420 return true
421 end
422
423 private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
424 if mtype == null then
425 print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
426 abort
427 end
428 if mtype isa MVirtualType then
429 return mtype.anchor_to(mainmodule, anchor)
430 else if mtype isa MParameterType then
431 return mtype.anchor_to(mainmodule, anchor)
432 else
433 return mtype
434 end
435 end
436
437 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
438
439 # Stats
440
441 private var class_tables: Map[MClass, Array[nullable MClass]]
442 private var vt_tables: Map[MClass, Array[nullable MPropDef]]
443
444 redef fun display_sizes
445 do
446 print "# size of subtyping tables"
447 print "\ttotal \tholes"
448 var total = 0
449 var holes = 0
450 for t, table in class_tables do
451 total += table.length
452 for e in table do if e == null then holes += 1
453 end
454 print "\t{total}\t{holes}"
455
456 print "# size of resolution tables"
457 print "\ttotal \tholes"
458 total = 0
459 holes = 0
460 for t, table in vt_tables do
461 total += table.length
462 for e in table do if e == null then holes += 1
463 end
464 print "\t{total}\t{holes}"
465
466 print "# size of methods tables"
467 print "\ttotal \tholes"
468 total = 0
469 holes = 0
470 for t, table in method_tables do
471 total += table.length
472 for e in table do if e == null then holes += 1
473 end
474 print "\t{total}\t{holes}"
475
476 print "# size of attributes tables"
477 print "\ttotal \tholes"
478 total = 0
479 holes = 0
480 for t, table in attr_tables do
481 total += table.length
482 for e in table do if e == null then holes += 1
483 end
484 print "\t{total}\t{holes}"
485 end
486 end
487
488 class SeparateErasureCompilerVisitor
489 super SeparateCompilerVisitor
490
491 redef fun compile_callsite(callsite, arguments)
492 do
493 var res = super
494 if callsite.erasure_cast and not self.compiler.as(SeparateErasureCompiler).modelbuilder.toolcontext.opt_no_check_erasure_cast.value then
495 assert res != null
496 var mtype = callsite.msignature.return_mtype
497 assert mtype != null
498 self.add("/* Erasure cast for return {res} isa {mtype} */")
499 var cond = self.type_test(res, mtype, "erasure")
500 self.add("if (!{cond}) \{")
501 #var x = self.class_name_string(res)
502 #var y = self.class_name_string(arguments.first)
503 #self.add("PRINT_ERROR(\"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
504 self.add_abort("Cast failed")
505 self.add("\}")
506 end
507 return res
508 end
509
510 redef fun init_instance(mtype)
511 do
512 self.require_declaration("NEW_{mtype.mclass.c_name}")
513 return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
514 end
515
516 redef fun type_test(value, mtype, tag)
517 do
518 self.add("/* type test for {value.inspect} isa {mtype} */")
519
520 var res = self.new_var(bool_type)
521
522 var cltype = self.get_name("cltype")
523 self.add_decl("int {cltype};")
524 var idtype = self.get_name("idtype")
525 self.add_decl("int {idtype};")
526
527 var maybe_null = self.maybe_null(value)
528 var accept_null = "0"
529 if mtype isa MNullableType then
530 mtype = mtype.mtype
531 accept_null = "1"
532 end
533 if mtype isa MParameterType then
534 # Here we get the bound of the the formal type (eh, erasure...)
535 mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, false)
536 if mtype isa MNullableType then
537 mtype = mtype.mtype
538 accept_null = "1"
539 end
540 end
541
542 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
543 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
544 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
545 self.compiler.count_type_test_skipped[tag] += 1
546 self.add("count_type_test_skipped_{tag}++;")
547 end
548 return res
549 end
550
551 var class_ptr
552 if value.mtype.ctype == "val*" then
553 class_ptr = "{value}->class->"
554 else
555 var mclass = value.mtype.as(MClassType).mclass
556 self.require_declaration("class_{mclass.c_name}")
557 class_ptr = "class_{mclass.c_name}."
558 end
559
560 if mtype isa MClassType then
561 self.require_declaration("class_{mtype.mclass.c_name}")
562 self.add("{cltype} = class_{mtype.mclass.c_name}.color;")
563 self.add("{idtype} = class_{mtype.mclass.c_name}.id;")
564 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
565 self.compiler.count_type_test_resolved[tag] += 1
566 self.add("count_type_test_resolved_{tag}++;")
567 end
568 else if mtype isa MVirtualType then
569 var recv = self.frame.arguments.first
570 var recv_ptr
571 if recv.mtype.ctype == "val*" then
572 recv_ptr = "{recv}->class->"
573 else
574 var mclass = recv.mtype.as(MClassType).mclass
575 self.require_declaration("class_{mclass.c_name}")
576 recv_ptr = "class_{mclass.c_name}."
577 end
578 var entry = self.get_name("entry")
579 self.add("struct vts_entry {entry};")
580 self.require_declaration(mtype.mproperty.const_color)
581 self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
582 self.add("{cltype} = {entry}.class->color;")
583 self.add("{idtype} = {entry}.class->id;")
584 if maybe_null and accept_null == "0" then
585 var is_nullable = self.get_name("is_nullable")
586 self.add_decl("short int {is_nullable};")
587 self.add("{is_nullable} = {entry}.is_nullable;")
588 accept_null = is_nullable.to_s
589 end
590 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
591 self.compiler.count_type_test_unresolved[tag] += 1
592 self.add("count_type_test_unresolved_{tag}++;")
593 end
594 else
595 self.debug("type_test({value.inspect}, {mtype})")
596 abort
597 end
598
599 # check color is in table
600 if maybe_null then
601 self.add("if({value} == NULL) \{")
602 self.add("{res} = {accept_null};")
603 self.add("\} else \{")
604 end
605 self.add("if({cltype} >= {class_ptr}type_table->size) \{")
606 self.add("{res} = 0;")
607 self.add("\} else \{")
608 self.add("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
609 self.add("\}")
610 if maybe_null then
611 self.add("\}")
612 end
613
614 return res
615 end
616
617 redef fun unbox_extern(value, mtype)
618 do
619 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
620 mtype.mclass.name != "NativeString" then
621 var pointer_type = compiler.mainmodule.pointer_type
622 var res = self.new_var_extern(mtype)
623 self.add "{res} = ((struct instance_{pointer_type.c_name}*){value})->value; /* unboxing {value.mtype} */"
624 return res
625 else
626 return value
627 end
628 end
629
630 redef fun box_extern(value, mtype)
631 do
632 if mtype isa MClassType and mtype.mclass.kind == extern_kind and
633 mtype.mclass.name != "NativeString" then
634 var valtype = compiler.mainmodule.pointer_type
635 var res = self.new_var(mtype)
636 if compiler.runtime_type_analysis != null and not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
637 self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
638 self.add("PRINT_ERROR(\"Dead code executed!\\n\"); show_backtrace(1);")
639 return res
640 end
641 self.require_declaration("BOX_{valtype.c_name}")
642 self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
643 self.require_declaration("class_{mtype.c_name}")
644 self.add("{res}->class = &class_{mtype.c_name};")
645 return res
646 else
647 return value
648 end
649 end
650
651 redef fun class_name_string(value)
652 do
653 var res = self.get_name("var_class_name")
654 self.add_decl("const char* {res};")
655 if value.mtype.ctype == "val*" then
656 self.add "{res} = {value} == NULL ? \"null\" : {value}->class->name;"
657 else
658 self.require_declaration("class_{value.mtype.c_name}")
659 self.add "{res} = class_{value.mtype.c_name}.name;"
660 end
661 return res
662 end
663
664 redef fun native_array_instance(elttype, length)
665 do
666 var nclass = self.get_class("NativeArray")
667 var mtype = nclass.get_mtype([elttype])
668 var res = self.new_var(mtype)
669 res.is_exact = true
670 self.require_declaration("NEW_{nclass.c_name}")
671 self.add("{res} = NEW_{nclass.c_name}({length});")
672 return res
673 end
674
675 redef fun calloc_array(ret_type, arguments)
676 do
677 var ret = ret_type.as(MClassType)
678 self.require_declaration("NEW_{ret.mclass.c_name}")
679 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
680 end
681 end