X-Git-Url: http://nitlanguage.org diff --git a/src/typing.nit b/src/typing.nit index be0b742..edb1c89 100644 --- a/src/typing.nit +++ b/src/typing.nit @@ -27,11 +27,6 @@ redef class ToolContext var typing_phase: Phase = new TypingPhase(self, [flow_phase, modelize_property_phase, local_var_init_phase]) end -redef class MPropDef - # Does the MPropDef contains a call to super or a call of a super-constructor? - var has_supercall: Bool = false -end - private class TypingPhase super Phase redef fun process_npropdef(npropdef) do npropdef.do_typing(toolcontext.modelbuilder) @@ -39,61 +34,64 @@ end private class TypeVisitor var modelbuilder: ModelBuilder - var nclassdef: AClassdef - var mpropdef: MPropDef + + # The module of the analysis + # Used to correctly query the model + var mmodule: MModule + + # The static type of the receiver + # Mainly used for type tests and type resolutions + var anchor: nullable MClassType + + # The analyzed mclassdef + var mclassdef: nullable MClassDef + + # The analyzed property + var mpropdef: nullable MPropDef var selfvariable: Variable = new Variable("self") - init(modelbuilder: ModelBuilder, nclassdef: AClassdef, mpropdef: MPropDef) + init(modelbuilder: ModelBuilder, mmodule: MModule, mpropdef: nullable MPropDef) do self.modelbuilder = modelbuilder - self.nclassdef = nclassdef - self.mpropdef = mpropdef + self.mmodule = mmodule - var mclass = nclassdef.mclassdef.mclass + if mpropdef != null then + self.mpropdef = mpropdef + var mclassdef = mpropdef.mclassdef + self.mclassdef = mclassdef + self.anchor = mclassdef.bound_mtype - var selfvariable = new Variable("self") - self.selfvariable = selfvariable - selfvariable.declared_type = mclass.mclass_type - end + var mclass = mclassdef.mclass - fun mmodule: MModule do return self.nclassdef.mclassdef.mmodule - - fun anchor: MClassType do return self.nclassdef.mclassdef.bound_mtype + var selfvariable = new Variable("self") + self.selfvariable = selfvariable + selfvariable.declared_type = mclass.mclass_type + end + end fun anchor_to(mtype: MType): MType do - var mmodule = self.nclassdef.mclassdef.mmodule - var anchor = self.nclassdef.mclassdef.bound_mtype + var anchor = anchor + if anchor == null then + assert not mtype.need_anchor + return mtype + end return mtype.anchor_to(mmodule, anchor) end fun is_subtype(sub, sup: MType): Bool do - var mmodule = self.nclassdef.mclassdef.mmodule - var anchor = self.nclassdef.mclassdef.bound_mtype return sub.is_subtype(mmodule, anchor, sup) end fun resolve_for(mtype, subtype: MType, for_self: Bool): MType do - var mmodule = self.nclassdef.mclassdef.mmodule - var anchor = self.nclassdef.mclassdef.bound_mtype #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}" var res = mtype.resolve_for(subtype, anchor, mmodule, not for_self) return res end - # Retrieve the signature of a `MMethodDef` resolved for a specific call. - # This method is an helper to symplify the query on the model. - # - # Note: `for_self` indicates if the reciever is self or not. - # If yes, virtual types are not resolved. - fun resolve_signature_for(mmethoddef: MMethodDef, recv: MType, for_self: Bool): MSignature - do - return self.resolve_for(mmethoddef.msignature.as(not null), recv, for_self).as(MSignature) - end - # Check that `sub` is a subtype of `sup`. # If `sub` is not a valud suptype, then display an error on `node` an return null. # If `sub` is a safe subtype of `sup` then return `sub`. @@ -181,8 +179,6 @@ private class TypeVisitor var sup = self.resolve_mtype(ntype) if sup == null then return null # Forward error - var mmodule = self.nclassdef.mclassdef.mmodule - var anchor = self.nclassdef.mclassdef.bound_mtype if sup == sub then self.modelbuilder.warning(node, "Warning: Expression is already a {sup}.") else if self.is_subtype(sub, sup) and not sup.need_anchor then @@ -193,24 +189,22 @@ private class TypeVisitor fun try_get_mproperty_by_name2(anode: ANode, mtype: MType, name: String): nullable MProperty do - return self.modelbuilder.try_get_mproperty_by_name2(anode, self.nclassdef.mclassdef.mmodule, mtype, name) + return self.modelbuilder.try_get_mproperty_by_name2(anode, mmodule, mtype, name) end fun resolve_mtype(node: AType): nullable MType do - return self.modelbuilder.resolve_mtype(self.nclassdef, node) + return self.modelbuilder.resolve_mtype(mmodule, mclassdef, node) end fun try_get_mclass(node: ANode, name: String): nullable MClass do - var mmodule = self.nclassdef.mclassdef.mmodule var mclass = modelbuilder.try_get_mclass_by_name(node, mmodule, name) return mclass end fun get_mclass(node: ANode, name: String): nullable MClass do - var mmodule = self.nclassdef.mclassdef.mmodule var mclass = modelbuilder.try_get_mclass_by_name(node, mmodule, name) if mclass == null then self.modelbuilder.error(node, "Type Error: missing primitive class `{name}'.") @@ -247,8 +241,8 @@ private class TypeVisitor end assert mproperty isa MMethod - if mproperty.visibility == protected_visibility and not recv_is_self and self.mmodule.visibility_for(mproperty.intro_mclassdef.mmodule) < intrude_visibility then - self.modelbuilder.error(node, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}") + if mproperty.visibility == protected_visibility and not recv_is_self and self.mmodule.visibility_for(mproperty.intro_mclassdef.mmodule) < intrude_visibility and not modelbuilder.toolcontext.opt_ignore_visibility.value then + self.modelbuilder.error(node, "Error: Method '{name}' is protected and can only acceded by self.") return null end @@ -265,7 +259,8 @@ private class TypeVisitor end - var msignature = self.resolve_signature_for(mpropdef, recvtype, recv_is_self) + var msignature = mpropdef.msignature.as(not null) + msignature = resolve_for(msignature, recvtype, recv_is_self).as(MSignature) var erasure_cast = false var rettype = mpropdef.msignature.return_mtype @@ -279,7 +274,7 @@ private class TypeVisitor end end - var callsite = new CallSite(node, recvtype, recv_is_self, mproperty, mpropdef, msignature, erasure_cast) + var callsite = new CallSite(node, recvtype, mmodule, anchor, recv_is_self, mproperty, mpropdef, msignature, erasure_cast) return callsite end @@ -391,9 +386,15 @@ class CallSite # The assiciated node for location var node: ANode - # The statis type of the receiver + # The static type of the receiver (possibly unresolved) var recv: MType + # The module where the callsite is present + var mmodule: MModule + + # The anchor to use with `recv` or `msignature` + var anchor: nullable MClassType + # Is the receiver self? # If "for_self", virtual types of the signature are keeped # If "not_for_self", virtual type are erased @@ -478,17 +479,16 @@ redef class APropdef var selfvariable: nullable Variable end -redef class AConcreteMethPropdef +redef class AMethPropdef redef fun do_typing(modelbuilder: ModelBuilder) do - var nclassdef = self.parent.as(AClassdef) - var mpropdef = self.mpropdef.as(not null) - var v = new TypeVisitor(modelbuilder, nclassdef, mpropdef) - self.selfvariable = v.selfvariable - var nblock = self.n_block if nblock == null then return + var mpropdef = self.mpropdef.as(not null) + var v = new TypeVisitor(modelbuilder, mpropdef.mclassdef.mmodule, mpropdef) + self.selfvariable = v.selfvariable + var mmethoddef = self.mpropdef.as(not null) for i in [0..mmethoddef.msignature.arity[ do var mtype = mmethoddef.msignature.mparameters[i].mtype @@ -513,8 +513,8 @@ end redef class AAttrPropdef redef fun do_typing(modelbuilder: ModelBuilder) do - var nclassdef = self.parent.as(AClassdef) - var v = new TypeVisitor(modelbuilder, nclassdef, self.mpropdef.as(not null)) + var mpropdef = self.mpropdef.as(not null) + var v = new TypeVisitor(modelbuilder, mpropdef.mclassdef.mmodule, mpropdef) self.selfvariable = v.selfvariable var nexpr = self.n_expr @@ -529,7 +529,7 @@ end redef class AExpr # The static type of the expression. - # null if self is a statement of in case of error + # null if self is a statement or in case of error var mtype: nullable MType = null # Is the statement correctly typed? @@ -815,40 +815,35 @@ end redef class AForExpr var coltype: nullable MClassType - var method_iterator: nullable MMethod - var method_is_ok: nullable MMethod - var method_item: nullable MMethod - var method_next: nullable MMethod - var method_key: nullable MMethod + var method_iterator: nullable CallSite + var method_is_ok: nullable CallSite + var method_item: nullable CallSite + var method_next: nullable CallSite + var method_key: nullable CallSite private fun do_type_iterator(v: TypeVisitor, mtype: MType) do + if mtype isa MNullType then + v.error(self, "Type error: 'for' cannot iterate over 'null'") + return + end + # get obj class var objcla = v.get_mclass(self, "Object") if objcla == null then return # check iterator method - var unsafe_type = v.anchor_to(mtype) - if v.try_get_mproperty_by_name2(self, unsafe_type, "iterator") == null then - if v.try_get_mproperty_by_name2(self, unsafe_type, "iterate") == null then - v.error(self, "Type Error: Expected method 'iterator' in type {mtype}") - else - v.modelbuilder.error(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}") - end - return - end - var itdef = v.get_method(self, mtype, "iterator", true) if itdef == null then - v.error(self, "Type Error: Expected method 'iterator' in type {mtype}") + v.error(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.") return end - self.method_iterator = itdef.mproperty + self.method_iterator = itdef # check that iterator return something var ittype = itdef.msignature.return_mtype if ittype == null then - v.error(self, "Type Error: Expected method 'iterator' to return an Iterator or MapIterator type") + v.error(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.") return end @@ -863,7 +858,7 @@ redef class AForExpr var coltype = ittype.supertype_to(v.mmodule, v.anchor, colit_cla) var variables = self.variables if variables.length != 1 then - v.error(self, "Type Error: Expected one variable") + v.error(self, "Type Error: 'for' expects only one variable when using 'Iterator'.") else variables.first.declared_type = coltype.arguments.first end @@ -875,7 +870,7 @@ redef class AForExpr var coltype = ittype.supertype_to(v.mmodule, v.anchor, mapit_cla) var variables = self.variables if variables.length != 2 then - v.error(self, "Type Error: Expected two variables") + v.error(self, "Type Error: 'for' expects two variables when using 'MapIterator'.") else variables[0].declared_type = coltype.arguments[0] variables[1].declared_type = coltype.arguments[1] @@ -884,44 +879,45 @@ redef class AForExpr end if not is_col and not is_map then - v.error(self, "Type Error: Expected method 'iterator' to return an Iterator of MapIterator type") + v.error(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.") return end # anchor formal and virtual types if mtype.need_anchor then mtype = v.anchor_to(mtype) + if mtype isa MNullableType then mtype = mtype.mtype self.coltype = mtype.as(MClassType) # get methods is_ok, next, item var ikdef = v.get_method(self, ittype, "is_ok", false) if ikdef == null then - v.error(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}") + v.error(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.") return end - self.method_is_ok = ikdef.mproperty + self.method_is_ok = ikdef var itemdef = v.get_method(self, ittype, "item", false) if itemdef == null then - v.error(self, "Type Error: Expected method 'item' in Iterator type {ittype}") + v.error(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.") return end - self.method_item = itemdef.mproperty + self.method_item = itemdef var nextdef = v.get_method(self, ittype, "next", false) if nextdef == null then - v.error(self, "Type Error: Expected method 'next' in Iterator type {ittype}") + v.error(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.") return end - self.method_next = nextdef.mproperty + self.method_next = nextdef if is_map then var keydef = v.get_method(self, ittype, "key", false) if keydef == null then - v.error(self, "Type Error: Expected method 'key' in Iterator type {ittype}") + v.error(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.") return end - self.method_key = keydef.mproperty + self.method_key = keydef end end @@ -1072,6 +1068,9 @@ redef class ASuperstringExpr end redef class AArrayExpr + var with_capacity_callsite: nullable CallSite + var push_callsite: nullable CallSite + redef fun accept_typing(v) do var mtypes = new Array[nullable MType] @@ -1089,11 +1088,18 @@ redef class AArrayExpr end var mclass = v.get_mclass(self, "Array") if mclass == null then return # Forward error - self.mtype = mclass.get_mtype([mtype]) + var array_mtype = mclass.get_mtype([mtype]) + + with_capacity_callsite = v.get_method(self, array_mtype, "with_capacity", false) + push_callsite = v.get_method(self, array_mtype, "push", false) + + self.mtype = array_mtype end end redef class ARangeExpr + var init_callsite: nullable CallSite + redef fun accept_typing(v) do var discrete_class = v.get_mclass(self, "Discrete") @@ -1104,13 +1110,28 @@ redef class ARangeExpr if t1 == null or t2 == null then return var mclass = v.get_mclass(self, "Range") if mclass == null then return # Forward error + var mtype if v.is_subtype(t1, t2) then - self.mtype = mclass.get_mtype([t2]) + mtype = mclass.get_mtype([t2]) else if v.is_subtype(t2, t1) then - self.mtype = mclass.get_mtype([t1]) + mtype = mclass.get_mtype([t1]) else v.error(self, "Type Error: Cannot create range: {t1} vs {t2}") + return end + + self.mtype = mtype + + # get the constructor + var callsite + if self isa ACrangeExpr then + callsite = v.get_method(self, mtype, "init", false) + else if self isa AOrangeExpr then + callsite = v.get_method(self, mtype, "without_last", false) + else + abort + end + init_callsite = callsite end end @@ -1207,7 +1228,6 @@ redef class ASendExpr var msignature = callsite.msignature var args = compute_raw_arguments - self.raw_arguments = args callsite.check_signature(v, args) @@ -1231,7 +1251,7 @@ redef class ASendExpr private fun property_name: String is abstract # An array of all arguments (excluding self) - var raw_arguments: nullable Array[AExpr] + fun raw_arguments: Array[AExpr] do return compute_raw_arguments private fun compute_raw_arguments: Array[AExpr] is abstract end @@ -1368,7 +1388,6 @@ redef class ASendReassignFormExpr self.callsite = callsite var args = compute_raw_arguments - self.raw_arguments = args callsite.check_signature(v, args) @@ -1419,9 +1438,14 @@ redef class ASuperExpr # Note: if the super is a normal call-next-method, then this attribute is null var callsite: nullable CallSite + # The method to call is the super is a standard `call-next-method` super-call + # Note: if the super is a special super-init-call, then this attribute is null + var mpropdef: nullable MMethodDef + redef fun accept_typing(v) do - var recvtype = v.nclassdef.mclassdef.bound_mtype + var recvtype = v.anchor + assert recvtype != null var mproperty = v.mpropdef.mproperty if not mproperty isa MMethod then v.error(self, "Error: super only usable in a method") @@ -1438,9 +1462,9 @@ redef class ASuperExpr end # FIXME: covariance of return type in linear extension? var superprop = superprops.first - assert superprop isa MMethodDef - var msignature = v.resolve_signature_for(superprop, recvtype, true) + var msignature = superprop.msignature.as(not null) + msignature = v.resolve_for(msignature, recvtype, true).as(MSignature) var args = self.n_args.to_a if args.length > 0 then v.check_signature(self, args, mproperty.name, msignature) @@ -1448,14 +1472,18 @@ redef class ASuperExpr self.mtype = msignature.return_mtype self.is_typed = true v.mpropdef.has_supercall = true + mpropdef = v.mpropdef.as(MMethodDef) end private fun process_superinit(v: TypeVisitor) do - var recvtype = v.nclassdef.mclassdef.bound_mtype - var mproperty = v.mpropdef.mproperty + var recvtype = v.anchor + assert recvtype != null + var mpropdef = v.mpropdef + assert mpropdef isa MMethodDef + var mproperty = mpropdef.mproperty var superprop: nullable MMethodDef = null - for msupertype in v.nclassdef.mclassdef.supertypes do + for msupertype in mpropdef.mclassdef.supertypes do msupertype = msupertype.anchor_to(v.mmodule, recvtype) var errcount = v.modelbuilder.toolcontext.error_count var candidate = v.try_get_mproperty_by_name2(self, msupertype, mproperty.name).as(nullable MMethod) @@ -1483,15 +1511,31 @@ redef class ASuperExpr return end - var msignature = v.resolve_signature_for(superprop, recvtype, true) - var callsite = new CallSite(self, recvtype, true, superprop.mproperty, superprop, msignature, false) + var msignature = superprop.msignature.as(not null) + msignature = v.resolve_for(msignature, recvtype, true).as(MSignature) + + var callsite = new CallSite(self, recvtype, v.mmodule, v.anchor, true, superprop.mproperty, superprop, msignature, false) self.callsite = callsite var args = self.n_args.to_a if args.length > 0 then callsite.check_signature(v, args) else - # TODO: Check signature + # Check there is at least enough parameters + if mpropdef.msignature.arity < msignature.arity then + v.error(self, "Error: Not enough implicit arguments to pass. Got {mpropdef.msignature.arity}, expected at least {msignature.arity}. Signature is {msignature}") + return + end + # Check that each needed parameter is conform + var i = 0 + for sp in msignature.mparameters do + var p = mpropdef.msignature.mparameters[i] + if not v.is_subtype(p.mtype, sp.mtype) then + v.error(self, "Type error: expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}. Signature is {msignature}") + return + end + i += 1 + end end self.is_typed = true