Remove union exp_element
[deliverable/binutils-gdb.git] / gdb / parser-defs.h
1 /* Parser definitions for GDB.
2
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
4
5 Modified from expread.y by the Department of Computer Science at the
6 State University of New York at Buffalo.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #if !defined (PARSER_DEFS_H)
24 #define PARSER_DEFS_H 1
25
26 #include "expression.h"
27 #include "symtab.h"
28 #include "expop.h"
29
30 struct block;
31 struct language_defn;
32 struct internalvar;
33 class innermost_block_tracker;
34
35 extern bool parser_debug;
36
37 /* A class that can be used to build a "struct expression". */
38
39 struct expr_builder
40 {
41 /* Constructor. LANG is the language used to parse the expression.
42 And GDBARCH is the gdbarch to use during parsing. */
43
44 expr_builder (const struct language_defn *lang,
45 struct gdbarch *gdbarch);
46
47 DISABLE_COPY_AND_ASSIGN (expr_builder);
48
49 /* Resize the allocated expression to the correct size, and return
50 it as an expression_up -- passing ownership to the caller. */
51 ATTRIBUTE_UNUSED_RESULT expression_up release ();
52
53 /* Return the gdbarch that was passed to the constructor. */
54
55 struct gdbarch *gdbarch ()
56 {
57 return expout->gdbarch;
58 }
59
60 /* Return the language that was passed to the constructor. */
61
62 const struct language_defn *language ()
63 {
64 return expout->language_defn;
65 }
66
67 /* Set the root operation of the expression that is currently being
68 built. */
69 void set_operation (expr::operation_up &&op)
70 {
71 expout->op = std::move (op);
72 }
73
74 /* The expression related to this parser state. */
75
76 expression_up expout;
77 };
78
79 /* This is used for expression completion. */
80
81 struct expr_completion_state
82 {
83 /* The last struct expression directly before a '.' or '->'. This
84 is set when parsing and is only used when completing a field
85 name. It is nullptr if no dereference operation was found. */
86 expr::structop_base_operation *expout_last_op = nullptr;
87
88 /* If we are completing a tagged type name, this will be nonzero. */
89 enum type_code expout_tag_completion_type = TYPE_CODE_UNDEF;
90
91 /* The token for tagged type name completion. */
92 gdb::unique_xmalloc_ptr<char> expout_completion_name;
93 };
94
95 /* An instance of this type is instantiated during expression parsing,
96 and passed to the appropriate parser. It holds both inputs to the
97 parser, and result. */
98
99 struct parser_state : public expr_builder
100 {
101 /* Constructor. LANG is the language used to parse the expression.
102 And GDBARCH is the gdbarch to use during parsing. */
103
104 parser_state (const struct language_defn *lang,
105 struct gdbarch *gdbarch,
106 const struct block *context_block,
107 CORE_ADDR context_pc,
108 int comma,
109 const char *input,
110 int completion,
111 innermost_block_tracker *tracker,
112 bool void_p)
113 : expr_builder (lang, gdbarch),
114 expression_context_block (context_block),
115 expression_context_pc (context_pc),
116 comma_terminates (comma),
117 lexptr (input),
118 parse_completion (completion),
119 block_tracker (tracker),
120 void_context_p (void_p)
121 {
122 }
123
124 DISABLE_COPY_AND_ASSIGN (parser_state);
125
126 /* Begin counting arguments for a function call,
127 saving the data about any containing call. */
128
129 void start_arglist ()
130 {
131 m_funcall_chain.push_back (arglist_len);
132 arglist_len = 0;
133 }
134
135 /* Return the number of arguments in a function call just terminated,
136 and restore the data for the containing function call. */
137
138 int end_arglist ()
139 {
140 int val = arglist_len;
141 arglist_len = m_funcall_chain.back ();
142 m_funcall_chain.pop_back ();
143 return val;
144 }
145
146 /* Mark the given operation as the starting location of a structure
147 expression. This is used when completing on field names. */
148
149 void mark_struct_expression (expr::structop_base_operation *op);
150
151 /* Indicate that the current parser invocation is completing a tag.
152 TAG is the type code of the tag, and PTR and LENGTH represent the
153 start of the tag name. */
154
155 void mark_completion_tag (enum type_code tag, const char *ptr, int length);
156
157 /* Push an operation on the stack. */
158 void push (expr::operation_up &&op)
159 {
160 m_operations.push_back (std::move (op));
161 }
162
163 /* Create a new operation and push it on the stack. */
164 template<typename T, typename... Arg>
165 void push_new (Arg... args)
166 {
167 m_operations.emplace_back (new T (std::forward<Arg> (args)...));
168 }
169
170 /* Push a new C string operation. */
171 void push_c_string (int, struct stoken_vector *vec);
172
173 /* Push a symbol reference. If SYM is nullptr, look for a minimal
174 symbol. */
175 void push_symbol (const char *name, block_symbol sym);
176
177 /* Push a reference to $mumble. This may result in a convenience
178 variable, a history reference, or a register. */
179 void push_dollar (struct stoken str);
180
181 /* Pop an operation from the stack. */
182 expr::operation_up pop ()
183 {
184 expr::operation_up result = std::move (m_operations.back ());
185 m_operations.pop_back ();
186 return result;
187 }
188
189 /* Pop N elements from the stack and return a vector. */
190 std::vector<expr::operation_up> pop_vector (int n)
191 {
192 std::vector<expr::operation_up> result (n);
193 for (int i = 1; i <= n; ++i)
194 result[n - i] = pop ();
195 return result;
196 }
197
198 /* A helper that pops an operation, wraps it in some other
199 operation, and pushes it again. */
200 template<typename T>
201 void wrap ()
202 {
203 using namespace expr;
204 operation_up v = ::expr::make_operation<T> (pop ());
205 push (std::move (v));
206 }
207
208 /* A helper that pops two operations, wraps them in some other
209 operation, and pushes the result. */
210 template<typename T>
211 void wrap2 ()
212 {
213 expr::operation_up rhs = pop ();
214 expr::operation_up lhs = pop ();
215 push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
216 }
217
218 /* If this is nonzero, this block is used as the lexical context for
219 symbol names. */
220
221 const struct block * const expression_context_block;
222
223 /* If expression_context_block is non-zero, then this is the PC
224 within the block that we want to evaluate expressions at. When
225 debugging C or C++ code, we use this to find the exact line we're
226 at, and then look up the macro definitions active at that
227 point. */
228 const CORE_ADDR expression_context_pc;
229
230 /* Nonzero means stop parsing on first comma (if not within parentheses). */
231
232 int comma_terminates;
233
234 /* During parsing of a C expression, the pointer to the next character
235 is in this variable. */
236
237 const char *lexptr;
238
239 /* After a token has been recognized, this variable points to it.
240 Currently used only for error reporting. */
241 const char *prev_lexptr = nullptr;
242
243 /* Number of arguments seen so far in innermost function call. */
244
245 int arglist_len = 0;
246
247 /* True if parsing an expression to attempt completion. */
248 int parse_completion;
249
250 /* Completion state is updated here. */
251 expr_completion_state m_completion_state;
252
253 /* The innermost block tracker. */
254 innermost_block_tracker *block_tracker;
255
256 /* True if no value is expected from the expression. */
257 bool void_context_p;
258
259 private:
260
261 /* Data structure for saving values of arglist_len for function calls whose
262 arguments contain other function calls. */
263
264 std::vector<int> m_funcall_chain;
265
266 /* Stack of operations. */
267 std::vector<expr::operation_up> m_operations;
268 };
269
270 /* When parsing expressions we track the innermost block that was
271 referenced. */
272
273 class innermost_block_tracker
274 {
275 public:
276 innermost_block_tracker (innermost_block_tracker_types types
277 = INNERMOST_BLOCK_FOR_SYMBOLS)
278 : m_types (types),
279 m_innermost_block (NULL)
280 { /* Nothing. */ }
281
282 /* Update the stored innermost block if the new block B is more inner
283 than the currently stored block, or if no block is stored yet. The
284 type T tells us whether the block B was for a symbol or for a
285 register. The stored innermost block is only updated if the type T is
286 a type we are interested in, the types we are interested in are held
287 in M_TYPES and set during RESET. */
288 void update (const struct block *b, innermost_block_tracker_types t);
289
290 /* Overload of main UPDATE method which extracts the block from BS. */
291 void update (const struct block_symbol &bs)
292 {
293 update (bs.block, INNERMOST_BLOCK_FOR_SYMBOLS);
294 }
295
296 /* Return the stored innermost block. Can be nullptr if no symbols or
297 registers were found during an expression parse, and so no innermost
298 block was defined. */
299 const struct block *block () const
300 {
301 return m_innermost_block;
302 }
303
304 private:
305 /* The type of innermost block being looked for. */
306 innermost_block_tracker_types m_types;
307
308 /* The currently stored innermost block found while parsing an
309 expression. */
310 const struct block *m_innermost_block;
311 };
312
313 /* A string token, either a char-string or bit-string. Char-strings are
314 used, for example, for the names of symbols. */
315
316 struct stoken
317 {
318 /* Pointer to first byte of char-string or first bit of bit-string. */
319 const char *ptr;
320 /* Length of string in bytes for char-string or bits for bit-string. */
321 int length;
322 };
323
324 struct typed_stoken
325 {
326 /* A language-specific type field. */
327 int type;
328 /* Pointer to first byte of char-string or first bit of bit-string. */
329 char *ptr;
330 /* Length of string in bytes for char-string or bits for bit-string. */
331 int length;
332 };
333
334 struct stoken_vector
335 {
336 int len;
337 struct typed_stoken *tokens;
338 };
339
340 struct ttype
341 {
342 struct stoken stoken;
343 struct type *type;
344 };
345
346 struct symtoken
347 {
348 struct stoken stoken;
349 struct block_symbol sym;
350 int is_a_field_of_this;
351 };
352
353 struct objc_class_str
354 {
355 struct stoken stoken;
356 struct type *type;
357 int theclass;
358 };
359
360 extern const char *find_template_name_end (const char *);
361
362 extern std::string copy_name (struct stoken);
363
364 extern bool parse_float (const char *p, int len,
365 const struct type *type, gdb_byte *data);
366 \f
367
368 /* Function used to avoid direct calls to fprintf
369 in the code generated by the bison parser. */
370
371 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
372
373 extern int exp_uses_objfile (struct expression *exp, struct objfile *objfile);
374
375 #endif /* PARSER_DEFS_H */
376
This page took 0.038485 seconds and 5 git commands to generate.