]>
Commit | Line | Data |
---|---|---|
1 | /* Definitions for expressions stored in reversed prefix form, for GDB. | |
2 | ||
3 | Copyright (C) 1986-2025 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #ifndef GDB_EXPRESSION_H | |
21 | #define GDB_EXPRESSION_H | |
22 | ||
23 | #include "gdbtypes.h" | |
24 | #include "symtab.h" | |
25 | ||
26 | /* While parsing expressions we need to track the innermost lexical block | |
27 | that we encounter. In some situations we need to track the innermost | |
28 | block just for symbols, and in other situations we want to track the | |
29 | innermost block for symbols and registers. These flags are used by the | |
30 | innermost block tracker to control which blocks we consider for the | |
31 | innermost block. These flags can be combined together as needed. */ | |
32 | ||
33 | enum innermost_block_tracker_type | |
34 | { | |
35 | /* Track the innermost block for symbols within an expression. */ | |
36 | INNERMOST_BLOCK_FOR_SYMBOLS = (1 << 0), | |
37 | ||
38 | /* Track the innermost block for registers within an expression. */ | |
39 | INNERMOST_BLOCK_FOR_REGISTERS = (1 << 1) | |
40 | }; | |
41 | DEF_ENUM_FLAGS_TYPE (enum innermost_block_tracker_type, | |
42 | innermost_block_tracker_types); | |
43 | ||
44 | enum exp_opcode : uint8_t | |
45 | { | |
46 | #define OP(name) name , | |
47 | ||
48 | #include "std-operator.def" | |
49 | ||
50 | #undef OP | |
51 | }; | |
52 | ||
53 | /* Values of NOSIDE argument to eval_subexp. */ | |
54 | ||
55 | enum noside | |
56 | { | |
57 | EVAL_NORMAL, | |
58 | EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or | |
59 | call any functions. The value | |
60 | returned will have the correct | |
61 | type, and will have an | |
62 | approximately correct lvalue | |
63 | type (inaccuracy: anything that is | |
64 | listed as being in a register in | |
65 | the function in which it was | |
66 | declared will be lval_register). | |
67 | Ideally this would not even read | |
68 | target memory, but currently it | |
69 | does in many situations. */ | |
70 | }; | |
71 | ||
72 | struct expression; | |
73 | struct agent_expr; | |
74 | struct axs_value; | |
75 | struct type; | |
76 | struct ui_file; | |
77 | ||
78 | namespace expr | |
79 | { | |
80 | ||
81 | class operation; | |
82 | typedef std::unique_ptr<operation> operation_up; | |
83 | ||
84 | /* Base class for an operation. An operation is a single component of | |
85 | an expression. */ | |
86 | ||
87 | class operation | |
88 | { | |
89 | protected: | |
90 | ||
91 | operation () = default; | |
92 | DISABLE_COPY_AND_ASSIGN (operation); | |
93 | ||
94 | public: | |
95 | ||
96 | virtual ~operation () = default; | |
97 | ||
98 | /* Evaluate this operation. */ | |
99 | virtual value *evaluate (struct type *expect_type, | |
100 | struct expression *exp, | |
101 | enum noside noside) = 0; | |
102 | ||
103 | /* Evaluate this operation in a context where C-like coercion is | |
104 | needed. */ | |
105 | virtual value *evaluate_with_coercion (struct expression *exp, | |
106 | enum noside noside) | |
107 | { | |
108 | return evaluate (nullptr, exp, noside); | |
109 | } | |
110 | ||
111 | /* Evaluate this expression in the context of a cast to | |
112 | EXPECT_TYPE. */ | |
113 | virtual value *evaluate_for_cast (struct type *expect_type, | |
114 | struct expression *exp, | |
115 | enum noside noside); | |
116 | ||
117 | /* Evaluate this expression in the context of a sizeof | |
118 | operation. */ | |
119 | virtual value *evaluate_for_sizeof (struct expression *exp, | |
120 | enum noside noside); | |
121 | ||
122 | /* Evaluate this expression in the context of an address-of | |
123 | operation. Must return the address. */ | |
124 | virtual value *evaluate_for_address (struct expression *exp, | |
125 | enum noside noside); | |
126 | ||
127 | /* Evaluate a function call, with this object as the callee. | |
128 | EXPECT_TYPE, EXP, and NOSIDE have the same meaning as in | |
129 | 'evaluate'. ARGS holds the operations that should be evaluated | |
130 | to get the arguments to the call. */ | |
131 | virtual value *evaluate_funcall (struct type *expect_type, | |
132 | struct expression *exp, | |
133 | enum noside noside, | |
134 | const std::vector<operation_up> &args) | |
135 | { | |
136 | /* Defer to the helper overload. */ | |
137 | return evaluate_funcall (expect_type, exp, noside, nullptr, args); | |
138 | } | |
139 | ||
140 | /* True if this is a constant expression. */ | |
141 | virtual bool constant_p () const | |
142 | { return false; } | |
143 | ||
144 | /* Return true if this operation uses OBJFILE (and will become | |
145 | dangling when OBJFILE is unloaded), otherwise return false. | |
146 | OBJFILE must not be a separate debug info file. */ | |
147 | virtual bool uses_objfile (struct objfile *objfile) const | |
148 | { return false; } | |
149 | ||
150 | /* Some expression nodes represent a type, not a value. This method | |
151 | should be overridden to return 'true' in these situations. */ | |
152 | virtual bool type_p () const | |
153 | { return false; } | |
154 | ||
155 | /* Generate agent expression bytecodes for this operation. */ | |
156 | void generate_ax (struct expression *exp, struct agent_expr *ax, | |
157 | struct axs_value *value, | |
158 | struct type *cast_type = nullptr); | |
159 | ||
160 | /* Return the opcode that is implemented by this operation. */ | |
161 | virtual enum exp_opcode opcode () const = 0; | |
162 | ||
163 | /* Print this operation to STREAM. */ | |
164 | virtual void dump (struct ui_file *stream, int depth) const = 0; | |
165 | ||
166 | /* Call to indicate that this is the outermost operation in the | |
167 | expression. This should almost never be overridden. */ | |
168 | virtual void set_outermost () { } | |
169 | ||
170 | protected: | |
171 | ||
172 | /* A helper overload that wraps evaluate_subexp_do_call. */ | |
173 | value *evaluate_funcall (struct type *expect_type, | |
174 | struct expression *exp, | |
175 | enum noside noside, | |
176 | const char *function_name, | |
177 | const std::vector<operation_up> &args); | |
178 | ||
179 | /* Called by generate_ax to do the work for this particular | |
180 | operation. */ | |
181 | virtual void do_generate_ax (struct expression *exp, | |
182 | struct agent_expr *ax, | |
183 | struct axs_value *value, | |
184 | struct type *cast_type) | |
185 | { | |
186 | error (_("Cannot translate to agent expression")); | |
187 | } | |
188 | }; | |
189 | ||
190 | /* A helper function for creating an operation_up, given a type. */ | |
191 | template<typename T, typename... Arg> | |
192 | operation_up | |
193 | make_operation (Arg... args) | |
194 | { | |
195 | return operation_up (new T (std::forward<Arg> (args)...)); | |
196 | } | |
197 | ||
198 | } | |
199 | ||
200 | struct expression | |
201 | { | |
202 | expression (const struct language_defn *lang, struct gdbarch *arch) | |
203 | : language_defn (lang), | |
204 | gdbarch (arch) | |
205 | { | |
206 | } | |
207 | ||
208 | DISABLE_COPY_AND_ASSIGN (expression); | |
209 | ||
210 | /* Return the opcode for the outermost sub-expression of this | |
211 | expression. */ | |
212 | enum exp_opcode first_opcode () const | |
213 | { | |
214 | return op->opcode (); | |
215 | } | |
216 | ||
217 | /* Dump the expression to STREAM. */ | |
218 | void dump (struct ui_file *stream) | |
219 | { | |
220 | op->dump (stream, 0); | |
221 | } | |
222 | ||
223 | /* Call the type_p method on the outermost sub-expression of this | |
224 | expression, and return the result. */ | |
225 | bool type_p () const | |
226 | { return op->type_p (); } | |
227 | ||
228 | /* Return true if this expression uses OBJFILE (and will become | |
229 | dangling when OBJFILE is unloaded), otherwise return false. | |
230 | OBJFILE must not be a separate debug info file. */ | |
231 | bool uses_objfile (struct objfile *objfile) const; | |
232 | ||
233 | /* Evaluate the expression. EXPECT_TYPE is the context type of the | |
234 | expression; normally this should be nullptr. NOSIDE controls how | |
235 | evaluation is performed. */ | |
236 | struct value *evaluate (struct type *expect_type = nullptr, | |
237 | enum noside noside = EVAL_NORMAL); | |
238 | ||
239 | /* Evaluate an expression, avoiding all memory references | |
240 | and getting a value whose type alone is correct. */ | |
241 | struct value *evaluate_type () | |
242 | { return evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS); } | |
243 | ||
244 | /* Language it was entered in. */ | |
245 | const struct language_defn *language_defn; | |
246 | /* Architecture it was parsed in. */ | |
247 | struct gdbarch *gdbarch; | |
248 | expr::operation_up op; | |
249 | }; | |
250 | ||
251 | typedef std::unique_ptr<expression> expression_up; | |
252 | ||
253 | /* When parsing expressions we track the innermost block that was | |
254 | referenced. */ | |
255 | ||
256 | class innermost_block_tracker | |
257 | { | |
258 | public: | |
259 | innermost_block_tracker (innermost_block_tracker_types types | |
260 | = INNERMOST_BLOCK_FOR_SYMBOLS) | |
261 | : m_types (types), | |
262 | m_innermost_block (NULL) | |
263 | { /* Nothing. */ } | |
264 | ||
265 | /* Update the stored innermost block if the new block B is more inner | |
266 | than the currently stored block, or if no block is stored yet. The | |
267 | type T tells us whether the block B was for a symbol or for a | |
268 | register. The stored innermost block is only updated if the type T is | |
269 | a type we are interested in, the types we are interested in are held | |
270 | in M_TYPES and set during RESET. */ | |
271 | void update (const struct block *b, innermost_block_tracker_types t); | |
272 | ||
273 | /* Overload of main UPDATE method which extracts the block from BS. */ | |
274 | void update (const struct block_symbol &bs) | |
275 | { | |
276 | update (bs.block, INNERMOST_BLOCK_FOR_SYMBOLS); | |
277 | } | |
278 | ||
279 | /* Return the stored innermost block. Can be nullptr if no symbols or | |
280 | registers were found during an expression parse, and so no innermost | |
281 | block was defined. */ | |
282 | const struct block *block () const | |
283 | { | |
284 | return m_innermost_block; | |
285 | } | |
286 | ||
287 | private: | |
288 | /* The type of innermost block being looked for. */ | |
289 | innermost_block_tracker_types m_types; | |
290 | ||
291 | /* The currently stored innermost block found while parsing an | |
292 | expression. */ | |
293 | const struct block *m_innermost_block; | |
294 | }; | |
295 | ||
296 | /* Flags that can affect the parsers. */ | |
297 | ||
298 | enum parser_flag | |
299 | { | |
300 | /* This flag is set if the expression is being evaluated in a | |
301 | context where a 'void' result type is expected. Parsers are free | |
302 | to ignore this, or to use it to help with overload resolution | |
303 | decisions. */ | |
304 | PARSER_VOID_CONTEXT = (1 << 0), | |
305 | ||
306 | /* This flag is set if a top-level comma terminates the | |
307 | expression. */ | |
308 | PARSER_COMMA_TERMINATES = (1 << 1), | |
309 | ||
310 | /* This flag is set if the parser should print debugging output as | |
311 | it parses. For yacc-based parsers, this translates to setting | |
312 | yydebug. */ | |
313 | PARSER_DEBUG = (1 << 2), | |
314 | ||
315 | /* Normally the expression-parsing functions like parse_exp_1 will | |
316 | attempt to find a context block if one is not passed in. If set, | |
317 | this flag suppresses this search and uses a null context for the | |
318 | parse. */ | |
319 | PARSER_LEAVE_BLOCK_ALONE = (1 << 3), | |
320 | }; | |
321 | DEF_ENUM_FLAGS_TYPE (enum parser_flag, parser_flags); | |
322 | ||
323 | /* From parse.c */ | |
324 | ||
325 | extern expression_up parse_expression (const char *, | |
326 | innermost_block_tracker * = nullptr, | |
327 | parser_flags flags = 0); | |
328 | ||
329 | extern expression_up parse_expression_with_language (const char *string, | |
330 | enum language lang); | |
331 | ||
332 | ||
333 | class completion_tracker; | |
334 | ||
335 | /* Base class for expression completion. An instance of this | |
336 | represents a completion request from the parser. */ | |
337 | struct expr_completion_base | |
338 | { | |
339 | /* Perform this object's completion. EXP is the expression in which | |
340 | the completion occurs. TRACKER is the tracker to update with the | |
341 | results. Return true if completion was possible (even if no | |
342 | completions were found), false to fall back to ordinary | |
343 | expression completion (i.e., symbol names). */ | |
344 | virtual bool complete (struct expression *exp, | |
345 | completion_tracker &tracker) = 0; | |
346 | ||
347 | virtual ~expr_completion_base () = default; | |
348 | }; | |
349 | ||
350 | extern expression_up parse_expression_for_completion | |
351 | (const char *, std::unique_ptr<expr_completion_base> *completer); | |
352 | ||
353 | extern expression_up parse_exp_1 (const char **, CORE_ADDR pc, | |
354 | const struct block *, | |
355 | parser_flags flags, | |
356 | innermost_block_tracker * = nullptr); | |
357 | ||
358 | /* From eval.c */ | |
359 | ||
360 | /* Evaluate a function call. The function to be called is in CALLEE and | |
361 | the arguments passed to the function are in ARGVEC. | |
362 | FUNCTION_NAME is the name of the function, if known. | |
363 | DEFAULT_RETURN_TYPE is used as the function's return type if the return | |
364 | type is unknown. */ | |
365 | ||
366 | extern struct value *evaluate_subexp_do_call (expression *exp, | |
367 | enum noside noside, | |
368 | value *callee, | |
369 | gdb::array_view<value *> argvec, | |
370 | const char *function_name, | |
371 | type *default_return_type); | |
372 | ||
373 | /* In an OP_RANGE expression, either bound could be empty, indicating | |
374 | that its value is by default that of the corresponding bound of the | |
375 | array or string. Also, the upper end of the range can be exclusive | |
376 | or inclusive. So we have six sorts of subrange. This enumeration | |
377 | type is to identify this. */ | |
378 | ||
379 | enum range_flag : unsigned | |
380 | { | |
381 | /* This is a standard range. Both the lower and upper bounds are | |
382 | defined, and the bounds are inclusive. */ | |
383 | RANGE_STANDARD = 0, | |
384 | ||
385 | /* The low bound was not given. */ | |
386 | RANGE_LOW_BOUND_DEFAULT = 1 << 0, | |
387 | ||
388 | /* The high bound was not given. */ | |
389 | RANGE_HIGH_BOUND_DEFAULT = 1 << 1, | |
390 | ||
391 | /* The high bound of this range is exclusive. */ | |
392 | RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2, | |
393 | ||
394 | /* The range has a stride. */ | |
395 | RANGE_HAS_STRIDE = 1 << 3, | |
396 | }; | |
397 | ||
398 | DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags); | |
399 | ||
400 | #endif /* GDB_EXPRESSION_H */ |