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