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