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