X-Git-Url: http://nitlanguage.org diff --git a/src/modelize_property.nit b/src/modelize_property.nit index 5a0ece8..8273fb8 100644 --- a/src/modelize_property.nit +++ b/src/modelize_property.nit @@ -18,6 +18,7 @@ module modelize_property import modelize_class +import annotation redef class ToolContext var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase]) @@ -118,8 +119,17 @@ redef class ModelBuilder var mparameters = new Array[MParameter] var anode: nullable ANode = null for npropdef in nclassdef.n_propdefs do - if npropdef isa AAttrPropdef and npropdef.n_expr == null then + if npropdef isa AAttrPropdef then if npropdef.mpropdef == null then return # Skip broken attribute + var at = npropdef.get_single_annotation("noinit", self) + if at != null then + npropdef.noinit = true + if npropdef.n_expr != null then + self.error(at, "Error: `noinit` attributes cannot have an initial value") + end + continue # Skip noinit attributes + end + if npropdef.n_expr != null then continue var paramname = npropdef.mpropdef.mproperty.name.substring_from(1) var ret_type = npropdef.mpropdef.static_mtype if ret_type == null then return @@ -177,7 +187,7 @@ redef class ModelBuilder # It is a case-by case var vis_type: nullable MVisibility = null # The own visibility of the type var mmodule_type: nullable MModule = null # The origial module of the type - if mtype isa MNullableType then mtype = mtype.mtype + mtype = mtype.as_notnullable if mtype isa MClassType then vis_type = mtype.mclass.visibility mmodule_type = mtype.mclass.intro.mmodule @@ -205,7 +215,11 @@ redef class ModelBuilder # No error, try to go deeper in generic types if node isa AType then - for a in node.n_types do check_visibility(a, a.mtype.as(not null), mpropdef) + for a in node.n_types do + var t = a.mtype + if t == null then continue # Error, thus skipped + check_visibility(a, t, mpropdef) + end else if mtype isa MGenericType then for t in mtype.arguments do check_visibility(node, t, mpropdef) end @@ -628,10 +642,23 @@ end redef class AAttrPropdef redef type MPROPDEF: MAttributeDef + # Is the node tagged `noinit`? + var noinit = false + + # Is the node taggeg lazy? + var is_lazy = false + + # The guard associated to a lasy attribute. + # Because some engines does not have a working `isset`, + # this additionnal attribute is used to guard the lazy initialization. + # TODO: to remove once isset is correctly implemented + var mlazypropdef: nullable MAttributeDef + # The associated getter (read accessor) if any var mreadpropdef: nullable MMethodDef writable # The associated setter (write accessor) if any var mwritepropdef: nullable MMethodDef writable + redef fun build_property(modelbuilder, mclassdef) do var mclass = mclassdef.mclass @@ -678,7 +705,7 @@ redef class AAttrPropdef else # New attribute style var nid2 = self.n_id2.as(not null) - var mprop = new MAttribute(mclassdef, "@" + name, none_visibility) + var mprop = new MAttribute(mclassdef, "_" + name, private_visibility) var mpropdef = new MAttributeDef(mclassdef, mprop, self.location) self.mpropdef = mpropdef modelbuilder.mpropdef2npropdef[mpropdef] = self @@ -701,15 +728,44 @@ redef class AAttrPropdef modelbuilder.mpropdef2npropdef[mreadpropdef] = self mreadpropdef.mdoc = mpropdef.mdoc + var atlazy = self.get_single_annotation("lazy", modelbuilder) + if atlazy != null then + if n_expr == null then + modelbuilder.error(atlazy, "Error: a lazy attribute needs a value") + end + is_lazy = true + var mlazyprop = new MAttribute(mclassdef, "lazy _" + name, none_visibility) + var mlazypropdef = new MAttributeDef(mclassdef, mlazyprop, self.location) + self.mlazypropdef = mlazypropdef + end + + var atreadonly = self.get_single_annotation("readonly", modelbuilder) + if atreadonly != null then + if n_expr == null then + modelbuilder.error(atreadonly, "Error: a readonly attribute needs a value") + end + # No setter, so just leave + return + end + var writename = name + "=" var nwritable = self.n_writable + var atwritable = self.get_single_annotation("writable", modelbuilder) + if atwritable != null then + if not atwritable.n_args.is_empty then + writename = atwritable.arg_as_id(modelbuilder) or else writename + end + end var mwriteprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, writename).as(nullable MMethod) var nwkwredef: nullable Token = null if nwritable != null then nwkwredef = nwritable.n_kwredef + if atwritable != null then nwkwredef = atwritable.n_kwredef if mwriteprop == null then var mvisibility if nwritable != null then mvisibility = new_property_visibility(modelbuilder, mclassdef, nwritable.n_visibility) + else if atwritable != null then + mvisibility = new_property_visibility(modelbuilder, mclassdef, atwritable.n_visibility) else mvisibility = private_visibility end @@ -719,6 +775,8 @@ redef class AAttrPropdef if not self.check_redef_keyword(modelbuilder, mclassdef, nwkwredef or else n_kwredef, true, mwriteprop) then return if nwritable != null then check_redef_property_visibility(modelbuilder, nwritable.n_visibility, mwriteprop) + else if atwritable != null then + check_redef_property_visibility(modelbuilder, atwritable.n_visibility, mwriteprop) end end mclassdef.mprop2npropdef[mwriteprop] = self @@ -738,12 +796,21 @@ redef class AAttrPropdef var mmodule = mclassdef.mmodule var mtype: nullable MType = null + var mreadpropdef = self.mreadpropdef + var ntype = self.n_type if ntype != null then mtype = modelbuilder.resolve_mtype(mmodule, mclassdef, ntype) if mtype == null then return end + # Inherit the type from the getter (usually an abstact getter) + if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then + var msignature = mreadpropdef.mproperty.intro.msignature + if msignature == null then return # Error, thus skiped + mtype = msignature.return_mtype + end + var nexpr = self.n_expr if mtype == null then if nexpr != null then @@ -771,11 +838,9 @@ redef class AAttrPropdef modelbuilder.error(self, "Error: Untyped attribute {mpropdef}. Implicit typing allowed only for literals and new.") end - else - modelbuilder.error(self, "Error: Untyped attribute {mpropdef}") + if mtype == null then return end - else - assert ntype != null + else if ntype != null then if nexpr isa ANewExpr then var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type) if xmtype == mtype and modelbuilder.toolcontext.opt_warn.value >= 2 then @@ -784,17 +849,19 @@ redef class AAttrPropdef end end - if mtype == null then return + if mtype == null then + modelbuilder.error(self, "Error: Untyped attribute {mpropdef}") + return + end mpropdef.static_mtype = mtype - var mreadpropdef = self.mreadpropdef if mreadpropdef != null then var msignature = new MSignature(new Array[MParameter], mtype) mreadpropdef.msignature = msignature end - var msritepropdef = self.mwritepropdef + var mwritepropdef = self.mwritepropdef if mwritepropdef != null then var name: String if n_id != null then @@ -806,6 +873,11 @@ redef class AAttrPropdef var msignature = new MSignature([mparameter], null) mwritepropdef.msignature = msignature end + + var mlazypropdef = self.mlazypropdef + if mlazypropdef != null then + mlazypropdef.static_mtype = modelbuilder.model.get_mclasses_by_name("Bool").first.mclass_type + end end redef fun check_signature(modelbuilder) @@ -929,6 +1001,11 @@ redef class ATypePropdef self.mpropdef = mpropdef modelbuilder.mpropdef2npropdef[mpropdef] = self set_doc(mpropdef) + + var atfixed = get_single_annotation("fixed", modelbuilder) + if atfixed != null then + mpropdef.is_fixed = true + end end redef fun build_signature(modelbuilder) @@ -955,27 +1032,46 @@ redef class ATypePropdef var bound = self.mpropdef.bound if bound == null then return # Error thus skiped - modelbuilder.check_visibility(n_type.as(not null), bound, mpropdef) - - # Fast case: the bound is not a formal type - if not bound isa MVirtualType then return + modelbuilder.check_visibility(n_type, bound, mpropdef) var mclassdef = mpropdef.mclassdef var mmodule = mclassdef.mmodule var anchor = mclassdef.bound_mtype - # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type - var seen = [self.mpropdef.mproperty.mvirtualtype] - loop - if seen.has(bound) then + # Check circularity + if bound isa MVirtualType then + # Slow case: progress on each resolution until: (i) we loop, or (ii) we found a non formal type + var seen = [self.mpropdef.mproperty.mvirtualtype] + loop + if seen.has(bound) then + seen.add(bound) + modelbuilder.error(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}") + return + end seen.add(bound) - modelbuilder.error(self, "Error: circularity of virtual type definition: {seen.join(" -> ")}") - return + var next = bound.lookup_bound(mmodule, anchor) + if not next isa MVirtualType then break + bound = next + end + end + + # Check redefinitions + bound = mpropdef.bound.as(not null) + for p in mpropdef.mproperty.lookup_super_definitions(mmodule, anchor) do + var supbound = p.bound.as(not null) + if p.is_fixed then + modelbuilder.error(self, "Redef Error: Virtual type {mpropdef.mproperty} is fixed in super-class {p.mclassdef.mclass}") + break + end + if p.mclassdef.mclass == mclassdef.mclass then + # Still a warning to pass existing bad code + modelbuilder.warning(n_type, "Redef Error: a virtual type cannot be refined.") + break + end + if not bound.is_subtype(mmodule, anchor, supbound) then + modelbuilder.error(n_type, "Redef Error: Wrong bound type. Found {bound}, expected a subtype of {supbound}, as in {p}.") + break end - seen.add(bound) - var next = bound.lookup_bound(mmodule, anchor) - if not next isa MVirtualType then break - bound = next end end end