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