[vala/0.52] codegen: Accept CCode.type attribute on parameters



commit 711d66f16e2473b3a47c227c0f193a8402ed82ae
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Nov 27 19:26:35 2021 +0100

    codegen: Accept CCode.type attribute on parameters
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/876

 codegen/valaccodearraymodule.vala       | 22 +++++++-------
 codegen/valaccodemethodmodule.vala      | 31 +++++++++++---------
 codegen/valagtypemodule.vala            |  9 ++++--
 tests/methods/parameter-ccode-type.vala | 51 +++++++++++++++++++++++++++++++++
 4 files changed, 86 insertions(+), 27 deletions(-)
---
diff --git a/codegen/valaccodearraymodule.vala b/codegen/valaccodearraymodule.vala
index e9a8b0701..117395d65 100644
--- a/codegen/valaccodearraymodule.vala
+++ b/codegen/valaccodearraymodule.vala
@@ -842,23 +842,25 @@ public class Vala.CCodeArrayModule : CCodeMethodCallModule {
        }
 
        public override CCodeParameter generate_parameter (Parameter param, CCodeFile decl_space, 
Map<int,CCodeParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
-               if (param.params_array || !(param.variable_type is ArrayType)) {
+               unowned ArrayType? array_type = param.variable_type as ArrayType;
+               if (array_type == null || param.params_array) {
                        return base.generate_parameter (param, decl_space, cparam_map, carg_map);
                }
 
-               string ctypename = get_ccode_name (param.variable_type);
-               string name = get_ccode_name (param);
-               var array_type = (ArrayType) param.variable_type;
+               string? ctypename = get_ccode_type (param);
+               if (ctypename == null) {
+                       ctypename = get_ccode_name (param.variable_type);
 
-               if (array_type.fixed_length) {
-                       ctypename += "*";
-               }
+                       if (array_type.fixed_length) {
+                               ctypename += "*";
+                       }
 
-               if (param.direction != ParameterDirection.IN) {
-                       ctypename += "*";
+                       if (param.direction != ParameterDirection.IN) {
+                               ctypename += "*";
+                       }
                }
 
-               var main_cparam = new CCodeParameter (name, ctypename);
+               var main_cparam = new CCodeParameter (get_ccode_name (param), ctypename);
 
                generate_type_declaration (array_type.element_type, decl_space);
 
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index 063c3aa1d..b9e56f909 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -873,26 +873,29 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule {
        public virtual CCodeParameter generate_parameter (Parameter param, CCodeFile decl_space, 
Map<int,CCodeParameter> cparam_map, Map<int,CCodeExpression>? carg_map) {
                CCodeParameter cparam;
                if (!param.ellipsis && !param.params_array) {
-                       string ctypename = get_ccode_name (param.variable_type);
-
                        generate_type_declaration (param.variable_type, decl_space);
 
-                       // pass non-simple structs always by reference
-                       unowned Struct? st = param.variable_type.type_symbol as Struct;
-                       if (st != null) {
-                               if (!st.is_simple_type () && param.direction == ParameterDirection.IN) {
-                                       if (st.is_immutable && !param.variable_type.value_owned) {
-                                               ctypename = "const " + ctypename;
-                                       }
+                       string? ctypename = get_ccode_type (param);
+                       if (ctypename == null) {
+                               ctypename = get_ccode_name (param.variable_type);
+
+                               // pass non-simple structs always by reference
+                               unowned Struct? st = param.variable_type.type_symbol as Struct;
+                               if (st != null) {
+                                       if (!st.is_simple_type () && param.direction == 
ParameterDirection.IN) {
+                                               if (st.is_immutable && !param.variable_type.value_owned) {
+                                                       ctypename = "const " + ctypename;
+                                               }
 
-                                       if (!param.variable_type.nullable) {
-                                               ctypename += "*";
+                                               if (!param.variable_type.nullable) {
+                                                       ctypename += "*";
+                                               }
                                        }
                                }
-                       }
 
-                       if (param.direction != ParameterDirection.IN) {
-                               ctypename += "*";
+                               if (param.direction != ParameterDirection.IN) {
+                                       ctypename += "*";
+                               }
                        }
 
                        cparam = new CCodeParameter (get_ccode_name (param), ctypename);
diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala
index edd417a31..735f7d728 100644
--- a/codegen/valagtypemodule.vala
+++ b/codegen/valagtypemodule.vala
@@ -31,10 +31,13 @@ public class Vala.GTypeModule : GErrorModule {
 
                generate_type_declaration (param.variable_type, decl_space);
 
-               string ctypename = get_ccode_name (param.variable_type);
+               string? ctypename = get_ccode_type (param);
+               if (ctypename == null) {
+                       ctypename = get_ccode_name (param.variable_type);
 
-               if (param.direction != ParameterDirection.IN) {
-                       ctypename = "%s*".printf (ctypename);
+                       if (param.direction != ParameterDirection.IN) {
+                               ctypename = "%s*".printf (ctypename);
+                       }
                }
 
                var cparam = new CCodeParameter (get_ccode_name (param), ctypename);
diff --git a/tests/methods/parameter-ccode-type.vala b/tests/methods/parameter-ccode-type.vala
new file mode 100644
index 000000000..c9f7206fa
--- /dev/null
+++ b/tests/methods/parameter-ccode-type.vala
@@ -0,0 +1,51 @@
+void foo ([CCode (array_length = false, type = "const gchar**")] string[]? sa) {
+       assert (sa[1] == "bar");
+}
+
+const string[] BARS = { "foo", "bar", null };
+
+void bar ([CCode (array_length = false, type = "const gchar***")] out unowned string[]? sa) {
+       sa = BARS;
+}
+
+void manam ([CCode (array_length = false, type = "const GObject**")] Object[]? oa) {
+       assert (oa[1] == null);
+}
+
+void faz ([CCode (type = "const GObject*")] Object? o) {
+       assert (o == null);
+}
+
+void baz ([CCode (type = "const GObject**")] out unowned Object? o) {
+       o = null ;
+}
+
+void minim ([CCode (type = "gpointer*")] out unowned void* p) {
+       p = null;
+}
+
+void main () {
+       {
+               foo ({ "foo", "bar", null });
+       }
+       {
+               unowned string[] sa;
+               bar (out sa);
+       }
+       {
+               manam ({ null });
+       }
+       {
+               faz (null);
+       }
+       {
+               unowned Object? o;
+               baz (out o);
+               assert (o == null);
+       }
+       {
+               unowned void* p;
+               minim (out p);
+               assert (p == null);
+       }
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]