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