]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/eval.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / eval.c
CommitLineData
c906108c 1/* Evaluate expressions for GDB.
1bac305b 2
1d506c26 3 Copyright (C) 1986-2024 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"
ef0f16cc 28#include "language.h"
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"
bf31fd38 39#include "gdbsupport/gdb_obstack.h"
4de283e4
TT
40#include "objfiles.h"
41#include "typeprint.h"
42#include <ctype.h>
e2803273 43#include "expop.h"
06dc61b9 44#include "c-exp.h"
328d42d8 45#include "inferior.h"
bc3b79fd 46
c906108c
SS
47\f
48/* Parse the string EXP as a C expression, evaluate it,
49 and return the result as a number. */
50
51CORE_ADDR
bbc13ae3 52parse_and_eval_address (const char *exp)
c906108c 53{
4d01a485
PA
54 expression_up expr = parse_expression (exp);
55
43048e46 56 return value_as_address (expr->evaluate ());
c906108c
SS
57}
58
bb518678 59/* Like parse_and_eval_address, but treats the value of the expression
0963b4bd 60 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
bb518678 61LONGEST
a1b8c4cc 62parse_and_eval_long (const char *exp)
bb518678 63{
4d01a485
PA
64 expression_up expr = parse_expression (exp);
65
43048e46 66 return value_as_long (expr->evaluate ());
bb518678
DT
67}
68
61051030 69struct value *
2c64cbb7 70parse_and_eval (const char *exp, parser_flags flags)
c906108c 71{
2c64cbb7 72 expression_up expr = parse_expression (exp, nullptr, flags);
c906108c 73
43048e46 74 return expr->evaluate ();
c906108c
SS
75}
76
77/* Parse up to a comma (or to a closeparen)
78 in the string EXPP as an expression, evaluate it, and return the value.
79 EXPP is advanced to point to the comma. */
80
61051030 81struct value *
bbc13ae3 82parse_to_comma_and_eval (const char **expp)
c906108c 83{
b8c03634
TT
84 expression_up expr = parse_exp_1 (expp, 0, nullptr,
85 PARSER_COMMA_TERMINATES);
c906108c 86
43048e46 87 return expr->evaluate ();
c906108c
SS
88}
89\f
c906108c 90
26f53cd3
TT
91/* See expression.h. */
92
aa9bd445
TT
93bool
94expression::uses_objfile (struct objfile *objfile) const
95{
96 gdb_assert (objfile->separate_debug_objfile_backlink == nullptr);
97 return op->uses_objfile (objfile);
98}
99
100/* See expression.h. */
101
26f53cd3
TT
102struct value *
103expression::evaluate (struct type *expect_type, enum noside noside)
104{
6b09f134 105 std::optional<enable_thread_stack_temporaries> stack_temporaries;
df5bc734 106 if (target_has_execution () && inferior_ptid != null_ptid
26f53cd3
TT
107 && language_defn->la_language == language_cplus
108 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
109 stack_temporaries.emplace (inferior_thread ());
110
1eaebe02 111 struct value *retval = op->evaluate (expect_type, this, noside);
26f53cd3
TT
112
113 if (stack_temporaries.has_value ()
114 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
aa9f4538 115 retval = retval->non_lval ();
26f53cd3
TT
116
117 return retval;
118}
119
0cf6dd15
TJB
120/* Find the current value of a watchpoint on EXP. Return the value in
121 *VALP and *RESULTP and the chain of intermediate and final values
122 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
123 not need them.
124
3a1115a0
TT
125 If PRESERVE_ERRORS is true, then exceptions are passed through.
126 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
127 occurs while evaluating the expression, *RESULTP will be set to
128 NULL. *RESULTP may be a lazy value, if the result could not be
129 read from memory. It is used to determine whether a value is
130 user-specified (we should watch the whole value) or intermediate
0cf6dd15
TJB
131 (we should watch only the bit used to locate the final value).
132
133 If the final value, or any intermediate value, could not be read
134 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
135 set to any referenced values. *VALP will never be a lazy value.
136 This is the value which we store in struct breakpoint.
137
a6535de1
TT
138 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
139 released from the value chain. If VAL_CHAIN is NULL, all generated
140 values will be left on the value chain. */
0cf6dd15
TJB
141
142void
1eaebe02 143fetch_subexp_value (struct expression *exp,
413403fc
TT
144 expr::operation *op,
145 struct value **valp, struct value **resultp,
a6535de1 146 std::vector<value_ref_ptr> *val_chain,
2e362716 147 bool preserve_errors)
0cf6dd15
TJB
148{
149 struct value *mark, *new_mark, *result;
0cf6dd15
TJB
150
151 *valp = NULL;
152 if (resultp)
153 *resultp = NULL;
154 if (val_chain)
a6535de1 155 val_chain->clear ();
0cf6dd15
TJB
156
157 /* Evaluate the expression. */
158 mark = value_mark ();
159 result = NULL;
160
a70b8144 161 try
0cf6dd15 162 {
1eaebe02 163 result = op->evaluate (nullptr, exp, EVAL_NORMAL);
0cf6dd15 164 }
230d2906 165 catch (const gdb_exception &ex)
0cf6dd15 166 {
3a1115a0 167 /* Ignore memory errors if we want watchpoints pointing at
0cf6dd15
TJB
168 inaccessible memory to still be created; otherwise, throw the
169 error to some higher catcher. */
170 switch (ex.error)
171 {
172 case MEMORY_ERROR:
3a1115a0
TT
173 if (!preserve_errors)
174 break;
d182e398 175 [[fallthrough]];
0cf6dd15 176 default:
eedc3f4f 177 throw;
0cf6dd15
TJB
178 break;
179 }
180 }
181
182 new_mark = value_mark ();
183 if (mark == new_mark)
184 return;
185 if (resultp)
186 *resultp = result;
187
188 /* Make sure it's not lazy, so that after the target stops again we
189 have a non-lazy previous value to compare with. */
8e7b59a5
KS
190 if (result != NULL)
191 {
3ee3b270 192 if (!result->lazy ())
8e7b59a5
KS
193 *valp = result;
194 else
195 {
8e7b59a5 196
a70b8144 197 try
8e7b59a5 198 {
78259c36 199 result->fetch_lazy ();
8e7b59a5
KS
200 *valp = result;
201 }
230d2906 202 catch (const gdb_exception_error &except)
492d29ea
PA
203 {
204 }
8e7b59a5
KS
205 }
206 }
0cf6dd15
TJB
207
208 if (val_chain)
209 {
210 /* Return the chain of intermediate values. We use this to
211 decide which addresses to watch. */
a6535de1 212 *val_chain = value_release_to_mark (mark);
0cf6dd15
TJB
213 }
214}
215
4066e646
UW
216/* Promote value ARG1 as appropriate before performing a unary operation
217 on this argument.
218 If the result is not appropriate for any particular language then it
219 needs to patch this function. */
220
221void
222unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
223 struct value **arg1)
224{
225 struct type *type1;
226
227 *arg1 = coerce_ref (*arg1);
d0c97917 228 type1 = check_typedef ((*arg1)->type ());
4066e646
UW
229
230 if (is_integral_type (type1))
231 {
232 switch (language->la_language)
233 {
234 default:
235 /* Perform integral promotion for ANSI C/C++.
85102364 236 If not appropriate for any particular language
4066e646
UW
237 it needs to modify this function. */
238 {
239 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
d7f9d729 240
df86565b 241 if (type1->length () < builtin_int->length ())
4066e646
UW
242 *arg1 = value_cast (builtin_int, *arg1);
243 }
244 break;
245 }
246 }
247}
248
249/* Promote values ARG1 and ARG2 as appropriate before performing a binary
250 operation on those two operands.
251 If the result is not appropriate for any particular language then it
252 needs to patch this function. */
253
254void
255binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
256 struct value **arg1, struct value **arg2)
257{
258 struct type *promoted_type = NULL;
259 struct type *type1;
260 struct type *type2;
261
262 *arg1 = coerce_ref (*arg1);
263 *arg2 = coerce_ref (*arg2);
264
d0c97917
TT
265 type1 = check_typedef ((*arg1)->type ());
266 type2 = check_typedef ((*arg2)->type ());
4066e646 267
78134374
SM
268 if ((type1->code () != TYPE_CODE_FLT
269 && type1->code () != TYPE_CODE_DECFLOAT
4066e646 270 && !is_integral_type (type1))
78134374
SM
271 || (type2->code () != TYPE_CODE_FLT
272 && type2->code () != TYPE_CODE_DECFLOAT
4066e646
UW
273 && !is_integral_type (type2)))
274 return;
275
0a12719e 276 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
4d603089 277 return;
0a12719e 278
78134374
SM
279 if (type1->code () == TYPE_CODE_DECFLOAT
280 || type2->code () == TYPE_CODE_DECFLOAT)
4066e646
UW
281 {
282 /* No promotion required. */
283 }
78134374
SM
284 else if (type1->code () == TYPE_CODE_FLT
285 || type2->code () == TYPE_CODE_FLT)
4066e646
UW
286 {
287 switch (language->la_language)
288 {
289 case language_c:
290 case language_cplus:
291 case language_asm:
292 case language_objc:
f4b8a18d 293 case language_opencl:
4066e646
UW
294 /* No promotion required. */
295 break;
296
297 default:
298 /* For other languages the result type is unchanged from gdb
299 version 6.7 for backward compatibility.
300 If either arg was long double, make sure that value is also long
301 double. Otherwise use double. */
df86565b
SM
302 if (type1->length () * 8 > gdbarch_double_bit (gdbarch)
303 || type2->length () * 8 > gdbarch_double_bit (gdbarch))
4066e646
UW
304 promoted_type = builtin_type (gdbarch)->builtin_long_double;
305 else
306 promoted_type = builtin_type (gdbarch)->builtin_double;
307 break;
308 }
309 }
78134374
SM
310 else if (type1->code () == TYPE_CODE_BOOL
311 && type2->code () == TYPE_CODE_BOOL)
4066e646
UW
312 {
313 /* No promotion required. */
314 }
315 else
316 /* Integral operations here. */
317 /* FIXME: Also mixed integral/booleans, with result an integer. */
318 {
319 const struct builtin_type *builtin = builtin_type (gdbarch);
df86565b
SM
320 unsigned int promoted_len1 = type1->length ();
321 unsigned int promoted_len2 = type2->length ();
c6d940a9
SM
322 int is_unsigned1 = type1->is_unsigned ();
323 int is_unsigned2 = type2->is_unsigned ();
4066e646
UW
324 unsigned int result_len;
325 int unsigned_operation;
326
327 /* Determine type length and signedness after promotion for
dda83cd7 328 both operands. */
df86565b 329 if (promoted_len1 < builtin->builtin_int->length ())
4066e646
UW
330 {
331 is_unsigned1 = 0;
df86565b 332 promoted_len1 = builtin->builtin_int->length ();
4066e646 333 }
df86565b 334 if (promoted_len2 < builtin->builtin_int->length ())
4066e646
UW
335 {
336 is_unsigned2 = 0;
df86565b 337 promoted_len2 = builtin->builtin_int->length ();
4066e646
UW
338 }
339
340 if (promoted_len1 > promoted_len2)
341 {
342 unsigned_operation = is_unsigned1;
343 result_len = promoted_len1;
344 }
345 else if (promoted_len2 > promoted_len1)
346 {
347 unsigned_operation = is_unsigned2;
348 result_len = promoted_len2;
349 }
350 else
351 {
352 unsigned_operation = is_unsigned1 || is_unsigned2;
353 result_len = promoted_len1;
354 }
355
356 switch (language->la_language)
357 {
f4b8a18d 358 case language_opencl:
df86565b
SM
359 if (result_len
360 <= lookup_signed_typename (language, "int")->length())
f4b8a18d
KW
361 {
362 promoted_type =
363 (unsigned_operation
b858499d
SM
364 ? lookup_unsigned_typename (language, "int")
365 : lookup_signed_typename (language, "int"));
f4b8a18d 366 }
df86565b
SM
367 else if (result_len
368 <= lookup_signed_typename (language, "long")->length())
f4b8a18d
KW
369 {
370 promoted_type =
371 (unsigned_operation
b858499d
SM
372 ? lookup_unsigned_typename (language, "long")
373 : lookup_signed_typename (language,"long"));
f4b8a18d
KW
374 }
375 break;
4066e646 376 default:
eb52a497
TT
377 if (result_len <= builtin->builtin_int->length ())
378 {
379 promoted_type = (unsigned_operation
380 ? builtin->builtin_unsigned_int
381 : builtin->builtin_int);
382 }
383 else if (result_len <= builtin->builtin_long->length ())
384 {
385 promoted_type = (unsigned_operation
386 ? builtin->builtin_unsigned_long
387 : builtin->builtin_long);
388 }
389 else if (result_len <= builtin->builtin_long_long->length ())
4066e646 390 {
eb52a497
TT
391 promoted_type = (unsigned_operation
392 ? builtin->builtin_unsigned_long_long
393 : builtin->builtin_long_long);
4066e646
UW
394 }
395 else
396 {
eb52a497
TT
397 promoted_type = (unsigned_operation
398 ? builtin->builtin_uint128
399 : builtin->builtin_int128);
4066e646
UW
400 }
401 break;
402 }
403 }
404
405 if (promoted_type)
406 {
407 /* Promote both operands to common type. */
408 *arg1 = value_cast (promoted_type, *arg1);
409 *arg2 = value_cast (promoted_type, *arg2);
410 }
411}
412
89eef114 413static int
cc73bb8c 414ptrmath_type_p (const struct language_defn *lang, struct type *type)
89eef114
UW
415{
416 type = check_typedef (type);
aa006118 417 if (TYPE_IS_REFERENCE (type))
27710edb 418 type = type->target_type ();
89eef114 419
78134374 420 switch (type->code ())
89eef114
UW
421 {
422 case TYPE_CODE_PTR:
423 case TYPE_CODE_FUNC:
424 return 1;
425
426 case TYPE_CODE_ARRAY:
67bd3fd5 427 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
89eef114
UW
428
429 default:
430 return 0;
431 }
432}
433
c83833f4
PA
434/* Represents a fake method with the given parameter types. This is
435 used by the parser to construct a temporary "expected" type for
3693fdb3
PA
436 method overload resolution. FLAGS is used as instance flags of the
437 new type, in order to be able to make the new type represent a
438 const/volatile overload. */
072bba3b 439
c83833f4 440class fake_method
072bba3b 441{
c83833f4
PA
442public:
443 fake_method (type_instance_flags flags,
444 int num_types, struct type **param_types);
445 ~fake_method ();
446
447 /* The constructed type. */
448 struct type *type () { return &m_type; }
449
450private:
451 struct type m_type {};
452 main_type m_main_type {};
453};
454
455fake_method::fake_method (type_instance_flags flags,
456 int num_types, struct type **param_types)
457{
458 struct type *type = &m_type;
459
460 TYPE_MAIN_TYPE (type) = &m_main_type;
b6cdbc9a 461 type->set_length (1);
67607e24 462 type->set_code (TYPE_CODE_METHOD);
072bba3b 463 TYPE_CHAIN (type) = type;
314ad88d 464 type->set_instance_flags (flags);
e314d629 465 if (num_types > 0)
a6fb9c08 466 {
e314d629
TT
467 if (param_types[num_types - 1] == NULL)
468 {
469 --num_types;
1d6286ed 470 type->set_has_varargs (true);
e314d629 471 }
78134374 472 else if (check_typedef (param_types[num_types - 1])->code ()
e314d629
TT
473 == TYPE_CODE_VOID)
474 {
475 --num_types;
476 /* Caller should have ensured this. */
477 gdb_assert (num_types == 0);
27e69b7a 478 type->set_is_prototyped (true);
e314d629 479 }
a6fb9c08 480 }
e314d629 481
2fabdf33
AB
482 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
483 neither an objfile nor a gdbarch. As a result we must manually
484 allocate memory for auxiliary fields, and free the memory ourselves
485 when we are done with it. */
5e33d5f4 486 type->set_num_fields (num_types);
3cabb6b0
SM
487 type->set_fields
488 ((struct field *) xzalloc (sizeof (struct field) * num_types));
072bba3b
KS
489
490 while (num_types-- > 0)
5d14b6e5 491 type->field (num_types).set_type (param_types[num_types]);
c83833f4 492}
072bba3b 493
c83833f4
PA
494fake_method::~fake_method ()
495{
80fc5e77 496 xfree (m_type.fields ());
072bba3b
KS
497}
498
44b675c8
TT
499namespace expr
500{
501
502value *
503type_instance_operation::evaluate (struct type *expect_type,
504 struct expression *exp,
505 enum noside noside)
506{
507 type_instance_flags flags = std::get<0> (m_storage);
508 std::vector<type *> &types = std::get<1> (m_storage);
509
510 fake_method fake_expect_type (flags, types.size (), types.data ());
511 return std::get<2> (m_storage)->evaluate (fake_expect_type.type (),
512 exp, noside);
513}
514
515}
516
fe13dfec
PA
517/* Helper for evaluating an OP_VAR_VALUE. */
518
ced9779b 519value *
fe13dfec
PA
520evaluate_var_value (enum noside noside, const block *blk, symbol *var)
521{
ee7bb294 522 /* JYG: We used to just return value::zero of the symbol type if
fe13dfec
PA
523 we're asked to avoid side effects. Otherwise we return
524 value_of_variable (...). However I'm not sure if
525 value_of_variable () has any side effect. We need a full value
526 object returned here for whatis_exp () to call evaluate_type ()
527 and then pass the full value to value_rtti_target_type () if we
528 are dealing with a pointer or reference to a base class and print
529 object is on. */
530
531 struct value *ret = NULL;
532
a70b8144 533 try
fe13dfec
PA
534 {
535 ret = value_of_variable (var, blk);
536 }
537
230d2906 538 catch (const gdb_exception_error &except)
fe13dfec
PA
539 {
540 if (noside != EVAL_AVOID_SIDE_EFFECTS)
eedc3f4f 541 throw;
fe13dfec 542
ee7bb294 543 ret = value::zero (var->type (), not_lval);
fe13dfec 544 }
fe13dfec
PA
545
546 return ret;
547}
548
e82a5afc
TT
549namespace expr
550
551{
552
553value *
554var_value_operation::evaluate (struct type *expect_type,
555 struct expression *exp,
556 enum noside noside)
557{
9e5e03df 558 symbol *var = std::get<0> (m_storage).symbol;
5f9c5a63 559 if (var->type ()->code () == TYPE_CODE_ERROR)
e82a5afc 560 error_unknown_type (var->print_name ());
9e5e03df 561 return evaluate_var_value (noside, std::get<0> (m_storage).block, var);
e82a5afc
TT
562}
563
564} /* namespace expr */
565
74ea4be4
PA
566/* Helper for evaluating an OP_VAR_MSYM_VALUE. */
567
ced9779b 568value *
74ea4be4
PA
569evaluate_var_msym_value (enum noside noside,
570 struct objfile *objfile, minimal_symbol *msymbol)
571{
8388016d
PA
572 CORE_ADDR address;
573 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
574
0becda7a 575 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ())
ee7bb294 576 return value::zero (the_type, not_lval);
74ea4be4 577 else
8388016d 578 return value_at_lazy (the_type, address);
74ea4be4
PA
579}
580
6d816919 581/* See expression.h. */
e69570ee 582
6d816919
AB
583value *
584evaluate_subexp_do_call (expression *exp, enum noside noside,
1ab8280d
TT
585 value *callee,
586 gdb::array_view<value *> argvec,
6d816919
AB
587 const char *function_name,
588 type *default_return_type)
e69570ee 589{
1ab8280d 590 if (callee == NULL)
e69570ee
PA
591 error (_("Cannot evaluate function -- may be inlined"));
592 if (noside == EVAL_AVOID_SIDE_EFFECTS)
593 {
594 /* If the return type doesn't look like a function type,
595 call an error. This can happen if somebody tries to turn
596 a variable into a function call. */
597
d0c97917 598 type *ftype = callee->type ();
e69570ee 599
78134374 600 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
e69570ee
PA
601 {
602 /* We don't know anything about what the internal
603 function might return, but we have to return
604 something. */
ee7bb294 605 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
e69570ee
PA
606 not_lval);
607 }
78134374 608 else if (ftype->code () == TYPE_CODE_XMETHOD)
e69570ee 609 {
6bd5c754 610 type *return_type = callee->result_type_of_xmethod (argvec);
e69570ee
PA
611
612 if (return_type == NULL)
613 error (_("Xmethod is missing return type."));
ee7bb294 614 return value::zero (return_type, not_lval);
e69570ee 615 }
78134374
SM
616 else if (ftype->code () == TYPE_CODE_FUNC
617 || ftype->code () == TYPE_CODE_METHOD)
e69570ee 618 {
0becda7a 619 if (ftype->is_gnu_ifunc ())
8388016d 620 {
9feb2d07 621 CORE_ADDR address = callee->address ();
8388016d
PA
622 type *resolved_type = find_gnu_ifunc_target_type (address);
623
624 if (resolved_type != NULL)
625 ftype = resolved_type;
626 }
627
27710edb 628 type *return_type = ftype->target_type ();
e69570ee
PA
629
630 if (return_type == NULL)
631 return_type = default_return_type;
632
633 if (return_type == NULL)
634 error_call_unknown_return_type (function_name);
635
317c3ed9 636 return value::allocate (return_type);
e69570ee
PA
637 }
638 else
639 error (_("Expression of type other than "
640 "\"Function returning ...\" used as function"));
641 }
d0c97917 642 switch (callee->type ()->code ())
e69570ee
PA
643 {
644 case TYPE_CODE_INTERNAL_FUNCTION:
645 return call_internal_function (exp->gdbarch, exp->language_defn,
1ab8280d 646 callee, argvec.size (), argvec.data ());
e69570ee 647 case TYPE_CODE_XMETHOD:
6bd5c754 648 return callee->call_xmethod (argvec);
e69570ee 649 default:
1ab8280d 650 return call_function_by_hand (callee, default_return_type, argvec);
e69570ee
PA
651 }
652}
653
a00b7254
TT
654namespace expr
655{
656
657value *
658operation::evaluate_funcall (struct type *expect_type,
659 struct expression *exp,
660 enum noside noside,
661 const char *function_name,
662 const std::vector<operation_up> &args)
663{
664 std::vector<value *> vals (args.size ());
665
666 value *callee = evaluate_with_coercion (exp, noside);
d0c97917 667 struct type *type = callee->type ();
ccdc02ed 668 if (type->code () == TYPE_CODE_PTR)
27710edb 669 type = type->target_type ();
a00b7254 670 for (int i = 0; i < args.size (); ++i)
ccdc02ed
TT
671 {
672 if (i < type->num_fields ())
673 vals[i] = args[i]->evaluate (type->field (i).type (), exp, noside);
674 else
675 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
676 }
a00b7254
TT
677
678 return evaluate_subexp_do_call (exp, noside, callee, vals,
679 function_name, expect_type);
680}
681
682value *
683var_value_operation::evaluate_funcall (struct type *expect_type,
684 struct expression *exp,
685 enum noside noside,
686 const std::vector<operation_up> &args)
687{
688 if (!overload_resolution
689 || exp->language_defn->la_language != language_cplus)
690 return operation::evaluate_funcall (expect_type, exp, noside, args);
691
692 std::vector<value *> argvec (args.size ());
693 for (int i = 0; i < args.size (); ++i)
694 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
695
696 struct symbol *symp;
697 find_overload_match (argvec, NULL, NON_METHOD,
9e5e03df 698 NULL, std::get<0> (m_storage).symbol,
a00b7254
TT
699 NULL, &symp, NULL, 0, noside);
700
5f9c5a63 701 if (symp->type ()->code () == TYPE_CODE_ERROR)
a00b7254 702 error_unknown_type (symp->print_name ());
9e5e03df
TT
703 value *callee = evaluate_var_value (noside, std::get<0> (m_storage).block,
704 symp);
a00b7254
TT
705
706 return evaluate_subexp_do_call (exp, noside, callee, argvec,
707 nullptr, expect_type);
708}
709
710value *
711scope_operation::evaluate_funcall (struct type *expect_type,
712 struct expression *exp,
713 enum noside noside,
714 const std::vector<operation_up> &args)
715{
716 if (!overload_resolution
717 || exp->language_defn->la_language != language_cplus)
718 return operation::evaluate_funcall (expect_type, exp, noside, args);
719
720 /* Unpack it locally so we can properly handle overload
721 resolution. */
722 const std::string &name = std::get<1> (m_storage);
723 struct type *type = std::get<0> (m_storage);
724
725 symbol *function = NULL;
726 const char *function_name = NULL;
727 std::vector<value *> argvec (1 + args.size ());
728 if (type->code () == TYPE_CODE_NAMESPACE)
729 {
730 function = cp_lookup_symbol_namespace (type->name (),
731 name.c_str (),
732 get_selected_block (0),
733 VAR_DOMAIN).symbol;
734 if (function == NULL)
735 error (_("No symbol \"%s\" in namespace \"%s\"."),
736 name.c_str (), type->name ());
737 }
738 else
739 {
740 gdb_assert (type->code () == TYPE_CODE_STRUCT
741 || type->code () == TYPE_CODE_UNION);
742 function_name = name.c_str ();
743
744 /* We need a properly typed value for method lookup. */
ee7bb294 745 argvec[0] = value::zero (type, lval_memory);
a00b7254
TT
746 }
747
748 for (int i = 0; i < args.size (); ++i)
749 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
750 gdb::array_view<value *> arg_view = argvec;
751
752 value *callee = nullptr;
753 if (function_name != nullptr)
754 {
755 int static_memfuncp;
756
757 find_overload_match (arg_view, function_name, METHOD,
758 &argvec[0], nullptr, &callee, nullptr,
759 &static_memfuncp, 0, noside);
760 if (!static_memfuncp)
761 {
762 /* For the time being, we don't handle this. */
763 error (_("Call to overloaded function %s requires "
764 "`this' pointer"),
765 function_name);
766 }
767
768 arg_view = arg_view.slice (1);
769 }
770 else
771 {
772 symbol *symp;
773 arg_view = arg_view.slice (1);
774 find_overload_match (arg_view, nullptr,
775 NON_METHOD, nullptr, function,
776 nullptr, &symp, nullptr, 1, noside);
777 callee = value_of_variable (symp, get_selected_block (0));
778 }
779
780 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
781 nullptr, expect_type);
782}
783
784value *
785structop_member_base::evaluate_funcall (struct type *expect_type,
786 struct expression *exp,
787 enum noside noside,
788 const std::vector<operation_up> &args)
789{
790 /* First, evaluate the structure into lhs. */
791 value *lhs;
792 if (opcode () == STRUCTOP_MEMBER)
793 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
794 else
795 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
796
797 std::vector<value *> vals (args.size () + 1);
798 gdb::array_view<value *> val_view = vals;
799 /* If the function is a virtual function, then the aggregate
800 value (providing the structure) plays its part by providing
801 the vtable. Otherwise, it is just along for the ride: call
802 the function directly. */
803 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
804 value *callee;
805
d0c97917 806 type *a1_type = check_typedef (rhs->type ());
a00b7254
TT
807 if (a1_type->code () == TYPE_CODE_METHODPTR)
808 {
809 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 810 callee = value::zero (a1_type->target_type (), not_lval);
a00b7254
TT
811 else
812 callee = cplus_method_ptr_to_value (&lhs, rhs);
813
814 vals[0] = lhs;
815 }
816 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
817 {
818 struct type *type_ptr
819 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
820 struct type *target_type_ptr
27710edb 821 = lookup_pointer_type (a1_type->target_type ());
a00b7254
TT
822
823 /* Now, convert this value to an address. */
824 lhs = value_cast (type_ptr, lhs);
825
826 long mem_offset = value_as_long (rhs);
827
828 callee = value_from_pointer (target_type_ptr,
829 value_as_long (lhs) + mem_offset);
830 callee = value_ind (callee);
831
832 val_view = val_view.slice (1);
833 }
834 else
835 error (_("Non-pointer-to-member value used in pointer-to-member "
836 "construct"));
837
838 for (int i = 0; i < args.size (); ++i)
839 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
840
841 return evaluate_subexp_do_call (exp, noside, callee, val_view,
842 nullptr, expect_type);
843
844}
845
846value *
847structop_base_operation::evaluate_funcall
848 (struct type *expect_type, struct expression *exp, enum noside noside,
849 const std::vector<operation_up> &args)
850{
13221aec
AB
851 /* Allocate space for the function call arguments, Including space for a
852 `this' pointer at the start. */
853 std::vector<value *> vals (args.size () + 1);
a00b7254
TT
854 /* First, evaluate the structure into vals[0]. */
855 enum exp_opcode op = opcode ();
856 if (op == STRUCTOP_STRUCT)
857 {
858 /* If v is a variable in a register, and the user types
859 v.method (), this will produce an error, because v has no
860 address.
861
862 A possible way around this would be to allocate a copy of
863 the variable on the stack, copy in the contents, call the
864 function, and copy out the contents. I.e. convert this
865 from call by reference to call by copy-return (or
866 whatever it's called). However, this does not work
867 because it is not the same: the method being called could
868 stash a copy of the address, and then future uses through
869 that address (after the method returns) would be expected
870 to use the variable itself, not some copy of it. */
871 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
872 }
873 else
874 {
875 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
876 /* Check to see if the operator '->' has been overloaded.
877 If the operator has been overloaded replace vals[0] with the
878 value returned by the custom operator and continue
879 evaluation. */
880 while (unop_user_defined_p (op, vals[0]))
881 {
882 struct value *value = nullptr;
883 try
884 {
885 value = value_x_unop (vals[0], op, noside);
886 }
887 catch (const gdb_exception_error &except)
888 {
889 if (except.error == NOT_FOUND_ERROR)
890 break;
891 else
892 throw;
893 }
894
895 vals[0] = value;
896 }
897 }
898
13221aec
AB
899 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
900 pointer we placed into vals[0]. */
a00b7254
TT
901 for (int i = 0; i < args.size (); ++i)
902 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
79bd4d34 903
13221aec
AB
904 /* The array view includes the `this' pointer. */
905 gdb::array_view<value *> arg_view (vals);
a00b7254
TT
906
907 int static_memfuncp;
908 value *callee;
909 const char *tstr = std::get<1> (m_storage).c_str ();
910 if (overload_resolution
911 && exp->language_defn->la_language == language_cplus)
912 {
913 /* Language is C++, do some overload resolution before
914 evaluation. */
915 value *val0 = vals[0];
916 find_overload_match (arg_view, tstr, METHOD,
917 &val0, nullptr, &callee, nullptr,
918 &static_memfuncp, 0, noside);
919 vals[0] = val0;
920 }
921 else
922 /* Non-C++ case -- or no overload resolution. */
923 {
924 struct value *temp = vals[0];
925
158cc4fe 926 callee = value_struct_elt (&temp, arg_view, tstr,
a00b7254
TT
927 &static_memfuncp,
928 op == STRUCTOP_STRUCT
929 ? "structure" : "structure pointer");
930 /* value_struct_elt updates temp with the correct value of the
931 ``this'' pointer if necessary, so modify it to reflect any
932 ``this'' changes. */
d0c97917 933 vals[0] = value_from_longest (lookup_pointer_type (temp->type ()),
9feb2d07 934 temp->address ()
391f8628 935 + temp->embedded_offset ());
a00b7254
TT
936 }
937
938 /* Take out `this' if needed. */
939 if (static_memfuncp)
940 arg_view = arg_view.slice (1);
941
942 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
943 nullptr, expect_type);
944}
945
1e237aba
TT
946/* Helper for structop_base_operation::complete which recursively adds
947 field and method names from TYPE, a struct or union type, to the
d4da1b2c 948 OUTPUT list. PREFIX is prepended to each result. */
1e237aba
TT
949
950static void
951add_struct_fields (struct type *type, completion_list &output,
d4da1b2c 952 const char *fieldname, int namelen, const char *prefix)
1e237aba
TT
953{
954 int i;
955 int computed_type_name = 0;
956 const char *type_name = NULL;
957
958 type = check_typedef (type);
959 for (i = 0; i < type->num_fields (); ++i)
960 {
961 if (i < TYPE_N_BASECLASSES (type))
962 add_struct_fields (TYPE_BASECLASS (type, i),
d4da1b2c 963 output, fieldname, namelen, prefix);
1e237aba
TT
964 else if (type->field (i).name ())
965 {
966 if (type->field (i).name ()[0] != '\0')
967 {
968 if (! strncmp (type->field (i).name (),
969 fieldname, namelen))
d4da1b2c
TT
970 output.emplace_back (concat (prefix, type->field (i).name (),
971 nullptr));
1e237aba
TT
972 }
973 else if (type->field (i).type ()->code () == TYPE_CODE_UNION)
974 {
975 /* Recurse into anonymous unions. */
976 add_struct_fields (type->field (i).type (),
d4da1b2c 977 output, fieldname, namelen, prefix);
1e237aba
TT
978 }
979 }
980 }
981
982 for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
983 {
984 const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
985
986 if (name && ! strncmp (name, fieldname, namelen))
987 {
988 if (!computed_type_name)
989 {
990 type_name = type->name ();
991 computed_type_name = 1;
992 }
993 /* Omit constructors from the completion list. */
994 if (!type_name || strcmp (type_name, name))
d4da1b2c 995 output.emplace_back (concat (prefix, name, nullptr));
1e237aba
TT
996 }
997 }
998}
999
1000/* See expop.h. */
1001
1002bool
1003structop_base_operation::complete (struct expression *exp,
d4da1b2c
TT
1004 completion_tracker &tracker,
1005 const char *prefix)
1e237aba
TT
1006{
1007 const std::string &fieldname = std::get<1> (m_storage);
1008
1009 value *lhs = std::get<0> (m_storage)->evaluate (nullptr, exp,
1010 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 1011 struct type *type = lhs->type ();
1e237aba
TT
1012 for (;;)
1013 {
1014 type = check_typedef (type);
1015 if (!type->is_pointer_or_reference ())
1016 break;
27710edb 1017 type = type->target_type ();
1e237aba
TT
1018 }
1019
1020 if (type->code () == TYPE_CODE_UNION
1021 || type->code () == TYPE_CODE_STRUCT)
1022 {
1023 completion_list result;
1024
1025 add_struct_fields (type, result, fieldname.c_str (),
d4da1b2c 1026 fieldname.length (), prefix);
1e237aba
TT
1027 tracker.add_completions (std::move (result));
1028 return true;
1029 }
1030
1031 return false;
1032}
a00b7254
TT
1033
1034} /* namespace expr */
1035
60e22c1e
HD
1036/* Return true if type is integral or reference to integral */
1037
1038static bool
1039is_integral_or_integral_reference (struct type *type)
1040{
1041 if (is_integral_type (type))
1042 return true;
1043
1044 type = check_typedef (type);
1045 return (type != nullptr
1046 && TYPE_IS_REFERENCE (type)
27710edb 1047 && is_integral_type (type->target_type ()));
60e22c1e
HD
1048}
1049
ea2d29f7
TT
1050/* Helper function that implements the body of OP_SCOPE. */
1051
d5ab122c 1052struct value *
ea2d29f7
TT
1053eval_op_scope (struct type *expect_type, struct expression *exp,
1054 enum noside noside,
1055 struct type *type, const char *string)
1056{
ea2d29f7
TT
1057 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
1058 0, noside);
1059 if (arg1 == NULL)
1060 error (_("There is no field named %s"), string);
1061 return arg1;
1062}
1063
50b98adc
TT
1064/* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1065
b5cc3923 1066struct value *
50b98adc
TT
1067eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
1068 enum noside noside, symbol *sym)
1069{
50b98adc 1070 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 1071 return value::zero (sym->type (), not_lval);
50b98adc
TT
1072
1073 if (SYMBOL_COMPUTED_OPS (sym) == NULL
1074 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
1075 error (_("Symbol \"%s\" does not have any specific entry value"),
1076 sym->print_name ());
1077
bd2b40ac 1078 frame_info_ptr frame = get_selected_frame (NULL);
50b98adc
TT
1079 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1080}
1081
c0df9289
TT
1082/* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1083
0c8effa3 1084struct value *
c0df9289
TT
1085eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1086 enum noside noside, bool outermost_p,
9c79936b 1087 bound_minimal_symbol msymbol)
c0df9289 1088{
9c79936b
TT
1089 value *val = evaluate_var_msym_value (noside, msymbol.objfile,
1090 msymbol.minsym);
c0df9289 1091
d0c97917 1092 struct type *type = val->type ();
c0df9289
TT
1093 if (type->code () == TYPE_CODE_ERROR
1094 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
9c79936b 1095 error_unknown_type (msymbol.minsym->print_name ());
c0df9289
TT
1096 return val;
1097}
1098
9b1d8af6
TT
1099/* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1100
17679395 1101struct value *
9b1d8af6
TT
1102eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1103 enum noside noside,
1104 value *func, const char *var)
1105{
9feb2d07 1106 CORE_ADDR addr = func->address ();
9b1d8af6
TT
1107 const block *blk = block_for_pc (addr);
1108 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1109 if (sym.symbol == NULL)
1110 error (_("No symbol \"%s\" in specified context."), var);
1111 return evaluate_var_value (noside, sym.block, sym.symbol);
1112}
1113
ffff730b
TT
1114/* Helper function that implements the body of OP_REGISTER. */
1115
55bdbff8 1116struct value *
ffff730b
TT
1117eval_op_register (struct type *expect_type, struct expression *exp,
1118 enum noside noside, const char *name)
1119{
1120 int regno;
1121 struct value *val;
1122
1123 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1124 name, strlen (name));
1125 if (regno == -1)
1126 error (_("Register $%s not available."), name);
1127
1128 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1129 a value with the appropriate register type. Unfortunately,
1130 we don't have easy access to the type of user registers.
1131 So for these registers, we fetch the register value regardless
1132 of the evaluation mode. */
1133 if (noside == EVAL_AVOID_SIDE_EFFECTS
1134 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
ee7bb294 1135 val = value::zero (register_type (exp->gdbarch, regno), not_lval);
ffff730b 1136 else
a7952927
SM
1137 val = value_of_register
1138 (regno, get_next_frame_sentinel_okay (get_selected_frame ()));
ffff730b
TT
1139 if (val == NULL)
1140 error (_("Value of register %s not available."), name);
1141 else
1142 return val;
1143}
1144
16b6c361
TT
1145namespace expr
1146{
14a1c64a 1147
16b6c361
TT
1148value *
1149string_operation::evaluate (struct type *expect_type,
1150 struct expression *exp,
1151 enum noside noside)
14a1c64a 1152{
16b6c361 1153 const std::string &str = std::get<0> (m_storage);
14a1c64a
TT
1154 struct type *type = language_string_char_type (exp->language_defn,
1155 exp->gdbarch);
16b6c361 1156 return value_string (str.c_str (), str.size (), type);
14a1c64a
TT
1157}
1158
4bea97df
TT
1159struct value *
1160ternop_slice_operation::evaluate (struct type *expect_type,
1161 struct expression *exp,
1162 enum noside noside)
1163{
1164 struct value *array
1165 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
1166 struct value *low
1167 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
1168 struct value *upper
1169 = std::get<2> (m_storage)->evaluate (nullptr, exp, noside);
1170
1171 int lowbound = value_as_long (low);
1172 int upperbound = value_as_long (upper);
1173 return value_slice (array, lowbound, upperbound - lowbound + 1);
1174}
1175
16b6c361
TT
1176} /* namespace expr */
1177
f871bae1
TT
1178/* Helper function that implements the body of OP_OBJC_SELECTOR. */
1179
09db3700 1180struct value *
f871bae1
TT
1181eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1182 enum noside noside,
1183 const char *sel)
1184{
f871bae1
TT
1185 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1186 return value_from_longest (selector_type,
1187 lookup_child_selector (exp->gdbarch, sel));
1188}
1189
3e96c4fc
TT
1190/* A helper function for STRUCTOP_STRUCT. */
1191
808b22cf 1192struct value *
3e96c4fc
TT
1193eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1194 enum noside noside,
1195 struct value *arg1, const char *string)
1196{
158cc4fe 1197 struct value *arg3 = value_struct_elt (&arg1, {}, string,
3e96c4fc
TT
1198 NULL, "structure");
1199 if (noside == EVAL_AVOID_SIDE_EFFECTS)
736355f2 1200 arg3 = value::zero (arg3->type (), arg3->lval ());
3e96c4fc
TT
1201 return arg3;
1202}
1203
fb461aa3
TT
1204/* A helper function for STRUCTOP_PTR. */
1205
ab0609be 1206struct value *
fb461aa3 1207eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
ab0609be 1208 enum noside noside,
fb461aa3
TT
1209 struct value *arg1, const char *string)
1210{
fb461aa3
TT
1211 /* Check to see if operator '->' has been overloaded. If so replace
1212 arg1 with the value returned by evaluating operator->(). */
ab0609be 1213 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
fb461aa3
TT
1214 {
1215 struct value *value = NULL;
1216 try
1217 {
ab0609be 1218 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
fb461aa3
TT
1219 }
1220
1221 catch (const gdb_exception_error &except)
1222 {
1223 if (except.error == NOT_FOUND_ERROR)
1224 break;
1225 else
1226 throw;
1227 }
1228
1229 arg1 = value;
1230 }
1231
1232 /* JYG: if print object is on we need to replace the base type
1233 with rtti type in order to continue on with successful
1234 lookup of member / method only available in the rtti type. */
1235 {
d0c97917 1236 struct type *arg_type = arg1->type ();
fb461aa3
TT
1237 struct type *real_type;
1238 int full, using_enc;
1239 LONGEST top;
1240 struct value_print_options opts;
1241
1242 get_user_print_options (&opts);
27710edb
SM
1243 if (opts.objectprint && arg_type->target_type ()
1244 && (arg_type->target_type ()->code () == TYPE_CODE_STRUCT))
fb461aa3
TT
1245 {
1246 real_type = value_rtti_indirect_type (arg1, &full, &top,
1247 &using_enc);
1248 if (real_type)
1249 arg1 = value_cast (real_type, arg1);
1250 }
1251 }
1252
158cc4fe 1253 struct value *arg3 = value_struct_elt (&arg1, {}, string,
fb461aa3
TT
1254 NULL, "structure pointer");
1255 if (noside == EVAL_AVOID_SIDE_EFFECTS)
736355f2 1256 arg3 = value::zero (arg3->type (), arg3->lval ());
fb461aa3
TT
1257 return arg3;
1258}
1259
b7a96ed2
TT
1260/* A helper function for STRUCTOP_MEMBER. */
1261
07f724a8 1262struct value *
b7a96ed2
TT
1263eval_op_member (struct type *expect_type, struct expression *exp,
1264 enum noside noside,
1265 struct value *arg1, struct value *arg2)
1266{
1267 long mem_offset;
1268
b7a96ed2 1269 struct value *arg3;
d0c97917 1270 struct type *type = check_typedef (arg2->type ());
b7a96ed2
TT
1271 switch (type->code ())
1272 {
1273 case TYPE_CODE_METHODPTR:
1274 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 1275 return value::zero (type->target_type (), not_lval);
b7a96ed2
TT
1276 else
1277 {
1278 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
d0c97917 1279 gdb_assert (arg2->type ()->code () == TYPE_CODE_PTR);
b7a96ed2
TT
1280 return value_ind (arg2);
1281 }
1282
1283 case TYPE_CODE_MEMBERPTR:
1284 /* Now, convert these values to an address. */
d0c97917 1285 if (check_typedef (arg1->type ())->code () != TYPE_CODE_PTR)
bf716a53 1286 arg1 = value_addr (arg1);
b7a96ed2
TT
1287 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1288 arg1, 1);
1289
1290 mem_offset = value_as_long (arg2);
1291
27710edb 1292 arg3 = value_from_pointer (lookup_pointer_type (type->target_type ()),
b7a96ed2
TT
1293 value_as_long (arg1) + mem_offset);
1294 return value_ind (arg3);
1295
1296 default:
1297 error (_("non-pointer-to-member value used "
1298 "in pointer-to-member construct"));
1299 }
1300}
1301
aedaf9ac
TT
1302/* A helper function for BINOP_ADD. */
1303
a94323b6 1304struct value *
aedaf9ac 1305eval_op_add (struct type *expect_type, struct expression *exp,
a94323b6 1306 enum noside noside,
aedaf9ac
TT
1307 struct value *arg1, struct value *arg2)
1308{
a94323b6
TT
1309 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1310 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
d0c97917
TT
1311 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1312 && is_integral_or_integral_reference (arg2->type ()))
aedaf9ac 1313 return value_ptradd (arg1, value_as_long (arg2));
d0c97917
TT
1314 else if (ptrmath_type_p (exp->language_defn, arg2->type ())
1315 && is_integral_or_integral_reference (arg1->type ()))
aedaf9ac
TT
1316 return value_ptradd (arg2, value_as_long (arg1));
1317 else
1318 {
1319 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1320 return value_binop (arg1, arg2, BINOP_ADD);
1321 }
1322}
1323
d9790e22
TT
1324/* A helper function for BINOP_SUB. */
1325
5133d78b 1326struct value *
d9790e22 1327eval_op_sub (struct type *expect_type, struct expression *exp,
5133d78b 1328 enum noside noside,
d9790e22
TT
1329 struct value *arg1, struct value *arg2)
1330{
5133d78b
TT
1331 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1332 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
d0c97917
TT
1333 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1334 && ptrmath_type_p (exp->language_defn, arg2->type ()))
d9790e22
TT
1335 {
1336 /* FIXME -- should be ptrdiff_t */
1337 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1338 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1339 }
d0c97917
TT
1340 else if (ptrmath_type_p (exp->language_defn, arg1->type ())
1341 && is_integral_or_integral_reference (arg2->type ()))
d9790e22
TT
1342 return value_ptradd (arg1, - value_as_long (arg2));
1343 else
1344 {
1345 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1346 return value_binop (arg1, arg2, BINOP_SUB);
1347 }
1348}
1349
7cdcdd02
TT
1350/* Helper function for several different binary operations. */
1351
373907ff 1352struct value *
7cdcdd02
TT
1353eval_op_binary (struct type *expect_type, struct expression *exp,
1354 enum noside noside, enum exp_opcode op,
1355 struct value *arg1, struct value *arg2)
1356{
7cdcdd02
TT
1357 if (binop_user_defined_p (op, arg1, arg2))
1358 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1359 else
1360 {
1361 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1362 fudge arg2 to avoid division-by-zero, the caller is
1363 (theoretically) only looking for the type of the result. */
1364 if (noside == EVAL_AVOID_SIDE_EFFECTS
1365 /* ??? Do we really want to test for BINOP_MOD here?
1366 The implementation of value_binop gives it a well-defined
1367 value. */
1368 && (op == BINOP_DIV
1369 || op == BINOP_INTDIV
1370 || op == BINOP_REM
1371 || op == BINOP_MOD)
1372 && value_logical_not (arg2))
1373 {
1374 struct value *v_one;
1375
d0c97917 1376 v_one = value_one (arg2->type ());
7cdcdd02
TT
1377 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1378 return value_binop (arg1, v_one, op);
1379 }
1380 else
1381 {
1382 /* For shift and integer exponentiation operations,
1383 only promote the first argument. */
1384 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
d0c97917 1385 && is_integral_type (arg2->type ()))
7cdcdd02
TT
1386 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1387 else
1388 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1389
1390 return value_binop (arg1, arg2, op);
1391 }
1392 }
1393}
1394
288d26bc
TT
1395/* A helper function for BINOP_SUBSCRIPT. */
1396
224d6424 1397struct value *
288d26bc
TT
1398eval_op_subscript (struct type *expect_type, struct expression *exp,
1399 enum noside noside, enum exp_opcode op,
1400 struct value *arg1, struct value *arg2)
1401{
288d26bc
TT
1402 if (binop_user_defined_p (op, arg1, arg2))
1403 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1404 else
1405 {
1406 /* If the user attempts to subscript something that is not an
1407 array or pointer type (like a plain int variable for example),
1408 then report this as an error. */
1409
1410 arg1 = coerce_ref (arg1);
d0c97917 1411 struct type *type = check_typedef (arg1->type ());
288d26bc
TT
1412 if (type->code () != TYPE_CODE_ARRAY
1413 && type->code () != TYPE_CODE_PTR)
1414 {
1415 if (type->name ())
1416 error (_("cannot subscript something of type `%s'"),
1417 type->name ());
1418 else
1419 error (_("cannot subscript requested type"));
1420 }
1421
1422 if (noside == EVAL_AVOID_SIDE_EFFECTS)
736355f2 1423 return value::zero (type->target_type (), arg1->lval ());
288d26bc
TT
1424 else
1425 return value_subscript (arg1, value_as_long (arg2));
1426 }
1427}
1428
0cc96de8
TT
1429/* A helper function for BINOP_EQUAL. */
1430
46916f2b 1431struct value *
0cc96de8
TT
1432eval_op_equal (struct type *expect_type, struct expression *exp,
1433 enum noside noside, enum exp_opcode op,
1434 struct value *arg1, struct value *arg2)
1435{
0cc96de8
TT
1436 if (binop_user_defined_p (op, arg1, arg2))
1437 {
1438 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1439 }
1440 else
1441 {
1442 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1443 int tem = value_equal (arg1, arg2);
1444 struct type *type = language_bool_type (exp->language_defn,
1445 exp->gdbarch);
1446 return value_from_longest (type, (LONGEST) tem);
1447 }
1448}
1449
1fcb3559
TT
1450/* A helper function for BINOP_NOTEQUAL. */
1451
46916f2b 1452struct value *
1fcb3559
TT
1453eval_op_notequal (struct type *expect_type, struct expression *exp,
1454 enum noside noside, enum exp_opcode op,
1455 struct value *arg1, struct value *arg2)
1456{
1fcb3559
TT
1457 if (binop_user_defined_p (op, arg1, arg2))
1458 {
1459 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1460 }
1461 else
1462 {
1463 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1464 int tem = value_equal (arg1, arg2);
1465 struct type *type = language_bool_type (exp->language_defn,
1466 exp->gdbarch);
1467 return value_from_longest (type, (LONGEST) ! tem);
1468 }
1469}
1470
6cad1349
TT
1471/* A helper function for BINOP_LESS. */
1472
46916f2b 1473struct value *
6cad1349
TT
1474eval_op_less (struct type *expect_type, struct expression *exp,
1475 enum noside noside, enum exp_opcode op,
1476 struct value *arg1, struct value *arg2)
1477{
6cad1349
TT
1478 if (binop_user_defined_p (op, arg1, arg2))
1479 {
1480 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1481 }
1482 else
1483 {
1484 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1485 int tem = value_less (arg1, arg2);
1486 struct type *type = language_bool_type (exp->language_defn,
1487 exp->gdbarch);
1488 return value_from_longest (type, (LONGEST) tem);
1489 }
1490}
1491
1f78d732
TT
1492/* A helper function for BINOP_GTR. */
1493
46916f2b 1494struct value *
1f78d732
TT
1495eval_op_gtr (struct type *expect_type, struct expression *exp,
1496 enum noside noside, enum exp_opcode op,
1497 struct value *arg1, struct value *arg2)
1498{
1f78d732
TT
1499 if (binop_user_defined_p (op, arg1, arg2))
1500 {
1501 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1502 }
1503 else
1504 {
1505 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1506 int tem = value_less (arg2, arg1);
1507 struct type *type = language_bool_type (exp->language_defn,
1508 exp->gdbarch);
1509 return value_from_longest (type, (LONGEST) tem);
1510 }
1511}
1512
96e3efd9
TT
1513/* A helper function for BINOP_GEQ. */
1514
46916f2b 1515struct value *
96e3efd9
TT
1516eval_op_geq (struct type *expect_type, struct expression *exp,
1517 enum noside noside, enum exp_opcode op,
1518 struct value *arg1, struct value *arg2)
1519{
96e3efd9
TT
1520 if (binop_user_defined_p (op, arg1, arg2))
1521 {
1522 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1523 }
1524 else
1525 {
1526 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1527 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1528 struct type *type = language_bool_type (exp->language_defn,
1529 exp->gdbarch);
1530 return value_from_longest (type, (LONGEST) tem);
1531 }
1532}
1533
60cdd487
TT
1534/* A helper function for BINOP_LEQ. */
1535
46916f2b 1536struct value *
60cdd487
TT
1537eval_op_leq (struct type *expect_type, struct expression *exp,
1538 enum noside noside, enum exp_opcode op,
1539 struct value *arg1, struct value *arg2)
1540{
60cdd487
TT
1541 if (binop_user_defined_p (op, arg1, arg2))
1542 {
1543 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1544 }
1545 else
1546 {
1547 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1548 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1549 struct type *type = language_bool_type (exp->language_defn,
1550 exp->gdbarch);
1551 return value_from_longest (type, (LONGEST) tem);
1552 }
1553}
1554
eed70b1c
TT
1555/* A helper function for BINOP_REPEAT. */
1556
d4eff4c1 1557struct value *
eed70b1c 1558eval_op_repeat (struct type *expect_type, struct expression *exp,
d4eff4c1 1559 enum noside noside, enum exp_opcode op,
eed70b1c
TT
1560 struct value *arg1, struct value *arg2)
1561{
d0c97917 1562 struct type *type = check_typedef (arg2->type ());
eed70b1c
TT
1563 if (type->code () != TYPE_CODE_INT
1564 && type->code () != TYPE_CODE_ENUM)
1565 error (_("Non-integral right operand for \"@\" operator."));
1566 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1567 {
d0c97917 1568 return allocate_repeat_value (arg1->type (),
eed70b1c
TT
1569 longest_to_int (value_as_long (arg2)));
1570 }
1571 else
1572 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1573}
1574
39f288be
TT
1575/* A helper function for UNOP_PLUS. */
1576
9307d17b 1577struct value *
39f288be
TT
1578eval_op_plus (struct type *expect_type, struct expression *exp,
1579 enum noside noside, enum exp_opcode op,
1580 struct value *arg1)
1581{
39f288be
TT
1582 if (unop_user_defined_p (op, arg1))
1583 return value_x_unop (arg1, op, noside);
1584 else
1585 {
1586 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1587 return value_pos (arg1);
1588 }
1589}
1590
606d105f
TT
1591/* A helper function for UNOP_NEG. */
1592
9307d17b 1593struct value *
606d105f
TT
1594eval_op_neg (struct type *expect_type, struct expression *exp,
1595 enum noside noside, enum exp_opcode op,
1596 struct value *arg1)
1597{
606d105f
TT
1598 if (unop_user_defined_p (op, arg1))
1599 return value_x_unop (arg1, op, noside);
1600 else
1601 {
1602 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1603 return value_neg (arg1);
1604 }
1605}
1606
1f09ec81
TT
1607/* A helper function for UNOP_COMPLEMENT. */
1608
9307d17b 1609struct value *
1f09ec81
TT
1610eval_op_complement (struct type *expect_type, struct expression *exp,
1611 enum noside noside, enum exp_opcode op,
1612 struct value *arg1)
1613{
1f09ec81
TT
1614 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1615 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1616 else
1617 {
1618 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1619 return value_complement (arg1);
1620 }
1621}
1622
24338fb9
TT
1623/* A helper function for UNOP_LOGICAL_NOT. */
1624
9307d17b 1625struct value *
24338fb9
TT
1626eval_op_lognot (struct type *expect_type, struct expression *exp,
1627 enum noside noside, enum exp_opcode op,
1628 struct value *arg1)
1629{
24338fb9
TT
1630 if (unop_user_defined_p (op, arg1))
1631 return value_x_unop (arg1, op, noside);
1632 else
1633 {
1634 struct type *type = language_bool_type (exp->language_defn,
1635 exp->gdbarch);
1636 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1637 }
1638}
1639
786f70ee
TT
1640/* A helper function for UNOP_IND. */
1641
876469ff 1642struct value *
786f70ee 1643eval_op_ind (struct type *expect_type, struct expression *exp,
876469ff 1644 enum noside noside,
786f70ee
TT
1645 struct value *arg1)
1646{
d0c97917 1647 struct type *type = check_typedef (arg1->type ());
786f70ee
TT
1648 if (type->code () == TYPE_CODE_METHODPTR
1649 || type->code () == TYPE_CODE_MEMBERPTR)
1650 error (_("Attempt to dereference pointer "
1651 "to member without an object"));
876469ff
TT
1652 if (unop_user_defined_p (UNOP_IND, arg1))
1653 return value_x_unop (arg1, UNOP_IND, noside);
786f70ee
TT
1654 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1655 {
d0c97917 1656 type = check_typedef (arg1->type ());
786f70ee
TT
1657
1658 /* If the type pointed to is dynamic then in order to resolve the
1659 dynamic properties we must actually dereference the pointer.
1660 There is a risk that this dereference will have side-effects
1661 in the inferior, but being able to print accurate type
1662 information seems worth the risk. */
809f3be1 1663 if (!type->is_pointer_or_reference ()
27710edb 1664 || !is_dynamic_type (type->target_type ()))
786f70ee 1665 {
809f3be1 1666 if (type->is_pointer_or_reference ()
786f70ee
TT
1667 /* In C you can dereference an array to get the 1st elt. */
1668 || type->code () == TYPE_CODE_ARRAY)
ee7bb294 1669 return value::zero (type->target_type (),
786f70ee
TT
1670 lval_memory);
1671 else if (type->code () == TYPE_CODE_INT)
1672 /* GDB allows dereferencing an int. */
ee7bb294 1673 return value::zero (builtin_type (exp->gdbarch)->builtin_int,
786f70ee
TT
1674 lval_memory);
1675 else
1676 error (_("Attempt to take contents of a non-pointer value."));
1677 }
1678 }
1679
1680 /* Allow * on an integer so we can cast it to whatever we want.
1681 This returns an int, which seems like the most C-like thing to
1682 do. "long long" variables are rare enough that
1683 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1684 if (type->code () == TYPE_CODE_INT)
1685 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1686 (CORE_ADDR) value_as_address (arg1));
1687 return value_ind (arg1);
1688}
1689
acee9468
TT
1690/* A helper function for UNOP_ALIGNOF. */
1691
ae4bb61e 1692struct value *
acee9468
TT
1693eval_op_alignof (struct type *expect_type, struct expression *exp,
1694 enum noside noside,
1695 struct value *arg1)
1696{
d0c97917 1697 struct type *type = arg1->type ();
acee9468
TT
1698 /* FIXME: This should be size_t. */
1699 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1700 ULONGEST align = type_align (type);
1701 if (align == 0)
1702 error (_("could not determine alignment of type"));
1703 return value_from_longest (size_type, align);
1704}
1705
3aef2a07
TT
1706/* A helper function for UNOP_MEMVAL. */
1707
cbc18219 1708struct value *
3aef2a07
TT
1709eval_op_memval (struct type *expect_type, struct expression *exp,
1710 enum noside noside,
1711 struct value *arg1, struct type *type)
1712{
3aef2a07 1713 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 1714 return value::zero (type, lval_memory);
3aef2a07
TT
1715 else
1716 return value_at_lazy (type, value_as_address (arg1));
1717}
1718
00f50884
TT
1719/* A helper function for UNOP_PREINCREMENT. */
1720
6d89e296 1721struct value *
00f50884
TT
1722eval_op_preinc (struct type *expect_type, struct expression *exp,
1723 enum noside noside, enum exp_opcode op,
1724 struct value *arg1)
1725{
0b2b0b82 1726 if (noside == EVAL_AVOID_SIDE_EFFECTS)
00f50884
TT
1727 return arg1;
1728 else if (unop_user_defined_p (op, arg1))
1729 {
1730 return value_x_unop (arg1, op, noside);
1731 }
1732 else
1733 {
1734 struct value *arg2;
d0c97917 1735 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
00f50884
TT
1736 arg2 = value_ptradd (arg1, 1);
1737 else
1738 {
1739 struct value *tmp = arg1;
1740
d0c97917 1741 arg2 = value_one (arg1->type ());
00f50884
TT
1742 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1743 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1744 }
1745
1746 return value_assign (arg1, arg2);
1747 }
1748}
1749
9e1361b7
TT
1750/* A helper function for UNOP_PREDECREMENT. */
1751
6d89e296 1752struct value *
9e1361b7
TT
1753eval_op_predec (struct type *expect_type, struct expression *exp,
1754 enum noside noside, enum exp_opcode op,
1755 struct value *arg1)
1756{
0b2b0b82 1757 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9e1361b7
TT
1758 return arg1;
1759 else if (unop_user_defined_p (op, arg1))
1760 {
1761 return value_x_unop (arg1, op, noside);
1762 }
1763 else
1764 {
1765 struct value *arg2;
d0c97917 1766 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
9e1361b7
TT
1767 arg2 = value_ptradd (arg1, -1);
1768 else
1769 {
1770 struct value *tmp = arg1;
1771
d0c97917 1772 arg2 = value_one (arg1->type ());
9e1361b7
TT
1773 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1774 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1775 }
1776
1777 return value_assign (arg1, arg2);
1778 }
1779}
1780
abffe116
TT
1781/* A helper function for UNOP_POSTINCREMENT. */
1782
6d89e296 1783struct value *
abffe116
TT
1784eval_op_postinc (struct type *expect_type, struct expression *exp,
1785 enum noside noside, enum exp_opcode op,
1786 struct value *arg1)
1787{
0b2b0b82 1788 if (noside == EVAL_AVOID_SIDE_EFFECTS)
abffe116
TT
1789 return arg1;
1790 else if (unop_user_defined_p (op, arg1))
1791 {
1792 return value_x_unop (arg1, op, noside);
1793 }
1794 else
1795 {
aa9f4538 1796 struct value *arg3 = arg1->non_lval ();
abffe116
TT
1797 struct value *arg2;
1798
d0c97917 1799 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
abffe116
TT
1800 arg2 = value_ptradd (arg1, 1);
1801 else
1802 {
1803 struct value *tmp = arg1;
1804
d0c97917 1805 arg2 = value_one (arg1->type ());
abffe116
TT
1806 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1807 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1808 }
1809
1810 value_assign (arg1, arg2);
1811 return arg3;
1812 }
1813}
1814
a220ead5
TT
1815/* A helper function for UNOP_POSTDECREMENT. */
1816
6d89e296 1817struct value *
a220ead5
TT
1818eval_op_postdec (struct type *expect_type, struct expression *exp,
1819 enum noside noside, enum exp_opcode op,
1820 struct value *arg1)
1821{
0b2b0b82 1822 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a220ead5
TT
1823 return arg1;
1824 else if (unop_user_defined_p (op, arg1))
1825 {
1826 return value_x_unop (arg1, op, noside);
1827 }
1828 else
1829 {
aa9f4538 1830 struct value *arg3 = arg1->non_lval ();
a220ead5
TT
1831 struct value *arg2;
1832
d0c97917 1833 if (ptrmath_type_p (exp->language_defn, arg1->type ()))
a220ead5
TT
1834 arg2 = value_ptradd (arg1, -1);
1835 else
1836 {
1837 struct value *tmp = arg1;
1838
d0c97917 1839 arg2 = value_one (arg1->type ());
a220ead5
TT
1840 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1841 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1842 }
1843
1844 value_assign (arg1, arg2);
1845 return arg3;
1846 }
1847}
1848
aec95807
TT
1849/* A helper function for OP_TYPE. */
1850
5b5f5140 1851struct value *
aec95807
TT
1852eval_op_type (struct type *expect_type, struct expression *exp,
1853 enum noside noside, struct type *type)
1854{
0b2b0b82 1855 if (noside == EVAL_AVOID_SIDE_EFFECTS)
317c3ed9 1856 return value::allocate (type);
aec95807
TT
1857 else
1858 error (_("Attempt to use a type name as an expression"));
1859}
1860
fb5ba2ab
TT
1861/* A helper function for BINOP_ASSIGN_MODIFY. */
1862
e5946e16 1863struct value *
fb5ba2ab
TT
1864eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1865 enum noside noside, enum exp_opcode op,
1866 struct value *arg1, struct value *arg2)
1867{
0b2b0b82 1868 if (noside == EVAL_AVOID_SIDE_EFFECTS)
fb5ba2ab
TT
1869 return arg1;
1870 if (binop_user_defined_p (op, arg1, arg2))
1871 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1872 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
d0c97917
TT
1873 arg1->type ())
1874 && is_integral_type (arg2->type ()))
fb5ba2ab
TT
1875 arg2 = value_ptradd (arg1, value_as_long (arg2));
1876 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
d0c97917
TT
1877 arg1->type ())
1878 && is_integral_type (arg2->type ()))
fb5ba2ab
TT
1879 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1880 else
1881 {
1882 struct value *tmp = arg1;
1883
1884 /* For shift and integer exponentiation operations,
1885 only promote the first argument. */
1886 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
d0c97917 1887 && is_integral_type (arg2->type ()))
fb5ba2ab
TT
1888 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1889 else
1890 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1891
1892 arg2 = value_binop (tmp, arg2, op);
1893 }
1894 return value_assign (arg1, arg2);
1895}
1896
5e80600e
TT
1897/* Note that ARGS needs 2 empty slots up front and must end with a
1898 null pointer. */
1899static struct value *
1900eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1901 enum noside noside, CORE_ADDR selector,
1902 value *target, gdb::array_view<value *> args)
1903{
1904 CORE_ADDR responds_selector = 0;
1905 CORE_ADDR method_selector = 0;
1906
1907 int struct_return = 0;
1908
1909 struct value *msg_send = NULL;
1910 struct value *msg_send_stret = NULL;
1911 int gnu_runtime = 0;
1912
1913 struct value *method = NULL;
1914 struct value *called_method = NULL;
1915
1916 struct type *selector_type = NULL;
1917 struct type *long_type;
1918 struct type *type;
1919
1920 struct value *ret = NULL;
1921 CORE_ADDR addr = 0;
1922
1923 value *argvec[5];
1924
1925 long_type = builtin_type (exp->gdbarch)->builtin_long;
1926 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1927
1928 if (value_as_long (target) == 0)
1929 return value_from_longest (long_type, 0);
1930
1931 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1932 gnu_runtime = 1;
1933
1934 /* Find the method dispatch (Apple runtime) or method lookup
1935 (GNU runtime) function for Objective-C. These will be used
1936 to lookup the symbol information for the method. If we
1937 can't find any symbol information, then we'll use these to
1938 call the method, otherwise we can call the method
1939 directly. The msg_send_stret function is used in the special
1940 case of a method that returns a structure (Apple runtime
1941 only). */
1942 if (gnu_runtime)
1943 {
1944 type = selector_type;
1945
1946 type = lookup_function_type (type);
1947 type = lookup_pointer_type (type);
1948 type = lookup_function_type (type);
1949 type = lookup_pointer_type (type);
1950
1951 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1952 msg_send_stret
1953 = find_function_in_inferior ("objc_msg_lookup", NULL);
1954
1955 msg_send = value_from_pointer (type, value_as_address (msg_send));
1956 msg_send_stret = value_from_pointer (type,
1957 value_as_address (msg_send_stret));
1958 }
1959 else
1960 {
1961 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1962 /* Special dispatcher for methods returning structs. */
1963 msg_send_stret
1964 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1965 }
1966
1967 /* Verify the target object responds to this method. The
1968 standard top-level 'Object' class uses a different name for
1969 the verification method than the non-standard, but more
1970 often used, 'NSObject' class. Make sure we check for both. */
1971
1972 responds_selector
1973 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1974 if (responds_selector == 0)
1975 responds_selector
1976 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1977
1978 if (responds_selector == 0)
1979 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1980
1981 method_selector
1982 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1983 if (method_selector == 0)
1984 method_selector
1985 = lookup_child_selector (exp->gdbarch, "methodFor:");
1986
1987 if (method_selector == 0)
1988 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1989
1990 /* Call the verification method, to make sure that the target
1991 class implements the desired method. */
1992
1993 argvec[0] = msg_send;
1994 argvec[1] = target;
1995 argvec[2] = value_from_longest (long_type, responds_selector);
1996 argvec[3] = value_from_longest (long_type, selector);
1997 argvec[4] = 0;
1998
1999 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2000 if (gnu_runtime)
2001 {
2002 /* Function objc_msg_lookup returns a pointer. */
2003 argvec[0] = ret;
2004 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2005 }
2006 if (value_as_long (ret) == 0)
2007 error (_("Target does not respond to this message selector."));
2008
2009 /* Call "methodForSelector:" method, to get the address of a
2010 function method that implements this selector for this
2011 class. If we can find a symbol at that address, then we
2012 know the return type, parameter types etc. (that's a good
2013 thing). */
2014
2015 argvec[0] = msg_send;
2016 argvec[1] = target;
2017 argvec[2] = value_from_longest (long_type, method_selector);
2018 argvec[3] = value_from_longest (long_type, selector);
2019 argvec[4] = 0;
2020
2021 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2022 if (gnu_runtime)
2023 {
2024 argvec[0] = ret;
2025 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
2026 }
2027
2028 /* ret should now be the selector. */
2029
2030 addr = value_as_long (ret);
2031 if (addr)
2032 {
2033 struct symbol *sym = NULL;
2034
2035 /* The address might point to a function descriptor;
2036 resolve it to the actual code address instead. */
328d42d8
SM
2037 addr = gdbarch_convert_from_func_ptr_addr
2038 (exp->gdbarch, addr, current_inferior ()->top_target ());
5e80600e
TT
2039
2040 /* Is it a high_level symbol? */
2041 sym = find_pc_function (addr);
2042 if (sym != NULL)
2043 method = value_of_variable (sym, 0);
2044 }
2045
2046 /* If we found a method with symbol information, check to see
2047 if it returns a struct. Otherwise assume it doesn't. */
2048
2049 if (method)
2050 {
2051 CORE_ADDR funaddr;
2052 struct type *val_type;
2053
2054 funaddr = find_function_addr (method, &val_type);
2055
2056 block_for_pc (funaddr);
2057
2058 val_type = check_typedef (val_type);
2059
2060 if ((val_type == NULL)
2061 || (val_type->code () == TYPE_CODE_ERROR))
2062 {
2063 if (expect_type != NULL)
2064 val_type = expect_type;
2065 }
2066
2067 struct_return = using_struct_return (exp->gdbarch, method,
2068 val_type);
2069 }
2070 else if (expect_type != NULL)
2071 {
2072 struct_return = using_struct_return (exp->gdbarch, NULL,
2073 check_typedef (expect_type));
2074 }
2075
2076 /* Found a function symbol. Now we will substitute its
2077 value in place of the message dispatcher (obj_msgSend),
2078 so that we call the method directly instead of thru
2079 the dispatcher. The main reason for doing this is that
2080 we can now evaluate the return value and parameter values
2081 according to their known data types, in case we need to
2082 do things like promotion, dereferencing, special handling
2083 of structs and doubles, etc.
2084
2085 We want to use the type signature of 'method', but still
2086 jump to objc_msgSend() or objc_msgSend_stret() to better
2087 mimic the behavior of the runtime. */
2088
2089 if (method)
2090 {
d0c97917 2091 if (method->type ()->code () != TYPE_CODE_FUNC)
5e80600e
TT
2092 error (_("method address has symbol information "
2093 "with non-function type; skipping"));
2094
2095 /* Create a function pointer of the appropriate type, and
2096 replace its value with the value of msg_send or
2097 msg_send_stret. We must use a pointer here, as
2098 msg_send and msg_send_stret are of pointer type, and
2099 the representation may be different on systems that use
2100 function descriptors. */
2101 if (struct_return)
2102 called_method
d0c97917 2103 = value_from_pointer (lookup_pointer_type (method->type ()),
5e80600e
TT
2104 value_as_address (msg_send_stret));
2105 else
2106 called_method
d0c97917 2107 = value_from_pointer (lookup_pointer_type (method->type ()),
5e80600e
TT
2108 value_as_address (msg_send));
2109 }
2110 else
2111 {
2112 if (struct_return)
2113 called_method = msg_send_stret;
2114 else
2115 called_method = msg_send;
2116 }
2117
5e80600e
TT
2118
2119 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2120 {
2121 /* If the return type doesn't look like a function type,
2122 call an error. This can happen if somebody tries to
2123 turn a variable into a function call. This is here
2124 because people often want to call, eg, strcmp, which
2125 gdb doesn't know is a function. If gdb isn't asked for
2126 it's opinion (ie. through "whatis"), it won't offer
2127 it. */
2128
d0c97917 2129 struct type *callee_type = called_method->type ();
5e80600e
TT
2130
2131 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
27710edb
SM
2132 callee_type = callee_type->target_type ();
2133 callee_type = callee_type->target_type ();
5e80600e
TT
2134
2135 if (callee_type)
2136 {
2137 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
317c3ed9 2138 return value::allocate (expect_type);
5e80600e 2139 else
317c3ed9 2140 return value::allocate (callee_type);
5e80600e
TT
2141 }
2142 else
2143 error (_("Expression of type other than "
2144 "\"method returning ...\" used as a method"));
2145 }
2146
2147 /* Now depending on whether we found a symbol for the method,
2148 we will either call the runtime dispatcher or the method
2149 directly. */
2150
2151 args[0] = target;
2152 args[1] = value_from_longest (long_type, selector);
2153
2154 if (gnu_runtime && (method != NULL))
2155 {
2156 /* Function objc_msg_lookup returns a pointer. */
d0c97917 2157 struct type *tem_type = called_method->type ();
5e80600e 2158 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
81ae560c 2159 called_method->deprecated_set_type (tem_type);
5e80600e
TT
2160 called_method = call_function_by_hand (called_method, NULL, args);
2161 }
2162
2163 return call_function_by_hand (called_method, NULL, args);
2164}
2165
c0d7ed8c
TT
2166/* Helper function for MULTI_SUBSCRIPT. */
2167
2168static struct value *
2169eval_multi_subscript (struct type *expect_type, struct expression *exp,
2170 enum noside noside, value *arg1,
2171 gdb::array_view<value *> args)
2172{
c0d7ed8c
TT
2173 for (value *arg2 : args)
2174 {
2175 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2176 {
2177 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2178 }
2179 else
2180 {
2181 arg1 = coerce_ref (arg1);
d0c97917 2182 struct type *type = check_typedef (arg1->type ());
c0d7ed8c
TT
2183
2184 switch (type->code ())
2185 {
2186 case TYPE_CODE_PTR:
2187 case TYPE_CODE_ARRAY:
2188 case TYPE_CODE_STRING:
2189 arg1 = value_subscript (arg1, value_as_long (arg2));
2190 break;
2191
2192 default:
2193 if (type->name ())
2194 error (_("cannot subscript something of type `%s'"),
2195 type->name ());
2196 else
2197 error (_("cannot subscript requested type"));
2198 }
2199 }
2200 }
2201 return (arg1);
2202}
2203
085734dd
TT
2204namespace expr
2205{
2206
2207value *
2208objc_msgcall_operation::evaluate (struct type *expect_type,
2209 struct expression *exp,
2210 enum noside noside)
2211{
2212 enum noside sub_no_side = EVAL_NORMAL;
2213 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2214
2215 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2216 sub_no_side = EVAL_NORMAL;
2217 else
2218 sub_no_side = noside;
2219 value *target
2220 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2221
2222 if (value_as_long (target) == 0)
2223 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2224 else
2225 sub_no_side = noside;
2226 std::vector<operation_up> &args = std::get<2> (m_storage);
2227 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2228 argvec[0] = nullptr;
2229 argvec[1] = nullptr;
2230 for (int i = 0; i < args.size (); ++i)
2231 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2232 argvec[args.size () + 2] = nullptr;
2233
2234 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2235 get<0> (m_storage), target,
2236 gdb::make_array_view (argvec,
2237 args.size () + 3));
2238}
2239
821e72d7
TT
2240value *
2241multi_subscript_operation::evaluate (struct type *expect_type,
2242 struct expression *exp,
2243 enum noside noside)
2244{
2245 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2246 std::vector<operation_up> &values = std::get<1> (m_storage);
2247 value **argvec = XALLOCAVEC (struct value *, values.size ());
2248 for (int ix = 0; ix < values.size (); ++ix)
2249 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2250 return eval_multi_subscript (expect_type, exp, noside, arg1,
2251 gdb::make_array_view (argvec, values.size ()));
085734dd
TT
2252}
2253
5019124b
TT
2254value *
2255logical_and_operation::evaluate (struct type *expect_type,
2256 struct expression *exp,
2257 enum noside noside)
2258{
2259 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
5019124b
TT
2260
2261 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2262 EVAL_AVOID_SIDE_EFFECTS);
2263
2264 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2265 {
2266 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2267 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2268 }
2269 else
2270 {
7ebaa5f7 2271 bool tem = value_logical_not (arg1);
5019124b
TT
2272 if (!tem)
2273 {
2274 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2275 tem = value_logical_not (arg2);
2276 }
2277 struct type *type = language_bool_type (exp->language_defn,
2278 exp->gdbarch);
2279 return value_from_longest (type, !tem);
2280 }
2281}
2282
2283value *
2284logical_or_operation::evaluate (struct type *expect_type,
2285 struct expression *exp,
2286 enum noside noside)
2287{
2288 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
5019124b
TT
2289
2290 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2291 EVAL_AVOID_SIDE_EFFECTS);
2292
2293 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2294 {
2295 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2296 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2297 }
2298 else
2299 {
7ebaa5f7 2300 bool tem = value_logical_not (arg1);
5019124b
TT
2301 if (tem)
2302 {
2303 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2304 tem = value_logical_not (arg2);
2305 }
2306
2307 struct type *type = language_bool_type (exp->language_defn,
2308 exp->gdbarch);
2309 return value_from_longest (type, !tem);
2310 }
2311}
2312
e4479080
TT
2313value *
2314adl_func_operation::evaluate (struct type *expect_type,
2315 struct expression *exp,
2316 enum noside noside)
2317{
2318 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2319 std::vector<value *> args (arg_ops.size ());
2320 for (int i = 0; i < arg_ops.size (); ++i)
2321 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2322
2323 struct symbol *symp;
2324 find_overload_match (args, std::get<0> (m_storage).c_str (),
2325 NON_METHOD,
2326 nullptr, nullptr,
2327 nullptr, &symp, nullptr, 0, noside);
5f9c5a63 2328 if (symp->type ()->code () == TYPE_CODE_ERROR)
e4479080
TT
2329 error_unknown_type (symp->print_name ());
2330 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2331 return evaluate_subexp_do_call (exp, noside, callee, args,
2332 nullptr, expect_type);
2333
2334}
2335
1c02eb30
TT
2336/* This function evaluates brace-initializers (in C/C++) for
2337 structure types. */
2338
2339struct value *
2340array_operation::evaluate_struct_tuple (struct value *struct_val,
2341 struct expression *exp,
2342 enum noside noside, int nargs)
2343{
2344 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
d0c97917 2345 struct type *struct_type = check_typedef (struct_val->type ());
1c02eb30
TT
2346 struct type *field_type;
2347 int fieldno = -1;
2348
2349 int idx = 0;
2350 while (--nargs >= 0)
2351 {
2352 struct value *val = NULL;
2353 int bitpos, bitsize;
2354 bfd_byte *addr;
2355
2356 fieldno++;
2357 /* Skip static fields. */
2358 while (fieldno < struct_type->num_fields ()
c819a338 2359 && struct_type->field (fieldno).is_static ())
1c02eb30
TT
2360 fieldno++;
2361 if (fieldno >= struct_type->num_fields ())
2362 error (_("too many initializers"));
2363 field_type = struct_type->field (fieldno).type ();
2364 if (field_type->code () == TYPE_CODE_UNION
33d16dd9 2365 && struct_type->field (fieldno).name ()[0] == '0')
1c02eb30
TT
2366 error (_("don't know which variant you want to set"));
2367
2368 /* Here, struct_type is the type of the inner struct,
2369 while substruct_type is the type of the inner struct.
2370 These are the same for normal structures, but a variant struct
2371 contains anonymous union fields that contain substruct fields.
2372 The value fieldno is the index of the top-level (normal or
2373 anonymous union) field in struct_field, while the value
2374 subfieldno is the index of the actual real (named inner) field
2375 in substruct_type. */
2376
2377 field_type = struct_type->field (fieldno).type ();
2378 if (val == 0)
2379 val = in_args[idx++]->evaluate (field_type, exp, noside);
2380
2381 /* Now actually set the field in struct_val. */
2382
2383 /* Assign val to field fieldno. */
d0c97917 2384 if (val->type () != field_type)
1c02eb30
TT
2385 val = value_cast (field_type, val);
2386
3757d2d4 2387 bitsize = struct_type->field (fieldno).bitsize ();
b610c045 2388 bitpos = struct_type->field (fieldno).loc_bitpos ();
bbe912ba 2389 addr = struct_val->contents_writeable ().data () + bitpos / 8;
1c02eb30
TT
2390 if (bitsize)
2391 modify_field (struct_type, addr,
2392 value_as_long (val), bitpos % 8, bitsize);
2393 else
efaf1ae0 2394 memcpy (addr, val->contents ().data (),
d0c97917 2395 val->type ()->length ());
1c02eb30
TT
2396
2397 }
2398 return struct_val;
2399}
2400
2401value *
2402array_operation::evaluate (struct type *expect_type,
2403 struct expression *exp,
2404 enum noside noside)
2405{
2922821e 2406 const int provided_low_bound = std::get<0> (m_storage);
1c02eb30 2407 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2922821e 2408 const int nargs = std::get<1> (m_storage) - provided_low_bound + 1;
1c02eb30
TT
2409 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2410
0b2b0b82 2411 if (expect_type != nullptr
1c02eb30
TT
2412 && type->code () == TYPE_CODE_STRUCT)
2413 {
317c3ed9 2414 struct value *rec = value::allocate (expect_type);
1c02eb30 2415
bbe912ba 2416 memset (rec->contents_raw ().data (), '\0', type->length ());
1c02eb30
TT
2417 return evaluate_struct_tuple (rec, exp, noside, nargs);
2418 }
2419
0b2b0b82 2420 if (expect_type != nullptr
1c02eb30
TT
2421 && type->code () == TYPE_CODE_ARRAY)
2422 {
2423 struct type *range_type = type->index_type ();
27710edb 2424 struct type *element_type = type->target_type ();
317c3ed9 2425 struct value *array = value::allocate (expect_type);
df86565b 2426 int element_size = check_typedef (element_type)->length ();
8b2ac9b2 2427 LONGEST low_bound, high_bound;
1c02eb30
TT
2428
2429 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2430 {
2431 low_bound = 0;
df86565b 2432 high_bound = (type->length () / element_size) - 1;
1c02eb30 2433 }
9c00ec6f
TT
2434 if (low_bound + nargs - 1 > high_bound)
2435 error (_("Too many array elements"));
bbe912ba 2436 memset (array->contents_raw ().data (), 0, expect_type->length ());
8b2ac9b2 2437 for (int idx = 0; idx < nargs; ++idx)
1c02eb30
TT
2438 {
2439 struct value *element;
2440
8b2ac9b2 2441 element = in_args[idx]->evaluate (element_type, exp, noside);
d0c97917 2442 if (element->type () != element_type)
1c02eb30 2443 element = value_cast (element_type, element);
8b2ac9b2 2444 memcpy (array->contents_raw ().data () + idx * element_size,
efaf1ae0 2445 element->contents ().data (),
1c02eb30 2446 element_size);
1c02eb30
TT
2447 }
2448 return array;
2449 }
2450
0b2b0b82 2451 if (expect_type != nullptr
1c02eb30
TT
2452 && type->code () == TYPE_CODE_SET)
2453 {
317c3ed9 2454 struct value *set = value::allocate (expect_type);
bbe912ba 2455 gdb_byte *valaddr = set->contents_raw ().data ();
1c02eb30
TT
2456 struct type *element_type = type->index_type ();
2457 struct type *check_type = element_type;
2458 LONGEST low_bound, high_bound;
2459
2460 /* Get targettype of elementtype. */
2461 while (check_type->code () == TYPE_CODE_RANGE
2462 || check_type->code () == TYPE_CODE_TYPEDEF)
27710edb 2463 check_type = check_type->target_type ();
1c02eb30
TT
2464
2465 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2466 error (_("(power)set type with unknown size"));
df86565b 2467 memset (valaddr, '\0', type->length ());
21bdf43a 2468 for (int idx = 0; idx < nargs; idx++)
1c02eb30
TT
2469 {
2470 LONGEST range_low, range_high;
2471 struct type *range_low_type, *range_high_type;
2472 struct value *elem_val;
2473
21bdf43a 2474 elem_val = in_args[idx]->evaluate (element_type, exp, noside);
d0c97917 2475 range_low_type = range_high_type = elem_val->type ();
1c02eb30
TT
2476 range_low = range_high = value_as_long (elem_val);
2477
2478 /* Check types of elements to avoid mixture of elements from
2479 different types. Also check if type of element is "compatible"
2480 with element type of powerset. */
2481 if (range_low_type->code () == TYPE_CODE_RANGE)
27710edb 2482 range_low_type = range_low_type->target_type ();
1c02eb30 2483 if (range_high_type->code () == TYPE_CODE_RANGE)
27710edb 2484 range_high_type = range_high_type->target_type ();
1c02eb30
TT
2485 if ((range_low_type->code () != range_high_type->code ())
2486 || (range_low_type->code () == TYPE_CODE_ENUM
2487 && (range_low_type != range_high_type)))
2488 /* different element modes. */
2489 error (_("POWERSET tuple elements of different mode"));
2490 if ((check_type->code () != range_low_type->code ())
2491 || (check_type->code () == TYPE_CODE_ENUM
2492 && range_low_type != check_type))
2493 error (_("incompatible POWERSET tuple elements"));
2494 if (range_low > range_high)
2495 {
2496 warning (_("empty POWERSET tuple range"));
2497 continue;
2498 }
2499 if (range_low < low_bound || range_high > high_bound)
2500 error (_("POWERSET tuple element out of range"));
2501 range_low -= low_bound;
2502 range_high -= low_bound;
2503 for (; range_low <= range_high; range_low++)
2504 {
2505 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2506
2507 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2508 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2509 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2510 |= 1 << bit_index;
2511 }
2512 }
2513 return set;
2514 }
2515
c73556cb 2516 std::vector<value *> argvec (nargs);
0f2d28db 2517 for (int tem = 0; tem < nargs; tem++)
1c02eb30
TT
2518 {
2519 /* Ensure that array expressions are coerced into pointer
2520 objects. */
2521 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2522 }
2922821e 2523 return value_array (provided_low_bound, argvec);
1c02eb30
TT
2524}
2525
6f0dabd4
AB
2526value *
2527unop_extract_operation::evaluate (struct type *expect_type,
2528 struct expression *exp,
2529 enum noside noside)
2530{
2531 value *old_value = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2532 struct type *type = get_type ();
2533
d0c97917 2534 if (type->length () > old_value->type ()->length ())
6f0dabd4
AB
2535 error (_("length type is larger than the value type"));
2536
317c3ed9 2537 struct value *result = value::allocate (type);
6c49729e 2538 old_value->contents_copy (result, 0, 0, type->length ());
6f0dabd4
AB
2539 return result;
2540}
2541
821e72d7 2542}
085734dd 2543
c906108c 2544\f
13ea014a
TT
2545/* Helper for evaluate_subexp_for_address. */
2546
2547static value *
2548evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
2549 value *x)
2550{
2551 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2552 {
d0c97917 2553 struct type *type = check_typedef (x->type ());
13ea014a
TT
2554
2555 if (TYPE_IS_REFERENCE (type))
ee7bb294 2556 return value::zero (lookup_pointer_type (type->target_type ()),
13ea014a 2557 not_lval);
736355f2 2558 else if (x->lval () == lval_memory || value_must_coerce_to_target (x))
ee7bb294 2559 return value::zero (lookup_pointer_type (x->type ()),
13ea014a
TT
2560 not_lval);
2561 else
2562 error (_("Attempt to take address of "
2563 "value not located in memory."));
2564 }
2565 return value_addr (x);
2566}
2567
e2803273
TT
2568namespace expr
2569{
2570
2571value *
2572operation::evaluate_for_cast (struct type *expect_type,
2573 struct expression *exp,
2574 enum noside noside)
2575{
2576 value *val = evaluate (expect_type, exp, noside);
e2803273
TT
2577 return value_cast (expect_type, val);
2578}
2579
2580value *
2581operation::evaluate_for_address (struct expression *exp, enum noside noside)
2582{
2583 value *val = evaluate (nullptr, exp, noside);
2584 return evaluate_subexp_for_address_base (exp, noside, val);
2585}
2586
d5ab122c
TT
2587value *
2588scope_operation::evaluate_for_address (struct expression *exp,
2589 enum noside noside)
2590{
2591 value *x = value_aggregate_elt (std::get<0> (m_storage),
2592 std::get<1> (m_storage).c_str (),
2593 NULL, 1, noside);
2594 if (x == NULL)
2595 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
2596 return x;
2597}
2598
876469ff
TT
2599value *
2600unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2601 enum noside noside)
2602{
2603 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2604
2605 /* We can't optimize out "&*" if there's a user-defined operator*. */
2606 if (unop_user_defined_p (UNOP_IND, x))
2607 {
2608 x = value_x_unop (x, UNOP_IND, noside);
2609 return evaluate_subexp_for_address_base (exp, noside, x);
2610 }
2611
2612 return coerce_array (x);
2613}
2614
0c8effa3
TT
2615value *
2616var_msym_value_operation::evaluate_for_address (struct expression *exp,
2617 enum noside noside)
2618{
9c79936b
TT
2619 const bound_minimal_symbol &b = std::get<0> (m_storage);
2620 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
0c8effa3
TT
2621 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2622 {
d0c97917 2623 struct type *type = lookup_pointer_type (val->type ());
ee7bb294 2624 return value::zero (type, not_lval);
0c8effa3
TT
2625 }
2626 else
2627 return value_addr (val);
2628}
2629
cbc18219
TT
2630value *
2631unop_memval_operation::evaluate_for_address (struct expression *exp,
2632 enum noside noside)
2633{
2634 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2635 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2636}
2637
2638value *
2639unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2640 enum noside noside)
2641{
2642 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2643 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 2644 struct type *type = typeval->type ();
cbc18219
TT
2645 return value_cast (lookup_pointer_type (type),
2646 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2647}
2648
e82a5afc
TT
2649value *
2650var_value_operation::evaluate_for_address (struct expression *exp,
2651 enum noside noside)
2652{
9e5e03df 2653 symbol *var = std::get<0> (m_storage).symbol;
e82a5afc
TT
2654
2655 /* C++: The "address" of a reference should yield the address
2656 * of the object pointed to. Let value_addr() deal with it. */
5f9c5a63 2657 if (TYPE_IS_REFERENCE (var->type ()))
e82a5afc
TT
2658 return operation::evaluate_for_address (exp, noside);
2659
2660 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2661 {
5f9c5a63 2662 struct type *type = lookup_pointer_type (var->type ());
66d7f48f 2663 enum address_class sym_class = var->aclass ();
e82a5afc
TT
2664
2665 if (sym_class == LOC_CONST
2666 || sym_class == LOC_CONST_BYTES
2667 || sym_class == LOC_REGISTER)
2668 error (_("Attempt to take address of register or constant."));
2669
ee7bb294 2670 return value::zero (type, not_lval);
e82a5afc
TT
2671 }
2672 else
9e5e03df 2673 return address_of_variable (var, std::get<0> (m_storage).block);
e82a5afc
TT
2674}
2675
2676value *
2677var_value_operation::evaluate_with_coercion (struct expression *exp,
2678 enum noside noside)
2679{
9e5e03df 2680 struct symbol *var = std::get<0> (m_storage).symbol;
5f9c5a63 2681 struct type *type = check_typedef (var->type ());
e82a5afc
TT
2682 if (type->code () == TYPE_CODE_ARRAY
2683 && !type->is_vector ()
2684 && CAST_IS_CONVERSION (exp->language_defn))
2685 {
9e5e03df
TT
2686 struct value *val = address_of_variable (var,
2687 std::get<0> (m_storage).block);
27710edb 2688 return value_cast (lookup_pointer_type (type->target_type ()), val);
e82a5afc
TT
2689 }
2690 return evaluate (nullptr, exp, noside);
2691}
2692
2693}
2694
13ea014a
TT
2695/* Helper function for evaluating the size of a type. */
2696
2697static value *
2698evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2699{
2700 /* FIXME: This should be size_t. */
2701 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2702 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2703 "When applied to a reference or a reference type, the result is
2704 the size of the referenced type." */
2705 type = check_typedef (type);
2706 if (exp->language_defn->la_language == language_cplus
2707 && (TYPE_IS_REFERENCE (type)))
27710edb 2708 type = check_typedef (type->target_type ());
df86565b 2709 return value_from_longest (size_type, (LONGEST) type->length ());
13ea014a
TT
2710}
2711
e2803273
TT
2712namespace expr
2713{
2714
2715value *
2716operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2717{
2718 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
d0c97917 2719 return evaluate_subexp_for_sizeof_base (exp, val->type ());
e2803273
TT
2720}
2721
0c8effa3
TT
2722value *
2723var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2724 enum noside noside)
2725
2726{
9c79936b
TT
2727 const bound_minimal_symbol &b = std::get<0> (m_storage);
2728 value *mval = evaluate_var_msym_value (noside, b.objfile, b.minsym);
0c8effa3 2729
d0c97917 2730 struct type *type = mval->type ();
0c8effa3 2731 if (type->code () == TYPE_CODE_ERROR)
9c79936b 2732 error_unknown_type (b.minsym->print_name ());
0c8effa3
TT
2733
2734 /* FIXME: This should be size_t. */
2735 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
df86565b 2736 return value_from_longest (size_type, type->length ());
0c8effa3
TT
2737}
2738
224d6424
TT
2739value *
2740subscript_operation::evaluate_for_sizeof (struct expression *exp,
2741 enum noside noside)
2742{
2743 if (noside == EVAL_NORMAL)
2744 {
2745 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2746 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 2747 struct type *type = check_typedef (val->type ());
224d6424
TT
2748 if (type->code () == TYPE_CODE_ARRAY)
2749 {
27710edb 2750 type = check_typedef (type->target_type ());
224d6424
TT
2751 if (type->code () == TYPE_CODE_ARRAY)
2752 {
2753 type = type->index_type ();
2754 /* Only re-evaluate the right hand side if the resulting type
2755 is a variable length type. */
2756 if (type->bounds ()->flag_bound_evaluated)
2757 {
2758 val = evaluate (nullptr, exp, EVAL_NORMAL);
2759 /* FIXME: This should be size_t. */
2760 struct type *size_type
2761 = builtin_type (exp->gdbarch)->builtin_int;
2762 return value_from_longest
d0c97917 2763 (size_type, (LONGEST) val->type ()->length ());
224d6424
TT
2764 }
2765 }
2766 }
2767 }
2768
2769 return operation::evaluate_for_sizeof (exp, noside);
2770}
2771
876469ff
TT
2772value *
2773unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2774 enum noside noside)
2775{
2776 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2777 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 2778 struct type *type = check_typedef (val->type ());
809f3be1 2779 if (!type->is_pointer_or_reference ()
876469ff
TT
2780 && type->code () != TYPE_CODE_ARRAY)
2781 error (_("Attempt to take contents of a non-pointer value."));
27710edb 2782 type = type->target_type ();
876469ff 2783 if (is_dynamic_type (type))
d0c97917 2784 type = value_ind (val)->type ();
876469ff
TT
2785 /* FIXME: This should be size_t. */
2786 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
df86565b 2787 return value_from_longest (size_type, (LONGEST) type->length ());
876469ff
TT
2788}
2789
cbc18219
TT
2790value *
2791unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2792 enum noside noside)
2793{
2794 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2795}
2796
2797value *
2798unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2799 enum noside noside)
2800{
2801 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2802 EVAL_AVOID_SIDE_EFFECTS);
d0c97917 2803 return evaluate_subexp_for_sizeof_base (exp, typeval->type ());
cbc18219
TT
2804}
2805
e82a5afc
TT
2806value *
2807var_value_operation::evaluate_for_sizeof (struct expression *exp,
2808 enum noside noside)
2809{
5f9c5a63 2810 struct type *type = std::get<0> (m_storage).symbol->type ();
e82a5afc
TT
2811 if (is_dynamic_type (type))
2812 {
2813 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
d0c97917 2814 type = val->type ();
e82a5afc
TT
2815 if (type->code () == TYPE_CODE_ARRAY)
2816 {
2817 /* FIXME: This should be size_t. */
2818 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2819 if (type_not_allocated (type) || type_not_associated (type))
ee7bb294 2820 return value::zero (size_type, not_lval);
e82a5afc
TT
2821 else if (is_dynamic_type (type->index_type ())
2822 && type->bounds ()->high.kind () == PROP_UNDEFINED)
b27556e3 2823 return value::allocate_optimized_out (size_type);
e82a5afc
TT
2824 }
2825 }
2826 return evaluate_subexp_for_sizeof_base (exp, type);
2827}
2828
0c8effa3
TT
2829value *
2830var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2831 struct expression *exp,
2832 enum noside noside)
2833{
2834 if (noside == EVAL_AVOID_SIDE_EFFECTS)
ee7bb294 2835 return value::zero (to_type, not_lval);
0c8effa3 2836
9c79936b
TT
2837 const bound_minimal_symbol &b = std::get<0> (m_storage);
2838 value *val = evaluate_var_msym_value (noside, b.objfile, b.minsym);
0c8effa3 2839
0c8effa3
TT
2840 val = value_cast (to_type, val);
2841
2842 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
736355f2 2843 if (val->lval () == lval_memory)
0c8effa3 2844 {
3ee3b270 2845 if (val->lazy ())
78259c36 2846 val->fetch_lazy ();
6f9c9d71 2847 val->set_lval (not_lval);
0c8effa3
TT
2848 }
2849 return val;
2850}
2851
e82a5afc
TT
2852value *
2853var_value_operation::evaluate_for_cast (struct type *to_type,
2854 struct expression *exp,
2855 enum noside noside)
2856{
2857 value *val = evaluate_var_value (noside,
9e5e03df
TT
2858 std::get<0> (m_storage).block,
2859 std::get<0> (m_storage).symbol);
e82a5afc 2860
e82a5afc
TT
2861 val = value_cast (to_type, val);
2862
2863 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
736355f2 2864 if (val->lval () == lval_memory)
e82a5afc 2865 {
3ee3b270 2866 if (val->lazy ())
78259c36 2867 val->fetch_lazy ();
6f9c9d71 2868 val->set_lval (not_lval);
e82a5afc
TT
2869 }
2870 return val;
2871}
2872
0c8effa3
TT
2873}
2874
0963b4bd 2875/* Parse a type expression in the string [P..P+LENGTH). */
c906108c
SS
2876
2877struct type *
f5756acc 2878parse_and_eval_type (const char *p, int length)
c906108c 2879{
c5aa993b 2880 char *tmp = (char *) alloca (length + 4);
d7f9d729 2881
c5aa993b
JM
2882 tmp[0] = '(';
2883 memcpy (tmp + 1, p, length);
2884 tmp[length + 1] = ')';
2885 tmp[length + 2] = '0';
2886 tmp[length + 3] = '\0';
4d01a485 2887 expression_up expr = parse_expression (tmp);
1eaebe02
TT
2888 expr::unop_cast_operation *op
2889 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2890 if (op == nullptr)
8a3fe4f8 2891 error (_("Internal error in eval_type."));
1eaebe02 2892 return op->get_type ();
c906108c 2893}