vala r1502 - in trunk: . gobject tests



Author: juergbi
Date: Fri May 30 22:48:14 2008
New Revision: 1502
URL: http://svn.gnome.org/viewvc/vala?rev=1502&view=rev

Log:
2008-05-31  Juerg Billeter  <j bitron ch>

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

	Add basic support for nullable structs

	* tests/structs.vala:

	Test nullable struct parameters


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodegenerator.vala
   trunk/gobject/valaccodeinvocationexpressionbinding.vala
   trunk/gobject/valaccodememberaccessbinding.vala
   trunk/tests/structs.vala

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Fri May 30 22:48:14 2008
@@ -3385,8 +3385,13 @@
 			cexpr = csink;
 		}
 
+		bool boxing = (expression_type is ValueType && !expression_type.nullable
+		               && target_type is ValueType && target_type.nullable);
+		bool unboxing = (expression_type is ValueType && expression_type.nullable
+		                 && target_type is ValueType && !target_type.nullable);
+
 		if (expression_type.value_owned
-		    && (target_type == null || !target_type.value_owned)) {
+		    && (target_type == null || !target_type.value_owned || boxing)) {
 			// value leaked, destroy it
 			var pointer_type = target_type as PointerType;
 			if (pointer_type != null && !(pointer_type.base_type is VoidType)) {
@@ -3418,7 +3423,49 @@
 			return cexpr;
 		}
 
-		cexpr = get_implicit_cast_expression (cexpr, expression_type, target_type, expr);
+		if (boxing) {
+			// value needs to be boxed
+
+			if (cexpr is CCodeIdentifier) {
+				cexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, cexpr);
+			} else {
+				var decl = get_temp_variable (expression_type, expression_type.value_owned, expression_type);
+				temp_vars.insert (0, decl);
+
+				var ccomma = new CCodeCommaExpression ();
+				ccomma.append_expression (new CCodeAssignment (new CCodeIdentifier (get_variable_cname (decl.name)), cexpr));
+				ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (get_variable_cname (decl.name))));
+				cexpr = ccomma;
+			}
+
+			if (!target_type.value_owned) {
+				// reference to stack value
+			} else {
+				// heap-allocated
+				string dup_func = expression_type.data_type.get_dup_function ();
+				if (dup_func == null) {
+					// default to g_memdup
+					var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_memdup"));
+					ccall.add_argument (cexpr);
+					var csizeof = new CCodeFunctionCall (new CCodeIdentifier ("sizeof"));
+					csizeof.add_argument (new CCodeIdentifier (expression_type.data_type.get_cname ()));
+					ccall.add_argument (csizeof);
+
+					cexpr = ccall;
+				} else {
+					var ccall = new CCodeFunctionCall (new CCodeIdentifier (dup_func));
+					ccall.add_argument (cexpr);
+
+					cexpr = ccall;
+				}
+			}
+		} else if (unboxing) {
+			// unbox value
+
+			cexpr = new CCodeParenthesizedExpression (new CCodeUnaryExpression (CCodeUnaryOperator.POINTER_INDIRECTION, cexpr));
+		} else {
+			cexpr = get_implicit_cast_expression (cexpr, expression_type, target_type, expr);
+		}
 
 		if (expression_type.is_type_argument) {
 			cexpr = convert_from_generic_pointer (cexpr, target_type);

Modified: trunk/gobject/valaccodeinvocationexpressionbinding.vala
==============================================================================
--- trunk/gobject/valaccodeinvocationexpressionbinding.vala	(original)
+++ trunk/gobject/valaccodeinvocationexpressionbinding.vala	Fri May 30 22:48:14 2008
@@ -176,8 +176,8 @@
 
 					// 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 and out parameters
-						if (param.direction == ParameterDirection.IN) {
+						// we already use a reference for arguments of ref, out, and nullable parameters
+						if (param.direction == ParameterDirection.IN && !param.parameter_type.nullable) {
 							if (cexpr is CCodeIdentifier) {
 								cexpr = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, cexpr);
 							} else {

Modified: trunk/gobject/valaccodememberaccessbinding.vala
==============================================================================
--- trunk/gobject/valaccodememberaccessbinding.vala	(original)
+++ trunk/gobject/valaccodememberaccessbinding.vala	Fri May 30 22:48:14 2008
@@ -199,7 +199,7 @@
 			} else {
 				var type_as_struct = p.parameter_type.data_type as Struct;
 				if (p.direction != ParameterDirection.IN
-				    || (type_as_struct != null && !type_as_struct.is_simple_type ())) {
+				    || (type_as_struct != null && !type_as_struct.is_simple_type () && !p.parameter_type.nullable)) {
 					expr.ccodenode = new CCodeIdentifier ("(*%s)".printf (p.name));
 				} else {
 					// Property setters of non simple structs shall replace all occurences

Modified: trunk/tests/structs.vala
==============================================================================
--- trunk/tests/structs.vala	(original)
+++ trunk/tests/structs.vala	Fri May 30 22:48:14 2008
@@ -28,6 +28,10 @@
 	stdout.printf ("test_in_parameter: st.field = %d\n", st.field);
 }
 
+void test_in_nullable_parameter (SimpleStruct? st) {
+	assert (st.field == 1);
+}
+
 void test_ref_parameter (ref SimpleStruct st) {
 	stdout.printf ("test_ref_parameter: st.field = %d\n", st.field);
 	st.field++;
@@ -55,6 +59,7 @@
 	stdout.printf ("simple_struct.field = %d\n", simple_struct.field);
 
 	test_in_parameter (simple_struct);
+	test_in_nullable_parameter (simple_struct);
 	test_ref_parameter (ref simple_struct);
 	stdout.printf ("after test_ref_parameter: st.field = %d\n", simple_struct.field);
 	test_out_parameter (out simple_struct);



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