]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Use %? in the parser rather than stringifying expressions
authorLuca Bruno <luca.bruno@immobiliare.it>
Mon, 3 Feb 2014 13:30:02 +0000 (14:30 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Mon, 11 Mar 2019 12:52:38 +0000 (13:52 +0100)
codegen/valaccodetransformer.vala
codegen/valagvarianttransformer.vala
vala/valacodebuilder.vala
vala/valacodetransformer.vala
vala/valaparser.vala

index 18f64e8e717667bff98b0963d15d0fb659f6442c..d1f3e095efc5aa9aaa4fec2ff74ad9b894688b7d 100644 (file)
@@ -174,7 +174,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                if (!always_false (stmt.condition)) {
                        b.open_loop ();
                        if (!always_true (stmt.condition)) {
-                               var cond = expression (@"!$(stmt.condition)");
+                               var cond = expression ("!%?", {stmt.condition});
                                b.open_if (cond);
                                b.add_break ();
                                b.close ();
@@ -231,7 +231,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        b.close ();
 
                        if (stmt.condition != null && !always_true (stmt.condition)) {
-                               statements (@"if (!$(stmt.condition)) break;");
+                               statements ("if (!%?) break;", {stmt.condition});
                        }
                        b.add_statement (stmt.body);
 
@@ -378,11 +378,11 @@ public class Vala.CCodeTransformer : CodeTransformer {
                begin_replace_expression (expr);
 
                var result = b.add_temp_declaration (expr.value_type);
-               statements (@"if ($(expr.condition)) {
-                                       $result = $(expr.true_expression);
+               statements (@"if (%?) {
+                                       $result = %?;
                                        } else {
-                                       $result = $(expr.false_expression);
-                                       }");
+                                       $result = %?;
+                                       }", {expr.condition, expr.true_expression, expr.false_expression});
 
                replacement = return_temp_access (result, expr.value_type, target_type, formal_target_type);
                end_replace_expression (replacement);
@@ -407,11 +407,11 @@ public class Vala.CCodeTransformer : CodeTransformer {
                        if (is_and) {
                                b.add_assignment (expression (result), expr.right);
                        } else {
-                               b.add_expression (expression (@"$result = true"));
+                               statements (@"$result = true;");
                        }
                        b.add_else ();
                        if (is_and) {
-                               b.add_expression (expression (@"$result = false"));
+                               statements (@"$result = false;");
                        } else {
                                b.add_assignment (expression (result), expr.right);
                        }
@@ -420,9 +420,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                } else if (expr.operator == BinaryOperator.COALESCE) {
                        var result = b.add_temp_declaration (copy_type (expr.value_type), expr.left);
 
-                       b.open_if (expression (@"$result == null"));
-                       b.add_assignment (expression (result), expr.right);
-                       b.close ();
+                       statements (@"if ($result == null) { $result = %?; }", {expr.right});
 
                        replacement = return_temp_access (result, expr.value_type, target_type);
                } 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)) {
@@ -494,7 +492,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                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 ()");
+                       return expression (@"%?.to_string ()", {expr});
                }
        }
 
@@ -509,7 +507,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
                } else {
                        replacement = stringify (expression_list[0]);
                        if (expression_list.size > 1) {
-                               var concat = (MethodCall) expression (@"$replacement.concat()");
+                               var concat = (MethodCall) expression ("%?.concat()", {replacement});
                                for (int i = 1; i < expression_list.size; i++) {
                                        concat.add_argument (stringify (expression_list[i]));
                                }
@@ -526,7 +524,7 @@ public class Vala.CCodeTransformer : CodeTransformer {
 
                var result = b.add_temp_declaration (copy_type (expr.value_type), expr.inner);
                var op = expr.increment ? "+ 1" : "- 1";
-               b.add_expression (expression (@"$(expr.inner) = $result $op"));
+               statements (@"$(expr.inner) = $result $op;");
 
                var replacement = return_temp_access (result, expr.value_type, expr.target_type);
 
index 6a77ea22d5dd8372f6ab5b881d7e2cde97cbe60c..30fe2be7d08f0a5fd8b01f799fe5877dcb2c636c 100644 (file)
@@ -367,7 +367,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        m.add_parameter (new Parameter ("variant", data_type ("GLib.Variant", false), b.source_reference));
                        push_builder (new CodeBuilder.for_subroutine (m));
 
-                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression (@"variant.iterator ()"));
+                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression ("variant.iterator ()"));
 
                        var result = b.add_temp_declaration (type, expression ("{}"));
 
@@ -397,7 +397,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        m.add_parameter (new Parameter ("variant", data_type ("GLib.Variant", false), b.source_reference));
                        push_builder (new CodeBuilder.for_subroutine (m));
 
-                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression (@"variant.iterator ()"));
+                       var iterator = b.add_temp_declaration (data_type ("GLib.VariantIter"), expression ("variant.iterator ()"));
 
                        var type_args = type.get_type_arguments ();
                        assert (type_args.size == 2);
