5c5371e9660d7585f33bd23c65944506c5e278c9
[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
52 /* Values of NOSIDE argument to eval_subexp. */
53
54 enum noside
55 {
56 EVAL_NORMAL,
57 EVAL_SKIP, /* Only effect is to increment pos.
58 Return type information where
59 possible. */
60 EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
61 call any functions. The value
62 returned will have the correct
63 type, and will have an
64 approximately correct lvalue
65 type (inaccuracy: anything that is
66 listed as being in a register in
67 the function in which it was
68 declared will be lval_register).
69 Ideally this would not even read
70 target memory, but currently it
71 does in many situations. */
72 };
73
74 struct expression;
75 struct agent_expr;
76 struct axs_value;
77 struct type;
78 struct ui_file;
79
80 namespace expr
81 {
82
83 class operation;
84 typedef std::unique_ptr<operation> operation_up;
85
86 /* Base class for an operation. An operation is a single component of
87 an expression. */
88
89 class operation
90 {
91 protected:
92
93 operation () = default;
94 DISABLE_COPY_AND_ASSIGN (operation);
95
96 public:
97
98 virtual ~operation () = default;
99
100 /* Evaluate this operation. */
101 virtual value *evaluate (struct type *expect_type,
102 struct expression *exp,
103 enum noside noside) = 0;
104
105 /* Evaluate this operation in a context where C-like coercion is
106 needed. */
107 virtual value *evaluate_with_coercion (struct expression *exp,
108 enum noside noside)
109 {
110 return evaluate (nullptr, exp, noside);
111 }
112
113 /* Evaluate this expression in the context of a cast to
114 EXPECT_TYPE. */
115 virtual value *evaluate_for_cast (struct type *expect_type,
116 struct expression *exp,
117 enum noside noside);
118
119 /* Evaluate this expression in the context of a sizeof
120 operation. */
121 virtual value *evaluate_for_sizeof (struct expression *exp,
122 enum noside noside);
123
124 /* Evaluate this expression in the context of an address-of
125 operation. Must return the address. */
126 virtual value *evaluate_for_address (struct expression *exp,
127 enum noside noside);
128
129 /* Evaluate a function call, with this object as the callee.
130 EXPECT_TYPE, EXP, and NOSIDE have the same meaning as in
131 'evaluate'. ARGS holds the operations that should be evaluated
132 to get the arguments to the call. */
133 virtual value *evaluate_funcall (struct type *expect_type,
134 struct expression *exp,
135 enum noside noside,
136 const std::vector<operation_up> &args)
137 {
138 /* Defer to the helper overload. */
139 return evaluate_funcall (expect_type, exp, noside, nullptr, args);
140 }
141
142 /* True if this is a constant expression. */
143 virtual bool constant_p () const
144 { return false; }
145
146 /* Return true if this operation uses OBJFILE (and will become
147 dangling when OBJFILE is unloaded), otherwise return false.
148 OBJFILE must not be a separate debug info file. */
149 virtual bool uses_objfile (struct objfile *objfile) const
150 { return false; }
151
152 /* Generate agent expression bytecodes for this operation. */
153 void generate_ax (struct expression *exp, struct agent_expr *ax,
154 struct axs_value *value,
155 struct type *cast_type = nullptr);
156
157 /* Return the opcode that is implemented by this operation. */
158 virtual enum exp_opcode opcode () const = 0;
159
160 /* Print this operation to STREAM. */
161 virtual void dump (struct ui_file *stream, int depth) const = 0;
162
163 /* Call to indicate that this is the outermost operation in the
164 expression. This should almost never be overridden. */
165 virtual void set_outermost () { }
166
167 protected:
168
169 /* A helper overload that wraps evaluate_subexp_do_call. */
170 value *evaluate_funcall (struct type *expect_type,
171 struct expression *exp,
172 enum noside noside,
173 const char *function_name,
174 const std::vector<operation_up> &args);
175
176 /* Called by generate_ax to do the work for this particular
177 operation. */
178 virtual void do_generate_ax (struct expression *exp,
179 struct agent_expr *ax,
180 struct axs_value *value,
181 struct type *cast_type)
182 {
183 error (_("Cannot translate to agent expression"));
184 }
185 };
186
187 /* A helper function for creating an operation_up, given a type. */
188 template<typename T, typename... Arg>
189 operation_up
190 make_operation (Arg... args)
191 {
192 return operation_up (new T (std::forward<Arg> (args)...));
193 }
194
195 }
196
197 struct expression
198 {
199 expression (const struct language_defn *lang, struct gdbarch *arch)
200 : language_defn (lang),
201 gdbarch (arch)
202 {
203 }
204
205 DISABLE_COPY_AND_ASSIGN (expression);
206
207 /* Return the opcode for the outermost sub-expression of this
208 expression. */
209 enum exp_opcode first_opcode () const
210 {
211 return op->opcode ();
212 }
213
214 /* Evaluate the expression. EXPECT_TYPE is the context type of the
215 expression; normally this should be nullptr. NOSIDE controls how
216 evaluation is performed. */
217 struct value *evaluate (struct type *expect_type, enum noside noside);
218
219 /* Language it was entered in. */
220 const struct language_defn *language_defn;
221 /* Architecture it was parsed in. */
222 struct gdbarch *gdbarch;
223 expr::operation_up op;
224 };
225
226 typedef std::unique_ptr<expression> expression_up;
227
228 /* From parse.c */
229
230 class innermost_block_tracker;
231 extern expression_up parse_expression (const char *,
232 innermost_block_tracker * = nullptr,
233 bool void_context_p = false);
234
235 extern expression_up parse_expression_with_language (const char *string,
236 enum language lang);
237
238 extern struct type *parse_expression_for_completion
239 (const char *, gdb::unique_xmalloc_ptr<char> *, enum type_code *);
240
241 class innermost_block_tracker;
242 extern expression_up parse_exp_1 (const char **, CORE_ADDR pc,
243 const struct block *, int,
244 innermost_block_tracker * = nullptr);
245
246 /* From eval.c */
247
248 /* Evaluate a function call. The function to be called is in CALLEE and
249 the arguments passed to the function are in ARGVEC.
250 FUNCTION_NAME is the name of the function, if known.
251 DEFAULT_RETURN_TYPE is used as the function's return type if the return
252 type is unknown. */
253
254 extern struct value *evaluate_subexp_do_call (expression *exp,
255 enum noside noside,
256 value *callee,
257 gdb::array_view<value *> argvec,
258 const char *function_name,
259 type *default_return_type);
260
261 /* From expprint.c */
262
263 extern const char *op_name (enum exp_opcode opcode);
264
265 extern void dump_prefix_expression (struct expression *, struct ui_file *);
266
267 /* In an OP_RANGE expression, either bound could be empty, indicating
268 that its value is by default that of the corresponding bound of the
269 array or string. Also, the upper end of the range can be exclusive
270 or inclusive. So we have six sorts of subrange. This enumeration
271 type is to identify this. */
272
273 enum range_flag : unsigned
274 {
275 /* This is a standard range. Both the lower and upper bounds are
276 defined, and the bounds are inclusive. */
277 RANGE_STANDARD = 0,
278
279 /* The low bound was not given. */
280 RANGE_LOW_BOUND_DEFAULT = 1 << 0,
281
282 /* The high bound was not given. */
283 RANGE_HIGH_BOUND_DEFAULT = 1 << 1,
284
285 /* The high bound of this range is exclusive. */
286 RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2,
287
288 /* The range has a stride. */
289 RANGE_HAS_STRIDE = 1 << 3,
290 };
291
292 DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags);
293
294 #endif /* !defined (EXPRESSION_H) */