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