[vala/staging: 48/91] Fix unary expression and active locals



commit 2a10ebb0f56d157945770bc971702b3a0446021c
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               |   11 +++++++-
 vala/valaunaryexpression.vala     |    9 +------
 3 files changed, 58 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index e12dc9c..6ea689a 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 81e9d35..488c499 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
         */
@@ -115,6 +115,15 @@ public class Vala.Block : Symbol, Statement {
                return local_variables;
        }
 
+       /**
+        * Returns the list of local constants.
+        *
+        * @return variable declarator list
+        */
+       public List<Constant> get_local_constants () {
+               return local_constants;
+       }
+
        public void add_local_constant (Constant constant) {
                var parent_block = parent_symbol;
                while (parent_block is Block || parent_block is Method || parent_block is PropertyAccessor) {
diff --git a/vala/valaunaryexpression.vala b/vala/valaunaryexpression.vala
index 11c3bb4..f090b97 100644
--- a/vala/valaunaryexpression.vala
+++ b/vala/valaunaryexpression.vala
@@ -215,14 +215,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]