Automatic date update in version.in
[binutils-gdb.git] / gdb / parser-defs.h
blob5d3c1893836ba3573453ea2f555a960912c734dc
1 /* Parser definitions for GDB.
3 Copyright (C) 1986-2024 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 start_of_input (input),
156 block_tracker (tracker),
157 comma_terminates ((flags & PARSER_COMMA_TERMINATES) != 0),
158 parse_completion (completion),
159 void_context_p ((flags & PARSER_VOID_CONTEXT) != 0),
160 debug ((flags & PARSER_DEBUG) != 0)
164 DISABLE_COPY_AND_ASSIGN (parser_state);
166 /* Begin counting arguments for a function call,
167 saving the data about any containing call. */
169 void start_arglist ()
171 m_funcall_chain.push_back (arglist_len);
172 arglist_len = 0;
175 /* Return the number of arguments in a function call just terminated,
176 and restore the data for the containing function call. */
178 int end_arglist ()
180 int val = arglist_len;
181 arglist_len = m_funcall_chain.back ();
182 m_funcall_chain.pop_back ();
183 return val;
186 /* Mark the given operation as the starting location of a structure
187 expression. This is used when completing on field names. */
189 void mark_struct_expression (expr::structop_base_operation *op);
191 /* Indicate that the current parser invocation is completing a tag.
192 TAG is the type code of the tag, and PTR and LENGTH represent the
193 start of the tag name. */
195 void mark_completion_tag (enum type_code tag, const char *ptr, int length);
197 /* Mark for completion, using an arbitrary completer. */
199 void mark_completion (std::unique_ptr<expr_completion_base> completer)
201 gdb_assert (m_completion_state == nullptr);
202 m_completion_state = std::move (completer);
205 /* Push an operation on the stack. */
206 void push (expr::operation_up &&op)
208 m_operations.push_back (std::move (op));
211 /* Create a new operation and push it on the stack. */
212 template<typename T, typename... Arg>
213 void push_new (Arg... args)
215 m_operations.emplace_back (new T (std::forward<Arg> (args)...));
218 /* Push a new C string operation. */
219 void push_c_string (int, struct stoken_vector *vec);
221 /* Push a symbol reference. If SYM is nullptr, look for a minimal
222 symbol. */
223 void push_symbol (const char *name, block_symbol sym);
225 /* Push a reference to $mumble. This may result in a convenience
226 variable, a history reference, or a register. */
227 void push_dollar (struct stoken str);
229 /* Pop an operation from the stack. */
230 expr::operation_up pop ()
232 expr::operation_up result = std::move (m_operations.back ());
233 m_operations.pop_back ();
234 return result;
237 /* Pop N elements from the stack and return a vector. */
238 std::vector<expr::operation_up> pop_vector (int n)
240 std::vector<expr::operation_up> result (n);
241 for (int i = 1; i <= n; ++i)
242 result[n - i] = pop ();
243 return result;
246 /* A helper that pops an operation, wraps it in some other
247 operation, and pushes it again. */
248 template<typename T>
249 void wrap ()
251 using namespace expr;
252 operation_up v = ::expr::make_operation<T> (pop ());
253 push (std::move (v));
256 /* A helper that pops two operations, wraps them in some other
257 operation, and pushes the result. */
258 template<typename T>
259 void wrap2 ()
261 expr::operation_up rhs = pop ();
262 expr::operation_up lhs = pop ();
263 push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
266 /* Function called from the various parsers' yyerror functions to throw
267 an error. The error will include a message identifying the location
268 of the error within the current expression. */
269 void parse_error (const char *msg);
271 /* If this is nonzero, this block is used as the lexical context for
272 symbol names. */
274 const struct block * const expression_context_block;
276 /* If expression_context_block is non-zero, then this is the PC
277 within the block that we want to evaluate expressions at. When
278 debugging C or C++ code, we use this to find the exact line we're
279 at, and then look up the macro definitions active at that
280 point. */
281 const CORE_ADDR expression_context_pc;
283 /* During parsing of a C expression, the pointer to the next character
284 is in this variable. */
286 const char *lexptr;
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;
292 /* A pointer to the start of the full input, used for error reporting. */
293 const char *start_of_input = nullptr;
295 /* Number of arguments seen so far in innermost function call. */
297 int arglist_len = 0;
299 /* Completion state is updated here. */
300 std::unique_ptr<expr_completion_base> m_completion_state;
302 /* The innermost block tracker. */
303 innermost_block_tracker *block_tracker;
305 /* Nonzero means stop parsing on first comma (if not within parentheses). */
306 bool comma_terminates;
308 /* True if parsing an expression to attempt completion. */
309 bool parse_completion;
311 /* True if no value is expected from the expression. */
312 bool void_context_p;
314 /* True if parser debugging should be enabled. */
315 bool debug;
317 private:
319 /* Data structure for saving values of arglist_len for function calls whose
320 arguments contain other function calls. */
322 std::vector<int> m_funcall_chain;
324 /* Stack of operations. */
325 std::vector<expr::operation_up> m_operations;
328 /* A string token, either a char-string or bit-string. Char-strings are
329 used, for example, for the names of symbols. */
331 struct stoken
333 /* Pointer to first byte of char-string or first bit of bit-string. */
334 const char *ptr;
335 /* Length of string in bytes for char-string or bits for bit-string. */
336 int length;
339 struct typed_stoken
341 /* A language-specific type field. */
342 int type;
343 /* Pointer to first byte of char-string or first bit of bit-string. */
344 char *ptr;
345 /* Length of string in bytes for char-string or bits for bit-string. */
346 int length;
349 struct stoken_vector
351 int len;
352 struct typed_stoken *tokens;
355 struct ttype
357 struct stoken stoken;
358 struct type *type;
361 struct symtoken
363 struct stoken stoken;
364 struct block_symbol sym;
365 int is_a_field_of_this;
368 struct objc_class_str
370 struct stoken stoken;
371 struct type *type;
372 int theclass;
375 extern const char *find_template_name_end (const char *);
377 extern std::string copy_name (struct stoken);
379 extern bool parse_float (const char *p, int len,
380 const struct type *type, gdb_byte *data);
381 extern bool fits_in_type (int n_sign, ULONGEST n, int type_bits,
382 bool type_signed_p);
383 extern bool fits_in_type (int n_sign, const gdb_mpz &n, int type_bits,
384 bool type_signed_p);
387 /* Function used to avoid direct calls to fprintf
388 in the code generated by the bison parser. */
390 extern void parser_fprintf (FILE *, const char *, ...) ATTRIBUTE_PRINTF (2, 3);
392 #endif /* PARSER_DEFS_H */