[vala/wip/transform: 219/237] Move Template transformation to the code transformer



commit c0d484fed1d6efed6fecb62696023ae1385104af
Author: Luca Bruno <lucabru src gnome org>
Date:   Sat Feb 1 12:26:29 2014 +0100

    Move Template transformation to the code transformer

 codegen/valaccodetransformer.vala |   36 +++++++++++++++++++++++++++++++++++-
 vala/valamethodcall.vala          |   19 +++++++++++++++++++
 vala/valatemplate.vala            |   33 +++++++++------------------------
 3 files changed, 63 insertions(+), 25 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 432dea0..626c5a7 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -443,7 +443,6 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        b.close ();
                        replacement = expression (result);
                } else if (expr.operator == BinaryOperator.COALESCE) {
-                       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), expr.left);
 
                        b.open_if (expression (@"$result == null"));
@@ -527,6 +526,41 @@ public class Vala.CCodeTransformer : CodeTransformer {
                }
        }
 
+       Expression stringify (Expression expr) {
+               if (expr.value_type.data_type != null && expr.value_type.data_type.is_subtype_of 
(context.analyzer.string_type.data_type)) {
+                       return expr;
+               } else {
+                       return expression (@"($expr).to_string ()");
+               }
+       }
+
+       public override void visit_template (Template expr) {
+               push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));
+
+               Expression replacement;
+
+               var expression_list = expr.get_expressions ();
+               if (expression_list.size == 0) {
+                       replacement = expression ("\"\"");
+               } else {
+                       replacement = stringify (expression_list[0]);
+                       if (expression_list.size > 1) {
+                               var concat = (MethodCall) expression (@"($replacement).concat()");
+                               for (int i = 1; i < expression_list.size; i++) {
+                                       concat.add_argument (stringify (expression_list[i]));
+                               }
+                               replacement = concat;
+                       }
+               }
+               replacement.target_type = expr.target_type;
+
+               context.analyzer.replaced_nodes.add (expr);
+               expr.parent_node.replace_expression (expr, replacement);
+               b.check (this);
+               pop_builder ();
+               check (replacement);
+       }
+
        public override void visit_assignment (Assignment a) {
                a.accept_children (this);
        }
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 7cbc62f..b29decb 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -636,4 +636,23 @@ public class Vala.MethodCall : Expression {
 
                return null;
        }
+
+       public override string to_string () {
+               var b = new StringBuilder ();
+               b.append_c ('(');
+               b.append (call.to_string ());
+               b.append_c ('(');
+
+               bool first = true;
+               foreach (var expr in argument_list) {
+                       if (!first) {
+                               b.append (", ");
+                       }
+                       b.append (expr.to_string ());
+                       first = true;
+               }
+               b.append ("))");
+
+               return b.str;
+       }
 }
diff --git a/vala/valatemplate.vala b/vala/valatemplate.vala
index 5e27a7b..f346e90 100644
--- a/vala/valatemplate.vala
+++ b/vala/valatemplate.vala
@@ -39,6 +39,7 @@ public class Vala.Template : Expression {
        }
 
        public void add_expression (Expression expr) {
+               expr.parent_node = this;
                expression_list.add (expr);
        }
 
@@ -50,14 +51,6 @@ public class Vala.Template : Expression {
                return false;
        }
 
-       Expression stringify (Expression expr) {
-               if (expr is StringLiteral) {
-                       return expr;
-               } else {
-                       return new MethodCall (new MemberAccess (expr, "to_string", expr.source_reference), 
expr.source_reference);
-               }
-       }
-
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
@@ -65,25 +58,17 @@ public class Vala.Template : Expression {
 
                checked = true;
 
-               Expression expr;
-
-               if (expression_list.size == 0) {
-                       expr = new StringLiteral ("\"\"", source_reference);
-               } else {
-                       expr = stringify (expression_list[0]);
-                       if (expression_list.size > 1) {
-                               var concat = new MethodCall (new MemberAccess (expr, "concat", 
source_reference), source_reference);
-                               for (int i = 1; i < expression_list.size; i++) {
-                                       concat.add_argument (stringify (expression_list[i]));
-                               }
-                               expr = concat;
+               foreach (var expr in expression_list) {
+                       if (!expr.check (context)) {
+                               error = true;
+                               continue;
                        }
                }
-               expr.target_type = target_type;
 
-               context.analyzer.replaced_nodes.add (this);
-               parent_node.replace_expression (this, expr);
-               return expr.check (context);
+               value_type = context.analyzer.string_type.copy ();
+               value_type.value_owned = true;
+
+               return !error;
        }
 }
 


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