GDB (xrefs)
Loading...
Searching...
No Matches
expression.h
Go to the documentation of this file.
1/* Definitions for expressions stored in reversed prefix form, for GDB.
2
3 Copyright (C) 1986-2023 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#if !defined (EXPRESSION_H)
21#define EXPRESSION_H 1
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
34{
35 /* Track the innermost block for symbols within an expression. */
37
38 /* Track the innermost block for registers within an expression. */
40};
42 innermost_block_tracker_types);
43
44enum 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
56 {
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
72struct expression;
73struct agent_expr;
74struct axs_value;
75struct type;
76struct ui_file;
77
78namespace expr
79{
80
81class operation;
82typedef std::unique_ptr<operation> operation_up;
83
84/* Base class for an operation. An operation is a single component of
85 an expression. */
86
88{
89protected:
90
91 operation () = default;
93
94public:
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. */
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 /* Generate agent expression bytecodes for this operation. */
151 void generate_ax (struct expression *exp, struct agent_expr *ax,
152 struct axs_value *value,
153 struct type *cast_type = nullptr);
154
155 /* Return the opcode that is implemented by this operation. */
156 virtual enum exp_opcode opcode () const = 0;
157
158 /* Print this operation to STREAM. */
159 virtual void dump (struct ui_file *stream, int depth) const = 0;
160
161 /* Call to indicate that this is the outermost operation in the
162 expression. This should almost never be overridden. */
163 virtual void set_outermost () { }
164
165protected:
166
167 /* A helper overload that wraps evaluate_subexp_do_call. */
168 value *evaluate_funcall (struct type *expect_type,
169 struct expression *exp,
170 enum noside noside,
171 const char *function_name,
172 const std::vector<operation_up> &args);
173
174 /* Called by generate_ax to do the work for this particular
175 operation. */
176 virtual void do_generate_ax (struct expression *exp,
177 struct agent_expr *ax,
178 struct axs_value *value,
179 struct type *cast_type)
180 {
181 error (_("Cannot translate to agent expression"));
182 }
183};
184
185/* A helper function for creating an operation_up, given a type. */
186template<typename T, typename... Arg>
188make_operation (Arg... args)
189{
190 return operation_up (new T (std::forward<Arg> (args)...));
191}
192
193}
194
196{
197 expression (const struct language_defn *lang, struct gdbarch *arch)
198 : language_defn (lang),
199 gdbarch (arch)
200 {
201 }
202
204
205 /* Return the opcode for the outermost sub-expression of this
206 expression. */
208 {
209 return op->opcode ();
210 }
211
212 /* Dump the expression to STREAM. */
213 void dump (struct ui_file *stream)
214 {
215 op->dump (stream, 0);
216 }
217
218 /* Return true if this expression uses OBJFILE (and will become
219 dangling when OBJFILE is unloaded), otherwise return false.
220 OBJFILE must not be a separate debug info file. */
221 bool uses_objfile (struct objfile *objfile) const;
222
223 /* Evaluate the expression. EXPECT_TYPE is the context type of the
224 expression; normally this should be nullptr. NOSIDE controls how
225 evaluation is performed. */
226 struct value *evaluate (struct type *expect_type = nullptr,
227 enum noside noside = EVAL_NORMAL);
228
229 /* Evaluate an expression, avoiding all memory references
230 and getting a value whose type alone is correct. */
232 { return evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS); }
233
234 /* Language it was entered in. */
236 /* Architecture it was parsed in. */
239};
240
241typedef std::unique_ptr<expression> expression_up;
242
243/* When parsing expressions we track the innermost block that was
244 referenced. */
245
247{
248public:
249 innermost_block_tracker (innermost_block_tracker_types types
251 : m_types (types),
252 m_innermost_block (NULL)
253 { /* Nothing. */ }
254
255 /* Update the stored innermost block if the new block B is more inner
256 than the currently stored block, or if no block is stored yet. The
257 type T tells us whether the block B was for a symbol or for a
258 register. The stored innermost block is only updated if the type T is
259 a type we are interested in, the types we are interested in are held
260 in M_TYPES and set during RESET. */
261 void update (const struct block *b, innermost_block_tracker_types t);
262
263 /* Overload of main UPDATE method which extracts the block from BS. */
264 void update (const struct block_symbol &bs)
265 {
267 }
268
269 /* Return the stored innermost block. Can be nullptr if no symbols or
270 registers were found during an expression parse, and so no innermost
271 block was defined. */
272 const struct block *block () const
273 {
274 return m_innermost_block;
275 }
276
277private:
278 /* The type of innermost block being looked for. */
279 innermost_block_tracker_types m_types;
280
281 /* The currently stored innermost block found while parsing an
282 expression. */
284};
285
286/* Flags that can affect the parsers. */
287
289{
290 /* This flag is set if the expression is being evaluated in a
291 context where a 'void' result type is expected. Parsers are free
292 to ignore this, or to use it to help with overload resolution
293 decisions. */
295
296 /* This flag is set if a top-level comma terminates the
297 expression. */
299
300 /* This flag is set if the parser should print debugging output as
301 it parses. For yacc-based parsers, this translates to setting
302 yydebug. */
303 PARSER_DEBUG = (1 << 2),
304
305 /* Normally the expression-parsing functions like parse_exp_1 will
306 attempt to find a context block if one is not passed in. If set,
307 this flag suppresses this search and uses a null context for the
308 parse. */
310};
311DEF_ENUM_FLAGS_TYPE (enum parser_flag, parser_flags);
312
313/* From parse.c */
314
315extern expression_up parse_expression (const char *,
316 innermost_block_tracker * = nullptr,
317 parser_flags flags = 0);
318
319extern expression_up parse_expression_with_language (const char *string,
320 enum language lang);
321
322
324
325/* Base class for expression completion. An instance of this
326 represents a completion request from the parser. */
328{
329 /* Perform this object's completion. EXP is the expression in which
330 the completion occurs. TRACKER is the tracker to update with the
331 results. Return true if completion was possible (even if no
332 completions were found), false to fall back to ordinary
333 expression completion (i.e., symbol names). */
334 virtual bool complete (struct expression *exp,
335 completion_tracker &tracker) = 0;
336
337 virtual ~expr_completion_base () = default;
338};
339
341 (const char *, std::unique_ptr<expr_completion_base> *completer);
342
343extern expression_up parse_exp_1 (const char **, CORE_ADDR pc,
344 const struct block *,
345 parser_flags flags,
346 innermost_block_tracker * = nullptr);
347
348/* From eval.c */
349
350/* Evaluate a function call. The function to be called is in CALLEE and
351 the arguments passed to the function are in ARGVEC.
352 FUNCTION_NAME is the name of the function, if known.
353 DEFAULT_RETURN_TYPE is used as the function's return type if the return
354 type is unknown. */
355
356extern struct value *evaluate_subexp_do_call (expression *exp,
357 enum noside noside,
358 value *callee,
359 gdb::array_view<value *> argvec,
360 const char *function_name,
361 type *default_return_type);
362
363/* In an OP_RANGE expression, either bound could be empty, indicating
364 that its value is by default that of the corresponding bound of the
365 array or string. Also, the upper end of the range can be exclusive
366 or inclusive. So we have six sorts of subrange. This enumeration
367 type is to identify this. */
368
369enum range_flag : unsigned
370{
371 /* This is a standard range. Both the lower and upper bounds are
372 defined, and the bounds are inclusive. */
374
375 /* The low bound was not given. */
377
378 /* The high bound was not given. */
380
381 /* The high bound of this range is exclusive. */
383
384 /* The range has a stride. */
386};
387
388DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags);
389
390#endif /* !defined (EXPRESSION_H) */
virtual ~operation()=default
virtual value * evaluate_for_address(struct expression *exp, enum noside noside)
Definition eval.c:2580
virtual value * evaluate_funcall(struct type *expect_type, struct expression *exp, enum noside noside, const std::vector< operation_up > &args)
Definition expression.h:131
virtual void do_generate_ax(struct expression *exp, struct agent_expr *ax, struct axs_value *value, struct type *cast_type)
Definition expression.h:176
virtual void set_outermost()
Definition expression.h:163
virtual bool uses_objfile(struct objfile *objfile) const
Definition expression.h:147
virtual value * evaluate_with_coercion(struct expression *exp, enum noside noside)
Definition expression.h:105
virtual bool constant_p() const
Definition expression.h:141
operation()=default
virtual value * evaluate_for_sizeof(struct expression *exp, enum noside noside)
Definition eval.c:2715
void generate_ax(struct expression *exp, struct agent_expr *ax, struct axs_value *value, struct type *cast_type=nullptr)
Definition ax-gdb.c:1592
virtual void dump(struct ui_file *stream, int depth) const =0
DISABLE_COPY_AND_ASSIGN(operation)
virtual value * evaluate(struct type *expect_type, struct expression *exp, enum noside noside)=0
virtual enum exp_opcode opcode() const =0
virtual value * evaluate_for_cast(struct type *expect_type, struct expression *exp, enum noside noside)
Definition eval.c:2571
innermost_block_tracker_types m_types
Definition expression.h:279
innermost_block_tracker(innermost_block_tracker_types types=INNERMOST_BLOCK_FOR_SYMBOLS)
Definition expression.h:249
void update(const struct block *b, innermost_block_tracker_types t)
Definition parse.c:78
void update(const struct block_symbol &bs)
Definition expression.h:264
const struct block * m_innermost_block
Definition expression.h:283
const struct block * block() const
Definition expression.h:272
language
Definition defs.h:211
std::unique_ptr< expression > expression_up
Definition expression.h:241
expression_up parse_expression_with_language(const char *string, enum language lang)
Definition parse.c:472
parser_flag
Definition expression.h:289
@ PARSER_LEAVE_BLOCK_ALONE
Definition expression.h:309
@ PARSER_VOID_CONTEXT
Definition expression.h:294
@ PARSER_DEBUG
Definition expression.h:303
@ PARSER_COMMA_TERMINATES
Definition expression.h:298
expression_up parse_expression(const char *, innermost_block_tracker *=nullptr, parser_flags flags=0)
Definition parse.c:458
exp_opcode
Definition expression.h:45
innermost_block_tracker_type
Definition expression.h:34
@ INNERMOST_BLOCK_FOR_REGISTERS
Definition expression.h:39
@ INNERMOST_BLOCK_FOR_SYMBOLS
Definition expression.h:36
expression_up parse_expression_for_completion(const char *, std::unique_ptr< expr_completion_base > *completer)
Definition parse.c:490
DEF_ENUM_FLAGS_TYPE(enum innermost_block_tracker_type, innermost_block_tracker_types)
expression_up parse_exp_1(const char **, CORE_ADDR pc, const struct block *, parser_flags flags, innermost_block_tracker *=nullptr)
Definition parse.c:446
struct value * evaluate_subexp_do_call(expression *exp, enum noside noside, value *callee, gdb::array_view< value * > argvec, const char *function_name, type *default_return_type)
Definition eval.c:584
noside
Definition expression.h:56
@ EVAL_NORMAL
Definition expression.h:57
@ EVAL_AVOID_SIDE_EFFECTS
Definition expression.h:58
range_flag
Definition expression.h:370
@ RANGE_LOW_BOUND_DEFAULT
Definition expression.h:376
@ RANGE_HIGH_BOUND_EXCLUSIVE
Definition expression.h:382
@ RANGE_HIGH_BOUND_DEFAULT
Definition expression.h:379
@ RANGE_STANDARD
Definition expression.h:373
@ RANGE_HAS_STRIDE
Definition expression.h:385
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
operation_up make_operation(Arg... args)
Definition expression.h:188
std::unique_ptr< operation > operation_up
Definition expression.h:82
Definition 1.cc:26
const struct block * block
Definition symtab.h:1537
Definition block.h:109
virtual bool complete(struct expression *exp, completion_tracker &tracker)=0
virtual ~expr_completion_base()=default
const struct language_defn * language_defn
Definition expression.h:235
struct value * evaluate(struct type *expect_type=nullptr, enum noside noside=EVAL_NORMAL)
Definition eval.c:103
expression(const struct language_defn *lang, struct gdbarch *arch)
Definition expression.h:197
struct value * evaluate_type()
Definition expression.h:231
void dump(struct ui_file *stream)
Definition expression.h:213
expr::operation_up op
Definition expression.h:238
DISABLE_COPY_AND_ASSIGN(expression)
enum exp_opcode first_opcode() const
Definition expression.h:207
bool uses_objfile(struct objfile *objfile) const
Definition eval.c:94
struct gdbarch * gdbarch
Definition expression.h:237
Definition value.h:130