[vala] Add experimental support for closures in async methods



commit 10c6f55e6c64fbaef129475ffa0760e5a829ad05
Author: Jürg Billeter <j bitron ch>
Date:   Wed Sep 16 17:20:23 2009 +0200

    Add experimental support for closures in async methods

 codegen/valaccodearraymodule.vala        |    4 +-
 codegen/valaccodebasemodule.vala         |   33 +++++++++++++++++------------
 codegen/valaccodedelegatemodule.vala     |    6 ++--
 codegen/valaccodememberaccessmodule.vala |    4 +-
 codegen/valaccodemethodmodule.vala       |    4 +-
 5 files changed, 28 insertions(+), 23 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index c2c5d7b..1bc1f63 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -172,7 +172,7 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
 				if (param.captured) {
 					// captured variables are stored on the heap
 					var block = ((Method) param.parent_symbol).body;
-					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_array_length_cname (param.name, dim));
+					return new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_array_length_cname (param.name, dim));
 				} else if (current_method != null && current_method.coroutine) {
 					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_array_length_cname (param.name, dim));
 				} else {
@@ -201,7 +201,7 @@ internal class Vala.CCodeArrayModule : CCodeMethodCallModule {
 				if (local.captured) {
 					// captured variables are stored on the heap
 					var block = (Block) local.parent_symbol;
-					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_array_length_cname (local.name, dim));
+					return new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_array_length_cname (local.name, dim));
 				} else if (current_method != null && current_method.coroutine) {
 					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_array_length_cname (local.name, dim));
 				} else {
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index ae1db97..ac7b3d6 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -1660,25 +1660,30 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			var data_alloc = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_new0"));
 			data_alloc.add_argument (new CCodeIdentifier (struct_name));
 
-			var data_decl = new CCodeDeclaration (struct_name + "*");
-			data_decl.add_declarator (new CCodeVariableDeclarator ("_data%d_".printf (block_id), data_alloc));
-			cblock.add_statement (data_decl);
+			if (current_method != null && current_method.coroutine) {
+				closure_struct.add_field (struct_name + "*", "_data%d_".printf (block_id));
+				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (get_variable_cexpression ("_data%d_".printf (block_id)), data_alloc)));
+			} else {
+				var data_decl = new CCodeDeclaration (struct_name + "*");
+				data_decl.add_declarator (new CCodeVariableDeclarator ("_data%d_".printf (block_id), data_alloc));
+				cblock.add_statement (data_decl);
+			}
 
 			// initialize ref_count
-			cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "ref_count"), new CCodeIdentifier ("1"))));
+			cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "ref_count"), new CCodeIdentifier ("1"))));
 
 			if (parent_block != null) {
 				int parent_block_id = get_block_id (parent_block);
 
 				var ref_call = new CCodeFunctionCall (new CCodeIdentifier ("block%d_data_ref".printf (parent_block_id)));
-				ref_call.add_argument (new CCodeIdentifier ("_data%d_".printf (parent_block_id)));
+				ref_call.add_argument (get_variable_cexpression ("_data%d_".printf (parent_block_id)));
 
-				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), ref_call)));
+				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id)), ref_call)));
 			} else if (current_method.binding == MemberBinding.INSTANCE) {
 				var ref_call = new CCodeFunctionCall (get_dup_func_expression (new ObjectType (current_class), b.source_reference));
 				ref_call.add_argument (new CCodeIdentifier ("self"));
 
-				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "self"), ref_call)));
+				cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "self"), ref_call)));
 			}
 
 			if (b.parent_symbol is Method) {
@@ -1686,17 +1691,17 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 				foreach (var param in ((Method) b.parent_symbol).get_parameters ()) {
 					if (param.captured) {
 						data.add_field (param.parameter_type.get_cname (), get_variable_cname (param.name));
-						cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), get_variable_cname (param.name)), new CCodeIdentifier (get_variable_cname (param.name)))));
+						cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), get_variable_cname (param.name)), new CCodeIdentifier (get_variable_cname (param.name)))));
 
 						if (param.parameter_type is ArrayType) {
 							var array_type = (ArrayType) param.parameter_type;
 							for (int dim = 1; dim <= array_type.rank; dim++) {
 								data.add_field ("gint", get_array_length_cname (get_variable_cname (param.name), dim));
-								cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), get_array_length_cname (get_variable_cname (param.name), dim)), new CCodeIdentifier (get_array_length_cname (get_variable_cname (param.name), dim)))));
+								cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), get_array_length_cname (get_variable_cname (param.name), dim)), new CCodeIdentifier (get_array_length_cname (get_variable_cname (param.name), dim)))));
 							}
 						} else if (param.parameter_type is DelegateType) {
 							data.add_field ("gpointer", get_delegate_target_cname (get_variable_cname (param.name)));
-							cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), get_delegate_target_cname (get_variable_cname (param.name))), new CCodeIdentifier (get_delegate_target_cname (get_variable_cname (param.name))))));
+							cblock.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), get_delegate_target_cname (get_variable_cname (param.name))), new CCodeIdentifier (get_delegate_target_cname (get_variable_cname (param.name))))));
 						}
 					}
 				}
@@ -1764,7 +1769,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			int block_id = get_block_id (b);
 
 			var data_unref = new CCodeFunctionCall (new CCodeIdentifier ("block%d_data_unref".printf (block_id)));