index 036303cb9a88502591970b59faa5a0e6389f6a47..a79abd53f24af6ca44d91ffca76fa38bd9e99d46 100644 (file)
@@ -268,12 +268,12 @@ public class Vala.CodeBuilder {
 
        /* Utilities for building the code */
 
-       public Expression expression (string str) {
-               return new Parser().parse_expression_string (str, source_reference);
+       public Expression expression (string str, owned Expression[]? replacements = null) {
+               return new Parser().parse_expression_string (str, (owned) replacements, source_reference);
        }
 
-       public void statements (string str) {
-               new Parser().parse_statements_string (str, current_block, source_reference);
+       public void statements (string str, owned Expression[]? replacements = null) {
+               new Parser().parse_statements_string (str, current_block, (owned) replacements, source_reference);
        }
 
        // only qualified types, will slightly simplify the work of SymbolResolver
index f6444b8e976d6ce5e9a947d09aa67b891ef100ad..0f5333dcfacfe6ec8feb7b4031687d37907808ab 100644 (file)
@@ -180,12 +180,12 @@ public class Vala.CodeTransformer : CodeVisitor {
                return CodeBuilder.data_type (s, value_owned, nullable);
        }
 
-       public Expression expression (string str) {
-               return b.expression (str);
+       public Expression expression (string str, Expression[]? replacements = null) {
+               return b.expression (str, replacements);
        }
 
-       public void statements (string str) {
-               b.statements (str);
+       public void statements (string str, owned Expression[]? replacements = null) {
+               b.statements (str, (owned) replacements);
        }
 
        public void check (CodeNode node) {
index fdeedb7dc89146e3288b6929f68731b938b50b3c..dc10c33d507ac7a4240621eb4cb82900dd8f1f12 100644 (file)
@@ -31,6 +31,8 @@ public class Vala.Parser : CodeVisitor {
        CodeContext context;
        bool compiler_code = false;
        SourceReference? from_string_reference = null;
+       Expression[]? replacements = null;
+       int replacement_index = 0;
 
        // token buffer
        TokenInfo[] tokens;
@@ -337,10 +339,12 @@ public class Vala.Parser : CodeVisitor {
                }
        }
 
-       public Expression? parse_expression_string (string str, SourceReference source_reference) {
+       public Expression? parse_expression_string (string str, owned Expression[]? replacements, SourceReference source_reference) {
                compiler_code = true;
                context = source_reference.file.context;
                from_string_reference = source_reference;
+               this.replacements = (owned) replacements;
+               replacement_index = 0;
 
                scanner = new Scanner.from_string (str, source_reference.file);
                index = -1;
@@ -358,10 +362,12 @@ public class Vala.Parser : CodeVisitor {
                return null;
        }
 
-       public void parse_statements_string (string str, Block block, SourceReference source_reference) {
+       public void parse_statements_string (string str, Block block, owned Expression[]? replacements, SourceReference source_reference) {
                compiler_code = true;
                context = source_reference.file.context;
                from_string_reference = source_reference;
+               this.replacements = (owned) replacements;
+               replacement_index = 0;
 
                scanner = new Scanner.from_string (str, source_reference.file);
                index = -1;
@@ -704,7 +710,18 @@ public class Vala.Parser : CodeVisitor {
                        expr = parse_typeof_expression ();
                        break;
                default:
-                       expr = parse_simple_name ();
+                       if (compiler_code && current () == TokenType.PERCENT) {
+                               var begin2 = get_location ();
+                               next ();
+                               if (accept (TokenType.INTERR)) {
+                                       expr = replacements[replacement_index++];
+                               } else {
+                                       rollback (begin2);
+                                       expr = parse_simple_name ();
+                               }
+                       } else {
+                               expr = parse_simple_name ();
+                       }
                        break;
                }