[vala] Use g_type_class_add_private() when targeting GLib >= 2.24



commit 460ec9fd2ff63166e7bdb0ab46fe6e028170de97
Author: Sebastian Dröge <sebastian droege collabora co uk>
Date:   Mon Mar 22 09:05:43 2010 +0100

    Use g_type_class_add_private() when targeting GLib >= 2.24
    
    Fixes bug 592942.

 codegen/valaccodebasemodule.vala         |   23 +++++++++++++++---
 codegen/valaccodememberaccessmodule.vala |    4 +--
 codegen/valaclassregisterfunction.vala   |    4 +-
 codegen/valagtypemodule.vala             |   37 +++++++++++++++++-------------
 codegen/valatyperegisterfunction.vala    |   11 ++++++++-
 5 files changed, 53 insertions(+), 26 deletions(-)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 2633f8d..5c7153b 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -821,8 +821,10 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 				finalize_fragment = instance_finalize_fragment;
 			} else if (m.is_class_member ()) {
 				TypeSymbol parent = (TypeSymbol)m.parent_symbol;
-				l = new CCodeIdentifier ("%s_GET_CLASS_PRIVATE(%s)".printf(parent.get_upper_case_cname (), parent.get_type_id ()));
-				l = new CCodeMemberAccess.pointer (l, get_symbol_lock_name (m.name));
+
+				var get_class_private_call = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(parent.get_upper_case_cname ())));
+				get_class_private_call.add_argument (new CCodeIdentifier ("klass"));
+				l = new CCodeMemberAccess.pointer (get_class_private_call, get_symbol_lock_name (m.name));
 			} else {
 				l = new CCodeIdentifier (get_symbol_lock_name ("%s_%s".printf(m.parent_symbol.get_lower_case_cname (), m.name)));
 			}
@@ -3390,8 +3392,21 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 
 			l = new CCodeMemberAccess.pointer (new CCodeMemberAccess.pointer (l, "priv"), get_symbol_lock_name (stmt.resource.symbol_reference.name));
 		} else if (member.is_class_member ()) {
-			l = new CCodeIdentifier ("%s_GET_CLASS_PRIVATE(%s)".printf(parent.get_upper_case_cname (), parent.get_type_id ()));
-			l = new CCodeMemberAccess.pointer (l, get_symbol_lock_name (stmt.resource.symbol_reference.name));
+			CCodeExpression klass;
+
+		        if (current_method != null && current_method.binding == MemberBinding.INSTANCE ||
+			    current_property_accessor != null && current_property_accessor.prop.binding == MemberBinding.INSTANCE ||
+			    (in_constructor && !in_static_or_class_context)) {
+				var k = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_GET_CLASS"));
+				k.add_argument (new CCodeIdentifier ("self"));
+				klass = k;
+			} else {
+				klass = new CCodeIdentifier ("klass");
+			}
+
+			var get_class_private_call = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf(parent.get_upper_case_cname ())));
+			get_class_private_call.add_argument (klass);
+			l = new CCodeMemberAccess.pointer (get_class_private_call, get_symbol_lock_name (stmt.resource.symbol_reference.name));
 		} else {
 			string lock_name = "%s_%s".printf(parent.get_lower_case_cname (), stmt.resource.symbol_reference.name);
 			l = new CCodeIdentifier (get_symbol_lock_name (lock_name));
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index f55da36..b3994bc 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -161,9 +161,7 @@ internal class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 
 				if (f.access == SymbolAccessibility.PRIVATE) {
 					var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl.get_upper_case_cname ())));
