[vala/0.48] girparser: Move special handling for certain parameters to process_callable()



commit fa8684bf214da23f33912495773404386f2f47f6
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Apr 19 18:20:41 2020 +0200

    girparser: Move special handling for certain parameters to process_callable()
    
    - Detect delegate throwing error when GLib.Error out-parameter exists
    - Mark method as async based on finding an AsyncReadyCallback parameter
    - Apply null-literal default-value for trailing GLib.Cancellable? parameters

 vala/valagirparser.vala | 70 +++++++++++++++++++++++++------------------------
 1 file changed, 36 insertions(+), 34 deletions(-)
---
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 88f874d8f..1fd97af80 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -3276,7 +3276,6 @@ public class Vala.GirParser : CodeVisitor {
                                                ((Delegate) s).add_error_type (error_type);
                                        }
                                }
-                               throws_string = "1";
                        } else if (throws_string == "1") {
                                if (s is Method) {
                                        ((Method) s).add_error_type (new ErrorType (null, null));
@@ -3411,24 +3410,6 @@ public class Vala.GirParser : CodeVisitor {
                                }
 
                                var info = new ParameterInfo (param, array_length_idx, closure_idx, 
destroy_idx, scope == "async");
-                               unowned UnresolvedType? unresolved_type = param.variable_type as 
UnresolvedType;
-
-                               if (s is Method && scope == "async") {
-                                       if (unresolved_type != null && unresolved_type.unresolved_symbol.name 
== "AsyncReadyCallback") {
-                                               // GAsync-style method
-                                               ((Method) s).coroutine = true;
-                                               info.keep = false;
-                                       }
-                               }
-
-                               if (s is Delegate && throws_string != "1" && param.direction == 
ParameterDirection.OUT) {
-                                       if (unresolved_type != null && 
unresolved_type.unresolved_symbol.to_string () == "GLib.Error") {
-                                               ((Delegate) s).add_error_type (new ErrorType (null, null));
-                                               info.is_error = true;
-                                               info.keep = false;
-                                       }
-                               }
-
                                parameters.add (info);
                                pop_metadata ();
                        }
@@ -3436,18 +3417,6 @@ public class Vala.GirParser : CodeVisitor {
                }
                current.parameters = parameters;
 
-               for (int param_n = parameters.size - 1 ; param_n >= 0 ; param_n--) {
-                       ParameterInfo pi = parameters[param_n];
-                       if (!pi.param.ellipsis && pi.param.initializer == null) {
-                               string type_string = pi.param.variable_type.to_string ();
-                               if (type_string == "Gio.Cancellable?") {
-                                       pi.param.initializer = new Vala.NullLiteral ();
-                               } else {
-                                       break;
-                               }
-                       }
-               }
-
                pop_node ();
                end_element (element_name);
        }
@@ -3923,10 +3892,11 @@ public class Vala.GirParser : CodeVisitor {
                        }
                }
 
-               // Do not mark out-parameters as nullable if they are simple-types,
-               // since it would result in a boxed-type in vala
                foreach (ParameterInfo info in parameters) {
-                       var type = info.param.variable_type;
+                       unowned DataType type = info.param.variable_type;
+
+                       // Do not mark out-parameters as nullable if they are simple-types,
+                       // since it would result in a boxed-type in vala
                        if (info.param.direction == ParameterDirection.OUT && type.nullable) {
                                Struct? st = null;
                                if (type is UnresolvedType) {
@@ -3938,6 +3908,38 @@ public class Vala.GirParser : CodeVisitor {
                                        type.nullable = false;
                                }
                        }
+
+                       // Check and mark GAsync-style methods
+                       if (info.is_async && s is Method) {
+                               unowned UnresolvedType? unresolved_type = type as UnresolvedType;
+                               if (unresolved_type != null && unresolved_type.unresolved_symbol.name == 
"AsyncReadyCallback") {
+                                       ((Method) s).coroutine = true;
+                                       info.keep = false;
+                               }
+                       }
+
+                       // More thorough check for delegates throwing an error
+                       if (info.param.direction == ParameterDirection.OUT && s is Delegate && 
!s.tree_can_fail) {
+                               unowned UnresolvedType? unresolved_type = type as UnresolvedType;
+                               if (unresolved_type != null && unresolved_type.unresolved_symbol.to_string () 
== "GLib.Error") {
+                                       ((Delegate) s).add_error_type (new ErrorType (null, null));
+                                       info.is_error = true;
+                                       info.keep = false;
+                               }
+                       }
+               }
+
+               // Add null-literal as default-value for trailing GLib.Cancellable parameters
+               for (int param_n = parameters.size - 1 ; param_n >= 0 ; param_n--) {
+                       ParameterInfo info = parameters[param_n];
+                       if (!info.param.ellipsis && info.param.initializer == null) {
+                               string type_string = info.param.variable_type.to_string ();
+                               if (type_string == "GLib.Cancellable?" || type_string == "Gio.Cancellable?") {
+                                       info.param.initializer = new Vala.NullLiteral ();
+                               } else {
+                                       break;
+                               }
+                       }
                }
 
                if (parameters.size > 1) {


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