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