Automatic date update in version.in
[binutils-gdb.git] / gdb / parser-defs.h
blob93ebdf5c06145b5cd3decb29f07f6f73674b554b
1 /* Parser definitions for GDB.
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
5 Modified from expread.y by the Department of Computer Science at the
6 State University of New York at Buffalo.
8 This file is part of GDB.
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.
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.
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/>. */
23 #if !defined (PARSER_DEFS_H)
24 #define PARSER_DEFS_H 1
26 #include "expression.h"
27 #include "symtab.h"
28 #include "expop.h"
30 struct block;
31 struct language_defn;
32 struct internalvar;
33 class innermost_block_tracker;
35 /* A class that can be used to build a "struct expression". */
37 struct expr_builder
39 /* Constructor. LANG is the language used to parse the expression.
40 And GDBARCH is the gdbarch to use during parsing. */
42 expr_builder (const struct language_defn *lang,
43 struct gdbarch *gdbarch)
44 : expout (new expression (lang, gdbarch))
48 DISABLE_COPY_AND_ASSIGN (expr_builder);
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 ()
54 return std::move (expout);
57 /* Return the gdbarch that was passed to the constructor. */
59 struct gdbarch *gdbarch ()
61 return expout->gdbarch;
64 /* Return the language that was passed to the constructor. */
66 const struct language_defn *language ()
68 return expout->language_defn;
71 /* Set the root operation of the expression that is currently being
72 built. */
73 void set_operation (expr::operation_up &&op)
75 expout->op = std::move (op);
78 /* The expression related to this parser state. */
80 expression_up expout;
83 /* Complete an expression that references a field, like "x->y". */
85 struct expr_complete_structop : public expr_completion_base
87 explicit expr_complete_structop (expr::structop_base_operation *op)
88 : m_op (op)
92 bool complete (struct expression *exp,
93 completion_tracker &tracker) override
95 return m_op->complete (exp, tracker);
98 private:
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;
106 /* Complete a tag name in an expression. This is used for something
107 like "enum abc<TAB>". */
109 struct expr_complete_tag : public expr_completion_base
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))
116 /* Parsers should enforce this statically. */
117 gdb_assert (code == TYPE_CODE_ENUM
118 || code == TYPE_CODE_UNION
119 || code == TYPE_CODE_STRUCT);
122 bool complete (struct expression *exp,
123 completion_tracker &tracker) override;
125 private:
127 /* The kind of tag to complete. */
128 enum type_code m_code;
130 /* The token for tagged type name completion. */
131 gdb::unique_xmalloc_ptr<char> m_name;
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. */
138 struct parser_state : public expr_builder
140 /* Constructor. LANG is the language used to parse the expression.
141 And GDBARCH is the gdbarch to use during parsing. */
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)
163 DISABLE_COPY_AND_ASSIGN (parser_state);
165 /* Begin counting arguments for a function call,
166 saving the data about any containing call. */
168 void start_arglist ()
170 m_funcall_chain.push_back (arglist_len);
171 arglist_len = 0;
174 /* Return the number of arguments in a function call just terminated,
175 and restore the data for the containing function call. */
177 int end_arglist ()
179 int val = arglist_len;
180 arglist_len = m_funcall_chain.back ();
181 m_funcall_chain.pop_back ();
182 return val;
185 /* Mark the given operation as the starting location of a structure
186 expression. This is used when completing on field names. */
188 void mark_struct_expression (expr::structop_base_operation *op);
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. */
194 void mark_completion_tag (enum type_code tag, const char *ptr, int length);
196 /* Mark for completion, using an arbitrary completer. */
198 void mark_completion (std::unique_ptr<expr_completion_base> completer)
200 gdb_assert (m_completion_state == nullptr);
201 m_completion_state = std::move (completer);
204 /* Push an operation on the stack. */
205 void push (expr::operation_up &&op)
207 m_operations.push_back (std::move (op));
210 /* Create a new operation and push it on the stack. */
211 template<typename T, typename... Arg>
212 void push_new (Arg... args)
214 m_operations.emplace_back (new T (std::forward<Arg> (args)...));
217 /* Push a new C string operation. */
218 void push_c_string (int, struct stoken_vector *vec);
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);
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);
228 /* Pop an operation from the stack. */
229 expr::operation_up pop ()
231 expr::operation_up result = std::move (m_operations.back ());
232 m_operations.pop_back ();
233 return result;
236 /* Pop N elements from the stack and return a vector. */
237 std::vector<expr::operation_up> pop_vector (int n)
239 std::vector<expr::operation_up> result (n);
240 for (int i = 1; i <= n; ++i)
241 result[n - i] = pop ();
242 return result;
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 ()
250 using namespace expr;
251 operation_up v = ::expr::make_operation<T> (pop ());
252 push (std::move (v));
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 ()
260 expr::operation_up rhs = pop ();
261 expr::operation_up lhs = pop ();
262 push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
265 /* If this is nonzero, this block is used as the lexical context for
266 symbol names. */
268 const struct block * const expression_context_block;
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;
277 /* During parsing of a C expression, the pointer to the next character
278 is in this variable. */
280 const char *lexptr;
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;
286 /* Number of arguments seen so far in innermost function call. */
288 int arglist_len = 0;
290 /* Completion state is updated here. */
291 std::unique_ptr<expr_completion_base> m_completion_state;
293 /* The innermost block tracker. */
294 innermost_block_tracker *block_tracker;
296 /* Nonzero means stop parsing on first comma (if not within parentheses). */
297 bool comma_terminates;
299 /* True if parsing an expression to attempt completion. */
300 bool parse_completion;
302 /* True if no value is expected from the expression. */
303 bool void_context_p;
305 /* True if parser debugging should be enabled. */
306 bool debug;
308 private:
310 /* Data structure for saving values of arglist_len for function calls whose
311 arguments contain other function calls. */
313 std::vector<int> m_funcall_chain;
315 /* Stack of operations. */
316 std::vector<expr::operation_up> m_operations;
319 /* A string token, either a char-string or bit-string. Char-strings are
320 used, for example, for the names of symbols. */
322 struct stoken
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;
330 struct typed_stoken
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;
340 struct stoken_vector
342 int len;
343 struct typed_stoken *tokens;
346 struct ttype
348 struct stoken stoken;
349 struct type *type;
352 struct symtoken
354 struct stoken stoken;
355 struct block_symbol sym;
356 int is_a_field_of_this;
359 struct objc_class_str
361 struct stoken stoken;
362 struct type *type;
363 int theclass;
366 extern const char *find_template_name_end (const char *);
368 extern std::string copy_name (struct stoken);
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);
378 /* Function used to avoid direct calls to fprintf
379 in the code generated by the bison parser. */
381 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
383 #endif /* PARSER_DEFS_H */