model: `new` factories are named "new", not init.
authorJean Privat <jean@pryen.org>
Wed, 22 Oct 2014 18:52:16 +0000 (14:52 -0400)
committerJean Privat <jean@pryen.org>
Thu, 23 Oct 2014 15:39:44 +0000 (11:39 -0400)
It means that in `new X`, first the method name `new` is searched,
then the method named `init` as a fallback.

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

src/compiler/abstract_compiler.nit
src/interpreter/naive_interpreter.nit
src/modelbuilder.nit
src/modelize/modelize_property.nit
src/nitni/nitni_callbacks.nit
src/semantize/typing.nit

index d4d2302..d0cbef5 100644 (file)
@@ -2171,7 +2171,7 @@ redef class AMethPropdef
                        else if pname == "atoi" then
                                v.ret(v.new_expr("atoi({arguments[0]});", ret.as(not null)))
                                return true
-                       else if pname == "init" then
+                       else if pname == "new" then
                                v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
                                return true
                        end
index a0b9e54..6e4a356 100644 (file)
@@ -891,7 +891,7 @@ redef class AMethPropdef
                                return v.bool_instance(args[0].to_f.is_inf != 0)
                        end
                else if cname == "NativeString" then
-                       if pname == "init" then
+                       if pname == "new" then
                                return v.native_string_instance("!" * args[1].to_i)
                        end
                        var recvval = args.first.val.as(Buffer)
@@ -952,7 +952,7 @@ redef class AMethPropdef
                else if pname == "calloc_string" then
                        return v.native_string_instance("!" * args[1].to_i)
                else if cname == "NativeArray" then
-                       if pname == "init" then
+                       if pname == "new" then
                                var val = new Array[Instance].filled_with(v.null_instance, args[1].to_i)
                                return new PrimitiveInstance[Array[Instance]](args[0].mtype, val)
                        end
index 30a0617..cc613f3 100644 (file)
@@ -225,6 +225,12 @@ class ModelBuilder
                for mprop in props do
                        if not mtype.has_mproperty(mmodule, mprop) then continue
                        if not mmodule.is_visible(mprop.intro_mclassdef.mmodule, mprop.visibility) then continue
+
+                       # new-factories are invisible outside of the class
+                       if mprop isa MMethod and mprop.is_new and (not mtype isa MClassType or mprop.intro_mclassdef.mclass != mtype.mclass) then
+                               continue
+                       end
+
                        if res == null then
                                res = mprop
                                continue
index 6a34e2e..7e1a93a 100644 (file)
@@ -529,7 +529,7 @@ redef class AMethPropdef
                                name = "init"
                                name_node = n_kwinit
                        else if n_kwnew != null then
-                               name = "init"
+                               name = "new"
                                name_node = n_kwnew
                        else
                                abort
index 8af9779..f53466c 100644 (file)
@@ -345,11 +345,17 @@ redef class AInitPropExternCall
                        return
                end
 
-               var meth_name = "init"
+               var meth_name = "new"
                var meth = toolcontext.modelbuilder.try_get_mproperty_by_name2( self,
                        mmodule, mtype, meth_name )
 
                if meth == null then
+                       meth_name = "init"
+                       meth = toolcontext.modelbuilder.try_get_mproperty_by_name2( self,
+                               mmodule, mtype, meth_name )
+               end
+
+               if meth == null then
                        toolcontext.error(location, "Method {meth_name} not found in {n_type.collect_text}." )
                        return
                end
index d3d4da7..3aa87a8 100644 (file)
@@ -272,6 +272,11 @@ private class TypeVisitor
                end
 
                var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
+               if name == "new" and mproperty == null then
+                       name = "init"
+                       mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
+               end
+
                if mproperty == null then
                        #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
                        if recv_is_self then
@@ -1704,7 +1709,7 @@ redef class ANewExpr
                if nid != null then
                        name = nid.text
                else
-                       name = "init"
+                       name = "new"
                end
                var callsite = v.get_method(self, recvtype, name, false)
                if callsite == null then return