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