1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
18 module abstract_compiler
22 import auto_super_init
24 # Add compiling options
25 redef class ToolContext
27 var opt_output
: OptionString = new OptionString("Output file", "-o", "--output")
29 var opt_no_cc
: OptionBool = new OptionBool("Do not invoke C compiler", "--no-cc")
31 var opt_make_flags
: OptionString = new OptionString("Additional options to make", "--make-flags")
33 var opt_hardening
: OptionBool = new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
35 var opt_no_shortcut_range
: OptionBool = new OptionBool("Always insantiate a range and its iterator on 'for' loops", "--no-shortcut-range")
36 # --no-check-covariance
37 var opt_no_check_covariance
: OptionBool = new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
38 # --no-check-initialization
39 var opt_no_check_initialization
: OptionBool = new OptionBool("Disable isset tests at the end of constructors (dangerous)", "--no-check-initialization")
41 var opt_no_check_assert
: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
43 var opt_no_check_autocast
: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
45 var opt_no_check_other
: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
46 # --typing-test-metrics
47 var opt_typing_test_metrics
: OptionBool = new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
52 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_make_flags
, self.opt_hardening
, self.opt_no_shortcut_range
)
53 self.option_context
.add_option
(self.opt_no_check_covariance
, self.opt_no_check_initialization
, self.opt_no_check_assert
, self.opt_no_check_autocast
, self.opt_no_check_other
)
54 self.option_context
.add_option
(self.opt_typing_test_metrics
)
58 redef class ModelBuilder
59 protected fun write_and_make
(compiler
: AbstractCompiler)
61 var mainmodule
= compiler
.mainmodule
63 # Generate the .h and .c files
64 # A single C file regroups many compiled rumtime functions
65 # Note that we do not try to be clever an a small change in a Nit source file may change the content of all the generated .c files
68 var outname
= self.toolcontext
.opt_output
.value
69 if outname
== null then
70 outname
= "{mainmodule.name}.bin"
73 var hfilename
= ".nit_compile/{mainmodule.name}.1.h"
74 var h
= new OFStream.open
(hfilename
)
75 for l
in compiler
.header
.decl_lines
do
81 var cfiles
= new Array[String]
83 var file
: nullable OFStream = null
88 for vis
in compiler
.visitors
do
89 count
+= vis
.lines
.length
90 if file
== null or count
> 10000 or vis
.file_break
then
92 if file
!= null then file
.close
93 var cfilename
= ".nit_compile/{mainmodule.name}.{i}.c"
95 file
= new OFStream.open
(cfilename
)
96 file
.write
"#include \"{mainmodule.name}.1.h\
"\n"
97 count
= vis
.lines
.length
99 if vis
!= compiler
.header
then
100 for l
in vis
.decl_lines
do
105 for l
in vis
.lines
do
110 if file
!= null then file
.close
112 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
114 # Generate the Makefile
116 var makename
= ".nit_compile/{mainmodule.name}.mk"
117 var makefile
= new OFStream.open
(makename
)
119 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
120 makefile
.write
("all: {outname}\n\n")
122 var ofiles
= new Array[String]
123 # Compile each generated file
125 var o
= f
.strip_extension
(".c") + ".o"
126 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
129 # Compile each required extern body into a specific .o
130 for f
in compiler
.extern_bodies
do
132 var o
= ".nit_compile/{mainmodule.name}.{i}.o"
133 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
137 makefile
.write
("{outname}: {ofiles.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outname} {ofiles.join(" ")} $(LDLIBS)\n\n")
139 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
141 self.toolcontext
.info
("Generated makefile: {makename}", 2)
144 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
146 # Execute the Makefile
148 if self.toolcontext
.opt_no_cc
.value
then return
151 self.toolcontext
.info
("*** COMPILING C ***", 1)
152 var makeflags
= self.toolcontext
.opt_make_flags
.value
153 if makeflags
== null then makeflags
= ""
154 self.toolcontext
.info
("make -B -f {makename} -j 4 {makeflags}", 2)
157 if self.toolcontext
.verbose_level
>= 3 then
158 res
= sys
.system
("make -B -f {makename} -j 4 {makeflags} 2>&1")
160 res
= sys
.system
("make -B -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
163 toolcontext
.error
(null, "make failed! Error code: {res}.")
167 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
171 # Singleton that store the knowledge about the compilation process
172 abstract class AbstractCompiler
173 type VISITOR: AbstractCompilerVisitor
175 # The main module of the program
176 var mainmodule
: MModule protected writable
178 # The modeulbuilder used to know the model and the AST
179 var modelbuilder
: ModelBuilder protected writable
181 # Is hardening asked? (see --hardening)
182 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
184 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
186 self.mainmodule
= mainmodule
187 self.modelbuilder
= modelbuilder
190 # Force the creation of a new file
191 # The point is to avoid contamination between must-be-compiled-separately files
194 var v
= self.new_visitor
198 # The list of all associated visitors
199 # Used to generate .c files
200 # FIXME: should not be vistors but just somewhere to store lines
201 var visitors
: List[VISITOR] = new List[VISITOR]
203 # Initialize a visitor specific for a compiler engine
204 fun new_visitor
: VISITOR is abstract
206 # Where global declaration are stored (the main .h)
208 # FIXME: should not be a visitor but just somewhere to store lines
209 # FIXME: should not have a global .h since it does not help recompilations
210 var header
: VISITOR writable
213 # This method call compile_header_strucs method that has to be refined
214 fun compile_header
do
216 self.header
.add_decl
("#include <stdlib.h>")
217 self.header
.add_decl
("#include <stdio.h>")
218 self.header
.add_decl
("#include <string.h>")
219 self.header
.add_decl
("#ifndef NOBOEHM")
220 self.header
.add_decl
("#include <gc/gc.h>")
221 self.header
.add_decl
("#ifdef NOBOEHM_ATOMIC")
222 self.header
.add_decl
("#undef GC_MALLOC_ATOMIC")
223 self.header
.add_decl
("#define GC_MALLOC_ATOMIC(x) GC_MALLOC(x)")
224 self.header
.add_decl
("#endif /*NOBOEHM_ATOMIC*/")
225 self.header
.add_decl
("#else /*NOBOEHM*/")
226 self.header
.add_decl
("#define GC_MALLOC(x) calloc(1, (x))")
227 self.header
.add_decl
("#define GC_MALLOC_ATOMIC(x) calloc(1, (x))")
228 self.header
.add_decl
("#endif /*NOBOEHM*/")
230 compile_header_structs
232 # Global variable used by the legacy native interface
233 self.header
.add_decl
("extern int glob_argc;")
234 self.header
.add_decl
("extern char **glob_argv;")
235 self.header
.add_decl
("extern val *glob_sys;")
238 # Declaration of structures the live Nit types
239 protected fun compile_header_structs
is abstract
241 # Generate the main C function.
243 # * allocate the Sys object if it exists
244 # * call init if is exists
245 # * call main if it exists
246 fun compile_main_function
248 var v
= self.new_visitor
249 v
.add_decl
("int glob_argc;")
250 v
.add_decl
("char **glob_argv;")
251 v
.add_decl
("val *glob_sys;")
253 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
254 for tag
in count_type_test_tags
do
255 v
.add_decl
("long count_type_test_resolved_{tag};")
256 v
.add_decl
("long count_type_test_unresolved_{tag};")
257 v
.add_decl
("long count_type_test_skipped_{tag};")
258 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
259 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
260 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
263 v
.add_decl
("int main(int argc, char** argv) \{")
264 v
.add
("glob_argc = argc; glob_argv = argv;")
265 var main_type
= mainmodule
.sys_type
266 if main_type
!= null then
267 var mainmodule
= v
.compiler
.mainmodule
268 var glob_sys
= v
.init_instance
(main_type
)
269 v
.add
("glob_sys = {glob_sys};")
270 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
271 if main_init
!= null then
272 v
.send
(main_init
, [glob_sys
])
274 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
275 if main_method
!= null then
276 v
.send
(main_method
, [glob_sys
])
280 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
281 v
.add_decl
("long count_type_test_resolved_total = 0;")
282 v
.add_decl
("long count_type_test_unresolved_total = 0;")
283 v
.add_decl
("long count_type_test_skipped_total = 0;")
284 v
.add_decl
("long count_type_test_total_total = 0;")
285 for tag
in count_type_test_tags
do
286 v
.add_decl
("long count_type_test_total_{tag};")
287 v
.add
("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
288 v
.add
("count_type_test_resolved_total += count_type_test_resolved_{tag};")
289 v
.add
("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
290 v
.add
("count_type_test_skipped_total += count_type_test_skipped_{tag};")
291 v
.add
("count_type_test_total_total += count_type_test_total_{tag};")
293 v
.add
("printf(\"# dynamic count_type_test: total %l\\n\");")
294 v
.add
("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\
");")
295 var tags
= count_type_test_tags
.to_a
298 v
.add
("printf(\"{tag}\
");")
299 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
300 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
301 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
302 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\\n\
", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
309 # look for a needed .h and .c file for a given .nit source-file
310 # FIXME: bad API, parameter should be a MModule, not its source-file
311 fun add_extern
(file
: String)
313 file
= file
.strip_extension
(".nit")
314 var tryfile
= file
+ ".nit.h"
315 if tryfile
.file_exists
then
316 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
318 tryfile
= file
+ "_nit.h"
319 if tryfile
.file_exists
then
320 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
322 tryfile
= file
+ ".nit.c"
323 if tryfile
.file_exists
then
324 self.extern_bodies
.add
(tryfile
)
326 tryfile
= file
+ "_nit.c"
327 if tryfile
.file_exists
then
328 self.extern_bodies
.add
(tryfile
)
332 # List of additional .c files required to compile (native interface)
333 var extern_bodies
= new ArraySet[String]
335 # Generate code that check if an instance is correctly initialized
336 fun generate_check_init_instance
(mtype
: MClassType) is abstract
338 # Generate code that initialize the attributes on a new instance
339 fun generate_init_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
341 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
342 self.mainmodule
.linearize_mclassdefs
(cds
)
344 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
345 for npropdef
in n
.n_propdefs
do
346 if npropdef
isa AAttrPropdef then
347 npropdef
.init_expr
(v
, recv
)
353 # Generate code that check if an attribute is correctly initialized
354 fun generate_check_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
356 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
357 self.mainmodule
.linearize_mclassdefs
(cds
)
359 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
360 for npropdef
in n
.n_propdefs
do
361 if npropdef
isa AAttrPropdef then
362 npropdef
.check_expr
(v
, recv
)
370 var count_type_test_tags
: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
371 var count_type_test_resolved
: HashMap[String, Int] = init_count_type_test_tags
372 var count_type_test_unresolved
: HashMap[String, Int] = init_count_type_test_tags
373 var count_type_test_skipped
: HashMap[String, Int] = init_count_type_test_tags
375 protected fun init_count_type_test_tags
: HashMap[String, Int]
377 var res
= new HashMap[String, Int]
378 for tag
in count_type_test_tags
do
384 # Display stats about compilation process
386 # * type tests against resolved types (x isa Collection[Animal])
387 # * type tests against unresolved types (x isa Collection[E])
388 # * type tests skipped
393 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
394 print
"# static count_type_test"
395 print
"\tresolved:\tunresolved\tskipped\ttotal"
396 var count_type_test_total
= init_count_type_test_tags
397 count_type_test_resolved
["total"] = 0
398 count_type_test_unresolved
["total"] = 0
399 count_type_test_skipped
["total"] = 0
400 count_type_test_total
["total"] = 0
401 for tag
in count_type_test_tags
do
402 count_type_test_total
[tag
] = count_type_test_resolved
[tag
] + count_type_test_unresolved
[tag
] + count_type_test_skipped
[tag
]
403 count_type_test_resolved
["total"] += count_type_test_resolved
[tag
]
404 count_type_test_unresolved
["total"] += count_type_test_unresolved
[tag
]
405 count_type_test_skipped
["total"] += count_type_test_skipped
[tag
]
406 count_type_test_total
["total"] += count_type_test_total
[tag
]
408 var count_type_test
= count_type_test_total
["total"]
409 var tags
= count_type_test_tags
.to_a
413 printn
"\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
414 printn
"\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
415 printn
"\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
416 printn
"\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
423 # Avoid division by zero by returning the string "n/a"
424 fun div
(a
,b
:Int):String
426 if b
== 0 then return "n/a"
427 return ((a
*10000/b
).to_f
/ 100.0).to_precision
(2)
431 # A visitor on the AST of property definition that generate the C code.
432 abstract class AbstractCompilerVisitor
434 type COMPILER: AbstractCompiler
436 # The associated compiler
437 var compiler
: COMPILER
439 # The current visited AST node
440 var current_node
: nullable ANode writable = null
443 var frame
: nullable Frame writable
445 # Alias for self.compiler.mainmodule.object_type
446 fun object_type
: MClassType do return self.compiler
.mainmodule
.object_type
448 # Alias for self.compiler.mainmodule.bool_type
449 fun bool_type
: MClassType do return self.compiler
.mainmodule
.bool_type
451 var file_break
: Bool = false
453 init(compiler
: COMPILER)
455 self.compiler
= compiler
456 compiler
.visitors
.add
(self)
459 # Force to get the primitive class named `name' or abort
460 fun get_class
(name
: String): MClass do return self.compiler
.mainmodule
.get_primitive_class
(name
)
462 # Force to get the primitive property named `name' in the instance `recv' or abort
463 fun get_property
(name
: String, recv
: MType): MMethod
465 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, recv
, self.compiler
.mainmodule
)
468 fun compile_callsite
(callsite
: CallSite, args
: Array[RuntimeVariable]): nullable RuntimeVariable
470 return self.send
(callsite
.mproperty
, args
)
473 fun calloc_array
(ret_type
: MType, arguments
: Array[RuntimeVariable]) is abstract
475 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable]) is abstract
477 # Transform varargs, in raw arguments, into a single argument of type Array
478 # Note: this method modify the given `args`
479 # If there is no vararg, then `args` is not modified.
480 fun varargize
(mpropdef
: MPropDef, msignature
: MSignature, args
: Array[RuntimeVariable])
482 var recv
= args
.first
483 var vararg_rank
= msignature
.vararg_rank
484 if vararg_rank
>= 0 then
485 assert args
.length
>= msignature
.arity
+ 1 # because of self
487 args
= new Array[RuntimeVariable]
489 args
.add
(rawargs
.first
) # recv
491 for i
in [0..vararg_rank
[ do
492 args
.add
(rawargs
[i
+1])
495 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-msignature
.arity
496 var vararg
= new Array[RuntimeVariable]
497 for i
in [vararg_rank
..vararg_lastrank
] do
498 vararg
.add
(rawargs
[i
+1])
501 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
502 args
.add
(self.vararg_instance
(mpropdef
, recv
, vararg
, elttype
))
504 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
505 args
.add
(rawargs
[i
+1])
508 rawargs
.add_all
(args
)
514 # Anchor a type to the main module and the current receiver
515 fun anchor
(mtype
: MType): MType
517 if not mtype
.need_anchor
then return mtype
518 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
521 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
523 if not mtype
.need_anchor
then return mtype
524 return mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
527 # Unsafely cast a value to a new type
528 # ie the result share the same C variable but my have a different mcasttype
529 # NOTE: if the adaptation is useless then `value' is returned as it.
530 # ENSURE: return.name == value.name
531 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
533 mtype
= self.anchor
(mtype
)
534 var valmtype
= value
.mcasttype
535 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
539 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
540 var res
= new RuntimeVariable(value
.name
, valmtype
, valmtype
.mtype
)
543 var res
= new RuntimeVariable(value
.name
, valmtype
, mtype
)
548 # Generate a super call from a method definition
549 fun supercall
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
551 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
553 # Box or unbox a value to another type iff a C type conversion is needed
554 # ENSURE: result.mtype.ctype == mtype.ctype
555 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
557 # Generate a polymorphic subtype test
558 fun type_test
(value
: RuntimeVariable, mtype
: MType, tag
: String): RuntimeVariable is abstract
560 # Generate the code required to dynamically check if 2 objects share the same runtime type
561 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
563 # Generate a Nit "is" for two runtime_variables
564 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
568 # Generate a static call on a method definition
569 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
571 # Generate a polymorphic send for the method `m' and the arguments `args'
572 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
574 # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
575 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
577 assert t
isa MClassType
578 var propdef
= m
.lookup_first_definition
(self.compiler
.mainmodule
, t
)
579 return self.call
(propdef
, t
, args
)
582 # Attributes handling
584 # Generate a polymorphic attribute is_set test
585 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
587 # Generate a polymorphic attribute read
588 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
590 # Generate a polymorphic attribute write
591 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable) is abstract
595 # Add a check and an abort for a null reciever if needed
596 fun check_recv_notnull
(recv
: RuntimeVariable)
598 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then return
600 var maybenull
= recv
.mcasttype
isa MNullableType or recv
.mcasttype
isa MNullType
602 self.add
("if ({recv} == NULL) \{")
603 self.add_abort
("Reciever is null")
608 # Generate a check-init-instance
609 fun check_init_instance
(recv
: RuntimeVariable, mtype
: MClassType) is abstract
613 private var names
: HashSet[String] = new HashSet[String]
614 private var last
: Int = 0
616 # Return a new name based on `s' and unique in the visitor
617 fun get_name
(s
: String): String
619 if not self.names
.has
(s
) then
623 var i
= self.last
+ 1
626 if not self.names
.has
(s2
) then
635 # Return an unique and stable identifier associated with an escapemark
636 fun escapemark_name
(e
: nullable EscapeMark): String
639 if escapemark_names
.has_key
(e
) then return escapemark_names
[e
]
641 if name
== null then name
= "label"
642 name
= get_name
(name
)
643 escapemark_names
[e
] = name
647 private var escapemark_names
= new HashMap[EscapeMark, String]
649 # Return a "const char*" variable associated to the classname of the dynamic type of an object
650 # NOTE: we do not return a RuntimeVariable "NativeString" as the class may not exist in the module/program
651 fun class_name_string
(value
: RuntimeVariable): String is abstract
655 protected var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
657 # Return the local runtime_variable associated to a Nit local variable
658 fun variable
(variable
: Variable): RuntimeVariable
660 if self.variables
.has_key
(variable
) then
661 return self.variables
[variable
]
663 var name
= self.get_name
("var_{variable.name}")
664 var mtype
= variable
.declared_type
.as(not null)
665 mtype
= self.anchor
(mtype
)
666 var res
= new RuntimeVariable(name
, mtype
, mtype
)
667 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
668 self.variables
[variable
] = res
673 # Return a new uninitialized local runtime_variable
674 fun new_var
(mtype
: MType): RuntimeVariable
676 mtype
= self.anchor
(mtype
)
677 var name
= self.get_name
("var")
678 var res
= new RuntimeVariable(name
, mtype
, mtype
)
679 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
683 # Return a new uninitialized named runtime_variable
684 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
686 mtype
= self.anchor
(mtype
)
687 var res
= new RuntimeVariable(name
, mtype
, mtype
)
688 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
692 # Correctly assign a left and a right value
693 # Boxing and unboxing is performed if required
694 fun assign
(left
, right
: RuntimeVariable)
696 right
= self.autobox
(right
, left
.mtype
)
697 self.add
("{left} = {right};")
702 # Generate a alloc-instance + init-attributes
703 fun init_instance
(mtype
: MClassType): RuntimeVariable is abstract
705 # Generate an integer value
706 fun int_instance
(value
: Int): RuntimeVariable
708 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
709 self.add
("{res} = {value};")
713 # Generate a string value
714 fun string_instance
(string
: String): RuntimeVariable
716 var mtype
= self.get_class
("String").mclass_type
717 var name
= self.get_name
("varonce")
718 self.add_decl
("static {mtype.ctype} {name};")
719 var res
= self.new_var
(mtype
)
720 self.add
("if ({name}) \{")
721 self.add
("{res} = {name};")
722 self.add
("\} else \{")
723 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
724 self.add
("{nat} = \"{string.escape_to_c}\
";")
725 var res2
= self.init_instance
(mtype
)
726 self.add
("{res} = {res2};")
727 var length
= self.int_instance
(string
.length
)
728 self.send
(self.get_property
("with_native", mtype
), [res
, nat
, length
])
729 self.check_init_instance
(res
, mtype
)
730 self.add
("{name} = {res};")
735 # Generate an array value
736 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
738 # Get an instance of a array for a vararg
739 fun vararg_instance
(mpropdef
: MPropDef, recv
: RuntimeVariable, varargs
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
743 private var lines
: List[String] = new List[String]
744 private var decl_lines
: List[String] = new List[String]
746 # Add a line in the main part of the generated C
747 fun add
(s
: String) do self.lines
.add
(s
)
750 # (used for local or global declaration)
751 fun add_decl
(s
: String) do self.decl_lines
.add
(s
)
753 # Return a new local runtime_variable initialized with the C expression `cexpr'.
754 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
756 var res
= new_var
(mtype
)
757 self.add
("{res} = {cexpr};")
761 # Generate generic abort
762 # used by aborts, asserts, casts, etc.
763 fun add_abort
(message
: String)
765 if self.current_node
!= null and self.current_node
.location
.file
!= null then
766 self.add
("fprintf(stderr, \"Runtime error
: %s
(%s
:%d
)\\n\
", \"{message.escape_to_c}\
", \"{self.current_node.location.file.filename.escape_to_c}\
", {current_node.location.line_start});")
768 self.add
("fprintf(stderr, \"Runtime error
: %s\\n\
", \"{message.escape_to_c}\
");")
773 # Generate a return with the value `s'
774 fun ret
(s
: RuntimeVariable)
776 self.assign
(self.frame
.returnvar
.as(not null), s
)
777 self.add
("goto {self.frame.returnlabel.as(not null)};")
780 # Compile a statement (if any)
781 fun stmt
(nexpr
: nullable AExpr)
783 if nexpr
== null then return
784 var old
= self.current_node
785 self.current_node
= nexpr
787 self.current_node
= old
790 # Compile an expression an return its result
791 # `mtype` is the expected return type, pass null if no specific type is expected.
792 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
794 var old
= self.current_node
795 self.current_node
= nexpr
796 var res
= nexpr
.expr
(self).as(not null)
797 if mtype
!= null then
798 mtype
= self.anchor
(mtype
)
799 res
= self.autobox
(res
, mtype
)
801 res
= autoadapt
(res
, nexpr
.mtype
.as(not null))
802 var implicit_cast_to
= nexpr
.implicit_cast_to
803 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
804 var castres
= self.type_test
(res
, implicit_cast_to
, "auto")
805 self.add
("if (!{castres}) \{")
806 self.add_abort
("Cast failed")
808 res
= autoadapt
(res
, implicit_cast_to
)
810 self.current_node
= old
814 # Alias for `self.expr(nexpr, self.bool_type)'
815 fun expr_bool
(nexpr
: AExpr): RuntimeVariable do return expr
(nexpr
, bool_type
)
817 # Safely show a debug message on the current node and repeat the message in the C code as a comment
818 fun debug
(message
: String)
820 var node
= self.current_node
826 self.add
("/* DEBUG: {message} */")
830 # A C function associated to a Nit method
831 # Because of customization, a given Nit method can be compiler more that once
832 abstract class AbstractRuntimeFunction
834 type COMPILER: AbstractCompiler
835 type VISITOR: AbstractCompilerVisitor
837 # The associated Nit method
838 var mmethoddef
: MMethodDef
840 # The mangled c name of the runtime_function
841 # Subclasses should redefine `build_c_name` instead
844 var res
= self.c_name_cache
845 if res
!= null then return res
846 res
= self.build_c_name
847 self.c_name_cache
= res
851 # Non cached version of `c_name`
852 protected fun build_c_name
: String is abstract
854 protected var c_name_cache
: nullable String writable = null
856 # Implements a call of the runtime_function
857 # May inline the body or generate a C function call
858 fun call
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
860 # Generate the code for the RuntimeFunction
861 # Warning: compile more than once compilation makes CC unhappy
862 fun compile_to_c
(compiler
: COMPILER) is abstract
865 # A runtime variable hold a runtime value in C.
866 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
868 # The tricky point is that a single C variable can be associated to more than one RuntimeVariable because the static knowledge of the type of an expression can vary in the C code.
869 class RuntimeVariable
870 # The name of the variable in the C code
873 # The static type of the variable (as declard in C)
876 # The current casted type of the variable (as known in Nit)
877 var mcasttype
: MType writable
879 # If the variable exaclty a mcasttype?
880 # false (usual value) means that the variable is a mcasttype or a subtype.
881 var is_exact
: Bool writable = false
883 init(name
: String, mtype
: MType, mcasttype
: MType)
887 self.mcasttype
= mcasttype
888 assert not mtype
.need_anchor
889 assert not mcasttype
.need_anchor
892 redef fun to_s
do return name
897 if self.is_exact
then
903 if self.mtype
== self.mcasttype
then
904 type_str
= "{mtype}{exact_str}"
906 type_str
= "{mtype}({mcasttype}{exact_str})"
908 return "<{name}:{type_str}>"
912 # A frame correspond to a visited property in a GlobalCompilerVisitor
915 type VISITOR: AbstractCompilerVisitor
917 # The associated visitor
920 # The executed property.
921 # A Method in case of a call, an attribute in case of a default initialization.
922 var mpropdef
: MPropDef
924 # The static type of the receiver
925 var receiver
: MClassType
927 # Arguments of the method (the first is the receiver)
928 var arguments
: Array[RuntimeVariable]
930 # The runtime_variable associated to the return (in a function)
931 var returnvar
: nullable RuntimeVariable writable = null
933 # The label at the end of the property
934 var returnlabel
: nullable String writable = null
938 # Mangle a string to be a unique valid C identifier
939 fun to_cmangle
: String
942 var underscore
= false
944 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
950 res
.append
('_'.ascii
.to_s
)
953 if c
>= '0' and c
<= '9' then
956 else if c
== '_' then
961 res
.append
(c
.ascii
.to_s
)
969 # Escape " \ ' and non printable characters for literal C strings or characters
970 fun escape_to_c
: String
976 else if c
== '\0' then
978 else if c
== '"' then
980 else if c == '\'' then
982 else if c == '\\
' then
984 else if c.ascii < 32 then
985 b.append("\\{c.ascii.to_base(8, false)}")
995 # Return the C type associated to a given Nit static type
996 fun ctype: String do return "val*"
998 fun ctypename: String do return "val"
1000 # Return the name of the C structure associated to a Nit live type
1001 fun c_name: String is abstract
1002 protected var c_name_cache: nullable String protected writable
1005 redef class MClassType
1008 var res = self.c_name_cache
1009 if res != null then return res
1010 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1011 self.c_name_cache = res
1015 redef fun ctype: String
1017 if mclass.name == "Int" then
1019 else if mclass.name == "Bool" then
1021 else if mclass.name == "Char" then
1023 else if mclass.name == "Float" then
1025 else if mclass.name == "NativeString" then
1027 else if mclass.name == "NativeArray" then
1029 else if mclass.kind == extern_kind then
1036 redef fun ctypename: String
1038 if mclass.name == "Int" then
1040 else if mclass.name == "Bool" then
1042 else if mclass.name == "Char" then
1044 else if mclass.name == "Float" then
1046 else if mclass.name == "NativeString" then
1048 else if mclass.name == "NativeArray" then
1049 #return "{self.arguments.first.ctype}*"
1051 else if mclass.kind == extern_kind then
1059 redef class MGenericType
1062 var res = self.c_name_cache
1063 if res != null then return res
1065 for t in self.arguments do
1066 res = res + t.c_name
1068 self.c_name_cache = res
1073 redef class MParameterType
1076 var res = self.c_name_cache
1077 if res != null then return res
1078 res = "{self.mclass.c_name}_FT{self.rank}"
1079 self.c_name_cache = res
1084 redef class MVirtualType
1087 var res = self.c_name_cache
1088 if res != null then return res
1089 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1090 self.c_name_cache = res
1095 redef class MNullableType
1098 var res = self.c_name_cache
1099 if res != null then return res
1100 res = "nullable_{self.mtype.c_name}"
1101 self.c_name_cache = res
1107 # Return the name of the C structure associated to a Nit class
1108 fun c_name: String do
1109 var res = self.c_name_cache
1110 if res != null then return res
1111 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1112 self.c_name_cache = res
1115 private var c_name_cache: nullable String
1118 redef class MProperty
1119 fun c_name: String do
1120 var res = self.c_name_cache
1121 if res != null then return res
1122 res = "{self.intro.c_name}"
1123 self.c_name_cache = res
1126 private var c_name_cache: nullable String
1129 redef class MPropDef
1130 type VISITOR: AbstractCompilerVisitor
1132 private var c_name_cache: nullable String
1134 # The mangled name associated to the property
1137 var res = self.c_name_cache
1138 if res != null then return res
1139 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1140 self.c_name_cache = res
1145 redef class MMethodDef
1146 # Can the body be inlined?
1147 fun can_inline(v: VISITOR): Bool
1149 var modelbuilder = v.compiler.modelbuilder
1150 if modelbuilder.mpropdef2npropdef.has_key(self) then
1151 var npropdef = modelbuilder.mpropdef2npropdef[self]
1152 return npropdef.can_inline
1153 else if self.mproperty.name == "init" then
1154 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1161 # Inline the body in another visitor
1162 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1164 var modelbuilder = v.compiler.modelbuilder
1165 if modelbuilder.mpropdef2npropdef.has_key(self) then
1166 var npropdef = modelbuilder.mpropdef2npropdef[self]
1167 var oldnode = v.current_node
1168 v.current_node = npropdef
1169 self.compile_parameter_check(v, arguments)
1170 npropdef.compile_to_c(v, self, arguments)
1171 v.current_node = oldnode
1172 else if self.mproperty.name == "init" then
1173 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1174 var oldnode = v.current_node
1175 v.current_node = nclassdef
1176 self.compile_parameter_check(v, arguments)
1177 nclassdef.compile_to_c(v, self, arguments)
1178 v.current_node = oldnode
1185 # Generate type checks in the C code to check covariant parameters
1186 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1188 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1190 for i in [0..msignature.arity[ do
1191 # skip test for vararg since the array is instantiated with the correct polymorphic type
1192 if msignature.vararg_rank == i then continue
1194 # skip if the cast is not required
1195 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1196 if not origmtype.need_anchor then continue
1198 # get the parameter type
1199 var mtype = self.msignature.mparameters[i].mtype
1202 # note that v decides if and how to implements the cast
1203 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1204 var cond = v.type_test(arguments[i+1], mtype, "covariance")
1205 v.add("if (!{cond}) \{")
1206 v.add_abort("Cast failed")
1214 redef class APropdef
1215 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1217 v.add("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1218 debug("Not yet implemented")
1221 fun can_inline: Bool do return true
1224 redef class AConcreteMethPropdef
1225 redef fun compile_to_c(v, mpropdef, arguments)
1227 for i in [0..mpropdef.msignature.arity[ do
1228 var variable = self.n_signature.n_params[i].variable.as(not null)
1229 v.assign(v.variable(variable), arguments[i+1])
1231 # Call the implicit super-init
1232 var auto_super_inits = self.auto_super_inits
1233 if auto_super_inits != null then
1234 var selfarg = [arguments.first]
1235 for auto_super_init in auto_super_inits do
1236 if auto_super_init.intro.msignature.arity == 0 then
1237 v.send(auto_super_init, selfarg)
1239 v.send(auto_super_init, arguments)
1243 v.stmt(self.n_block)
1246 redef fun can_inline
1248 if self.auto_super_inits != null then return false
1249 var nblock = self.n_block
1250 if nblock == null then return true
1251 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1252 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1257 redef class AInternMethPropdef
1258 redef fun compile_to_c(v, mpropdef, arguments)
1260 var pname = mpropdef.mproperty.name
1261 var cname = mpropdef.mclassdef.mclass.name
1262 var ret = mpropdef.msignature.return_mtype
1264 ret = v.resolve_for(ret, arguments.first)
1266 if pname != "==" and pname != "!=" then
1267 v.adapt_signature(mpropdef, arguments)
1269 if cname == "Int" then
1270 if pname == "output" then
1271 v.add("printf(\"%ld\\n\", {arguments.first});")
1273 else if pname == "object_id" then
1274 v.ret(arguments.first)
1276 else if pname == "+" then
1277 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1279 else if pname == "-" then
1280 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1282 else if pname == "unary -" then
1283 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1285 else if pname == "succ" then
1286 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1288 else if pname == "prec" then
1289 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1291 else if pname == "*" then
1292 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1294 else if pname == "/" then
1295 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1297 else if pname == "%" then
1298 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
1300 else if pname == "lshift" then
1301 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
1303 else if pname == "rshift" then
1304 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
1306 else if pname == "==" then
1307 v.ret(v.equal_test(arguments[0], arguments[1]))
1309 else if pname == "!=" then
1310 var res = v.equal_test(arguments[0], arguments[1])
1311 v.ret(v.new_expr("!{res}", ret.as(not null)))
1313 else if pname == "<" then
1314 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1316 else if pname == ">" then
1317 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1319 else if pname == "<=" then
1320 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1322 else if pname == ">=" then
1323 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1325 else if pname == "to_f" then
1326 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
1328 else if pname == "ascii" then
1329 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1332 else if cname == "Char" then
1333 if pname == "output" then
1334 v.add("printf(\"%c\", {arguments.first});")
1336 else if pname == "object_id" then
1337 v.ret(arguments.first)
1339 else if pname == "+" then
1340 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1342 else if pname == "-" then
1343 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1345 else if pname == "==" then
1346 v.ret(v.equal_test(arguments[0], arguments[1]))
1348 else if pname == "!=" then
1349 var res = v.equal_test(arguments[0], arguments[1])
1350 v.ret(v.new_expr("!{res}", ret.as(not null)))
1352 else if pname == "succ" then
1353 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1355 else if pname == "prec" then
1356 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1358 else if pname == "<" then
1359 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1361 else if pname == ">" then
1362 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1364 else if pname == "<=" then
1365 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1367 else if pname == ">=" then
1368 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1370 else if pname == "to_i" then
1371 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
1373 else if pname == "ascii" then
1374 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
1377 else if cname == "Bool" then
1378 if pname == "output" then
1379 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
1381 else if pname == "object_id" then
1382 v.ret(arguments.first)
1384 else if pname == "==" then
1385 v.ret(v.equal_test(arguments[0], arguments[1]))
1387 else if pname == "!=" then
1388 var res = v.equal_test(arguments[0], arguments[1])
1389 v.ret(v.new_expr("!{res}", ret.as(not null)))
1392 else if cname == "Float" then
1393 if pname == "output" then
1394 v.add("printf(\"%f\\n\", {arguments.first});")
1396 else if pname == "object_id" then
1397 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
1399 else if pname == "+" then
1400 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1402 else if pname == "-" then
1403 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1405 else if pname == "unary -" then
1406 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1408 else if pname == "succ" then
1409 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1411 else if pname == "prec" then
1412 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1414 else if pname == "*" then
1415 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1417 else if pname == "/" then
1418 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1420 else if pname == "==" then
1421 v.ret(v.equal_test(arguments[0], arguments[1]))
1423 else if pname == "!=" then
1424 var res = v.equal_test(arguments[0], arguments[1])
1425 v.ret(v.new_expr("!{res}", ret.as(not null)))
1427 else if pname == "<" then
1428 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1430 else if pname == ">" then
1431 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1433 else if pname == "<=" then
1434 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1436 else if pname == ">=" then
1437 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1439 else if pname == "to_i" then
1440 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
1443 else if cname == "Char" then
1444 if pname == "output" then
1445 v.add("printf(\"%c\", {arguments.first});")
1447 else if pname == "object_id" then
1448 v.ret(arguments.first)
1450 else if pname == "==" then
1451 v.ret(v.equal_test(arguments[0], arguments[1]))
1453 else if pname == "!=" then
1454 var res = v.equal_test(arguments[0], arguments[1])
1455 v.ret(v.new_expr("!{res}", ret.as(not null)))
1457 else if pname == "ascii" then
1458 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1461 else if cname == "NativeString" then
1462 if pname == "[]" then
1463 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
1465 else if pname == "[]=" then
1466 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1468 else if pname == "copy_to" then
1469 v.add("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
1471 else if pname == "atoi" then
1472 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
1475 else if cname == "NativeArray" then
1476 v.native_array_def(pname, ret, arguments)
1479 if pname == "exit" then
1480 v.add("exit({arguments[1]});")
1482 else if pname == "sys" then
1483 v.ret(v.new_expr("glob_sys", ret.as(not null)))
1485 else if pname == "calloc_string" then
1486 v.ret(v.new_expr("(char*)GC_MALLOC_ATOMIC({arguments[1]})", ret.as(not null)))
1488 else if pname == "calloc_array" then
1489 v.calloc_array(ret.as(not null), arguments)
1491 else if pname == "object_id" then
1492 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
1494 else if pname == "is_same_type" then
1495 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
1497 else if pname == "output_class_name" then
1498 var nat = v.class_name_string(arguments.first)
1499 v.add("printf(\"%s\\n\", {nat});")
1501 else if pname == "native_class_name" then
1502 var nat = v.class_name_string(arguments.first)
1503 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
1505 else if pname == "force_garbage_collection" then
1506 v.add("GC_gcollect();")
1509 v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
1510 debug("Not implemented {mpropdef}")
1514 redef class AExternMethPropdef
1515 redef fun compile_to_c(v, mpropdef, arguments)
1518 var nextern = self.n_extern
1519 if nextern == null then
1520 v.add("fprintf(stderr, \"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
1524 externname = nextern.text.substring(1, nextern.text.length-2)
1525 if location.file != null then
1526 var file = location.file.filename
1527 v.compiler.add_extern(file)
1529 var res: nullable RuntimeVariable = null
1530 var ret = mpropdef.msignature.return_mtype
1532 ret = v.resolve_for(ret, arguments.first)
1533 res = v.new_var(ret)
1535 v.adapt_signature(mpropdef, arguments)
1538 v.add("{externname}({arguments.join(", ")});")
1540 v.add("{res} = {externname}({arguments.join(", ")});")
1546 redef class AExternInitPropdef
1547 redef fun compile_to_c(v, mpropdef, arguments)
1550 var nextern = self.n_extern
1551 if nextern == null then
1552 debug("{mpropdef} need extern name")
1555 externname = nextern.text.substring(1, nextern.text.length-2)
1556 if location.file != null then
1557 var file = location.file.filename
1558 v.compiler.add_extern(file)
1560 v.adapt_signature(mpropdef, arguments)
1561 var ret = arguments.first.mtype
1562 var res = v.new_var(ret)
1566 v.add("{res} = {externname}({arguments.join(", ")});")
1571 redef class AAttrPropdef
1572 redef fun compile_to_c(v, mpropdef, arguments)
1574 if arguments.length == 1 then
1575 var res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
1576 v.assign(v.frame.returnvar.as(not null), res)
1578 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
1582 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1584 var nexpr = self.n_expr
1585 if nexpr != null then
1586 var oldnode = v.current_node
1587 v.current_node = self
1588 var old_frame = v.frame
1589 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1591 var value = v.expr(nexpr, self.mpropdef.static_mtype)
1592 v.write_attribute(self.mpropdef.mproperty, recv, value)
1594 v.current_node = oldnode
1598 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1600 var nexpr = self.n_expr
1601 if nexpr != null then return
1603 var oldnode = v.current_node
1604 v.current_node = self
1605 var old_frame = v.frame
1606 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1608 # Force read to check the initialization
1609 v.read_attribute(self.mpropdef.mproperty, recv)
1611 v.current_node = oldnode
1615 redef class AClassdef
1616 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1618 if mpropdef == self.mfree_init then
1619 var super_inits = self.super_inits
1620 if super_inits != null then
1621 assert arguments.length == 1
1622 for su in super_inits do
1623 v.send(su, arguments)
1627 var recv = arguments.first
1629 # Collect undefined attributes
1630 for npropdef in self.n_propdefs do
1631 if npropdef isa AAttrPropdef and npropdef.n_expr == null then
1632 v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
1642 redef class ADeferredMethPropdef
1643 redef fun compile_to_c(v, mpropdef, arguments) do v.add_abort("Deferred method called")
1644 redef fun can_inline do return true
1648 # Try to compile self as an expression
1649 # Do not call this method directly, use `v.expr' instead
1650 private fun expr
(v
: AbstractCompilerVisitor): nullable RuntimeVariable
1652 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
1653 var mtype
= self.mtype
1654 if mtype
== null then
1657 var res
= v
.new_var
(mtype
)
1658 v
.add
("/* {res} = NOT YET {class_name} */")
1663 # Try to compile self as a statement
1664 # Do not call this method directly, use `v.stmt' instead
1665 private fun stmt
(v
: AbstractCompilerVisitor)
1668 if res
!= null then v
.add
("{res};")
1672 redef class ABlockExpr
1675 for e
in self.n_expr
do v
.stmt
(e
)
1679 redef class AVardeclExpr
1682 var variable
= self.variable
.as(not null)
1683 var ne
= self.n_expr
1685 var i
= v
.expr
(ne
, variable
.declared_type
)
1686 v
.assign
(v
.variable
(variable
), i
)
1691 redef class AVarExpr
1694 var res
= v
.variable
(self.variable
.as(not null))
1695 var mtype
= self.mtype
.as(not null)
1696 return v
.autoadapt
(res
, mtype
)
1700 redef class AVarAssignExpr
1703 var variable
= self.variable
.as(not null)
1704 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
1705 v
.assign
(v
.variable
(variable
), i
)
1709 redef class AVarReassignExpr
1712 var variable
= self.variable
.as(not null)
1713 var vari
= v
.variable
(variable
)
1714 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
1715 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
1717 v
.assign
(v
.variable
(variable
), res
)
1721 redef class ASelfExpr
1722 redef fun expr
(v
) do return v
.frame
.arguments
.first
1725 redef class AContinueExpr
1726 redef fun stmt
(v
) do v
.add
("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
1729 redef class ABreakExpr
1730 redef fun stmt
(v
) do v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
1733 redef class AReturnExpr
1736 var nexpr
= self.n_expr
1737 if nexpr
!= null then
1738 var returnvar
= v
.frame
.returnvar
.as(not null)
1739 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
1740 v
.assign
(returnvar
, i
)
1742 v
.add
("goto {v.frame.returnlabel.as(not null)};")
1746 redef class AAbortExpr
1747 redef fun stmt
(v
) do v
.add_abort
("Aborted")
1753 var cond
= v
.expr_bool
(self.n_expr
)
1754 v
.add
("if ({cond})\{")
1762 redef class AIfexprExpr
1765 var res
= v
.new_var
(self.mtype
.as(not null))
1766 var cond
= v
.expr_bool
(self.n_expr
)
1767 v
.add
("if ({cond})\{")
1768 v
.assign
(res
, v
.expr
(self.n_then
, null))
1770 v
.assign
(res
, v
.expr
(self.n_else
, null))
1779 v
.stmt
(self.n_block
)
1780 var escapemark
= self.escapemark
1781 if escapemark
!= null then
1782 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1787 redef class AWhileExpr
1791 var cond
= v
.expr_bool
(self.n_expr
)
1792 v
.add
("if (!{cond}) break;")
1793 v
.stmt
(self.n_block
)
1794 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1796 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1800 redef class ALoopExpr
1804 v
.stmt
(self.n_block
)
1805 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1807 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1811 redef class AForExpr
1814 # Shortcut on explicit range
1815 # Avoid the instantiation of the range and the iterator
1816 var nexpr
= self.n_expr
1817 if self.variables
.length
== 1 and nexpr
isa AOrangeExpr and not v
.compiler
.modelbuilder
.toolcontext
.opt_no_shortcut_range
.value
then
1818 var from
= v
.expr
(nexpr
.n_expr
, null)
1819 var to
= v
.expr
(nexpr
.n_expr2
, null)
1820 var variable
= v
.variable
(variables
.first
)
1822 v
.assign
(variable
, from
)
1823 v
.add
("for(;;) \{ /* shortcut range */")
1825 var ok
= v
.send
(v
.get_property
("<", variable
.mtype
), [variable
, to
])
1827 v
.add
("if(!{ok}) break;")
1829 v
.stmt
(self.n_block
)
1831 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1832 var succ
= v
.send
(v
.get_property
("succ", variable
.mtype
), [variable
])
1834 v
.assign
(variable
, succ
)
1836 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1840 var cl
= v
.expr
(self.n_expr
, null)
1841 var it_meth
= self.method_iterator
1842 assert it_meth
!= null
1843 var it
= v
.send
(it_meth
, [cl
])
1846 var isok_meth
= self.method_is_ok
1847 assert isok_meth
!= null
1848 var ok
= v
.send
(isok_meth
, [it
])
1850 v
.add
("if(!{ok}) break;")
1851 if self.variables
.length
== 1 then
1852 var item_meth
= self.method_item
1853 assert item_meth
!= null
1854 var i
= v
.send
(item_meth
, [it
])
1856 v
.assign
(v
.variable
(variables
.first
), i
)
1857 else if self.variables
.length
== 2 then
1858 var key_meth
= self.method_key
1859 assert key_meth
!= null
1860 var i
= v
.send
(key_meth
, [it
])
1862 v
.assign
(v
.variable
(variables
[0]), i
)
1863 var item_meth
= self.method_item
1864 assert item_meth
!= null
1865 i
= v
.send
(item_meth
, [it
])
1867 v
.assign
(v
.variable
(variables
[1]), i
)
1871 v
.stmt
(self.n_block
)
1872 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1873 var next_meth
= self.method_next
1874 assert next_meth
!= null
1875 v
.send
(next_meth
, [it
])
1877 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1881 redef class AAssertExpr
1884 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
1886 var cond
= v
.expr_bool
(self.n_expr
)
1887 v
.add
("if (!{cond}) \{")
1891 v
.add_abort
("Assert '{nid.text}' failed")
1893 v
.add_abort
("Assert failed")
1902 var res
= v
.new_var
(self.mtype
.as(not null))
1903 var i1
= v
.expr_bool
(self.n_expr
)
1904 v
.add
("if ({i1}) \{")
1907 var i2
= v
.expr_bool
(self.n_expr2
)
1908 v
.add
("{res} = {i2};")
1914 redef class AAndExpr
1917 var res
= v
.new_var
(self.mtype
.as(not null))
1918 var i1
= v
.expr_bool
(self.n_expr
)
1919 v
.add
("if (!{i1}) \{")
1922 var i2
= v
.expr_bool
(self.n_expr2
)
1923 v
.add
("{res} = {i2};")
1929 redef class ANotExpr
1932 var cond
= v
.expr_bool
(self.n_expr
)
1933 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
1937 redef class AOrElseExpr
1940 var res
= v
.new_var
(self.mtype
.as(not null))
1941 var i1
= v
.expr
(self.n_expr
, null)
1942 v
.add
("if ({i1}!=NULL) \{")
1945 var i2
= v
.expr
(self.n_expr2
, null)
1955 var value1
= v
.expr
(self.n_expr
, null)
1956 var value2
= v
.expr
(self.n_expr2
, null)
1957 return v
.equal_test
(value1
, value2
)
1961 redef class AIntExpr
1962 redef fun expr
(v
) do return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
1965 redef class AFloatExpr
1966 redef fun expr
(v
) do return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
1969 redef class ACharExpr
1970 redef fun expr
(v
) do return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
1973 redef class AArrayExpr
1976 var mtype
= self.mtype
.as(MClassType).arguments
.first
1977 var array
= new Array[RuntimeVariable]
1978 for nexpr
in self.n_exprs
.n_exprs
do
1979 var i
= v
.expr
(nexpr
, mtype
)
1982 return v
.array_instance
(array
, mtype
)
1986 redef class AStringFormExpr
1987 redef fun expr
(v
) do return v
.string_instance
(self.value
.as(not null))
1990 redef class ASuperstringExpr
1993 var array
= new Array[RuntimeVariable]
1994 for ne
in self.n_exprs
do
1995 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
1996 var i
= v
.expr
(ne
, null)
1999 var a
= v
.array_instance
(array
, v
.object_type
)
2000 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2005 redef class ACrangeExpr
2008 var i1
= v
.expr
(self.n_expr
, null)
2009 var i2
= v
.expr
(self.n_expr2
, null)
2010 var mtype
= self.mtype
.as(MClassType)
2011 var res
= v
.init_instance
(mtype
)
2012 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2013 v
.check_init_instance
(res
, mtype
)
2018 redef class AOrangeExpr
2021 var i1
= v
.expr
(self.n_expr
, null)
2022 var i2
= v
.expr
(self.n_expr2
, null)
2023 var mtype
= self.mtype
.as(MClassType)
2024 var res
= v
.init_instance
(mtype
)
2025 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2026 v
.check_init_instance
(res
, mtype
)
2031 redef class ATrueExpr
2032 redef fun expr
(v
) do return v
.new_expr
("1", self.mtype
.as(not null))
2035 redef class AFalseExpr
2036 redef fun expr
(v
) do return v
.new_expr
("0", self.mtype
.as(not null))
2039 redef class ANullExpr
2040 redef fun expr
(v
) do return v
.new_expr
("NULL", self.mtype
.as(not null))
2043 redef class AIsaExpr
2046 var i
= v
.expr
(self.n_expr
, null)
2047 return v
.type_test
(i
, self.cast_type
.as(not null), "isa")
2051 redef class AAsCastExpr
2054 var i
= v
.expr
(self.n_expr
, null)
2055 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2057 var cond
= v
.type_test
(i
, self.mtype
.as(not null), "as")
2058 v
.add
("if (!{cond}) \{")
2059 v
.add_abort
("Cast failed")
2065 redef class AAsNotnullExpr
2068 var i
= v
.expr
(self.n_expr
, null)
2069 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2071 v
.add
("if ({i} == NULL) \{")
2072 v
.add_abort
("Cast failed")
2078 redef class AParExpr
2079 redef fun expr
(v
) do return v
.expr
(self.n_expr
, null)
2082 redef class AOnceExpr
2085 var mtype
= self.mtype
.as(not null)
2086 var name
= v
.get_name
("varonce")
2087 var guard
= v
.get_name
(name
+ "_guard")
2088 v
.add_decl
("static {mtype.ctype} {name};")
2089 v
.add_decl
("static int {guard};")
2090 var res
= v
.new_var
(mtype
)
2091 v
.add
("if ({guard}) \{")
2092 v
.add
("{res} = {name};")
2094 var i
= v
.expr
(self.n_expr
, mtype
)
2095 v
.add
("{res} = {i};")
2096 v
.add
("{name} = {res};")
2097 v
.add
("{guard} = 1;")
2103 redef class ASendExpr
2106 var recv
= v
.expr
(self.n_expr
, null)
2108 for a
in self.raw_arguments
.as(not null) do
2109 args
.add
(v
.expr
(a
, null))
2111 return v
.compile_callsite
(self.callsite
.as(not null), args
)
2115 redef class ASendReassignFormExpr
2118 var recv
= v
.expr
(self.n_expr
, null)
2120 for a
in self.raw_arguments
.as(not null) do
2121 args
.add
(v
.expr
(a
, null))
2123 var value
= v
.expr
(self.n_value
, null)
2125 var left
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2128 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
2132 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
2136 redef class ASuperExpr
2139 var recv
= v
.frame
.arguments
.first
2141 for a
in self.n_args
.n_exprs
do
2142 args
.add
(v
.expr
(a
, null))
2144 if args
.length
== 1 then
2145 args
= v
.frame
.arguments
2148 var mproperty
= self.mproperty
2149 if mproperty
!= null then
2150 if mproperty
.intro
.msignature
.arity
== 0 then
2154 var res
= v
.send
(mproperty
, args
)
2158 # stantard call-next-method
2159 return v
.supercall
(v
.frame
.mpropdef
.as(MMethodDef), recv
.mtype
.as(MClassType), args
)
2163 redef class ANewExpr
2166 var mtype
= self.mtype
.as(MClassType)
2168 var ctype
= mtype
.ctype
2169 if ctype
== "val*" then
2170 recv
= v
.init_instance
(mtype
)
2171 else if ctype
== "void*" then
2172 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2174 debug
("cannot new {mtype}")
2178 for a
in self.n_args
.n_exprs
do
2179 args
.add
(v
.expr
(a
, null))
2181 var res2
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2182 if res2
!= null then
2183 #self.debug("got {res2} from {mproperty}. drop {recv}")
2186 v
.check_init_instance
(recv
, mtype
)
2191 redef class AAttrExpr
2194 var recv
= v
.expr
(self.n_expr
, null)
2195 var mproperty
= self.mproperty
.as(not null)
2196 return v
.read_attribute
(mproperty
, recv
)
2200 redef class AAttrAssignExpr
2203 var recv
= v
.expr
(self.n_expr
, null)
2204 var i
= v
.expr
(self.n_value
, null)
2205 var mproperty
= self.mproperty
.as(not null)
2206 v
.write_attribute
(mproperty
, recv
, i
)
2210 redef class AAttrReassignExpr
2213 var recv
= v
.expr
(self.n_expr
, null)
2214 var value
= v
.expr
(self.n_value
, null)
2215 var mproperty
= self.mproperty
.as(not null)
2216 var attr
= v
.read_attribute
(mproperty
, recv
)
2217 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
2219 v
.write_attribute
(mproperty
, recv
, res
)
2223 redef class AIssetAttrExpr
2226 var recv
= v
.expr
(self.n_expr
, null)
2227 var mproperty
= self.mproperty
.as(not null)
2228 return v
.isset_attribute
(mproperty
, recv
)
2232 redef class ADebugTypeExpr
2241 redef class HashSet[E
]
2242 init from
(elements
: Collection[E
]) do
2244 self.add_all
(elements
)
2248 redef class Array[E
]
2249 init from
(elements
: Collection[E
]) do
2251 self.add_all
(elements
)
2254 # Return a new Array with the elements only contened in 'self' and not in 'o'
2255 fun -(o
: Array[E
]): Array[E
] do
2256 var res
= new Array[E
]
2257 for e
in self do if not o
.has
(e
) then res
.add
(e
)
2264 # Return a linearization of a set of mtypes
2265 fun linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2266 var lin
= new Array[MType].from
(mtypes
)
2267 var sorter
= new TypeSorter(self)
2272 # Return a reverse linearization of a set of mtypes
2273 fun reverse_linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2274 var lin
= new Array[MType].from
(mtypes
)
2275 var sorter
= new ReverseTypeSorter(self)
2280 # Return super types of a `mtype` in `self`
2281 fun super_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2282 if not self.super_mtypes_cache
.has_key
(mtype
) then
2283 var supers
= new HashSet[MType]
2284 for otype
in mtypes
do
2285 if otype
== mtype
then continue
2286 if mtype
.is_subtype
(self, null, otype
) then
2290 self.super_mtypes_cache
[mtype
] = supers
2292 return self.super_mtypes_cache
[mtype
]
2295 private var super_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2297 # Return all sub mtypes (directs and indirects) of a `mtype` in `self`
2298 fun sub_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2299 if not self.sub_mtypes_cache
.has_key
(mtype
) then
2300 var subs
= new HashSet[MType]
2301 for otype
in mtypes
do
2302 if otype
== mtype
then continue
2303 if otype
.is_subtype
(self, null, mtype
) then
2307 self.sub_mtypes_cache
[mtype
] = subs
2309 return self.sub_mtypes_cache
[mtype
]
2312 private var sub_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2314 # Return a linearization of a set of mclasses
2315 fun linearize_mclasses_2
(mclasses
: Set[MClass]): Array[MClass] do
2316 var lin
= new Array[MClass].from
(mclasses
)
2317 var sorter
= new ClassSorter(self)
2322 # Return a reverse linearization of a set of mtypes
2323 fun reverse_linearize_mclasses
(mclasses
: Set[MClass]): Array[MClass] do
2324 var lin
= new Array[MClass].from
(mclasses
)
2325 var sorter
= new ReverseClassSorter(self)
2330 # Return all super mclasses (directs and indirects) of a `mclass` in `self`
2331 fun super_mclasses
(mclass
: MClass): Set[MClass] do
2332 if not self.super_mclasses_cache
.has_key
(mclass
) then
2333 var supers
= new HashSet[MClass]
2334 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2335 for sup
in self.flatten_mclass_hierarchy
[mclass
].greaters
do
2336 if sup
== mclass
then continue
2340 self.super_mclasses_cache
[mclass
] = supers
2342 return self.super_mclasses_cache
[mclass
]
2345 private var super_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2347 # Return all parents of a `mclass` in `self`
2348 fun parent_mclasses
(mclass
: MClass): Set[MClass] do
2349 if not self.parent_mclasses_cache
.has_key
(mclass
) then
2350 var parents
= new HashSet[MClass]
2351 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2352 for sup
in self.flatten_mclass_hierarchy
[mclass
].direct_greaters
do
2353 if sup
== mclass
then continue
2357 self.parent_mclasses_cache
[mclass
] = parents
2359 return self.parent_mclasses_cache
[mclass
]
2362 private var parent_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2364 # Return all sub mclasses (directs and indirects) of a `mclass` in `self`
2365 fun sub_mclasses
(mclass
: MClass): Set[MClass] do
2366 if not self.sub_mclasses_cache
.has_key
(mclass
) then
2367 var subs
= new HashSet[MClass]
2368 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2369 for sub
in self.flatten_mclass_hierarchy
[mclass
].smallers
do
2370 if sub
== mclass
then continue
2374 self.sub_mclasses_cache
[mclass
] = subs
2376 return self.sub_mclasses_cache
[mclass
]
2379 private var sub_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2381 # All 'mproperties' associated to all 'mclassdefs' of `mclass`
2382 fun properties
(mclass
: MClass): Set[MProperty] do
2383 if not self.properties_cache
.has_key
(mclass
) then
2384 var properties
= new HashSet[MProperty]
2385 var parents
= self.super_mclasses
(mclass
)
2386 for parent
in parents
do
2387 properties
.add_all
(self.properties
(parent
))
2390 for mclassdef
in mclass
.mclassdefs
do
2391 for mpropdef
in mclassdef
.mpropdefs
do
2392 properties
.add
(mpropdef
.mproperty
)
2395 self.properties_cache
[mclass
] = properties
2397 return properties_cache
[mclass
]
2400 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
2403 # A sorter for linearize list of types
2404 private class TypeSorter
2405 super AbstractSorter[MType]
2407 private var mmodule
: MModule
2409 init(mmodule
: MModule) do self.mmodule
= mmodule
2411 redef fun compare
(a
, b
) do
2414 else if a
.is_subtype
(self.mmodule
, null, b
) then
2421 # A sorter for reverse linearization
2422 private class ReverseTypeSorter
2425 init(mmodule
: MModule) do end
2427 redef fun compare
(a
, b
) do
2430 else if a
.is_subtype
(self.mmodule
, null, b
) then
2437 # A sorter for linearize list of classes
2438 private class ClassSorter
2439 super AbstractSorter[MClass]
2441 var mmodule
: MModule
2443 redef fun compare
(a
, b
) do
2446 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then
2453 # A sorter for reverse linearization
2454 private class ReverseClassSorter
2455 super AbstractSorter[MClass]
2457 var mmodule
: MModule
2459 redef fun compare
(a
, b
) do
2462 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then