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