[PATCH] gdb: rewrite how per language primitive types are managed

Andrew Burgess andrew.burgess@embecosm.com
Mon Nov 16 22:16:54 GMT 2020


* Pedro Alves <pedro@palves.net> [2020-11-16 19:20:34 +0000]:

> On 10/31/20 11:22 PM, Andrew Burgess wrote:
> > +template struct type *
> > +language_lookup_primitive_type (const struct language_defn *la,
> > +				struct gdbarch *gdbarch,
> > +				std::function<bool (struct type *)> arg);
> 
> I'd rather we only use std::function when we actually need to store
> the object somewhere, and use gdb::function_view for callbacks,
> to avoid the potential heap allocation inside std::function.
> 
> I guess you did it because of the language_lookup_primitive_type
> template and the hiding of its body.   But avoiding the template
> in the public interface isn't worse, IMO.  I would say that it
> results in a clearer interface in language.h, even.
> 
> Would you be OK with this change?

Of course, this looks great.

And thanks for continuing to clean up after me :)

Andrew


> 
> From ffdcd1ccaea5d7d662632cbccceb36e689a9c214 Mon Sep 17 00:00:00 2001
> From: Pedro Alves <pedro@palves.net>
> Date: Mon, 16 Nov 2020 19:15:00 +0000
> Subject: [PATCH] language_lookup_primitive_type, std::function ->
>  gdb::function_view
> 
> gdb/ChangeLog:
> 
> 	* language.c (language_arch_info::lookup_primitive_type): Use
> 	gdb::function_view instead of gdb::function.
> 	(template language_lookup_primitive_type): Rename to ...
> 	(language_lookup_primitive_type_1): ... this, and make static.
> 	(language_lookup_primitive_type(const struct language_defn *,
> 	struct gdbarch *, const char *): Make non-template.
> 	(language_lookup_primitive_type(const struct language_defn *,
> 	struct gdbarch *, std::function<bool (struct type *)>): Make
> 	non-template and use gdb::function_view.
> 	* language.h (language_arch_info::lookup_primitive_type): Use
> 	gdb::function_view instead of std::function.
> 	(language_lookup_primitive_type): No longer template.
> 	* opencl-lang.c (lookup_opencl_vector_type): 'filter' is now a
> 	lambda instead of a std::function.
> ---
>  gdb/language.c    | 31 +++++++++++++++++++------------
>  gdb/language.h    | 25 ++++++++++++++++++-------
>  gdb/opencl-lang.c |  2 +-
>  3 files changed, 38 insertions(+), 20 deletions(-)
> 
> diff --git a/gdb/language.c b/gdb/language.c
> index 579cf9198c8..5e64e6af45f 100644
> --- a/gdb/language.c
> +++ b/gdb/language.c
> @@ -1088,7 +1088,7 @@ language_arch_info::lookup_primitive_type (const char *name)
>  
>  struct type *
>  language_arch_info::lookup_primitive_type
> -	(std::function<bool (struct type *)> filter)
> +  (gdb::function_view<bool (struct type *)> filter)
>  {
>    for (struct type_and_symbol &tas : primitive_types_and_symbols)
>      {
> @@ -1111,32 +1111,39 @@ language_arch_info::lookup_primitive_type_as_symbol (const char *name,
>    return nullptr;
>  }
>  
> -/* See language.h.  */
> +/* Helper for the language_lookup_primitive_type overloads to forward
> +   to the corresponding language's lookup_primitive_type overload.  */
>  
>  template<typename T>
> -struct type *
> -language_lookup_primitive_type (const struct language_defn *la,
> -				struct gdbarch *gdbarch,
> -				T arg)
> +static struct type *
> +language_lookup_primitive_type_1 (const struct language_defn *la,
> +				  struct gdbarch *gdbarch,
> +				  T arg)
>  {
>    struct language_gdbarch *ld =
>      (struct language_gdbarch *) gdbarch_data (gdbarch, language_gdbarch_data);
>    return ld->arch_info[la->la_language].lookup_primitive_type (arg);
>  }
>  
> -/* Template instantiation.  */
> +/* See language.h.  */
>  
> -template struct type *
> +struct type *
>  language_lookup_primitive_type (const struct language_defn *la,
>  				struct gdbarch *gdbarch,
> -				const char *arg);
> +				const char *name)
> +{
> +  return language_lookup_primitive_type_1 (la, gdbarch, name);
> +}
>  
> -/* Template instantiation.  */
> +/* See language.h.  */
>  
> -template struct type *
> +struct type *
>  language_lookup_primitive_type (const struct language_defn *la,
>  				struct gdbarch *gdbarch,
> -				std::function<bool (struct type *)> arg);
> +				gdb::function_view<bool (struct type *)> filter)
> +{
> +  return language_lookup_primitive_type_1 (la, gdbarch, filter);
> +}
>  
>  /* See language.h.  */
>  
> diff --git a/gdb/language.h b/gdb/language.h
> index 1b602646651..e955340440d 100644
> --- a/gdb/language.h
> +++ b/gdb/language.h
> @@ -140,7 +140,7 @@ struct language_arch_info
>    /* Lookup a primitive type for which FILTER returns true.  Will return
>       nullptr if no matching type is found.  */
>    struct type *lookup_primitive_type
> -	(std::function<bool (struct type *)> filter);
> +    (gdb::function_view<bool (struct type *)> filter);
>  
>    /* Lookup a primitive type called NAME and return the type as a symbol.
>       LANG is the language for which type is being looked up.  */
> @@ -719,15 +719,26 @@ struct type *language_bool_type (const struct language_defn *l,
>  struct type *language_string_char_type (const struct language_defn *l,
>  					struct gdbarch *gdbarch);
>  
> -/* Look up a type from the set of OS/ABI specific types defined in GDBARCH
> -   for language L.  ARG is used for selecting the matching type, and is
> -   passed through to the corresponding lookup_primitive_type member
> -   function inside the language_arch_info class.  */
> +/* Look up a type from the set of OS/ABI specific types defined in
> +   GDBARCH for language L.  NAME is used for selecting the matching
> +   type, and is passed through to the corresponding
> +   lookup_primitive_type member function inside the language_arch_info
> +   class.  */
>  
> -template<typename T>
>  struct type *language_lookup_primitive_type (const struct language_defn *l,
>  					     struct gdbarch *gdbarch,
> -					     T arg);
> +					     const char *name);
> +
> +/* Look up a type from the set of OS/ABI specific types defined in
> +   GDBARCH for language L.  FILTER is used for selecting the matching
> +   type, and is passed through to the corresponding
> +   lookup_primitive_type member function inside the language_arch_info
> +   class.  */
> +
> +struct type *language_lookup_primitive_type
> +  (const struct language_defn *la,
> +   struct gdbarch *gdbarch,
> +   gdb::function_view<bool (struct type *)> filter);
>  
>  /* Wrapper around language_lookup_primitive_type to return the
>     corresponding symbol.  */
> diff --git a/gdb/opencl-lang.c b/gdb/opencl-lang.c
> index e04fc546f63..830a6acaaab 100644
> --- a/gdb/opencl-lang.c
> +++ b/gdb/opencl-lang.c
> @@ -46,7 +46,7 @@ lookup_opencl_vector_type (struct gdbarch *gdbarch, enum type_code code,
>    /* Triple vectors have the size of a quad vector.  */
>    length = (n == 3) ?  el_length * 4 : el_length * n;
>  
> -  std::function<bool (struct type *)> filter = [&] (struct type *type)
> +  auto filter = [&] (struct type *type)
>    {
>      LONGEST lowb, highb;
>  
> 
> base-commit: 119e99bb7f5059ed31e574f0ceda8fbe9951403b
> -- 
> 2.14.5
> 


More information about the Gdb-patches mailing list