Remove path name from test case
[binutils-gdb.git] / gdb / parser-defs.h
1 /* Parser definitions for GDB.
2
3 Copyright (C) 1986-2023 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 /* A class that can be used to build a "struct expression". */
36
37 struct expr_builder
38 {
39 /* Constructor. LANG is the language used to parse the expression.
40 And GDBARCH is the gdbarch to use during parsing. */
41
42 expr_builder (const struct language_defn *lang,
43 struct gdbarch *gdbarch)
44 : expout (new expression (lang, gdbarch))
45 {
46 }
47
48 DISABLE_COPY_AND_ASSIGN (expr_builder);
49
50 /* Resize the allocated expression to the correct size, and return
51 it as an expression_up -- passing ownership to the caller. */
52 ATTRIBUTE_UNUSED_RESULT expression_up release ()
53 {
54 return std::move (expout);
55 }
56
57 /* Return the gdbarch that was passed to the constructor. */
58
59 struct gdbarch *gdbarch ()
60 {
61 return expout->gdbarch;
62 }
63
64 /* Return the language that was passed to the constructor. */
65
66 const struct language_defn *language ()
67 {
68 return expout->language_defn;
69 }
70
71 /* Set the root operation of the expression that is currently being
72 built. */
73 void set_operation (expr::operation_up &&op)
74 {
75 expout->op = std::move (op);
76 }
77
78 /* The expression related to this parser state. */
79
80 expression_up expout;
81 };
82
83 /* Complete an expression that references a field, like "x->y". */
84
85 struct expr_complete_structop : public expr_completion_base
86 {
87 explicit expr_complete_structop (expr::structop_base_operation *op)
88 : m_op (op)
89 {
90 }
91
92 bool complete (struct expression *exp,
93 completion_tracker &tracker) override
94 {
95 return m_op->complete (exp, tracker);
96 }
97
98 private:
99
100 /* The last struct expression directly before a '.' or '->'. This
101 is set when parsing and is only used when completing a field
102 name. It is nullptr if no dereference operation was found. */
103 expr::structop_base_operation *m_op = nullptr;
104 };
105
106 /* Complete a tag name in an expression. This is used for something
107 like "enum abc<TAB>". */
108
109 struct expr_complete_tag : public expr_completion_base
110 {
111 expr_complete_tag (enum type_code code,
112 gdb::unique_xmalloc_ptr<char> name)
113 : m_code (code),
114 m_name (std::move (name))
115 {
116 /* Parsers should enforce this statically. */
117 gdb_assert (code == TYPE_CODE_ENUM
118 || code == TYPE_CODE_UNION
119 || code == TYPE_CODE_STRUCT);
120 }
121
122 bool complete (struct expression *exp,
123 completion_tracker &tracker) override;
124
125 private:
126
127 /* The kind of tag to complete. */
128 enum type_code m_code;
129
130 /* The token for tagged type name completion. */
131 gdb::unique_xmalloc_ptr<char> m_name;
132 };
133
134 /* An instance of this type is instantiated during expression parsing,
135 and passed to the appropriate parser. It holds both inputs to the
136 parser, and result. */
137
138 struct parser_state : public expr_builder
139 {
140 /* Constructor. LANG is the language used to parse the expression.
141 And GDBARCH is the gdbarch to use during parsing. */
142
143 parser_state (const struct language_defn *lang,
144 struct gdbarch *gdbarch,
145 const struct block *context_block,
146 CORE_ADDR context_pc,
147 parser_flags flags,
148 const char *input,
149 bool completion,
150 innermost_block_tracker *tracker)
151 : expr_builder (lang, gdbarch),
152 expression_context_block (context_block),
153 expression_context_pc (context_pc),
154 lexptr (input),
155 block_tracker (tracker),
156 comma_terminates ((flags & PARSER_COMMA_TERMINATES) != 0),
157 parse_completion (completion),
158 void_context_p ((flags & PARSER_VOID_CONTEXT) != 0),
159 debug ((flags & PARSER_DEBUG) != 0)
160 {
161 }
162
163 DISABLE_COPY_AND_ASSIGN (parser_state);
164
165 /* Begin counting arguments for a function call,
166 saving the data about any containing call. */
167
168 void start_arglist ()
169 {
170 m_funcall_chain.push_back (arglist_len);
171 arglist_len = 0;
172 }
173
174 /* Return the number of arguments in a function call just terminated,
175 and restore the data for the containing function call. */
176
177 int end_arglist ()
178 {
179 int val = arglist_len;
180 arglist_len = m_funcall_chain.back ();
181 m_funcall_chain.pop_back ();
182 return val;
183 }
184
185 /* Mark the given operation as the starting location of a structure
186 expression. This is used when completing on field names. */
187
188 void mark_struct_expression (expr::structop_base_operation *op);
189
190 /* Indicate that the current parser invocation is completing a tag.
191 TAG is the type code of the tag, and PTR and LENGTH represent the
192 start of the tag name. */
193
194 void mark_completion_tag (enum type_code tag, const char *ptr, int length);
195
196 /* Mark for completion, using an arbitrary completer. */
197
198 void mark_completion (std::unique_ptr<expr_completion_base> completer)
199 {
200 gdb_assert (m_completion_state == nullptr);
201 m_completion_state = std::move (completer);
202 }
203
204 /* Push an operation on the stack. */
205 void push (expr::operation_up &&op)
206 {
207 m_operations.push_back (std::move (op));
208 }
209
210 /* Create a new operation and push it on the stack. */
211 template<typename T, typename... Arg>
212 void push_new (Arg... args)
213 {
214 m_operations.emplace_back (new T (std::forward<Arg> (args)...));
215 }
216
217 /* Push a new C string operation. */
218 void push_c_string (int, struct stoken_vector *vec);
219
220 /* Push a symbol reference. If SYM is nullptr, look for a minimal
221 symbol. */
222 void push_symbol (const char *name, block_symbol sym);
223
224 /* Push a reference to $mumble. This may result in a convenience
225 variable, a history reference, or a register. */
226 void push_dollar (struct stoken str);
227
228 /* Pop an operation from the stack. */
229 expr::operation_up pop ()
230 {
231 expr::operation_up result = std::move (m_operations.back ());
232 m_operations.pop_back ();
233 return result;
234 }
235
236 /* Pop N elements from the stack and return a vector. */
237 std::vector<expr::operation_up> pop_vector (int n)
238 {
239 std::vector<expr::operation_up> result (n);
240 for (int i = 1; i <= n; ++i)
241 result[n - i] = pop ();
242 return result;
243 }
244
245 /* A helper that pops an operation, wraps it in some other
246 operation, and pushes it again. */
247 template<typename T>
248 void wrap ()
249 {
250 using namespace expr;
251 operation_up v = ::expr::make_operation<T> (pop ());
252 push (std::move (v));
253 }
254
255 /* A helper that pops two operations, wraps them in some other
256 operation, and pushes the result. */
257 template<typename T>
258 void wrap2 ()
259 {
260 expr::operation_up rhs = pop ();
261 expr::operation_up lhs = pop ();
262 push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
263 }
264
265 /* If this is nonzero, this block is used as the lexical context for
266 symbol names. */
267
268 const struct block * const expression_context_block;
269
270 /* If expression_context_block is non-zero, then this is the PC
271 within the block that we want to evaluate expressions at. When
272 debugging C or C++ code, we use this to find the exact line we're
273 at, and then look up the macro definitions active at that
274 point. */
275 const CORE_ADDR expression_context_pc;
276
277 /* During parsing of a C expression, the pointer to the next character
278 is in this variable. */
279
280 const char *lexptr;
281
282 /* After a token has been recognized, this variable points to it.
283 Currently used only for error reporting. */
284 const char *prev_lexptr = nullptr;
285
286 /* Number of arguments seen so far in innermost function call. */
287
288 int arglist_len = 0;
289
290 /* Completion state is updated here. */
291 std::unique_ptr<expr_completion_base> m_completion_state;
292
293 /* The innermost block tracker. */
294 innermost_block_tracker *block_tracker;
295
296 /* Nonzero means stop parsing on first comma (if not within parentheses). */
297 bool comma_terminates;
298
299 /* True if parsing an expression to attempt completion. */
300 bool parse_completion;
301
302 /* True if no value is expected from the expression. */
303 bool void_context_p;
304
305 /* True if parser debugging should be enabled. */
306 bool debug;
307
308 private:
309
310 /* Data structure for saving values of arglist_len for function calls whose
311 arguments contain other function calls. */
312
313 std::vector<int> m_funcall_chain;
314
315 /* Stack of operations. */
316 std::vector<expr::operation_up> m_operations;
317 };
318
319 /* A string token, either a char-string or bit-string. Char-strings are
320 used, for example, for the names of symbols. */
321
322 struct stoken
323 {
324 /* Pointer to first byte of char-string or first bit of bit-string. */
325 const char *ptr;
326 /* Length of string in bytes for char-string or bits for bit-string. */
327 int length;
328 };
329
330 struct typed_stoken
331 {
332 /* A language-specific type field. */
333 int type;
334 /* Pointer to first byte of char-string or first bit of bit-string. */
335 char *ptr;
336 /* Length of string in bytes for char-string or bits for bit-string. */
337 int length;
338 };
339
340 struct stoken_vector
341 {
342 int len;
343 struct typed_stoken *tokens;
344 };
345
346 struct ttype
347 {
348 struct stoken stoken;
349 struct type *type;
350 };
351
352 struct symtoken
353 {
354 struct stoken stoken;
355 struct block_symbol sym;
356 int is_a_field_of_this;
357 };
358
359 struct objc_class_str
360 {
361 struct stoken stoken;
362 struct type *type;
363 int theclass;
364 };
365
366 extern const char *find_template_name_end (const char *);
367
368 extern std::string copy_name (struct stoken);
369
370 extern bool parse_float (const char *p, int len,
371 const struct type *type, gdb_byte *data);
372 extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits,
373 bool type_signed_p);
374 extern bool fits_in_type (int n_sign, const gdb_mpz &n, int type_bits,
375 bool type_signed_p);
376 \f
377
378 /* Function used to avoid direct calls to fprintf
379 in the code generated by the bison parser. */
380
381 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
382
383 #endif /* PARSER_DEFS_H */
384