- redef fun hash
- # used in the compiler work-list
- do
- var res = self.mmethoddef.hash + self.recv.hash
- return res
- end
-
- redef fun to_s
- do
- if self.mmethoddef.mclassdef.bound_mtype == self.recv then
- return self.mmethoddef.to_s
- else
- return "{self.mmethoddef}@{self.recv}"
- end
- end
-
- # compile the code customized for the reciever
- redef fun compile_to_c(compiler)
- do
- var recv = self.recv
- var mmethoddef = self.mmethoddef
- if not recv.is_subtype(compiler.mainmodule, null, mmethoddef.mclassdef.bound_mtype) then
- print("problem: why do we compile {self} for {recv}?")
- abort
- end
-
- var v = new GlobalCompilerVisitor(compiler)
- var selfvar = new RuntimeVariable("self", recv, recv)
- if compiler.runtime_type_analysis.live_types.has(recv) then
- selfvar.is_exact = true
- end
- var arguments = new Array[RuntimeVariable]
- var frame = new Frame(v, mmethoddef, recv, arguments)
- v.frame = frame
-
- var sig = new Buffer
- var comment = new Buffer
- var ret = mmethoddef.msignature.return_mtype
- if ret != null then
- ret = v.resolve_for(ret, selfvar)
- sig.append("{ret.ctype} ")
- else if mmethoddef.mproperty.is_new then
- ret = recv
- sig.append("{ret.ctype} ")
- else
- sig.append("void ")
- end
- sig.append(self.c_name)
- sig.append("({recv.ctype} self")
- comment.append("(self: {recv}")
- arguments.add(selfvar)
- for i in [0..mmethoddef.msignature.arity[ do
- var mtype = mmethoddef.msignature.mparameters[i].mtype
- if i == mmethoddef.msignature.vararg_rank then
- mtype = v.get_class("Array").get_mtype([mtype])
- end
- mtype = v.resolve_for(mtype, selfvar)
- comment.append(", {mtype}")
- sig.append(", {mtype.ctype} p{i}")
- var argvar = new RuntimeVariable("p{i}", mtype, mtype)
- arguments.add(argvar)
- end
- sig.append(")")
- comment.append(")")
- if ret != null then
- comment.append(": {ret}")
- end
- compiler.header.add_decl("{sig};")
-
- v.add_decl("/* method {self} for {comment} */")
- v.add_decl("{sig} \{")
- #v.add("printf(\"method {self} for {comment}\\n\");")
- if ret != null then
- frame.returnvar = v.new_var(ret)
- end
- frame.returnlabel = v.get_name("RET_LABEL")
-
- mmethoddef.compile_inside_to_c(v, arguments)
-
- v.add("{frame.returnlabel.as(not null)}:;")
- if ret != null then
- v.add("return {frame.returnvar.as(not null)};")
- end
- v.add("\}")
- end
-
- redef fun call(v: GlobalCompilerVisitor, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
- do
- var ret = self.mmethoddef.msignature.return_mtype
- if self.mmethoddef.mproperty.is_new then
- ret = recv
- end
- if ret != null then
- ret = v.resolve_for(ret, arguments.first)
- end
- if self.mmethoddef.can_inline(v) then
- var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
- frame.returnlabel = v.get_name("RET_LABEL")
- if ret != null then
- frame.returnvar = v.new_var(ret)
- end
- var old_frame = v.frame
- v.frame = frame
- v.add("\{ /* Inline {self} ({arguments.join(",")}) */")
- self.mmethoddef.compile_inside_to_c(v, arguments)
- v.add("{frame.returnlabel.as(not null)}:(void)0;")
- v.add("\}")
- v.frame = old_frame
- return frame.returnvar
- end
- v.adapt_signature(self.mmethoddef, arguments)
- v.compiler.todo(self)
- if ret == null then
- v.add("{self.c_name}({arguments.join(",")});")
- return null
- else
- var res = v.new_var(ret)
- v.add("{res} = {self.c_name}({arguments.join(",")});")
- return res
- end
- end
-end
-
-# A runtime variable hold a runtime value in C.
-# Runtime variables are associated to Nit local variables and intermediate results in Nit expressions.
-#
-# The tricky point is that a single C variable can be associated to more than one RuntimeVariable because the static knowledge of the type of an expression can vary in the C code.
-class RuntimeVariable
- # The name of the variable in the C code
- var name: String
-
- # The static type of the variable (as declard in C)
- var mtype: MType
-
- # The current casted type of the variable (as known in Nit)
- var mcasttype: MType writable
-
- # If the variable exaclty a mcasttype?
- # false (usual value) means that the variable is a mcasttype or a subtype.
- var is_exact: Bool writable = false
-
- init(name: String, mtype: MType, mcasttype: MType)
- do
- self.name = name
- self.mtype = mtype
- self.mcasttype = mcasttype
- assert not mtype.need_anchor
- assert not mcasttype.need_anchor
- end
-
- 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
-
-# A visitor on the AST of property definition that generate the C code.
-# Because of inlining, a visitor can visit more than one property.
-class GlobalCompilerVisitor
- # The associated compiler
- var compiler: GlobalCompiler
-
- init(compiler: GlobalCompiler)
- do
- self.compiler = compiler
- compiler.visitors.add(self)
- end
-
- # Alias for self.compiler.mainmodule.object_type
- fun object_type: MClassType do return self.compiler.mainmodule.object_type
-
- # Alias for self.compiler.mainmodule.bool_type
- fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
-
- # Force to get the primitive class named `name' or abort
- fun get_class(name: String): MClass
- do
- return self.compiler.mainmodule.get_primitive_class(name)
- end
-
- # Force to get the primitive property named `name' in the instance `recv' or abort
- fun get_property(name: String, recv: MType): MMethod
- do
- return self.compiler.modelbuilder.force_get_primitive_method(self.current_node.as(not null), name, recv, self.compiler.mainmodule)
- end
-
- # The current Frame
- var frame: nullable Frame writable
-
- # Anchor a type to the main module and the current receiver
- fun anchor(mtype: MType): MType
- do
- if not mtype.need_anchor then return mtype
- #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
- return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
- end
-
- # Add a line in the main part of the generated C
- fun add(s: String)
- do
- self.lines.add(s)
- end
-
- # Add a line in the
- # (used for local or global declaration)
- fun add_decl(s: String)
- do
- self.decl_lines.add(s)
- end
-
- private var lines: List[String] = new List[String]
- private var decl_lines: List[String] = new List[String]
-
- # The current visited AST node
- var current_node: nullable AExpr = null
-
- # Compile an expression an return its result
- # `mtype` is the expected return type, pass null if no specific type is expected.
- fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
- do
- var old = self.current_node
- self.current_node = nexpr
- var res = nexpr.expr(self).as(not null)
- if mtype != null then
- mtype = self.anchor(mtype)
- res = self.autobox(res, mtype)
- end
- self.current_node = old
- return res
- end
-
- # Unsafely cast a value to a new type
- # ie the result share the same C variable but my have a different mcasttype
- # NOTE: if the adaptation is useless then `value' is returned as it.
- # ENSURE: return.name == value.name
- fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
- do
- mtype = self.anchor(mtype)
- var valmtype = value.mcasttype
- if valmtype.is_subtype(self.compiler.mainmodule, null, mtype) then
- return value
- end
-
- if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
- var res = new RuntimeVariable(value.name, valmtype, valmtype.mtype)
- return res
- else
- var res = new RuntimeVariable(value.name, valmtype, mtype)
- return res
- end
- end
-
- # Box or unbox a value to another type iff a C type conversion is needed
- # ENSURE: result.mtype.ctype == mtype.ctype
- fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable
- do
- if value.mtype.ctype == mtype.ctype then
- return value
- else if value.mtype.ctype == "val*" then
- return self.new_expr("((struct {mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
- else if mtype.ctype == "val*" then
- var valtype = value.mtype.as(MClassType)
- var res = self.new_var(mtype)
- if not compiler.runtime_type_analysis.live_types.has(valtype) then
- self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
- self.add("printf(\"Dead code executed!\\n\"); exit(1);")
- return res
- end
- self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
- return res
- else
- # Bad things will appen!
- var res = self.new_var(mtype)
- self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
- self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
- return res
- end
- end
-
- # Correctly assign a left and a right value
- # Boxing and unboxing is performed if required
- fun assign(left, right: RuntimeVariable)
- do
- right = self.autobox(right, left.mtype)
- self.add("{left} = {right};")
- end
-
-
- # Alias for `self.expr(nexpr, self.bool_type)'
- fun expr_bool(nexpr: AExpr): RuntimeVariable
- do
- return expr(nexpr, bool_type)
- end
-
- # Compile a statement (if any)
- fun stmt(nexpr: nullable AExpr)
- do
- if nexpr == null then return
- var old = self.current_node
- self.current_node = nexpr
- nexpr.stmt(self)
- self.current_node = old
- end
-
- # Safely show a debug message on the current node and repeat the message in the C code as a comment
- fun debug(message: String)
- do
- var node = self.current_node
- if node == null then
- print "?: {message}"
- else
- node.debug(message)
- end
- self.add("/* DEBUG: {message} */")
- end
-
- # Return a new uninitialized local runtime_variable
- fun new_var(mtype: MType): RuntimeVariable
- do
- mtype = self.anchor(mtype)
- var name = self.get_name("var")
- var res = new RuntimeVariable(name, mtype, mtype)
- self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
- return res
- end
-
- # Return a new local runtime_variable initialized with the C expression `cexpr'.
- fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
- do
- var res = new_var(mtype)
- self.add("{res} = {cexpr};")
- return res
- end
-
- # Return the local runtime_variable associated to a Nit local variable
- fun variable(variable: Variable): RuntimeVariable
- do
- if self.variables.has_key(variable) then
- return self.variables[variable]
- else
- var name = self.get_name("var_{variable.name}")
- var mtype = variable.declared_type.as(not null)
- mtype = self.anchor(mtype)
- var res = new RuntimeVariable(name, mtype, mtype)
- self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
- self.variables[variable] = res
- return res
- end
- end
-
- private var variables: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
-
- # Return an unique and stable identifier associated with an escapemark
- fun escapemark_name(e: nullable EscapeMark): String
- do
- assert e != null
- if escapemark_names.has_key(e) then return escapemark_names[e]
- var name = e.name
- if name == null then name = "label"
- name = get_name(name)
- escapemark_names[e] = name
- return name
- end
-
- private var escapemark_names = new HashMap[EscapeMark, String]
-
- # Return a new name based on `s' and unique in the visitor
- fun get_name(s: String): String
- do
- if not self.names.has(s) then
- self.names.add(s)
- return s
- end
- var i = self.last + 1
- loop
- var s2 = s + i.to_s
- if not self.names.has(s2) then
- self.last = i
- self.names.add(s2)
- return s2
- end
- i = i + 1
- end
- end
-
- private var last: Int = 0
-
- private var names: HashSet[String] = new HashSet[String]
-
- # Generate a return with the value `s'
- fun ret(s: RuntimeVariable)
- do
- self.assign(self.frame.returnvar.as(not null), s)
- self.add("goto {self.frame.returnlabel.as(not null)};")
- end
-
- # The runtime types that are acceptable for a given receiver.
- fun collect_types(recv: RuntimeVariable): Array[MClassType]
- do
- var mtype = recv.mcasttype
- if recv.is_exact then
- assert mtype isa MClassType
- assert self.compiler.runtime_type_analysis.live_types.has(mtype)
- var types = [mtype]
- return types
- end
- var cache = self.compiler.collect_types_cache
- if cache.has_key(mtype) then
- return cache[mtype]
- end
- var types = new Array[MClassType]
- var mainmodule = self.compiler.mainmodule
- for t in self.compiler.runtime_type_analysis.live_types do
- if not t.is_subtype(mainmodule, null, mtype) then continue
- types.add(t)
- end
- cache[mtype] = types
- return types
- end
-
- fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
- do
- if not mtype.need_anchor then return mtype
- #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
- var res = mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
- return res
- end
-
- # Generate a polymorphic send for the method `m' and the arguments `args'
- fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable
- do
- var types = self.collect_types(args.first)
-
- var res: nullable RuntimeVariable
- var ret = m.intro.msignature.return_mtype
- if m.is_new then
- ret = args.first.mtype
- res = self.new_var(ret)
- else if ret == null then
- res = null
- else
- ret = self.resolve_for(ret, args.first)
- res = self.new_var(ret)
- end
-
- if types.is_empty then
- self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
- return res
- end
- self.add("/* send {m} on {args.first.inspect} */")
- if args.first.mtype.ctype != "val*" then
- var propdefs = m.lookup_definitions(self.compiler.mainmodule, args.first.mtype)
- if propdefs.length == 0 then
- self.add("/* skip, no method {m} */")
- return res
- end
- assert propdefs.length == 1
- var propdef = propdefs.first
- var res2 = self.call(propdef, args.first.mtype.as(MClassType), args)
- if res != null then self.assign(res, res2.as(not null))
- return res
- end
- if args.first.mcasttype isa MNullableType then
- # The reciever is potentially null, so we have to 3 cases: ==, != or NullPointerException
- self.add("if ({args.first} == NULL) \{ /* Special null case */")
- if m.name == "==" then
- assert res != null
- if args[1].mcasttype isa MNullableType then
- self.add("{res} = ({args[1]} == NULL);")
- else if args[1].mcasttype isa MNullType then
- self.add("{res} = 1; /* is null */")
- else
- self.add("{res} = 0; /* {args[1].inspect} cannot be null */")
- end
- else if m.name == "!=" then
- assert res != null
- if args[1].mcasttype isa MNullableType then
- self.add("{res} = ({args[1]} != NULL);")
- else if args[1].mcasttype isa MNullType then
- self.add("{res} = 0; /* is null */")
- else
- self.add("{res} = 1; /* {args[1].inspect} cannot be null */")
- end
- else
- self.add_abort("Reciever is null")
- end
- self.add "\} else"
- end
- self.add("switch({args.first}->classid) \{")
- var last = types.last
- var defaultpropdef: nullable MMethodDef = null
- for t in types do
- var propdefs = m.lookup_definitions(self.compiler.mainmodule, t)
- if propdefs.length == 0 then
- self.add("/* skip {t}, no method {m} */")
- continue
- end
- if propdefs.length > 1 then
- self.debug("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
- end
- var propdef = propdefs.first
- if propdef.mclassdef.mclass.name == "Object" and t.ctype == "val*" then
- defaultpropdef = propdef
- continue
- end
- if not self.compiler.hardening and t == last and defaultpropdef == null then
- self.add("default: /* test {t} */")
- else
- self.add("case {self.compiler.classid(t)}: /* test {t} */")
- end
- var res2 = self.call(propdef, t, args)
- if res != null then self.assign(res, res2.as(not null))
- self.add "break;"
- end
- if defaultpropdef != null then
- self.add("default: /* default is Object */")
- var res2 = self.call(defaultpropdef, defaultpropdef.mclassdef.bound_mtype, args)
- if res != null then self.assign(res, res2.as(not null))
- else if self.compiler.hardening then
- self.add("default: /* bug */")
- self.bugtype(args.first)
- end
- self.add("\}")
- return res
- end
-
- # Generate a monomorphic send for the method `m', the type `t' and the arguments `args'
- fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable
- do
- assert t isa MClassType
- var propdefs = m.lookup_definitions(self.compiler.mainmodule, t)
- if propdefs.length == 0 then
- abort
- end
- if propdefs.length > 1 then
- self.debug("NOT YET IMPLEMENTED conflict for {t}.{m}: {propdefs.join(" ")}. choose the first")
- end
- var propdef = propdefs.first
- return self.call(propdef, t, args)
- end
-
- fun check_valid_reciever(recvtype: MClassType)
- do
- if self.compiler.runtime_type_analysis.live_types.has(recvtype) or recvtype.mclass.name == "Object" then return
- print "{recvtype} is not a live type"
- abort
- end
-
- # Generate a static call on a method definition
- fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
- do
- check_valid_reciever(recvtype)
- #debug("call {m} on {recvtype} on {args.first}:{args.first.mtype}")
- if m.mclassdef.mclass.name == "Object" and recvtype.ctype == "val*" then
- recvtype = m.mclassdef.bound_mtype
- end
- var recv = self.autobox(args.first, recvtype)
- recv = self.autoadapt(recv, recvtype)
-
- var vararg_rank = m.msignature.vararg_rank
- if vararg_rank >= 0 then
- assert args.length >= m.msignature.arity + 1 # because of self
- var rawargs = args
- args = new Array[RuntimeVariable]
-
- args.add(rawargs.first) # recv
-
- for i in [0..vararg_rank[ do
- args.add(rawargs[i+1])
- end
-
- var vararg_lastrank = vararg_rank + rawargs.length-1-m.msignature.arity
- var vararg = new Array[RuntimeVariable]
- for i in [vararg_rank..vararg_lastrank] do
- vararg.add(rawargs[i+1])
- end
- # FIXME: its it to late to determine the vararg type, this should have been done during a previous analysis
- var elttype = m.msignature.mparameters[vararg_rank].mtype
- elttype = self.resolve_for(elttype, recv)
- args.add(self.array_instance(vararg, elttype))
-
- for i in [vararg_lastrank+1..rawargs.length-1[ do
- args.add(rawargs[i+1])
- end
- else
- args = args.to_a
- end
- if args.length != m.msignature.arity + 1 then # because of self
- add("printf(\"NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.\\n\"); exit(1);")
- debug("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
- return null
- end
-
- args.first = recv
- var rm = new CustomizedRuntimeFunction(m, recvtype)
- return rm.call(self, args)
- end
-
- fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable])
- do
- var recv = args.first
- for i in [0..m.msignature.arity[ do
- var t = m.msignature.mparameters[i].mtype
- if i == m.msignature.vararg_rank then
- t = args[i+1].mtype
- end
- t = self.resolve_for(t, recv)
- args[i+1] = self.autobox(args[i+1], t)
- end
- end
-
- fun bugtype(recv: RuntimeVariable)
- do
- if recv.mtype.ctype != "val*" then return
- self.add("fprintf(stderr, \"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
- end
-
- # Generate a polymorphic attribute read
- fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable
- do
- var types = self.collect_types(recv)
-
- var ret = a.intro.static_mtype.as(not null)
- ret = self.resolve_for(ret, recv)
- var res = self.new_var(ret)
-
- if types.is_empty then
- self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
- return res
- end
- self.add("/* read {a} on {recv.inspect} */")
- self.add("switch({recv}->classid) \{")
- var last = types.last
- for t in types do
- if not self.compiler.hardening and t == last then
- self.add("default: /*{self.compiler.classid(t)}*/")
- else
- self.add("case {self.compiler.classid(t)}:")
- end
- var recv2 = self.autoadapt(recv, t)
- var ta = a.intro.static_mtype.as(not null)
- ta = self.resolve_for(ta, recv2)
- var res2 = self.new_expr("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta)
- if not ta isa MNullableType then
- if ta.ctype == "val*" then
- self.add("if ({res2} == NULL) \{")
- self.add_abort("Uninitialized attribute {a.name}")
- self.add("\}")
- else
- self.add("/*NOTYET isset on primitive attributes*/")
- end
- end
- self.assign(res, res2)
- self.add("break;")
- end
- if self.compiler.hardening then
- self.add("default: /* Bug */")
- self.bugtype(recv)
- end
- self.add("\}")
-
- return res
- end
-
- # Generate a polymorphic attribute write
- fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable)
- do
- var types = self.collect_types(recv)
-
- if types.is_empty then
- self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
- return
- end
- self.add("/* write {a} on {recv.inspect} */")
- self.add("switch({recv}->classid) \{")
- var last = types.last
- for t in types do
- if not self.compiler.hardening and t == last then
- self.add("default: /*{self.compiler.classid(t)}*/")
- else
- self.add("case {self.compiler.classid(t)}:")
- end
- var recv2 = self.autoadapt(recv, t)
- var ta = a.intro.static_mtype.as(not null)
- ta = self.resolve_for(ta, recv2)
- self.add("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
- self.add("break;")
- end
- if self.compiler.hardening then
- self.add("default: /* Bug*/")
- self.bugtype(recv)
- end
- self.add("\}")
- end
-
- # Generate a alloc-instance + init-attributes
- fun init_instance(mtype: MClassType): RuntimeVariable
- do
- mtype = self.anchor(mtype).as(MClassType)
- if not self.compiler.runtime_type_analysis.live_types.has(mtype) then
- debug "problem: {mtype} was detected dead"
- end
- var res = self.new_expr("NEW_{mtype.c_name}()", mtype)
- res.is_exact = true
- return res
- end
-
- # Generate a polymorphic subtype test
- fun type_test(value: RuntimeVariable, mtype: MType): RuntimeVariable
- do
- mtype = self.anchor(mtype)
- var types = self.collect_types(value)
-
- var res = self.new_var(bool_type)
-
- self.add("/* isa {mtype} on {value.inspect} */")
- if value.mcasttype isa MNullableType then
- self.add("if ({value} == NULL) \{")
- if mtype isa MNullableType then
- self.add("{res} = 1; /* isa {mtype} */")
- else
- self.add("{res} = 0; /* not isa {mtype} */")
- end
- self.add("\} else ")
- end
- self.add("switch({value}->classid) \{")
- for t in types do
- if t.is_subtype(self.compiler.mainmodule, null, mtype) then
- self.add("case {self.compiler.classid(t)}: /* {t} */")
- end
- end
- self.add("{res} = 1;")
- self.add("break;")
- self.add("default:")
- self.add("{res} = 0;")
- self.add("\}")
-
- return res
- end
-
- # Generate the code required to dynamically check if 2 objects share the same runtime type
- fun is_same_type_test(value1, value2: RuntimeVariable): RuntimeVariable
- do
- var res = self.new_var(bool_type)
- if value2.mtype.ctype == "val*" then
- if value1.mtype.ctype == "val*" then
- self.add "{res} = {value1}->classid == {value2}->classid;"
- else
- self.add "{res} = {self.compiler.classid(value1.mtype.as(MClassType))} == {value2}->classid;"
- end
- else
- if value1.mtype.ctype == "val*" then
- self.add "{res} = {value1}->classid == {self.compiler.classid(value2.mtype.as(MClassType))};"
- else if value1.mcasttype == value2.mcasttype then
- self.add "{res} = 1;"
- else
- self.add "{res} = 0;"
- end
- end
- return res
- end
-
- # Generate a Nit "is" for two runtime_variables
- fun equal_test(value1, value2: RuntimeVariable): RuntimeVariable
- do
- var res = self.new_var(bool_type)
- if value2.mtype.ctype != "val*" and value1.mtype.ctype == "val*" then
- var tmp = value1
- value1 = value2
- value2 = tmp
- end
- if value1.mtype.ctype != "val*" then
- if value2.mtype.ctype == value1.mtype.ctype then
- self.add("{res} = {value1} == {value2};")
- else if value2.mtype.ctype != "val*" then
- self.add("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
- else
- var mtype1 = value1.mtype.as(MClassType)
- self.add("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
- self.add("if ({res}) \{")
- self.add("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
- self.add("\}")
- end
- else
- var s = new Array[String]
- for t in self.compiler.live_primitive_types do
- if not t.is_subtype(self.compiler.mainmodule, null, value1.mcasttype) then continue
- if not t.is_subtype(self.compiler.mainmodule, null, value2.mcasttype) then continue
- s.add "({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
- end
- if s.is_empty then
- self.add("{res} = {value1} == {value2};")
- else
- self.add("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")