Add an expr::operation_up to struct expression
[binutils-gdb.git] / gdb / expression.h
1 /* Definitions for expressions stored in reversed prefix form, for GDB.
2
3 Copyright (C) 1986-2021 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
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. */
31
32 enum innermost_block_tracker_type
33 {
34 /* Track the innermost block for symbols within an expression. */
35 INNERMOST_BLOCK_FOR_SYMBOLS = (1 << 0),
36
37 /* Track the innermost block for registers within an expression. */
38 INNERMOST_BLOCK_FOR_REGISTERS = (1 << 1)
39 };
40 DEF_ENUM_FLAGS_TYPE (enum innermost_block_tracker_type,
41 innermost_block_tracker_types);
42
43 /* Definitions for saved C expressions. */
44
45 /* An expression is represented as a vector of union exp_element's.
46 Each exp_element is an opcode, except that some opcodes cause
47 the following exp_element to be treated as a long or double constant
48 or as a variable. The opcodes are obeyed, using a stack for temporaries.
49 The value is left on the temporary stack at the end. */
50
51 /* When it is necessary to include a string,
52 it can occupy as many exp_elements as it needs.
53 We find the length of the string using strlen,
54 divide to find out how many exp_elements are used up,
55 and skip that many. Strings, like numbers, are indicated
56 by the preceding opcode. */
57
58 enum exp_opcode : uint8_t
59 {
60 #define OP(name) name ,
61
62 #include "std-operator.def"
63
64 #undef OP
65
66 /* Existing only to swallow the last comma (',') from last .inc file. */
67 OP_UNUSED_LAST
68 };
69
70 /* Values of NOSIDE argument to eval_subexp. */
71
72 enum noside
73 {
74 EVAL_NORMAL,
75 EVAL_SKIP, /* Only effect is to increment pos.
76 Return type information where
77 possible. */
78 EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
79 call any functions. The value
80 returned will have the correct
81 type, and will have an
82 approximately correct lvalue
83 type (inaccuracy: anything that is
84 listed as being in a register in
85 the function in which it was
86 declared will be lval_register).
87 Ideally this would not even read
88 target memory, but currently it
89 does in many situations. */
90 };
91
92 struct expression;
93 struct agent_expr;
94 struct axs_value;
95 struct type;
96 struct ui_file;
97
98 namespace expr
99 {
100
101 class operation;
102 typedef std::unique_ptr<operation> operation_up;
103
104 /* Base class for an operation. An operation is a single component of
105 an expression. */
106
107 class operation
108 {
109 protected:
110
111 operation () = default;
112 DISABLE_COPY_AND_ASSIGN (operation);
113
114 public:
115
116 virtual ~operation () = default;
117
118 /* Evaluate this operation. */
119 virtual value *evaluate (struct type *expect_type,
120 struct expression *exp,
121 enum noside noside) = 0;
122
123 /* Evaluate this operation in a context where C-like coercion is
124 needed. */
125 virtual value *evaluate_with_coercion (struct expression *exp,
126 enum noside noside)
127 {
128 return evaluate (nullptr, exp, noside);
129 }
130
131 /* Evaluate this expression in the context of a cast to
132 EXPECT_TYPE. */
133 virtual value *evaluate_for_cast (struct type *expect_type,
134 struct expression *exp,
135 enum noside noside);
136
137 /* Evaluate this expression in the context of a sizeof
138 operation. */
139 virtual value *evaluate_for_sizeof (struct expression *exp,
140 enum noside noside);
141
142 /* Evaluate this expression in the context of an address-of
143 operation. Must return the address. */
144 virtual value *evaluate_for_address (struct expression *exp,
145 enum noside noside);
146
147 /* Evaluate a function call, with this object as the callee.
148 EXPECT_TYPE, EXP, and NOSIDE have the same meaning as in
149 'evaluate'. ARGS holds the operations that should be evaluated
150 to get the arguments to the call. */
151 virtual value *evaluate_funcall (struct type *expect_type,
152 struct expression *exp,
153 enum noside noside,
154 const std::vector<operation_up> &args)
155 {
156 /* Defer to the helper overload. */
157 return evaluate_funcall (expect_type, exp, noside, nullptr, args);
158 }
159
160 /* True if this is a constant expression. */
161 virtual bool constant_p () const
162 { return false; }
163
164 /* Return true if this operation uses OBJFILE (and will become
165 dangling when OBJFILE is unloaded), otherwise return false.
166 OBJFILE must not be a separate debug info file. */
167 virtual bool uses_objfile (struct objfile *objfile) const
168 { return false; }
169
170 /* Generate agent expression bytecodes for this operation. */
171 void generate_ax (struct expression *exp, struct agent_expr *ax,
172 struct axs_value *value,
173 struct type *cast_type = nullptr);
174
175 /* Return the opcode that is implemented by this operation. */
176 virtual enum exp_opcode opcode () const = 0;
177
178 /* Print this operation to STREAM. */
179 virtual void dump (struct ui_file *stream, int depth) const = 0;
180
181 /* Call to indicate that this is the outermost operation in the
182 expression. This should almost never be overridden. */
183 virtual void set_outermost () { }
184
185 protected:
186
187 /* A helper overload that wraps evaluate_subexp_do_call. */
188 value *evaluate_funcall (struct type *expect_type,
189 struct expression *exp,
190 enum noside noside,
191 const char *function_name,
192 const std::vector<operation_up> &args);
193
194 /* Called by generate_ax to do the work for this particular
195 operation. */
196 virtual void do_generate_ax (struct expression *exp,
197 struct agent_expr *ax,
198 struct axs_value *value,
199 struct type *cast_type)
200 {
201 error (_("Cannot translate to agent expression"));
202 }
203 };
204
205 /* A helper function for creating an operation_up, given a type. */
206 template<typename T, typename... Arg>
207 operation_up
208 make_operation (Arg... args)
209 {
210 return operation_up (new T (std::forward<Arg> (args)...));
211 }
212
213 }
214
215 union exp_element
216 {
217 enum exp_opcode opcode;
218 struct symbol *symbol;
219 struct minimal_symbol *msymbol;
220 LONGEST longconst;
221 gdb_byte floatconst[16];
222 /* Really sizeof (union exp_element) characters (or less for the last
223 element of a string). */
224 char string;
225 struct type *type;
226 struct internalvar *internalvar;
227 const struct block *block;
228 struct objfile *objfile;
229 };
230
231 struct expression
232 {
233 expression (const struct language_defn *, struct gdbarch *, size_t);
234 ~expression ();
235 DISABLE_COPY_AND_ASSIGN (expression);
236
237 void resize (size_t);
238
239 /* Return the opcode for the outermost sub-expression of this
240 expression. */
241 enum exp_opcode first_opcode () const
242 {
243 if (op != nullptr)
244 return op->opcode ();
245 return elts[0].opcode;
246 }
247
248 /* Evaluate the expression. EXPECT_TYPE is the context type of the
249 expression; normally this should be nullptr. NOSIDE controls how
250 evaluation is performed. */
251 struct value *evaluate (struct type *expect_type, enum noside noside);
252
253 /* Language it was entered in. */
254 const struct language_defn *language_defn;
255 /* Architecture it was parsed in. */
256 struct gdbarch *gdbarch;
257 expr::operation_up op;
258 int nelts = 0;
259 union exp_element *elts;
260 };
261
262 typedef std::unique_ptr<expression> expression_up;
263
264 /* Macros for converting between number of expression elements and bytes
265 to store that many expression elements. */
266
267 #define EXP_ELEM_TO_BYTES(elements) \
268 ((elements) * sizeof (union exp_element))
269 #define BYTES_TO_EXP_ELEM(bytes) \
270 (((bytes) + sizeof (union exp_element) - 1) / sizeof (union exp_element))
271
272 /* From parse.c */
273
274 class innermost_block_tracker;
275 extern expression_up parse_expression (const char *,
276 innermost_block_tracker * = nullptr,
277 bool void_context_p = false);
278
279 extern expression_up parse_expression_with_language (const char *string,
280 enum language lang);
281
282 extern struct type *parse_expression_for_completion
283 (const char *, gdb::unique_xmalloc_ptr<char> *, enum type_code *);
284
285 class innermost_block_tracker;
286 extern expression_up parse_exp_1 (const char **, CORE_ADDR pc,
287 const struct block *, int,
288 innermost_block_tracker * = nullptr);
289
290 /* From eval.c */
291
292 extern struct value *evaluate_subexp_standard
293 (struct type *, struct expression *, int *, enum noside);
294
295 /* Evaluate a function call. The function to be called is in CALLEE and
296 the arguments passed to the function are in ARGVEC.
297 FUNCTION_NAME is the name of the function, if known.
298 DEFAULT_RETURN_TYPE is used as the function's return type if the return
299 type is unknown. */
300
301 extern struct value *evaluate_subexp_do_call (expression *exp,
302 enum noside noside,
303 value *callee,
304 gdb::array_view<value *> argvec,
305 const char *function_name,
306 type *default_return_type);
307
308 /* From expprint.c */
309
310 extern void print_expression (struct expression *, struct ui_file *);
311
312 extern const char *op_name (enum exp_opcode opcode);
313
314 extern const char *op_string (enum exp_opcode);
315
316 extern void dump_raw_expression (struct expression *,
317 struct ui_file *, const char *);
318 extern void dump_prefix_expression (struct expression *, struct ui_file *);
319
320 /* In an OP_RANGE expression, either bound could be empty, indicating
321 that its value is by default that of the corresponding bound of the
322 array or string. Also, the upper end of the range can be exclusive
323 or inclusive. So we have six sorts of subrange. This enumeration
324 type is to identify this. */
325
326 enum range_flag : unsigned
327 {
328 /* This is a standard range. Both the lower and upper bounds are
329 defined, and the bounds are inclusive. */
330 RANGE_STANDARD = 0,
331
332 /* The low bound was not given. */
333 RANGE_LOW_BOUND_DEFAULT = 1 << 0,
334
335 /* The high bound was not given. */
336 RANGE_HIGH_BOUND_DEFAULT = 1 << 1,
337
338 /* The high bound of this range is exclusive. */
339 RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2,
340
341 /* The range has a stride. */
342 RANGE_HAS_STRIDE = 1 << 3,
343 };
344
345 DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags);
346
347 #endif /* !defined (EXPRESSION_H) */