type-stack.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #include "defs.h"
  15. #include "type-stack.h"
  16. #include "gdbtypes.h"
  17. #include "parser-defs.h"
  18. /* See type-stack.h. */
  19. void
  20. type_stack::insert (enum type_pieces tp)
  21. {
  22. union type_stack_elt element;
  23. int slot;
  24. gdb_assert (tp == tp_pointer || tp == tp_reference
  25. || tp == tp_rvalue_reference || tp == tp_const
  26. || tp == tp_volatile || tp == tp_restrict
  27. || tp == tp_atomic);
  28. /* If there is anything on the stack (we know it will be a
  29. tp_pointer), insert the qualifier above it. Otherwise, simply
  30. push this on the top of the stack. */
  31. if (!m_elements.empty () && (tp == tp_const || tp == tp_volatile
  32. || tp == tp_restrict))
  33. slot = 1;
  34. else
  35. slot = 0;
  36. element.piece = tp;
  37. insert_into (slot, element);
  38. }
  39. /* See type-stack.h. */
  40. void
  41. type_stack::insert (struct expr_builder *pstate, const char *string)
  42. {
  43. union type_stack_elt element;
  44. int slot;
  45. /* If there is anything on the stack (we know it will be a
  46. tp_pointer), insert the address space qualifier above it.
  47. Otherwise, simply push this on the top of the stack. */
  48. if (!m_elements.empty ())
  49. slot = 1;
  50. else
  51. slot = 0;
  52. element.piece = tp_space_identifier;
  53. insert_into (slot, element);
  54. element.int_val
  55. = address_space_name_to_type_instance_flags (pstate->gdbarch (),
  56. string);
  57. insert_into (slot, element);
  58. }
  59. /* See type-stack.h. */
  60. type_instance_flags
  61. type_stack::follow_type_instance_flags ()
  62. {
  63. type_instance_flags flags = 0;
  64. for (;;)
  65. switch (pop ())
  66. {
  67. case tp_end:
  68. return flags;
  69. case tp_const:
  70. flags |= TYPE_INSTANCE_FLAG_CONST;
  71. break;
  72. case tp_volatile:
  73. flags |= TYPE_INSTANCE_FLAG_VOLATILE;
  74. break;
  75. case tp_atomic:
  76. flags |= TYPE_INSTANCE_FLAG_ATOMIC;
  77. break;
  78. case tp_restrict:
  79. flags |= TYPE_INSTANCE_FLAG_RESTRICT;
  80. break;
  81. default:
  82. gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
  83. }
  84. }
  85. /* See type-stack.h. */
  86. struct type *
  87. type_stack::follow_types (struct type *follow_type)
  88. {
  89. int done = 0;
  90. int make_const = 0;
  91. int make_volatile = 0;
  92. type_instance_flags make_addr_space = 0;
  93. bool make_restrict = false;
  94. bool make_atomic = false;
  95. int array_size;
  96. while (!done)
  97. switch (pop ())
  98. {
  99. case tp_end:
  100. done = 1;
  101. goto process_qualifiers;
  102. break;
  103. case tp_const:
  104. make_const = 1;
  105. break;
  106. case tp_volatile:
  107. make_volatile = 1;
  108. break;
  109. case tp_space_identifier:
  110. make_addr_space = (enum type_instance_flag_value) pop_int ();
  111. break;
  112. case tp_atomic:
  113. make_atomic = true;
  114. break;
  115. case tp_restrict:
  116. make_restrict = true;
  117. break;
  118. case tp_pointer:
  119. follow_type = lookup_pointer_type (follow_type);
  120. goto process_qualifiers;
  121. case tp_reference:
  122. follow_type = lookup_lvalue_reference_type (follow_type);
  123. goto process_qualifiers;
  124. case tp_rvalue_reference:
  125. follow_type = lookup_rvalue_reference_type (follow_type);
  126. process_qualifiers:
  127. if (make_const)
  128. follow_type = make_cv_type (make_const,
  129. TYPE_VOLATILE (follow_type),
  130. follow_type, 0);
  131. if (make_volatile)
  132. follow_type = make_cv_type (TYPE_CONST (follow_type),
  133. make_volatile,
  134. follow_type, 0);
  135. if (make_addr_space)
  136. follow_type = make_type_with_address_space (follow_type,
  137. make_addr_space);
  138. if (make_restrict)
  139. follow_type = make_restrict_type (follow_type);
  140. if (make_atomic)
  141. follow_type = make_atomic_type (follow_type);
  142. make_const = make_volatile = 0;
  143. make_addr_space = 0;
  144. make_restrict = make_atomic = false;
  145. break;
  146. case tp_array:
  147. array_size = pop_int ();
  148. /* FIXME-type-allocation: need a way to free this type when we are
  149. done with it. */
  150. follow_type =
  151. lookup_array_range_type (follow_type,
  152. 0, array_size >= 0 ? array_size - 1 : 0);
  153. if (array_size < 0)
  154. follow_type->bounds ()->high.set_undefined ();
  155. break;
  156. case tp_function:
  157. /* FIXME-type-allocation: need a way to free this type when we are
  158. done with it. */
  159. follow_type = lookup_function_type (follow_type);
  160. break;
  161. case tp_function_with_arguments:
  162. {
  163. std::vector<struct type *> *args = pop_typelist ();
  164. follow_type
  165. = lookup_function_type_with_arguments (follow_type,
  166. args->size (),
  167. args->data ());
  168. }
  169. break;
  170. case tp_type_stack:
  171. {
  172. struct type_stack *stack = pop_type_stack ();
  173. follow_type = stack->follow_types (follow_type);
  174. }
  175. break;
  176. default:
  177. gdb_assert_not_reached ("unrecognized tp_ value in follow_types");
  178. }
  179. return follow_type;
  180. }