[vala/staging] Move Template transformation to the CCodeTransformer



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

    Move Template transformation to the CCodeTransformer

 codegen/valaccodetransformer.vala | 23 ++++++++++++++++++++
 vala/valacodetransformer.vala     |  8 +++++++
 vala/valatemplate.vala            | 45 +++++++++++++++++++++------------------
 3 files changed, 55 insertions(+), 21 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index 1b82c09cd..ecff4bc8e 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -347,4 +347,27 @@ public class Vala.CCodeTransformer : CodeTransformer {
 
                base.visit_object_creation_expression (expr);
        }
+
+       public override void visit_template (Template expr) {
+               begin_replace_expression (expr);
+
+               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;
+
+               end_replace_expression (replacement);
+       }
 }
diff --git a/vala/valacodetransformer.vala b/vala/valacodetransformer.vala
index 6a50a576c..cfb661f64 100644
--- a/vala/valacodetransformer.vala
+++ b/vala/valacodetransformer.vala
@@ -225,6 +225,14 @@ public class Vala.CodeTransformer : CodeVisitor {
                return (literal != null && !literal.value);
        }
 
+       public Expression stringify (Expression expr) {
+               if (expr.value_type != null && 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_addressof_expression (Vala.AddressofExpression expr) {
                expr.accept_children (this);
        }
diff --git a/vala/valatemplate.vala b/vala/valatemplate.vala
index 5def6781b..7921d30f6 100644
--- a/vala/valatemplate.vala
+++ b/vala/valatemplate.vala
@@ -51,12 +51,23 @@ 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 string to_string () {
+               var b = new StringBuilder ();
+               b.append ("@\"");
+
+               foreach (var expr in expression_list) {
+                       if (expr is StringLiteral) {
+                               unowned string value = ((StringLiteral) expr).value;
+                               b.append (value.substring (1, (uint) (value.length - 2)));
+                       } else {
+                               b.append ("$(");
+                               b.append (expr.to_string ());
+                               b.append_c (')');
+                       }
                }
+
+               b.append_c ('"');
+               return b.str;
        }
 
        public override void replace_expression (Expression old_node, Expression new_node) {
@@ -74,25 +85,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]