Merge: Enforce namespace rules
[nit.git] / src / modelize / modelize_property.nit
index e04fd81..d8fd96d 100644 (file)
 # Analysis and verification of property definitions to instantiate model element
 module modelize_property
 
-import modelize_class
+intrude import modelize_class
 private import annotation
 
 redef class ToolContext
+       # Run `AClassdef::build_property` on the classdefs of each module
        var modelize_property_phase: Phase = new ModelizePropertyPhase(self, [modelize_class_phase])
 end
 
@@ -36,9 +37,47 @@ private class ModelizePropertyPhase
 end
 
 redef class ModelBuilder
-       # Register the npropdef associated to each mpropdef
-       # FIXME: why not refine the `MPropDef` class with a nullable attribute?
-       var mpropdef2npropdef = new HashMap[MPropDef, APropdef]
+       # Registration of the npropdef associated to each mpropdef.
+       #
+       # Public clients need to use `mpropdef2node` to access stuff.
+       private var mpropdef2npropdef = new HashMap[MPropDef, APropdef]
+
+       # Retrieve the associated AST node of a mpropertydef.
+       # This method is used to associate model entity with syntactic entities.
+       #
+       # If the property definition is not associated with a node, returns `null`.
+       fun mpropdef2node(mpropdef: MPropDef): nullable ANode
+       do
+               var res
+               res = mpropdef2npropdef.get_or_null(mpropdef)
+               if res != null then
+                       # Run the phases on it
+                       toolcontext.run_phases_on_npropdef(res)
+                       return res
+               end
+               if mpropdef isa MMethodDef and mpropdef.mproperty.is_root_init then
+                       res = mclassdef2nclassdef.get_or_null(mpropdef.mclassdef)
+                       if res != null then return res
+               end
+               return null
+       end
+
+       # Retrieve all the attributes nodes localy definied
+       # FIXME think more about this method and how the separations separate/global and ast/model should be done.
+       fun collect_attr_propdef(mclassdef: MClassDef): Array[AAttrPropdef]
+       do
+               var res = new Array[AAttrPropdef]
+               var n = mclassdef2nclassdef.get_or_null(mclassdef)
+               if n == null then return res
+               for npropdef in n.n_propdefs do
+                       if npropdef isa AAttrPropdef then
+                               # Run the phases on it
+                               toolcontext.run_phases_on_npropdef(npropdef)
+                               res.add(npropdef)
+                       end
+               end
+               return res
+       end
 
        # Build the properties of `nclassdef`.
        # REQUIRE: all superclasses are built.
@@ -293,7 +332,8 @@ redef class MPropDef
 end
 
 redef class AClassdef
-       var build_properties_is_done = false
+       # Marker used in `ModelBuilder::build_properties`
+       private var build_properties_is_done = false
 
        # The free init (implicitely constructed by the class if required)
        var mfree_init: nullable MMethodDef = null
@@ -439,11 +479,24 @@ redef class APropdef
                        return false
 
                end
+               if mprop isa MMethod and mprop.is_root_init then return true
                if kwredef == null then
                        if need_redef then
                                modelbuilder.error(self, "Redef error: {mclassdef.mclass}::{mprop.name} is an inherited property. To redefine it, add the redef keyword.")
                                return false
                        end
+
+                       # Check for full-name conflicts in the project.
+                       # A public property should have a unique qualified name `project::class::prop`.
+                       if mprop.intro_mclassdef.mmodule.mgroup != null and mprop.visibility >= protected_visibility then
+                               var others = modelbuilder.model.get_mproperties_by_name(mprop.name)
+                               if others != null then for other in others do
+                                       if other != mprop and other.intro_mclassdef.mmodule.mgroup != null and other.intro_mclassdef.mmodule.mgroup.mproject == mprop.intro_mclassdef.mmodule.mgroup.mproject and other.intro_mclassdef.mclass.name == mprop.intro_mclassdef.mclass.name and other.visibility >= protected_visibility then
+                                               modelbuilder.advice(self, "full-name-conflict", "Warning: A property named `{other.full_name}` is already defined in module `{other.intro_mclassdef.mmodule}` for the class `{other.intro_mclassdef.mclass.name}`.")
+                                               break
+                                       end
+                               end
+                       end
                else
                        if not need_redef then
                                modelbuilder.error(self, "Error: No property {mclassdef.mclass}::{mprop.name} is inherited. Remove the redef keyword to define a new property.")
@@ -619,11 +672,22 @@ redef class AMethPropdef
                        mprop.is_init = is_init
                        mprop.is_new = n_kwnew != null
                        if parent isa ATopClassdef then mprop.is_toplevel = true
-                       if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mprop) then return
+                       self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, mprop)
                else
-                       if not mprop.is_root_init and not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, not self isa AMainMethPropdef, mprop) then return
+                       if not self.check_redef_keyword(modelbuilder, mclassdef, n_kwredef, not self isa AMainMethPropdef, mprop) then return
                        check_redef_property_visibility(modelbuilder, self.n_visibility, mprop)
                end
+
+               # Check name conflicts in the local class for constructors.
+               if is_init then
+                       for p, n in mclassdef.mprop2npropdef do
+                               if p != mprop and p isa MMethod and p.name == name then
+                                       check_redef_keyword(modelbuilder, mclassdef, n_kwredef, false, p)
+                                       break
+                               end
+                       end
+               end
+
                mclassdef.mprop2npropdef[mprop] = self
 
                var mpropdef = new MMethodDef(mclassdef, mprop, self.location)
@@ -677,6 +741,9 @@ redef class AMethPropdef
                        msignature = mpropdef.mproperty.intro.msignature
                        if msignature == null then return # Skip error
 
+                       # The local signature is adapted to use the local formal types, if any.
+                       msignature = msignature.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
+
                        # Check inherited signature arity
                        if param_names.length != msignature.arity then
                                var node: ANode
@@ -946,11 +1013,17 @@ redef class AAttrPropdef
                        if mtype == null then return
                end
 
+               var inherited_type: nullable MType = null
                # Inherit the type from the getter (usually an abstract getter)
-               if mtype == null and mreadpropdef != null and not mreadpropdef.is_intro then
+               if mreadpropdef != null and not mreadpropdef.is_intro then
                        var msignature = mreadpropdef.mproperty.intro.msignature
                        if msignature == null then return # Error, thus skipped
-                       mtype = msignature.return_mtype
+                       inherited_type = msignature.return_mtype
+                       if inherited_type != null then
+                               # The inherited type is adapted to use the local formal types, if any.
+                               inherited_type = inherited_type.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
+                               if mtype == null then mtype = inherited_type
+                       end
                end
 
                var nexpr = self.n_expr
@@ -982,7 +1055,7 @@ redef class AAttrPropdef
 
                                if mtype == null then return
                        end
-               else if ntype != null then
+               else if ntype != null and inherited_type == mtype then
                        if nexpr isa ANewExpr then
                                var xmtype = modelbuilder.resolve_mtype(mmodule, mclassdef, nexpr.n_type)
                                if xmtype == mtype then
@@ -1136,6 +1209,11 @@ redef class ATypePropdef
                var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
                self.mpropdef = mpropdef
                modelbuilder.mpropdef2npropdef[mpropdef] = self
+               if mpropdef.is_intro then
+                       modelbuilder.toolcontext.info("{mpropdef} introduces new type {mprop.full_name}", 4)
+               else
+                       modelbuilder.toolcontext.info("{mpropdef} redefines type {mprop.full_name}", 4)
+               end
                set_doc(mpropdef, modelbuilder)
 
                var atfixed = get_single_annotation("fixed", modelbuilder)