]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: introduce new function create_function_type
authorJan Vrany <jan.vrany@labware.com>
Fri, 10 Oct 2025 20:38:44 +0000 (21:38 +0100)
committerJan Vrany <jan.vrany@labware.com>
Fri, 10 Oct 2025 20:38:44 +0000 (21:38 +0100)
While working on new Python API to create new function types I realized
that there's no easy way to create a new function type and control where
it is going to be allocated (whether in gdbarch's obstack or objfile's).
Functions lookup_function_type and lookup_function_type_with_arguments
always allocate at the same obstack as its return type.

This is not sufficient for the new Python API - the user may use any
type it can get hold of. For example, one may want to create a function
returning arch-owned type and taking one argument of objfile-owned type.
In that case we need to allocate the new type on that very objfile's
obstack.

This commit introduces new function - create_function_type - that takes
type_allocator as first parameter, allowing caller to control the
allocation. Existing functions (lookup_function_type and
lookup_function_type_with_arguments) are reimplemented by means of new
create_function_type.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/gdbtypes.c
gdb/gdbtypes.h

index f1c545f173ed23db7cc0a673b453ff0ec999ecf1..715db74ab0e41e857e6996c8fb1fe36e573764c9 100644 (file)
@@ -561,27 +561,19 @@ make_function_type (struct type *type, struct type **typeptr)
   return ntype;
 }
 
-/* Given a type TYPE, return a type of functions that return that type.
-   May need to construct such a type if this is the first use.  */
-
-struct type *
-lookup_function_type (struct type *type)
-{
-  return make_function_type (type, (struct type **) 0);
-}
-
-/* Given a type TYPE and argument types, return the appropriate
-   function type.  If the final type in PARAM_TYPES is NULL, make a
-   varargs function.  */
+/* See gdbtypes.h.  */
 
 struct type *
-lookup_function_type_with_arguments (struct type *type,
-                                    int nparams,
-                                    struct type **param_types)
+create_function_type (type_allocator &alloc,
+                    struct type *return_type,
+                    int nparams,
+                    struct type **param_types)
 {
-  struct type *fn = make_function_type (type, (struct type **) 0);
+  struct type *fn = alloc.new_type ();
   int i;
 
+  make_function_type (return_type, &fn);
+
   if (nparams > 0)
     {
       if (param_types[nparams - 1] == NULL)
@@ -608,6 +600,26 @@ lookup_function_type_with_arguments (struct type *type,
   return fn;
 }
 
+/* See gdbtypes.h.  */
+
+struct type *
+lookup_function_type (struct type *return_type)
+{
+  type_allocator alloc (return_type);
+  return create_function_type (alloc, return_type, 0, nullptr);
+}
+
+/* See gdbtypes.h.  */
+
+struct type *
+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);
+}
+
 /* Identify address space identifier by name -- return a
    type_instance_flags.  */
 
index 75c77b3a90c6474dcc5fe1fba5edf5b3233dcc27..0d514d036e3362381d840efa5598c59b34ba8881 100644 (file)
@@ -2528,11 +2528,25 @@ extern struct type *lookup_pointer_type (struct type *);
 
 extern struct type *make_function_type (struct type *, struct type **);
 
-extern struct type *lookup_function_type (struct type *);
-
-extern struct type *lookup_function_type_with_arguments (struct type *,
-                                                        int,
-                                                        struct type **);
+/* Given a return type and argument types, create new function type.
+   If the final type in PARAM_TYPES is NULL, create a varargs function.
+   New type is allocated using ALLOC.  */
+extern struct type *create_function_type (type_allocator &alloc,
+                                         struct type *return_type,
+                                         int nparams,
+                                         struct type **param_types);
+
+/* Like create_function_type, but allocate the new function type at
+   the same obstack as RETURN_TYPE and with unspecified number of
+   parameters and their types.  */
+extern struct type *lookup_function_type (struct type *return_type);
+
+/* Like create_function_type, but allocate the new function type at
+   the same obstack as RETURN_TYPE.  */
+extern struct type *lookup_function_type_with_arguments
+                                       (struct type *return_type,
+                                        int nparams,
+                                        struct type **param_types);
 
 /* Create a range type using ALLOC.