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