compile: compile_stmt is a noop on null PExpr
[nit.git] / src / compiling / compiling_methods.nit
index c823b83..4955594 100644 (file)
@@ -24,6 +24,7 @@ redef class CompilerVisitor
        # Compile a statment node
        meth compile_stmt(n: PExpr)
        do
+               if n == null then return
                add_instr("/* Compile stmt {n.locate} */")
                n.prepare_compile_stmt(self)
                var i = cfc._variable_index
@@ -38,7 +39,7 @@ redef class CompilerVisitor
                var i = cfc._variable_index
                var s = n.compile_expr(self)
                cfc._variable_index = i
-               if s[0] == ' ' then
+               if s[0] == ' ' or cfc.is_valid_variable(s) then
                        return s
                end
                var v = cfc.get_var("Result for expr {n.locate}")
@@ -49,7 +50,8 @@ redef class CompilerVisitor
        # Ensure that a c expression is a var
        meth ensure_var(s: String, comment: String): String
        do
-               if s.substring(0,3) == "variable" then
+               if cfc.is_valid_variable(s) then
+                       add_instr("/* Ensure var {s}: {comment}*/")
                        return s
                end
                var v = cfc.get_var(null)
@@ -235,6 +237,15 @@ class CFunctionContext
                end
        end
 
+       # Is s a valid variable
+       protected meth is_valid_variable(s: String): Bool
+       do
+               for i in [0.._variable_index[ do
+                       if s == variable(i) then return true
+               end
+               return false
+       end
+
        # Mark the variable available
        meth free_var(v: String)
        do
@@ -670,9 +681,7 @@ redef class AConcreteMethPropdef
                if self isa AConcreteInitPropdef then
                        v.invoke_super_init_calls_after(null)
                end
-               if n_block != null then
-                       v.compile_stmt(n_block)
-               end
+               v.compile_stmt(n_block)
                v.add_instr("{v.nmc.return_label}: while(false);")
                if self isa AConcreteInitPropdef then
                        v.add_instr("init_table[{itpos}] = 1;")
@@ -991,9 +1000,7 @@ end
 redef class ADoExpr
        redef meth compile_stmt(v)
        do
-                if n_block != null then
-                        v.compile_stmt(n_block)
-                end
+               v.compile_stmt(n_block)
        end
 end
 
@@ -1064,9 +1071,7 @@ redef class AWhileExpr
                var e = v.compile_expr(n_expr)
                v.add_instr("if (!UNTAG_Bool({e})) break; /* while*/")
                v.cfc.free_var(e)
-               if n_block != null then
-                       v.compile_stmt(n_block)
-               end
+               v.compile_stmt(n_block)
                v.add_instr("{v.nmc.continue_label}: while(0);")
                v.unindent
                v.add_instr("}")
@@ -1092,10 +1097,7 @@ redef class AForExpr
                e = v.ensure_var(e, "For item")
                var cname = v.cfc.register_variable(variable)
                v.add_assignment(cname, e)
-               var n_block = n_block
-               if n_block != null then
-                       v.compile_stmt(n_block)
-               end
+               v.compile_stmt(n_block)
                v.add_instr("{v.nmc.continue_label}: while(0);")
                e = meth_next.compile_call(v, [iter])
                assert e == null
@@ -1323,8 +1325,10 @@ redef class ASuperstringExpr
                for ne in n_exprs do
                        var e = v.ensure_var(v.compile_expr(ne), "super-string element")
                        if ne.stype != stype then
-                               v.add_assignment(e, meth_to_s.compile_call(v, [e]))
+                               v.cfc.free_var(e)
+                               e = meth_to_s.compile_call(v, [e])
                        end
+                       v.cfc.free_var(e)
                        meth_add.compile_call(v, [array, e])
                end
 
@@ -1603,7 +1607,7 @@ redef class AClosureDef
                v.nmc.continue_label = "continue_label{v.new_number}"
                v.nmc.break_label = v.nmc.return_label
 
-               if n_expr != null then v.compile_stmt(n_expr)
+               v.compile_stmt(n_expr)
 
                v.add_instr("{v.nmc.continue_label}: while(false);")
 
@@ -1634,7 +1638,7 @@ redef class AClosureDecl
                v.nmc.continue_label = "continue_label{v.new_number}"
                v.nmc.break_label = v.nmc.return_label
 
-               if n_expr != null then v.compile_stmt(n_expr)
+               v.compile_stmt(n_expr)
 
                v.add_instr("{v.nmc.continue_label}: while(false);")