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