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