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