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, | |
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 | ||
e2803273 TT |
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 | ||
a00b7254 TT |
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 | ||
e2803273 TT |
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 | ||
0c8effa3 TT |
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 | ||
e2803273 TT |
167 | protected: |
168 | ||
a00b7254 TT |
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 | ||
e2803273 TT |
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 | ||
c906108c | 197 | struct expression |
77bf7e99 | 198 | { |
1eaebe02 | 199 | expression (const struct language_defn *, struct gdbarch *); |
77bf7e99 TT |
200 | ~expression (); |
201 | DISABLE_COPY_AND_ASSIGN (expression); | |
202 | ||
2adab65c TT |
203 | /* Return the opcode for the outermost sub-expression of this |
204 | expression. */ | |
205 | enum exp_opcode first_opcode () const | |
206 | { | |
1eaebe02 | 207 | return op->opcode (); |
2adab65c TT |
208 | } |
209 | ||
26f53cd3 TT |
210 | /* Evaluate the expression. EXPECT_TYPE is the context type of the |
211 | expression; normally this should be nullptr. NOSIDE controls how | |
212 | evaluation is performed. */ | |
213 | struct value *evaluate (struct type *expect_type, enum noside noside); | |
214 | ||
77bf7e99 TT |
215 | /* Language it was entered in. */ |
216 | const struct language_defn *language_defn; | |
217 | /* Architecture it was parsed in. */ | |
218 | struct gdbarch *gdbarch; | |
413403fc | 219 | expr::operation_up op; |
77bf7e99 | 220 | }; |
c906108c | 221 | |
77bf7e99 | 222 | typedef std::unique_ptr<expression> expression_up; |
4d01a485 | 223 | |
c906108c SS |
224 | /* From parse.c */ |
225 | ||
699bd4cf TT |
226 | class innermost_block_tracker; |
227 | extern expression_up parse_expression (const char *, | |
8fc48b79 TT |
228 | innermost_block_tracker * = nullptr, |
229 | bool void_context_p = false); | |
c906108c | 230 | |
4d01a485 PA |
231 | extern expression_up parse_expression_with_language (const char *string, |
232 | enum language lang); | |
429e1e81 | 233 | |
3eac2b65 TT |
234 | extern struct type *parse_expression_for_completion |
235 | (const char *, gdb::unique_xmalloc_ptr<char> *, enum type_code *); | |
65d12d83 | 236 | |
699bd4cf | 237 | class innermost_block_tracker; |
4d01a485 | 238 | extern expression_up parse_exp_1 (const char **, CORE_ADDR pc, |
7ad417dd | 239 | const struct block *, int, |
699bd4cf | 240 | innermost_block_tracker * = nullptr); |
c906108c | 241 | |
c906108c SS |
242 | /* From eval.c */ |
243 | ||
1ab8280d TT |
244 | /* Evaluate a function call. The function to be called is in CALLEE and |
245 | the arguments passed to the function are in ARGVEC. | |
6d816919 AB |
246 | FUNCTION_NAME is the name of the function, if known. |
247 | DEFAULT_RETURN_TYPE is used as the function's return type if the return | |
248 | type is unknown. */ | |
249 | ||
250 | extern struct value *evaluate_subexp_do_call (expression *exp, | |
251 | enum noside noside, | |
1ab8280d TT |
252 | value *callee, |
253 | gdb::array_view<value *> argvec, | |
6d816919 AB |
254 | const char *function_name, |
255 | type *default_return_type); | |
256 | ||
c906108c SS |
257 | /* From expprint.c */ |
258 | ||
88b91969 | 259 | extern const char *op_name (enum exp_opcode opcode); |
bd0b9f9e | 260 | |
24daaebc | 261 | extern void dump_prefix_expression (struct expression *, struct ui_file *); |
c906108c | 262 | |
01739a3b TT |
263 | /* In an OP_RANGE expression, either bound could be empty, indicating |
264 | that its value is by default that of the corresponding bound of the | |
6873858b TT |
265 | array or string. Also, the upper end of the range can be exclusive |
266 | or inclusive. So we have six sorts of subrange. This enumeration | |
267 | type is to identify this. */ | |
268 | ||
f2d8e4c5 | 269 | enum range_flag : unsigned |
6873858b | 270 | { |
2f1b18db AB |
271 | /* This is a standard range. Both the lower and upper bounds are |
272 | defined, and the bounds are inclusive. */ | |
273 | RANGE_STANDARD = 0, | |
274 | ||
275 | /* The low bound was not given. */ | |
276 | RANGE_LOW_BOUND_DEFAULT = 1 << 0, | |
277 | ||
278 | /* The high bound was not given. */ | |
279 | RANGE_HIGH_BOUND_DEFAULT = 1 << 1, | |
280 | ||
281 | /* The high bound of this range is exclusive. */ | |
282 | RANGE_HIGH_BOUND_EXCLUSIVE = 1 << 2, | |
6b4c676c AB |
283 | |
284 | /* The range has a stride. */ | |
285 | RANGE_HAS_STRIDE = 1 << 3, | |
6873858b | 286 | }; |
01739a3b | 287 | |
f2d8e4c5 | 288 | DEF_ENUM_FLAGS_TYPE (enum range_flag, range_flags); |
2f1b18db | 289 | |
c5aa993b | 290 | #endif /* !defined (EXPRESSION_H) */ |