1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
24 #include "expression.h"
27 #include "gdbthread.h"
28 #include "language.h" /* For CAST_IS_CONVERSION. */
31 #include "objc-lang.h"
33 #include "parser-defs.h"
34 #include "cp-support.h"
37 #include "user-regs.h"
39 #include "gdb_obstack.h"
41 #include "typeprint.h"
47 /* Parse the string EXP as a C expression, evaluate it,
48 and return the result as a number. */
51 parse_and_eval_address (const char *exp
)
53 expression_up expr
= parse_expression (exp
);
55 return value_as_address (evaluate_expression (expr
.get ()));
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. */
61 parse_and_eval_long (const char *exp
)
63 expression_up expr
= parse_expression (exp
);
65 return value_as_long (evaluate_expression (expr
.get ()));
69 parse_and_eval (const char *exp
)
71 expression_up expr
= parse_expression (exp
);
73 return evaluate_expression (expr
.get ());
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. */
81 parse_to_comma_and_eval (const char **expp
)
83 expression_up expr
= parse_exp_1 (expp
, 0, nullptr, 1);
85 return evaluate_expression (expr
.get ());
89 /* See expression.h. */
92 expression::evaluate (struct type
*expect_type
, enum noside noside
)
94 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
95 if (target_has_execution ()
96 && language_defn
->la_language
== language_cplus
97 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
98 stack_temporaries
.emplace (inferior_thread ());
100 struct value
*retval
= op
->evaluate (expect_type
, this, noside
);
102 if (stack_temporaries
.has_value ()
103 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
104 retval
= value_non_lval (retval
);
112 evaluate_expression (struct expression
*exp
, struct type
*expect_type
)
114 return exp
->evaluate (expect_type
, EVAL_NORMAL
);
117 /* Evaluate an expression, avoiding all memory references
118 and getting a value whose type alone is correct. */
121 evaluate_type (struct expression
*exp
)
123 return exp
->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS
);
126 /* Find the current value of a watchpoint on EXP. Return the value in
127 *VALP and *RESULTP and the chain of intermediate and final values
128 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
131 If PRESERVE_ERRORS is true, then exceptions are passed through.
132 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
133 occurs while evaluating the expression, *RESULTP will be set to
134 NULL. *RESULTP may be a lazy value, if the result could not be
135 read from memory. It is used to determine whether a value is
136 user-specified (we should watch the whole value) or intermediate
137 (we should watch only the bit used to locate the final value).
139 If the final value, or any intermediate value, could not be read
140 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
141 set to any referenced values. *VALP will never be a lazy value.
142 This is the value which we store in struct breakpoint.
144 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
145 released from the value chain. If VAL_CHAIN is NULL, all generated
146 values will be left on the value chain. */
149 fetch_subexp_value (struct expression
*exp
,
151 struct value
**valp
, struct value
**resultp
,
152 std::vector
<value_ref_ptr
> *val_chain
,
153 bool preserve_errors
)
155 struct value
*mark
, *new_mark
, *result
;
163 /* Evaluate the expression. */
164 mark
= value_mark ();
169 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
171 catch (const gdb_exception
&ex
)
173 /* Ignore memory errors if we want watchpoints pointing at
174 inaccessible memory to still be created; otherwise, throw the
175 error to some higher catcher. */
179 if (!preserve_errors
)
188 new_mark
= value_mark ();
189 if (mark
== new_mark
)
194 /* Make sure it's not lazy, so that after the target stops again we
195 have a non-lazy previous value to compare with. */
198 if (!value_lazy (result
))
205 value_fetch_lazy (result
);
208 catch (const gdb_exception_error
&except
)
216 /* Return the chain of intermediate values. We use this to
217 decide which addresses to watch. */
218 *val_chain
= value_release_to_mark (mark
);
222 /* Promote value ARG1 as appropriate before performing a unary operation
224 If the result is not appropriate for any particular language then it
225 needs to patch this function. */
228 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
233 *arg1
= coerce_ref (*arg1
);
234 type1
= check_typedef (value_type (*arg1
));
236 if (is_integral_type (type1
))
238 switch (language
->la_language
)
241 /* Perform integral promotion for ANSI C/C++.
242 If not appropriate for any particular language
243 it needs to modify this function. */
245 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
247 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_int
))
248 *arg1
= value_cast (builtin_int
, *arg1
);
255 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
256 operation on those two operands.
257 If the result is not appropriate for any particular language then it
258 needs to patch this function. */
261 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
262 struct value
**arg1
, struct value
**arg2
)
264 struct type
*promoted_type
= NULL
;
268 *arg1
= coerce_ref (*arg1
);
269 *arg2
= coerce_ref (*arg2
);
271 type1
= check_typedef (value_type (*arg1
));
272 type2
= check_typedef (value_type (*arg2
));
274 if ((type1
->code () != TYPE_CODE_FLT
275 && type1
->code () != TYPE_CODE_DECFLOAT
276 && !is_integral_type (type1
))
277 || (type2
->code () != TYPE_CODE_FLT
278 && type2
->code () != TYPE_CODE_DECFLOAT
279 && !is_integral_type (type2
)))
282 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
285 if (type1
->code () == TYPE_CODE_DECFLOAT
286 || type2
->code () == TYPE_CODE_DECFLOAT
)
288 /* No promotion required. */
290 else if (type1
->code () == TYPE_CODE_FLT
291 || type2
->code () == TYPE_CODE_FLT
)
293 switch (language
->la_language
)
299 case language_opencl
:
300 /* No promotion required. */
304 /* For other languages the result type is unchanged from gdb
305 version 6.7 for backward compatibility.
306 If either arg was long double, make sure that value is also long
307 double. Otherwise use double. */
308 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (gdbarch
)
309 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (gdbarch
))
310 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
312 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
316 else if (type1
->code () == TYPE_CODE_BOOL
317 && type2
->code () == TYPE_CODE_BOOL
)
319 /* No promotion required. */
322 /* Integral operations here. */
323 /* FIXME: Also mixed integral/booleans, with result an integer. */
325 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
326 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
327 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
328 int is_unsigned1
= type1
->is_unsigned ();
329 int is_unsigned2
= type2
->is_unsigned ();
330 unsigned int result_len
;
331 int unsigned_operation
;
333 /* Determine type length and signedness after promotion for
335 if (promoted_len1
< TYPE_LENGTH (builtin
->builtin_int
))
338 promoted_len1
= TYPE_LENGTH (builtin
->builtin_int
);
340 if (promoted_len2
< TYPE_LENGTH (builtin
->builtin_int
))
343 promoted_len2
= TYPE_LENGTH (builtin
->builtin_int
);
346 if (promoted_len1
> promoted_len2
)
348 unsigned_operation
= is_unsigned1
;
349 result_len
= promoted_len1
;
351 else if (promoted_len2
> promoted_len1
)
353 unsigned_operation
= is_unsigned2
;
354 result_len
= promoted_len2
;
358 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
359 result_len
= promoted_len1
;
362 switch (language
->la_language
)
368 if (result_len
<= TYPE_LENGTH (builtin
->builtin_int
))
370 promoted_type
= (unsigned_operation
371 ? builtin
->builtin_unsigned_int
372 : builtin
->builtin_int
);
374 else if (result_len
<= TYPE_LENGTH (builtin
->builtin_long
))
376 promoted_type
= (unsigned_operation
377 ? builtin
->builtin_unsigned_long
378 : builtin
->builtin_long
);
382 promoted_type
= (unsigned_operation
383 ? builtin
->builtin_unsigned_long_long
384 : builtin
->builtin_long_long
);
387 case language_opencl
:
388 if (result_len
<= TYPE_LENGTH (lookup_signed_typename
393 ? lookup_unsigned_typename (language
, "int")
394 : lookup_signed_typename (language
, "int"));
396 else if (result_len
<= TYPE_LENGTH (lookup_signed_typename
401 ? lookup_unsigned_typename (language
, "long")
402 : lookup_signed_typename (language
,"long"));
406 /* For other languages the result type is unchanged from gdb
407 version 6.7 for backward compatibility.
408 If either arg was long long, make sure that value is also long
409 long. Otherwise use long. */
410 if (unsigned_operation
)
412 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
413 promoted_type
= builtin
->builtin_unsigned_long_long
;
415 promoted_type
= builtin
->builtin_unsigned_long
;
419 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
420 promoted_type
= builtin
->builtin_long_long
;
422 promoted_type
= builtin
->builtin_long
;
430 /* Promote both operands to common type. */
431 *arg1
= value_cast (promoted_type
, *arg1
);
432 *arg2
= value_cast (promoted_type
, *arg2
);
437 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
439 type
= check_typedef (type
);
440 if (TYPE_IS_REFERENCE (type
))
441 type
= TYPE_TARGET_TYPE (type
);
443 switch (type
->code ())
449 case TYPE_CODE_ARRAY
:
450 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
457 /* Represents a fake method with the given parameter types. This is
458 used by the parser to construct a temporary "expected" type for
459 method overload resolution. FLAGS is used as instance flags of the
460 new type, in order to be able to make the new type represent a
461 const/volatile overload. */
466 fake_method (type_instance_flags flags
,
467 int num_types
, struct type
**param_types
);
470 /* The constructed type. */
471 struct type
*type () { return &m_type
; }
474 struct type m_type
{};
475 main_type m_main_type
{};
478 fake_method::fake_method (type_instance_flags flags
,
479 int num_types
, struct type
**param_types
)
481 struct type
*type
= &m_type
;
483 TYPE_MAIN_TYPE (type
) = &m_main_type
;
484 TYPE_LENGTH (type
) = 1;
485 type
->set_code (TYPE_CODE_METHOD
);
486 TYPE_CHAIN (type
) = type
;
487 type
->set_instance_flags (flags
);
490 if (param_types
[num_types
- 1] == NULL
)
493 type
->set_has_varargs (true);
495 else if (check_typedef (param_types
[num_types
- 1])->code ()
499 /* Caller should have ensured this. */
500 gdb_assert (num_types
== 0);
501 type
->set_is_prototyped (true);
505 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
506 neither an objfile nor a gdbarch. As a result we must manually
507 allocate memory for auxiliary fields, and free the memory ourselves
508 when we are done with it. */
509 type
->set_num_fields (num_types
);
511 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
513 while (num_types
-- > 0)
514 type
->field (num_types
).set_type (param_types
[num_types
]);
517 fake_method::~fake_method ()
519 xfree (m_type
.fields ());
526 type_instance_operation::evaluate (struct type
*expect_type
,
527 struct expression
*exp
,
530 type_instance_flags flags
= std::get
<0> (m_storage
);
531 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
533 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
534 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
540 /* Helper for evaluating an OP_VAR_VALUE. */
543 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
545 /* JYG: We used to just return value_zero of the symbol type if
546 we're asked to avoid side effects. Otherwise we return
547 value_of_variable (...). However I'm not sure if
548 value_of_variable () has any side effect. We need a full value
549 object returned here for whatis_exp () to call evaluate_type ()
550 and then pass the full value to value_rtti_target_type () if we
551 are dealing with a pointer or reference to a base class and print
554 struct value
*ret
= NULL
;
558 ret
= value_of_variable (var
, blk
);
561 catch (const gdb_exception_error
&except
)
563 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
566 ret
= value_zero (SYMBOL_TYPE (var
), not_lval
);
577 var_value_operation::evaluate (struct type
*expect_type
,
578 struct expression
*exp
,
581 symbol
*var
= std::get
<0> (m_storage
);
582 if (SYMBOL_TYPE (var
)->code () == TYPE_CODE_ERROR
)
583 error_unknown_type (var
->print_name ());
584 return evaluate_var_value (noside
, std::get
<1> (m_storage
), var
);
587 } /* namespace expr */
589 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
592 evaluate_var_msym_value (enum noside noside
,
593 struct objfile
*objfile
, minimal_symbol
*msymbol
)
596 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
598 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
599 return value_zero (the_type
, not_lval
);
601 return value_at_lazy (the_type
, address
);
604 /* See expression.h. */
607 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
609 gdb::array_view
<value
*> argvec
,
610 const char *function_name
,
611 type
*default_return_type
)
614 error (_("Cannot evaluate function -- may be inlined"));
615 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
617 /* If the return type doesn't look like a function type,
618 call an error. This can happen if somebody tries to turn
619 a variable into a function call. */
621 type
*ftype
= value_type (callee
);
623 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
625 /* We don't know anything about what the internal
626 function might return, but we have to return
628 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
631 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
633 type
*return_type
= result_type_of_xmethod (callee
, argvec
);
635 if (return_type
== NULL
)
636 error (_("Xmethod is missing return type."));
637 return value_zero (return_type
, not_lval
);
639 else if (ftype
->code () == TYPE_CODE_FUNC
640 || ftype
->code () == TYPE_CODE_METHOD
)
642 if (ftype
->is_gnu_ifunc ())
644 CORE_ADDR address
= value_address (callee
);
645 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
647 if (resolved_type
!= NULL
)
648 ftype
= resolved_type
;
651 type
*return_type
= TYPE_TARGET_TYPE (ftype
);
653 if (return_type
== NULL
)
654 return_type
= default_return_type
;
656 if (return_type
== NULL
)
657 error_call_unknown_return_type (function_name
);
659 return allocate_value (return_type
);
662 error (_("Expression of type other than "
663 "\"Function returning ...\" used as function"));
665 switch (value_type (callee
)->code ())
667 case TYPE_CODE_INTERNAL_FUNCTION
:
668 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
669 callee
, argvec
.size (), argvec
.data ());
670 case TYPE_CODE_XMETHOD
:
671 return call_xmethod (callee
, argvec
);
673 return call_function_by_hand (callee
, default_return_type
, argvec
);
681 operation::evaluate_funcall (struct type
*expect_type
,
682 struct expression
*exp
,
684 const char *function_name
,
685 const std::vector
<operation_up
> &args
)
687 std::vector
<value
*> vals (args
.size ());
689 value
*callee
= evaluate_with_coercion (exp
, noside
);
690 struct type
*type
= value_type (callee
);
691 if (type
->code () == TYPE_CODE_PTR
)
692 type
= TYPE_TARGET_TYPE (type
);
693 for (int i
= 0; i
< args
.size (); ++i
)
695 if (i
< type
->num_fields ())
696 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
698 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
701 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
702 function_name
, expect_type
);
706 var_value_operation::evaluate_funcall (struct type
*expect_type
,
707 struct expression
*exp
,
709 const std::vector
<operation_up
> &args
)
711 if (!overload_resolution
712 || exp
->language_defn
->la_language
!= language_cplus
)
713 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
715 std::vector
<value
*> argvec (args
.size ());
716 for (int i
= 0; i
< args
.size (); ++i
)
717 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
720 find_overload_match (argvec
, NULL
, NON_METHOD
,
721 NULL
, std::get
<0> (m_storage
),
722 NULL
, &symp
, NULL
, 0, noside
);
724 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
725 error_unknown_type (symp
->print_name ());
726 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
728 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
729 nullptr, expect_type
);
733 scope_operation::evaluate_funcall (struct type
*expect_type
,
734 struct expression
*exp
,
736 const std::vector
<operation_up
> &args
)
738 if (!overload_resolution
739 || exp
->language_defn
->la_language
!= language_cplus
)
740 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
742 /* Unpack it locally so we can properly handle overload
744 const std::string
&name
= std::get
<1> (m_storage
);
745 struct type
*type
= std::get
<0> (m_storage
);
747 symbol
*function
= NULL
;
748 const char *function_name
= NULL
;
749 std::vector
<value
*> argvec (1 + args
.size ());
750 if (type
->code () == TYPE_CODE_NAMESPACE
)
752 function
= cp_lookup_symbol_namespace (type
->name (),
754 get_selected_block (0),
756 if (function
== NULL
)
757 error (_("No symbol \"%s\" in namespace \"%s\"."),
758 name
.c_str (), type
->name ());
762 gdb_assert (type
->code () == TYPE_CODE_STRUCT
763 || type
->code () == TYPE_CODE_UNION
);
764 function_name
= name
.c_str ();
766 /* We need a properly typed value for method lookup. */
767 argvec
[0] = value_zero (type
, lval_memory
);
770 for (int i
= 0; i
< args
.size (); ++i
)
771 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
772 gdb::array_view
<value
*> arg_view
= argvec
;
774 value
*callee
= nullptr;
775 if (function_name
!= nullptr)
779 find_overload_match (arg_view
, function_name
, METHOD
,
780 &argvec
[0], nullptr, &callee
, nullptr,
781 &static_memfuncp
, 0, noside
);
782 if (!static_memfuncp
)
784 /* For the time being, we don't handle this. */
785 error (_("Call to overloaded function %s requires "
790 arg_view
= arg_view
.slice (1);
795 arg_view
= arg_view
.slice (1);
796 find_overload_match (arg_view
, nullptr,
797 NON_METHOD
, nullptr, function
,
798 nullptr, &symp
, nullptr, 1, noside
);
799 callee
= value_of_variable (symp
, get_selected_block (0));
802 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
803 nullptr, expect_type
);
807 structop_member_base::evaluate_funcall (struct type
*expect_type
,
808 struct expression
*exp
,
810 const std::vector
<operation_up
> &args
)
812 /* First, evaluate the structure into lhs. */
814 if (opcode () == STRUCTOP_MEMBER
)
815 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
817 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
819 std::vector
<value
*> vals (args
.size () + 1);
820 gdb::array_view
<value
*> val_view
= vals
;
821 /* If the function is a virtual function, then the aggregate
822 value (providing the structure) plays its part by providing
823 the vtable. Otherwise, it is just along for the ride: call
824 the function directly. */
825 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
828 type
*a1_type
= check_typedef (value_type (rhs
));
829 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
831 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
832 callee
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
834 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
838 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
840 struct type
*type_ptr
841 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
842 struct type
*target_type_ptr
843 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
845 /* Now, convert this value to an address. */
846 lhs
= value_cast (type_ptr
, lhs
);
848 long mem_offset
= value_as_long (rhs
);
850 callee
= value_from_pointer (target_type_ptr
,
851 value_as_long (lhs
) + mem_offset
);
852 callee
= value_ind (callee
);
854 val_view
= val_view
.slice (1);
857 error (_("Non-pointer-to-member value used in pointer-to-member "
860 for (int i
= 0; i
< args
.size (); ++i
)
861 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
863 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
864 nullptr, expect_type
);
869 structop_base_operation::evaluate_funcall
870 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
871 const std::vector
<operation_up
> &args
)
873 std::vector
<value
*> vals (args
.size () + 1);
874 /* First, evaluate the structure into vals[0]. */
875 enum exp_opcode op
= opcode ();
876 if (op
== STRUCTOP_STRUCT
)
878 /* If v is a variable in a register, and the user types
879 v.method (), this will produce an error, because v has no
882 A possible way around this would be to allocate a copy of
883 the variable on the stack, copy in the contents, call the
884 function, and copy out the contents. I.e. convert this
885 from call by reference to call by copy-return (or
886 whatever it's called). However, this does not work
887 because it is not the same: the method being called could
888 stash a copy of the address, and then future uses through
889 that address (after the method returns) would be expected
890 to use the variable itself, not some copy of it. */
891 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
895 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
896 /* Check to see if the operator '->' has been overloaded.
897 If the operator has been overloaded replace vals[0] with the
898 value returned by the custom operator and continue
900 while (unop_user_defined_p (op
, vals
[0]))
902 struct value
*value
= nullptr;
905 value
= value_x_unop (vals
[0], op
, noside
);
907 catch (const gdb_exception_error
&except
)
909 if (except
.error
== NOT_FOUND_ERROR
)
919 for (int i
= 0; i
< args
.size (); ++i
)
920 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
921 gdb::array_view
<value
*> arg_view
= vals
;
925 const char *tstr
= std::get
<1> (m_storage
).c_str ();
926 if (overload_resolution
927 && exp
->language_defn
->la_language
== language_cplus
)
929 /* Language is C++, do some overload resolution before
931 value
*val0
= vals
[0];
932 find_overload_match (arg_view
, tstr
, METHOD
,
933 &val0
, nullptr, &callee
, nullptr,
934 &static_memfuncp
, 0, noside
);
938 /* Non-C++ case -- or no overload resolution. */
940 struct value
*temp
= vals
[0];
942 callee
= value_struct_elt (&temp
, &vals
[1], tstr
,
944 op
== STRUCTOP_STRUCT
945 ? "structure" : "structure pointer");
946 /* value_struct_elt updates temp with the correct value of the
947 ``this'' pointer if necessary, so modify it to reflect any
949 vals
[0] = value_from_longest (lookup_pointer_type (value_type (temp
)),
951 + value_embedded_offset (temp
));
954 /* Take out `this' if needed. */
956 arg_view
= arg_view
.slice (1);
958 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
959 nullptr, expect_type
);
963 } /* namespace expr */
965 /* Return true if type is integral or reference to integral */
968 is_integral_or_integral_reference (struct type
*type
)
970 if (is_integral_type (type
))
973 type
= check_typedef (type
);
974 return (type
!= nullptr
975 && TYPE_IS_REFERENCE (type
)
976 && is_integral_type (TYPE_TARGET_TYPE (type
)));
979 /* Helper function that implements the body of OP_SCOPE. */
982 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
984 struct type
*type
, const char *string
)
986 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
989 error (_("There is no field named %s"), string
);
993 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
996 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
997 enum noside noside
, symbol
*sym
)
999 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1000 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
1002 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1003 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1004 error (_("Symbol \"%s\" does not have any specific entry value"),
1005 sym
->print_name ());
1007 struct frame_info
*frame
= get_selected_frame (NULL
);
1008 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1011 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1014 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1015 enum noside noside
, bool outermost_p
,
1016 bound_minimal_symbol msymbol
)
1018 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1021 struct type
*type
= value_type (val
);
1022 if (type
->code () == TYPE_CODE_ERROR
1023 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1024 error_unknown_type (msymbol
.minsym
->print_name ());
1028 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1031 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1033 value
*func
, const char *var
)
1035 CORE_ADDR addr
= value_address (func
);
1036 const block
*blk
= block_for_pc (addr
);
1037 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1038 if (sym
.symbol
== NULL
)
1039 error (_("No symbol \"%s\" in specified context."), var
);
1040 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1043 /* Helper function that implements the body of OP_REGISTER. */
1046 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1047 enum noside noside
, const char *name
)
1052 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1053 name
, strlen (name
));
1055 error (_("Register $%s not available."), name
);
1057 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1058 a value with the appropriate register type. Unfortunately,
1059 we don't have easy access to the type of user registers.
1060 So for these registers, we fetch the register value regardless
1061 of the evaluation mode. */
1062 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1063 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1064 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1066 val
= value_of_register (regno
, get_selected_frame (NULL
));
1068 error (_("Value of register %s not available."), name
);
1073 /* Helper function that implements the body of OP_STRING. */
1076 eval_op_string (struct type
*expect_type
, struct expression
*exp
,
1077 enum noside noside
, int len
, const char *string
)
1079 struct type
*type
= language_string_char_type (exp
->language_defn
,
1081 return value_string (string
, len
, type
);
1084 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1087 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1091 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1092 return value_from_longest (selector_type
,
1093 lookup_child_selector (exp
->gdbarch
, sel
));
1096 /* Helper function that implements the body of BINOP_CONCAT. */
1099 eval_op_concat (struct type
*expect_type
, struct expression
*exp
,
1100 enum noside noside
, struct value
*arg1
, struct value
*arg2
)
1102 if (binop_user_defined_p (BINOP_CONCAT
, arg1
, arg2
))
1103 return value_x_binop (arg1
, arg2
, BINOP_CONCAT
, OP_NULL
, noside
);
1105 return value_concat (arg1
, arg2
);
1108 /* A helper function for TERNOP_SLICE. */
1111 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1113 struct value
*array
, struct value
*low
, struct value
*upper
)
1115 int lowbound
= value_as_long (low
);
1116 int upperbound
= value_as_long (upper
);
1117 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1120 /* A helper function for STRUCTOP_STRUCT. */
1123 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1125 struct value
*arg1
, const char *string
)
1127 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1129 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1130 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1134 /* A helper function for STRUCTOP_PTR. */
1137 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1139 struct value
*arg1
, const char *string
)
1141 /* Check to see if operator '->' has been overloaded. If so replace
1142 arg1 with the value returned by evaluating operator->(). */
1143 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1145 struct value
*value
= NULL
;
1148 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1151 catch (const gdb_exception_error
&except
)
1153 if (except
.error
== NOT_FOUND_ERROR
)
1162 /* JYG: if print object is on we need to replace the base type
1163 with rtti type in order to continue on with successful
1164 lookup of member / method only available in the rtti type. */
1166 struct type
*arg_type
= value_type (arg1
);
1167 struct type
*real_type
;
1168 int full
, using_enc
;
1170 struct value_print_options opts
;
1172 get_user_print_options (&opts
);
1173 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1174 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1176 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1179 arg1
= value_cast (real_type
, arg1
);
1183 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1184 NULL
, "structure pointer");
1185 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1186 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1190 /* A helper function for STRUCTOP_MEMBER. */
1193 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1195 struct value
*arg1
, struct value
*arg2
)
1200 struct type
*type
= check_typedef (value_type (arg2
));
1201 switch (type
->code ())
1203 case TYPE_CODE_METHODPTR
:
1204 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1205 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1208 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1209 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1210 return value_ind (arg2
);
1213 case TYPE_CODE_MEMBERPTR
:
1214 /* Now, convert these values to an address. */
1215 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1218 mem_offset
= value_as_long (arg2
);
1220 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1221 value_as_long (arg1
) + mem_offset
);
1222 return value_ind (arg3
);
1225 error (_("non-pointer-to-member value used "
1226 "in pointer-to-member construct"));
1230 /* A helper function for BINOP_ADD. */
1233 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1235 struct value
*arg1
, struct value
*arg2
)
1237 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1238 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1239 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1240 && is_integral_or_integral_reference (value_type (arg2
)))
1241 return value_ptradd (arg1
, value_as_long (arg2
));
1242 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
1243 && is_integral_or_integral_reference (value_type (arg1
)))
1244 return value_ptradd (arg2
, value_as_long (arg1
));
1247 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1248 return value_binop (arg1
, arg2
, BINOP_ADD
);
1252 /* A helper function for BINOP_SUB. */
1255 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1257 struct value
*arg1
, struct value
*arg2
)
1259 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1260 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1261 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1262 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
1264 /* FIXME -- should be ptrdiff_t */
1265 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1266 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1268 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1269 && is_integral_or_integral_reference (value_type (arg2
)))
1270 return value_ptradd (arg1
, - value_as_long (arg2
));
1273 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1274 return value_binop (arg1
, arg2
, BINOP_SUB
);
1278 /* Helper function for several different binary operations. */
1281 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1282 enum noside noside
, enum exp_opcode op
,
1283 struct value
*arg1
, struct value
*arg2
)
1285 if (binop_user_defined_p (op
, arg1
, arg2
))
1286 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1289 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1290 fudge arg2 to avoid division-by-zero, the caller is
1291 (theoretically) only looking for the type of the result. */
1292 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1293 /* ??? Do we really want to test for BINOP_MOD here?
1294 The implementation of value_binop gives it a well-defined
1297 || op
== BINOP_INTDIV
1300 && value_logical_not (arg2
))
1302 struct value
*v_one
;
1304 v_one
= value_one (value_type (arg2
));
1305 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1306 return value_binop (arg1
, v_one
, op
);
1310 /* For shift and integer exponentiation operations,
1311 only promote the first argument. */
1312 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1313 && is_integral_type (value_type (arg2
)))
1314 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1316 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1318 return value_binop (arg1
, arg2
, op
);
1323 /* A helper function for BINOP_SUBSCRIPT. */
1326 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1327 enum noside noside
, enum exp_opcode op
,
1328 struct value
*arg1
, struct value
*arg2
)
1330 if (binop_user_defined_p (op
, arg1
, arg2
))
1331 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1334 /* If the user attempts to subscript something that is not an
1335 array or pointer type (like a plain int variable for example),
1336 then report this as an error. */
1338 arg1
= coerce_ref (arg1
);
1339 struct type
*type
= check_typedef (value_type (arg1
));
1340 if (type
->code () != TYPE_CODE_ARRAY
1341 && type
->code () != TYPE_CODE_PTR
)
1344 error (_("cannot subscript something of type `%s'"),
1347 error (_("cannot subscript requested type"));
1350 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1351 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1353 return value_subscript (arg1
, value_as_long (arg2
));
1357 /* A helper function for BINOP_EQUAL. */
1360 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1361 enum noside noside
, enum exp_opcode op
,
1362 struct value
*arg1
, struct value
*arg2
)
1364 if (binop_user_defined_p (op
, arg1
, arg2
))
1366 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1370 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1371 int tem
= value_equal (arg1
, arg2
);
1372 struct type
*type
= language_bool_type (exp
->language_defn
,
1374 return value_from_longest (type
, (LONGEST
) tem
);
1378 /* A helper function for BINOP_NOTEQUAL. */
1381 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1382 enum noside noside
, enum exp_opcode op
,
1383 struct value
*arg1
, struct value
*arg2
)
1385 if (binop_user_defined_p (op
, arg1
, arg2
))
1387 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1391 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1392 int tem
= value_equal (arg1
, arg2
);
1393 struct type
*type
= language_bool_type (exp
->language_defn
,
1395 return value_from_longest (type
, (LONGEST
) ! tem
);
1399 /* A helper function for BINOP_LESS. */
1402 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1403 enum noside noside
, enum exp_opcode op
,
1404 struct value
*arg1
, struct value
*arg2
)
1406 if (binop_user_defined_p (op
, arg1
, arg2
))
1408 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1412 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1413 int tem
= value_less (arg1
, arg2
);
1414 struct type
*type
= language_bool_type (exp
->language_defn
,
1416 return value_from_longest (type
, (LONGEST
) tem
);
1420 /* A helper function for BINOP_GTR. */
1423 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1424 enum noside noside
, enum exp_opcode op
,
1425 struct value
*arg1
, struct value
*arg2
)
1427 if (binop_user_defined_p (op
, arg1
, arg2
))
1429 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1433 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1434 int tem
= value_less (arg2
, arg1
);
1435 struct type
*type
= language_bool_type (exp
->language_defn
,
1437 return value_from_longest (type
, (LONGEST
) tem
);
1441 /* A helper function for BINOP_GEQ. */
1444 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1445 enum noside noside
, enum exp_opcode op
,
1446 struct value
*arg1
, struct value
*arg2
)
1448 if (binop_user_defined_p (op
, arg1
, arg2
))
1450 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1454 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1455 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1456 struct type
*type
= language_bool_type (exp
->language_defn
,
1458 return value_from_longest (type
, (LONGEST
) tem
);
1462 /* A helper function for BINOP_LEQ. */
1465 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1466 enum noside noside
, enum exp_opcode op
,
1467 struct value
*arg1
, struct value
*arg2
)
1469 if (binop_user_defined_p (op
, arg1
, arg2
))
1471 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1475 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1476 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1477 struct type
*type
= language_bool_type (exp
->language_defn
,
1479 return value_from_longest (type
, (LONGEST
) tem
);
1483 /* A helper function for BINOP_REPEAT. */
1486 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1487 enum noside noside
, enum exp_opcode op
,
1488 struct value
*arg1
, struct value
*arg2
)
1490 struct type
*type
= check_typedef (value_type (arg2
));
1491 if (type
->code () != TYPE_CODE_INT
1492 && type
->code () != TYPE_CODE_ENUM
)
1493 error (_("Non-integral right operand for \"@\" operator."));
1494 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1496 return allocate_repeat_value (value_type (arg1
),
1497 longest_to_int (value_as_long (arg2
)));
1500 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1503 /* A helper function for UNOP_PLUS. */
1506 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1507 enum noside noside
, enum exp_opcode op
,
1510 if (unop_user_defined_p (op
, arg1
))
1511 return value_x_unop (arg1
, op
, noside
);
1514 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1515 return value_pos (arg1
);
1519 /* A helper function for UNOP_NEG. */
1522 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1523 enum noside noside
, enum exp_opcode op
,
1526 if (unop_user_defined_p (op
, arg1
))
1527 return value_x_unop (arg1
, op
, noside
);
1530 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1531 return value_neg (arg1
);
1535 /* A helper function for UNOP_COMPLEMENT. */
1538 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1539 enum noside noside
, enum exp_opcode op
,
1542 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1543 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1546 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1547 return value_complement (arg1
);
1551 /* A helper function for UNOP_LOGICAL_NOT. */
1554 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1555 enum noside noside
, enum exp_opcode op
,
1558 if (unop_user_defined_p (op
, arg1
))
1559 return value_x_unop (arg1
, op
, noside
);
1562 struct type
*type
= language_bool_type (exp
->language_defn
,
1564 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1568 /* A helper function for UNOP_IND. */
1571 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1575 struct type
*type
= check_typedef (value_type (arg1
));
1576 if (type
->code () == TYPE_CODE_METHODPTR
1577 || type
->code () == TYPE_CODE_MEMBERPTR
)
1578 error (_("Attempt to dereference pointer "
1579 "to member without an object"));
1580 if (unop_user_defined_p (UNOP_IND
, arg1
))
1581 return value_x_unop (arg1
, UNOP_IND
, noside
);
1582 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1584 type
= check_typedef (value_type (arg1
));
1586 /* If the type pointed to is dynamic then in order to resolve the
1587 dynamic properties we must actually dereference the pointer.
1588 There is a risk that this dereference will have side-effects
1589 in the inferior, but being able to print accurate type
1590 information seems worth the risk. */
1591 if ((type
->code () != TYPE_CODE_PTR
1592 && !TYPE_IS_REFERENCE (type
))
1593 || !is_dynamic_type (TYPE_TARGET_TYPE (type
)))
1595 if (type
->code () == TYPE_CODE_PTR
1596 || TYPE_IS_REFERENCE (type
)
1597 /* In C you can dereference an array to get the 1st elt. */
1598 || type
->code () == TYPE_CODE_ARRAY
)
1599 return value_zero (TYPE_TARGET_TYPE (type
),
1601 else if (type
->code () == TYPE_CODE_INT
)
1602 /* GDB allows dereferencing an int. */
1603 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1606 error (_("Attempt to take contents of a non-pointer value."));
1610 /* Allow * on an integer so we can cast it to whatever we want.
1611 This returns an int, which seems like the most C-like thing to
1612 do. "long long" variables are rare enough that
1613 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1614 if (type
->code () == TYPE_CODE_INT
)
1615 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1616 (CORE_ADDR
) value_as_address (arg1
));
1617 return value_ind (arg1
);
1620 /* A helper function for UNOP_ALIGNOF. */
1623 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1627 struct type
*type
= value_type (arg1
);
1628 /* FIXME: This should be size_t. */
1629 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1630 ULONGEST align
= type_align (type
);
1632 error (_("could not determine alignment of type"));
1633 return value_from_longest (size_type
, align
);
1636 /* A helper function for UNOP_MEMVAL. */
1639 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1641 struct value
*arg1
, struct type
*type
)
1643 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1644 return value_zero (type
, lval_memory
);
1646 return value_at_lazy (type
, value_as_address (arg1
));
1649 /* A helper function for UNOP_PREINCREMENT. */
1652 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1653 enum noside noside
, enum exp_opcode op
,
1656 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1658 else if (unop_user_defined_p (op
, arg1
))
1660 return value_x_unop (arg1
, op
, noside
);
1665 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1666 arg2
= value_ptradd (arg1
, 1);
1669 struct value
*tmp
= arg1
;
1671 arg2
= value_one (value_type (arg1
));
1672 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1673 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1676 return value_assign (arg1
, arg2
);
1680 /* A helper function for UNOP_PREDECREMENT. */
1683 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1684 enum noside noside
, enum exp_opcode op
,
1687 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1689 else if (unop_user_defined_p (op
, arg1
))
1691 return value_x_unop (arg1
, op
, noside
);
1696 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1697 arg2
= value_ptradd (arg1
, -1);
1700 struct value
*tmp
= arg1
;
1702 arg2
= value_one (value_type (arg1
));
1703 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1704 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1707 return value_assign (arg1
, arg2
);
1711 /* A helper function for UNOP_POSTINCREMENT. */
1714 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1715 enum noside noside
, enum exp_opcode op
,
1718 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1720 else if (unop_user_defined_p (op
, arg1
))
1722 return value_x_unop (arg1
, op
, noside
);
1726 struct value
*arg3
= value_non_lval (arg1
);
1729 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1730 arg2
= value_ptradd (arg1
, 1);
1733 struct value
*tmp
= arg1
;
1735 arg2
= value_one (value_type (arg1
));
1736 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1737 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1740 value_assign (arg1
, arg2
);
1745 /* A helper function for UNOP_POSTDECREMENT. */
1748 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1749 enum noside noside
, enum exp_opcode op
,
1752 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1754 else if (unop_user_defined_p (op
, arg1
))
1756 return value_x_unop (arg1
, op
, noside
);
1760 struct value
*arg3
= value_non_lval (arg1
);
1763 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1764 arg2
= value_ptradd (arg1
, -1);
1767 struct value
*tmp
= arg1
;
1769 arg2
= value_one (value_type (arg1
));
1770 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1771 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1774 value_assign (arg1
, arg2
);
1779 /* A helper function for OP_TYPE. */
1782 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1783 enum noside noside
, struct type
*type
)
1785 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1786 return allocate_value (type
);
1788 error (_("Attempt to use a type name as an expression"));
1791 /* A helper function for BINOP_ASSIGN_MODIFY. */
1794 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1795 enum noside noside
, enum exp_opcode op
,
1796 struct value
*arg1
, struct value
*arg2
)
1798 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1800 if (binop_user_defined_p (op
, arg1
, arg2
))
1801 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1802 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1804 && is_integral_type (value_type (arg2
)))
1805 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1806 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1808 && is_integral_type (value_type (arg2
)))
1809 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1812 struct value
*tmp
= arg1
;
1814 /* For shift and integer exponentiation operations,
1815 only promote the first argument. */
1816 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1817 && is_integral_type (value_type (arg2
)))
1818 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1820 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1822 arg2
= value_binop (tmp
, arg2
, op
);
1824 return value_assign (arg1
, arg2
);
1827 /* Note that ARGS needs 2 empty slots up front and must end with a
1829 static struct value
*
1830 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1831 enum noside noside
, CORE_ADDR selector
,
1832 value
*target
, gdb::array_view
<value
*> args
)
1834 CORE_ADDR responds_selector
= 0;
1835 CORE_ADDR method_selector
= 0;
1837 int struct_return
= 0;
1839 struct value
*msg_send
= NULL
;
1840 struct value
*msg_send_stret
= NULL
;
1841 int gnu_runtime
= 0;
1843 struct value
*method
= NULL
;
1844 struct value
*called_method
= NULL
;
1846 struct type
*selector_type
= NULL
;
1847 struct type
*long_type
;
1850 struct value
*ret
= NULL
;
1855 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1856 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1858 if (value_as_long (target
) == 0)
1859 return value_from_longest (long_type
, 0);
1861 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1864 /* Find the method dispatch (Apple runtime) or method lookup
1865 (GNU runtime) function for Objective-C. These will be used
1866 to lookup the symbol information for the method. If we
1867 can't find any symbol information, then we'll use these to
1868 call the method, otherwise we can call the method
1869 directly. The msg_send_stret function is used in the special
1870 case of a method that returns a structure (Apple runtime
1874 type
= selector_type
;
1876 type
= lookup_function_type (type
);
1877 type
= lookup_pointer_type (type
);
1878 type
= lookup_function_type (type
);
1879 type
= lookup_pointer_type (type
);
1881 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1883 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1885 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1886 msg_send_stret
= value_from_pointer (type
,
1887 value_as_address (msg_send_stret
));
1891 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1892 /* Special dispatcher for methods returning structs. */
1894 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1897 /* Verify the target object responds to this method. The
1898 standard top-level 'Object' class uses a different name for
1899 the verification method than the non-standard, but more
1900 often used, 'NSObject' class. Make sure we check for both. */
1903 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1904 if (responds_selector
== 0)
1906 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1908 if (responds_selector
== 0)
1909 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1912 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1913 if (method_selector
== 0)
1915 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1917 if (method_selector
== 0)
1918 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1920 /* Call the verification method, to make sure that the target
1921 class implements the desired method. */
1923 argvec
[0] = msg_send
;
1925 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1926 argvec
[3] = value_from_longest (long_type
, selector
);
1929 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1932 /* Function objc_msg_lookup returns a pointer. */
1934 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1936 if (value_as_long (ret
) == 0)
1937 error (_("Target does not respond to this message selector."));
1939 /* Call "methodForSelector:" method, to get the address of a
1940 function method that implements this selector for this
1941 class. If we can find a symbol at that address, then we
1942 know the return type, parameter types etc. (that's a good
1945 argvec
[0] = msg_send
;
1947 argvec
[2] = value_from_longest (long_type
, method_selector
);
1948 argvec
[3] = value_from_longest (long_type
, selector
);
1951 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1955 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1958 /* ret should now be the selector. */
1960 addr
= value_as_long (ret
);
1963 struct symbol
*sym
= NULL
;
1965 /* The address might point to a function descriptor;
1966 resolve it to the actual code address instead. */
1967 addr
= gdbarch_convert_from_func_ptr_addr (exp
->gdbarch
, addr
,
1968 current_top_target ());
1970 /* Is it a high_level symbol? */
1971 sym
= find_pc_function (addr
);
1973 method
= value_of_variable (sym
, 0);
1976 /* If we found a method with symbol information, check to see
1977 if it returns a struct. Otherwise assume it doesn't. */
1982 struct type
*val_type
;
1984 funaddr
= find_function_addr (method
, &val_type
);
1986 block_for_pc (funaddr
);
1988 val_type
= check_typedef (val_type
);
1990 if ((val_type
== NULL
)
1991 || (val_type
->code () == TYPE_CODE_ERROR
))
1993 if (expect_type
!= NULL
)
1994 val_type
= expect_type
;
1997 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2000 else if (expect_type
!= NULL
)
2002 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2003 check_typedef (expect_type
));
2006 /* Found a function symbol. Now we will substitute its
2007 value in place of the message dispatcher (obj_msgSend),
2008 so that we call the method directly instead of thru
2009 the dispatcher. The main reason for doing this is that
2010 we can now evaluate the return value and parameter values
2011 according to their known data types, in case we need to
2012 do things like promotion, dereferencing, special handling
2013 of structs and doubles, etc.
2015 We want to use the type signature of 'method', but still
2016 jump to objc_msgSend() or objc_msgSend_stret() to better
2017 mimic the behavior of the runtime. */
2021 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
2022 error (_("method address has symbol information "
2023 "with non-function type; skipping"));
2025 /* Create a function pointer of the appropriate type, and
2026 replace its value with the value of msg_send or
2027 msg_send_stret. We must use a pointer here, as
2028 msg_send and msg_send_stret are of pointer type, and
2029 the representation may be different on systems that use
2030 function descriptors. */
2033 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2034 value_as_address (msg_send_stret
));
2037 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2038 value_as_address (msg_send
));
2043 called_method
= msg_send_stret
;
2045 called_method
= msg_send
;
2049 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2051 /* If the return type doesn't look like a function type,
2052 call an error. This can happen if somebody tries to
2053 turn a variable into a function call. This is here
2054 because people often want to call, eg, strcmp, which
2055 gdb doesn't know is a function. If gdb isn't asked for
2056 it's opinion (ie. through "whatis"), it won't offer
2059 struct type
*callee_type
= value_type (called_method
);
2061 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2062 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2063 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2067 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2068 return allocate_value (expect_type
);
2070 return allocate_value (callee_type
);
2073 error (_("Expression of type other than "
2074 "\"method returning ...\" used as a method"));
2077 /* Now depending on whether we found a symbol for the method,
2078 we will either call the runtime dispatcher or the method
2082 args
[1] = value_from_longest (long_type
, selector
);
2084 if (gnu_runtime
&& (method
!= NULL
))
2086 /* Function objc_msg_lookup returns a pointer. */
2087 struct type
*tem_type
= value_type (called_method
);
2088 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2089 deprecated_set_value_type (called_method
, tem_type
);
2090 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2093 return call_function_by_hand (called_method
, NULL
, args
);
2096 /* Helper function for MULTI_SUBSCRIPT. */
2098 static struct value
*
2099 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2100 enum noside noside
, value
*arg1
,
2101 gdb::array_view
<value
*> args
)
2103 for (value
*arg2
: args
)
2105 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2107 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2111 arg1
= coerce_ref (arg1
);
2112 struct type
*type
= check_typedef (value_type (arg1
));
2114 switch (type
->code ())
2117 case TYPE_CODE_ARRAY
:
2118 case TYPE_CODE_STRING
:
2119 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2124 error (_("cannot subscript something of type `%s'"),
2127 error (_("cannot subscript requested type"));
2138 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2139 struct expression
*exp
,
2142 enum noside sub_no_side
= EVAL_NORMAL
;
2143 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2145 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2146 sub_no_side
= EVAL_NORMAL
;
2148 sub_no_side
= noside
;
2150 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2152 if (value_as_long (target
) == 0)
2153 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2155 sub_no_side
= noside
;
2156 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2157 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2158 argvec
[0] = nullptr;
2159 argvec
[1] = nullptr;
2160 for (int i
= 0; i
< args
.size (); ++i
)
2161 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2162 argvec
[args
.size () + 2] = nullptr;
2164 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2165 get
<0> (m_storage
), target
,
2166 gdb::make_array_view (argvec
,
2171 multi_subscript_operation::evaluate (struct type
*expect_type
,
2172 struct expression
*exp
,
2175 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2176 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2177 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2178 for (int ix
= 0; ix
< values
.size (); ++ix
)
2179 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2180 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2181 gdb::make_array_view (argvec
, values
.size ()));
2185 logical_and_operation::evaluate (struct type
*expect_type
,
2186 struct expression
*exp
,
2189 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2191 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2192 EVAL_AVOID_SIDE_EFFECTS
);
2194 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2196 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2197 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2201 int tem
= value_logical_not (arg1
);
2204 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2205 tem
= value_logical_not (arg2
);
2207 struct type
*type
= language_bool_type (exp
->language_defn
,
2209 return value_from_longest (type
, !tem
);
2214 logical_or_operation::evaluate (struct type
*expect_type
,
2215 struct expression
*exp
,
2218 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2220 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2221 EVAL_AVOID_SIDE_EFFECTS
);
2223 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2225 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2226 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2230 int tem
= value_logical_not (arg1
);
2233 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2234 tem
= value_logical_not (arg2
);
2237 struct type
*type
= language_bool_type (exp
->language_defn
,
2239 return value_from_longest (type
, !tem
);
2244 adl_func_operation::evaluate (struct type
*expect_type
,
2245 struct expression
*exp
,
2248 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2249 std::vector
<value
*> args (arg_ops
.size ());
2250 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2251 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2253 struct symbol
*symp
;
2254 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2257 nullptr, &symp
, nullptr, 0, noside
);
2258 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
2259 error_unknown_type (symp
->print_name ());
2260 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2261 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2262 nullptr, expect_type
);
2266 /* This function evaluates brace-initializers (in C/C++) for
2270 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2271 struct expression
*exp
,
2272 enum noside noside
, int nargs
)
2274 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2275 struct type
*struct_type
= check_typedef (value_type (struct_val
));
2276 struct type
*field_type
;
2280 while (--nargs
>= 0)
2282 struct value
*val
= NULL
;
2283 int bitpos
, bitsize
;
2287 /* Skip static fields. */
2288 while (fieldno
< struct_type
->num_fields ()
2289 && field_is_static (&struct_type
->field (fieldno
)))
2291 if (fieldno
>= struct_type
->num_fields ())
2292 error (_("too many initializers"));
2293 field_type
= struct_type
->field (fieldno
).type ();
2294 if (field_type
->code () == TYPE_CODE_UNION
2295 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
2296 error (_("don't know which variant you want to set"));
2298 /* Here, struct_type is the type of the inner struct,
2299 while substruct_type is the type of the inner struct.
2300 These are the same for normal structures, but a variant struct
2301 contains anonymous union fields that contain substruct fields.
2302 The value fieldno is the index of the top-level (normal or
2303 anonymous union) field in struct_field, while the value
2304 subfieldno is the index of the actual real (named inner) field
2305 in substruct_type. */
2307 field_type
= struct_type
->field (fieldno
).type ();
2309 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2311 /* Now actually set the field in struct_val. */
2313 /* Assign val to field fieldno. */
2314 if (value_type (val
) != field_type
)
2315 val
= value_cast (field_type
, val
);
2317 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2318 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
2319 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
2321 modify_field (struct_type
, addr
,
2322 value_as_long (val
), bitpos
% 8, bitsize
);
2324 memcpy (addr
, value_contents (val
),
2325 TYPE_LENGTH (value_type (val
)));
2332 array_operation::evaluate (struct type
*expect_type
,
2333 struct expression
*exp
,
2337 int tem2
= std::get
<0> (m_storage
);
2338 int tem3
= std::get
<1> (m_storage
);
2339 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2340 int nargs
= tem3
- tem2
+ 1;
2341 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2343 if (expect_type
!= nullptr
2344 && type
->code () == TYPE_CODE_STRUCT
)
2346 struct value
*rec
= allocate_value (expect_type
);
2348 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
2349 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2352 if (expect_type
!= nullptr
2353 && type
->code () == TYPE_CODE_ARRAY
)
2355 struct type
*range_type
= type
->index_type ();
2356 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2357 struct value
*array
= allocate_value (expect_type
);
2358 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
2359 LONGEST low_bound
, high_bound
, index
;
2361 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2364 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
2367 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
2368 for (tem
= nargs
; --nargs
>= 0;)
2370 struct value
*element
;
2372 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2374 if (value_type (element
) != element_type
)
2375 element
= value_cast (element_type
, element
);
2376 if (index
> high_bound
)
2377 /* To avoid memory corruption. */
2378 error (_("Too many array elements"));
2379 memcpy (value_contents_raw (array
)
2380 + (index
- low_bound
) * element_size
,
2381 value_contents (element
),
2388 if (expect_type
!= nullptr
2389 && type
->code () == TYPE_CODE_SET
)
2391 struct value
*set
= allocate_value (expect_type
);
2392 gdb_byte
*valaddr
= value_contents_raw (set
);
2393 struct type
*element_type
= type
->index_type ();
2394 struct type
*check_type
= element_type
;
2395 LONGEST low_bound
, high_bound
;
2397 /* Get targettype of elementtype. */
2398 while (check_type
->code () == TYPE_CODE_RANGE
2399 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2400 check_type
= TYPE_TARGET_TYPE (check_type
);
2402 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2403 error (_("(power)set type with unknown size"));
2404 memset (valaddr
, '\0', TYPE_LENGTH (type
));
2406 for (tem
= 0; tem
< nargs
; tem
++)
2408 LONGEST range_low
, range_high
;
2409 struct type
*range_low_type
, *range_high_type
;
2410 struct value
*elem_val
;
2412 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2413 range_low_type
= range_high_type
= value_type (elem_val
);
2414 range_low
= range_high
= value_as_long (elem_val
);
2416 /* Check types of elements to avoid mixture of elements from
2417 different types. Also check if type of element is "compatible"
2418 with element type of powerset. */
2419 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2420 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
2421 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2422 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
2423 if ((range_low_type
->code () != range_high_type
->code ())
2424 || (range_low_type
->code () == TYPE_CODE_ENUM
2425 && (range_low_type
!= range_high_type
)))
2426 /* different element modes. */
2427 error (_("POWERSET tuple elements of different mode"));
2428 if ((check_type
->code () != range_low_type
->code ())
2429 || (check_type
->code () == TYPE_CODE_ENUM
2430 && range_low_type
!= check_type
))
2431 error (_("incompatible POWERSET tuple elements"));
2432 if (range_low
> range_high
)
2434 warning (_("empty POWERSET tuple range"));
2437 if (range_low
< low_bound
|| range_high
> high_bound
)
2438 error (_("POWERSET tuple element out of range"));
2439 range_low
-= low_bound
;
2440 range_high
-= low_bound
;
2441 for (; range_low
<= range_high
; range_low
++)
2443 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2445 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2446 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2447 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2454 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2455 for (tem
= 0; tem
< nargs
; tem
++)
2457 /* Ensure that array expressions are coerced into pointer
2459 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2461 return value_array (tem2
, tem3
, argvec
);
2467 /* Helper for evaluate_subexp_for_address. */
2470 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2473 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2475 struct type
*type
= check_typedef (value_type (x
));
2477 if (TYPE_IS_REFERENCE (type
))
2478 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2480 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2481 return value_zero (lookup_pointer_type (value_type (x
)),
2484 error (_("Attempt to take address of "
2485 "value not located in memory."));
2487 return value_addr (x
);
2494 operation::evaluate_for_cast (struct type
*expect_type
,
2495 struct expression
*exp
,
2498 value
*val
= evaluate (expect_type
, exp
, noside
);
2499 return value_cast (expect_type
, val
);
2503 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2505 value
*val
= evaluate (nullptr, exp
, noside
);
2506 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2510 scope_operation::evaluate_for_address (struct expression
*exp
,
2513 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2514 std::get
<1> (m_storage
).c_str (),
2517 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2522 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2525 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2527 /* We can't optimize out "&*" if there's a user-defined operator*. */
2528 if (unop_user_defined_p (UNOP_IND
, x
))
2530 x
= value_x_unop (x
, UNOP_IND
, noside
);
2531 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2534 return coerce_array (x
);
2538 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2541 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2542 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2543 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2545 struct type
*type
= lookup_pointer_type (value_type (val
));
2546 return value_zero (type
, not_lval
);
2549 return value_addr (val
);
2553 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2556 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2557 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2561 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2564 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2565 EVAL_AVOID_SIDE_EFFECTS
);
2566 struct type
*type
= value_type (typeval
);
2567 return value_cast (lookup_pointer_type (type
),
2568 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2572 var_value_operation::evaluate_for_address (struct expression
*exp
,
2575 symbol
*var
= std::get
<0> (m_storage
);
2577 /* C++: The "address" of a reference should yield the address
2578 * of the object pointed to. Let value_addr() deal with it. */
2579 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2580 return operation::evaluate_for_address (exp
, noside
);
2582 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2584 struct type
*type
= lookup_pointer_type (SYMBOL_TYPE (var
));
2585 enum address_class sym_class
= SYMBOL_CLASS (var
);
2587 if (sym_class
== LOC_CONST
2588 || sym_class
== LOC_CONST_BYTES
2589 || sym_class
== LOC_REGISTER
)
2590 error (_("Attempt to take address of register or constant."));
2592 return value_zero (type
, not_lval
);
2595 return address_of_variable (var
, std::get
<1> (m_storage
));
2599 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2602 struct symbol
*var
= std::get
<0> (m_storage
);
2603 struct type
*type
= check_typedef (SYMBOL_TYPE (var
));
2604 if (type
->code () == TYPE_CODE_ARRAY
2605 && !type
->is_vector ()
2606 && CAST_IS_CONVERSION (exp
->language_defn
))
2608 struct value
*val
= address_of_variable (var
, std::get
<1> (m_storage
));
2609 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)), val
);
2611 return evaluate (nullptr, exp
, noside
);
2616 /* Helper function for evaluating the size of a type. */
2619 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2621 /* FIXME: This should be size_t. */
2622 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2623 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2624 "When applied to a reference or a reference type, the result is
2625 the size of the referenced type." */
2626 type
= check_typedef (type
);
2627 if (exp
->language_defn
->la_language
== language_cplus
2628 && (TYPE_IS_REFERENCE (type
)))
2629 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2630 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2637 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2639 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2640 return evaluate_subexp_for_sizeof_base (exp
, value_type (val
));
2644 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2648 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2649 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2651 struct type
*type
= value_type (mval
);
2652 if (type
->code () == TYPE_CODE_ERROR
)
2653 error_unknown_type (b
.minsym
->print_name ());
2655 /* FIXME: This should be size_t. */
2656 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2657 return value_from_longest (size_type
, TYPE_LENGTH (type
));
2661 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2664 if (noside
== EVAL_NORMAL
)
2666 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2667 EVAL_AVOID_SIDE_EFFECTS
);
2668 struct type
*type
= check_typedef (value_type (val
));
2669 if (type
->code () == TYPE_CODE_ARRAY
)
2671 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2672 if (type
->code () == TYPE_CODE_ARRAY
)
2674 type
= type
->index_type ();
2675 /* Only re-evaluate the right hand side if the resulting type
2676 is a variable length type. */
2677 if (type
->bounds ()->flag_bound_evaluated
)
2679 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2680 /* FIXME: This should be size_t. */
2681 struct type
*size_type
2682 = builtin_type (exp
->gdbarch
)->builtin_int
;
2683 return value_from_longest
2684 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
2690 return operation::evaluate_for_sizeof (exp
, noside
);
2694 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2697 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2698 EVAL_AVOID_SIDE_EFFECTS
);
2699 struct type
*type
= check_typedef (value_type (val
));
2700 if (type
->code () != TYPE_CODE_PTR
2701 && !TYPE_IS_REFERENCE (type
)
2702 && type
->code () != TYPE_CODE_ARRAY
)
2703 error (_("Attempt to take contents of a non-pointer value."));
2704 type
= TYPE_TARGET_TYPE (type
);
2705 if (is_dynamic_type (type
))
2706 type
= value_type (value_ind (val
));
2707 /* FIXME: This should be size_t. */
2708 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2709 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2713 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2716 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2720 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2723 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2724 EVAL_AVOID_SIDE_EFFECTS
);
2725 return evaluate_subexp_for_sizeof_base (exp
, value_type (typeval
));
2729 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2732 struct type
*type
= SYMBOL_TYPE (std::get
<0> (m_storage
));
2733 if (is_dynamic_type (type
))
2735 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2736 type
= value_type (val
);
2737 if (type
->code () == TYPE_CODE_ARRAY
)
2739 /* FIXME: This should be size_t. */
2740 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2741 if (type_not_allocated (type
) || type_not_associated (type
))
2742 return value_zero (size_type
, not_lval
);
2743 else if (is_dynamic_type (type
->index_type ())
2744 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2745 return allocate_optimized_out_value (size_type
);
2748 return evaluate_subexp_for_sizeof_base (exp
, type
);
2752 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2753 struct expression
*exp
,
2756 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2757 return value_zero (to_type
, not_lval
);
2759 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2760 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2762 val
= value_cast (to_type
, val
);
2764 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2765 if (VALUE_LVAL (val
) == lval_memory
)
2767 if (value_lazy (val
))
2768 value_fetch_lazy (val
);
2769 VALUE_LVAL (val
) = not_lval
;
2775 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2776 struct expression
*exp
,
2779 value
*val
= evaluate_var_value (noside
,
2780 std::get
<1> (m_storage
),
2781 std::get
<0> (m_storage
));
2783 val
= value_cast (to_type
, val
);
2785 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2786 if (VALUE_LVAL (val
) == lval_memory
)
2788 if (value_lazy (val
))
2789 value_fetch_lazy (val
);
2790 VALUE_LVAL (val
) = not_lval
;
2797 /* Parse a type expression in the string [P..P+LENGTH). */
2800 parse_and_eval_type (const char *p
, int length
)
2802 char *tmp
= (char *) alloca (length
+ 4);
2805 memcpy (tmp
+ 1, p
, length
);
2806 tmp
[length
+ 1] = ')';
2807 tmp
[length
+ 2] = '0';
2808 tmp
[length
+ 3] = '\0';
2809 expression_up expr
= parse_expression (tmp
);
2810 expr::unop_cast_operation
*op
2811 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2813 error (_("Internal error in eval_type."));
2814 return op
->get_type ();