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