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