src: Update init
[nit.git] / src / semantize / auto_super_init.nit
index 07b4f46..0be7107 100644 (file)
@@ -57,9 +57,9 @@ redef class AMethPropdef
        fun do_auto_super_init(modelbuilder: ModelBuilder)
        do
                var mclassdef = self.parent.as(AClassdef).mclassdef
-               if mclassdef == null then return # skip error
+               if mclassdef == null or mclassdef.is_broken then return # skip error
                var mpropdef = self.mpropdef
-               if mpropdef == null then return # skip error
+               if mpropdef == null or mpropdef.is_broken then return # skip error
                var mmodule = mpropdef.mclassdef.mmodule
                var anchor = mclassdef.bound_mtype
                var recvtype = mclassdef.mclass.mclass_type
@@ -69,17 +69,10 @@ redef class AMethPropdef
 
                # Collect only for constructors
                if not mpropdef.mproperty.is_init or mpropdef.mproperty.is_new then
-                       if nosuper != null then modelbuilder.error(nosuper, "Error: nosuper only in `init`")
+                       if nosuper != null then modelbuilder.error(nosuper, "Error: `nosuper` only allowed in `init`.")
                        return
                end
 
-               # Do we inherit for a constructor?
-               var skip = true
-               for cd in mclassdef.in_hierarchy.direct_greaters do
-                       if cd.mclass.kind.need_init then skip = false
-               end
-               if skip then return
-
                # Now we search for the absence of any explicit super-init invocation
                #  * via a "super"
                #  * via a call of an other init
@@ -89,7 +82,7 @@ redef class AMethPropdef
                        v.enter_visit(nblock)
                        var anode = v.has_explicit_super_init
                        if anode != null then
-                               if nosuper != null then modelbuilder.error(anode, "Error: method is annotated nosuper but a constructor call is present")
+                               if nosuper != null then modelbuilder.error(anode, "Error: method is annotated `nosuper` but a super-constructor call is present.")
                                return
                        end
                        if v.is_broken then return # skip
@@ -108,100 +101,86 @@ redef class AMethPropdef
                # Still here? So it means that we must determine what super inits need to be automatically invoked
                # The code that follow is required to deal with complex cases with old-style and new-style inits
 
-               # Look for old-style super constructors
                var auto_super_inits = new Array[CallSite]
-               for msupertype in mclassdef.supertypes do
-                       # FIXME: the order is quite arbitrary
-                       if not msupertype.mclass.kind.need_init then continue
-                       msupertype = msupertype.anchor_to(mmodule, mclassdef.bound_mtype)
-                       var candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, mpropdef.mproperty.name)
-                       if candidate == null then
-                               candidate = modelbuilder.try_get_mproperty_by_name2(self, mmodule, msupertype, "init")
-                       end
-                       if candidate == null then
-                               modelbuilder.error(self, "Error: Cannot do an implicit constructor call in {mpropdef}; there is no constructor named {mpropdef.mproperty.name} in {msupertype}.")
-                               return
-                       end
-                       assert candidate isa MMethod
 
-                       # Skip new-style init
-                       if candidate.is_root_init then continue
+               # The look for new-style super constructors (called from a old style constructor)
+               var candidatedefs = get_super_candidatedefs(modelbuilder)
+
+               if not candidatedefs.is_empty and auto_super_inits.is_empty then
 
-                       var candidatedefs = candidate.lookup_definitions(mmodule, anchor)
                        var candidatedef = candidatedefs.first
-                       # TODO, we drop the others propdefs in the callsite, that is not great :(
+                       if candidatedefs.length > 1 then
+                               var cd2 = candidatedefs[1]
+                               modelbuilder.error(self, "Error: cannot do an implicit constructor call to conflicting inherited inits `{cd2}({cd2.initializers.join(", ")}`) and `{candidatedef}({candidatedef.initializers.join(", ")}`). NOTE: Do not mix old-style and new-style init!")
+                               is_broken = true
+                               return
+                       end
 
-                       var msignature = candidatedef.new_msignature or else candidatedef.msignature
+                       var msignature = candidatedef.msignature
                        msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
 
-                       var callsite = new CallSite(self, recvtype, mmodule, anchor, true, candidate, candidatedef, msignature, false)
-                       auto_super_inits.add(callsite)
-                       modelbuilder.toolcontext.info("Old-style auto-super init for {mpropdef} to {candidate.full_name}", 4)
-               end
-
-               # No old style? The look for new-style super constructors (called from a old style constructor)
-               var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
-               if the_root_init_mmethod != null and auto_super_inits.is_empty then
-                       var candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
+                       if msignature.arity > mpropdef.msignature.arity then
+                               modelbuilder.error(self, "Error: cannot do an implicit constructor call to `{candidatedef}{msignature}`. Expected at least `{msignature.arity}` arguments.")
+                               is_broken = true
+                               return
+                       end
 
