From 5b7d941b90d1a232dc144dc14850dd2fb63c35da Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Fri, 22 Jan 2021 12:21:09 -0500 Subject: [PATCH] gdb: add owner-related methods to struct type Add the following methods to struct type: * is_objfile_owned * set_owner (objfile and gdbarch overloads) * objfile and arch getters Rename the fields in main_type to ensure no other code accesses them directly. As usual, we can't make them actually private, but giving them the `m_` prefix will help making sure they are not accessed when not supposed to, by convention. Remove the TYPE_OWNER macro to ensure no code uses the type_owner struct directly. gdb/ChangeLog: * gdbtypes.h (TYPE_OBJFILE_OWNED): Adjust. (TYPE_OWNER): Remove. (TYPE_OBJFILE): Adjust. (struct main_type) : Rename to... : ... this. : Rename to... : ... this. (struct type) : New methods. (TYPE_ALLOC): Adjust. * gdbtypes.c (alloc_type): Adjust. (alloc_type_arch): Adjust. (alloc_type_copy): Adjust. (get_type_arch): Adjust. (smash_type): Adjust. (lookup_array_range_type): Adjust. (recursive_dump_type): Adjust. (copy_type_recursive): Adjust. * compile/compile-c-types.c (convert_func): Adjust. (convert_type_basic): Adjust. * compile/compile-cplus-types.c (compile_cplus_convert_func): Adjust. * language.c (language_arch_info::type_and_symbol::alloc_type_symbol): Adjust. Change-Id: I7f92e869d9f92e2402a3d3007dd0832e05aa6ac8 --- gdb/ChangeLog | 28 ++++++++++++++++ gdb/compile/compile-c-types.c | 8 ++--- gdb/compile/compile-cplus-types.c | 4 +-- gdb/gdbtypes.c | 38 +++++++++++---------- gdb/gdbtypes.h | 56 +++++++++++++++++++++++++++---- gdb/language.c | 2 +- 6 files changed, 104 insertions(+), 32 deletions(-) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 83c1de9f543..dc7ecf13f73 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,31 @@ +2021-01-22 Simon Marchi + + * gdbtypes.h (TYPE_OBJFILE_OWNED): Adjust. + (TYPE_OWNER): Remove. + (TYPE_OBJFILE): Adjust. + (struct main_type) : Rename to... + : ... this. + : Rename to... + : ... this. + (struct type) : New + methods. + (TYPE_ALLOC): Adjust. + * gdbtypes.c (alloc_type): Adjust. + (alloc_type_arch): Adjust. + (alloc_type_copy): Adjust. + (get_type_arch): Adjust. + (smash_type): Adjust. + (lookup_array_range_type): Adjust. + (recursive_dump_type): Adjust. + (copy_type_recursive): Adjust. + * compile/compile-c-types.c (convert_func): Adjust. + (convert_type_basic): Adjust. + * compile/compile-cplus-types.c (compile_cplus_convert_func): + Adjust. + * language.c + (language_arch_info::type_and_symbol::alloc_type_symbol): + Adjust. + 2021-01-21 Luis Machado * coffread.c (enter_linenos): Passing string to complaint. diff --git a/gdb/compile/compile-c-types.c b/gdb/compile/compile-c-types.c index eea084469a8..90de2084c85 100644 --- a/gdb/compile/compile-c-types.c +++ b/gdb/compile/compile-c-types.c @@ -165,9 +165,9 @@ convert_func (compile_c_instance *context, struct type *type) if (target_type == NULL) { if (TYPE_OBJFILE_OWNED (type)) - target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int; + target_type = objfile_type (type->objfile ())->builtin_int; else - target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int; + target_type = builtin_type (type->arch ())->builtin_int; warning (_("function has unknown return type; assuming int")); } @@ -324,9 +324,9 @@ convert_type_basic (compile_c_instance *context, struct type *type) built-in parser used to do, but at least warn. */ struct type *fallback; if (TYPE_OBJFILE_OWNED (type)) - fallback = objfile_type (TYPE_OWNER (type).objfile)->builtin_int; + fallback = objfile_type (type->objfile ())->builtin_int; else - fallback = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int; + fallback = builtin_type (type->arch ())->builtin_int; warning (_("variable has unknown type; assuming int")); return convert_int (context, fallback); } diff --git a/gdb/compile/compile-cplus-types.c b/gdb/compile/compile-cplus-types.c index 5289f6f4592..ddb0d8a0368 100644 --- a/gdb/compile/compile-cplus-types.c +++ b/gdb/compile/compile-cplus-types.c @@ -971,9 +971,9 @@ compile_cplus_convert_func (compile_cplus_instance *instance, if (target_type == nullptr) { if (TYPE_OBJFILE_OWNED (type)) - target_type = objfile_type (TYPE_OWNER (type).objfile)->builtin_int; + target_type = objfile_type (type->objfile ())->builtin_int; else - target_type = builtin_type (TYPE_OWNER (type).gdbarch)->builtin_int; + target_type = builtin_type (type->arch ())->builtin_int; warning (_("function has unknown return type; assuming int")); } diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index d9842817462..e2f8837dcb4 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -183,8 +183,7 @@ alloc_type (struct objfile *objfile) struct main_type); OBJSTAT (objfile, n_types++); - TYPE_OBJFILE_OWNED (type) = 1; - TYPE_OWNER (type).objfile = objfile; + type->set_owner (objfile); /* Initialize the fields that might not be zero. */ @@ -210,8 +209,7 @@ alloc_type_arch (struct gdbarch *gdbarch) type = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct type); TYPE_MAIN_TYPE (type) = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct main_type); - TYPE_OBJFILE_OWNED (type) = 0; - TYPE_OWNER (type).gdbarch = gdbarch; + type->set_owner (gdbarch); /* Initialize the fields that might not be zero. */ @@ -229,9 +227,9 @@ struct type * alloc_type_copy (const struct type *type) { if (TYPE_OBJFILE_OWNED (type)) - return alloc_type (TYPE_OWNER (type).objfile); + return alloc_type (type->objfile ()); else - return alloc_type_arch (TYPE_OWNER (type).gdbarch); + return alloc_type_arch (type->arch ()); } /* If TYPE is gdbarch-associated, return that architecture. @@ -243,9 +241,9 @@ get_type_arch (const struct type *type) struct gdbarch *arch; if (TYPE_OBJFILE_OWNED (type)) - arch = TYPE_OWNER (type).objfile->arch (); + arch = type->objfile ()->arch (); else - arch = TYPE_OWNER (type).gdbarch; + arch = type->arch (); /* The ARCH can be NULL if TYPE is associated with neither an objfile nor a gdbarch, however, this is very rare, and even then, in most cases @@ -311,14 +309,17 @@ alloc_type_instance (struct type *oldtype) static void smash_type (struct type *type) { - int objfile_owned = TYPE_OBJFILE_OWNED (type); - union type_owner owner = TYPE_OWNER (type); + bool objfile_owned = type->is_objfile_owned (); + objfile *objfile = type->objfile (); + gdbarch *arch = type->arch (); memset (TYPE_MAIN_TYPE (type), 0, sizeof (struct main_type)); /* Restore owner information. */ - TYPE_OBJFILE_OWNED (type) = objfile_owned; - TYPE_OWNER (type) = owner; + if (objfile_owned) + type->set_owner (objfile); + else + type->set_owner (arch); /* For now, delete the rings. */ TYPE_CHAIN (type) = type; @@ -1429,9 +1430,10 @@ lookup_array_range_type (struct type *element_type, struct type *range_type; if (TYPE_OBJFILE_OWNED (element_type)) - index_type = objfile_type (TYPE_OWNER (element_type).objfile)->builtin_int; + index_type = objfile_type (element_type->objfile ())->builtin_int; else - index_type = builtin_type (get_type_arch (element_type))->builtin_int; + index_type = builtin_type (element_type->arch ())->builtin_int; + range_type = create_static_range_type (NULL, index_type, low_bound, high_bound); @@ -5190,12 +5192,12 @@ recursive_dump_type (struct type *type, int spaces) if (TYPE_OBJFILE_OWNED (type)) { printf_filtered ("%*sobjfile ", spaces, ""); - gdb_print_host_address (TYPE_OWNER (type).objfile, gdb_stdout); + gdb_print_host_address (type->objfile (), gdb_stdout); } else { printf_filtered ("%*sgdbarch ", spaces, ""); - gdb_print_host_address (TYPE_OWNER (type).gdbarch, gdb_stdout); + gdb_print_host_address (type->arch (), gdb_stdout); } printf_filtered ("\n"); printf_filtered ("%*starget_type ", spaces, ""); @@ -5515,8 +5517,8 @@ copy_type_recursive (struct objfile *objfile, /* Copy the common fields of types. For the main type, we simply copy the entire thing and then update specific fields as needed. */ *TYPE_MAIN_TYPE (new_type) = *TYPE_MAIN_TYPE (type); - TYPE_OBJFILE_OWNED (new_type) = 0; - TYPE_OWNER (new_type).gdbarch = get_type_arch (type); + + new_type->set_owner (type->arch ()); if (type->name ()) new_type->set_name (xstrdup (type->name ())); diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index c74098826b8..bc28e851880 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -224,9 +224,8 @@ DEF_ENUM_FLAGS_TYPE (enum type_instance_flag_value, type_instance_flags); the objfile retrieved as TYPE_OBJFILE. Otherwise, the type is owned by an architecture; TYPE_OBJFILE is NULL in this case. */ -#define TYPE_OBJFILE_OWNED(t) (TYPE_MAIN_TYPE (t)->flag_objfile_owned) -#define TYPE_OWNER(t) TYPE_MAIN_TYPE(t)->owner -#define TYPE_OBJFILE(t) (TYPE_OBJFILE_OWNED(t)? TYPE_OWNER(t).objfile : NULL) +#define TYPE_OBJFILE_OWNED(t) ((t)->is_objfile_owned ()) +#define TYPE_OBJFILE(t) ((t)->objfile ()) /* * True if this type was declared using the "class" keyword. This is only valid for C++ structure and enum types. If false, a structure @@ -817,7 +816,7 @@ struct main_type unsigned int m_flag_stub_supported : 1; unsigned int m_flag_gnu_ifunc : 1; unsigned int m_flag_fixed_instance : 1; - unsigned int flag_objfile_owned : 1; + unsigned int m_flag_objfile_owned : 1; unsigned int m_flag_endianity_not_default : 1; /* * True if this type was declared with "class" rather than @@ -860,7 +859,7 @@ struct main_type this is somewhat ugly, but without major overhaul of the internal type system, it can't be avoided for now. */ - union type_owner owner; + union type_owner m_owner; /* * For a pointer type, describes the type of object pointed to. - For an array type, describes the type of the elements. @@ -1243,6 +1242,49 @@ struct type /* * Remove dynamic property of kind KIND from this type, if it exists. */ void remove_dyn_prop (dynamic_prop_node_kind kind); + /* Return true if this type is owned by an objfile. Return false if it is + owned by an architecture. */ + bool is_objfile_owned () const + { + return this->main_type->m_flag_objfile_owned; + } + + /* Set the owner of the type to be OBJFILE. */ + void set_owner (objfile *objfile) + { + this->main_type->m_owner.objfile = objfile; + this->main_type->m_flag_objfile_owned = true; + } + + /* Set the owner of the type to be ARCH. */ + void set_owner (gdbarch *arch) + { + this->main_type->m_owner.gdbarch = arch; + this->main_type->m_flag_objfile_owned = false; + } + + /* Return the objfile owner of this type. + + Return nullptr if this type is not objfile-owned. */ + struct objfile *objfile () const + { + if (!this->is_objfile_owned ()) + return nullptr; + + return this->main_type->m_owner.objfile; + } + + /* Return the gdbarch owner of this type. + + Return nullptr if this type is not gdbarch-owned. */ + gdbarch *arch () const + { + if (this->is_objfile_owned ()) + return nullptr; + + return this->main_type->m_owner.gdbarch; + } + /* * Return true if this is an integer type whose logical (bit) size differs from its storage size; false otherwise. Always return false for non-integer (i.e., non-TYPE_SPECIFIC_INT) types. */ @@ -2201,8 +2243,8 @@ extern const struct floatformat *floatformats_bfloat16[BFD_ENDIAN_UNKNOWN]; #define TYPE_ALLOC(t,size) \ (obstack_alloc ((TYPE_OBJFILE_OWNED (t) \ - ? &TYPE_OBJFILE (t)->objfile_obstack \ - : gdbarch_obstack (TYPE_OWNER (t).gdbarch)), \ + ? &((t)->objfile ()->objfile_obstack) \ + : gdbarch_obstack ((t)->arch ())), \ size)) diff --git a/gdb/language.c b/gdb/language.c index 1770ba1f8e1..d4b84911f8b 100644 --- a/gdb/language.c +++ b/gdb/language.c @@ -1037,7 +1037,7 @@ language_arch_info::type_and_symbol::alloc_type_symbol struct symbol *symbol; struct gdbarch *gdbarch; gdb_assert (!TYPE_OBJFILE_OWNED (type)); - gdbarch = TYPE_OWNER (type).gdbarch; + gdbarch = type->arch (); symbol = new (gdbarch_obstack (gdbarch)) struct symbol (); symbol->m_name = type->name (); symbol->set_language (lang, nullptr); -- 2.30.2