[vala/wip/transform: 345/373] Fix coalescing operator



commit 3eedb49b2daf7986073bf0296c4c4d12496d19d3
Author: Luca Bruno <lucabru src gnome org>
Date:   Fri Jan 31 00:10:57 2014 +0100

    Fix coalescing operator

 codegen/valaccodetransformer.vala   | 26 ++++++++++++++++++--------
 vala/valabinaryexpression.vala      |  2 +-
 vala/valacodetransformer.vala       |  6 +++++-
 vala/valaconditionalexpression.vala |  2 +-
 4 files changed, 25 insertions(+), 11 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 6473a99ca..8e28b1f97 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -369,15 +369,15 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        } else {
                                // store parent_node as we need to replace the expression in the old parent 
node later on
                                var old_parent_node = expr.parent_node;
-                               var formal_target_type = expr.target_type != null ? expr.target_type.copy () 
: null;
-                               var target_type = expr.target_type != null ? expr.target_type.copy () : null;
+                               var formal_target_type = copy_type (expr.target_type);
+                               var target_type = copy_type (expr.target_type);
                                push_builder (new CodeBuilder (context, expr.parent_statement, 
expr.source_reference));
 
                                // FIXME: use create_temp_access behavior
-                               var replacement = expression (b.add_temp_declaration (expr.value_type.copy 
(), expr, true));
+                               var replacement = expression (b.add_temp_declaration (copy_type 
(expr.value_type), expr, true));
 
-                               replacement.target_type = target_type.copy ();
-                               replacement.formal_target_type = formal_target_type.copy ();
+                               replacement.target_type = copy_type (target_type);
+                               replacement.formal_target_type = copy_type (formal_target_type);
                                context.analyzer.replaced_nodes.add (expr);
                                old_parent_node.replace_expression (expr, replacement);
                                b.check (this);
@@ -391,8 +391,8 @@ public class Vala.CCodeTransformer : CodeTransformer {
                // convert to if statement
                Expression replacement = null;
                var old_parent_node = expr.parent_node;
-               var formal_target_type = expr.target_type != null ? expr.target_type.copy () : null;
-               var target_type = expr.target_type != null ? expr.target_type.copy () : null;
+               var formal_target_type = copy_type (expr.target_type);
+               var target_type = copy_type (expr.target_type);
                push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));
 
                var result = b.add_temp_declaration (expr.value_type);
@@ -443,7 +443,13 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        b.close ();
                        replacement = expression (result);
                } else if (expr.operator == BinaryOperator.COALESCE) {
-                       replacement = new ConditionalExpression (new BinaryExpression 
(BinaryOperator.EQUALITY, expr.left, new NullLiteral (expr.source_reference), expr.source_reference), 
expr.right, expr.left, expr.source_reference);
+                       var is_owned = expr.left.value_type.value_owned || expr.right.value_type.value_owned;
+                       var result = b.add_temp_declaration (copy_type (expr.value_type, is_owned, true), 
expr.left);
+
+                       b.open_if (expression (@"$result == null"));
+                       b.add_assignment (expression (result), expr.right);
+                       b.close ();
+                       replacement = expression (result);
                } else if (expr.operator == BinaryOperator.IN && !(expr.left.value_type.compatible 
(context.analyzer.int_type) && expr.right.value_type.compatible (context.analyzer.int_type)) && 
!(expr.right.value_type is ArrayType)) {
                        // neither enums nor array, it's contains()
                        var call = new MethodCall (new MemberAccess (expr.right, "contains", 
expr.source_reference), expr.source_reference);
@@ -519,4 +525,8 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        }
                }
        }
+
+       public override void visit_assignment (Assignment a) {
+               a.accept_children (this);
+       }
 }
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index 11a0d4cd2..f6498f6a2 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -135,7 +135,7 @@ public class Vala.BinaryExpression : Expression {
        }
 
        public override string to_string () {
-               return _left.to_string () + get_operator_string () + _right.to_string ();
+               return "(" + _left.to_string () + " " + get_operator_string () + " " + _right.to_string () + 
")";
        }
 
        public override bool is_constant () {
diff --git a/vala/valacodetransformer.vala b/vala/valacodetransformer.vala
index 31bed91f3..86e3a2d01 100644
--- a/vala/valacodetransformer.vala
+++ b/vala/valacodetransformer.vala
@@ -63,7 +63,11 @@ public class Vala.CodeTransformer : CodeVisitor {
                }
        }
 
-       public static DataType copy_type (DataType type, bool? value_owned = null, bool? nullable = null) {
+       public static DataType? copy_type (DataType? type, bool? value_owned = null, bool? nullable = null) {
+               if (type == null) {
+                       return null;
+               }
+
                var ret = type.copy ();
                if (value_owned != null) {
                        ret.value_owned = value_owned;
diff --git a/vala/valaconditionalexpression.vala b/vala/valaconditionalexpression.vala
index f70c8c004..fc139a48a 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -111,7 +111,7 @@ public class Vala.ConditionalExpression : Expression {
        }
 
        public override string to_string () {
-               return "%s ? %s : %s".printf (condition.to_string (), true_expression.to_string (), 
false_expression.to_string ());
+               return "(%s ? %s : %s)".printf (condition.to_string (), true_expression.to_string (), 
false_expression.to_string ());
        }
 
        public override void replace_expression (Expression old_node, Expression new_node) {


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