X-Git-Url: http://nitlanguage.org?ds=sidebyside diff --git a/src/naive_interpreter.nit b/src/naive_interpreter.nit index 0682043..4818e57 100644 --- a/src/naive_interpreter.nit +++ b/src/naive_interpreter.nit @@ -341,9 +341,11 @@ private class NaiveInterpreter var mproperty = mpropdef.mproperty if self.modelbuilder.mpropdef2npropdef.has_key(mpropdef) then var npropdef = self.modelbuilder.mpropdef2npropdef[mpropdef] + self.parameter_check(npropdef, mpropdef, args) return npropdef.call(self, mpropdef, args) else if mproperty.name == "init" then var nclassdef = self.modelbuilder.mclassdef2nclassdef[mpropdef.mclassdef] + self.parameter_check(nclassdef, mpropdef, args) return nclassdef.call(self, mpropdef, args) else fatal("Fatal Error: method {mpropdef} not found in the AST") @@ -351,6 +353,28 @@ private class NaiveInterpreter end end + # Generate type checks in the C code to check covariant parameters + fun parameter_check(node: ANode, mpropdef: MMethodDef, args: Array[Instance]) + do + var msignature = mpropdef.msignature + for i in [0..msignature.arity[ do + # skip test for vararg since the array is instantiated with the correct polymorphic type + if msignature.vararg_rank == i then continue + + # skip if the cast is not required + var origmtype = mpropdef.mproperty.intro.msignature.mparameters[i].mtype + if not origmtype.need_anchor then continue + + # get the parameter type + var mtype = msignature.mparameters[i].mtype + var anchor = args.first.mtype.as(MClassType) + mtype = mtype.anchor_to(self.mainmodule, anchor) + if not args[i+1].mtype.is_subtype(self.mainmodule, anchor, mtype) then + node.fatal(self, "Cast failed") + end + end + end + fun call_closure(closure: ClosureInstance, args: Array[Instance]): nullable Instance do var nclosuredef = closure.nclosuredef @@ -391,16 +415,7 @@ private class NaiveInterpreter fatal("Reciever is null") abort end - var propdefs = mproperty.lookup_definitions(self.mainmodule, mtype) - if propdefs.length > 1 then - fatal("NOT YET IMPLEMETED ERROR: Property conflict: {propdefs.join(", ")}") - abort - end - assert propdefs.length == 1 else - fatal("Fatal Error: No property '{mproperty}' for '{recv}'") - abort - end - var propdef = propdefs.first + var propdef = mproperty.lookup_first_definition(self.mainmodule, mtype) return self.call(propdef, args) end @@ -662,11 +677,11 @@ redef class AInternMethPropdef end else if pname == "output_class_name" then var recv = args.first - print recv.mtype.as(MClassType).mclass + print recv.mtype return null else if pname == "native_class_name" then var recv = args.first - var txt = recv.mtype.as(MClassType).mclass.to_s + var txt = recv.mtype.to_s return v.native_string_instance(txt) else if pname == "==" then # == is correclt redefined for instances @@ -1578,6 +1593,7 @@ redef class AAttrExpr do var recv = v.expr(self.n_expr) if recv == null then return null + if recv.mtype isa MNullType then fatal(v, "Reciever is null") var mproperty = self.mproperty.as(not null) return v.read_attribute(mproperty, recv) end @@ -1588,6 +1604,7 @@ redef class AAttrAssignExpr do var recv = v.expr(self.n_expr) if recv == null then return + if recv.mtype isa MNullType then fatal(v, "Reciever is null") var i = v.expr(self.n_value) if i == null then return var mproperty = self.mproperty.as(not null) @@ -1601,6 +1618,7 @@ redef class AAttrReassignExpr do var recv = v.expr(self.n_expr) if recv == null then return + if recv.mtype isa MNullType then fatal(v, "Reciever is null") var value = v.expr(self.n_value) if value == null then return var mproperty = self.mproperty.as(not null) @@ -1617,6 +1635,7 @@ redef class AIssetAttrExpr do var recv = v.expr(self.n_expr) if recv == null then return null + if recv.mtype isa MNullType then fatal(v, "Reciever is null") var mproperty = self.mproperty.as(not null) assert recv isa MutableInstance return v.bool_instance(recv.attributes.has_key(mproperty))