model: add `MEntity::is_broken`
[nit.git] / src / modelize / modelize_class.nit
index ff0295f..b3ab0bb 100644 (file)
@@ -20,6 +20,7 @@ module modelize_class
 import modelbuilder
 
 redef class ToolContext
+       # Run `AModule::build_classes` on each module
        var modelize_class_phase: Phase = new ModelizeClassPhase(self, null)
 end
 
@@ -44,6 +45,7 @@ redef class ModelBuilder
                var nvisibility: nullable AVisibility
                var mvisibility: nullable MVisibility
                var arity = 0
+               var names = new Array[String]
                if nclassdef isa AStdClassdef then
                        name = nclassdef.n_id.text
                        nkind = nclassdef.n_classkind
@@ -58,43 +60,76 @@ redef class ModelBuilder
                                error(nvisibility, "Error: intrude is not a legal visibility for classes.")
                                return
                        end
-               else if nclassdef isa ATopClassdef then
+                       # Collect formal parameter names
+                       for i in [0..arity[ do
+                               var nfd = nclassdef.n_formaldefs[i]
+                               var ptname = nfd.n_id.text
+                               if names.has(ptname) then
+                                       error(nfd, "Error: a formal parameter type `{ptname}` already exists.")
+                                       return
+                               end
+                               for c in ptname.chars do if c >= 'a' and c<= 'z' then
+                                       warning(nfd, "formal-type-name", "Warning: lowercase in the formal parameter type `{ptname}`.")
+                                       break
+                               end
+                               names.add(ptname)
+                       end
+
+               else if nclassdef isa ATopClassdef and nclassdef.n_propdefs.first.as(AMethPropdef).n_methid.collect_text == "sys" then
+                       # Special case to keep `sys` in object.
+                       # Needed to keep working bootstrap and a working java FFI together.
+                       # TODO: remove once safe to remove
                        name = "Object"
                        nkind = null
                        mkind = interface_kind
                        nvisibility = null
                        mvisibility = public_visibility
-               else if nclassdef isa AMainClassdef then
+               else
                        name = "Sys"
                        nkind = null
                        mkind = concrete_kind
                        nvisibility = null
                        mvisibility = public_visibility
-               else
-                       abort
                end
 
                var mclass = try_get_mclass_by_name(nclassdef, mmodule, name)
                if mclass == null then
                        if nclassdef isa AStdClassdef and nclassdef.n_kwredef != null then
-                               error(nclassdef, "Redef error: No imported class {name} to refine.")
+                               error(nclassdef, "Redef Error: no imported class `{name}` to refine.")
                                return
                        end
-                       mclass = new MClass(mmodule, name, arity, mkind, mvisibility)
+
+                       # Check for conflicting class full-names in the package
+                       if mmodule.mgroup != null and mvisibility >= protected_visibility then
+                               var mclasses = model.get_mclasses_by_name(name)
+                               if mclasses != null then for other in mclasses do
+                                       if other.intro_mmodule.mgroup != null and other.intro_mmodule.mgroup.mpackage == mmodule.mgroup.mpackage then
+                                               # Skip classes that are buggy
+                                               if other.try_intro == null then continue
+                                               warning(nclassdef, "full-name-conflict", "Error: a class named `{other.full_name}` is already defined in module `{other.intro_mmodule}` at {other.intro.location}.")
+                                               break
+                                       end
+                               end
+                       end
+
+                       mclass = new MClass(mmodule, name, names, mkind, mvisibility)
                        #print "new class {mclass}"
                else if nclassdef isa AStdClassdef and nmodule.mclass2nclassdef.has_key(mclass) then
-                       error(nclassdef, "Error: A class {name} is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
+                       error(nclassdef, "Error: a class `{name}` is already defined at line {nmodule.mclass2nclassdef[mclass].location.line_start}.")
+                       mclass.is_broken = true
                        return
                else if nclassdef isa AStdClassdef and nclassdef.n_kwredef == null then
-                       error(nclassdef, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
+                       error(nclassdef, "Redef Error: `{name}` is an imported class. Add the `redef` keyword to refine it.")
+                       mclass.is_broken = true
                        return
-               else if mclass.arity != arity then
-                       error(nclassdef, "Redef error: Formal parameter arity missmatch; got {arity}, expected {mclass.arity}.")
+               else if arity != 0 and mclass.arity != arity then
+                       error(nclassdef, "Redef Error: expected {mclass.arity} formal parameter(s) for {mclass.signature_to_s}; got {arity}.")
+                       mclass.is_broken = true
                        return
                else if nkind != null and mkind != concrete_kind and mclass.kind != mkind then
-                       error(nkind, "Error: refinement changed the kind from a {mclass.kind} to a {mkind}")
+                       error(nkind, "Redef Error: refinement changed the kind from `{mclass.kind}` to `{mkind}`.")
                else if nvisibility != null and mvisibility != public_visibility and mclass.visibility != mvisibility then
-                       error(nvisibility, "Error: refinement changed the visibility from a {mclass.visibility} to a {mvisibility}")
+                       error(nvisibility, "Redef Error: refinement changed the visibility from `{mclass.visibility}` to `{mvisibility}`")
                end
                nclassdef.mclass = mclass
                if not nmodule.mclass2nclassdef.has_key(mclass) then
@@ -121,43 +156,41 @@ redef class ModelBuilder
                        return
                end
 
-               var names = new Array[String]
                var bounds = new Array[MType]
                if nclassdef isa AStdClassdef and mclass.arity > 0 then
-                       # Collect formal parameter names
+                       # Revolve bound for formal parameters
                        for i in [0..mclass.arity[ do
-                               var nfd = nclassdef.n_formaldefs[i]
-                               var ptname = nfd.n_id.text
-                               if names.has(ptname) then
-                                       error(nfd, "Error: A formal parameter type `{ptname}' already exists")
-                                       return
-                               end
-                               for c in ptname.chars do if c >= 'a' and c<= 'z' then
-                                       warning(nfd, "Warning: lowercase in the formal parameter type {ptname}")
-                                       break
+                               if nclassdef.n_formaldefs.is_empty then
+                                       # Inherit the bound
+                                       var bound = mclass.intro.bound_mtype.arguments[i]
+                                       bounds.add(bound)
+                                       continue
                                end
-                               names.add(ptname)
-                               nfd.mtype = mclass.mclass_type.arguments[i].as(MParameterType)
-                       end
 
-                       # Revolve bound for formal parameter names
-                       for i in [0..mclass.arity[ do
                                var nfd = nclassdef.n_formaldefs[i]
+                               var pname = mclass.mparameters[i].name
+                               if nfd.n_id.text != pname then
+                                       error(nfd.n_id, "Error: formal parameter type #{i} `{nfd.n_id.text}` must be named `{pname}` as in the original definition in module `{mclass.intro.mmodule}`.")
+                               end
                                var nfdt = nfd.n_type
                                if nfdt != null then
                                        var bound = resolve_mtype_unchecked(mmodule, null, nfdt, false)
                                        if bound == null then return # Forward error
                                        if bound.need_anchor then
                                                # No F-bounds!
-                                               error(nfd, "Error: Formal parameter type `{names[i]}' bounded with a formal parameter type")
+                                               error(nfd, "Error: formal parameter type `{pname}` bounded with a formal parameter type.")
                                        else
                                                bounds.add(bound)
                                                nfd.bound = bound
                                        end
                                        if bound isa MClassType and bound.mclass.kind == enum_kind then
-                                               warning(nfdt, "Warning: Useless formal parameter type since `{bound}` cannnot have subclasses.")
+                                               warning(nfdt, "useless-bound", "Warning: useless formal parameter type since `{bound}` cannot have subclasses.")
                                        end
                                else if mclass.mclassdefs.is_empty then
+                                       if objectclass == null then
+                                               error(nfd, "Error: formal parameter type `{pname}` unbounded but no `Object` class exists.")
+                                               return
+                                       end
                                        # No bound, then implicitely bound by nullable Object
                                        var bound = objectclass.mclass_type.as_nullable
                                        bounds.add(bound)
@@ -172,7 +205,7 @@ redef class ModelBuilder
                end
 
                var bound_mtype = mclass.get_mtype(bounds)
-               var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location, names)
+               var mclassdef = new MClassDef(mmodule, bound_mtype, nclassdef.location)
                nclassdef.mclassdef = mclassdef
                self.mclassdef2nclassdef[mclassdef] = nclassdef
 
@@ -182,6 +215,8 @@ redef class ModelBuilder
                                var mdoc = ndoc.to_mdoc
                                mclassdef.mdoc = mdoc
                                mdoc.original_mentity = mclassdef
+                       else if mclassdef.is_intro and mclass.visibility >= public_visibility then
+                               advice(nclassdef, "missing-doc", "Documentation warning: Undocumented public class `{mclass}`")
                        end
                end
 
@@ -195,11 +230,14 @@ redef class ModelBuilder
        # Visit the AST and set the super-types of the `MClassDef` objects
        private fun collect_a_mclassdef_inheritance(nmodule: AModule, nclassdef: AClassdef)
        do
-               var mmodule = nmodule.mmodule.as(not null)
+               var mmodule = nmodule.mmodule
+               if mmodule == null then return
                var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
                var pointerclass = try_get_mclass_by_name(nmodule, mmodule, "Pointer")
-               var mclass = nclassdef.mclass.as(not null)
-               var mclassdef = nclassdef.mclassdef.as(not null)
+               var mclass = nclassdef.mclass
+               if mclass == null then return
+               var mclassdef = nclassdef.mclassdef
+               if mclassdef == null then return
 
                # Do we need to specify Object as a super class?
                var specobject = true
@@ -216,11 +254,11 @@ redef class ModelBuilder
                                var mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, false)
                                if mtype == null then continue # Skip because of error
                                if not mtype isa MClassType then
-                                       error(ntype, "Error: supertypes cannot be a formal type")
+                                       error(ntype, "Error: supertypes cannot be a formal type.")
                                        return
                                end
                                if not mclass.kind.can_specialize(mtype.mclass.kind) then
-                                       error(ntype, "Error: {mclass.kind} {mclass} cannot specialize {mtype.mclass.kind} {mtype.mclass}")
+                                       error(ntype, "Error: {mclass.kind} `{mclass}` cannot specialize {mtype.mclass.kind} `{mtype.mclass}`.")
                                end
                                supertypes.add mtype
                                #print "new super : {mclass} < {mtype}"
@@ -245,14 +283,16 @@ redef class ModelBuilder
        # Check the validity of the specialization heirarchy
        private fun check_supertypes(nmodule: AModule, nclassdef: AClassdef)
        do
-               var mmodule = nmodule.mmodule.as(not null)
-               var objectclass = try_get_mclass_by_name(nmodule, mmodule, "Object")
-               var mclass = nclassdef.mclass.as(not null)
-               var mclassdef = nclassdef.mclassdef.as(not null)
+               var mmodule = nmodule.mmodule
+               if mmodule == null then return
+               var mclass = nclassdef.mclass
+               if mclass == null then return
+               var mclassdef = nclassdef.mclassdef
+               if mclassdef == null then return
 
                for s in mclassdef.supertypes do
                        if s.is_subtype(mmodule, mclassdef.bound_mtype, mclassdef.bound_mtype) then
-                               error(nclassdef, "Error: Inheritance loop for class {mclass} with type {s}")
+                               error(nclassdef, "Error: inheritance loop for class `{mclass}` with type `{s}`.")
                        end
                end
        end
@@ -265,17 +305,23 @@ redef class ModelBuilder
                # Force building recursively
                if nmodule.build_classes_is_done then return
                nmodule.build_classes_is_done = true
-               var mmodule = nmodule.mmodule.as(not null)
+               var mmodule = nmodule.mmodule
+               if mmodule == null then return
                for imp in mmodule.in_importation.direct_greaters do
-
-                       if not mmodule2nmodule.has_key(imp) then continue
-                       build_classes(mmodule2nmodule[imp])
+                       var nimp = mmodule2node(imp)
+                       if nimp != null then build_classes(nimp)
                end
 
                if errcount != toolcontext.error_count then return
 
                # Create all classes
+               # process AStdClassdef before so that non-AStdClassdef classes can be attached to existing ones, if any
+               for nclassdef in nmodule.n_classdefs do
+                       if not nclassdef isa AStdClassdef then continue
+                       self.build_a_mclass(nmodule, nclassdef)
+               end
                for nclassdef in nmodule.n_classdefs do
+                       if nclassdef isa AStdClassdef then continue
                        self.build_a_mclass(nmodule, nclassdef)
                end
 
@@ -283,6 +329,11 @@ redef class ModelBuilder
 
                # Create all classdefs
                for nclassdef in nmodule.n_classdefs do
+                       if not nclassdef isa AStdClassdef then continue
+                       self.build_a_mclassdef(nmodule, nclassdef)
+               end
+               for nclassdef in nmodule.n_classdefs do
+                       if nclassdef isa AStdClassdef then continue
                        self.build_a_mclassdef(nmodule, nclassdef)
                end
 
@@ -336,7 +387,8 @@ redef class ModelBuilder
 
                # Check clash of ancestors
                for nclassdef in nmodule.n_classdefs do
-                       var mclassdef = nclassdef.mclassdef.as(not null)
+                       var mclassdef = nclassdef.mclassdef
+                       if mclassdef == null then continue
                        var superclasses = new HashMap[MClass, MClassType]
                        for scd in mclassdef.in_hierarchy.greaters do
                                for st in scd.supertypes do
@@ -346,7 +398,7 @@ redef class ModelBuilder
                                                var st1 = superclasses[st.mclass].resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
                                                var st2 = st.resolve_for(mclassdef.mclass.mclass_type, mclassdef.bound_mtype, mmodule, false)
                                                if st1 != st2 then
-                                                       error(nclassdef, "Error: Incompatibles ancestors for {mclassdef.mclass}: {st1}, {st2}")
+                                                       error(nclassdef, "Error: incompatible ancestors for `{mclassdef.mclass}`; conflict: `{st1}` and `{st2}`")
                                                end
                                        end
                                end
@@ -360,7 +412,8 @@ redef class ModelBuilder
                # Check that the superclasses are not already known (by transitivity)
                for nclassdef in nmodule.n_classdefs do
                        if not nclassdef isa AStdClassdef then continue
-                       var mclassdef = nclassdef.mclassdef.as(not null)
+                       var mclassdef = nclassdef.mclassdef
+                       if mclassdef == null then continue
 
                        # Get the direct superclasses
                        # Since we are a mclassdef, just look at the mclassdef hierarchy
@@ -385,128 +438,27 @@ redef class ModelBuilder
                                if not parents.has(sc) or sc == objectclass then
                                        # Skip the warning on generated code
                                        if ntype.location.file != null and not ntype.location.file.filename.is_empty then
-                                               warning(ntype, "Warning: superfluous super-class {mtype} in class {mclassdef.mclass}.")
+                                               warning(ntype, "useless-superclass", "Warning: superfluous super-class `{mtype}` in class `{mclassdef.mclass}`.")
                                        end
                                else if not seen_parents.has_key(sc) then
                                        seen_parents[sc] = ntype
                                else
-                                       warning(ntype, "Warning: duplicated super-class {mtype} in class {mclassdef.mclass}.")
-                               end
-                       end
-               end
-       end
-
-       # Register the nclassdef associated to each mclassdef
-       # FIXME: why not refine the `MClassDef` class with a nullable attribute?
-       var mclassdef2nclassdef: HashMap[MClassDef, AClassdef] = new HashMap[MClassDef, AClassdef]
-
-       # Return the static type associated to the node `ntype`.
-       # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
-       # In case of problem, an error is displayed on `ntype` and null is returned.
-       # FIXME: the name "resolve_mtype" is awful
-       fun resolve_mtype_unchecked(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType, with_virtual: Bool): nullable MType
-       do
-               var name = ntype.n_id.text
-               var res: MType
-
-               # Check virtual type
-               if mclassdef != null and with_virtual then
-                       var prop = try_get_mproperty_by_name(ntype, mclassdef, name).as(nullable MVirtualTypeProp)
-                       if prop != null then
-                               if not ntype.n_types.is_empty then
-                                       error(ntype, "Type error: formal type {name} cannot have formal parameters.")
-                               end
-                               res = prop.mvirtualtype
-                               if ntype.n_kwnullable != null then res = res.as_nullable
-                               ntype.mtype = res
-                               return res
-                       end
-               end
-
-               # Check parameter type
-               if mclassdef != null and mclassdef.parameter_names.has(name) then
-                       if not ntype.n_types.is_empty then
-                               error(ntype, "Type error: formal type {name} cannot have formal parameters.")
-                       end
-                       for i in [0..mclassdef.parameter_names.length[ do
-                               if mclassdef.parameter_names[i] == name then
-                                       res = mclassdef.mclass.mclass_type.arguments[i]
-                                       if ntype.n_kwnullable != null then res = res.as_nullable
-                                       ntype.mtype = res
-                                       return res
-                               end
-                       end
-                       abort
-               end
-
-               # Check class
-               var mclass = try_get_mclass_by_name(ntype, mmodule, name)
-               if mclass != null then
-                       var arity = ntype.n_types.length
-                       if arity != mclass.arity then
-                               if arity == 0 then
-                                       error(ntype, "Type error: '{name}' is a generic class.")
-                               else if mclass.arity == 0 then
-                                       error(ntype, "Type error: '{name}' is not a generic class.")
-                               else
-                                       error(ntype, "Type error: '{name}' has {mclass.arity} parameters ({arity} are provided).")
-                               end
-                               return null
-                       end
-                       if arity == 0 then
-                               res = mclass.mclass_type
-                               if ntype.n_kwnullable != null then res = res.as_nullable
-                               ntype.mtype = res
-                               return res
-                       else
-                               var mtypes = new Array[MType]
-                               for nt in ntype.n_types do
-                                       var mt = resolve_mtype_unchecked(mmodule, mclassdef, nt, with_virtual)
-                                       if mt == null then return null # Forward error
-                                       mtypes.add(mt)
+                                       warning(ntype, "useless-superclass", "Warning: duplicated super-class `{mtype}` in class `{mclassdef.mclass}`.")
                                end
-                               res = mclass.get_mtype(mtypes)
-                               if ntype.n_kwnullable != null then res = res.as_nullable
-                               ntype.mtype = res
-                               return res
                        end
                end
-
-               # If everything fail, then give up :(
-               error(ntype, "Type error: class {name} not found in module {mmodule}.")
-               return null
        end
 
-       # Return the static type associated to the node `ntype`.
-       # `mmodule` and `mclassdef` is the context where the call is made (used to understand formal types)
-       # In case of problem, an error is displayed on `ntype` and null is returned.
-       # FIXME: the name "resolve_mtype" is awful
-       fun resolve_mtype(mmodule: MModule, mclassdef: nullable MClassDef, ntype: AType): nullable MType
-       do
-               var mtype = ntype.mtype
-               if mtype == null then mtype = resolve_mtype_unchecked(mmodule, mclassdef, ntype, true)
-               if mtype == null then return null # Forward error
+       # Registration of the nclassdef associated to each mclassdef
+       private var mclassdef2nclassdef = new HashMap[MClassDef, AClassdef]
 
-               if ntype.checked_mtype then return mtype
-               if mtype isa MGenericType then
-                       var mclass = mtype.mclass
-                       for i in [0..mclass.arity[ do
-                               var bound = mclass.intro.bound_mtype.arguments[i]
-                               var nt = ntype.n_types[i]
-                               var mt = resolve_mtype(mmodule, mclassdef, nt)
-                               if mt == null then return null # forward error
-                               var anchor
-                               if mclassdef != null then anchor = mclassdef.bound_mtype else anchor = null
-                               if not mt.is_subtype(mmodule, anchor, bound) then
-                                       error(nt, "Type error: expected {bound}, got {mt}")
-                                       return null
-                               end
-                       end
-               end
-               ntype.checked_mtype = true
-               return mtype
+       # Retrieve the associated AST node of a mclassdef.
+       #
+       # This method is used to associate model entity with syntactic entities.
+       # If the class definition is not associated with a node, returns `null`.
+       fun mclassdef2node(mclassdef: MClassDef): nullable AClassdef do
+               return mclassdef2nclassdef.get_or_null(mclassdef)
        end
-
 end
 
 redef class AModule
@@ -553,11 +505,3 @@ redef class AFormaldef
        # The associated bound
        var bound: nullable MType = null
 end
-
-redef class AType
-       # The mtype associated to the node
-       var mtype: nullable MType = null
-
-       # Is the mtype a valid one?
-       var checked_mtype: Bool = false
-end