[vala/wip/transform: 63/107] Fix unary expression and active locals



commit 22dbf698525bdd1df7c7dcc651b90126d2ea437e
Author: Luca Bruno <lucabru src gnome org>
Date:   Sat Feb 25 21:25:57 2012 +0100

    Fix unary expression and active locals

 codegen/valaccodetransformer.vala | 49 +++++++++++++++++++++++++++++++++++++--
 vala/valablock.vala               |  2 +-
 vala/valaunaryexpression.vala     |  9 +------
 3 files changed, 49 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 12c0a033b..8af5ee62e 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -1,6 +1,6 @@
 /* valaccodetransformer.vala
  *
- * Copyright (C) 2011  Luca Bruno
+ * Copyright (C) 2012  Luca Bruno
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -105,7 +105,21 @@ public class Vala.CCodeTransformer : CodeTransformer {
        }
 
        public override void visit_block (Block b) {
+               foreach (LocalVariable local in b.get_local_variables ()) {
+                       local.active = true;
+               }
+               foreach (Constant constant in b.get_local_constants ()) {
+                       constant.active = true;
+               }
+
                b.accept_children (this);
+
+               foreach (LocalVariable local in b.get_local_variables ()) {
+                       local.active = false;
+               }
+               foreach (Constant constant in b.get_local_constants ()) {
+                       constant.active = false;
+               }
        }
 
        public override void visit_declaration_statement (DeclarationStatement stmt) {
@@ -164,8 +178,8 @@ public class Vala.CCodeTransformer : CodeTransformer {
                                b.open_if (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, 
stmt.condition, stmt.condition.source_reference));
                                b.add_break ();
                                b.close ();
-                               b.add_statement (stmt.body);
                        }
+                       b.add_statement (stmt.body);
                        b.close ();
                }
 
@@ -459,6 +473,37 @@ public class Vala.CCodeTransformer : CodeTransformer {
                }
        }
 
+       public override void visit_unary_expression (UnaryExpression expr) {
+               var parent_statement = expr.parent_statement;
+               if (parent_statement == null) {
+                       base.visit_unary_expression (expr);
+                       return;
+               }
+
+               if (expr.operator == UnaryOperator.INCREMENT || expr.operator == UnaryOperator.DECREMENT) {
+                       var old_parent_node = expr.parent_node;
+                       var target_type = expr.target_type != null ? expr.target_type.copy () : null;
+
+                       push_builder (new CodeBuilder (context, parent_statement, expr.source_reference));
+                       Expression replacement;
+                       if (expr.operator == UnaryOperator.INCREMENT) {
+                               replacement = expression (@"$(expr.inner) = $(expr.inner) + 1");
+                       } else {
+                               replacement = expression (@"$(expr.inner) = $(expr.inner) - 1");
+                       }
+                       replacement.target_type = target_type;
+                       context.analyzer.replaced_nodes.add (expr);
+                       old_parent_node.replace_expression (expr, replacement);
+                       b.check (this);
+
+                       pop_builder ();
+                       check (replacement);
+                       return;
+               }
+
+               base.visit_unary_expression (expr);
+       }
+
        public override void visit_object_creation_expression (ObjectCreationExpression expr) {
                if (expr.tree_can_fail) {
                        if (expr.parent_node is LocalVariable || expr.parent_node is ExpressionStatement) {
diff --git a/vala/valablock.vala b/vala/valablock.vala
index 44d8f8424..3e82d8014 100644
--- a/vala/valablock.vala
+++ b/vala/valablock.vala
@@ -107,7 +107,7 @@ public class Vala.Block : Symbol, Statement {
        }
 
        /**
-        * Returns a copy of the list of local variables.
+        * Returns the list of local variables.
         *
         * @return variable declarator list
         */
diff --git a/vala/valaunaryexpression.vala b/vala/valaunaryexpression.vala
index e04dcb57d..1f5ce01a9 100644
--- a/vala/valaunaryexpression.vala
+++ b/vala/valaunaryexpression.vala
@@ -209,14 +209,7 @@ public class Vala.UnaryExpression : Expression {
                                return false;
                        }
 
-                       var old_value = new MemberAccess (ma.inner, ma.member_name, inner.source_reference);
-                       var bin = new BinaryExpression (operator == UnaryOperator.INCREMENT ? 
BinaryOperator.PLUS : BinaryOperator.MINUS, old_value, new IntegerLiteral ("1"), source_reference);
-
-                       var assignment = new Assignment (ma, bin, AssignmentOperator.SIMPLE, 
source_reference);
-                       assignment.target_type = target_type;
-                       context.analyzer.replaced_nodes.add (this);
-                       parent_node.replace_expression (this, assignment);
-                       assignment.check (context);
+                       value_type = inner.value_type.copy ();
                        return true;
                } else if (operator == UnaryOperator.REF || operator == UnaryOperator.OUT) {
                        var ea = inner as ElementAccess;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]