]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Inherit "array_length_type" from (base-/base-interface) parameter/property...
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 17 Mar 2020 14:26:36 +0000 (15:26 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 17 Mar 2020 14:35:01 +0000 (15:35 +0100)
See https://gitlab.gnome.org/GNOME/vala/issues/938

codegen/valaccode.vala
codegen/valaccodeattribute.vala

index c8dfb167c48d1ccd8120f8108406b725955d95be..e39fdbeccbdeb23421b0837c4f31395635702e34 100644 (file)
@@ -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;
                }
        }
index ce61b36676107bd2d2cd7d29347f03ea9c4ab759..f8491e015efa6d0d40a5e530369906491cf5c1fa 100644 (file)
@@ -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 "";
+               }
+       }
 }