From df56f916e52e5efe3876afed59a6190f6cead1c8 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Mon, 18 Jun 2018 11:34:25 +0200 Subject: [PATCH] codegen: Avoid using deprecated "g_type_class_add_private" --- codegen/valagtypemodule.vala | 29 ++++++++++++++++++++++----- codegen/valatyperegisterfunction.vala | 7 +++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index 938bd1766..6b60f7ef3 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -567,8 +567,27 @@ public class Vala.GTypeModule : GErrorModule { /* only add the *Private struct if it is not empty, i.e. we actually have private data */ if (cl.has_private_fields || cl.get_type_parameters ().size > 0) { decl_space.add_type_definition (instance_priv_struct); - var macro = "(G_TYPE_INSTANCE_GET_PRIVATE ((o), %s, %sPrivate))".printf (get_ccode_type_id (cl), get_ccode_name (cl)); - decl_space.add_type_member_declaration (new CCodeMacroReplacement ("%s_GET_PRIVATE(o)".printf (get_ccode_upper_case_name (cl, null)), macro)); + + var parent_decl = new CCodeDeclaration ("gint"); + var parent_var_decl = new CCodeVariableDeclarator ("%s_private_offset".printf (get_ccode_name (cl))); + parent_decl.add_declarator (parent_var_decl); + parent_decl.modifiers = CCodeModifiers.STATIC; + cfile.add_type_member_declaration (parent_decl); + + var function = new CCodeFunction ("%s_get_instance_private".printf (get_ccode_lower_case_name (cl, null)), "gpointer"); + function.modifiers = CCodeModifiers.STATIC | CCodeModifiers.INLINE; + function.add_parameter (new CCodeParameter ("self", "%s*".printf (get_ccode_name (cl)))); + + push_function (function); + + function.block = new CCodeBlock (); + var ccall = new CCodeFunctionCall (new CCodeIdentifier ("G_STRUCT_MEMBER_P")); + ccall.add_argument (new CCodeIdentifier ("self")); + ccall.add_argument (new CCodeIdentifier ("%s_private_offset".printf (get_ccode_name (cl)))); + function.block.add_statement (new CCodeReturnStatement (ccall)); + + pop_function (); + cfile.add_function (function); } if (cl.has_class_private_fields || has_class_locks) { @@ -1262,9 +1281,9 @@ public class Vala.GTypeModule : GErrorModule { /* add struct for private fields */ if (cl.has_private_fields || cl.get_type_parameters ().size > 0) { - ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_add_private")); + ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_adjust_private_offset")); ccall.add_argument (new CCodeIdentifier ("klass")); - ccall.add_argument (new CCodeConstant ("sizeof (%sPrivate)".printf (get_ccode_name (cl)))); + ccall.add_argument (new CCodeIdentifier ("&%s_private_offset".printf (get_ccode_name (cl)))); ccode.add_expression (ccall); } @@ -1668,7 +1687,7 @@ public class Vala.GTypeModule : GErrorModule { } if (!cl.is_compact && (cl.has_private_fields || cl.get_type_parameters ().size > 0)) { - var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_GET_PRIVATE".printf (get_ccode_upper_case_name (cl, null)))); + var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_get_instance_private".printf (get_ccode_lower_case_name (cl, null)))); ccall.add_argument (new CCodeIdentifier ("self")); func.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), ccall); } diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala index 9b7c46a2f..c1811d949 100644 --- a/codegen/valatyperegisterfunction.vala +++ b/codegen/valatyperegisterfunction.vala @@ -213,6 +213,13 @@ public abstract class Vala.TypeRegisterFunction { type_init.add_statement (new CCodeExpressionStatement (add_class_private_call)); } + if (cl != null && (cl.has_private_fields || cl.get_type_parameters ().size > 0)) { + var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_instance_private")); + ccall.add_argument (new CCodeIdentifier (type_id_name)); + ccall.add_argument (new CCodeIdentifier ("sizeof (%sPrivate)".printf (get_ccode_name (cl)))); + type_init.add_statement (new CCodeExpressionStatement (new CCodeAssignment (new CCodeIdentifier ("%s_private_offset".printf (get_ccode_name (cl))), ccall))); + } + if (!declaration_only) { get_type_interface_init_statements (context, type_init, plugin); } -- 2.47.2