lib/gtk: improve documentation on main modules
[nit.git] / src / 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: OptionBool = 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: OptionBool = 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 var erasure_compiler_phase = new ErasureCompilerPhase(self, null)
36 end
37
38 class ErasureCompilerPhase
39 super Phase
40 redef fun process_mainmodule(mainmodule, given_mmodules) do
41 if not toolcontext.opt_erasure.value then return
42
43 var modelbuilder = toolcontext.modelbuilder
44 var analysis = null
45 if toolcontext.opt_rta.value then
46 analysis = modelbuilder.do_rapid_type_analysis(mainmodule)
47 end
48 modelbuilder.run_separate_erasure_compiler(mainmodule, analysis)
49 end
50 end
51
52 redef class ModelBuilder
53 fun run_separate_erasure_compiler(mainmodule: MModule, runtime_type_analysis: nullable RapidTypeAnalysis)
54 do
55 var time0 = get_time
56 self.toolcontext.info("*** GENERATING C ***", 1)
57
58 var compiler = new SeparateErasureCompiler(mainmodule, self, runtime_type_analysis)
59 compiler.compile_header
60
61 # compile class structures
62 self.toolcontext.info("Property coloring", 2)
63 compiler.new_file("{mainmodule.name}.tables")
64 compiler.do_property_coloring
65 for m in mainmodule.in_importation.greaters do
66 for mclass in m.intro_mclasses do
67 compiler.compile_class_to_c(mclass)
68 end
69 end
70 compiler.compile_color_consts(compiler.vt_layout.pos)
71
72 # The main function of the C
73 compiler.new_file("{mainmodule.name}.main")
74 compiler.compile_main_function
75
76 # compile methods
77 for m in mainmodule.in_importation.greaters do
78 self.toolcontext.info("Generate C for module {m}", 2)
79 compiler.new_file("{m.name}.sep")
80 compiler.compile_module_to_c(m)
81 end
82
83 compiler.display_stats
84
85 var time1 = get_time
86 self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
87 write_and_make(compiler)
88 end
89 end
90
91 class SeparateErasureCompiler
92 super SeparateCompiler
93
94 private var class_layout: nullable Layout[MClass]
95 protected var vt_layout: nullable Layout[MVirtualTypeProp]
96
97 init(mainmodule: MModule, mmbuilder: ModelBuilder, runtime_type_analysis: nullable RapidTypeAnalysis) do
98 super
99
100 var mclasses = new HashSet[MClass].from(mmbuilder.model.mclasses)
101
102 var layout_builder: TypingLayoutBuilder[MClass]
103 var class_colorer = new MClassColorer(mainmodule)
104 if modelbuilder.toolcontext.opt_phmod_typing.value then
105 layout_builder = new MClassHasher(new PHModOperator, mainmodule)
106 class_colorer.build_layout(mclasses)
107 else if modelbuilder.toolcontext.opt_phand_typing.value then
108 layout_builder = new MClassHasher(new PHAndOperator, mainmodule)
109 class_colorer.build_layout(mclasses)
110 else if modelbuilder.toolcontext.opt_bm_typing.value then
111 layout_builder = new MClassBMizer(mainmodule)
112 class_colorer.build_layout(mclasses)
113 else
114 layout_builder = class_colorer
115 end
116 self.class_layout = layout_builder.build_layout(mclasses)
117 self.class_tables = self.build_class_typing_tables(mclasses)
118
119 # lookup vt to build layout with
120 var vts = new HashMap[MClass, Set[MVirtualTypeProp]]
121 for mclass in mclasses do
122 vts[mclass] = new HashSet[MVirtualTypeProp]
123 for mprop in self.mainmodule.properties(mclass) do
124 if mprop isa MVirtualTypeProp then
125 vts[mclass].add(mprop)
126 end
127 end
128 end
129
130 # vt coloration
131 var vt_coloring = new MPropertyColorer[MVirtualTypeProp](mainmodule, class_colorer)
132 var vt_layout = vt_coloring.build_layout(vts)
133 self.vt_tables = build_vt_tables(mclasses, vt_layout)
134 self.vt_layout = vt_layout
135 end
136
137 fun build_vt_tables(mclasses: Set[MClass], layout: Layout[MProperty]): 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 = layout.pos[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 = layout.pos[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 var layout = self.class_layout
189 for mclass in mclasses do
190 var table = new Array[nullable MClass]
191 var supers = new Array[MClass]
192 if mainmodule.flatten_mclass_hierarchy.has(mclass) then
193 supers = mclass.in_hierarchy(mainmodule).greaters.to_a
194 end
195 for sup in supers do
196 var color: Int
197 if layout isa PHLayout[MClass, MClass] then
198 color = layout.hashes[mclass][sup]
199 else
200 color = layout.pos[sup]
201 end
202 if table.length <= color then
203 for i in [table.length .. color[ do
204 table[i] = null
205 end
206 end
207 table[color] = sup
208 end
209 tables[mclass] = table
210 end
211 return tables
212 end
213
214 redef fun compile_header_structs do
215 self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
216 self.compile_header_attribute_structs
217 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. */")
218 self.header.add_decl("struct type_table \{ int size; int table[]; \}; /* colorized type table. */")
219 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. */")
220
221 if self.vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
222 self.header.add_decl("struct vts_table \{ int mask; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
223 else
224 self.header.add_decl("struct vts_table \{ int dummy; const struct vts_entry vts[]; \}; /* vts list of a C type representation. */")
225 end
226
227 if modelbuilder.toolcontext.opt_phmod_typing.value then
228 self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
229 else if modelbuilder.toolcontext.opt_phand_typing.value then
230 self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
231 end
232
233 self.header.add_decl("typedef struct instance \{ const struct class *class; nitattribute_t attrs[1]; \} val; /* general C type representing a Nit instance. */")
234 end
235
236 redef fun compile_class_to_c(mclass: MClass)
237 do
238 var mtype = mclass.intro.bound_mtype
239 var c_name = mclass.c_name
240 var c_instance_name = mclass.c_instance_name
241
242 var vft = self.method_tables[mclass]
243 var attrs = self.attr_tables[mclass]
244 var class_table = self.class_tables[mclass]
245 var v = self.new_visitor
246
247 var rta = runtime_type_analysis
248 var is_dead = mclass.kind == abstract_kind or mclass.kind == interface_kind
249 if not is_dead and rta != null and not rta.live_classes.has(mclass) and mtype.ctype == "val*" and mclass.name != "NativeArray" then
250 is_dead = true
251 end
252
253 v.add_decl("/* runtime class {c_name} */")
254
255 self.provide_declaration("class_{c_name}", "extern const struct class class_{c_name};")
256 v.add_decl("extern const struct type_table type_table_{c_name};")
257
258 # Build class vft
259 v.add_decl("const struct class class_{c_name} = \{")
260 v.add_decl("{self.class_layout.ids[mclass]},")
261 v.add_decl("\"{mclass.name}\", /* class_name_string */")
262 v.add_decl("{self.box_kind_of(mclass)}, /* box_kind */")
263 var layout = self.class_layout
264 if layout isa PHLayout[MClass, MClass] then
265 v.add_decl("{layout.masks[mclass]},")
266 else
267 v.add_decl("{layout.pos[mclass]},")
268 end
269 if not is_dead then
270 if build_class_vts_table(mclass) then
271 v.require_declaration("vts_table_{c_name}")
272 v.add_decl("&vts_table_{c_name},")
273 else
274 v.add_decl("NULL,")
275 end
276 v.add_decl("&type_table_{c_name},")
277 v.add_decl("\{")
278 for i in [0 .. vft.length[ do
279 var mpropdef = vft[i]
280 if mpropdef == null then
281 v.add_decl("NULL, /* empty */")
282 else
283 assert mpropdef isa MMethodDef
284 if rta != null and not rta.live_methoddefs.has(mpropdef) then
285 v.add_decl("NULL, /* DEAD {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
286 continue
287 end
288 if true or mpropdef.mclassdef.bound_mtype.ctype != "val*" then
289 v.require_declaration("VIRTUAL_{mpropdef.c_name}")
290 v.add_decl("(nitmethod_t)VIRTUAL_{mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
291 else
292 v.require_declaration("{mpropdef.c_name}")
293 v.add_decl("(nitmethod_t){mpropdef.c_name}, /* pointer to {mclass.intro_mmodule}:{mclass}:{mpropdef} */")
294 end
295 end
296 end
297 v.add_decl("\}")
298 end
299 v.add_decl("\};")
300
301 # Build class type table
302
303 v.add_decl("const struct type_table type_table_{c_name} = \{")
304 v.add_decl("{class_table.length},")
305 v.add_decl("\{")
306 for msuper in class_table do
307 if msuper == null then
308 v.add_decl("-1, /* empty */")
309 else
310 v.add_decl("{self.class_layout.ids[msuper]}, /* {msuper} */")
311 end
312 end
313 v.add_decl("\}")
314 v.add_decl("\};")
315
316 if mtype.ctype != "val*" then
317 if mtype.mclass.name == "Pointer" or mtype.mclass.kind != extern_kind then
318 #Build instance struct
319 self.header.add_decl("struct instance_{c_instance_name} \{")
320 self.header.add_decl("const struct class *class;")
321 self.header.add_decl("{mtype.ctype} value;")
322 self.header.add_decl("\};")
323 end
324
325 #Build BOX
326 self.provide_declaration("BOX_{c_name}", "val* BOX_{c_name}({mtype.ctype});")
327 v.add_decl("/* allocate {mtype} */")
328 v.add_decl("val* BOX_{mtype.c_name}({mtype.ctype} value) \{")
329 v.add("struct instance_{c_instance_name}*res = nit_alloc(sizeof(struct instance_{c_instance_name}));")
330 v.require_declaration("class_{c_name}")
331 v.add("res->class = &class_{c_name};")
332 v.add("res->value = value;")
333 v.add("return (val*)res;")
334 v.add("\}")
335 return
336 else if mclass.name == "NativeArray" then
337 #Build instance struct
338 self.header.add_decl("struct instance_{c_name} \{")
339 self.header.add_decl("const struct class *class;")
340 self.header.add_decl("int length;")
341 self.header.add_decl("val* values[];")
342 self.header.add_decl("\};")
343
344 #Build NEW
345 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(int length);")
346 v.add_decl("/* allocate {mtype} */")
347 v.add_decl("{mtype.ctype} NEW_{c_name}(int length) \{")
348 var res = v.get_name("self")
349 v.add_decl("struct instance_{c_name} *{res};")
350 var mtype_elt = mtype.arguments.first
351 v.add("{res} = nit_alloc(sizeof(struct instance_{c_name}) + length*sizeof({mtype_elt.ctype}));")
352 v.require_declaration("class_{c_name}")
353 v.add("{res}->class = &class_{c_name};")
354 v.add("{res}->length = length;")
355 v.add("return (val*){res};")
356 v.add("\}")
357 return
358 end
359
360 #Build NEW
361 self.provide_declaration("NEW_{c_name}", "{mtype.ctype} NEW_{c_name}(void);")
362 v.add_decl("/* allocate {mtype} */")
363 v.add_decl("{mtype.ctype} NEW_{c_name}(void) \{")
364 if is_dead then
365 v.add_abort("{mclass} is DEAD")
366 else
367
368 var res = v.new_named_var(mtype, "self")
369 res.is_exact = true
370 v.add("{res} = nit_alloc(sizeof(struct instance) + {attrs.length}*sizeof(nitattribute_t));")
371 v.require_declaration("class_{c_name}")
372 v.add("{res}->class = &class_{c_name};")
373 self.generate_init_attr(v, res, mtype)
374 v.add("return {res};")
375 end
376 v.add("\}")
377 end
378
379 private fun build_class_vts_table(mclass: MClass): Bool do
380 if self.vt_tables[mclass].is_empty then return false
381
382 self.provide_declaration("vts_table_{mclass.c_name}", "extern const struct vts_table vts_table_{mclass.c_name};")
383
384 var v = new_visitor
385 v.add_decl("const struct vts_table vts_table_{mclass.c_name} = \{")
386 if self.vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
387 #TODO redo this when PHPropertyLayoutBuilder will be implemented
388 #v.add_decl("{vt_masks[mclass]},")
389 else
390 v.add_decl("0, /* dummy */")
391 end
392 v.add_decl("\{")
393
394 for vt in self.vt_tables[mclass] do
395 if vt == null then
396 v.add_decl("\{-1, NULL\}, /* empty */")
397 else
398 var is_null = 0
399 var bound = retrieve_vt_bound(mclass.intro.bound_mtype, vt.as(MVirtualTypeDef).bound)
400 while bound isa MNullableType do
401 bound = retrieve_vt_bound(mclass.intro.bound_mtype, bound.mtype)
402 is_null = 1
403 end
404 var vtclass = bound.as(MClassType).mclass
405 v.require_declaration("class_{vtclass.c_name}")
406 v.add_decl("\{{is_null}, &class_{vtclass.c_name}\}, /* {vt} */")
407 end
408 end
409 v.add_decl("\},")
410 v.add_decl("\};")
411 return true
412 end
413
414 private fun retrieve_vt_bound(anchor: MClassType, mtype: nullable MType): MType do
415 if mtype == null then
416 print "NOT YET IMPLEMENTED: retrieve_vt_bound on null"
417 abort
418 end
419 if mtype isa MVirtualType then
420 return mtype.anchor_to(mainmodule, anchor)
421 else if mtype isa MParameterType then
422 return mtype.anchor_to(mainmodule, anchor)
423 else
424 return mtype
425 end
426 end
427
428 redef fun new_visitor do return new SeparateErasureCompilerVisitor(self)
429
430 # Stats
431
432 private var class_tables: Map[MClass, Array[nullable MClass]]
433 private var vt_tables: Map[MClass, Array[nullable MPropDef]]
434
435 redef fun display_sizes
436 do
437 print "# size of subtyping tables"
438 print "\ttotal \tholes"
439 var total = 0
440 var holes = 0
441 for t, table in class_tables do
442 total += table.length
443 for e in table do if e == null then holes += 1
444 end
445 print "\t{total}\t{holes}"
446
447 print "# size of resolution tables"
448 print "\ttotal \tholes"
449 total = 0
450 holes = 0
451 for t, table in vt_tables do
452 total += table.length
453 for e in table do if e == null then holes += 1
454 end
455 print "\t{total}\t{holes}"
456
457 print "# size of methods tables"
458 print "\ttotal \tholes"
459 total = 0
460 holes = 0
461 for t, table in method_tables do
462 total += table.length
463 for e in table do if e == null then holes += 1
464 end
465 print "\t{total}\t{holes}"
466
467 print "# size of attributes tables"
468 print "\ttotal \tholes"
469 total = 0
470 holes = 0
471 for t, table in attr_tables do
472 total += table.length
473 for e in table do if e == null then holes += 1
474 end
475 print "\t{total}\t{holes}"
476 end
477 end
478
479 class SeparateErasureCompilerVisitor
480 super SeparateCompilerVisitor
481
482 redef fun compile_callsite(callsite, arguments)
483 do
484 var res = super
485 if callsite.erasure_cast and not self.compiler.as(SeparateErasureCompiler).modelbuilder.toolcontext.opt_no_check_erasure_cast.value then
486 assert res != null
487 var mtype = callsite.msignature.return_mtype
488 assert mtype != null
489 self.add("/* Erasure cast for return {res} isa {mtype} */")
490 var cond = self.type_test(res, mtype, "erasure")
491 self.add("if (!{cond}) \{")
492 #var x = self.class_name_string(res)
493 #var y = self.class_name_string(arguments.first)
494 #self.add("fprintf(stderr, \"Erasure cast: expected {mtype} (self is %s), got %s for {res}\\n\", {y}, {x});")
495 self.add_abort("Cast failed")
496 self.add("\}")
497 end
498 return res
499 end
500
501 redef fun init_instance(mtype)
502 do
503 self.require_declaration("NEW_{mtype.mclass.c_name}")
504 return self.new_expr("NEW_{mtype.mclass.c_name}()", mtype)
505 end
506
507 redef fun type_test(value, mtype, tag)
508 do
509 self.add("/* type test for {value.inspect} isa {mtype} */")
510
511 var res = self.new_var(bool_type)
512
513 var cltype = self.get_name("cltype")
514 self.add_decl("int {cltype};")
515 var idtype = self.get_name("idtype")
516 self.add_decl("int {idtype};")
517
518 var maybe_null = self.maybe_null(value)
519 var accept_null = "0"
520 if mtype isa MNullableType then
521 mtype = mtype.mtype
522 accept_null = "1"
523 end
524 if mtype isa MParameterType then
525 # Here we get the bound of the the formal type (eh, erasure...)
526 mtype = mtype.resolve_for(self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.bound_mtype, self.frame.mpropdef.mclassdef.mmodule, false)
527 if mtype isa MNullableType then
528 mtype = mtype.mtype
529 accept_null = "1"
530 end
531 end
532
533 if value.mcasttype.is_subtype(self.frame.mpropdef.mclassdef.mmodule, self.frame.mpropdef.mclassdef.bound_mtype, mtype) then
534 self.add("{res} = 1; /* easy {value.inspect} isa {mtype}*/")
535 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
536 self.compiler.count_type_test_skipped[tag] += 1
537 self.add("count_type_test_skipped_{tag}++;")
538 end
539 return res
540 end
541
542 var class_ptr
543 var type_table
544 if value.mtype.ctype == "val*" then
545 class_ptr = "{value}->class->"
546 else
547 var mclass = value.mtype.as(MClassType).mclass
548 self.require_declaration("class_{mclass.c_name}")
549 class_ptr = "class_{mclass.c_name}."
550 end
551
552 if mtype isa MClassType then
553 self.require_declaration("class_{mtype.mclass.c_name}")
554 self.add("{cltype} = class_{mtype.mclass.c_name}.color;")
555 self.add("{idtype} = class_{mtype.mclass.c_name}.id;")
556 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
557 self.compiler.count_type_test_resolved[tag] += 1
558 self.add("count_type_test_resolved_{tag}++;")
559 end
560 else if mtype isa MVirtualType then
561 var recv = self.frame.arguments.first
562 var recv_ptr
563 if recv.mtype.ctype == "val*" then
564 recv_ptr = "{recv}->class->"
565 else
566 var mclass = recv.mtype.as(MClassType).mclass
567 self.require_declaration("class_{mclass.c_name}")
568 recv_ptr = "class_{mclass.c_name}."
569 end
570 var entry = self.get_name("entry")
571 self.add("struct vts_entry {entry};")
572 self.require_declaration(mtype.mproperty.const_color)
573 if self.compiler.as(SeparateErasureCompiler).vt_layout isa PHLayout[MClass, MVirtualTypeProp] then
574 self.add("{entry} = {recv_ptr}vts_table->vts[HASH({recv_ptr}vts_table->mask, {mtype.mproperty.const_color})];")
575 else
576 self.add("{entry} = {recv_ptr}vts_table->vts[{mtype.mproperty.const_color}];")
577 end
578 self.add("{cltype} = {entry}.class->color;")
579 self.add("{idtype} = {entry}.class->id;")
580 if maybe_null and accept_null == "0" then
581 var is_nullable = self.get_name("is_nullable")
582 self.add_decl("short int {is_nullable};")
583 self.add("{is_nullable} = {entry}.is_nullable;")
584 accept_null = is_nullable.to_s
585 end
586 if compiler.modelbuilder.toolcontext.opt_typing_test_metrics.value then
587 self.compiler.count_type_test_unresolved[tag] += 1
588 self.add("count_type_test_unresolved_{tag}++;")
589 end
590 else
591 self.debug("type_test({value.inspect}, {mtype})")
592 abort
593 end
594
595 # check color is in table
596 if maybe_null then
597 self.add("if({value} == NULL) \{")
598 self.add("{res} = {accept_null};")
599 self.add("\} else \{")
600 end
601 if self.compiler.as(SeparateErasureCompiler).class_layout isa PHLayout[MClass, MClass] then
602 self.add("{cltype} = HASH({class_ptr}color, {idtype});")
603 end
604 self.add("if({cltype} >= {class_ptr}type_table->size) \{")
605 self.add("{res} = 0;")
606 self.add("\} else \{")
607 self.add("{res} = {class_ptr}type_table->table[{cltype}] == {idtype};")
608 self.add("\}")
609 if maybe_null then
610 self.add("\}")
611 end
612
613 return res
614 end
615
616 redef fun class_name_string(value)
617 do
618 var res = self.get_name("var_class_name")
619 self.add_decl("const char* {res};")
620 if value.mtype.ctype == "val*" then
621 self.add "{res} = {value} == NULL ? \"null\" : {value}->class->name;"
622 else
623 self.require_declaration("class_{value.mtype.c_name}")
624 self.add "{res} = class_{value.mtype.c_name}.name;"
625 end
626 return res
627 end
628
629 redef fun array_instance(array, elttype)
630 do
631 var nclass = self.get_class("NativeArray")
632 elttype = self.anchor(elttype)
633 var arraytype = self.get_class("Array").get_mtype([elttype])
634 var res = self.init_instance(arraytype)
635 self.add("\{ /* {res} = array_instance Array[{elttype}] */")
636 var nat = self.new_var(self.get_class("NativeArray").get_mtype([elttype]))
637 nat.is_exact = true
638 self.require_declaration("NEW_{nclass.c_name}")
639 self.add("{nat} = NEW_{nclass.c_name}({array.length});")
640 for i in [0..array.length[ do
641 var r = self.autobox(array[i], self.object_type)
642 self.add("((struct instance_{nclass.c_instance_name}*){nat})->values[{i}] = (val*) {r};")
643 end
644 var length = self.int_instance(array.length)
645 self.send(self.get_property("with_native", arraytype), [res, nat, length])
646 self.add("\}")
647 return res
648 end
649
650 redef fun calloc_array(ret_type, arguments)
651 do
652 var ret = ret_type.as(MClassType)
653 self.require_declaration("NEW_{ret.mclass.c_name}")
654 self.ret(self.new_expr("NEW_{ret.mclass.c_name}({arguments[1]})", ret_type))
655 end
656 end