Change exp_uses_objfile to return bool
[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);
690 for (int i = 0; i < args.size (); ++i)
691 vals[i] = args[i]->evaluate_with_coercion (exp, noside);
692
693 return evaluate_subexp_do_call (exp, noside, callee, vals,
694 function_name, expect_type);
695}
696
697value *
698var_value_operation::evaluate_funcall (struct type *expect_type,
699 struct expression *exp,
700 enum noside noside,
701 const std::vector<operation_up> &args)
702{
703 if (!overload_resolution
704 || exp->language_defn->la_language != language_cplus)
705 return operation::evaluate_funcall (expect_type, exp, noside, args);
706
707 std::vector<value *> argvec (args.size ());
708 for (int i = 0; i < args.size (); ++i)
709 argvec[i] = args[i]->evaluate_with_coercion (exp, noside);
710
711 struct symbol *symp;
712 find_overload_match (argvec, NULL, NON_METHOD,
713 NULL, std::get<0> (m_storage),
714 NULL, &symp, NULL, 0, noside);
715
716 if (SYMBOL_TYPE (symp)->code () == TYPE_CODE_ERROR)
717 error_unknown_type (symp->print_name ());
718 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
719
720 return evaluate_subexp_do_call (exp, noside, callee, argvec,
721 nullptr, expect_type);
722}
723
724value *
725scope_operation::evaluate_funcall (struct type *expect_type,
726 struct expression *exp,
727 enum noside noside,
728 const std::vector<operation_up> &args)
729{
730 if (!overload_resolution
731 || exp->language_defn->la_language != language_cplus)
732 return operation::evaluate_funcall (expect_type, exp, noside, args);
733
734 /* Unpack it locally so we can properly handle overload
735 resolution. */
736 const std::string &name = std::get<1> (m_storage);
737 struct type *type = std::get<0> (m_storage);
738
739 symbol *function = NULL;
740 const char *function_name = NULL;
741 std::vector<value *> argvec (1 + args.size ());
742 if (type->code () == TYPE_CODE_NAMESPACE)
743 {
744 function = cp_lookup_symbol_namespace (type->name (),
745 name.c_str (),
746 get_selected_block (0),
747 VAR_DOMAIN).symbol;
748 if (function == NULL)
749 error (_("No symbol \"%s\" in namespace \"%s\"."),
750 name.c_str (), type->name ());
751 }
752 else
753 {
754 gdb_assert (type->code () == TYPE_CODE_STRUCT
755 || type->code () == TYPE_CODE_UNION);
756 function_name = name.c_str ();
757
758 /* We need a properly typed value for method lookup. */
759 argvec[0] = value_zero (type, lval_memory);
760 }
761
762 for (int i = 0; i < args.size (); ++i)
763 argvec[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
764 gdb::array_view<value *> arg_view = argvec;
765
766 value *callee = nullptr;
767 if (function_name != nullptr)
768 {
769 int static_memfuncp;
770
771 find_overload_match (arg_view, function_name, METHOD,
772 &argvec[0], nullptr, &callee, nullptr,
773 &static_memfuncp, 0, noside);
774 if (!static_memfuncp)
775 {
776 /* For the time being, we don't handle this. */
777 error (_("Call to overloaded function %s requires "
778 "`this' pointer"),
779 function_name);
780 }
781
782 arg_view = arg_view.slice (1);
783 }
784 else
785 {
786 symbol *symp;
787 arg_view = arg_view.slice (1);
788 find_overload_match (arg_view, nullptr,
789 NON_METHOD, nullptr, function,
790 nullptr, &symp, nullptr, 1, noside);
791 callee = value_of_variable (symp, get_selected_block (0));
792 }
793
794 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
795 nullptr, expect_type);
796}
797
798value *
799structop_member_base::evaluate_funcall (struct type *expect_type,
800 struct expression *exp,
801 enum noside noside,
802 const std::vector<operation_up> &args)
803{
804 /* First, evaluate the structure into lhs. */
805 value *lhs;
806 if (opcode () == STRUCTOP_MEMBER)
807 lhs = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
808 else
809 lhs = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
810
811 std::vector<value *> vals (args.size () + 1);
812 gdb::array_view<value *> val_view = vals;
813 /* If the function is a virtual function, then the aggregate
814 value (providing the structure) plays its part by providing
815 the vtable. Otherwise, it is just along for the ride: call
816 the function directly. */
817 value *rhs = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
818 value *callee;
819
820 type *a1_type = check_typedef (value_type (rhs));
821 if (a1_type->code () == TYPE_CODE_METHODPTR)
822 {
823 if (noside == EVAL_AVOID_SIDE_EFFECTS)
824 callee = value_zero (TYPE_TARGET_TYPE (a1_type), not_lval);
825 else
826 callee = cplus_method_ptr_to_value (&lhs, rhs);
827
828 vals[0] = lhs;
829 }
830 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
831 {
832 struct type *type_ptr
833 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
834 struct type *target_type_ptr
835 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type));
836
837 /* Now, convert this value to an address. */
838 lhs = value_cast (type_ptr, lhs);
839
840 long mem_offset = value_as_long (rhs);
841
842 callee = value_from_pointer (target_type_ptr,
843 value_as_long (lhs) + mem_offset);
844 callee = value_ind (callee);
845
846 val_view = val_view.slice (1);
847 }
848 else
849 error (_("Non-pointer-to-member value used in pointer-to-member "
850 "construct"));
851
852 for (int i = 0; i < args.size (); ++i)
853 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
854
855 return evaluate_subexp_do_call (exp, noside, callee, val_view,
856 nullptr, expect_type);
857
858}
859
860value *
861structop_base_operation::evaluate_funcall
862 (struct type *expect_type, struct expression *exp, enum noside noside,
863 const std::vector<operation_up> &args)
864{
865 std::vector<value *> vals (args.size () + 1);
866 /* First, evaluate the structure into vals[0]. */
867 enum exp_opcode op = opcode ();
868 if (op == STRUCTOP_STRUCT)
869 {
870 /* If v is a variable in a register, and the user types
871 v.method (), this will produce an error, because v has no
872 address.
873
874 A possible way around this would be to allocate a copy of
875 the variable on the stack, copy in the contents, call the
876 function, and copy out the contents. I.e. convert this
877 from call by reference to call by copy-return (or
878 whatever it's called). However, this does not work
879 because it is not the same: the method being called could
880 stash a copy of the address, and then future uses through
881 that address (after the method returns) would be expected
882 to use the variable itself, not some copy of it. */
883 vals[0] = std::get<0> (m_storage)->evaluate_for_address (exp, noside);
884 }
885 else
886 {
887 vals[0] = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
888 /* Check to see if the operator '->' has been overloaded.
889 If the operator has been overloaded replace vals[0] with the
890 value returned by the custom operator and continue
891 evaluation. */
892 while (unop_user_defined_p (op, vals[0]))
893 {
894 struct value *value = nullptr;
895 try
896 {
897 value = value_x_unop (vals[0], op, noside);
898 }
899 catch (const gdb_exception_error &except)
900 {
901 if (except.error == NOT_FOUND_ERROR)
902 break;
903 else
904 throw;
905 }
906
907 vals[0] = value;
908 }
909 }
910
911 for (int i = 0; i < args.size (); ++i)
912 vals[i + 1] = args[i]->evaluate_with_coercion (exp, noside);
913 gdb::array_view<value *> arg_view = vals;
914
915 int static_memfuncp;
916 value *callee;
917 const char *tstr = std::get<1> (m_storage).c_str ();
918 if (overload_resolution
919 && exp->language_defn->la_language == language_cplus)
920 {
921 /* Language is C++, do some overload resolution before
922 evaluation. */
923 value *val0 = vals[0];
924 find_overload_match (arg_view, tstr, METHOD,
925 &val0, nullptr, &callee, nullptr,
926 &static_memfuncp, 0, noside);
927 vals[0] = val0;
928 }
929 else
930 /* Non-C++ case -- or no overload resolution. */
931 {
932 struct value *temp = vals[0];
933
934 callee = value_struct_elt (&temp, &vals[1], tstr,
935 &static_memfuncp,
936 op == STRUCTOP_STRUCT
937 ? "structure" : "structure pointer");
938 /* value_struct_elt updates temp with the correct value of the
939 ``this'' pointer if necessary, so modify it to reflect any
940 ``this'' changes. */
941 vals[0] = value_from_longest (lookup_pointer_type (value_type (temp)),
942 value_address (temp)
943 + value_embedded_offset (temp));
944 }
945
946 /* Take out `this' if needed. */
947 if (static_memfuncp)
948 arg_view = arg_view.slice (1);
949
950 return evaluate_subexp_do_call (exp, noside, callee, arg_view,
951 nullptr, expect_type);
952}
953
954
955} /* namespace expr */
956
60e22c1e
HD
957/* Return true if type is integral or reference to integral */
958
959static bool
960is_integral_or_integral_reference (struct type *type)
961{
962 if (is_integral_type (type))
963 return true;
964
965 type = check_typedef (type);
966 return (type != nullptr
967 && TYPE_IS_REFERENCE (type)
968 && is_integral_type (TYPE_TARGET_TYPE (type)));
969}
970
ea2d29f7
TT
971/* Helper function that implements the body of OP_SCOPE. */
972
d5ab122c 973struct value *
ea2d29f7
TT
974eval_op_scope (struct type *expect_type, struct expression *exp,
975 enum noside noside,
976 struct type *type, const char *string)
977{
ea2d29f7
TT
978 struct value *arg1 = value_aggregate_elt (type, string, expect_type,
979 0, noside);
980 if (arg1 == NULL)
981 error (_("There is no field named %s"), string);
982 return arg1;
983}
984
50b98adc
TT
985/* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
986
b5cc3923 987struct value *
50b98adc
TT
988eval_op_var_entry_value (struct type *expect_type, struct expression *exp,
989 enum noside noside, symbol *sym)
990{
50b98adc
TT
991 if (noside == EVAL_AVOID_SIDE_EFFECTS)
992 return value_zero (SYMBOL_TYPE (sym), not_lval);
993
994 if (SYMBOL_COMPUTED_OPS (sym) == NULL
995 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
996 error (_("Symbol \"%s\" does not have any specific entry value"),
997 sym->print_name ());
998
999 struct frame_info *frame = get_selected_frame (NULL);
1000 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1001}
1002
c0df9289
TT
1003/* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1004
0c8effa3 1005struct value *
c0df9289
TT
1006eval_op_var_msym_value (struct type *expect_type, struct expression *exp,
1007 enum noside noside, bool outermost_p,
1008 minimal_symbol *msymbol, struct objfile *objfile)
1009{
1010 value *val = evaluate_var_msym_value (noside, objfile, msymbol);
1011
1012 struct type *type = value_type (val);
1013 if (type->code () == TYPE_CODE_ERROR
1014 && (noside != EVAL_AVOID_SIDE_EFFECTS || !outermost_p))
1015 error_unknown_type (msymbol->print_name ());
1016 return val;
1017}
1018
9b1d8af6
TT
1019/* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1020
17679395 1021struct value *
9b1d8af6
TT
1022eval_op_func_static_var (struct type *expect_type, struct expression *exp,
1023 enum noside noside,
1024 value *func, const char *var)
1025{
9b1d8af6
TT
1026 CORE_ADDR addr = value_address (func);
1027 const block *blk = block_for_pc (addr);
1028 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1029 if (sym.symbol == NULL)
1030 error (_("No symbol \"%s\" in specified context."), var);
1031 return evaluate_var_value (noside, sym.block, sym.symbol);
1032}
1033
ffff730b
TT
1034/* Helper function that implements the body of OP_REGISTER. */
1035
55bdbff8 1036struct value *
ffff730b
TT
1037eval_op_register (struct type *expect_type, struct expression *exp,
1038 enum noside noside, const char *name)
1039{
1040 int regno;
1041 struct value *val;
1042
1043 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1044 name, strlen (name));
1045 if (regno == -1)
1046 error (_("Register $%s not available."), name);
1047
1048 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1049 a value with the appropriate register type. Unfortunately,
1050 we don't have easy access to the type of user registers.
1051 So for these registers, we fetch the register value regardless
1052 of the evaluation mode. */
1053 if (noside == EVAL_AVOID_SIDE_EFFECTS
1054 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1055 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
1056 else
1057 val = value_of_register (regno, get_selected_frame (NULL));
1058 if (val == NULL)
1059 error (_("Value of register %s not available."), name);
1060 else
1061 return val;
1062}
1063
14a1c64a
TT
1064/* Helper function that implements the body of OP_STRING. */
1065
b50db09f 1066struct value *
14a1c64a
TT
1067eval_op_string (struct type *expect_type, struct expression *exp,
1068 enum noside noside, int len, const char *string)
1069{
14a1c64a
TT
1070 struct type *type = language_string_char_type (exp->language_defn,
1071 exp->gdbarch);
1072 return value_string (string, len, type);
1073}
1074
f871bae1
TT
1075/* Helper function that implements the body of OP_OBJC_SELECTOR. */
1076
09db3700 1077struct value *
f871bae1
TT
1078eval_op_objc_selector (struct type *expect_type, struct expression *exp,
1079 enum noside noside,
1080 const char *sel)
1081{
f871bae1
TT
1082 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1083 return value_from_longest (selector_type,
1084 lookup_child_selector (exp->gdbarch, sel));
1085}
1086
5c2f201e
TT
1087/* Helper function that implements the body of BINOP_CONCAT. */
1088
e51e26a0 1089struct value *
5c2f201e 1090eval_op_concat (struct type *expect_type, struct expression *exp,
e51e26a0 1091 enum noside noside, struct value *arg1, struct value *arg2)
5c2f201e 1092{
e51e26a0
TT
1093 if (binop_user_defined_p (BINOP_CONCAT, arg1, arg2))
1094 return value_x_binop (arg1, arg2, BINOP_CONCAT, OP_NULL, noside);
5c2f201e
TT
1095 else
1096 return value_concat (arg1, arg2);
1097}
1098
f960a617
TT
1099/* A helper function for TERNOP_SLICE. */
1100
1594e0bb 1101struct value *
f960a617
TT
1102eval_op_ternop (struct type *expect_type, struct expression *exp,
1103 enum noside noside,
1104 struct value *array, struct value *low, struct value *upper)
1105{
f960a617
TT
1106 int lowbound = value_as_long (low);
1107 int upperbound = value_as_long (upper);
1108 return value_slice (array, lowbound, upperbound - lowbound + 1);
1109}
1110
3e96c4fc
TT
1111/* A helper function for STRUCTOP_STRUCT. */
1112
808b22cf 1113struct value *
3e96c4fc
TT
1114eval_op_structop_struct (struct type *expect_type, struct expression *exp,
1115 enum noside noside,
1116 struct value *arg1, const char *string)
1117{
3e96c4fc
TT
1118 struct value *arg3 = value_struct_elt (&arg1, NULL, string,
1119 NULL, "structure");
1120 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1121 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1122 return arg3;
1123}
1124
fb461aa3
TT
1125/* A helper function for STRUCTOP_PTR. */
1126
ab0609be 1127struct value *
fb461aa3 1128eval_op_structop_ptr (struct type *expect_type, struct expression *exp,
ab0609be 1129 enum noside noside,
fb461aa3
TT
1130 struct value *arg1, const char *string)
1131{
fb461aa3
TT
1132 /* Check to see if operator '->' has been overloaded. If so replace
1133 arg1 with the value returned by evaluating operator->(). */
ab0609be 1134 while (unop_user_defined_p (STRUCTOP_PTR, arg1))
fb461aa3
TT
1135 {
1136 struct value *value = NULL;
1137 try
1138 {
ab0609be 1139 value = value_x_unop (arg1, STRUCTOP_PTR, noside);
fb461aa3
TT
1140 }
1141
1142 catch (const gdb_exception_error &except)
1143 {
1144 if (except.error == NOT_FOUND_ERROR)
1145 break;
1146 else
1147 throw;
1148 }
1149
1150 arg1 = value;
1151 }
1152
1153 /* JYG: if print object is on we need to replace the base type
1154 with rtti type in order to continue on with successful
1155 lookup of member / method only available in the rtti type. */
1156 {
1157 struct type *arg_type = value_type (arg1);
1158 struct type *real_type;
1159 int full, using_enc;
1160 LONGEST top;
1161 struct value_print_options opts;
1162
1163 get_user_print_options (&opts);
1164 if (opts.objectprint && TYPE_TARGET_TYPE (arg_type)
1165 && (TYPE_TARGET_TYPE (arg_type)->code () == TYPE_CODE_STRUCT))
1166 {
1167 real_type = value_rtti_indirect_type (arg1, &full, &top,
1168 &using_enc);
1169 if (real_type)
1170 arg1 = value_cast (real_type, arg1);
1171 }
1172 }
1173
1174 struct value *arg3 = value_struct_elt (&arg1, NULL, string,
1175 NULL, "structure pointer");
1176 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1177 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1178 return arg3;
1179}
1180
b7a96ed2
TT
1181/* A helper function for STRUCTOP_MEMBER. */
1182
07f724a8 1183struct value *
b7a96ed2
TT
1184eval_op_member (struct type *expect_type, struct expression *exp,
1185 enum noside noside,
1186 struct value *arg1, struct value *arg2)
1187{
1188 long mem_offset;
1189
b7a96ed2
TT
1190 struct value *arg3;
1191 struct type *type = check_typedef (value_type (arg2));
1192 switch (type->code ())
1193 {
1194 case TYPE_CODE_METHODPTR:
1195 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1196 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1197 else
1198 {
1199 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1200 gdb_assert (value_type (arg2)->code () == TYPE_CODE_PTR);
1201 return value_ind (arg2);
1202 }
1203
1204 case TYPE_CODE_MEMBERPTR:
1205 /* Now, convert these values to an address. */
1206 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1207 arg1, 1);
1208
1209 mem_offset = value_as_long (arg2);
1210
1211 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1212 value_as_long (arg1) + mem_offset);
1213 return value_ind (arg3);
1214
1215 default:
1216 error (_("non-pointer-to-member value used "
1217 "in pointer-to-member construct"));
1218 }
1219}
1220
aedaf9ac
TT
1221/* A helper function for BINOP_ADD. */
1222
a94323b6 1223struct value *
aedaf9ac 1224eval_op_add (struct type *expect_type, struct expression *exp,
a94323b6 1225 enum noside noside,
aedaf9ac
TT
1226 struct value *arg1, struct value *arg2)
1227{
a94323b6
TT
1228 if (binop_user_defined_p (BINOP_ADD, arg1, arg2))
1229 return value_x_binop (arg1, arg2, BINOP_ADD, OP_NULL, noside);
aedaf9ac
TT
1230 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1231 && is_integral_or_integral_reference (value_type (arg2)))
1232 return value_ptradd (arg1, value_as_long (arg2));
1233 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
1234 && is_integral_or_integral_reference (value_type (arg1)))
1235 return value_ptradd (arg2, value_as_long (arg1));
1236 else
1237 {
1238 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1239 return value_binop (arg1, arg2, BINOP_ADD);
1240 }
1241}
1242
d9790e22
TT
1243/* A helper function for BINOP_SUB. */
1244
5133d78b 1245struct value *
d9790e22 1246eval_op_sub (struct type *expect_type, struct expression *exp,
5133d78b 1247 enum noside noside,
d9790e22
TT
1248 struct value *arg1, struct value *arg2)
1249{
5133d78b
TT
1250 if (binop_user_defined_p (BINOP_SUB, arg1, arg2))
1251 return value_x_binop (arg1, arg2, BINOP_SUB, OP_NULL, noside);
d9790e22
TT
1252 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1253 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
1254 {
1255 /* FIXME -- should be ptrdiff_t */
1256 struct type *type = builtin_type (exp->gdbarch)->builtin_long;
1257 return value_from_longest (type, value_ptrdiff (arg1, arg2));
1258 }
1259 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
1260 && is_integral_or_integral_reference (value_type (arg2)))
1261 return value_ptradd (arg1, - value_as_long (arg2));
1262 else
1263 {
1264 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1265 return value_binop (arg1, arg2, BINOP_SUB);
1266 }
1267}
1268
7cdcdd02
TT
1269/* Helper function for several different binary operations. */
1270
373907ff 1271struct value *
7cdcdd02
TT
1272eval_op_binary (struct type *expect_type, struct expression *exp,
1273 enum noside noside, enum exp_opcode op,
1274 struct value *arg1, struct value *arg2)
1275{
7cdcdd02
TT
1276 if (binop_user_defined_p (op, arg1, arg2))
1277 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1278 else
1279 {
1280 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1281 fudge arg2 to avoid division-by-zero, the caller is
1282 (theoretically) only looking for the type of the result. */
1283 if (noside == EVAL_AVOID_SIDE_EFFECTS
1284 /* ??? Do we really want to test for BINOP_MOD here?
1285 The implementation of value_binop gives it a well-defined
1286 value. */
1287 && (op == BINOP_DIV
1288 || op == BINOP_INTDIV
1289 || op == BINOP_REM
1290 || op == BINOP_MOD)
1291 && value_logical_not (arg2))
1292 {
1293 struct value *v_one;
1294
1295 v_one = value_one (value_type (arg2));
1296 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
1297 return value_binop (arg1, v_one, op);
1298 }
1299 else
1300 {
1301 /* For shift and integer exponentiation operations,
1302 only promote the first argument. */
1303 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1304 && is_integral_type (value_type (arg2)))
1305 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1306 else
1307 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1308
1309 return value_binop (arg1, arg2, op);
1310 }
1311 }
1312}
1313
288d26bc
TT
1314/* A helper function for BINOP_SUBSCRIPT. */
1315
224d6424 1316struct value *
288d26bc
TT
1317eval_op_subscript (struct type *expect_type, struct expression *exp,
1318 enum noside noside, enum exp_opcode op,
1319 struct value *arg1, struct value *arg2)
1320{
288d26bc
TT
1321 if (binop_user_defined_p (op, arg1, arg2))
1322 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1323 else
1324 {
1325 /* If the user attempts to subscript something that is not an
1326 array or pointer type (like a plain int variable for example),
1327 then report this as an error. */
1328
1329 arg1 = coerce_ref (arg1);
1330 struct type *type = check_typedef (value_type (arg1));
1331 if (type->code () != TYPE_CODE_ARRAY
1332 && type->code () != TYPE_CODE_PTR)
1333 {
1334 if (type->name ())
1335 error (_("cannot subscript something of type `%s'"),
1336 type->name ());
1337 else
1338 error (_("cannot subscript requested type"));
1339 }
1340
1341 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1342 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
1343 else
1344 return value_subscript (arg1, value_as_long (arg2));
1345 }
1346}
1347
0cc96de8
TT
1348/* A helper function for BINOP_EQUAL. */
1349
46916f2b 1350struct value *
0cc96de8
TT
1351eval_op_equal (struct type *expect_type, struct expression *exp,
1352 enum noside noside, enum exp_opcode op,
1353 struct value *arg1, struct value *arg2)
1354{
0cc96de8
TT
1355 if (binop_user_defined_p (op, arg1, arg2))
1356 {
1357 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1358 }
1359 else
1360 {
1361 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1362 int tem = value_equal (arg1, arg2);
1363 struct type *type = language_bool_type (exp->language_defn,
1364 exp->gdbarch);
1365 return value_from_longest (type, (LONGEST) tem);
1366 }
1367}
1368
1fcb3559
TT
1369/* A helper function for BINOP_NOTEQUAL. */
1370
46916f2b 1371struct value *
1fcb3559
TT
1372eval_op_notequal (struct type *expect_type, struct expression *exp,
1373 enum noside noside, enum exp_opcode op,
1374 struct value *arg1, struct value *arg2)
1375{
1fcb3559
TT
1376 if (binop_user_defined_p (op, arg1, arg2))
1377 {
1378 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1379 }
1380 else
1381 {
1382 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1383 int tem = value_equal (arg1, arg2);
1384 struct type *type = language_bool_type (exp->language_defn,
1385 exp->gdbarch);
1386 return value_from_longest (type, (LONGEST) ! tem);
1387 }
1388}
1389
6cad1349
TT
1390/* A helper function for BINOP_LESS. */
1391
46916f2b 1392struct value *
6cad1349
TT
1393eval_op_less (struct type *expect_type, struct expression *exp,
1394 enum noside noside, enum exp_opcode op,
1395 struct value *arg1, struct value *arg2)
1396{
6cad1349
TT
1397 if (binop_user_defined_p (op, arg1, arg2))
1398 {
1399 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1400 }
1401 else
1402 {
1403 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1404 int tem = value_less (arg1, arg2);
1405 struct type *type = language_bool_type (exp->language_defn,
1406 exp->gdbarch);
1407 return value_from_longest (type, (LONGEST) tem);
1408 }
1409}
1410
1f78d732
TT
1411/* A helper function for BINOP_GTR. */
1412
46916f2b 1413struct value *
1f78d732
TT
1414eval_op_gtr (struct type *expect_type, struct expression *exp,
1415 enum noside noside, enum exp_opcode op,
1416 struct value *arg1, struct value *arg2)
1417{
1f78d732
TT
1418 if (binop_user_defined_p (op, arg1, arg2))
1419 {
1420 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1421 }
1422 else
1423 {
1424 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1425 int tem = value_less (arg2, arg1);
1426 struct type *type = language_bool_type (exp->language_defn,
1427 exp->gdbarch);
1428 return value_from_longest (type, (LONGEST) tem);
1429 }
1430}
1431
96e3efd9
TT
1432/* A helper function for BINOP_GEQ. */
1433
46916f2b 1434struct value *
96e3efd9
TT
1435eval_op_geq (struct type *expect_type, struct expression *exp,
1436 enum noside noside, enum exp_opcode op,
1437 struct value *arg1, struct value *arg2)
1438{
96e3efd9
TT
1439 if (binop_user_defined_p (op, arg1, arg2))
1440 {
1441 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1442 }
1443 else
1444 {
1445 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1446 int tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
1447 struct type *type = language_bool_type (exp->language_defn,
1448 exp->gdbarch);
1449 return value_from_longest (type, (LONGEST) tem);
1450 }
1451}
1452
60cdd487
TT
1453/* A helper function for BINOP_LEQ. */
1454
46916f2b 1455struct value *
60cdd487
TT
1456eval_op_leq (struct type *expect_type, struct expression *exp,
1457 enum noside noside, enum exp_opcode op,
1458 struct value *arg1, struct value *arg2)
1459{
60cdd487
TT
1460 if (binop_user_defined_p (op, arg1, arg2))
1461 {
1462 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1463 }
1464 else
1465 {
1466 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
1467 int tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
1468 struct type *type = language_bool_type (exp->language_defn,
1469 exp->gdbarch);
1470 return value_from_longest (type, (LONGEST) tem);
1471 }
1472}
1473
eed70b1c
TT
1474/* A helper function for BINOP_REPEAT. */
1475
d4eff4c1 1476struct value *
eed70b1c 1477eval_op_repeat (struct type *expect_type, struct expression *exp,
d4eff4c1 1478 enum noside noside, enum exp_opcode op,
eed70b1c
TT
1479 struct value *arg1, struct value *arg2)
1480{
eed70b1c
TT
1481 struct type *type = check_typedef (value_type (arg2));
1482 if (type->code () != TYPE_CODE_INT
1483 && type->code () != TYPE_CODE_ENUM)
1484 error (_("Non-integral right operand for \"@\" operator."));
1485 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1486 {
1487 return allocate_repeat_value (value_type (arg1),
1488 longest_to_int (value_as_long (arg2)));
1489 }
1490 else
1491 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
1492}
1493
39f288be
TT
1494/* A helper function for UNOP_PLUS. */
1495
9307d17b 1496struct value *
39f288be
TT
1497eval_op_plus (struct type *expect_type, struct expression *exp,
1498 enum noside noside, enum exp_opcode op,
1499 struct value *arg1)
1500{
39f288be
TT
1501 if (unop_user_defined_p (op, arg1))
1502 return value_x_unop (arg1, op, noside);
1503 else
1504 {
1505 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1506 return value_pos (arg1);
1507 }
1508}
1509
606d105f
TT
1510/* A helper function for UNOP_NEG. */
1511
9307d17b 1512struct value *
606d105f
TT
1513eval_op_neg (struct type *expect_type, struct expression *exp,
1514 enum noside noside, enum exp_opcode op,
1515 struct value *arg1)
1516{
606d105f
TT
1517 if (unop_user_defined_p (op, arg1))
1518 return value_x_unop (arg1, op, noside);
1519 else
1520 {
1521 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1522 return value_neg (arg1);
1523 }
1524}
1525
1f09ec81
TT
1526/* A helper function for UNOP_COMPLEMENT. */
1527
9307d17b 1528struct value *
1f09ec81
TT
1529eval_op_complement (struct type *expect_type, struct expression *exp,
1530 enum noside noside, enum exp_opcode op,
1531 struct value *arg1)
1532{
1f09ec81
TT
1533 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
1534 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
1535 else
1536 {
1537 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
1538 return value_complement (arg1);
1539 }
1540}
1541
24338fb9
TT
1542/* A helper function for UNOP_LOGICAL_NOT. */
1543
9307d17b 1544struct value *
24338fb9
TT
1545eval_op_lognot (struct type *expect_type, struct expression *exp,
1546 enum noside noside, enum exp_opcode op,
1547 struct value *arg1)
1548{
24338fb9
TT
1549 if (unop_user_defined_p (op, arg1))
1550 return value_x_unop (arg1, op, noside);
1551 else
1552 {
1553 struct type *type = language_bool_type (exp->language_defn,
1554 exp->gdbarch);
1555 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
1556 }
1557}
1558
786f70ee
TT
1559/* A helper function for UNOP_IND. */
1560
876469ff 1561struct value *
786f70ee 1562eval_op_ind (struct type *expect_type, struct expression *exp,
876469ff 1563 enum noside noside,
786f70ee
TT
1564 struct value *arg1)
1565{
1566 struct type *type = check_typedef (value_type (arg1));
1567 if (type->code () == TYPE_CODE_METHODPTR
1568 || type->code () == TYPE_CODE_MEMBERPTR)
1569 error (_("Attempt to dereference pointer "
1570 "to member without an object"));
876469ff
TT
1571 if (unop_user_defined_p (UNOP_IND, arg1))
1572 return value_x_unop (arg1, UNOP_IND, noside);
786f70ee
TT
1573 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
1574 {
1575 type = check_typedef (value_type (arg1));
1576
1577 /* If the type pointed to is dynamic then in order to resolve the
1578 dynamic properties we must actually dereference the pointer.
1579 There is a risk that this dereference will have side-effects
1580 in the inferior, but being able to print accurate type
1581 information seems worth the risk. */
1582 if ((type->code () != TYPE_CODE_PTR
1583 && !TYPE_IS_REFERENCE (type))
1584 || !is_dynamic_type (TYPE_TARGET_TYPE (type)))
1585 {
1586 if (type->code () == TYPE_CODE_PTR
1587 || TYPE_IS_REFERENCE (type)
1588 /* In C you can dereference an array to get the 1st elt. */
1589 || type->code () == TYPE_CODE_ARRAY)
1590 return value_zero (TYPE_TARGET_TYPE (type),
1591 lval_memory);
1592 else if (type->code () == TYPE_CODE_INT)
1593 /* GDB allows dereferencing an int. */
1594 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
1595 lval_memory);
1596 else
1597 error (_("Attempt to take contents of a non-pointer value."));
1598 }
1599 }
1600
1601 /* Allow * on an integer so we can cast it to whatever we want.
1602 This returns an int, which seems like the most C-like thing to
1603 do. "long long" variables are rare enough that
1604 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1605 if (type->code () == TYPE_CODE_INT)
1606 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
1607 (CORE_ADDR) value_as_address (arg1));
1608 return value_ind (arg1);
1609}
1610
acee9468
TT
1611/* A helper function for UNOP_ALIGNOF. */
1612
ae4bb61e 1613struct value *
acee9468
TT
1614eval_op_alignof (struct type *expect_type, struct expression *exp,
1615 enum noside noside,
1616 struct value *arg1)
1617{
1618 struct type *type = value_type (arg1);
1619 /* FIXME: This should be size_t. */
1620 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
1621 ULONGEST align = type_align (type);
1622 if (align == 0)
1623 error (_("could not determine alignment of type"));
1624 return value_from_longest (size_type, align);
1625}
1626
3aef2a07
TT
1627/* A helper function for UNOP_MEMVAL. */
1628
cbc18219 1629struct value *
3aef2a07
TT
1630eval_op_memval (struct type *expect_type, struct expression *exp,
1631 enum noside noside,
1632 struct value *arg1, struct type *type)
1633{
3aef2a07
TT
1634 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1635 return value_zero (type, lval_memory);
1636 else
1637 return value_at_lazy (type, value_as_address (arg1));
1638}
1639
00f50884
TT
1640/* A helper function for UNOP_PREINCREMENT. */
1641
6d89e296 1642struct value *
00f50884
TT
1643eval_op_preinc (struct type *expect_type, struct expression *exp,
1644 enum noside noside, enum exp_opcode op,
1645 struct value *arg1)
1646{
0b2b0b82 1647 if (noside == EVAL_AVOID_SIDE_EFFECTS)
00f50884
TT
1648 return arg1;
1649 else if (unop_user_defined_p (op, arg1))
1650 {
1651 return value_x_unop (arg1, op, noside);
1652 }
1653 else
1654 {
1655 struct value *arg2;
1656 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
1657 arg2 = value_ptradd (arg1, 1);
1658 else
1659 {
1660 struct value *tmp = arg1;
1661
1662 arg2 = value_one (value_type (arg1));
1663 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1664 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1665 }
1666
1667 return value_assign (arg1, arg2);
1668 }
1669}
1670
9e1361b7
TT
1671/* A helper function for UNOP_PREDECREMENT. */
1672
6d89e296 1673struct value *
9e1361b7
TT
1674eval_op_predec (struct type *expect_type, struct expression *exp,
1675 enum noside noside, enum exp_opcode op,
1676 struct value *arg1)
1677{
0b2b0b82 1678 if (noside == EVAL_AVOID_SIDE_EFFECTS)
9e1361b7
TT
1679 return arg1;
1680 else if (unop_user_defined_p (op, arg1))
1681 {
1682 return value_x_unop (arg1, op, noside);
1683 }
1684 else
1685 {
1686 struct value *arg2;
1687 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
1688 arg2 = value_ptradd (arg1, -1);
1689 else
1690 {
1691 struct value *tmp = arg1;
1692
1693 arg2 = value_one (value_type (arg1));
1694 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1695 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1696 }
1697
1698 return value_assign (arg1, arg2);
1699 }
1700}
1701
abffe116
TT
1702/* A helper function for UNOP_POSTINCREMENT. */
1703
6d89e296 1704struct value *
abffe116
TT
1705eval_op_postinc (struct type *expect_type, struct expression *exp,
1706 enum noside noside, enum exp_opcode op,
1707 struct value *arg1)
1708{
0b2b0b82 1709 if (noside == EVAL_AVOID_SIDE_EFFECTS)
abffe116
TT
1710 return arg1;
1711 else if (unop_user_defined_p (op, arg1))
1712 {
1713 return value_x_unop (arg1, op, noside);
1714 }
1715 else
1716 {
1717 struct value *arg3 = value_non_lval (arg1);
1718 struct value *arg2;
1719
1720 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
1721 arg2 = value_ptradd (arg1, 1);
1722 else
1723 {
1724 struct value *tmp = arg1;
1725
1726 arg2 = value_one (value_type (arg1));
1727 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1728 arg2 = value_binop (tmp, arg2, BINOP_ADD);
1729 }
1730
1731 value_assign (arg1, arg2);
1732 return arg3;
1733 }
1734}
1735
a220ead5
TT
1736/* A helper function for UNOP_POSTDECREMENT. */
1737
6d89e296 1738struct value *
a220ead5
TT
1739eval_op_postdec (struct type *expect_type, struct expression *exp,
1740 enum noside noside, enum exp_opcode op,
1741 struct value *arg1)
1742{
0b2b0b82 1743 if (noside == EVAL_AVOID_SIDE_EFFECTS)
a220ead5
TT
1744 return arg1;
1745 else if (unop_user_defined_p (op, arg1))
1746 {
1747 return value_x_unop (arg1, op, noside);
1748 }
1749 else
1750 {
1751 struct value *arg3 = value_non_lval (arg1);
1752 struct value *arg2;
1753
1754 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
1755 arg2 = value_ptradd (arg1, -1);
1756 else
1757 {
1758 struct value *tmp = arg1;
1759
1760 arg2 = value_one (value_type (arg1));
1761 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1762 arg2 = value_binop (tmp, arg2, BINOP_SUB);
1763 }
1764
1765 value_assign (arg1, arg2);
1766 return arg3;
1767 }
1768}
1769
aec95807
TT
1770/* A helper function for OP_TYPE. */
1771
5b5f5140 1772struct value *
aec95807
TT
1773eval_op_type (struct type *expect_type, struct expression *exp,
1774 enum noside noside, struct type *type)
1775{
0b2b0b82 1776 if (noside == EVAL_AVOID_SIDE_EFFECTS)
aec95807
TT
1777 return allocate_value (type);
1778 else
1779 error (_("Attempt to use a type name as an expression"));
1780}
1781
fb5ba2ab
TT
1782/* A helper function for BINOP_ASSIGN_MODIFY. */
1783
e5946e16 1784struct value *
fb5ba2ab
TT
1785eval_binop_assign_modify (struct type *expect_type, struct expression *exp,
1786 enum noside noside, enum exp_opcode op,
1787 struct value *arg1, struct value *arg2)
1788{
0b2b0b82 1789 if (noside == EVAL_AVOID_SIDE_EFFECTS)
fb5ba2ab
TT
1790 return arg1;
1791 if (binop_user_defined_p (op, arg1, arg2))
1792 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1793 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1794 value_type (arg1))
1795 && is_integral_type (value_type (arg2)))
1796 arg2 = value_ptradd (arg1, value_as_long (arg2));
1797 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1798 value_type (arg1))
1799 && is_integral_type (value_type (arg2)))
1800 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1801 else
1802 {
1803 struct value *tmp = arg1;
1804
1805 /* For shift and integer exponentiation operations,
1806 only promote the first argument. */
1807 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1808 && is_integral_type (value_type (arg2)))
1809 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1810 else
1811 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1812
1813 arg2 = value_binop (tmp, arg2, op);
1814 }
1815 return value_assign (arg1, arg2);
1816}
1817
5e80600e
TT
1818/* Note that ARGS needs 2 empty slots up front and must end with a
1819 null pointer. */
1820static struct value *
1821eval_op_objc_msgcall (struct type *expect_type, struct expression *exp,
1822 enum noside noside, CORE_ADDR selector,
1823 value *target, gdb::array_view<value *> args)
1824{
1825 CORE_ADDR responds_selector = 0;
1826 CORE_ADDR method_selector = 0;
1827
1828 int struct_return = 0;
1829
1830 struct value *msg_send = NULL;
1831 struct value *msg_send_stret = NULL;
1832 int gnu_runtime = 0;
1833
1834 struct value *method = NULL;
1835 struct value *called_method = NULL;
1836
1837 struct type *selector_type = NULL;
1838 struct type *long_type;
1839 struct type *type;
1840
1841 struct value *ret = NULL;
1842 CORE_ADDR addr = 0;
1843
1844 value *argvec[5];
1845
1846 long_type = builtin_type (exp->gdbarch)->builtin_long;
1847 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1848
1849 if (value_as_long (target) == 0)
1850 return value_from_longest (long_type, 0);
1851
1852 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1853 gnu_runtime = 1;
1854
1855 /* Find the method dispatch (Apple runtime) or method lookup
1856 (GNU runtime) function for Objective-C. These will be used
1857 to lookup the symbol information for the method. If we
1858 can't find any symbol information, then we'll use these to
1859 call the method, otherwise we can call the method
1860 directly. The msg_send_stret function is used in the special
1861 case of a method that returns a structure (Apple runtime
1862 only). */
1863 if (gnu_runtime)
1864 {
1865 type = selector_type;
1866
1867 type = lookup_function_type (type);
1868 type = lookup_pointer_type (type);
1869 type = lookup_function_type (type);
1870 type = lookup_pointer_type (type);
1871
1872 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1873 msg_send_stret
1874 = find_function_in_inferior ("objc_msg_lookup", NULL);
1875
1876 msg_send = value_from_pointer (type, value_as_address (msg_send));
1877 msg_send_stret = value_from_pointer (type,
1878 value_as_address (msg_send_stret));
1879 }
1880 else
1881 {
1882 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1883 /* Special dispatcher for methods returning structs. */
1884 msg_send_stret
1885 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1886 }
1887
1888 /* Verify the target object responds to this method. The
1889 standard top-level 'Object' class uses a different name for
1890 the verification method than the non-standard, but more
1891 often used, 'NSObject' class. Make sure we check for both. */
1892
1893 responds_selector
1894 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1895 if (responds_selector == 0)
1896 responds_selector
1897 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1898
1899 if (responds_selector == 0)
1900 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1901
1902 method_selector
1903 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1904 if (method_selector == 0)
1905 method_selector
1906 = lookup_child_selector (exp->gdbarch, "methodFor:");
1907
1908 if (method_selector == 0)
1909 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1910
1911 /* Call the verification method, to make sure that the target
1912 class implements the desired method. */
1913
1914 argvec[0] = msg_send;
1915 argvec[1] = target;
1916 argvec[2] = value_from_longest (long_type, responds_selector);
1917 argvec[3] = value_from_longest (long_type, selector);
1918 argvec[4] = 0;
1919
1920 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1921 if (gnu_runtime)
1922 {
1923 /* Function objc_msg_lookup returns a pointer. */
1924 argvec[0] = ret;
1925 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1926 }
1927 if (value_as_long (ret) == 0)
1928 error (_("Target does not respond to this message selector."));
1929
1930 /* Call "methodForSelector:" method, to get the address of a
1931 function method that implements this selector for this
1932 class. If we can find a symbol at that address, then we
1933 know the return type, parameter types etc. (that's a good
1934 thing). */
1935
1936 argvec[0] = msg_send;
1937 argvec[1] = target;
1938 argvec[2] = value_from_longest (long_type, method_selector);
1939 argvec[3] = value_from_longest (long_type, selector);
1940 argvec[4] = 0;
1941
1942 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1943 if (gnu_runtime)
1944 {
1945 argvec[0] = ret;
1946 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1947 }
1948
1949 /* ret should now be the selector. */
1950
1951 addr = value_as_long (ret);
1952 if (addr)
1953 {
1954 struct symbol *sym = NULL;
1955
1956 /* The address might point to a function descriptor;
1957 resolve it to the actual code address instead. */
1958 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1959 current_top_target ());
1960
1961 /* Is it a high_level symbol? */
1962 sym = find_pc_function (addr);
1963 if (sym != NULL)
1964 method = value_of_variable (sym, 0);
1965 }
1966
1967 /* If we found a method with symbol information, check to see
1968 if it returns a struct. Otherwise assume it doesn't. */
1969
1970 if (method)
1971 {
1972 CORE_ADDR funaddr;
1973 struct type *val_type;
1974
1975 funaddr = find_function_addr (method, &val_type);
1976
1977 block_for_pc (funaddr);
1978
1979 val_type = check_typedef (val_type);
1980
1981 if ((val_type == NULL)
1982 || (val_type->code () == TYPE_CODE_ERROR))
1983 {
1984 if (expect_type != NULL)
1985 val_type = expect_type;
1986 }
1987
1988 struct_return = using_struct_return (exp->gdbarch, method,
1989 val_type);
1990 }
1991 else if (expect_type != NULL)
1992 {
1993 struct_return = using_struct_return (exp->gdbarch, NULL,
1994 check_typedef (expect_type));
1995 }
1996
1997 /* Found a function symbol. Now we will substitute its
1998 value in place of the message dispatcher (obj_msgSend),
1999 so that we call the method directly instead of thru
2000 the dispatcher. The main reason for doing this is that
2001 we can now evaluate the return value and parameter values
2002 according to their known data types, in case we need to
2003 do things like promotion, dereferencing, special handling
2004 of structs and doubles, etc.
2005
2006 We want to use the type signature of 'method', but still
2007 jump to objc_msgSend() or objc_msgSend_stret() to better
2008 mimic the behavior of the runtime. */
2009
2010 if (method)
2011 {
2012 if (value_type (method)->code () != TYPE_CODE_FUNC)
2013 error (_("method address has symbol information "
2014 "with non-function type; skipping"));
2015
2016 /* Create a function pointer of the appropriate type, and
2017 replace its value with the value of msg_send or
2018 msg_send_stret. We must use a pointer here, as
2019 msg_send and msg_send_stret are of pointer type, and
2020 the representation may be different on systems that use
2021 function descriptors. */
2022 if (struct_return)
2023 called_method
2024 = value_from_pointer (lookup_pointer_type (value_type (method)),
2025 value_as_address (msg_send_stret));
2026 else
2027 called_method
2028 = value_from_pointer (lookup_pointer_type (value_type (method)),
2029 value_as_address (msg_send));
2030 }
2031 else
2032 {
2033 if (struct_return)
2034 called_method = msg_send_stret;
2035 else
2036 called_method = msg_send;
2037 }
2038
5e80600e
TT
2039
2040 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2041 {
2042 /* If the return type doesn't look like a function type,
2043 call an error. This can happen if somebody tries to
2044 turn a variable into a function call. This is here
2045 because people often want to call, eg, strcmp, which
2046 gdb doesn't know is a function. If gdb isn't asked for
2047 it's opinion (ie. through "whatis"), it won't offer
2048 it. */
2049
2050 struct type *callee_type = value_type (called_method);
2051
2052 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
2053 callee_type = TYPE_TARGET_TYPE (callee_type);
2054 callee_type = TYPE_TARGET_TYPE (callee_type);
2055
2056 if (callee_type)
2057 {
2058 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
2059 return allocate_value (expect_type);
2060 else
2061 return allocate_value (callee_type);
2062 }
2063 else
2064 error (_("Expression of type other than "
2065 "\"method returning ...\" used as a method"));
2066 }
2067
2068 /* Now depending on whether we found a symbol for the method,
2069 we will either call the runtime dispatcher or the method
2070 directly. */
2071
2072 args[0] = target;
2073 args[1] = value_from_longest (long_type, selector);
2074
2075 if (gnu_runtime && (method != NULL))
2076 {
2077 /* Function objc_msg_lookup returns a pointer. */
2078 struct type *tem_type = value_type (called_method);
2079 tem_type = lookup_pointer_type (lookup_function_type (tem_type));
2080 deprecated_set_value_type (called_method, tem_type);
2081 called_method = call_function_by_hand (called_method, NULL, args);
2082 }
2083
2084 return call_function_by_hand (called_method, NULL, args);
2085}
2086
c0d7ed8c
TT
2087/* Helper function for MULTI_SUBSCRIPT. */
2088
2089static struct value *
2090eval_multi_subscript (struct type *expect_type, struct expression *exp,
2091 enum noside noside, value *arg1,
2092 gdb::array_view<value *> args)
2093{
c0d7ed8c
TT
2094 for (value *arg2 : args)
2095 {
2096 if (binop_user_defined_p (MULTI_SUBSCRIPT, arg1, arg2))
2097 {
2098 arg1 = value_x_binop (arg1, arg2, MULTI_SUBSCRIPT, OP_NULL, noside);
2099 }
2100 else
2101 {
2102 arg1 = coerce_ref (arg1);
2103 struct type *type = check_typedef (value_type (arg1));
2104
2105 switch (type->code ())
2106 {
2107 case TYPE_CODE_PTR:
2108 case TYPE_CODE_ARRAY:
2109 case TYPE_CODE_STRING:
2110 arg1 = value_subscript (arg1, value_as_long (arg2));
2111 break;
2112
2113 default:
2114 if (type->name ())
2115 error (_("cannot subscript something of type `%s'"),
2116 type->name ());
2117 else
2118 error (_("cannot subscript requested type"));
2119 }
2120 }
2121 }
2122 return (arg1);
2123}
2124
085734dd
TT
2125namespace expr
2126{
2127
2128value *
2129objc_msgcall_operation::evaluate (struct type *expect_type,
2130 struct expression *exp,
2131 enum noside noside)
2132{
2133 enum noside sub_no_side = EVAL_NORMAL;
2134 struct type *selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
2135
2136 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2137 sub_no_side = EVAL_NORMAL;
2138 else
2139 sub_no_side = noside;
2140 value *target
2141 = std::get<1> (m_storage)->evaluate (selector_type, exp, sub_no_side);
2142
2143 if (value_as_long (target) == 0)
2144 sub_no_side = EVAL_AVOID_SIDE_EFFECTS;
2145 else
2146 sub_no_side = noside;
2147 std::vector<operation_up> &args = std::get<2> (m_storage);
2148 value **argvec = XALLOCAVEC (struct value *, args.size () + 3);
2149 argvec[0] = nullptr;
2150 argvec[1] = nullptr;
2151 for (int i = 0; i < args.size (); ++i)
2152 argvec[i + 2] = args[i]->evaluate_with_coercion (exp, sub_no_side);
2153 argvec[args.size () + 2] = nullptr;
2154
2155 return eval_op_objc_msgcall (expect_type, exp, noside, std::
2156 get<0> (m_storage), target,
2157 gdb::make_array_view (argvec,
2158 args.size () + 3));
2159}
2160
821e72d7
TT
2161value *
2162multi_subscript_operation::evaluate (struct type *expect_type,
2163 struct expression *exp,
2164 enum noside noside)
2165{
2166 value *arg1 = std::get<0> (m_storage)->evaluate_with_coercion (exp, noside);
2167 std::vector<operation_up> &values = std::get<1> (m_storage);
2168 value **argvec = XALLOCAVEC (struct value *, values.size ());
2169 for (int ix = 0; ix < values.size (); ++ix)
2170 argvec[ix] = values[ix]->evaluate_with_coercion (exp, noside);
2171 return eval_multi_subscript (expect_type, exp, noside, arg1,
2172 gdb::make_array_view (argvec, values.size ()));
085734dd
TT
2173}
2174
5019124b
TT
2175value *
2176logical_and_operation::evaluate (struct type *expect_type,
2177 struct expression *exp,
2178 enum noside noside)
2179{
2180 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
5019124b
TT
2181
2182 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2183 EVAL_AVOID_SIDE_EFFECTS);
2184
2185 if (binop_user_defined_p (BINOP_LOGICAL_AND, arg1, arg2))
2186 {
2187 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2188 return value_x_binop (arg1, arg2, BINOP_LOGICAL_AND, OP_NULL, noside);
2189 }
2190 else
2191 {
2192 int tem = value_logical_not (arg1);
2193 if (!tem)
2194 {
2195 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2196 tem = value_logical_not (arg2);
2197 }
2198 struct type *type = language_bool_type (exp->language_defn,
2199 exp->gdbarch);
2200 return value_from_longest (type, !tem);
2201 }
2202}
2203
2204value *
2205logical_or_operation::evaluate (struct type *expect_type,
2206 struct expression *exp,
2207 enum noside noside)
2208{
2209 value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
5019124b
TT
2210
2211 value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp,
2212 EVAL_AVOID_SIDE_EFFECTS);
2213
2214 if (binop_user_defined_p (BINOP_LOGICAL_OR, arg1, arg2))
2215 {
2216 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2217 return value_x_binop (arg1, arg2, BINOP_LOGICAL_OR, OP_NULL, noside);
2218 }
2219 else
2220 {
2221 int tem = value_logical_not (arg1);
2222 if (tem)
2223 {
2224 arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
2225 tem = value_logical_not (arg2);
2226 }
2227
2228 struct type *type = language_bool_type (exp->language_defn,
2229 exp->gdbarch);
2230 return value_from_longest (type, !tem);
2231 }
2232}
2233
e4479080
TT
2234value *
2235adl_func_operation::evaluate (struct type *expect_type,
2236 struct expression *exp,
2237 enum noside noside)
2238{
2239 std::vector<operation_up> &arg_ops = std::get<2> (m_storage);
2240 std::vector<value *> args (arg_ops.size ());
2241 for (int i = 0; i < arg_ops.size (); ++i)
2242 args[i] = arg_ops[i]->evaluate_with_coercion (exp, noside);
2243
2244 struct symbol *symp;
2245 find_overload_match (args, std::get<0> (m_storage).c_str (),
2246 NON_METHOD,
2247 nullptr, nullptr,
2248 nullptr, &symp, nullptr, 0, noside);
2249 if (SYMBOL_TYPE (symp)->code () == TYPE_CODE_ERROR)
2250 error_unknown_type (symp->print_name ());
2251 value *callee = evaluate_var_value (noside, std::get<1> (m_storage), symp);
2252 return evaluate_subexp_do_call (exp, noside, callee, args,
2253 nullptr, expect_type);
2254
2255}
2256
1c02eb30
TT
2257/* This function evaluates brace-initializers (in C/C++) for
2258 structure types. */
2259
2260struct value *
2261array_operation::evaluate_struct_tuple (struct value *struct_val,
2262 struct expression *exp,
2263 enum noside noside, int nargs)
2264{
2265 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2266 struct type *struct_type = check_typedef (value_type (struct_val));
2267 struct type *field_type;
2268 int fieldno = -1;
2269
2270 int idx = 0;
2271 while (--nargs >= 0)
2272 {
2273 struct value *val = NULL;
2274 int bitpos, bitsize;
2275 bfd_byte *addr;
2276
2277 fieldno++;
2278 /* Skip static fields. */
2279 while (fieldno < struct_type->num_fields ()
2280 && field_is_static (&struct_type->field (fieldno)))
2281 fieldno++;
2282 if (fieldno >= struct_type->num_fields ())
2283 error (_("too many initializers"));
2284 field_type = struct_type->field (fieldno).type ();
2285 if (field_type->code () == TYPE_CODE_UNION
2286 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
2287 error (_("don't know which variant you want to set"));
2288
2289 /* Here, struct_type is the type of the inner struct,
2290 while substruct_type is the type of the inner struct.
2291 These are the same for normal structures, but a variant struct
2292 contains anonymous union fields that contain substruct fields.
2293 The value fieldno is the index of the top-level (normal or
2294 anonymous union) field in struct_field, while the value
2295 subfieldno is the index of the actual real (named inner) field
2296 in substruct_type. */
2297
2298 field_type = struct_type->field (fieldno).type ();
2299 if (val == 0)
2300 val = in_args[idx++]->evaluate (field_type, exp, noside);
2301
2302 /* Now actually set the field in struct_val. */
2303
2304 /* Assign val to field fieldno. */
2305 if (value_type (val) != field_type)
2306 val = value_cast (field_type, val);
2307
2308 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
2309 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
2310 addr = value_contents_writeable (struct_val) + bitpos / 8;
2311 if (bitsize)
2312 modify_field (struct_type, addr,
2313 value_as_long (val), bitpos % 8, bitsize);
2314 else
2315 memcpy (addr, value_contents (val),
2316 TYPE_LENGTH (value_type (val)));
2317
2318 }
2319 return struct_val;
2320}
2321
2322value *
2323array_operation::evaluate (struct type *expect_type,
2324 struct expression *exp,
2325 enum noside noside)
2326{
2327 int tem;
2328 int tem2 = std::get<0> (m_storage);
2329 int tem3 = std::get<1> (m_storage);
2330 const std::vector<operation_up> &in_args = std::get<2> (m_storage);
2331 int nargs = tem3 - tem2 + 1;
2332 struct type *type = expect_type ? check_typedef (expect_type) : nullptr;
2333
0b2b0b82 2334 if (expect_type != nullptr
1c02eb30
TT
2335 && type->code () == TYPE_CODE_STRUCT)
2336 {
2337 struct value *rec = allocate_value (expect_type);
2338
2339 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
2340 return evaluate_struct_tuple (rec, exp, noside, nargs);
2341 }
2342
0b2b0b82 2343 if (expect_type != nullptr
1c02eb30
TT
2344 && type->code () == TYPE_CODE_ARRAY)
2345 {
2346 struct type *range_type = type->index_type ();
2347 struct type *element_type = TYPE_TARGET_TYPE (type);
2348 struct value *array = allocate_value (expect_type);
2349 int element_size = TYPE_LENGTH (check_typedef (element_type));
2350 LONGEST low_bound, high_bound, index;
2351
2352 if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
2353 {
2354 low_bound = 0;
2355 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
2356 }
2357 index = low_bound;
2358 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
2359 for (tem = nargs; --nargs >= 0;)
2360 {
2361 struct value *element;
2362
2363 element = in_args[index - low_bound]->evaluate (element_type,
2364 exp, noside);
2365 if (value_type (element) != element_type)
2366 element = value_cast (element_type, element);
2367 if (index > high_bound)
2368 /* To avoid memory corruption. */
2369 error (_("Too many array elements"));
2370 memcpy (value_contents_raw (array)
2371 + (index - low_bound) * element_size,
2372 value_contents (element),
2373 element_size);
2374 index++;
2375 }
2376 return array;
2377 }
2378
0b2b0b82 2379 if (expect_type != nullptr
1c02eb30
TT
2380 && type->code () == TYPE_CODE_SET)
2381 {
2382 struct value *set = allocate_value (expect_type);
2383 gdb_byte *valaddr = value_contents_raw (set);
2384 struct type *element_type = type->index_type ();
2385 struct type *check_type = element_type;
2386 LONGEST low_bound, high_bound;
2387
2388 /* Get targettype of elementtype. */
2389 while (check_type->code () == TYPE_CODE_RANGE
2390 || check_type->code () == TYPE_CODE_TYPEDEF)
2391 check_type = TYPE_TARGET_TYPE (check_type);
2392
2393 if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
2394 error (_("(power)set type with unknown size"));
2395 memset (valaddr, '\0', TYPE_LENGTH (type));
2396 int idx = 0;
2397 for (tem = 0; tem < nargs; tem++)
2398 {
2399 LONGEST range_low, range_high;
2400 struct type *range_low_type, *range_high_type;
2401 struct value *elem_val;
2402
2403 elem_val = in_args[idx++]->evaluate (element_type, exp, noside);
2404 range_low_type = range_high_type = value_type (elem_val);
2405 range_low = range_high = value_as_long (elem_val);
2406
2407 /* Check types of elements to avoid mixture of elements from
2408 different types. Also check if type of element is "compatible"
2409 with element type of powerset. */
2410 if (range_low_type->code () == TYPE_CODE_RANGE)
2411 range_low_type = TYPE_TARGET_TYPE (range_low_type);
2412 if (range_high_type->code () == TYPE_CODE_RANGE)
2413 range_high_type = TYPE_TARGET_TYPE (range_high_type);
2414 if ((range_low_type->code () != range_high_type->code ())
2415 || (range_low_type->code () == TYPE_CODE_ENUM
2416 && (range_low_type != range_high_type)))
2417 /* different element modes. */
2418 error (_("POWERSET tuple elements of different mode"));
2419 if ((check_type->code () != range_low_type->code ())
2420 || (check_type->code () == TYPE_CODE_ENUM
2421 && range_low_type != check_type))
2422 error (_("incompatible POWERSET tuple elements"));
2423 if (range_low > range_high)
2424 {
2425 warning (_("empty POWERSET tuple range"));
2426 continue;
2427 }
2428 if (range_low < low_bound || range_high > high_bound)
2429 error (_("POWERSET tuple element out of range"));
2430 range_low -= low_bound;
2431 range_high -= low_bound;
2432 for (; range_low <= range_high; range_low++)
2433 {
2434 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
2435
2436 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
2437 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
2438 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
2439 |= 1 << bit_index;
2440 }
2441 }
2442 return set;
2443 }
2444
2445 value **argvec = XALLOCAVEC (struct value *, nargs);
2446 for (tem = 0; tem < nargs; tem++)
2447 {
2448 /* Ensure that array expressions are coerced into pointer
2449 objects. */
2450 argvec[tem] = in_args[tem]->evaluate_with_coercion (exp, noside);
2451 }
1c02eb30
TT
2452 return value_array (tem2, tem3, argvec);
2453}
2454
821e72d7 2455}
085734dd 2456
c906108c 2457\f
13ea014a
TT
2458/* Helper for evaluate_subexp_for_address. */
2459
2460static value *
2461evaluate_subexp_for_address_base (struct expression *exp, enum noside noside,
2462 value *x)
2463{
2464 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2465 {
2466 struct type *type = check_typedef (value_type (x));
2467
2468 if (TYPE_IS_REFERENCE (type))
2469 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2470 not_lval);
2471 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2472 return value_zero (lookup_pointer_type (value_type (x)),
2473 not_lval);
2474 else
2475 error (_("Attempt to take address of "
2476 "value not located in memory."));
2477 }
2478 return value_addr (x);
2479}
2480
e2803273
TT
2481namespace expr
2482{
2483
2484value *
2485operation::evaluate_for_cast (struct type *expect_type,
2486 struct expression *exp,
2487 enum noside noside)
2488{
2489 value *val = evaluate (expect_type, exp, noside);
e2803273
TT
2490 return value_cast (expect_type, val);
2491}
2492
2493value *
2494operation::evaluate_for_address (struct expression *exp, enum noside noside)
2495{
2496 value *val = evaluate (nullptr, exp, noside);
2497 return evaluate_subexp_for_address_base (exp, noside, val);
2498}
2499
d5ab122c
TT
2500value *
2501scope_operation::evaluate_for_address (struct expression *exp,
2502 enum noside noside)
2503{
2504 value *x = value_aggregate_elt (std::get<0> (m_storage),
2505 std::get<1> (m_storage).c_str (),
2506 NULL, 1, noside);
2507 if (x == NULL)
2508 error (_("There is no field named %s"), std::get<1> (m_storage).c_str ());
2509 return x;
2510}
2511
876469ff
TT
2512value *
2513unop_ind_base_operation::evaluate_for_address (struct expression *exp,
2514 enum noside noside)
2515{
2516 value *x = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
2517
2518 /* We can't optimize out "&*" if there's a user-defined operator*. */
2519 if (unop_user_defined_p (UNOP_IND, x))
2520 {
2521 x = value_x_unop (x, UNOP_IND, noside);
2522 return evaluate_subexp_for_address_base (exp, noside, x);
2523 }
2524
2525 return coerce_array (x);
2526}
2527
0c8effa3
TT
2528value *
2529var_msym_value_operation::evaluate_for_address (struct expression *exp,
2530 enum noside noside)
2531{
2532 value *val = evaluate_var_msym_value (noside,
2533 std::get<1> (m_storage),
2534 std::get<0> (m_storage));
2535 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2536 {
2537 struct type *type = lookup_pointer_type (value_type (val));
2538 return value_zero (type, not_lval);
2539 }
2540 else
2541 return value_addr (val);
2542}
2543
cbc18219
TT
2544value *
2545unop_memval_operation::evaluate_for_address (struct expression *exp,
2546 enum noside noside)
2547{
2548 return value_cast (lookup_pointer_type (std::get<1> (m_storage)),
2549 std::get<0> (m_storage)->evaluate (nullptr, exp, noside));
2550}
2551
2552value *
2553unop_memval_type_operation::evaluate_for_address (struct expression *exp,
2554 enum noside noside)
2555{
2556 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2557 EVAL_AVOID_SIDE_EFFECTS);
2558 struct type *type = value_type (typeval);
2559 return value_cast (lookup_pointer_type (type),
2560 std::get<1> (m_storage)->evaluate (nullptr, exp, noside));
2561}
2562
e82a5afc
TT
2563value *
2564var_value_operation::evaluate_for_address (struct expression *exp,
2565 enum noside noside)
2566{
2567 symbol *var = std::get<0> (m_storage);
2568
2569 /* C++: The "address" of a reference should yield the address
2570 * of the object pointed to. Let value_addr() deal with it. */
2571 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
2572 return operation::evaluate_for_address (exp, noside);
2573
2574 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2575 {
2576 struct type *type = lookup_pointer_type (SYMBOL_TYPE (var));
2577 enum address_class sym_class = SYMBOL_CLASS (var);
2578
2579 if (sym_class == LOC_CONST
2580 || sym_class == LOC_CONST_BYTES
2581 || sym_class == LOC_REGISTER)
2582 error (_("Attempt to take address of register or constant."));
2583
2584 return value_zero (type, not_lval);
2585 }
2586 else
2587 return address_of_variable (var, std::get<1> (m_storage));
2588}
2589
2590value *
2591var_value_operation::evaluate_with_coercion (struct expression *exp,
2592 enum noside noside)
2593{
2594 struct symbol *var = std::get<0> (m_storage);
2595 struct type *type = check_typedef (SYMBOL_TYPE (var));
2596 if (type->code () == TYPE_CODE_ARRAY
2597 && !type->is_vector ()
2598 && CAST_IS_CONVERSION (exp->language_defn))
2599 {
2600 struct value *val = address_of_variable (var, std::get<1> (m_storage));
2601 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), val);
2602 }
2603 return evaluate (nullptr, exp, noside);
2604}
2605
2606}
2607
13ea014a
TT
2608/* Helper function for evaluating the size of a type. */
2609
2610static value *
2611evaluate_subexp_for_sizeof_base (struct expression *exp, struct type *type)
2612{
2613 /* FIXME: This should be size_t. */
2614 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2615 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2616 "When applied to a reference or a reference type, the result is
2617 the size of the referenced type." */
2618 type = check_typedef (type);
2619 if (exp->language_defn->la_language == language_cplus
2620 && (TYPE_IS_REFERENCE (type)))
2621 type = check_typedef (TYPE_TARGET_TYPE (type));
2622 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2623}
2624
e2803273
TT
2625namespace expr
2626{
2627
2628value *
2629operation::evaluate_for_sizeof (struct expression *exp, enum noside noside)
2630{
2631 value *val = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
2632 return evaluate_subexp_for_sizeof_base (exp, value_type (val));
2633}
2634
0c8effa3
TT
2635value *
2636var_msym_value_operation::evaluate_for_sizeof (struct expression *exp,
2637 enum noside noside)
2638
2639{
2640 minimal_symbol *msymbol = std::get<0> (m_storage);
2641 value *mval = evaluate_var_msym_value (noside,
2642 std::get<1> (m_storage),
2643 msymbol);
2644
2645 struct type *type = value_type (mval);
2646 if (type->code () == TYPE_CODE_ERROR)
2647 error_unknown_type (msymbol->print_name ());
2648
2649 /* FIXME: This should be size_t. */
2650 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2651 return value_from_longest (size_type, TYPE_LENGTH (type));
2652}
2653
224d6424
TT
2654value *
2655subscript_operation::evaluate_for_sizeof (struct expression *exp,
2656 enum noside noside)
2657{
2658 if (noside == EVAL_NORMAL)
2659 {
2660 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2661 EVAL_AVOID_SIDE_EFFECTS);
2662 struct type *type = check_typedef (value_type (val));
2663 if (type->code () == TYPE_CODE_ARRAY)
2664 {
2665 type = check_typedef (TYPE_TARGET_TYPE (type));
2666 if (type->code () == TYPE_CODE_ARRAY)
2667 {
2668 type = type->index_type ();
2669 /* Only re-evaluate the right hand side if the resulting type
2670 is a variable length type. */
2671 if (type->bounds ()->flag_bound_evaluated)
2672 {
2673 val = evaluate (nullptr, exp, EVAL_NORMAL);
2674 /* FIXME: This should be size_t. */
2675 struct type *size_type
2676 = builtin_type (exp->gdbarch)->builtin_int;
2677 return value_from_longest
2678 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
2679 }
2680 }
2681 }
2682 }
2683
2684 return operation::evaluate_for_sizeof (exp, noside);
2685}
2686
876469ff
TT
2687value *
2688unop_ind_base_operation::evaluate_for_sizeof (struct expression *exp,
2689 enum noside noside)
2690{
2691 value *val = std::get<0> (m_storage)->evaluate (nullptr, exp,
2692 EVAL_AVOID_SIDE_EFFECTS);
2693 struct type *type = check_typedef (value_type (val));
2694 if (type->code () != TYPE_CODE_PTR
2695 && !TYPE_IS_REFERENCE (type)
2696 && type->code () != TYPE_CODE_ARRAY)
2697 error (_("Attempt to take contents of a non-pointer value."));
2698 type = TYPE_TARGET_TYPE (type);
2699 if (is_dynamic_type (type))
2700 type = value_type (value_ind (val));
2701 /* FIXME: This should be size_t. */
2702 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2703 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2704}
2705
cbc18219
TT
2706value *
2707unop_memval_operation::evaluate_for_sizeof (struct expression *exp,
2708 enum noside noside)
2709{
2710 return evaluate_subexp_for_sizeof_base (exp, std::get<1> (m_storage));
2711}
2712
2713value *
2714unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp,
2715 enum noside noside)
2716{
2717 value *typeval = std::get<0> (m_storage)->evaluate (nullptr, exp,
2718 EVAL_AVOID_SIDE_EFFECTS);
2719 return evaluate_subexp_for_sizeof_base (exp, value_type (typeval));
2720}
2721
e82a5afc
TT
2722value *
2723var_value_operation::evaluate_for_sizeof (struct expression *exp,
2724 enum noside noside)
2725{
2726 struct type *type = SYMBOL_TYPE (std::get<0> (m_storage));
2727 if (is_dynamic_type (type))
2728 {
2729 value *val = evaluate (nullptr, exp, EVAL_NORMAL);
2730 type = value_type (val);
2731 if (type->code () == TYPE_CODE_ARRAY)
2732 {
2733 /* FIXME: This should be size_t. */
2734 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2735 if (type_not_allocated (type) || type_not_associated (type))
2736 return value_zero (size_type, not_lval);
2737 else if (is_dynamic_type (type->index_type ())
2738 && type->bounds ()->high.kind () == PROP_UNDEFINED)
2739 return allocate_optimized_out_value (size_type);
2740 }
2741 }
2742 return evaluate_subexp_for_sizeof_base (exp, type);
2743}
2744
0c8effa3
TT
2745value *
2746var_msym_value_operation::evaluate_for_cast (struct type *to_type,
2747 struct expression *exp,
2748 enum noside noside)
2749{
2750 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2751 return value_zero (to_type, not_lval);
2752
2753 value *val = evaluate_var_msym_value (noside,
2754 std::get<1> (m_storage),
2755 std::get<0> (m_storage));
2756
0c8effa3
TT
2757 val = value_cast (to_type, val);
2758
2759 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2760 if (VALUE_LVAL (val) == lval_memory)
2761 {
2762 if (value_lazy (val))
2763 value_fetch_lazy (val);
2764 VALUE_LVAL (val) = not_lval;
2765 }
2766 return val;
2767}
2768
e82a5afc
TT
2769value *
2770var_value_operation::evaluate_for_cast (struct type *to_type,
2771 struct expression *exp,
2772 enum noside noside)
2773{
2774 value *val = evaluate_var_value (noside,
2775 std::get<1> (m_storage),
2776 std::get<0> (m_storage));
2777
e82a5afc
TT
2778 val = value_cast (to_type, val);
2779
2780 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2781 if (VALUE_LVAL (val) == lval_memory)
2782 {
2783 if (value_lazy (val))
2784 value_fetch_lazy (val);
2785 VALUE_LVAL (val) = not_lval;
2786 }
2787 return val;
2788}
2789
0c8effa3
TT
2790}
2791
0963b4bd 2792/* Parse a type expression in the string [P..P+LENGTH). */
c906108c
SS
2793
2794struct type *
f5756acc 2795parse_and_eval_type (const char *p, int length)
c906108c 2796{
c5aa993b 2797 char *tmp = (char *) alloca (length + 4);
d7f9d729 2798
c5aa993b
JM
2799 tmp[0] = '(';
2800 memcpy (tmp + 1, p, length);
2801 tmp[length + 1] = ')';
2802 tmp[length + 2] = '0';
2803 tmp[length + 3] = '\0';
4d01a485 2804 expression_up expr = parse_expression (tmp);
1eaebe02
TT
2805 expr::unop_cast_operation *op
2806 = dynamic_cast<expr::unop_cast_operation *> (expr->op.get ());
2807 if (op == nullptr)
8a3fe4f8 2808 error (_("Internal error in eval_type."));
1eaebe02 2809 return op->get_type ();
c906108c 2810}
This page took 1.598315 seconds and 4 git commands to generate.