[vala/emit-let] codegen: Add convenience is_in_coroutine() function.



commit 22e2e9d4108f92ad65e0c72f19d8261ca8525bcb
Author: Luca Bruno <lethalman88 gmail com>
Date:   Tue Jan 18 10:21:00 2011 +0100

    codegen: Add convenience is_in_coroutine() function.

 codegen/valaccodebasemodule.vala         |   26 +++++++++++++++-----------
 codegen/valaccodecontrolflowmodule.vala  |   18 +++++++++---------
 codegen/valaccodememberaccessmodule.vala |    8 ++++----
 codegen/valagasyncmodule.vala            |    6 +++---
 codegen/valagerrormodule.vala            |    6 +++---
 5 files changed, 34 insertions(+), 30 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index cf1092a..5a71851 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -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);
@@ -3014,7 +3018,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 (), get_variable_cname (local.name));
 
 			// even though closure struct is zerod, we need to initialize temporary variables
@@ -3179,7 +3183,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);
@@ -3197,7 +3201,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;
@@ -3205,7 +3209,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);
@@ -3220,7 +3224,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));
@@ -3256,7 +3260,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 ();
@@ -3522,7 +3526,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 {
diff --git a/codegen/valaccodecontrolflowmodule.vala b/codegen/valaccodecontrolflowmodule.vala
index b51fe5f..6ad5f01 100644
--- a/codegen/valaccodecontrolflowmodule.vala
+++ b/codegen/valaccodecontrolflowmodule.vala
@@ -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));
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index f2058eb..74545e0 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -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) {
diff --git a/codegen/valagasyncmodule.vala b/codegen/valagasyncmodule.vala
index 9f78a98..d4ce037 100644
--- a/codegen/valagasyncmodule.vala
+++ b/codegen/valagasyncmodule.vala
@@ -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;
 		}
 
diff --git a/codegen/valagerrormodule.vala b/codegen/valagerrormodule.vala
index 7329582..0918a20 100644
--- a/codegen/valagerrormodule.vala
+++ b/codegen/valagerrormodule.vala
@@ -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));



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