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