vala r2223 - in trunk: . gobject



Author: juergbi
Date: Fri Dec 19 14:49:31 2008
New Revision: 2223
URL: http://svn.gnome.org/viewvc/vala?rev=2223&view=rev

Log:
2008-12-19  JÃrg Billeter  <j bitron ch>

	* gobject/valaccodebasemodule.vala:
	* gobject/valagasyncmodule.vala:

	Use closure struct to declare and initialize local variables in
	coroutines


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodebasemodule.vala
   trunk/gobject/valagasyncmodule.vala

Modified: trunk/gobject/valaccodebasemodule.vala
==============================================================================
--- trunk/gobject/valaccodebasemodule.vala	(original)
+++ trunk/gobject/valaccodebasemodule.vala	Fri Dec 19 14:49:31 2008
@@ -64,6 +64,7 @@
 	public CCodeStruct type_struct;
 	public CCodeStruct instance_priv_struct;
 	public CCodeStruct type_priv_struct;
+	public CCodeStruct closure_struct;
 	public CCodeEnum prop_enum;
 	public CCodeEnum cenum;
 	public CCodeFunction function;
@@ -1475,21 +1476,31 @@
 				rhs = ccomma;
 			}
 		}
-			
-		var cvar = new CCodeVariableDeclarator.with_initializer (get_variable_cname (local.name), rhs);
 
 		var cfrag = new CCodeFragment ();
-		var cdecl = new CCodeDeclaration (local.variable_type.get_cname ());
-		cdecl.add_declarator (cvar);
-		cfrag.append (cdecl);
 
-		if (local.initializer != null && local.initializer.tree_can_fail) {
-			head.add_simple_check (local.initializer, cfrag);
+		if (current_method != null && current_method.coroutine) {
+			closure_struct.add_field (local.variable_type.get_cname (), get_variable_cname (local.name));
+
+			if (local.initializer != null) {
+				cfrag.append (new CCodeExpressionStatement (new CCodeAssignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("data"), get_variable_cname (local.name)), rhs)));
+			}
+		} else {
+			var cvar = new CCodeVariableDeclarator.with_initializer (get_variable_cname (local.name), rhs);
+
+			var cdecl = new CCodeDeclaration (local.variable_type.get_cname ());
+			cdecl.add_declarator (cvar);
+			cfrag.append (cdecl);
+
+			// try to initialize uninitialized variables
+			// initialization not necessary for variables stored in closure
+			if (cvar.initializer == null) {
+				cvar.initializer = default_value_for_type (local.variable_type, true);
+			}
 		}
 
-		/* try to initialize uninitialized variables */
-		if (cvar.initializer == null) {
-			cvar.initializer = default_value_for_type (local.variable_type, true);
+		if (local.initializer != null && local.initializer.tree_can_fail) {
+			head.add_simple_check (local.initializer, cfrag);
 		}
 
 		local.ccodenode = cfrag;

Modified: trunk/gobject/valagasyncmodule.vala
==============================================================================
--- trunk/gobject/valagasyncmodule.vala	(original)
+++ trunk/gobject/valagasyncmodule.vala	Fri Dec 19 14:49:31 2008
@@ -29,9 +29,8 @@
 	}
 
 	public override void visit_method (Method m) {
-		base.visit_method (m);
-
 		if (!m.coroutine) {
+			base.visit_method (m);
 			return;
 		}
 
@@ -42,22 +41,24 @@
 
 		// generate struct to hold parameters, local variables, and the return value
 		string dataname = Symbol.lower_case_to_camel_case (m.get_cname ()) + "Data";
-		var datastruct = new CCodeStruct ("_" + dataname);
+		closure_struct = new CCodeStruct ("_" + dataname);
 
-		datastruct.add_field ("int", "state");
-		datastruct.add_field ("GAsyncReadyCallback", "callback");
-		datastruct.add_field ("gpointer", "user_data");
-		datastruct.add_field ("GAsyncResult*", "res");
+		closure_struct.add_field ("int", "state");
+		closure_struct.add_field ("GAsyncReadyCallback", "callback");
+		closure_struct.add_field ("gpointer", "user_data");
+		closure_struct.add_field ("GAsyncResult*", "res");
 
 		foreach (FormalParameter param in m.get_parameters ()) {
-			datastruct.add_field (param.parameter_type.get_cname (), param.name);
+			closure_struct.add_field (param.parameter_type.get_cname (), param.name);
 		}
 
 		if (!(m.return_type is VoidType)) {
-			datastruct.add_field (m.return_type.get_cname (), "result");
+			closure_struct.add_field (m.return_type.get_cname (), "result");
 		}
 
-		source_type_definition.append (datastruct);
+		base.visit_method (m);
+
+		source_type_definition.append (closure_struct);
 		source_type_declaration.append (new CCodeTypeDefinition ("struct _" + dataname, new CCodeVariableDeclarator (dataname)));
 
 		// generate async function



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