-					var ccall2 = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_FROM_CLASS"));
-					ccall2.add_argument (cast);
-					ccall.add_argument (ccall2);
+					ccall.add_argument (klass);
 					expr.ccodenode = new CCodeMemberAccess.pointer (ccall, f.get_cname ());
 				} else {
 					expr.ccodenode = new CCodeMemberAccess.pointer (cast, f.get_cname ());
diff --git a/codegen/valaclassregisterfunction.vala b/codegen/valaclassregisterfunction.vala
index f73e960..64f0725 100644
--- a/codegen/valaclassregisterfunction.vala
+++ b/codegen/valaclassregisterfunction.vala
@@ -51,7 +51,7 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
 	}
 
 	public override string get_base_init_func_name () {
-		if (class_reference.class_constructor != null || class_reference.has_class_private_fields) {
+		if (class_reference.class_constructor != null || (!context.require_glib_version (2, 24) && class_reference.has_class_private_fields)) {
 			return "%s_base_init".printf (class_reference.get_lower_case_cname (null));
 		} else {
 			return "NULL";
@@ -67,7 +67,7 @@ public class Vala.ClassRegisterFunction : TypeRegisterFunction {
 	}
 
 	public override string get_base_finalize_func_name () {
-		if (class_reference.class_destructor != null || class_reference.has_class_private_fields) {
+		if (class_reference.class_destructor != null || (!context.require_glib_version (2, 24) && class_reference.has_class_private_fields)) {
 			return "%s_base_finalize".printf (class_reference.get_lower_case_cname (null));
 		} else {
 			return "NULL";
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index 152fcae..401e82c 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -448,10 +448,12 @@ internal class Vala.GTypeModule : GErrorModule {
 		if (is_gtypeinstance) {
 			if (cl.has_class_private_fields || has_class_locks) {
 				decl_space.add_type_declaration (new CCodeTypeDefinition ("struct %s".printf (type_priv_struct.name), new CCodeVariableDeclarator ("%sClassPrivate".printf (cl.get_cname ()))));
-				var cdecl = new CCodeDeclaration ("GQuark");
-				cdecl.add_declarator (new CCodeVariableDeclarator ("_vala_%s_class_private_quark".printf (cl.get_lower_case_cname ()), new CCodeConstant ("0")));
-				cdecl.modifiers = CCodeModifiers.STATIC;
-				decl_space.add_type_declaration (cdecl);
+				if (!context.require_glib_version (2, 24)) {
+					var cdecl = new CCodeDeclaration ("GQuark");
+					cdecl.add_declarator (new CCodeVariableDeclarator ("_vala_%s_class_private_quark".printf (cl.get_lower_case_cname ()), new CCodeConstant ("0")));
+					cdecl.modifiers = CCodeModifiers.STATIC;
+					decl_space.add_type_declaration (cdecl);
+				}
 			}
 
 			/* only add the *Private struct if it is not empty, i.e. we actually have private data */
@@ -464,8 +466,13 @@ internal class Vala.GTypeModule : GErrorModule {
 			if (cl.has_class_private_fields || has_class_locks) {
 				decl_space.add_type_member_declaration (type_priv_struct);
 
-				var macro = "((%sClassPrivate *) g_type_get_qdata (type, _vala_%s_class_private_quark))".printf (cl.get_cname(), cl.get_lower_case_cname ());
-				decl_space.add_type_member_declaration (new CCodeMacroReplacement ("%s_GET_CLASS_PRIVATE(type)".printf (cl.get_upper_case_cname (null)), macro));
+				string macro;
+				if (context.require_glib_version (2, 24)) {
+					macro = "(G_TYPE_CLASS_GET_PRIVATE (klass, %s, %sClassPrivate))".printf (cl.get_type_id (), cl.get_cname ());
+				} else {
+					macro = "((%sClassPrivate *) g_type_get_qdata (G_TYPE_FROM_CLASS (klass), _vala_%s_class_private_quark))".printf (cl.get_cname(), cl.get_lower_case_cname ());
+				}
+				decl_space.add_type_member_declaration (new CCodeMacroReplacement ("%s_GET_CLASS_PRIVATE(klass)".printf (cl.get_upper_case_cname (null)), macro));
 			}
 			decl_space.add_type_member_declaration (prop_enum);
 		} else {
@@ -556,12 +563,12 @@ internal class Vala.GTypeModule : GErrorModule {
 			}
 
 
-			if (cl.class_constructor != null || cl.has_class_private_fields) {
+			if (cl.class_constructor != null || (!context.require_glib_version (2, 24) && cl.has_class_private_fields)) {
 				add_base_init_function (cl);
 			}
 			add_class_init_function (cl);
 
-			if (cl.class_destructor != null || cl.has_class_private_fields) {
+			if (cl.class_destructor != null || (!context.require_glib_version (2, 24) && cl.has_class_private_fields)) {
 				add_base_finalize_function (cl);
 			}
 
@@ -1111,7 +1118,7 @@ internal class Vala.GTypeModule : GErrorModule {
 		var init_block = new CCodeBlock ();
 		base_init.block = init_block;
 
-		if (cl.has_class_private_fields) {
+		if (!context.require_glib_version (2, 24) && cl.has_class_private_fields) {
 			var block = new CCodeBlock ();
 			var cdecl = new CCodeDeclaration ("%sClassPrivate *".printf (cl.get_cname ()));
 			cdecl.add_declarator (new CCodeVariableDeclarator ("priv"));
@@ -1131,7 +1138,9 @@ internal class Vala.GTypeModule : GErrorModule {
 
 			var iftrue = new CCodeBlock ();
 			ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl.get_upper_case_cname (null))));
-			ccall.add_argument (new CCodeIdentifier ("parent_type"));
+			ccall2 = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_peek"));
+			ccall2.add_argument (new CCodeIdentifier ("parent_type"));
+			ccall.add_argument (ccall2);
 			iftrue.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("parent_priv"), ccall)));
 			block.add_statement (new CCodeIfStatement (new CCodeIdentifier ("parent_type"), iftrue));
 
@@ -1167,9 +1176,7 @@ internal class Vala.GTypeModule : GErrorModule {
 			block.add_statement (cdecl);
 
 			ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl.get_upper_case_cname (null))));
-			ccall2 = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_FROM_CLASS"));
-			ccall2.add_argument (new CCodeIdentifier ("klass"));
-			ccall.add_argument (ccall2);
+			ccall.add_argument (new CCodeConstant ("klass"));
 			block.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("priv"), ccall)));
 
 			ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_slice_free"));
@@ -1292,9 +1299,7 @@ internal class Vala.GTypeModule : GErrorModule {
 
 			if (field.access == SymbolAccessibility.PRIVATE) {
 				ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_CLASS_PRIVATE".printf (cl.get_upper_case_cname ())));
-				var ccall2 = new CCodeFunctionCall (new CCodeIdentifier ("G_TYPE_FROM_CLASS"));
-				ccall2.add_argument (new CCodeIdentifier ("klass"));
-				ccall.add_argument (ccall2);
+				ccall.add_argument (new CCodeIdentifier ("klass"));
 				left = new CCodeMemberAccess (ccall, field.get_cname (), true);
 			} else {
 				left = new CCodeMemberAccess (new CCodeIdentifier ("klass"), field.get_cname (), true);
diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala
index b56f159..697e702 100644
--- a/codegen/valatyperegisterfunction.vala
+++ b/codegen/valatyperegisterfunction.vala
@@ -124,7 +124,7 @@ public abstract class Vala.TypeRegisterFunction {
 
 		type_init.add_statement (get_type_interface_init_declaration ());
 
-		if (cl != null && cl.has_class_private_fields) {
+		if (cl != null && cl.has_class_private_fields && !context.require_glib_version (2, 24)) {
 			CCodeFunctionCall quark_reg_call;
 
 			if (plugin) {
@@ -213,6 +213,15 @@ public abstract class Vala.TypeRegisterFunction {
 			type_init.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier (type_id_name), reg_call)));
 		}
 
+		 if (cl != null && cl.has_class_private_fields && context.require_glib_version (2, 24)) {
+			CCodeFunctionCall add_class_private_call;
+
+			add_class_private_call = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_class_private"));
+			add_class_private_call.add_argument (new CCodeIdentifier (type_id_name));
+			add_class_private_call.add_argument (new CCodeIdentifier ("sizeof (%sClassPrivate)".printf (get_type_declaration ().get_cname ())));
+			type_init.add_statement (new CCodeExpressionStatement (add_class_private_call));
+		}
+
 		type_init.add_statement (get_type_interface_init_statements (plugin));
 
 		if (!plugin) {



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