[4/7] gdb: remove make_function_type typeptr parameter

Message ID 20251217155550.37654-5-simon.marchi@efficios.com
State New
Headers
Series gdbtypes cleanups |

Commit Message

Simon Marchi Dec. 17, 2025, 3:54 p.m. UTC
  From: Simon Marchi <simon.marchi@polymtl.ca>

In a few places, it is passed nullptr, meaning that we allocate a type
using a type allocator derived from the return type.

In the

  -> lookup_function_type_with_arguments
    -> create_function_type
      -> make_function_type

and

  -> lookup_function_type
    -> create_function_type
      -> make_function_type

paths, we create an allocator based on the return type, pass it down,
and create a type using that, which then gets passed to
make_function_type.  Instead, we can let make_function_type allocate the
type based on the return type.

Change-Id: I3f38e2f4744ab664bd91b006b00501332df617b5
---
 gdb/avr-tdep.c  |  2 +-
 gdb/ft32-tdep.c |  2 +-
 gdb/gdbtypes.c  | 46 +++++++++++-----------------------------------
 gdb/gdbtypes.h  |  4 +++-
 gdb/z80-tdep.c  |  2 +-
 5 files changed, 17 insertions(+), 39 deletions(-)
  

Patch

diff --git a/gdb/avr-tdep.c b/gdb/avr-tdep.c
index 4f6d1fc9d00e..1a80e0b4ef8b 100644
--- a/gdb/avr-tdep.c
+++ b/gdb/avr-tdep.c
@@ -1475,7 +1475,7 @@  avr_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
      be defined.  */
   type_allocator alloc (gdbarch);
   tdep->void_type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
-  tdep->func_void_type = make_function_type (tdep->void_type, NULL);
+  tdep->func_void_type = make_function_type (tdep->void_type);
   tdep->pc_type = init_pointer_type (alloc, 4 * TARGET_CHAR_BIT, NULL,
 				     tdep->func_void_type);
 
diff --git a/gdb/ft32-tdep.c b/gdb/ft32-tdep.c
index b1ebd7209eea..1f2c2d970afa 100644
--- a/gdb/ft32-tdep.c
+++ b/gdb/ft32-tdep.c
@@ -576,7 +576,7 @@  ft32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
      be defined.  */
   type_allocator alloc (gdbarch);
   void_type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
-  func_void_type = make_function_type (void_type, NULL);
+  func_void_type = make_function_type (void_type);
   tdep->pc_type = init_pointer_type (alloc, 4 * TARGET_CHAR_BIT, NULL,
 				     func_void_type);
   tdep->pc_type->set_instance_flags (tdep->pc_type->instance_flags ()
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index aa0cb2c515a3..f42fee408fdb 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -476,30 +476,14 @@  lookup_rvalue_reference_type (struct type *type)
   return lookup_reference_type (type, TYPE_CODE_RVALUE_REF);
 }
 
-/* Lookup a function type that returns type TYPE.  TYPEPTR, if
-   nonzero, points to a pointer to memory where the function type
-   should be stored.  If *TYPEPTR is zero, update it to point to the
-   function type we return.  We allocate new memory if needed.  */
+/* See gdbtypes.h.  */
 
-struct type *
-make_function_type (struct type *type, struct type **typeptr)
+type *
+make_function_type (type *return_type)
 {
-  struct type *ntype;	/* New type */
-
-  if (typeptr == 0 || *typeptr == 0)	/* We'll need to allocate one.  */
-    {
-      ntype = type_allocator (type).new_type ();
-      if (typeptr)
-	*typeptr = ntype;
-    }
-  else			/* We have storage, but need to reset it.  */
-    {
-      ntype = *typeptr;
-      smash_type (ntype);
-    }
-
-  ntype->set_target_type (type);
+  type *ntype = type_allocator (return_type).new_type ();
 
+  ntype->set_target_type (return_type);
   ntype->set_length (1);
   ntype->set_code (TYPE_CODE_FUNC);
 
@@ -512,16 +496,10 @@  make_function_type (struct type *type, struct type **typeptr)
    If the final type in PARAM_TYPES is NULL, create a varargs function.
    New type is allocated using ALLOC.  */
 
-static struct type *
-create_function_type (type_allocator &alloc,
-		     struct type *return_type,
-		     int nparams,
-		     struct type **param_types)
+static type *
+create_function_type (type *return_type, int nparams, type **param_types)
 {
-  struct type *fn = alloc.new_type ();
-  int i;
-
-  make_function_type (return_type, &fn);
+  type *fn = make_function_type (return_type);
 
   if (nparams > 0)
     {
@@ -543,7 +521,7 @@  create_function_type (type_allocator &alloc,
     }
 
   fn->alloc_fields (nparams);
-  for (i = 0; i < nparams; ++i)
+  for (int i = 0; i < nparams; ++i)
     fn->field (i).set_type (param_types[i]);
 
   return fn;
@@ -554,8 +532,7 @@  create_function_type (type_allocator &alloc,
 struct type *
 lookup_function_type (struct type *return_type)
 {
-  type_allocator alloc (return_type);
-  return create_function_type (alloc, return_type, 0, nullptr);
+  return create_function_type (return_type, 0, nullptr);
 }
 
 /* See gdbtypes.h.  */
@@ -565,8 +542,7 @@  lookup_function_type_with_arguments (struct type *return_type,
 				     int nparams,
 				     struct type **param_types)
 {
-  type_allocator alloc (return_type);
-  return create_function_type (alloc, return_type, nparams, param_types);
+  return create_function_type (return_type, nparams, param_types);
 }
 
 /* Identify address space identifier by name -- return a
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index afb8319cca6b..b467190ffb7f 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2518,7 +2518,9 @@  extern type *make_pointer_type (type *type);
 
 extern struct type *lookup_pointer_type (struct type *);
 
-extern struct type *make_function_type (struct type *, struct type **);
+/* Lookup a function type that returns type RETURN_TYPE.  */
+
+extern struct type *make_function_type (type *return_type);
 
 /* Create a new function type with return type RETURN_TYPE and unspecified
    number and types of parameters.
diff --git a/gdb/z80-tdep.c b/gdb/z80-tdep.c
index b07d6b15385b..fd4171abac76 100644
--- a/gdb/z80-tdep.c
+++ b/gdb/z80-tdep.c
@@ -1142,7 +1142,7 @@  z80_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
   type_allocator alloc (gdbarch);
   tdep->void_type = alloc.new_type (TYPE_CODE_VOID, TARGET_CHAR_BIT,
 				    "void");
-  tdep->func_void_type = make_function_type (tdep->void_type, NULL);
+  tdep->func_void_type = make_function_type (tdep->void_type);
   tdep->pc_type = init_pointer_type (alloc,
 				     tdep->addr_length * TARGET_CHAR_BIT,
 				     NULL, tdep->func_void_type);