From 0c23ea47f678814deea7f4603358225990773105 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Fri, 25 Nov 2011 10:31:50 -0500 Subject: [PATCH] compile: better types checks on virtual type Two new TableElt are added for each global property of a virtual type, one (TableEltVTClassColor) is used to store the color of the class of the bound and the other (TableEltVTClassId) is for the identifier. The C generation of ITypeCheck is therefore updated to use theses when the static type is a virtual type. Note that generics is still erased and that type checks on formal type parameter is blindly (and wrongly) performed on the bound. Signed-off-by: Jean Privat --- src/compiling/compiling_base.nit | 12 ++++++++++ src/compiling/compiling_global.nit | 34 +++++++++++++++++++++++++++ src/compiling/compiling_icode.nit | 43 ++++++++++++++++++++++++++--------- src/compiling/table_computation.nit | 13 +++++++++++ 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/src/compiling/compiling_base.nit b/src/compiling/compiling_base.nit index 24d4fcb..0f1918f 100644 --- a/src/compiling/compiling_base.nit +++ b/src/compiling/compiling_base.nit @@ -246,6 +246,18 @@ redef class MMGlobalProperty do return "ATTR_{intro.cname}" end + + # C symbol refering a virtual type class color + fun vt_class_color: String + do + return "VTCOLOR_{intro.cname}" + end + + # C symbol refering a virtual type class id + fun vt_class_id: String + do + return "VTID_{intro.cname}" + end end redef class MMGlobalClass diff --git a/src/compiling/compiling_global.nit b/src/compiling/compiling_global.nit index da63476..9833ccb 100644 --- a/src/compiling/compiling_global.nit +++ b/src/compiling/compiling_global.nit @@ -203,6 +203,39 @@ redef class TableEltSuper end end +redef class TableEltVTClassColor + redef fun compile_macros(v, value) + do + var pg = property.global + v.add_decl("#define {pg.vt_class_color}(recv) (VAL2VFT(recv)[{value}].i)") + end + + redef fun compile_to_c(v, c) + do + var prog = v.program + var p = c[property.global] + var g = p.signature_for(c.get_type).return_type.local_class.global + var col = g.intro.as(MMConcreteClass).class_color_pos + return "{prog.table_information.color(col)} /* {prog.table_information.color(self)}: VT {c}::{p} : color of {g} */" + end +end + +redef class TableEltVTClassId + redef fun compile_macros(v, value) + do + var pg = property.global + v.add_decl("#define {pg.vt_class_id}(recv) (VAL2VFT(recv)[{value}].i)") + end + + redef fun compile_to_c(v, c) + do + var prog = v.program + var p = c[property.global] + var g = p.signature_for(c.get_type).return_type.local_class.global + return "{prog.compiled_classes[g].id} /* {prog.table_information.color(self)}: VT {c}::{p} : id of {g} */" + end +end + redef class TableEltAttr redef fun compile_macros(v, value) do @@ -218,6 +251,7 @@ redef class TableEltAttr end end + redef class AbsTableEltClass # The C macro name refering the value fun symbol: String is abstract diff --git a/src/compiling/compiling_icode.nit b/src/compiling/compiling_icode.nit index 4467942..3238811 100644 --- a/src/compiling/compiling_icode.nit +++ b/src/compiling/compiling_icode.nit @@ -1016,9 +1016,7 @@ redef class ITypeCheck redef fun compile_to_c(v) do if not need_result then return - # FIXME handle formaltypes v.add_location(location) - var g = stype.local_class.global var recv = v.register(expr2) var w = new_result(v) w.add("TAG_Bool(") @@ -1037,15 +1035,38 @@ redef class ITypeCheck w.add("!=NIT_NULL) && ") end end - w.add("VAL_ISA(") - w.add(recv) - w.add(", ") - w.add(g.color_id) - w.add(", ") - w.add(g.id_id) - w.add(")) /*cast ") - w.add(stype.to_s) - w.add("*/") + # FIXME handle formaltypes + var t = stype + if t isa MMVirtualType then + var slf = v.register(expr1) + var g = t.property.global + w.add("VAL_ISA(") + w.add(recv) + w.add(", ") + w.add(g.vt_class_color) + w.add("(") + w.add(slf) + w.add(")") + w.add(", ") + w.add(g.vt_class_id) + w.add("(") + w.add(slf) + w.add(")") + w.add(")) /*cast ") + w.add(t.to_s) + w.add("*/") + else + var g = t.local_class.global + w.add("VAL_ISA(") + w.add(recv) + w.add(", ") + w.add(g.color_id) + w.add(", ") + w.add(g.id_id) + w.add(")) /*cast ") + w.add(t.to_s) + w.add("*/") + end end end diff --git a/src/compiling/table_computation.nit b/src/compiling/table_computation.nit index 20461ff..16e7a39 100644 --- a/src/compiling/table_computation.nit +++ b/src/compiling/table_computation.nit @@ -112,6 +112,9 @@ redef class MMConcreteClass ilt.add(new TableEltAttr(p)) else if p isa MMMethod then clt.add(new TableEltMeth(p)) + else if p isa MMTypeProperty then + clt.add(new TableEltVTClassId(p)) + clt.add(new TableEltVTClassColor(p)) end end if p isa MMMethod and p.need_super then @@ -452,6 +455,16 @@ class TableEltMeth super TableEltProp end +# An element that represents a class color value for a virtual type +class TableEltVTClassColor + super TableEltProp +end + +# An element that represents a class id value for a virtual type +class TableEltVTClassId + super TableEltProp +end + # An element that represents a function pointer to the super method of a local method class TableEltSuper super TableEltProp -- 1.7.9.5