From 4a987986cd5255fe2a430ea2296679270839d9b5 Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Sun, 19 Apr 2020 18:20:41 +0200 Subject: [PATCH] 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 07e70aa34..6a1ba6caf 100644 --- a/vala/valagirparser.vala +++ b/vala/valagirparser.vala @@ -3280,7 +3280,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)); @@ -3415,24 +3414,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 (); } @@ -3440,18 +3421,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); } @@ -3927,10 +3896,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) { @@ -3942,6 +3912,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) { -- 2.47.2