]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Added struct generation:
authorJCWasmx86 <JCWasmx86@t-online.de>
Sun, 5 Jun 2022 15:30:29 +0000 (17:30 +0200)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 29 Apr 2023 19:00:17 +0000 (21:00 +0200)
Only message is:
(valadbusgen:196678): vala-CRITICAL **: 17:30:19.916: vala_collection_add: assertion 'self != NULL' failed
albeit I'm not sure whether it is the fault of the code or of my environment

dbusgen/valadbusparser.vala
dbusgen/valadbusvariantmodule.vala

index 5321340b5d702cded57353ed7672b0a9a7ab8e3a..4222fdc953824ba372a0b9b5edd817b1bc25ac00 100644 (file)
@@ -72,6 +72,7 @@ public class Vala.DBusParser : CodeVisitor {
                current_ns = context.root;
 
                dbus_module = new DBusVariantModule (context);
+               dbus_module.current_ns = context.root;
 
                context.accept (this);
        }
@@ -136,6 +137,7 @@ public class Vala.DBusParser : CodeVisitor {
                        var ns = new Namespace (ns_name, get_current_src ());
                        current_ns.add_namespace (ns);
                        current_ns = ns;
+                       dbus_module.current_ns = current_ns;
                }
        }
 
index d584568bb327e77e0ef0b570c39895d1b033f9f7..611611b06063d3499639f09acde3c43018052555 100644 (file)
@@ -26,6 +26,8 @@ public class Vala.DBusVariantModule {
 
        public Symbol root_symbol;
 
+       public Namespace current_ns;
+
        public DataType void_type = new VoidType ();
        public DataType bool_type;
        public DataType char_type;
@@ -162,6 +164,15 @@ public class Vala.DBusVariantModule {
                return get_complex_type (type);
        }
 
+       private string generate_struct_name (VariantType type) {
+               return "DBusProxyStruct" + ((string) type.peek_string ())
+                                       .replace ("(", "_1")
+                                       .replace (")", "1_")
+                                       .replace ("{", "_2")
+                                       .replace ("}", "2_")
+                                       ;
+       }
+
        private bool invalid_generic_type (VariantType type) {
                return  type.equal (VariantType.BOOLEAN)
                                || type.equal (VariantType.BYTE)
@@ -195,7 +206,7 @@ public class Vala.DBusVariantModule {
                                var invalid_generic_arg = invalid_generic_type (element.key()) || invalid_generic_type (element.value ());
                                var key = get_variant_type (element.key ());
                                var value = get_variant_type (element.value ());
-                               var valid_types = !((key is ArrayType) || (value is ArrayType) || invalid_generic_arg);
+                               var valid_types = !((key is ArrayType) || (value is ArrayType) || (key is StructValueType) || (value is StructValueType) || invalid_generic_arg);
                                if (key != null && value != null && valid_types) {
                                        res.add_type_argument (key);
                                        res.add_type_argument (value);
@@ -209,6 +220,37 @@ public class Vala.DBusVariantModule {
                                        return array;
                                }
                        }
+               } else if (type.is_tuple ()) {
+                       var generated_name = generate_struct_name (type);
+                       foreach (var st in current_ns.get_structs ()) {
+                               if (st.name == generated_name) {
+                                       return new StructValueType (st, null);
+                               }
+                       }
+                       var n = type.n_items ();
+                       var able_to_add_all = true;
+                       unowned var sub = type.first ();
+                       var sref = new SourceReference (
+                                                               new SourceFile (context, SourceFileType.NONE, "<artificial>", null, true),
+                                                               new SourceLocation (null, 0, 0),
+                                                               new SourceLocation (null, 0, 1));
+                       var new_struct = new Struct (generated_name, sref);
+                       new_struct.access = SymbolAccessibility.PUBLIC;
+                       for (var i = 0; i < n; i++) {
+                               var dt = get_dbus_type (sub.dup_string ());
+                               if (dt == null) {
+                                       able_to_add_all = false;
+                                       break;
+                               }
+                               var field = new Field ("arg%d".printf (i), dt, null);
+                               field.access = SymbolAccessibility.PUBLIC;
+                               new_struct.add_field (field);
+                               sub = sub.next();
+                       }
+                       if (able_to_add_all) {
+                               current_ns.add_struct (new_struct);
+                               return new StructValueType (new_struct, null);
+                       }
                }
 
                if (!skipped_generation) {