model: make autoinit the main construction mechanism
authorJean Privat <jean@pryen.org>
Fri, 26 Feb 2016 19:30:33 +0000 (14:30 -0500)
committerJean Privat <jean@pryen.org>
Fri, 26 Feb 2016 19:30:33 +0000 (14:30 -0500)
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/model/model.nit
src/modelize/modelize_property.nit
src/rapid_type_analysis.nit
src/vm/vm_optimizations.nit

index 3cba054..2375940 100644 (file)
@@ -3173,6 +3173,32 @@ redef class AClassdef
                                v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
                        end
                        return
+               else if mclassdef.auto_init == mpropdef then
+                       var recv = arguments.first
+                       var initializers = mpropdef.initializers
+                       var no_init = false
+                       if not initializers.is_empty then
+
+                               var i = 1
+                               for p in initializers do
+                                       if p isa MMethod then
+                                               var args = [recv]
+                                               for x in p.intro.msignature.mparameters do
+                                                       args.add arguments[i]
+                                                       i += 1
+                                               end
+                                               v.send(p, args)
+                                               if p.intro.is_calling_init then no_init = true
+                                       else if p isa MAttribute then
+                                               v.write_attribute(p, recv, arguments[i])
+                                               i += 1
+                                       else abort
+                               end
+                               assert i == arguments.length
+
+                       end
+                       if not no_init then v.send(mclass.the_root_init_mmethod.as(not null), [recv])
+                       return
                else
                        abort
                end
index bbe07d1..545d89d 100644 (file)
@@ -1563,6 +1563,31 @@ redef class AClassdef
                                v.call(superpd, arguments)
                        end
                        return null
+               else if mclassdef.auto_init == mpropdef then
+                       var recv = arguments.first
+                       var initializers = mpropdef.initializers
+                       var no_init = false
+                       if not initializers.is_empty then
+                               var i = 1
+                               for p in initializers do
+                                       if p isa MMethod then
+                                               var args = [recv]
+                                               for x in p.intro.msignature.mparameters do
+                                                       args.add arguments[i]
+                                                       i += 1
+                                               end
+                                               v.send(p, args)
+                                               if p.intro.is_calling_init then no_init = true
+                                       else if p isa MAttribute then
+                                               assert recv isa MutableInstance
+                                               v.write_attribute(p, recv, arguments[i])
+                                               i += 1
+                                       else abort
+                               end
+                               assert i == arguments.length
+                       end
+                       if not no_init then v.send(mclass.the_root_init_mmethod.as(not null), [recv])
+                       return null
                else
                        abort
                end
index 3115286..1560022 100644 (file)
@@ -706,6 +706,9 @@ class MClassDef
 
        # All property definitions in the class (introductions and redefinitions)
        var mpropdefs = new Array[MPropDef]
+
+       # The special autoinit constructor
+       var auto_init: nullable MMethodDef = null is writable
 end
 
 # A global static type
index 158dbec..fe755a5 100644 (file)
@@ -154,7 +154,6 @@ redef class ModelBuilder
                        mprop.is_init = true
                        self.toolcontext.info("{mclassdef} gets a free empty constructor {mpropdef}{msignature}", 3)
                        the_root_init_mmethod = mprop
-                       return
                end
 
                # Is there already a constructor defined?
@@ -171,6 +170,10 @@ redef class ModelBuilder
                        end
                end
 
+               if mclassdef.auto_init != null then
+                       return
+               end
+
                if not nclassdef isa AStdClassdef then return
 
                # Collect undefined attributes
@@ -225,10 +228,12 @@ redef class ModelBuilder
                if the_root_init_mmethod == null then return
 
                # Look for most-specific new-stype init definitions
-               var spropdefs = the_root_init_mmethod.lookup_super_definitions(mclassdef.mmodule, mclassdef.bound_mtype)
-               if spropdefs.is_empty then
-                       toolcontext.error(nclassdef.location, "Error: `{mclassdef}` does not specialize `{the_root_init_mmethod.intro_mclassdef}`. Possible duplication of the root class `Object`?")
-                       return
+               var spropdefs = new ArraySet[MMethodDef]
+               for x in mclassdef.in_hierarchy.direct_greaters do
+                       var y = x.mclass.intro.auto_init
+                       if y == null then continue
+                       if y.is_broken or y.msignature == null then return
+                       spropdefs.add y
                end
 
                # Look at the autoinit class-annotation
