[vala/wip/transform: 92/106] Use more statements()



commit 364ea0def275dffed6629aecce8c62d4b0b58d25
Author: Luca Bruno <luca bruno immobiliare it>
Date:   Mon Feb 3 15:54:54 2014 +0100

    Use more statements()

 codegen/valaccodetransformer.vala       | 41 ++++++++++++++++-----------------
 codegen/valagdbusclienttransformer.vala | 38 +++++++++++++++---------------
 codegen/valagdbusservertransformer.vala | 27 +++++++++++-----------
 codegen/valagvarianttransformer.vala    | 40 +++++++++++++++-----------------
 4 files changed, 70 insertions(+), 76 deletions(-)
---
diff --git a/codegen/valaccodetransformer.vala b/codegen/valaccodetransformer.vala
index d1f3e095e..91c6e9cf4 100644
--- a/codegen/valaccodetransformer.vala
+++ b/codegen/valaccodetransformer.vala
@@ -174,10 +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});
-                               b.open_if (cond);
-                               b.add_break ();
-                               b.close ();
+                               statements ("if (!%?) { break; }", {stmt.condition});
                        }
                        b.add_statement (stmt.body);
                        b.close ();
@@ -195,13 +192,13 @@ public class Vala.CCodeTransformer : CodeTransformer {
                // do not generate variable and if block if condition is always true
                if (!always_true (stmt.condition)) {
                        var notfirst = b.add_temp_declaration (null, expression ("false"));
-                       b.open_if (expression (notfirst));
-                       b.open_if (new UnaryExpression (UnaryOperator.LOGICAL_NEGATION, stmt.condition, 
stmt.source_reference));
-                       b.add_break ();
-                       b.close ();
-                       b.add_else ();
-                       b.add_assignment (expression (notfirst), expression ("true"));
-                       b.close ();
+                       statements (@"if ($notfirst) {
+                                                       if (!%?) {
+                                                               break;
+                                                       }
+                                               } else {
+                                                       $notfirst = true;
+                                               }", {stmt.condition});
                }
                stmt.body.checked = false;
                b.add_statement (stmt.body);
@@ -403,19 +400,21 @@ public class Vala.CCodeTransformer : CodeTransformer {
                    && (expr.operator == BinaryOperator.AND || expr.operator == BinaryOperator.OR)) {
                        var is_and = expr.operator == BinaryOperator.AND;
                        var result = b.add_temp_declaration (data_type ("bool"));
-                       b.open_if (expr.left);
-                       if (is_and) {
-                               b.add_assignment (expression (result), expr.right);
-                       } else {
-                               statements (@"$result = true;");
-                       }
-                       b.add_else ();
+
                        if (is_and) {
-                               statements (@"$result = false;");
+                               statements (@"if (%?) {
+                                                       $result = %?;
+                                                       } else {
+                                                       $result = false;
+                                                       }", {expr.left, expr.right});
                        } else {
-                               b.add_assignment (expression (result), expr.right);
+                               statements (@"if (%?) {
+                                                       $result = true;
+                                                       } else {
+                                                       $result = %?;
+                                                       }", {expr.left, expr.right});
                        }
-                       b.close ();
+
                        replacement = expression (result);
                } else if (expr.operator == BinaryOperator.COALESCE) {
                        var result = b.add_temp_declaration (copy_type (expr.value_type), expr.left);
diff --git a/codegen/valagdbusclienttransformer.vala b/codegen/valagdbusclienttransformer.vala
index ba3499498..2f006cfbe 100644
--- a/codegen/valagdbusclienttransformer.vala
+++ b/codegen/valagdbusclienttransformer.vala
@@ -50,9 +50,9 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                        if (fd_list == null) {
                                fd_list = b.add_temp_declaration (data_type ("GLib.UnixFDList"), null);
                                fd_index = b.add_temp_declaration (null, expression ("0"));
-                               b.add_expression (expression (@"$fd_list = $message.get_unix_fd_list ()"));
+                               statements (@"$fd_list = $message.get_unix_fd_list ();");
                        }
-                       b.add_expression (expression (@"$iter.next (\"h\", out $fd_index)"));
+                       statements (@"$iter.next (\"h\", out $fd_index);");
                        if (type_name == "GLib.UnixInputStream") {
                                return expression (@"new GLib.UnixInputStream ($fd_list.get ($fd_index), 
true)");
                        } else if (type_name == "GLib.UnixOutputStream") {
@@ -80,11 +80,11 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                        if (fd_list == null) {
                                fd_list = b.add_temp_declaration (null, expression ("new GLib.UnixFDList 
()"));
                        }
-                       b.add_expression (expression (@"$builder.add (\"h\", $fd_list.append ($value.get_fd 
()))"));
+                       statements (@"$builder.add (\"h\", $fd_list.append ($value.get_fd ()));");
                } else if (type_name == "GLib.Variant") {
-                       b.add_expression (expression (@"$builder.add (\"v\", $value)"));
+                       statements (@"$builder.add (\"v\", $value);");
                } else {
-                       b.add_expression (expression (@"$builder.add_value ($value)"));
+                       statements (@"$builder.add_value ($value);");
                }
        }
 
@@ -110,21 +110,21 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                                has_result = true;
                        }
                }
-               b.add_expression (expression (@"$message.set_body ($builder.end ())"));
+               statements (@"$message.set_body ($builder.end ());");
                if (fd_list != null) {
-                       b.add_expression (expression (@"$message.set_unix_fd_list ($fd_list)"));
+                       statements (@"$message.set_unix_fd_list ($fd_list);");
                }
 
                // send the message
                if (is_dbus_no_reply (m)) {
-                       b.add_expression (expression (@"this.get_connection ().send_message ($message, 
GLib.DBusSendMessageFlags.NO_REPLY_EXPECTED, null)"));
+                       statements (@"this.get_connection ().send_message ($message, 
GLib.DBusSendMessageFlags.NO_REPLY_EXPECTED, null);");
                } else {
                        var yield_str = m.coroutine ? "yield " : "";
                        var method_str = m.coroutine ? "send_message_with_reply" : 
"send_message_with_reply_sync";
                        var timeout_str = method_timeout > 0 ? @"$method_timeout" : "this.get_default_timeout 
()";
-                       b.add_expression (expression (@"$reply = $yield_str this.get_connection 
().$method_str ($message, GLib.DBusSendMessageFlags.NONE, $timeout_str, null, $cancellable)"));
+                       statements (@"$reply = $yield_str this.get_connection ().$method_str ($message, 
GLib.DBusSendMessageFlags.NONE, $timeout_str, null, $cancellable);");
 
-                       b.add_expression (expression (@"$reply.to_gerror ()"));
+                       statements (@"$reply.to_gerror ();");
                }
 
                // deserialize the result
@@ -132,7 +132,7 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                string fd_index = null;
                if (has_result) {
                        var iter = b.add_temp_declaration (data_type ("GLib.VariantIter"));
-                       b.add_expression (expression (@"$iter = $reply.get_body ().iterator ()"));
+                       statements (@"$iter = $reply.get_body ().iterator ();");
                        foreach (var param in m.get_parameters ()) {
                                if (param.direction == ParameterDirection.OUT) {
                                        b.add_assignment (expression (param.name), read_dbus_value 
(param.variable_type, iter, reply, ref fd_list, ref fd_index));
@@ -194,9 +194,9 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                foreach (var param in sig.get_parameters ()) {
                        var temp = b.add_temp_declaration (copy_type (param.variable_type, true));
                        if (is_gvariant_type (param.variable_type)) {
-                               b.add_expression (expression (@"$temp = $iter.next_value ().get_variant ()"));
+                               statements (@"$temp = $iter.next_value ().get_variant ();");
                        } else {
-                               b.add_expression (expression (@"$temp = ($(param.variable_type)) 
($iter.next_value ())"));
+                               statements (@"$temp = ($(param.variable_type)) ($iter.next_value ());");
                        }
                        call.add_argument (expression (temp));
                }
@@ -219,7 +219,7 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                push_builder (new CodeBuilder.for_subroutine (m));
 
                b.open_switch (expression ("signal_name"), null);
-               b.add_expression (expression ("GLib.assert_not_reached ()"));
+               statements ("GLib.assert_not_reached ();");
                b.add_break ();
                foreach (var sig in sym.get_signals ()) {
                        if (sig.access != SymbolAccessibility.PUBLIC) {
@@ -228,7 +228,7 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
 
                        b.add_section (expression (@"\"$(get_dbus_name_for_member (sig))\""));
                        var handler_name = generate_dbus_proxy_signal (proxy_class, sig, sym);
-                       b.add_expression (expression (@"$handler_name (parameters)"));
+                       statements (@"$handler_name (parameters);");
                        b.add_break ();
                }
                b.close ();
@@ -269,14 +269,14 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                        b.open_if (expression (@"$result == null"));
 
                        b.open_try ();
-                       b.add_expression (expression (@"$result = call_sync 
(\"org.freedesktop.DBus.Properties.Get\", new Variant (\"(ss)\", \"$dbus_iface_name\", \"$dbus_name\"), 
GLib.DBusCallFlags.NONE, $timeout, null)"));
+                       statements (@"$result = call_sync (\"org.freedesktop.DBus.Properties.Get\", new 
GLib.Variant (\"(ss)\", \"$dbus_iface_name\", \"$dbus_name\"), GLib.DBusCallFlags.NONE, $timeout, null);");
                        b.add_catch_uncaught_error ();
                        b.close ();
 
-                       b.add_expression (expression (@"$result.get (\"(v)\", out $result)"));
+                       statements (@"$result.get (\"(v)\", out $result);");
                        b.close ();
 
-                       b.add_return (expression (@"($(prop.property_type)) ($result)"));
+                       statements (@"return ($(prop.property_type)) ($result);");
                        pop_builder ();
                }
 
@@ -285,7 +285,7 @@ public class Vala.GDBusClientTransformer : GVariantTransformer {
                        proxy_set = new PropertyAccessor (false, true, false, prop.set_accessor.value_type, 
null, prop.set_accessor.source_reference);
                        push_builder (new CodeBuilder.for_subroutine (proxy_set));
                        var variant = b.add_temp_declaration (data_type ("GLib.Variant"), expression 
("value"));
-                       b.add_expression (expression (@"call_sync (\"org.freedesktop.DBus.Properties.Set\", 
new Variant (\"(ssv)\", \"$dbus_iface_name\", \"$dbus_name\", $variant), GLib.DBusCallFlags.NONE, $timeout, 
null)"));
+                       statements (@"call_sync (\"org.freedesktop.DBus.Properties.Set\", new GLib.Variant 
(\"(ssv)\", \"$dbus_iface_name\", \"$dbus_name\", $variant), GLib.DBusCallFlags.NONE, $timeout, null);");
                        pop_builder ();
                }
 
diff --git a/codegen/valagdbusservertransformer.vala b/codegen/valagdbusservertransformer.vala
index 22ae5b5c4..ba03293c8 100644
--- a/codegen/valagdbusservertransformer.vala
+++ b/codegen/valagdbusservertransformer.vala
@@ -117,8 +117,7 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                        b.add_expression (finish_call);
                }
                b.add_catch_all ("_invocation_gerror_");
-               b.add_expression (expression ("invocation.return_gerror (_invocation_gerror_)"));
-               b.add_return ();
+               statements ("invocation.return_gerror (_invocation_gerror_); return;");
                b.close ();
 
                fd_list = null;
@@ -130,12 +129,12 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                if (result != null) {
                        write_dbus_value (m.return_type.copy (), builder, result, ref fd_list);
                }
-               b.add_expression (expression (@"$reply.set_body ($builder.end ())"));
+               statements (@"$reply.set_body ($builder.end ());");
                if (fd_list != null) {
-                       b.add_expression (expression (@"$reply.set_unix_fd_list ($fd_list)"));
+                       statements (@"$reply.set_unix_fd_list ($fd_list);");
                }
                b.open_try ();
-               b.add_expression (expression (@"invocation.get_connection ().send_message ($reply, 
GLib.DBusSendMessageFlags.NONE, null)"));
+               statements (@"invocation.get_connection ().send_message ($reply, 
GLib.DBusSendMessageFlags.NONE, null);");
                b.add_catch_uncaught_error ();
                b.close ();
 
@@ -191,7 +190,7 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                        }
                        b.add_section (expression (@"\"$(get_dbus_name_for_member (m))\""));
                        var wrapper = generate_dbus_method_wrapper (m, sym);
-                       b.add_expression (expression (@"$wrapper ($object, _parameters_, invocation)"));
+                       statements (@"$wrapper ($object, _parameters_, invocation);");
                        b.add_break ();
                }
                b.close ();
@@ -220,7 +219,7 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                var object_type = SemanticAnalyzer.get_data_type_for_symbol (sym);
                var object = b.add_temp_declaration (null, expression (@"($object_type) (((GLib.Object[]) 
user_data)[0])"));
                b.open_switch (expression ("property_name"), null);
-               b.add_return (expression ("null"));
+               statements ("return null;");
                foreach (var prop in sym.get_properties ()) {
                        if (prop.binding != MemberBinding.INSTANCE
                            || prop.overrides || prop.access != SymbolAccessibility.PUBLIC
@@ -229,7 +228,7 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                                continue;
                        }
                        b.add_section (expression (@"\"$(get_dbus_name_for_member (prop))\""));
-                       b.add_return (expression (@"$object.$(prop.name)"));
+                       statements (@"return $object.$(prop.name);");
                }
                pop_builder ();
                check (m);
@@ -257,7 +256,7 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                var object_type = SemanticAnalyzer.get_data_type_for_symbol (sym);
                var object = b.add_temp_declaration (null, expression (@"($object_type) (((GLib.Object[]) 
user_data)[0])"));
                b.open_switch (expression ("property_name"), null);
-               b.add_return (expression ("false"));
+               statements ("return false;");
                foreach (var prop in sym.get_properties ()) {
                        if (prop.binding != MemberBinding.INSTANCE
                            || prop.overrides || prop.access != SymbolAccessibility.PUBLIC
@@ -266,8 +265,8 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                                continue;
                        }
                        b.add_section (expression (@"\"$(get_dbus_name_for_member (prop))\""));
-                       b.add_expression (expression (@"$object.$(prop.name) = ($(prop.property_type)) 
value"));
-                       b.add_return (expression ("true"));
+                       statements (@"$object.$(prop.name) = ($(prop.property_type)) value;
+                                               return true;");
                }
                pop_builder ();
                check (m);
@@ -293,12 +292,12 @@ public class Vala.GDBusServerTransformer : GDBusClientTransformer {
                var builder = b.add_temp_declaration (null, expression ("new GLib.VariantBuilder 
(GLib.VariantType.TUPLE)"));
                foreach (var param in sig.get_parameters ()) {
                        if (is_gvariant_type (param.variable_type)) {
-                               b.add_expression (expression (@"$builder.add (\"v\", $(param.name))"));
+                               statements (@"$builder.add (\"v\", $(param.name));");
                        } else {
-                               b.add_expression (expression (@"$builder.add_value ($(param.name))"));
+                               statements (@"$builder.add_value ($(param.name));");
                        }
                }
-               b.add_expression (expression (@"((GLib.DBusConnection) _data[1]).emit_signal (null, (string) 
_data[2], \"$dbus_iface_name\", \"$(get_dbus_name_for_member (sig))\", $builder.end ())"));
+               statements (@"((GLib.DBusConnection) _data[1]).emit_signal (null, (string) _data[2], 
\"$dbus_iface_name\", \"$(get_dbus_name_for_member (sig))\", $builder.end ());");
                pop_builder ();
                check (m);
        }
diff --git a/codegen/valagvarianttransformer.vala b/codegen/valagvarianttransformer.vala
index 30fe2be7d..bef62bd74 100644
--- a/codegen/valagvarianttransformer.vala
+++ b/codegen/valagvarianttransformer.vala
@@ -241,7 +241,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                                if (is_gvariant_type (f.variable_type)) {
                                        serialized_field = @"new GLib.Variant.variant ($serialized_field)";
                                }
-                               b.add_expression (expression (@"$builder.add_value ($serialized_field)"));
+                               statements (@"$builder.add_value ($serialized_field);");
                        }
                        b.add_return (expression (@"$builder.end ()"));
 
@@ -270,9 +270,8 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        string serialized_key = is_gvariant_type (key_type) ? "new GLib.Variant.variant (k1)" 
: "k1";
                        string serialized_value = is_gvariant_type (value_type) ? "new GLib.Variant.variant 
(v1)" : "v1";
 
-                       var for_each = expression (@"ht.for_each ((k, v) => { GLib.Variant k1 = k; 
GLib.Variant v1 = v; $builder.add (\"{?*}\", $serialized_key, $serialized_value); })");
-                       b.add_expression (for_each);
-                       b.add_return (expression (@"$builder.end ()"));
+                       statements (@"ht.for_each ((k, v) => { GLib.Variant k1 = k; GLib.Variant v1 = v; 
$builder.add (\"{?*}\", $serialized_key, $serialized_value); });
+                                               return $builder.end ();");
 
                        pop_builder ();
                        check (m);
@@ -310,7 +309,7 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                                array_new.append_size (expression (length));
                                indices[dim - 1] = b.add_temp_declaration (null, expression ("0"));
                                if (dim < array_type.rank) {
-                                       b.add_expression (expression (@"$iterator.next_value ()"));
+                                       statements (@"$iterator.next_value ();");
                                }
                        }
                        b.add_assignment (expression (array), array_new);
@@ -377,9 +376,9 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                                }
 
                                if (is_gvariant_type (f.variable_type)) {
-                                       b.add_expression (expression (@"$result.$(f.name) = 
$iterator.next_value ().get_variant ()"));
+                                       statements (@"$result.$(f.name) = $iterator.next_value ().get_variant 
();");
                                } else {
-                                       b.add_expression (expression (@"$result.$(f.name) = 
($(f.variable_type)) ($iterator.next_value ())"));
+                                       statements (@"$result.$(f.name) = ($(f.variable_type)) 
($iterator.next_value ());");
                                }
                        }
                        b.add_return (expression (result));
@@ -415,15 +414,17 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        var hash_table = b.add_temp_declaration (hash_table_new_type, hash_table_new);
                        var new_variant = b.add_temp_declaration (data_type ("GLib.Variant"));
 
-                       b.open_while (expression (@"($new_variant = $iterator.next_value ()) != null"));
                        var serialized_key = @"$new_variant.get_child_value (0)";
                        serialized_key = is_gvariant_type (key_type) ? @"$serialized_key.get_variant ()" : 
@"($key_type)($serialized_key)";
                        var serialized_value = @"$new_variant.get_child_value (1)";
                        serialized_value = is_gvariant_type (value_type) ? @"$serialized_value.get_variant 
()" : @"($value_type)($serialized_value)";
-                       b.add_expression (expression (@"$hash_table.insert ($serialized_key, 
$serialized_value)"));
-                       b.close ();
 
-                       b.add_return (expression (hash_table));
+                       statements (@"while (($new_variant = $iterator.next_value ()) != null) {
+                                               $hash_table.insert ($serialized_key, $serialized_value);
+                                               }
+                                               return $hash_table;
+                                               ");
+
                        pop_builder ();
                        check (m);
                }
@@ -453,11 +454,11 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                push_builder (new CodeBuilder.for_subroutine (m));
 
                b.open_switch (expression ("str"), null);
-               b.add_throw (expression ("new GLib.DBusError.INVALID_ARGS (\"Invalid value for enum 
`%s'\")".printf (get_ccode_name (en))));
+               statements ("throw new GLib.DBusError.INVALID_ARGS (\"Invalid value for enum `%s'\");".printf 
(get_ccode_name (en)));
                foreach (var enum_value in en.get_values ()) {
                        string dbus_value = get_dbus_value (enum_value, enum_value.name);
                        b.add_section (expression (@"\"$dbus_value\""));
-                       b.add_return (expression (@"$(en.get_full_name ()).$(enum_value.name)"));
+                       statements (@"return $(en.get_full_name ()).$(enum_value.name);");
                }
                b.close ();
                pop_builder ();
@@ -546,10 +547,9 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                        return;
                }
 
-               push_builder (new CodeBuilder (context, expr.parent_statement, expr.source_reference));
-               var old_parent_node = expr.parent_node;
-               var target_type = expr.target_type.copy ();
-               var type = expr.value_type;
+               var target_type = copy_type (expr.target_type);
+               var type = copy_type (expr.value_type);
+               begin_replace_expression (expr);
 
                BasicTypeInfo basic_type;
                Expression result = null;
@@ -571,10 +571,6 @@ public class Vala.GVariantTransformer : CCodeTransformer {
                }
 
                result.target_type = target_type;
-               context.analyzer.replaced_nodes.add (expr);
-               old_parent_node.replace_expression (expr, result);
-               b.check (this);
-               pop_builder ();
-               check (result);
+               end_replace_expression (result);
        }
 }


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