From 5aff8911350d265c33bb98be1f69095ba16fa76d Mon Sep 17 00:00:00 2001 From: Jan Vrany Date: Fri, 10 Oct 2025 21:38:44 +0100 Subject: [PATCH] gdb: introduce new function create_function_type 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 --- gdb/gdbtypes.c | 44 ++++++++++++++++++++++++++++---------------- gdb/gdbtypes.h | 24 +++++++++++++++++++----- 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c index f1c545f173e..715db74ab0e 100644 --- a/gdb/gdbtypes.c +++ b/gdb/gdbtypes.c @@ -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. */ diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h index 75c77b3a90c..0d514d036e3 100644 --- a/gdb/gdbtypes.h +++ b/gdb/gdbtypes.h @@ -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. -- 2.47.3