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_cc_path
: OptionArray = new OptionArray("Set include path for C header files (may be used more than once)", "--cc-path")
33 var opt_make_flags
: OptionString = new OptionString("Additional options to make", "--make-flags")
35 var opt_hardening
: OptionBool = new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
37 var opt_no_shortcut_range
: OptionBool = new OptionBool("Always insantiate a range and its iterator on 'for' loops", "--no-shortcut-range")
38 # --no-check-covariance
39 var opt_no_check_covariance
: OptionBool = new OptionBool("Disable type tests of covariant parameters (dangerous)", "--no-check-covariance")
40 # --no-check-initialization
41 var opt_no_check_initialization
: OptionBool = new OptionBool("Disable isset tests at the end of constructors (dangerous)", "--no-check-initialization")
43 var opt_no_check_assert
: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
45 var opt_no_check_autocast
: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
47 var opt_no_check_other
: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
48 # --typing-test-metrics
49 var opt_typing_test_metrics
: OptionBool = new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics")
54 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_make_flags
, self.opt_hardening
, self.opt_no_shortcut_range
)
55 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
)
56 self.option_context
.add_option
(self.opt_typing_test_metrics
)
60 redef class ModelBuilder
61 # The list of directories to search for included C headers (-I for C compilers)
62 # The list is initially set with :
63 # * the toolcontext --cc-path option
64 # * the NIT_CC_PATH environment variable
65 # * some heuristics including the NIT_DIR environment variable and the progname of the process
66 # Path can be added (or removed) by the client
67 var cc_paths
= new Array[String]
69 redef init(model
, toolcontext
)
73 # Look for the the Nit clib path
74 var path_env
= "NIT_DIR".environ
75 if not path_env
.is_empty
then
76 var libname
= "{path_env}/clib"
77 if libname
.file_exists
then cc_paths
.add
(libname
)
80 var libname
= "{sys.program_name.dirname}/../clib"
81 if libname
.file_exists
then cc_paths
.add
(libname
.simplify_path
)
83 if cc_paths
.is_empty
then
84 toolcontext
.error
(null, "Cannot determine the nit clib path. define envvar NIT_DIR.")
87 # Add user defined cc_paths
88 cc_paths
.append
(toolcontext
.opt_cc_path
.value
)
90 path_env
= "NIT_CC_PATH".environ
91 if not path_env
.is_empty
then
92 cc_paths
.append
(path_env
.split_with
(':'))
97 protected fun write_and_make
(compiler
: AbstractCompiler)
99 var mainmodule
= compiler
.mainmodule
101 # Generate the .h and .c files
102 # A single C file regroups many compiled rumtime functions
103 # 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
108 var outname
= self.toolcontext
.opt_output
.value
109 if outname
== null then
110 outname
= "{mainmodule.name}.bin"
113 var hfilename
= compiler
.header
.file
.name
+ ".h"
114 var hfilepath
= ".nit_compile/{hfilename}"
115 var h
= new OFStream.open
(hfilepath
)
116 for l
in compiler
.header
.decl_lines
do
120 for l
in compiler
.header
.lines
do
126 var cfiles
= new Array[String]
128 for f
in compiler
.files
do
130 var hfile
: nullable OFStream = null
132 var cfilename
= ".nit_compile/{f.name}.0.h"
133 hfile
= new OFStream.open
(cfilename
)
134 hfile
.write
"#include \"{hfilename}\
"\n"
135 for key
in f
.required_declarations
do
136 if not compiler
.provided_declarations
.has_key
(key
) then
137 print
"No provided declaration for {key}"
140 hfile
.write compiler
.provided_declarations
[key
]
144 var file
: nullable OFStream = null
145 for vis
in f
.writers
do
146 if vis
== compiler
.header
then continue
147 var total_lines
= vis
.lines
.length
+ vis
.decl_lines
.length
148 if total_lines
== 0 then continue
150 if file
== null or count
> 10000 then
152 if file
!= null then file
.close
153 cfilename
= ".nit_compile/{f.name}.{i}.c"
154 self.toolcontext
.info
("new C source files to compile: {cfilename}", 3)
155 cfiles
.add
(cfilename
)
156 file
= new OFStream.open
(cfilename
)
157 file
.write
"#include \"{f.name}.0.h\
"\n"
160 for l
in vis
.decl_lines
do
164 for l
in vis
.lines
do
169 if file
!= null then file
.close
172 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
174 # Generate the Makefile
176 var makename
= ".nit_compile/{mainmodule.name}.mk"
177 var makefile
= new OFStream.open
(makename
)
181 #p = "..".join_path(p)
182 cc_includes
+= " -I \"" + p + "\
""
184 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2{cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
185 makefile
.write
("all: {outname}\n\n")
187 var ofiles
= new Array[String]
188 # Compile each generated file
190 var o
= f
.strip_extension
(".c") + ".o"
191 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -D NONITCNI -c -o {o} {f}\n\n")
195 # Add gc_choser.h to aditionnal bodies
196 var gc_chooser
= new ExternCFile("{cc_paths.first}/gc_chooser.c", "-DWITH_LIBGC")
197 compiler
.extern_bodies
.add
(gc_chooser
)
199 # Compile each required extern body into a specific .o
200 for f
in compiler
.extern_bodies
do
201 var basename
= f
.filename
.basename
(".c")
202 var o
= ".nit_compile/{basename}.extern.o"
203 makefile
.write
("{o}: {f.filename}\n\t$(CC) $(CFLAGS) -D NONITCNI {f.cflags} -c -o {o} {f.filename}\n\n")
208 makefile
.write
("{outname}: {ofiles.join(" ")}\n\t$(CC) $(LDFLAGS) -o {outname} {ofiles.join(" ")} $(LDLIBS)\n\n")
210 makefile
.write
("clean:\n\trm {ofiles.join(" ")} 2>/dev/null\n\n")
212 self.toolcontext
.info
("Generated makefile: {makename}", 2)
215 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
217 # Execute the Makefile
219 if self.toolcontext
.opt_no_cc
.value
then return
222 self.toolcontext
.info
("*** COMPILING C ***", 1)
223 var makeflags
= self.toolcontext
.opt_make_flags
.value
224 if makeflags
== null then makeflags
= ""
225 self.toolcontext
.info
("make -B -f {makename} -j 4 {makeflags}", 2)
228 if self.toolcontext
.verbose_level
>= 3 then
229 res
= sys
.system
("make -B -f {makename} -j 4 {makeflags} 2>&1")
231 res
= sys
.system
("make -B -f {makename} -j 4 {makeflags} 2>&1 >/dev/null")
234 toolcontext
.error
(null, "make failed! Error code: {res}.")
238 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
242 # Singleton that store the knowledge about the compilation process
243 abstract class AbstractCompiler
244 type VISITOR: AbstractCompilerVisitor
246 # The main module of the program
247 var mainmodule
: MModule protected writable
249 # The modeulbuilder used to know the model and the AST
250 var modelbuilder
: ModelBuilder protected writable
252 # Is hardening asked? (see --hardening)
253 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
255 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
257 self.mainmodule
= mainmodule
258 self.modelbuilder
= modelbuilder
261 # Force the creation of a new file
262 # The point is to avoid contamination between must-be-compiled-separately files
263 fun new_file
(name
: String): CodeFile
265 var f
= new CodeFile(name
)
270 # The list of all associated files
271 # Used to generate .c files
272 var files
: List[CodeFile] = new List[CodeFile]
274 # Initialize a visitor specific for a compiler engine
275 fun new_visitor
: VISITOR is abstract
277 # Where global declaration are stored (the main .h)
278 var header
: CodeWriter writable
280 # Provide a declaration that can be requested (before or latter) by a visitor
281 fun provide_declaration
(key
: String, s
: String)
283 if self.provided_declarations
.has_key
(key
) then
284 assert self.provided_declarations
[key
] == s
286 self.provided_declarations
[key
] = s
289 private var provided_declarations
= new HashMap[String, String]
292 # This method call compile_header_strucs method that has to be refined
293 fun compile_header
do
295 self.header
.add_decl
("#include <stdlib.h>")
296 self.header
.add_decl
("#include <stdio.h>")
297 self.header
.add_decl
("#include <string.h>")
298 self.header
.add_decl
("#include <gc_chooser.h>")
300 compile_header_structs
302 # Global variable used by the legacy native interface
303 self.header
.add_decl
("extern int glob_argc;")
304 self.header
.add_decl
("extern char **glob_argv;")
305 self.header
.add_decl
("extern val *glob_sys;")
308 # Declaration of structures the live Nit types
309 protected fun compile_header_structs
is abstract
311 # Generate the main C function.
313 # * allocate the Sys object if it exists
314 # * call init if is exists
315 # * call main if it exists
316 fun compile_main_function
318 var v
= self.new_visitor
319 v
.add_decl
("int glob_argc;")
320 v
.add_decl
("char **glob_argv;")
321 v
.add_decl
("val *glob_sys;")
323 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
324 for tag
in count_type_test_tags
do
325 v
.add_decl
("long count_type_test_resolved_{tag};")
326 v
.add_decl
("long count_type_test_unresolved_{tag};")
327 v
.add_decl
("long count_type_test_skipped_{tag};")
328 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
329 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
330 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
333 v
.add_decl
("int main(int argc, char** argv) \{")
334 v
.add
("glob_argc = argc; glob_argv = argv;")
335 v
.add
("initialize_gc_option();")
336 var main_type
= mainmodule
.sys_type
337 if main_type
!= null then
338 var mainmodule
= v
.compiler
.mainmodule
339 var glob_sys
= v
.init_instance
(main_type
)
340 v
.add
("glob_sys = {glob_sys};")
341 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
342 if main_init
!= null then
343 v
.send
(main_init
, [glob_sys
])
345 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
346 if main_method
!= null then
347 v
.send
(main_method
, [glob_sys
])
351 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
352 v
.add_decl
("long count_type_test_resolved_total = 0;")
353 v
.add_decl
("long count_type_test_unresolved_total = 0;")
354 v
.add_decl
("long count_type_test_skipped_total = 0;")
355 v
.add_decl
("long count_type_test_total_total = 0;")
356 for tag
in count_type_test_tags
do
357 v
.add_decl
("long count_type_test_total_{tag};")
358 v
.add
("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
359 v
.add
("count_type_test_resolved_total += count_type_test_resolved_{tag};")
360 v
.add
("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
361 v
.add
("count_type_test_skipped_total += count_type_test_skipped_{tag};")
362 v
.add
("count_type_test_total_total += count_type_test_total_{tag};")
364 v
.add
("printf(\"# dynamic count_type_test: total %l\\n\");")
365 v
.add
("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\
");")
366 var tags
= count_type_test_tags
.to_a
369 v
.add
("printf(\"{tag}\
");")
370 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
371 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
372 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
373 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\\n\
", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
380 # List of additional .c files required to compile (native interface)
381 var extern_bodies
= new Array[ExternCFile]
383 # This is used to avoid adding an extern file more than once
384 private var seen_extern
= new ArraySet[String]
386 # Generate code that check if an instance is correctly initialized
387 fun generate_check_init_instance
(mtype
: MClassType) is abstract
389 # Generate code that initialize the attributes on a new instance
390 fun generate_init_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
392 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
393 self.mainmodule
.linearize_mclassdefs
(cds
)
395 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
396 for npropdef
in n
.n_propdefs
do
397 if npropdef
isa AAttrPropdef then
398 npropdef
.init_expr
(v
, recv
)
404 # Generate code that check if an attribute is correctly initialized
405 fun generate_check_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
407 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
408 self.mainmodule
.linearize_mclassdefs
(cds
)
410 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
411 for npropdef
in n
.n_propdefs
do
412 if npropdef
isa AAttrPropdef then
413 npropdef
.check_expr
(v
, recv
)
421 var count_type_test_tags
: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
422 var count_type_test_resolved
: HashMap[String, Int] = init_count_type_test_tags
423 var count_type_test_unresolved
: HashMap[String, Int] = init_count_type_test_tags
424 var count_type_test_skipped
: HashMap[String, Int] = init_count_type_test_tags
426 protected fun init_count_type_test_tags
: HashMap[String, Int]
428 var res
= new HashMap[String, Int]
429 for tag
in count_type_test_tags
do
435 # Display stats about compilation process
437 # * type tests against resolved types (x isa Collection[Animal])
438 # * type tests against unresolved types (x isa Collection[E])
439 # * type tests skipped
444 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
445 print
"# static count_type_test"
446 print
"\tresolved:\tunresolved\tskipped\ttotal"
447 var count_type_test_total
= init_count_type_test_tags
448 count_type_test_resolved
["total"] = 0
449 count_type_test_unresolved
["total"] = 0
450 count_type_test_skipped
["total"] = 0
451 count_type_test_total
["total"] = 0
452 for tag
in count_type_test_tags
do
453 count_type_test_total
[tag
] = count_type_test_resolved
[tag
] + count_type_test_unresolved
[tag
] + count_type_test_skipped
[tag
]
454 count_type_test_resolved
["total"] += count_type_test_resolved
[tag
]
455 count_type_test_unresolved
["total"] += count_type_test_unresolved
[tag
]
456 count_type_test_skipped
["total"] += count_type_test_skipped
[tag
]
457 count_type_test_total
["total"] += count_type_test_total
[tag
]
459 var count_type_test
= count_type_test_total
["total"]
460 var tags
= count_type_test_tags
.to_a
464 printn
"\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
465 printn
"\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
466 printn
"\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
467 printn
"\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
474 # Avoid division by zero by returning the string "n/a"
475 fun div
(a
,b
:Int):String
477 if b
== 0 then return "n/a"
478 return ((a
*10000/b
).to_f
/ 100.0).to_precision
(2)
482 # A file unit (may be more than one file if
483 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
486 var writers
= new Array[CodeWriter]
487 var required_declarations
= new HashSet[String]
490 # Where to store generated lines
493 var lines
: List[String] = new List[String]
494 var decl_lines
: List[String] = new List[String]
496 # Add a line in the main part of the generated C
497 fun add
(s
: String) do self.lines
.add
(s
)
500 # (used for local or global declaration)
501 fun add_decl
(s
: String) do self.decl_lines
.add
(s
)
506 file
.writers
.add
(self)
510 # A visitor on the AST of property definition that generate the C code.
511 abstract class AbstractCompilerVisitor
513 type COMPILER: AbstractCompiler
515 # The associated compiler
516 var compiler
: COMPILER
518 # The current visited AST node
519 var current_node
: nullable ANode writable = null
522 var frame
: nullable Frame writable
524 # Alias for self.compiler.mainmodule.object_type
525 fun object_type
: MClassType do return self.compiler
.mainmodule
.object_type
527 # Alias for self.compiler.mainmodule.bool_type
528 fun bool_type
: MClassType do return self.compiler
.mainmodule
.bool_type
530 var writer
: CodeWriter
532 init(compiler
: COMPILER)
534 self.compiler
= compiler
535 self.writer
= new CodeWriter(compiler
.files
.last
)
538 # Force to get the primitive class named `name' or abort
539 fun get_class
(name
: String): MClass do return self.compiler
.mainmodule
.get_primitive_class
(name
)
541 # Force to get the primitive property named `name' in the instance `recv' or abort
542 fun get_property
(name
: String, recv
: MType): MMethod
544 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, recv
, self.compiler
.mainmodule
)
547 fun compile_callsite
(callsite
: CallSite, args
: Array[RuntimeVariable]): nullable RuntimeVariable
549 return self.send
(callsite
.mproperty
, args
)
552 fun calloc_array
(ret_type
: MType, arguments
: Array[RuntimeVariable]) is abstract
554 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable]) is abstract
556 # Transform varargs, in raw arguments, into a single argument of type Array
557 # Note: this method modify the given `args`
558 # If there is no vararg, then `args` is not modified.
559 fun varargize
(mpropdef
: MPropDef, msignature
: MSignature, args
: Array[RuntimeVariable])
561 var recv
= args
.first
562 var vararg_rank
= msignature
.vararg_rank
563 if vararg_rank
>= 0 then
564 assert args
.length
>= msignature
.arity
+ 1 # because of self
566 args
= new Array[RuntimeVariable]
568 args
.add
(rawargs
.first
) # recv
570 for i
in [0..vararg_rank
[ do
571 args
.add
(rawargs
[i
+1])
574 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-msignature
.arity
575 var vararg
= new Array[RuntimeVariable]
576 for i
in [vararg_rank
..vararg_lastrank
] do
577 vararg
.add
(rawargs
[i
+1])
580 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
581 args
.add
(self.vararg_instance
(mpropdef
, recv
, vararg
, elttype
))
583 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
584 args
.add
(rawargs
[i
+1])
587 rawargs
.add_all
(args
)
593 # Anchor a type to the main module and the current receiver
594 fun anchor
(mtype
: MType): MType
596 if not mtype
.need_anchor
then return mtype
597 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
600 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
602 if not mtype
.need_anchor
then return mtype
603 return mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
606 # Unsafely cast a value to a new type
607 # ie the result share the same C variable but my have a different mcasttype
608 # NOTE: if the adaptation is useless then `value' is returned as it.
609 # ENSURE: return.name == value.name
610 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
612 mtype
= self.anchor
(mtype
)
613 var valmtype
= value
.mcasttype
614 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
618 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
619 var res
= new RuntimeVariable(value
.name
, valmtype
, valmtype
.mtype
)
622 var res
= new RuntimeVariable(value
.name
, valmtype
, mtype
)
627 # Generate a super call from a method definition
628 fun supercall
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
630 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
632 # Box or unbox a value to another type iff a C type conversion is needed
633 # ENSURE: result.mtype.ctype == mtype.ctype
634 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
636 # Generate a polymorphic subtype test
637 fun type_test
(value
: RuntimeVariable, mtype
: MType, tag
: String): RuntimeVariable is abstract
639 # Generate the code required to dynamically check if 2 objects share the same runtime type
640 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
642 # Generate a Nit "is" for two runtime_variables
643 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
647 # Generate a static call on a method definition
648 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
650 # Generate a polymorphic send for the method `m' and the arguments `args'
651 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
653 # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
654 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
656 assert t
isa MClassType
657 var propdef
= m
.lookup_first_definition
(self.compiler
.mainmodule
, t
)
658 return self.call
(propdef
, t
, args
)
661 # Attributes handling
663 # Generate a polymorphic attribute is_set test
664 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
666 # Generate a polymorphic attribute read
667 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
669 # Generate a polymorphic attribute write
670 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable) is abstract
674 # Add a check and an abort for a null reciever if needed
675 fun check_recv_notnull
(recv
: RuntimeVariable)
677 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then return
679 var maybenull
= recv
.mcasttype
isa MNullableType or recv
.mcasttype
isa MNullType
681 self.add
("if ({recv} == NULL) \{")
682 self.add_abort
("Reciever is null")
687 # Generate a check-init-instance
688 fun check_init_instance
(recv
: RuntimeVariable, mtype
: MClassType) is abstract
692 private var names
: HashSet[String] = new HashSet[String]
693 private var last
: Int = 0
695 # Return a new name based on `s' and unique in the visitor
696 fun get_name
(s
: String): String
698 if not self.names
.has
(s
) then
702 var i
= self.last
+ 1
705 if not self.names
.has
(s2
) then
714 # Return an unique and stable identifier associated with an escapemark
715 fun escapemark_name
(e
: nullable EscapeMark): String
718 if escapemark_names
.has_key
(e
) then return escapemark_names
[e
]
720 if name
== null then name
= "label"
721 name
= get_name
(name
)
722 escapemark_names
[e
] = name
726 private var escapemark_names
= new HashMap[EscapeMark, String]
728 # Return a "const char*" variable associated to the classname of the dynamic type of an object
729 # NOTE: we do not return a RuntimeVariable "NativeString" as the class may not exist in the module/program
730 fun class_name_string
(value
: RuntimeVariable): String is abstract
734 protected var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
736 # Return the local runtime_variable associated to a Nit local variable
737 fun variable
(variable
: Variable): RuntimeVariable
739 if self.variables
.has_key
(variable
) then
740 return self.variables
[variable
]
742 var name
= self.get_name
("var_{variable.name}")
743 var mtype
= variable
.declared_type
.as(not null)
744 mtype
= self.anchor
(mtype
)
745 var res
= new RuntimeVariable(name
, mtype
, mtype
)
746 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
747 self.variables
[variable
] = res
752 # Return a new uninitialized local runtime_variable
753 fun new_var
(mtype
: MType): RuntimeVariable
755 mtype
= self.anchor
(mtype
)
756 var name
= self.get_name
("var")
757 var res
= new RuntimeVariable(name
, mtype
, mtype
)
758 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
762 # Return a new uninitialized named runtime_variable
763 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
765 mtype
= self.anchor
(mtype
)
766 var res
= new RuntimeVariable(name
, mtype
, mtype
)
767 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
771 # Correctly assign a left and a right value
772 # Boxing and unboxing is performed if required
773 fun assign
(left
, right
: RuntimeVariable)
775 right
= self.autobox
(right
, left
.mtype
)
776 self.add
("{left} = {right};")
781 # Generate a alloc-instance + init-attributes
782 fun init_instance
(mtype
: MClassType): RuntimeVariable is abstract
784 # Generate an integer value
785 fun int_instance
(value
: Int): RuntimeVariable
787 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
788 self.add
("{res} = {value};")
792 # Generate a string value
793 fun string_instance
(string
: String): RuntimeVariable
795 var mtype
= self.get_class
("String").mclass_type
796 var name
= self.get_name
("varonce")
797 self.add_decl
("static {mtype.ctype} {name};")
798 var res
= self.new_var
(mtype
)
799 self.add
("if ({name}) \{")
800 self.add
("{res} = {name};")
801 self.add
("\} else \{")
802 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
803 self.add
("{nat} = \"{string.escape_to_c}\
";")
804 var res2
= self.init_instance
(mtype
)
805 self.add
("{res} = {res2};")
806 var length
= self.int_instance
(string
.length
)
807 self.send
(self.get_property
("with_native", mtype
), [res
, nat
, length
])
808 self.check_init_instance
(res
, mtype
)
809 self.add
("{name} = {res};")
814 # Generate an array value
815 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
817 # Get an instance of a array for a vararg
818 fun vararg_instance
(mpropdef
: MPropDef, recv
: RuntimeVariable, varargs
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
822 # Add a line in the main part of the generated C
823 fun add
(s
: String) do self.writer
.lines
.add
(s
)
826 # (used for local or global declaration)
827 fun add_decl
(s
: String) do self.writer
.decl_lines
.add
(s
)
829 # Request the presence of a global declaration
830 fun require_declaration
(key
: String)
832 self.writer
.file
.required_declarations
.add
(key
)
835 # Add a declaration in the local-header
836 # The declaration is ensured to be present once
837 fun declare_once
(s
: String)
839 self.compiler
.provide_declaration
(s
, s
)
840 self.require_declaration
(s
)
843 # look for a needed .h and .c file for a given .nit source-file
844 # FIXME: bad API, parameter should be a MModule, not its source-file
845 fun add_extern
(file
: String)
847 file
= file
.strip_extension
(".nit")
848 var tryfile
= file
+ ".nit.h"
849 if tryfile
.file_exists
then
850 self.declare_once
("#include \"{"..".join_path(tryfile)}\
"")
852 tryfile
= file
+ "_nit.h"
853 if tryfile
.file_exists
then
854 self.declare_once
("#include \"{"..".join_path(tryfile)}\
"")
857 if self.compiler
.seen_extern
.has
(file
) then return
858 self.compiler
.seen_extern
.add
(file
)
859 tryfile
= file
+ ".nit.c"
860 if not tryfile
.file_exists
then
861 tryfile
= file
+ "_nit.c"
862 if not tryfile
.file_exists
then return
864 var f
= new ExternCFile(tryfile
, "")
865 self.compiler
.extern_bodies
.add
(f
)
868 # Return a new local runtime_variable initialized with the C expression `cexpr'.
869 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
871 var res
= new_var
(mtype
)
872 self.add
("{res} = {cexpr};")
876 # Generate generic abort
877 # used by aborts, asserts, casts, etc.
878 fun add_abort
(message
: String)
880 if self.current_node
!= null and self.current_node
.location
.file
!= null then
881 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});")
883 self.add
("fprintf(stderr, \"Runtime error
: %s\\n\
", \"{message.escape_to_c}\
");")
888 # Generate a return with the value `s'
889 fun ret
(s
: RuntimeVariable)
891 self.assign
(self.frame
.returnvar
.as(not null), s
)
892 self.add
("goto {self.frame.returnlabel.as(not null)};")
895 # Compile a statement (if any)
896 fun stmt
(nexpr
: nullable AExpr)
898 if nexpr
== null then return
899 var old
= self.current_node
900 self.current_node
= nexpr
902 self.current_node
= old
905 # Compile an expression an return its result
906 # `mtype` is the expected return type, pass null if no specific type is expected.
907 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
909 var old
= self.current_node
910 self.current_node
= nexpr
911 var res
= nexpr
.expr
(self).as(not null)
912 if mtype
!= null then
913 mtype
= self.anchor
(mtype
)
914 res
= self.autobox
(res
, mtype
)
916 res
= autoadapt
(res
, nexpr
.mtype
.as(not null))
917 var implicit_cast_to
= nexpr
.implicit_cast_to
918 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
919 var castres
= self.type_test
(res
, implicit_cast_to
, "auto")
920 self.add
("if (!{castres}) \{")
921 self.add_abort
("Cast failed")
923 res
= autoadapt
(res
, implicit_cast_to
)
925 self.current_node
= old
929 # Alias for `self.expr(nexpr, self.bool_type)'
930 fun expr_bool
(nexpr
: AExpr): RuntimeVariable do return expr
(nexpr
, bool_type
)
932 # Safely show a debug message on the current node and repeat the message in the C code as a comment
933 fun debug
(message
: String)
935 var node
= self.current_node
941 self.add
("/* DEBUG: {message} */")
945 # A C function associated to a Nit method
946 # Because of customization, a given Nit method can be compiler more that once
947 abstract class AbstractRuntimeFunction
949 type COMPILER: AbstractCompiler
950 type VISITOR: AbstractCompilerVisitor
952 # The associated Nit method
953 var mmethoddef
: MMethodDef
955 # The mangled c name of the runtime_function
956 # Subclasses should redefine `build_c_name` instead
959 var res
= self.c_name_cache
960 if res
!= null then return res
961 res
= self.build_c_name
962 self.c_name_cache
= res
966 # Non cached version of `c_name`
967 protected fun build_c_name
: String is abstract
969 protected var c_name_cache
: nullable String writable = null
971 # Implements a call of the runtime_function
972 # May inline the body or generate a C function call
973 fun call
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
975 # Generate the code for the RuntimeFunction
976 # Warning: compile more than once compilation makes CC unhappy
977 fun compile_to_c
(compiler
: COMPILER) is abstract
980 # A runtime variable hold a runtime value in C.
981 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
983 # 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.
984 class RuntimeVariable
985 # The name of the variable in the C code
988 # The static type of the variable (as declard in C)
991 # The current casted type of the variable (as known in Nit)
992 var mcasttype
: MType writable
994 # If the variable exaclty a mcasttype?
995 # false (usual value) means that the variable is a mcasttype or a subtype.
996 var is_exact
: Bool writable = false
998 init(name
: String, mtype
: MType, mcasttype
: MType)
1002 self.mcasttype
= mcasttype
1003 assert not mtype
.need_anchor
1004 assert not mcasttype
.need_anchor
1007 redef fun to_s
do return name
1012 if self.is_exact
then
1013 exact_str
= " exact"
1018 if self.mtype
== self.mcasttype
then
1019 type_str
= "{mtype}{exact_str}"
1021 type_str
= "{mtype}({mcasttype}{exact_str})"
1023 return "<{name}:{type_str}>"
1027 # A frame correspond to a visited property in a GlobalCompilerVisitor
1030 type VISITOR: AbstractCompilerVisitor
1032 # The associated visitor
1033 var visitor
: VISITOR
1035 # The executed property.
1036 # A Method in case of a call, an attribute in case of a default initialization.
1037 var mpropdef
: MPropDef
1039 # The static type of the receiver
1040 var receiver
: MClassType
1042 # Arguments of the method (the first is the receiver)
1043 var arguments
: Array[RuntimeVariable]
1045 # The runtime_variable associated to the return (in a function)
1046 var returnvar
: nullable RuntimeVariable writable = null
1048 # The label at the end of the property
1049 var returnlabel
: nullable String writable = null
1052 # An extern C file to compile
1054 # The filename of the file
1055 var filename
: String
1056 # Additionnal specific CC compiler -c flags
1061 # Mangle a string to be a unique valid C identifier
1062 fun to_cmangle
: String
1064 var res
= new Buffer
1065 var underscore
= false
1067 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
1073 res
.append
('_'.ascii
.to_s
)
1076 if c
>= '0' and c
<= '9' then
1079 else if c
== '_' then
1084 res
.append
(c
.ascii
.to_s
)
1092 # Escape " \ ' and non printable characters for literal C strings or characters
1093 fun escape_to_c
: String
1099 else if c
== '\0' then
1101 else if c
== '"' then
1103 else if c == '\'' then
1105 else if c == '\\
' then
1107 else if c.ascii < 32 then
1108 b.append("\\{c.ascii.to_base(8, false)}")
1118 # Return the C type associated to a given Nit static type
1119 fun ctype: String do return "val*"
1121 fun ctypename: String do return "val"
1123 # Return the name of the C structure associated to a Nit live type
1124 fun c_name: String is abstract
1125 protected var c_name_cache: nullable String protected writable
1128 redef class MClassType
1131 var res = self.c_name_cache
1132 if res != null then return res
1133 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1134 self.c_name_cache = res
1138 redef fun ctype: String
1140 if mclass.name == "Int" then
1142 else if mclass.name == "Bool" then
1144 else if mclass.name == "Char" then
1146 else if mclass.name == "Float" then
1148 else if mclass.name == "NativeString" then
1150 else if mclass.name == "NativeArray" then
1152 else if mclass.kind == extern_kind then
1159 redef fun ctypename: String
1161 if mclass.name == "Int" then
1163 else if mclass.name == "Bool" then
1165 else if mclass.name == "Char" then
1167 else if mclass.name == "Float" then
1169 else if mclass.name == "NativeString" then
1171 else if mclass.name == "NativeArray" then
1172 #return "{self.arguments.first.ctype}*"
1174 else if mclass.kind == extern_kind then
1182 redef class MGenericType
1185 var res = self.c_name_cache
1186 if res != null then return res
1188 for t in self.arguments do
1189 res = res + t.c_name
1191 self.c_name_cache = res
1196 redef class MParameterType
1199 var res = self.c_name_cache
1200 if res != null then return res
1201 res = "{self.mclass.c_name}_FT{self.rank}"
1202 self.c_name_cache = res
1207 redef class MVirtualType
1210 var res = self.c_name_cache
1211 if res != null then return res
1212 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1213 self.c_name_cache = res
1218 redef class MNullableType
1221 var res = self.c_name_cache
1222 if res != null then return res
1223 res = "nullable_{self.mtype.c_name}"
1224 self.c_name_cache = res
1230 # Return the name of the C structure associated to a Nit class
1231 fun c_name: String do
1232 var res = self.c_name_cache
1233 if res != null then return res
1234 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1235 self.c_name_cache = res
1238 private var c_name_cache: nullable String
1241 redef class MProperty
1242 fun c_name: String do
1243 var res = self.c_name_cache
1244 if res != null then return res
1245 res = "{self.intro.c_name}"
1246 self.c_name_cache = res
1249 private var c_name_cache: nullable String
1252 redef class MPropDef
1253 type VISITOR: AbstractCompilerVisitor
1255 private var c_name_cache: nullable String
1257 # The mangled name associated to the property
1260 var res = self.c_name_cache
1261 if res != null then return res
1262 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1263 self.c_name_cache = res
1268 redef class MMethodDef
1269 # Can the body be inlined?
1270 fun can_inline(v: VISITOR): Bool
1272 var modelbuilder = v.compiler.modelbuilder
1273 if modelbuilder.mpropdef2npropdef.has_key(self) then
1274 var npropdef = modelbuilder.mpropdef2npropdef[self]
1275 return npropdef.can_inline
1276 else if self.mproperty.name == "init" then
1277 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1284 # Inline the body in another visitor
1285 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1287 var modelbuilder = v.compiler.modelbuilder
1288 if modelbuilder.mpropdef2npropdef.has_key(self) then
1289 var npropdef = modelbuilder.mpropdef2npropdef[self]
1290 var oldnode = v.current_node
1291 v.current_node = npropdef
1292 self.compile_parameter_check(v, arguments)
1293 npropdef.compile_to_c(v, self, arguments)
1294 v.current_node = oldnode
1295 else if self.mproperty.name == "init" then
1296 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1297 var oldnode = v.current_node
1298 v.current_node = nclassdef
1299 self.compile_parameter_check(v, arguments)
1300 nclassdef.compile_to_c(v, self, arguments)
1301 v.current_node = oldnode
1308 # Generate type checks in the C code to check covariant parameters
1309 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1311 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1313 for i in [0..msignature.arity[ do
1314 # skip test for vararg since the array is instantiated with the correct polymorphic type
1315 if msignature.vararg_rank == i then continue
1317 # skip if the cast is not required
1318 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1319 if not origmtype.need_anchor then continue
1321 # get the parameter type
1322 var mtype = self.msignature.mparameters[i].mtype
1325 # note that v decides if and how to implements the cast
1326 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1327 var cond = v.type_test(arguments[i+1], mtype, "covariance")
1328 v.add("if (!{cond}) \{")
1329 v.add_abort("Cast failed")
1337 redef class APropdef
1338 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1340 v.add("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1341 debug("Not yet implemented")
1344 fun can_inline: Bool do return true
1347 redef class AConcreteMethPropdef
1348 redef fun compile_to_c(v, mpropdef, arguments)
1350 for i in [0..mpropdef.msignature.arity[ do
1351 var variable = self.n_signature.n_params[i].variable.as(not null)
1352 v.assign(v.variable(variable), arguments[i+1])
1354 # Call the implicit super-init
1355 var auto_super_inits = self.auto_super_inits
1356 if auto_super_inits != null then
1357 var selfarg = [arguments.first]
1358 for auto_super_init in auto_super_inits do
1359 if auto_super_init.intro.msignature.arity == 0 then
1360 v.send(auto_super_init, selfarg)
1362 v.send(auto_super_init, arguments)
1366 v.stmt(self.n_block)
1369 redef fun can_inline
1371 if self.auto_super_inits != null then return false
1372 var nblock = self.n_block
1373 if nblock == null then return true
1374 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1375 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1380 redef class AInternMethPropdef
1381 redef fun compile_to_c(v, mpropdef, arguments)
1383 var pname = mpropdef.mproperty.name
1384 var cname = mpropdef.mclassdef.mclass.name
1385 var ret = mpropdef.msignature.return_mtype
1387 ret = v.resolve_for(ret, arguments.first)
1389 if pname != "==" and pname != "!=" then
1390 v.adapt_signature(mpropdef, arguments)
1392 if cname == "Int" then
1393 if pname == "output" then
1394 v.add("printf(\"%ld\\n\", {arguments.first});")
1396 else if pname == "object_id" then
1397 v.ret(arguments.first)
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.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
1423 else if pname == "lshift" then
1424 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
1426 else if pname == "rshift" then
1427 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
1429 else if pname == "==" then
1430 v.ret(v.equal_test(arguments[0], arguments[1]))
1432 else if pname == "!=" then
1433 var res = v.equal_test(arguments[0], arguments[1])
1434 v.ret(v.new_expr("!{res}", 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 == ">" then
1440 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1442 else if pname == "<=" then
1443 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1445 else if pname == ">=" then
1446 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1448 else if pname == "to_f" then
1449 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
1451 else if pname == "ascii" then
1452 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1455 else if cname == "Char" then
1456 if pname == "output" then
1457 v.add("printf(\"%c\", {arguments.first});")
1459 else if pname == "object_id" then
1460 v.ret(arguments.first)
1462 else if pname == "+" then
1463 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1465 else if pname == "-" then
1466 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1468 else if pname == "==" then
1469 v.ret(v.equal_test(arguments[0], arguments[1]))
1471 else if pname == "!=" then
1472 var res = v.equal_test(arguments[0], arguments[1])
1473 v.ret(v.new_expr("!{res}", ret.as(not null)))
1475 else if pname == "succ" then
1476 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1478 else if pname == "prec" then
1479 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1481 else if pname == "<" then
1482 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1484 else if pname == ">" then
1485 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1487 else if pname == "<=" then
1488 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1490 else if pname == ">=" then
1491 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1493 else if pname == "to_i" then
1494 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
1496 else if pname == "ascii" then
1497 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
1500 else if cname == "Bool" then
1501 if pname == "output" then
1502 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
1504 else if pname == "object_id" then
1505 v.ret(arguments.first)
1507 else if pname == "==" then
1508 v.ret(v.equal_test(arguments[0], arguments[1]))
1510 else if pname == "!=" then
1511 var res = v.equal_test(arguments[0], arguments[1])
1512 v.ret(v.new_expr("!{res}", ret.as(not null)))
1515 else if cname == "Float" then
1516 if pname == "output" then
1517 v.add("printf(\"%f\\n\", {arguments.first});")
1519 else if pname == "object_id" then
1520 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
1522 else if pname == "+" then
1523 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1525 else if pname == "-" then
1526 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1528 else if pname == "unary -" then
1529 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1531 else if pname == "succ" then
1532 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1534 else if pname == "prec" then
1535 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1537 else if pname == "*" then
1538 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1540 else if pname == "/" then
1541 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1543 else if pname == "==" then
1544 v.ret(v.equal_test(arguments[0], arguments[1]))
1546 else if pname == "!=" then
1547 var res = v.equal_test(arguments[0], arguments[1])
1548 v.ret(v.new_expr("!{res}", ret.as(not null)))
1550 else if pname == "<" then
1551 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1553 else if pname == ">" then
1554 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1556 else if pname == "<=" then
1557 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1559 else if pname == ">=" then
1560 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1562 else if pname == "to_i" then
1563 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
1566 else if cname == "Char" then
1567 if pname == "output" then
1568 v.add("printf(\"%c\", {arguments.first});")
1570 else if pname == "object_id" then
1571 v.ret(arguments.first)
1573 else if pname == "==" then
1574 v.ret(v.equal_test(arguments[0], arguments[1]))
1576 else if pname == "!=" then
1577 var res = v.equal_test(arguments[0], arguments[1])
1578 v.ret(v.new_expr("!{res}", ret.as(not null)))
1580 else if pname == "ascii" then
1581 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1584 else if cname == "NativeString" then
1585 if pname == "[]" then
1586 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
1588 else if pname == "[]=" then
1589 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1591 else if pname == "copy_to" then
1592 v.add("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
1594 else if pname == "atoi" then
1595 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
1598 else if cname == "NativeArray" then
1599 v.native_array_def(pname, ret, arguments)
1602 if pname == "exit" then
1603 v.add("exit({arguments[1]});")
1605 else if pname == "sys" then
1606 v.ret(v.new_expr("glob_sys", ret.as(not null)))
1608 else if pname == "calloc_string" then
1609 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
1611 else if pname == "calloc_array" then
1612 v.calloc_array(ret.as(not null), arguments)
1614 else if pname == "object_id" then
1615 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
1617 else if pname == "is_same_type" then
1618 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
1620 else if pname == "output_class_name" then
1621 var nat = v.class_name_string(arguments.first)
1622 v.add("printf(\"%s\\n\", {nat});")
1624 else if pname == "native_class_name" then
1625 var nat = v.class_name_string(arguments.first)
1626 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
1628 else if pname == "force_garbage_collection" then
1629 v.add("nit_gcollect();")
1632 v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
1633 debug("Not implemented {mpropdef}")
1637 redef class AExternMethPropdef
1638 redef fun compile_to_c(v, mpropdef, arguments)
1641 var nextern = self.n_extern
1642 if nextern == null then
1643 v.add("fprintf(stderr, \"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
1647 externname = nextern.text.substring(1, nextern.text.length-2)
1648 if location.file != null then
1649 var file = location.file.filename
1652 var res: nullable RuntimeVariable = null
1653 var ret = mpropdef.msignature.return_mtype
1655 ret = v.resolve_for(ret, arguments.first)
1656 res = v.new_var(ret)
1658 v.adapt_signature(mpropdef, arguments)
1661 v.add("{externname}({arguments.join(", ")});")
1663 v.add("{res} = {externname}({arguments.join(", ")});")
1669 redef class AExternInitPropdef
1670 redef fun compile_to_c(v, mpropdef, arguments)
1673 var nextern = self.n_extern
1674 if nextern == null then
1675 v.add("printf(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
1679 externname = nextern.text.substring(1, nextern.text.length-2)
1680 if location.file != null then
1681 var file = location.file.filename
1684 v.adapt_signature(mpropdef, arguments)
1685 var ret = arguments.first.mtype
1686 var res = v.new_var(ret)
1690 v.add("{res} = {externname}({arguments.join(", ")});")
1695 redef class AAttrPropdef
1696 redef fun compile_to_c(v, mpropdef, arguments)
1698 if arguments.length == 1 then
1699 var res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
1700 v.assign(v.frame.returnvar.as(not null), res)
1702 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
1706 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1708 var nexpr = self.n_expr
1709 if nexpr != null then
1710 var oldnode = v.current_node
1711 v.current_node = self
1712 var old_frame = v.frame
1713 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1715 var value = v.expr(nexpr, self.mpropdef.static_mtype)
1716 v.write_attribute(self.mpropdef.mproperty, recv, value)
1718 v.current_node = oldnode
1722 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1724 var nexpr = self.n_expr
1725 if nexpr != null then return
1727 var oldnode = v.current_node
1728 v.current_node = self
1729 var old_frame = v.frame
1730 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1732 # Force read to check the initialization
1733 v.read_attribute(self.mpropdef.mproperty, recv)
1735 v.current_node = oldnode
1739 redef class AClassdef
1740 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1742 if mpropdef == self.mfree_init then
1743 var super_inits = self.super_inits
1744 if super_inits != null then
1745 assert arguments.length == 1
1746 for su in super_inits do
1747 v.send(su, arguments)
1751 var recv = arguments.first
1753 # Collect undefined attributes
1754 for npropdef in self.n_propdefs do
1755 if npropdef isa AAttrPropdef and npropdef.n_expr == null then
1756 v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
1766 redef class ADeferredMethPropdef
1767 redef fun compile_to_c(v, mpropdef, arguments) do v.add_abort("Deferred method called")
1768 redef fun can_inline do return true
1772 # Try to compile self as an expression
1773 # Do not call this method directly, use `v.expr' instead
1774 private fun expr
(v
: AbstractCompilerVisitor): nullable RuntimeVariable
1776 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
1777 var mtype
= self.mtype
1778 if mtype
== null then
1781 var res
= v
.new_var
(mtype
)
1782 v
.add
("/* {res} = NOT YET {class_name} */")
1787 # Try to compile self as a statement
1788 # Do not call this method directly, use `v.stmt' instead
1789 private fun stmt
(v
: AbstractCompilerVisitor)
1792 if res
!= null then v
.add
("{res};")
1796 redef class ABlockExpr
1799 for e
in self.n_expr
do v
.stmt
(e
)
1803 redef class AVardeclExpr
1806 var variable
= self.variable
.as(not null)
1807 var ne
= self.n_expr
1809 var i
= v
.expr
(ne
, variable
.declared_type
)
1810 v
.assign
(v
.variable
(variable
), i
)
1815 redef class AVarExpr
1818 var res
= v
.variable
(self.variable
.as(not null))
1819 var mtype
= self.mtype
.as(not null)
1820 return v
.autoadapt
(res
, mtype
)
1824 redef class AVarAssignExpr
1827 var variable
= self.variable
.as(not null)
1828 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
1829 v
.assign
(v
.variable
(variable
), i
)
1833 redef class AVarReassignExpr
1836 var variable
= self.variable
.as(not null)
1837 var vari
= v
.variable
(variable
)
1838 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
1839 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
1841 v
.assign
(v
.variable
(variable
), res
)
1845 redef class ASelfExpr
1846 redef fun expr
(v
) do return v
.frame
.arguments
.first
1849 redef class AContinueExpr
1850 redef fun stmt
(v
) do v
.add
("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
1853 redef class ABreakExpr
1854 redef fun stmt
(v
) do v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
1857 redef class AReturnExpr
1860 var nexpr
= self.n_expr
1861 if nexpr
!= null then
1862 var returnvar
= v
.frame
.returnvar
.as(not null)
1863 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
1864 v
.assign
(returnvar
, i
)
1866 v
.add
("goto {v.frame.returnlabel.as(not null)};")
1870 redef class AAbortExpr
1871 redef fun stmt
(v
) do v
.add_abort
("Aborted")
1877 var cond
= v
.expr_bool
(self.n_expr
)
1878 v
.add
("if ({cond})\{")
1886 redef class AIfexprExpr
1889 var res
= v
.new_var
(self.mtype
.as(not null))
1890 var cond
= v
.expr_bool
(self.n_expr
)
1891 v
.add
("if ({cond})\{")
1892 v
.assign
(res
, v
.expr
(self.n_then
, null))
1894 v
.assign
(res
, v
.expr
(self.n_else
, null))
1903 v
.stmt
(self.n_block
)
1904 var escapemark
= self.escapemark
1905 if escapemark
!= null then
1906 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1911 redef class AWhileExpr
1915 var cond
= v
.expr_bool
(self.n_expr
)
1916 v
.add
("if (!{cond}) break;")
1917 v
.stmt
(self.n_block
)
1918 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1920 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1924 redef class ALoopExpr
1928 v
.stmt
(self.n_block
)
1929 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1931 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1935 redef class AForExpr
1938 # Shortcut on explicit range
1939 # Avoid the instantiation of the range and the iterator
1940 var nexpr
= self.n_expr
1941 if self.variables
.length
== 1 and nexpr
isa AOrangeExpr and not v
.compiler
.modelbuilder
.toolcontext
.opt_no_shortcut_range
.value
then
1942 var from
= v
.expr
(nexpr
.n_expr
, null)
1943 var to
= v
.expr
(nexpr
.n_expr2
, null)
1944 var variable
= v
.variable
(variables
.first
)
1946 v
.assign
(variable
, from
)
1947 v
.add
("for(;;) \{ /* shortcut range */")
1949 var ok
= v
.send
(v
.get_property
("<", variable
.mtype
), [variable
, to
])
1951 v
.add
("if(!{ok}) break;")
1953 v
.stmt
(self.n_block
)
1955 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1956 var succ
= v
.send
(v
.get_property
("succ", variable
.mtype
), [variable
])
1958 v
.assign
(variable
, succ
)
1960 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1964 var cl
= v
.expr
(self.n_expr
, null)
1965 var it_meth
= self.method_iterator
1966 assert it_meth
!= null
1967 var it
= v
.send
(it_meth
, [cl
])
1970 var isok_meth
= self.method_is_ok
1971 assert isok_meth
!= null
1972 var ok
= v
.send
(isok_meth
, [it
])
1974 v
.add
("if(!{ok}) break;")
1975 if self.variables
.length
== 1 then
1976 var item_meth
= self.method_item
1977 assert item_meth
!= null
1978 var i
= v
.send
(item_meth
, [it
])
1980 v
.assign
(v
.variable
(variables
.first
), i
)
1981 else if self.variables
.length
== 2 then
1982 var key_meth
= self.method_key
1983 assert key_meth
!= null
1984 var i
= v
.send
(key_meth
, [it
])
1986 v
.assign
(v
.variable
(variables
[0]), i
)
1987 var item_meth
= self.method_item
1988 assert item_meth
!= null
1989 i
= v
.send
(item_meth
, [it
])
1991 v
.assign
(v
.variable
(variables
[1]), i
)
1995 v
.stmt
(self.n_block
)
1996 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1997 var next_meth
= self.method_next
1998 assert next_meth
!= null
1999 v
.send
(next_meth
, [it
])
2001 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2005 redef class AAssertExpr
2008 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
2010 var cond
= v
.expr_bool
(self.n_expr
)
2011 v
.add
("if (!{cond}) \{")
2015 v
.add_abort
("Assert '{nid.text}' failed")
2017 v
.add_abort
("Assert failed")
2026 var res
= v
.new_var
(self.mtype
.as(not null))
2027 var i1
= v
.expr_bool
(self.n_expr
)
2028 v
.add
("if ({i1}) \{")
2031 var i2
= v
.expr_bool
(self.n_expr2
)
2032 v
.add
("{res} = {i2};")
2038 redef class AAndExpr
2041 var res
= v
.new_var
(self.mtype
.as(not null))
2042 var i1
= v
.expr_bool
(self.n_expr
)
2043 v
.add
("if (!{i1}) \{")
2046 var i2
= v
.expr_bool
(self.n_expr2
)
2047 v
.add
("{res} = {i2};")
2053 redef class ANotExpr
2056 var cond
= v
.expr_bool
(self.n_expr
)
2057 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2061 redef class AOrElseExpr
2064 var res
= v
.new_var
(self.mtype
.as(not null))
2065 var i1
= v
.expr
(self.n_expr
, null)
2066 v
.add
("if ({i1}!=NULL) \{")
2069 var i2
= v
.expr
(self.n_expr2
, null)
2079 var value1
= v
.expr
(self.n_expr
, null)
2080 var value2
= v
.expr
(self.n_expr2
, null)
2081 return v
.equal_test
(value1
, value2
)
2085 redef class AIntExpr
2086 redef fun expr
(v
) do return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2089 redef class AFloatExpr
2090 redef fun expr
(v
) do return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2093 redef class ACharExpr
2094 redef fun expr
(v
) do return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2097 redef class AArrayExpr
2100 var mtype
= self.mtype
.as(MClassType).arguments
.first
2101 var array
= new Array[RuntimeVariable]
2102 for nexpr
in self.n_exprs
.n_exprs
do
2103 var i
= v
.expr
(nexpr
, mtype
)
2106 return v
.array_instance
(array
, mtype
)
2110 redef class AStringFormExpr
2111 redef fun expr
(v
) do return v
.string_instance
(self.value
.as(not null))
2114 redef class ASuperstringExpr
2117 var array
= new Array[RuntimeVariable]
2118 for ne
in self.n_exprs
do
2119 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
2120 var i
= v
.expr
(ne
, null)
2123 var a
= v
.array_instance
(array
, v
.object_type
)
2124 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2129 redef class ACrangeExpr
2132 var i1
= v
.expr
(self.n_expr
, null)
2133 var i2
= v
.expr
(self.n_expr2
, null)
2134 var mtype
= self.mtype
.as(MClassType)
2135 var res
= v
.init_instance
(mtype
)
2136 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2137 v
.check_init_instance
(res
, mtype
)
2142 redef class AOrangeExpr
2145 var i1
= v
.expr
(self.n_expr
, null)
2146 var i2
= v
.expr
(self.n_expr2
, null)
2147 var mtype
= self.mtype
.as(MClassType)
2148 var res
= v
.init_instance
(mtype
)
2149 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2150 v
.check_init_instance
(res
, mtype
)
2155 redef class ATrueExpr
2156 redef fun expr
(v
) do return v
.new_expr
("1", self.mtype
.as(not null))
2159 redef class AFalseExpr
2160 redef fun expr
(v
) do return v
.new_expr
("0", self.mtype
.as(not null))
2163 redef class ANullExpr
2164 redef fun expr
(v
) do return v
.new_expr
("NULL", self.mtype
.as(not null))
2167 redef class AIsaExpr
2170 var i
= v
.expr
(self.n_expr
, null)
2171 return v
.type_test
(i
, self.cast_type
.as(not null), "isa")
2175 redef class AAsCastExpr
2178 var i
= v
.expr
(self.n_expr
, null)
2179 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2181 var cond
= v
.type_test
(i
, self.mtype
.as(not null), "as")
2182 v
.add
("if (!{cond}) \{")
2183 v
.add_abort
("Cast failed")
2189 redef class AAsNotnullExpr
2192 var i
= v
.expr
(self.n_expr
, null)
2193 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2195 v
.add
("if ({i} == NULL) \{")
2196 v
.add_abort
("Cast failed")
2202 redef class AParExpr
2203 redef fun expr
(v
) do return v
.expr
(self.n_expr
, null)
2206 redef class AOnceExpr
2209 var mtype
= self.mtype
.as(not null)
2210 var name
= v
.get_name
("varonce")
2211 var guard
= v
.get_name
(name
+ "_guard")
2212 v
.add_decl
("static {mtype.ctype} {name};")
2213 v
.add_decl
("static int {guard};")
2214 var res
= v
.new_var
(mtype
)
2215 v
.add
("if ({guard}) \{")
2216 v
.add
("{res} = {name};")
2218 var i
= v
.expr
(self.n_expr
, mtype
)
2219 v
.add
("{res} = {i};")
2220 v
.add
("{name} = {res};")
2221 v
.add
("{guard} = 1;")
2227 redef class ASendExpr
2230 var recv
= v
.expr
(self.n_expr
, null)
2232 for a
in self.raw_arguments
.as(not null) do
2233 args
.add
(v
.expr
(a
, null))
2235 return v
.compile_callsite
(self.callsite
.as(not null), args
)
2239 redef class ASendReassignFormExpr
2242 var recv
= v
.expr
(self.n_expr
, null)
2244 for a
in self.raw_arguments
.as(not null) do
2245 args
.add
(v
.expr
(a
, null))
2247 var value
= v
.expr
(self.n_value
, null)
2249 var left
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2252 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
2256 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
2260 redef class ASuperExpr
2263 var recv
= v
.frame
.arguments
.first
2265 for a
in self.n_args
.n_exprs
do
2266 args
.add
(v
.expr
(a
, null))
2268 if args
.length
== 1 then
2269 args
= v
.frame
.arguments
2272 var mproperty
= self.mproperty
2273 if mproperty
!= null then
2274 if mproperty
.intro
.msignature
.arity
== 0 then
2278 var res
= v
.send
(mproperty
, args
)
2282 # stantard call-next-method
2283 return v
.supercall
(v
.frame
.mpropdef
.as(MMethodDef), recv
.mtype
.as(MClassType), args
)
2287 redef class ANewExpr
2290 var mtype
= self.mtype
.as(MClassType)
2292 var ctype
= mtype
.ctype
2293 if ctype
== "val*" then
2294 recv
= v
.init_instance
(mtype
)
2295 else if ctype
== "void*" then
2296 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2298 debug
("cannot new {mtype}")
2302 for a
in self.n_args
.n_exprs
do
2303 args
.add
(v
.expr
(a
, null))
2305 var res2
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2306 if res2
!= null then
2307 #self.debug("got {res2} from {mproperty}. drop {recv}")
2310 v
.check_init_instance
(recv
, mtype
)
2315 redef class AAttrExpr
2318 var recv
= v
.expr
(self.n_expr
, null)
2319 var mproperty
= self.mproperty
.as(not null)
2320 return v
.read_attribute
(mproperty
, recv
)
2324 redef class AAttrAssignExpr
2327 var recv
= v
.expr
(self.n_expr
, null)
2328 var i
= v
.expr
(self.n_value
, null)
2329 var mproperty
= self.mproperty
.as(not null)
2330 v
.write_attribute
(mproperty
, recv
, i
)
2334 redef class AAttrReassignExpr
2337 var recv
= v
.expr
(self.n_expr
, null)
2338 var value
= v
.expr
(self.n_value
, null)
2339 var mproperty
= self.mproperty
.as(not null)
2340 var attr
= v
.read_attribute
(mproperty
, recv
)
2341 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
2343 v
.write_attribute
(mproperty
, recv
, res
)
2347 redef class AIssetAttrExpr
2350 var recv
= v
.expr
(self.n_expr
, null)
2351 var mproperty
= self.mproperty
.as(not null)
2352 return v
.isset_attribute
(mproperty
, recv
)
2356 redef class ADebugTypeExpr
2365 redef class Array[E
]
2366 # Return a new Array with the elements only contened in 'self' and not in 'o'
2367 fun -(o
: Array[E
]): Array[E
] do
2368 var res
= new Array[E
]
2369 for e
in self do if not o
.has
(e
) then res
.add
(e
)
2376 # Return a linearization of a set of mtypes
2377 fun linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2378 var lin
= new Array[MType].from
(mtypes
)
2379 var sorter
= new TypeSorter(self)
2384 # Return a reverse linearization of a set of mtypes
2385 fun reverse_linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2386 var lin
= new Array[MType].from
(mtypes
)
2387 var sorter
= new ReverseTypeSorter(self)
2392 # Return super types of a `mtype` in `self`
2393 fun super_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2394 if not self.super_mtypes_cache
.has_key
(mtype
) then
2395 var supers
= new HashSet[MType]
2396 for otype
in mtypes
do
2397 if otype
== mtype
then continue
2398 if mtype
.is_subtype
(self, null, otype
) then
2402 self.super_mtypes_cache
[mtype
] = supers
2404 return self.super_mtypes_cache
[mtype
]
2407 private var super_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2409 # Return all sub mtypes (directs and indirects) of a `mtype` in `self`
2410 fun sub_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2411 if not self.sub_mtypes_cache
.has_key
(mtype
) then
2412 var subs
= new HashSet[MType]
2413 for otype
in mtypes
do
2414 if otype
== mtype
then continue
2415 if otype
.is_subtype
(self, null, mtype
) then
2419 self.sub_mtypes_cache
[mtype
] = subs
2421 return self.sub_mtypes_cache
[mtype
]
2424 private var sub_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2426 # Return a linearization of a set of mclasses
2427 fun linearize_mclasses_2
(mclasses
: Set[MClass]): Array[MClass] do
2428 var lin
= new Array[MClass].from
(mclasses
)
2429 var sorter
= new ClassSorter(self)
2434 # Return a reverse linearization of a set of mtypes
2435 fun reverse_linearize_mclasses
(mclasses
: Set[MClass]): Array[MClass] do
2436 var lin
= new Array[MClass].from
(mclasses
)
2437 var sorter
= new ReverseClassSorter(self)
2442 # Return all super mclasses (directs and indirects) of a `mclass` in `self`
2443 fun super_mclasses
(mclass
: MClass): Set[MClass] do
2444 if not self.super_mclasses_cache
.has_key
(mclass
) then
2445 var supers
= new HashSet[MClass]
2446 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2447 for sup
in self.flatten_mclass_hierarchy
[mclass
].greaters
do
2448 if sup
== mclass
then continue
2452 self.super_mclasses_cache
[mclass
] = supers
2454 return self.super_mclasses_cache
[mclass
]
2457 private var super_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2459 # Return all parents of a `mclass` in `self`
2460 fun parent_mclasses
(mclass
: MClass): Set[MClass] do
2461 if not self.parent_mclasses_cache
.has_key
(mclass
) then
2462 var parents
= new HashSet[MClass]
2463 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2464 for sup
in self.flatten_mclass_hierarchy
[mclass
].direct_greaters
do
2465 if sup
== mclass
then continue
2469 self.parent_mclasses_cache
[mclass
] = parents
2471 return self.parent_mclasses_cache
[mclass
]
2474 private var parent_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2476 # Return all sub mclasses (directs and indirects) of a `mclass` in `self`
2477 fun sub_mclasses
(mclass
: MClass): Set[MClass] do
2478 if not self.sub_mclasses_cache
.has_key
(mclass
) then
2479 var subs
= new HashSet[MClass]
2480 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2481 for sub
in self.flatten_mclass_hierarchy
[mclass
].smallers
do
2482 if sub
== mclass
then continue
2486 self.sub_mclasses_cache
[mclass
] = subs
2488 return self.sub_mclasses_cache
[mclass
]
2491 private var sub_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2493 # All 'mproperties' associated to all 'mclassdefs' of `mclass`
2494 fun properties
(mclass
: MClass): Set[MProperty] do
2495 if not self.properties_cache
.has_key
(mclass
) then
2496 var properties
= new HashSet[MProperty]
2497 var parents
= self.super_mclasses
(mclass
)
2498 for parent
in parents
do
2499 properties
.add_all
(self.properties
(parent
))
2502 for mclassdef
in mclass
.mclassdefs
do
2503 for mpropdef
in mclassdef
.mpropdefs
do
2504 properties
.add
(mpropdef
.mproperty
)
2507 self.properties_cache
[mclass
] = properties
2509 return properties_cache
[mclass
]
2512 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
2515 # A sorter for linearize list of types
2516 private class TypeSorter
2517 super AbstractSorter[MType]
2519 private var mmodule
: MModule
2521 init(mmodule
: MModule) do self.mmodule
= mmodule
2523 redef fun compare
(a
, b
) do
2526 else if a
.is_subtype
(self.mmodule
, null, b
) then
2533 # A sorter for reverse linearization
2534 private class ReverseTypeSorter
2537 init(mmodule
: MModule) do end
2539 redef fun compare
(a
, b
) do
2542 else if a
.is_subtype
(self.mmodule
, null, b
) then
2549 # A sorter for linearize list of classes
2550 private class ClassSorter
2551 super AbstractSorter[MClass]
2553 var mmodule
: MModule
2555 redef fun compare
(a
, b
) do
2558 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then
2565 # A sorter for reverse linearization
2566 private class ReverseClassSorter
2567 super AbstractSorter[MClass]
2569 var mmodule
: MModule
2571 redef fun compare
(a
, b
) do
2574 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then