Remove union exp_element
[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 enum exp_opcode : uint8_t
44 {
45 #define OP(name) name ,
46
47 #include "std-operator.def"
48
49 #undef OP
50
51 /* Existing only to swallow the last comma (',') from last .inc file. */
52 OP_UNUSED_LAST
53 };
54
55 /* Values of NOSIDE argument to eval_subexp. */
56
57 enum noside
58 {
59 EVAL_NORMAL,
60 EVAL_SKIP, /* Only effect is to increment pos.
61 Return type information where
62 possible. */
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. */
75 };
76
77 struct expression;
78 struct agent_expr;
79 struct axs_value;
80 struct type;
81 struct ui_file;
82
83 namespace expr
84 {
85
86 class operation;
87 typedef std::unique_ptr<operation> operation_up;
88
89 /* Base class for an operation. An operation is a single component of
90 an expression. */
91
92 class operation
93 {
94 protected:
95
96 operation () = default;
97 DISABLE_COPY_AND_ASSIGN (operation);
98
99 public:
100
101 virtual ~operation () = default;
102
103 /* Evaluate this operation. */
104 virtual value *evaluate (struct type *expect_type,
105 struct expression *exp,
106 enum noside noside) = 0;
107
108 /* Evaluate this operation in a context where C-like coercion is
109 needed. */
110 virtual value *evaluate_with_coercion (struct expression *exp,
111 enum noside noside)
112 {
113 return evaluate (nullptr, exp, noside);
114 }
115
116 /* Evaluate this expression in the context of a cast to
117 EXPECT_TYPE. */
118 virtual value *evaluate_for_cast (struct type *expect_type,
119 struct expression *exp,
120 enum noside noside);
121
122 /* Evaluate this expression in the context of a sizeof
123 operation. */
124 virtual value *evaluate_for_sizeof (struct expression *exp,
125 enum noside noside);
126
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,
130 enum noside noside);
131
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,
138 enum noside noside,
139 const std::vector<operation_up> &args)
140 {
141 /* Defer to the helper overload. */
142 return evaluate_funcall (expect_type, exp, noside, nullptr, args);
143 }
144
145 /* True if this is a constant expression. */
146 virtual bool constant_p () const
147 { return false; }
148
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
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 *, struct gdbarch *);
203 ~expression ();
204 DISABLE_COPY_AND_ASSIGN (expression);
205
206 /* Return the opcode for the outermost sub-expression of this
207 expression. */
208 enum exp_opcode first_opcode () const
209 {
210 return op->opcode ();
211 }
212
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);
217
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;
223 };
224
225 typedef std::unique_ptr<expression> expression_up;
226
227 /* From parse.c */
228
229 class innermost_block_tracker;
230 extern expression_up parse_expression (const char *,
231 innermost_block_tracker * = nullptr,
232 bool void_context_p = false);
233
234 extern expression_up parse_expression_with_language (const char *string,
235 enum language lang);
236
237 extern struct type *parse_expression_for_completion
238 (const char *, gdb::unique_xmalloc_ptr<char> *, enum type_code *);
239
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);
244
245 /* From eval.c */
246
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
251 type is unknown. */
252
253 extern struct value *evaluate_subexp_do_call (expression *exp,
254 enum noside noside,
255 value *callee,
256 gdb::array_view<value *> argvec,
257 const char *function_name,
258 type *default_return_type);
259
260 /* From expprint.c */
261
262 extern const char *op_name (enum exp_opcode opcode);
263
264 extern void dump_prefix_expression (struct expression *, struct ui_file *);
265
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. */
271
272 enum range_flag : unsigned
273 {
274 /* This is a standard range. Both the lower and upper bounds are
275 defined, and the bounds are inclusive. */
276 RANGE_STANDARD = 0,
277
278 /* The low bound was not given. */
279 RANGE_LOW_BOUND_DEFAULT = 1 << 0,
280
281 /* The high bound was not given. */
282 RANGE_HIGH_BOUND_DEFAULT = 1 << 1,
283
284 /* The high bound of this range is exclusive. */
285 RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2,
286
287 /* The range has a stride. */
288 RANGE_HAS_STRIDE = 1 << 3,
289 };
290
291 DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags);
292
293 #endif /* !defined (EXPRESSION_H) */