Because of inlining, a visitor can visit more than one property.
nitc :: GlobalCompilerVisitor :: bugtype
nitc :: GlobalCompilerVisitor :: collect_types
The runtime types that are acceptable for a given receiver.nitc :: GlobalCompilerVisitor :: get_recvtype
Subpart of old call functionnitc $ GlobalCompilerVisitor :: SELF
Type of this instance, automatically specialized in every classnitc $ GlobalCompilerVisitor :: adapt_signature
Adapt the arguments of a method according to targettedMMethodDef
nitc $ GlobalCompilerVisitor :: array_instance
Generate an array valuenitc $ GlobalCompilerVisitor :: autobox
Box or unbox a value to another type iff a C type conversion is needednitc $ GlobalCompilerVisitor :: box_extern
Box extern classes to be used in the generated codenitc $ GlobalCompilerVisitor :: call
Generate a static call on a method definitionnitc $ GlobalCompilerVisitor :: class_name_string
Return a "const char*" variable associated to the classname of the dynamic type of an objectnitc $ GlobalCompilerVisitor :: equal_test
Generate a Nit "is" for two runtime_variablesnitc $ GlobalCompilerVisitor :: init_instance
Generate a alloc-instance + init-attributesnitc $ GlobalCompilerVisitor :: is_same_type_test
Generate the code required to dynamically check if 2 objects share the same runtime typenitc $ GlobalCompilerVisitor :: isset_attribute
Generate a polymorphic attribute is_set testnitc $ GlobalCompilerVisitor :: native_array_get
Return an element of a native array.nitc $ GlobalCompilerVisitor :: native_array_set
Store an element in a native array.nitc $ GlobalCompilerVisitor :: read_attribute
Generate a polymorphic attribute readnitc $ GlobalCompilerVisitor :: routine_ref_call
Call the underlying referenced functionnitc $ GlobalCompilerVisitor :: routine_ref_instance
Instantiate a new routine pointernitc $ GlobalCompilerVisitor :: send
Generate a polymorphic send for the methodm
and the arguments args
nitc $ GlobalCompilerVisitor :: supercall
Generate a super call from a method definitionnitc $ GlobalCompilerVisitor :: type_test
Generate a polymorphic subtype testnitc $ GlobalCompilerVisitor :: unbox_extern
Unbox extern classes to be used in extern code (legacy NI and FFI)nitc $ GlobalCompilerVisitor :: unbox_signature_extern
Unbox all the arguments of a method when implementedextern
or intern
nitc $ GlobalCompilerVisitor :: vararg_instance
FIXME: this is currently buggy since recv is not exactnitc $ GlobalCompilerVisitor :: write_attribute
Generate a polymorphic attribute writenitc :: AbstractCompilerVisitor :: _compiler
The associated compilernitc :: AbstractCompilerVisitor :: _current_node
The current visited AST nodenitc :: AbstractCompilerVisitor :: _frame
The currentStaticFrame
nitc :: AbstractCompilerVisitor :: _last
nitc :: AbstractCompilerVisitor :: adapt_signature
Adapt the arguments of a method according to targettedMMethodDef
nitc :: AbstractCompilerVisitor :: add
Add a line in the main part of the generated Cnitc :: AbstractCompilerVisitor :: add_cast
Add a dynamic castnitc :: AbstractCompilerVisitor :: add_escape_label
Insert a C label for associated with an escapemarknitc :: AbstractCompilerVisitor :: add_extern
Look for a needed .h and .c file for a given modulenitc :: AbstractCompilerVisitor :: add_raw_abort
Generate abort without a message.nitc :: AbstractCompilerVisitor :: add_raw_throw
Generate a long jump if there is a catch block.nitc :: AbstractCompilerVisitor :: array_instance
Generate an array valuenitc :: AbstractCompilerVisitor :: assign
Correctly assign a left and a right valuenitc :: AbstractCompilerVisitor :: autoadapt
Unsafely cast a value to a new typenitc :: AbstractCompilerVisitor :: autobox
Box or unbox a value to another type iff a C type conversion is needednitc :: AbstractCompilerVisitor :: bool_instance
Generate an integer valuenitc :: AbstractCompilerVisitor :: bool_type
Alias for self.compiler.mainmodule.bool_typenitc :: AbstractCompilerVisitor :: box_extern
Box extern classes to be used in the generated codenitc :: GlobalCompilerVisitor :: bugtype
nitc :: AbstractCompilerVisitor :: byte_instance
Generate a byte valuenitc :: AbstractCompilerVisitor :: c_string_instance
Generates a CString instance fully escaped in C-style \xHH fashionnitc :: AbstractCompilerVisitor :: call
Generate a static call on a method definitionnitc :: AbstractCompilerVisitor :: char_instance
Generate a char valuenitc :: AbstractCompilerVisitor :: check_recv_notnull
Add a check and an abort for a null receiver if neededcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
nitc :: AbstractCompilerVisitor :: class_name_string
Return a "const char*" variable associated to the classname of the dynamic type of an objectnitc :: GlobalCompilerVisitor :: collect_types
The runtime types that are acceptable for a given receiver.nitc :: AbstractCompilerVisitor :: compiler=
The associated compilernitc :: AbstractCompilerVisitor :: current_node
The current visited AST nodenitc :: AbstractCompilerVisitor :: current_node=
The current visited AST nodenitc :: AbstractCompilerVisitor :: debug
Safely show a debug message on the current node and repeat the message in the C code as a commentnitc :: AbstractCompilerVisitor :: declare_once
Add a declaration in the local-headercore :: Object :: defaultinit
nitc :: AbstractCompilerVisitor :: equal_test
Generate a Nit "is" for two runtime_variablesnitc :: AbstractCompilerVisitor :: escapemark_name
Return an unique and stable identifier associated with an escapemarknitc :: AbstractCompilerVisitor :: expr
Compile an expression an return its resultnitc :: AbstractCompilerVisitor :: expr_bool
Alias forself.expr(nexpr, self.bool_type)
nitc :: AbstractCompilerVisitor :: float_instance
Generate a float valuenitc :: AbstractCompilerVisitor :: frame=
The currentStaticFrame
nitc :: AbstractCompilerVisitor :: get_property
Force to get the primitive property namedname
in the instance recv
or abort
nitc :: GlobalCompilerVisitor :: get_recvtype
Subpart of old call functionnitc :: AbstractCompilerVisitor :: init_instance
Generate a alloc-instance + init-attributesnitc :: AbstractCompilerVisitor :: init_instance_or_extern
Allocate and init attributes of an instance of a standard or extern classnitc :: AbstractCompilerVisitor :: int16_instance
Generate an int16 valuenitc :: AbstractCompilerVisitor :: int32_instance
Generate an int32 valuenitc :: AbstractCompilerVisitor :: int8_instance
Generate an int8 valuenitc :: AbstractCompilerVisitor :: int_instance
Generate an integer valuecore :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
nitc :: AbstractCompilerVisitor :: is_same_type_test
Generate the code required to dynamically check if 2 objects share the same runtime typenitc :: AbstractCompilerVisitor :: isset_attribute
Generate a polymorphic attribute is_set testnitc :: AbstractCompilerVisitor :: last
nitc :: AbstractCompilerVisitor :: last=
nitc :: AbstractCompilerVisitor :: maybe_null
Can value be null? (according to current knowledge)nitc :: AbstractCompilerVisitor :: monomorphic_send
Generate a monomorphic send for the methodm
, the type t
and the arguments args
nitc :: AbstractCompilerVisitor :: monomorphic_super_send
Generate a monomorphic super send from the methodm
, the type t
and the arguments args
nitc :: AbstractCompilerVisitor :: native_array_get
Return an element of a native array.nitc :: AbstractCompilerVisitor :: native_array_set
Store an element in a native array.core :: Object :: native_class_name
The class name of the object in CString format.nitc :: AbstractCompilerVisitor :: new_expr
Return a new local runtime_variable initialized with the C expressioncexpr
.
nitc :: AbstractCompilerVisitor :: new_named_var
Return a new uninitialized named runtime_variablenitc :: AbstractCompilerVisitor :: new_var
Return a new uninitialized local runtime_variablenitc :: AbstractCompilerVisitor :: new_var_extern
The difference withnew_var
is the C static type of the local variable
nitc :: AbstractCompilerVisitor :: null_instance
Generate thenull
value
nitc :: AbstractCompilerVisitor :: object_type
Alias for self.compiler.mainmodule.object_typecore :: Object :: output_class_name
Display class name on stdout (debug only).nitc :: AbstractCompilerVisitor :: read_attribute
Generate a polymorphic attribute readnitc :: AbstractCompilerVisitor :: require_declaration
Request the presence of a global declarationnitc :: AbstractCompilerVisitor :: ret
Generate a return with the values
nitc :: AbstractCompilerVisitor :: ret_to_c
Return aRuntimeVarible
to C user code
nitc :: AbstractCompilerVisitor :: routine_ref_call
Call the underlying referenced functionnitc :: AbstractCompilerVisitor :: routine_ref_instance
Instantiate a new routine pointernitc :: AbstractCompilerVisitor :: send
Generate a polymorphic send for the methodm
and the arguments args
nitc :: AbstractCompilerVisitor :: set_finalizer
Set a GC finalizer onrecv
, only if recv
isa Finalizable
nitc :: AbstractCompilerVisitor :: stmt
Compile a statement (if any)nitc :: AbstractCompilerVisitor :: string_instance
Generate a string valuenitc :: AbstractCompilerVisitor :: supercall
Generate a super call from a method definitionnitc :: AbstractCompilerVisitor :: type_test
Generate a polymorphic subtype testnitc :: AbstractCompilerVisitor :: uint16_instance
Generate a uint16 valuenitc :: AbstractCompilerVisitor :: uint32_instance
Generate a uint32 valuenitc :: AbstractCompilerVisitor :: unbox_extern
Unbox extern classes to be used in extern code (legacy NI and FFI)nitc :: AbstractCompilerVisitor :: unbox_signature_extern
Unbox all the arguments of a method when implementedextern
or intern
nitc :: AbstractCompilerVisitor :: var_from_c
Create aRuntimeVariable
for this C variable originating from C user code
nitc :: AbstractCompilerVisitor :: vararg_instance
Get an instance of a array for a varargnitc :: AbstractCompilerVisitor :: varargize
Evaluateargs
as expressions in the call of mpropdef
on recv
.
nitc :: AbstractCompilerVisitor :: variable
Return the local runtime_variable associated to a Nit local variablenitc :: AbstractCompilerVisitor :: write_attribute
Generate a polymorphic attribute writenitc :: AbstractCompilerVisitor
A visitor on the AST of property definition that generate the C code.
# 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
super AbstractCompilerVisitor
redef type COMPILER: GlobalCompiler
redef fun autobox(value, mtype)
do
if value.mtype == mtype then
return value
else if not value.mtype.is_c_primitive and not mtype.is_c_primitive then
return value
else if not value.mtype.is_c_primitive then
return self.new_expr("((struct {mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
else if not mtype.is_c_primitive 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("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
return res
end
self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
return res
else if value.mtype.ctype == "void*" and mtype.ctype == "void*" then
return value
else
# Bad things will appen!
var res = self.new_var(mtype)
self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
self.add("PRINT_ERROR(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); fatal_exit(1);")
return res
end
end
redef fun unbox_extern(value, mtype)
do
if mtype isa MClassType and mtype.mclass.kind == extern_kind and
mtype.mclass.name != "CString" then
var res = self.new_var_extern(mtype)
self.add "{res} = ((struct {mtype.c_name}*){value})->value; /* unboxing {value.mtype} */"
return res
else
return value
end
end
redef fun box_extern(value, mtype)
do
if not mtype isa MClassType or mtype.mclass.kind != extern_kind or
mtype.mclass.name == "CString" then return value
var valtype = value.mtype.as(MClassType)
var res = self.new_var(mtype)
if not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
self.add("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
return res
end
self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
return res
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
redef fun native_array_def(pname, ret_type, arguments)
do
var elttype = arguments.first.mtype
var recv = "((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->values"
if pname == "[]" then
self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
return true
else if pname == "[]=" then
self.add("{recv}[{arguments[1]}]={arguments[2]};")
return true
else if pname == "length" then
self.ret(self.new_expr("((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->length", ret_type.as(not null)))
return true
else if pname == "copy_to" then
var recv1 = "((struct {arguments[1].mcasttype.c_name}*){arguments[1]})->values"
self.add("memmove({recv1},{recv},{arguments[2]}*sizeof({elttype.ctype}));")
return true
else if pname == "memmove" then
# fun memmove(start: Int, length: Int, dest: NativeArray[E], dest_start: Int) is intern do
var recv1 = "((struct {arguments[3].mcasttype.c_name}*){arguments[3]})->values"
self.add("memmove({recv1}+{arguments[4]}, {recv}+{arguments[1]}, {arguments[2]}*sizeof({elttype.ctype}));")
return true
end
return false
end
redef fun native_array_instance(elttype, length)
do
var ret_type = mmodule.native_array_type(elttype)
ret_type = anchor(ret_type).as(MClassType)
length = autobox(length, compiler.mainmodule.int_type)
return self.new_expr("NEW_{ret_type.c_name}((int){length})", ret_type)
end
redef fun native_array_get(nat, i)
do
var recv = "((struct {nat.mcasttype.c_name}*){nat})->values"
var ret_type = nat.mcasttype.as(MClassType).arguments.first
return self.new_expr("{recv}[{i}]", ret_type)
end
redef fun native_array_set(nat, i, val)
do
var recv = "((struct {nat.mcasttype.c_name}*){nat})->values"
self.add("{recv}[{i}]={val};")
end
redef fun routine_ref_instance(routine_mclass_type, recv, callsite)
do
var mmethoddef = callsite.mpropdef
var method = new CustomizedRuntimeFunction(mmethoddef, recv.mcasttype.as(MClassType))
var my_recv = recv
if recv.mtype.is_c_primitive then
var object_type = mmodule.object_type
my_recv = autobox(recv, object_type)
end
var thunk = new CustomizedThunkFunction(mmethoddef, my_recv.mtype.as(MClassType))
thunk.polymorph_call_flag = not my_recv.is_exact
compiler.todo(method)
compiler.todo(thunk)
var ret_type = self.anchor(routine_mclass_type).as(MClassType)
var res = self.new_expr("NEW_{ret_type.c_name}({my_recv}, &{thunk.c_name})", ret_type)
return res
end
redef fun routine_ref_call(mmethoddef, arguments)
do
var routine = arguments.first
var routine_type = routine.mtype.as(MClassType)
var routine_class = routine_type.mclass
var underlying_recv = "((struct {routine.mcasttype.c_name}*){routine})->recv"
var underlying_method = "((struct {routine.mcasttype.c_name}*){routine})->method"
adapt_signature(mmethoddef, arguments)
arguments.shift
var ss = "{underlying_recv}"
if arguments.length > 0 then
ss = "{ss}, {arguments.join(", ")}"
end
arguments.unshift routine
var ret_mtype = mmethoddef.msignature.return_mtype
if ret_mtype != null then
ret_mtype = resolve_for(ret_mtype, routine)
end
var callsite = "{underlying_method}({ss})"
if ret_mtype != null then
var subres = new_expr("{callsite}", ret_mtype)
ret(subres)
else
add("{callsite};")
end
end
redef fun send(m, args)
do
var types = self.collect_types(args.first)
var res: nullable RuntimeVariable
var ret = m.intro.msignature.return_mtype
if ret == null then
res = null
else
ret = self.resolve_for(ret, args.first)
res = self.new_var(ret)
end
self.add("/* send {m} on {args.first.inspect} */")
if args.first.mtype.is_c_primitive then
var mclasstype = args.first.mtype.as(MClassType)
if not self.compiler.runtime_type_analysis.live_types.has(mclasstype) then
self.add("/* skip, dead class {mclasstype} */")
return res
end
if not mclasstype.has_mproperty(self.compiler.mainmodule, m) then
self.add("/* skip, no method {m} */")
return res
end
var propdef = m.lookup_first_definition(self.compiler.mainmodule, mclasstype)
var res2 = self.call(propdef, mclasstype, args)
if res != null then self.assign(res, res2.as(not null))
return res
end
var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_null.value or m.name == "==" or m.name == "!="
if args.first.mcasttype isa MNullableType or args.first.mcasttype isa MNullType and consider_null 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 == "==" or m.name == "is_same_instance" 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("Receiver is null")
end
self.add "\} else"
end
if types.is_empty then
self.add("\{")
self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
self.bugtype(args.first)
self.add("\}")
return res
end
self.add("switch({args.first}->classid) \{")
var last = types.last
var defaultpropdef: nullable MMethodDef = null
for t in types do
var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
if propdef.mclassdef.mclass.name == "Object" and not t.is_c_primitive 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
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_error "{recvtype} is not a live type"
abort
end
# Subpart of old call function
#
# Checks if the type of the receiver is valid and corrects it if necessary
private fun get_recvtype(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): MClassType
do
check_valid_reciever(recvtype)
return recvtype
end
redef fun call(m, recvtype, args)
do
var recv_type = get_recvtype(m, recvtype, args)
var recv = self.autoadapt(self.autobox(args.first, recvtype), recvtype)
if m.is_extern then recv = unbox_extern(recv, recv_type)
args = args.to_a
args.first = recv
assert args.length == m.msignature.arity + 1 else debug("Invalid arity for {m}. {args.length} arguments given.")
var rm = new CustomizedRuntimeFunction(m, recvtype)
return rm.call(self, args)
end
redef fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
do
var types = self.collect_types(args.first)
var res: nullable RuntimeVariable
var ret = m.mproperty.intro.msignature.return_mtype
if ret == null then
res = null
else
ret = self.resolve_for(ret, args.first)
res = self.new_var(ret)
end
self.add("/* super {m} on {args.first.inspect} */")
if args.first.mtype.is_c_primitive then
var mclasstype = args.first.mtype.as(MClassType)
if not self.compiler.runtime_type_analysis.live_types.has(mclasstype) then
self.add("/* skip, no method {m} */")
return res
end
var propdef = m.lookup_next_definition(self.compiler.mainmodule, mclasstype)
var res2 = self.call(propdef, mclasstype, args)
if res != null then self.assign(res, res2.as(not null))
return res
end
if types.is_empty then
self.add("\{")
self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
self.bugtype(args.first)
self.add("\}")
return res
end
self.add("switch({args.first}->classid) \{")
var last = types.last
for t in types do
var propdef = m.lookup_next_definition(self.compiler.mainmodule, t)
if not self.compiler.hardening and t == last 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 self.compiler.hardening then
self.add("default: /* bug */")
self.bugtype(args.first)
end
self.add("\}")
return res
end
redef fun adapt_signature(m, args)
do
var recv = args.first
for i in [0..m.msignature.arity[ do
var mp = m.msignature.mparameters[i]
var t = mp.mtype
if mp.is_vararg then
t = args[i+1].mtype
end
t = self.resolve_for(t, recv)
args[i+1] = self.autobox(args[i+1], t)
end
end
redef fun unbox_signature_extern(m, args)
do
var recv = args.first
for i in [0..m.msignature.arity[ do
var mp = m.msignature.mparameters[i]
var t = mp.mtype
if mp.is_vararg then
t = args[i+1].mtype
end
t = self.resolve_for(t, recv)
if m.is_extern then args[i+1] = self.unbox_extern(args[i+1], t)
end
end
# FIXME: this is currently buggy since recv is not exact
redef fun vararg_instance(mpropdef, recv, varargs, elttype)
do
elttype = self.resolve_for(elttype, recv)
return self.array_instance(varargs, elttype)
end
fun bugtype(recv: RuntimeVariable)
do
if recv.mtype.is_c_primitive then return
self.add("PRINT_ERROR(\"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
self.add("fatal_exit(1);")
end
redef fun isset_attribute(a, recv)
do
check_recv_notnull(recv)
var types = self.collect_types(recv)
var res = self.new_var(bool_type)
if types.is_empty then
self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
self.bugtype(recv)
return res
end
self.add("/* isset {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 attr = self.new_expr("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta)
if not ta isa MNullableType then
if not ta.is_c_primitive then
self.add("{res} = ({attr} != NULL);")
else
self.add("{res} = 1; /*NOTYET isset on primitive attributes*/")
end
end
self.add("break;")
end
if self.compiler.hardening then
self.add("default: /* Bug */")
self.bugtype(recv)
end
self.add("\}")
return res
end
redef fun read_attribute(a, recv)
do
check_recv_notnull(recv)
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}*/")
self.bugtype(recv)
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 and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
if not ta.is_c_primitive 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
redef fun write_attribute(a, recv, value)
do
check_recv_notnull(recv)
var types = self.collect_types(recv)
if types.is_empty then
self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
self.bugtype(recv)
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
redef fun init_instance(mtype)
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
redef fun type_test(value, mtype, tag)
do
mtype = self.anchor(mtype)
if not self.compiler.runtime_type_analysis.live_cast_types.has(mtype) then
debug "problem: {mtype} was detected cast-dead"
abort
end
var types = self.collect_types(value)
var res = self.new_var(bool_type)
self.add("/* isa {mtype} on {value.inspect} */")
if value.mtype.is_c_primitive then
if value.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
self.add("{res} = 1;")
else
self.add("{res} = 0;")
end
return res
end
if value.mcasttype isa MNullableType or value.mcasttype isa MNullType 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
redef fun is_same_type_test(value1, value2)
do
var res = self.new_var(bool_type)
if not value2.mtype.is_c_primitive then
if not value1.mtype.is_c_primitive then
self.add "{res} = {value1}->classid == {value2}->classid;"
else
self.add "{res} = {self.compiler.classid(value1.mtype.as(MClassType))} == {value2}->classid;"
end
else
if not value1.mtype.is_c_primitive 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
redef fun class_name_string(value)
do
var res = self.get_name("var_class_name")
self.add_decl("const char* {res};")
if not value.mtype.is_c_primitive then
self.add "{res} = class_names[{value}->classid];"
else
self.add "{res} = class_names[{self.compiler.classid(value.mtype.as(MClassType))}];"
end
return res
end
redef fun equal_test(value1, value2)
do
var res = self.new_var(bool_type)
if value2.mtype.is_c_primitive and not value1.mtype.is_c_primitive then
var tmp = value1
value1 = value2
value2 = tmp
end
if value1.mtype.is_c_primitive then
if value2.mtype == value1.mtype then
self.add("{res} = {value1} == {value2};")
else if value2.mtype.is_c_primitive 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 self.compiler.mainmodule.model.get_mclasses_by_name("Pointer") != null then
var pointer_type = self.compiler.mainmodule.pointer_type
if value1.mcasttype.is_subtype(self.compiler.mainmodule, null, pointer_type) or
value2.mcasttype.is_subtype(self.compiler.mainmodule, null, pointer_type) then
s.add "(((struct {pointer_type.c_name}*){value1})->value == ((struct {pointer_type.c_name}*){value2})->value)"
end
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(" || ")}));")
end
end
return res
end
redef fun array_instance(array, elttype)
do
elttype = self.anchor(elttype)
var arraytype = mmodule.array_type(elttype)
var res = self.init_instance(arraytype)
self.add("\{ /* {res} = array_instance Array[{elttype}] */")
var nat = self.new_var(mmodule.native_array_type(elttype))
nat.is_exact = true
self.add("{nat} = NEW_{nat.mtype.c_name}({array.length});")
for i in [0..array.length[ do
var r = self.autobox(array[i], elttype)
self.add("((struct {nat.mtype.c_name}*) {nat})->values[{i}] = {r};")
end
var length = self.int_instance(array.length)
self.send(self.get_property("with_native", arraytype), [res, nat, length])
self.add("\}")
return res
end
end
src/compiler/global_compiler.nit:331,1--1011,3