[vala] Improve consistency in constructor chain up handling



commit 8aa6299ea7a39e7a652ce3896324aaa47af99a18
Author: Jürg Billeter <j bitron ch>
Date:   Sat Aug 21 19:12:16 2010 +0200

    Improve consistency in constructor chain up handling

 codegen/valaccodebasemodule.vala   |    4 --
 codegen/valaccodemethodmodule.vala |   87 ++----------------------------------
 codegen/valagobjectmodule.vala     |   34 --------------
 vala/valacreationmethod.vala       |   12 +++++-
 4 files changed, 15 insertions(+), 122 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 66b3761..fcfe5c9 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -5715,10 +5715,6 @@ public class Vala.CCodeBaseModule : CodeGenerator {
 		return false;
 	}
 
-	public virtual CCodeExpression get_construct_property_assignment (CCodeConstant canonical_cconstant, DataType property_type, CCodeExpression value) {
-		return new CCodeConstant ("");
-	}
-
 	public virtual CCodeFunctionCall get_param_spec (Property prop) {
 		return new CCodeFunctionCall (new CCodeIdentifier (""));
 	}
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 3aff095..cef50c6 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -492,47 +492,10 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
 
 				if (m is CreationMethod) {
 					if (in_gobject_creation_method) {
-						if (!((CreationMethod) m).chain_up) {
-							if (current_class.get_type_parameters ().size > 0) {
-								// declare construction parameter array
-								var cparamsinit = new CCodeFunctionCall (new CCodeIdentifier ("g_new0"));
-								cparamsinit.add_argument (new CCodeIdentifier ("GParameter"));
-								cparamsinit.add_argument (new CCodeConstant ((3 * current_class.get_type_parameters ().size).to_string ()));
-						
-								var cdecl = new CCodeDeclaration ("GParameter *");
-								cdecl.add_declarator (new CCodeVariableDeclarator ("__params", cparamsinit));
-								cinit.append (cdecl);
-						
-								cdecl = new CCodeDeclaration ("GParameter *");
-								cdecl.add_declarator (new CCodeVariableDeclarator ("__params_it", new CCodeIdentifier ("__params")));
-								cinit.append (cdecl);
-							}
-
-							/* type, dup func, and destroy func properties for generic types */
-							foreach (TypeParameter type_param in current_class.get_type_parameters ()) {
-								CCodeConstant prop_name;
-								CCodeIdentifier param_name;
-
-								prop_name = new CCodeConstant ("\"%s-type\"".printf (type_param.name.down ()));
-								param_name = new CCodeIdentifier ("%s_type".printf (type_param.name.down ()));
-								cinit.append (new CCodeExpressionStatement (get_construct_property_assignment (prop_name, new IntegerType ((Struct) gtype_type), param_name)));
+						var cdeclaration = new CCodeDeclaration ("%s *".printf (((Class) current_type_symbol).get_cname ()));
+						cdeclaration.add_declarator (new CCodeVariableDeclarator ("self"));
 
-								prop_name = new CCodeConstant ("\"%s-dup-func\"".printf (type_param.name.down ()));
-								param_name = new CCodeIdentifier ("%s_dup_func".printf (type_param.name.down ()));
-								cinit.append (new CCodeExpressionStatement (get_construct_property_assignment (prop_name, new PointerType (new VoidType ()), param_name)));
-
-								prop_name = new CCodeConstant ("\"%s-destroy-func\"".printf (type_param.name.down ()));
-								param_name = new CCodeIdentifier ("%s_destroy_func".printf (type_param.name.down ()));
-								cinit.append (new CCodeExpressionStatement (get_construct_property_assignment (prop_name, new PointerType (new VoidType ()), param_name)));
-							}
-
-							add_object_creation (cinit, current_class.get_type_parameters ().size > 0);
-						} else {
-							var cdeclaration = new CCodeDeclaration ("%s *".printf (((Class) current_type_symbol).get_cname ()));
-							cdeclaration.add_declarator (new CCodeVariableDeclarator ("self"));
-
-							cinit.append (cdeclaration);
-						}
+						cinit.append (cdeclaration);
 					} else if (is_gtypeinstance_creation_method (m)) {
 						var cl = (Class) m.parent_symbol;
 						var cdeclaration = new CCodeDeclaration (cl.get_cname () + "*");
@@ -540,8 +503,7 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
 						cdeclaration.add_declarator (cdecl);
 						cinit.append (cdeclaration);
 
-						if (!((CreationMethod) m).chain_up) {
-							// TODO implicitly chain up to base class as in add_object_creation
+						if (cl.is_fundamental ()) {
 							var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_create_instance"));
 							ccall.add_argument (new CCodeIdentifier ("object_type"));
 							cdecl.initializer = new CCodeCastExpression (ccall, cl.get_cname () + "*");
@@ -1037,31 +999,6 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
 		return null;
 	}
 
-	private void add_object_creation (CCodeFragment ccode, bool has_params) {
-		var cl = (Class) current_type_symbol;
-
-		if (!has_params && cl.base_class != gobject_type) {
-			// possibly report warning or error about missing base call
-		}
-
-		var cdecl = new CCodeVariableDeclarator ("self");
-		var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_object_newv"));
-		ccall.add_argument (new CCodeIdentifier ("object_type"));
-		if (has_params) {
-			ccall.add_argument (new CCodeConstant ("__params_it - __params"));
-			ccall.add_argument (new CCodeConstant ("__params"));
-		} else {
-			ccall.add_argument (new CCodeConstant ("0"));
-			ccall.add_argument (new CCodeConstant ("NULL"));
-		}
-		cdecl.initializer = ccall;
-		
-		var cdeclaration = new CCodeDeclaration ("%s *".printf (cl.get_cname ()));
-		cdeclaration.add_declarator (cdecl);
-		
-		ccode.append (cdeclaration);
-	}
-
 	public override void visit_creation_method (CreationMethod m) {
 		bool visible = !m.is_private_symbol ();
 
@@ -1102,22 +1039,6 @@ public class Vala.CCodeMethodModule : CCodeStructModule {
 			cfile.add_function (vfunc);
 		}
 
-		if (current_type_symbol is Class && gobject_type != null && current_class.is_subtype_of (gobject_type)
-		    && current_class.get_type_parameters ().size > 0
-		    && !((CreationMethod) m).chain_up) {
-			var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN, new CCodeIdentifier ("__params_it"), new CCodeIdentifier ("__params"));
-			var cdofreeparam = new CCodeBlock ();
-			cdofreeparam.add_statement (new CCodeExpressionStatement (new CCodeUnaryExpression (CCodeUnaryOperator.PREFIX_DECREMENT, new CCodeIdentifier ("__params_it"))));
-			var cunsetcall = new CCodeFunctionCall (new CCodeIdentifier ("g_value_unset"));
-			cunsetcall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeMemberAccess.pointer (new CCodeIdentifier ("__params_it"), "value")));
-			cdofreeparam.add_statement (new CCodeExpressionStatement (cunsetcall));
-			function.block.add_statement (new CCodeWhileStatement (ccond, cdofreeparam));
-
-			var cfreeparams = new CCodeFunctionCall (new CCodeIdentifier ("g_free"));
-			cfreeparams.add_argument (new CCodeIdentifier ("__params"));
-			function.block.add_statement (new CCodeExpressionStatement (cfreeparams));
-		}
-
 		if (current_type_symbol is Class) {
 			CCodeExpression cresult = new CCodeIdentifier ("self");
 			if (get_custom_creturn_type (m) != null) {
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index 4fe80d3..c2be061 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -411,40 +411,6 @@ public class Vala.GObjectModule : GTypeModule {
 		return new CCodeExpressionStatement (cwarn);
 	}
 
-	public override CCodeExpression get_construct_property_assignment (CCodeConstant canonical_cconstant, DataType property_type, CCodeExpression value) {
-		// this property is used as a construction parameter
-		var cpointer = new CCodeIdentifier ("__params_it");
-		
-		var ccomma = new CCodeCommaExpression ();
-		// set name in array for current parameter
-		var cnamemember = new CCodeMemberAccess.pointer (cpointer, "name");
-		var cnameassign = new CCodeAssignment (cnamemember, canonical_cconstant);
-		ccomma.append_expression (cnameassign);
-		
-		var gvaluearg = new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeMemberAccess.pointer (cpointer, "value"));
-		
-		// initialize GValue in array for current parameter
-		var cvalueinit = new CCodeFunctionCall (new CCodeIdentifier ("g_value_init"));
-		cvalueinit.add_argument (gvaluearg);
-		cvalueinit.add_argument (new CCodeIdentifier (property_type.get_type_id ()));
-		ccomma.append_expression (cvalueinit);
-		
-		// set GValue for current parameter
-		var cvalueset = new CCodeFunctionCall (get_value_setter_function (property_type));
-		cvalueset.add_argument (gvaluearg);
-		if (property_type.is_real_struct_type ()) {
-			cvalueset.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, value));
-		} else {
-			cvalueset.add_argument (value);
-		}
-		ccomma.append_expression (cvalueset);
-		
-		// move pointer to next parameter in array
-		ccomma.append_expression (new CCodeUnaryExpression (CCodeUnaryOperator.POSTFIX_INCREMENT, cpointer));
-
-		return ccomma;
-	}
-
 	public override void visit_constructor (Constructor c) {
 		bool old_method_inner_error = current_method_inner_error;
 		current_method_inner_error = false;
diff --git a/vala/valacreationmethod.vala b/vala/valacreationmethod.vala
index 1243be2..01d09fc 100644
--- a/vala/valacreationmethod.vala
+++ b/vala/valacreationmethod.vala
@@ -193,7 +193,17 @@ public class Vala.CreationMethod : Method {
 				if (analyzer.context.profile == Profile.GOBJECT
 				    && cl.base_class.default_construction_method != null
 				    && !cl.base_class.default_construction_method.has_construct_function) {
-					// chain up impossible
+					// directly chain up to Object
+					var old_insert_block = analyzer.insert_block;
+					analyzer.current_symbol = body;
+					analyzer.insert_block = body;
+
+					var stmt = new ExpressionStatement (new MethodCall (new MemberAccess (new MemberAccess.simple ("GLib", source_reference), "Object", source_reference), source_reference), source_reference);
+					body.insert_statement (0, stmt);
+					stmt.check (analyzer);
+
+					analyzer.current_symbol = this;
+					analyzer.insert_block = old_insert_block;
 				} else if (cl.base_class.default_construction_method == null
 				    || cl.base_class.default_construction_method.access == SymbolAccessibility.PRIVATE) {
 					Report.error (source_reference, "unable to chain up to private base constructor");



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