vala r1788 - in trunk: . gobject



Author: juergbi
Date: Fri Sep 26 21:13:36 2008
New Revision: 1788
URL: http://svn.gnome.org/viewvc/vala?rev=1788&view=rev

Log:
2008-09-26  JÃrg Billeter  <j bitron ch>

	* gobject/valaccodegenerator.vala:
	* gobject/valaccodeinvocationexpressionbinding.vala:

	Avoid code duplication for handling struct arguments


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodegenerator.vala
   trunk/gobject/valaccodeinvocationexpressionbinding.vala

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Fri Sep 26 21:13:36 2008
@@ -3178,26 +3178,7 @@
 					param = params_it.get ();
 					ellipsis = param.ellipsis;
 					if (!param.ellipsis) {
-						// pass non-simple struct instances always by reference
-						if (param.parameter_type.data_type is Struct && !((Struct) param.parameter_type.data_type).is_simple_type ()) {
-							// we already use a reference for arguments of ref and out parameters
-							if (param.direction == ParameterDirection.IN) {
-								if (cexpr is CCodeIdentifier) {
-									cexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, cexpr);
-								} else {
-									// if cexpr is e.g. a function call, we can't take the address of the expression
-									// (tmp = expr, &tmp)
-									var ccomma = new CCodeCommaExpression ();
-
-									var temp_var = get_temp_variable (arg.value_type);
-									temp_vars.insert (0, temp_var);
-									ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (temp_var.name), cexpr));
-									ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (temp_var.name)));
-
-									cexpr = ccomma;
-								}
-							}
-						}
+						cexpr = handle_struct_argument (param, arg, cexpr);
 					}
 				}
 			
@@ -3314,6 +3295,35 @@
 		}
 	}
 
+	public CCodeExpression? handle_struct_argument (FormalParameter param, Expression arg, CCodeExpression? cexpr) {
+		// pass non-simple struct instances always by reference
+		if (!(arg.value_type is NullType) && param.parameter_type.data_type is Struct && !((Struct) param.parameter_type.data_type).is_simple_type ()) {
+			// we already use a reference for arguments of ref, out, and nullable parameters
+			if (param.direction == ParameterDirection.IN && !param.parameter_type.nullable) {
+				var unary = cexpr as CCodeUnaryExpression;
+				if (unary != null && unary.operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
+					// *expr => expr
+					return unary.inner;
+				} else if (cexpr is CCodeIdentifier || cexpr is CCodeMemberAccess) {
+					return new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, cexpr);
+				} else {
+					// if cexpr is e.g. a function call, we can't take the address of the expression
+					// (tmp = expr, &tmp)
+					var ccomma = new CCodeCommaExpression ();
+
+					var temp_var = get_temp_variable (arg.value_type);
+					temp_vars.insert (0, temp_var);
+					ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (temp_var.name), cexpr));
+					ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (temp_var.name)));
+
+					return ccomma;
+				}
+			}
+		}
+
+		return cexpr;
+	}
+
 	public override void visit_sizeof_expression (SizeofExpression expr) {
 		var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
 		csizeof.add_argument (new CCodeIdentifier (expr.type_reference.data_type.get_cname ()));

Modified: trunk/gobject/valaccodeinvocationexpressionbinding.vala
==============================================================================
--- trunk/gobject/valaccodeinvocationexpressionbinding.vala	(original)
+++ trunk/gobject/valaccodeinvocationexpressionbinding.vala	Fri Sep 26 21:13:36 2008
@@ -208,30 +208,7 @@
 						multiple_cargs = true;
 					}
 
-					// pass non-simple struct instances always by reference
-					if (!(arg.value_type is NullType) && param.parameter_type.data_type is Struct && !((Struct) param.parameter_type.data_type).is_simple_type ()) {
-						// we already use a reference for arguments of ref, out, and nullable parameters
-						if (param.direction == ParameterDirection.IN && !param.parameter_type.nullable) {
-							var unary = cexpr as CCodeUnaryExpression;
-							if (unary != null && unary.operator == CCodeUnaryOperator.POINTER_INDIRECTION) {
-								// *expr => expr
-								cexpr = unary.inner;
-							} else if (cexpr is CCodeIdentifier || cexpr is CCodeMemberAccess) {
-								cexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, cexpr);
-							} else {
-								// if cexpr is e.g. a function call, we can't take the address of the expression
-								// (tmp = expr, &tmp)
-								var ccomma = new CCodeCommaExpression ();
-
-								var temp_var = codegen.get_temp_variable (arg.value_type);
-								codegen.temp_vars.insert (0, temp_var);
-								ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (temp_var.name), cexpr));
-								ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (temp_var.name)));
-
-								cexpr = ccomma;
-							}
-						}
-					}
+					cexpr = codegen.handle_struct_argument (param, arg, cexpr);
 
 					if (multiple_cargs && arg is InvocationExpression) {
 						// if vala argument is invocation expression



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