From 2684574fcbd9d866e53f6e03aeb785d74a8fe9ac Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Tue, 17 Mar 2020 15:26:36 +0100 Subject: [PATCH] codegen: Inherit "array_length_type" from (base-/base-interface) parameter/property/method See https://gitlab.gnome.org/GNOME/vala/issues/938 --- codegen/valaccode.vala | 7 +++-- codegen/valaccodeattribute.vala | 52 +++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/codegen/valaccode.vala b/codegen/valaccode.vala index c8dfb167c..e39fdbecc 100644 --- a/codegen/valaccode.vala +++ b/codegen/valaccode.vala @@ -302,11 +302,14 @@ namespace Vala { return get_ccode_attribute(node).array_length; } - public static string? get_ccode_array_length_type (CodeNode node) { + public static string get_ccode_array_length_type (CodeNode node) { if (node is ArrayType) { return get_ccode_name (((ArrayType) node).length_type); + } else if (node is DataType) { + Report.error (node.source_reference, "`CCode.array_length_type' not supported"); + return ""; } else { - assert (node is Method || node is Parameter || node is Delegate); + assert (node is Method || node is Parameter || node is Delegate || node is Property || node is Field); return get_ccode_attribute(node).array_length_type; } } diff --git a/codegen/valaccodeattribute.vala b/codegen/valaccodeattribute.vala index ce61b3667..f8491e015 100644 --- a/codegen/valaccodeattribute.vala +++ b/codegen/valaccodeattribute.vala @@ -602,6 +602,19 @@ public class Vala.CCodeAttribute : AttributeCache { } } + public string array_length_type { + get { + if (_array_length_type == null) { + if (ccode != null && ccode.has_argument ("array_length_type")) { + _array_length_type = ccode.get_string ("array_length_type"); + } else { + _array_length_type = get_default_array_length_type (); + } + } + return _array_length_type; + } + } + public string sentinel { get { if (_sentinel == null) { @@ -615,7 +628,6 @@ public class Vala.CCodeAttribute : AttributeCache { } } - public string? array_length_type { get; private set; } public string? array_length_name { get; private set; } public string? array_length_expr { get; private set; } @@ -663,6 +675,7 @@ public class Vala.CCodeAttribute : AttributeCache { private string _ctype; private bool ctype_set = false; private bool? _array_length; + private string _array_length_type; private bool? _array_null_terminated; private string _sentinel; @@ -674,7 +687,6 @@ public class Vala.CCodeAttribute : AttributeCache { ccode = node.get_attribute ("CCode"); if (ccode != null) { - array_length_type = ccode.get_string ("array_length_type"); array_length_name = ccode.get_string ("array_length_cname"); array_length_expr = ccode.get_string ("array_length_cexpr"); } @@ -1567,4 +1579,40 @@ public class Vala.CCodeAttribute : AttributeCache { } return false; } + + private string get_default_array_length_type () { + if (node is Field || node is Parameter) { + if (node is Parameter) { + unowned Parameter param = (Parameter) node; + if (param.base_parameter != null) { + return get_ccode_array_length_type (param.base_parameter); + } + } + return get_ccode_array_length_type (((Variable) node).variable_type); + } else if (node is Method || node is Delegate) { + if (node is Method) { + unowned Method method = (Method) node; + if (method.base_method != null && method.base_method != method) { + return get_ccode_array_length_type (method.base_method); + } else if (method.base_interface_method != null && method.base_interface_method != method) { + return get_ccode_array_length_type (method.base_interface_method); + } + } + return get_ccode_array_length_type (((Callable) node).return_type); + } else if (node is Property) { + unowned Property prop = (Property) node; + if (prop.base_property != null && prop.base_property != prop) { + return get_ccode_array_length_type (prop.base_property); + } else if (prop.base_interface_property != null && prop.base_interface_property != prop) { + return get_ccode_array_length_type (prop.base_interface_property); + } else { + return get_ccode_array_length_type (prop.property_type); + } + } else if (node is PropertyAccessor) { + return get_ccode_array_length_type (((PropertyAccessor) node).prop); + } else { + Report.error (node.source_reference, "`CCode.array_length_type' not supported"); + return ""; + } + } } -- 2.47.2