type-stack.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Type stack for GDB parser.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef TYPE_STACK_H
  15. #define TYPE_STACK_H
  16. #include "gdbtypes.h"
  17. #include <vector>
  18. struct type;
  19. struct expr_builder;
  20. /* For parsing of complicated types.
  21. An array should be preceded in the list by the size of the array. */
  22. enum type_pieces
  23. {
  24. tp_end = -1,
  25. tp_pointer,
  26. tp_reference,
  27. tp_rvalue_reference,
  28. tp_array,
  29. tp_function,
  30. tp_function_with_arguments,
  31. tp_const,
  32. tp_volatile,
  33. tp_space_identifier,
  34. tp_atomic,
  35. tp_restrict,
  36. tp_type_stack,
  37. tp_kind
  38. };
  39. /* The stack can contain either an enum type_pieces or an int. */
  40. union type_stack_elt
  41. {
  42. enum type_pieces piece;
  43. int int_val;
  44. struct type_stack *stack_val;
  45. std::vector<struct type *> *typelist_val;
  46. };
  47. /* The type stack is an instance of this structure. */
  48. struct type_stack
  49. {
  50. public:
  51. type_stack () = default;
  52. DISABLE_COPY_AND_ASSIGN (type_stack);
  53. type_stack *create ()
  54. {
  55. type_stack *result = new type_stack ();
  56. result->m_elements = std::move (m_elements);
  57. return result;
  58. }
  59. /* Insert a new type, TP, at the bottom of the type stack. If TP is
  60. tp_pointer, tp_reference or tp_rvalue_reference, it is inserted at the
  61. bottom. If TP is a qualifier, it is inserted at slot 1 (just above a
  62. previous tp_pointer) if there is anything on the stack, or simply pushed
  63. if the stack is empty. Other values for TP are invalid. */
  64. void insert (enum type_pieces tp);
  65. void push (enum type_pieces tp)
  66. {
  67. type_stack_elt elt;
  68. elt.piece = tp;
  69. m_elements.push_back (elt);
  70. }
  71. void push (int n)
  72. {
  73. type_stack_elt elt;
  74. elt.int_val = n;
  75. m_elements.push_back (elt);
  76. }
  77. /* Push the type stack STACK as an element on this type stack. */
  78. void push (struct type_stack *stack)
  79. {
  80. type_stack_elt elt;
  81. elt.stack_val = stack;
  82. m_elements.push_back (elt);
  83. push (tp_type_stack);
  84. }
  85. /* Push a function type with arguments onto the global type stack.
  86. LIST holds the argument types. If the final item in LIST is NULL,
  87. then the function will be varargs. */
  88. void push (std::vector<struct type *> *list)
  89. {
  90. type_stack_elt elt;
  91. elt.typelist_val = list;
  92. m_elements.push_back (elt);
  93. push (tp_function_with_arguments);
  94. }
  95. enum type_pieces pop ()
  96. {
  97. if (m_elements.empty ())
  98. return tp_end;
  99. type_stack_elt elt = m_elements.back ();
  100. m_elements.pop_back ();
  101. return elt.piece;
  102. }
  103. int pop_int ()
  104. {
  105. if (m_elements.empty ())
  106. {
  107. /* "Can't happen". */
  108. return 0;
  109. }
  110. type_stack_elt elt = m_elements.back ();
  111. m_elements.pop_back ();
  112. return elt.int_val;
  113. }
  114. std::vector<struct type *> *pop_typelist ()
  115. {
  116. gdb_assert (!m_elements.empty ());
  117. type_stack_elt elt = m_elements.back ();
  118. m_elements.pop_back ();
  119. return elt.typelist_val;
  120. }
  121. /* Pop a type_stack element. */
  122. struct type_stack *pop_type_stack ()
  123. {
  124. gdb_assert (!m_elements.empty ());
  125. type_stack_elt elt = m_elements.back ();
  126. m_elements.pop_back ();
  127. return elt.stack_val;
  128. }
  129. /* Insert a tp_space_identifier and the corresponding address space
  130. value into the stack. STRING is the name of an address space, as
  131. recognized by address_space_name_to_type_instance_flags. If the
  132. stack is empty, the new elements are simply pushed. If the stack
  133. is not empty, this function assumes that the first item on the
  134. stack is a tp_pointer, and the new values are inserted above the
  135. first item. */
  136. void insert (struct expr_builder *pstate, const char *string);
  137. /* Append the elements of the type stack FROM to the type stack
  138. THIS. Always returns THIS. */
  139. struct type_stack *append (struct type_stack *from)
  140. {
  141. m_elements.insert (m_elements.end (), from->m_elements.begin (),
  142. from->m_elements.end ());
  143. return this;
  144. }
  145. /* Pop the type stack and return a type_instance_flags that
  146. corresponds the const/volatile qualifiers on the stack. This is
  147. called by the C++ parser when parsing methods types, and as such no
  148. other kind of type in the type stack is expected. */
  149. type_instance_flags follow_type_instance_flags ();
  150. /* Pop the type stack and return the type which corresponds to
  151. FOLLOW_TYPE as modified by all the stuff on the stack. */
  152. struct type *follow_types (struct type *follow_type);
  153. private:
  154. /* A helper function for insert_type and insert_type_address_space.
  155. This does work of expanding the type stack and inserting the new
  156. element, ELEMENT, into the stack at location SLOT. */
  157. void insert_into (int slot, union type_stack_elt element)
  158. {
  159. gdb_assert (slot <= m_elements.size ());
  160. m_elements.insert (m_elements.begin () + slot, element);
  161. }
  162. /* Elements on the stack. */
  163. std::vector<union type_stack_elt> m_elements;
  164. };
  165. #endif /* TYPE_STACK_H */