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