public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 00/27] C++-ify struct block
@ 2023-01-20 21:45 Tom Tromey
  2023-01-20 21:45 ` [PATCH 01/27] Rearrange block.c to avoid a forward declaration Tom Tromey
                   ` (26 more replies)
  0 siblings, 27 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches

I was poking at struct block and noticed an extra memory allocation,
and then it sort of ballooned into C++-ification.

Regression tested on x86-64 Fedora 36.

Tom



^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 01/27] Rearrange block.c to avoid a forward declaration
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 02/27] Avoid extra allocations in block Tom Tromey
                   ` (25 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

Moving block_initialize_namespace before its callers lets us avoid a
forward declaration.
---
 gdb/block.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 167cb08e0bb..751f67d30f7 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -37,9 +37,6 @@ struct block_namespace_info : public allocate_on_obstack
   struct using_direct *using_decl = nullptr;
 };
 
-static void block_initialize_namespace (struct block *block,
-					struct obstack *obstack);
-
 /* See block.h.  */
 
 struct objfile *
@@ -305,6 +302,16 @@ block_scope (const struct block *block)
   return "";
 }
 
+/* If block->namespace_info () is NULL, allocate it via OBSTACK and
+   initialize its members to zero.  */
+
+static void
+block_initialize_namespace (struct block *block, struct obstack *obstack)
+{
+  if (block->namespace_info () == NULL)
+    block->set_namespace_info (new (obstack) struct block_namespace_info ());
+}
+
 /* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
    OBSTACK.  (It won't make a copy of SCOPE, however, so that already
    has to be allocated correctly.)  */
@@ -344,16 +351,6 @@ block_set_using (struct block *block,
   block->namespace_info ()->using_decl = using_decl;
 }
 
-/* If block->namespace_info () is NULL, allocate it via OBSTACK and
-   initialize its members to zero.  */
-
-static void
-block_initialize_namespace (struct block *block, struct obstack *obstack)
-{
-  if (block->namespace_info () == NULL)
-    block->set_namespace_info (new (obstack) struct block_namespace_info ());
-}
-
 /* Return the static block associated to BLOCK.  Return NULL if block
    is NULL or if block is a global block.  */
 
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 02/27] Avoid extra allocations in block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
  2023-01-20 21:45 ` [PATCH 01/27] Rearrange block.c to avoid a forward declaration Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 03/27] Don't allow NULL as an argument to block_scope Tom Tromey
                   ` (24 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

block_set_scope and block_set_using unconditionally allocate the block
namespace object.  However, this isn't truly needed, so arrange to
only allocate when it is.
---
 gdb/block.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/gdb/block.c b/gdb/block.c
index 751f67d30f7..f24a2b5d084 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -320,6 +320,12 @@ void
 block_set_scope (struct block *block, const char *scope,
 		 struct obstack *obstack)
 {
+  if (scope == nullptr || scope[0] == '\0')
+    {
+      /* Don't bother.  */
+      return;
+    }
+
   block_initialize_namespace (block, obstack);
 
   block->namespace_info ()->scope = scope;
@@ -346,6 +352,12 @@ block_set_using (struct block *block,
 		 struct using_direct *using_decl,
 		 struct obstack *obstack)
 {
+  if (using_decl == nullptr)
+    {
+      /* Don't bother.  */
+      return;
+    }
+
   block_initialize_namespace (block, obstack);
 
   block->namespace_info ()->using_decl = using_decl;
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 03/27] Don't allow NULL as an argument to block_scope
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
  2023-01-20 21:45 ` [PATCH 01/27] Rearrange block.c to avoid a forward declaration Tom Tromey
  2023-01-20 21:45 ` [PATCH 02/27] Avoid extra allocations in block Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 04/27] Don't allow NULL as an argument to block_using Tom Tromey
                   ` (23 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

block_scope has special behavior when the block is NULL.
Remove this and patch up the callers instead.
---
 gdb/d-namespace.c | 2 +-
 gdb/rust-lang.h   | 5 ++---
 gdb/rust-parse.c  | 4 +++-
 3 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/gdb/d-namespace.c b/gdb/d-namespace.c
index b6184034d5d..2978e5fecaf 100644
--- a/gdb/d-namespace.c
+++ b/gdb/d-namespace.c
@@ -511,7 +511,7 @@ d_lookup_symbol_nonlocal (const struct language_defn *langdef,
 			  const domain_enum domain)
 {
   struct block_symbol sym;
-  const char *scope = block_scope (block);
+  const char *scope = block == nullptr ? "" : block_scope (block);
 
   sym = lookup_module_scope (langdef, name, block, domain, scope, 0);
   if (sym.symbol != NULL)
diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h
index 89e03550fb7..497342d4ef3 100644
--- a/gdb/rust-lang.h
+++ b/gdb/rust-lang.h
@@ -148,17 +148,16 @@ class rust_language : public language_defn
   {
     struct block_symbol result = {};
 
+    const char *scope = block == nullptr ? "" : block_scope (block);
     symbol_lookup_debug_printf
       ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
-       name, host_address_to_string (block), block_scope (block),
+       name, host_address_to_string (block), scope,
        domain_name (domain));
 
     /* Look up bare names in the block's scope.  */
     std::string scopedname;
     if (name[cp_find_first_component (name)] == '\0')
       {
-	const char *scope = block_scope (block);
-
 	if (scope[0] != '\0')
 	  {
 	    scopedname = std::string (scope) + "::" + name;
diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 489be4b87e3..72b843ef40c 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -373,7 +373,9 @@ rust_parser::crate_name (const std::string &name)
 std::string
 rust_parser::super_name (const std::string &ident, unsigned int n_supers)
 {
-  const char *scope = block_scope (pstate->expression_context_block);
+  const char *scope = "";
+  if (pstate->expression_context_block != nullptr)
+    scope = block_scope (pstate->expression_context_block);
   int offset;
 
   if (scope[0] == '\0')
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 04/27] Don't allow NULL as an argument to block_using
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (2 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 03/27] Don't allow NULL as an argument to block_scope Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 05/27] Don't allow NULL as an argument to block_static_block Tom Tromey
                   ` (22 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

block_using has special behavior when the block is NULL.
Remove this.  No caller seems to be affected.
---
 gdb/block.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/gdb/block.c b/gdb/block.c
index f24a2b5d084..97a0214e037 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -337,7 +337,7 @@ block_set_scope (struct block *block, const char *scope,
 struct using_direct *
 block_using (const struct block *block)
 {
-  if (block == NULL || block->namespace_info () == NULL)
+  if (block->namespace_info () == NULL)
     return NULL;
   else
     return block->namespace_info ()->using_decl;
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 05/27] Don't allow NULL as an argument to block_static_block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (3 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 04/27] Don't allow NULL as an argument to block_using Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 06/27] Don't allow NULL as an argument to block_global_block Tom Tromey
                   ` (21 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

block_static_block has special behavior when the block is NULL.
Remove this and patch up the callers instead.
---
 gdb/ada-lang.c                      |  6 +++++-
 gdb/block.c                         |  4 ++--
 gdb/compile/compile-c-symbols.c     |  8 ++++++--
 gdb/compile/compile-cplus-symbols.c |  4 +++-
 gdb/cp-support.c                    |  6 +++++-
 gdb/symtab.c                        | 13 +++++++++----
 6 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 40f85914a56..dfcd9a5fcd1 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13815,7 +13815,11 @@ class ada_language : public language_defn
   {
     struct block_symbol sym;
 
-    sym = ada_lookup_symbol (name, block_static_block (block), domain);
+    sym = ada_lookup_symbol (name,
+			     (block == nullptr
+			      ? nullptr
+			      : block_static_block (block)),
+			     domain);
     if (sym.symbol != NULL)
       return sym;
 
diff --git a/gdb/block.c b/gdb/block.c
index 97a0214e037..d21729f069d 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -364,12 +364,12 @@ block_set_using (struct block *block,
 }
 
 /* Return the static block associated to BLOCK.  Return NULL if block
-   is NULL or if block is a global block.  */
+   is a global block.  */
 
 const struct block *
 block_static_block (const struct block *block)
 {
-  if (block == NULL || block->superblock () == NULL)
+  if (block->superblock () == NULL)
     return NULL;
 
   while (block->superblock ()->superblock () != NULL)
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index 00ac4523f86..79490b811c3 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -212,7 +212,6 @@ static void
 convert_symbol_sym (compile_c_instance *context, const char *identifier,
 		    struct block_symbol sym, domain_enum domain)
 {
-  const struct block *static_block;
   int is_local_symbol;
 
   /* If we found a symbol and it is not in the  static or global
@@ -227,7 +226,9 @@ convert_symbol_sym (compile_c_instance *context, const char *identifier,
      }
   */
 
-  static_block = block_static_block (sym.block);
+  const struct block *static_block = nullptr;
+  if (sym.block != nullptr)
+    static_block = block_static_block (sym.block);
   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
   is_local_symbol = (sym.block != static_block && static_block != NULL);
   if (is_local_symbol)
@@ -613,6 +614,9 @@ generate_c_for_variable_locations (compile_instance *compiler,
 				   const struct block *block,
 				   CORE_ADDR pc)
 {
+  if (block == nullptr)
+    return {};
+
   const struct block *static_block = block_static_block (block);
 
   /* If we're already in the static or global block, there is nothing
diff --git a/gdb/compile/compile-cplus-symbols.c b/gdb/compile/compile-cplus-symbols.c
index 2df68b7dd32..45a76eef303 100644
--- a/gdb/compile/compile-cplus-symbols.c
+++ b/gdb/compile/compile-cplus-symbols.c
@@ -239,7 +239,9 @@ convert_symbol_sym (compile_cplus_instance *instance,
      }
   */
 
-  const struct block *static_block = block_static_block (sym.block);
+  const struct block *static_block = nullptr;
+  if (sym.block != nullptr)
+    static_block = block_static_block (sym.block);
   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
   bool is_local_symbol = (sym.block != static_block && static_block != nullptr);
   if (is_local_symbol)
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index b7ed2101b78..08f7c2b4140 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1306,7 +1306,8 @@ add_symbol_overload_list_namespace (const char *func_name,
     }
 
   /* Look in the static block.  */
-  block = block_static_block (get_selected_block (0));
+  block = get_selected_block (0);
+  block = block == nullptr ? nullptr : block_static_block (block);
   if (block)
     add_symbol_overload_list_block (name, block, overload_list);
 
@@ -1456,6 +1457,9 @@ add_symbol_overload_list_qualified (const char *func_name,
     add_symbol_overload_list_block (func_name, b, overload_list);
 
   surrounding_static_block = block_static_block (get_selected_block (0));
+  surrounding_static_block = (surrounding_static_block == nullptr
+			      ? nullptr
+			      : block_static_block (surrounding_static_block));
 
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
diff --git a/gdb/symtab.c b/gdb/symtab.c
index b3445133c8c..0e522b2c7f9 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2179,13 +2179,15 @@ lookup_local_symbol (const char *name,
 		     const domain_enum domain,
 		     enum language language)
 {
+  if (block == nullptr)
+    return {};
+
   struct symbol *sym;
   const struct block *static_block = block_static_block (block);
   const char *scope = block_scope (block);
   
-  /* Check if either no block is specified or it's a global block.  */
-
-  if (static_block == NULL)
+  /* Check if it's a global block.  */
+  if (static_block == nullptr)
     return {};
 
   while (block != static_block)
@@ -2484,6 +2486,9 @@ lookup_symbol_in_static_block (const char *name,
 			       const struct block *block,
 			       const domain_enum domain)
 {
+  if (block == nullptr)
+    return {};
+
   const struct block *static_block = block_static_block (block);
   struct symbol *sym;
 
@@ -5855,7 +5860,7 @@ default_collect_symbol_completion_matches_break_on
      visible from current context.  */
 
   b = get_selected_block (0);
-  surrounding_static_block = block_static_block (b);
+  surrounding_static_block = b == nullptr ? nullptr : block_static_block (b);
   surrounding_global_block = block_global_block (b);
   if (surrounding_static_block != NULL)
     while (b != surrounding_static_block)
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 06/27] Don't allow NULL as an argument to block_global_block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (4 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 05/27] Don't allow NULL as an argument to block_static_block Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 07/27] Convert block_objfile to method Tom Tromey
                   ` (20 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

block_global_block has special behavior when the block is NULL.
Remove this and patch up the callers instead.
---
 gdb/block.c      |  6 +-----
 gdb/cp-support.c | 15 ++++++++-------
 gdb/symtab.c     |  5 +++--
 3 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index d21729f069d..5751a6b6f84 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -378,15 +378,11 @@ block_static_block (const struct block *block)
   return block;
 }
 
-/* Return the static block associated to BLOCK.  Return NULL if block
-   is NULL.  */
+/* Return the static block associated to BLOCK.  */
 
 const struct block *
 block_global_block (const struct block *block)
 {
-  if (block == NULL)
-    return NULL;
-
   while (block->superblock () != NULL)
     block = block->superblock ();
 
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 08f7c2b4140..38225a6d9ba 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1308,14 +1308,15 @@ add_symbol_overload_list_namespace (const char *func_name,
   /* Look in the static block.  */
   block = get_selected_block (0);
   block = block == nullptr ? nullptr : block_static_block (block);
-  if (block)
-    add_symbol_overload_list_block (name, block, overload_list);
-
-  /* Look in the global block.  */
-  block = block_global_block (block);
-  if (block)
-    add_symbol_overload_list_block (name, block, overload_list);
+  if (block != nullptr)
+    {
+      add_symbol_overload_list_block (name, block, overload_list);
 
+      /* Look in the global block.  */
+      block = block_global_block (block);
+      if (block)
+	add_symbol_overload_list_block (name, block, overload_list);
+    }
 }
 
 /* Search the namespace of the given type and namespace of and public
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 0e522b2c7f9..924bd5d8ec0 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2623,7 +2623,8 @@ lookup_global_symbol (const char *name,
   /* If a block was passed in, we want to search the corresponding
      global block first.  This yields "more expected" behavior, and is
      needed to support 'FILENAME'::VARIABLE lookups.  */
-  const struct block *global_block = block_global_block (block);
+  const struct block *global_block
+    = block == nullptr ? nullptr : block_global_block (block);
   symbol *sym = NULL;
   if (global_block != nullptr)
     {
@@ -5861,7 +5862,7 @@ default_collect_symbol_completion_matches_break_on
 
   b = get_selected_block (0);
   surrounding_static_block = b == nullptr ? nullptr : block_static_block (b);
-  surrounding_global_block = block_global_block (b);
+  surrounding_global_block = b == nullptr : nullptr : block_global_block (b);
   if (surrounding_static_block != NULL)
     while (b != surrounding_static_block)
       {
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 07/27] Convert block_objfile to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (5 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 06/27] Don't allow NULL as an argument to block_global_block Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:45 ` [PATCH 08/27] Convert block_gdbarch " Tom Tromey
                   ` (19 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_objfile to be a method.  This was mostly written
by script.
---
 gdb/ada-exp.y    |  2 +-
 gdb/block.c      | 12 ++++++------
 gdb/block.h      |  8 ++++----
 gdb/cp-support.c |  2 +-
 gdb/expop.h      |  2 +-
 gdb/printcmd.c   |  2 +-
 gdb/symtab.c     | 10 +++++-----
 gdb/varobj.c     |  2 +-
 8 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index 2f466e4de0a..9e9ff8d75d4 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -1699,7 +1699,7 @@ write_var_or_type (struct parser_state *par_state,
 	    {
 	      struct objfile *objfile = nullptr;
 	      if (block != nullptr)
-		objfile = block_objfile (block);
+		objfile = block->objfile ();
 
 	      struct bound_minimal_symbol msym
 		= ada_lookup_simple_minsym (decoded_name.c_str (), objfile);
diff --git a/gdb/block.c b/gdb/block.c
index 5751a6b6f84..d7d5f0bf19e 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -40,14 +40,14 @@ struct block_namespace_info : public allocate_on_obstack
 /* See block.h.  */
 
 struct objfile *
-block_objfile (const struct block *block)
+block::objfile () const
 {
   const struct global_block *global_block;
 
-  if (block->function () != nullptr)
-    return block->function ()->objfile ();
+  if (function () != nullptr)
+    return function ()->objfile ();
 
-  global_block = (struct global_block *) block_global_block (block);
+  global_block = (struct global_block *) block_global_block (this);
   return global_block->compunit_symtab->objfile ();
 }
 
@@ -59,7 +59,7 @@ block_gdbarch (const struct block *block)
   if (block->function () != nullptr)
     return block->function ()->arch ();
 
-  return block_objfile (block)->arch ();
+  return block->objfile ()->arch ();
 }
 
 /* See block.h.  */
@@ -434,7 +434,7 @@ set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu)
 struct dynamic_prop *
 block_static_link (const struct block *block)
 {
-  struct objfile *objfile = block_objfile (block);
+  struct objfile *objfile = block->objfile ();
 
   /* Only objfile-owned blocks that materialize top function scopes can have
      static links.  */
diff --git a/gdb/block.h b/gdb/block.h
index 379359f07be..2243c9a2a52 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -204,6 +204,10 @@ struct block
       return this->ranges ()[0].start ();
   }
 
+  /* Return the objfile of this block.  */
+
+  struct objfile *objfile () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -331,10 +335,6 @@ struct blockvector
   struct block *m_blocks[1];
 };
 
-/* Return the objfile of BLOCK, which must be non-NULL.  */
-
-extern struct objfile *block_objfile (const struct block *block);
-
 /* Return the architecture of BLOCK, which must be non-NULL.  */
 
 extern struct gdbarch *block_gdbarch (const struct block *block);
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 38225a6d9ba..be4a636336d 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1466,7 +1466,7 @@ add_symbol_overload_list_qualified (const char *func_name,
      symbols which match.  */
 
   const block *block = get_selected_block (0);
-  struct objfile *current_objfile = block ? block_objfile (block) : nullptr;
+  struct objfile *current_objfile = block ? block->objfile () : nullptr;
 
   gdbarch_iterate_over_objfiles_in_search_order
     (current_objfile ? current_objfile->arch () : target_gdbarch (),
diff --git a/gdb/expop.h b/gdb/expop.h
index de74e88998f..03e8b8aae1d 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -231,7 +231,7 @@ check_objfile (struct symbol *sym, struct objfile *objfile)
 static inline bool
 check_objfile (const struct block *block, struct objfile *objfile)
 {
-  return check_objfile (block_objfile (block), objfile);
+  return check_objfile (block->objfile (), objfile);
 }
 
 static inline bool
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index 7f3551327a8..e3ee847a955 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -2355,7 +2355,7 @@ clear_dangling_display_expressions (struct objfile *objfile)
       struct objfile *bl_objf = nullptr;
       if (d->block != nullptr)
 	{
-	  bl_objf = block_objfile (d->block);
+	  bl_objf = d->block->objfile ();
 	  if (bl_objf->separate_debug_objfile_backlink != nullptr)
 	    bl_objf = bl_objf->separate_debug_objfile_backlink;
 	}
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 924bd5d8ec0..60bed9f0b4b 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -1994,7 +1994,7 @@ lookup_language_this (const struct language_defn *lang,
 
   symbol_lookup_debug_printf_v ("lookup_language_this (%s, %s (objfile %s))",
 				lang->name (), host_address_to_string (block),
-				objfile_debug_name (block_objfile (block)));
+				objfile_debug_name (block->objfile ()));
 
   while (block)
     {
@@ -2082,7 +2082,7 @@ lookup_symbol_aux (const char *name, symbol_name_match_type match_type,
   if (symbol_lookup_debug)
     {
       struct objfile *objfile = (block == nullptr
-				 ? nullptr : block_objfile (block));
+				 ? nullptr : block->objfile ());
 
       symbol_lookup_debug_printf
 	("demangled symbol name = \"%s\", block @ %s (objfile %s)",
@@ -2228,7 +2228,7 @@ lookup_symbol_in_block (const char *name, symbol_name_match_type match_type,
   if (symbol_lookup_debug)
     {
       struct objfile *objfile
-	= block == nullptr ? nullptr : block_objfile (block);
+	= block == nullptr ? nullptr : block->objfile ();
 
       symbol_lookup_debug_printf_v
 	("lookup_symbol_in_block (%s, %s (objfile %s), %s)",
@@ -2498,7 +2498,7 @@ lookup_symbol_in_static_block (const char *name,
   if (symbol_lookup_debug)
     {
       struct objfile *objfile = (block == nullptr
-				 ? nullptr : block_objfile (block));
+				 ? nullptr : block->objfile ());
 
       symbol_lookup_debug_printf
 	("lookup_symbol_in_static_block (%s, %s (objfile %s), %s)",
@@ -2638,7 +2638,7 @@ lookup_global_symbol (const char *name,
   struct objfile *objfile = nullptr;
   if (block != nullptr)
     {
-      objfile = block_objfile (block);
+      objfile = block->objfile ();
       if (objfile->separate_debug_objfile_backlink != nullptr)
 	objfile = objfile->separate_debug_objfile_backlink;
     }
diff --git a/gdb/varobj.c b/gdb/varobj.c
index eb47ecada3e..58a3ad5041a 100644
--- a/gdb/varobj.c
+++ b/gdb/varobj.c
@@ -2397,7 +2397,7 @@ varobj_invalidate_if_uses_objfile (struct objfile *objfile)
     {
       if (var->root->valid_block != nullptr)
 	{
-	  struct objfile *bl_objfile = block_objfile (var->root->valid_block);
+	  struct objfile *bl_objfile = var->root->valid_block->objfile ();
 	  if (bl_objfile->separate_debug_objfile_backlink != nullptr)
 	    bl_objfile = bl_objfile->separate_debug_objfile_backlink;
 
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 08/27] Convert block_gdbarch to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (6 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 07/27] Convert block_objfile to method Tom Tromey
@ 2023-01-20 21:45 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 09/27] Convert block_inlined_p " Tom Tromey
                   ` (18 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:45 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_gdbarch to be a method.  This was mostly written
by script.
---
 gdb/ada-lang.c     | 2 +-
 gdb/block.c        | 8 ++++----
 gdb/block.h        | 8 ++++----
 gdb/cp-namespace.c | 2 +-
 gdb/d-namespace.c  | 2 +-
 gdb/symtab.c       | 2 +-
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index dfcd9a5fcd1..8fdf5fcef5a 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13842,7 +13842,7 @@ class ada_language : public language_defn
 	if (block == NULL)
 	  gdbarch = target_gdbarch ();
 	else
-	  gdbarch = block_gdbarch (block);
+	  gdbarch = block->gdbarch ();
 	sym.symbol
 	  = language_lookup_primitive_type_as_symbol (this, gdbarch, name);
 	if (sym.symbol != NULL)
diff --git a/gdb/block.c b/gdb/block.c
index d7d5f0bf19e..b9c48e79d46 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -54,12 +54,12 @@ block::objfile () const
 /* See block.  */
 
 struct gdbarch *
-block_gdbarch (const struct block *block)
+block::gdbarch () const
 {
-  if (block->function () != nullptr)
-    return block->function ()->arch ();
+  if (function () != nullptr)
+    return function ()->arch ();
 
-  return block->objfile ()->arch ();
+  return objfile ()->arch ();
 }
 
 /* See block.h.  */
diff --git a/gdb/block.h b/gdb/block.h
index 2243c9a2a52..b9f1ba81b50 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -208,6 +208,10 @@ struct block
 
   struct objfile *objfile () const;
 
+  /* Return the architecture of this block.  */
+
+  struct gdbarch *gdbarch () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -335,10 +339,6 @@ struct blockvector
   struct block *m_blocks[1];
 };
 
-/* Return the architecture of BLOCK, which must be non-NULL.  */
-
-extern struct gdbarch *block_gdbarch (const struct block *block);
-
 extern struct symbol *block_linkage_function (const struct block *);
 
 extern struct symbol *block_containing_function (const struct block *);
diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index b1c54c57da6..97b7a653e33 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -197,7 +197,7 @@ cp_lookup_bare_symbol (const struct language_defn *langdef,
       if (block == NULL)
 	gdbarch = target_gdbarch ();
       else
-	gdbarch = block_gdbarch (block);
+	gdbarch = block->gdbarch ();
       sym.symbol
 	= language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
       sym.block = NULL;
diff --git a/gdb/d-namespace.c b/gdb/d-namespace.c
index 2978e5fecaf..aaef1dfae1f 100644
--- a/gdb/d-namespace.c
+++ b/gdb/d-namespace.c
@@ -94,7 +94,7 @@ d_lookup_symbol (const struct language_defn *langdef,
       if (block == NULL)
 	gdbarch = target_gdbarch ();
       else
-	gdbarch = block_gdbarch (block);
+	gdbarch = block->gdbarch ();
       sym.symbol
 	= language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
       sym.block = NULL;
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 60bed9f0b4b..78f56d9d377 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2468,7 +2468,7 @@ language_defn::lookup_symbol_nonlocal (const char *name,
       if (block == NULL)
 	gdbarch = target_gdbarch ();
       else
-	gdbarch = block_gdbarch (block);
+	gdbarch = block->gdbarch ();
       result.symbol = language_lookup_primitive_type_as_symbol (this,
 								gdbarch, name);
       result.block = NULL;
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 09/27] Convert block_inlined_p to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (7 preceding siblings ...)
  2023-01-20 21:45 ` [PATCH 08/27] Convert block_gdbarch " Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 10/27] Convert more block functions to methods Tom Tromey
                   ` (17 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_inlined_p to be a method.  This was mostly written
by script.
---
 gdb/block.c        | 12 ++++++------
 gdb/block.h        |  6 ++++--
 gdb/blockframe.c   |  2 +-
 gdb/findvar.c      |  2 +-
 gdb/inline-frame.c |  4 ++--
 gdb/symtab.c       |  8 ++++----
 6 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index b9c48e79d46..574086aa4f6 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -77,7 +77,7 @@ contained_in (const struct block *a, const struct block *b,
 	return true;
       /* If A is a function block, then A cannot be contained in B,
 	 except if A was inlined.  */
-      if (!allow_nested && a->function () != NULL && !block_inlined_p (a))
+      if (!allow_nested && a->function () != NULL && !a->inlined_p ())
 	return false;
       a = a->superblock ();
     }
@@ -95,7 +95,7 @@ contained_in (const struct block *a, const struct block *b,
 struct symbol *
 block_linkage_function (const struct block *bl)
 {
-  while ((bl->function () == NULL || block_inlined_p (bl))
+  while ((bl->function () == NULL || bl->inlined_p ())
 	 && bl->superblock () != NULL)
     bl = bl->superblock ();
 
@@ -116,12 +116,12 @@ block_containing_function (const struct block *bl)
   return bl->function ();
 }
 
-/* Return one if BL represents an inlined function.  */
+/* See block.h.  */
 
-int
-block_inlined_p (const struct block *bl)
+bool
+block::inlined_p () const
 {
-  return bl->function () != NULL && bl->function ()->is_inlined ();
+  return function () != nullptr && function ()->is_inlined ();
 }
 
 /* A helper function that checks whether PC is in the blockvector BL.
diff --git a/gdb/block.h b/gdb/block.h
index b9f1ba81b50..ab343b44698 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -212,6 +212,10 @@ struct block
 
   struct gdbarch *gdbarch () const;
 
+  /* Return true if BL represents an inlined function.  */
+
+  bool inlined_p () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -343,8 +347,6 @@ extern struct symbol *block_linkage_function (const struct block *);
 
 extern struct symbol *block_containing_function (const struct block *);
 
-extern int block_inlined_p (const struct block *block);
-
 /* Return true if block A is lexically nested within block B, or if a
    and b have the same pc range.  Return false otherwise.  If
    ALLOW_NESTED is true, then block A is considered to be in block B
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index 071254824b0..2796fc99154 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -72,7 +72,7 @@ get_frame_block (frame_info_ptr frame, CORE_ADDR *addr_in_block)
 
   while (inline_count > 0)
     {
-      if (block_inlined_p (bl))
+      if (bl->inlined_p ())
 	inline_count--;
 
       bl = bl->superblock ();
diff --git a/gdb/findvar.c b/gdb/findvar.c
index 02d498d8c58..656f982be2d 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -533,7 +533,7 @@ get_hosting_frame (struct symbol *var, const struct block *var_block,
       if (frame == NULL)
 	{
 	  if (var_block->function ()
-	      && !block_inlined_p (var_block)
+	      && !var_block->inlined_p ()
 	      && var_block->function ()->print_name ())
 	    error (_("No frame is currently executing in block %s."),
 		   var_block->function ()->print_name ());
diff --git a/gdb/inline-frame.c b/gdb/inline-frame.c
index f02f6353c64..21431a280fa 100644
--- a/gdb/inline-frame.c
+++ b/gdb/inline-frame.c
@@ -228,7 +228,7 @@ inline_frame_sniffer (const struct frame_unwind *self,
   cur_block = frame_block;
   while (cur_block->superblock ())
     {
-      if (block_inlined_p (cur_block))
+      if (cur_block->inlined_p ())
 	depth++;
       else if (cur_block->function () != NULL)
 	break;
@@ -357,7 +357,7 @@ skip_inline_frames (thread_info *thread, bpstat *stop_chain)
       cur_block = frame_block;
       while (cur_block->superblock ())
 	{
-	  if (block_inlined_p (cur_block))
+	  if (cur_block->inlined_p ())
 	    {
 	      /* See comments in inline_frame_this_id about this use
 		 of BLOCK_ENTRY_PC.  */
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 78f56d9d377..530de418e53 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2206,7 +2206,7 @@ lookup_local_symbol (const char *name,
 	    return blocksym;
 	}
 
-      if (block->function () != NULL && block_inlined_p (block))
+      if (block->function () != NULL && block->inlined_p ())
 	break;
       block = block->superblock ();
     }
@@ -3927,7 +3927,7 @@ skip_prologue_sal (struct symtab_and_line *sal)
   function_block = NULL;
   while (b != NULL)
     {
-      if (b->function () != NULL && block_inlined_p (b))
+      if (b->function () != NULL && b->inlined_p ())
 	function_block = b;
       else if (b->function () != NULL)
 	break;
@@ -4023,7 +4023,7 @@ skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
 	  bl = block_for_pc (prologue_sal.end);
 	  while (bl)
 	    {
-	      if (block_inlined_p (bl))
+	      if (bl->inlined_p ())
 		break;
 	      if (bl->function ())
 		{
@@ -5886,7 +5886,7 @@ default_collect_symbol_completion_matches_break_on
 	/* Stop when we encounter an enclosing function.  Do not stop for
 	   non-inlined functions - the locals of the enclosing function
 	   are in scope for a nested function.  */
-	if (b->function () != NULL && block_inlined_p (b))
+	if (b->function () != NULL && b->inlined_p ())
 	  break;
 	b = b->superblock ();
       }
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 10/27] Convert more block functions to methods
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (8 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 09/27] Convert block_inlined_p " Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 11/27] Convert block_linkage_function to method Tom Tromey
                   ` (16 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_scope, block_set_scope, block_using, and
block_set_using to be methods.  These are all done at once to make it
easier to also convert block_initialize_namespace at the same time.
This was mostly written by script.
---
 gdb/ada-lang.c     |  2 +-
 gdb/block.c        | 63 +++++++++++++++++++---------------------------
 gdb/block.h        | 48 +++++++++++++++++++++--------------
 gdb/buildsym.c     |  9 +++----
 gdb/cp-namespace.c |  6 ++---
 gdb/cp-support.c   |  2 +-
 gdb/d-namespace.c  |  4 +--
 gdb/dbxread.c      |  4 +--
 gdb/dwarf2/read.c  |  4 +--
 gdb/rust-lang.c    |  2 +-
 gdb/rust-lang.h    |  2 +-
 gdb/rust-parse.c   |  2 +-
 gdb/symtab.c       |  2 +-
 13 files changed, 74 insertions(+), 76 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 8fdf5fcef5a..7830fff6d8e 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5428,7 +5428,7 @@ ada_add_block_renamings (std::vector<struct block_symbol> &result,
   symbol_name_matcher_ftype *name_match
     = ada_get_symbol_name_matcher (lookup_name);
 
-  for (renaming = block_using (block);
+  for (renaming = block->get_using ();
        renaming != NULL;
        renaming = renaming->next)
     {
diff --git a/gdb/block.c b/gdb/block.c
index 574086aa4f6..fc523332a3d 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -285,40 +285,36 @@ block_for_pc (CORE_ADDR pc)
 /* Now come some functions designed to deal with C++ namespace issues.
    The accessors are safe to use even in the non-C++ case.  */
 
-/* This returns the namespace that BLOCK is enclosed in, or "" if it
-   isn't enclosed in a namespace at all.  This travels the chain of
-   superblocks looking for a scope, if necessary.  */
+/* See block.h.  */
 
 const char *
-block_scope (const struct block *block)
+block::scope () const
 {
-  for (; block != NULL; block = block->superblock ())
+  for (const block *block = this;
+       block != nullptr;
+       block = block->superblock ())
     {
-      if (block->namespace_info () != NULL
-	  && block->namespace_info ()->scope != NULL)
-	return block->namespace_info ()->scope;
+      if (block->m_namespace_info != nullptr
+	  && block->m_namespace_info->scope != nullptr)
+	return block->m_namespace_info->scope;
     }
 
   return "";
 }
 
-/* If block->namespace_info () is NULL, allocate it via OBSTACK and
-   initialize its members to zero.  */
+/* See block.h.  */
 
-static void
-block_initialize_namespace (struct block *block, struct obstack *obstack)
+void
+block::initialize_namespace (struct obstack *obstack)
 {
-  if (block->namespace_info () == NULL)
-    block->set_namespace_info (new (obstack) struct block_namespace_info ());
+  if (m_namespace_info == nullptr)
+    m_namespace_info = new (obstack) struct block_namespace_info;
 }
 
-/* Set BLOCK's scope member to SCOPE; if needed, allocate memory via
-   OBSTACK.  (It won't make a copy of SCOPE, however, so that already
-   has to be allocated correctly.)  */
+/* See block.h.  */
 
 void
-block_set_scope (struct block *block, const char *scope,
-		 struct obstack *obstack)
+block::set_scope (const char *scope, struct obstack *obstack)
 {
   if (scope == nullptr || scope[0] == '\0')
     {
@@ -326,31 +322,25 @@ block_set_scope (struct block *block, const char *scope,
       return;
     }
 
-  block_initialize_namespace (block, obstack);
-
-  block->namespace_info ()->scope = scope;
+  initialize_namespace (obstack);
+  m_namespace_info->scope = scope;
 }
 
-/* This returns the using directives list associated with BLOCK, if
-   any.  */
+/* See block.h.  */
 
 struct using_direct *
-block_using (const struct block *block)
+block::get_using () const
 {
-  if (block->namespace_info () == NULL)
-    return NULL;
+  if (m_namespace_info == nullptr)
+    return nullptr;
   else
-    return block->namespace_info ()->using_decl;
+    return m_namespace_info->using_decl;
 }
 
-/* Set BLOCK's using member to USING; if needed, allocate memory via
-   OBSTACK.  (It won't make a copy of USING, however, so that already
-   has to be allocated correctly.)  */
+/* See block.h.  */
 
 void
-block_set_using (struct block *block,
-		 struct using_direct *using_decl,
-		 struct obstack *obstack)
+block::set_using (struct using_direct *using_decl, struct obstack *obstack)
 {
   if (using_decl == nullptr)
     {
@@ -358,9 +348,8 @@ block_set_using (struct block *block,
       return;
     }
 
-  block_initialize_namespace (block, obstack);
-
-  block->namespace_info ()->using_decl = using_decl;
+  initialize_namespace (obstack);
+  m_namespace_info->using_decl = using_decl;
 }
 
 /* Return the static block associated to BLOCK.  Return NULL if block
diff --git a/gdb/block.h b/gdb/block.h
index ab343b44698..680b7d0161e 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -147,14 +147,6 @@ struct block
   void set_multidict (multidictionary *multidict)
   { m_multidict = multidict; }
 
-  /* Return this block's namespace info.  */
-  block_namespace_info *namespace_info () const
-  { return m_namespace_info; }
-
-  /* Set this block's namespace info.  */
-  void set_namespace_info (block_namespace_info *namespace_info)
-  { m_namespace_info = namespace_info; }
-
   /* Return a view on this block's ranges.  */
   gdb::array_view<blockrange> ranges ()
   {
@@ -216,6 +208,29 @@ struct block
 
   bool inlined_p () const;
 
+  /* This returns the namespace that this block is enclosed in, or ""
+     if it isn't enclosed in a namespace at all.  This travels the
+     chain of superblocks looking for a scope, if necessary.  */
+
+  const char *scope () const;
+
+  /* Set this block's scope member to SCOPE; if needed, allocate
+     memory via OBSTACK.  (It won't make a copy of SCOPE, however, so
+     that already has to be allocated correctly.)  */
+
+  void set_scope (const char *scope, struct obstack *obstack);
+
+  /* This returns the using directives list associated with this
+     block, if any.  */
+
+  struct using_direct *get_using () const;
+
+  /* Set this block's using member to USING; if needed, allocate
+     memory via OBSTACK.  (It won't make a copy of USING, however, so
+     that already has to be allocated correctly.)  */
+
+  void set_using (struct using_direct *using_decl, struct obstack *obstack);
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -248,6 +263,12 @@ struct block
      startaddr and endaddr above.  */
 
   struct blockranges *m_ranges;
+
+private:
+
+  /* If the namespace_info is NULL, allocate it via OBSTACK and
+     initialize its members to zero.  */
+  void initialize_namespace (struct obstack *obstack);
 };
 
 /* The global block is singled out so that we can provide a back-link
@@ -373,17 +394,6 @@ extern const struct block *block_for_pc (CORE_ADDR);
 
 extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
 
-extern const char *block_scope (const struct block *block);
-
-extern void block_set_scope (struct block *block, const char *scope,
-			     struct obstack *obstack);
-
-extern struct using_direct *block_using (const struct block *block);
-
-extern void block_set_using (struct block *block,
-			     struct using_direct *using_decl,
-			     struct obstack *obstack);
-
 extern const struct block *block_static_block (const struct block *block);
 
 extern const struct block *block_global_block (const struct block *block);
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index adab927235c..fd76e56599d 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -371,11 +371,10 @@ buildsym_compunit::finish_block_internal
       opblock = pblock;
     }
 
-  block_set_using (block,
-		   (is_global
-		    ? m_global_using_directives
-		    : m_local_using_directives),
-		   &m_objfile->objfile_obstack);
+  block->set_using ((is_global
+		     ? m_global_using_directives
+		     : m_local_using_directives),
+		    &m_objfile->objfile_obstack);
   if (is_global)
     m_global_using_directives = NULL;
   else
diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index 97b7a653e33..2d44b7bc047 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -413,7 +413,7 @@ cp_lookup_symbol_via_imports (const char *scope,
   /* Go through the using directives.  If any of them add new names to
      the namespace we're searching in, see if we can find a match by
      applying them.  */
-  for (current = block_using (block);
+  for (current = block->get_using ();
        current != NULL;
        current = current->next)
     {
@@ -769,7 +769,7 @@ cp_lookup_symbol_nonlocal (const struct language_defn *langdef,
 			   const domain_enum domain)
 {
   struct block_symbol sym;
-  const char *scope = block_scope (block);
+  const char *scope = block == nullptr ? "" : block->scope ();
 
   symbol_lookup_debug_printf
     ("cp_lookup_symbol_non_local (%s, %s (scope %s), %s)",
@@ -1025,7 +1025,7 @@ cp_lookup_transparent_type (const char *name)
 
   /* If that doesn't work and we're within a namespace, look there
      instead.  */
-  scope = block_scope (get_selected_block (0));
+  scope = get_selected_block (0)->scope ();
 
   if (scope[0] == '\0')
     return NULL;
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index be4a636336d..76407fdeb2f 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1402,7 +1402,7 @@ add_symbol_overload_list_using (const char *func_name,
   for (block = get_selected_block (0);
        block != NULL;
        block = block->superblock ())
-    for (current = block_using (block);
+    for (current = block->get_using ();
 	current != NULL;
 	current = current->next)
       {
diff --git a/gdb/d-namespace.c b/gdb/d-namespace.c
index aaef1dfae1f..6153e5b3239 100644
--- a/gdb/d-namespace.c
+++ b/gdb/d-namespace.c
@@ -375,7 +375,7 @@ d_lookup_symbol_imports (const char *scope, const char *name,
      the module we're searching in, see if we can find a match by
      applying them.  */
 
-  for (current = block_using (block);
+  for (current = block->get_using ();
        current != NULL;
        current = current->next)
     {
@@ -511,7 +511,7 @@ d_lookup_symbol_nonlocal (const struct language_defn *langdef,
 			  const domain_enum domain)
 {
   struct block_symbol sym;
-  const char *scope = block == nullptr ? "" : block_scope (block);
+  const char *scope = block == nullptr ? "" : block->scope ();
 
   sym = lookup_module_scope (langdef, name, block, domain, scope, 0);
   if (sym.symbol != NULL)
diff --git a/gdb/dbxread.c b/gdb/dbxread.c
index ab0734f0218..dbfbf9a6574 100644
--- a/gdb/dbxread.c
+++ b/gdb/dbxread.c
@@ -2362,8 +2362,8 @@ cp_set_block_scope (const struct symbol *symbol,
       const char *name = symbol->demangled_name ();
       unsigned int prefix_len = cp_entire_prefix_len (name);
 
-      block_set_scope (block, obstack_strndup (obstack, name, prefix_len),
-		       obstack);
+      block->set_scope (obstack_strndup (obstack, name, prefix_len),
+			obstack);
     }
 }
 
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index cd937f24ee7..9789f871eb9 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -12195,8 +12195,8 @@ read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
        || cu->lang () == language_d
        || cu->lang () == language_rust)
       && cu->processing_has_namespace_info)
-    block_set_scope (block, determine_prefix (die, cu),
-		     &objfile->objfile_obstack);
+    block->set_scope (determine_prefix (die, cu),
+		      &objfile->objfile_obstack);
 
   /* If we have address ranges, record them.  */
   dwarf2_record_block_ranges (die, block, baseaddr, cu);
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 6653f7a9c64..24fb514831f 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -58,7 +58,7 @@ rust_last_path_segment (const char *path)
 std::string
 rust_crate_for_block (const struct block *block)
 {
-  const char *scope = block_scope (block);
+  const char *scope = block->scope ();
 
   if (scope[0] == '\0')
     return std::string ();
diff --git a/gdb/rust-lang.h b/gdb/rust-lang.h
index 497342d4ef3..37a22e7a404 100644
--- a/gdb/rust-lang.h
+++ b/gdb/rust-lang.h
@@ -148,7 +148,7 @@ class rust_language : public language_defn
   {
     struct block_symbol result = {};
 
-    const char *scope = block == nullptr ? "" : block_scope (block);
+    const char *scope = block == nullptr ? "" : block->scope ();
     symbol_lookup_debug_printf
       ("rust_lookup_symbol_non_local (%s, %s (scope %s), %s)",
        name, host_address_to_string (block), scope,
diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 72b843ef40c..9074d651d85 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -375,7 +375,7 @@ rust_parser::super_name (const std::string &ident, unsigned int n_supers)
 {
   const char *scope = "";
   if (pstate->expression_context_block != nullptr)
-    scope = block_scope (pstate->expression_context_block);
+    scope = pstate->expression_context_block->scope ();
   int offset;
 
   if (scope[0] == '\0')
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 530de418e53..71b9ebe9efc 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2184,7 +2184,7 @@ lookup_local_symbol (const char *name,
 
   struct symbol *sym;
   const struct block *static_block = block_static_block (block);
-  const char *scope = block_scope (block);
+  const char *scope = block->scope ();
   
   /* Check if it's a global block.  */
   if (static_block == nullptr)
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 11/27] Convert block_linkage_function to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (9 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 10/27] Convert more block functions to methods Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 12/27] Convert block_containing_function " Tom Tromey
                   ` (15 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_linkage_function to be a method.  This was mostly
written by script.
---
 gdb/ada-lang.c              |  2 +-
 gdb/ax-gdb.c                |  2 +-
 gdb/block.c                 | 10 ++++------
 gdb/block.h                 |  9 +++++++--
 gdb/blockframe.c            |  4 ++--
 gdb/breakpoint.c            |  2 +-
 gdb/compile/compile-loc2c.c |  2 +-
 gdb/dwarf2/expr.c           |  2 +-
 gdb/dwarf2/loc.c            |  6 +++---
 gdb/parse.c                 |  2 +-
 10 files changed, 22 insertions(+), 19 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 7830fff6d8e..2602700e0ce 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -5309,7 +5309,7 @@ remove_irrelevant_renamings (std::vector<struct block_symbol> *syms,
   if (current_block == NULL)
     return;
 
-  current_function = block_linkage_function (current_block);
+  current_function = current_block->linkage_function ();
   if (current_function == NULL)
     return;
 
diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c
index fc214e7dfe1..f4f9b150c46 100644
--- a/gdb/ax-gdb.c
+++ b/gdb/ax-gdb.c
@@ -1887,7 +1887,7 @@ op_this_operation::do_generate_ax (struct expression *exp,
   const struct language_defn *lang;
 
   b = block_for_pc (ax->scope);
-  func = block_linkage_function (b);
+  func = b->linkage_function ();
   lang = language_def (func->language ());
 
   sym = lookup_language_this (lang, b).symbol;
diff --git a/gdb/block.c b/gdb/block.c
index fc523332a3d..dca6ff36f3b 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -86,15 +86,13 @@ contained_in (const struct block *a, const struct block *b,
   return false;
 }
 
-
-/* Return the symbol for the function which contains a specified
-   lexical block, described by a struct block BL.  The return value
-   will not be an inlined function; the containing function will be
-   returned instead.  */
+/* See block.h.  */
 
 struct symbol *
-block_linkage_function (const struct block *bl)
+block::linkage_function () const
 {
+  const block *bl = this;
+
   while ((bl->function () == NULL || bl->inlined_p ())
 	 && bl->superblock () != NULL)
     bl = bl->superblock ();
diff --git a/gdb/block.h b/gdb/block.h
index 680b7d0161e..d1f4409d863 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -231,6 +231,13 @@ struct block
 
   void set_using (struct using_direct *using_decl, struct obstack *obstack);
 
+  /* Return the symbol for the function which contains a specified
+     lexical block, described by a struct block.  The return value
+     will not be an inlined function; the containing function will be
+     returned instead.  */
+
+  struct symbol *linkage_function () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -364,8 +371,6 @@ struct blockvector
   struct block *m_blocks[1];
 };
 
-extern struct symbol *block_linkage_function (const struct block *);
-
 extern struct symbol *block_containing_function (const struct block *);
 
 /* Return true if block A is lexically nested within block B, or if a
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index 2796fc99154..65183b1946b 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -91,7 +91,7 @@ get_pc_function_start (CORE_ADDR pc)
   bl = block_for_pc (pc);
   if (bl)
     {
-      struct symbol *symbol = block_linkage_function (bl);
+      struct symbol *symbol = bl->linkage_function ();
 
       if (symbol)
 	{
@@ -139,7 +139,7 @@ find_pc_sect_function (CORE_ADDR pc, struct obj_section *section)
 
   if (b == 0)
     return 0;
-  return block_linkage_function (b);
+  return b->linkage_function ();
 }
 
 /* Return the function containing pc value PC.
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 00cc2ab401c..475a16e8d73 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -9194,7 +9194,7 @@ resolve_sal_pc (struct symtab_and_line *sal)
 				    sal->symtab->compunit ());
       if (bv != NULL)
 	{
-	  sym = block_linkage_function (b);
+	  sym = b->linkage_function ();
 	  if (sym != NULL)
 	    {
 	      fixup_symbol_section (sym, sal->symtab->compunit ()->objfile ());
diff --git a/gdb/compile/compile-loc2c.c b/gdb/compile/compile-loc2c.c
index 517cf89b9d2..853535258dc 100644
--- a/gdb/compile/compile-loc2c.c
+++ b/gdb/compile/compile-loc2c.c
@@ -892,7 +892,7 @@ do_compile_dwarf_expr_to_c (int indent, string_file *stream,
 	    if (!b)
 	      error (_("No block found for address"));
 
-	    framefunc = block_linkage_function (b);
+	    framefunc = b->linkage_function ();
 
 	    if (!framefunc)
 	      error (_("No function found for block"));
diff --git a/gdb/dwarf2/expr.c b/gdb/dwarf2/expr.c
index eecd18f59c9..5d597bfa9a6 100644
--- a/gdb/dwarf2/expr.c
+++ b/gdb/dwarf2/expr.c
@@ -771,7 +771,7 @@ dwarf_expr_context::get_frame_base (const gdb_byte **start,
   /* Use block_linkage_function, which returns a real (not inlined)
      function, instead of get_frame_function, which may return an
      inlined function.  */
-  symbol *framefunc = block_linkage_function (bl);
+  symbol *framefunc = bl->linkage_function ();
 
   /* If we found a frame-relative symbol then it was certainly within
      some function associated with a frame. If we can't find the frame,
diff --git a/gdb/dwarf2/loc.c b/gdb/dwarf2/loc.c
index fe91d609f19..92003c39d0e 100644
--- a/gdb/dwarf2/loc.c
+++ b/gdb/dwarf2/loc.c
@@ -451,7 +451,7 @@ dwarf2_find_location_expression (struct dwarf2_loclist_baton *baton,
 	  struct symbol *pc_func = NULL;
 
 	  if (pc_block)
-	    pc_func = block_linkage_function (pc_block);
+	    pc_func = pc_block->linkage_function ();
 
 	  if (pc_func && pc == pc_func->value_block ()->entry_pc ())
 	    {
@@ -2647,7 +2647,7 @@ dwarf2_compile_expr_to_ax (struct agent_expr *expr, struct axs_value *loc,
 	    if (!b)
 	      error (_("No block found for address"));
 
-	    framefunc = block_linkage_function (b);
+	    framefunc = b->linkage_function ();
 
 	    if (!framefunc)
 	      error (_("No function found for block"));
@@ -3173,7 +3173,7 @@ locexpr_describe_location_piece (struct symbol *symbol, struct ui_file *stream,
 	error (_("No block found for address for symbol \"%s\"."),
 	       symbol->print_name ());
 
-      framefunc = block_linkage_function (b);
+      framefunc = b->linkage_function ();
 
       if (!framefunc)
 	error (_("No function found for block for symbol \"%s\"."),
diff --git a/gdb/parse.c b/gdb/parse.c
index 2f7d58061ab..4c20b91f238 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -488,7 +488,7 @@ parse_exp_in_context (const char **stringptr, CORE_ADDR pc,
 	 the current frame language to parse the expression.  That's why
 	 we do the following language detection only if the context block
 	 has been specifically provided.  */
-      struct symbol *func = block_linkage_function (block);
+      struct symbol *func = block->linkage_function ();
 
       if (func != NULL)
 	lang = language_def (func->language ());
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 12/27] Convert block_containing_function to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (10 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 11/27] Convert block_linkage_function to method Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 13/27] Convert block_static_block and block_global_block to methods Tom Tromey
                   ` (14 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_containing_function to be a method.  This was
mostly written by script.
---
 gdb/block.c      | 9 ++++-----
 gdb/block.h      | 9 +++++++--
 gdb/blockframe.c | 2 +-
 gdb/linespec.c   | 2 +-
 4 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index dca6ff36f3b..31916ce48dc 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -100,14 +100,13 @@ block::linkage_function () const
   return bl->function ();
 }
 
-/* Return the symbol for the function which contains a specified
-   block, described by a struct block BL.  The return value will be
-   the closest enclosing function, which might be an inline
-   function.  */
+/* See block.h.  */
 
 struct symbol *
-block_containing_function (const struct block *bl)
+block::containing_function () const
 {
+  const block *bl = this;
+
   while (bl->function () == NULL && bl->superblock () != NULL)
     bl = bl->superblock ();
 
diff --git a/gdb/block.h b/gdb/block.h
index d1f4409d863..9f1ba935abb 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -238,6 +238,13 @@ struct block
 
   struct symbol *linkage_function () const;
 
+  /* Return the symbol for the function which contains a specified
+     block, described by a struct block.  The return value will be the
+     closest enclosing function, which might be an inline
+     function.  */
+
+  struct symbol *containing_function () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -371,8 +378,6 @@ struct blockvector
   struct block *m_blocks[1];
 };
 
-extern struct symbol *block_containing_function (const struct block *);
-
 /* Return true if block A is lexically nested within block B, or if a
    and b have the same pc range.  Return false otherwise.  If
    ALLOW_NESTED is true, then block A is considered to be in block B
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index 65183b1946b..7c9faf487a7 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -162,7 +162,7 @@ find_pc_sect_containing_function (CORE_ADDR pc, struct obj_section *section)
   if (bl == nullptr)
     return nullptr;
 
-  return block_containing_function (bl);
+  return bl->containing_function ();
 }
 
 /* These variables are used to cache the most recent result of
diff --git a/gdb/linespec.c b/gdb/linespec.c
index d3def7ae070..61da545eb2e 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -2128,7 +2128,7 @@ create_sals_line_offset (struct linespec_state *self,
 	if (filter[i])
 	  {
 	    struct symbol *sym = (blocks[i]
-				  ? block_containing_function (blocks[i])
+				  ? blocks[i]->containing_function ()
 				  : NULL);
 
 	    if (self->funfirstline)
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 13/27] Convert block_static_block and block_global_block to methods
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (11 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 12/27] Convert block_containing_function " Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 14/27] Convert set_block_compunit_symtab to method Tom Tromey
                   ` (13 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_static_block and block_global_block to be methods.
This was mostly written by script.  It was simpler to convert them at
the same time because they're often used near each other.
---
 gdb/ada-lang.c                      |  2 +-
 gdb/block.c                         | 18 ++++++++++--------
 gdb/block.h                         | 13 +++++++++----
 gdb/compile/compile-c-symbols.c     |  6 +++---
 gdb/compile/compile-cplus-symbols.c |  4 ++--
 gdb/cp-namespace.c                  |  2 +-
 gdb/cp-support.c                    |  8 ++++----
 gdb/findvar.c                       |  6 +++---
 gdb/guile/scm-block.c               |  4 ++--
 gdb/python/py-block.c               |  4 ++--
 gdb/rust-parse.c                    |  2 +-
 gdb/symtab.c                        | 10 +++++-----
 12 files changed, 43 insertions(+), 36 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 2602700e0ce..57676f0de95 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -13818,7 +13818,7 @@ class ada_language : public language_defn
     sym = ada_lookup_symbol (name,
 			     (block == nullptr
 			      ? nullptr
-			      : block_static_block (block)),
+			      : block->static_block ()),
 			     domain);
     if (sym.symbol != NULL)
       return sym;
diff --git a/gdb/block.c b/gdb/block.c
index 31916ce48dc..c0d6ed7da97 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -47,7 +47,7 @@ block::objfile () const
   if (function () != nullptr)
     return function ()->objfile ();
 
-  global_block = (struct global_block *) block_global_block (this);
+  global_block = (struct global_block *) this->global_block ();
   return global_block->compunit_symtab->objfile ();
 }
 
@@ -349,26 +349,28 @@ block::set_using (struct using_direct *using_decl, struct obstack *obstack)
   m_namespace_info->using_decl = using_decl;
 }
 
-/* Return the static block associated to BLOCK.  Return NULL if block
-   is a global block.  */
+/* See block.h.  */
 
 const struct block *
-block_static_block (const struct block *block)
+block::static_block () const
 {
-  if (block->superblock () == NULL)
-    return NULL;
+  if (superblock () == nullptr)
+    return nullptr;
 
+  const block *block = this;
   while (block->superblock ()->superblock () != NULL)
     block = block->superblock ();
 
   return block;
 }
 
-/* Return the static block associated to BLOCK.  */
+/* See block.h.  */
 
 const struct block *
-block_global_block (const struct block *block)
+block::global_block () const
 {
+  const block *block = this;
+
   while (block->superblock () != NULL)
     block = block->superblock ();
 
diff --git a/gdb/block.h b/gdb/block.h
index 9f1ba935abb..9ce8ef51945 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -245,6 +245,15 @@ struct block
 
   struct symbol *containing_function () const;
 
+  /* Return the static block associated with this block.  Return NULL
+     if block is a global block.  */
+
+  const struct block *static_block () const;
+
+  /* Return the static block associated with block.  */
+
+  const struct block *global_block () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -404,10 +413,6 @@ extern const struct block *block_for_pc (CORE_ADDR);
 
 extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
 
-extern const struct block *block_static_block (const struct block *block);
-
-extern const struct block *block_global_block (const struct block *block);
-
 extern struct block *allocate_block (struct obstack *obstack);
 
 extern struct block *allocate_global_block (struct obstack *obstack);
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index 79490b811c3..678cbcd5c44 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -228,7 +228,7 @@ convert_symbol_sym (compile_c_instance *context, const char *identifier,
 
   const struct block *static_block = nullptr;
   if (sym.block != nullptr)
-    static_block = block_static_block (sym.block);
+    static_block = sym.block->static_block ();
   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
   is_local_symbol = (sym.block != static_block && static_block != NULL);
   if (is_local_symbol)
@@ -239,7 +239,7 @@ convert_symbol_sym (compile_c_instance *context, const char *identifier,
       /* If the outer symbol is in the static block, we ignore it, as
 	 it cannot be referenced.  */
       if (global_sym.symbol != NULL
-	  && global_sym.block != block_static_block (global_sym.block))
+	  && global_sym.block != global_sym.block->static_block ())
 	{
 	  if (compile_debug)
 	    gdb_printf (gdb_stdlog,
@@ -617,7 +617,7 @@ generate_c_for_variable_locations (compile_instance *compiler,
   if (block == nullptr)
     return {};
 
-  const struct block *static_block = block_static_block (block);
+  const struct block *static_block = block->static_block ();
 
   /* If we're already in the static or global block, there is nothing
      to write.  */
diff --git a/gdb/compile/compile-cplus-symbols.c b/gdb/compile/compile-cplus-symbols.c
index 45a76eef303..eff04406fb5 100644
--- a/gdb/compile/compile-cplus-symbols.c
+++ b/gdb/compile/compile-cplus-symbols.c
@@ -241,7 +241,7 @@ convert_symbol_sym (compile_cplus_instance *instance,
 
   const struct block *static_block = nullptr;
   if (sym.block != nullptr)
-    static_block = block_static_block (sym.block);
+    static_block = sym.block->static_block ();
   /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block.  */
   bool is_local_symbol = (sym.block != static_block && static_block != nullptr);
   if (is_local_symbol)
@@ -252,7 +252,7 @@ convert_symbol_sym (compile_cplus_instance *instance,
       /* If the outer symbol is in the static block, we ignore it, as
 	 it cannot be referenced.  */
       if (global_sym.symbol != nullptr
-	  && global_sym.block != block_static_block (global_sym.block))
+	  && global_sym.block != global_sym.block->static_block ())
 	{
 	  if (compile_debug)
 	    gdb_printf (gdb_stdlog,
diff --git a/gdb/cp-namespace.c b/gdb/cp-namespace.c
index 2d44b7bc047..14d807694b7 100644
--- a/gdb/cp-namespace.c
+++ b/gdb/cp-namespace.c
@@ -141,7 +141,7 @@ cp_basic_lookup_symbol (const char *name, const struct block *block,
 	 but should be treated as local to a single file nonetheless.
 	 So we only search the current file's global block.  */
 
-      const struct block *global_block = block_global_block (block);
+      const struct block *global_block = block->global_block ();
 
       if (global_block != NULL)
 	{
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index 76407fdeb2f..cdaf5fd3e8e 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1307,13 +1307,13 @@ add_symbol_overload_list_namespace (const char *func_name,
 
   /* Look in the static block.  */
   block = get_selected_block (0);
-  block = block == nullptr ? nullptr : block_static_block (block);
+  block = block == nullptr ? nullptr : block->static_block ();
   if (block != nullptr)
     {
       add_symbol_overload_list_block (name, block, overload_list);
 
       /* Look in the global block.  */
-      block = block_global_block (block);
+      block = block->global_block ();
       if (block)
 	add_symbol_overload_list_block (name, block, overload_list);
     }
@@ -1457,10 +1457,10 @@ add_symbol_overload_list_qualified (const char *func_name,
        b = b->superblock ())
     add_symbol_overload_list_block (func_name, b, overload_list);
 
-  surrounding_static_block = block_static_block (get_selected_block (0));
+  surrounding_static_block = get_selected_block (0);
   surrounding_static_block = (surrounding_static_block == nullptr
 			      ? nullptr
-			      : block_static_block (surrounding_static_block));
+			      : surrounding_static_block->static_block ());
 
   /* Go through the symtabs and check the externs and statics for
      symbols which match.  */
diff --git a/gdb/findvar.c b/gdb/findvar.c
index 656f982be2d..586ceb9dc72 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -460,8 +460,8 @@ get_hosting_frame (struct symbol *var, const struct block *var_block,
      tests that embed global/static symbols with null location lists.
      We want to get <optimized out> instead of <frame required> when evaluating
      them so return a frame instead of raising an error.  */
-  else if (var_block == block_global_block (var_block)
-	   || var_block == block_static_block (var_block))
+  else if (var_block == var_block->global_block ()
+	   || var_block == var_block->static_block ())
     return frame;
 
   /* We have to handle the "my_func::my_local_var" notation.  This requires us
@@ -486,7 +486,7 @@ get_hosting_frame (struct symbol *var, const struct block *var_block,
 
       /* If we failed to find the proper frame, fallback to the heuristic
 	 method below.  */
-      else if (frame_block == block_global_block (frame_block))
+      else if (frame_block == frame_block->global_block ())
 	{
 	  frame = NULL;
 	  break;
diff --git a/gdb/guile/scm-block.c b/gdb/guile/scm-block.c
index 02cd3ff9a43..c9d847a4d60 100644
--- a/gdb/guile/scm-block.c
+++ b/gdb/guile/scm-block.c
@@ -438,7 +438,7 @@ gdbscm_block_global_block (SCM self)
   const struct block *block = b_smob->block;
   const struct block *global_block;
 
-  global_block = block_global_block (block);
+  global_block = block->global_block ();
 
   return bkscm_scm_from_block (global_block, b_smob->objfile);
 }
@@ -458,7 +458,7 @@ gdbscm_block_static_block (SCM self)
   if (block->superblock () == NULL)
     return SCM_BOOL_F;
 
-  static_block = block_static_block (block);
+  static_block = block->static_block ();
 
   return bkscm_scm_from_block (static_block, b_smob->objfile);
 }
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index c399a01c4fb..bd2ce7fc181 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -188,7 +188,7 @@ blpy_get_global_block (PyObject *self, void *closure)
 
   BLPY_REQUIRE_VALID (self, block);
 
-  global_block = block_global_block (block);
+  global_block = block->global_block ();
 
   return block_to_block_object (global_block,
 				self_obj->objfile);
@@ -210,7 +210,7 @@ blpy_get_static_block (PyObject *self, void *closure)
   if (block->superblock () == NULL)
     Py_RETURN_NONE;
 
-  static_block = block_static_block (block);
+  static_block = block->static_block ();
 
   return block_to_block_object (static_block, self_obj->objfile);
 }
diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index 9074d651d85..545d0141183 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -421,7 +421,7 @@ munge_name_and_block (const char **name, const struct block **block)
   if (startswith (*name, "::"))
     {
       *name += 2;
-      *block = block_static_block (*block);
+      *block = (*block)->static_block ();
     }
 }
 
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 71b9ebe9efc..dd9cc7d5163 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2183,7 +2183,7 @@ lookup_local_symbol (const char *name,
     return {};
 
   struct symbol *sym;
-  const struct block *static_block = block_static_block (block);
+  const struct block *static_block = block->static_block ();
   const char *scope = block->scope ();
   
   /* Check if it's a global block.  */
@@ -2489,7 +2489,7 @@ lookup_symbol_in_static_block (const char *name,
   if (block == nullptr)
     return {};
 
-  const struct block *static_block = block_static_block (block);
+  const struct block *static_block = block->static_block ();
   struct symbol *sym;
 
   if (static_block == NULL)
@@ -2624,7 +2624,7 @@ lookup_global_symbol (const char *name,
      global block first.  This yields "more expected" behavior, and is
      needed to support 'FILENAME'::VARIABLE lookups.  */
   const struct block *global_block
-    = block == nullptr ? nullptr : block_global_block (block);
+    = block == nullptr ? nullptr : block->global_block ();
   symbol *sym = NULL;
   if (global_block != nullptr)
     {
@@ -5861,8 +5861,8 @@ default_collect_symbol_completion_matches_break_on
      visible from current context.  */
 
   b = get_selected_block (0);
-  surrounding_static_block = b == nullptr ? nullptr : block_static_block (b);
-  surrounding_global_block = b == nullptr : nullptr : block_global_block (b);
+  surrounding_static_block = b == nullptr ? nullptr : b->static_block ();
+  surrounding_global_block = b == nullptr ? nullptr : b->global_block ();
   if (surrounding_static_block != NULL)
     while (b != surrounding_static_block)
       {
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 14/27] Convert set_block_compunit_symtab to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (12 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 13/27] Convert block_static_block and block_global_block to methods Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 15/27] Convert block_static_link " Tom Tromey
                   ` (12 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts set_block_compunit_symtab to be a method.  This was
mostly written by script.
---
 gdb/block.c    | 8 ++++----
 gdb/block.h    | 7 ++++---
 gdb/buildsym.c | 2 +-
 gdb/jit.c      | 2 +-
 4 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index c0d6ed7da97..3cd90dcb4ed 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -404,15 +404,15 @@ allocate_global_block (struct obstack *obstack)
   return &bl->block;
 }
 
-/* Set the compunit of the global block.  */
+/* See block.h.  */
 
 void
-set_block_compunit_symtab (struct block *block, struct compunit_symtab *cu)
+block::set_compunit_symtab (struct compunit_symtab *cu)
 {
   struct global_block *gb;
 
-  gdb_assert (block->superblock () == NULL);
-  gb = (struct global_block *) block;
+  gdb_assert (superblock () == NULL);
+  gb = (struct global_block *) this;
   gdb_assert (gb->compunit_symtab == NULL);
   gb->compunit_symtab = cu;
 }
diff --git a/gdb/block.h b/gdb/block.h
index 9ce8ef51945..ce8c9679a07 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -254,6 +254,10 @@ struct block
 
   const struct block *global_block () const;
 
+  /* Set the compunit of this block, which must be a global block.  */
+
+  void set_compunit_symtab (struct compunit_symtab *);
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -417,9 +421,6 @@ extern struct block *allocate_block (struct obstack *obstack);
 
 extern struct block *allocate_global_block (struct obstack *obstack);
 
-extern void set_block_compunit_symtab (struct block *,
-				       struct compunit_symtab *);
-
 /* Return a property to evaluate the static link associated to BLOCK.
 
    In the context of nested functions (available in Pascal, Ada and GNU C, for
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index fd76e56599d..7a009c2f626 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -970,7 +970,7 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
   {
     struct block *b = blockvector->global_block ();
 
-    set_block_compunit_symtab (b, cu);
+    b->set_compunit_symtab (cu);
   }
 
   cu->set_block_line_section (section);
diff --git a/gdb/jit.c b/gdb/jit.c
index 48bfef0a026..f584438c229 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -623,7 +623,7 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
       bv->set_block (i, new_block);
 
       if (i == GLOBAL_BLOCK)
-	set_block_compunit_symtab (new_block, cust);
+	new_block->set_compunit_symtab (cust);
     }
 
   /* Fill up the superblock fields for the real blocks, using the
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 15/27] Convert block_static_link to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (13 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 14/27] Convert set_block_compunit_symtab to method Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 16/27] Store 'name' in block_iterator Tom Tromey
                   ` (11 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts block_static_link to be a method.  This was mostly
written by script.
---
 gdb/block.c   |  8 ++++----
 gdb/block.h   | 24 +++++++++++++-----------
 gdb/findvar.c |  2 +-
 3 files changed, 18 insertions(+), 16 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 3cd90dcb4ed..8aa0e676894 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -420,16 +420,16 @@ block::set_compunit_symtab (struct compunit_symtab *cu)
 /* See block.h.  */
 
 struct dynamic_prop *
-block_static_link (const struct block *block)
+block::static_link () const
 {
-  struct objfile *objfile = block->objfile ();
+  struct objfile *objfile = this->objfile ();
 
   /* Only objfile-owned blocks that materialize top function scopes can have
      static links.  */
-  if (objfile == NULL || block->function () == NULL)
+  if (objfile == NULL || function () == NULL)
     return NULL;
 
-  return (struct dynamic_prop *) objfile_lookup_static_link (objfile, block);
+  return (struct dynamic_prop *) objfile_lookup_static_link (objfile, this);
 }
 
 /* Return the compunit of the global block.  */
diff --git a/gdb/block.h b/gdb/block.h
index ce8c9679a07..9bc80490d7c 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -258,6 +258,19 @@ struct block
 
   void set_compunit_symtab (struct compunit_symtab *);
 
+  /* Return a property to evaluate the static link associated to this
+     block.
+
+     In the context of nested functions (available in Pascal, Ada and
+     GNU C, for instance), a static link (as in DWARF's
+     DW_AT_static_link attribute) for a function is a way to get the
+     frame corresponding to the enclosing function.
+
+     Note that only objfile-owned and function-level blocks can have a
+     static link.  Return NULL if there is no such property.  */
+
+  struct dynamic_prop *static_link () const;
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start;
@@ -421,17 +434,6 @@ extern struct block *allocate_block (struct obstack *obstack);
 
 extern struct block *allocate_global_block (struct obstack *obstack);
 
-/* Return a property to evaluate the static link associated to BLOCK.
-
-   In the context of nested functions (available in Pascal, Ada and GNU C, for
-   instance), a static link (as in DWARF's DW_AT_static_link attribute) for a
-   function is a way to get the frame corresponding to the enclosing function.
-
-   Note that only objfile-owned and function-level blocks can have a static
-   link.  Return NULL if there is no such property.  */
-
-extern struct dynamic_prop *block_static_link (const struct block *block);
-
 /* A block iterator.  This structure should be treated as though it
    were opaque; it is only defined here because we want to support
    stack allocation of iterators.  */
diff --git a/gdb/findvar.c b/gdb/findvar.c
index 586ceb9dc72..8564047290e 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -498,7 +498,7 @@ get_hosting_frame (struct symbol *var, const struct block *var_block,
       else if (frame_block->function ())
 	{
 	  const struct dynamic_prop *static_link
-	    = block_static_link (frame_block);
+	    = frame_block->static_link ();
 	  int could_climb_up = 0;
 
 	  if (static_link != NULL)
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 16/27] Store 'name' in block_iterator
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (14 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 15/27] Convert block_static_link " Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 17/27] Combine both styles of block iterator Tom Tromey
                   ` (10 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes the block_iterator to store the 'name' that is used by
block_iter_match_next.  This avoids any problem where the name could
be passed inconsistently, and also makes the subsequent patches easier
to understand.
---
 gdb/ada-lang.c |  2 +-
 gdb/block.c    | 22 ++++++++++++----------
 gdb/block.h    | 10 ++++++----
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 57676f0de95..c04d75590fb 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6087,7 +6087,7 @@ ada_add_block_symbols (std::vector<struct block_symbol> &result,
   found_sym = false;
   for (sym = block_iter_match_first (block, lookup_name, &iter);
        sym != NULL;
-       sym = block_iter_match_next (lookup_name, &iter))
+       sym = block_iter_match_next (&iter))
     {
       if (symbol_matches_domain (sym->language (), sym->domain (), domain))
 	{
diff --git a/gdb/block.c b/gdb/block.c
index 8aa0e676894..e56c95013b8 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -453,12 +453,14 @@ get_block_compunit_symtab (const struct block *block)
 
 static void
 initialize_block_iterator (const struct block *block,
-			   struct block_iterator *iter)
+			   struct block_iterator *iter,
+			   const lookup_name_info *name = nullptr)
 {
   enum block_enum which;
   struct compunit_symtab *cu;
 
   iter->idx = -1;
+  iter->name = name;
 
   if (block->superblock () == NULL)
     {
@@ -585,7 +587,6 @@ block_iterator_next (struct block_iterator *iterator)
 
 static struct symbol *
 block_iter_match_step (struct block_iterator *iterator,
-		       const lookup_name_info &name,
 		       int first)
 {
   struct symbol *sym;
@@ -605,11 +606,11 @@ block_iter_match_step (struct block_iterator *iterator,
 	    return  NULL;
 
 	  block = cust->blockvector ()->block (iterator->which);
-	  sym = mdict_iter_match_first (block->multidict (), name,
+	  sym = mdict_iter_match_first (block->multidict (), *iterator->name,
 					&iterator->mdict_iter);
 	}
       else
-	sym = mdict_iter_match_next (name, &iterator->mdict_iter);
+	sym = mdict_iter_match_next (*iterator->name, &iterator->mdict_iter);
 
       if (sym != NULL)
 	return sym;
@@ -629,25 +630,26 @@ block_iter_match_first (const struct block *block,
 			const lookup_name_info &name,
 			struct block_iterator *iterator)
 {
-  initialize_block_iterator (block, iterator);
+  initialize_block_iterator (block, iterator, &name);
 
   if (iterator->which == FIRST_LOCAL_BLOCK)
     return mdict_iter_match_first (block->multidict (), name,
 				   &iterator->mdict_iter);
 
-  return block_iter_match_step (iterator, name, 1);
+  return block_iter_match_step (iterator, 1);
 }
 
 /* See block.h.  */
 
 struct symbol *
-block_iter_match_next (const lookup_name_info &name,
-		       struct block_iterator *iterator)
+block_iter_match_next (struct block_iterator *iterator)
 {
+  gdb_assert (iterator->name != nullptr);
+
   if (iterator->which == FIRST_LOCAL_BLOCK)
-    return mdict_iter_match_next (name, &iterator->mdict_iter);
+    return mdict_iter_match_next (*iterator->name, &iterator->mdict_iter);
 
-  return block_iter_match_step (iterator, name, 0);
+  return block_iter_match_step (iterator, 0);
 }
 
 /* See block.h.  */
diff --git a/gdb/block.h b/gdb/block.h
index 9bc80490d7c..5fc41c6df02 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -449,6 +449,9 @@ struct block_iterator
     const struct block *block;
   } d;
 
+  /* If we're trying to match a name, this will be non-NULL.  */
+  const lookup_name_info *name;
+
   /* If we're iterating over a single block, this is always -1.
      Otherwise, it holds the index of the current "included" symtab in
      the canonical symtab (that is, d.symtab->includes[idx]), with -1
@@ -493,10 +496,9 @@ extern struct symbol *block_iter_match_first (const struct block *block,
    symbols.  Don't call this if you've previously received NULL from
    block_iterator_match_first or block_iterator_match_next on this
    iteration.  And don't call it unless ITERATOR was created by a
-   previous call to block_iter_match_first with the same NAME.  */
+   previous call to block_iter_match_first.  */
 
-extern struct symbol *block_iter_match_next
-  (const lookup_name_info &name, struct block_iterator *iterator);
+extern struct symbol *block_iter_match_next (struct block_iterator *iterator);
 
 /* Return true if symbol A is the best match possible for DOMAIN.  */
 
@@ -574,7 +576,7 @@ extern int block_find_non_opaque_type_preferred (struct symbol *sym,
 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym)		\
   for ((sym) = block_iter_match_first ((block), (name), &(iter));	\
        (sym) != NULL;							\
-       (sym) = block_iter_match_next ((name), &(iter)))
+       (sym) = block_iter_match_next (&(iter)))
 
 /* Given a vector of pairs, allocate and build an obstack allocated
    blockranges struct for a block.  */
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 17/27] Combine both styles of block iterator
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (15 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 16/27] Store 'name' in block_iterator Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 18/27] Introduce a block iterator wrapper Tom Tromey
                   ` (9 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This merges the two styles of block iterator, having the
initialization API decide which to use based on an optional parameter.
---
 gdb/ada-lang.c |  4 ++--
 gdb/block.c    | 54 ++++++++++++++++++++------------------------------
 gdb/block.h    | 30 ++++++++--------------------
 3 files changed, 32 insertions(+), 56 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index c04d75590fb..4d2873cfde4 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6085,9 +6085,9 @@ ada_add_block_symbols (std::vector<struct block_symbol> &result,
 
   arg_sym = NULL;
   found_sym = false;
-  for (sym = block_iter_match_first (block, lookup_name, &iter);
+  for (sym = block_iterator_first (block, &iter, &lookup_name);
        sym != NULL;
-       sym = block_iter_match_next (&iter))
+       sym = block_iterator_next (&iter))
     {
       if (symbol_matches_domain (sym->language (), sym->domain (), domain))
 	{
diff --git a/gdb/block.c b/gdb/block.c
index e56c95013b8..e7a51af8cea 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -556,31 +556,6 @@ block_iterator_step (struct block_iterator *iterator, int first)
     }
 }
 
-/* See block.h.  */
-
-struct symbol *
-block_iterator_first (const struct block *block,
-		      struct block_iterator *iterator)
-{
-  initialize_block_iterator (block, iterator);
-
-  if (iterator->which == FIRST_LOCAL_BLOCK)
-    return mdict_iterator_first (block->multidict (), &iterator->mdict_iter);
-
-  return block_iterator_step (iterator, 1);
-}
-
-/* See block.h.  */
-
-struct symbol *
-block_iterator_next (struct block_iterator *iterator)
-{
-  if (iterator->which == FIRST_LOCAL_BLOCK)
-    return mdict_iterator_next (&iterator->mdict_iter);
-
-  return block_iterator_step (iterator, 0);
-}
-
 /* Perform a single step for a "match" block iterator, iterating
    across symbol tables as needed.  Returns the next symbol, or NULL
    when iteration is complete.  */
@@ -626,14 +601,23 @@ block_iter_match_step (struct block_iterator *iterator,
 /* See block.h.  */
 
 struct symbol *
-block_iter_match_first (const struct block *block,
-			const lookup_name_info &name,
-			struct block_iterator *iterator)
+block_iterator_first (const struct block *block,
+		      struct block_iterator *iterator,
+		      const lookup_name_info *name)
 {
-  initialize_block_iterator (block, iterator, &name);
+  initialize_block_iterator (block, iterator, name);
+
+  if (name == nullptr)
+    {
+      if (iterator->which == FIRST_LOCAL_BLOCK)
+	return mdict_iterator_first (block->multidict (),
+				     &iterator->mdict_iter);
+
+      return block_iterator_step (iterator, 1);
+    }
 
   if (iterator->which == FIRST_LOCAL_BLOCK)
-    return mdict_iter_match_first (block->multidict (), name,
+    return mdict_iter_match_first (block->multidict (), *name,
 				   &iterator->mdict_iter);
 
   return block_iter_match_step (iterator, 1);
@@ -642,9 +626,15 @@ block_iter_match_first (const struct block *block,
 /* See block.h.  */
 
 struct symbol *
-block_iter_match_next (struct block_iterator *iterator)
+block_iterator_next (struct block_iterator *iterator)
 {
-  gdb_assert (iterator->name != nullptr);
+  if (iterator->name == nullptr)
+    {
+      if (iterator->which == FIRST_LOCAL_BLOCK)
+	return mdict_iterator_next (&iterator->mdict_iter);
+
+      return block_iterator_step (iterator, 0);
+    }
 
   if (iterator->which == FIRST_LOCAL_BLOCK)
     return mdict_iter_match_next (*iterator->name, &iterator->mdict_iter);
diff --git a/gdb/block.h b/gdb/block.h
index 5fc41c6df02..03aeebddc6b 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -471,10 +471,13 @@ struct block_iterator
 };
 
 /* Initialize ITERATOR to point at the first symbol in BLOCK, and
-   return that first symbol, or NULL if BLOCK is empty.  */
+   return that first symbol, or NULL if BLOCK is empty.  If NAME is
+   not NULL, only return symbols matching that name.  */
 
-extern struct symbol *block_iterator_first (const struct block *block,
-					    struct block_iterator *iterator);
+extern struct symbol *block_iterator_first
+     (const struct block *block,
+      struct block_iterator *iterator,
+      const lookup_name_info *name = nullptr);
 
 /* Advance ITERATOR, and return the next symbol, or NULL if there are
    no more symbols.  Don't call this if you've previously received
@@ -483,23 +486,6 @@ extern struct symbol *block_iterator_first (const struct block *block,
 
 extern struct symbol *block_iterator_next (struct block_iterator *iterator);
 
-/* Initialize ITERATOR to point at the first symbol in BLOCK whose
-   search_name () matches NAME, and return that first symbol, or
-   NULL if there are no such symbols.  */
-
-extern struct symbol *block_iter_match_first (const struct block *block,
-					      const lookup_name_info &name,
-					      struct block_iterator *iterator);
-
-/* Advance ITERATOR to point at the next symbol in BLOCK whose
-   search_name () matches NAME, or NULL if there are no more such
-   symbols.  Don't call this if you've previously received NULL from
-   block_iterator_match_first or block_iterator_match_next on this
-   iteration.  And don't call it unless ITERATOR was created by a
-   previous call to block_iter_match_first.  */
-
-extern struct symbol *block_iter_match_next (struct block_iterator *iterator);
-
 /* Return true if symbol A is the best match possible for DOMAIN.  */
 
 extern bool best_symbol (struct symbol *a, const domain_enum domain);
@@ -574,9 +560,9 @@ extern int block_find_non_opaque_type_preferred (struct symbol *sym,
    current symbol.  */
 
 #define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym)		\
-  for ((sym) = block_iter_match_first ((block), (name), &(iter));	\
+  for ((sym) = block_iterator_first ((block), &(iter), &(name));	\
        (sym) != NULL;							\
-       (sym) = block_iter_match_next (&(iter)))
+       (sym) = block_iterator_next (&(iter)))
 
 /* Given a vector of pairs, allocate and build an obstack allocated
    blockranges struct for a block.  */
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 18/27] Introduce a block iterator wrapper
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (16 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 17/27] Combine both styles of block iterator Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 19/27] Convert explicit iterator uses to foreach Tom Tromey
                   ` (8 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This introduces a C++-style iterator that wraps the existing
block_iterator.  It also adds a range adapter.  These will be used in
a later patch.
---
 gdb/block.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/gdb/block.h b/gdb/block.h
index 03aeebddc6b..7341d03a07e 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -486,6 +486,56 @@ extern struct symbol *block_iterator_first
 
 extern struct symbol *block_iterator_next (struct block_iterator *iterator);
 
+/* An iterator that wraps a block_iterator.  The naming here is
+   unfortunate, but block_iterator was named before gdb switched to
+   C++.  */
+struct block_iterator_wrapper
+{
+  typedef block_iterator_wrapper self_type;
+  typedef struct symbol *value_type;
+
+  explicit block_iterator_wrapper (const struct block *block,
+				   const lookup_name_info *name = nullptr)
+    : m_sym (block_iterator_first (block, &m_iter, name))
+  {
+  }
+
+  block_iterator_wrapper ()
+    : m_sym (nullptr)
+  {
+  }
+
+  value_type operator* () const
+  {
+    return m_sym;
+  }
+
+  bool operator== (const self_type &other) const
+  {
+    return m_sym == other.m_sym;
+  }
+
+  bool operator!= (const self_type &other) const
+  {
+    return m_sym != other.m_sym;
+  }
+
+  self_type &operator++ ()
+  {
+    m_sym = block_iterator_next (&m_iter);
+    return *this;
+  }
+
+private:
+
+  struct symbol *m_sym;
+  struct block_iterator m_iter;
+};
+
+/* An iterator range for block_iterator_wrapper.  */
+
+typedef iterator_range<block_iterator_wrapper> block_iterator_range;
+
 /* Return true if symbol A is the best match possible for DOMAIN.  */
 
 extern bool best_symbol (struct symbol *a, const domain_enum domain);
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 19/27] Convert explicit iterator uses to foreach
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (17 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 18/27] Introduce a block iterator wrapper Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 20/27] Remove ALL_BLOCK_SYMBOLS_WITH_NAME Tom Tromey
                   ` (7 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts most existing explicit uses of block_iterator to use
foreach with the range iterator instead.
---
 gdb/ada-lang.c                  | 8 +++-----
 gdb/compile/compile-c-symbols.c | 7 +------
 gdb/guile/scm-block.c           | 7 +------
 3 files changed, 5 insertions(+), 17 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 4d2873cfde4..b5e9f2ecff6 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6076,18 +6076,14 @@ ada_add_block_symbols (std::vector<struct block_symbol> &result,
 		       const lookup_name_info &lookup_name,
 		       domain_enum domain, struct objfile *objfile)
 {
-  struct block_iterator iter;
   /* A matching argument symbol, if any.  */
   struct symbol *arg_sym;
   /* Set true when we find a matching non-argument symbol.  */
   bool found_sym;
-  struct symbol *sym;
 
   arg_sym = NULL;
   found_sym = false;
-  for (sym = block_iterator_first (block, &iter, &lookup_name);
-       sym != NULL;
-       sym = block_iterator_next (&iter))
+  for (struct symbol *sym : block_iterator_range (block, &lookup_name))
     {
       if (symbol_matches_domain (sym->language (), sym->domain (), domain))
 	{
@@ -6125,6 +6121,8 @@ ada_add_block_symbols (std::vector<struct block_symbol> &result,
       const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
       const char *name = ada_lookup_name.c_str ();
       size_t name_len = ada_lookup_name.size ();
+      struct symbol *sym;
+      struct block_iterator iter;
 
       ALL_BLOCK_SYMBOLS (block, iter, sym)
       {
diff --git a/gdb/compile/compile-c-symbols.c b/gdb/compile/compile-c-symbols.c
index 678cbcd5c44..e7d565bcd77 100644
--- a/gdb/compile/compile-c-symbols.c
+++ b/gdb/compile/compile-c-symbols.c
@@ -633,14 +633,9 @@ generate_c_for_variable_locations (compile_instance *compiler,
 
   while (1)
     {
-      struct symbol *sym;
-      struct block_iterator iter;
-
       /* Iterate over symbols in this block, generating code to
 	 compute the location of each local variable.  */
-      for (sym = block_iterator_first (block, &iter);
-	   sym != NULL;
-	   sym = block_iterator_next (&iter))
+      for (struct symbol *sym : block_iterator_range (block))
 	{
 	  if (!symbol_seen (symhash.get (), sym))
 	    generate_c_for_for_one_variable (compiler, stream, gdbarch,
diff --git a/gdb/guile/scm-block.c b/gdb/guile/scm-block.c
index c9d847a4d60..02b7a48be37 100644
--- a/gdb/guile/scm-block.c
+++ b/gdb/guile/scm-block.c
@@ -501,20 +501,15 @@ gdbscm_block_symbols (SCM self)
   block_smob *b_smob
     = bkscm_get_valid_block_smob_arg_unsafe (self, SCM_ARG1, FUNC_NAME);
   const struct block *block = b_smob->block;
-  struct block_iterator iter;
-  struct symbol *sym;
   SCM result;
 
   result = SCM_EOL;
 
-  sym = block_iterator_first (block, &iter);
-
-  while (sym != NULL)
+  for (struct symbol *sym : block_iterator_range (block))
     {
       SCM s_scm = syscm_scm_from_symbol (sym);
 
       result = scm_cons (s_scm, result);
-      sym = block_iterator_next (&iter);
     }
 
   return scm_reverse_x (result, SCM_EOL);
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 20/27] Remove ALL_BLOCK_SYMBOLS_WITH_NAME
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (18 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 19/27] Convert explicit iterator uses to foreach Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 21/27] Remove ALL_BLOCK_SYMBOLS Tom Tromey
                   ` (6 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes ALL_BLOCK_SYMBOLS_WITH_NAME in favor of foreach.
---
 gdb/block.c           | 12 +++---------
 gdb/block.h           | 10 ----------
 gdb/cp-support.c      |  5 +----
 gdb/python/py-block.c | 22 ++++++++--------------
 gdb/symtab.c          |  5 +----
 5 files changed, 13 insertions(+), 41 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index e7a51af8cea..0870793dcd4 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -692,16 +692,13 @@ block_lookup_symbol (const struct block *block, const char *name,
 		     symbol_name_match_type match_type,
 		     const domain_enum domain)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
-
   lookup_name_info lookup_name (name, match_type);
 
   if (!block->function ())
     {
       struct symbol *other = NULL;
 
-      ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block, &lookup_name))
 	{
 	  /* See comment related to PR gcc/debug/91507 in
 	     block_lookup_symbol_primary.  */
@@ -730,7 +727,7 @@ block_lookup_symbol (const struct block *block, const char *name,
 
       struct symbol *sym_found = NULL;
 
-      ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block, &lookup_name))
 	{
 	  if (symbol_matches_domain (sym->language (),
 				     sym->domain (), domain))
@@ -815,16 +812,13 @@ block_find_symbol (const struct block *block, const char *name,
 		   const domain_enum domain,
 		   block_symbol_matcher_ftype *matcher, void *data)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
-
   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
 
   /* Verify BLOCK is STATIC_BLOCK or GLOBAL_BLOCK.  */
   gdb_assert (block->superblock () == NULL
 	      || block->superblock ()->superblock () == NULL);
 
-  ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
+  for (struct symbol *sym : block_iterator_range (block, &lookup_name))
     {
       /* MATCHER is deliberately called second here so that it never sees
 	 a non-domain-matching symbol.  */
diff --git a/gdb/block.h b/gdb/block.h
index 7341d03a07e..35c41377f2c 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -604,16 +604,6 @@ extern int block_find_non_opaque_type_preferred (struct symbol *sym,
        (sym);						\
        (sym) = block_iterator_next (&(iter)))
 
-/* Macro to loop through all symbols in BLOCK with a name that matches
-   NAME, in no particular order.  ITER helps keep track of the
-   iteration, and must be a struct block_iterator.  SYM points to the
-   current symbol.  */
-
-#define ALL_BLOCK_SYMBOLS_WITH_NAME(block, name, iter, sym)		\
-  for ((sym) = block_iterator_first ((block), &(iter), &(name));	\
-       (sym) != NULL;							\
-       (sym) = block_iterator_next (&(iter)))
-
 /* Given a vector of pairs, allocate and build an obstack allocated
    blockranges struct for a block.  */
 struct blockranges *make_blockranges (struct objfile *objfile,
diff --git a/gdb/cp-support.c b/gdb/cp-support.c
index cdaf5fd3e8e..52510543f00 100644
--- a/gdb/cp-support.c
+++ b/gdb/cp-support.c
@@ -1273,12 +1273,9 @@ add_symbol_overload_list_block (const char *name,
 				const struct block *block,
 				std::vector<symbol *> *overload_list)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
-
   lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
 
-  ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
+  for (struct symbol *sym : block_iterator_range (block, &lookup_name))
     overload_list_add_symbol (sym, name, overload_list);
 }
 
diff --git a/gdb/python/py-block.c b/gdb/python/py-block.c
index bd2ce7fc181..da33d4cdd94 100644
--- a/gdb/python/py-block.c
+++ b/gdb/python/py-block.c
@@ -265,24 +265,18 @@ blpy_getitem (PyObject *self, PyObject *key)
 
   lookup_name_info lookup_name (name.get(), symbol_name_match_type::FULL);
 
-  /* We use ALL_BLOCK_SYMBOLS_WITH_NAME instead of block_lookup_symbol so
-     that we can look up symbols irrespective of the domain, matching the
-     iterator. It would be confusing if the iterator returns symbols you
-     can't find via getitem.  */
-  struct block_iterator iter;
-  struct symbol *sym = nullptr;
-  ALL_BLOCK_SYMBOLS_WITH_NAME (block, lookup_name, iter, sym)
+  /* We use an iterator instead of block_lookup_symbol so that we can
+     look up symbols irrespective of the domain, matching the
+     iterator. It would be confusing if the iterator returns symbols
+     you can't find via getitem.  */
+  for (struct symbol *sym : block_iterator_range (block, &lookup_name))
     {
       /* Just stop at the first match */
-      break;
+      return symbol_to_symbol_object (sym);
     }
 
-  if (sym == nullptr)
-    {
-      PyErr_SetObject (PyExc_KeyError, key);
-      return nullptr;
-    }
-  return symbol_to_symbol_object (sym);
+  PyErr_SetObject (PyExc_KeyError, key);
+  return nullptr;
 }
 
 static void
diff --git a/gdb/symtab.c b/gdb/symtab.c
index dd9cc7d5163..511299233ad 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2797,10 +2797,7 @@ iterate_over_symbols (const struct block *block,
 		      const domain_enum domain,
 		      gdb::function_view<symbol_found_callback_ftype> callback)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
-
-  ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
+  for (struct symbol *sym : block_iterator_range (block, &name))
     {
       if (symbol_matches_domain (sym->language (), sym->domain (), domain))
 	{
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 21/27] Remove ALL_BLOCK_SYMBOLS
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (19 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 20/27] Remove ALL_BLOCK_SYMBOLS_WITH_NAME Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 22/27] Fix memory leak in mdebugread.c Tom Tromey
                   ` (5 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes ALL_BLOCK_SYMBOLS in favor of foreach.
---
 gdb/ada-lang.c        | 21 ++++++---------------
 gdb/block.h           |  9 ---------
 gdb/buildsym.c        |  2 +-
 gdb/coffread.c        |  5 +----
 gdb/f-valprint.c      |  4 +---
 gdb/infrun.c          |  4 +---
 gdb/linespec.c        |  4 +---
 gdb/mdebugread.c      |  9 ++-------
 gdb/mi/mi-cmd-stack.c |  4 +---
 gdb/stack.c           | 19 ++++++-------------
 gdb/symtab.c          | 34 ++++++++++++++--------------------
 gdb/tracepoint.c      |  4 +---
 12 files changed, 35 insertions(+), 84 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index b5e9f2ecff6..137925db81b 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -6121,10 +6121,8 @@ ada_add_block_symbols (std::vector<struct block_symbol> &result,
       const std::string &ada_lookup_name = lookup_name.ada ().lookup_name ();
       const char *name = ada_lookup_name.c_str ();
       size_t name_len = ada_lookup_name.size ();
-      struct symbol *sym;
-      struct block_iterator iter;
 
-      ALL_BLOCK_SYMBOLS (block, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block))
       {
 	if (symbol_matches_domain (sym->language (),
 				   sym->domain (), domain))
@@ -13069,10 +13067,7 @@ ada_add_exceptions_from_frame (compiled_regex *preg,
 
   while (block != 0)
     {
-      struct block_iterator iter;
-      struct symbol *sym;
-
-      ALL_BLOCK_SYMBOLS (block, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block))
 	{
 	  switch (sym->aclass ())
 	    {
@@ -13154,10 +13149,8 @@ ada_add_global_exceptions (compiled_regex *preg,
 	  for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
 	    {
 	      const struct block *b = bv->block (i);
-	      struct block_iterator iter;
-	      struct symbol *sym;
 
-	      ALL_BLOCK_SYMBOLS (b, iter, sym)
+	      for (struct symbol *sym : block_iterator_range (b))
 		if (ada_is_non_standard_exception_sym (sym)
 		    && name_matches_regex (sym->natural_name (), preg))
 		  {
@@ -13659,9 +13652,7 @@ class ada_language : public language_defn
 					  const char *text, const char *word,
 					  enum type_code code) const override
   {
-    struct symbol *sym;
     const struct block *b, *surrounding_static_block = 0;
-    struct block_iterator iter;
 
     gdb_assert (code == TYPE_CODE_UNDEF);
 
@@ -13721,7 +13712,7 @@ class ada_language : public language_defn
 	if (!b->superblock ())
 	  surrounding_static_block = b;   /* For elmin of dups */
 
-	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	for (struct symbol *sym : block_iterator_range (b))
 	  {
 	    if (completion_skip_symbol (mode, sym))
 	      continue;
@@ -13742,7 +13733,7 @@ class ada_language : public language_defn
 	  {
 	    QUIT;
 	    b = s->blockvector ()->global_block ();
-	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	    for (struct symbol *sym : block_iterator_range (b))
 	      {
 		if (completion_skip_symbol (mode, sym))
 		  continue;
@@ -13764,7 +13755,7 @@ class ada_language : public language_defn
 	    /* Don't do this block twice.  */
 	    if (b == surrounding_static_block)
 	      continue;
-	    ALL_BLOCK_SYMBOLS (b, iter, sym)
+	    for (struct symbol *sym : block_iterator_range (b))
 	      {
 		if (completion_skip_symbol (mode, sym))
 		  continue;
diff --git a/gdb/block.h b/gdb/block.h
index 35c41377f2c..6ccf3766d8f 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -595,15 +595,6 @@ extern int block_find_non_opaque_type (struct symbol *sym, void *data);
 extern int block_find_non_opaque_type_preferred (struct symbol *sym,
 						 void *data);
 
-/* Macro to loop through all symbols in BLOCK, in no particular
-   order.  ITER helps keep track of the iteration, and must be a
-   struct block_iterator.  SYM points to the current symbol.  */
-
-#define ALL_BLOCK_SYMBOLS(block, iter, sym)		\
-  for ((sym) = block_iterator_first ((block), &(iter));	\
-       (sym);						\
-       (sym) = block_iterator_next (&(iter)))
-
 /* Given a vector of pairs, allocate and build an obstack allocated
    blockranges struct for a block.  */
 struct blockranges *make_blockranges (struct objfile *objfile,
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 7a009c2f626..9d45938f8e9 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -998,7 +998,7 @@ buildsym_compunit::end_compunit_symtab_with_blockvector
 
 	/* Note that we only want to fix up symbols from the local
 	   blocks, not blocks coming from included symtabs.  That is why
-	   we use ALL_DICT_SYMBOLS here and not ALL_BLOCK_SYMBOLS.  */
+	   we use ALL_DICT_SYMBOLS here and not a block iterator.  */
 	ALL_DICT_SYMBOLS (block->multidict (), miter, sym)
 	  if (sym->symtab () == NULL)
 	    sym->set_symtab (symtab);
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 4950a7324c8..69c9772f429 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1495,12 +1495,9 @@ patch_type (struct type *type, struct type *real_type)
 static void
 patch_opaque_types (struct symtab *s)
 {
-  struct block_iterator iter;
-  struct symbol *real_sym;
-
   /* Go through the per-file symbols only.  */
   const struct block *b = s->compunit ()->blockvector ()->static_block ();
-  ALL_BLOCK_SYMBOLS (b, iter, real_sym)
+  for (struct symbol *real_sym : block_iterator_range (b))
     {
       /* Find completed typedefs to use to fix opaque ones.
 	 Remove syms from the chain when their types are stored,
diff --git a/gdb/f-valprint.c b/gdb/f-valprint.c
index 55dbcc8309f..fe0ee288a8e 100644
--- a/gdb/f-valprint.c
+++ b/gdb/f-valprint.c
@@ -600,13 +600,11 @@ static void
 info_common_command_for_block (const struct block *block, const char *comname,
 			       int *any_printed)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
   struct value_print_options opts;
 
   get_user_print_options (&opts);
 
-  ALL_BLOCK_SYMBOLS (block, iter, sym)
+  for (struct symbol *sym : block_iterator_range (block))
     if (sym->domain () == COMMON_BLOCK_DOMAIN)
       {
 	const struct common_block *common = sym->value_common_block ();
diff --git a/gdb/infrun.c b/gdb/infrun.c
index edfb5ab0a91..42fc95fb0ed 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -8069,8 +8069,6 @@ check_exception_resume (struct execution_control_state *ecs,
   try
     {
       const struct block *b;
-      struct block_iterator iter;
-      struct symbol *sym;
       int argno = 0;
 
       /* The exception breakpoint is a thread-specific breakpoint on
@@ -8088,7 +8086,7 @@ check_exception_resume (struct execution_control_state *ecs,
 	 handler.  */
 
       b = func->value_block ();
-      ALL_BLOCK_SYMBOLS (b, iter, sym)
+      for (struct symbol *sym : block_iterator_range (b))
 	{
 	  if (!sym->is_argument ())
 	    continue;
diff --git a/gdb/linespec.c b/gdb/linespec.c
index 61da545eb2e..bf1e5ee9727 100644
--- a/gdb/linespec.c
+++ b/gdb/linespec.c
@@ -3900,14 +3900,12 @@ find_label_symbols_in_block (const struct block *block,
 {
   if (completion_mode)
     {
-      struct block_iterator iter;
-      struct symbol *sym;
       size_t name_len = strlen (name);
 
       int (*cmp) (const char *, const char *, size_t);
       cmp = case_sensitivity == case_sensitive_on ? strncmp : strncasecmp;
 
-      ALL_BLOCK_SYMBOLS (block, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block))
 	{
 	  if (symbol_matches_domain (sym->language (),
 				     sym->domain (), LABEL_DOMAIN)
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 8dc836a0f6e..7fa25ae3183 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -1183,19 +1183,16 @@ parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 		 type.  Set that from the type of the parameter symbols.  */
 	      int nparams = top_stack->numargs;
 	      int iparams;
-	      struct symbol *sym;
 
 	      if (nparams > 0)
 		{
-		  struct block_iterator iter;
-
 		  ftype->set_num_fields (nparams);
 		  ftype->set_fields
 		    ((struct field *)
 		     TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
 
 		  iparams = 0;
-		  ALL_BLOCK_SYMBOLS (cblock, iter, sym)
+		  for (struct symbol *sym : block_iterator_range (cblock))
 		    {
 		      if (iparams == nparams)
 			break;
@@ -4455,12 +4452,10 @@ static struct symbol *
 mylookup_symbol (const char *name, const struct block *block,
 		 domain_enum domain, enum address_class theclass)
 {
-  struct block_iterator iter;
   int inc;
-  struct symbol *sym;
 
   inc = name[0];
-  ALL_BLOCK_SYMBOLS (block, iter, sym)
+  for (struct symbol *sym : block_iterator_range (block))
     {
       if (sym->linkage_name ()[0] == inc
 	  && sym->domain () == domain
diff --git a/gdb/mi/mi-cmd-stack.c b/gdb/mi/mi-cmd-stack.c
index 21bcf73ed24..30322b6fc56 100644
--- a/gdb/mi/mi-cmd-stack.c
+++ b/gdb/mi/mi-cmd-stack.c
@@ -571,8 +571,6 @@ list_args_or_locals (const frame_print_options &fp_opts,
 		     frame_info_ptr fi, int skip_unavailable)
 {
   const struct block *block;
-  struct symbol *sym;
-  struct block_iterator iter;
   struct type *type;
   const char *name_of_result;
   struct ui_out *uiout = current_uiout;
@@ -598,7 +596,7 @@ list_args_or_locals (const frame_print_options &fp_opts,
 
   while (block != 0)
     {
-      ALL_BLOCK_SYMBOLS (block, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block))
 	{
 	  int print_me = 0;
 
diff --git a/gdb/stack.c b/gdb/stack.c
index 0fd797836dc..0ffb1b512e1 100644
--- a/gdb/stack.c
+++ b/gdb/stack.c
@@ -758,10 +758,8 @@ print_frame_args (const frame_print_options &fp_opts,
   if (func)
     {
       const struct block *b = func->value_block ();
-      struct block_iterator iter;
-      struct symbol *sym;
 
-      ALL_BLOCK_SYMBOLS (b, iter, sym)
+      for (struct symbol *sym : block_iterator_range (b))
 	{
 	  struct frame_arg arg, entryarg;
 
@@ -2237,10 +2235,7 @@ static void
 iterate_over_block_locals (const struct block *b,
 			   iterate_over_block_arg_local_vars_cb cb)
 {
-  struct block_iterator iter;
-  struct symbol *sym;
-
-  ALL_BLOCK_SYMBOLS (b, iter, sym)
+  for (struct symbol *sym : block_iterator_range (b))
     {
       switch (sym->aclass ())
 	{
@@ -2486,10 +2481,7 @@ void
 iterate_over_block_arg_vars (const struct block *b,
 			     iterate_over_block_arg_local_vars_cb cb)
 {
-  struct block_iterator iter;
-  struct symbol *sym, *sym2;
-
-  ALL_BLOCK_SYMBOLS (b, iter, sym)
+  for (struct symbol *sym : block_iterator_range (b))
     {
       /* Don't worry about things which aren't arguments.  */
       if (sym->is_argument ())
@@ -2505,8 +2497,9 @@ iterate_over_block_arg_vars (const struct block *b,
 	     float).  There are also LOC_ARG/LOC_REGISTER pairs which
 	     are not combined in symbol-reading.  */
 
-	  sym2 = lookup_symbol_search_name (sym->search_name (),
-					    b, VAR_DOMAIN).symbol;
+	  struct symbol *sym2
+	    = lookup_symbol_search_name (sym->search_name (),
+					 b, VAR_DOMAIN).symbol;
 	  cb (sym->print_name (), sym2);
 	}
     }
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 511299233ad..3ab26f3a003 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -2906,23 +2906,25 @@ find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
 
 	  if (section != 0)
 	    {
-	      struct symbol *sym = NULL;
-	      struct block_iterator iter;
+	      struct symbol *found_sym = nullptr;
 
 	      for (int b_index = GLOBAL_BLOCK;
-		   b_index <= STATIC_BLOCK && sym == NULL;
+		   b_index <= STATIC_BLOCK && found_sym == nullptr;
 		   ++b_index)
 		{
 		  const struct block *b = bv->block (b_index);
-		  ALL_BLOCK_SYMBOLS (b, iter, sym)
+		  for (struct symbol *sym : block_iterator_range (b))
 		    {
 		      fixup_symbol_section (sym, obj_file);
 		      if (matching_obj_sections (sym->obj_section (obj_file),
 						 section))
-			break;
+			{
+			  found_sym = sym;
+			  break;
+			}
 		    }
 		}
-	      if (sym == NULL)
+	      if (found_sym == nullptr)
 		continue;		/* No symbol in this symtab matches
 					   section.  */
 	    }
@@ -2973,10 +2975,8 @@ find_symbol_at_address (CORE_ADDR address)
       for (int i = GLOBAL_BLOCK; i <= STATIC_BLOCK; ++i)
 	{
 	  const struct block *b = bv->block (i);
-	  struct block_iterator iter;
-	  struct symbol *sym;
 
-	  ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  for (struct symbol *sym : block_iterator_range (b))
 	    {
 	      if (sym->aclass () == LOC_STATIC
 		  && sym->value_address () == addr)
@@ -4739,11 +4739,9 @@ global_symbol_searcher::add_matching_symbols
 
       for (block_enum block : { GLOBAL_BLOCK, STATIC_BLOCK })
 	{
-	  struct block_iterator iter;
-	  struct symbol *sym;
 	  const struct block *b = bv->block (block);
 
-	  ALL_BLOCK_SYMBOLS (b, iter, sym)
+	  for (struct symbol *sym : block_iterator_range (b))
 	    {
 	      struct symtab *real_symtab = sym->symtab ();
 
@@ -5703,8 +5701,6 @@ add_symtab_completions (struct compunit_symtab *cust,
 			const char *text, const char *word,
 			enum type_code code)
 {
-  struct symbol *sym;
-  struct block_iterator iter;
   int i;
 
   if (cust == NULL)
@@ -5715,7 +5711,7 @@ add_symtab_completions (struct compunit_symtab *cust,
       QUIT;
 
       const struct block *b = cust->blockvector ()->block (i);
-      ALL_BLOCK_SYMBOLS (b, iter, sym)
+      for (struct symbol *sym : block_iterator_range (b))
 	{
 	  if (completion_skip_symbol (mode, sym))
 	    continue;
@@ -5741,10 +5737,8 @@ default_collect_symbol_completion_matches_break_on
      frees them.  I'm not going to worry about this; hopefully there
      won't be that many.  */
 
-  struct symbol *sym;
   const struct block *b;
   const struct block *surrounding_static_block, *surrounding_global_block;
-  struct block_iterator iter;
   /* The symbol we are completing on.  Points in same buffer as text.  */
   const char *sym_text;
 
@@ -5865,7 +5859,7 @@ default_collect_symbol_completion_matches_break_on
       {
 	QUIT;
 
-	ALL_BLOCK_SYMBOLS (b, iter, sym)
+	for (struct symbol *sym : block_iterator_range (b))
 	  {
 	    if (code == TYPE_CODE_UNDEF)
 	      {
@@ -5893,12 +5887,12 @@ default_collect_symbol_completion_matches_break_on
   if (code == TYPE_CODE_UNDEF)
     {
       if (surrounding_static_block != NULL)
-	ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
+	for (struct symbol *sym : block_iterator_range (surrounding_static_block))
 	  completion_list_add_fields (tracker, sym, lookup_name,
 				      sym_text, word);
 
       if (surrounding_global_block != NULL)
-	ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
+	for (struct symbol *sym : block_iterator_range (surrounding_global_block))
 	  completion_list_add_fields (tracker, sym, lookup_name,
 				      sym_text, word);
     }
diff --git a/gdb/tracepoint.c b/gdb/tracepoint.c
index 7e172dfb3fc..ad16b1e9d42 100644
--- a/gdb/tracepoint.c
+++ b/gdb/tracepoint.c
@@ -2459,12 +2459,10 @@ tfind_outside_command (const char *args, int from_tty)
 static void
 info_scope_command (const char *args_in, int from_tty)
 {
-  struct symbol *sym;
   struct bound_minimal_symbol msym;
   const struct block *block;
   const char *symname;
   const char *save_args = args_in;
-  struct block_iterator iter;
   int j, count = 0;
   struct gdbarch *gdbarch;
   int regno;
@@ -2492,7 +2490,7 @@ info_scope_command (const char *args_in, int from_tty)
   while (block != 0)
     {
       QUIT;			/* Allow user to bail out with ^C.  */
-      ALL_BLOCK_SYMBOLS (block, iter, sym)
+      for (struct symbol *sym : block_iterator_range (block))
 	{
 	  QUIT;			/* Allow user to bail out with ^C.  */
 	  if (count == 0)
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 22/27] Fix memory leak in mdebugread.c
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (20 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 21/27] Remove ALL_BLOCK_SYMBOLS Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 23/27] Use 'new' for block and global_block Tom Tromey
                   ` (4 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

mdebugread.c allocates blocks on the heap.  However, this is a memory
leak if the corresponding objfile is ever destroyed.

This patch changes this code to use allocate_block instead, fixing a
FIXME from 2003.

I don't know how to test this patch.
---
 gdb/mdebugread.c | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 7fa25ae3183..b6a0b3c07df 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -236,7 +236,8 @@ static struct type *new_type (char *);
 
 enum block_type { FUNCTION_BLOCK, NON_FUNCTION_BLOCK };
 
-static struct block *new_block (enum block_type, enum language);
+static struct block *new_block (struct objfile *objfile,
+				enum block_type, enum language);
 
 static struct compunit_symtab *new_symtab (const char *, int, struct objfile *);
 
@@ -794,7 +795,7 @@ parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 	s->type ()->set_is_prototyped (true);
 
       /* Create and enter a new lexical context.  */
-      b = new_block (FUNCTION_BLOCK, s->language ());
+      b = new_block (objfile, FUNCTION_BLOCK, s->language ());
       s->set_value_block (b);
       b->set_function (s);
       b->set_start (sh->value);
@@ -1125,7 +1126,7 @@ parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 	}
 
       top_stack->blocktype = stBlock;
-      b = new_block (NON_FUNCTION_BLOCK, psymtab_language);
+      b = new_block (objfile, NON_FUNCTION_BLOCK, psymtab_language);
       b->set_start (sh->value + top_stack->procadr);
       b->set_superblock (top_stack->cur_block);
       top_stack->cur_block = b;
@@ -4622,8 +4623,8 @@ new_symtab (const char *name, int maxlines, struct objfile *objfile)
 
   /* All symtabs must have at least two blocks.  */
   bv = new_bvect (2);
-  bv->set_block (GLOBAL_BLOCK, new_block (NON_FUNCTION_BLOCK, lang));
-  bv->set_block (STATIC_BLOCK, new_block (NON_FUNCTION_BLOCK, lang));
+  bv->set_block (GLOBAL_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
+  bv->set_block (STATIC_BLOCK, new_block (objfile, NON_FUNCTION_BLOCK, lang));
   bv->static_block ()->set_superblock (bv->global_block ());
   cust->set_blockvector (bv);
 
@@ -4710,12 +4711,10 @@ new_bvect (int nblocks)
    linearly; otherwise, store them hashed.  */
 
 static struct block *
-new_block (enum block_type type, enum language language)
+new_block (struct objfile *objfile, enum block_type type,
+	   enum language language)
 {
-  /* FIXME: carlton/2003-09-11: This should use allocate_block to
-     allocate the block.  Which, in turn, suggests that the block
-     should be allocated on an obstack.  */
-  struct block *retval = XCNEW (struct block);
+  struct block *retval = allocate_block (&objfile->objfile_obstack);
 
   if (type == FUNCTION_BLOCK)
     retval->set_multidict (mdict_create_linear_expandable (language));
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 23/27] Use 'new' for block and global_block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (21 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 22/27] Fix memory leak in mdebugread.c Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 24/27] Have global_block inherit from block Tom Tromey
                   ` (3 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes block and global_block to add initializers, and then to
use 'new' for allocation.
---
 gdb/block.c |  6 ++----
 gdb/block.h | 20 ++++++++++----------
 2 files changed, 12 insertions(+), 14 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 0870793dcd4..8f15cdf23f8 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -389,9 +389,7 @@ block::global_block () const
 struct block *
 allocate_block (struct obstack *obstack)
 {
-  struct block *bl = OBSTACK_ZALLOC (obstack, struct block);
-
-  return bl;
+  return new (obstack) struct block;
 }
 
 /* Allocate a global block.  */
@@ -399,7 +397,7 @@ allocate_block (struct obstack *obstack)
 struct block *
 allocate_global_block (struct obstack *obstack)
 {
-  struct global_block *bl = OBSTACK_ZALLOC (obstack, struct global_block);
+  struct global_block *bl = new (obstack) struct global_block;
 
   return &bl->block;
 }
diff --git a/gdb/block.h b/gdb/block.h
index 6ccf3766d8f..9a60140581d 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -105,7 +105,7 @@ struct blockranges
    This implies that within the body of one function
    the blocks appear in the order of a depth-first tree walk.  */
 
-struct block
+struct block : public allocate_on_obstack
 {
   /* Return this block's start address.  */
   CORE_ADDR start () const
@@ -273,13 +273,13 @@ struct block
 
   /* Addresses in the executable code that are in this block.  */
 
-  CORE_ADDR m_start;
-  CORE_ADDR m_end;
+  CORE_ADDR m_start = 0;
+  CORE_ADDR m_end = 0;
 
   /* The symbol that names this block, if the block is the body of a
      function (real or inlined); otherwise, zero.  */
 
-  struct symbol *m_function;
+  struct symbol *m_function = nullptr;
 
   /* The `struct block' for the containing block, or 0 if none.
 
@@ -287,22 +287,22 @@ struct block
      case of C) is the STATIC_BLOCK.  The superblock of the
      STATIC_BLOCK is the GLOBAL_BLOCK.  */
 
-  const struct block *m_superblock;
+  const struct block *m_superblock = nullptr;
 
   /* This is used to store the symbols in the block.  */
 
-  struct multidictionary *m_multidict;
+  struct multidictionary *m_multidict = nullptr;
 
   /* Contains information about namespace-related info relevant to this block:
      using directives and the current namespace scope.  */
 
-  struct block_namespace_info *m_namespace_info;
+  struct block_namespace_info *m_namespace_info = nullptr;
 
   /* Address ranges for blocks with non-contiguous ranges.  If this
      is NULL, then there is only one range which is specified by
      startaddr and endaddr above.  */
 
-  struct blockranges *m_ranges;
+  struct blockranges *m_ranges = nullptr;
 
 private:
 
@@ -314,7 +314,7 @@ struct block
 /* The global block is singled out so that we can provide a back-link
    to the compunit symtab.  */
 
-struct global_block
+struct global_block : public allocate_on_obstack
 {
   /* The block.  */
 
@@ -322,7 +322,7 @@ struct global_block
 
   /* This holds a pointer to the compunit symtab holding this block.  */
 
-  struct compunit_symtab *compunit_symtab;
+  struct compunit_symtab *compunit_symtab = nullptr;
 };
 
 struct blockvector
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 24/27] Have global_block inherit from block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (22 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 23/27] Use 'new' for block and global_block Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 25/27] Remove allocate_block and allocate_global_block Tom Tromey
                   ` (2 subsequent siblings)
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes global_block to inherit from block, which is what was
always intended.
---
 gdb/block.c | 4 +---
 gdb/block.h | 6 +-----
 2 files changed, 2 insertions(+), 8 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 8f15cdf23f8..1698ee51e7b 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -397,9 +397,7 @@ allocate_block (struct obstack *obstack)
 struct block *
 allocate_global_block (struct obstack *obstack)
 {
-  struct global_block *bl = new (obstack) struct global_block;
-
-  return &bl->block;
+  return new (obstack) struct global_block;
 }
 
 /* See block.h.  */
diff --git a/gdb/block.h b/gdb/block.h
index 9a60140581d..3d71e7daab1 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -314,12 +314,8 @@ struct block : public allocate_on_obstack
 /* The global block is singled out so that we can provide a back-link
    to the compunit symtab.  */
 
-struct global_block : public allocate_on_obstack
+struct global_block : public block
 {
-  /* The block.  */
-
-  struct block block;
-
   /* This holds a pointer to the compunit symtab holding this block.  */
 
   struct compunit_symtab *compunit_symtab = nullptr;
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 25/27] Remove allocate_block and allocate_global_block
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (23 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 24/27] Have global_block inherit from block Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 26/27] Make block members 'private' Tom Tromey
  2023-01-20 21:46 ` [PATCH 27/27] Convert contained_in to method Tom Tromey
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This removes allocate_block and allocate_global_block in favor of
simply calling 'new'.
---
 gdb/block.c      | 23 -----------------------
 gdb/block.h      |  4 ----
 gdb/buildsym.c   |  7 ++++---
 gdb/jit.c        |  9 +++++----
 gdb/mdebugread.c |  2 +-
 5 files changed, 10 insertions(+), 35 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 1698ee51e7b..334d8180448 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -377,29 +377,6 @@ block::global_block () const
   return block;
 }
 
-/* Allocate a block on OBSTACK, and initialize its elements to
-   zero/NULL.  This is useful for creating "dummy" blocks that don't
-   correspond to actual source files.
-
-   Warning: it sets the block's BLOCK_MULTIDICT to NULL, which isn't a
-   valid value.  If you really don't want the block to have a
-   dictionary, then you should subsequently set its BLOCK_MULTIDICT to
-   dict_create_linear (obstack, NULL).  */
-
-struct block *
-allocate_block (struct obstack *obstack)
-{
-  return new (obstack) struct block;
-}
-
-/* Allocate a global block.  */
-
-struct block *
-allocate_global_block (struct obstack *obstack)
-{
-  return new (obstack) struct global_block;
-}
-
 /* See block.h.  */
 
 void
diff --git a/gdb/block.h b/gdb/block.h
index 3d71e7daab1..7c40bae9af2 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -426,10 +426,6 @@ extern const struct block *block_for_pc (CORE_ADDR);
 
 extern const struct block *block_for_pc_sect (CORE_ADDR, struct obj_section *);
 
-extern struct block *allocate_block (struct obstack *obstack);
-
-extern struct block *allocate_global_block (struct obstack *obstack);
-
 /* A block iterator.  This structure should be treated as though it
    were opaque; it is only defined here because we want to support
    stack allocation of iterators.  */
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index 9d45938f8e9..303eed52282 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -211,9 +211,10 @@ buildsym_compunit::finish_block_internal
   struct pending_block *pblock;
   struct pending_block *opblock;
 
-  block = (is_global
-	   ? allocate_global_block (&m_objfile->objfile_obstack)
-	   : allocate_block (&m_objfile->objfile_obstack));
+  if (is_global)
+    block = new (&m_objfile->objfile_obstack) global_block;
+  else
+    block = new (&m_objfile->objfile_obstack) struct block;
 
   if (symbol)
     {
diff --git a/gdb/jit.c b/gdb/jit.c
index f584438c229..0b089bc6a82 100644
--- a/gdb/jit.c
+++ b/gdb/jit.c
@@ -567,7 +567,7 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
   int block_idx = FIRST_LOCAL_BLOCK;
   for (gdb_block &gdb_block_iter : stab->blocks)
     {
-      struct block *new_block = allocate_block (&objfile->objfile_obstack);
+      struct block *new_block = new (&objfile->objfile_obstack) block;
       struct symbol *block_name = new (&objfile->objfile_obstack) symbol;
       struct type *block_type = arch_type (objfile->arch (),
 					   TYPE_CODE_VOID,
@@ -609,9 +609,10 @@ finalize_symtab (struct gdb_symtab *stab, struct objfile *objfile)
     {
       struct block *new_block;
 
-      new_block = (i == GLOBAL_BLOCK
-		   ? allocate_global_block (&objfile->objfile_obstack)
-		   : allocate_block (&objfile->objfile_obstack));
+      if (i == GLOBAL_BLOCK)
+	new_block = new (&objfile->objfile_obstack) global_block;
+      else
+	new_block = new (&objfile->objfile_obstack) block;
       new_block->set_multidict
 	(mdict_create_linear (&objfile->objfile_obstack, NULL));
       new_block->set_superblock (block_iter);
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index b6a0b3c07df..18b59cff6bf 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -4714,7 +4714,7 @@ static struct block *
 new_block (struct objfile *objfile, enum block_type type,
 	   enum language language)
 {
-  struct block *retval = allocate_block (&objfile->objfile_obstack);
+  struct block *retval = new (&objfile->objfile_obstack) block;
 
   if (type == FUNCTION_BLOCK)
     retval->set_multidict (mdict_create_linear_expandable (language));
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 26/27] Make block members 'private'
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (24 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 25/27] Remove allocate_block and allocate_global_block Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  2023-01-20 21:46 ` [PATCH 27/27] Convert contained_in to method Tom Tromey
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes block to make the data members 'private'.
---
 gdb/block.h | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gdb/block.h b/gdb/block.h
index 7c40bae9af2..32e3c9ebcfd 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -271,6 +271,12 @@ struct block : public allocate_on_obstack
 
   struct dynamic_prop *static_link () const;
 
+private:
+
+  /* If the namespace_info is NULL, allocate it via OBSTACK and
+     initialize its members to zero.  */
+  void initialize_namespace (struct obstack *obstack);
+
   /* Addresses in the executable code that are in this block.  */
 
   CORE_ADDR m_start = 0;
@@ -303,12 +309,6 @@ struct block : public allocate_on_obstack
      startaddr and endaddr above.  */
 
   struct blockranges *m_ranges = nullptr;
-
-private:
-
-  /* If the namespace_info is NULL, allocate it via OBSTACK and
-     initialize its members to zero.  */
-  void initialize_namespace (struct obstack *obstack);
 };
 
 /* The global block is singled out so that we can provide a back-link
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

* [PATCH 27/27] Convert contained_in to method
  2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
                   ` (25 preceding siblings ...)
  2023-01-20 21:46 ` [PATCH 26/27] Make block members 'private' Tom Tromey
@ 2023-01-20 21:46 ` Tom Tromey
  26 siblings, 0 replies; 28+ messages in thread
From: Tom Tromey @ 2023-01-20 21:46 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This converts contained_in to be a method of block.
---
 gdb/block.c        |  7 +++----
 gdb/block.h        | 19 +++++++++----------
 gdb/blockframe.c   |  2 +-
 gdb/breakpoint.c   |  2 +-
 gdb/frame.c        |  2 +-
 gdb/inline-frame.c |  2 +-
 gdb/parse.c        |  2 +-
 gdb/printcmd.c     |  6 +++---
 8 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/gdb/block.c b/gdb/block.c
index 334d8180448..4e40247b79c 100644
--- a/gdb/block.c
+++ b/gdb/block.c
@@ -65,15 +65,14 @@ block::gdbarch () const
 /* See block.h.  */
 
 bool
-contained_in (const struct block *a, const struct block *b,
-	      bool allow_nested)
+block::contains (const struct block *a, bool allow_nested) const
 {
-  if (!a || !b)
+  if (a == nullptr)
     return false;
 
   do
     {
-      if (a == b)
+      if (a == this)
 	return true;
       /* If A is a function block, then A cannot be contained in B,
 	 except if A was inlined.  */
diff --git a/gdb/block.h b/gdb/block.h
index 32e3c9ebcfd..cdcee0844ec 100644
--- a/gdb/block.h
+++ b/gdb/block.h
@@ -271,6 +271,15 @@ struct block : public allocate_on_obstack
 
   struct dynamic_prop *static_link () const;
 
+  /* Return true if block A is lexically nested within this block, or
+     if A and this block have the same pc range.  Return false
+     otherwise.  If ALLOW_NESTED is true, then block A is considered
+     to be in this block if A is in a nested function in this block's
+     function.  If ALLOW_NESTED is false (the default), then blocks in
+     nested functions are not considered to be contained.  */
+
+  bool contains (const struct block *a, bool allow_nested = false) const;
+
 private:
 
   /* If the namespace_info is NULL, allocate it via OBSTACK and
@@ -400,16 +409,6 @@ struct blockvector
   struct block *m_blocks[1];
 };
 
-/* Return true if block A is lexically nested within block B, or if a
-   and b have the same pc range.  Return false otherwise.  If
-   ALLOW_NESTED is true, then block A is considered to be in block B
-   if A is in a nested function in B's function.  If ALLOW_NESTED is
-   false (the default), then blocks in nested functions are not
-   considered to be contained.  */
-
-extern bool contained_in (const struct block *a, const struct block *b,
-			  bool allow_nested = false);
-
 extern const struct blockvector *blockvector_for_pc (CORE_ADDR,
 					       const struct block **);
 
diff --git a/gdb/blockframe.c b/gdb/blockframe.c
index 7c9faf487a7..633a9674d97 100644
--- a/gdb/blockframe.c
+++ b/gdb/blockframe.c
@@ -469,7 +469,7 @@ block_innermost_frame (const struct block *block)
   while (frame != NULL)
     {
       const struct block *frame_block = get_frame_block (frame, NULL);
-      if (frame_block != NULL && contained_in (frame_block, block))
+      if (frame_block != NULL && block->contains (frame_block))
 	return frame;
 
       frame = get_prev_frame (frame);
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 475a16e8d73..f6aa3940429 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -5142,7 +5142,7 @@ watchpoint_check (bpstat *bs)
 
 	  function = get_frame_function (fr);
 	  if (function == NULL
-	      || !contained_in (b->exp_valid_block, function->value_block ()))
+	      || !function->value_block ()->contains (b->exp_valid_block))
 	    within_current_scope = false;
 	}
 
diff --git a/gdb/frame.c b/gdb/frame.c
index 2f9622ad2b2..2e29348110e 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -848,7 +848,7 @@ frame_id_inner (struct gdbarch *gdbarch, struct frame_id l, struct frame_id r)
 	/* This will return true if LB and RB are the same block, or
 	   if the block with the smaller depth lexically encloses the
 	   block with the greater depth.  */
-	inner = contained_in (lb, rb);
+	inner = rb->contains (lb);
     }
   else
     /* Only return non-zero when strictly inner than.  Note that, per
diff --git a/gdb/inline-frame.c b/gdb/inline-frame.c
index 21431a280fa..80765d5cca5 100644
--- a/gdb/inline-frame.c
+++ b/gdb/inline-frame.c
@@ -293,7 +293,7 @@ block_starting_point_at (CORE_ADDR pc, const struct block *block)
   if (new_block == NULL)
     return 1;
 
-  if (new_block == block || contained_in (new_block, block))
+  if (new_block == block || block->contains (new_block))
     return 0;
 
   /* The immediately preceding address belongs to a different block,
diff --git a/gdb/parse.c b/gdb/parse.c
index 4c20b91f238..f2917b30740 100644
--- a/gdb/parse.c
+++ b/gdb/parse.c
@@ -86,7 +86,7 @@ innermost_block_tracker::update (const struct block *b,
 {
   if ((m_types & t) != 0
       && (m_innermost_block == NULL
-	  || contained_in (b, m_innermost_block)))
+	  || m_innermost_block->contains (b)))
     m_innermost_block = b;
 }
 
diff --git a/gdb/printcmd.c b/gdb/printcmd.c
index e3ee847a955..74e2f5160ba 100644
--- a/gdb/printcmd.c
+++ b/gdb/printcmd.c
@@ -2123,8 +2123,8 @@ do_one_display (struct display *d)
   if (d->block)
     {
       if (d->pspace == current_program_space)
-	within_current_scope = contained_in (get_selected_block (0), d->block,
-					     true);
+	within_current_scope = d->block->contains (get_selected_block (0),
+						   true);
       else
 	within_current_scope = 0;
     }
@@ -2282,7 +2282,7 @@ Num Enb Expression\n"));
       else if (d->format.format)
 	gdb_printf ("/%c ", d->format.format);
       gdb_puts (d->exp_string.c_str ());
-      if (d->block && !contained_in (get_selected_block (0), d->block, true))
+      if (d->block && !d->block->contains (get_selected_block (0), true))
 	gdb_printf (_(" (cannot be evaluated in the current context)"));
       gdb_printf ("\n");
     }
-- 
2.39.0


^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2023-01-20 21:46 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-20 21:45 [PATCH 00/27] C++-ify struct block Tom Tromey
2023-01-20 21:45 ` [PATCH 01/27] Rearrange block.c to avoid a forward declaration Tom Tromey
2023-01-20 21:45 ` [PATCH 02/27] Avoid extra allocations in block Tom Tromey
2023-01-20 21:45 ` [PATCH 03/27] Don't allow NULL as an argument to block_scope Tom Tromey
2023-01-20 21:45 ` [PATCH 04/27] Don't allow NULL as an argument to block_using Tom Tromey
2023-01-20 21:45 ` [PATCH 05/27] Don't allow NULL as an argument to block_static_block Tom Tromey
2023-01-20 21:45 ` [PATCH 06/27] Don't allow NULL as an argument to block_global_block Tom Tromey
2023-01-20 21:45 ` [PATCH 07/27] Convert block_objfile to method Tom Tromey
2023-01-20 21:45 ` [PATCH 08/27] Convert block_gdbarch " Tom Tromey
2023-01-20 21:46 ` [PATCH 09/27] Convert block_inlined_p " Tom Tromey
2023-01-20 21:46 ` [PATCH 10/27] Convert more block functions to methods Tom Tromey
2023-01-20 21:46 ` [PATCH 11/27] Convert block_linkage_function to method Tom Tromey
2023-01-20 21:46 ` [PATCH 12/27] Convert block_containing_function " Tom Tromey
2023-01-20 21:46 ` [PATCH 13/27] Convert block_static_block and block_global_block to methods Tom Tromey
2023-01-20 21:46 ` [PATCH 14/27] Convert set_block_compunit_symtab to method Tom Tromey
2023-01-20 21:46 ` [PATCH 15/27] Convert block_static_link " Tom Tromey
2023-01-20 21:46 ` [PATCH 16/27] Store 'name' in block_iterator Tom Tromey
2023-01-20 21:46 ` [PATCH 17/27] Combine both styles of block iterator Tom Tromey
2023-01-20 21:46 ` [PATCH 18/27] Introduce a block iterator wrapper Tom Tromey
2023-01-20 21:46 ` [PATCH 19/27] Convert explicit iterator uses to foreach Tom Tromey
2023-01-20 21:46 ` [PATCH 20/27] Remove ALL_BLOCK_SYMBOLS_WITH_NAME Tom Tromey
2023-01-20 21:46 ` [PATCH 21/27] Remove ALL_BLOCK_SYMBOLS Tom Tromey
2023-01-20 21:46 ` [PATCH 22/27] Fix memory leak in mdebugread.c Tom Tromey
2023-01-20 21:46 ` [PATCH 23/27] Use 'new' for block and global_block Tom Tromey
2023-01-20 21:46 ` [PATCH 24/27] Have global_block inherit from block Tom Tromey
2023-01-20 21:46 ` [PATCH 25/27] Remove allocate_block and allocate_global_block Tom Tromey
2023-01-20 21:46 ` [PATCH 26/27] Make block members 'private' Tom Tromey
2023-01-20 21:46 ` [PATCH 27/27] Convert contained_in to method Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).