No more default value for local variables.
[nit.git] / src / compiling / compiling_methods.nit
index af1e848..8674c03 100644 (file)
@@ -114,9 +114,13 @@ redef class CompilerVisitor
                while i < l do
                        var p = n.super_init_calls[i]
                        if p == stop_prop then break
-                       var cargs = nmc.method_params
+                       var cargs = new Array[String]
                        if p.signature.arity == 0 then
-                               cargs = [nmc.method_params[0]]
+                               cargs.add(cfc.varname(nmc.method_params[0]))
+                       else
+                               for va in nmc.method_params do
+                                       cargs.add(cfc.varname(va))
+                               end
                        end
                        #s.append(" {p}")
                        p.compile_call(self, cargs)
@@ -205,8 +209,8 @@ class NitMethodContext
        # Is a "return" found in the method body
        readable writable attr _has_return: Bool = false
 
-       # Association between parameters and the corresponding c variables
-       readable writable attr _method_params: Array[String] 
+       # Association between parameters and the corresponding variables
+       readable writable attr _method_params: Array[ParamVariable] 
 
        # Where a nit return must branch
        readable writable attr _return_label: String 
@@ -310,15 +314,10 @@ redef class MMSrcMethod
        protected meth decl_csignature(v: CompilerVisitor, args: Array[String]): String
        do
                var params = new Array[String]
-               var params_new: Array[String] = null
-               if global.is_init then
-                       params_new = new Array[String]
-               end
                params.add("val_t {args[0]}")
                for i in [0..signature.arity[ do
                        var p = "val_t {args[i+1]}"
                        params.add(p)
-                       if params_new != null then params_new.add(p)
                end
                if global.is_init then
                        params.add("int* init_table")
@@ -333,9 +332,6 @@ redef class MMSrcMethod
                var s = "{ret} {cname}({p})"
                v.add_decl("typedef {ret} (* {cname}_t)({p});")
                v.add_decl(s + ";")
-               if params_new != null then
-                       v.add_decl("val_t NEW_{cname}({params_new.join(", ")});")
-               end
                return s
        end
 
@@ -463,6 +459,10 @@ redef class AConcreteMethPropdef
                var old_nmc = v.nmc
                v.nmc = new NitMethodContext(method)
 
+               var cname = v.cfc.register_variable(self_var)
+               v.add_assignment(cname, params[0])
+               v.nmc.method_params = [self_var]
+
                var orig_meth: MMLocalProperty = method.global.intro
                var orig_sig = orig_meth.signature_for(method.signature.recv)
                if n_signature != null then
@@ -470,6 +470,7 @@ redef class AConcreteMethPropdef
                        assert sig isa ASignature
                        for ap in sig.n_params do
                                var cname = v.cfc.register_variable(ap.variable)
+                               v.nmc.method_params.add(ap.variable)
                                var orig_type = orig_sig[ap.position]
                                if not orig_type < ap.variable.stype then
                                        # FIXME: do not test always
@@ -488,7 +489,6 @@ redef class AConcreteMethPropdef
                        v.add_instr("if (init_table[{itpos}]) return;")
                end
 
-               v.nmc.method_params = params
                v.nmc.return_label = "return_label{v.new_number}"
                if method.signature.return_type != null then
                        v.nmc.return_value = v.cfc.get_var
@@ -518,7 +518,7 @@ end
 redef class ADeferredMethPropdef
        redef meth do_compile_inside(v, method, params)
        do
-               v.add_instr("fprintf(stderr, \"Deferred method %s called\");")
+               v.add_instr("fprintf(stderr, \"Deferred method called\");")
                v.add_instr(v.printf_locate_error(self))
                v.add_instr("nit_exit(1);")
                if method.signature.return_type != null then
@@ -568,7 +568,7 @@ redef class AInternMethPropdef
                        else if n == once "unary -".to_symbol then
                                s = "TAG_Int(-UNTAG_Int({p[0]}))"
                        else if n == once "output".to_symbol then
-                               v.add_instr("printf(\"%d\\n\", UNTAG_Int({p[0]}));")
+                               v.add_instr("printf(\"%ld\\n\", UNTAG_Int({p[0]}));")
                        else if n == once "ascii".to_symbol then
                                s = "TAG_Char(UNTAG_Int({p[0]}))"
                        else if n == once "succ".to_symbol then
@@ -765,8 +765,7 @@ redef class AVardeclExpr
        do
                var cname = v.cfc.varname(variable)
                if n_expr == null then
-                       var t = variable.stype
-                       v.add_assignment(cname, "{t.default_cvalue} /*decl variable {variable.name}*/")
+                       v.add_instr("/*{cname} is variable {variable.name}*/")
                else
                        var e = v.compile_expr(n_expr)
                        v.add_assignment(cname, e)
@@ -993,7 +992,7 @@ end
 redef class ASelfExpr
        redef meth compile_expr(v)
        do
-               return v.nmc.method_params[0]
+               return v.cfc.varname(v.nmc.method_params[0])
        end
 end
 
@@ -1227,10 +1226,10 @@ redef class ASuperExpr
                        arity = init_in_superclass.signature.arity
                end
                var args = new Array[String].with_capacity(arity + 1)
-               args.add(v.nmc.method_params[0])
+               args.add(v.cfc.varname(v.nmc.method_params[0]))
                if n_args.length != arity then
                        for i in [0..arity[ do
-                               args.add(v.nmc.method_params[i + 1])
+                               args.add(v.cfc.varname(v.nmc.method_params[i + 1]))
                        end
                else
                        for na in n_args do