Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Evaluate expressions 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 | #include "defs.h" | |
4de283e4 TT |
21 | #include "symtab.h" |
22 | #include "gdbtypes.h" | |
23 | #include "value.h" | |
c906108c | 24 | #include "expression.h" |
4de283e4 | 25 | #include "target.h" |
c906108c | 26 | #include "frame.h" |
6c659fc2 | 27 | #include "gdbthread.h" |
4de283e4 | 28 | #include "language.h" /* For CAST_IS_CONVERSION. */ |
4de283e4 | 29 | #include "cp-abi.h" |
04714b91 | 30 | #include "infcall.h" |
a9fa03de | 31 | #include "objc-lang.h" |
4de283e4 | 32 | #include "block.h" |
5f9769d1 | 33 | #include "parser-defs.h" |
4de283e4 | 34 | #include "cp-support.h" |
d55e5aa6 | 35 | #include "ui-out.h" |
4de283e4 | 36 | #include "regcache.h" |
029a67e4 | 37 | #include "user-regs.h" |
79a45b7d | 38 | #include "valprint.h" |
4de283e4 TT |
39 | #include "gdb_obstack.h" |
40 | #include "objfiles.h" | |
41 | #include "typeprint.h" | |
42 | #include <ctype.h> | |
e2803273 | 43 | #include "expop.h" |
06dc61b9 | 44 | #include "c-exp.h" |
bc3b79fd | 45 | |
0963b4bd | 46 | /* Prototypes for local functions. */ |
c906108c | 47 | |
5ecaaa66 SA |
48 | static struct value *evaluate_subexp_for_sizeof (struct expression *, int *, |
49 | enum noside); | |
c906108c | 50 | |
61051030 AC |
51 | static struct value *evaluate_subexp_for_address (struct expression *, |
52 | int *, enum noside); | |
c906108c | 53 | |
46a4882b PA |
54 | static value *evaluate_subexp_for_cast (expression *exp, int *pos, |
55 | enum noside noside, | |
56 | struct type *type); | |
57 | ||
61051030 AC |
58 | static struct value *evaluate_struct_tuple (struct value *, |
59 | struct expression *, int *, | |
60 | enum noside, int); | |
c906108c | 61 | |
4b27a620 | 62 | struct value * |
aa1ee363 AC |
63 | evaluate_subexp (struct type *expect_type, struct expression *exp, |
64 | int *pos, enum noside noside) | |
c906108c | 65 | { |
26f53cd3 TT |
66 | return ((*exp->language_defn->expression_ops ()->evaluate_exp) |
67 | (expect_type, exp, pos, noside)); | |
c906108c SS |
68 | } |
69 | \f | |
70 | /* Parse the string EXP as a C expression, evaluate it, | |
71 | and return the result as a number. */ | |
72 | ||
73 | CORE_ADDR | |
bbc13ae3 | 74 | parse_and_eval_address (const char *exp) |
c906108c | 75 | { |
4d01a485 PA |
76 | expression_up expr = parse_expression (exp); |
77 | ||
78 | return value_as_address (evaluate_expression (expr.get ())); | |
c906108c SS |
79 | } |
80 | ||
bb518678 | 81 | /* Like parse_and_eval_address, but treats the value of the expression |
0963b4bd | 82 | as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */ |
bb518678 | 83 | LONGEST |
a1b8c4cc | 84 | parse_and_eval_long (const char *exp) |
bb518678 | 85 | { |
4d01a485 PA |
86 | expression_up expr = parse_expression (exp); |
87 | ||
88 | return value_as_long (evaluate_expression (expr.get ())); | |
bb518678 DT |
89 | } |
90 | ||
61051030 | 91 | struct value * |
bbc13ae3 | 92 | parse_and_eval (const char *exp) |
c906108c | 93 | { |
4d01a485 | 94 | expression_up expr = parse_expression (exp); |
c906108c | 95 | |
4d01a485 | 96 | return evaluate_expression (expr.get ()); |
c906108c SS |
97 | } |
98 | ||
99 | /* Parse up to a comma (or to a closeparen) | |
100 | in the string EXPP as an expression, evaluate it, and return the value. | |
101 | EXPP is advanced to point to the comma. */ | |
102 | ||
61051030 | 103 | struct value * |
bbc13ae3 | 104 | parse_to_comma_and_eval (const char **expp) |
c906108c | 105 | { |
582942f4 | 106 | expression_up expr = parse_exp_1 (expp, 0, nullptr, 1); |
c906108c | 107 | |
4d01a485 | 108 | return evaluate_expression (expr.get ()); |
c906108c SS |
109 | } |
110 | \f | |
c906108c | 111 | |
26f53cd3 TT |
112 | /* See expression.h. */ |
113 | ||
114 | struct value * | |
115 | expression::evaluate (struct type *expect_type, enum noside noside) | |
116 | { | |
117 | gdb::optional<enable_thread_stack_temporaries> stack_temporaries; | |
118 | if (target_has_execution () | |
119 | && language_defn->la_language == language_cplus | |
120 | && !thread_stack_temporaries_enabled_p (inferior_thread ())) | |
121 | stack_temporaries.emplace (inferior_thread ()); | |
122 | ||
123 | int pos = 0; | |
124 | struct value *retval = evaluate_subexp (expect_type, this, &pos, noside); | |
125 | ||
126 | if (stack_temporaries.has_value () | |
127 | && value_in_thread_stack_temporaries (retval, inferior_thread ())) | |
128 | retval = value_non_lval (retval); | |
129 | ||
130 | return retval; | |
131 | } | |
132 | ||
efd7ff14 | 133 | /* See value.h. */ |
c906108c | 134 | |
61051030 | 135 | struct value * |
efd7ff14 | 136 | evaluate_expression (struct expression *exp, struct type *expect_type) |
c906108c | 137 | { |
26f53cd3 | 138 | return exp->evaluate (expect_type, EVAL_NORMAL); |
c906108c SS |
139 | } |
140 | ||
141 | /* Evaluate an expression, avoiding all memory references | |
142 | and getting a value whose type alone is correct. */ | |
143 | ||
61051030 | 144 | struct value * |
fba45db2 | 145 | evaluate_type (struct expression *exp) |
c906108c | 146 | { |
26f53cd3 | 147 | return exp->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS); |
c906108c SS |
148 | } |
149 | ||
65d12d83 TT |
150 | /* Evaluate a subexpression, avoiding all memory references and |
151 | getting a value whose type alone is correct. */ | |
152 | ||
153 | struct value * | |
154 | evaluate_subexpression_type (struct expression *exp, int subexp) | |
155 | { | |
fe1fe7ea | 156 | return evaluate_subexp (nullptr, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS); |
65d12d83 TT |
157 | } |
158 | ||
0cf6dd15 TJB |
159 | /* Find the current value of a watchpoint on EXP. Return the value in |
160 | *VALP and *RESULTP and the chain of intermediate and final values | |
161 | in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does | |
162 | not need them. | |
163 | ||
3a1115a0 TT |
164 | If PRESERVE_ERRORS is true, then exceptions are passed through. |
165 | Otherwise, if PRESERVE_ERRORS is false, then if a memory error | |
166 | occurs while evaluating the expression, *RESULTP will be set to | |
167 | NULL. *RESULTP may be a lazy value, if the result could not be | |
168 | read from memory. It is used to determine whether a value is | |
169 | user-specified (we should watch the whole value) or intermediate | |
0cf6dd15 TJB |
170 | (we should watch only the bit used to locate the final value). |
171 | ||
172 | If the final value, or any intermediate value, could not be read | |
173 | from memory, *VALP will be set to NULL. *VAL_CHAIN will still be | |
174 | set to any referenced values. *VALP will never be a lazy value. | |
175 | This is the value which we store in struct breakpoint. | |
176 | ||
a6535de1 TT |
177 | If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be |
178 | released from the value chain. If VAL_CHAIN is NULL, all generated | |
179 | values will be left on the value chain. */ | |
0cf6dd15 TJB |
180 | |
181 | void | |
182 | fetch_subexp_value (struct expression *exp, int *pc, struct value **valp, | |
a6535de1 TT |
183 | struct value **resultp, |
184 | std::vector<value_ref_ptr> *val_chain, | |
2e362716 | 185 | bool preserve_errors) |
0cf6dd15 TJB |
186 | { |
187 | struct value *mark, *new_mark, *result; | |
0cf6dd15 TJB |
188 | |
189 | *valp = NULL; | |
190 | if (resultp) | |
191 | *resultp = NULL; | |
192 | if (val_chain) | |
a6535de1 | 193 | val_chain->clear (); |
0cf6dd15 TJB |
194 | |
195 | /* Evaluate the expression. */ | |
196 | mark = value_mark (); | |
197 | result = NULL; | |
198 | ||
a70b8144 | 199 | try |
0cf6dd15 | 200 | { |
fe1fe7ea | 201 | result = evaluate_subexp (nullptr, exp, pc, EVAL_NORMAL); |
0cf6dd15 | 202 | } |
230d2906 | 203 | catch (const gdb_exception &ex) |
0cf6dd15 | 204 | { |
3a1115a0 | 205 | /* Ignore memory errors if we want watchpoints pointing at |
0cf6dd15 TJB |
206 | inaccessible memory to still be created; otherwise, throw the |
207 | error to some higher catcher. */ | |
208 | switch (ex.error) | |
209 | { | |
210 | case MEMORY_ERROR: | |
3a1115a0 TT |
211 | if (!preserve_errors) |
212 | break; | |
565e0eda | 213 | /* Fall through. */ |
0cf6dd15 | 214 | default: |
eedc3f4f | 215 | throw; |
0cf6dd15 TJB |
216 | break; |
217 | } | |
218 | } | |
219 | ||
220 | new_mark = value_mark (); | |
221 | if (mark == new_mark) | |
222 | return; | |
223 | if (resultp) | |
224 | *resultp = result; | |
225 | ||
226 | /* Make sure it's not lazy, so that after the target stops again we | |
227 | have a non-lazy previous value to compare with. */ | |
8e7b59a5 KS |
228 | if (result != NULL) |
229 | { | |
230 | if (!value_lazy (result)) | |
231 | *valp = result; | |
232 | else | |
233 | { | |
8e7b59a5 | 234 | |
a70b8144 | 235 | try |
8e7b59a5 KS |
236 | { |
237 | value_fetch_lazy (result); | |
238 | *valp = result; | |
239 | } | |
230d2906 | 240 | catch (const gdb_exception_error &except) |
492d29ea PA |
241 | { |
242 | } | |
8e7b59a5 KS |
243 | } |
244 | } | |
0cf6dd15 TJB |
245 | |
246 | if (val_chain) | |
247 | { | |
248 | /* Return the chain of intermediate values. We use this to | |
249 | decide which addresses to watch. */ | |
a6535de1 | 250 | *val_chain = value_release_to_mark (mark); |
0cf6dd15 TJB |
251 | } |
252 | } | |
253 | ||
65d12d83 TT |
254 | /* Extract a field operation from an expression. If the subexpression |
255 | of EXP starting at *SUBEXP is not a structure dereference | |
256 | operation, return NULL. Otherwise, return the name of the | |
257 | dereferenced field, and advance *SUBEXP to point to the | |
258 | subexpression of the left-hand-side of the dereference. This is | |
259 | used when completing field names. */ | |
260 | ||
3eac2b65 | 261 | const char * |
65d12d83 TT |
262 | extract_field_op (struct expression *exp, int *subexp) |
263 | { | |
264 | int tem; | |
265 | char *result; | |
d7f9d729 | 266 | |
65d12d83 TT |
267 | if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT |
268 | && exp->elts[*subexp].opcode != STRUCTOP_PTR) | |
269 | return NULL; | |
270 | tem = longest_to_int (exp->elts[*subexp + 1].longconst); | |
271 | result = &exp->elts[*subexp + 2].string; | |
272 | (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
273 | return result; | |
274 | } | |
275 | ||
f0559fff YQ |
276 | /* This function evaluates brace-initializers (in C/C++) for |
277 | structure types. */ | |
c906108c | 278 | |
61051030 AC |
279 | static struct value * |
280 | evaluate_struct_tuple (struct value *struct_val, | |
aa1ee363 AC |
281 | struct expression *exp, |
282 | int *pos, enum noside noside, int nargs) | |
c906108c | 283 | { |
df407dfe | 284 | struct type *struct_type = check_typedef (value_type (struct_val)); |
c906108c SS |
285 | struct type *field_type; |
286 | int fieldno = -1; | |
d7f9d729 | 287 | |
c5aa993b | 288 | while (--nargs >= 0) |
c906108c | 289 | { |
61051030 | 290 | struct value *val = NULL; |
c906108c | 291 | int bitpos, bitsize; |
0fd88904 | 292 | bfd_byte *addr; |
c5aa993b | 293 | |
f0559fff YQ |
294 | fieldno++; |
295 | /* Skip static fields. */ | |
1f704f76 | 296 | while (fieldno < struct_type->num_fields () |
ceacbf6e | 297 | && field_is_static (&struct_type->field (fieldno))) |
f0559fff | 298 | fieldno++; |
1f704f76 | 299 | if (fieldno >= struct_type->num_fields ()) |
f0559fff | 300 | error (_("too many initializers")); |
940da03e | 301 | field_type = struct_type->field (fieldno).type (); |
78134374 | 302 | if (field_type->code () == TYPE_CODE_UNION |
f0559fff YQ |
303 | && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0') |
304 | error (_("don't know which variant you want to set")); | |
305 | ||
306 | /* Here, struct_type is the type of the inner struct, | |
307 | while substruct_type is the type of the inner struct. | |
308 | These are the same for normal structures, but a variant struct | |
309 | contains anonymous union fields that contain substruct fields. | |
310 | The value fieldno is the index of the top-level (normal or | |
311 | anonymous union) field in struct_field, while the value | |
312 | subfieldno is the index of the actual real (named inner) field | |
313 | in substruct_type. */ | |
314 | ||
940da03e | 315 | field_type = struct_type->field (fieldno).type (); |
f0559fff YQ |
316 | if (val == 0) |
317 | val = evaluate_subexp (field_type, exp, pos, noside); | |
318 | ||
319 | /* Now actually set the field in struct_val. */ | |
320 | ||
321 | /* Assign val to field fieldno. */ | |
322 | if (value_type (val) != field_type) | |
323 | val = value_cast (field_type, val); | |
324 | ||
325 | bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno); | |
326 | bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno); | |
327 | addr = value_contents_writeable (struct_val) + bitpos / 8; | |
328 | if (bitsize) | |
329 | modify_field (struct_type, addr, | |
330 | value_as_long (val), bitpos % 8, bitsize); | |
331 | else | |
332 | memcpy (addr, value_contents (val), | |
333 | TYPE_LENGTH (value_type (val))); | |
c906108c | 334 | |
c906108c SS |
335 | } |
336 | return struct_val; | |
337 | } | |
338 | ||
4066e646 UW |
339 | /* Promote value ARG1 as appropriate before performing a unary operation |
340 | on this argument. | |
341 | If the result is not appropriate for any particular language then it | |
342 | needs to patch this function. */ | |
343 | ||
344 | void | |
345 | unop_promote (const struct language_defn *language, struct gdbarch *gdbarch, | |
346 | struct value **arg1) | |
347 | { | |
348 | struct type *type1; | |
349 | ||
350 | *arg1 = coerce_ref (*arg1); | |
351 | type1 = check_typedef (value_type (*arg1)); | |
352 | ||
353 | if (is_integral_type (type1)) | |
354 | { | |
355 | switch (language->la_language) | |
356 | { | |
357 | default: | |
358 | /* Perform integral promotion for ANSI C/C++. | |
85102364 | 359 | If not appropriate for any particular language |
4066e646 UW |
360 | it needs to modify this function. */ |
361 | { | |
362 | struct type *builtin_int = builtin_type (gdbarch)->builtin_int; | |
d7f9d729 | 363 | |
4066e646 UW |
364 | if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int)) |
365 | *arg1 = value_cast (builtin_int, *arg1); | |
366 | } | |
367 | break; | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | /* Promote values ARG1 and ARG2 as appropriate before performing a binary | |
373 | operation on those two operands. | |
374 | If the result is not appropriate for any particular language then it | |
375 | needs to patch this function. */ | |
376 | ||
377 | void | |
378 | binop_promote (const struct language_defn *language, struct gdbarch *gdbarch, | |
379 | struct value **arg1, struct value **arg2) | |
380 | { | |
381 | struct type *promoted_type = NULL; | |
382 | struct type *type1; | |
383 | struct type *type2; | |
384 | ||
385 | *arg1 = coerce_ref (*arg1); | |
386 | *arg2 = coerce_ref (*arg2); | |
387 | ||
388 | type1 = check_typedef (value_type (*arg1)); | |
389 | type2 = check_typedef (value_type (*arg2)); | |
390 | ||
78134374 SM |
391 | if ((type1->code () != TYPE_CODE_FLT |
392 | && type1->code () != TYPE_CODE_DECFLOAT | |
4066e646 | 393 | && !is_integral_type (type1)) |
78134374 SM |
394 | || (type2->code () != TYPE_CODE_FLT |
395 | && type2->code () != TYPE_CODE_DECFLOAT | |
4066e646 UW |
396 | && !is_integral_type (type2))) |
397 | return; | |
398 | ||
0a12719e JB |
399 | if (is_fixed_point_type (type1) || is_fixed_point_type (type2)) |
400 | return; | |
401 | ||
78134374 SM |
402 | if (type1->code () == TYPE_CODE_DECFLOAT |
403 | || type2->code () == TYPE_CODE_DECFLOAT) | |
4066e646 UW |
404 | { |
405 | /* No promotion required. */ | |
406 | } | |
78134374 SM |
407 | else if (type1->code () == TYPE_CODE_FLT |
408 | || type2->code () == TYPE_CODE_FLT) | |
4066e646 UW |
409 | { |
410 | switch (language->la_language) | |
411 | { | |
412 | case language_c: | |
413 | case language_cplus: | |
414 | case language_asm: | |
415 | case language_objc: | |
f4b8a18d | 416 | case language_opencl: |
4066e646 UW |
417 | /* No promotion required. */ |
418 | break; | |
419 | ||
420 | default: | |
421 | /* For other languages the result type is unchanged from gdb | |
422 | version 6.7 for backward compatibility. | |
423 | If either arg was long double, make sure that value is also long | |
424 | double. Otherwise use double. */ | |
425 | if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch) | |
426 | || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch)) | |
427 | promoted_type = builtin_type (gdbarch)->builtin_long_double; | |
428 | else | |
429 | promoted_type = builtin_type (gdbarch)->builtin_double; | |
430 | break; | |
431 | } | |
432 | } | |
78134374 SM |
433 | else if (type1->code () == TYPE_CODE_BOOL |
434 | && type2->code () == TYPE_CODE_BOOL) | |
4066e646 UW |
435 | { |
436 | /* No promotion required. */ | |
437 | } | |
438 | else | |
439 | /* Integral operations here. */ | |
440 | /* FIXME: Also mixed integral/booleans, with result an integer. */ | |
441 | { | |
442 | const struct builtin_type *builtin = builtin_type (gdbarch); | |
443 | unsigned int promoted_len1 = TYPE_LENGTH (type1); | |
444 | unsigned int promoted_len2 = TYPE_LENGTH (type2); | |
c6d940a9 SM |
445 | int is_unsigned1 = type1->is_unsigned (); |
446 | int is_unsigned2 = type2->is_unsigned (); | |
4066e646 UW |
447 | unsigned int result_len; |
448 | int unsigned_operation; | |
449 | ||
450 | /* Determine type length and signedness after promotion for | |
dda83cd7 | 451 | both operands. */ |
4066e646 UW |
452 | if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int)) |
453 | { | |
454 | is_unsigned1 = 0; | |
455 | promoted_len1 = TYPE_LENGTH (builtin->builtin_int); | |
456 | } | |
457 | if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int)) | |
458 | { | |
459 | is_unsigned2 = 0; | |
460 | promoted_len2 = TYPE_LENGTH (builtin->builtin_int); | |
461 | } | |
462 | ||
463 | if (promoted_len1 > promoted_len2) | |
464 | { | |
465 | unsigned_operation = is_unsigned1; | |
466 | result_len = promoted_len1; | |
467 | } | |
468 | else if (promoted_len2 > promoted_len1) | |
469 | { | |
470 | unsigned_operation = is_unsigned2; | |
471 | result_len = promoted_len2; | |
472 | } | |
473 | else | |
474 | { | |
475 | unsigned_operation = is_unsigned1 || is_unsigned2; | |
476 | result_len = promoted_len1; | |
477 | } | |
478 | ||
479 | switch (language->la_language) | |
480 | { | |
481 | case language_c: | |
482 | case language_cplus: | |
483 | case language_asm: | |
484 | case language_objc: | |
485 | if (result_len <= TYPE_LENGTH (builtin->builtin_int)) | |
486 | { | |
487 | promoted_type = (unsigned_operation | |
488 | ? builtin->builtin_unsigned_int | |
489 | : builtin->builtin_int); | |
490 | } | |
491 | else if (result_len <= TYPE_LENGTH (builtin->builtin_long)) | |
492 | { | |
493 | promoted_type = (unsigned_operation | |
494 | ? builtin->builtin_unsigned_long | |
495 | : builtin->builtin_long); | |
496 | } | |
497 | else | |
498 | { | |
499 | promoted_type = (unsigned_operation | |
500 | ? builtin->builtin_unsigned_long_long | |
501 | : builtin->builtin_long_long); | |
502 | } | |
503 | break; | |
f4b8a18d KW |
504 | case language_opencl: |
505 | if (result_len <= TYPE_LENGTH (lookup_signed_typename | |
b858499d | 506 | (language, "int"))) |
f4b8a18d KW |
507 | { |
508 | promoted_type = | |
509 | (unsigned_operation | |
b858499d SM |
510 | ? lookup_unsigned_typename (language, "int") |
511 | : lookup_signed_typename (language, "int")); | |
f4b8a18d KW |
512 | } |
513 | else if (result_len <= TYPE_LENGTH (lookup_signed_typename | |
b858499d | 514 | (language, "long"))) |
f4b8a18d KW |
515 | { |
516 | promoted_type = | |
517 | (unsigned_operation | |
b858499d SM |
518 | ? lookup_unsigned_typename (language, "long") |
519 | : lookup_signed_typename (language,"long")); | |
f4b8a18d KW |
520 | } |
521 | break; | |
4066e646 UW |
522 | default: |
523 | /* For other languages the result type is unchanged from gdb | |
524 | version 6.7 for backward compatibility. | |
525 | If either arg was long long, make sure that value is also long | |
526 | long. Otherwise use long. */ | |
527 | if (unsigned_operation) | |
528 | { | |
529 | if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT) | |
530 | promoted_type = builtin->builtin_unsigned_long_long; | |
531 | else | |
532 | promoted_type = builtin->builtin_unsigned_long; | |
533 | } | |
534 | else | |
535 | { | |
536 | if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT) | |
537 | promoted_type = builtin->builtin_long_long; | |
538 | else | |
539 | promoted_type = builtin->builtin_long; | |
540 | } | |
541 | break; | |
542 | } | |
543 | } | |
544 | ||
545 | if (promoted_type) | |
546 | { | |
547 | /* Promote both operands to common type. */ | |
548 | *arg1 = value_cast (promoted_type, *arg1); | |
549 | *arg2 = value_cast (promoted_type, *arg2); | |
550 | } | |
551 | } | |
552 | ||
89eef114 | 553 | static int |
cc73bb8c | 554 | ptrmath_type_p (const struct language_defn *lang, struct type *type) |
89eef114 UW |
555 | { |
556 | type = check_typedef (type); | |
aa006118 | 557 | if (TYPE_IS_REFERENCE (type)) |
89eef114 UW |
558 | type = TYPE_TARGET_TYPE (type); |
559 | ||
78134374 | 560 | switch (type->code ()) |
89eef114 UW |
561 | { |
562 | case TYPE_CODE_PTR: | |
563 | case TYPE_CODE_FUNC: | |
564 | return 1; | |
565 | ||
566 | case TYPE_CODE_ARRAY: | |
67bd3fd5 | 567 | return type->is_vector () ? 0 : lang->c_style_arrays_p (); |
89eef114 UW |
568 | |
569 | default: | |
570 | return 0; | |
571 | } | |
572 | } | |
573 | ||
c83833f4 PA |
574 | /* Represents a fake method with the given parameter types. This is |
575 | used by the parser to construct a temporary "expected" type for | |
3693fdb3 PA |
576 | method overload resolution. FLAGS is used as instance flags of the |
577 | new type, in order to be able to make the new type represent a | |
578 | const/volatile overload. */ | |
072bba3b | 579 | |
c83833f4 | 580 | class fake_method |
072bba3b | 581 | { |
c83833f4 PA |
582 | public: |
583 | fake_method (type_instance_flags flags, | |
584 | int num_types, struct type **param_types); | |
585 | ~fake_method (); | |
586 | ||
587 | /* The constructed type. */ | |
588 | struct type *type () { return &m_type; } | |
589 | ||
590 | private: | |
591 | struct type m_type {}; | |
592 | main_type m_main_type {}; | |
593 | }; | |
594 | ||
595 | fake_method::fake_method (type_instance_flags flags, | |
596 | int num_types, struct type **param_types) | |
597 | { | |
598 | struct type *type = &m_type; | |
599 | ||
600 | TYPE_MAIN_TYPE (type) = &m_main_type; | |
072bba3b | 601 | TYPE_LENGTH (type) = 1; |
67607e24 | 602 | type->set_code (TYPE_CODE_METHOD); |
072bba3b | 603 | TYPE_CHAIN (type) = type; |
314ad88d | 604 | type->set_instance_flags (flags); |
e314d629 | 605 | if (num_types > 0) |
a6fb9c08 | 606 | { |
e314d629 TT |
607 | if (param_types[num_types - 1] == NULL) |
608 | { | |
609 | --num_types; | |
1d6286ed | 610 | type->set_has_varargs (true); |
e314d629 | 611 | } |
78134374 | 612 | else if (check_typedef (param_types[num_types - 1])->code () |
e314d629 TT |
613 | == TYPE_CODE_VOID) |
614 | { | |
615 | --num_types; | |
616 | /* Caller should have ensured this. */ | |
617 | gdb_assert (num_types == 0); | |
27e69b7a | 618 | type->set_is_prototyped (true); |
e314d629 | 619 | } |
a6fb9c08 | 620 | } |
e314d629 | 621 | |
2fabdf33 AB |
622 | /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by |
623 | neither an objfile nor a gdbarch. As a result we must manually | |
624 | allocate memory for auxiliary fields, and free the memory ourselves | |
625 | when we are done with it. */ | |
5e33d5f4 | 626 | type->set_num_fields (num_types); |
3cabb6b0 SM |
627 | type->set_fields |
628 | ((struct field *) xzalloc (sizeof (struct field) * num_types)); | |
072bba3b KS |
629 | |
630 | while (num_types-- > 0) | |
5d14b6e5 | 631 | type->field (num_types).set_type (param_types[num_types]); |
c83833f4 | 632 | } |
072bba3b | 633 | |
c83833f4 PA |
634 | fake_method::~fake_method () |
635 | { | |
80fc5e77 | 636 | xfree (m_type.fields ()); |
072bba3b KS |
637 | } |
638 | ||
44b675c8 TT |
639 | namespace expr |
640 | { | |
641 | ||
642 | value * | |
643 | type_instance_operation::evaluate (struct type *expect_type, | |
644 | struct expression *exp, | |
645 | enum noside noside) | |
646 | { | |
647 | type_instance_flags flags = std::get<0> (m_storage); | |
648 | std::vector<type *> &types = std::get<1> (m_storage); | |
649 | ||
650 | fake_method fake_expect_type (flags, types.size (), types.data ()); | |
651 | return std::get<2> (m_storage)->evaluate (fake_expect_type.type (), | |
652 | exp, noside); | |
653 | } | |
654 | ||
655 | } | |
656 | ||
fe13dfec PA |
657 | /* Helper for evaluating an OP_VAR_VALUE. */ |
658 | ||
ced9779b | 659 | value * |
fe13dfec PA |
660 | evaluate_var_value (enum noside noside, const block *blk, symbol *var) |
661 | { | |
662 | /* JYG: We used to just return value_zero of the symbol type if | |
663 | we're asked to avoid side effects. Otherwise we return | |
664 | value_of_variable (...). However I'm not sure if | |
665 | value_of_variable () has any side effect. We need a full value | |
666 | object returned here for whatis_exp () to call evaluate_type () | |
667 | and then pass the full value to value_rtti_target_type () if we | |
668 | are dealing with a pointer or reference to a base class and print | |
669 | object is on. */ | |
670 | ||
671 | struct value *ret = NULL; | |
672 | ||
a70b8144 | 673 | try |
fe13dfec PA |
674 | { |
675 | ret = value_of_variable (var, blk); | |
676 | } | |
677 | ||
230d2906 | 678 | catch (const gdb_exception_error &except) |
fe13dfec PA |
679 | { |
680 | if (noside != EVAL_AVOID_SIDE_EFFECTS) | |
eedc3f4f | 681 | throw; |
fe13dfec PA |
682 | |
683 | ret = value_zero (SYMBOL_TYPE (var), not_lval); | |
684 | } | |
fe13dfec PA |
685 | |
686 | return ret; | |
687 | } | |
688 | ||
e82a5afc TT |
689 | namespace expr |
690 | ||
691 | { | |
692 | ||
693 | value * | |
694 | var_value_operation::evaluate (struct type *expect_type, | |
695 | struct expression *exp, | |
696 | enum noside noside) | |
697 | { | |
698 | symbol *var = std::get<0> (m_storage); | |
699 | if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR) | |
700 | error_unknown_type (var->print_name ()); | |
701 | return evaluate_var_value (noside, std::get<1> (m_storage), var); | |
702 | } | |
703 | ||
704 | } /* namespace expr */ | |
705 | ||
74ea4be4 PA |
706 | /* Helper for evaluating an OP_VAR_MSYM_VALUE. */ |
707 | ||
ced9779b | 708 | value * |
74ea4be4 PA |
709 | evaluate_var_msym_value (enum noside noside, |
710 | struct objfile *objfile, minimal_symbol *msymbol) | |
711 | { | |
8388016d PA |
712 | CORE_ADDR address; |
713 | type *the_type = find_minsym_type_and_address (msymbol, objfile, &address); | |
714 | ||
0becda7a | 715 | if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ()) |
8388016d | 716 | return value_zero (the_type, not_lval); |
74ea4be4 | 717 | else |
8388016d | 718 | return value_at_lazy (the_type, address); |
74ea4be4 PA |
719 | } |
720 | ||
827d0c51 PA |
721 | /* Helper for returning a value when handling EVAL_SKIP. */ |
722 | ||
ced9779b | 723 | value * |
827d0c51 PA |
724 | eval_skip_value (expression *exp) |
725 | { | |
726 | return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1); | |
727 | } | |
728 | ||
6d816919 | 729 | /* See expression.h. */ |
e69570ee | 730 | |
6d816919 AB |
731 | value * |
732 | evaluate_subexp_do_call (expression *exp, enum noside noside, | |
1ab8280d TT |
733 | value *callee, |
734 | gdb::array_view<value *> argvec, | |
6d816919 AB |
735 | const char *function_name, |
736 | type *default_return_type) | |
e69570ee | 737 | { |
1ab8280d | 738 | if (callee == NULL) |
e69570ee PA |
739 | error (_("Cannot evaluate function -- may be inlined")); |
740 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
741 | { | |
742 | /* If the return type doesn't look like a function type, | |
743 | call an error. This can happen if somebody tries to turn | |
744 | a variable into a function call. */ | |
745 | ||
1ab8280d | 746 | type *ftype = value_type (callee); |
e69570ee | 747 | |
78134374 | 748 | if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION) |
e69570ee PA |
749 | { |
750 | /* We don't know anything about what the internal | |
751 | function might return, but we have to return | |
752 | something. */ | |
753 | return value_zero (builtin_type (exp->gdbarch)->builtin_int, | |
754 | not_lval); | |
755 | } | |
78134374 | 756 | else if (ftype->code () == TYPE_CODE_XMETHOD) |
e69570ee | 757 | { |
1ab8280d | 758 | type *return_type = result_type_of_xmethod (callee, argvec); |
e69570ee PA |
759 | |
760 | if (return_type == NULL) | |
761 | error (_("Xmethod is missing return type.")); | |
762 | return value_zero (return_type, not_lval); | |
763 | } | |
78134374 SM |
764 | else if (ftype->code () == TYPE_CODE_FUNC |
765 | || ftype->code () == TYPE_CODE_METHOD) | |
e69570ee | 766 | { |
0becda7a | 767 | if (ftype->is_gnu_ifunc ()) |
8388016d | 768 | { |
1ab8280d | 769 | CORE_ADDR address = value_address (callee); |
8388016d PA |
770 | type *resolved_type = find_gnu_ifunc_target_type (address); |
771 | ||
772 | if (resolved_type != NULL) | |
773 | ftype = resolved_type; | |
774 | } | |
775 | ||
e69570ee PA |
776 | type *return_type = TYPE_TARGET_TYPE (ftype); |
777 | ||
778 | if (return_type == NULL) | |
779 | return_type = default_return_type; | |
780 | ||
781 | if (return_type == NULL) | |
782 | error_call_unknown_return_type (function_name); | |
783 | ||
784 | return allocate_value (return_type); | |
785 | } | |
786 | else | |
787 | error (_("Expression of type other than " | |
788 | "\"Function returning ...\" used as function")); | |
789 | } | |
1ab8280d | 790 | switch (value_type (callee)->code ()) |
e69570ee PA |
791 | { |
792 | case TYPE_CODE_INTERNAL_FUNCTION: | |
793 | return call_internal_function (exp->gdbarch, exp->language_defn, | |
1ab8280d | 794 | callee, argvec.size (), argvec.data ()); |
e69570ee | 795 | case TYPE_CODE_XMETHOD: |
1ab8280d | 796 | return call_xmethod (callee, argvec); |
e69570ee | 797 | default: |
1ab8280d | 798 | return call_function_by_hand (callee, default_return_type, argvec); |
e69570ee PA |
799 | } |
800 | } | |
801 | ||
802 | /* Helper for evaluating an OP_FUNCALL. */ | |
803 | ||
804 | static value * | |
805 | evaluate_funcall (type *expect_type, expression *exp, int *pos, | |
806 | enum noside noside) | |
807 | { | |
808 | int tem; | |
809 | int pc2 = 0; | |
810 | value *arg1 = NULL; | |
811 | value *arg2 = NULL; | |
812 | int save_pos1; | |
813 | symbol *function = NULL; | |
814 | char *function_name = NULL; | |
815 | const char *var_func_name = NULL; | |
816 | ||
817 | int pc = (*pos); | |
818 | (*pos) += 2; | |
819 | ||
820 | exp_opcode op = exp->elts[*pos].opcode; | |
821 | int nargs = longest_to_int (exp->elts[pc].longconst); | |
822 | /* Allocate arg vector, including space for the function to be | |
823 | called in argvec[0], a potential `this', and a terminating | |
824 | NULL. */ | |
825 | value **argvec = (value **) alloca (sizeof (value *) * (nargs + 3)); | |
826 | if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) | |
827 | { | |
828 | /* First, evaluate the structure into arg2. */ | |
829 | pc2 = (*pos)++; | |
830 | ||
831 | if (op == STRUCTOP_MEMBER) | |
832 | { | |
833 | arg2 = evaluate_subexp_for_address (exp, pos, noside); | |
834 | } | |
835 | else | |
836 | { | |
fe1fe7ea | 837 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
e69570ee PA |
838 | } |
839 | ||
840 | /* If the function is a virtual function, then the aggregate | |
841 | value (providing the structure) plays its part by providing | |
842 | the vtable. Otherwise, it is just along for the ride: call | |
843 | the function directly. */ | |
844 | ||
fe1fe7ea | 845 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
e69570ee PA |
846 | |
847 | type *a1_type = check_typedef (value_type (arg1)); | |
848 | if (noside == EVAL_SKIP) | |
849 | tem = 1; /* Set it to the right arg index so that all | |
850 | arguments can also be skipped. */ | |
78134374 | 851 | else if (a1_type->code () == TYPE_CODE_METHODPTR) |
e69570ee PA |
852 | { |
853 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
854 | arg1 = value_zero (TYPE_TARGET_TYPE (a1_type), not_lval); | |
855 | else | |
856 | arg1 = cplus_method_ptr_to_value (&arg2, arg1); | |
857 | ||
858 | /* Now, say which argument to start evaluating from. */ | |
859 | nargs++; | |
860 | tem = 2; | |
861 | argvec[1] = arg2; | |
862 | } | |
78134374 | 863 | else if (a1_type->code () == TYPE_CODE_MEMBERPTR) |
e69570ee PA |
864 | { |
865 | struct type *type_ptr | |
866 | = lookup_pointer_type (TYPE_SELF_TYPE (a1_type)); | |
867 | struct type *target_type_ptr | |
868 | = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type)); | |
869 | ||
870 | /* Now, convert these values to an address. */ | |
871 | arg2 = value_cast (type_ptr, arg2); | |
872 | ||
873 | long mem_offset = value_as_long (arg1); | |
874 | ||
875 | arg1 = value_from_pointer (target_type_ptr, | |
876 | value_as_long (arg2) + mem_offset); | |
877 | arg1 = value_ind (arg1); | |
878 | tem = 1; | |
879 | } | |
880 | else | |
881 | error (_("Non-pointer-to-member value used in pointer-to-member " | |
882 | "construct")); | |
883 | } | |
884 | else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR) | |
885 | { | |
886 | /* Hair for method invocations. */ | |
887 | int tem2; | |
888 | ||
889 | nargs++; | |
890 | /* First, evaluate the structure into arg2. */ | |
891 | pc2 = (*pos)++; | |
892 | tem2 = longest_to_int (exp->elts[pc2 + 1].longconst); | |
893 | *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1); | |
894 | ||
895 | if (op == STRUCTOP_STRUCT) | |
896 | { | |
897 | /* If v is a variable in a register, and the user types | |
898 | v.method (), this will produce an error, because v has no | |
899 | address. | |
900 | ||
901 | A possible way around this would be to allocate a copy of | |
902 | the variable on the stack, copy in the contents, call the | |
903 | function, and copy out the contents. I.e. convert this | |
904 | from call by reference to call by copy-return (or | |
905 | whatever it's called). However, this does not work | |
906 | because it is not the same: the method being called could | |
907 | stash a copy of the address, and then future uses through | |
908 | that address (after the method returns) would be expected | |
909 | to use the variable itself, not some copy of it. */ | |
910 | arg2 = evaluate_subexp_for_address (exp, pos, noside); | |
911 | } | |
912 | else | |
913 | { | |
fe1fe7ea | 914 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
e69570ee PA |
915 | |
916 | /* Check to see if the operator '->' has been overloaded. | |
917 | If the operator has been overloaded replace arg2 with the | |
918 | value returned by the custom operator and continue | |
919 | evaluation. */ | |
920 | while (unop_user_defined_p (op, arg2)) | |
921 | { | |
922 | struct value *value = NULL; | |
a70b8144 | 923 | try |
e69570ee PA |
924 | { |
925 | value = value_x_unop (arg2, op, noside); | |
926 | } | |
927 | ||
230d2906 | 928 | catch (const gdb_exception_error &except) |
e69570ee PA |
929 | { |
930 | if (except.error == NOT_FOUND_ERROR) | |
931 | break; | |
932 | else | |
eedc3f4f | 933 | throw; |
e69570ee | 934 | } |
e69570ee PA |
935 | |
936 | arg2 = value; | |
937 | } | |
938 | } | |
939 | /* Now, say which argument to start evaluating from. */ | |
940 | tem = 2; | |
941 | } | |
942 | else if (op == OP_SCOPE | |
943 | && overload_resolution | |
944 | && (exp->language_defn->la_language == language_cplus)) | |
945 | { | |
946 | /* Unpack it locally so we can properly handle overload | |
947 | resolution. */ | |
948 | char *name; | |
949 | int local_tem; | |
950 | ||
951 | pc2 = (*pos)++; | |
952 | local_tem = longest_to_int (exp->elts[pc2 + 2].longconst); | |
953 | (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1); | |
954 | struct type *type = exp->elts[pc2 + 1].type; | |
955 | name = &exp->elts[pc2 + 3].string; | |
956 | ||
957 | function = NULL; | |
958 | function_name = NULL; | |
78134374 | 959 | if (type->code () == TYPE_CODE_NAMESPACE) |
e69570ee | 960 | { |
7d93a1e0 | 961 | function = cp_lookup_symbol_namespace (type->name (), |
e69570ee PA |
962 | name, |
963 | get_selected_block (0), | |
964 | VAR_DOMAIN).symbol; | |
965 | if (function == NULL) | |
966 | error (_("No symbol \"%s\" in namespace \"%s\"."), | |
7d93a1e0 | 967 | name, type->name ()); |
e69570ee PA |
968 | |
969 | tem = 1; | |
970 | /* arg2 is left as NULL on purpose. */ | |
971 | } | |
972 | else | |
973 | { | |
78134374 SM |
974 | gdb_assert (type->code () == TYPE_CODE_STRUCT |
975 | || type->code () == TYPE_CODE_UNION); | |
e69570ee PA |
976 | function_name = name; |
977 | ||
978 | /* We need a properly typed value for method lookup. For | |
979 | static methods arg2 is otherwise unused. */ | |
980 | arg2 = value_zero (type, lval_memory); | |
981 | ++nargs; | |
982 | tem = 2; | |
983 | } | |
984 | } | |
985 | else if (op == OP_ADL_FUNC) | |
986 | { | |
987 | /* Save the function position and move pos so that the arguments | |
988 | can be evaluated. */ | |
989 | int func_name_len; | |
990 | ||
991 | save_pos1 = *pos; | |
992 | tem = 1; | |
993 | ||
994 | func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst); | |
995 | (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1); | |
996 | } | |
997 | else | |
998 | { | |
999 | /* Non-method function call. */ | |
1000 | save_pos1 = *pos; | |
1001 | tem = 1; | |
1002 | ||
1003 | /* If this is a C++ function wait until overload resolution. */ | |
1004 | if (op == OP_VAR_VALUE | |
1005 | && overload_resolution | |
1006 | && (exp->language_defn->la_language == language_cplus)) | |
1007 | { | |
1008 | (*pos) += 4; /* Skip the evaluation of the symbol. */ | |
1009 | argvec[0] = NULL; | |
1010 | } | |
1011 | else | |
1012 | { | |
1013 | if (op == OP_VAR_MSYM_VALUE) | |
1014 | { | |
3e5ef9a4 | 1015 | minimal_symbol *msym = exp->elts[*pos + 2].msymbol; |
c9d95fa3 | 1016 | var_func_name = msym->print_name (); |
e69570ee PA |
1017 | } |
1018 | else if (op == OP_VAR_VALUE) | |
1019 | { | |
3e5ef9a4 | 1020 | symbol *sym = exp->elts[*pos + 2].symbol; |
987012b8 | 1021 | var_func_name = sym->print_name (); |
e69570ee PA |
1022 | } |
1023 | ||
1024 | argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside); | |
1025 | type *type = value_type (argvec[0]); | |
78134374 | 1026 | if (type && type->code () == TYPE_CODE_PTR) |
e69570ee | 1027 | type = TYPE_TARGET_TYPE (type); |
78134374 | 1028 | if (type && type->code () == TYPE_CODE_FUNC) |
e69570ee | 1029 | { |
1f704f76 | 1030 | for (; tem <= nargs && tem <= type->num_fields (); tem++) |
e69570ee | 1031 | { |
940da03e | 1032 | argvec[tem] = evaluate_subexp (type->field (tem - 1).type (), |
e69570ee PA |
1033 | exp, pos, noside); |
1034 | } | |
1035 | } | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | /* Evaluate arguments (if not already done, e.g., namespace::func() | |
1040 | and overload-resolution is off). */ | |
1041 | for (; tem <= nargs; tem++) | |
1042 | { | |
1043 | /* Ensure that array expressions are coerced into pointer | |
1044 | objects. */ | |
1045 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); | |
1046 | } | |
1047 | ||
1048 | /* Signal end of arglist. */ | |
1049 | argvec[tem] = 0; | |
1050 | ||
1051 | if (noside == EVAL_SKIP) | |
1052 | return eval_skip_value (exp); | |
1053 | ||
1054 | if (op == OP_ADL_FUNC) | |
1055 | { | |
1056 | struct symbol *symp; | |
1057 | char *func_name; | |
1058 | int name_len; | |
1059 | int string_pc = save_pos1 + 3; | |
1060 | ||
1061 | /* Extract the function name. */ | |
1062 | name_len = longest_to_int (exp->elts[string_pc].longconst); | |
1063 | func_name = (char *) alloca (name_len + 1); | |
1064 | strcpy (func_name, &exp->elts[string_pc + 1].string); | |
1065 | ||
6b1747cd PA |
1066 | find_overload_match (gdb::make_array_view (&argvec[1], nargs), |
1067 | func_name, | |
e69570ee PA |
1068 | NON_METHOD, /* not method */ |
1069 | NULL, NULL, /* pass NULL symbol since | |
1070 | symbol is unknown */ | |
1071 | NULL, &symp, NULL, 0, noside); | |
1072 | ||
1073 | /* Now fix the expression being evaluated. */ | |
1074 | exp->elts[save_pos1 + 2].symbol = symp; | |
1075 | argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside); | |
1076 | } | |
1077 | ||
1078 | if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR | |
1079 | || (op == OP_SCOPE && function_name != NULL)) | |
1080 | { | |
1081 | int static_memfuncp; | |
1082 | char *tstr; | |
1083 | ||
1084 | /* Method invocation: stuff "this" as first parameter. If the | |
1085 | method turns out to be static we undo this below. */ | |
1086 | argvec[1] = arg2; | |
1087 | ||
1088 | if (op != OP_SCOPE) | |
1089 | { | |
1090 | /* Name of method from expression. */ | |
1091 | tstr = &exp->elts[pc2 + 2].string; | |
1092 | } | |
1093 | else | |
1094 | tstr = function_name; | |
1095 | ||
1096 | if (overload_resolution && (exp->language_defn->la_language | |
1097 | == language_cplus)) | |
1098 | { | |
1099 | /* Language is C++, do some overload resolution before | |
1100 | evaluation. */ | |
1101 | struct value *valp = NULL; | |
1102 | ||
6b1747cd PA |
1103 | (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs), |
1104 | tstr, | |
e69570ee PA |
1105 | METHOD, /* method */ |
1106 | &arg2, /* the object */ | |
1107 | NULL, &valp, NULL, | |
1108 | &static_memfuncp, 0, noside); | |
1109 | ||
1110 | if (op == OP_SCOPE && !static_memfuncp) | |
1111 | { | |
1112 | /* For the time being, we don't handle this. */ | |
1113 | error (_("Call to overloaded function %s requires " | |
1114 | "`this' pointer"), | |
1115 | function_name); | |
1116 | } | |
1117 | argvec[1] = arg2; /* the ``this'' pointer */ | |
1118 | argvec[0] = valp; /* Use the method found after overload | |
1119 | resolution. */ | |
1120 | } | |
1121 | else | |
1122 | /* Non-C++ case -- or no overload resolution. */ | |
1123 | { | |
1124 | struct value *temp = arg2; | |
1125 | ||
1126 | argvec[0] = value_struct_elt (&temp, argvec + 1, tstr, | |
1127 | &static_memfuncp, | |
1128 | op == STRUCTOP_STRUCT | |
1129 | ? "structure" : "structure pointer"); | |
1130 | /* value_struct_elt updates temp with the correct value of | |
1131 | the ``this'' pointer if necessary, so modify argvec[1] to | |
1132 | reflect any ``this'' changes. */ | |
1133 | arg2 | |
1134 | = value_from_longest (lookup_pointer_type(value_type (temp)), | |
1135 | value_address (temp) | |
1136 | + value_embedded_offset (temp)); | |
1137 | argvec[1] = arg2; /* the ``this'' pointer */ | |
1138 | } | |
1139 | ||
1140 | /* Take out `this' if needed. */ | |
1141 | if (static_memfuncp) | |
1142 | { | |
1143 | argvec[1] = argvec[0]; | |
1144 | nargs--; | |
1145 | argvec++; | |
1146 | } | |
1147 | } | |
1148 | else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR) | |
1149 | { | |
1150 | /* Pointer to member. argvec[1] is already set up. */ | |
1151 | argvec[0] = arg1; | |
1152 | } | |
1153 | else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL)) | |
1154 | { | |
1155 | /* Non-member function being called. */ | |
1156 | /* fn: This can only be done for C++ functions. A C-style | |
1157 | function in a C++ program, for instance, does not have the | |
1158 | fields that are expected here. */ | |
1159 | ||
1160 | if (overload_resolution && (exp->language_defn->la_language | |
1161 | == language_cplus)) | |
1162 | { | |
1163 | /* Language is C++, do some overload resolution before | |
1164 | evaluation. */ | |
1165 | struct symbol *symp; | |
1166 | int no_adl = 0; | |
1167 | ||
1168 | /* If a scope has been specified disable ADL. */ | |
1169 | if (op == OP_SCOPE) | |
1170 | no_adl = 1; | |
1171 | ||
1172 | if (op == OP_VAR_VALUE) | |
1173 | function = exp->elts[save_pos1+2].symbol; | |
1174 | ||
6b1747cd | 1175 | (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs), |
e69570ee PA |
1176 | NULL, /* no need for name */ |
1177 | NON_METHOD, /* not method */ | |
1178 | NULL, function, /* the function */ | |
1179 | NULL, &symp, NULL, no_adl, noside); | |
1180 | ||
1181 | if (op == OP_VAR_VALUE) | |
1182 | { | |
1183 | /* Now fix the expression being evaluated. */ | |
1184 | exp->elts[save_pos1+2].symbol = symp; | |
1185 | argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, | |
1186 | noside); | |
1187 | } | |
1188 | else | |
1189 | argvec[0] = value_of_variable (symp, get_selected_block (0)); | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | /* Not C++, or no overload resolution allowed. */ | |
1194 | /* Nothing to be done; argvec already correctly set up. */ | |
1195 | } | |
1196 | } | |
1197 | else | |
1198 | { | |
1199 | /* It is probably a C-style function. */ | |
1200 | /* Nothing to be done; argvec already correctly set up. */ | |
1201 | } | |
1202 | ||
1ab8280d TT |
1203 | return evaluate_subexp_do_call (exp, noside, argvec[0], |
1204 | gdb::make_array_view (argvec + 1, nargs), | |
6d816919 | 1205 | var_func_name, expect_type); |
23be8da7 RB |
1206 | } |
1207 | ||
60e22c1e HD |
1208 | /* Return true if type is integral or reference to integral */ |
1209 | ||
1210 | static bool | |
1211 | is_integral_or_integral_reference (struct type *type) | |
1212 | { | |
1213 | if (is_integral_type (type)) | |
1214 | return true; | |
1215 | ||
1216 | type = check_typedef (type); | |
1217 | return (type != nullptr | |
1218 | && TYPE_IS_REFERENCE (type) | |
1219 | && is_integral_type (TYPE_TARGET_TYPE (type))); | |
1220 | } | |
1221 | ||
ea2d29f7 TT |
1222 | /* Helper function that implements the body of OP_SCOPE. */ |
1223 | ||
d5ab122c | 1224 | struct value * |
ea2d29f7 TT |
1225 | eval_op_scope (struct type *expect_type, struct expression *exp, |
1226 | enum noside noside, | |
1227 | struct type *type, const char *string) | |
1228 | { | |
1229 | if (noside == EVAL_SKIP) | |
1230 | return eval_skip_value (exp); | |
1231 | struct value *arg1 = value_aggregate_elt (type, string, expect_type, | |
1232 | 0, noside); | |
1233 | if (arg1 == NULL) | |
1234 | error (_("There is no field named %s"), string); | |
1235 | return arg1; | |
1236 | } | |
1237 | ||
50b98adc TT |
1238 | /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */ |
1239 | ||
b5cc3923 | 1240 | struct value * |
50b98adc TT |
1241 | eval_op_var_entry_value (struct type *expect_type, struct expression *exp, |
1242 | enum noside noside, symbol *sym) | |
1243 | { | |
1244 | if (noside == EVAL_SKIP) | |
1245 | return eval_skip_value (exp); | |
1246 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1247 | return value_zero (SYMBOL_TYPE (sym), not_lval); | |
1248 | ||
1249 | if (SYMBOL_COMPUTED_OPS (sym) == NULL | |
1250 | || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL) | |
1251 | error (_("Symbol \"%s\" does not have any specific entry value"), | |
1252 | sym->print_name ()); | |
1253 | ||
1254 | struct frame_info *frame = get_selected_frame (NULL); | |
1255 | return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame); | |
1256 | } | |
1257 | ||
c0df9289 TT |
1258 | /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */ |
1259 | ||
0c8effa3 | 1260 | struct value * |
c0df9289 TT |
1261 | eval_op_var_msym_value (struct type *expect_type, struct expression *exp, |
1262 | enum noside noside, bool outermost_p, | |
1263 | minimal_symbol *msymbol, struct objfile *objfile) | |
1264 | { | |
1265 | value *val = evaluate_var_msym_value (noside, objfile, msymbol); | |
1266 | ||
1267 | struct type *type = value_type (val); | |
1268 | if (type->code () == TYPE_CODE_ERROR | |
1269 | && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p)) | |
1270 | error_unknown_type (msymbol->print_name ()); | |
1271 | return val; | |
1272 | } | |
1273 | ||
9b1d8af6 TT |
1274 | /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */ |
1275 | ||
17679395 | 1276 | struct value * |
9b1d8af6 TT |
1277 | eval_op_func_static_var (struct type *expect_type, struct expression *exp, |
1278 | enum noside noside, | |
1279 | value *func, const char *var) | |
1280 | { | |
1281 | if (noside == EVAL_SKIP) | |
1282 | return eval_skip_value (exp); | |
1283 | CORE_ADDR addr = value_address (func); | |
1284 | const block *blk = block_for_pc (addr); | |
1285 | struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL); | |
1286 | if (sym.symbol == NULL) | |
1287 | error (_("No symbol \"%s\" in specified context."), var); | |
1288 | return evaluate_var_value (noside, sym.block, sym.symbol); | |
1289 | } | |
1290 | ||
ffff730b TT |
1291 | /* Helper function that implements the body of OP_REGISTER. */ |
1292 | ||
55bdbff8 | 1293 | struct value * |
ffff730b TT |
1294 | eval_op_register (struct type *expect_type, struct expression *exp, |
1295 | enum noside noside, const char *name) | |
1296 | { | |
1297 | int regno; | |
1298 | struct value *val; | |
1299 | ||
1300 | regno = user_reg_map_name_to_regnum (exp->gdbarch, | |
1301 | name, strlen (name)); | |
1302 | if (regno == -1) | |
1303 | error (_("Register $%s not available."), name); | |
1304 | ||
1305 | /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return | |
1306 | a value with the appropriate register type. Unfortunately, | |
1307 | we don't have easy access to the type of user registers. | |
1308 | So for these registers, we fetch the register value regardless | |
1309 | of the evaluation mode. */ | |
1310 | if (noside == EVAL_AVOID_SIDE_EFFECTS | |
1311 | && regno < gdbarch_num_cooked_regs (exp->gdbarch)) | |
1312 | val = value_zero (register_type (exp->gdbarch, regno), not_lval); | |
1313 | else | |
1314 | val = value_of_register (regno, get_selected_frame (NULL)); | |
1315 | if (val == NULL) | |
1316 | error (_("Value of register %s not available."), name); | |
1317 | else | |
1318 | return val; | |
1319 | } | |
1320 | ||
14a1c64a TT |
1321 | /* Helper function that implements the body of OP_STRING. */ |
1322 | ||
b50db09f | 1323 | struct value * |
14a1c64a TT |
1324 | eval_op_string (struct type *expect_type, struct expression *exp, |
1325 | enum noside noside, int len, const char *string) | |
1326 | { | |
1327 | if (noside == EVAL_SKIP) | |
1328 | return eval_skip_value (exp); | |
1329 | struct type *type = language_string_char_type (exp->language_defn, | |
1330 | exp->gdbarch); | |
1331 | return value_string (string, len, type); | |
1332 | } | |
1333 | ||
f871bae1 TT |
1334 | /* Helper function that implements the body of OP_OBJC_SELECTOR. */ |
1335 | ||
09db3700 | 1336 | struct value * |
f871bae1 TT |
1337 | eval_op_objc_selector (struct type *expect_type, struct expression *exp, |
1338 | enum noside noside, | |
1339 | const char *sel) | |
1340 | { | |
1341 | if (noside == EVAL_SKIP) | |
1342 | return eval_skip_value (exp); | |
1343 | ||
1344 | struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; | |
1345 | return value_from_longest (selector_type, | |
1346 | lookup_child_selector (exp->gdbarch, sel)); | |
1347 | } | |
1348 | ||
5c2f201e TT |
1349 | /* Helper function that implements the body of BINOP_CONCAT. */ |
1350 | ||
e51e26a0 | 1351 | struct value * |
5c2f201e | 1352 | eval_op_concat (struct type *expect_type, struct expression *exp, |
e51e26a0 | 1353 | enum noside noside, struct value *arg1, struct value *arg2) |
5c2f201e TT |
1354 | { |
1355 | if (noside == EVAL_SKIP) | |
1356 | return eval_skip_value (exp); | |
e51e26a0 TT |
1357 | if (binop_user_defined_p (BINOP_CONCAT, arg1, arg2)) |
1358 | return value_x_binop (arg1, arg2, BINOP_CONCAT, OP_NULL, noside); | |
5c2f201e TT |
1359 | else |
1360 | return value_concat (arg1, arg2); | |
1361 | } | |
1362 | ||
f960a617 TT |
1363 | /* A helper function for TERNOP_SLICE. */ |
1364 | ||
1594e0bb | 1365 | struct value * |
f960a617 TT |
1366 | eval_op_ternop (struct type *expect_type, struct expression *exp, |
1367 | enum noside noside, | |
1368 | struct value *array, struct value *low, struct value *upper) | |
1369 | { | |
1370 | if (noside == EVAL_SKIP) | |
1371 | return eval_skip_value (exp); | |
1372 | int lowbound = value_as_long (low); | |
1373 | int upperbound = value_as_long (upper); | |
1374 | return value_slice (array, lowbound, upperbound - lowbound + 1); | |
1375 | } | |
1376 | ||
3e96c4fc TT |
1377 | /* A helper function for STRUCTOP_STRUCT. */ |
1378 | ||
808b22cf | 1379 | struct value * |
3e96c4fc TT |
1380 | eval_op_structop_struct (struct type *expect_type, struct expression *exp, |
1381 | enum noside noside, | |
1382 | struct value *arg1, const char *string) | |
1383 | { | |
1384 | if (noside == EVAL_SKIP) | |
1385 | return eval_skip_value (exp); | |
1386 | struct value *arg3 = value_struct_elt (&arg1, NULL, string, | |
1387 | NULL, "structure"); | |
1388 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1389 | arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3)); | |
1390 | return arg3; | |
1391 | } | |
1392 | ||
fb461aa3 TT |
1393 | /* A helper function for STRUCTOP_PTR. */ |
1394 | ||
ab0609be | 1395 | struct value * |
fb461aa3 | 1396 | eval_op_structop_ptr (struct type *expect_type, struct expression *exp, |
ab0609be | 1397 | enum noside noside, |
fb461aa3 TT |
1398 | struct value *arg1, const char *string) |
1399 | { | |
1400 | if (noside == EVAL_SKIP) | |
1401 | return eval_skip_value (exp); | |
1402 | ||
1403 | /* Check to see if operator '->' has been overloaded. If so replace | |
1404 | arg1 with the value returned by evaluating operator->(). */ | |
ab0609be | 1405 | while (unop_user_defined_p (STRUCTOP_PTR, arg1)) |
fb461aa3 TT |
1406 | { |
1407 | struct value *value = NULL; | |
1408 | try | |
1409 | { | |
ab0609be | 1410 | value = value_x_unop (arg1, STRUCTOP_PTR, noside); |
fb461aa3 TT |
1411 | } |
1412 | ||
1413 | catch (const gdb_exception_error &except) | |
1414 | { | |
1415 | if (except.error == NOT_FOUND_ERROR) | |
1416 | break; | |
1417 | else | |
1418 | throw; | |
1419 | } | |
1420 | ||
1421 | arg1 = value; | |
1422 | } | |
1423 | ||
1424 | /* JYG: if print object is on we need to replace the base type | |
1425 | with rtti type in order to continue on with successful | |
1426 | lookup of member / method only available in the rtti type. */ | |
1427 | { | |
1428 | struct type *arg_type = value_type (arg1); | |
1429 | struct type *real_type; | |
1430 | int full, using_enc; | |
1431 | LONGEST top; | |
1432 | struct value_print_options opts; | |
1433 | ||
1434 | get_user_print_options (&opts); | |
1435 | if (opts.objectprint && TYPE_TARGET_TYPE (arg_type) | |
1436 | && (TYPE_TARGET_TYPE (arg_type)->code () == TYPE_CODE_STRUCT)) | |
1437 | { | |
1438 | real_type = value_rtti_indirect_type (arg1, &full, &top, | |
1439 | &using_enc); | |
1440 | if (real_type) | |
1441 | arg1 = value_cast (real_type, arg1); | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | struct value *arg3 = value_struct_elt (&arg1, NULL, string, | |
1446 | NULL, "structure pointer"); | |
1447 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1448 | arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3)); | |
1449 | return arg3; | |
1450 | } | |
1451 | ||
b7a96ed2 TT |
1452 | /* A helper function for STRUCTOP_MEMBER. */ |
1453 | ||
07f724a8 | 1454 | struct value * |
b7a96ed2 TT |
1455 | eval_op_member (struct type *expect_type, struct expression *exp, |
1456 | enum noside noside, | |
1457 | struct value *arg1, struct value *arg2) | |
1458 | { | |
1459 | long mem_offset; | |
1460 | ||
1461 | if (noside == EVAL_SKIP) | |
1462 | return eval_skip_value (exp); | |
1463 | ||
1464 | struct value *arg3; | |
1465 | struct type *type = check_typedef (value_type (arg2)); | |
1466 | switch (type->code ()) | |
1467 | { | |
1468 | case TYPE_CODE_METHODPTR: | |
1469 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1470 | return value_zero (TYPE_TARGET_TYPE (type), not_lval); | |
1471 | else | |
1472 | { | |
1473 | arg2 = cplus_method_ptr_to_value (&arg1, arg2); | |
1474 | gdb_assert (value_type (arg2)->code () == TYPE_CODE_PTR); | |
1475 | return value_ind (arg2); | |
1476 | } | |
1477 | ||
1478 | case TYPE_CODE_MEMBERPTR: | |
1479 | /* Now, convert these values to an address. */ | |
1480 | arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)), | |
1481 | arg1, 1); | |
1482 | ||
1483 | mem_offset = value_as_long (arg2); | |
1484 | ||
1485 | arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)), | |
1486 | value_as_long (arg1) + mem_offset); | |
1487 | return value_ind (arg3); | |
1488 | ||
1489 | default: | |
1490 | error (_("non-pointer-to-member value used " | |
1491 | "in pointer-to-member construct")); | |
1492 | } | |
1493 | } | |
1494 | ||
aedaf9ac TT |
1495 | /* A helper function for BINOP_ADD. */ |
1496 | ||
a94323b6 | 1497 | struct value * |
aedaf9ac | 1498 | eval_op_add (struct type *expect_type, struct expression *exp, |
a94323b6 | 1499 | enum noside noside, |
aedaf9ac TT |
1500 | struct value *arg1, struct value *arg2) |
1501 | { | |
1502 | if (noside == EVAL_SKIP) | |
1503 | return eval_skip_value (exp); | |
a94323b6 TT |
1504 | if (binop_user_defined_p (BINOP_ADD, arg1, arg2)) |
1505 | return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside); | |
aedaf9ac TT |
1506 | else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) |
1507 | && is_integral_or_integral_reference (value_type (arg2))) | |
1508 | return value_ptradd (arg1, value_as_long (arg2)); | |
1509 | else if (ptrmath_type_p (exp->language_defn, value_type (arg2)) | |
1510 | && is_integral_or_integral_reference (value_type (arg1))) | |
1511 | return value_ptradd (arg2, value_as_long (arg1)); | |
1512 | else | |
1513 | { | |
1514 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1515 | return value_binop (arg1, arg2, BINOP_ADD); | |
1516 | } | |
1517 | } | |
1518 | ||
d9790e22 TT |
1519 | /* A helper function for BINOP_SUB. */ |
1520 | ||
5133d78b | 1521 | struct value * |
d9790e22 | 1522 | eval_op_sub (struct type *expect_type, struct expression *exp, |
5133d78b | 1523 | enum noside noside, |
d9790e22 TT |
1524 | struct value *arg1, struct value *arg2) |
1525 | { | |
1526 | if (noside == EVAL_SKIP) | |
1527 | return eval_skip_value (exp); | |
5133d78b TT |
1528 | if (binop_user_defined_p (BINOP_SUB, arg1, arg2)) |
1529 | return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside); | |
d9790e22 TT |
1530 | else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) |
1531 | && ptrmath_type_p (exp->language_defn, value_type (arg2))) | |
1532 | { | |
1533 | /* FIXME -- should be ptrdiff_t */ | |
1534 | struct type *type = builtin_type (exp->gdbarch)->builtin_long; | |
1535 | return value_from_longest (type, value_ptrdiff (arg1, arg2)); | |
1536 | } | |
1537 | else if (ptrmath_type_p (exp->language_defn, value_type (arg1)) | |
1538 | && is_integral_or_integral_reference (value_type (arg2))) | |
1539 | return value_ptradd (arg1, - value_as_long (arg2)); | |
1540 | else | |
1541 | { | |
1542 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1543 | return value_binop (arg1, arg2, BINOP_SUB); | |
1544 | } | |
1545 | } | |
1546 | ||
7cdcdd02 TT |
1547 | /* Helper function for several different binary operations. */ |
1548 | ||
373907ff | 1549 | struct value * |
7cdcdd02 TT |
1550 | eval_op_binary (struct type *expect_type, struct expression *exp, |
1551 | enum noside noside, enum exp_opcode op, | |
1552 | struct value *arg1, struct value *arg2) | |
1553 | { | |
1554 | if (noside == EVAL_SKIP) | |
1555 | return eval_skip_value (exp); | |
1556 | if (binop_user_defined_p (op, arg1, arg2)) | |
1557 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1558 | else | |
1559 | { | |
1560 | /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero, | |
1561 | fudge arg2 to avoid division-by-zero, the caller is | |
1562 | (theoretically) only looking for the type of the result. */ | |
1563 | if (noside == EVAL_AVOID_SIDE_EFFECTS | |
1564 | /* ??? Do we really want to test for BINOP_MOD here? | |
1565 | The implementation of value_binop gives it a well-defined | |
1566 | value. */ | |
1567 | && (op == BINOP_DIV | |
1568 | || op == BINOP_INTDIV | |
1569 | || op == BINOP_REM | |
1570 | || op == BINOP_MOD) | |
1571 | && value_logical_not (arg2)) | |
1572 | { | |
1573 | struct value *v_one; | |
1574 | ||
1575 | v_one = value_one (value_type (arg2)); | |
1576 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one); | |
1577 | return value_binop (arg1, v_one, op); | |
1578 | } | |
1579 | else | |
1580 | { | |
1581 | /* For shift and integer exponentiation operations, | |
1582 | only promote the first argument. */ | |
1583 | if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP) | |
1584 | && is_integral_type (value_type (arg2))) | |
1585 | unop_promote (exp->language_defn, exp->gdbarch, &arg1); | |
1586 | else | |
1587 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1588 | ||
1589 | return value_binop (arg1, arg2, op); | |
1590 | } | |
1591 | } | |
1592 | } | |
1593 | ||
288d26bc TT |
1594 | /* A helper function for BINOP_SUBSCRIPT. */ |
1595 | ||
224d6424 | 1596 | struct value * |
288d26bc TT |
1597 | eval_op_subscript (struct type *expect_type, struct expression *exp, |
1598 | enum noside noside, enum exp_opcode op, | |
1599 | struct value *arg1, struct value *arg2) | |
1600 | { | |
1601 | if (noside == EVAL_SKIP) | |
1602 | return eval_skip_value (exp); | |
1603 | if (binop_user_defined_p (op, arg1, arg2)) | |
1604 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1605 | else | |
1606 | { | |
1607 | /* If the user attempts to subscript something that is not an | |
1608 | array or pointer type (like a plain int variable for example), | |
1609 | then report this as an error. */ | |
1610 | ||
1611 | arg1 = coerce_ref (arg1); | |
1612 | struct type *type = check_typedef (value_type (arg1)); | |
1613 | if (type->code () != TYPE_CODE_ARRAY | |
1614 | && type->code () != TYPE_CODE_PTR) | |
1615 | { | |
1616 | if (type->name ()) | |
1617 | error (_("cannot subscript something of type `%s'"), | |
1618 | type->name ()); | |
1619 | else | |
1620 | error (_("cannot subscript requested type")); | |
1621 | } | |
1622 | ||
1623 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1624 | return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1)); | |
1625 | else | |
1626 | return value_subscript (arg1, value_as_long (arg2)); | |
1627 | } | |
1628 | } | |
1629 | ||
0cc96de8 TT |
1630 | /* A helper function for BINOP_EQUAL. */ |
1631 | ||
46916f2b | 1632 | struct value * |
0cc96de8 TT |
1633 | eval_op_equal (struct type *expect_type, struct expression *exp, |
1634 | enum noside noside, enum exp_opcode op, | |
1635 | struct value *arg1, struct value *arg2) | |
1636 | { | |
1637 | if (noside == EVAL_SKIP) | |
1638 | return eval_skip_value (exp); | |
1639 | if (binop_user_defined_p (op, arg1, arg2)) | |
1640 | { | |
1641 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1642 | } | |
1643 | else | |
1644 | { | |
1645 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1646 | int tem = value_equal (arg1, arg2); | |
1647 | struct type *type = language_bool_type (exp->language_defn, | |
1648 | exp->gdbarch); | |
1649 | return value_from_longest (type, (LONGEST) tem); | |
1650 | } | |
1651 | } | |
1652 | ||
1fcb3559 TT |
1653 | /* A helper function for BINOP_NOTEQUAL. */ |
1654 | ||
46916f2b | 1655 | struct value * |
1fcb3559 TT |
1656 | eval_op_notequal (struct type *expect_type, struct expression *exp, |
1657 | enum noside noside, enum exp_opcode op, | |
1658 | struct value *arg1, struct value *arg2) | |
1659 | { | |
1660 | if (noside == EVAL_SKIP) | |
1661 | return eval_skip_value (exp); | |
1662 | if (binop_user_defined_p (op, arg1, arg2)) | |
1663 | { | |
1664 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1665 | } | |
1666 | else | |
1667 | { | |
1668 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1669 | int tem = value_equal (arg1, arg2); | |
1670 | struct type *type = language_bool_type (exp->language_defn, | |
1671 | exp->gdbarch); | |
1672 | return value_from_longest (type, (LONGEST) ! tem); | |
1673 | } | |
1674 | } | |
1675 | ||
6cad1349 TT |
1676 | /* A helper function for BINOP_LESS. */ |
1677 | ||
46916f2b | 1678 | struct value * |
6cad1349 TT |
1679 | eval_op_less (struct type *expect_type, struct expression *exp, |
1680 | enum noside noside, enum exp_opcode op, | |
1681 | struct value *arg1, struct value *arg2) | |
1682 | { | |
1683 | if (noside == EVAL_SKIP) | |
1684 | return eval_skip_value (exp); | |
1685 | if (binop_user_defined_p (op, arg1, arg2)) | |
1686 | { | |
1687 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1688 | } | |
1689 | else | |
1690 | { | |
1691 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1692 | int tem = value_less (arg1, arg2); | |
1693 | struct type *type = language_bool_type (exp->language_defn, | |
1694 | exp->gdbarch); | |
1695 | return value_from_longest (type, (LONGEST) tem); | |
1696 | } | |
1697 | } | |
1698 | ||
1f78d732 TT |
1699 | /* A helper function for BINOP_GTR. */ |
1700 | ||
46916f2b | 1701 | struct value * |
1f78d732 TT |
1702 | eval_op_gtr (struct type *expect_type, struct expression *exp, |
1703 | enum noside noside, enum exp_opcode op, | |
1704 | struct value *arg1, struct value *arg2) | |
1705 | { | |
1706 | if (noside == EVAL_SKIP) | |
1707 | return eval_skip_value (exp); | |
1708 | if (binop_user_defined_p (op, arg1, arg2)) | |
1709 | { | |
1710 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1711 | } | |
1712 | else | |
1713 | { | |
1714 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1715 | int tem = value_less (arg2, arg1); | |
1716 | struct type *type = language_bool_type (exp->language_defn, | |
1717 | exp->gdbarch); | |
1718 | return value_from_longest (type, (LONGEST) tem); | |
1719 | } | |
1720 | } | |
1721 | ||
96e3efd9 TT |
1722 | /* A helper function for BINOP_GEQ. */ |
1723 | ||
46916f2b | 1724 | struct value * |
96e3efd9 TT |
1725 | eval_op_geq (struct type *expect_type, struct expression *exp, |
1726 | enum noside noside, enum exp_opcode op, | |
1727 | struct value *arg1, struct value *arg2) | |
1728 | { | |
1729 | if (noside == EVAL_SKIP) | |
1730 | return eval_skip_value (exp); | |
1731 | if (binop_user_defined_p (op, arg1, arg2)) | |
1732 | { | |
1733 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1734 | } | |
1735 | else | |
1736 | { | |
1737 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1738 | int tem = value_less (arg2, arg1) || value_equal (arg1, arg2); | |
1739 | struct type *type = language_bool_type (exp->language_defn, | |
1740 | exp->gdbarch); | |
1741 | return value_from_longest (type, (LONGEST) tem); | |
1742 | } | |
1743 | } | |
1744 | ||
60cdd487 TT |
1745 | /* A helper function for BINOP_LEQ. */ |
1746 | ||
46916f2b | 1747 | struct value * |
60cdd487 TT |
1748 | eval_op_leq (struct type *expect_type, struct expression *exp, |
1749 | enum noside noside, enum exp_opcode op, | |
1750 | struct value *arg1, struct value *arg2) | |
1751 | { | |
1752 | if (noside == EVAL_SKIP) | |
1753 | return eval_skip_value (exp); | |
1754 | if (binop_user_defined_p (op, arg1, arg2)) | |
1755 | { | |
1756 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
1757 | } | |
1758 | else | |
1759 | { | |
1760 | binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); | |
1761 | int tem = value_less (arg1, arg2) || value_equal (arg1, arg2); | |
1762 | struct type *type = language_bool_type (exp->language_defn, | |
1763 | exp->gdbarch); | |
1764 | return value_from_longest (type, (LONGEST) tem); | |
1765 | } | |
1766 | } | |
1767 | ||
eed70b1c TT |
1768 | /* A helper function for BINOP_REPEAT. */ |
1769 | ||
d4eff4c1 | 1770 | struct value * |
eed70b1c | 1771 | eval_op_repeat (struct type *expect_type, struct expression *exp, |
d4eff4c1 | 1772 | enum noside noside, enum exp_opcode op, |
eed70b1c TT |
1773 | struct value *arg1, struct value *arg2) |
1774 | { | |
1775 | if (noside == EVAL_SKIP) | |
1776 | return eval_skip_value (exp); | |
1777 | struct type *type = check_typedef (value_type (arg2)); | |
1778 | if (type->code () != TYPE_CODE_INT | |
1779 | && type->code () != TYPE_CODE_ENUM) | |
1780 | error (_("Non-integral right operand for \"@\" operator.")); | |
1781 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1782 | { | |
1783 | return allocate_repeat_value (value_type (arg1), | |
1784 | longest_to_int (value_as_long (arg2))); | |
1785 | } | |
1786 | else | |
1787 | return value_repeat (arg1, longest_to_int (value_as_long (arg2))); | |
1788 | } | |
1789 | ||
39f288be TT |
1790 | /* A helper function for UNOP_PLUS. */ |
1791 | ||
9307d17b | 1792 | struct value * |
39f288be TT |
1793 | eval_op_plus (struct type *expect_type, struct expression *exp, |
1794 | enum noside noside, enum exp_opcode op, | |
1795 | struct value *arg1) | |
1796 | { | |
1797 | if (noside == EVAL_SKIP) | |
1798 | return eval_skip_value (exp); | |
1799 | if (unop_user_defined_p (op, arg1)) | |
1800 | return value_x_unop (arg1, op, noside); | |
1801 | else | |
1802 | { | |
1803 | unop_promote (exp->language_defn, exp->gdbarch, &arg1); | |
1804 | return value_pos (arg1); | |
1805 | } | |
1806 | } | |
1807 | ||
606d105f TT |
1808 | /* A helper function for UNOP_NEG. */ |
1809 | ||
9307d17b | 1810 | struct value * |
606d105f TT |
1811 | eval_op_neg (struct type *expect_type, struct expression *exp, |
1812 | enum noside noside, enum exp_opcode op, | |
1813 | struct value *arg1) | |
1814 | { | |
1815 | if (noside == EVAL_SKIP) | |
1816 | return eval_skip_value (exp); | |
1817 | if (unop_user_defined_p (op, arg1)) | |
1818 | return value_x_unop (arg1, op, noside); | |
1819 | else | |
1820 | { | |
1821 | unop_promote (exp->language_defn, exp->gdbarch, &arg1); | |
1822 | return value_neg (arg1); | |
1823 | } | |
1824 | } | |
1825 | ||
1f09ec81 TT |
1826 | /* A helper function for UNOP_COMPLEMENT. */ |
1827 | ||
9307d17b | 1828 | struct value * |
1f09ec81 TT |
1829 | eval_op_complement (struct type *expect_type, struct expression *exp, |
1830 | enum noside noside, enum exp_opcode op, | |
1831 | struct value *arg1) | |
1832 | { | |
1833 | if (noside == EVAL_SKIP) | |
1834 | return eval_skip_value (exp); | |
1835 | if (unop_user_defined_p (UNOP_COMPLEMENT, arg1)) | |
1836 | return value_x_unop (arg1, UNOP_COMPLEMENT, noside); | |
1837 | else | |
1838 | { | |
1839 | unop_promote (exp->language_defn, exp->gdbarch, &arg1); | |
1840 | return value_complement (arg1); | |
1841 | } | |
1842 | } | |
1843 | ||
24338fb9 TT |
1844 | /* A helper function for UNOP_LOGICAL_NOT. */ |
1845 | ||
9307d17b | 1846 | struct value * |
24338fb9 TT |
1847 | eval_op_lognot (struct type *expect_type, struct expression *exp, |
1848 | enum noside noside, enum exp_opcode op, | |
1849 | struct value *arg1) | |
1850 | { | |
1851 | if (noside == EVAL_SKIP) | |
1852 | return eval_skip_value (exp); | |
1853 | if (unop_user_defined_p (op, arg1)) | |
1854 | return value_x_unop (arg1, op, noside); | |
1855 | else | |
1856 | { | |
1857 | struct type *type = language_bool_type (exp->language_defn, | |
1858 | exp->gdbarch); | |
1859 | return value_from_longest (type, (LONGEST) value_logical_not (arg1)); | |
1860 | } | |
1861 | } | |
1862 | ||
786f70ee TT |
1863 | /* A helper function for UNOP_IND. */ |
1864 | ||
876469ff | 1865 | struct value * |
786f70ee | 1866 | eval_op_ind (struct type *expect_type, struct expression *exp, |
876469ff | 1867 | enum noside noside, |
786f70ee TT |
1868 | struct value *arg1) |
1869 | { | |
1870 | struct type *type = check_typedef (value_type (arg1)); | |
1871 | if (type->code () == TYPE_CODE_METHODPTR | |
1872 | || type->code () == TYPE_CODE_MEMBERPTR) | |
1873 | error (_("Attempt to dereference pointer " | |
1874 | "to member without an object")); | |
1875 | if (noside == EVAL_SKIP) | |
1876 | return eval_skip_value (exp); | |
876469ff TT |
1877 | if (unop_user_defined_p (UNOP_IND, arg1)) |
1878 | return value_x_unop (arg1, UNOP_IND, noside); | |
786f70ee TT |
1879 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) |
1880 | { | |
1881 | type = check_typedef (value_type (arg1)); | |
1882 | ||
1883 | /* If the type pointed to is dynamic then in order to resolve the | |
1884 | dynamic properties we must actually dereference the pointer. | |
1885 | There is a risk that this dereference will have side-effects | |
1886 | in the inferior, but being able to print accurate type | |
1887 | information seems worth the risk. */ | |
1888 | if ((type->code () != TYPE_CODE_PTR | |
1889 | && !TYPE_IS_REFERENCE (type)) | |
1890 | || !is_dynamic_type (TYPE_TARGET_TYPE (type))) | |
1891 | { | |
1892 | if (type->code () == TYPE_CODE_PTR | |
1893 | || TYPE_IS_REFERENCE (type) | |
1894 | /* In C you can dereference an array to get the 1st elt. */ | |
1895 | || type->code () == TYPE_CODE_ARRAY) | |
1896 | return value_zero (TYPE_TARGET_TYPE (type), | |
1897 | lval_memory); | |
1898 | else if (type->code () == TYPE_CODE_INT) | |
1899 | /* GDB allows dereferencing an int. */ | |
1900 | return value_zero (builtin_type (exp->gdbarch)->builtin_int, | |
1901 | lval_memory); | |
1902 | else | |
1903 | error (_("Attempt to take contents of a non-pointer value.")); | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | /* Allow * on an integer so we can cast it to whatever we want. | |
1908 | This returns an int, which seems like the most C-like thing to | |
1909 | do. "long long" variables are rare enough that | |
1910 | BUILTIN_TYPE_LONGEST would seem to be a mistake. */ | |
1911 | if (type->code () == TYPE_CODE_INT) | |
1912 | return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int, | |
1913 | (CORE_ADDR) value_as_address (arg1)); | |
1914 | return value_ind (arg1); | |
1915 | } | |
1916 | ||
acee9468 TT |
1917 | /* A helper function for UNOP_ALIGNOF. */ |
1918 | ||
ae4bb61e | 1919 | struct value * |
acee9468 TT |
1920 | eval_op_alignof (struct type *expect_type, struct expression *exp, |
1921 | enum noside noside, | |
1922 | struct value *arg1) | |
1923 | { | |
1924 | struct type *type = value_type (arg1); | |
1925 | /* FIXME: This should be size_t. */ | |
1926 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
1927 | ULONGEST align = type_align (type); | |
1928 | if (align == 0) | |
1929 | error (_("could not determine alignment of type")); | |
1930 | return value_from_longest (size_type, align); | |
1931 | } | |
1932 | ||
3aef2a07 TT |
1933 | /* A helper function for UNOP_MEMVAL. */ |
1934 | ||
cbc18219 | 1935 | struct value * |
3aef2a07 TT |
1936 | eval_op_memval (struct type *expect_type, struct expression *exp, |
1937 | enum noside noside, | |
1938 | struct value *arg1, struct type *type) | |
1939 | { | |
1940 | if (noside == EVAL_SKIP) | |
1941 | return eval_skip_value (exp); | |
1942 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
1943 | return value_zero (type, lval_memory); | |
1944 | else | |
1945 | return value_at_lazy (type, value_as_address (arg1)); | |
1946 | } | |
1947 | ||
00f50884 TT |
1948 | /* A helper function for UNOP_PREINCREMENT. */ |
1949 | ||
6d89e296 | 1950 | struct value * |
00f50884 TT |
1951 | eval_op_preinc (struct type *expect_type, struct expression *exp, |
1952 | enum noside noside, enum exp_opcode op, | |
1953 | struct value *arg1) | |
1954 | { | |
1955 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
1956 | return arg1; | |
1957 | else if (unop_user_defined_p (op, arg1)) | |
1958 | { | |
1959 | return value_x_unop (arg1, op, noside); | |
1960 | } | |
1961 | else | |
1962 | { | |
1963 | struct value *arg2; | |
1964 | if (ptrmath_type_p (exp->language_defn, value_type (arg1))) | |
1965 | arg2 = value_ptradd (arg1, 1); | |
1966 | else | |
1967 | { | |
1968 | struct value *tmp = arg1; | |
1969 | ||
1970 | arg2 = value_one (value_type (arg1)); | |
1971 | binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); | |
1972 | arg2 = value_binop (tmp, arg2, BINOP_ADD); | |
1973 | } | |
1974 | ||
1975 | return value_assign (arg1, arg2); | |
1976 | } | |
1977 | } | |
1978 | ||
9e1361b7 TT |
1979 | /* A helper function for UNOP_PREDECREMENT. */ |
1980 | ||
6d89e296 | 1981 | struct value * |
9e1361b7 TT |
1982 | eval_op_predec (struct type *expect_type, struct expression *exp, |
1983 | enum noside noside, enum exp_opcode op, | |
1984 | struct value *arg1) | |
1985 | { | |
1986 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
1987 | return arg1; | |
1988 | else if (unop_user_defined_p (op, arg1)) | |
1989 | { | |
1990 | return value_x_unop (arg1, op, noside); | |
1991 | } | |
1992 | else | |
1993 | { | |
1994 | struct value *arg2; | |
1995 | if (ptrmath_type_p (exp->language_defn, value_type (arg1))) | |
1996 | arg2 = value_ptradd (arg1, -1); | |
1997 | else | |
1998 | { | |
1999 | struct value *tmp = arg1; | |
2000 | ||
2001 | arg2 = value_one (value_type (arg1)); | |
2002 | binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); | |
2003 | arg2 = value_binop (tmp, arg2, BINOP_SUB); | |
2004 | } | |
2005 | ||
2006 | return value_assign (arg1, arg2); | |
2007 | } | |
2008 | } | |
2009 | ||
abffe116 TT |
2010 | /* A helper function for UNOP_POSTINCREMENT. */ |
2011 | ||
6d89e296 | 2012 | struct value * |
abffe116 TT |
2013 | eval_op_postinc (struct type *expect_type, struct expression *exp, |
2014 | enum noside noside, enum exp_opcode op, | |
2015 | struct value *arg1) | |
2016 | { | |
2017 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2018 | return arg1; | |
2019 | else if (unop_user_defined_p (op, arg1)) | |
2020 | { | |
2021 | return value_x_unop (arg1, op, noside); | |
2022 | } | |
2023 | else | |
2024 | { | |
2025 | struct value *arg3 = value_non_lval (arg1); | |
2026 | struct value *arg2; | |
2027 | ||
2028 | if (ptrmath_type_p (exp->language_defn, value_type (arg1))) | |
2029 | arg2 = value_ptradd (arg1, 1); | |
2030 | else | |
2031 | { | |
2032 | struct value *tmp = arg1; | |
2033 | ||
2034 | arg2 = value_one (value_type (arg1)); | |
2035 | binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); | |
2036 | arg2 = value_binop (tmp, arg2, BINOP_ADD); | |
2037 | } | |
2038 | ||
2039 | value_assign (arg1, arg2); | |
2040 | return arg3; | |
2041 | } | |
2042 | } | |
2043 | ||
a220ead5 TT |
2044 | /* A helper function for UNOP_POSTDECREMENT. */ |
2045 | ||
6d89e296 | 2046 | struct value * |
a220ead5 TT |
2047 | eval_op_postdec (struct type *expect_type, struct expression *exp, |
2048 | enum noside noside, enum exp_opcode op, | |
2049 | struct value *arg1) | |
2050 | { | |
2051 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2052 | return arg1; | |
2053 | else if (unop_user_defined_p (op, arg1)) | |
2054 | { | |
2055 | return value_x_unop (arg1, op, noside); | |
2056 | } | |
2057 | else | |
2058 | { | |
2059 | struct value *arg3 = value_non_lval (arg1); | |
2060 | struct value *arg2; | |
2061 | ||
2062 | if (ptrmath_type_p (exp->language_defn, value_type (arg1))) | |
2063 | arg2 = value_ptradd (arg1, -1); | |
2064 | else | |
2065 | { | |
2066 | struct value *tmp = arg1; | |
2067 | ||
2068 | arg2 = value_one (value_type (arg1)); | |
2069 | binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); | |
2070 | arg2 = value_binop (tmp, arg2, BINOP_SUB); | |
2071 | } | |
2072 | ||
2073 | value_assign (arg1, arg2); | |
2074 | return arg3; | |
2075 | } | |
2076 | } | |
2077 | ||
aec95807 TT |
2078 | /* A helper function for OP_TYPE. */ |
2079 | ||
5b5f5140 | 2080 | struct value * |
aec95807 TT |
2081 | eval_op_type (struct type *expect_type, struct expression *exp, |
2082 | enum noside noside, struct type *type) | |
2083 | { | |
2084 | if (noside == EVAL_SKIP) | |
2085 | return eval_skip_value (exp); | |
2086 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
2087 | return allocate_value (type); | |
2088 | else | |
2089 | error (_("Attempt to use a type name as an expression")); | |
2090 | } | |
2091 | ||
fb5ba2ab TT |
2092 | /* A helper function for BINOP_ASSIGN_MODIFY. */ |
2093 | ||
e5946e16 | 2094 | struct value * |
fb5ba2ab TT |
2095 | eval_binop_assign_modify (struct type *expect_type, struct expression *exp, |
2096 | enum noside noside, enum exp_opcode op, | |
2097 | struct value *arg1, struct value *arg2) | |
2098 | { | |
2099 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) | |
2100 | return arg1; | |
2101 | if (binop_user_defined_p (op, arg1, arg2)) | |
2102 | return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside); | |
2103 | else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn, | |
2104 | value_type (arg1)) | |
2105 | && is_integral_type (value_type (arg2))) | |
2106 | arg2 = value_ptradd (arg1, value_as_long (arg2)); | |
2107 | else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn, | |
2108 | value_type (arg1)) | |
2109 | && is_integral_type (value_type (arg2))) | |
2110 | arg2 = value_ptradd (arg1, - value_as_long (arg2)); | |
2111 | else | |
2112 | { | |
2113 | struct value *tmp = arg1; | |
2114 | ||
2115 | /* For shift and integer exponentiation operations, | |
2116 | only promote the first argument. */ | |
2117 | if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP) | |
2118 | && is_integral_type (value_type (arg2))) | |
2119 | unop_promote (exp->language_defn, exp->gdbarch, &tmp); | |
2120 | else | |
2121 | binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2); | |
2122 | ||
2123 | arg2 = value_binop (tmp, arg2, op); | |
2124 | } | |
2125 | return value_assign (arg1, arg2); | |
2126 | } | |
2127 | ||
5e80600e TT |
2128 | /* Note that ARGS needs 2 empty slots up front and must end with a |
2129 | null pointer. */ | |
2130 | static struct value * | |
2131 | eval_op_objc_msgcall (struct type *expect_type, struct expression *exp, | |
2132 | enum noside noside, CORE_ADDR selector, | |
2133 | value *target, gdb::array_view<value *> args) | |
2134 | { | |
2135 | CORE_ADDR responds_selector = 0; | |
2136 | CORE_ADDR method_selector = 0; | |
2137 | ||
2138 | int struct_return = 0; | |
2139 | ||
2140 | struct value *msg_send = NULL; | |
2141 | struct value *msg_send_stret = NULL; | |
2142 | int gnu_runtime = 0; | |
2143 | ||
2144 | struct value *method = NULL; | |
2145 | struct value *called_method = NULL; | |
2146 | ||
2147 | struct type *selector_type = NULL; | |
2148 | struct type *long_type; | |
2149 | struct type *type; | |
2150 | ||
2151 | struct value *ret = NULL; | |
2152 | CORE_ADDR addr = 0; | |
2153 | ||
2154 | value *argvec[5]; | |
2155 | ||
2156 | long_type = builtin_type (exp->gdbarch)->builtin_long; | |
2157 | selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; | |
2158 | ||
2159 | if (value_as_long (target) == 0) | |
2160 | return value_from_longest (long_type, 0); | |
2161 | ||
2162 | if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym) | |
2163 | gnu_runtime = 1; | |
2164 | ||
2165 | /* Find the method dispatch (Apple runtime) or method lookup | |
2166 | (GNU runtime) function for Objective-C. These will be used | |
2167 | to lookup the symbol information for the method. If we | |
2168 | can't find any symbol information, then we'll use these to | |
2169 | call the method, otherwise we can call the method | |
2170 | directly. The msg_send_stret function is used in the special | |
2171 | case of a method that returns a structure (Apple runtime | |
2172 | only). */ | |
2173 | if (gnu_runtime) | |
2174 | { | |
2175 | type = selector_type; | |
2176 | ||
2177 | type = lookup_function_type (type); | |
2178 | type = lookup_pointer_type (type); | |
2179 | type = lookup_function_type (type); | |
2180 | type = lookup_pointer_type (type); | |
2181 | ||
2182 | msg_send = find_function_in_inferior ("objc_msg_lookup", NULL); | |
2183 | msg_send_stret | |
2184 | = find_function_in_inferior ("objc_msg_lookup", NULL); | |
2185 | ||
2186 | msg_send = value_from_pointer (type, value_as_address (msg_send)); | |
2187 | msg_send_stret = value_from_pointer (type, | |
2188 | value_as_address (msg_send_stret)); | |
2189 | } | |
2190 | else | |
2191 | { | |
2192 | msg_send = find_function_in_inferior ("objc_msgSend", NULL); | |
2193 | /* Special dispatcher for methods returning structs. */ | |
2194 | msg_send_stret | |
2195 | = find_function_in_inferior ("objc_msgSend_stret", NULL); | |
2196 | } | |
2197 | ||
2198 | /* Verify the target object responds to this method. The | |
2199 | standard top-level 'Object' class uses a different name for | |
2200 | the verification method than the non-standard, but more | |
2201 | often used, 'NSObject' class. Make sure we check for both. */ | |
2202 | ||
2203 | responds_selector | |
2204 | = lookup_child_selector (exp->gdbarch, "respondsToSelector:"); | |
2205 | if (responds_selector == 0) | |
2206 | responds_selector | |
2207 | = lookup_child_selector (exp->gdbarch, "respondsTo:"); | |
2208 | ||
2209 | if (responds_selector == 0) | |
2210 | error (_("no 'respondsTo:' or 'respondsToSelector:' method")); | |
2211 | ||
2212 | method_selector | |
2213 | = lookup_child_selector (exp->gdbarch, "methodForSelector:"); | |
2214 | if (method_selector == 0) | |
2215 | method_selector | |
2216 | = lookup_child_selector (exp->gdbarch, "methodFor:"); | |
2217 | ||
2218 | if (method_selector == 0) | |
2219 | error (_("no 'methodFor:' or 'methodForSelector:' method")); | |
2220 | ||
2221 | /* Call the verification method, to make sure that the target | |
2222 | class implements the desired method. */ | |
2223 | ||
2224 | argvec[0] = msg_send; | |
2225 | argvec[1] = target; | |
2226 | argvec[2] = value_from_longest (long_type, responds_selector); | |
2227 | argvec[3] = value_from_longest (long_type, selector); | |
2228 | argvec[4] = 0; | |
2229 | ||
2230 | ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); | |
2231 | if (gnu_runtime) | |
2232 | { | |
2233 | /* Function objc_msg_lookup returns a pointer. */ | |
2234 | argvec[0] = ret; | |
2235 | ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); | |
2236 | } | |
2237 | if (value_as_long (ret) == 0) | |
2238 | error (_("Target does not respond to this message selector.")); | |
2239 | ||
2240 | /* Call "methodForSelector:" method, to get the address of a | |
2241 | function method that implements this selector for this | |
2242 | class. If we can find a symbol at that address, then we | |
2243 | know the return type, parameter types etc. (that's a good | |
2244 | thing). */ | |
2245 | ||
2246 | argvec[0] = msg_send; | |
2247 | argvec[1] = target; | |
2248 | argvec[2] = value_from_longest (long_type, method_selector); | |
2249 | argvec[3] = value_from_longest (long_type, selector); | |
2250 | argvec[4] = 0; | |
2251 | ||
2252 | ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); | |
2253 | if (gnu_runtime) | |
2254 | { | |
2255 | argvec[0] = ret; | |
2256 | ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3}); | |
2257 | } | |
2258 | ||
2259 | /* ret should now be the selector. */ | |
2260 | ||
2261 | addr = value_as_long (ret); | |
2262 | if (addr) | |
2263 | { | |
2264 | struct symbol *sym = NULL; | |
2265 | ||
2266 | /* The address might point to a function descriptor; | |
2267 | resolve it to the actual code address instead. */ | |
2268 | addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr, | |
2269 | current_top_target ()); | |
2270 | ||
2271 | /* Is it a high_level symbol? */ | |
2272 | sym = find_pc_function (addr); | |
2273 | if (sym != NULL) | |
2274 | method = value_of_variable (sym, 0); | |
2275 | } | |
2276 | ||
2277 | /* If we found a method with symbol information, check to see | |
2278 | if it returns a struct. Otherwise assume it doesn't. */ | |
2279 | ||
2280 | if (method) | |
2281 | { | |
2282 | CORE_ADDR funaddr; | |
2283 | struct type *val_type; | |
2284 | ||
2285 | funaddr = find_function_addr (method, &val_type); | |
2286 | ||
2287 | block_for_pc (funaddr); | |
2288 | ||
2289 | val_type = check_typedef (val_type); | |
2290 | ||
2291 | if ((val_type == NULL) | |
2292 | || (val_type->code () == TYPE_CODE_ERROR)) | |
2293 | { | |
2294 | if (expect_type != NULL) | |
2295 | val_type = expect_type; | |
2296 | } | |
2297 | ||
2298 | struct_return = using_struct_return (exp->gdbarch, method, | |
2299 | val_type); | |
2300 | } | |
2301 | else if (expect_type != NULL) | |
2302 | { | |
2303 | struct_return = using_struct_return (exp->gdbarch, NULL, | |
2304 | check_typedef (expect_type)); | |
2305 | } | |
2306 | ||
2307 | /* Found a function symbol. Now we will substitute its | |
2308 | value in place of the message dispatcher (obj_msgSend), | |
2309 | so that we call the method directly instead of thru | |
2310 | the dispatcher. The main reason for doing this is that | |
2311 | we can now evaluate the return value and parameter values | |
2312 | according to their known data types, in case we need to | |
2313 | do things like promotion, dereferencing, special handling | |
2314 | of structs and doubles, etc. | |
2315 | ||
2316 | We want to use the type signature of 'method', but still | |
2317 | jump to objc_msgSend() or objc_msgSend_stret() to better | |
2318 | mimic the behavior of the runtime. */ | |
2319 | ||
2320 | if (method) | |
2321 | { | |
2322 | if (value_type (method)->code () != TYPE_CODE_FUNC) | |
2323 | error (_("method address has symbol information " | |
2324 | "with non-function type; skipping")); | |
2325 | ||
2326 | /* Create a function pointer of the appropriate type, and | |
2327 | replace its value with the value of msg_send or | |
2328 | msg_send_stret. We must use a pointer here, as | |
2329 | msg_send and msg_send_stret are of pointer type, and | |
2330 | the representation may be different on systems that use | |
2331 | function descriptors. */ | |
2332 | if (struct_return) | |
2333 | called_method | |
2334 | = value_from_pointer (lookup_pointer_type (value_type (method)), | |
2335 | value_as_address (msg_send_stret)); | |
2336 | else | |
2337 | called_method | |
2338 | = value_from_pointer (lookup_pointer_type (value_type (method)), | |
2339 | value_as_address (msg_send)); | |
2340 | } | |
2341 | else | |
2342 | { | |
2343 | if (struct_return) | |
2344 | called_method = msg_send_stret; | |
2345 | else | |
2346 | called_method = msg_send; | |
2347 | } | |
2348 | ||
2349 | if (noside == EVAL_SKIP) | |
2350 | return eval_skip_value (exp); | |
2351 | ||
2352 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
2353 | { | |
2354 | /* If the return type doesn't look like a function type, | |
2355 | call an error. This can happen if somebody tries to | |
2356 | turn a variable into a function call. This is here | |
2357 | because people often want to call, eg, strcmp, which | |
2358 | gdb doesn't know is a function. If gdb isn't asked for | |
2359 | it's opinion (ie. through "whatis"), it won't offer | |
2360 | it. */ | |
2361 | ||
2362 | struct type *callee_type = value_type (called_method); | |
2363 | ||
2364 | if (callee_type && callee_type->code () == TYPE_CODE_PTR) | |
2365 | callee_type = TYPE_TARGET_TYPE (callee_type); | |
2366 | callee_type = TYPE_TARGET_TYPE (callee_type); | |
2367 | ||
2368 | if (callee_type) | |
2369 | { | |
2370 | if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type) | |
2371 | return allocate_value (expect_type); | |
2372 | else | |
2373 | return allocate_value (callee_type); | |
2374 | } | |
2375 | else | |
2376 | error (_("Expression of type other than " | |
2377 | "\"method returning ...\" used as a method")); | |
2378 | } | |
2379 | ||
2380 | /* Now depending on whether we found a symbol for the method, | |
2381 | we will either call the runtime dispatcher or the method | |
2382 | directly. */ | |
2383 | ||
2384 | args[0] = target; | |
2385 | args[1] = value_from_longest (long_type, selector); | |
2386 | ||
2387 | if (gnu_runtime && (method != NULL)) | |
2388 | { | |
2389 | /* Function objc_msg_lookup returns a pointer. */ | |
2390 | struct type *tem_type = value_type (called_method); | |
2391 | tem_type = lookup_pointer_type (lookup_function_type (tem_type)); | |
2392 | deprecated_set_value_type (called_method, tem_type); | |
2393 | called_method = call_function_by_hand (called_method, NULL, args); | |
2394 | } | |
2395 | ||
2396 | return call_function_by_hand (called_method, NULL, args); | |
2397 | } | |
2398 | ||
c0d7ed8c TT |
2399 | /* Helper function for MULTI_SUBSCRIPT. */ |
2400 | ||
2401 | static struct value * | |
2402 | eval_multi_subscript (struct type *expect_type, struct expression *exp, | |
2403 | enum noside noside, value *arg1, | |
2404 | gdb::array_view<value *> args) | |
2405 | { | |
2406 | if (noside == EVAL_SKIP) | |
2407 | return arg1; | |
2408 | for (value *arg2 : args) | |
2409 | { | |
2410 | if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2)) | |
2411 | { | |
2412 | arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside); | |
2413 | } | |
2414 | else | |
2415 | { | |
2416 | arg1 = coerce_ref (arg1); | |
2417 | struct type *type = check_typedef (value_type (arg1)); | |
2418 | ||
2419 | switch (type->code ()) | |
2420 | { | |
2421 | case TYPE_CODE_PTR: | |
2422 | case TYPE_CODE_ARRAY: | |
2423 | case TYPE_CODE_STRING: | |
2424 | arg1 = value_subscript (arg1, value_as_long (arg2)); | |
2425 | break; | |
2426 | ||
2427 | default: | |
2428 | if (type->name ()) | |
2429 | error (_("cannot subscript something of type `%s'"), | |
2430 | type->name ()); | |
2431 | else | |
2432 | error (_("cannot subscript requested type")); | |
2433 | } | |
2434 | } | |
2435 | } | |
2436 | return (arg1); | |
2437 | } | |
2438 | ||
61051030 | 2439 | struct value * |
fba45db2 | 2440 | evaluate_subexp_standard (struct type *expect_type, |
aa1ee363 | 2441 | struct expression *exp, int *pos, |
fba45db2 | 2442 | enum noside noside) |
c906108c SS |
2443 | { |
2444 | enum exp_opcode op; | |
2445 | int tem, tem2, tem3; | |
e69570ee | 2446 | int pc, oldpos; |
61051030 AC |
2447 | struct value *arg1 = NULL; |
2448 | struct value *arg2 = NULL; | |
c906108c SS |
2449 | struct type *type; |
2450 | int nargs; | |
61051030 | 2451 | struct value **argvec; |
c906108c | 2452 | int ix; |
c5aa993b | 2453 | struct type **arg_types; |
c906108c | 2454 | |
c906108c SS |
2455 | pc = (*pos)++; |
2456 | op = exp->elts[pc].opcode; | |
2457 | ||
2458 | switch (op) | |
2459 | { | |
2460 | case OP_SCOPE: | |
2461 | tem = longest_to_int (exp->elts[pc + 2].longconst); | |
2462 | (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1); | |
ea2d29f7 TT |
2463 | return eval_op_scope (expect_type, exp, noside, |
2464 | exp->elts[pc + 1].type, | |
2465 | &exp->elts[pc + 3].string); | |
c906108c SS |
2466 | |
2467 | case OP_LONG: | |
2468 | (*pos) += 3; | |
2469 | return value_from_longest (exp->elts[pc + 1].type, | |
2470 | exp->elts[pc + 2].longconst); | |
2471 | ||
edd079d9 | 2472 | case OP_FLOAT: |
c906108c | 2473 | (*pos) += 3; |
edd079d9 UW |
2474 | return value_from_contents (exp->elts[pc + 1].type, |
2475 | exp->elts[pc + 2].floatconst); | |
27bc4d80 | 2476 | |
7322dca9 | 2477 | case OP_ADL_FUNC: |
c906108c | 2478 | case OP_VAR_VALUE: |
46a4882b | 2479 | { |
23be8da7 | 2480 | (*pos) += 3; |
46a4882b | 2481 | symbol *var = exp->elts[pc + 2].symbol; |
78134374 | 2482 | if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR) |
987012b8 | 2483 | error_unknown_type (var->print_name ()); |
23be8da7 RB |
2484 | if (noside != EVAL_SKIP) |
2485 | return evaluate_var_value (noside, exp->elts[pc + 1].block, var); | |
2486 | else | |
2487 | { | |
2488 | /* Return a dummy value of the correct type when skipping, so | |
2489 | that parent functions know what is to be skipped. */ | |
2490 | return allocate_value (SYMBOL_TYPE (var)); | |
2491 | } | |
46a4882b PA |
2492 | } |
2493 | ||
74ea4be4 | 2494 | case OP_VAR_MSYM_VALUE: |
46a4882b PA |
2495 | { |
2496 | (*pos) += 3; | |
2497 | ||
2498 | minimal_symbol *msymbol = exp->elts[pc + 2].msymbol; | |
c0df9289 TT |
2499 | return eval_op_var_msym_value (expect_type, exp, noside, |
2500 | pc == 0, msymbol, | |
2501 | exp->elts[pc + 1].objfile); | |
46a4882b | 2502 | } |
c906108c | 2503 | |
36b11add JK |
2504 | case OP_VAR_ENTRY_VALUE: |
2505 | (*pos) += 2; | |
36b11add JK |
2506 | |
2507 | { | |
2508 | struct symbol *sym = exp->elts[pc + 1].symbol; | |
36b11add | 2509 | |
50b98adc | 2510 | return eval_op_var_entry_value (expect_type, exp, noside, sym); |
36b11add JK |
2511 | } |
2512 | ||
858be34c PA |
2513 | case OP_FUNC_STATIC_VAR: |
2514 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
2515 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
2516 | if (noside == EVAL_SKIP) | |
2517 | return eval_skip_value (exp); | |
2518 | ||
2519 | { | |
2520 | value *func = evaluate_subexp_standard (NULL, exp, pos, noside); | |
858be34c | 2521 | |
9b1d8af6 TT |
2522 | return eval_op_func_static_var (expect_type, exp, noside, func, |
2523 | &exp->elts[pc + 2].string); | |
858be34c PA |
2524 | } |
2525 | ||
c906108c SS |
2526 | case OP_LAST: |
2527 | (*pos) += 2; | |
2528 | return | |
2529 | access_value_history (longest_to_int (exp->elts[pc + 1].longconst)); | |
2530 | ||
2531 | case OP_REGISTER: | |
2532 | { | |
67f3407f | 2533 | const char *name = &exp->elts[pc + 2].string; |
67f3407f DJ |
2534 | |
2535 | (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1); | |
ffff730b | 2536 | return eval_op_register (expect_type, exp, noside, name); |
c906108c SS |
2537 | } |
2538 | case OP_BOOL: | |
2539 | (*pos) += 2; | |
fbb06eb1 UW |
2540 | type = language_bool_type (exp->language_defn, exp->gdbarch); |
2541 | return value_from_longest (type, exp->elts[pc + 1].longconst); | |
c906108c SS |
2542 | |
2543 | case OP_INTERNALVAR: | |
2544 | (*pos) += 2; | |
78267919 UW |
2545 | return value_of_internalvar (exp->gdbarch, |
2546 | exp->elts[pc + 1].internalvar); | |
c906108c SS |
2547 | |
2548 | case OP_STRING: | |
2549 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
2550 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
14a1c64a TT |
2551 | return eval_op_string (expect_type, exp, noside, tem, |
2552 | &exp->elts[pc + 2].string); | |
c906108c | 2553 | |
3e43a32a MS |
2554 | case OP_OBJC_NSSTRING: /* Objective C Foundation Class |
2555 | NSString constant. */ | |
a9fa03de AF |
2556 | tem = longest_to_int (exp->elts[pc + 1].longconst); |
2557 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
2558 | if (noside == EVAL_SKIP) | |
827d0c51 | 2559 | return eval_skip_value (exp); |
3b7538c0 | 2560 | return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1); |
a9fa03de | 2561 | |
c906108c SS |
2562 | case OP_ARRAY: |
2563 | (*pos) += 3; | |
2564 | tem2 = longest_to_int (exp->elts[pc + 1].longconst); | |
2565 | tem3 = longest_to_int (exp->elts[pc + 2].longconst); | |
2566 | nargs = tem3 - tem2 + 1; | |
fe1fe7ea | 2567 | type = expect_type ? check_typedef (expect_type) : nullptr; |
c906108c | 2568 | |
fe1fe7ea | 2569 | if (expect_type != nullptr && noside != EVAL_SKIP |
78134374 | 2570 | && type->code () == TYPE_CODE_STRUCT) |
c906108c | 2571 | { |
61051030 | 2572 | struct value *rec = allocate_value (expect_type); |
d7f9d729 | 2573 | |
990a07ab | 2574 | memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type)); |
c906108c SS |
2575 | return evaluate_struct_tuple (rec, exp, pos, noside, nargs); |
2576 | } | |
2577 | ||
fe1fe7ea | 2578 | if (expect_type != nullptr && noside != EVAL_SKIP |
78134374 | 2579 | && type->code () == TYPE_CODE_ARRAY) |
c906108c | 2580 | { |
3d967001 | 2581 | struct type *range_type = type->index_type (); |
c906108c | 2582 | struct type *element_type = TYPE_TARGET_TYPE (type); |
61051030 | 2583 | struct value *array = allocate_value (expect_type); |
c906108c SS |
2584 | int element_size = TYPE_LENGTH (check_typedef (element_type)); |
2585 | LONGEST low_bound, high_bound, index; | |
d7f9d729 | 2586 | |
1f8d2881 | 2587 | if (!get_discrete_bounds (range_type, &low_bound, &high_bound)) |
c906108c SS |
2588 | { |
2589 | low_bound = 0; | |
2590 | high_bound = (TYPE_LENGTH (type) / element_size) - 1; | |
2591 | } | |
2592 | index = low_bound; | |
990a07ab | 2593 | memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type)); |
c5aa993b | 2594 | for (tem = nargs; --nargs >= 0;) |
c906108c | 2595 | { |
61051030 | 2596 | struct value *element; |
d7f9d729 | 2597 | |
c906108c | 2598 | element = evaluate_subexp (element_type, exp, pos, noside); |
df407dfe | 2599 | if (value_type (element) != element_type) |
c906108c | 2600 | element = value_cast (element_type, element); |
1cd49c43 TT |
2601 | if (index > high_bound) |
2602 | /* To avoid memory corruption. */ | |
2603 | error (_("Too many array elements")); | |
2604 | memcpy (value_contents_raw (array) | |
2605 | + (index - low_bound) * element_size, | |
2606 | value_contents (element), | |
2607 | element_size); | |
c906108c SS |
2608 | index++; |
2609 | } | |
2610 | return array; | |
2611 | } | |
2612 | ||
fe1fe7ea | 2613 | if (expect_type != nullptr && noside != EVAL_SKIP |
78134374 | 2614 | && type->code () == TYPE_CODE_SET) |
c906108c | 2615 | { |
61051030 | 2616 | struct value *set = allocate_value (expect_type); |
47b667de | 2617 | gdb_byte *valaddr = value_contents_raw (set); |
3d967001 | 2618 | struct type *element_type = type->index_type (); |
c906108c SS |
2619 | struct type *check_type = element_type; |
2620 | LONGEST low_bound, high_bound; | |
2621 | ||
0963b4bd | 2622 | /* Get targettype of elementtype. */ |
78134374 SM |
2623 | while (check_type->code () == TYPE_CODE_RANGE |
2624 | || check_type->code () == TYPE_CODE_TYPEDEF) | |
c906108c SS |
2625 | check_type = TYPE_TARGET_TYPE (check_type); |
2626 | ||
1f8d2881 | 2627 | if (!get_discrete_bounds (element_type, &low_bound, &high_bound)) |
8a3fe4f8 | 2628 | error (_("(power)set type with unknown size")); |
c906108c SS |
2629 | memset (valaddr, '\0', TYPE_LENGTH (type)); |
2630 | for (tem = 0; tem < nargs; tem++) | |
2631 | { | |
2632 | LONGEST range_low, range_high; | |
2633 | struct type *range_low_type, *range_high_type; | |
61051030 | 2634 | struct value *elem_val; |
d7f9d729 | 2635 | |
ae8fddda YQ |
2636 | elem_val = evaluate_subexp (element_type, exp, pos, noside); |
2637 | range_low_type = range_high_type = value_type (elem_val); | |
2638 | range_low = range_high = value_as_long (elem_val); | |
2639 | ||
0963b4bd | 2640 | /* Check types of elements to avoid mixture of elements from |
dda83cd7 SM |
2641 | different types. Also check if type of element is "compatible" |
2642 | with element type of powerset. */ | |
78134374 | 2643 | if (range_low_type->code () == TYPE_CODE_RANGE) |
c906108c | 2644 | range_low_type = TYPE_TARGET_TYPE (range_low_type); |
78134374 | 2645 | if (range_high_type->code () == TYPE_CODE_RANGE) |
c906108c | 2646 | range_high_type = TYPE_TARGET_TYPE (range_high_type); |
78134374 SM |
2647 | if ((range_low_type->code () != range_high_type->code ()) |
2648 | || (range_low_type->code () == TYPE_CODE_ENUM | |
905e0470 | 2649 | && (range_low_type != range_high_type))) |
0963b4bd | 2650 | /* different element modes. */ |
8a3fe4f8 | 2651 | error (_("POWERSET tuple elements of different mode")); |
78134374 SM |
2652 | if ((check_type->code () != range_low_type->code ()) |
2653 | || (check_type->code () == TYPE_CODE_ENUM | |
905e0470 | 2654 | && range_low_type != check_type)) |
8a3fe4f8 | 2655 | error (_("incompatible POWERSET tuple elements")); |
c906108c SS |
2656 | if (range_low > range_high) |
2657 | { | |
8a3fe4f8 | 2658 | warning (_("empty POWERSET tuple range")); |
c906108c SS |
2659 | continue; |
2660 | } | |
2661 | if (range_low < low_bound || range_high > high_bound) | |
8a3fe4f8 | 2662 | error (_("POWERSET tuple element out of range")); |
c906108c SS |
2663 | range_low -= low_bound; |
2664 | range_high -= low_bound; | |
c5aa993b | 2665 | for (; range_low <= range_high; range_low++) |
c906108c SS |
2666 | { |
2667 | int bit_index = (unsigned) range_low % TARGET_CHAR_BIT; | |
d7f9d729 | 2668 | |
d5a22e77 | 2669 | if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG) |
c906108c | 2670 | bit_index = TARGET_CHAR_BIT - 1 - bit_index; |
c5aa993b | 2671 | valaddr[(unsigned) range_low / TARGET_CHAR_BIT] |
c906108c SS |
2672 | |= 1 << bit_index; |
2673 | } | |
2674 | } | |
2675 | return set; | |
2676 | } | |
2677 | ||
8d749320 | 2678 | argvec = XALLOCAVEC (struct value *, nargs); |
c906108c SS |
2679 | for (tem = 0; tem < nargs; tem++) |
2680 | { | |
0963b4bd MS |
2681 | /* Ensure that array expressions are coerced into pointer |
2682 | objects. */ | |
c906108c SS |
2683 | argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside); |
2684 | } | |
2685 | if (noside == EVAL_SKIP) | |
827d0c51 | 2686 | return eval_skip_value (exp); |
c906108c SS |
2687 | return value_array (tem2, tem3, argvec); |
2688 | ||
2689 | case TERNOP_SLICE: | |
2690 | { | |
fe1fe7ea | 2691 | struct value *array = evaluate_subexp (nullptr, exp, pos, noside); |
f960a617 TT |
2692 | struct value *low = evaluate_subexp (nullptr, exp, pos, noside); |
2693 | struct value *upper = evaluate_subexp (nullptr, exp, pos, noside); | |
2694 | return eval_op_ternop (expect_type, exp, noside, array, low, upper); | |
c906108c SS |
2695 | } |
2696 | ||
c906108c SS |
2697 | case TERNOP_COND: |
2698 | /* Skip third and second args to evaluate the first one. */ | |
fe1fe7ea | 2699 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
2700 | if (value_logical_not (arg1)) |
2701 | { | |
fe1fe7ea SM |
2702 | evaluate_subexp (nullptr, exp, pos, EVAL_SKIP); |
2703 | return evaluate_subexp (nullptr, exp, pos, noside); | |
c906108c SS |
2704 | } |
2705 | else | |
2706 | { | |
fe1fe7ea SM |
2707 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
2708 | evaluate_subexp (nullptr, exp, pos, EVAL_SKIP); | |
c906108c SS |
2709 | return arg2; |
2710 | } | |
2711 | ||
a9fa03de AF |
2712 | case OP_OBJC_SELECTOR: |
2713 | { /* Objective C @selector operator. */ | |
2714 | char *sel = &exp->elts[pc + 2].string; | |
2715 | int len = longest_to_int (exp->elts[pc + 1].longconst); | |
2716 | ||
2717 | (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1); | |
a9fa03de AF |
2718 | if (sel[len] != 0) |
2719 | sel[len] = 0; /* Make sure it's terminated. */ | |
d4dbb9c7 | 2720 | |
f871bae1 | 2721 | return eval_op_objc_selector (expect_type, exp, noside, sel); |
a9fa03de AF |
2722 | } |
2723 | ||
2724 | case OP_OBJC_MSGCALL: | |
2725 | { /* Objective C message (method) call. */ | |
c253954e | 2726 | CORE_ADDR selector = 0; |
a9fa03de | 2727 | |
f486487f | 2728 | enum noside sub_no_side = EVAL_NORMAL; |
a9fa03de | 2729 | |
a9fa03de | 2730 | struct value *target = NULL; |
a9fa03de AF |
2731 | |
2732 | struct type *selector_type = NULL; | |
a9fa03de AF |
2733 | |
2734 | selector = exp->elts[pc + 1].longconst; | |
2735 | nargs = exp->elts[pc + 2].longconst; | |
5e80600e | 2736 | argvec = XALLOCAVEC (struct value *, nargs + 3); |
a9fa03de AF |
2737 | |
2738 | (*pos) += 3; | |
2739 | ||
d4dbb9c7 UW |
2740 | selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr; |
2741 | ||
a9fa03de AF |
2742 | if (noside == EVAL_AVOID_SIDE_EFFECTS) |
2743 | sub_no_side = EVAL_NORMAL; | |
2744 | else | |
2745 | sub_no_side = noside; | |
2746 | ||
2747 | target = evaluate_subexp (selector_type, exp, pos, sub_no_side); | |
2748 | ||
2749 | if (value_as_long (target) == 0) | |
5e80600e | 2750 | sub_no_side = EVAL_SKIP; |
a9fa03de | 2751 | else |
5e80600e | 2752 | sub_no_side = noside; |
a9fa03de AF |
2753 | |
2754 | /* Now depending on whether we found a symbol for the method, | |
2755 | we will either call the runtime dispatcher or the method | |
2756 | directly. */ | |
2757 | ||
5e80600e TT |
2758 | argvec[0] = nullptr; |
2759 | argvec[1] = nullptr; | |
a9fa03de AF |
2760 | /* User-supplied arguments. */ |
2761 | for (tem = 0; tem < nargs; tem++) | |
5e80600e TT |
2762 | argvec[tem + 2] = evaluate_subexp_with_coercion (exp, pos, |
2763 | sub_no_side); | |
a9fa03de AF |
2764 | argvec[tem + 3] = 0; |
2765 | ||
5e80600e | 2766 | auto call_args = gdb::make_array_view (argvec, nargs + 3); |
a9fa03de | 2767 | |
5e80600e TT |
2768 | return eval_op_objc_msgcall (expect_type, exp, noside, selector, |
2769 | target, call_args); | |
a9fa03de AF |
2770 | } |
2771 | break; | |
2772 | ||
c906108c | 2773 | case OP_FUNCALL: |
e69570ee | 2774 | return evaluate_funcall (expect_type, exp, pos, noside); |
c906108c | 2775 | |
c906108c SS |
2776 | case OP_COMPLEX: |
2777 | /* We have a complex number, There should be 2 floating | |
dda83cd7 | 2778 | point numbers that compose it. */ |
c806c55a | 2779 | (*pos) += 2; |
fe1fe7ea SM |
2780 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
2781 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); | |
c906108c | 2782 | |
c806c55a | 2783 | return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type); |
c906108c SS |
2784 | |
2785 | case STRUCTOP_STRUCT: | |
2786 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
2787 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
fe1fe7ea | 2788 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
3e96c4fc TT |
2789 | return eval_op_structop_struct (expect_type, exp, noside, arg1, |
2790 | &exp->elts[pc + 2].string); | |
c906108c SS |
2791 | |
2792 | case STRUCTOP_PTR: | |
2793 | tem = longest_to_int (exp->elts[pc + 1].longconst); | |
2794 | (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1); | |
fe1fe7ea | 2795 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
ab0609be | 2796 | return eval_op_structop_ptr (expect_type, exp, noside, arg1, |
fb461aa3 | 2797 | &exp->elts[pc + 2].string); |
c906108c SS |
2798 | |
2799 | case STRUCTOP_MEMBER: | |
0d5de010 DJ |
2800 | case STRUCTOP_MPTR: |
2801 | if (op == STRUCTOP_MEMBER) | |
2802 | arg1 = evaluate_subexp_for_address (exp, pos, noside); | |
2803 | else | |
fe1fe7ea | 2804 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
0d5de010 | 2805 | |
fe1fe7ea | 2806 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c | 2807 | |
b7a96ed2 | 2808 | return eval_op_member (expect_type, exp, noside, arg1, arg2); |
c906108c | 2809 | |
072bba3b | 2810 | case TYPE_INSTANCE: |
3693fdb3 PA |
2811 | { |
2812 | type_instance_flags flags | |
2813 | = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst); | |
2814 | nargs = longest_to_int (exp->elts[pc + 2].longconst); | |
2815 | arg_types = (struct type **) alloca (nargs * sizeof (struct type *)); | |
2816 | for (ix = 0; ix < nargs; ++ix) | |
2817 | arg_types[ix] = exp->elts[pc + 2 + ix + 1].type; | |
2818 | ||
b926417a | 2819 | fake_method fake_expect_type (flags, nargs, arg_types); |
3693fdb3 | 2820 | *(pos) += 4 + nargs; |
b926417a TT |
2821 | return evaluate_subexp_standard (fake_expect_type.type (), exp, pos, |
2822 | noside); | |
3693fdb3 | 2823 | } |
072bba3b | 2824 | |
c906108c SS |
2825 | case BINOP_CONCAT: |
2826 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
2827 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
e51e26a0 | 2828 | return eval_op_concat (expect_type, exp, noside, arg1, arg2); |
c906108c SS |
2829 | |
2830 | case BINOP_ASSIGN: | |
fe1fe7ea | 2831 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
55708e99 TT |
2832 | /* Special-case assignments where the left-hand-side is a |
2833 | convenience variable -- in these, don't bother setting an | |
2834 | expected type. This avoids a weird case where re-assigning a | |
2835 | string or array to an internal variable could error with "Too | |
2836 | many array elements". */ | |
2837 | arg2 = evaluate_subexp (VALUE_LVAL (arg1) == lval_internalvar | |
fe1fe7ea SM |
2838 | ? nullptr |
2839 | : value_type (arg1), | |
55708e99 | 2840 | exp, pos, noside); |
c906108c | 2841 | |
c906108c SS |
2842 | if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS) |
2843 | return arg1; | |
2844 | if (binop_user_defined_p (op, arg1, arg2)) | |
2845 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); | |
2846 | else | |
2847 | return value_assign (arg1, arg2); | |
2848 | ||
2849 | case BINOP_ASSIGN_MODIFY: | |
2850 | (*pos) += 2; | |
fe1fe7ea | 2851 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2852 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
c906108c | 2853 | op = exp->elts[pc + 1].opcode; |
fb5ba2ab TT |
2854 | return eval_binop_assign_modify (expect_type, exp, noside, op, |
2855 | arg1, arg2); | |
c906108c SS |
2856 | |
2857 | case BINOP_ADD: | |
2858 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
2859 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
a94323b6 | 2860 | return eval_op_add (expect_type, exp, noside, arg1, arg2); |
c906108c SS |
2861 | |
2862 | case BINOP_SUB: | |
2863 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
2864 | arg2 = evaluate_subexp_with_coercion (exp, pos, noside); | |
5133d78b | 2865 | return eval_op_sub (expect_type, exp, noside, arg1, arg2); |
c906108c | 2866 | |
bd49c137 | 2867 | case BINOP_EXP: |
c906108c SS |
2868 | case BINOP_MUL: |
2869 | case BINOP_DIV: | |
9b3442ee | 2870 | case BINOP_INTDIV: |
c906108c SS |
2871 | case BINOP_REM: |
2872 | case BINOP_MOD: | |
2873 | case BINOP_LSH: | |
2874 | case BINOP_RSH: | |
2875 | case BINOP_BITWISE_AND: | |
2876 | case BINOP_BITWISE_IOR: | |
2877 | case BINOP_BITWISE_XOR: | |
fe1fe7ea SM |
2878 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
2879 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); | |
7cdcdd02 | 2880 | return eval_op_binary (expect_type, exp, noside, op, arg1, arg2); |
c906108c | 2881 | |
c906108c | 2882 | case BINOP_SUBSCRIPT: |
fe1fe7ea SM |
2883 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
2884 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); | |
288d26bc | 2885 | return eval_op_subscript (expect_type, exp, noside, op, arg1, arg2); |
c906108c | 2886 | |
c906108c SS |
2887 | case MULTI_SUBSCRIPT: |
2888 | (*pos) += 2; | |
2889 | nargs = longest_to_int (exp->elts[pc + 1].longconst); | |
2890 | arg1 = evaluate_subexp_with_coercion (exp, pos, noside); | |
c8f02daa TT |
2891 | argvec = XALLOCAVEC (struct value *, nargs); |
2892 | for (ix = 0; ix < nargs; ++ix) | |
2893 | argvec[ix] = evaluate_subexp_with_coercion (exp, pos, noside); | |
c0d7ed8c TT |
2894 | return eval_multi_subscript (expect_type, exp, noside, arg1, |
2895 | gdb::make_array_view (argvec, nargs)); | |
c906108c | 2896 | |
c906108c | 2897 | case BINOP_LOGICAL_AND: |
fe1fe7ea | 2898 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
2899 | if (noside == EVAL_SKIP) |
2900 | { | |
fe1fe7ea | 2901 | evaluate_subexp (nullptr, exp, pos, noside); |
827d0c51 | 2902 | return eval_skip_value (exp); |
c906108c | 2903 | } |
c5aa993b | 2904 | |
c906108c | 2905 | oldpos = *pos; |
fe1fe7ea | 2906 | arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
c906108c | 2907 | *pos = oldpos; |
c5aa993b JM |
2908 | |
2909 | if (binop_user_defined_p (op, arg1, arg2)) | |
c906108c | 2910 | { |
fe1fe7ea | 2911 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
2912 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); |
2913 | } | |
2914 | else | |
2915 | { | |
2916 | tem = value_logical_not (arg1); | |
fe1fe7ea SM |
2917 | arg2 |
2918 | = evaluate_subexp (nullptr, exp, pos, (tem ? EVAL_SKIP : noside)); | |
fbb06eb1 UW |
2919 | type = language_bool_type (exp->language_defn, exp->gdbarch); |
2920 | return value_from_longest (type, | |
c5aa993b | 2921 | (LONGEST) (!tem && !value_logical_not (arg2))); |
c906108c SS |
2922 | } |
2923 | ||
2924 | case BINOP_LOGICAL_OR: | |
fe1fe7ea | 2925 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
2926 | if (noside == EVAL_SKIP) |
2927 | { | |
fe1fe7ea | 2928 | evaluate_subexp (nullptr, exp, pos, noside); |
827d0c51 | 2929 | return eval_skip_value (exp); |
c906108c | 2930 | } |
c5aa993b | 2931 | |
c906108c | 2932 | oldpos = *pos; |
fe1fe7ea | 2933 | arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
c906108c | 2934 | *pos = oldpos; |
c5aa993b JM |
2935 | |
2936 | if (binop_user_defined_p (op, arg1, arg2)) | |
c906108c | 2937 | { |
fe1fe7ea | 2938 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
2939 | return value_x_binop (arg1, arg2, op, OP_NULL, noside); |
2940 | } | |
2941 | else | |
2942 | { | |
2943 | tem = value_logical_not (arg1); | |
fe1fe7ea SM |
2944 | arg2 |
2945 | = evaluate_subexp (nullptr, exp, pos, (!tem ? EVAL_SKIP : noside)); | |
fbb06eb1 UW |
2946 | type = language_bool_type (exp->language_defn, exp->gdbarch); |
2947 | return value_from_longest (type, | |
c5aa993b | 2948 | (LONGEST) (!tem || !value_logical_not (arg2))); |
c906108c SS |
2949 | } |
2950 | ||
2951 | case BINOP_EQUAL: | |
fe1fe7ea | 2952 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2953 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
0cc96de8 | 2954 | return eval_op_equal (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2955 | |
2956 | case BINOP_NOTEQUAL: | |
fe1fe7ea | 2957 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2958 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
1fcb3559 | 2959 | return eval_op_notequal (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2960 | |
2961 | case BINOP_LESS: | |
fe1fe7ea | 2962 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2963 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
6cad1349 | 2964 | return eval_op_less (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2965 | |
2966 | case BINOP_GTR: | |
fe1fe7ea | 2967 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2968 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
1f78d732 | 2969 | return eval_op_gtr (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2970 | |
2971 | case BINOP_GEQ: | |
fe1fe7ea | 2972 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2973 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
96e3efd9 | 2974 | return eval_op_geq (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2975 | |
2976 | case BINOP_LEQ: | |
fe1fe7ea | 2977 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
df407dfe | 2978 | arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside); |
60cdd487 | 2979 | return eval_op_leq (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2980 | |
2981 | case BINOP_REPEAT: | |
fe1fe7ea SM |
2982 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
2983 | arg2 = evaluate_subexp (nullptr, exp, pos, noside); | |
d4eff4c1 | 2984 | return eval_op_repeat (expect_type, exp, noside, op, arg1, arg2); |
c906108c SS |
2985 | |
2986 | case BINOP_COMMA: | |
fe1fe7ea SM |
2987 | evaluate_subexp (nullptr, exp, pos, noside); |
2988 | return evaluate_subexp (nullptr, exp, pos, noside); | |
c906108c | 2989 | |
36e9969c | 2990 | case UNOP_PLUS: |
fe1fe7ea | 2991 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
39f288be | 2992 | return eval_op_plus (expect_type, exp, noside, op, arg1); |
36e9969c | 2993 | |
c906108c | 2994 | case UNOP_NEG: |
fe1fe7ea | 2995 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
606d105f | 2996 | return eval_op_neg (expect_type, exp, noside, op, arg1); |
c906108c SS |
2997 | |
2998 | case UNOP_COMPLEMENT: | |
2999 | /* C++: check for and handle destructor names. */ | |
c906108c | 3000 | |
fe1fe7ea | 3001 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
1f09ec81 | 3002 | return eval_op_complement (expect_type, exp, noside, op, arg1); |
c906108c SS |
3003 | |
3004 | case UNOP_LOGICAL_NOT: | |
fe1fe7ea | 3005 | arg1 = evaluate_subexp (nullptr, exp, pos, noside); |
24338fb9 | 3006 | return eval_op_lognot (expect_type, exp, noside, op, arg1); |
c906108c SS |
3007 | |
3008 | case UNOP_IND: | |
78134374 | 3009 | if (expect_type && expect_type->code () == TYPE_CODE_PTR) |
c5aa993b | 3010 | expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type)); |
c906108c | 3011 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); |
876469ff | 3012 | return eval_op_ind (expect_type, exp, noside, arg1); |
c906108c SS |
3013 | |
3014 | case UNOP_ADDR: | |
3015 | /* C++: check for and handle pointer to members. */ | |
c5aa993b | 3016 | |
c906108c SS |
3017 | if (noside == EVAL_SKIP) |
3018 | { | |
fe1fe7ea | 3019 | evaluate_subexp (nullptr, exp, pos, EVAL_SKIP); |
827d0c51 | 3020 | return eval_skip_value (exp); |
c906108c | 3021 | } |
c5aa993b | 3022 | else |
cbfa382a | 3023 | return evaluate_subexp_for_address (exp, pos, noside); |
c5aa993b | 3024 | |
c906108c SS |
3025 | case UNOP_SIZEOF: |
3026 | if (noside == EVAL_SKIP) | |
3027 | { | |
fe1fe7ea | 3028 | evaluate_subexp (nullptr, exp, pos, EVAL_SKIP); |
827d0c51 | 3029 | return eval_skip_value (exp); |
c906108c | 3030 | } |
5ecaaa66 | 3031 | return evaluate_subexp_for_sizeof (exp, pos, noside); |
c906108c | 3032 | |
007e1530 | 3033 | case UNOP_ALIGNOF: |
acee9468 TT |
3034 | arg1 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
3035 | return eval_op_alignof (expect_type, exp, noside, arg1); | |
007e1530 | 3036 | |
c906108c SS |
3037 | case UNOP_CAST: |
3038 | (*pos) += 2; | |
3039 | type = exp->elts[pc + 1].type; | |
46a4882b | 3040 | return evaluate_subexp_for_cast (exp, pos, noside, type); |
c906108c | 3041 | |
9eaf6705 TT |
3042 | case UNOP_CAST_TYPE: |
3043 | arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
3044 | type = value_type (arg1); | |
46a4882b | 3045 | return evaluate_subexp_for_cast (exp, pos, noside, type); |
9eaf6705 | 3046 | |
4e8f195d | 3047 | case UNOP_DYNAMIC_CAST: |
9eaf6705 TT |
3048 | arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
3049 | type = value_type (arg1); | |
4e8f195d TT |
3050 | arg1 = evaluate_subexp (type, exp, pos, noside); |
3051 | if (noside == EVAL_SKIP) | |
827d0c51 | 3052 | return eval_skip_value (exp); |
4e8f195d TT |
3053 | return value_dynamic_cast (type, arg1); |
3054 | ||
3055 | case UNOP_REINTERPRET_CAST: | |
9eaf6705 TT |
3056 | arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
3057 | type = value_type (arg1); | |
4e8f195d TT |
3058 | arg1 = evaluate_subexp (type, exp, pos, noside); |
3059 | if (noside == EVAL_SKIP) | |
827d0c51 | 3060 | return eval_skip_value (exp); |
4e8f195d TT |
3061 | return value_reinterpret_cast (type, arg1); |
3062 | ||
c906108c SS |
3063 | case UNOP_MEMVAL: |
3064 | (*pos) += 2; | |
3065 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
3aef2a07 TT |
3066 | return eval_op_memval (expect_type, exp, noside, arg1, |
3067 | exp->elts[pc + 1].type); | |
c906108c | 3068 | |
9eaf6705 TT |
3069 | case UNOP_MEMVAL_TYPE: |
3070 | arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
3071 | type = value_type (arg1); | |
3072 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
3aef2a07 | 3073 | return eval_op_memval (expect_type, exp, noside, arg1, type); |
9eaf6705 | 3074 | |
c906108c SS |
3075 | case UNOP_PREINCREMENT: |
3076 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
00f50884 | 3077 | return eval_op_preinc (expect_type, exp, noside, op, arg1); |
c906108c SS |
3078 | |
3079 | case UNOP_PREDECREMENT: | |
3080 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
9e1361b7 | 3081 | return eval_op_predec (expect_type, exp, noside, op, arg1); |
c906108c SS |
3082 | |
3083 | case UNOP_POSTINCREMENT: | |
3084 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
abffe116 | 3085 | return eval_op_postinc (expect_type, exp, noside, op, arg1); |
c906108c SS |
3086 | |
3087 | case UNOP_POSTDECREMENT: | |
3088 | arg1 = evaluate_subexp (expect_type, exp, pos, noside); | |
a220ead5 | 3089 | return eval_op_postdec (expect_type, exp, noside, op, arg1); |
c5aa993b | 3090 | |
c906108c SS |
3091 | case OP_THIS: |
3092 | (*pos) += 1; | |
85bc8cb7 | 3093 | return value_of_this (exp->language_defn); |
a9fa03de | 3094 | |
c906108c | 3095 | case OP_TYPE: |
d843c49c | 3096 | /* The value is not supposed to be used. This is here to make it |
dda83cd7 | 3097 | easier to accommodate expressions that contain types. */ |
d843c49c | 3098 | (*pos) += 2; |
aec95807 | 3099 | return eval_op_type (expect_type, exp, noside, exp->elts[pc + 1].type); |
c906108c | 3100 | |
608b4967 TT |
3101 | case OP_TYPEOF: |
3102 | case OP_DECLTYPE: | |
3103 | if (noside == EVAL_SKIP) | |
3104 | { | |
fe1fe7ea | 3105 | evaluate_subexp (nullptr, exp, pos, EVAL_SKIP); |
827d0c51 | 3106 | return eval_skip_value (exp); |
608b4967 TT |
3107 | } |
3108 | else if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3109 | { | |
3110 | enum exp_opcode sub_op = exp->elts[*pos].opcode; | |
3111 | struct value *result; | |
3112 | ||
fe1fe7ea | 3113 | result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
608b4967 TT |
3114 | |
3115 | /* 'decltype' has special semantics for lvalues. */ | |
3116 | if (op == OP_DECLTYPE | |
3117 | && (sub_op == BINOP_SUBSCRIPT | |
3118 | || sub_op == STRUCTOP_MEMBER | |
3119 | || sub_op == STRUCTOP_MPTR | |
3120 | || sub_op == UNOP_IND | |
3121 | || sub_op == STRUCTOP_STRUCT | |
3122 | || sub_op == STRUCTOP_PTR | |
3123 | || sub_op == OP_SCOPE)) | |
3124 | { | |
b926417a | 3125 | type = value_type (result); |
608b4967 | 3126 | |
aa006118 | 3127 | if (!TYPE_IS_REFERENCE (type)) |
608b4967 | 3128 | { |
3b224330 | 3129 | type = lookup_lvalue_reference_type (type); |
608b4967 TT |
3130 | result = allocate_value (type); |
3131 | } | |
3132 | } | |
3133 | ||
3134 | return result; | |
3135 | } | |
3136 | else | |
dda83cd7 | 3137 | error (_("Attempt to use a type as an expression")); |
608b4967 | 3138 | |
6e72ca20 TT |
3139 | case OP_TYPEID: |
3140 | { | |
3141 | struct value *result; | |
3142 | enum exp_opcode sub_op = exp->elts[*pos].opcode; | |
3143 | ||
3144 | if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF) | |
fe1fe7ea | 3145 | result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
6e72ca20 | 3146 | else |
fe1fe7ea | 3147 | result = evaluate_subexp (nullptr, exp, pos, noside); |
6e72ca20 TT |
3148 | |
3149 | if (noside != EVAL_NORMAL) | |
3150 | return allocate_value (cplus_typeid_type (exp->gdbarch)); | |
3151 | ||
3152 | return cplus_typeid (result); | |
3153 | } | |
3154 | ||
c906108c SS |
3155 | default: |
3156 | /* Removing this case and compiling with gcc -Wall reveals that | |
dda83cd7 SM |
3157 | a lot of cases are hitting this case. Some of these should |
3158 | probably be removed from expression.h; others are legitimate | |
3159 | expressions which are (apparently) not fully implemented. | |
c906108c | 3160 | |
dda83cd7 SM |
3161 | If there are any cases landing here which mean a user error, |
3162 | then they should be separate cases, with more descriptive | |
3163 | error messages. */ | |
c906108c | 3164 | |
3e43a32a MS |
3165 | error (_("GDB does not (yet) know how to " |
3166 | "evaluate that kind of expression")); | |
c906108c SS |
3167 | } |
3168 | ||
827d0c51 | 3169 | gdb_assert_not_reached ("missed return?"); |
c906108c SS |
3170 | } |
3171 | \f | |
13ea014a TT |
3172 | /* Helper for evaluate_subexp_for_address. */ |
3173 | ||
3174 | static value * | |
3175 | evaluate_subexp_for_address_base (struct expression *exp, enum noside noside, | |
3176 | value *x) | |
3177 | { | |
3178 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3179 | { | |
3180 | struct type *type = check_typedef (value_type (x)); | |
3181 | ||
3182 | if (TYPE_IS_REFERENCE (type)) | |
3183 | return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)), | |
3184 | not_lval); | |
3185 | else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x)) | |
3186 | return value_zero (lookup_pointer_type (value_type (x)), | |
3187 | not_lval); | |
3188 | else | |
3189 | error (_("Attempt to take address of " | |
3190 | "value not located in memory.")); | |
3191 | } | |
3192 | return value_addr (x); | |
3193 | } | |
3194 | ||
c906108c SS |
3195 | /* Evaluate a subexpression of EXP, at index *POS, |
3196 | and return the address of that subexpression. | |
3197 | Advance *POS over the subexpression. | |
3198 | If the subexpression isn't an lvalue, get an error. | |
3199 | NOSIDE may be EVAL_AVOID_SIDE_EFFECTS; | |
3200 | then only the type of the result need be correct. */ | |
3201 | ||
61051030 | 3202 | static struct value * |
aa1ee363 | 3203 | evaluate_subexp_for_address (struct expression *exp, int *pos, |
fba45db2 | 3204 | enum noside noside) |
c906108c SS |
3205 | { |
3206 | enum exp_opcode op; | |
52f0bd74 | 3207 | int pc; |
c906108c | 3208 | struct symbol *var; |
ab5c9f60 | 3209 | struct value *x; |
0d5de010 | 3210 | int tem; |
c906108c SS |
3211 | |
3212 | pc = (*pos); | |
3213 | op = exp->elts[pc].opcode; | |
3214 | ||
3215 | switch (op) | |
3216 | { | |
3217 | case UNOP_IND: | |
3218 | (*pos)++; | |
fe1fe7ea | 3219 | x = evaluate_subexp (nullptr, exp, pos, noside); |
ab5c9f60 DJ |
3220 | |
3221 | /* We can't optimize out "&*" if there's a user-defined operator*. */ | |
3222 | if (unop_user_defined_p (op, x)) | |
3223 | { | |
3224 | x = value_x_unop (x, op, noside); | |
0d5de010 | 3225 | goto default_case_after_eval; |
ab5c9f60 DJ |
3226 | } |
3227 | ||
708ead4e | 3228 | return coerce_array (x); |
c906108c SS |
3229 | |
3230 | case UNOP_MEMVAL: | |
3231 | (*pos) += 3; | |
3232 | return value_cast (lookup_pointer_type (exp->elts[pc + 1].type), | |
fe1fe7ea | 3233 | evaluate_subexp (nullptr, exp, pos, noside)); |
c906108c | 3234 | |
9eaf6705 TT |
3235 | case UNOP_MEMVAL_TYPE: |
3236 | { | |
3237 | struct type *type; | |
3238 | ||
3239 | (*pos) += 1; | |
fe1fe7ea | 3240 | x = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
9eaf6705 TT |
3241 | type = value_type (x); |
3242 | return value_cast (lookup_pointer_type (type), | |
fe1fe7ea | 3243 | evaluate_subexp (nullptr, exp, pos, noside)); |
9eaf6705 TT |
3244 | } |
3245 | ||
c906108c SS |
3246 | case OP_VAR_VALUE: |
3247 | var = exp->elts[pc + 2].symbol; | |
3248 | ||
3249 | /* C++: The "address" of a reference should yield the address | |
0963b4bd | 3250 | * of the object pointed to. Let value_addr() deal with it. */ |
aa006118 | 3251 | if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var))) |
c5aa993b | 3252 | goto default_case; |
c906108c SS |
3253 | |
3254 | (*pos) += 4; | |
3255 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3256 | { | |
3257 | struct type *type = | |
d7f9d729 | 3258 | lookup_pointer_type (SYMBOL_TYPE (var)); |
c906108c SS |
3259 | enum address_class sym_class = SYMBOL_CLASS (var); |
3260 | ||
3261 | if (sym_class == LOC_CONST | |
3262 | || sym_class == LOC_CONST_BYTES | |
2a2d4dc3 | 3263 | || sym_class == LOC_REGISTER) |
8a3fe4f8 | 3264 | error (_("Attempt to take address of register or constant.")); |
c906108c | 3265 | |
c5aa993b JM |
3266 | return |
3267 | value_zero (type, not_lval); | |
c906108c | 3268 | } |
ceef53c1 | 3269 | else |
61212c0f | 3270 | return address_of_variable (var, exp->elts[pc + 1].block); |
c906108c | 3271 | |
46a4882b PA |
3272 | case OP_VAR_MSYM_VALUE: |
3273 | { | |
3274 | (*pos) += 4; | |
3275 | ||
3276 | value *val = evaluate_var_msym_value (noside, | |
3277 | exp->elts[pc + 1].objfile, | |
3278 | exp->elts[pc + 2].msymbol); | |
3279 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3280 | { | |
3281 | struct type *type = lookup_pointer_type (value_type (val)); | |
3282 | return value_zero (type, not_lval); | |
3283 | } | |
3284 | else | |
3285 | return value_addr (val); | |
3286 | } | |
3287 | ||
0d5de010 DJ |
3288 | case OP_SCOPE: |
3289 | tem = longest_to_int (exp->elts[pc + 2].longconst); | |
3290 | (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1); | |
3291 | x = value_aggregate_elt (exp->elts[pc + 1].type, | |
3292 | &exp->elts[pc + 3].string, | |
072bba3b | 3293 | NULL, 1, noside); |
0d5de010 DJ |
3294 | if (x == NULL) |
3295 | error (_("There is no field named %s"), &exp->elts[pc + 3].string); | |
3296 | return x; | |
3297 | ||
c906108c SS |
3298 | default: |
3299 | default_case: | |
fe1fe7ea | 3300 | x = evaluate_subexp (nullptr, exp, pos, noside); |
0d5de010 | 3301 | default_case_after_eval: |
13ea014a | 3302 | return evaluate_subexp_for_address_base (exp, noside, x); |
c906108c SS |
3303 | } |
3304 | } | |
3305 | ||
e2803273 TT |
3306 | namespace expr |
3307 | { | |
3308 | ||
3309 | value * | |
3310 | operation::evaluate_for_cast (struct type *expect_type, | |
3311 | struct expression *exp, | |
3312 | enum noside noside) | |
3313 | { | |
3314 | value *val = evaluate (expect_type, exp, noside); | |
3315 | if (noside == EVAL_SKIP) | |
3316 | return eval_skip_value (exp); | |
3317 | return value_cast (expect_type, val); | |
3318 | } | |
3319 | ||
3320 | value * | |
3321 | operation::evaluate_for_address (struct expression *exp, enum noside noside) | |
3322 | { | |
3323 | value *val = evaluate (nullptr, exp, noside); | |
3324 | return evaluate_subexp_for_address_base (exp, noside, val); | |
3325 | } | |
3326 | ||
d5ab122c TT |
3327 | value * |
3328 | scope_operation::evaluate_for_address (struct expression *exp, | |
3329 | enum noside noside) | |
3330 | { | |
3331 | value *x = value_aggregate_elt (std::get<0> (m_storage), | |
3332 | std::get<1> (m_storage).c_str (), | |
3333 | NULL, 1, noside); | |
3334 | if (x == NULL) | |
3335 | error (_("There is no field named %s"), std::get<1> (m_storage).c_str ()); | |
3336 | return x; | |
3337 | } | |
3338 | ||
876469ff TT |
3339 | value * |
3340 | unop_ind_base_operation::evaluate_for_address (struct expression *exp, | |
3341 | enum noside noside) | |
3342 | { | |
3343 | value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside); | |
3344 | ||
3345 | /* We can't optimize out "&*" if there's a user-defined operator*. */ | |
3346 | if (unop_user_defined_p (UNOP_IND, x)) | |
3347 | { | |
3348 | x = value_x_unop (x, UNOP_IND, noside); | |
3349 | return evaluate_subexp_for_address_base (exp, noside, x); | |
3350 | } | |
3351 | ||
3352 | return coerce_array (x); | |
3353 | } | |
3354 | ||
0c8effa3 TT |
3355 | value * |
3356 | var_msym_value_operation::evaluate_for_address (struct expression *exp, | |
3357 | enum noside noside) | |
3358 | { | |
3359 | value *val = evaluate_var_msym_value (noside, | |
3360 | std::get<1> (m_storage), | |
3361 | std::get<0> (m_storage)); | |
3362 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3363 | { | |
3364 | struct type *type = lookup_pointer_type (value_type (val)); | |
3365 | return value_zero (type, not_lval); | |
3366 | } | |
3367 | else | |
3368 | return value_addr (val); | |
3369 | } | |
3370 | ||
cbc18219 TT |
3371 | value * |
3372 | unop_memval_operation::evaluate_for_address (struct expression *exp, | |
3373 | enum noside noside) | |
3374 | { | |
3375 | return value_cast (lookup_pointer_type (std::get<1> (m_storage)), | |
3376 | std::get<0> (m_storage)->evaluate (nullptr, exp, noside)); | |
3377 | } | |
3378 | ||
3379 | value * | |
3380 | unop_memval_type_operation::evaluate_for_address (struct expression *exp, | |
3381 | enum noside noside) | |
3382 | { | |
3383 | value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
3384 | EVAL_AVOID_SIDE_EFFECTS); | |
3385 | struct type *type = value_type (typeval); | |
3386 | return value_cast (lookup_pointer_type (type), | |
3387 | std::get<1> (m_storage)->evaluate (nullptr, exp, noside)); | |
3388 | } | |
3389 | ||
e2803273 TT |
3390 | } |
3391 | ||
c906108c SS |
3392 | /* Evaluate like `evaluate_subexp' except coercing arrays to pointers. |
3393 | When used in contexts where arrays will be coerced anyway, this is | |
3394 | equivalent to `evaluate_subexp' but much faster because it avoids | |
3395 | actually fetching array contents (perhaps obsolete now that we have | |
d69fe07e | 3396 | value_lazy()). |
c906108c SS |
3397 | |
3398 | Note that we currently only do the coercion for C expressions, where | |
3399 | arrays are zero based and the coercion is correct. For other languages, | |
3400 | with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION | |
0963b4bd | 3401 | to decide if coercion is appropriate. */ |
c906108c | 3402 | |
61051030 | 3403 | struct value * |
aa1ee363 AC |
3404 | evaluate_subexp_with_coercion (struct expression *exp, |
3405 | int *pos, enum noside noside) | |
c906108c | 3406 | { |
52f0bd74 AC |
3407 | enum exp_opcode op; |
3408 | int pc; | |
61051030 | 3409 | struct value *val; |
c906108c | 3410 | struct symbol *var; |
61212c0f | 3411 | struct type *type; |
c906108c SS |
3412 | |
3413 | pc = (*pos); | |
3414 | op = exp->elts[pc].opcode; | |
3415 | ||
3416 | switch (op) | |
3417 | { | |
3418 | case OP_VAR_VALUE: | |
3419 | var = exp->elts[pc + 2].symbol; | |
61212c0f | 3420 | type = check_typedef (SYMBOL_TYPE (var)); |
78134374 | 3421 | if (type->code () == TYPE_CODE_ARRAY |
bd63c870 | 3422 | && !type->is_vector () |
cc73bb8c | 3423 | && CAST_IS_CONVERSION (exp->language_defn)) |
c906108c SS |
3424 | { |
3425 | (*pos) += 4; | |
61212c0f UW |
3426 | val = address_of_variable (var, exp->elts[pc + 1].block); |
3427 | return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), | |
c906108c SS |
3428 | val); |
3429 | } | |
3430 | /* FALLTHROUGH */ | |
3431 | ||
3432 | default: | |
fe1fe7ea | 3433 | return evaluate_subexp (nullptr, exp, pos, noside); |
c906108c SS |
3434 | } |
3435 | } | |
3436 | ||
e82a5afc TT |
3437 | namespace expr |
3438 | { | |
3439 | ||
3440 | value * | |
3441 | var_value_operation::evaluate_for_address (struct expression *exp, | |
3442 | enum noside noside) | |
3443 | { | |
3444 | symbol *var = std::get<0> (m_storage); | |
3445 | ||
3446 | /* C++: The "address" of a reference should yield the address | |
3447 | * of the object pointed to. Let value_addr() deal with it. */ | |
3448 | if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var))) | |
3449 | return operation::evaluate_for_address (exp, noside); | |
3450 | ||
3451 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3452 | { | |
3453 | struct type *type = lookup_pointer_type (SYMBOL_TYPE (var)); | |
3454 | enum address_class sym_class = SYMBOL_CLASS (var); | |
3455 | ||
3456 | if (sym_class == LOC_CONST | |
3457 | || sym_class == LOC_CONST_BYTES | |
3458 | || sym_class == LOC_REGISTER) | |
3459 | error (_("Attempt to take address of register or constant.")); | |
3460 | ||
3461 | return value_zero (type, not_lval); | |
3462 | } | |
3463 | else | |
3464 | return address_of_variable (var, std::get<1> (m_storage)); | |
3465 | } | |
3466 | ||
3467 | value * | |
3468 | var_value_operation::evaluate_with_coercion (struct expression *exp, | |
3469 | enum noside noside) | |
3470 | { | |
3471 | struct symbol *var = std::get<0> (m_storage); | |
3472 | struct type *type = check_typedef (SYMBOL_TYPE (var)); | |
3473 | if (type->code () == TYPE_CODE_ARRAY | |
3474 | && !type->is_vector () | |
3475 | && CAST_IS_CONVERSION (exp->language_defn)) | |
3476 | { | |
3477 | struct value *val = address_of_variable (var, std::get<1> (m_storage)); | |
3478 | return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), val); | |
3479 | } | |
3480 | return evaluate (nullptr, exp, noside); | |
3481 | } | |
3482 | ||
3483 | } | |
3484 | ||
13ea014a TT |
3485 | /* Helper function for evaluating the size of a type. */ |
3486 | ||
3487 | static value * | |
3488 | evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type) | |
3489 | { | |
3490 | /* FIXME: This should be size_t. */ | |
3491 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
3492 | /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof: | |
3493 | "When applied to a reference or a reference type, the result is | |
3494 | the size of the referenced type." */ | |
3495 | type = check_typedef (type); | |
3496 | if (exp->language_defn->la_language == language_cplus | |
3497 | && (TYPE_IS_REFERENCE (type))) | |
3498 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
3499 | return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type)); | |
3500 | } | |
3501 | ||
c906108c SS |
3502 | /* Evaluate a subexpression of EXP, at index *POS, |
3503 | and return a value for the size of that subexpression. | |
5ecaaa66 SA |
3504 | Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL |
3505 | we allow side-effects on the operand if its type is a variable | |
3506 | length array. */ | |
c906108c | 3507 | |
61051030 | 3508 | static struct value * |
5ecaaa66 SA |
3509 | evaluate_subexp_for_sizeof (struct expression *exp, int *pos, |
3510 | enum noside noside) | |
c906108c | 3511 | { |
98b90dd8 UW |
3512 | /* FIXME: This should be size_t. */ |
3513 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
c906108c | 3514 | enum exp_opcode op; |
52f0bd74 | 3515 | int pc; |
c906108c | 3516 | struct type *type; |
61051030 | 3517 | struct value *val; |
c906108c SS |
3518 | |
3519 | pc = (*pos); | |
3520 | op = exp->elts[pc].opcode; | |
3521 | ||
3522 | switch (op) | |
3523 | { | |
3524 | /* This case is handled specially | |
dda83cd7 SM |
3525 | so that we avoid creating a value for the result type. |
3526 | If the result type is very big, it's desirable not to | |
3527 | create a value unnecessarily. */ | |
c906108c SS |
3528 | case UNOP_IND: |
3529 | (*pos)++; | |
fe1fe7ea | 3530 | val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
df407dfe | 3531 | type = check_typedef (value_type (val)); |
78134374 | 3532 | if (type->code () != TYPE_CODE_PTR |
aa006118 | 3533 | && !TYPE_IS_REFERENCE (type) |
78134374 | 3534 | && type->code () != TYPE_CODE_ARRAY) |
8a3fe4f8 | 3535 | error (_("Attempt to take contents of a non-pointer value.")); |
6b662e19 | 3536 | type = TYPE_TARGET_TYPE (type); |
3c8452d4 SA |
3537 | if (is_dynamic_type (type)) |
3538 | type = value_type (value_ind (val)); | |
3539 | return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type)); | |
c906108c SS |
3540 | |
3541 | case UNOP_MEMVAL: | |
3542 | (*pos) += 3; | |
245a5f0b KS |
3543 | type = exp->elts[pc + 1].type; |
3544 | break; | |
c906108c | 3545 | |
9eaf6705 TT |
3546 | case UNOP_MEMVAL_TYPE: |
3547 | (*pos) += 1; | |
3548 | val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS); | |
245a5f0b KS |
3549 | type = value_type (val); |
3550 | break; | |
9eaf6705 | 3551 | |
c906108c | 3552 | case OP_VAR_VALUE: |
6b662e19 | 3553 | type = SYMBOL_TYPE (exp->elts[pc + 2].symbol); |
4ad88275 SA |
3554 | if (is_dynamic_type (type)) |
3555 | { | |
fe1fe7ea | 3556 | val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL); |
4ad88275 | 3557 | type = value_type (val); |
b7874836 AB |
3558 | if (type->code () == TYPE_CODE_ARRAY) |
3559 | { | |
3560 | if (type_not_allocated (type) || type_not_associated (type)) | |
3561 | return value_zero (size_type, not_lval); | |
3562 | else if (is_dynamic_type (type->index_type ()) | |
3563 | && type->bounds ()->high.kind () == PROP_UNDEFINED) | |
3564 | return allocate_optimized_out_value (size_type); | |
3565 | } | |
4ad88275 SA |
3566 | } |
3567 | else | |
3568 | (*pos) += 4; | |
245a5f0b | 3569 | break; |
c906108c | 3570 | |
46a4882b PA |
3571 | case OP_VAR_MSYM_VALUE: |
3572 | { | |
3573 | (*pos) += 4; | |
3574 | ||
3575 | minimal_symbol *msymbol = exp->elts[pc + 2].msymbol; | |
b926417a TT |
3576 | value *mval = evaluate_var_msym_value (noside, |
3577 | exp->elts[pc + 1].objfile, | |
3578 | msymbol); | |
46a4882b | 3579 | |
b926417a | 3580 | type = value_type (mval); |
78134374 | 3581 | if (type->code () == TYPE_CODE_ERROR) |
c9d95fa3 | 3582 | error_unknown_type (msymbol->print_name ()); |
46a4882b PA |
3583 | |
3584 | return value_from_longest (size_type, TYPE_LENGTH (type)); | |
3585 | } | |
3586 | break; | |
3587 | ||
5ecaaa66 SA |
3588 | /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting |
3589 | type of the subscript is a variable length array type. In this case we | |
85102364 | 3590 | must re-evaluate the right hand side of the subscription to allow |
5ecaaa66 SA |
3591 | side-effects. */ |
3592 | case BINOP_SUBSCRIPT: | |
3593 | if (noside == EVAL_NORMAL) | |
3594 | { | |
b926417a | 3595 | int npc = (*pos) + 1; |
5ecaaa66 | 3596 | |
fe1fe7ea | 3597 | val = evaluate_subexp (nullptr, exp, &npc, EVAL_AVOID_SIDE_EFFECTS); |
5ecaaa66 | 3598 | type = check_typedef (value_type (val)); |
78134374 | 3599 | if (type->code () == TYPE_CODE_ARRAY) |
5ecaaa66 SA |
3600 | { |
3601 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
78134374 | 3602 | if (type->code () == TYPE_CODE_ARRAY) |
5ecaaa66 | 3603 | { |
3d967001 | 3604 | type = type->index_type (); |
5ecaaa66 SA |
3605 | /* Only re-evaluate the right hand side if the resulting type |
3606 | is a variable length type. */ | |
599088e3 | 3607 | if (type->bounds ()->flag_bound_evaluated) |
5ecaaa66 | 3608 | { |
fe1fe7ea | 3609 | val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL); |
5ecaaa66 SA |
3610 | return value_from_longest |
3611 | (size_type, (LONGEST) TYPE_LENGTH (value_type (val))); | |
3612 | } | |
3613 | } | |
3614 | } | |
3615 | } | |
3616 | ||
3617 | /* Fall through. */ | |
3618 | ||
c906108c | 3619 | default: |
fe1fe7ea | 3620 | val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS); |
245a5f0b KS |
3621 | type = value_type (val); |
3622 | break; | |
c906108c | 3623 | } |
245a5f0b | 3624 | |
13ea014a | 3625 | return evaluate_subexp_for_sizeof_base (exp, type); |
c906108c SS |
3626 | } |
3627 | ||
e2803273 TT |
3628 | namespace expr |
3629 | { | |
3630 | ||
3631 | value * | |
3632 | operation::evaluate_for_sizeof (struct expression *exp, enum noside noside) | |
3633 | { | |
3634 | value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS); | |
3635 | return evaluate_subexp_for_sizeof_base (exp, value_type (val)); | |
3636 | } | |
3637 | ||
0c8effa3 TT |
3638 | value * |
3639 | var_msym_value_operation::evaluate_for_sizeof (struct expression *exp, | |
3640 | enum noside noside) | |
3641 | ||
3642 | { | |
3643 | minimal_symbol *msymbol = std::get<0> (m_storage); | |
3644 | value *mval = evaluate_var_msym_value (noside, | |
3645 | std::get<1> (m_storage), | |
3646 | msymbol); | |
3647 | ||
3648 | struct type *type = value_type (mval); | |
3649 | if (type->code () == TYPE_CODE_ERROR) | |
3650 | error_unknown_type (msymbol->print_name ()); | |
3651 | ||
3652 | /* FIXME: This should be size_t. */ | |
3653 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
3654 | return value_from_longest (size_type, TYPE_LENGTH (type)); | |
3655 | } | |
3656 | ||
224d6424 TT |
3657 | value * |
3658 | subscript_operation::evaluate_for_sizeof (struct expression *exp, | |
3659 | enum noside noside) | |
3660 | { | |
3661 | if (noside == EVAL_NORMAL) | |
3662 | { | |
3663 | value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
3664 | EVAL_AVOID_SIDE_EFFECTS); | |
3665 | struct type *type = check_typedef (value_type (val)); | |
3666 | if (type->code () == TYPE_CODE_ARRAY) | |
3667 | { | |
3668 | type = check_typedef (TYPE_TARGET_TYPE (type)); | |
3669 | if (type->code () == TYPE_CODE_ARRAY) | |
3670 | { | |
3671 | type = type->index_type (); | |
3672 | /* Only re-evaluate the right hand side if the resulting type | |
3673 | is a variable length type. */ | |
3674 | if (type->bounds ()->flag_bound_evaluated) | |
3675 | { | |
3676 | val = evaluate (nullptr, exp, EVAL_NORMAL); | |
3677 | /* FIXME: This should be size_t. */ | |
3678 | struct type *size_type | |
3679 | = builtin_type (exp->gdbarch)->builtin_int; | |
3680 | return value_from_longest | |
3681 | (size_type, (LONGEST) TYPE_LENGTH (value_type (val))); | |
3682 | } | |
3683 | } | |
3684 | } | |
3685 | } | |
3686 | ||
3687 | return operation::evaluate_for_sizeof (exp, noside); | |
3688 | } | |
3689 | ||
876469ff TT |
3690 | value * |
3691 | unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp, | |
3692 | enum noside noside) | |
3693 | { | |
3694 | value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
3695 | EVAL_AVOID_SIDE_EFFECTS); | |
3696 | struct type *type = check_typedef (value_type (val)); | |
3697 | if (type->code () != TYPE_CODE_PTR | |
3698 | && !TYPE_IS_REFERENCE (type) | |
3699 | && type->code () != TYPE_CODE_ARRAY) | |
3700 | error (_("Attempt to take contents of a non-pointer value.")); | |
3701 | type = TYPE_TARGET_TYPE (type); | |
3702 | if (is_dynamic_type (type)) | |
3703 | type = value_type (value_ind (val)); | |
3704 | /* FIXME: This should be size_t. */ | |
3705 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
3706 | return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type)); | |
3707 | } | |
3708 | ||
cbc18219 TT |
3709 | value * |
3710 | unop_memval_operation::evaluate_for_sizeof (struct expression *exp, | |
3711 | enum noside noside) | |
3712 | { | |
3713 | return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage)); | |
3714 | } | |
3715 | ||
3716 | value * | |
3717 | unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp, | |
3718 | enum noside noside) | |
3719 | { | |
3720 | value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp, | |
3721 | EVAL_AVOID_SIDE_EFFECTS); | |
3722 | return evaluate_subexp_for_sizeof_base (exp, value_type (typeval)); | |
3723 | } | |
3724 | ||
e82a5afc TT |
3725 | value * |
3726 | var_value_operation::evaluate_for_sizeof (struct expression *exp, | |
3727 | enum noside noside) | |
3728 | { | |
3729 | struct type *type = SYMBOL_TYPE (std::get<0> (m_storage)); | |
3730 | if (is_dynamic_type (type)) | |
3731 | { | |
3732 | value *val = evaluate (nullptr, exp, EVAL_NORMAL); | |
3733 | type = value_type (val); | |
3734 | if (type->code () == TYPE_CODE_ARRAY) | |
3735 | { | |
3736 | /* FIXME: This should be size_t. */ | |
3737 | struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; | |
3738 | if (type_not_allocated (type) || type_not_associated (type)) | |
3739 | return value_zero (size_type, not_lval); | |
3740 | else if (is_dynamic_type (type->index_type ()) | |
3741 | && type->bounds ()->high.kind () == PROP_UNDEFINED) | |
3742 | return allocate_optimized_out_value (size_type); | |
3743 | } | |
3744 | } | |
3745 | return evaluate_subexp_for_sizeof_base (exp, type); | |
3746 | } | |
3747 | ||
e2803273 TT |
3748 | } |
3749 | ||
46a4882b PA |
3750 | /* Evaluate a subexpression of EXP, at index *POS, and return a value |
3751 | for that subexpression cast to TO_TYPE. Advance *POS over the | |
3752 | subexpression. */ | |
3753 | ||
3754 | static value * | |
3755 | evaluate_subexp_for_cast (expression *exp, int *pos, | |
3756 | enum noside noside, | |
3757 | struct type *to_type) | |
3758 | { | |
3759 | int pc = *pos; | |
3760 | ||
3761 | /* Don't let symbols be evaluated with evaluate_subexp because that | |
3762 | throws an "unknown type" error for no-debug data symbols. | |
3763 | Instead, we want the cast to reinterpret the symbol. */ | |
3764 | if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE | |
3765 | || exp->elts[pc].opcode == OP_VAR_VALUE) | |
3766 | { | |
3767 | (*pos) += 4; | |
3768 | ||
3769 | value *val; | |
3770 | if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE) | |
3771 | { | |
3772 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3773 | return value_zero (to_type, not_lval); | |
3774 | ||
3775 | val = evaluate_var_msym_value (noside, | |
3776 | exp->elts[pc + 1].objfile, | |
3777 | exp->elts[pc + 2].msymbol); | |
3778 | } | |
3779 | else | |
3780 | val = evaluate_var_value (noside, | |
3781 | exp->elts[pc + 1].block, | |
3782 | exp->elts[pc + 2].symbol); | |
3783 | ||
3784 | if (noside == EVAL_SKIP) | |
3785 | return eval_skip_value (exp); | |
3786 | ||
3787 | val = value_cast (to_type, val); | |
3788 | ||
3789 | /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ | |
3790 | if (VALUE_LVAL (val) == lval_memory) | |
3791 | { | |
3792 | if (value_lazy (val)) | |
3793 | value_fetch_lazy (val); | |
3794 | VALUE_LVAL (val) = not_lval; | |
3795 | } | |
3796 | return val; | |
3797 | } | |
3798 | ||
3799 | value *val = evaluate_subexp (to_type, exp, pos, noside); | |
3800 | if (noside == EVAL_SKIP) | |
3801 | return eval_skip_value (exp); | |
3802 | return value_cast (to_type, val); | |
3803 | } | |
3804 | ||
0c8effa3 TT |
3805 | namespace expr |
3806 | { | |
3807 | ||
3808 | value * | |
3809 | var_msym_value_operation::evaluate_for_cast (struct type *to_type, | |
3810 | struct expression *exp, | |
3811 | enum noside noside) | |
3812 | { | |
3813 | if (noside == EVAL_AVOID_SIDE_EFFECTS) | |
3814 | return value_zero (to_type, not_lval); | |
3815 | ||
3816 | value *val = evaluate_var_msym_value (noside, | |
3817 | std::get<1> (m_storage), | |
3818 | std::get<0> (m_storage)); | |
3819 | ||
3820 | if (noside == EVAL_SKIP) | |
3821 | return eval_skip_value (exp); | |
3822 | ||
3823 | val = value_cast (to_type, val); | |
3824 | ||
3825 | /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ | |
3826 | if (VALUE_LVAL (val) == lval_memory) | |
3827 | { | |
3828 | if (value_lazy (val)) | |
3829 | value_fetch_lazy (val); | |
3830 | VALUE_LVAL (val) = not_lval; | |
3831 | } | |
3832 | return val; | |
3833 | } | |
3834 | ||
e82a5afc TT |
3835 | value * |
3836 | var_value_operation::evaluate_for_cast (struct type *to_type, | |
3837 | struct expression *exp, | |
3838 | enum noside noside) | |
3839 | { | |
3840 | value *val = evaluate_var_value (noside, | |
3841 | std::get<1> (m_storage), | |
3842 | std::get<0> (m_storage)); | |
3843 | ||
3844 | if (noside == EVAL_SKIP) | |
3845 | return eval_skip_value (exp); | |
3846 | ||
3847 | val = value_cast (to_type, val); | |
3848 | ||
3849 | /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ | |
3850 | if (VALUE_LVAL (val) == lval_memory) | |
3851 | { | |
3852 | if (value_lazy (val)) | |
3853 | value_fetch_lazy (val); | |
3854 | VALUE_LVAL (val) = not_lval; | |
3855 | } | |
3856 | return val; | |
3857 | } | |
3858 | ||
0c8effa3 TT |
3859 | } |
3860 | ||
0963b4bd | 3861 | /* Parse a type expression in the string [P..P+LENGTH). */ |
c906108c SS |
3862 | |
3863 | struct type * | |
f5756acc | 3864 | parse_and_eval_type (const char *p, int length) |
c906108c | 3865 | { |
c5aa993b | 3866 | char *tmp = (char *) alloca (length + 4); |
d7f9d729 | 3867 | |
c5aa993b JM |
3868 | tmp[0] = '('; |
3869 | memcpy (tmp + 1, p, length); | |
3870 | tmp[length + 1] = ')'; | |
3871 | tmp[length + 2] = '0'; | |
3872 | tmp[length + 3] = '\0'; | |
4d01a485 | 3873 | expression_up expr = parse_expression (tmp); |
2adab65c | 3874 | if (expr->first_opcode () != UNOP_CAST) |
8a3fe4f8 | 3875 | error (_("Internal error in eval_type.")); |
c5aa993b | 3876 | return expr->elts[1].type; |
c906108c | 3877 | } |