+
+# Handler for runtime classes generation
+#
+# We need 3 kinds of runtime structures:
+# * `RTClass` to represent a global class
+# * `RTMethod` to represent a method definition
+# * `RTVal` to represent runtime variables
+class JavaRuntimeModel
+
+ # Compile JavaRuntimeModel structures
+ fun compile_rtmodel(compiler: JavaCompiler) do
+ compile_rtclass(compiler)
+ compile_rtmethod(compiler)
+ compile_rtval(compiler)
+ end
+
+ # Compile the abstract runtime class structure
+ #
+ # Runtime classes have 3 attributes:
+ # * `class_name`: the class name as a String
+ # * `vft`: the virtual function table for the class (flattened)
+ # * `supers`: the super type table (used for type tests)
+ fun compile_rtclass(compiler: JavaCompiler) do
+ var v = compiler.new_visitor("RTClass.java")
+ v.add("import java.util.HashMap;")
+ v.add("public abstract class RTClass \{")
+ v.add(" public String class_name;")
+ v.add(" public HashMap<String, RTMethod> vft = new HashMap<>();")
+ v.add(" public HashMap<String, RTClass> supers = new HashMap<>();")
+ v.add(" protected RTClass() \{\}")
+ v.add("\}")
+ end
+
+ # Compile the abstract runtime method structure
+ #
+ # Method body is executed through the `exec` method:
+ # * `exec` always take an array of RTVal as arg, the first one must be the receiver
+ # * `exec` always returns a RTVal (or null if the Nit return type is void)
+ fun compile_rtmethod(compiler: JavaCompiler) do
+ var v = compiler.new_visitor("RTMethod.java")
+ v.add("public abstract class RTMethod \{")
+ v.add(" protected RTMethod() \{\}")
+ v.add(" public abstract RTVal exec(RTVal[] args);")
+ v.add("\}")
+ end
+
+ # Compile the runtime value structure
+ #
+ # RTVal both represents object instances and primitives values:
+ # * object instances:
+ # * `rtclass` the class of the RTVal is instance of
+ # * `attrs` contains the attributes of the instance
+ # * primitive values:
+ # * `rtclass` represents the class of the primitive value Nit type
+ # * `value` contains the primitive value of the instance
+ # * null values:
+ # * they must have both `rtclass` and `value` as null
+ fun compile_rtval(compiler: JavaCompiler) do
+ var v = compiler.new_visitor("RTVal.java")
+ v.add("import java.util.HashMap;")
+ v.add("public class RTVal \{")
+ v.add(" public RTClass rtclass;")
+ v.add(" public HashMap<String, RTVal> attrs = new HashMap<>();")
+ v.add(" Object value;")
+ v.add(" public RTVal(RTClass rtclass) \{")
+ v.add(" this.rtclass = rtclass;")
+ v.add(" \}")
+ v.add(" public RTVal(RTClass rtclass, Object value) \{")
+ v.add(" this.rtclass = rtclass;")
+ v.add(" this.value = value;")
+ v.add(" \}")
+ v.add(" public boolean is_null() \{ return rtclass == null && value == null; \}")
+ v.add("\}")
+ end
+end
+
+# A runtime variable hold a runtime value in Java.
+# Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
+class RuntimeVariable
+
+ # The name of the variable in the Java code
+ var name: String
+
+ # The static type of the variable (as declard in Java)
+ var mtype: MType
+
+ # The current casted type of the variable (as known in Nit)
+ var mcasttype: MType is writable
+
+ # If the variable exaclty a mcasttype?
+ # false (usual value) means that the variable is a mcasttype or a subtype.
+ var is_exact: Bool = false is writable
+
+ # Is this variable declared as a RTVal or a Java primitive one?
+ var is_boxed = false
+
+ redef fun to_s do return name
+
+ redef fun inspect
+ do
+ var exact_str
+ if self.is_exact then
+ exact_str = " exact"
+ else
+ exact_str = ""
+ end
+ var type_str
+ if self.mtype == self.mcasttype then
+ type_str = "{mtype}{exact_str}"
+ else
+ type_str = "{mtype}({mcasttype}{exact_str})"
+ end
+ return "<{name}:{type_str}>"
+ end
+end
+
+# The static context of a visited property in a `JavaCompilerVisitor`
+class JavaStaticFrame
+ # The associated visitor
+ var visitor: JavaCompilerVisitor
+
+ # The executed property.
+ # A Method in case of a call, an attribute in case of a default initialization.
+ var mpropdef: MPropDef
+
+ # The static type of the receiver
+ var receiver: MClassType
+
+ # Arguments of the method (the first is the receiver)
+ var arguments: Array[RuntimeVariable]
+
+ # The runtime_variable associated to the return (in a function)
+ var returnvar: nullable RuntimeVariable = null is writable
+
+ # The label at the end of the property
+ var returnlabel: nullable String = null is writable
+
+ # Labels associated to a each escapemarks.
+ # Because of inlinings, escape-marks must be associated to their context (the frame)
+ private var escapemark_names = new HashMap[EscapeMark, String]
+end
+
+redef class Location
+ # Return a shortened version of the location with `"{file}:{line_start}"`
+ fun short_location: String do
+ var file = self.file
+ if file == null then return "<no file>:{line_start}"
+ return "{file.filename.escape_to_c}:{line_start}"
+ end
+end
+
+redef class MType
+ # Return the Java type associated to a given Nit static type
+ fun java_type: String do return "RTVal"
+
+ # Is the associated Java type a primitive one?
+ #
+ # ENSURE `result == (java_type != "RTVal")`
+ var is_java_primitive: Bool is lazy do return java_type != "RTVal"
+end
+
+redef class MClassType
+
+ redef var java_type is lazy do
+ if mclass.name == "Int" then
+ return "int"
+ else if mclass.name == "Bool" then
+ return "boolean"
+ else if mclass.name == "Char" then
+ return "char"
+ else if mclass.name == "Float" then
+ return "double"
+ else if mclass.name == "Byte" then
+ return "byte"
+ else if mclass.name == "CString" then
+ return "String"
+ else if mclass.name == "NativeArray" then
+ return "Array"
+ end
+ return "RTVal"
+ end
+end
+
+redef class MClass
+
+ # Runtime name
+ private fun rt_name: String do return "RTClass_{intro_mmodule.jname}_{jname}"
+
+ # Generate a Java RTClass for a Nit MClass
+ fun compile_to_java(v: JavaCompilerVisitor) do
+ v.add("public class {rt_name} extends RTClass \{")
+ v.add(" protected static RTClass instance;")
+ v.add(" private {rt_name}() \{")
+ v.add(" this.class_name = \"{name}\";")
+ compile_vft(v)
+ compile_type_table(v)
+ v.add(" \}")
+ v.add(" public static RTClass get{rt_name}() \{")
+ v.add(" if(instance == null) \{")
+ v.add(" instance = new {rt_name}();")
+ v.add(" \}")
+ v.add(" return instance;")
+ v.add(" \}")
+ v.add("\}")
+ end
+
+ # Compile the virtual function table for the mclass
+ private fun compile_vft(v: JavaCompilerVisitor) do
+ # TODO handle generics
+ if mclass_type.need_anchor then return
+ var mclassdefs = mclass_type.collect_mclassdefs(v.compiler.mainmodule).to_a
+ v.compiler.mainmodule.linearize_mclassdefs(mclassdefs)
+
+ var mainmodule = v.compiler.mainmodule
+ for mclassdef in mclassdefs.reversed do
+ for mprop in mclassdef.intro_mproperties do
+ var mpropdef = mprop.lookup_first_definition(mainmodule, intro.bound_mtype)
+ if not mpropdef isa MMethodDef then continue
+ var rt_name = mpropdef.rt_name
+ v.add("this.vft.put(\"{mprop.full_name}\", {rt_name}.get{rt_name}());")
+
+ # fill super next definitions
+ while mpropdef.has_supercall do
+ var prefix = mpropdef.full_name
+ mpropdef = mpropdef.lookup_next_definition(mainmodule, intro.bound_mtype)
+ rt_name = mpropdef.rt_name
+ v.add("this.vft.put(\"{prefix}\", {rt_name}.get{rt_name}());")
+ end
+ end
+ end
+ end
+
+ # Compile the type table for the MClass
+ fun compile_type_table(v: JavaCompilerVisitor) do
+ for pclass in in_hierarchy(v.compiler.mainmodule).greaters do
+ if pclass == self then
+ v.add("supers.put(\"{pclass.jname}\", this);")
+ else
+ v.add("supers.put(\"{pclass.jname}\", {pclass.rt_name}.get{pclass.rt_name}());")
+ end
+ end
+ end
+end
+
+# Used as a common type between MMethod and MMethodDef for `table_send`
+private interface TableCallable
+end
+
+redef class MMethod
+ super TableCallable
+end
+
+redef class MMethodDef
+ super TableCallable
+
+ # Runtime name
+ private fun rt_name: String do
+ return "RTMethod_{mclassdef.mmodule.jname}_{mclassdef.mclass.jname}_{mproperty.jname}"
+ end
+
+ # Generate a Java RTMethod for `self`
+ fun compile_to_java(v: JavaCompilerVisitor) do
+ v.add("public class {rt_name} extends RTMethod \{")
+ v.add(" protected static RTMethod instance;")
+ v.add(" public static RTMethod get{rt_name}() \{")
+ v.add(" if(instance == null) \{")
+ v.add(" instance = new {rt_name}();")
+ v.add(" \}")
+ v.add(" return instance;")
+ v.add(" \}")
+ v.add(" @Override")
+ v.add(" public RTVal exec(RTVal[] args) \{")
+ compile_inside_to_java(v)
+ v.add(" \}")
+ v.add("\}")
+ end
+
+ # Compile the body of this function
+ fun compile_inside_to_java(v: JavaCompilerVisitor) do
+
+ var modelbuilder = v.compiler.modelbuilder
+ var node = modelbuilder.mpropdef2node(self)
+
+ var recv = mclassdef.bound_mtype
+ var arguments = new Array[RuntimeVariable]
+ var frame = new JavaStaticFrame(v, self, recv, arguments)
+ v.frame = frame
+
+ var selfvar = v.decl_var("self", recv)
+ arguments.add(selfvar)
+ var boxed = v.new_expr("args[0]", v.compiler.mainmodule.object_type)
+ v.add "{selfvar} = {v.autobox(boxed, recv)};"
+
+ var msignature = self.msignature
+ var ret = null
+ if msignature != null then
+ ret = msignature.return_mtype
+ if ret != null then
+ var retvar = v.decl_var("ret", ret)
+ if ret.name == "Int" then v.add "{retvar} = 0;"
+ if ret.name == "Float" then v.add "{retvar} = 0.0;"
+ if ret.name == "Bool" then v.add "{retvar} = false;"
+ if ret.name == "Char" then v.add "{retvar} = 0;"
+ if ret.name == "Byte" then v.add "{retvar} = 0;"
+ frame.returnvar = retvar
+ end
+ end
+ frame.returnlabel = v.get_name("RET_LABEL")
+
+ v.current_node = node
+ if is_abstract then
+ v.add_abort("Abstract method `{mproperty.name}` called on `\" + {selfvar}.rtclass.class_name +\"`")
+ v.add("return null;")
+ return
+ end
+ v.current_node = null
+
+ v.add("{frame.returnlabel.as(not null)}: \{")
+
+ if node isa APropdef then
+ node.compile_to_java(v, self, arguments)
+ else if node isa AClassdef then
+ node.compile_to_java(v, self, arguments)
+ else
+ abort
+ end
+
+ v.add("\}")
+ if ret != null then
+ v.add("return {v.autobox(frame.returnvar.as(not null), v.compiler.mainmodule.object_type)};")
+ else
+ v.add("return null;")
+ end
+ end
+end
+
+redef class AClassdef
+ private fun compile_to_java(v: JavaCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]) do
+ if mpropdef.mproperty.is_root_init then
+ if not mpropdef.is_intro then
+ v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
+ end
+ else
+ abort
+ end
+ end
+end
+
+redef class APropdef
+
+ # Compile that property definition to java code
+ fun compile_to_java(v: JavaCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]) do
+ v.info("NOT YET IMPLEMENTED {class_name}::compile_to_java")
+ end
+end
+
+redef class AMethPropdef
+ redef fun compile_to_java(v, mpropdef, arguments) do
+ if mpropdef.msignature != null then
+ var i = 0
+ for mparam in mpropdef.msignature.as(not null).mparameters do
+ var variable = n_signature.as(not null).n_params[i].variable
+ if variable == null then continue
+ var argvar = v.variable(variable)
+ v.assign(argvar, v.new_expr("args[{i + 1}]", v.compiler.mainmodule.object_type))
+ arguments.add(argvar)
+ i += 1
+ end
+ end
+
+ # Call the implicit super-init
+ var auto_super_inits = self.auto_super_inits
+ if auto_super_inits != null then
+ var args = [arguments.first]
+ for auto_super_init in auto_super_inits do
+ assert auto_super_init.mproperty != mpropdef.mproperty
+ args.clear
+ for i in [0..auto_super_init.msignature.arity+1[ do
+ args.add(arguments[i])
+ end
+ assert auto_super_init.mproperty != mpropdef.mproperty
+ v.compile_callsite(auto_super_init, args)
+ end
+ end
+ if auto_super_call then
+ v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
+ end
+
+ compile_inside_to_java(v, mpropdef, arguments)
+ end
+
+ # Compile the inside of the method body
+ private fun compile_inside_to_java(v: JavaCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]) do
+ # Compile intern methods
+ if mpropdef.is_intern then
+ if compile_intern_to_java(v, mpropdef, arguments) then return
+ v.info("NOT YET IMPLEMENTED compile_intern for {mpropdef}")
+ v.ret(v.null_instance)
+ return
+ end
+
+ # Compile block if any
+ var n_block = n_block
+ if n_block != null then
+ v.stmt(n_block)
+ return
+ end
+ end
+
+ # Compile an intern method using Java primitives
+ fun compile_intern_to_java(v: JavaCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable]): Bool do
+ var pname = mpropdef.mproperty.name
+ var cname = mpropdef.mclassdef.mclass.name
+ var ret = mpropdef.msignature.as(not null).return_mtype
+ if cname == "Int" then
+ if pname == "output" then
+ v.add("System.out.println({arguments[0]});")
+ v.ret(v.null_instance)
+ return true
+ else if pname == "object_id" then
+ v.ret(arguments.first)
+ return true
+ else if pname == "+" then
+ v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "-" then
+ v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "unary -" then
+ v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "unary +" then
+ v.ret(arguments[0])
+ return true
+ else if pname == "*" then
+ v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "/" then
+ v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "%" then
+ v.ret(v.new_expr("{arguments[0]} % {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "<<" then
+ v.ret(v.new_expr("{arguments[0]} << {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">>" then
+ v.ret(v.new_expr("{arguments[0]} >> {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
+ else if pname == "<" then
+ v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">" then
+ v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "<=" then
+ v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">=" then
+ v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "to_f" then
+ v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "to_b" then
+ v.ret(v.new_expr("(byte){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "ascii" then
+ v.ret(v.new_expr("(char){arguments[0]}", ret.as(not null)))
+ return true
+ end
+ else if cname == "Char" then
+ if pname == "output" then
+ v.add("System.out.print({arguments[0]});")
+ v.ret(v.null_instance)
+ return true
+ else if pname == "object_id" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "successor" then
+ v.ret(v.new_expr("(char)({arguments[0]} + {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "predecessor" then
+ v.ret(v.new_expr("(char)({arguments[0]} - {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
+ else if pname == "<" then
+ v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">" then
+ v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "<=" then
+ v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">=" then
+ v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "to_i" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "ascii" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ end
+ else if cname == "Byte" then
+ if pname == "output" then
+ v.add("System.out.println({arguments[0]});")
+ v.ret(v.null_instance)
+ return true
+ else if pname == "object_id" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "+" then
+ v.ret(v.new_expr("(byte)({arguments[0]} + {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "-" then
+ v.ret(v.new_expr("(byte)({arguments[0]} - {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "unary -" then
+ v.ret(v.new_expr("(byte)(-{arguments[0]})", ret.as(not null)))
+ return true
+ else if pname == "unary +" then
+ v.ret(arguments[0])
+ return true
+ else if pname == "*" then
+ v.ret(v.new_expr("(byte)({arguments[0]} * {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "/" then
+ v.ret(v.new_expr("(byte)({arguments[0]} / {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "%" then
+ v.ret(v.new_expr("(byte)({arguments[0]} % {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "<<" then
+ v.ret(v.new_expr("(byte)({arguments[0]} << {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == ">>" then
+ v.ret(v.new_expr("(byte)({arguments[0]} >> {arguments[1]})", ret.as(not null)))
+ return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
+ else if pname == "<" then
+ v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">" then
+ v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "<=" then
+ v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">=" then
+ v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "to_i" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "to_f" then
+ v.ret(v.new_expr("(double){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "ascii" then
+ v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
+ return true
+ end
+ else if cname == "Bool" then
+ if pname == "output" then
+ v.add("System.out.println({arguments[0]});")
+ v.ret(v.null_instance)
+ return true
+ else if pname == "object_id" then
+ v.ret(v.new_expr("{arguments[0]}?1:0", ret.as(not null)))
+ return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
+ end
+ else if cname == "Float" then
+ if pname == "output" then
+ v.add "if({arguments[0]} == Double.POSITIVE_INFINITY) \{"
+ v.add "System.out.println(\"inf\");"
+ v.add "\} else if({arguments[0]} == Double.POSITIVE_INFINITY) \{"
+ v.add "System.out.println(\"-inf\");"
+ v.add "\} else \{"
+ var df = v.get_name("df")
+ v.add "java.text.DecimalFormat {df} = new java.text.DecimalFormat(\"0.000000\");"
+ v.add "System.out.println({df}.format({arguments[0]}));"
+ v.add "\}"
+ v.ret(v.null_instance)
+ return true
+ else if pname == "object_id" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "+" then
+ v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "-" then
+ v.ret(v.new_expr("{arguments[0]} - {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "unary -" then
+ v.ret(v.new_expr("-{arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "unary +" then
+ v.ret(arguments[0])
+ return true
+ else if pname == "succ" then
+ v.ret(v.new_expr("{arguments[0]} + 1", ret.as(not null)))
+ return true
+ else if pname == "prec" then
+ v.ret(v.new_expr("{arguments[0]} - 1", ret.as(not null)))
+ return true
+ else if pname == "*" then
+ v.ret(v.new_expr("{arguments[0]} * {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "/" then
+ v.ret(v.new_expr("{arguments[0]} / {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "==" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "!=" then
+ var res = v.equal_test(arguments[0], arguments[1])
+ v.ret(v.new_expr("!{res}", ret.as(not null)))
+ return true
+ else if pname == "<" then
+ v.ret(v.new_expr("{arguments[0]} < {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">" then
+ v.ret(v.new_expr("{arguments[0]} > {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "<=" then
+ v.ret(v.new_expr("{arguments[0]} <= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == ">=" then
+ v.ret(v.new_expr("{arguments[0]} >= {arguments[1]}", ret.as(not null)))
+ return true
+ else if pname == "to_i" then
+ v.ret(v.new_expr("(int){arguments[0]}", ret.as(not null)))
+ return true
+ else if pname == "to_b" then
+ v.ret(v.new_expr("(byte){arguments[0]}", ret.as(not null)))
+ return true
+ end
+ end
+ if pname == "exit" then
+ v.add("System.exit({arguments[1]});")
+ v.ret(v.null_instance)
+ return true
+ else if pname == "sys" then
+ # TODO singleton
+ var main_type = v.compiler.mainmodule.sys_type.as(not null)
+ var sys = main_type.mclass
+ v.ret(v.new_expr("new RTVal({sys.rt_name}.get{sys.rt_name}())", main_type))
+ return true
+ else if pname == "object_id" then
+ v.ret(v.new_expr("{arguments[0]}.hashCode()", ret.as(not null)))
+ return true
+ else if pname == "is_same_type" then
+ v.ret(v.is_same_type_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "is_same_instance" then
+ v.ret(v.equal_test(arguments[0], arguments[1]))
+ return true
+ else if pname == "output_class_name" then
+ v.add("System.out.println({arguments[0]}.rtclass.class_name);")
+ v.ret(v.null_instance)
+ return true
+ end
+ return false
+ end
+end
+
+redef class AAttrPropdef
+ redef fun compile_to_java(v, mpropdef, arguments) do
+ v.current_node = self
+ if mpropdef == mreadpropdef then
+ compile_getter(v, mpropdef, arguments)
+ else if mpropdef == mwritepropdef then
+ compile_setter(v, mpropdef, arguments)
+ else
+ abort
+ end
+ v.current_node = null
+ end
+
+ # Compile the setter method
+ private fun compile_setter(v: JavaCompilerVisitor, mpropdef: MPropDef, arguments: Array[RuntimeVariable]) do
+ var mtype = v.compiler.mainmodule.object_type
+ var recv = arguments.first
+ var val = v.new_expr("args[1]", mtype)
+ v.write_attribute(self.mpropdef.as(not null).mproperty, recv, val)
+ v.ret v.null_instance
+ end
+
+ # Compile the getter method
+ private fun compile_getter(v: JavaCompilerVisitor, mpropdef: MPropDef, arguments: Array[RuntimeVariable]) do
+ var recv = arguments.first
+ v.ret v.read_attribute(self.mpropdef.as(not null).mproperty, recv)
+ end
+
+ private fun init_expr(v: JavaCompilerVisitor, recv: RuntimeVariable) do
+ if has_value and not is_lazy and not n_expr isa ANullExpr then evaluate_expr(v, recv)
+ end
+
+ # Evaluate, store and return the default value of the attribute
+ private fun evaluate_expr(v: JavaCompilerVisitor, recv: RuntimeVariable): RuntimeVariable do
+ var old = v.frame
+ var frame = new JavaStaticFrame(v, self.mreadpropdef.as(not null), recv.mcasttype.undecorate.as(MClassType), [recv])
+ v.frame = frame
+
+ var value
+ var mtype = self.mtype
+ assert mtype != null
+
+ var nexpr = self.n_expr
+ var nblock = self.n_block
+ if nexpr != null then
+ value = v.expr(nexpr, mtype)
+ else if nblock != null then
+ value = v.new_var(mtype)
+ frame.returnvar = value
+ frame.returnlabel = v.get_name("RET_LABEL")
+ v.add("{frame.returnlabel.as(not null)}: \{")
+ v.stmt(nblock)
+ v.add("\}")
+ else
+ abort
+ end
+
+ v.write_attribute(self.mpropdef.as(not null).mproperty, recv, value)
+ v.frame = old
+ return value
+ end
+end
+
+redef class AExpr
+ # Try to compile self as an expression
+ # Do not call this method directly, use `v.expr` instead
+ private fun expr(v: JavaCompilerVisitor): nullable RuntimeVariable do
+ v.info("NOT YET IMPLEMENTED {class_name}::expr")
+ return null
+ end
+
+ # Try to compile self as a statement
+ # Do not call this method directly, use `v.stmt` instead
+ private fun stmt(v: JavaCompilerVisitor) do expr(v)
+end
+
+redef class ABlockExpr
+ redef fun stmt(v)
+ do
+ for e in self.n_expr do v.stmt(e)
+ end
+ redef fun expr(v)
+ do
+ var last = self.n_expr.last
+ for e in self.n_expr do
+ if e == last then break
+ v.stmt(e)
+ end
+ return v.expr(last, null)
+ end
+end
+
+redef class ASendExpr
+ redef fun expr(v) do
+ var recv = v.expr(n_expr, null)
+ var callsite = callsite.as(not null)
+ var args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, raw_arguments)
+ return v.compile_callsite(callsite, args)
+ end
+end
+
+redef class ANewExpr
+ redef fun expr(v)
+ do
+ var mtype = self.recvtype
+ assert mtype != null
+
+ if mtype.mclass.name == "NativeArray" then
+ # TODO handle native arrays
+ v.info("NOT YET IMPLEMENTED new NativeArray")
+ end
+
+ var recv = v.init_instance(mtype)
+
+ var callsite = self.callsite
+ if callsite == null then return recv
+
+ var args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, self.n_args.n_exprs)
+ var res2 = v.compile_callsite(callsite, args)
+ if res2 != null then
+ return res2
+ end
+ return recv
+ end
+end
+
+redef class ASuperExpr
+ redef fun expr(v)
+ do
+ var frame = v.frame
+ assert frame != null
+ var recv = frame.arguments.first
+
+ var callsite = self.callsite
+ if callsite != null then
+ var args
+
+ if self.n_args.n_exprs.is_empty then
+ # Add automatic arguments for the super init call
+ args = [recv]
+ for i in [0..callsite.msignature.arity[ do
+ args.add(frame.arguments[i+1])
+ end
+ else
+ args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, self.n_args.n_exprs)
+ end
+
+ # Super init call
+ var res = v.compile_callsite(callsite, args)
+ return res
+ end
+
+ var mpropdef = self.mpropdef.as(not null)
+
+ var args
+ if self.n_args.n_exprs.is_empty then
+ args = frame.arguments
+ else
+ args = v.varargize(mpropdef, signaturemap, recv, self.n_args.n_exprs)
+ end
+
+ # Standard call-next-method
+ return v.supercall(mpropdef, recv.mtype.as(MClassType), args)
+ end
+end
+
+redef class ASelfExpr
+ redef fun expr(v) do return v.frame.as(not null).arguments.first
+end
+
+redef class AImplicitSelfExpr
+ redef fun expr(v) do return v.frame.as(not null).arguments.first
+end
+
+redef class AAttrExpr
+ redef fun expr(v) do
+ var recv = v.expr(self.n_expr, null)
+ var mproperty = self.mproperty.as(not null)
+ return v.read_attribute(mproperty, recv)
+ end
+end
+
+redef class AAttrAssignExpr
+ redef fun expr(v) do
+ var recv = v.expr(self.n_expr, null)
+ var i = v.expr(self.n_value, null)
+ var mproperty = self.mproperty.as(not null)
+ v.write_attribute(mproperty, recv, i)
+ return i
+ end
+end
+
+redef class AAttrReassignExpr
+ redef fun stmt(v) do
+ var recv = v.expr(self.n_expr, null)
+ var value = v.expr(self.n_value, null)
+ var mproperty = self.mproperty.as(not null)
+ var attr = v.read_attribute(mproperty, recv)
+ var res = v.compile_callsite(self.reassign_callsite.as(not null), [attr, value])
+ assert res != null
+ v.write_attribute(mproperty, recv, res)
+ end
+end
+
+redef class AIssetAttrExpr
+ redef fun expr(v) do
+ var recv = v.expr(self.n_expr, null)
+ var mproperty = self.mproperty.as(not null)
+ return v.isset_attribute(mproperty, recv)
+ end
+end
+
+redef class AReturnExpr
+ redef fun stmt(v) do
+ var nexpr = self.n_expr
+ var frame = v.frame
+ assert frame != null
+ if nexpr != null then
+ v.ret(v.expr(nexpr, frame.returnvar.as(not null).mtype))
+ else
+ v.ret(v.null_instance)
+ end
+ end
+end
+
+redef class AIfExpr
+ redef fun stmt(v) do
+ var cond = v.expr_bool(self.n_expr)
+ v.add("if ({cond})\{")
+ v.stmt(self.n_then)
+ v.add("\} else \{")
+ v.stmt(self.n_else)
+ v.add("\}")
+ end
+
+ redef fun expr(v) do
+ var res = v.new_var(self.mtype.as(not null))
+ var cond = v.expr_bool(self.n_expr)
+ v.add("if ({cond})\{")
+ v.assign(res, v.expr(self.n_then.as(not null), null))
+ v.add("\} else \{")
+ v.assign(res, v.expr(self.n_else.as(not null), null))
+ v.add("\}")
+ return res
+ end
+end
+
+redef class ADoExpr
+ redef fun stmt(v)
+ do
+ v.add_escape_label(break_mark)
+ v.add "\{"
+ v.stmt(self.n_block)
+ v.add "\}"
+ end
+end
+
+redef class AWhileExpr
+ redef fun stmt(v)
+ do
+ v.add_escape_label(break_mark)
+ v.add_escape_label(continue_mark)
+ v.add("for(;;) \{")
+ var cond = v.expr_bool(self.n_expr)
+ v.add("if (!{cond}) break;")
+ v.stmt(self.n_block)
+ v.add("\}")
+ end
+end
+
+redef class ALoopExpr
+ redef fun stmt(v)
+ do
+ v.add_escape_label(break_mark)
+ v.add_escape_label(continue_mark)
+ v.add("for(;;) \{")
+ v.stmt(self.n_block)
+ v.add("\}")
+ end
+end
+
+redef class AEscapeExpr
+ redef fun stmt(v) do v.add("break BREAK_{v.escapemark_name(escapemark)};")
+end
+
+redef class AVardeclExpr
+ redef fun stmt(v) do
+ var variable = self.variable.as(not null)
+ var ne = self.n_expr
+ var decl = v.variable(variable)
+ if ne != null then
+ var i = v.expr(ne, variable.declared_type)
+ v.assign(decl, i)
+ end
+ end
+end
+
+redef class AVarExpr
+ redef fun expr(v) do
+ return v.variable(self.variable.as(not null))
+ end
+end
+
+redef class AVarAssignExpr
+ redef fun expr(v) do
+ var variable = self.variable.as(not null)
+ var i = v.expr(self.n_value, variable.declared_type)
+ v.assign(v.variable(variable), i)
+ return i
+ end
+end
+
+
+redef class AAssertExpr
+ redef fun stmt(v) do
+ var cond = v.expr_bool(self.n_expr)
+ v.add("if (!{cond}) \{")
+ v.stmt(self.n_else)
+ var nid = self.n_id
+ if nid != null then
+ v.add_abort("Assert '{nid.text}' failed")
+ else
+ v.add_abort("Assert failed")
+ end
+ v.add("\}")
+ end
+end
+
+redef class AImpliesExpr
+ redef fun expr(v) do
+ var res = v.new_var(mtype.as(not null))
+ var i1 = v.expr_bool(n_expr)
+ v.add("if (!{i1}) \{")
+ v.add("{res} = true;")
+ v.add("\} else \{")
+ var i2 = v.expr_bool(n_expr2)
+ v.add("{res} = {i2};")
+ v.add("\}")
+ return res
+ end
+end
+
+redef class AOrElseExpr
+ redef fun expr(v)
+ do
+ var res = v.new_var(self.mtype.as(not null))
+ var i1 = v.expr(self.n_expr, null)
+ v.add("if ({i1} != null && !{i1}.is_null()) \{")
+ v.assign(res, i1)
+ v.add("\} else \{")
+ var i2 = v.expr(self.n_expr2, null)
+ v.assign(res, i2)
+ v.add("\}")
+ return res
+ end
+end
+
+redef class AOrExpr
+ redef fun expr(v) do
+ var res = v.new_var(self.mtype.as(not null))
+ var i1 = v.expr_bool(self.n_expr)
+ v.add("if ({i1}) \{")
+ v.add("{res} = true;")
+ v.add("\} else \{")
+ var i2 = v.expr_bool(self.n_expr2)
+ v.add("{res} = {i2};")
+ v.add("\}")
+ return res
+ end
+end
+
+redef class AAndExpr
+ redef fun expr(v) do
+ var res = v.new_var(self.mtype.as(not null))
+ var i1 = v.expr_bool(self.n_expr)
+ v.add("if (!{i1}) \{")
+ v.add("{res} = false;")
+ v.add("\} else \{")
+ var i2 = v.expr_bool(self.n_expr2)
+ v.add("{res} = {i2};")
+ v.add("\}")
+ return res
+ end
+end
+
+redef class ANotExpr
+ redef fun expr(v) do
+ var cond = v.expr_bool(self.n_expr)
+ return v.new_expr("!{cond}", self.mtype.as(not null))
+ end
+end
+
+redef class AIntegerExpr
+ redef fun expr(v) do
+ if value isa Int then
+ return v.int_instance(self.value.as(Int))
+ else if value isa Byte then
+ return v.byte_instance(self.value.as(Byte))
+ else
+ # Should not happen
+ abort
+ end
+ end
+end
+
+redef class AFloatExpr
+ redef fun expr(v) do return v.float_instance("{self.n_float.text}") # FIXME use value, not n_float
+end
+
+redef class ACharExpr
+ redef fun expr(v) do return v.char_instance(self.value.as(not null))
+end
+
+redef class ATrueExpr
+ redef fun expr(v) do return v.bool_instance(true)
+end
+
+redef class AFalseExpr
+ redef fun expr(v) do return v.bool_instance(false)
+end
+
+redef class ANullExpr
+ redef fun expr(v) do return v.null_instance
+end
+
+redef class AAsCastExpr
+ redef fun expr(v)
+ do
+ var i = v.expr(n_expr, null)
+ v.add_cast(i, mtype.as(not null))
+ return i
+ end
+end
+
+redef class AAsNotnullExpr
+ redef fun expr(v) do
+ var i = v.expr(n_expr, null)
+ if i.mtype.is_java_primitive then return i
+
+ v.add("if ({i} == null || {i}.is_null()) \{")
+ v.add_abort("Cast failed")
+ v.add("\}")
+ return i
+ end
+end
+
+redef class AIsaExpr
+ redef fun expr(v)
+ do
+ var i = v.expr(self.n_expr, null)
+ var cast_type = self.cast_type
+ if cast_type == null then return null # no-no on broken node
+ return v.type_test(i, cast_type)
+ end
+end
+
+redef class AParExpr
+ redef fun expr(v) do return v.expr(self.n_expr, null)
+end
+
+redef class AAbortExpr
+ redef fun stmt(v) do v.add_abort("Aborted")
+end
+
+redef class ADebugTypeExpr
+ redef fun stmt(v) do end # do nothing
+end