GDB (xrefs)
Loading...
Searching...
No Matches
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
35/* A class that can be used to build a "struct expression". */
36
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
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. */
74 {
75 expout->op = std::move (op);
76 }
77
78 /* The expression related to this parser state. */
79
81};
82
83/* Complete an expression that references a field, like "x->y". */
84
86{
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
98private:
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. */
104};
105
106/* Complete a tag name in an expression. This is used for something
107 like "enum abc<TAB>". */
108
110{
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
125private:
126
127 /* The kind of tag to complete. */
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
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,
151 : expr_builder (lang, gdbarch),
152 expression_context_block (context_block),
153 expression_context_pc (context_pc),
154 lexptr (input),
155 block_tracker (tracker),
157 parse_completion (completion),
159 debug ((flags & PARSER_DEBUG) != 0)
160 {
161 }
162
164
165 /* Begin counting arguments for a function call,
166 saving the data about any containing call. */
167
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
178 {
179 int val = arglist_len;
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
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. */
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. */
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. */
295
296 /* Nonzero means stop parsing on first comma (if not within parentheses). */
298
299 /* True if parsing an expression to attempt completion. */
301
302 /* True if no value is expected from the expression. */
304
305 /* True if parser debugging should be enabled. */
306 bool debug;
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/* A string token, either a char-string or bit-string. Char-strings are
320 used, for example, for the names of symbols. */
321
322struct 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. */
328 };
329
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. */
338 };
339
341 {
342 int len;
344 };
345
346struct ttype
347 {
349 struct type *type;
350 };
351
353 {
357 };
358
360 {
362 struct type *type;
364 };
365
366extern const char *find_template_name_end (const char *);
367
368extern std::string copy_name (struct stoken);
369
370extern bool parse_float (const char *p, int len,
371 const struct type *type, gdb_byte *data);
372extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits,
373 bool type_signed_p);
374extern bool fits_in_type (int n_sign, const gdb_mpz &n, int type_bits,
375 bool type_signed_p);
376
377
378/* Function used to avoid direct calls to fprintf
379 in the code generated by the bison parser. */
380
381extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
382
383#endif /* PARSER_DEFS_H */
384
const char *const name
int code
Definition ada-lex.l:670
virtual bool complete(struct expression *exp, completion_tracker &tracker)
Definition expop.h:1014
std::unique_ptr< expression > expression_up
Definition expression.h:241
@ PARSER_VOID_CONTEXT
Definition expression.h:294
@ PARSER_DEBUG
Definition expression.h:303
@ PARSER_COMMA_TERMINATES
Definition expression.h:298
static void ATTRIBUTE_PRINTF(1, 0)
Definition gdb_bfd.c:1154
type_code
Definition gdbtypes.h:82
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
Definition ada-exp.h:87
std::unique_ptr< operation > operation_up
Definition expression.h:82
Definition aarch64.h:67
bool parse_float(const char *p, int len, const struct type *type, gdb_byte *data)
Definition parse.c:520
bool fits_in_type(int n_sign, ULONGEST n, int type_bits, bool type_signed_p)
Definition parse.c:530
const char * find_template_name_end(const char *)
Definition parse.c:250
void parser_fprintf(FILE *, const char *,...) ATTRIBUTE_PRINTF(2
std::string copy_name(struct stoken)
Definition parse.c:319
Definition 1.cc:26
Definition block.h:109
DISABLE_COPY_AND_ASSIGN(expr_builder)
void set_operation(expr::operation_up &&op)
Definition parser-defs.h:73
const struct language_defn * language()
Definition parser-defs.h:66
struct gdbarch * gdbarch()
Definition parser-defs.h:59
expression_up expout
Definition parser-defs.h:80
ATTRIBUTE_UNUSED_RESULT expression_up release()
Definition parser-defs.h:52
expr_builder(const struct language_defn *lang, struct gdbarch *gdbarch)
Definition parser-defs.h:42
expr::structop_base_operation * m_op
bool complete(struct expression *exp, completion_tracker &tracker) override
Definition parser-defs.h:92
expr_complete_structop(expr::structop_base_operation *op)
Definition parser-defs.h:87
gdb::unique_xmalloc_ptr< char > m_name
expr_complete_tag(enum type_code code, gdb::unique_xmalloc_ptr< char > name)
bool complete(struct expression *exp, completion_tracker &tracker) override
Definition parse.c:90
enum type_code m_code
language_defn(enum language lang)
Definition language.h:265
struct type * type
void push_new(Arg... args)
int end_arglist()
const CORE_ADDR expression_context_pc
const struct block *const expression_context_block
std::vector< expr::operation_up > pop_vector(int n)
bool parse_completion
std::unique_ptr< expr_completion_base > m_completion_state
std::vector< expr::operation_up > m_operations
void push_dollar(struct stoken str)
Definition parse.c:162
void mark_completion(std::unique_ptr< expr_completion_base > completer)
const char * prev_lexptr
void push_symbol(const char *name, block_symbol sym)
Definition parse.c:139
void start_arglist()
innermost_block_tracker * block_tracker
std::vector< int > m_funcall_chain
bool comma_terminates
void mark_completion_tag(enum type_code tag, const char *ptr, int length)
Definition parse.c:112
DISABLE_COPY_AND_ASSIGN(parser_state)
expr::operation_up pop()
void push_c_string(int, struct stoken_vector *vec)
Definition parse.c:126
void push(expr::operation_up &&op)
const char * lexptr
bool void_context_p
parser_state(const struct language_defn *lang, struct gdbarch *gdbarch, const struct block *context_block, CORE_ADDR context_pc, parser_flags flags, const char *input, bool completion, innermost_block_tracker *tracker)
void mark_struct_expression(expr::structop_base_operation *op)
Definition parse.c:101
struct typed_stoken * tokens
int length
const char * ptr
int is_a_field_of_this
struct block_symbol sym
struct type * type