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 for (int i
= 0; i
< args
.size (); ++i
)
691 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
693 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
694 function_name
, expect_type
);
698 var_value_operation::evaluate_funcall (struct type
*expect_type
,
699 struct expression
*exp
,
701 const std::vector
<operation_up
> &args
)
703 if (!overload_resolution
704 || exp
->language_defn
->la_language
!= language_cplus
)
705 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
707 std::vector
<value
*> argvec (args
.size ());
708 for (int i
= 0; i
< args
.size (); ++i
)
709 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
712 find_overload_match (argvec
, NULL
, NON_METHOD
,
713 NULL
, std::get
<0> (m_storage
),
714 NULL
, &symp
, NULL
, 0, noside
);
716 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
717 error_unknown_type (symp
->print_name ());
718 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
720 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
721 nullptr, expect_type
);
725 scope_operation::evaluate_funcall (struct type
*expect_type
,
726 struct expression
*exp
,
728 const std::vector
<operation_up
> &args
)
730 if (!overload_resolution
731 || exp
->language_defn
->la_language
!= language_cplus
)
732 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
734 /* Unpack it locally so we can properly handle overload
736 const std::string
&name
= std::get
<1> (m_storage
);
737 struct type
*type
= std::get
<0> (m_storage
);
739 symbol
*function
= NULL
;
740 const char *function_name
= NULL
;
741 std::vector
<value
*> argvec (1 + args
.size ());
742 if (type
->code () == TYPE_CODE_NAMESPACE
)
744 function
= cp_lookup_symbol_namespace (type
->name (),
746 get_selected_block (0),
748 if (function
== NULL
)
749 error (_("No symbol \"%s\" in namespace \"%s\"."),
750 name
.c_str (), type
->name ());
754 gdb_assert (type
->code () == TYPE_CODE_STRUCT
755 || type
->code () == TYPE_CODE_UNION
);
756 function_name
= name
.c_str ();
758 /* We need a properly typed value for method lookup. */
759 argvec
[0] = value_zero (type
, lval_memory
);
762 for (int i
= 0; i
< args
.size (); ++i
)
763 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
764 gdb::array_view
<value
*> arg_view
= argvec
;
766 value
*callee
= nullptr;
767 if (function_name
!= nullptr)
771 find_overload_match (arg_view
, function_name
, METHOD
,
772 &argvec
[0], nullptr, &callee
, nullptr,
773 &static_memfuncp
, 0, noside
);
774 if (!static_memfuncp
)
776 /* For the time being, we don't handle this. */
777 error (_("Call to overloaded function %s requires "
782 arg_view
= arg_view
.slice (1);
787 arg_view
= arg_view
.slice (1);
788 find_overload_match (arg_view
, nullptr,
789 NON_METHOD
, nullptr, function
,
790 nullptr, &symp
, nullptr, 1, noside
);
791 callee
= value_of_variable (symp
, get_selected_block (0));
794 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
795 nullptr, expect_type
);
799 structop_member_base::evaluate_funcall (struct type
*expect_type
,
800 struct expression
*exp
,
802 const std::vector
<operation_up
> &args
)
804 /* First, evaluate the structure into lhs. */
806 if (opcode () == STRUCTOP_MEMBER
)
807 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
809 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
811 std::vector
<value
*> vals (args
.size () + 1);
812 gdb::array_view
<value
*> val_view
= vals
;
813 /* If the function is a virtual function, then the aggregate
814 value (providing the structure) plays its part by providing
815 the vtable. Otherwise, it is just along for the ride: call
816 the function directly. */
817 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
820 type
*a1_type
= check_typedef (value_type (rhs
));
821 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
823 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
824 callee
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
826 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
830 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
832 struct type
*type_ptr
833 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
834 struct type
*target_type_ptr
835 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
837 /* Now, convert this value to an address. */
838 lhs
= value_cast (type_ptr
, lhs
);
840 long mem_offset
= value_as_long (rhs
);
842 callee
= value_from_pointer (target_type_ptr
,
843 value_as_long (lhs
) + mem_offset
);
844 callee
= value_ind (callee
);
846 val_view
= val_view
.slice (1);
849 error (_("Non-pointer-to-member value used in pointer-to-member "
852 for (int i
= 0; i
< args
.size (); ++i
)
853 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
855 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
856 nullptr, expect_type
);
861 structop_base_operation::evaluate_funcall
862 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
863 const std::vector
<operation_up
> &args
)
865 std::vector
<value
*> vals (args
.size () + 1);
866 /* First, evaluate the structure into vals[0]. */
867 enum exp_opcode op
= opcode ();
868 if (op
== STRUCTOP_STRUCT
)
870 /* If v is a variable in a register, and the user types
871 v.method (), this will produce an error, because v has no
874 A possible way around this would be to allocate a copy of
875 the variable on the stack, copy in the contents, call the
876 function, and copy out the contents. I.e. convert this
877 from call by reference to call by copy-return (or
878 whatever it's called). However, this does not work
879 because it is not the same: the method being called could
880 stash a copy of the address, and then future uses through
881 that address (after the method returns) would be expected
882 to use the variable itself, not some copy of it. */
883 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
887 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
888 /* Check to see if the operator '->' has been overloaded.
889 If the operator has been overloaded replace vals[0] with the
890 value returned by the custom operator and continue
892 while (unop_user_defined_p (op
, vals
[0]))
894 struct value
*value
= nullptr;
897 value
= value_x_unop (vals
[0], op
, noside
);
899 catch (const gdb_exception_error
&except
)
901 if (except
.error
== NOT_FOUND_ERROR
)
911 for (int i
= 0; i
< args
.size (); ++i
)
912 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
913 gdb::array_view
<value
*> arg_view
= vals
;
917 const char *tstr
= std::get
<1> (m_storage
).c_str ();
918 if (overload_resolution
919 && exp
->language_defn
->la_language
== language_cplus
)
921 /* Language is C++, do some overload resolution before
923 value
*val0
= vals
[0];
924 find_overload_match (arg_view
, tstr
, METHOD
,
925 &val0
, nullptr, &callee
, nullptr,
926 &static_memfuncp
, 0, noside
);
930 /* Non-C++ case -- or no overload resolution. */
932 struct value
*temp
= vals
[0];
934 callee
= value_struct_elt (&temp
, &vals
[1], tstr
,
936 op
== STRUCTOP_STRUCT
937 ? "structure" : "structure pointer");
938 /* value_struct_elt updates temp with the correct value of the
939 ``this'' pointer if necessary, so modify it to reflect any
941 vals
[0] = value_from_longest (lookup_pointer_type (value_type (temp
)),
943 + value_embedded_offset (temp
));
946 /* Take out `this' if needed. */
948 arg_view
= arg_view
.slice (1);
950 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
951 nullptr, expect_type
);
955 } /* namespace expr */
957 /* Return true if type is integral or reference to integral */
960 is_integral_or_integral_reference (struct type
*type
)
962 if (is_integral_type (type
))
965 type
= check_typedef (type
);
966 return (type
!= nullptr
967 && TYPE_IS_REFERENCE (type
)
968 && is_integral_type (TYPE_TARGET_TYPE (type
)));
971 /* Helper function that implements the body of OP_SCOPE. */
974 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
976 struct type
*type
, const char *string
)
978 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
981 error (_("There is no field named %s"), string
);
985 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
988 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
989 enum noside noside
, symbol
*sym
)
991 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
992 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
994 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
995 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
996 error (_("Symbol \"%s\" does not have any specific entry value"),
999 struct frame_info
*frame
= get_selected_frame (NULL
);
1000 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1003 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1006 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1007 enum noside noside
, bool outermost_p
,
1008 minimal_symbol
*msymbol
, struct objfile
*objfile
)
1010 value
*val
= evaluate_var_msym_value (noside
, objfile
, msymbol
);
1012 struct type
*type
= value_type (val
);
1013 if (type
->code () == TYPE_CODE_ERROR
1014 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1015 error_unknown_type (msymbol
->print_name ());
1019 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1022 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1024 value
*func
, const char *var
)
1026 CORE_ADDR addr
= value_address (func
);
1027 const block
*blk
= block_for_pc (addr
);
1028 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1029 if (sym
.symbol
== NULL
)
1030 error (_("No symbol \"%s\" in specified context."), var
);
1031 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1034 /* Helper function that implements the body of OP_REGISTER. */
1037 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1038 enum noside noside
, const char *name
)
1043 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1044 name
, strlen (name
));
1046 error (_("Register $%s not available."), name
);
1048 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1049 a value with the appropriate register type. Unfortunately,
1050 we don't have easy access to the type of user registers.
1051 So for these registers, we fetch the register value regardless
1052 of the evaluation mode. */
1053 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1054 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1055 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1057 val
= value_of_register (regno
, get_selected_frame (NULL
));
1059 error (_("Value of register %s not available."), name
);
1064 /* Helper function that implements the body of OP_STRING. */
1067 eval_op_string (struct type
*expect_type
, struct expression
*exp
,
1068 enum noside noside
, int len
, const char *string
)
1070 struct type
*type
= language_string_char_type (exp
->language_defn
,
1072 return value_string (string
, len
, type
);
1075 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1078 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1082 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1083 return value_from_longest (selector_type
,
1084 lookup_child_selector (exp
->gdbarch
, sel
));
1087 /* Helper function that implements the body of BINOP_CONCAT. */
1090 eval_op_concat (struct type
*expect_type
, struct expression
*exp
,
1091 enum noside noside
, struct value
*arg1
, struct value
*arg2
)
1093 if (binop_user_defined_p (BINOP_CONCAT
, arg1
, arg2
))
1094 return value_x_binop (arg1
, arg2
, BINOP_CONCAT
, OP_NULL
, noside
);
1096 return value_concat (arg1
, arg2
);
1099 /* A helper function for TERNOP_SLICE. */
1102 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1104 struct value
*array
, struct value
*low
, struct value
*upper
)
1106 int lowbound
= value_as_long (low
);
1107 int upperbound
= value_as_long (upper
);
1108 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1111 /* A helper function for STRUCTOP_STRUCT. */
1114 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1116 struct value
*arg1
, const char *string
)
1118 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1120 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1121 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1125 /* A helper function for STRUCTOP_PTR. */
1128 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1130 struct value
*arg1
, const char *string
)
1132 /* Check to see if operator '->' has been overloaded. If so replace
1133 arg1 with the value returned by evaluating operator->(). */
1134 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1136 struct value
*value
= NULL
;
1139 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1142 catch (const gdb_exception_error
&except
)
1144 if (except
.error
== NOT_FOUND_ERROR
)
1153 /* JYG: if print object is on we need to replace the base type
1154 with rtti type in order to continue on with successful
1155 lookup of member / method only available in the rtti type. */
1157 struct type
*arg_type
= value_type (arg1
);
1158 struct type
*real_type
;
1159 int full
, using_enc
;
1161 struct value_print_options opts
;
1163 get_user_print_options (&opts
);
1164 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1165 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1167 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1170 arg1
= value_cast (real_type
, arg1
);
1174 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1175 NULL
, "structure pointer");
1176 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1177 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1181 /* A helper function for STRUCTOP_MEMBER. */
1184 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1186 struct value
*arg1
, struct value
*arg2
)
1191 struct type
*type
= check_typedef (value_type (arg2
));
1192 switch (type
->code ())
1194 case TYPE_CODE_METHODPTR
:
1195 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1196 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1199 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1200 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1201 return value_ind (arg2
);
1204 case TYPE_CODE_MEMBERPTR
:
1205 /* Now, convert these values to an address. */
1206 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1209 mem_offset
= value_as_long (arg2
);
1211 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1212 value_as_long (arg1
) + mem_offset
);
1213 return value_ind (arg3
);
1216 error (_("non-pointer-to-member value used "
1217 "in pointer-to-member construct"));
1221 /* A helper function for BINOP_ADD. */
1224 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1226 struct value
*arg1
, struct value
*arg2
)
1228 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1229 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1230 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1231 && is_integral_or_integral_reference (value_type (arg2
)))
1232 return value_ptradd (arg1
, value_as_long (arg2
));
1233 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
1234 && is_integral_or_integral_reference (value_type (arg1
)))
1235 return value_ptradd (arg2
, value_as_long (arg1
));
1238 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1239 return value_binop (arg1
, arg2
, BINOP_ADD
);
1243 /* A helper function for BINOP_SUB. */
1246 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1248 struct value
*arg1
, struct value
*arg2
)
1250 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1251 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1252 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1253 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
1255 /* FIXME -- should be ptrdiff_t */
1256 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1257 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1259 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1260 && is_integral_or_integral_reference (value_type (arg2
)))
1261 return value_ptradd (arg1
, - value_as_long (arg2
));
1264 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1265 return value_binop (arg1
, arg2
, BINOP_SUB
);
1269 /* Helper function for several different binary operations. */
1272 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1273 enum noside noside
, enum exp_opcode op
,
1274 struct value
*arg1
, struct value
*arg2
)
1276 if (binop_user_defined_p (op
, arg1
, arg2
))
1277 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1280 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1281 fudge arg2 to avoid division-by-zero, the caller is
1282 (theoretically) only looking for the type of the result. */
1283 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1284 /* ??? Do we really want to test for BINOP_MOD here?
1285 The implementation of value_binop gives it a well-defined
1288 || op
== BINOP_INTDIV
1291 && value_logical_not (arg2
))
1293 struct value
*v_one
;
1295 v_one
= value_one (value_type (arg2
));
1296 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1297 return value_binop (arg1
, v_one
, op
);
1301 /* For shift and integer exponentiation operations,
1302 only promote the first argument. */
1303 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1304 && is_integral_type (value_type (arg2
)))
1305 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1307 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1309 return value_binop (arg1
, arg2
, op
);
1314 /* A helper function for BINOP_SUBSCRIPT. */
1317 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1318 enum noside noside
, enum exp_opcode op
,
1319 struct value
*arg1
, struct value
*arg2
)
1321 if (binop_user_defined_p (op
, arg1
, arg2
))
1322 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1325 /* If the user attempts to subscript something that is not an
1326 array or pointer type (like a plain int variable for example),
1327 then report this as an error. */
1329 arg1
= coerce_ref (arg1
);
1330 struct type
*type
= check_typedef (value_type (arg1
));
1331 if (type
->code () != TYPE_CODE_ARRAY
1332 && type
->code () != TYPE_CODE_PTR
)
1335 error (_("cannot subscript something of type `%s'"),
1338 error (_("cannot subscript requested type"));
1341 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1342 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1344 return value_subscript (arg1
, value_as_long (arg2
));
1348 /* A helper function for BINOP_EQUAL. */
1351 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1352 enum noside noside
, enum exp_opcode op
,
1353 struct value
*arg1
, struct value
*arg2
)
1355 if (binop_user_defined_p (op
, arg1
, arg2
))
1357 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1361 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1362 int tem
= value_equal (arg1
, arg2
);
1363 struct type
*type
= language_bool_type (exp
->language_defn
,
1365 return value_from_longest (type
, (LONGEST
) tem
);
1369 /* A helper function for BINOP_NOTEQUAL. */
1372 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1373 enum noside noside
, enum exp_opcode op
,
1374 struct value
*arg1
, struct value
*arg2
)
1376 if (binop_user_defined_p (op
, arg1
, arg2
))
1378 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1382 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1383 int tem
= value_equal (arg1
, arg2
);
1384 struct type
*type
= language_bool_type (exp
->language_defn
,
1386 return value_from_longest (type
, (LONGEST
) ! tem
);
1390 /* A helper function for BINOP_LESS. */
1393 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1394 enum noside noside
, enum exp_opcode op
,
1395 struct value
*arg1
, struct value
*arg2
)
1397 if (binop_user_defined_p (op
, arg1
, arg2
))
1399 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1403 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1404 int tem
= value_less (arg1
, arg2
);
1405 struct type
*type
= language_bool_type (exp
->language_defn
,
1407 return value_from_longest (type
, (LONGEST
) tem
);
1411 /* A helper function for BINOP_GTR. */
1414 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1415 enum noside noside
, enum exp_opcode op
,
1416 struct value
*arg1
, struct value
*arg2
)
1418 if (binop_user_defined_p (op
, arg1
, arg2
))
1420 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1424 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1425 int tem
= value_less (arg2
, arg1
);
1426 struct type
*type
= language_bool_type (exp
->language_defn
,
1428 return value_from_longest (type
, (LONGEST
) tem
);
1432 /* A helper function for BINOP_GEQ. */
1435 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1436 enum noside noside
, enum exp_opcode op
,
1437 struct value
*arg1
, struct value
*arg2
)
1439 if (binop_user_defined_p (op
, arg1
, arg2
))
1441 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1445 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1446 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1447 struct type
*type
= language_bool_type (exp
->language_defn
,
1449 return value_from_longest (type
, (LONGEST
) tem
);
1453 /* A helper function for BINOP_LEQ. */
1456 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1457 enum noside noside
, enum exp_opcode op
,
1458 struct value
*arg1
, struct value
*arg2
)
1460 if (binop_user_defined_p (op
, arg1
, arg2
))
1462 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1466 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1467 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1468 struct type
*type
= language_bool_type (exp
->language_defn
,
1470 return value_from_longest (type
, (LONGEST
) tem
);
1474 /* A helper function for BINOP_REPEAT. */
1477 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1478 enum noside noside
, enum exp_opcode op
,
1479 struct value
*arg1
, struct value
*arg2
)
1481 struct type
*type
= check_typedef (value_type (arg2
));
1482 if (type
->code () != TYPE_CODE_INT
1483 && type
->code () != TYPE_CODE_ENUM
)
1484 error (_("Non-integral right operand for \"@\" operator."));
1485 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1487 return allocate_repeat_value (value_type (arg1
),
1488 longest_to_int (value_as_long (arg2
)));
1491 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1494 /* A helper function for UNOP_PLUS. */
1497 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1498 enum noside noside
, enum exp_opcode op
,
1501 if (unop_user_defined_p (op
, arg1
))
1502 return value_x_unop (arg1
, op
, noside
);
1505 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1506 return value_pos (arg1
);
1510 /* A helper function for UNOP_NEG. */
1513 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1514 enum noside noside
, enum exp_opcode op
,
1517 if (unop_user_defined_p (op
, arg1
))
1518 return value_x_unop (arg1
, op
, noside
);
1521 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1522 return value_neg (arg1
);
1526 /* A helper function for UNOP_COMPLEMENT. */
1529 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1530 enum noside noside
, enum exp_opcode op
,
1533 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1534 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1537 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1538 return value_complement (arg1
);
1542 /* A helper function for UNOP_LOGICAL_NOT. */
1545 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1546 enum noside noside
, enum exp_opcode op
,
1549 if (unop_user_defined_p (op
, arg1
))
1550 return value_x_unop (arg1
, op
, noside
);
1553 struct type
*type
= language_bool_type (exp
->language_defn
,
1555 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1559 /* A helper function for UNOP_IND. */
1562 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1566 struct type
*type
= check_typedef (value_type (arg1
));
1567 if (type
->code () == TYPE_CODE_METHODPTR
1568 || type
->code () == TYPE_CODE_MEMBERPTR
)
1569 error (_("Attempt to dereference pointer "
1570 "to member without an object"));
1571 if (unop_user_defined_p (UNOP_IND
, arg1
))
1572 return value_x_unop (arg1
, UNOP_IND
, noside
);
1573 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1575 type
= check_typedef (value_type (arg1
));
1577 /* If the type pointed to is dynamic then in order to resolve the
1578 dynamic properties we must actually dereference the pointer.
1579 There is a risk that this dereference will have side-effects
1580 in the inferior, but being able to print accurate type
1581 information seems worth the risk. */
1582 if ((type
->code () != TYPE_CODE_PTR
1583 && !TYPE_IS_REFERENCE (type
))
1584 || !is_dynamic_type (TYPE_TARGET_TYPE (type
)))
1586 if (type
->code () == TYPE_CODE_PTR
1587 || TYPE_IS_REFERENCE (type
)
1588 /* In C you can dereference an array to get the 1st elt. */
1589 || type
->code () == TYPE_CODE_ARRAY
)
1590 return value_zero (TYPE_TARGET_TYPE (type
),
1592 else if (type
->code () == TYPE_CODE_INT
)
1593 /* GDB allows dereferencing an int. */
1594 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1597 error (_("Attempt to take contents of a non-pointer value."));
1601 /* Allow * on an integer so we can cast it to whatever we want.
1602 This returns an int, which seems like the most C-like thing to
1603 do. "long long" variables are rare enough that
1604 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1605 if (type
->code () == TYPE_CODE_INT
)
1606 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1607 (CORE_ADDR
) value_as_address (arg1
));
1608 return value_ind (arg1
);
1611 /* A helper function for UNOP_ALIGNOF. */
1614 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1618 struct type
*type
= value_type (arg1
);
1619 /* FIXME: This should be size_t. */
1620 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1621 ULONGEST align
= type_align (type
);
1623 error (_("could not determine alignment of type"));
1624 return value_from_longest (size_type
, align
);
1627 /* A helper function for UNOP_MEMVAL. */
1630 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1632 struct value
*arg1
, struct type
*type
)
1634 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1635 return value_zero (type
, lval_memory
);
1637 return value_at_lazy (type
, value_as_address (arg1
));
1640 /* A helper function for UNOP_PREINCREMENT. */
1643 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1644 enum noside noside
, enum exp_opcode op
,
1647 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1649 else if (unop_user_defined_p (op
, arg1
))
1651 return value_x_unop (arg1
, op
, noside
);
1656 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1657 arg2
= value_ptradd (arg1
, 1);
1660 struct value
*tmp
= arg1
;
1662 arg2
= value_one (value_type (arg1
));
1663 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1664 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1667 return value_assign (arg1
, arg2
);
1671 /* A helper function for UNOP_PREDECREMENT. */
1674 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1675 enum noside noside
, enum exp_opcode op
,
1678 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1680 else if (unop_user_defined_p (op
, arg1
))
1682 return value_x_unop (arg1
, op
, noside
);
1687 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1688 arg2
= value_ptradd (arg1
, -1);
1691 struct value
*tmp
= arg1
;
1693 arg2
= value_one (value_type (arg1
));
1694 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1695 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1698 return value_assign (arg1
, arg2
);
1702 /* A helper function for UNOP_POSTINCREMENT. */
1705 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1706 enum noside noside
, enum exp_opcode op
,
1709 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1711 else if (unop_user_defined_p (op
, arg1
))
1713 return value_x_unop (arg1
, op
, noside
);
1717 struct value
*arg3
= value_non_lval (arg1
);
1720 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1721 arg2
= value_ptradd (arg1
, 1);
1724 struct value
*tmp
= arg1
;
1726 arg2
= value_one (value_type (arg1
));
1727 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1728 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1731 value_assign (arg1
, arg2
);
1736 /* A helper function for UNOP_POSTDECREMENT. */
1739 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1740 enum noside noside
, enum exp_opcode op
,
1743 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1745 else if (unop_user_defined_p (op
, arg1
))
1747 return value_x_unop (arg1
, op
, noside
);
1751 struct value
*arg3
= value_non_lval (arg1
);
1754 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1755 arg2
= value_ptradd (arg1
, -1);
1758 struct value
*tmp
= arg1
;
1760 arg2
= value_one (value_type (arg1
));
1761 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1762 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1765 value_assign (arg1
, arg2
);
1770 /* A helper function for OP_TYPE. */
1773 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1774 enum noside noside
, struct type
*type
)
1776 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1777 return allocate_value (type
);
1779 error (_("Attempt to use a type name as an expression"));
1782 /* A helper function for BINOP_ASSIGN_MODIFY. */
1785 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1786 enum noside noside
, enum exp_opcode op
,
1787 struct value
*arg1
, struct value
*arg2
)
1789 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1791 if (binop_user_defined_p (op
, arg1
, arg2
))
1792 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1793 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1795 && is_integral_type (value_type (arg2
)))
1796 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1797 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1799 && is_integral_type (value_type (arg2
)))
1800 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1803 struct value
*tmp
= arg1
;
1805 /* For shift and integer exponentiation operations,
1806 only promote the first argument. */
1807 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1808 && is_integral_type (value_type (arg2
)))
1809 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1811 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1813 arg2
= value_binop (tmp
, arg2
, op
);
1815 return value_assign (arg1
, arg2
);
1818 /* Note that ARGS needs 2 empty slots up front and must end with a
1820 static struct value
*
1821 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1822 enum noside noside
, CORE_ADDR selector
,
1823 value
*target
, gdb::array_view
<value
*> args
)
1825 CORE_ADDR responds_selector
= 0;
1826 CORE_ADDR method_selector
= 0;
1828 int struct_return
= 0;
1830 struct value
*msg_send
= NULL
;
1831 struct value
*msg_send_stret
= NULL
;
1832 int gnu_runtime
= 0;
1834 struct value
*method
= NULL
;
1835 struct value
*called_method
= NULL
;
1837 struct type
*selector_type
= NULL
;
1838 struct type
*long_type
;
1841 struct value
*ret
= NULL
;
1846 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1847 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1849 if (value_as_long (target
) == 0)
1850 return value_from_longest (long_type
, 0);
1852 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1855 /* Find the method dispatch (Apple runtime) or method lookup
1856 (GNU runtime) function for Objective-C. These will be used
1857 to lookup the symbol information for the method. If we
1858 can't find any symbol information, then we'll use these to
1859 call the method, otherwise we can call the method
1860 directly. The msg_send_stret function is used in the special
1861 case of a method that returns a structure (Apple runtime
1865 type
= selector_type
;
1867 type
= lookup_function_type (type
);
1868 type
= lookup_pointer_type (type
);
1869 type
= lookup_function_type (type
);
1870 type
= lookup_pointer_type (type
);
1872 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1874 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1876 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1877 msg_send_stret
= value_from_pointer (type
,
1878 value_as_address (msg_send_stret
));
1882 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1883 /* Special dispatcher for methods returning structs. */
1885 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1888 /* Verify the target object responds to this method. The
1889 standard top-level 'Object' class uses a different name for
1890 the verification method than the non-standard, but more
1891 often used, 'NSObject' class. Make sure we check for both. */
1894 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1895 if (responds_selector
== 0)
1897 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1899 if (responds_selector
== 0)
1900 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1903 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1904 if (method_selector
== 0)
1906 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1908 if (method_selector
== 0)
1909 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1911 /* Call the verification method, to make sure that the target
1912 class implements the desired method. */
1914 argvec
[0] = msg_send
;
1916 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1917 argvec
[3] = value_from_longest (long_type
, selector
);
1920 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1923 /* Function objc_msg_lookup returns a pointer. */
1925 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1927 if (value_as_long (ret
) == 0)
1928 error (_("Target does not respond to this message selector."));
1930 /* Call "methodForSelector:" method, to get the address of a
1931 function method that implements this selector for this
1932 class. If we can find a symbol at that address, then we
1933 know the return type, parameter types etc. (that's a good
1936 argvec
[0] = msg_send
;
1938 argvec
[2] = value_from_longest (long_type
, method_selector
);
1939 argvec
[3] = value_from_longest (long_type
, selector
);
1942 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1946 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1949 /* ret should now be the selector. */
1951 addr
= value_as_long (ret
);
1954 struct symbol
*sym
= NULL
;
1956 /* The address might point to a function descriptor;
1957 resolve it to the actual code address instead. */
1958 addr
= gdbarch_convert_from_func_ptr_addr (exp
->gdbarch
, addr
,
1959 current_top_target ());
1961 /* Is it a high_level symbol? */
1962 sym
= find_pc_function (addr
);
1964 method
= value_of_variable (sym
, 0);
1967 /* If we found a method with symbol information, check to see
1968 if it returns a struct. Otherwise assume it doesn't. */
1973 struct type
*val_type
;
1975 funaddr
= find_function_addr (method
, &val_type
);
1977 block_for_pc (funaddr
);
1979 val_type
= check_typedef (val_type
);
1981 if ((val_type
== NULL
)
1982 || (val_type
->code () == TYPE_CODE_ERROR
))
1984 if (expect_type
!= NULL
)
1985 val_type
= expect_type
;
1988 struct_return
= using_struct_return (exp
->gdbarch
, method
,
1991 else if (expect_type
!= NULL
)
1993 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
1994 check_typedef (expect_type
));
1997 /* Found a function symbol. Now we will substitute its
1998 value in place of the message dispatcher (obj_msgSend),
1999 so that we call the method directly instead of thru
2000 the dispatcher. The main reason for doing this is that
2001 we can now evaluate the return value and parameter values
2002 according to their known data types, in case we need to
2003 do things like promotion, dereferencing, special handling
2004 of structs and doubles, etc.
2006 We want to use the type signature of 'method', but still
2007 jump to objc_msgSend() or objc_msgSend_stret() to better
2008 mimic the behavior of the runtime. */
2012 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
2013 error (_("method address has symbol information "
2014 "with non-function type; skipping"));
2016 /* Create a function pointer of the appropriate type, and
2017 replace its value with the value of msg_send or
2018 msg_send_stret. We must use a pointer here, as
2019 msg_send and msg_send_stret are of pointer type, and
2020 the representation may be different on systems that use
2021 function descriptors. */
2024 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2025 value_as_address (msg_send_stret
));
2028 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2029 value_as_address (msg_send
));
2034 called_method
= msg_send_stret
;
2036 called_method
= msg_send
;
2040 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2042 /* If the return type doesn't look like a function type,
2043 call an error. This can happen if somebody tries to
2044 turn a variable into a function call. This is here
2045 because people often want to call, eg, strcmp, which
2046 gdb doesn't know is a function. If gdb isn't asked for
2047 it's opinion (ie. through "whatis"), it won't offer
2050 struct type
*callee_type
= value_type (called_method
);
2052 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2053 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2054 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2058 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2059 return allocate_value (expect_type
);
2061 return allocate_value (callee_type
);
2064 error (_("Expression of type other than "
2065 "\"method returning ...\" used as a method"));
2068 /* Now depending on whether we found a symbol for the method,
2069 we will either call the runtime dispatcher or the method
2073 args
[1] = value_from_longest (long_type
, selector
);
2075 if (gnu_runtime
&& (method
!= NULL
))
2077 /* Function objc_msg_lookup returns a pointer. */
2078 struct type
*tem_type
= value_type (called_method
);
2079 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2080 deprecated_set_value_type (called_method
, tem_type
);
2081 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2084 return call_function_by_hand (called_method
, NULL
, args
);
2087 /* Helper function for MULTI_SUBSCRIPT. */
2089 static struct value
*
2090 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2091 enum noside noside
, value
*arg1
,
2092 gdb::array_view
<value
*> args
)
2094 for (value
*arg2
: args
)
2096 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2098 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2102 arg1
= coerce_ref (arg1
);
2103 struct type
*type
= check_typedef (value_type (arg1
));
2105 switch (type
->code ())
2108 case TYPE_CODE_ARRAY
:
2109 case TYPE_CODE_STRING
:
2110 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2115 error (_("cannot subscript something of type `%s'"),
2118 error (_("cannot subscript requested type"));
2129 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2130 struct expression
*exp
,
2133 enum noside sub_no_side
= EVAL_NORMAL
;
2134 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2136 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2137 sub_no_side
= EVAL_NORMAL
;
2139 sub_no_side
= noside
;
2141 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2143 if (value_as_long (target
) == 0)
2144 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2146 sub_no_side
= noside
;
2147 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2148 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2149 argvec
[0] = nullptr;
2150 argvec
[1] = nullptr;
2151 for (int i
= 0; i
< args
.size (); ++i
)
2152 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2153 argvec
[args
.size () + 2] = nullptr;
2155 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2156 get
<0> (m_storage
), target
,
2157 gdb::make_array_view (argvec
,
2162 multi_subscript_operation::evaluate (struct type
*expect_type
,
2163 struct expression
*exp
,
2166 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2167 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2168 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2169 for (int ix
= 0; ix
< values
.size (); ++ix
)
2170 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2171 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2172 gdb::make_array_view (argvec
, values
.size ()));
2176 logical_and_operation::evaluate (struct type
*expect_type
,
2177 struct expression
*exp
,
2180 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2182 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2183 EVAL_AVOID_SIDE_EFFECTS
);
2185 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2187 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2188 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2192 int tem
= value_logical_not (arg1
);
2195 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2196 tem
= value_logical_not (arg2
);
2198 struct type
*type
= language_bool_type (exp
->language_defn
,
2200 return value_from_longest (type
, !tem
);
2205 logical_or_operation::evaluate (struct type
*expect_type
,
2206 struct expression
*exp
,
2209 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2211 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2212 EVAL_AVOID_SIDE_EFFECTS
);
2214 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2216 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2217 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2221 int tem
= value_logical_not (arg1
);
2224 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2225 tem
= value_logical_not (arg2
);
2228 struct type
*type
= language_bool_type (exp
->language_defn
,
2230 return value_from_longest (type
, !tem
);
2235 adl_func_operation::evaluate (struct type
*expect_type
,
2236 struct expression
*exp
,
2239 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2240 std::vector
<value
*> args (arg_ops
.size ());
2241 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2242 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2244 struct symbol
*symp
;
2245 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2248 nullptr, &symp
, nullptr, 0, noside
);
2249 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
2250 error_unknown_type (symp
->print_name ());
2251 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2252 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2253 nullptr, expect_type
);
2257 /* This function evaluates brace-initializers (in C/C++) for
2261 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2262 struct expression
*exp
,
2263 enum noside noside
, int nargs
)
2265 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2266 struct type
*struct_type
= check_typedef (value_type (struct_val
));
2267 struct type
*field_type
;
2271 while (--nargs
>= 0)
2273 struct value
*val
= NULL
;
2274 int bitpos
, bitsize
;
2278 /* Skip static fields. */
2279 while (fieldno
< struct_type
->num_fields ()
2280 && field_is_static (&struct_type
->field (fieldno
)))
2282 if (fieldno
>= struct_type
->num_fields ())
2283 error (_("too many initializers"));
2284 field_type
= struct_type
->field (fieldno
).type ();
2285 if (field_type
->code () == TYPE_CODE_UNION
2286 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
2287 error (_("don't know which variant you want to set"));
2289 /* Here, struct_type is the type of the inner struct,
2290 while substruct_type is the type of the inner struct.
2291 These are the same for normal structures, but a variant struct
2292 contains anonymous union fields that contain substruct fields.
2293 The value fieldno is the index of the top-level (normal or
2294 anonymous union) field in struct_field, while the value
2295 subfieldno is the index of the actual real (named inner) field
2296 in substruct_type. */
2298 field_type
= struct_type
->field (fieldno
).type ();
2300 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2302 /* Now actually set the field in struct_val. */
2304 /* Assign val to field fieldno. */
2305 if (value_type (val
) != field_type
)
2306 val
= value_cast (field_type
, val
);
2308 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2309 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
2310 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
2312 modify_field (struct_type
, addr
,
2313 value_as_long (val
), bitpos
% 8, bitsize
);
2315 memcpy (addr
, value_contents (val
),
2316 TYPE_LENGTH (value_type (val
)));
2323 array_operation::evaluate (struct type
*expect_type
,
2324 struct expression
*exp
,
2328 int tem2
= std::get
<0> (m_storage
);
2329 int tem3
= std::get
<1> (m_storage
);
2330 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2331 int nargs
= tem3
- tem2
+ 1;
2332 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2334 if (expect_type
!= nullptr
2335 && type
->code () == TYPE_CODE_STRUCT
)
2337 struct value
*rec
= allocate_value (expect_type
);
2339 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
2340 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2343 if (expect_type
!= nullptr
2344 && type
->code () == TYPE_CODE_ARRAY
)
2346 struct type
*range_type
= type
->index_type ();
2347 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2348 struct value
*array
= allocate_value (expect_type
);
2349 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
2350 LONGEST low_bound
, high_bound
, index
;
2352 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2355 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
2358 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
2359 for (tem
= nargs
; --nargs
>= 0;)
2361 struct value
*element
;
2363 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2365 if (value_type (element
) != element_type
)
2366 element
= value_cast (element_type
, element
);
2367 if (index
> high_bound
)
2368 /* To avoid memory corruption. */
2369 error (_("Too many array elements"));
2370 memcpy (value_contents_raw (array
)
2371 + (index
- low_bound
) * element_size
,
2372 value_contents (element
),
2379 if (expect_type
!= nullptr
2380 && type
->code () == TYPE_CODE_SET
)
2382 struct value
*set
= allocate_value (expect_type
);
2383 gdb_byte
*valaddr
= value_contents_raw (set
);
2384 struct type
*element_type
= type
->index_type ();
2385 struct type
*check_type
= element_type
;
2386 LONGEST low_bound
, high_bound
;
2388 /* Get targettype of elementtype. */
2389 while (check_type
->code () == TYPE_CODE_RANGE
2390 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2391 check_type
= TYPE_TARGET_TYPE (check_type
);
2393 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2394 error (_("(power)set type with unknown size"));
2395 memset (valaddr
, '\0', TYPE_LENGTH (type
));
2397 for (tem
= 0; tem
< nargs
; tem
++)
2399 LONGEST range_low
, range_high
;
2400 struct type
*range_low_type
, *range_high_type
;
2401 struct value
*elem_val
;
2403 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2404 range_low_type
= range_high_type
= value_type (elem_val
);
2405 range_low
= range_high
= value_as_long (elem_val
);
2407 /* Check types of elements to avoid mixture of elements from
2408 different types. Also check if type of element is "compatible"
2409 with element type of powerset. */
2410 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2411 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
2412 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2413 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
2414 if ((range_low_type
->code () != range_high_type
->code ())
2415 || (range_low_type
->code () == TYPE_CODE_ENUM
2416 && (range_low_type
!= range_high_type
)))
2417 /* different element modes. */
2418 error (_("POWERSET tuple elements of different mode"));
2419 if ((check_type
->code () != range_low_type
->code ())
2420 || (check_type
->code () == TYPE_CODE_ENUM
2421 && range_low_type
!= check_type
))
2422 error (_("incompatible POWERSET tuple elements"));
2423 if (range_low
> range_high
)
2425 warning (_("empty POWERSET tuple range"));
2428 if (range_low
< low_bound
|| range_high
> high_bound
)
2429 error (_("POWERSET tuple element out of range"));
2430 range_low
-= low_bound
;
2431 range_high
-= low_bound
;
2432 for (; range_low
<= range_high
; range_low
++)
2434 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2436 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2437 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2438 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2445 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2446 for (tem
= 0; tem
< nargs
; tem
++)
2448 /* Ensure that array expressions are coerced into pointer
2450 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2452 return value_array (tem2
, tem3
, argvec
);
2458 /* Helper for evaluate_subexp_for_address. */
2461 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2464 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2466 struct type
*type
= check_typedef (value_type (x
));
2468 if (TYPE_IS_REFERENCE (type
))
2469 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2471 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2472 return value_zero (lookup_pointer_type (value_type (x
)),
2475 error (_("Attempt to take address of "
2476 "value not located in memory."));
2478 return value_addr (x
);
2485 operation::evaluate_for_cast (struct type
*expect_type
,
2486 struct expression
*exp
,
2489 value
*val
= evaluate (expect_type
, exp
, noside
);
2490 return value_cast (expect_type
, val
);
2494 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2496 value
*val
= evaluate (nullptr, exp
, noside
);
2497 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2501 scope_operation::evaluate_for_address (struct expression
*exp
,
2504 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2505 std::get
<1> (m_storage
).c_str (),
2508 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2513 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2516 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2518 /* We can't optimize out "&*" if there's a user-defined operator*. */
2519 if (unop_user_defined_p (UNOP_IND
, x
))
2521 x
= value_x_unop (x
, UNOP_IND
, noside
);
2522 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2525 return coerce_array (x
);
2529 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2532 value
*val
= evaluate_var_msym_value (noside
,
2533 std::get
<1> (m_storage
),
2534 std::get
<0> (m_storage
));
2535 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2537 struct type
*type
= lookup_pointer_type (value_type (val
));
2538 return value_zero (type
, not_lval
);
2541 return value_addr (val
);
2545 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2548 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2549 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2553 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2556 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2557 EVAL_AVOID_SIDE_EFFECTS
);
2558 struct type
*type
= value_type (typeval
);
2559 return value_cast (lookup_pointer_type (type
),
2560 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2564 var_value_operation::evaluate_for_address (struct expression
*exp
,
2567 symbol
*var
= std::get
<0> (m_storage
);
2569 /* C++: The "address" of a reference should yield the address
2570 * of the object pointed to. Let value_addr() deal with it. */
2571 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2572 return operation::evaluate_for_address (exp
, noside
);
2574 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2576 struct type
*type
= lookup_pointer_type (SYMBOL_TYPE (var
));
2577 enum address_class sym_class
= SYMBOL_CLASS (var
);
2579 if (sym_class
== LOC_CONST
2580 || sym_class
== LOC_CONST_BYTES
2581 || sym_class
== LOC_REGISTER
)
2582 error (_("Attempt to take address of register or constant."));
2584 return value_zero (type
, not_lval
);
2587 return address_of_variable (var
, std::get
<1> (m_storage
));
2591 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2594 struct symbol
*var
= std::get
<0> (m_storage
);
2595 struct type
*type
= check_typedef (SYMBOL_TYPE (var
));
2596 if (type
->code () == TYPE_CODE_ARRAY
2597 && !type
->is_vector ()
2598 && CAST_IS_CONVERSION (exp
->language_defn
))
2600 struct value
*val
= address_of_variable (var
, std::get
<1> (m_storage
));
2601 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)), val
);
2603 return evaluate (nullptr, exp
, noside
);
2608 /* Helper function for evaluating the size of a type. */
2611 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2613 /* FIXME: This should be size_t. */
2614 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2615 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2616 "When applied to a reference or a reference type, the result is
2617 the size of the referenced type." */
2618 type
= check_typedef (type
);
2619 if (exp
->language_defn
->la_language
== language_cplus
2620 && (TYPE_IS_REFERENCE (type
)))
2621 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2622 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2629 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2631 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2632 return evaluate_subexp_for_sizeof_base (exp
, value_type (val
));
2636 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2640 minimal_symbol
*msymbol
= std::get
<0> (m_storage
);
2641 value
*mval
= evaluate_var_msym_value (noside
,
2642 std::get
<1> (m_storage
),
2645 struct type
*type
= value_type (mval
);
2646 if (type
->code () == TYPE_CODE_ERROR
)
2647 error_unknown_type (msymbol
->print_name ());
2649 /* FIXME: This should be size_t. */
2650 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2651 return value_from_longest (size_type
, TYPE_LENGTH (type
));
2655 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2658 if (noside
== EVAL_NORMAL
)
2660 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2661 EVAL_AVOID_SIDE_EFFECTS
);
2662 struct type
*type
= check_typedef (value_type (val
));
2663 if (type
->code () == TYPE_CODE_ARRAY
)
2665 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2666 if (type
->code () == TYPE_CODE_ARRAY
)
2668 type
= type
->index_type ();
2669 /* Only re-evaluate the right hand side if the resulting type
2670 is a variable length type. */
2671 if (type
->bounds ()->flag_bound_evaluated
)
2673 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2674 /* FIXME: This should be size_t. */
2675 struct type
*size_type
2676 = builtin_type (exp
->gdbarch
)->builtin_int
;
2677 return value_from_longest
2678 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
2684 return operation::evaluate_for_sizeof (exp
, noside
);
2688 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2691 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2692 EVAL_AVOID_SIDE_EFFECTS
);
2693 struct type
*type
= check_typedef (value_type (val
));
2694 if (type
->code () != TYPE_CODE_PTR
2695 && !TYPE_IS_REFERENCE (type
)
2696 && type
->code () != TYPE_CODE_ARRAY
)
2697 error (_("Attempt to take contents of a non-pointer value."));
2698 type
= TYPE_TARGET_TYPE (type
);
2699 if (is_dynamic_type (type
))
2700 type
= value_type (value_ind (val
));
2701 /* FIXME: This should be size_t. */
2702 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2703 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2707 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2710 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2714 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2717 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2718 EVAL_AVOID_SIDE_EFFECTS
);
2719 return evaluate_subexp_for_sizeof_base (exp
, value_type (typeval
));
2723 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2726 struct type
*type
= SYMBOL_TYPE (std::get
<0> (m_storage
));
2727 if (is_dynamic_type (type
))
2729 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2730 type
= value_type (val
);
2731 if (type
->code () == TYPE_CODE_ARRAY
)
2733 /* FIXME: This should be size_t. */
2734 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2735 if (type_not_allocated (type
) || type_not_associated (type
))
2736 return value_zero (size_type
, not_lval
);
2737 else if (is_dynamic_type (type
->index_type ())
2738 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2739 return allocate_optimized_out_value (size_type
);
2742 return evaluate_subexp_for_sizeof_base (exp
, type
);
2746 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2747 struct expression
*exp
,
2750 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2751 return value_zero (to_type
, not_lval
);
2753 value
*val
= evaluate_var_msym_value (noside
,
2754 std::get
<1> (m_storage
),
2755 std::get
<0> (m_storage
));
2757 val
= value_cast (to_type
, val
);
2759 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2760 if (VALUE_LVAL (val
) == lval_memory
)
2762 if (value_lazy (val
))
2763 value_fetch_lazy (val
);
2764 VALUE_LVAL (val
) = not_lval
;
2770 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2771 struct expression
*exp
,
2774 value
*val
= evaluate_var_value (noside
,
2775 std::get
<1> (m_storage
),
2776 std::get
<0> (m_storage
));
2778 val
= value_cast (to_type
, val
);
2780 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2781 if (VALUE_LVAL (val
) == lval_memory
)
2783 if (value_lazy (val
))
2784 value_fetch_lazy (val
);
2785 VALUE_LVAL (val
) = not_lval
;
2792 /* Parse a type expression in the string [P..P+LENGTH). */
2795 parse_and_eval_type (const char *p
, int length
)
2797 char *tmp
= (char *) alloca (length
+ 4);
2800 memcpy (tmp
+ 1, p
, length
);
2801 tmp
[length
+ 1] = ')';
2802 tmp
[length
+ 2] = '0';
2803 tmp
[length
+ 3] = '\0';
2804 expression_up expr
= parse_expression (tmp
);
2805 expr::unop_cast_operation
*op
2806 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2808 error (_("Internal error in eval_type."));
2809 return op
->get_type ();