Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>
Subject: [RFC v5 04/18] gdb: introduce new function create_function_type
Date: Mon, 23 Jun 2025 17:09:59 +0100	[thread overview]
Message-ID: <20250623161013.650814-5-jan.vrany@labware.com> (raw)
In-Reply-To: <20250623161013.650814-1-jan.vrany@labware.com>

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 pr 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.
---
 gdb/gdbtypes.c | 47 +++++++++++++++++++++++++++++------------------
 gdb/gdbtypes.h | 24 +++++++++++++++++++-----
 2 files changed, 48 insertions(+), 23 deletions(-)

diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 9c57f30a1dd..591a3cc4323 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)
@@ -589,8 +581,7 @@ lookup_function_type_with_arguments (struct type *type,
 	  --nparams;
 	  fn->set_has_varargs (true);
 	}
-      else if (check_typedef (param_types[nparams - 1])->code ()
-	       == TYPE_CODE_VOID)
+      else if ((param_types[nparams - 1])->is_void ())
 	{
 	  --nparams;
 	  /* Caller should have ensured this.  */
@@ -608,6 +599,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 9e2efe99cff..ba216c6db54 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2526,11 +2526,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.2


  parent reply	other threads:[~2025-06-23 16:11 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-23 16:09 [RFC v5 00/19] Add Python "JIT" API Jan Vrany
2025-06-23 16:09 ` [RFC v5 01/18] gdb: introduce expand_symtabs_maybe_overlapping Jan Vrany
2025-06-24 15:22   ` Tom Tromey
2025-06-26 15:05     ` Jan Vraný
2025-06-23 16:09 ` [RFC v5 02/18] gdb: introduce compunit_symtab::maybe_contains Jan Vrany
2025-06-23 16:09 ` [RFC v5 03/18] gdb: update is_addr_in_objfile to support "dynamic" objfiles Jan Vrany
2025-06-23 16:09 ` Jan Vrany [this message]
2025-06-24 15:29   ` [RFC v5 04/18] gdb: introduce new function create_function_type Tom Tromey
2025-06-26 11:12     ` Jan Vraný
2025-06-27 14:21       ` Tom Tromey
2025-06-27 14:30         ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 05/18] gdb/python: add function () method to gdb.Type object Jan Vrany
2025-06-24 16:11   ` Tom Tromey
2025-06-26 11:13     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 06/18] gdb: use std::vector<> to hold on blocks in struct blockvector Jan Vrany
2025-06-23 16:10 ` [RFC v5 07/18] gdb/python: add gdb.Compunit Jan Vrany
2025-06-23 16:10 ` [RFC v5 08/18] gdb/python: allow instantiation of gdb.Objfile from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 09/18] gdb/python: add unlink () method to gdb.Objfile object Jan Vrany
2025-06-23 16:10 ` [RFC v5 10/18] gdb/python: allow instantiation of gdb.Compunit from Python Jan Vrany
2025-06-23 16:10 ` [RFC v5 11/18] gdb/python: allow instantiation of gdb.Symtab " Jan Vrany
2025-06-23 16:10 ` [RFC v5 12/18] gdb/python: allow instantiation of gdb.Block " Jan Vrany
2025-06-23 16:10 ` [RFC v5 13/18] gdb/python: allow instantiation of gdb.Symbol " Jan Vrany
2025-06-23 16:10 ` [RFC v5 14/18] gdb/python: add add_symbol () method to gdb.Block Jan Vrany
2025-08-29 14:10   ` Andrew Burgess
2025-08-29 14:14     ` Andrew Burgess
2025-06-23 16:10 ` [RFC v5 15/18] gdb/python: add more attributes to gdb.LinetableEntry objects Jan Vrany
2025-08-29 14:00   ` Andrew Burgess
2025-09-02 11:03     ` Jan Vraný
2025-06-23 16:10 ` [RFC v5 16/18] gdb/python: allow instantiation of gdb.LineTableEntry objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 17/18] gdb/python: allow instantiation of gdb.LineTable objects Jan Vrany
2025-06-23 16:10 ` [RFC v5 18/18] gdb/python: add section in documentation on implementing JIT interface Jan Vrany

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250623161013.650814-5-jan.vrany@labware.com \
    --to=jan.vrany@labware.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox