+ # Types handling
+
+ # 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
+ return mtype.anchor_to(compiler.mainmodule, frame.as(not null).receiver)
+ end
+
+ # Adapt the arguments of a method according to targetted `MMethodDef`
+ fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) do
+ var msignature = m.msignature.as(not null).resolve_for(
+ m.mclassdef.bound_mtype,
+ m.mclassdef.bound_mtype,
+ m.mclassdef.mmodule, true)
+ args.first = autobox(args.first, compiler.mainmodule.object_type)
+ for i in [0..msignature.arity[ do
+ args[i+1] = autobox(args[i + 1], compiler.mainmodule.object_type)
+ end
+ end
+
+ # Box primitive `value` to `mtype`.
+ private fun box(value: RuntimeVariable, mtype: MType): RuntimeVariable do
+ if value.is_boxed then return value
+ var obj_type = compiler.mainmodule.object_type
+ if value.mtype isa MNullType then
+ return new_expr("new RTVal(null, null)", compiler.mainmodule.model.null_type)
+ end
+ var mbox = value.mtype.as(MClassType).mclass
+ return new_expr("new RTVal({mbox.rt_name}.get{mbox.rt_name}(), {value})", obj_type)
+ end
+
+ # Unbox primitive `value` to `mtype`.
+ private fun unbox(value: RuntimeVariable, mtype: MType): RuntimeVariable do
+ if not value.is_boxed then return value
+ if not mtype.is_java_primitive then return value
+ if compiler.box_kinds.has(mtype) then
+ return new_expr("({mtype.java_type}){value}.value", mtype)
+ else
+ info "NOT YET IMPLEMENTED unbox for {value} ({mtype})"
+ abort
+ end
+ end
+
+ # Box or unbox primitive `value` to `mtype` if needed.
+ private fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable do
+ if mtype.is_java_primitive then return unbox(value, mtype)
+ return box(value, mtype)
+ end
+
+ # Can this `value` be a primitive Java value?
+ private fun can_be_primitive(value: RuntimeVariable): Bool do
+ var t = value.mcasttype.undecorate
+ if not t isa MClassType then return false
+ var k = t.mclass.kind
+ return k == interface_kind or t.is_java_primitive
+ end
+