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}"
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 currently compiled
247 # Is assigned during the separate compilation
248 var mainmodule
: MModule writable
250 # The real main module of the program
251 var realmainmodule
: MModule
253 # The modeulbuilder used to know the model and the AST
254 var modelbuilder
: ModelBuilder protected writable
256 # Is hardening asked? (see --hardening)
257 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
259 init(mainmodule
: MModule, modelbuilder
: ModelBuilder)
261 self.mainmodule
= mainmodule
262 self.realmainmodule
= mainmodule
263 self.modelbuilder
= modelbuilder
266 # Force the creation of a new file
267 # The point is to avoid contamination between must-be-compiled-separately files
268 fun new_file
(name
: String): CodeFile
270 var f
= new CodeFile(name
)
275 # The list of all associated files
276 # Used to generate .c files
277 var files
: List[CodeFile] = new List[CodeFile]
279 # Initialize a visitor specific for a compiler engine
280 fun new_visitor
: VISITOR is abstract
282 # Where global declaration are stored (the main .h)
283 var header
: CodeWriter writable
285 # Provide a declaration that can be requested (before or latter) by a visitor
286 fun provide_declaration
(key
: String, s
: String)
288 if self.provided_declarations
.has_key
(key
) then
289 assert self.provided_declarations
[key
] == s
291 self.provided_declarations
[key
] = s
294 private var provided_declarations
= new HashMap[String, String]
297 # This method call compile_header_strucs method that has to be refined
298 fun compile_header
do
300 self.header
.add_decl
("#include <stdlib.h>")
301 self.header
.add_decl
("#include <stdio.h>")
302 self.header
.add_decl
("#include <string.h>")
303 self.header
.add_decl
("#include <gc_chooser.h>")
305 compile_header_structs
307 # Global variable used by the legacy native interface
308 self.header
.add_decl
("extern int glob_argc;")
309 self.header
.add_decl
("extern char **glob_argv;")
310 self.header
.add_decl
("extern val *glob_sys;")
313 # Declaration of structures the live Nit types
314 protected fun compile_header_structs
is abstract
316 # Generate the main C function.
318 # * allocate the Sys object if it exists
319 # * call init if is exists
320 # * call main if it exists
321 fun compile_main_function
323 var v
= self.new_visitor
324 v
.add_decl
("int glob_argc;")
325 v
.add_decl
("char **glob_argv;")
326 v
.add_decl
("val *glob_sys;")
328 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
329 for tag
in count_type_test_tags
do
330 v
.add_decl
("long count_type_test_resolved_{tag};")
331 v
.add_decl
("long count_type_test_unresolved_{tag};")
332 v
.add_decl
("long count_type_test_skipped_{tag};")
333 v
.compiler
.header
.add_decl
("extern long count_type_test_resolved_{tag};")
334 v
.compiler
.header
.add_decl
("extern long count_type_test_unresolved_{tag};")
335 v
.compiler
.header
.add_decl
("extern long count_type_test_skipped_{tag};")
338 v
.add_decl
("int main(int argc, char** argv) \{")
339 v
.add
("glob_argc = argc; glob_argv = argv;")
340 v
.add
("initialize_gc_option();")
341 var main_type
= mainmodule
.sys_type
342 if main_type
!= null then
343 var mainmodule
= v
.compiler
.mainmodule
344 var glob_sys
= v
.init_instance
(main_type
)
345 v
.add
("glob_sys = {glob_sys};")
346 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
347 if main_init
!= null then
348 v
.send
(main_init
, [glob_sys
])
350 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
351 if main_method
!= null then
352 v
.send
(main_method
, [glob_sys
])
356 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
357 v
.add_decl
("long count_type_test_resolved_total = 0;")
358 v
.add_decl
("long count_type_test_unresolved_total = 0;")
359 v
.add_decl
("long count_type_test_skipped_total = 0;")
360 v
.add_decl
("long count_type_test_total_total = 0;")
361 for tag
in count_type_test_tags
do
362 v
.add_decl
("long count_type_test_total_{tag};")
363 v
.add
("count_type_test_total_{tag} = count_type_test_resolved_{tag} + count_type_test_unresolved_{tag} + count_type_test_skipped_{tag};")
364 v
.add
("count_type_test_resolved_total += count_type_test_resolved_{tag};")
365 v
.add
("count_type_test_unresolved_total += count_type_test_unresolved_{tag};")
366 v
.add
("count_type_test_skipped_total += count_type_test_skipped_{tag};")
367 v
.add
("count_type_test_total_total += count_type_test_total_{tag};")
369 v
.add
("printf(\"# dynamic count_type_test: total %l\\n\");")
370 v
.add
("printf(\"\\tresolved\\tunresolved\\tskipped\\ttotal\\n\
");")
371 var tags
= count_type_test_tags
.to_a
374 v
.add
("printf(\"{tag}\
");")
375 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_resolved_{tag}, 100.0*count_type_test_resolved_{tag}/count_type_test_total_total);")
376 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_unresolved_{tag}, 100.0*count_type_test_unresolved_{tag}/count_type_test_total_total);")
377 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\
", count_type_test_skipped_{tag}, 100.0*count_type_test_skipped_{tag}/count_type_test_total_total);")
378 v
.add
("printf(\"\\t
%ld
(%.2f
%%)\\n\
", count_type_test_total_{tag}, 100.0*count_type_test_total_{tag}/count_type_test_total_total);")
385 # List of additional .c files required to compile (native interface)
386 var extern_bodies
= new Array[ExternCFile]
388 # This is used to avoid adding an extern file more than once
389 private var seen_extern
= new ArraySet[String]
391 # Generate code that check if an instance is correctly initialized
392 fun generate_check_init_instance
(mtype
: MClassType) is abstract
394 # Generate code that initialize the attributes on a new instance
395 fun generate_init_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
397 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
398 self.mainmodule
.linearize_mclassdefs
(cds
)
400 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
401 for npropdef
in n
.n_propdefs
do
402 if npropdef
isa AAttrPropdef then
403 npropdef
.init_expr
(v
, recv
)
409 # Generate code that check if an attribute is correctly initialized
410 fun generate_check_attr
(v
: VISITOR, recv
: RuntimeVariable, mtype
: MClassType)
412 var cds
= mtype
.collect_mclassdefs
(self.mainmodule
).to_a
413 self.mainmodule
.linearize_mclassdefs
(cds
)
415 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
416 for npropdef
in n
.n_propdefs
do
417 if npropdef
isa AAttrPropdef then
418 npropdef
.check_expr
(v
, recv
)
426 var count_type_test_tags
: Array[String] = ["isa", "as", "auto", "covariance", "erasure"]
427 var count_type_test_resolved
: HashMap[String, Int] = init_count_type_test_tags
428 var count_type_test_unresolved
: HashMap[String, Int] = init_count_type_test_tags
429 var count_type_test_skipped
: HashMap[String, Int] = init_count_type_test_tags
431 protected fun init_count_type_test_tags
: HashMap[String, Int]
433 var res
= new HashMap[String, Int]
434 for tag
in count_type_test_tags
do
440 # Display stats about compilation process
442 # * type tests against resolved types (x isa Collection[Animal])
443 # * type tests against unresolved types (x isa Collection[E])
444 # * type tests skipped
449 if self.modelbuilder
.toolcontext
.opt_typing_test_metrics
.value
then
450 print
"# static count_type_test"
451 print
"\tresolved:\tunresolved\tskipped\ttotal"
452 var count_type_test_total
= init_count_type_test_tags
453 count_type_test_resolved
["total"] = 0
454 count_type_test_unresolved
["total"] = 0
455 count_type_test_skipped
["total"] = 0
456 count_type_test_total
["total"] = 0
457 for tag
in count_type_test_tags
do
458 count_type_test_total
[tag
] = count_type_test_resolved
[tag
] + count_type_test_unresolved
[tag
] + count_type_test_skipped
[tag
]
459 count_type_test_resolved
["total"] += count_type_test_resolved
[tag
]
460 count_type_test_unresolved
["total"] += count_type_test_unresolved
[tag
]
461 count_type_test_skipped
["total"] += count_type_test_skipped
[tag
]
462 count_type_test_total
["total"] += count_type_test_total
[tag
]
464 var count_type_test
= count_type_test_total
["total"]
465 var tags
= count_type_test_tags
.to_a
469 printn
"\t{count_type_test_resolved[tag]} ({div(count_type_test_resolved[tag],count_type_test)}%)"
470 printn
"\t{count_type_test_unresolved[tag]} ({div(count_type_test_unresolved[tag],count_type_test)}%)"
471 printn
"\t{count_type_test_skipped[tag]} ({div(count_type_test_skipped[tag],count_type_test)}%)"
472 printn
"\t{count_type_test_total[tag]} ({div(count_type_test_total[tag],count_type_test)}%)"
479 # Avoid division by zero by returning the string "n/a"
480 fun div
(a
,b
:Int):String
482 if b
== 0 then return "n/a"
483 return ((a
*10000/b
).to_f
/ 100.0).to_precision
(2)
487 # A file unit (may be more than one file if
488 # A file unit aim to be autonomous and is made or one or more `CodeWriter`s
491 var writers
= new Array[CodeWriter]
492 var required_declarations
= new HashSet[String]
495 # Where to store generated lines
498 var lines
: List[String] = new List[String]
499 var decl_lines
: List[String] = new List[String]
501 # Add a line in the main part of the generated C
502 fun add
(s
: String) do self.lines
.add
(s
)
505 # (used for local or global declaration)
506 fun add_decl
(s
: String) do self.decl_lines
.add
(s
)
511 file
.writers
.add
(self)
515 # A visitor on the AST of property definition that generate the C code.
516 abstract class AbstractCompilerVisitor
518 type COMPILER: AbstractCompiler
520 # The associated compiler
521 var compiler
: COMPILER
523 # The current visited AST node
524 var current_node
: nullable ANode writable = null
527 var frame
: nullable Frame writable
529 # Alias for self.compiler.mainmodule.object_type
530 fun object_type
: MClassType do return self.compiler
.mainmodule
.object_type
532 # Alias for self.compiler.mainmodule.bool_type
533 fun bool_type
: MClassType do return self.compiler
.mainmodule
.bool_type
535 var writer
: CodeWriter
537 init(compiler
: COMPILER)
539 self.compiler
= compiler
540 self.writer
= new CodeWriter(compiler
.files
.last
)
543 # Force to get the primitive class named `name' or abort
544 fun get_class
(name
: String): MClass do return self.compiler
.mainmodule
.get_primitive_class
(name
)
546 # Force to get the primitive property named `name' in the instance `recv' or abort
547 fun get_property
(name
: String, recv
: MType): MMethod
549 return self.compiler
.modelbuilder
.force_get_primitive_method
(self.current_node
.as(not null), name
, recv
, self.compiler
.mainmodule
)
552 fun compile_callsite
(callsite
: CallSite, args
: Array[RuntimeVariable]): nullable RuntimeVariable
554 return self.send
(callsite
.mproperty
, args
)
557 fun calloc_array
(ret_type
: MType, arguments
: Array[RuntimeVariable]) is abstract
559 fun native_array_def
(pname
: String, ret_type
: nullable MType, arguments
: Array[RuntimeVariable]) is abstract
561 # Transform varargs, in raw arguments, into a single argument of type Array
562 # Note: this method modify the given `args`
563 # If there is no vararg, then `args` is not modified.
564 fun varargize
(mpropdef
: MPropDef, msignature
: MSignature, args
: Array[RuntimeVariable])
566 var recv
= args
.first
567 var vararg_rank
= msignature
.vararg_rank
568 if vararg_rank
>= 0 then
569 assert args
.length
>= msignature
.arity
+ 1 # because of self
571 args
= new Array[RuntimeVariable]
573 args
.add
(rawargs
.first
) # recv
575 for i
in [0..vararg_rank
[ do
576 args
.add
(rawargs
[i
+1])
579 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-msignature
.arity
580 var vararg
= new Array[RuntimeVariable]
581 for i
in [vararg_rank
..vararg_lastrank
] do
582 vararg
.add
(rawargs
[i
+1])
585 var elttype
= msignature
.mparameters
[vararg_rank
].mtype
586 args
.add
(self.vararg_instance
(mpropdef
, recv
, vararg
, elttype
))
588 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
589 args
.add
(rawargs
[i
+1])
592 rawargs
.add_all
(args
)
598 # Anchor a type to the main module and the current receiver
599 fun anchor
(mtype
: MType): MType
601 if not mtype
.need_anchor
then return mtype
602 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
605 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
607 if not mtype
.need_anchor
then return mtype
608 return mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
611 # Unsafely cast a value to a new type
612 # ie the result share the same C variable but my have a different mcasttype
613 # NOTE: if the adaptation is useless then `value' is returned as it.
614 # ENSURE: return.name == value.name
615 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
617 mtype
= self.anchor
(mtype
)
618 var valmtype
= value
.mcasttype
619 if valmtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
623 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
624 var res
= new RuntimeVariable(value
.name
, valmtype
, valmtype
.mtype
)
627 var res
= new RuntimeVariable(value
.name
, valmtype
, mtype
)
632 # Generate a super call from a method definition
633 fun supercall
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
635 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable]) is abstract
637 # Box or unbox a value to another type iff a C type conversion is needed
638 # ENSURE: result.mtype.ctype == mtype.ctype
639 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable is abstract
641 # Generate a polymorphic subtype test
642 fun type_test
(value
: RuntimeVariable, mtype
: MType, tag
: String): RuntimeVariable is abstract
644 # Generate the code required to dynamically check if 2 objects share the same runtime type
645 fun is_same_type_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
647 # Generate a Nit "is" for two runtime_variables
648 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable is abstract
652 # Generate a static call on a method definition
653 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
655 # Generate a polymorphic send for the method `m' and the arguments `args'
656 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
658 # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
659 fun monomorphic_send
(m
: MMethod, t
: MType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
661 assert t
isa MClassType
662 var propdef
= m
.lookup_first_definition
(self.compiler
.mainmodule
, t
)
663 return self.call
(propdef
, t
, args
)
666 # Attributes handling
668 # Generate a polymorphic attribute is_set test
669 fun isset_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
671 # Generate a polymorphic attribute read
672 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable is abstract
674 # Generate a polymorphic attribute write
675 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable) is abstract
679 # Add a check and an abort for a null reciever if needed
680 fun check_recv_notnull
(recv
: RuntimeVariable)
682 if self.compiler
.modelbuilder
.toolcontext
.opt_no_check_other
.value
then return
684 var maybenull
= recv
.mcasttype
isa MNullableType or recv
.mcasttype
isa MNullType
686 self.add
("if ({recv} == NULL) \{")
687 self.add_abort
("Reciever is null")
692 # Generate a check-init-instance
693 fun check_init_instance
(recv
: RuntimeVariable, mtype
: MClassType) is abstract
697 private var names
: HashSet[String] = new HashSet[String]
698 private var last
: Int = 0
700 # Return a new name based on `s' and unique in the visitor
701 fun get_name
(s
: String): String
703 if not self.names
.has
(s
) then
707 var i
= self.last
+ 1
710 if not self.names
.has
(s2
) then
719 # Return an unique and stable identifier associated with an escapemark
720 fun escapemark_name
(e
: nullable EscapeMark): String
723 if escapemark_names
.has_key
(e
) then return escapemark_names
[e
]
725 if name
== null then name
= "label"
726 name
= get_name
(name
)
727 escapemark_names
[e
] = name
731 private var escapemark_names
= new HashMap[EscapeMark, String]
733 # Return a "const char*" variable associated to the classname of the dynamic type of an object
734 # NOTE: we do not return a RuntimeVariable "NativeString" as the class may not exist in the module/program
735 fun class_name_string
(value
: RuntimeVariable): String is abstract
739 protected var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
741 # Return the local runtime_variable associated to a Nit local variable
742 fun variable
(variable
: Variable): RuntimeVariable
744 if self.variables
.has_key
(variable
) then
745 return self.variables
[variable
]
747 var name
= self.get_name
("var_{variable.name}")
748 var mtype
= variable
.declared_type
.as(not null)
749 mtype
= self.anchor
(mtype
)
750 var res
= new RuntimeVariable(name
, mtype
, mtype
)
751 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
752 self.variables
[variable
] = res
757 # Return a new uninitialized local runtime_variable
758 fun new_var
(mtype
: MType): RuntimeVariable
760 mtype
= self.anchor
(mtype
)
761 var name
= self.get_name
("var")
762 var res
= new RuntimeVariable(name
, mtype
, mtype
)
763 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
767 # Return a new uninitialized named runtime_variable
768 fun new_named_var
(mtype
: MType, name
: String): RuntimeVariable
770 mtype
= self.anchor
(mtype
)
771 var res
= new RuntimeVariable(name
, mtype
, mtype
)
772 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
776 # Correctly assign a left and a right value
777 # Boxing and unboxing is performed if required
778 fun assign
(left
, right
: RuntimeVariable)
780 right
= self.autobox
(right
, left
.mtype
)
781 self.add
("{left} = {right};")
786 # Generate a alloc-instance + init-attributes
787 fun init_instance
(mtype
: MClassType): RuntimeVariable is abstract
789 # Generate an integer value
790 fun int_instance
(value
: Int): RuntimeVariable
792 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
793 self.add
("{res} = {value};")
797 # Generate a string value
798 fun string_instance
(string
: String): RuntimeVariable
800 var mtype
= self.get_class
("String").mclass_type
801 var name
= self.get_name
("varonce")
802 self.add_decl
("static {mtype.ctype} {name};")
803 var res
= self.new_var
(mtype
)
804 self.add
("if ({name}) \{")
805 self.add
("{res} = {name};")
806 self.add
("\} else \{")
807 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
808 self.add
("{nat} = \"{string.escape_to_c}\
";")
809 var res2
= self.init_instance
(mtype
)
810 self.add
("{res} = {res2};")
811 var length
= self.int_instance
(string
.length
)
812 self.send
(self.get_property
("with_native", mtype
), [res
, nat
, length
])
813 self.check_init_instance
(res
, mtype
)
814 self.add
("{name} = {res};")
819 # Generate an array value
820 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
822 # Get an instance of a array for a vararg
823 fun vararg_instance
(mpropdef
: MPropDef, recv
: RuntimeVariable, varargs
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable is abstract
827 # Add a line in the main part of the generated C
828 fun add
(s
: String) do self.writer
.lines
.add
(s
)
831 # (used for local or global declaration)
832 fun add_decl
(s
: String) do self.writer
.decl_lines
.add
(s
)
834 # Request the presence of a global declaration
835 fun require_declaration
(key
: String)
837 self.writer
.file
.required_declarations
.add
(key
)
840 # Add a declaration in the local-header
841 # The declaration is ensured to be present once
842 fun declare_once
(s
: String)
844 self.compiler
.provide_declaration
(s
, s
)
845 self.require_declaration
(s
)
848 # look for a needed .h and .c file for a given .nit source-file
849 # FIXME: bad API, parameter should be a MModule, not its source-file
850 fun add_extern
(file
: String)
852 file
= file
.strip_extension
(".nit")
853 var tryfile
= file
+ ".nit.h"
854 if tryfile
.file_exists
then
855 self.declare_once
("#include \"{"..".join_path(tryfile)}\
"")
857 tryfile
= file
+ "_nit.h"
858 if tryfile
.file_exists
then
859 self.declare_once
("#include \"{"..".join_path(tryfile)}\
"")
862 if self.compiler
.seen_extern
.has
(file
) then return
863 self.compiler
.seen_extern
.add
(file
)
864 tryfile
= file
+ ".nit.c"
865 if not tryfile
.file_exists
then
866 tryfile
= file
+ "_nit.c"
867 if not tryfile
.file_exists
then return
869 var f
= new ExternCFile(tryfile
, "")
870 self.compiler
.extern_bodies
.add
(f
)
873 # Return a new local runtime_variable initialized with the C expression `cexpr'.
874 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
876 var res
= new_var
(mtype
)
877 self.add
("{res} = {cexpr};")
881 # Generate generic abort
882 # used by aborts, asserts, casts, etc.
883 fun add_abort
(message
: String)
885 if self.current_node
!= null and self.current_node
.location
.file
!= null then
886 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});")
888 self.add
("fprintf(stderr, \"Runtime error
: %s\\n\
", \"{message.escape_to_c}\
");")
893 # Generate a return with the value `s'
894 fun ret
(s
: RuntimeVariable)
896 self.assign
(self.frame
.returnvar
.as(not null), s
)
897 self.add
("goto {self.frame.returnlabel.as(not null)};")
900 # Compile a statement (if any)
901 fun stmt
(nexpr
: nullable AExpr)
903 if nexpr
== null then return
904 var old
= self.current_node
905 self.current_node
= nexpr
907 self.current_node
= old
910 # Compile an expression an return its result
911 # `mtype` is the expected return type, pass null if no specific type is expected.
912 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
914 var old
= self.current_node
915 self.current_node
= nexpr
916 var res
= nexpr
.expr
(self).as(not null)
917 if mtype
!= null then
918 mtype
= self.anchor
(mtype
)
919 res
= self.autobox
(res
, mtype
)
921 res
= autoadapt
(res
, nexpr
.mtype
.as(not null))
922 var implicit_cast_to
= nexpr
.implicit_cast_to
923 if implicit_cast_to
!= null and not self.compiler
.modelbuilder
.toolcontext
.opt_no_check_autocast
.value
then
924 var castres
= self.type_test
(res
, implicit_cast_to
, "auto")
925 self.add
("if (!{castres}) \{")
926 self.add_abort
("Cast failed")
928 res
= autoadapt
(res
, implicit_cast_to
)
930 self.current_node
= old
934 # Alias for `self.expr(nexpr, self.bool_type)'
935 fun expr_bool
(nexpr
: AExpr): RuntimeVariable do return expr
(nexpr
, bool_type
)
937 # Safely show a debug message on the current node and repeat the message in the C code as a comment
938 fun debug
(message
: String)
940 var node
= self.current_node
946 self.add
("/* DEBUG: {message} */")
950 # A C function associated to a Nit method
951 # Because of customization, a given Nit method can be compiler more that once
952 abstract class AbstractRuntimeFunction
954 type COMPILER: AbstractCompiler
955 type VISITOR: AbstractCompilerVisitor
957 # The associated Nit method
958 var mmethoddef
: MMethodDef
960 # The mangled c name of the runtime_function
961 # Subclasses should redefine `build_c_name` instead
964 var res
= self.c_name_cache
965 if res
!= null then return res
966 res
= self.build_c_name
967 self.c_name_cache
= res
971 # Non cached version of `c_name`
972 protected fun build_c_name
: String is abstract
974 protected var c_name_cache
: nullable String writable = null
976 # Implements a call of the runtime_function
977 # May inline the body or generate a C function call
978 fun call
(v
: VISITOR, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
980 # Generate the code for the RuntimeFunction
981 # Warning: compile more than once compilation makes CC unhappy
982 fun compile_to_c
(compiler
: COMPILER) is abstract
985 # A runtime variable hold a runtime value in C.
986 # Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
988 # 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.
989 class RuntimeVariable
990 # The name of the variable in the C code
993 # The static type of the variable (as declard in C)
996 # The current casted type of the variable (as known in Nit)
997 var mcasttype
: MType writable
999 # If the variable exaclty a mcasttype?
1000 # false (usual value) means that the variable is a mcasttype or a subtype.
1001 var is_exact
: Bool writable = false
1003 init(name
: String, mtype
: MType, mcasttype
: MType)
1007 self.mcasttype
= mcasttype
1008 assert not mtype
.need_anchor
1009 assert not mcasttype
.need_anchor
1012 redef fun to_s
do return name
1017 if self.is_exact
then
1018 exact_str
= " exact"
1023 if self.mtype
== self.mcasttype
then
1024 type_str
= "{mtype}{exact_str}"
1026 type_str
= "{mtype}({mcasttype}{exact_str})"
1028 return "<{name}:{type_str}>"
1032 # A frame correspond to a visited property in a GlobalCompilerVisitor
1035 type VISITOR: AbstractCompilerVisitor
1037 # The associated visitor
1038 var visitor
: VISITOR
1040 # The executed property.
1041 # A Method in case of a call, an attribute in case of a default initialization.
1042 var mpropdef
: MPropDef
1044 # The static type of the receiver
1045 var receiver
: MClassType
1047 # Arguments of the method (the first is the receiver)
1048 var arguments
: Array[RuntimeVariable]
1050 # The runtime_variable associated to the return (in a function)
1051 var returnvar
: nullable RuntimeVariable writable = null
1053 # The label at the end of the property
1054 var returnlabel
: nullable String writable = null
1057 # An extern C file to compile
1059 # The filename of the file
1060 var filename
: String
1061 # Additionnal specific CC compiler -c flags
1066 # Mangle a string to be a unique valid C identifier
1067 fun to_cmangle
: String
1069 var res
= new Buffer
1070 var underscore
= false
1072 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
1078 res
.append
('_'.ascii
.to_s
)
1081 if c
>= '0' and c
<= '9' then
1084 else if c
== '_' then
1089 res
.append
(c
.ascii
.to_s
)
1097 # Escape " \ ' and non printable characters for literal C strings or characters
1098 fun escape_to_c
: String
1104 else if c
== '\0' then
1106 else if c
== '"' then
1108 else if c == '\'' then
1110 else if c == '\\
' then
1112 else if c.ascii < 32 then
1113 b.append("\\{c.ascii.to_base(8, false)}")
1123 # Return the C type associated to a given Nit static type
1124 fun ctype: String do return "val*"
1126 fun ctypename: String do return "val"
1128 # Return the name of the C structure associated to a Nit live type
1129 fun c_name: String is abstract
1130 protected var c_name_cache: nullable String protected writable
1133 redef class MClassType
1136 var res = self.c_name_cache
1137 if res != null then return res
1138 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
1139 self.c_name_cache = res
1143 redef fun ctype: String
1145 if mclass.name == "Int" then
1147 else if mclass.name == "Bool" then
1149 else if mclass.name == "Char" then
1151 else if mclass.name == "Float" then
1153 else if mclass.name == "NativeString" then
1155 else if mclass.name == "NativeArray" then
1157 else if mclass.kind == extern_kind then
1164 redef fun ctypename: String
1166 if mclass.name == "Int" then
1168 else if mclass.name == "Bool" then
1170 else if mclass.name == "Char" then
1172 else if mclass.name == "Float" then
1174 else if mclass.name == "NativeString" then
1176 else if mclass.name == "NativeArray" then
1177 #return "{self.arguments.first.ctype}*"
1179 else if mclass.kind == extern_kind then
1187 redef class MGenericType
1190 var res = self.c_name_cache
1191 if res != null then return res
1193 for t in self.arguments do
1194 res = res + t.c_name
1196 self.c_name_cache = res
1201 redef class MParameterType
1204 var res = self.c_name_cache
1205 if res != null then return res
1206 res = "{self.mclass.c_name}_FT{self.rank}"
1207 self.c_name_cache = res
1212 redef class MVirtualType
1215 var res = self.c_name_cache
1216 if res != null then return res
1217 res = "{self.mproperty.intro.mclassdef.mclass.c_name}_VT{self.mproperty.name}"
1218 self.c_name_cache = res
1223 redef class MNullableType
1226 var res = self.c_name_cache
1227 if res != null then return res
1228 res = "nullable_{self.mtype.c_name}"
1229 self.c_name_cache = res
1235 # Return the name of the C structure associated to a Nit class
1236 fun c_name: String do
1237 var res = self.c_name_cache
1238 if res != null then return res
1239 res = "{intro_mmodule.name.to_cmangle}__{name.to_cmangle}"
1240 self.c_name_cache = res
1243 private var c_name_cache: nullable String
1246 redef class MProperty
1247 fun c_name: String do
1248 var res = self.c_name_cache
1249 if res != null then return res
1250 res = "{self.intro.c_name}"
1251 self.c_name_cache = res
1254 private var c_name_cache: nullable String
1257 redef class MPropDef
1258 type VISITOR: AbstractCompilerVisitor
1260 private var c_name_cache: nullable String
1262 # The mangled name associated to the property
1265 var res = self.c_name_cache
1266 if res != null then return res
1267 res = "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1268 self.c_name_cache = res
1273 redef class MMethodDef
1274 # Can the body be inlined?
1275 fun can_inline(v: VISITOR): Bool
1277 var modelbuilder = v.compiler.modelbuilder
1278 if modelbuilder.mpropdef2npropdef.has_key(self) then
1279 var npropdef = modelbuilder.mpropdef2npropdef[self]
1280 return npropdef.can_inline
1281 else if self.mproperty.name == "init" then
1282 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1289 # Inline the body in another visitor
1290 fun compile_inside_to_c(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
1292 var modelbuilder = v.compiler.modelbuilder
1293 if modelbuilder.mpropdef2npropdef.has_key(self) then
1294 var npropdef = modelbuilder.mpropdef2npropdef[self]
1295 var oldnode = v.current_node
1296 v.current_node = npropdef
1297 self.compile_parameter_check(v, arguments)
1298 npropdef.compile_to_c(v, self, arguments)
1299 v.current_node = oldnode
1300 else if self.mproperty.name == "init" then
1301 var nclassdef = modelbuilder.mclassdef2nclassdef[self.mclassdef]
1302 var oldnode = v.current_node
1303 v.current_node = nclassdef
1304 self.compile_parameter_check(v, arguments)
1305 nclassdef.compile_to_c(v, self, arguments)
1306 v.current_node = oldnode
1313 # Generate type checks in the C code to check covariant parameters
1314 fun compile_parameter_check(v: VISITOR, arguments: Array[RuntimeVariable])
1316 if v.compiler.modelbuilder.toolcontext.opt_no_check_covariance.value then return
1318 for i in [0..msignature.arity[ do
1319 # skip test for vararg since the array is instantiated with the correct polymorphic type
1320 if msignature.vararg_rank == i then continue
1322 # skip if the cast is not required
1323 var origmtype = self.mproperty.intro.msignature.mparameters[i].mtype
1324 if not origmtype.need_anchor then continue
1326 # get the parameter type
1327 var mtype = self.msignature.mparameters[i].mtype
1330 # note that v decides if and how to implements the cast
1331 v.add("/* Covariant cast for argument {i} ({self.msignature.mparameters[i].name}) {arguments[i+1].inspect} isa {mtype} */")
1332 var cond = v.type_test(arguments[i+1], mtype, "covariance")
1333 v.add("if (!{cond}) \{")
1334 v.add_abort("Cast failed")
1342 redef class APropdef
1343 fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1345 v.add("printf(\"NOT YET IMPLEMENTED {class_name} {mpropdef} at {location.to_s}\\n\");")
1346 debug("Not yet implemented")
1349 fun can_inline: Bool do return true
1352 redef class AConcreteMethPropdef
1353 redef fun compile_to_c(v, mpropdef, arguments)
1355 for i in [0..mpropdef.msignature.arity[ do
1356 var variable = self.n_signature.n_params[i].variable.as(not null)
1357 v.assign(v.variable(variable), arguments[i+1])
1359 # Call the implicit super-init
1360 var auto_super_inits = self.auto_super_inits
1361 if auto_super_inits != null then
1362 var selfarg = [arguments.first]
1363 for auto_super_init in auto_super_inits do
1364 if auto_super_init.intro.msignature.arity == 0 then
1365 v.send(auto_super_init, selfarg)
1367 v.send(auto_super_init, arguments)
1371 v.stmt(self.n_block)
1374 redef fun can_inline
1376 if self.auto_super_inits != null then return false
1377 var nblock = self.n_block
1378 if nblock == null then return true
1379 if (mpropdef.mproperty.name == "==" or mpropdef.mproperty.name == "!=") and mpropdef.mclassdef.mclass.name == "Object" then return true
1380 if nblock isa ABlockExpr and nblock.n_expr.length == 0 then return true
1385 redef class AInternMethPropdef
1386 redef fun compile_to_c(v, mpropdef, arguments)
1388 var pname = mpropdef.mproperty.name
1389 var cname = mpropdef.mclassdef.mclass.name
1390 var ret = mpropdef.msignature.return_mtype
1392 ret = v.resolve_for(ret, arguments.first)
1394 if pname != "==" and pname != "!=" then
1395 v.adapt_signature(mpropdef, arguments)
1397 if cname == "Int" then
1398 if pname == "output" then
1399 v.add("printf(\"%ld\\n\", {arguments.first});")
1401 else if pname == "object_id" then
1402 v.ret(arguments.first)
1404 else if pname == "+" then
1405 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1407 else if pname == "-" then
1408 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1410 else if pname == "unary -" then
1411 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1413 else if pname == "succ" then
1414 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1416 else if pname == "prec" then
1417 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1419 else if pname == "*" then
1420 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1422 else if pname == "/" then
1423 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1425 else if pname == "%" then
1426 v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
1428 else if pname == "lshift" then
1429 v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
1431 else if pname == "rshift" then
1432 v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
1434 else if pname == "==" then
1435 v.ret(v.equal_test(arguments[0], arguments[1]))
1437 else if pname == "!=" then
1438 var res = v.equal_test(arguments[0], arguments[1])
1439 v.ret(v.new_expr("!{res}", ret.as(not null)))
1441 else if pname == "<" then
1442 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1444 else if pname == ">" then
1445 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1447 else if pname == "<=" then
1448 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1450 else if pname == ">=" then
1451 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1453 else if pname == "to_f" then
1454 v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
1456 else if pname == "ascii" then
1457 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1460 else if cname == "Char" then
1461 if pname == "output" then
1462 v.add("printf(\"%c\", {arguments.first});")
1464 else if pname == "object_id" then
1465 v.ret(arguments.first)
1467 else if pname == "+" then
1468 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1470 else if pname == "-" then
1471 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1473 else if pname == "==" then
1474 v.ret(v.equal_test(arguments[0], arguments[1]))
1476 else if pname == "!=" then
1477 var res = v.equal_test(arguments[0], arguments[1])
1478 v.ret(v.new_expr("!{res}", ret.as(not null)))
1480 else if pname == "succ" then
1481 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1483 else if pname == "prec" then
1484 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1486 else if pname == "<" then
1487 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1489 else if pname == ">" then
1490 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1492 else if pname == "<=" then
1493 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1495 else if pname == ">=" then
1496 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1498 else if pname == "to_i" then
1499 v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
1501 else if pname == "ascii" then
1502 v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
1505 else if cname == "Bool" then
1506 if pname == "output" then
1507 v.add("printf({arguments.first}?\"true\\n\":\"false\\n\");")
1509 else if pname == "object_id" then
1510 v.ret(arguments.first)
1512 else if pname == "==" then
1513 v.ret(v.equal_test(arguments[0], arguments[1]))
1515 else if pname == "!=" then
1516 var res = v.equal_test(arguments[0], arguments[1])
1517 v.ret(v.new_expr("!{res}", ret.as(not null)))
1520 else if cname == "Float" then
1521 if pname == "output" then
1522 v.add("printf(\"%f\\n\", {arguments.first});")
1524 else if pname == "object_id" then
1525 v.ret(v.new_expr("(double){arguments.first}", ret.as(not null)))
1527 else if pname == "+" then
1528 v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
1530 else if pname == "-" then
1531 v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
1533 else if pname == "unary -" then
1534 v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
1536 else if pname == "succ" then
1537 v.ret(v.new_expr("{arguments[0]}+1", ret.as(not null)))
1539 else if pname == "prec" then
1540 v.ret(v.new_expr("{arguments[0]}-1", ret.as(not null)))
1542 else if pname == "*" then
1543 v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
1545 else if pname == "/" then
1546 v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
1548 else if pname == "==" then
1549 v.ret(v.equal_test(arguments[0], arguments[1]))
1551 else if pname == "!=" then
1552 var res = v.equal_test(arguments[0], arguments[1])
1553 v.ret(v.new_expr("!{res}", ret.as(not null)))
1555 else if pname == "<" then
1556 v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
1558 else if pname == ">" then
1559 v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
1561 else if pname == "<=" then
1562 v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
1564 else if pname == ">=" then
1565 v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
1567 else if pname == "to_i" then
1568 v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
1571 else if cname == "Char" then
1572 if pname == "output" then
1573 v.add("printf(\"%c\", {arguments.first});")
1575 else if pname == "object_id" then
1576 v.ret(arguments.first)
1578 else if pname == "==" then
1579 v.ret(v.equal_test(arguments[0], arguments[1]))
1581 else if pname == "!=" then
1582 var res = v.equal_test(arguments[0], arguments[1])
1583 v.ret(v.new_expr("!{res}", ret.as(not null)))
1585 else if pname == "ascii" then
1586 v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
1589 else if cname == "NativeString" then
1590 if pname == "[]" then
1591 v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
1593 else if pname == "[]=" then
1594 v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1596 else if pname == "copy_to" then
1597 v.add("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
1599 else if pname == "atoi" then
1600 v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
1603 else if cname == "NativeArray" then
1604 v.native_array_def(pname, ret, arguments)
1607 if pname == "exit" then
1608 v.add("exit({arguments[1]});")
1610 else if pname == "sys" then
1611 v.ret(v.new_expr("glob_sys", ret.as(not null)))
1613 else if pname == "calloc_string" then
1614 v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
1616 else if pname == "calloc_array" then
1617 v.calloc_array(ret.as(not null), arguments)
1619 else if pname == "object_id" then
1620 v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
1622 else if pname == "is_same_type" then
1623 v.ret(v.is_same_type_test(arguments[0], arguments[1]))
1625 else if pname == "output_class_name" then
1626 var nat = v.class_name_string(arguments.first)
1627 v.add("printf(\"%s\\n\", {nat});")
1629 else if pname == "native_class_name" then
1630 var nat = v.class_name_string(arguments.first)
1631 v.ret(v.new_expr("(char*){nat}", ret.as(not null)))
1633 else if pname == "force_garbage_collection" then
1634 v.add("nit_gcollect();")
1637 v.add("printf(\"NOT YET IMPLEMENTED {class_name}:{mpropdef} at {location.to_s}\\n\");")
1638 debug("Not implemented {mpropdef}")
1642 redef class AExternMethPropdef
1643 redef fun compile_to_c(v, mpropdef, arguments)
1646 var nextern = self.n_extern
1647 if nextern == null then
1648 v.add("fprintf(stderr, \"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
1652 externname = nextern.text.substring(1, nextern.text.length-2)
1653 if location.file != null then
1654 var file = location.file.filename
1657 var res: nullable RuntimeVariable = null
1658 var ret = mpropdef.msignature.return_mtype
1660 ret = v.resolve_for(ret, arguments.first)
1661 res = v.new_var(ret)
1663 v.adapt_signature(mpropdef, arguments)
1666 v.add("{externname}({arguments.join(", ")});")
1668 v.add("{res} = {externname}({arguments.join(", ")});")
1674 redef class AExternInitPropdef
1675 redef fun compile_to_c(v, mpropdef, arguments)
1678 var nextern = self.n_extern
1679 if nextern == null then
1680 v.add("printf(\"NOT YET IMPLEMENTED nitni for {mpropdef} at {location.to_s}\\n\");")
1684 externname = nextern.text.substring(1, nextern.text.length-2)
1685 if location.file != null then
1686 var file = location.file.filename
1689 v.adapt_signature(mpropdef, arguments)
1690 var ret = arguments.first.mtype
1691 var res = v.new_var(ret)
1695 v.add("{res} = {externname}({arguments.join(", ")});")
1700 redef class AAttrPropdef
1701 redef fun compile_to_c(v, mpropdef, arguments)
1703 if arguments.length == 1 then
1704 var res = v.read_attribute(self.mpropdef.mproperty, arguments.first)
1705 v.assign(v.frame.returnvar.as(not null), res)
1707 v.write_attribute(self.mpropdef.mproperty, arguments.first, arguments[1])
1711 fun init_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1713 var nexpr = self.n_expr
1714 if nexpr != null then
1715 var oldnode = v.current_node
1716 v.current_node = self
1717 var old_frame = v.frame
1718 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1720 var value = v.expr(nexpr, self.mpropdef.static_mtype)
1721 v.write_attribute(self.mpropdef.mproperty, recv, value)
1723 v.current_node = oldnode
1727 fun check_expr(v: AbstractCompilerVisitor, recv: RuntimeVariable)
1729 var nexpr = self.n_expr
1730 if nexpr != null then return
1732 var oldnode = v.current_node
1733 v.current_node = self
1734 var old_frame = v.frame
1735 var frame = new Frame(v, self.mpropdef.as(not null), recv.mtype.as(MClassType), [recv])
1737 # Force read to check the initialization
1738 v.read_attribute(self.mpropdef.mproperty, recv)
1740 v.current_node = oldnode
1744 redef class AClassdef
1745 private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
1747 if mpropdef == self.mfree_init then
1748 var super_inits = self.super_inits
1749 if super_inits != null then
1750 assert arguments.length == 1
1751 for su in super_inits do
1752 v.send(su, arguments)
1756 var recv = arguments.first
1758 # Collect undefined attributes
1759 for npropdef in self.n_propdefs do
1760 if npropdef isa AAttrPropdef and npropdef.n_expr == null then
1761 v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
1771 redef class ADeferredMethPropdef
1772 redef fun compile_to_c(v, mpropdef, arguments) do v.add_abort("Deferred method called")
1773 redef fun can_inline do return true
1777 # Try to compile self as an expression
1778 # Do not call this method directly, use `v.expr' instead
1779 private fun expr
(v
: AbstractCompilerVisitor): nullable RuntimeVariable
1781 v
.add
("printf(\"NOT YET IMPLEMENTED {class_name}:{location.to_s}\\n\
");")
1782 var mtype
= self.mtype
1783 if mtype
== null then
1786 var res
= v
.new_var
(mtype
)
1787 v
.add
("/* {res} = NOT YET {class_name} */")
1792 # Try to compile self as a statement
1793 # Do not call this method directly, use `v.stmt' instead
1794 private fun stmt
(v
: AbstractCompilerVisitor)
1797 if res
!= null then v
.add
("{res};")
1801 redef class ABlockExpr
1804 for e
in self.n_expr
do v
.stmt
(e
)
1808 redef class AVardeclExpr
1811 var variable
= self.variable
.as(not null)
1812 var ne
= self.n_expr
1814 var i
= v
.expr
(ne
, variable
.declared_type
)
1815 v
.assign
(v
.variable
(variable
), i
)
1820 redef class AVarExpr
1823 var res
= v
.variable
(self.variable
.as(not null))
1824 var mtype
= self.mtype
.as(not null)
1825 return v
.autoadapt
(res
, mtype
)
1829 redef class AVarAssignExpr
1832 var variable
= self.variable
.as(not null)
1833 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
1834 v
.assign
(v
.variable
(variable
), i
)
1838 redef class AVarReassignExpr
1841 var variable
= self.variable
.as(not null)
1842 var vari
= v
.variable
(variable
)
1843 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
1844 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [vari
, value
])
1846 v
.assign
(v
.variable
(variable
), res
)
1850 redef class ASelfExpr
1851 redef fun expr
(v
) do return v
.frame
.arguments
.first
1854 redef class AContinueExpr
1855 redef fun stmt
(v
) do v
.add
("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
1858 redef class ABreakExpr
1859 redef fun stmt
(v
) do v
.add
("goto BREAK_{v.escapemark_name(self.escapemark)};")
1862 redef class AReturnExpr
1865 var nexpr
= self.n_expr
1866 if nexpr
!= null then
1867 var returnvar
= v
.frame
.returnvar
.as(not null)
1868 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
1869 v
.assign
(returnvar
, i
)
1871 v
.add
("goto {v.frame.returnlabel.as(not null)};")
1875 redef class AAbortExpr
1876 redef fun stmt
(v
) do v
.add_abort
("Aborted")
1882 var cond
= v
.expr_bool
(self.n_expr
)
1883 v
.add
("if ({cond})\{")
1891 redef class AIfexprExpr
1894 var res
= v
.new_var
(self.mtype
.as(not null))
1895 var cond
= v
.expr_bool
(self.n_expr
)
1896 v
.add
("if ({cond})\{")
1897 v
.assign
(res
, v
.expr
(self.n_then
, null))
1899 v
.assign
(res
, v
.expr
(self.n_else
, null))
1908 v
.stmt
(self.n_block
)
1909 var escapemark
= self.escapemark
1910 if escapemark
!= null then
1911 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1916 redef class AWhileExpr
1920 var cond
= v
.expr_bool
(self.n_expr
)
1921 v
.add
("if (!{cond}) break;")
1922 v
.stmt
(self.n_block
)
1923 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1925 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1929 redef class ALoopExpr
1933 v
.stmt
(self.n_block
)
1934 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1936 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1940 redef class AForExpr
1943 # Shortcut on explicit range
1944 # Avoid the instantiation of the range and the iterator
1945 var nexpr
= self.n_expr
1946 if self.variables
.length
== 1 and nexpr
isa AOrangeExpr and not v
.compiler
.modelbuilder
.toolcontext
.opt_no_shortcut_range
.value
then
1947 var from
= v
.expr
(nexpr
.n_expr
, null)
1948 var to
= v
.expr
(nexpr
.n_expr2
, null)
1949 var variable
= v
.variable
(variables
.first
)
1951 v
.assign
(variable
, from
)
1952 v
.add
("for(;;) \{ /* shortcut range */")
1954 var ok
= v
.send
(v
.get_property
("<", variable
.mtype
), [variable
, to
])
1956 v
.add
("if(!{ok}) break;")
1958 v
.stmt
(self.n_block
)
1960 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
1961 var succ
= v
.send
(v
.get_property
("succ", variable
.mtype
), [variable
])
1963 v
.assign
(variable
, succ
)
1965 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
1969 var cl
= v
.expr
(self.n_expr
, null)
1970 var it_meth
= self.method_iterator
1971 assert it_meth
!= null
1972 var it
= v
.send
(it_meth
, [cl
])
1975 var isok_meth
= self.method_is_ok
1976 assert isok_meth
!= null
1977 var ok
= v
.send
(isok_meth
, [it
])
1979 v
.add
("if(!{ok}) break;")
1980 if self.variables
.length
== 1 then
1981 var item_meth
= self.method_item
1982 assert item_meth
!= null
1983 var i
= v
.send
(item_meth
, [it
])
1985 v
.assign
(v
.variable
(variables
.first
), i
)
1986 else if self.variables
.length
== 2 then
1987 var key_meth
= self.method_key
1988 assert key_meth
!= null
1989 var i
= v
.send
(key_meth
, [it
])
1991 v
.assign
(v
.variable
(variables
[0]), i
)
1992 var item_meth
= self.method_item
1993 assert item_meth
!= null
1994 i
= v
.send
(item_meth
, [it
])
1996 v
.assign
(v
.variable
(variables
[1]), i
)
2000 v
.stmt
(self.n_block
)
2001 v
.add
("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
2002 var next_meth
= self.method_next
2003 assert next_meth
!= null
2004 v
.send
(next_meth
, [it
])
2006 v
.add
("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
2010 redef class AAssertExpr
2013 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return
2015 var cond
= v
.expr_bool
(self.n_expr
)
2016 v
.add
("if (!{cond}) \{")
2020 v
.add_abort
("Assert '{nid.text}' failed")
2022 v
.add_abort
("Assert failed")
2031 var res
= v
.new_var
(self.mtype
.as(not null))
2032 var i1
= v
.expr_bool
(self.n_expr
)
2033 v
.add
("if ({i1}) \{")
2036 var i2
= v
.expr_bool
(self.n_expr2
)
2037 v
.add
("{res} = {i2};")
2043 redef class AAndExpr
2046 var res
= v
.new_var
(self.mtype
.as(not null))
2047 var i1
= v
.expr_bool
(self.n_expr
)
2048 v
.add
("if (!{i1}) \{")
2051 var i2
= v
.expr_bool
(self.n_expr2
)
2052 v
.add
("{res} = {i2};")
2058 redef class ANotExpr
2061 var cond
= v
.expr_bool
(self.n_expr
)
2062 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2066 redef class AOrElseExpr
2069 var res
= v
.new_var
(self.mtype
.as(not null))
2070 var i1
= v
.expr
(self.n_expr
, null)
2071 v
.add
("if ({i1}!=NULL) \{")
2074 var i2
= v
.expr
(self.n_expr2
, null)
2084 var value1
= v
.expr
(self.n_expr
, null)
2085 var value2
= v
.expr
(self.n_expr2
, null)
2086 return v
.equal_test
(value1
, value2
)
2090 redef class AIntExpr
2091 redef fun expr
(v
) do return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2094 redef class AFloatExpr
2095 redef fun expr
(v
) do return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2098 redef class ACharExpr
2099 redef fun expr
(v
) do return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2102 redef class AArrayExpr
2105 var mtype
= self.mtype
.as(MClassType).arguments
.first
2106 var array
= new Array[RuntimeVariable]
2107 for nexpr
in self.n_exprs
.n_exprs
do
2108 var i
= v
.expr
(nexpr
, mtype
)
2111 return v
.array_instance
(array
, mtype
)
2115 redef class AStringFormExpr
2116 redef fun expr
(v
) do return v
.string_instance
(self.value
.as(not null))
2119 redef class ASuperstringExpr
2122 var array
= new Array[RuntimeVariable]
2123 for ne
in self.n_exprs
do
2124 if ne
isa AStringFormExpr and ne
.value
== "" then continue # skip empty sub-strings
2125 var i
= v
.expr
(ne
, null)
2128 var a
= v
.array_instance
(array
, v
.object_type
)
2129 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2134 redef class ACrangeExpr
2137 var i1
= v
.expr
(self.n_expr
, null)
2138 var i2
= v
.expr
(self.n_expr2
, null)
2139 var mtype
= self.mtype
.as(MClassType)
2140 var res
= v
.init_instance
(mtype
)
2141 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2142 v
.check_init_instance
(res
, mtype
)
2147 redef class AOrangeExpr
2150 var i1
= v
.expr
(self.n_expr
, null)
2151 var i2
= v
.expr
(self.n_expr2
, null)
2152 var mtype
= self.mtype
.as(MClassType)
2153 var res
= v
.init_instance
(mtype
)
2154 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2155 v
.check_init_instance
(res
, mtype
)
2160 redef class ATrueExpr
2161 redef fun expr
(v
) do return v
.new_expr
("1", self.mtype
.as(not null))
2164 redef class AFalseExpr
2165 redef fun expr
(v
) do return v
.new_expr
("0", self.mtype
.as(not null))
2168 redef class ANullExpr
2169 redef fun expr
(v
) do return v
.new_expr
("NULL", self.mtype
.as(not null))
2172 redef class AIsaExpr
2175 var i
= v
.expr
(self.n_expr
, null)
2176 return v
.type_test
(i
, self.cast_type
.as(not null), "isa")
2180 redef class AAsCastExpr
2183 var i
= v
.expr
(self.n_expr
, null)
2184 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2186 var cond
= v
.type_test
(i
, self.mtype
.as(not null), "as")
2187 v
.add
("if (!{cond}) \{")
2188 v
.add_abort
("Cast failed")
2194 redef class AAsNotnullExpr
2197 var i
= v
.expr
(self.n_expr
, null)
2198 if v
.compiler
.modelbuilder
.toolcontext
.opt_no_check_assert
.value
then return i
2200 v
.add
("if ({i} == NULL) \{")
2201 v
.add_abort
("Cast failed")
2207 redef class AParExpr
2208 redef fun expr
(v
) do return v
.expr
(self.n_expr
, null)
2211 redef class AOnceExpr
2214 var mtype
= self.mtype
.as(not null)
2215 var name
= v
.get_name
("varonce")
2216 var guard
= v
.get_name
(name
+ "_guard")
2217 v
.add_decl
("static {mtype.ctype} {name};")
2218 v
.add_decl
("static int {guard};")
2219 var res
= v
.new_var
(mtype
)
2220 v
.add
("if ({guard}) \{")
2221 v
.add
("{res} = {name};")
2223 var i
= v
.expr
(self.n_expr
, mtype
)
2224 v
.add
("{res} = {i};")
2225 v
.add
("{name} = {res};")
2226 v
.add
("{guard} = 1;")
2232 redef class ASendExpr
2235 var recv
= v
.expr
(self.n_expr
, null)
2237 for a
in self.raw_arguments
.as(not null) do
2238 args
.add
(v
.expr
(a
, null))
2240 return v
.compile_callsite
(self.callsite
.as(not null), args
)
2244 redef class ASendReassignFormExpr
2247 var recv
= v
.expr
(self.n_expr
, null)
2249 for a
in self.raw_arguments
.as(not null) do
2250 args
.add
(v
.expr
(a
, null))
2252 var value
= v
.expr
(self.n_value
, null)
2254 var left
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2257 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [left
, value
])
2261 v
.compile_callsite
(self.write_callsite
.as(not null), args
)
2265 redef class ASuperExpr
2268 var recv
= v
.frame
.arguments
.first
2270 for a
in self.n_args
.n_exprs
do
2271 args
.add
(v
.expr
(a
, null))
2273 if args
.length
== 1 then
2274 args
= v
.frame
.arguments
2277 var mproperty
= self.mproperty
2278 if mproperty
!= null then
2279 if mproperty
.intro
.msignature
.arity
== 0 then
2283 var res
= v
.send
(mproperty
, args
)
2287 # stantard call-next-method
2288 return v
.supercall
(v
.frame
.mpropdef
.as(MMethodDef), recv
.mtype
.as(MClassType), args
)
2292 redef class ANewExpr
2295 var mtype
= self.mtype
.as(MClassType)
2297 var ctype
= mtype
.ctype
2298 if ctype
== "val*" then
2299 recv
= v
.init_instance
(mtype
)
2300 else if ctype
== "void*" then
2301 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2303 debug
("cannot new {mtype}")
2307 for a
in self.n_args
.n_exprs
do
2308 args
.add
(v
.expr
(a
, null))
2310 var res2
= v
.compile_callsite
(self.callsite
.as(not null), args
)
2311 if res2
!= null then
2312 #self.debug("got {res2} from {mproperty}. drop {recv}")
2315 v
.check_init_instance
(recv
, mtype
)
2320 redef class AAttrExpr
2323 var recv
= v
.expr
(self.n_expr
, null)
2324 var mproperty
= self.mproperty
.as(not null)
2325 return v
.read_attribute
(mproperty
, recv
)
2329 redef class AAttrAssignExpr
2332 var recv
= v
.expr
(self.n_expr
, null)
2333 var i
= v
.expr
(self.n_value
, null)
2334 var mproperty
= self.mproperty
.as(not null)
2335 v
.write_attribute
(mproperty
, recv
, i
)
2339 redef class AAttrReassignExpr
2342 var recv
= v
.expr
(self.n_expr
, null)
2343 var value
= v
.expr
(self.n_value
, null)
2344 var mproperty
= self.mproperty
.as(not null)
2345 var attr
= v
.read_attribute
(mproperty
, recv
)
2346 var res
= v
.compile_callsite
(self.reassign_callsite
.as(not null), [attr
, value
])
2348 v
.write_attribute
(mproperty
, recv
, res
)
2352 redef class AIssetAttrExpr
2355 var recv
= v
.expr
(self.n_expr
, null)
2356 var mproperty
= self.mproperty
.as(not null)
2357 return v
.isset_attribute
(mproperty
, recv
)
2361 redef class ADebugTypeExpr
2370 redef class Array[E
]
2371 # Return a new Array with the elements only contened in 'self' and not in 'o'
2372 fun -(o
: Array[E
]): Array[E
] do
2373 var res
= new Array[E
]
2374 for e
in self do if not o
.has
(e
) then res
.add
(e
)
2381 # Return a linearization of a set of mtypes
2382 fun linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2383 var lin
= new Array[MType].from
(mtypes
)
2384 var sorter
= new TypeSorter(self)
2389 # Return a reverse linearization of a set of mtypes
2390 fun reverse_linearize_mtypes
(mtypes
: Set[MType]): Array[MType] do
2391 var lin
= new Array[MType].from
(mtypes
)
2392 var sorter
= new ReverseTypeSorter(self)
2397 # Return super types of a `mtype` in `self`
2398 fun super_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2399 if not self.super_mtypes_cache
.has_key
(mtype
) then
2400 var supers
= new HashSet[MType]
2401 for otype
in mtypes
do
2402 if otype
== mtype
then continue
2403 if mtype
.is_subtype
(self, null, otype
) then
2407 self.super_mtypes_cache
[mtype
] = supers
2409 return self.super_mtypes_cache
[mtype
]
2412 private var super_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2414 # Return all sub mtypes (directs and indirects) of a `mtype` in `self`
2415 fun sub_mtypes
(mtype
: MType, mtypes
: Set[MType]): Set[MType] do
2416 if not self.sub_mtypes_cache
.has_key
(mtype
) then
2417 var subs
= new HashSet[MType]
2418 for otype
in mtypes
do
2419 if otype
== mtype
then continue
2420 if otype
.is_subtype
(self, null, mtype
) then
2424 self.sub_mtypes_cache
[mtype
] = subs
2426 return self.sub_mtypes_cache
[mtype
]
2429 private var sub_mtypes_cache
: Map[MType, Set[MType]] = new HashMap[MType, Set[MType]]
2431 # Return a linearization of a set of mclasses
2432 fun linearize_mclasses_2
(mclasses
: Set[MClass]): Array[MClass] do
2433 var lin
= new Array[MClass].from
(mclasses
)
2434 var sorter
= new ClassSorter(self)
2439 # Return a reverse linearization of a set of mtypes
2440 fun reverse_linearize_mclasses
(mclasses
: Set[MClass]): Array[MClass] do
2441 var lin
= new Array[MClass].from
(mclasses
)
2442 var sorter
= new ReverseClassSorter(self)
2447 # Return all super mclasses (directs and indirects) of a `mclass` in `self`
2448 fun super_mclasses
(mclass
: MClass): Set[MClass] do
2449 if not self.super_mclasses_cache
.has_key
(mclass
) then
2450 var supers
= new HashSet[MClass]
2451 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2452 for sup
in self.flatten_mclass_hierarchy
[mclass
].greaters
do
2453 if sup
== mclass
then continue
2457 self.super_mclasses_cache
[mclass
] = supers
2459 return self.super_mclasses_cache
[mclass
]
2462 private var super_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2464 # Return all parents of a `mclass` in `self`
2465 fun parent_mclasses
(mclass
: MClass): Set[MClass] do
2466 if not self.parent_mclasses_cache
.has_key
(mclass
) then
2467 var parents
= new HashSet[MClass]
2468 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2469 for sup
in self.flatten_mclass_hierarchy
[mclass
].direct_greaters
do
2470 if sup
== mclass
then continue
2474 self.parent_mclasses_cache
[mclass
] = parents
2476 return self.parent_mclasses_cache
[mclass
]
2479 private var parent_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2481 # Return all sub mclasses (directs and indirects) of a `mclass` in `self`
2482 fun sub_mclasses
(mclass
: MClass): Set[MClass] do
2483 if not self.sub_mclasses_cache
.has_key
(mclass
) then
2484 var subs
= new HashSet[MClass]
2485 if self.flatten_mclass_hierarchy
.has
(mclass
) then
2486 for sub
in self.flatten_mclass_hierarchy
[mclass
].smallers
do
2487 if sub
== mclass
then continue
2491 self.sub_mclasses_cache
[mclass
] = subs
2493 return self.sub_mclasses_cache
[mclass
]
2496 private var sub_mclasses_cache
: Map[MClass, Set[MClass]] = new HashMap[MClass, Set[MClass]]
2498 # All 'mproperties' associated to all 'mclassdefs' of `mclass`
2499 fun properties
(mclass
: MClass): Set[MProperty] do
2500 if not self.properties_cache
.has_key
(mclass
) then
2501 var properties
= new HashSet[MProperty]
2502 var parents
= self.super_mclasses
(mclass
)
2503 for parent
in parents
do
2504 properties
.add_all
(self.properties
(parent
))
2507 for mclassdef
in mclass
.mclassdefs
do
2508 for mpropdef
in mclassdef
.mpropdefs
do
2509 properties
.add
(mpropdef
.mproperty
)
2512 self.properties_cache
[mclass
] = properties
2514 return properties_cache
[mclass
]
2517 private var properties_cache
: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]]
2520 # A sorter for linearize list of types
2521 private class TypeSorter
2522 super AbstractSorter[MType]
2524 private var mmodule
: MModule
2526 init(mmodule
: MModule) do self.mmodule
= mmodule
2528 redef fun compare
(a
, b
) do
2531 else if a
.is_subtype
(self.mmodule
, null, b
) then
2538 # A sorter for reverse linearization
2539 private class ReverseTypeSorter
2542 init(mmodule
: MModule) do end
2544 redef fun compare
(a
, b
) do
2547 else if a
.is_subtype
(self.mmodule
, null, b
) then
2554 # A sorter for linearize list of classes
2555 private class ClassSorter
2556 super AbstractSorter[MClass]
2558 var mmodule
: MModule
2560 redef fun compare
(a
, b
) do
2563 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then
2570 # A sorter for reverse linearization
2571 private class ReverseClassSorter
2572 super AbstractSorter[MClass]
2574 var mmodule
: MModule
2576 redef fun compare
(a
, b
) do
2579 else if self.mmodule
.flatten_mclass_hierarchy
.has
(a
) and self.mmodule
.flatten_mclass_hierarchy
[a
].greaters
.has
(b
) then