-                       # Search the longest-one and checks for conflict
-                       var candidatedef = candidatedefs.first
-                       if candidatedefs.length > 1 then
-                               # Check for conflict in the order of initializers
-                               # Each initializer list must me a prefix of the longest list
-                               # part 1. find the longest list
-                               for spd in candidatedefs do
-                                       if spd.initializers.length > candidatedef.initializers.length then candidatedef = spd
-                               end
-                               # compare
-                               for spd in candidatedefs do
-                                       var i = 0
-                                       for p in spd.initializers do
-                                               if p != candidatedef.initializers[i] then
-                                                       modelbuilder.error(self, "Error: Cannot do an implicit constructor call to comflicting for inherited inits {spd}({spd.initializers.join(", ")}) and {candidatedef}({candidatedef.initializers.join(", ")}). NOTE: Do not mix old-style and new-style init!")
-                                                       return
-                                               end
-                                               i += 1
+                       if candidatedef.mproperty != mpropdef.mproperty then
+                               var i = 0
+                               for candidat_mparameter in msignature.mparameters do
+                                       var actual_mparameter = mpropdef.msignature.mparameters[i]
+                                       if not candidat_mparameter.mtype.is_subtype(mmodule, anchor, actual_mparameter.mtype) then
+                                               modelbuilder.error(self, "Type Error: expected argument #{i} of type `{candidat_mparameter.mtype}`, got implicit argument `{candidat_mparameter.name}` of type `{actual_mparameter.mtype}`. Signature is {msignature}")
+                                               return
                                        end
+                                       i += 1
                                end
                        end
 
-                       var msignature = candidatedef.new_msignature or else candidatedef.msignature
-                       msignature = msignature.resolve_for(recvtype, anchor, mmodule, true)
-
-                       var callsite = new CallSite(self, recvtype, mmodule, anchor, true, the_root_init_mmethod, candidatedef, msignature, false)
+                       var callsite = new CallSite(hot_location, recvtype, mmodule, anchor, true, candidatedef.mproperty, candidatedef, msignature, false)
                        auto_super_inits.add(callsite)
-                       modelbuilder.toolcontext.info("Auto-super init for {mpropdef} to {the_root_init_mmethod.full_name}", 4)
+                       modelbuilder.toolcontext.info("Auto-super init for {mpropdef} to {candidatedef.full_name}", 4)
+               else if candidatedefs.is_empty then
+                       # skip broken
+                       is_broken = true
+                       return
                end
                if auto_super_inits.is_empty then
-                       modelbuilder.error(self, "Error: No constructors to call implicitely in {mpropdef}. Call one explicitely.")
+                       modelbuilder.error(self, "Error: no constructors to call implicitly in `{mpropdef}`. Call one explicitly.")
                        return
                end
 
-               # Can the super-constructors be called?
-               for auto_super_init in auto_super_inits do
-                       var auto_super_init_def = auto_super_init.mpropdef
-                       var msig = mpropdef.msignature.as(not null)
-                       var supermsig = auto_super_init.msignature
-                       if supermsig.arity > msig.arity then
-                               modelbuilder.error(self, "Error: Cannot do an implicit constructor call to {auto_super_init_def}{supermsig}. Expected at least {supermsig.arity} arguments, got {msig.arity}.")
-                               continue
-                       end
-                       var i = 0
-                       for sp in supermsig.mparameters do
-                               var p = msig.mparameters[i]
-                               var sub = p.mtype
-                               var sup = sp.mtype
-                               if not sub.is_subtype(mmodule, anchor, sup) then
-                                       modelbuilder.error(self, "Error: Cannot do an implicit constructor call to {auto_super_init_def}{supermsig}. Expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}.")
-                                       break
-                               end
-                               i += 1
-                       end
-               end
                self.auto_super_inits = auto_super_inits
        end
 
+       # This method returns the list of possible candidates for the current definition.
+       #
+       # Warning this method supports super call from old_style_init to default_inits without signature verification!!!
+       private fun get_super_candidatedefs(modelbuilder: ModelBuilder): Array[MMethodDef]
+       do
+               var candidatedefs = new Array[MMethodDef]
+
+               var mclassdef = self.parent.as(AClassdef).mclassdef
+               if mclassdef == null or mclassdef.is_broken then return candidatedefs # skip error
+               var mpropdef = self.mpropdef
+               if mpropdef == null or mpropdef.is_broken then return candidatedefs # skip error
+               var mmodule = mpropdef.mclassdef.mmodule
+               var anchor = mclassdef.bound_mtype
+               var mproperty = mpropdef.mproperty
+
+               # The look for new-style super constructors (called from a old style constructor)
+               var the_root_init_mmethod = modelbuilder.the_root_init_mmethod
+
+               if mpropdef.is_old_style_init then
+                       var superprop: nullable MMethodDef = null
+                       for mclass in mclassdef.mclass.in_hierarchy(mmodule).direct_greaters do
+                               candidatedefs.add(mclass.intro.default_init.as(not null))
+                       end
+               else
+                       candidatedefs = the_root_init_mmethod.lookup_definitions(mmodule, anchor)
+               end
+               return candidatedefs
+       end
 end
 
 redef class ANode