GDB (xrefs)
Loading...
Searching...
No Matches
/tmp/gdb-13.1/gdb/parser-defs.h
Go to the documentation of this file.
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
30struct block;
31struct language_defn;
32struct internalvar;
34
35extern bool parser_debug;
36
37/* A class that can be used to build a "struct expression". */
38
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 : expout (new expression (lang, gdbarch))
47 {
48 }
49
51
52 /* Resize the allocated expression to the correct size, and return
53 it as an expression_up -- passing ownership to the caller. */
54 ATTRIBUTE_UNUSED_RESULT expression_up release ()
55 {
56 return std::move (expout);
57 }
58
59 /* Return the gdbarch that was passed to the constructor. */
60
61 struct gdbarch *gdbarch ()
62 {
63 return expout->gdbarch;
64 }
65
66 /* Return the language that was passed to the constructor. */
67
68 const struct language_defn *language ()
69 {
70 return expout->language_defn;
71 }
72
73 /* Set the root operation of the expression that is currently being
74 built. */
76 {
77 expout->op = std::move (op);
78 }
79
80 /* The expression related to this parser state. */
81
83};
84
85/* Complete an expression that references a field, like "x->y". */
86
88{
90 : m_op (op)
91 {
92 }
93
94 bool complete (struct expression *exp,
95 completion_tracker &tracker) override
96 {
97 return m_op->complete (exp, tracker);
98 }
99
100private:
101
102 /* The last struct expression directly before a '.' or '->'. This
103 is set when parsing and is only used when completing a field
104 name. It is nullptr if no dereference operation was found. */
106};
107
108/* Complete a tag name in an expression. This is used for something
109 like "enum abc<TAB>". */
110
112{
114 gdb::unique_xmalloc_ptr<char> name)
115 : m_code (code),
116 m_name (std::move (name))
117 {
118 /* Parsers should enforce this statically. */
119 gdb_assert (code == TYPE_CODE_ENUM
120 || code == TYPE_CODE_UNION
121 || code == TYPE_CODE_STRUCT);
122 }
123
124 bool complete (struct expression *exp,
125 completion_tracker &tracker) override;
126
127private:
128
129 /* The kind of tag to complete. */
131
132 /* The token for tagged type name completion. */
133 gdb::unique_xmalloc_ptr<char> m_name;
134};
135
136/* An instance of this type is instantiated during expression parsing,
137 and passed to the appropriate parser. It holds both inputs to the
138 parser, and result. */
139
141{
142 /* Constructor. LANG is the language used to parse the expression.
143 And GDBARCH is the gdbarch to use during parsing. */
144
145 parser_state (const struct language_defn *lang,
146 struct gdbarch *gdbarch,
147 const struct block *context_block,
148 CORE_ADDR context_pc,
149 int comma,
150 const char *input,
151 bool completion,
153 bool void_p)
154 : expr_builder (lang, gdbarch),
155 expression_context_block (context_block),
156 expression_context_pc (context_pc),
157 comma_terminates (comma),
158 lexptr (input),
159 parse_completion (completion),
160 block_tracker (tracker),
161 void_context_p (void_p)
162 {
163 }
164
166
167 /* Begin counting arguments for a function call,
168 saving the data about any containing call. */
169
171 {
172 m_funcall_chain.push_back (arglist_len);
173 arglist_len = 0;
174 }
175
176 /* Return the number of arguments in a function call just terminated,
177 and restore the data for the containing function call. */
178
180 {
181 int val = arglist_len;
183 m_funcall_chain.pop_back ();
184 return val;
185 }
186
187 /* Mark the given operation as the starting location of a structure
188 expression. This is used when completing on field names. */
189
191
192 /* Indicate that the current parser invocation is completing a tag.
193 TAG is the type code of the tag, and PTR and LENGTH represent the
194 start of the tag name. */
195
196 void mark_completion_tag (enum type_code tag, const char *ptr, int length);
197
198 /* Mark for completion, using an arbitrary completer. */
199
200 void mark_completion (std::unique_ptr<expr_completion_base> completer)
201 {
202 gdb_assert (m_completion_state == nullptr);
203 m_completion_state = std::move (completer);
204 }
205
206 /* Push an operation on the stack. */
208 {
209 m_operations.push_back (std::move (op));
210 }
211
212 /* Create a new operation and push it on the stack. */
213 template<typename T, typename... Arg>
214 void push_new (Arg... args)
215 {
216 m_operations.emplace_back (new T (std::forward<Arg> (args)...));
217 }
218
219 /* Push a new C string operation. */
220 void push_c_string (int, struct stoken_vector *vec);
221
222 /* Push a symbol reference. If SYM is nullptr, look for a minimal
223 symbol. */
224 void push_symbol (const char *name, block_symbol sym);
225
226 /* Push a reference to $mumble. This may result in a convenience
227 variable, a history reference, or a register. */
228 void push_dollar (struct stoken str);
229
230 /* Pop an operation from the stack. */
232 {
233 expr::operation_up result = std::move (m_operations.back ());
234 m_operations.pop_back ();
235 return result;
236 }
237
238 /* Pop N elements from the stack and return a vector. */
239 std::vector<expr::operation_up> pop_vector (int n)
240 {
241 std::vector<expr::operation_up> result (n);
242 for (int i = 1; i <= n; ++i)
243 result[n - i] = pop ();
244 return result;
245 }
246
247 /* A helper that pops an operation, wraps it in some other
248 operation, and pushes it again. */
249 template<typename T>
250 void wrap ()
251 {
252 using namespace expr;
253 operation_up v = ::expr::make_operation<T> (pop ());
254 push (std::move (v));
255 }
256
257 /* A helper that pops two operations, wraps them in some other
258 operation, and pushes the result. */
259 template<typename T>
260 void wrap2 ()
261 {
262 expr::operation_up rhs = pop ();
263 expr::operation_up lhs = pop ();
264 push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
265 }
266
267 /* If this is nonzero, this block is used as the lexical context for
268 symbol names. */
269
270 const struct block * const expression_context_block;
271
272 /* If expression_context_block is non-zero, then this is the PC
273 within the block that we want to evaluate expressions at. When
274 debugging C or C++ code, we use this to find the exact line we're
275 at, and then look up the macro definitions active at that
276 point. */
277 const CORE_ADDR expression_context_pc;
278
279 /* Nonzero means stop parsing on first comma (if not within parentheses). */
280
282
283 /* During parsing of a C expression, the pointer to the next character
284 is in this variable. */
285
286 const char *lexptr;
287
288 /* After a token has been recognized, this variable points to it.
289 Currently used only for error reporting. */
290 const char *prev_lexptr = nullptr;
291
292 /* Number of arguments seen so far in innermost function call. */
293
294 int arglist_len = 0;
295
296 /* True if parsing an expression to attempt completion. */
298
299 /* Completion state is updated here. */
300 std::unique_ptr<expr_completion_base> m_completion_state;
301
302 /* The innermost block tracker. */
304
305 /* True if no value is expected from the expression. */
307
308private:
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/* When parsing expressions we track the innermost block that was
320 referenced. */
321
323{
324public:
325 innermost_block_tracker (innermost_block_tracker_types types
327 : m_types (types),
328 m_innermost_block (NULL)
329 { /* Nothing. */ }
330
331 /* Update the stored innermost block if the new block B is more inner
332 than the currently stored block, or if no block is stored yet. The
333 type T tells us whether the block B was for a symbol or for a
334 register. The stored innermost block is only updated if the type T is
335 a type we are interested in, the types we are interested in are held
336 in M_TYPES and set during RESET. */
337 void update (const struct block *b, innermost_block_tracker_types t);
338
339 /* Overload of main UPDATE method which extracts the block from BS. */
340 void update (const struct block_symbol &bs)
341 {
343 }
344
345 /* Return the stored innermost block. Can be nullptr if no symbols or
346 registers were found during an expression parse, and so no innermost
347 block was defined. */
348 const struct block *block () const
349 {
350 return m_innermost_block;
351 }
352
353private:
354 /* The type of innermost block being looked for. */
355 innermost_block_tracker_types m_types;
356
357 /* The currently stored innermost block found while parsing an
358 expression. */
360};
361
362/* A string token, either a char-string or bit-string. Char-strings are
363 used, for example, for the names of symbols. */
364
365struct stoken
366 {
367 /* Pointer to first byte of char-string or first bit of bit-string. */
368 const char *ptr;
369 /* Length of string in bytes for char-string or bits for bit-string. */
371 };
372
374 {
375 /* A language-specific type field. */
376 int type;
377 /* Pointer to first byte of char-string or first bit of bit-string. */
378 char *ptr;
379 /* Length of string in bytes for char-string or bits for bit-string. */
381 };
382
384 {
385 int len;
387 };
388
389struct ttype
390 {
392 struct type *type;
393 };
394
396 {
400 };
401
403 {
405 struct type *type;
407 };
408
409extern const char *find_template_name_end (const char *);
410
411extern std::string copy_name (struct stoken);
412
413extern bool parse_float (const char *p, int len,
414 const struct type *type, gdb_byte *data);
415extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits,
416 bool type_signed_p);
417
418
419/* Function used to avoid direct calls to fprintf
420 in the code generated by the bison parser. */
421
422extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
423
424extern bool exp_uses_objfile (struct expression *exp, struct objfile *objfile);
425
426#endif /* PARSER_DEFS_H */
427
const char *const name
Definition: aarch64-tdep.c:67
int code
Definition: ada-lex.l:688
virtual bool complete(struct expression *exp, completion_tracker &tracker)
Definition: expop.h:1008
innermost_block_tracker_types m_types
Definition: parser-defs.h:355
innermost_block_tracker(innermost_block_tracker_types types=INNERMOST_BLOCK_FOR_SYMBOLS)
Definition: parser-defs.h:325
void update(const struct block *b, innermost_block_tracker_types t)
Definition: parse.c:84
void update(const struct block_symbol &bs)
Definition: parser-defs.h:340
const struct block * m_innermost_block
Definition: parser-defs.h:359
const struct block * block() const
Definition: parser-defs.h:348
std::unique_ptr< expression > expression_up
Definition: expression.h:229
@ INNERMOST_BLOCK_FOR_SYMBOLS
Definition: expression.h:35
static void ATTRIBUTE_PRINTF(1, 0)
Definition: gdb_bfd.c:1150
type_code
Definition: gdbtypes.h:99
Definition: ada-exp.h:80
std::unique_ptr< operation > operation_up
Definition: expression.h:81
Definition: aarch64.h:50
bool parse_float(const char *p, int len, const struct type *type, gdb_byte *data)
Definition: parse.c:609
bool fits_in_type(int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
Definition: parse.c:619
const char * find_template_name_end(const char *)
Definition: parse.c:338
void parser_fprintf(FILE *, const char *,...) ATTRIBUTE_PRINTF(2
std::string copy_name(struct stoken)
Definition: parse.c:407
void bool exp_uses_objfile(struct expression *exp, struct objfile *objfile)
Definition: parse.c:675
bool parser_debug
Definition: parse.c:65
Definition: 1.cc:26
const struct block * block
Definition: symtab.h:1498
Definition: block.h:109
DISABLE_COPY_AND_ASSIGN(expr_builder)
void set_operation(expr::operation_up &&op)
Definition: parser-defs.h:75
const struct language_defn * language()
Definition: parser-defs.h:68
struct gdbarch * gdbarch()
Definition: parser-defs.h:61
expression_up expout
Definition: parser-defs.h:82
ATTRIBUTE_UNUSED_RESULT expression_up release()
Definition: parser-defs.h:54
expr_builder(const struct language_defn *lang, struct gdbarch *gdbarch)
Definition: parser-defs.h:44
expr::structop_base_operation * m_op
Definition: parser-defs.h:105
bool complete(struct expression *exp, completion_tracker &tracker) override
Definition: parser-defs.h:94
expr_complete_structop(expr::structop_base_operation *op)
Definition: parser-defs.h:89
gdb::unique_xmalloc_ptr< char > m_name
Definition: parser-defs.h:133
expr_complete_tag(enum type_code code, gdb::unique_xmalloc_ptr< char > name)
Definition: parser-defs.h:113
bool complete(struct expression *exp, completion_tracker &tracker) override
Definition: parse.c:176
enum type_code m_code
Definition: parser-defs.h:130
struct type * type
Definition: parser-defs.h:405
void push_new(Arg... args)
Definition: parser-defs.h:214
int end_arglist()
Definition: parser-defs.h:179
void wrap2()
Definition: parser-defs.h:260
const CORE_ADDR expression_context_pc
Definition: parser-defs.h:277
const struct block *const expression_context_block
Definition: parser-defs.h:270
std::vector< expr::operation_up > pop_vector(int n)
Definition: parser-defs.h:239
bool parse_completion
Definition: parser-defs.h:297
std::unique_ptr< expr_completion_base > m_completion_state
Definition: parser-defs.h:300
std::vector< expr::operation_up > m_operations
Definition: parser-defs.h:316
void push_dollar(struct stoken str)
Definition: parse.c:248
void mark_completion(std::unique_ptr< expr_completion_base > completer)
Definition: parser-defs.h:200
const char * prev_lexptr
Definition: parser-defs.h:290
void push_symbol(const char *name, block_symbol sym)
Definition: parse.c:225
void start_arglist()
Definition: parser-defs.h:170
innermost_block_tracker * block_tracker
Definition: parser-defs.h:303
std::vector< int > m_funcall_chain
Definition: parser-defs.h:313
void mark_completion_tag(enum type_code tag, const char *ptr, int length)
Definition: parse.c:198
DISABLE_COPY_AND_ASSIGN(parser_state)
expr::operation_up pop()
Definition: parser-defs.h:231
void push_c_string(int, struct stoken_vector *vec)
Definition: parse.c:212
void push(expr::operation_up &&op)
Definition: parser-defs.h:207
const char * lexptr
Definition: parser-defs.h:286
int comma_terminates
Definition: parser-defs.h:281
parser_state(const struct language_defn *lang, struct gdbarch *gdbarch, const struct block *context_block, CORE_ADDR context_pc, int comma, const char *input, bool completion, innermost_block_tracker *tracker, bool void_p)
Definition: parser-defs.h:145
bool void_context_p
Definition: parser-defs.h:306
void mark_struct_expression(expr::structop_base_operation *op)
Definition: parse.c:187
struct typed_stoken * tokens
Definition: parser-defs.h:386
int length
Definition: parser-defs.h:370
const char * ptr
Definition: parser-defs.h:368
int is_a_field_of_this
Definition: parser-defs.h:399
struct block_symbol sym
Definition: parser-defs.h:398
struct type * type
Definition: parser-defs.h:392
Definition: gdbtypes.h:922