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