diff options
author | Simon Werbeck <simon.werbeck@gmail.com> | 2019-09-30 16:37:33 +0200 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2019-10-09 08:10:03 +0200 |
commit | f413cb32470d9c2598416772bc0166c6f974ec9f (patch) | |
tree | 5e9ec232503accf07c2e5642a3d5b5a691fe2ea1 /codegen | |
parent | 91fc9702594fe7a59b06664aa077cee51960f72c (diff) | |
download | vala-f413cb32470d9c2598416772bc0166c6f974ec9f.tar.gz |
codegen: Fix custom reference-counting for compact classes
This fix defers emitting G_DEFINE_AUTOPTR_CLEANUP_FUNC when a compact
class defines custom unref function and set by ccode "unref_function".
Diffstat (limited to 'codegen')
-rw-r--r-- | codegen/valagtypemodule.vala | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index 4eb7878a1..bae49b2c5 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -195,17 +195,21 @@ public class Vala.GTypeModule : GErrorModule { while (base_class.base_class != null) { base_class = base_class.base_class; } - string autoptr_cleanup_func; - if (is_reference_counting (base_class)) { - autoptr_cleanup_func = get_ccode_unref_function (base_class); - } else { - autoptr_cleanup_func = get_ccode_free_function (base_class); - } - if (autoptr_cleanup_func == null || autoptr_cleanup_func == "") { - Report.error (cl.source_reference, "internal error: autoptr_cleanup_func not available"); + // Custom unref-methods need to be emitted before G_DEFINE_AUTOPTR_CLEANUP_FUNC, + // so we guard against that special case and handle it in generate_method_declaration. + if (!(base_class.is_compact && is_reference_counting (base_class))) { + string autoptr_cleanup_func; + if (is_reference_counting (base_class)) { + autoptr_cleanup_func = get_ccode_unref_function (base_class); + } else { + autoptr_cleanup_func = get_ccode_free_function (base_class); + } + if (autoptr_cleanup_func == null || autoptr_cleanup_func == "") { + Report.error (cl.source_reference, "internal error: autoptr_cleanup_func not available"); + } + decl_space.add_type_member_declaration (new CCodeIdentifier ("G_DEFINE_AUTOPTR_CLEANUP_FUNC (%s, %s)".printf (get_ccode_name (cl), autoptr_cleanup_func))); + decl_space.add_type_member_declaration (new CCodeNewline ()); } - decl_space.add_type_member_declaration (new CCodeIdentifier ("G_DEFINE_AUTOPTR_CLEANUP_FUNC (%s, %s)".printf (get_ccode_name (cl), autoptr_cleanup_func))); - decl_space.add_type_member_declaration (new CCodeNewline ()); } public override void generate_class_struct_declaration (Class cl, CCodeFile decl_space) { @@ -441,6 +445,22 @@ public class Vala.GTypeModule : GErrorModule { } } + public override bool generate_method_declaration (Method m, CCodeFile decl_space) { + if (base.generate_method_declaration (m, decl_space)) { + // Custom unref-methods need to be emitted before G_DEFINE_AUTOPTR_CLEANUP_FUNC, + // in addition to the non-ref-countable case in generate_class_declaration. + unowned Class? cl = m.parent_symbol as Class; + if (cl != null && cl.is_compact && get_ccode_unref_function (cl) == get_ccode_name (m)) { + decl_space.add_type_member_declaration (new CCodeIdentifier ("G_DEFINE_AUTOPTR_CLEANUP_FUNC (%s, %s)".printf (get_ccode_name (cl), get_ccode_name (m)))); + decl_space.add_type_member_declaration (new CCodeNewline ()); + } + + return true; + } + + return false; + } + public virtual void generate_virtual_method_declaration (Method m, CCodeFile decl_space, CCodeStruct type_struct) { if (!m.is_abstract && !m.is_virtual) { return; |