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