meth compile_stmt(n: PExpr)
do
if n == null then return
- add_instr("/* Compile stmt {n.locate} */")
+ #add_instr("/* Compile stmt {n.locate} */")
n.prepare_compile_stmt(self)
var i = cfc._variable_index
n.compile_stmt(self)
# Compile is expression node
meth compile_expr(n: PExpr): String
do
- add_instr("/* Compile expr {n.locate} */")
+ #add_instr("/* Compile expr {n.locate} */")
var i = cfc._variable_index
var s = n.compile_expr(self)
cfc._variable_index = i
if s[0] == ' ' or cfc.is_valid_variable(s) then
return s
end
- var v = cfc.get_var("Result for expr {n.locate}")
+ var v = cfc.get_var("Result")
add_assignment(v, s)
return v
end
end
redef class MMAttribute
- # Compile an acces on selffor a given reciever.
- # Result is a valid C left-value for assigment
- meth compile_access(v: CompilerVisitor, recv: String): String
+ # Compile a read acces on selffor a given reciever.
+ meth compile_isset(v: CompilerVisitor, n: PNode, recv: String): String
do
- return "{global.attr_access}({recv}) /*{local_class}::{name}*/"
+ return "TAG_Bool({global.attr_access}({recv})!=NIT_NULL) /* isset {local_class}::{name}*/"
+ end
+
+ # Compile a read acces on selffor a given reciever.
+ meth compile_read_access(v: CompilerVisitor, n: PNode, recv: String): String
+ do
+ var res = "{global.attr_access}({recv}) /*{local_class}::{name}*/"
+ if not signature.return_type.is_nullable and v.tc.opt_warn.value > 0 then
+ res = v.ensure_var(res, "{local_class}::{name}")
+ v.add_instr("if ({res} == NIT_NULL) \{ fprintf(stderr, \"Uninitialized attribute %s\", \"{name}\"); {v.printf_locate_error(n)} } /* implicit isset */;")
+ end
+ return res
+ end
+
+ # Compile a write acces on selffor a given reciever.
+ meth compile_write_access(v: CompilerVisitor, n: PNode, recv: String, value: String)
+ do
+ v.add_instr("{global.attr_access}({recv}) /*{local_class}::{name}*/ = {value};")
end
end
redef class MMReadImplementationMethod
redef meth do_compile_inside(v, params)
do
- return node.prop.compile_access(v, params[0])
+ return node.prop.compile_read_access(v, node, params[0])
end
end
redef class MMWriteImplementationMethod
redef meth do_compile_inside(v, params)
do
- v.add_assignment(node.prop.compile_access(v, params[0]), params[1])
+ node.prop.compile_write_access(v, node, params[0], params[1])
return null
end
end
end
for i in [f..params.length[ do
var attribute = unassigned_attributes[i-f]
- v.add_assignment(attribute.compile_access(v, recv), params[i])
+ attribute.compile_write_access(v, null, recv, params[i])
end
return null
end
redef class MMType
# Compile a subtype check to self
# Return a NIT Bool
- meth compile_cast(v: CompilerVisitor, recv: String): String
+ meth compile_cast(v: CompilerVisitor, recv: String, fromtype: MMType): String
do
# Fixme: handle formaltypes
var g = local_class.global
- return "TAG_Bool(({recv}==NIT_NULL) || VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
+ var s = ""
+ if fromtype.is_nullable then
+ if self.is_nullable then
+ s = "({recv}==NIT_NULL) || "
+ else
+ s = "({recv}!=NIT_NULL) && "
+ end
+ else
+ # FIXME This is used to not break code without the nullable KW
+ s = "({recv}==NIT_NULL) || "
+ end
+ return "TAG_Bool({s}VAL_ISA({recv}, {g.color_id}, {g.id_id})) /*cast {self}*/"
end
# Compile a cast assertion
- meth compile_type_check(v: CompilerVisitor, recv: String, n: PNode)
+ meth compile_type_check(v: CompilerVisitor, recv: String, n: PNode, fromtype: MMType)
do
# Fixme: handle formaltypes
var g = local_class.global
- v.add_instr("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
+ var s = ""
+ if fromtype.is_nullable then
+ if self.is_nullable then
+ s = "({recv}!=NIT_NULL) && "
+ else
+ s = "({recv}==NIT_NULL) || "
+ end
+ else
+ # FIXME This is used to not break code without the nullable KW
+ s = "({recv}!=NIT_NULL) && "
+ end
+ v.add_instr("if ({s}!VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
+ end
+
+ # Compile a notnull cast assertion
+ meth compile_notnull_check(v: CompilerVisitor, recv: String, n: PNode)
+ do
+ if is_nullable then
+ v.add_instr("if (({recv}==NIT_NULL)) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;")
+ end
end
end
# FIXME: do not test always
# FIXME: handle formal types
v.add_instr("/* check if p<{ap.variable.stype} with p:{orig_type} */")
- ap.variable.stype.compile_type_check(v, params[ap.position], ap)
+ ap.variable.stype.compile_type_check(v, params[ap.position], ap, orig_type)
end
v.add_assignment(cname, params[ap.position])
end
end
end
-redef class AControlableBlock
+class AControlableBlock
+special PExpr
meth compile_inside_block(v: CompilerVisitor) is abstract
redef meth compile_stmt(v)
do
end
redef class AWhileExpr
+special AControlableBlock
redef meth compile_inside_block(v)
do
v.add_instr("while (true) \{ /*while*/")
end
redef class AForExpr
+special AControlableBlock
redef meth compile_inside_block(v)
do
var e = v.compile_expr(n_expr)
redef meth compile_expr(v)
do
var e = v.compile_expr(n_expr)
- return n_type.stype.compile_cast(v, e)
+ return n_type.stype.compile_cast(v, e, n_expr.stype)
end
end
redef meth compile_expr(v)
do
var e = v.compile_expr(n_expr)
- n_type.stype.compile_type_check(v, e, self)
+ n_type.stype.compile_type_check(v, e, self, n_expr.stype)
+ return e
+ end
+end
+
+redef class AAsNotnullExpr
+ redef meth compile_expr(v)
+ do
+ var e = v.compile_expr(n_expr)
+ n_expr.stype.compile_notnull_check(v, e, self)
return e
end
end
redef meth compile_expr(v)
do
var e = v.compile_expr(n_expr)
- return prop.compile_access(v, e)
+ return prop.compile_read_access(v, n_id, e)
end
end
do
var e = v.compile_expr(n_expr)
var e2 = v.compile_expr(n_value)
- v.add_assignment(prop.compile_access(v, e), e2)
+ prop.compile_write_access(v, n_id, e, e2)
end
end
redef class AAttrReassignExpr
redef meth compile_stmt(v)
do
var e1 = v.compile_expr(n_expr)
- var e2 = prop.compile_access(v, e1)
+ var e2 = prop.compile_read_access(v, n_id, e1)
var e3 = v.compile_expr(n_value)
var e4 = assign_method.compile_expr_call(v, [e2, e3])
- v.add_assignment(e2, e4)
+ prop.compile_write_access(v, n_id, e1, e4)
+ end
+end
+
+redef class AIssetAttrExpr
+ redef meth compile_expr(v)
+ do
+ var e = v.compile_expr(n_expr)
+ return prop.compile_isset(v, n_id, e)
end
end
-redef class AAbsSendExpr
+redef class AAbsAbsSendExpr
# Compile each argument and add them to the array
meth compile_arguments_in(v: CompilerVisitor, cargs: Array[String])
do
end
redef class AClosureCallExpr
- redef meth intern_compile_call(v)
+ meth intern_compile_call(v: CompilerVisitor): String
do
var cargs = new Array[String]
compile_arguments_in(v, cargs)
end
return va
end
+
+ redef meth compile_expr(v)
+ do
+ var e = intern_compile_call(v)
+ assert e != null
+ return e
+ end
+
+ redef meth compile_stmt(v)
+ do
+ var e = intern_compile_call(v)
+ if e != null then
+ v.add_instr(e + ";")
+ end
+ end
end
redef class AProxyExpr