[PATCH v2 05/17] gdb: refactor type_stack::insert methods

Tankut Baris Aktemur <[email protected]> Wed, 22 Jul 2026 05:41:54 -0500
Newsgroups gmane.comp.gdb.patches
Message-ID <20260722-users-aktemur-type-instance-flags-v2-5-d60dcbc2a76f@amd.com>
Clone the 'insert_into' method of struct type_stack into two
overloads, one taking a type piece and the other taking an integer,
and use the overloads to simplify the 'insert' methods.  This is a
refactoring.
---
 gdb/type-stack.c | 27 +++++++--------------------
 gdb/type-stack.h | 18 ++++++++++++++++--
 2 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/gdb/type-stack.c b/gdb/type-stack.c
index 7e790a003ec..d19854363a2 100644
--- a/gdb/type-stack.c
+++ b/gdb/type-stack.c
@@ -26,9 +26,6 @@
 void
 type_stack::insert (enum type_pieces tp)
 {
-  union type_stack_elt element;
-  int slot;
-
   gdb_assert (tp == tp_pointer || tp == tp_reference
 	      || tp == tp_rvalue_reference || tp == tp_const
 	      || tp == tp_volatile || tp == tp_restrict
@@ -39,12 +36,9 @@ type_stack::insert (enum type_pieces tp)
      push this on the top of the stack.  */
   if (!m_elements.empty () && (tp == tp_const || tp == tp_volatile
 			       || tp == tp_restrict))
-    slot = 1;
+    insert_into (1, tp);
   else
-    slot = 0;
-
-  element.piece = tp;
-  insert_into (slot, element);
+    insert_into (0, tp);
 }
 
 /* See type-stack.h.  */
@@ -52,22 +46,15 @@ type_stack::insert (enum type_pieces tp)
 void
 type_stack::insert (struct gdbarch *gdbarch, const char *string)
 {
-  union type_stack_elt element;
-  int slot;
-
   /* If there is anything on the stack (we know it will be a
      tp_pointer), insert the address space qualifier above it.
      Otherwise, simply push this on the top of the stack.  */
-  if (!m_elements.empty ())
-    slot = 1;
-  else
-    slot = 0;
+  int slot = (!m_elements.empty ()) ? 1 : 0;
 
-  element.piece = tp_space_identifier;
-  insert_into (slot, element);
-  element.int_val
-    = address_space_name_to_type_instance_flags (gdbarch, string);
-  insert_into (slot, element);
+  insert_into (slot, tp_space_identifier);
+  insert_into (slot,
+	       address_space_name_to_type_instance_flags (gdbarch,
+							  string));
 }
 
 /* See type-stack.h.  */
diff --git a/gdb/type-stack.h b/gdb/type-stack.h
index 318ef715b2e..1be6d569d8e 100644
--- a/gdb/type-stack.h
+++ b/gdb/type-stack.h
@@ -238,12 +238,26 @@ struct type_stack
 private:
 
   /* A helper function for the insert methods.  This does work of
-     expanding the type stack and inserting the new element, ELEMENT,
+     expanding the type stack and inserting the new element, TP,
      into the stack at location SLOT.  */
 
-  void insert_into (int slot, union type_stack_elt element)
+  void insert_into (int slot, enum type_pieces tp)
   {
     gdb_assert (slot <= m_elements.size ());
+    union type_stack_elt element;
+    element.piece = tp;
+    m_elements.insert (m_elements.begin () + slot, element);
+  }
+
+  /* A helper function for the insert methods.  This does work of
+     expanding the type stack and inserting the new element, VAL,
+     into the stack at location SLOT.  */
+
+  void insert_into (int slot, int val)
+  {
+    gdb_assert (slot <= m_elements.size ());
+    union type_stack_elt element;
+    element.int_val = val;
     m_elements.insert (m_elements.begin () + slot, element);
   }
 

-- 
2.34.1