1 /* Definitions for expressions stored in reversed prefix form, for GDB.
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #if !defined (EXPRESSION_H)
21 #define EXPRESSION_H 1
25 /* While parsing expressions we need to track the innermost lexical block
26 that we encounter. In some situations we need to track the innermost
27 block just for symbols, and in other situations we want to track the
28 innermost block for symbols and registers. These flags are used by the
29 innermost block tracker to control which blocks we consider for the
30 innermost block. These flags can be combined together as needed. */
32 enum innermost_block_tracker_type
34 /* Track the innermost block for symbols within an expression. */
35 INNERMOST_BLOCK_FOR_SYMBOLS
= (1 << 0),
37 /* Track the innermost block for registers within an expression. */
38 INNERMOST_BLOCK_FOR_REGISTERS
= (1 << 1)
40 DEF_ENUM_FLAGS_TYPE (enum innermost_block_tracker_type
,
41 innermost_block_tracker_types
);
43 enum exp_opcode
: uint8_t
45 #define OP(name) name ,
47 #include "std-operator.def"
51 /* Existing only to swallow the last comma (',') from last .inc file. */
55 /* Values of NOSIDE argument to eval_subexp. */
60 EVAL_SKIP
, /* Only effect is to increment pos.
61 Return type information where
63 EVAL_AVOID_SIDE_EFFECTS
/* Don't modify any variables or
64 call any functions. The value
65 returned will have the correct
66 type, and will have an
67 approximately correct lvalue
68 type (inaccuracy: anything that is
69 listed as being in a register in
70 the function in which it was
71 declared will be lval_register).
72 Ideally this would not even read
73 target memory, but currently it
74 does in many situations. */
87 typedef std::unique_ptr
<operation
> operation_up
;
89 /* Base class for an operation. An operation is a single component of
96 operation () = default;
97 DISABLE_COPY_AND_ASSIGN (operation
);
101 virtual ~operation () = default;
103 /* Evaluate this operation. */
104 virtual value
*evaluate (struct type
*expect_type
,
105 struct expression
*exp
,
106 enum noside noside
) = 0;
108 /* Evaluate this operation in a context where C-like coercion is
110 virtual value
*evaluate_with_coercion (struct expression
*exp
,
113 return evaluate (nullptr, exp
, noside
);
116 /* Evaluate this expression in the context of a cast to
118 virtual value
*evaluate_for_cast (struct type
*expect_type
,
119 struct expression
*exp
,
122 /* Evaluate this expression in the context of a sizeof
124 virtual value
*evaluate_for_sizeof (struct expression
*exp
,
127 /* Evaluate this expression in the context of an address-of
128 operation. Must return the address. */
129 virtual value
*evaluate_for_address (struct expression
*exp
,
132 /* Evaluate a function call, with this object as the callee.
133 EXPECT_TYPE, EXP, and NOSIDE have the same meaning as in
134 'evaluate'. ARGS holds the operations that should be evaluated
135 to get the arguments to the call. */
136 virtual value
*evaluate_funcall (struct type
*expect_type
,
137 struct expression
*exp
,
139 const std::vector
<operation_up
> &args
)
141 /* Defer to the helper overload. */
142 return evaluate_funcall (expect_type
, exp
, noside
, nullptr, args
);
145 /* True if this is a constant expression. */
146 virtual bool constant_p () const
149 /* Return true if this operation uses OBJFILE (and will become
150 dangling when OBJFILE is unloaded), otherwise return false.
151 OBJFILE must not be a separate debug info file. */
152 virtual bool uses_objfile (struct objfile
*objfile
) const
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);
160 /* Return the opcode that is implemented by this operation. */
161 virtual enum exp_opcode
opcode () const = 0;
163 /* Print this operation to STREAM. */
164 virtual void dump (struct ui_file
*stream
, int depth
) const = 0;
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 () { }
172 /* A helper overload that wraps evaluate_subexp_do_call. */
173 value
*evaluate_funcall (struct type
*expect_type
,
174 struct expression
*exp
,
176 const char *function_name
,
177 const std::vector
<operation_up
> &args
);
179 /* Called by generate_ax to do the work for this particular
181 virtual void do_generate_ax (struct expression
*exp
,
182 struct agent_expr
*ax
,
183 struct axs_value
*value
,
184 struct type
*cast_type
)
186 error (_("Cannot translate to agent expression"));
190 /* A helper function for creating an operation_up, given a type. */
191 template<typename T
, typename
... Arg
>
193 make_operation (Arg
... args
)
195 return operation_up (new T (std::forward
<Arg
> (args
)...));
202 expression (const struct language_defn
*, struct gdbarch
*);
204 DISABLE_COPY_AND_ASSIGN (expression
);
206 /* Return the opcode for the outermost sub-expression of this
208 enum exp_opcode
first_opcode () const
210 return op
->opcode ();
213 /* Evaluate the expression. EXPECT_TYPE is the context type of the
214 expression; normally this should be nullptr. NOSIDE controls how
215 evaluation is performed. */
216 struct value
*evaluate (struct type
*expect_type
, enum noside noside
);
218 /* Language it was entered in. */
219 const struct language_defn
*language_defn
;
220 /* Architecture it was parsed in. */
221 struct gdbarch
*gdbarch
;
222 expr::operation_up op
;
225 typedef std::unique_ptr
<expression
> expression_up
;
229 class innermost_block_tracker
;
230 extern expression_up
parse_expression (const char *,
231 innermost_block_tracker
* = nullptr,
232 bool void_context_p
= false);
234 extern expression_up
parse_expression_with_language (const char *string
,
237 extern struct type
*parse_expression_for_completion
238 (const char *, gdb::unique_xmalloc_ptr
<char> *, enum type_code
*);
240 class innermost_block_tracker
;
241 extern expression_up
parse_exp_1 (const char **, CORE_ADDR pc
,
242 const struct block
*, int,
243 innermost_block_tracker
* = nullptr);
247 /* Evaluate a function call. The function to be called is in CALLEE and
248 the arguments passed to the function are in ARGVEC.
249 FUNCTION_NAME is the name of the function, if known.
250 DEFAULT_RETURN_TYPE is used as the function's return type if the return
253 extern struct value
*evaluate_subexp_do_call (expression
*exp
,
256 gdb::array_view
<value
*> argvec
,
257 const char *function_name
,
258 type
*default_return_type
);
260 /* From expprint.c */
262 extern const char *op_name (enum exp_opcode opcode
);
264 extern void dump_prefix_expression (struct expression
*, struct ui_file
*);
266 /* In an OP_RANGE expression, either bound could be empty, indicating
267 that its value is by default that of the corresponding bound of the
268 array or string. Also, the upper end of the range can be exclusive
269 or inclusive. So we have six sorts of subrange. This enumeration
270 type is to identify this. */
272 enum range_flag
: unsigned
274 /* This is a standard range. Both the lower and upper bounds are
275 defined, and the bounds are inclusive. */
278 /* The low bound was not given. */
279 RANGE_LOW_BOUND_DEFAULT
= 1 << 0,
281 /* The high bound was not given. */
282 RANGE_HIGH_BOUND_DEFAULT
= 1 << 1,
284 /* The high bound of this range is exclusive. */
285 RANGE_HIGH_BOUND_EXCLUSIVE
= 1 << 2,
287 /* The range has a stride. */
288 RANGE_HAS_STRIDE
= 1 << 3,
291 DEF_ENUM_FLAGS_TYPE (enum range_flag
, range_flags
);
293 #endif /* !defined (EXPRESSION_H) */