redef class ModelBuilder
# Register the npropdef associated to each mpropdef
- # FIXME: why not refine the MPropDef class with a nullable attribute?
+ # FIXME: why not refine the `MPropDef` class with a nullable attribute?
var mpropdef2npropdef: HashMap[MPropDef, APropdef] = new HashMap[MPropDef, APropdef]
- # Build the properties of `nclassdef'.
+ # Build the properties of `nclassdef`.
# REQUIRE: all superclasses are built.
private fun build_properties(nclassdef: AClassdef)
do
end
# Introduce or inherit default constructor
- # This is the last part of `build_properties'.
+ # This is the last part of `build_properties`.
private fun process_default_constructors(nclassdef: AClassdef)
do
var mclassdef = nclassdef.mclassdef.as(not null)
nclassdef.mfree_init = mpropdef
self.toolcontext.info("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
end
+
+ # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
+ fun check_visibility(node: ANode, mtype: MType, mpropdef: MPropDef)
+ do
+ var mmodule = mpropdef.mclassdef.mmodule
+ var mproperty = mpropdef.mproperty
+
+ # Extract visibility information of the main part of `mtype`
+ # 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
+ if mtype isa MClassType then
+ vis_type = mtype.mclass.visibility
+ mmodule_type = mtype.mclass.intro.mmodule
+ else if mtype isa MVirtualType then
+ vis_type = mtype.mproperty.visibility
+ mmodule_type = mtype.mproperty.intro_mclassdef.mmodule
+ else if mtype isa MParameterType then
+ # nothing, always visible
+ else
+ node.debug "Unexpected type {mtype}"
+ abort
+ end
+
+ if vis_type != null then
+ assert mmodule_type != null
+ var vis_module_type = mmodule.visibility_for(mmodule_type) # the visibility of the original module
+ if mproperty.visibility > vis_type then
+ error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the {vis_type} type `{mtype}`")
+ return
+ else if mproperty.visibility > vis_module_type then
+ error(node, "Error: The {mproperty.visibility} property `{mproperty}` cannot contain the type `{mtype}` from the {vis_module_type} module `{mmodule_type}`")
+ return
+ end
+ end
+
+ # 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)
+ else if mtype isa MGenericType then
+ for t in mtype.arguments do check_visibility(node, t, mpropdef)
+ end
+ end
end
redef class MClass
var inherit_init_from: nullable MClass = null
end
+redef class MClassDef
+ private var propdef_names = new HashSet[String]
+end
+
+redef class MPropDef
+ # Does the MPropDef contains a call to super or a call of a super-constructor?
+ # Subsequent phases of the frontend (esp. typing) set it if required
+ var has_supercall: Bool writable = false
+end
+
redef class AClassdef
var build_properties_is_done: Bool = false
# The list of super-constructor to call at the start of the free constructor
# The free init (implicitely constructed by the class if required)
var mfree_init: nullable MMethodDef = null
- # What is the APropdef associated to a MProperty?
+ # What is the `APropdef` associated to a `MProperty`?
# Used to check multiple definition of a property.
var mprop2npropdef: Map[MProperty, APropdef] = new HashMap[MProperty, APropdef]
end
# The associated main model entity
type MPROPDEF: MPropDef
- # The associated propdef once build by a `ModelBuilder'
+ # The associated propdef once build by a `ModelBuilder`
var mpropdef: nullable MPROPDEF writable
private fun build_property(modelbuilder: ModelBuilder, nclassdef: AClassdef) is abstract
private fun new_property_visibility(modelbuilder: ModelBuilder, nclassdef: AClassdef, nvisibility: nullable AVisibility): MVisibility
do
var mvisibility = public_visibility
- if nvisibility != null then mvisibility = nvisibility.mvisibility
+ if nvisibility != null then
+ mvisibility = nvisibility.mvisibility
+ if mvisibility == intrude_visibility then
+ modelbuilder.error(nvisibility, "Error: intrude is not a legal visibility for properties.")
+ mvisibility = public_visibility
+ end
+ end
if nclassdef.mclassdef.mclass.visibility == private_visibility then
if mvisibility == protected_visibility then
assert nvisibility != null
return mvisibility
end
+ private fun set_doc(mpropdef: MPropDef)
+ do
+ var ndoc = self.n_doc
+ if ndoc != null then mpropdef.mdoc = ndoc.to_mdoc
+ end
+
private fun check_redef_property_visibility(modelbuilder: ModelBuilder, nclassdef: AClassdef, nvisibility: nullable AVisibility, mprop: MProperty)
do
if nvisibility == null then return
if self.ret_type == null then return false # Skip errir
end
- for nclosure in self.n_closure_decls do
- if not nclosure.n_signature.visit_signature(modelbuilder, nclassdef) then return false
- end
-
self.is_visited = true
return true
end
var mparameters = new Array[MParameter]
for i in [0..param_names.length[ do
var mparameter = new MParameter(param_names[i], param_types[i], i == vararg_rank)
+ self.n_params[i].mparameter = mparameter
mparameters.add(mparameter)
end
end
end
+redef class AParam
+ # The associated mparameter if any
+ var mparameter: nullable MParameter = null
+end
+
redef class AMethPropdef
redef type MPROPDEF: MMethodDef
- # The associated super init if any
- var super_init: nullable MMethod
redef fun build_property(modelbuilder, nclassdef)
do
var is_init = self isa AInitPropdef
var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
+ if mclassdef.propdef_names.has(mprop.name) then
+ var loc: nullable Location = null
+ for i in mclassdef.mpropdefs do
+ if i.mproperty.name == mprop.name then
+ loc = i.location
+ break
+ end
+ end
+ if loc == null then abort
+ modelbuilder.error(self, "Error: a property {mprop} is already defined in class {mclassdef.mclass} at {loc}")
+ end
+
+ mclassdef.propdef_names.add(mpropdef.mproperty.name)
+
+ set_doc(mpropdef)
+
self.mpropdef = mpropdef
modelbuilder.mpropdef2npropdef[mpropdef] = self
if mpropdef.is_intro then
var mparameters = new Array[MParameter]
for i in [0..param_names.length[ do
var mparameter = new MParameter(param_names[i], param_types[i], i == vararg_rank)
+ if nsig != null then nsig.n_params[i].mparameter = mparameter
mparameters.add(mparameter)
end
msignature = new MSignature(mparameters, ret_type)
mpropdef.msignature = msignature
-
- if nsig != null then
- for nclosure in nsig.n_closure_decls do
- var clos_signature = nclosure.n_signature.build_signature(modelbuilder, nclassdef)
- if clos_signature == null then return
- var mparameter = new MParameter(nclosure.n_id.text, clos_signature, false)
- msignature.mclosures.add(mparameter)
- end
- end
-
+ mpropdef.is_abstract = self isa ADeferredMethPropdef
end
redef fun check_signature(modelbuilder, nclassdef)
for i in [0..mysignature.arity[ do
var myt = mysignature.mparameters[i].mtype
var prt = msignature.mparameters[i].mtype
- if not myt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, prt) and
+ if not myt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, prt) or
not prt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, myt) then
- modelbuilder.error(nsig.n_params[i], "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt}.")
+ modelbuilder.error(nsig.n_params[i], "Redef Error: Wrong type for parameter `{mysignature.mparameters[i].name}'. found {myt}, expected {prt} as in {mpropdef.mproperty.intro}.")
end
end
end
# Inherit the return type
ret_type = precursor_ret_type
else if not ret_type.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, precursor_ret_type) then
- modelbuilder.error(nsig.n_type.as(not null), "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type}.")
+ modelbuilder.error(nsig.n_type.as(not null), "Redef Error: Wrong return type. found {ret_type}, expected {precursor_ret_type} as in {mpropdef.mproperty.intro}.")
end
end
end
+
+ if mysignature.arity > 0 then
+ # Check parameters visibility
+ for i in [0..mysignature.arity[ do
+ var nt = nsig.n_params[i].n_type
+ if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
+ end
+ var nt = nsig.n_type
+ if nt != null then modelbuilder.check_visibility(nt, nt.mtype.as(not null), mpropdef)
+ end
end
end
var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
self.mpropdef = mpropdef
modelbuilder.mpropdef2npropdef[mpropdef] = self
+ set_doc(mpropdef)
var nreadable = self.n_readable
if nreadable != null then
var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
self.mreadpropdef = mreadpropdef
modelbuilder.mpropdef2npropdef[mreadpropdef] = self
+ mreadpropdef.mdoc = mpropdef.mdoc
end
var nwritable = self.n_writable
var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
self.mwritepropdef = mwritepropdef
modelbuilder.mpropdef2npropdef[mwritepropdef] = self
+ mwritepropdef.mdoc = mpropdef.mdoc
end
else
# New attribute style
var mpropdef = new MAttributeDef(mclassdef, mprop, self.location)
self.mpropdef = mpropdef
modelbuilder.mpropdef2npropdef[mpropdef] = self
+ set_doc(mpropdef)
var readname = name
var mreadprop = modelbuilder.try_get_mproperty_by_name(nid2, mclassdef, readname).as(nullable MMethod)
var mreadpropdef = new MMethodDef(mclassdef, mreadprop, self.location)
self.mreadpropdef = mreadpropdef
modelbuilder.mpropdef2npropdef[mreadpropdef] = self
+ mreadpropdef.mdoc = mpropdef.mdoc
var writename = name + "="
var nwritable = self.n_writable
var mwritepropdef = new MMethodDef(mclassdef, mwriteprop, self.location)
self.mwritepropdef = mwritepropdef
modelbuilder.mpropdef2npropdef[mwritepropdef] = self
+ mwritepropdef.mdoc = mpropdef.mdoc
end
end
if mtype == null then return
end
+ var nexpr = self.n_expr
if mtype == null then
- var nexpr = self.n_expr
if nexpr != null then
if nexpr isa ANewExpr then
mtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
else
modelbuilder.error(self, "Error: Untyped attribute {mpropdef}")
end
+ else
+ assert ntype != null
+ if nexpr isa ANewExpr then
+ var xmtype = modelbuilder.resolve_mtype(nclassdef, nexpr.n_type)
+ if xmtype == mtype and modelbuilder.toolcontext.opt_warn.value >= 2 then
+ modelbuilder.warning(ntype, "Warning: useless type definition")
+ end
+ end
end
if mtype == null then return
# Check getter and setter
var meth = self.mreadpropdef
- if meth != null then self.check_method_signature(modelbuilder, nclassdef, meth)
+ if meth != null then
+ self.check_method_signature(modelbuilder, nclassdef, meth)
+ var node: nullable ANode = ntype
+ if node == null then node = self
+ modelbuilder.check_visibility(node, mtype, meth)
+ end
meth = self.mwritepropdef
- if meth != null then self.check_method_signature(modelbuilder, nclassdef, meth)
+ if meth != null then
+ self.check_method_signature(modelbuilder, nclassdef, meth)
+ var node: nullable ANode = ntype
+ if node == null then node = self
+ modelbuilder.check_visibility(node, mtype, meth)
+ end
end
private fun check_method_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef, mpropdef: MMethodDef)
if mprop == null then
var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
mprop = new MVirtualTypeProp(mclassdef, name, mvisibility)
+ for c in name do if c >= 'a' and c<= 'z' then
+ modelbuilder.warning(n_id, "Warning: lowercase in the virtual type {name}")
+ break
+ end
if not self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, false, mprop) then return
else
if not self.check_redef_keyword(modelbuilder, nclassdef, self.n_kwredef, true, mprop) then return
var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
self.mpropdef = mpropdef
+ set_doc(mpropdef)
end
redef fun build_signature(modelbuilder, nclassdef)
redef fun check_signature(modelbuilder, nclassdef)
do
+ var mpropdef = self.mpropdef
+ if mpropdef == null then return # Error thus skiped
+
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
end
seen.add(bound)
var next = bound.lookup_bound(mmodule, anchor)
- if not next isa MVirtualType then return
+ if not next isa MVirtualType then break
bound = next
end
end