[vala/wip/transform: 73/93] Move Template transformation to the code transformer



commit ed54f122fd6858aeaeded0d0da99c3a25d81d1c9
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/valatemplate.vala            | 32 ++++++++------------------------
 2 files changed, 43 insertions(+), 25 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 904318f64..413b64a1a 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/valatemplate.vala b/vala/valatemplate.vala
index 5def6781b..cf45522cf 100644
--- a/vala/valatemplate.vala
+++ b/vala/valatemplate.vala
@@ -51,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 void replace_expression (Expression old_node, Expression new_node) {
                int index = expression_list.index_of (old_node);
                if (index >= 0) {
@@ -74,25 +66,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]