@@ -336,26 +341,19 @@ redef class ModelBuilder
                        end
                end
 
-               # If we already have a basic init definition, then setup its initializers
-               if defined_init != null then
-                       defined_init.initializers.add_all(initializers)
+               # Create a specific new autoinit constructor
+               do
+                       var mprop = new MMethod(mclassdef, "autoinit", public_visibility)
+                       mprop.is_init = true
+                       var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
+                       mpropdef.initializers.add_all(initializers)
                        var msignature = new MSignature(mparameters, null)
-                       defined_init.new_msignature = msignature
-                       self.toolcontext.info("{mclassdef} extends its basic constructor signature to {defined_init}{msignature}", 3)
-                       mclassdef.mclass.root_init = defined_init
-                       return
+                       mpropdef.msignature = msignature
+                       mclassdef.auto_init = mpropdef
+                       self.toolcontext.info("{mclassdef} gets a free auto constructor `{mpropdef}{msignature}`. {spropdefs}", 3)
+                       #mclassdef.mclass.root_init = mpropdef
+                       mclassdef.mclass.the_root_init_mmethod = the_root_init_mmethod
                end
-
-               # Else create the local implicit basic init definition
-               var mprop = the_root_init_mmethod
-               var mpropdef = new MMethodDef(mclassdef, mprop, nclassdef.location)
-               mpropdef.has_supercall = true
-               mpropdef.initializers.add_all(initializers)
-               var msignature = new MSignature(mparameters, null)
-               mpropdef.new_msignature = msignature
-               mpropdef.msignature = new MSignature(new Array[MParameter], null) # always an empty real signature
-               self.toolcontext.info("{mclassdef} gets a free constructor for attributes {mpropdef}{msignature}", 3)
-               mclassdef.mclass.root_init = mpropdef
        end
 
        # Check the visibility of `mtype` as an element of the signature of `mpropdef`.
@@ -489,11 +487,15 @@ end
 
 redef class MClass
        # The base init of the class.
-       # Used to get the common new_msignature and initializers
        #
        # TODO: Where to put this information is not clear because unlike other
        # informations, the initialisers are stable in a same class.
        var root_init: nullable MMethodDef = null
+
+       # The base init of the class.
+       #
+       # TODO: merge with `root_init` and `ModelBuilder::the_root_init_mmethod` if possible
+       var the_root_init_mmethod: nullable MMethod = null
 end
 
 redef class MClassDef
index f763a56..4f3651d 100644 (file)
@@ -259,9 +259,15 @@ class RapidTypeAnalysis
 
                        if npropdef isa AClassdef then
                                if mmethoddef.mproperty.is_root_init then
+                                       # Final init call
                                        if not mmethoddef.is_intro then
                                                self.add_super_send(v.receiver, mmethoddef)
                                        end
+                               else if mmethoddef.mclassdef.auto_init == mmethoddef then
+                                       # autoinit call
+                                       for i in mmethoddef.initializers do
+                                               if i isa MMethod then self.add_send(v.receiver, i)
+                                       end
                                else
                                        npropdef.debug "cannot RTA {mmethoddef}"
                                        abort
index 514d3d1..3fe9672 100644 (file)
@@ -24,28 +24,7 @@ redef class VirtualMachine
        # Add optimization of the method dispatch
        redef fun callsite(callsite: nullable CallSite, arguments: Array[Instance]): nullable Instance
        do
-               var initializers = callsite.mpropdef.initializers
-               if initializers.is_empty then return send_optimize(callsite.as(not null), arguments)
-
-               var recv = arguments.first
-               var i = 1
-               for p in initializers do
-                       if p isa MMethod then
-                               var args = [recv]
-                               for x in p.intro.msignature.mparameters do
-                                       args.add arguments[i]
-                                       i += 1
-                               end
-                               self.send(p, args)
-                       else if p isa MAttribute then
-                               assert recv isa MutableInstance
-                               write_attribute(p, recv, arguments[i])
-                               i += 1
-                       else abort
-               end
-               assert i == arguments.length
-
-               return send_optimize(callsite.as(not null), [recv])
+               return send_optimize(callsite.as(not null), arguments)
        end
 
        # Try to have the most efficient implementation of the method dispatch