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.
17 # Global compilation of a Nit program
19 # Techniques used are:
20 # * heterogeneous generics
24 module global_compiler
28 import auto_super_init
29 import rapid_type_analysis
31 redef class ToolContext
33 var opt_output
: OptionString = new OptionString("Output file", "-o", "--output")
36 var opt_no_cc
: OptionBool = new OptionBool("Do not invoke C compiler", "--no-cc")
39 var opt_hardening
: OptionBool = new OptionBool("Generate contracts in the C code against bugs in the compiler", "--hardening")
44 self.option_context
.add_option
(self.opt_output
, self.opt_no_cc
, self.opt_hardening
)
48 redef class ModelBuilder
49 fun run_global_compiler
(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis)
52 self.toolcontext
.info
("*** COMPILING TO C ***", 1)
54 var compiler
= new GlobalCompiler(mainmodule
, runtime_type_analysis
, self)
55 var v
= new GlobalCompilerVisitor(compiler
)
57 v
.add_decl
("#include <stdlib.h>")
58 v
.add_decl
("#include <stdio.h>")
59 v
.add_decl
("#include <string.h>")
61 # TODO: Better way to activate the GC
62 #v.add_decl("#include <gc/gc.h>")
63 v
.add_decl
("#define GC_MALLOC(x) calloc(1, (x))")
65 # Declare structure for each live type
67 v
.add_decl
("typedef struct \{int classid;\} val;")
68 for t
in runtime_type_analysis
.live_types
do
69 compiler
.declare_runtimeclass
(v
, t
)
72 v
.add_decl
("extern int glob_argc;")
73 v
.add_decl
("extern char **glob_argv;")
74 v
.add_decl
("extern val *glob_sys;")
76 # Class names (for the class_name and output_class_name methods)
78 v
.add_decl
("extern const char const * class_names[];")
79 v
.add
("const char const * class_names[] = \{")
80 for t
in runtime_type_analysis
.live_types
do
81 v
.add
("\"{t}\
", /* {compiler.classid(t)} */")
86 # Init instance code (allocate and init-arguments)
88 for t
in runtime_type_analysis
.live_types
do
89 if t
.ctype
!= "val*" then continue
90 compiler
.generate_init_instance
(t
)
93 # The main function of the C
95 v
= new GlobalCompilerVisitor(compiler
)
96 v
.add_decl
("int glob_argc;")
97 v
.add_decl
("char **glob_argv;")
98 v
.add_decl
("val *glob_sys;")
99 v
.add_decl
("int main(int argc, char** argv) \{")
100 v
.add
("glob_argc = argc; glob_argv = argv;")
101 var main_type
= mainmodule
.sys_type
102 if main_type
== null then return # Nothing to compile
103 var glob_sys
= v
.init_instance
(main_type
)
104 v
.add
("glob_sys = {glob_sys};")
105 var main_init
= mainmodule
.try_get_primitive_method
("init", main_type
)
106 if main_init
!= null then
107 v
.send
(main_init
, [glob_sys
])
109 var main_method
= mainmodule
.try_get_primitive_method
("main", main_type
)
110 if main_method
!= null then
111 v
.send
(main_method
, [glob_sys
])
115 # Compile until all runtime_functions are visited
117 while not compiler
.todos
.is_empty
do
118 var m
= compiler
.todos
.shift
119 self.toolcontext
.info
("Compile {m.mmethoddef} for {m.recv} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
120 m
.mmethoddef
.compile_to_c
(compiler
, self, m
.recv
)
122 self.toolcontext
.info
("Total methods to compile to C: {compiler.visitors.length}", 2)
124 # Generate the .h and .c files
126 var outname
= self.toolcontext
.opt_output
.value
127 if outname
== null then
128 outname
= "{mainmodule.name}.bin"
131 var hfilename
= ".nit_compile/{mainmodule.name}.1.h"
132 var h
= new OFStream.open
(hfilename
)
133 for l
in compiler
.header
.decl_lines
do
139 var cfiles
= new Array[String]
141 var file
: nullable OFStream = null
146 for vis
in compiler
.visitors
do
147 count
+= vis
.lines
.length
148 if file
== null or count
> 10000 then
150 if file
!= null then file
.close
151 var cfilename
= ".nit_compile/{mainmodule.name}.{i}.c"
152 cfiles
.add
(cfilename
)
153 file
= new OFStream.open
(cfilename
)
154 file
.write
"#include \"{mainmodule.name}.1.h\
"\n"
155 count
= vis
.lines
.length
157 if vis
!= compiler
.header
then
158 for l
in vis
.decl_lines
do
163 for l
in vis
.lines
do
168 if file
!= null then file
.close
170 self.toolcontext
.info
("Total C source files to compile: {cfiles.length}", 2)
172 # Generate the Makefile
174 var makename
= ".nit_compile/{mainmodule.name}.mk"
175 var makefile
= new OFStream.open
(makename
)
177 makefile
.write
("CC = ccache cc\nCFLAGS = -g -O2\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc\n\n")
178 makefile
.write
("all: {outname}\n\n")
180 var ofiles
= new Array[String]
182 var o
= f
.strip_extension
(".c") + ".o"
183 makefile
.write
("{o}: {f}\n\t$(CC) $(CFLAGS) -I .nit_compile -I ../clib -c -o {o} {f}\n\n")
187 makefile
.write
("{outname}: {ofiles.join(" ")} {compiler.extern_bodies.join(" ")}\n\t$(CC) -Wl,--warn-unresolved-symbols $(CFLAGS) $(LDFLAGS) $(LDLIBS) -I .nit_compile -I ../clib -o {outname} {ofiles.join(" ")} {compiler.extern_bodies.join(" ")}\n\n")
189 self.toolcontext
.info
("Generated makefile: {makename}", 2)
192 self.toolcontext
.info
("*** END COMPILING TO C: {time1-time0} ***", 2)
194 # Execute the Makefile
196 if self.toolcontext
.opt_no_cc
.value
then return
199 self.toolcontext
.info
("*** COMPILING C ***", 1)
200 self.toolcontext
.info
("make -f {makename} -j 4", 2)
203 if self.toolcontext
.verbose_level
>= 3 then
204 res
= sys
.system
("make -f {makename} -j 4 2>&1")
206 res
= sys
.system
("make -f {makename} -j 4 2>&1 >/dev/null")
209 toolcontext
.error
(null, "make failed! Error code: {res}.")
213 self.toolcontext
.info
("*** END COMPILING C: {time1-time0} ***", 2)
217 # Singleton that store the knowledge about the compilation process
218 private class GlobalCompiler
219 # The main module of the program
220 var mainmodule
: MModule
222 # The result of the RTA (used to know live types and methods)
223 var runtime_type_analysis
: RapidTypeAnalysis
225 # The modeulbuilder used to know the model and the AST
226 var modelbuilder
: ModelBuilder
228 # Is hardening asked (see --hardening)
229 fun hardening
: Bool do return self.modelbuilder
.toolcontext
.opt_hardening
.value
231 init(mainmodule
: MModule, runtime_type_analysis
: RapidTypeAnalysis, modelbuilder
: ModelBuilder)
233 self.mainmodule
= mainmodule
234 self.runtime_type_analysis
= runtime_type_analysis
235 self.modelbuilder
= modelbuilder
236 self.live_primitive_types
= new Array[MClassType]
237 for t
in runtime_type_analysis
.live_types
do
238 if t
.ctype
!= "val*" then
239 self.live_primitive_types
.add
(t
)
244 # Subset of runtime_type_analysis.live_types that contains only primitive types
245 var live_primitive_types
: Array[MClassType]
247 # runtime_functions that need to be compiled
248 private var todos
: List[RuntimeFunction] = new List[RuntimeFunction]
250 # runtime_functions already seen (todo or done)
251 private var seen
: HashSet[RuntimeFunction] = new HashSet[RuntimeFunction]
252 fun todo
(m
: RuntimeFunction)
254 if seen
.has
(m
) then return
259 # Where global declaration are stored (the main .h)
261 # FIXME: should not be a vistor but just somewhere to store lines
262 # FIXME: should not have a global .h since its does no helps recompilation
263 var header
: nullable GlobalCompilerVisitor = null
265 # The list of all associated visitors
266 # Used to generate .c files
267 private var visitors
: List[GlobalCompilerVisitor] = new List[GlobalCompilerVisitor]
269 # List of additional .c files required to compile (native interface)
270 var extern_bodies
= new ArraySet[String]
272 # Return the C symbol associated to a live type runtime
273 # REQUIRE: self.runtime_type_analysis.live_types.has(mtype)
274 fun classid
(mtype
: MClassType): String
276 if self.classids
.has_key
(mtype
) then
277 return self.classids
[mtype
]
279 print
"No classid for {mtype}"
283 # Cache for classid (computed by declare_runtimeclass)
284 private var classids
: HashMap[MClassType, String] = new HashMap[MClassType, String]
286 # Declare C structures and identifiers for a runtime class
287 fun declare_runtimeclass
(v
: GlobalCompilerVisitor, mtype
: MClassType)
289 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
290 v
.add_decl
("/* runtime class {mtype} */")
291 var idnum
= classids
.length
292 var idname
= "ID_" + mtype
.c_name
293 self.classids
[mtype
] = idname
294 v
.add_decl
("#define {idname} {idnum} /* {mtype} */")
295 v
.add_decl
("struct {mtype.c_name} \{")
296 v
.add_decl
("int classid; /* must be {idname} */")
297 if mtype
.ctype
!= "val*" then
298 v
.add_decl
("{mtype.ctype} value;")
300 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
) do
301 for p
in cd
.intro_mproperties
do
302 if not p
isa MAttribute then continue
303 var t
= p
.intro
.static_mtype
.as(not null)
304 t
= t
.anchor_to
(self.mainmodule
, mtype
)
305 v
.add_decl
("{t.ctype} {p.intro.c_name}; /* {p}: {t} */")
311 # Generate the init-instance of a live type (allocate + init-instance)
312 fun generate_init_instance
(mtype
: MClassType)
314 assert self.runtime_type_analysis
.live_types
.has
(mtype
)
315 assert mtype
.ctype
== "val*"
316 var v
= new GlobalCompilerVisitor(self)
318 self.header
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}(void);")
319 v
.add_decl
("/* allocate {mtype} */")
320 v
.add_decl
("{mtype.ctype} NEW_{mtype.c_name}(void) \{")
321 var res
= v
.new_var
(mtype
)
322 v
.add
("{res} = GC_MALLOC(sizeof(struct {mtype.c_name}));")
323 v
.add
("{res}->classid = {self.classid(mtype)};")
325 for cd
in mtype
.collect_mclassdefs
(self.mainmodule
)
327 var n
= self.modelbuilder
.mclassdef2nclassdef
[cd
]
328 for npropdef
in n
.n_propdefs
do
329 if npropdef
isa AAttrPropdef then
330 npropdef
.init_expr
(v
, res
)
334 v
.add
("return {res};")
338 # look for a needed .h and .c file for a given .nit source-file
339 # FIXME: bad API, parameter should be a MModule, not its source-file
340 fun add_extern
(file
: String)
342 file
= file
.strip_extension
(".nit")
343 var tryfile
= file
+ ".nit.h"
344 if tryfile
.file_exists
then
345 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
347 tryfile
= file
+ "_nit.h"
348 if tryfile
.file_exists
then
349 self.header
.add_decl
("#include \"{"..".join_path(tryfile)}\
"")
351 tryfile
= file
+ ".nit.c"
352 if tryfile
.file_exists
then
353 self.extern_bodies
.add
(tryfile
)
355 tryfile
= file
+ "_nit.c"
356 if tryfile
.file_exists
then
357 self.extern_bodies
.add
(tryfile
)
359 #(new OFStream.open("{file.basename("")}._nitni.h")).close
364 # Mangle a string to be a unique valid C identifier
365 fun to_cmangle
: String
368 var underscore
= false
370 if (c
>= 'a' and c
<= 'z') or (c
>='A' and c
<= 'Z') then
376 res
.append
('_'.ascii
.to_s
)
379 if c
>= '0' and c
<= '9' then
382 else if c
== '_' then
387 res
.append
(c
.ascii
.to_s
)
395 # Escape " \ ' and non printable characters for literal C strings or characters
396 fun escape_to_c
: String
402 else if c
== '\0' then
404 else if c
== '"' then
406 else if c == '\'' then
408 else if c == '\\
' then
410 else if c.ascii < 32 then
411 b.append("\\{c.ascii.to_base(8, false)}")
421 # Return the C type associated to a given Nit static type
427 # Return the name of the C structure associated to a Nit live type
428 # FIXME: move to GlobalCompiler so we can check that self is a live type
429 fun c_name: String is abstract
430 private var c_name_cache: nullable String
433 redef class MClassType
436 var res = self.c_name_cache
437 if res != null then return res
438 res = "{mclass.intro_mmodule.name.to_cmangle}__{mclass.name.to_cmangle}"
439 self.c_name_cache = res
443 redef fun ctype: String
445 if mclass.name == "Int" then
447 else if mclass.name == "Bool" then
449 else if mclass.name == "Char" then
451 else if mclass.name == "Float" then
453 else if mclass.name == "NativeString" then
455 else if mclass.name == "NativeArray" then
456 assert self isa MGenericType
457 return "{self.arguments.first.ctype}*"
458 else if mclass.kind == extern_kind then
466 redef class MGenericType
469 var res = self.c_name_cache
470 if res != null then return res
472 for t in self.arguments do
475 self.c_name_cache = res
480 redef class MNullableType
483 var res = self.c_name_cache
484 if res != null then return res
485 res = "nullable_{self.mtype.c_name}"
486 self.c_name_cache = res
491 # A C function associated to a Nit method
492 # Because of customization, a given Nit method can be compiler more that once
493 private class RuntimeFunction
494 # The associated Nit method
495 var mmethoddef: MMethodDef
497 # The considered reciever
498 # (usually is a live type but no strong guarantee)
501 # The mangled c name of the runtime_function
504 var res = self.c_name_cache
505 if res != null then return res
506 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
507 res = self.mmethoddef.c_name
509 res = "{mmethoddef.c_name}__{recv.c_name}"
511 self.c_name_cache = res
515 private var c_name_cache: nullable String = null
518 # used in the compiler worklist
520 if not o isa RuntimeFunction then return false
521 if self.mmethoddef != o.mmethoddef then return false
522 if self.recv != o.recv then return false
527 # used in the compiler work-list
529 var res = self.mmethoddef.hash + self.recv.hash
535 if self.mmethoddef.mclassdef.bound_mtype == self.recv then
536 return self.mmethoddef.to_s
538 return "{self.mmethoddef}@{self.recv}"
542 # Implements a call of the runtime_function
543 # May inline the body
544 fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
546 var ret = self.mmethoddef.msignature.return_mtype
547 if self.mmethoddef.mproperty.is_new then
548 ret = arguments.first.mtype
551 ret = v.resolve_for(ret, arguments.first)
553 if self.mmethoddef.can_inline(v) then
554 var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
555 frame.returnlabel = v.get_name("RET_LABEL")
557 frame.returnvar = v.new_var(ret)
559 var old_frame = v.frame
561 v.add("\{ /* Inline {self} ({arguments.join(",")}) */")
562 self.mmethoddef.compile_inside_to_c(v, arguments)
563 v.add("{frame.returnlabel.as(not null)}:(void)0;")
566 return frame.returnvar
568 v.adapt_signature(self.mmethoddef, arguments)
569 v.compiler.todo(self)
571 v.add("{self.c_name}({arguments.join(",")});")
574 var res = v.new_var(ret)
575 v.add("{res} = {self.c_name}({arguments.join(",")});")
581 # A runtime variable hold a runtime value in C
582 # Runtime variables are associated to local variables and intermediate results
583 private class RuntimeVariable
584 # The name of the variable in the C code
587 # The static type of the variable (as declard in C)
590 # The current casted type of the variable (as known in Nit)
593 redef fun to_s do return name
595 redef fun inspect do return "<{inspect_head} {name}:{mtype}({mcasttype})>"
598 # Visit the AST to generate the C code.
599 # Because of inlining, a visitor can visit more than one property.
600 private class GlobalCompilerVisitor
601 # The associated compiler
602 var compiler: GlobalCompiler
604 init(compiler: GlobalCompiler)
606 self.compiler = compiler
607 compiler.visitors.add(self)
610 # Alias for self.compiler.mainmodule.object_type
611 fun object_type: MClassType do return self.compiler.mainmodule.object_type
613 # Alias for self.compiler.mainmodule.bool_type
614 fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
616 # Force to get the primitive class named `name' or abort
617 fun get_class
(name
: String): MClass
619 return self.compiler
.mainmodule
.get_primitive_class
(name
)
622 # Force to get the primitive property named `name' in the instance `recv' or abort
623 fun get_property
(name
: String, recv
: MType): MMethod
625 return self.compiler
.mainmodule
.force_get_primitive_method
(name
, recv
)
629 var frame
: nullable Frame
631 # Anchor a type to the main module and the current receiver
632 fun anchor
(mtype
: MType): MType
634 if not mtype
.need_anchor
then return mtype
635 #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
636 return mtype
.anchor_to
(self.compiler
.mainmodule
, self.frame
.receiver
)
639 # Add a line in the main part of the generated C
646 # (used for local or global declaration)
647 fun add_decl
(s
: String)
649 self.decl_lines
.add
(s
)
652 private var lines
: List[String] = new List[String]
653 private var decl_lines
: List[String] = new List[String]
655 # The current visited AST node
656 var current_node
: nullable AExpr = null
658 # Compile an expression an return its
659 fun expr
(nexpr
: AExpr, mtype
: nullable MType): RuntimeVariable
661 var old
= self.current_node
662 self.current_node
= nexpr
663 var res
= nexpr
.expr
(self).as(not null)
664 if mtype
!= null then
665 mtype
= self.anchor
(mtype
)
666 res
= self.autobox
(res
, mtype
)
668 self.current_node
= old
672 # Unsafely cast a value to a new type
673 # ENSURE: return.name == value.name
674 fun autoadapt
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
676 mtype
= self.anchor
(mtype
)
677 if value
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
681 var valmtype
= value
.mtype
682 if valmtype
isa MNullableType and valmtype
.mtype
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
683 var res
= new RuntimeVariable(value
.name
, value
.mtype
, valmtype
.mtype
)
686 var res
= new RuntimeVariable(value
.name
, value
.mtype
, mtype
)
691 # Box or unbox a value to another type iff a C type conversion is needed
692 # ENSURE: result.mtype.ctype == mtype.ctype
693 fun autobox
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
695 if value
.mtype
.ctype
== mtype
.ctype
then
697 else if value
.mtype
.ctype
== "val*" then
698 return self.new_expr
("((struct {mtype.c_name}*){value})->value /* autounbox from {value.mtype} to {mtype} */", mtype
)
699 else if mtype
.ctype
== "val*" then
700 var valtype
= value
.mtype
.as(MClassType)
701 var res
= self.new_var
(mtype
)
702 if not compiler
.runtime_type_analysis
.live_types
.has
(valtype
) then
703 self.add
("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
704 self.add
("printf(\"Dead code executed
!\\n\
"); exit(1);")
707 self.add
("{res} = GC_MALLOC(sizeof(struct {valtype.c_name})); /* autobox from {value.mtype} to {mtype} */")
708 self.add
("{res}->classid = {self.compiler.classid(valtype)};")
709 self.add
("((struct {valtype.c_name}*){res})->value = {value};")
712 # Bad things will append!
713 var res
= self.new_var
(mtype
)
714 self.add
("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
715 self.add
("printf(\"Cast error
: Cannot cast
%s to
%s
.\\n\
", \"{value.mtype}\
", \"{mtype}\
"); exit(1);")
720 # Correctly assign a left and a right value
721 # Boxing and unboxing is performed if required
722 fun assign
(left
, right
: RuntimeVariable)
724 right
= self.autobox
(right
, left
.mtype
)
725 self.add
("{left} = {right};")
729 # Alias for `self.expr(nexpr, self.bool_type)'
730 fun expr_bool
(nexpr
: AExpr): RuntimeVariable
732 return expr
(nexpr
, bool_type
)
736 fun stmt
(nexpr
: nullable AExpr)
738 if nexpr
== null then return
739 var old
= self.current_node
740 self.current_node
= nexpr
742 self.current_node
= old
745 # Safely show a debug message on the current node and repeat the message in the C code
746 fun debug
(message
: String)
748 var node
= self.current_node
754 self.add
("/* DEBUG: {message} */")
757 # Return a new uninitialized local runtime_variable
758 fun new_var
(mtype
: MType): RuntimeVariable
760 if mtype
isa MNullType then
761 mtype
= self.object_type
763 mtype
= self.anchor
(mtype
)
765 var name
= self.get_name
("var")
766 var res
= new RuntimeVariable(name
, mtype
, mtype
)
767 self.add_decl
("{mtype.ctype} {name} /* : {mtype} */;")
771 # Return a new local runtime_variable initialized with the C expression `cexpr'.
772 fun new_expr
(cexpr
: String, mtype
: MType): RuntimeVariable
774 var res
= new_var
(mtype
)
775 self.add
("{res} = {cexpr};")
779 # Return the local runtime_variable associated to a Nit local variable
780 fun variable
(variable
: Variable): RuntimeVariable
782 if self.variables
.has_key
(variable
) then
783 return self.variables
[variable
]
785 var name
= self.get_name
("var_{variable.name}")
786 var mtype
= variable
.declared_type
.as(not null)
787 mtype
= self.anchor
(mtype
)
788 var res
= new RuntimeVariable(name
, mtype
, mtype
)
789 self.add_decl
("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
790 self.variables
[variable
] = res
795 private var variables
: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
797 # Return a new name based on `s' and unique in the visitor
798 fun get_name
(s
: String): String
800 if not self.names
.has
(s
) then
804 var i
= self.last
+ 1
807 if not self.names
.has
(s2
) then
816 private var last
: Int = 0
818 private var names
: HashSet[String] = new HashSet[String]
820 # Generate a return with the value `s'
821 fun ret
(s
: RuntimeVariable)
823 self.assign
(self.frame
.returnvar
.as(not null), s
)
824 self.add
("goto {self.frame.returnlabel.as(not null)};")
827 # The runtime types that are acceptable for a given receiver.
828 fun collect_types
(recv
: RuntimeVariable): Array[MClassType]
830 var mtype
= recv
.mcasttype
831 var cache
= self.collect_types_cache
832 if cache
.has_key
(mtype
) then
835 var types
= new Array[MClassType]
836 var mainmodule
= self.compiler
.mainmodule
837 for t
in self.compiler
.runtime_type_analysis
.live_types
do
838 if not t
.is_subtype
(mainmodule
, null, mtype
) then continue
845 private var collect_types_cache
: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
847 fun resolve_for
(mtype
: MType, recv
: RuntimeVariable): MType
849 if not mtype
.need_anchor
then return mtype
850 #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
851 var res
= mtype
.resolve_for
(recv
.mcasttype
, self.frame
.receiver
, self.compiler
.mainmodule
, true)
855 # Generate a polymorphic send for the method `m' and the arguments `args'
856 fun send
(m
: MMethod, args
: Array[RuntimeVariable]): nullable RuntimeVariable
858 var types
= self.collect_types
(args
.first
)
860 var res
: nullable RuntimeVariable
861 var ret
= m
.intro
.msignature
.return_mtype
863 ret
= args
.first
.mtype
864 res
= self.new_var
(ret
)
865 else if ret
== null then
868 ret
= self.resolve_for
(ret
, args
.first
)
869 res
= self.new_var
(ret
)
872 if types
.is_empty
then
873 self.add
("/*BUG: no live types for {args.first.mtype} . {m}*/")
876 self.add
("/* send {m} on {args.first}: {args.first.mcasttype} (declared {args.first.mtype}) */")
877 if args
.first
.mtype
.ctype
!= "val*" then
878 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, args
.first
.mtype
)
879 if propdefs
.length
== 0 then
880 self.add
("/* skip {args.first.mcasttype}, no method {m} */")
883 assert propdefs
.length
== 1
884 var propdef
= propdefs
.first
885 var res2
= self.call
(propdef
, args
.first
.mtype
.as(MClassType), args
)
886 if res
!= null then self.assign
(res
, res2
.as(not null))
889 if args
.first
.mcasttype
isa MNullableType then
890 self.add
("if ({args.first} == NULL) \{ /* Special null case */")
891 if m
.name
== "==" then
893 if args
[1].mcasttype
.ctype
== "val*" then
894 self.add
("{res} = ({args[1]} == NULL);")
896 self.add
("{res} = 0; /* {args[1]}: {args[1].mcasttype} cannot be null */")
898 else if m
.name
== "!=" then
900 if args
[1].mcasttype
.ctype
== "val*" then
901 self.add
("{res} = ({args[1]} != NULL);")
903 self.add
("{res} = 1; /* {args[1]}: {args[1].mcasttype} cannot be null */")
906 self.add_abort
("Reciever is null")
910 self.add
("switch({args.first}->classid) \{")
911 var last
= types
.last
912 var defaultpropdef
: nullable MMethodDef = null
914 var propdefs
= m
.lookup_definitions
(self.compiler
.mainmodule
, t
)
915 if propdefs
.length
== 0 then
916 self.add
("/* skip {t}, no method {m} */")
919 var propdef
= propdefs
.first
920 if propdef
.mclassdef
.mclass
.name
== "Object" and t
.ctype
== "val*" then
921 defaultpropdef
= propdef
924 if not self.compiler
.hardening
and t
== last
and defaultpropdef
== null then
925 self.add
("default: /* test {t} */")
927 self.add
("case {self.compiler.classid(t)}: /* test {t} */")
929 var res2
= self.call
(propdef
, t
, args
)
930 if res
!= null then self.assign
(res
, res2
.as(not null))
933 if defaultpropdef
!= null then
934 self.add
("default: /* default is Object */")
935 var res2
= self.call
(defaultpropdef
, defaultpropdef
.mclassdef
.bound_mtype
, args
)
936 if res
!= null then self.assign
(res
, res2
.as(not null))
937 else if self.compiler
.hardening
then
938 self.add
("default: /* bug */")
939 self.bugtype
(args
.first
)
945 fun check_valid_reciever
(recvtype
: MClassType)
947 if self.compiler
.runtime_type_analysis
.live_types
.has
(recvtype
) or recvtype
.mclass
.name
== "Object" then return
948 print
"{recvtype} is not a live type"
952 fun call
(m
: MMethodDef, recvtype
: MClassType, args
: Array[RuntimeVariable]): nullable RuntimeVariable
954 check_valid_reciever
(recvtype
)
955 #debug("call {m} on {recvtype} on {args.first}:{args.first.mtype}")
956 if m
.mclassdef
.mclass
.name
== "Object" and recvtype
.ctype
== "val*" then
957 recvtype
= m
.mclassdef
.bound_mtype
959 var recv
= self.autobox
(args
.first
, recvtype
)
960 recv
= self.autoadapt
(recv
, recvtype
)
962 var vararg_rank
= m
.msignature
.vararg_rank
963 if vararg_rank
>= 0 then
964 assert args
.length
>= m
.msignature
.arity
+ 1 # because of self
966 args
= new Array[RuntimeVariable]
968 args
.add
(rawargs
.first
) # recv
970 for i
in [0..vararg_rank
[ do
971 args
.add
(rawargs
[i
+1])
974 var vararg_lastrank
= vararg_rank
+ rawargs
.length-1-m
.msignature
.arity
975 var vararg
= new Array[RuntimeVariable]
976 for i
in [vararg_rank
..vararg_lastrank
] do
977 vararg
.add
(rawargs
[i
+1])
979 # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
980 var elttype
= m
.msignature
.parameter_mtypes
[vararg_rank
]
981 elttype
= self.resolve_for
(elttype
, recv
)
982 args
.add
(self.array_instance
(vararg
, elttype
))
984 for i
in [vararg_lastrank
+1..rawargs
.length-1
[ do
985 args
.add
(rawargs
[i
+1])
990 assert args
.length
== m
.msignature
.arity
+ 1 # because of self
993 var rm
= new RuntimeFunction(m
, recvtype
)
994 return rm
.call
(self, args
)
997 fun adapt_signature
(m
: MMethodDef, args
: Array[RuntimeVariable])
999 var recv
= args
.first
1000 for i
in [0..m
.msignature
.arity
[ do
1001 var t
= m
.msignature
.parameter_mtypes
[i
]
1002 if i
== m
.msignature
.vararg_rank
then
1005 t
= self.resolve_for
(t
, recv
)
1006 args
[i
+1] = self.autobox
(args
[i
+1], t
)
1010 fun bugtype
(recv
: RuntimeVariable)
1012 if recv
.mtype
.ctype
!= "val*" then return
1013 self.add
("fprintf(stderr, \"BTD BUG: Dynamic type is %s
, static
type is %s\\n\
", class_names[{recv}->classid], \"{recv.mcasttype}\
");")
1016 # Generate a polymorphic attribute read
1017 fun read_attribute
(a
: MAttribute, recv
: RuntimeVariable): RuntimeVariable
1019 var types
= self.collect_types
(recv
)
1021 var ret
= a
.intro
.static_mtype
.as(not null)
1022 ret
= self.resolve_for
(ret
, recv
)
1023 var res
= self.new_var
(ret
)
1025 if types
.is_empty
then
1026 self.add
("/*BUG: no live types for {recv.mtype} . {a}*/")
1029 self.add
("/* read {a} on {recv.mcasttype} */")
1030 self.add
("switch({recv}->classid) \{")
1031 var last
= types
.last
1033 if not self.compiler
.hardening
and t
== last
then
1034 self.add
("default: /*{self.compiler.classid(t)}*/")
1036 self.add
("case {self.compiler.classid(t)}:")
1038 var recv2
= self.autoadapt
(recv
, t
)
1039 var ta
= a
.intro
.static_mtype
.as(not null)
1040 ta
= self.resolve_for
(ta
, recv2
)
1041 var res2
= self.new_expr
("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta
)
1042 if not ta
isa MNullableType then
1043 if ta
.ctype
== "val*" then
1044 self.add
("if ({res2} == NULL) \{")
1045 self.add_abort
("Uninitialized attribute {a.name}")
1048 self.add
("/*NOTYET isset on primitive attributes*/")
1051 self.assign
(res
, res2
)
1054 if self.compiler
.hardening
then
1055 self.add
("default: /* Bug */")
1063 # Generate a polymorphic attribute write
1064 fun write_attribute
(a
: MAttribute, recv
: RuntimeVariable, value
: RuntimeVariable)
1066 var types
= self.collect_types
(recv
)
1068 if types
.is_empty
then
1069 self.add
("/*BUG: no live types for {recv.mtype} . {a}*/")
1072 self.add
("/* write {a} on {recv.mcasttype} */")
1073 self.add
("switch({recv}->classid) \{")
1074 var last
= types
.last
1076 if not self.compiler
.hardening
and t
== last
then
1077 self.add
("default: /*{self.compiler.classid(t)}*/")
1079 self.add
("case {self.compiler.classid(t)}:")
1081 var recv2
= self.autoadapt
(recv
, t
)
1082 var ta
= a
.intro
.static_mtype
.as(not null)
1083 ta
= self.resolve_for
(ta
, recv2
)
1084 self.add
("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
1087 if self.compiler
.hardening
then
1088 self.add
("default: /* Bug*/")
1094 # Generate a alloc-instance + init-attributes
1095 fun init_instance
(mtype
: MClassType): RuntimeVariable
1097 mtype
= self.anchor
(mtype
).as(MClassType)
1098 if not self.compiler
.runtime_type_analysis
.live_types
.has
(mtype
) then
1099 debug
"problem: {mtype} was detected dead"
1101 var res
= self.new_expr
("NEW_{mtype.c_name}()", mtype
)
1105 # Generate a polymorphic subtype test
1106 fun type_test
(value
: RuntimeVariable, mtype
: MType): RuntimeVariable
1108 mtype
= self.anchor
(mtype
)
1109 var types
= self.collect_types
(value
)
1111 var res
= self.new_var
(bool_type
)
1113 self.add
("/* isa {mtype} on {value.mcasttype} */")
1114 if value
.mcasttype
isa MNullableType then
1115 self.add
("if ({value} == NULL) \{")
1116 if mtype
isa MNullableType then
1117 self.add
("{res} = 1; /* isa {mtype} */")
1119 self.add
("{res} = 0; /* not isa {mtype} */")
1121 self.add
("\} else ")
1123 self.add
("switch({value}->classid) \{")
1125 if t
.is_subtype
(self.compiler
.mainmodule
, null, mtype
) then
1126 self.add
("case {self.compiler.classid(t)}: /* {t} */")
1129 self.add
("{res} = 1;")
1131 self.add
("default:")
1132 self.add
("{res} = 0;")
1138 # Generate a Nit "is" for two runtime_variables
1139 fun equal_test
(value1
, value2
: RuntimeVariable): RuntimeVariable
1141 var res
= self.new_var
(bool_type
)
1142 if value2
.mtype
.ctype
!= "val*" and value1
.mtype
.ctype
== "val*" then
1147 if value1
.mtype
.ctype
!= "val*" then
1148 if value2
.mtype
.ctype
== value1
.mtype
.ctype
then
1149 self.add
("{res} = {value1} == {value2};")
1150 else if value2
.mtype
.ctype
!= "val*" then
1151 self.add
("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
1153 var mtype1
= value1
.mtype
.as(MClassType)
1154 self.add
("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
1155 self.add
("if ({res}) \{")
1156 self.add
("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
1160 var s
= new Array[String]
1161 for t
in self.compiler
.live_primitive_types
do
1162 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value1
.mcasttype
) then continue
1163 if not t
.is_subtype
(self.compiler
.mainmodule
, null, value2
.mcasttype
) then continue
1164 s
.add
"({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
1167 self.add
("{res} = {value1} == {value2};")
1169 self.add
("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")
1175 # Generate a check-init-instance
1176 # TODO: is an empty stub currently
1177 fun check_init_instance
(recv
: RuntimeVariable)
1181 # Generate an integer value
1182 fun int_instance
(value
: Int): RuntimeVariable
1184 var res
= self.new_var
(self.get_class
("Int").mclass_type
)
1185 self.add
("{res} = {value};")
1189 # Generate an array value
1190 fun array_instance
(array
: Array[RuntimeVariable], elttype
: MType): RuntimeVariable
1192 elttype
= self.anchor
(elttype
)
1193 var res
= self.init_instance
(self.get_class
("Array").get_mtype
([elttype
]))
1194 self.add
("\{ /* {res} = array_instance Array[{elttype}] */")
1195 var nat
= self.new_var
(self.get_class
("NativeArray").get_mtype
([elttype
]))
1196 self.add
("{nat} = GC_MALLOC({array.length} * sizeof({elttype.ctype}));")
1197 for i
in [0..array
.length
[ do
1198 var r
= self.autobox
(array
[i
], elttype
)
1199 self.add
("{nat}[{i}] = {r};")
1201 var length
= self.int_instance
(array
.length
)
1202 self.send
(self.get_property
("with_native", res
.mtype
), [res
, nat
, length
])
1203 self.check_init_instance
(res
)
1208 # Generate a string value
1209 fun string_instance
(string
: String): RuntimeVariable
1211 var nat
= self.new_var
(self.get_class
("NativeString").mclass_type
)
1212 self.add
("{nat} = \"{string.escape_to_c}\
";")
1213 var res
= self.init_instance
(self.get_class
("String").mclass_type
)
1214 var length
= self.int_instance
(string
.length
)
1215 self.send
(self.get_property
("with_native", res
.mtype
), [res
, nat
, length
])
1216 self.check_init_instance
(res
)
1220 # Generate generic abort
1221 # used by aborts, asserts, casts, etc.
1222 fun add_abort
(message
: String)
1224 if self.current_node
!= null and self.current_node
.location
.file
!= null then
1225 self.add
("fprintf(stderr, \"%s
(%s
:%d
)\\n\
", \"{message.escape_to_c}\
", \"{self.current_node.location.file.filename.escape_to_c}\
", {current_node.location.line_start});")
1227 self.add
("fprintf(stderr, \"%s\\n\
", \"{message.escape_to_c}\
");")
1229 self.add
("exit(1);")
1233 # A frame correspond to a visited property in a GlobalCompilerVisitor
1235 # The associated visitor
1237 var visitor
: GlobalCompilerVisitor
1239 # The executed property.
1240 # A Method in case of a call, an attribute in case of a default initialization.
1241 var mpropdef
: MPropDef
1243 # The static type of the receiver
1244 var receiver
: MClassType
1246 # Arguments of the method (the first is the receiver)
1247 var arguments
: Array[RuntimeVariable]
1249 # The runtime_variable associated to the return (in a function)
1250 var returnvar
: nullable RuntimeVariable = null
1252 # The label at the end of the property
1253 var returnlabel
: nullable String = null
1256 redef class MPropDef
1257 private var c_name_cache
: nullable String
1259 # The mangled name associated to the property
1262 var res
= self.c_name_cache
1263 if res
!= null then return res
1264 res
= "{self.mclassdef.mmodule.name.to_cmangle}__{self.mclassdef.mclass.name.to_cmangle}__{self.mproperty.name.to_cmangle}"
1265 self.c_name_cache
= res
1270 redef class MMethodDef
1271 # Can the body be inlined?
1272 private fun can_inline
(v
: GlobalCompilerVisitor): Bool
1274 var modelbuilder
= v
.compiler
.modelbuilder
1275 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1276 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1277 return npropdef
.can_inline
1278 else if self.mproperty
.name
== "init" then
1279 # Automatic free init is always inlined since it is empty or contains only attribtes assigments
1286 # Inline the body in another visitor
1287 private fun compile_inside_to_c
(v
: GlobalCompilerVisitor, arguments
: Array[RuntimeVariable]): nullable RuntimeVariable
1289 var modelbuilder
= v
.compiler
.modelbuilder
1290 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1291 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1292 npropdef
.compile_to_c
(v
, self, arguments
)
1293 else if self.mproperty
.name
== "init" then
1294 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1295 nclassdef
.compile_to_c
(v
, self, arguments
)
1302 # Compile the body in a new visitor
1303 private fun compile_to_c
(compiler
: GlobalCompiler, modelbuilder
: ModelBuilder, recv
: MClassType)
1305 if not recv
.is_subtype
(compiler
.mainmodule
, null, self.mclassdef
.bound_mtype
) then
1306 print
("problem: why do we compile {self} for {recv}?")
1310 var v
= new GlobalCompilerVisitor(compiler
)
1311 var selfvar
= new RuntimeVariable("self", recv
, recv
)
1312 var arguments
= new Array[RuntimeVariable]
1313 var frame
= new Frame(v
, self, recv
, arguments
)
1316 var sig
= new Buffer
1317 var comment
= new Buffer
1318 var ret
= self.msignature
.return_mtype
1320 ret
= v
.resolve_for
(ret
, selfvar
)
1321 sig
.append
("{ret.ctype} ")
1322 else if self.mproperty
.is_new
then
1324 sig
.append
("{ret.ctype} ")
1328 sig
.append
(self.c_name
)
1329 if recv
!= self.mclassdef
.bound_mtype
then
1330 sig
.append
("__{recv.c_name}")
1332 sig
.append
("({recv.ctype} self")
1333 comment
.append
("(self: {recv}")
1334 arguments
.add
(selfvar
)
1335 for i
in [0..self.msignature
.arity
[ do
1336 var mtype
= self.msignature
.parameter_mtypes
[i
]
1337 if i
== self.msignature
.vararg_rank
then
1338 mtype
= v
.get_class
("Array").get_mtype
([mtype
])
1340 mtype
= v
.resolve_for
(mtype
, selfvar
)
1341 comment
.append
(", {mtype}")
1342 sig
.append
(", {mtype.ctype} p{i}")
1343 var argvar
= new RuntimeVariable("p{i}", mtype
, mtype
)
1344 arguments
.add
(argvar
)
1349 comment
.append
(": {ret}")
1351 compiler
.header
.add_decl
("{sig};")
1353 v
.add_decl
("/* method {self} for {comment} */")
1354 v
.add_decl
("{sig} \{")
1355 #v.add("printf(\"method {self} for {comment}\\n\");")
1357 frame
.returnvar
= v
.new_var
(ret
)
1359 frame
.returnlabel
= v
.get_name
("RET_LABEL")
1361 if modelbuilder
.mpropdef2npropdef
.has_key
(self) then
1362 var npropdef
= modelbuilder
.mpropdef2npropdef
[self]
1363 npropdef
.compile_to_c
(v
, self, arguments
)
1364 else if self.mproperty
.name
== "init" then
1365 var nclassdef
= modelbuilder
.mclassdef2nclassdef
[self.mclassdef
]
1366 nclassdef
.compile_to_c
(v
, self, arguments
)
1371 v
.add
("{frame.returnlabel.as(not null)}:;")
1373 v
.add
("return {frame.returnvar.as(not null)};")
1379 redef class APropdef
1380 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1382 v
.add
("printf(\"Not implemented
{class_name} {mpropdef} at
{location.to_s}\\n\
");")
1383 debug
("Not yet implemented")
1386 private fun can_inline
: Bool do return true
1389 redef class AConcreteMethPropdef
1390 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1392 for i
in [0..mpropdef
.msignature
.arity
[ do
1393 var variable
= self.n_signature
.n_params
[i
].variable
.as(not null)
1394 v
.assign
(v
.variable
(variable
), arguments
[i
+1])
1396 # Call the implicit super-init
1397 var auto_super_inits
= self.auto_super_inits
1398 if auto_super_inits
!= null then
1399 var selfarg
= [arguments
.first
]
1400 for auto_super_init
in auto_super_inits
do
1401 if auto_super_init
.intro
.msignature
.arity
== 0 then
1402 v
.send
(auto_super_init
, selfarg
)
1404 v
.send
(auto_super_init
, arguments
)
1409 v
.stmt
(self.n_block
)
1412 redef fun can_inline
1414 if self.auto_super_inits
!= null then return false
1415 var nblock
= self.n_block
1416 if nblock
== null then return true
1417 if (mpropdef
.mproperty
.name
== "==" or mpropdef
.mproperty
.name
== "!=") and mpropdef
.mclassdef
.mclass
.name
== "Object" then return true
1418 if nblock
isa ABlockExpr and nblock
.n_expr
.length
== 0 then return true
1423 redef class AInternMethPropdef
1424 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1426 var pname
= mpropdef
.mproperty
.name
1427 var cname
= mpropdef
.mclassdef
.mclass
.name
1428 var ret
= mpropdef
.msignature
.return_mtype
1430 ret
= v
.resolve_for
(ret
, arguments
.first
)
1432 if pname
!= "==" and pname
!= "!=" then
1433 v
.adapt_signature
(mpropdef
, arguments
)
1435 if cname
== "Int" then
1436 if pname
== "output" then
1437 v
.add
("printf(\"%ld\\n\
", {arguments.first});")
1439 else if pname
== "object_id" then
1440 v
.ret
(arguments
.first
)
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
== "unary -" then
1449 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1451 else if pname
== "succ" then
1452 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1454 else if pname
== "prec" then
1455 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1457 else if pname
== "*" then
1458 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
1460 else if pname
== "/" then
1461 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
1463 else if pname
== "%" then
1464 v
.ret
(v
.new_expr
("{arguments[0]} % {arguments[1]}", ret
.as(not null)))
1466 else if pname
== "lshift" then
1467 v
.ret
(v
.new_expr
("{arguments[0]} << {arguments[1]}", ret
.as(not null)))
1469 else if pname
== "rshift" then
1470 v
.ret
(v
.new_expr
("{arguments[0]} >> {arguments[1]}", ret
.as(not null)))
1472 else if pname
== "==" then
1473 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1475 else if pname
== "!=" then
1476 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1477 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1479 else if pname
== "<" then
1480 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1482 else if pname
== ">" then
1483 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1485 else if pname
== "<=" then
1486 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1488 else if pname
== ">=" then
1489 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1491 else if pname
== "to_f" then
1492 v
.ret
(v
.new_expr
("(double){arguments[0]}", ret
.as(not null)))
1494 else if pname
== "ascii" then
1495 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1498 else if cname
== "Char" then
1499 if pname
== "output" then
1500 v
.add
("printf(\"%c\
", {arguments.first});")
1502 else if pname
== "object_id" then
1503 v
.ret
(arguments
.first
)
1505 else if pname
== "==" then
1506 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1508 else if pname
== "!=" then
1509 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1510 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1512 else if pname
== "succ" then
1513 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1515 else if pname
== "prec" then
1516 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1518 else if pname
== "<" then
1519 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1521 else if pname
== ">" then
1522 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1524 else if pname
== "<=" then
1525 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", 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
== "to_i" then
1531 v
.ret
(v
.new_expr
("{arguments[0]}-'0'", ret
.as(not null)))
1533 else if pname
== "ascii" then
1534 v
.ret
(v
.new_expr
("(unsigned char){arguments[0]}", ret
.as(not null)))
1537 else if cname
== "Bool" then
1538 if pname
== "output" then
1539 v
.add
("printf({arguments.first}?\"true\\n\
":\"false\\n\
");")
1541 else if pname
== "object_id" then
1542 v
.ret
(arguments
.first
)
1544 else if pname
== "==" then
1545 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1547 else if pname
== "!=" then
1548 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1549 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1552 else if cname
== "Float" then
1553 if pname
== "output" then
1554 v
.add
("printf(\"%f\\n\
", {arguments.first});")
1556 else if pname
== "object_id" then
1557 v
.ret
(v
.new_expr
("(double){arguments.first}", 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
== "-" then
1563 v
.ret
(v
.new_expr
("{arguments[0]} - {arguments[1]}", ret
.as(not null)))
1565 else if pname
== "unary -" then
1566 v
.ret
(v
.new_expr
("-{arguments[0]}", ret
.as(not null)))
1568 else if pname
== "succ" then
1569 v
.ret
(v
.new_expr
("{arguments[0]}+1", ret
.as(not null)))
1571 else if pname
== "prec" then
1572 v
.ret
(v
.new_expr
("{arguments[0]}-1", ret
.as(not null)))
1574 else if pname
== "*" then
1575 v
.ret
(v
.new_expr
("{arguments[0]} * {arguments[1]}", ret
.as(not null)))
1577 else if pname
== "/" then
1578 v
.ret
(v
.new_expr
("{arguments[0]} / {arguments[1]}", ret
.as(not null)))
1580 else if pname
== "==" then
1581 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1583 else if pname
== "!=" then
1584 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1585 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1587 else if pname
== "<" then
1588 v
.ret
(v
.new_expr
("{arguments[0]} < {arguments[1]}", ret
.as(not null)))
1590 else if pname
== ">" then
1591 v
.ret
(v
.new_expr
("{arguments[0]} > {arguments[1]}", ret
.as(not null)))
1593 else if pname
== "<=" then
1594 v
.ret
(v
.new_expr
("{arguments[0]} <= {arguments[1]}", ret
.as(not null)))
1596 else if pname
== ">=" then
1597 v
.ret
(v
.new_expr
("{arguments[0]} >= {arguments[1]}", ret
.as(not null)))
1599 else if pname
== "to_i" then
1600 v
.ret
(v
.new_expr
("(long){arguments[0]}", ret
.as(not null)))
1603 else if cname
== "Char" then
1604 if pname
== "output" then
1605 v
.add
("printf(\"%c\
", {arguments.first});")
1607 else if pname
== "object_id" then
1608 v
.ret
(arguments
.first
)
1610 else if pname
== "==" then
1611 v
.ret
(v
.equal_test
(arguments
[0], arguments
[1]))
1613 else if pname
== "!=" then
1614 var res
= v
.equal_test
(arguments
[0], arguments
[1])
1615 v
.ret
(v
.new_expr
("!{res}", ret
.as(not null)))
1617 else if pname
== "ascii" then
1618 v
.ret
(v
.new_expr
("{arguments[0]}", ret
.as(not null)))
1621 else if cname
== "NativeString" then
1622 if pname
== "[]" then
1623 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
1625 else if pname
== "[]=" then
1626 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1628 else if pname
== "copy_to" then
1629 v
.add
("memcpy({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
1631 else if pname
== "atoi" then
1632 v
.ret
(v
.new_expr
("atoi({arguments[0]});", ret
.as(not null)))
1635 else if cname
== "NativeArray" then
1636 var elttype
= arguments
.first
.mtype
1637 if pname
== "[]" then
1638 v
.ret
(v
.new_expr
("{arguments[0]}[{arguments[1]}]", ret
.as(not null)))
1640 else if pname
== "[]=" then
1641 v
.add
("{arguments[0]}[{arguments[1]}]={arguments[2]};")
1643 else if pname
== "copy_to" then
1644 v
.add
("memcpy({arguments[1]},{arguments[0]},{arguments[2]}*sizeof({elttype.ctype}));")
1648 if pname
== "exit" then
1649 v
.add
("exit({arguments[1]});")
1651 else if pname
== "sys" then
1652 v
.ret
(v
.new_expr
("glob_sys", ret
.as(not null)))
1654 else if pname
== "calloc_string" then
1655 v
.ret
(v
.new_expr
("(char*)GC_MALLOC({arguments[1]})", ret
.as(not null)))
1657 else if pname
== "calloc_array" then
1658 var elttype
= arguments
.first
.mtype
.supertype_to
(v
.compiler
.mainmodule
,arguments
.first
.mtype
.as(MClassType),v
.get_class
("ArrayCapable")).as(MGenericType).arguments
.first
1659 v
.ret
(v
.new_expr
("({elttype.ctype}*)GC_MALLOC({arguments[1]} * sizeof({elttype.ctype}))", ret
.as(not null)))
1661 else if pname
== "object_id" then
1662 v
.ret
(v
.new_expr
("(long){arguments.first}", ret
.as(not null)))
1664 else if pname
== "is_same_type" then
1665 if arguments
[0].mtype
.ctype
== "val*" then
1666 v
.ret
(v
.new_expr
("{arguments[0]}->classid == {arguments[1]}->classid", ret
.as(not null)))
1668 v
.ret
(v
.new_expr
("{v.compiler.classid(arguments[0].mtype.as(MClassType))} == {arguments[1]}->classid", ret
.as(not null)))
1671 else if pname
== "output_class_name" then
1672 if arguments
[0].mtype
.ctype
== "val*" then
1673 v
.add
("printf(\"%s\\n\
", class_names[{arguments.first}->classid]);")
1675 v
.add
("printf(\"%s\\n\
", class_names[{v.compiler.classid(arguments.first.mtype.as(MClassType))}]);")
1678 else if pname
== "native_class_name" then
1679 if arguments
[0].mtype
.ctype
== "val*" then
1680 v
.ret
(v
.new_expr
("(char*)(void*)class_names[{arguments.first}->classid]", ret
.as(not null)))
1682 v
.ret
(v
.new_expr
("(char*)(void*)class_names[{v.compiler.classid(arguments.first.mtype.as(MClassType))}]", ret
.as(not null)))
1686 v
.add
("printf(\"Not implemented
{class_name}:{mpropdef} at
{location.to_s}\\n\
");")
1687 debug
("Not implemented {mpropdef}")
1691 redef class AExternMethPropdef
1692 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1695 var nextern
= self.n_extern
1696 if nextern
== null then
1697 debug
("{mpropdef} need extern name")
1700 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
1701 if location
.file
!= null then
1702 var file
= location
.file
.filename
1703 v
.compiler
.add_extern
(file
)
1705 var res
: nullable RuntimeVariable = null
1706 var ret
= mpropdef
.msignature
.return_mtype
1708 ret
= v
.resolve_for
(ret
, arguments
.first
)
1709 res
= v
.new_var
(ret
)
1713 v
.add
("{externname}({arguments.join(", ")});")
1715 v
.add
("{res} = {externname}({arguments.join(", ")});")
1721 redef class AExternInitPropdef
1722 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1725 var nextern
= self.n_extern
1726 if nextern
== null then
1727 debug
("{mpropdef} need extern name")
1730 externname
= nextern
.text
.substring
(1, nextern
.text
.length-2
)
1731 if location
.file
!= null then
1732 var file
= location
.file
.filename
1733 v
.compiler
.add_extern
(file
)
1735 var ret
= arguments
.first
.mtype
1736 var res
= v
.new_var
(ret
)
1740 v
.add
("{res} = {externname}({arguments.join(", ")});")
1745 redef class AAttrPropdef
1746 redef fun compile_to_c
(v
, mpropdef
, arguments
)
1748 if arguments
.length
== 1 then
1749 var res
= v
.read_attribute
(self.mpropdef
.mproperty
, arguments
.first
)
1750 v
.assign
(v
.frame
.returnvar
.as(not null), res
)
1752 v
.write_attribute
(self.mpropdef
.mproperty
, arguments
.first
, arguments
[1])
1756 private fun init_expr
(v
: GlobalCompilerVisitor, recv
: RuntimeVariable)
1758 var nexpr
= self.n_expr
1759 if nexpr
!= null then
1760 var old_frame
= v
.frame
1761 var frame
= new Frame(v
, self.mpropdef
.as(not null), recv
.mtype
.as(MClassType), [recv
])
1763 var value
= v
.expr
(nexpr
, self.mpropdef
.static_mtype
)
1764 v
.write_attribute
(self.mpropdef
.mproperty
, recv
, value
)
1770 redef class AClassdef
1771 private fun compile_to_c
(v
: GlobalCompilerVisitor, mpropdef
: MMethodDef, arguments
: Array[RuntimeVariable])
1773 if mpropdef
== self.mfree_init
then
1774 var super_inits
= self.super_inits
1775 if super_inits
!= null then
1776 assert arguments
.length
== 1
1777 for su
in super_inits
do
1778 v
.send
(su
, arguments
)
1782 var recv
= arguments
.first
1784 # Collect undefined attributes
1785 for npropdef
in self.n_propdefs
do
1786 if npropdef
isa AAttrPropdef and npropdef
.n_expr
== null then
1787 v
.write_attribute
(npropdef
.mpropdef
.mproperty
, recv
, arguments
[i
])
1798 # Try to compile self as an expression
1799 # Do not call this method directly, use `v.expr' instead
1800 private fun expr
(v
: GlobalCompilerVisitor): nullable RuntimeVariable
1802 debug
("Unimplemented expr {class_name}")
1803 v
.add
("printf(\"Not implemented
{class_name}:{location.to_s}\\n\
");")
1804 var mtype
= self.mtype
1805 if mtype
== null then
1808 var res
= v
.new_var
(mtype
)
1809 v
.add
("/* {res} = NOT YET {class_name} */")
1814 # Try to compile self as a statement
1815 # Do not call this method directly, use `v.stmt' instead
1816 private fun stmt
(v
: GlobalCompilerVisitor)
1819 if res
!= null then v
.add
("{res};")
1824 redef class ABlockExpr
1827 for e
in self.n_expr
do
1833 redef class AVardeclExpr
1836 var variable
= self.variable
.as(not null)
1837 var ne
= self.n_expr
1839 var i
= v
.expr
(ne
, variable
.declared_type
)
1840 v
.assign
(v
.variable
(variable
), i
)
1845 redef class AVarExpr
1848 var res
= v
.variable
(self.variable
.as(not null))
1849 var mtype
= self.mtype
.as(not null)
1850 return v
.autoadapt
(res
, mtype
)
1854 redef class AVarAssignExpr
1857 var variable
= self.variable
.as(not null)
1858 var i
= v
.expr
(self.n_value
, variable
.declared_type
)
1859 v
.assign
(v
.variable
(variable
), i
)
1863 redef class AVarReassignExpr
1866 var variable
= self.variable
.as(not null)
1867 var vari
= v
.variable
(variable
)
1868 var value
= v
.expr
(self.n_value
, variable
.declared_type
)
1869 var res
= v
.send
(reassign_property
.mproperty
, [vari
, value
])
1871 v
.assign
(v
.variable
(variable
), res
)
1875 redef class ASelfExpr
1878 return v
.frame
.arguments
.first
1882 redef class AContinueExpr
1885 v
.add
("goto CONTINUE_{self.escapemark.object_id};")
1889 redef class ABreakExpr
1892 v
.add
("goto BREAK_{self.escapemark.object_id};")
1896 redef class AReturnExpr
1899 var nexpr
= self.n_expr
1900 if nexpr
!= null then
1901 var returnvar
= v
.frame
.returnvar
.as(not null)
1902 var i
= v
.expr
(nexpr
, returnvar
.mtype
)
1903 v
.assign
(returnvar
, i
)
1905 v
.add
("goto {v.frame.returnlabel.as(not null)};")
1909 redef class AAbortExpr
1912 v
.add_abort
("Aborted")
1919 var cond
= v
.expr_bool
(self.n_expr
)
1920 v
.add
("if ({cond})\{")
1928 redef class AIfexprExpr
1931 var res
= v
.new_var
(self.mtype
.as(not null))
1932 var cond
= v
.expr_bool
(self.n_expr
)
1933 v
.add
("if ({cond})\{")
1934 v
.assign
(res
, v
.expr
(self.n_then
, null))
1936 v
.assign
(res
, v
.expr
(self.n_else
, null))
1945 v
.stmt
(self.n_block
)
1946 var escapemark
= self.escapemark
1947 if escapemark
!= null then
1948 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
1953 redef class AWhileExpr
1957 var cond
= v
.expr_bool
(self.n_expr
)
1958 v
.add
("if (!{cond}) break;")
1959 v
.stmt
(self.n_block
)
1960 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
1962 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
1966 redef class ALoopExpr
1970 v
.stmt
(self.n_block
)
1971 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
1973 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
1977 redef class AForExpr
1980 var cl
= v
.expr
(self.n_expr
, null)
1981 var it
= v
.send
(v
.get_property
("iterator", cl
.mtype
), [cl
])
1984 var ok
= v
.send
(v
.get_property
("is_ok", it
.mtype
), [it
])
1986 v
.add
("if(!{ok}) break;")
1987 var i
= v
.send
(v
.get_property
("item", it
.mtype
), [it
])
1989 v
.assign
(v
.variable
(variables
.first
), i
)
1990 v
.stmt
(self.n_block
)
1991 v
.add
("CONTINUE_{escapemark.object_id}: (void)0;")
1992 v
.send
(v
.get_property
("next", it
.mtype
), [it
])
1994 v
.add
("BREAK_{escapemark.object_id}: (void)0;")
1998 redef class AAssertExpr
2001 var cond
= v
.expr_bool
(self.n_expr
)
2002 v
.add
("if (!{cond}) \{")
2006 v
.add_abort
("Assert '{nid.text}' failed")
2008 v
.add_abort
("Assert failed")
2017 var res
= v
.new_var
(self.mtype
.as(not null))
2018 var i1
= v
.expr_bool
(self.n_expr
)
2019 v
.add
("if ({i1}) \{")
2022 var i2
= v
.expr_bool
(self.n_expr2
)
2023 v
.add
("{res} = {i2};")
2029 redef class AAndExpr
2032 var res
= v
.new_var
(self.mtype
.as(not null))
2033 var i1
= v
.expr_bool
(self.n_expr
)
2034 v
.add
("if (!{i1}) \{")
2037 var i2
= v
.expr_bool
(self.n_expr2
)
2038 v
.add
("{res} = {i2};")
2044 redef class ANotExpr
2047 var cond
= v
.expr_bool
(self.n_expr
)
2048 return v
.new_expr
("!{cond}", self.mtype
.as(not null))
2052 redef class AOrElseExpr
2055 var res
= v
.new_var
(self.mtype
.as(not null))
2056 var i1
= v
.expr
(self.n_expr
, null)
2057 v
.add
("if ({i1}!=NULL) \{")
2060 var i2
= v
.expr
(self.n_expr2
, null)
2070 var value1
= v
.expr
(self.n_expr
, null)
2071 var value2
= v
.expr
(self.n_expr2
, null)
2072 return v
.equal_test
(value1
, value2
)
2076 redef class AIntExpr
2079 return v
.new_expr
("{self.n_number.text}", self.mtype
.as(not null))
2083 redef class AFloatExpr
2086 return v
.new_expr
("{self.n_float.text}", self.mtype
.as(not null))
2090 redef class ACharExpr
2093 return v
.new_expr
("{self.n_char.text}", self.mtype
.as(not null))
2097 redef class AArrayExpr
2100 var mtype
= self.mtype
.as(MGenericType).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
2113 return v
.string_instance
(self.value
.as(not null))
2117 redef class ASuperstringExpr
2120 var array
= new Array[RuntimeVariable]
2121 for ne
in self.n_exprs
do
2122 var i
= v
.expr
(ne
, null)
2125 var a
= v
.array_instance
(array
, v
.object_type
)
2126 var res
= v
.send
(v
.get_property
("to_s", a
.mtype
), [a
])
2131 redef class ACrangeExpr
2134 var i1
= v
.expr
(self.n_expr
, null)
2135 var i2
= v
.expr
(self.n_expr2
, null)
2136 var res
= v
.init_instance
(self.mtype
.as(MClassType))
2137 var it
= v
.send
(v
.get_property
("init", res
.mtype
), [res
, i1
, i2
])
2138 v
.check_init_instance
(res
)
2143 redef class AOrangeExpr
2146 var i1
= v
.expr
(self.n_expr
, null)
2147 var i2
= v
.expr
(self.n_expr2
, null)
2148 var res
= v
.init_instance
(self.mtype
.as(MClassType))
2149 var it
= v
.send
(v
.get_property
("without_last", res
.mtype
), [res
, i1
, i2
])
2150 v
.check_init_instance
(res
)
2155 redef class ATrueExpr
2158 return v
.new_expr
("1", self.mtype
.as(not null))
2162 redef class AFalseExpr
2165 return v
.new_expr
("0", self.mtype
.as(not null))
2169 redef class ANullExpr
2172 var res
= v
.new_expr
("NULL", self.mtype
.as(not null))
2177 redef class AIsaExpr
2180 var i
= v
.expr
(self.n_expr
, null)
2181 return v
.type_test
(i
, self.cast_type
.as(not null))
2185 redef class AAsCastExpr
2188 var i
= v
.expr
(self.n_expr
, null)
2189 var cond
= v
.type_test
(i
, self.mtype
.as(not null))
2190 v
.add
("if (!{cond}) \{")
2191 v
.add_abort
("Cast failed")
2197 redef class AAsNotnullExpr
2200 var i
= v
.expr
(self.n_expr
, null)
2201 v
.add
("if ({i} == NULL) \{")
2202 v
.add_abort
("Cast failed")
2208 redef class AParExpr
2211 return v
.expr
(self.n_expr
, null)
2215 redef class AOnceExpr
2218 var mtype
= self.mtype
.as(not null)
2219 var name
= v
.get_name
("varonce")
2220 var guard
= v
.get_name
(name
+ "_guard")
2221 v
.add_decl
("static {mtype.ctype} {name};")
2222 v
.add_decl
("static int {guard};")
2223 var res
= v
.new_var
(mtype
)
2224 v
.add
("if ({guard}) \{")
2225 v
.add
("{res} = {name};")
2227 var i
= v
.expr
(self.n_expr
, mtype
)
2228 v
.add
("{res} = {i};")
2229 v
.add
("{name} = {res};")
2230 v
.add
("{guard} = 1;")
2236 redef class ASendExpr
2239 var recv
= v
.expr
(self.n_expr
, null)
2241 for a
in compute_raw_arguments
do
2242 args
.add
(v
.expr
(a
, null))
2244 var mproperty
= self.mproperty
.as(not null)
2245 return v
.send
(mproperty
, args
)
2249 redef class ASendReassignFormExpr
2252 var recv
= v
.expr
(self.n_expr
, null)
2254 for a
in compute_raw_arguments
do
2255 args
.add
(v
.expr
(a
, null))
2257 var value
= v
.expr
(self.n_value
, null)
2259 var mproperty
= self.mproperty
.as(not null)
2260 var left
= v
.send
(mproperty
, args
)
2263 var res
= v
.send
(reassign_property
.mproperty
, [left
, value
])
2267 v
.send
(self.write_mproperty
.as(not null), args
)
2271 redef class ASuperExpr
2274 var recv
= v
.frame
.arguments
.first
2276 for a
in self.n_args
.n_exprs
do
2277 args
.add
(v
.expr
(a
, null))
2279 if args
.length
== 1 then
2280 args
= v
.frame
.arguments
2283 var mproperty
= self.mproperty
2284 if mproperty
!= null then
2285 if mproperty
.intro
.msignature
.arity
== 0 then
2289 var res
= v
.send
(mproperty
, args
)
2293 # stantard call-next-method
2294 var mpropdef
= v
.frame
.mpropdef
2295 # FIXME: we do not want an ugly static call!
2296 var mpropdefs
= mpropdef
.mproperty
.lookup_super_definitions
(mpropdef
.mclassdef
.mmodule
, mpropdef
.mclassdef
.bound_mtype
)
2297 if mpropdefs
.length
!= 1 then
2298 debug
("MPRODFEFS for super {mpropdef} for {recv}: {mpropdefs.join(", ")}")
2300 mpropdef
= mpropdefs
.first
2301 assert mpropdef
isa MMethodDef
2302 var res
= v
.call
(mpropdef
, recv
.mtype
.as(MClassType), args
)
2307 redef class ANewExpr
2310 var mproperty
= self.mproperty
.as(not null)
2311 var mtype
= self.mtype
.as(MClassType)
2313 var ctype
= mtype
.ctype
2314 if ctype
== "val*" then
2315 recv
= v
.init_instance
(mtype
)
2316 else if ctype
== "void*" then
2317 recv
= v
.new_expr
("NULL/*special!*/", mtype
)
2319 debug
("cannot new {mtype}")
2323 for a
in self.n_args
.n_exprs
do
2324 args
.add
(v
.expr
(a
, null))
2326 var res2
= v
.send
(mproperty
, args
)
2327 if res2
!= null then
2328 #self.debug("got {res2} from {mproperty}. drop {recv}")
2331 v
.check_init_instance
(recv
)
2336 redef class AAttrExpr
2339 var recv
= v
.expr
(self.n_expr
, null)
2340 var mproperty
= self.mproperty
.as(not null)
2341 return v
.read_attribute
(mproperty
, recv
)
2345 redef class AAttrAssignExpr
2348 var recv
= v
.expr
(self.n_expr
, null)
2349 var i
= v
.expr
(self.n_value
, null)
2350 var mproperty
= self.mproperty
.as(not null)
2351 v
.write_attribute
(mproperty
, recv
, i
)
2355 redef class AAttrReassignExpr
2358 var recv
= v
.expr
(self.n_expr
, null)
2359 var value
= v
.expr
(self.n_value
, null)
2360 var mproperty
= self.mproperty
.as(not null)
2361 var attr
= v
.read_attribute
(mproperty
, recv
)
2362 var res
= v
.send
(reassign_property
.mproperty
, [attr
, value
])
2364 v
.write_attribute
(mproperty
, recv
, res
)
2368 redef class AIssetAttrExpr
2371 redef class ADebugTypeExpr