-			data_unref.add_argument (new CCodeIdentifier ("_data%d_".printf (block_id)));
+			data_unref.add_argument (get_variable_cexpression ("_data%d_".printf (block_id)));
 			cblock.add_statement (new CCodeExpressionStatement (data_unref));
 		}
 
@@ -1890,7 +1895,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 					var lhs_delegate_target = get_variable_cexpression (get_delegate_target_cname (get_variable_cname (local.name)));
 					if (local.captured) {
 						var block = (Block) local.parent_symbol;
-						lhs_delegate_target = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (local.name));
+						lhs_delegate_target = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (local.name));
 					}
 					var rhs_delegate_target = get_delegate_target_cexpression (local.initializer);
 					ccomma.append_expression (new CCodeAssignment (lhs_delegate_target, rhs_delegate_target));
@@ -1932,7 +1937,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 
 		if (local.captured) {
 			if (local.initializer != null) {
-				cfrag.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id ((Block) local.parent_symbol))), get_variable_cname (local.name)), rhs)));
+				cfrag.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id ((Block) local.parent_symbol))), get_variable_cname (local.name)), rhs)));
 			}
 		} else if (current_method != null && current_method.coroutine) {
 			closure_struct.add_field (local.variable_type.get_cname (), get_variable_cname (local.name) + local.variable_type.get_cdeclarator_suffix ());
@@ -2604,7 +2609,7 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 			int block_id = get_block_id (b);
 
 			var data_unref = new CCodeFunctionCall (new CCodeIdentifier ("block%d_data_unref".printf (block_id)));
-			data_unref.add_argument (new CCodeIdentifier ("_data%d_".printf (block_id)));
+			data_unref.add_argument (get_variable_cexpression ("_data%d_".printf (block_id)));
 			cfrag.append (new CCodeExpressionStatement (data_unref));
 		}
 
diff --git a/codegen/valaccodedelegatemodule.vala b/codegen/valaccodedelegatemodule.vala
index 2d1f2bf..2aab947 100644
--- a/codegen/valaccodedelegatemodule.vala
+++ b/codegen/valaccodedelegatemodule.vala
@@ -143,7 +143,7 @@ internal class Vala.CCodeDelegateModule : CCodeArrayModule {
 				closure_block = closure_block.parent_symbol as Block;
 			}
 			if (closure_block != null) {
-				return new CCodeIdentifier ("_data%d_".printf (get_block_id (closure_block)));
+				return get_variable_cexpression ("_data%d_".printf (get_block_id (closure_block)));
 			} else if (get_this_type () != null || in_constructor) {
 				return new CCodeIdentifier ("self");
 			} else {
@@ -155,7 +155,7 @@ internal class Vala.CCodeDelegateModule : CCodeArrayModule {
 				if (param.captured) {
 					// captured variables are stored on the heap
 					var block = ((Method) param.parent_symbol).body;
-					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (param.name));
+					return new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (param.name));
 				} else if (current_method != null && current_method.coroutine) {
 					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (param.name));
 				} else {
@@ -176,7 +176,7 @@ internal class Vala.CCodeDelegateModule : CCodeArrayModule {
 				if (local.captured) {
 					// captured variables are stored on the heap
 					var block = (Block) local.parent_symbol;
-					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (local.name));
+					return new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_delegate_target_cname (local.name));
 				} else if (current_method != null && current_method.coroutine) {
 					return new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_delegate_target_cname (local.name));
 				} else {
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 6c7dc86..fae83be 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -347,7 +347,7 @@ internal class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 			} else if (local.captured) {
 				// captured variables are stored on the heap
 				var block = (Block) local.parent_symbol;
-				expr.ccodenode = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_variable_cname (local.name));
+				expr.ccodenode = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_variable_cname (local.name));
 			} else {
 				expr.ccodenode = get_variable_cexpression (local.name);
 			}
@@ -369,7 +369,7 @@ internal class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 				if (p.captured) {
 					// captured variables are stored on the heap
 					var block = ((Method) p.parent_symbol).body;
-					expr.ccodenode = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (get_block_id (block))), get_variable_cname (p.name));
+					expr.ccodenode = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (block))), get_variable_cname (p.name));
 				} else if (current_method != null && current_method.coroutine) {
 					// use closure
 					expr.ccodenode = get_variable_cexpression (p.name);
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 2eb974b..d60c8d9 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -418,7 +418,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 						}
 						int parent_block_id = get_block_id (parent_closure_block);
 
-						var parent_data = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id));
+						var parent_data = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "_data%d_".printf (parent_block_id));
 						var cdecl = new CCodeDeclaration ("Block%dData*".printf (parent_block_id));
 						cdecl.add_declarator (new CCodeVariableDeclarator ("_data%d_".printf (parent_block_id), parent_data));
 
@@ -431,7 +431,7 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 					// add self variable for closures
 					// as closures have block data parameter
 					if (m.binding == MemberBinding.INSTANCE) {
-						var cself = new CCodeMemberAccess.pointer (new CCodeIdentifier ("_data%d_".printf (block_id)), "self");
+						var cself = new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (block_id)), "self");
 						var cdecl = new CCodeDeclaration ("%s *".printf (current_class.get_cname ()));
 						cdecl.add_declarator (new CCodeVariableDeclarator ("self", cself));
 



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