]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Add convenience is_in_coroutine() function
authorLuca Bruno <lucabru@src.gnome.org>
Tue, 18 Jan 2011 09:21:00 +0000 (10:21 +0100)
committerJürg Billeter <j@bitron.ch>
Wed, 19 Jan 2011 21:08:19 +0000 (22:08 +0100)
codegen/valaccodebasemodule.vala
codegen/valaccodecontrolflowmodule.vala
codegen/valaccodememberaccessmodule.vala
codegen/valagasyncmodule.vala
codegen/valagerrormodule.vala

index c0c249092c91c17e5e9cea8b10ff3f1e5face14e..e6df5df1f6c7413be5f04f591463ff9f28469b20 100644 (file)
@@ -136,6 +136,10 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                }
        }
 
+       public bool is_in_coroutine () {
+               return current_method != null && current_method.coroutine;
+       }
+
        public bool is_in_constructor () {
                if (current_method != null) {
                        // make sure to not return true in lambda expression inside constructor
@@ -1683,7 +1687,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                } else if (param.variable_type is DelegateType) {
                        CCodeExpression target_expr;
                        CCodeExpression delegate_target_destroy_notify;
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                target_expr = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (param.name)));
                                delegate_target_destroy_notify = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (param.name)));
                        } else {
@@ -1810,7 +1814,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        var data_alloc = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
                        data_alloc.add_argument (new CCodeIdentifier (struct_name));
 
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (struct_name + "*", "_data%d_".printf (block_id));
                        } else {
                                ccode.add_declaration (struct_name + "*", new CCodeVariableDeclarator ("_data%d_".printf (block_id)));
@@ -1959,7 +1963,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        }
 
        public CCodeExpression get_variable_cexpression (string name) {
-               if (current_method != null && current_method.coroutine) {
+               if (is_in_coroutine ()) {
                        return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (name));
                } else {
                        return new CCodeIdentifier (get_variable_cname (name));
@@ -1985,7 +1989,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        }
 
        public CCodeExpression get_result_cexpression (string cname = "result") {
-               if (current_method != null && current_method.coroutine) {
+               if (is_in_coroutine ()) {
                        return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), cname);
                } else {
                        return new CCodeIdentifier (cname);
@@ -3023,7 +3027,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        vardecl.init0 = true;
                }
 
-               if (current_method != null && current_method.coroutine) {
+               if (is_in_coroutine ()) {
                        closure_struct.add_field (local.variable_type.get_cname (), local.name);
 
                        // even though closure struct is zerod, we need to initialize temporary variables
@@ -3188,7 +3192,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
                        for (int dim = 1; dim <= array_type.rank; dim++) {
                                var len_l = get_result_cexpression (get_array_length_cname ("result", dim));
-                               if (current_method == null || !current_method.coroutine) {
+                               if (!is_in_coroutine ()) {
                                        len_l = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, len_l);
                                }
                                var len_r = get_array_length_cexpression (stmt.return_expression, dim);
@@ -3206,7 +3210,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                ccode.add_assignment (get_variable_cexpression (return_expr_decl.name), get_cvalue (stmt.return_expression));
 
                                var target_l = get_result_cexpression (get_delegate_target_cname ("result"));
-                               if (current_method == null || !current_method.coroutine) {
+                               if (!is_in_coroutine ()) {
                                        target_l = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, target_l);
                                }
                                CCodeExpression target_r_destroy_notify;
@@ -3214,7 +3218,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                                ccode.add_assignment (target_l, target_r);
                                if (delegate_type.value_owned) {
                                        var target_l_destroy_notify = get_result_cexpression (get_delegate_target_destroy_notify_cname ("result"));
-                                       if (current_method == null || !current_method.coroutine) {
+                                       if (!is_in_coroutine ()) {
                                                target_l_destroy_notify = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, target_l_destroy_notify);
                                        }
                                        ccode.add_assignment (target_l_destroy_notify, target_r_destroy_notify);
@@ -3229,7 +3233,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                if (stmt.return_expression != null) {
                        // assign method result to `result'
                        CCodeExpression result_lhs = get_result_cexpression ();
-                       if (current_return_type.is_real_non_null_struct_type () && (current_method == null || !current_method.coroutine)) {
+                       if (current_return_type.is_real_non_null_struct_type () && !is_in_coroutine ()) {
                                result_lhs = new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, result_lhs);
                        }
                        ccode.add_assignment (result_lhs, get_cvalue (stmt.return_expression));
@@ -3265,7 +3269,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                        ccode.add_goto ("_return");
                } else if (current_method is CreationMethod) {
                        ccode.add_return (new CCodeIdentifier ("self"));
-               } else if (current_method != null && current_method.coroutine) {
+               } else if (is_in_coroutine ()) {
                } else if (current_return_type is VoidType || current_return_type.is_real_non_null_struct_type ()) {
                        // structs are returned via out parameter
                        ccode.add_return ();
@@ -3531,7 +3535,7 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
        public override void visit_base_access (BaseAccess expr) {
                CCodeExpression this_access;
-               if (current_method != null && current_method.coroutine) {
+               if (is_in_coroutine ()) {
                        // use closure
                        this_access = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "self");
                } else {
index b51fe5fe45be9d55f501917346e8e7a681ea404a..6ad5f014d116fa9d6e61e2dc1dcbed7761c9fad2 100644 (file)
@@ -230,7 +230,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        array_type.fixed_length = false;
                }
 
-               if (current_method != null && current_method.coroutine) {
+               if (is_in_coroutine ()) {
                        closure_struct.add_field (collection_type.get_cname (), collection_backup.name);
                } else {
                        var ccolvardecl = new CCodeVariableDeclarator (collection_backup.name);
@@ -249,7 +249,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        var array_len = get_array_length_cexpression (stmt.collection);
 
                        // store array length for use by _vala_array_free
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field ("int", get_array_length_cname (collection_backup.name, 1));
                        } else {
                                ccode.add_declaration ("int", new CCodeVariableDeclarator (get_array_length_cname (collection_backup.name, 1)));
@@ -258,7 +258,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
                        var it_name = (stmt.variable_name + "_it");
                
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field ("int", it_name);
                        } else {
                                ccode.add_declaration ("int", new CCodeVariableDeclarator (it_name));
@@ -276,7 +276,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        element_type.value_owned = false;
                        element_expr = transform_expression (element_expr, element_type, stmt.type_reference);
 
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
                        } else {
                                ccode.add_declaration (stmt.type_reference.get_cname (), new CCodeVariableDeclarator (stmt.variable_name));
@@ -287,7 +287,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        if (stmt.type_reference is ArrayType) {
                                var inner_array_type = (ArrayType) stmt.type_reference;
                                for (int dim = 1; dim <= inner_array_type.rank; dim++) {
-                                       if (current_method != null && current_method.coroutine) {
+                                       if (is_in_coroutine ()) {
                                                closure_struct.add_field ("int", get_array_length_cname (stmt.variable_name, dim));
                                                ccode.add_assignment (get_variable_cexpression (get_array_length_cname (stmt.variable_name, dim)), new CCodeConstant ("-1"));
                                        } else {
@@ -304,7 +304,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
                        var it_name = "%s_it".printf (stmt.variable_name);
                
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (collection_type.get_cname (), it_name);
                        } else {
                                ccode.add_declaration (collection_type.get_cname (), new CCodeVariableDeclarator (it_name));
@@ -329,7 +329,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                        element_expr = convert_from_generic_pointer (element_expr, element_data_type);
                        element_expr = transform_expression (element_expr, element_data_type, stmt.type_reference);
 
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
                        } else {
                                ccode.add_declaration (stmt.type_reference.get_cname (), new CCodeVariableDeclarator (stmt.variable_name));
@@ -344,7 +344,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
 
                        var arr_index = "%s_index".printf (stmt.variable_name);
 
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (uint_type.get_cname (), arr_index);
                        } else {
                                ccode.add_declaration (uint_type.get_cname (), new CCodeVariableDeclarator (arr_index));
@@ -366,7 +366,7 @@ public abstract class Vala.CCodeControlFlowModule : CCodeMethodModule {
                                element_expr = get_ref_cexpression (stmt.type_reference, element_expr, null, new StructValueType (gvalue_type));
                        }
 
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field (stmt.type_reference.get_cname (), stmt.variable_name);
                        } else {
                                ccode.add_declaration (stmt.type_reference.get_cname (), new CCodeVariableDeclarator (stmt.variable_name));
index bad542dcabcfb50809e6ef19e66f1502e1e81a28..6e8d1a37284618311252fe2e5f0067ffd580e4bf 100644 (file)
@@ -560,7 +560,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                        result.array_size_cvalue = get_variable_cexpression (get_array_size_cname (get_variable_cname (local.name)));
                                }
                        } else if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
-                               if (current_method != null && current_method.coroutine) {
+                               if (is_in_coroutine ()) {
                                        result.delegate_target_cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (get_variable_cname (local.name)));
                                        result.delegate_target_destroy_notify_cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_destroy_notify_cname (get_variable_cname (local.name)));
                                } else {
@@ -578,7 +578,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
        /* Returns access values to the given parameter */
        public TargetValue get_parameter_cvalue (Parameter param) {
                var result = new GLibValue (param.variable_type.copy ());
-               if (param.captured || current_method != null && current_method.coroutine) {
+               if (param.captured || is_in_coroutine ()) {
                        result.value_type.value_owned = true;
                }
 
@@ -586,7 +586,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                var delegate_type = result.value_type as DelegateType;
 
                if (param.name == "this") {
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                // use closure
                                result.cvalue = new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), "self");
                        } else {
@@ -615,7 +615,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                        result.delegate_target_cvalue = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (get_variable_cname (param.name)));
                                        result.delegate_target_destroy_notify_cvalue = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_destroy_notify_cname (get_variable_cname (param.name)));
                                }
-                       } else if (current_method != null && current_method.coroutine) {
+                       } else if (is_in_coroutine ()) {
                                // use closure
                                result.cvalue = get_variable_cexpression (param.name);
                                if (delegate_type != null && delegate_type.delegate_symbol.has_target) {
index 9f78a9861eea53c93cdccf352e8ee70e915c3b80..d4ce037ab2b893527e92d6a8becb50673f132fa6 100644 (file)
@@ -556,7 +556,7 @@ public class Vala.GAsyncModule : GSignalModule {
        }
 
        public override void visit_yield_statement (YieldStatement stmt) {
-               if (current_method == null || !current_method.coroutine) {
+               if (!is_in_coroutine ()) {
                        return;
                }
 
@@ -595,7 +595,7 @@ public class Vala.GAsyncModule : GSignalModule {
 
        public override void return_with_exception (CCodeExpression error_expr)
        {
-               if (!current_method.coroutine) {
+               if (!is_in_coroutine ()) {
                        base.return_with_exception (error_expr);
                        return;
                }
@@ -617,7 +617,7 @@ public class Vala.GAsyncModule : GSignalModule {
        public override void visit_return_statement (ReturnStatement stmt) {
                base.visit_return_statement (stmt);
 
-               if (current_method == null || !current_method.coroutine) {
+               if (!is_in_coroutine ()) {
                        return;
                }
 
index 7329582d8ff0b33841e076af10d31b216471d5cd..0918a20be6cea639e90c9ed50fd833b2b7b1f563 100644 (file)
@@ -107,7 +107,7 @@ public class Vala.GErrorModule : CCodeDelegateModule {
                        var unref_call = get_unref_expression (new CCodeIdentifier ("self"), new ObjectType (cl), null);
                        ccode.add_expression (unref_call);
                        ccode.add_return (new CCodeConstant ("NULL"));
-               } else if (current_method != null && current_method.coroutine) {
+               } else if (is_in_coroutine ()) {
                        ccode.add_return (new CCodeConstant ("FALSE"));
                } else {
                        return_default_value (current_return_type);
@@ -143,7 +143,7 @@ public class Vala.GErrorModule : CCodeDelegateModule {
                        } else {
                                ccode.add_return (new CCodeConstant ("NULL"));
                        }
-               } else if (current_method != null && current_method.coroutine) {
+               } else if (is_in_coroutine ()) {
                        ccode.add_return (new CCodeConstant ("FALSE"));
                } else if (current_return_type != null) {
                        return_default_value (current_return_type);
@@ -353,7 +353,7 @@ public class Vala.GErrorModule : CCodeDelegateModule {
                }
 
                if (clause.variable_name != null) {
-                       if (current_method != null && current_method.coroutine) {
+                       if (is_in_coroutine ()) {
                                closure_struct.add_field ("GError *", variable_name);
                        } else {
                                ccode.add_declaration ("GError *", new CCodeVariableDeclarator (variable_name));