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