default_init: collect attributes on combined empty init
[nit.git] / src / modelize_property.nit
index c7f8e8b..c5847f6 100644 (file)
@@ -105,12 +105,27 @@ redef class ModelBuilder
                                inhc = inhc2
                        end
                end
+
+               # Collect undefined attributes
+               var mparameters = new Array[MParameter]
+               for npropdef in nclassdef.n_propdefs do
+                       if npropdef isa AAttrPropdef and npropdef.n_expr == null then
+                               if npropdef.mpropdef == null then return # Skip broken attribute
+                               var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
+                               var ret_type = npropdef.mpropdef.static_mtype
+                               if ret_type == null then return
+                               var mparameter = new MParameter(paramname, ret_type, false)
+                               mparameters.add(mparameter)
+                       end
+               end
+
                if combine.is_empty and inhc != null then
                        # TODO: actively inherit the consturctor
                        self.toolcontext.info("{mclassdef} inherits all constructors from {inhc}", 3)
                        mclassdef.mclass.inherit_init_from = inhc
                        return
                end
+
                if not combine.is_empty and inhc != null then
                        self.error(nclassdef, "Error: Cannot provide a defaut constructor: conflict for {combine.join(", ")} and {inhc}")
                        return
@@ -118,28 +133,6 @@ redef class ModelBuilder
 
                if not combine.is_empty then
                        nclassdef.super_inits = combine
-                       var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
-                       var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
-                       var mparameters = new Array[MParameter]
-                       var msignature = new MSignature(mparameters, null)
-                       mpropdef.msignature = msignature
-                       mprop.is_init = true
-                       nclassdef.mfree_init = mpropdef
-                       self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
-                       return
-               end
-
-               # Collect undefined attributes
-               var mparameters = new Array[MParameter]
-               for npropdef in nclassdef.n_propdefs do
-                       if npropdef isa AAttrPropdef and npropdef.n_expr == null then
-                               if npropdef.mpropdef == null then return # Skip broken attribute
-                               var paramname = npropdef.mpropdef.mproperty.name.substring_from(1)
-                               var ret_type = npropdef.mpropdef.static_mtype
-                               if ret_type == null then return
-                               var mparameter = new MParameter(paramname, ret_type, false)
-                               mparameters.add(mparameter)
-                       end
                end
 
                var mprop = new MMethod(mclassdef, "init", mclassdef.mclass.visibility)
@@ -399,21 +392,23 @@ redef class AMethPropdef
 
        redef fun build_property(modelbuilder, nclassdef)
        do
-               var is_init = self isa AInitPropdef
+               var n_kwinit = n_kwinit
+               var n_kwnew = n_kwnew
+               var is_init = n_kwinit != null or n_kwnew != null
                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
+                       if not is_init then
                                name = "main"
                                name_node = self
-                       else if self isa AConcreteInitPropdef then
+                       else if n_kwinit != null then
                                name = "init"
-                               name_node = self.n_kwinit
-                       else if self isa AExternInitPropdef then
+                               name_node = n_kwinit
+                       else if n_kwnew != null then
                                name = "init"
-                               name_node = self.n_kwnew
+                               name_node = n_kwnew
                        else
                                abort
                        end
@@ -436,7 +431,7 @@ redef class AMethPropdef
                        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
+                       mprop.is_new = n_kwnew != null
                        if not self.check_redef_keyword(modelbuilder, nclassdef, n_kwredef, false, mprop) then return
                else
                        if n_kwredef == null then
@@ -554,6 +549,8 @@ redef class AMethPropdef
                msignature = new MSignature(mparameters, ret_type)
                mpropdef.msignature = msignature
                mpropdef.is_abstract = self isa ADeferredMethPropdef
+               mpropdef.is_intern = self isa AInternMethPropdef
+               mpropdef.is_extern = self isa AExternPropdef
        end
 
        redef fun check_signature(modelbuilder, nclassdef)
@@ -933,7 +930,7 @@ redef class ATypePropdef
                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
+                       for c in name.chars do if c >= 'a' and c<= 'z' then
                                modelbuilder.warning(n_id, "Warning: lowercase in the virtual type {name}")
                                break
                        end
@@ -947,6 +944,7 @@ redef class ATypePropdef
 
                var mpropdef = new MVirtualTypeDef(mclassdef, mprop, self.location)
                self.mpropdef = mpropdef
+               modelbuilder.mpropdef2npropdef[mpropdef] = self
                set_doc(mpropdef)
        end