-redef class AClassdef
- # The associated MClass once build by a `ModelBuilder'
- var mclass: nullable MClass
- # The associated MClassDef once build by a `ModelBuilder'
- var mclassdef: nullable MClassDef
- var build_properties_is_done: Bool = false
- # The list of super-constructor to call at the start of the free constructor
- # FIXME: this is needed to implement the crazy constructor thing of the of old compiler. We need to think what to do with since this cannot stay in the modelbuilder
- var super_inits: nullable Collection[MMethod] = null
-
- # The free init (implicitely constructed by the class if required)
- var mfree_init: nullable MMethodDef = null
-end
-
-redef class AClasskind
- # The class kind associated with the AST node class
- private fun mkind: MClassKind is abstract
-end
-redef class AConcreteClasskind
- redef fun mkind do return concrete_kind
-end
-redef class AAbstractClasskind
- redef fun mkind do return abstract_kind
-end
-redef class AInterfaceClasskind
- redef fun mkind do return interface_kind
-end
-redef class AEnumClasskind
- redef fun mkind do return enum_kind
-end
-redef class AExternClasskind
- redef fun mkind do return extern_kind
-end
-
-redef class AVisibility
- # The visibility level associated with the AST node class
- private fun mvisibility: MVisibility is abstract
-end
-redef class AIntrudeVisibility
- redef fun mvisibility do return intrude_visibility
-end
-redef class APublicVisibility
- redef fun mvisibility do return public_visibility
-end
-redef class AProtectedVisibility
- redef fun mvisibility do return protected_visibility
-end
-redef class APrivateVisibility
- redef fun mvisibility do return private_visibility
-end
-
-
-#
-
-redef class Prod
- # Join the text of all tokens
- # Used to get the 'real name' of method definitions.
- fun collect_text: String
- do
- var v = new TextCollectorVisitor
- v.enter_visit(self)
- assert v.text != ""
- return v.text
- end
-end
-
-private class TextCollectorVisitor
- super Visitor
- var text: String = ""
- redef fun visit(n)
- do
- if n isa Token then text += n.text
- n.visit_all(self)
- end
-end
-
-redef class APropdef
- private fun build_property(modelbuilder: ModelBuilder, nclassdef: AClassdef)
- do
- end
- private fun build_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef)
- do
- end
- private fun check_signature(modelbuilder: ModelBuilder, nclassdef: AClassdef)
- do
- end
- 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 nclassdef.mclassdef.mclass.visibility == private_visibility then
- if mvisibility == protected_visibility then
- assert nvisibility != null
- modelbuilder.error(nvisibility, "Error: The only legal visibility for properties in a private class is private.")
- else if mvisibility == private_visibility then
- assert nvisibility != null
- # Not yet
- # modelbuilder.warning(nvisibility, "Warning: private is unrequired since the only legal visibility for properties in a private class is private.")
- end
- mvisibility = private_visibility
- end
- return mvisibility
- end
-
- private fun check_redef_property_visibility(modelbuilder: ModelBuilder, nclassdef: AClassdef, nvisibility: nullable AVisibility, mprop: MProperty)
- do
- if nvisibility == null then return
- var mvisibility = nvisibility.mvisibility
- if mvisibility != mprop.visibility and mvisibility != public_visibility then
- modelbuilder.error(nvisibility, "Error: redefinition changed the visibility from a {mprop.visibility} to a {mvisibility}")
- end
- end
-
- private fun check_redef_keyword(modelbuilder: ModelBuilder, nclassdef: AClassdef, kwredef: nullable Token, need_redef: Bool, mprop: MProperty)
- do
- if kwredef == null then
- if need_redef then
- modelbuilder.error(self, "Redef error: {nclassdef.mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
- end
- else
- if not need_redef then
- modelbuilder.error(self, "Error: No property {nclassdef.mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
- end
- end
- end
-end
-
-redef class AMethPropdef
- # The associated MMethodDef once build by a `ModelBuilder'
- var mpropdef: nullable 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 mclassdef = nclassdef.mclassdef.as(not null)
- var name: String
- var amethodid = self.n_methid
- var name_node: ANode
- if amethodid == null then
- if self isa AMainMethPropdef then
- name = "main"
- name_node = self
- else if self isa AConcreteInitPropdef then
- name = "init"
- name_node = self.n_kwinit
- else if self isa AExternInitPropdef then
- name = "new"
- name_node = self.n_kwnew
- else
- abort
- end
- else if amethodid isa AIdMethid then
- name = amethodid.n_id.text
- name_node = amethodid
- else
- # operator, bracket or assign
- name = amethodid.collect_text
- name_node = amethodid
-
- if name == "-" and self.n_signature.n_params.length == 0 then
- name = "unary -"
- end
- end
-
- var mprop: nullable MMethod = null
- if not is_init or n_kwredef != null then mprop = modelbuilder.try_get_mproperty_by_name(name_node, mclassdef, name).as(nullable MMethod)
- if mprop == null then
- var mvisibility = new_property_visibility(modelbuilder, nclassdef, self.n_visibility)
- mprop = new MMethod(mclassdef, name, mvisibility)
- mprop.is_init = is_init
- mprop.is_new = self isa AExternInitPropdef
- self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, false, mprop)
- else
- if n_kwredef == null then
- if self isa AMainMethPropdef then
- # no warning
- else
- self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, true, mprop)
- end
- end
- check_redef_property_visibility(modelbuilder, nclassdef, self.n_visibility, mprop)
- end
-
- var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
-
- self.mpropdef = mpropdef
- modelbuilder.mpropdef2npropdef[mpropdef] = self
- if mpropdef.is_intro then
- modelbuilder.toolcontext.info("{mpropdef} introduces new method {mprop.full_name}", 3)
- else
- modelbuilder.toolcontext.info("{mpropdef} redefines method {mprop.full_name}", 3)
- end
- end
-
- redef fun build_signature(modelbuilder, nclassdef)
- do
- var mpropdef = self.mpropdef
- if mpropdef == null then return # Error thus skiped
- var mmodule = mpropdef.mclassdef.mmodule
- var nsig = self.n_signature
-
- # Retrieve info from the signature AST
- var param_names = new Array[String] # Names of parameters from the AST
- var param_types = new Array[MType] # Types of parameters from the AST
- var vararg_rank = -1
- var ret_type: nullable MType = null # Return type from the AST
- if nsig != null then
- for np in nsig.n_params do
- param_names.add(np.n_id.text)
- var ntype = np.n_type
- if ntype != null then
- var mtype = modelbuilder.resolve_mtype(nclassdef, ntype)
- if mtype == null then return # Skip error
- for i in [0..param_names.length-param_types.length[ do
- param_types.add(mtype)
- end
- if np.n_dotdotdot != null then
- if vararg_rank != -1 then
- modelbuilder.error(np, "Error: {param_names[vararg_rank]} is already a vararg")
- else
- vararg_rank = param_names.length - 1
- end
- end
- end
- end
- var ntype = nsig.n_type
- if ntype != null then
- ret_type = modelbuilder.resolve_mtype(nclassdef, ntype)
- if ret_type == null then return # Skip errir
- end
- end
-
- # Look for some signature to inherit
- # FIXME: do not inherit from the intro, but from the most specific
- var msignature: nullable MSignature = null
- if not mpropdef.is_intro then
- msignature = mpropdef.mproperty.intro.msignature
- if msignature == null then return # Skip error
- else if mpropdef.mproperty.is_init then
- # FIXME UGLY: inherit signature from a super-constructor
- for msupertype in nclassdef.mclassdef.supertypes do
- msupertype = msupertype.anchor_to(mmodule, nclassdef.mclassdef.bound_mtype)
- var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
- if candidate != null then
- if msignature == null then
- msignature = candidate.intro.as(MMethodDef).msignature
- end
- end
- end
- end
-
- # Inherit the signature
- if msignature != null and param_names.length != param_types.length and param_names.length == msignature.arity and param_types.length == 0 then
- # Parameters are untyped, thus inherit them
- param_types = msignature.parameter_mtypes
- vararg_rank = msignature.vararg_rank
- end
- if msignature != null and ret_type == null then
- ret_type = msignature.return_mtype
- end
-
- if param_names.length != param_types.length then
- # Some parameters are typed, other parameters are not typed.
- modelbuilder.warning(nsig.n_params[param_types.length], "Error: Untyped parameter `{param_names[param_types.length]}'.")
- return
- end
-
- msignature = new MSignature(param_names, param_types, ret_type, vararg_rank)
- mpropdef.msignature = msignature
- end
-
- redef fun check_signature(modelbuilder, nclassdef)
- do
- var mpropdef = self.mpropdef
- if mpropdef == null then return # Error thus skiped
- var mmodule = mpropdef.mclassdef.mmodule
- var nsig = self.n_signature
- var mysignature = self.mpropdef.msignature
- if mysignature == null then return # Error thus skiped
-
- # Lookup for signature in the precursor
- # FIXME all precursors should be considered
- if not mpropdef.is_intro then
- var msignature = mpropdef.mproperty.intro.msignature
- if msignature == null then return
-
- if mysignature.arity != msignature.arity then
- var node: ANode
- if nsig != null then node = nsig else node = self
- modelbuilder.error(node, "Redef Error: {mysignature.arity} parameters found, {msignature.arity} expected. Signature is {mpropdef}{msignature}")
- return
- end
- var precursor_ret_type = msignature.return_mtype
- var ret_type = mysignature.return_mtype
- if ret_type != null and precursor_ret_type == null then
- modelbuilder.error(nsig.n_type.as(not null), "Redef Error: {mpropdef.mproperty} is a procedure, not a function.")
- return
- end
-
- if mysignature.arity > 0 then
- # Check parameters types
- for i in [0..mysignature.arity[ do
- var myt = mysignature.parameter_mtypes[i]
- var prt = msignature.parameter_mtypes[i]
- if not myt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, prt) and
- not prt.is_subtype(mmodule, nclassdef.mclassdef.bound_mtype, myt) then
- modelbuilder.error(nsig.n_params[i], "Redef Error: Wrong type for parameter `{mysignature.parameter_names[i]}'. found {myt}, expected {prt}.")
- end
- end
- end
- if precursor_ret_type != null then
- if ret_type == null then
- # 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}.")
- end
- end
- end
- end