From aaddffc7785014cdfa398f2470177dabc21775b7 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 22 Oct 2014 14:56:15 -0400 Subject: [PATCH] model: `new` factories have a return type. The default is the definition class, so it remove a special case for such methods. Signed-off-by: Jean Privat --- src/astbuilder.nit | 6 +++++- src/compiler/abstract_compiler.nit | 2 -- src/compiler/global_compiler.nit | 11 +---------- src/compiler/separate_compiler.nit | 16 ++-------------- src/modelize/modelize_property.nit | 5 ++++- src/rapid_type_analysis.nit | 4 +--- 6 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/astbuilder.nit b/src/astbuilder.nit index 630c1c0..cf31299 100644 --- a/src/astbuilder.nit +++ b/src/astbuilder.nit @@ -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 diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index d0cbef5..50f3c8e 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -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) diff --git a/src/compiler/global_compiler.nit b/src/compiler/global_compiler.nit index 7652368..c594451 100644 --- a/src/compiler/global_compiler.nit +++ b/src/compiler/global_compiler.nit @@ -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 diff --git a/src/compiler/separate_compiler.nit b/src/compiler/separate_compiler.nit index 3645f54..6797978 100644 --- a/src/compiler/separate_compiler.nit +++ b/src/compiler/separate_compiler.nit @@ -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 diff --git a/src/modelize/modelize_property.nit b/src/modelize/modelize_property.nit index 7e1a93a..0d2e3a6 100644 --- a/src/modelize/modelize_property.nit +++ b/src/modelize/modelize_property.nit @@ -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 diff --git a/src/rapid_type_analysis.nit b/src/rapid_type_analysis.nit index 7c4bcec..f63034c 100644 --- a/src/rapid_type_analysis.nit +++ b/src/rapid_type_analysis.nit @@ -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 -- 1.7.9.5