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