model: `new` factories have a return type.
authorJean Privat <jean@pryen.org>
Wed, 22 Oct 2014 18:56:15 +0000 (14:56 -0400)
committerJean Privat <jean@pryen.org>
Thu, 23 Oct 2014 15:39:44 +0000 (11:39 -0400)
The default is the definition class, so it remove a special case for
such methods.

Signed-off-by: Jean Privat <jean@pryen.org>

src/astbuilder.nit
src/compiler/abstract_compiler.nit
src/compiler/global_compiler.nit
src/compiler/separate_compiler.nit
src/modelize/modelize_property.nit
src/rapid_type_analysis.nit

index 630c1c0..cf31299 100644 (file)
@@ -275,7 +275,11 @@ redef class ANewExpr
                        n_args.n_exprs.add_all(args)
                end
                self.callsite = callsite
-               self.mtype = callsite.recv
+               if callsite.mproperty.is_new then
+                       self.mtype = callsite.msignature.return_mtype
+               else
+                       self.mtype = callsite.recv
+               end
                self.is_typed = true
        end
 end
index d0cbef5..50f3c8e 100644 (file)
@@ -1989,8 +1989,6 @@ redef class AMethPropdef
                var ret = mpropdef.msignature.return_mtype
                if ret != null then
                        ret = v.resolve_for(ret, arguments.first)
-               else if mpropdef.mproperty.is_new then
-                       ret = arguments.first.mcasttype
                end
                if pname != "==" and pname != "!=" then
                        v.adapt_signature(mpropdef, arguments)
index 7652368..c594451 100644 (file)
@@ -412,10 +412,7 @@ class GlobalCompilerVisitor
 
                var res: nullable RuntimeVariable
                var ret = m.intro.msignature.return_mtype
-               if m.is_new then
-                       ret = args.first.mtype
-                       res = self.new_var(ret)
-               else if ret == null then
+               if ret == null then
                        res = null
                else
                        ret = self.resolve_for(ret, args.first)
@@ -984,9 +981,6 @@ private class CustomizedRuntimeFunction
                if ret != null then
                        ret = v.resolve_for(ret, selfvar)
                        sig.append("{ret.ctype} ")
-               else if mmethoddef.mproperty.is_new then
-                       ret = recv
-                       sig.append("{ret.ctype} ")
                else
                        sig.append("void ")
                end
@@ -1033,9 +1027,6 @@ private class CustomizedRuntimeFunction
        redef fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
        do
                var ret = self.mmethoddef.msignature.return_mtype
-               if self.mmethoddef.mproperty.is_new then
-                       ret = recv
-               end
                if ret != null then
                        ret = v.resolve_for(ret, arguments.first)
                end
index 3645f54..6797978 100644 (file)
@@ -1172,10 +1172,7 @@ class SeparateCompilerVisitor
                var res: nullable RuntimeVariable
                var msignature = mmethod.intro.msignature.resolve_for(mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.bound_mtype, mmethod.intro.mclassdef.mmodule, true)
                var ret = msignature.return_mtype
-               if mmethod.is_new then
-                       ret = arguments.first.mtype
-                       res = self.new_var(ret)
-               else if ret == null then
+               if ret == null then
                        res = null
                else
                        res = self.new_var(ret)
@@ -1226,10 +1223,7 @@ class SeparateCompilerVisitor
 
                var res: nullable RuntimeVariable
                var ret = mmethoddef.msignature.return_mtype
-               if mmethoddef.mproperty.is_new then
-                       ret = arguments.first.mtype
-                       res = self.new_var(ret)
-               else if ret == null then
+               if ret == null then
                        res = null
                else
                        ret = ret.resolve_for(mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.bound_mtype, mmethoddef.mclassdef.mmodule, true)
@@ -1822,9 +1816,6 @@ class SeparateRuntimeFunction
                var ret = msignature.return_mtype
                if ret != null then
                        sig.append("{ret.ctype} ")
-               else if mmethoddef.mproperty.is_new then
-                       ret = recv
-                       sig.append("{ret.ctype} ")
                else
                        sig.append("void ")
                end
@@ -1899,9 +1890,6 @@ class VirtualRuntimeFunction
                var ret = msignature.return_mtype
                if ret != null then
                        sig.append("{ret.ctype} ")
-               else if mmethoddef.mproperty.is_new then
-                       ret = recv
-                       sig.append("{ret.ctype} ")
                else
                        sig.append("void ")
                end
index 7e1a93a..0d2e3a6 100644 (file)
@@ -631,7 +631,7 @@ redef class AMethPropdef
                                modelbuilder.error(node, "Redef error: {mpropdef} redefines {mpropdef.mproperty.intro} with {param_names.length} parameter(s), {msignature.arity} expected. Signature is {mpropdef}{msignature}")
                                return
                        end
-               else if mpropdef.mproperty.is_init then
+               else if mpropdef.mproperty.is_init and not mpropdef.mproperty.is_new then
                        # FIXME UGLY: inherit signature from a super-constructor
                        for msupertype in mclassdef.supertypes do
                                msupertype = msupertype.anchor_to(mmodule, mclassdef.bound_mtype)
@@ -671,6 +671,9 @@ redef class AMethPropdef
                        mparameters.add(mparameter)
                end
 
+               # In `new`-factories, the return type is by default the classtype.
+               if ret_type == null and mpropdef.mproperty.is_new then ret_type = mclassdef.mclass.mclass_type
+
                msignature = new MSignature(mparameters, ret_type)
                mpropdef.msignature = msignature
                mpropdef.is_abstract = self.get_single_annotation("abstract", modelbuilder) != null
index 7c4bcec..f63034c 100644 (file)
@@ -267,9 +267,7 @@ class RapidTypeAnalysis
                                end
                        end
 
-                       if mmeth.is_new then
-                               v.add_type(v.receiver)
-                       else if mmethoddef.is_intern or mmethoddef.is_extern then
+                       if mmethoddef.is_intern or mmethoddef.is_extern then
                                # UGLY: We force the "instantation" of the concrete return type if any
                                var ret = mmethoddef.msignature.return_mtype
                                if ret != null and ret isa MClassType and ret.mclass.kind != abstract_kind and ret.mclass.kind != interface_kind then