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