From 8260ce7efa16e84b51bae26de964d8797ac8171f Mon Sep 17 00:00:00 2001 From: Rico Tzschichholz Date: Wed, 21 Nov 2018 13:18:46 +0100 Subject: [PATCH] vala: Add Delegate/Method/ObjectTypeSymbol.has_type_parameters() and use them --- codegen/valaccodemethodcallmodule.vala | 6 +++--- codegen/valaccodemethodmodule.vala | 2 +- codegen/valagobjectmodule.vala | 8 ++++---- codegen/valagtypemodule.vala | 6 +++--- codegen/valatyperegisterfunction.vala | 2 +- vala/valadelegate.vala | 4 ++++ vala/valamemberaccess.vala | 4 ++-- vala/valamethod.vala | 6 +++++- vala/valamethodcall.vala | 2 +- vala/valaobjecttypesymbol.vala | 4 ++++ vala/valasignaltype.vala | 2 +- 11 files changed, 29 insertions(+), 17 deletions(-) diff --git a/codegen/valaccodemethodcallmodule.vala b/codegen/valaccodemethodcallmodule.vala index 0a9fea272..18505c247 100644 --- a/codegen/valaccodemethodcallmodule.vala +++ b/codegen/valaccodemethodcallmodule.vala @@ -185,7 +185,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule { } } else if (m is CreationMethod && m.parent_symbol is Struct) { ccall.add_argument (get_this_cexpression ()); - } else if (m != null && m.get_type_parameters ().size > 0 && !get_ccode_has_generic_type_parameter (m) && !get_ccode_simple_generics (m) && (ccall != finish_call || expr.is_yield_expression)) { + } else if (m != null && m.has_type_parameters () && !get_ccode_has_generic_type_parameter (m) && !get_ccode_simple_generics (m) && (ccall != finish_call || expr.is_yield_expression)) { // generic method // don't add generic arguments for .end() calls add_generic_type_arguments (in_arg_map, ma.get_type_arguments (), expr); @@ -270,7 +270,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule { if (m != null && get_ccode_has_generic_type_parameter (m)) { // insert type argument for macros - if (m.get_type_parameters ().size > 0) { + if (m.has_type_parameters ()) { // generic method int type_param_index = 0; foreach (var type_arg in ma.get_type_arguments ()) { @@ -328,7 +328,7 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule { ccode.add_assignment (new CCodeMemberAccess.pointer (get_variable_cexpression ("_data%d_".printf (get_block_id (current_method.body))), "self"), ref_call); } - if (!current_class.is_compact && current_class.get_type_parameters ().size > 0) { + if (!current_class.is_compact && current_class.has_type_parameters ()) { /* type, dup func, and destroy func fields for generic types */ var suffices = new string[] {"type", "dup_func", "destroy_func"}; foreach (TypeParameter type_param in current_class.get_type_parameters ()) { diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala index c9731a1ca..361fb0065 100644 --- a/codegen/valaccodemethodmodule.vala +++ b/codegen/valaccodemethodmodule.vala @@ -779,7 +779,7 @@ public abstract class Vala.CCodeMethodModule : CCodeStructModule { if (current_type_symbol is Class && gobject_type != null && current_class.is_subtype_of (gobject_type) - && current_class.get_type_parameters ().size > 0 + && current_class.has_type_parameters () && !((CreationMethod) m).chain_up) { var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.GREATER_THAN, new CCodeIdentifier ("__params_it"), new CCodeIdentifier ("__params")); ccode.open_while (ccond); diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala index 837046742..05e45e35d 100644 --- a/codegen/valagobjectmodule.vala +++ b/codegen/valagobjectmodule.vala @@ -35,10 +35,10 @@ public class Vala.GObjectModule : GTypeModule { } push_line (cl.source_reference); - if (class_has_readable_properties (cl) || cl.get_type_parameters ().size > 0) { + if (class_has_readable_properties (cl) || cl.has_type_parameters ()) { add_get_property_function (cl); } - if (class_has_writable_properties (cl) || cl.get_type_parameters ().size > 0) { + if (class_has_writable_properties (cl) || cl.has_type_parameters ()) { add_set_property_function (cl); } pop_line (); @@ -52,10 +52,10 @@ public class Vala.GObjectModule : GTypeModule { /* set property handlers */ var ccall = new CCodeFunctionCall (new CCodeIdentifier ("G_OBJECT_CLASS")); ccall.add_argument (new CCodeIdentifier ("klass")); - if (class_has_readable_properties (cl) || cl.get_type_parameters ().size > 0) { + if (class_has_readable_properties (cl) || cl.has_type_parameters ()) { ccode.add_assignment (new CCodeMemberAccess.pointer (ccall, "get_property"), new CCodeIdentifier ("_vala_%s_get_property".printf (get_ccode_lower_case_name (cl, null)))); } - if (class_has_writable_properties (cl) || cl.get_type_parameters ().size > 0) { + if (class_has_writable_properties (cl) || cl.has_type_parameters ()) { ccode.add_assignment (new CCodeMemberAccess.pointer (ccall, "set_property"), new CCodeIdentifier ("_vala_%s_set_property".printf (get_ccode_lower_case_name (cl, null)))); } diff --git a/codegen/valagtypemodule.vala b/codegen/valagtypemodule.vala index d81c530b8..32e7bf07c 100644 --- a/codegen/valagtypemodule.vala +++ b/codegen/valagtypemodule.vala @@ -585,7 +585,7 @@ public class Vala.GTypeModule : GErrorModule { } /* only add the *Private struct if it is not empty, i.e. we actually have private data */ - if (cl.has_private_fields || cl.get_type_parameters ().size > 0) { + if (cl.has_private_fields || cl.has_type_parameters ()) { decl_space.add_type_definition (instance_priv_struct); var parent_decl = new CCodeDeclaration ("gint"); @@ -1296,7 +1296,7 @@ public class Vala.GTypeModule : GErrorModule { } /* add struct for private fields */ - if (cl.has_private_fields || cl.get_type_parameters ().size > 0) { + if (cl.has_private_fields || cl.has_type_parameters ()) { ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_class_adjust_private_offset")); ccall.add_argument (new CCodeIdentifier ("klass")); ccall.add_argument (new CCodeIdentifier ("&%s_private_offset".printf (get_ccode_name (cl)))); @@ -1702,7 +1702,7 @@ public class Vala.GTypeModule : GErrorModule { } } - if (!cl.is_compact && (cl.has_private_fields || cl.get_type_parameters ().size > 0)) { + if (!cl.is_compact && (cl.has_private_fields || cl.has_type_parameters ())) { var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_get_instance_private".printf (get_ccode_lower_case_name (cl, null)))); ccall.add_argument (new CCodeIdentifier ("self")); func.add_assignment (new CCodeMemberAccess.pointer (new CCodeIdentifier ("self"), "priv"), ccall); diff --git a/codegen/valatyperegisterfunction.vala b/codegen/valatyperegisterfunction.vala index b78f4d217..394295f9b 100644 --- a/codegen/valatyperegisterfunction.vala +++ b/codegen/valatyperegisterfunction.vala @@ -221,7 +221,7 @@ public abstract class Vala.TypeRegisterFunction { get_type_interface_init_statements (context, type_init, plugin); } - if (cl != null && (cl.has_private_fields || cl.get_type_parameters ().size > 0)) { + if (cl != null && (cl.has_private_fields || cl.has_type_parameters ())) { if (!plugin) { var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_type_add_instance_private")); ccall.add_argument (new CCodeIdentifier (type_id_name)); diff --git a/vala/valadelegate.vala b/vala/valadelegate.vala index 16db6f1b8..4b4e7074a 100644 --- a/vala/valadelegate.vala +++ b/vala/valadelegate.vala @@ -97,6 +97,10 @@ public class Vala.Delegate : TypeSymbol, Callable { return type_parameters; } + public bool has_type_parameters () { + return (type_parameters != null && type_parameters.size > 0); + } + public override int get_type_parameter_index (string name) { int i = 0; foreach (TypeParameter parameter in type_parameters) { diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala index d2005f688..ab6d6a9dc 100644 --- a/vala/valamemberaccess.vala +++ b/vala/valamemberaccess.vala @@ -769,8 +769,8 @@ public class Vala.MemberAccess : Expression { // instance type might be a subtype of the parent symbol of the member // that subtype might not be generic, so do not report an error in that case var object_type = instance_type as ObjectType; - if (object_type != null && object_type.type_symbol.get_type_parameters ().size > 0 && - instance_type.get_type_arguments ().size == 0) { + if (object_type != null && object_type.type_symbol.has_type_parameters () + && !instance_type.has_type_arguments ()) { error = true; Report.error (inner.source_reference, "missing generic type arguments"); return false; diff --git a/vala/valamethod.vala b/vala/valamethod.vala index 955de7e00..7ec91729d 100644 --- a/vala/valamethod.vala +++ b/vala/valamethod.vala @@ -340,7 +340,7 @@ public class Vala.Method : Subroutine, Callable { } List method_type_args = null; - if (this.get_type_parameters ().size > 0) { + if (this.has_type_parameters ()) { method_type_args = new ArrayList (); foreach (TypeParameter type_parameter in this.get_type_parameters ()) { var type_arg = new GenericType (type_parameter); @@ -461,6 +461,10 @@ public class Vala.Method : Subroutine, Callable { return -1; } + public bool has_type_parameters () { + return (type_parameters != null && type_parameters.size > 0); + } + /** * Adds a precondition to this method. * diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala index 7fca36e39..3b5917668 100644 --- a/vala/valamethodcall.vala +++ b/vala/valamethodcall.vala @@ -535,7 +535,7 @@ public class Vala.MethodCall : Expression { dynamic_sig.handler.target_type = new DelegateType (dynamic_sig.get_delegate (new ObjectType ((ObjectTypeSymbol) dynamic_sig.parent_symbol), this)); } - if (m != null && m.get_type_parameters ().size > 0) { + if (m != null && m.has_type_parameters ()) { var ma = (MemberAccess) call; if (ma.get_type_arguments ().size == 0) { // infer type arguments diff --git a/vala/valaobjecttypesymbol.vala b/vala/valaobjecttypesymbol.vala index eab81c425..66e7f955f 100644 --- a/vala/valaobjecttypesymbol.vala +++ b/vala/valaobjecttypesymbol.vala @@ -254,6 +254,10 @@ public abstract class Vala.ObjectTypeSymbol : TypeSymbol { return type_parameters; } + public bool has_type_parameters () { + return (type_parameters != null && type_parameters.size > 0); + } + public override int get_type_parameter_index (string name) { int i = 0; foreach (TypeParameter parameter in type_parameters) { diff --git a/vala/valasignaltype.vala b/vala/valasignaltype.vala index 4a36816f0..f9a67e90c 100644 --- a/vala/valasignaltype.vala +++ b/vala/valasignaltype.vala @@ -66,7 +66,7 @@ public class Vala.SignalType : CallableType { var result = new DelegateType (signal_symbol.get_delegate (sender_type, this)); result.value_owned = true; - if (result.delegate_symbol.get_type_parameters ().size > 0) { + if (result.delegate_symbol.has_type_parameters ()) { foreach (var type_param in type_sym.get_type_parameters ()) { var type_arg = new GenericType (type_param); type_arg.value_owned = true; -- 2.47.2