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"
48 /* Parse the string EXP as a C expression, evaluate it,
49 and return the result as a number. */
52 parse_and_eval_address (const char *exp
)
54 expression_up expr
= parse_expression (exp
);
56 return value_as_address (evaluate_expression (expr
.get ()));
59 /* Like parse_and_eval_address, but treats the value of the expression
60 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
62 parse_and_eval_long (const char *exp
)
64 expression_up expr
= parse_expression (exp
);
66 return value_as_long (evaluate_expression (expr
.get ()));
70 parse_and_eval (const char *exp
)
72 expression_up expr
= parse_expression (exp
);
74 return evaluate_expression (expr
.get ());
77 /* Parse up to a comma (or to a closeparen)
78 in the string EXPP as an expression, evaluate it, and return the value.
79 EXPP is advanced to point to the comma. */
82 parse_to_comma_and_eval (const char **expp
)
84 expression_up expr
= parse_exp_1 (expp
, 0, nullptr, 1);
86 return evaluate_expression (expr
.get ());
90 /* See expression.h. */
93 expression::evaluate (struct type
*expect_type
, enum noside noside
)
95 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
96 if (target_has_execution ()
97 && language_defn
->la_language
== language_cplus
98 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
99 stack_temporaries
.emplace (inferior_thread ());
101 struct value
*retval
= op
->evaluate (expect_type
, this, noside
);
103 if (stack_temporaries
.has_value ()
104 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
105 retval
= value_non_lval (retval
);
113 evaluate_expression (struct expression
*exp
, struct type
*expect_type
)
115 return exp
->evaluate (expect_type
, EVAL_NORMAL
);
118 /* Evaluate an expression, avoiding all memory references
119 and getting a value whose type alone is correct. */
122 evaluate_type (struct expression
*exp
)
124 return exp
->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS
);
127 /* Find the current value of a watchpoint on EXP. Return the value in
128 *VALP and *RESULTP and the chain of intermediate and final values
129 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
132 If PRESERVE_ERRORS is true, then exceptions are passed through.
133 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
134 occurs while evaluating the expression, *RESULTP will be set to
135 NULL. *RESULTP may be a lazy value, if the result could not be
136 read from memory. It is used to determine whether a value is
137 user-specified (we should watch the whole value) or intermediate
138 (we should watch only the bit used to locate the final value).
140 If the final value, or any intermediate value, could not be read
141 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
142 set to any referenced values. *VALP will never be a lazy value.
143 This is the value which we store in struct breakpoint.
145 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
146 released from the value chain. If VAL_CHAIN is NULL, all generated
147 values will be left on the value chain. */
150 fetch_subexp_value (struct expression
*exp
,
152 struct value
**valp
, struct value
**resultp
,
153 std::vector
<value_ref_ptr
> *val_chain
,
154 bool preserve_errors
)
156 struct value
*mark
, *new_mark
, *result
;
164 /* Evaluate the expression. */
165 mark
= value_mark ();
170 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
172 catch (const gdb_exception
&ex
)
174 /* Ignore memory errors if we want watchpoints pointing at
175 inaccessible memory to still be created; otherwise, throw the
176 error to some higher catcher. */
180 if (!preserve_errors
)
189 new_mark
= value_mark ();
190 if (mark
== new_mark
)
195 /* Make sure it's not lazy, so that after the target stops again we
196 have a non-lazy previous value to compare with. */
199 if (!value_lazy (result
))
206 value_fetch_lazy (result
);
209 catch (const gdb_exception_error
&except
)
217 /* Return the chain of intermediate values. We use this to
218 decide which addresses to watch. */
219 *val_chain
= value_release_to_mark (mark
);
223 /* Promote value ARG1 as appropriate before performing a unary operation
225 If the result is not appropriate for any particular language then it
226 needs to patch this function. */
229 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
234 *arg1
= coerce_ref (*arg1
);
235 type1
= check_typedef (value_type (*arg1
));
237 if (is_integral_type (type1
))
239 switch (language
->la_language
)
242 /* Perform integral promotion for ANSI C/C++.
243 If not appropriate for any particular language
244 it needs to modify this function. */
246 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
248 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_int
))
249 *arg1
= value_cast (builtin_int
, *arg1
);
256 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
257 operation on those two operands.
258 If the result is not appropriate for any particular language then it
259 needs to patch this function. */
262 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
263 struct value
**arg1
, struct value
**arg2
)
265 struct type
*promoted_type
= NULL
;
269 *arg1
= coerce_ref (*arg1
);
270 *arg2
= coerce_ref (*arg2
);
272 type1
= check_typedef (value_type (*arg1
));
273 type2
= check_typedef (value_type (*arg2
));
275 if ((type1
->code () != TYPE_CODE_FLT
276 && type1
->code () != TYPE_CODE_DECFLOAT
277 && !is_integral_type (type1
))
278 || (type2
->code () != TYPE_CODE_FLT
279 && type2
->code () != TYPE_CODE_DECFLOAT
280 && !is_integral_type (type2
)))
283 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
286 if (type1
->code () == TYPE_CODE_DECFLOAT
287 || type2
->code () == TYPE_CODE_DECFLOAT
)
289 /* No promotion required. */
291 else if (type1
->code () == TYPE_CODE_FLT
292 || type2
->code () == TYPE_CODE_FLT
)
294 switch (language
->la_language
)
300 case language_opencl
:
301 /* No promotion required. */
305 /* For other languages the result type is unchanged from gdb
306 version 6.7 for backward compatibility.
307 If either arg was long double, make sure that value is also long
308 double. Otherwise use double. */
309 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (gdbarch
)
310 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (gdbarch
))
311 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
313 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
317 else if (type1
->code () == TYPE_CODE_BOOL
318 && type2
->code () == TYPE_CODE_BOOL
)
320 /* No promotion required. */
323 /* Integral operations here. */
324 /* FIXME: Also mixed integral/booleans, with result an integer. */
326 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
327 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
328 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
329 int is_unsigned1
= type1
->is_unsigned ();
330 int is_unsigned2
= type2
->is_unsigned ();
331 unsigned int result_len
;
332 int unsigned_operation
;
334 /* Determine type length and signedness after promotion for
336 if (promoted_len1
< TYPE_LENGTH (builtin
->builtin_int
))
339 promoted_len1
= TYPE_LENGTH (builtin
->builtin_int
);
341 if (promoted_len2
< TYPE_LENGTH (builtin
->builtin_int
))
344 promoted_len2
= TYPE_LENGTH (builtin
->builtin_int
);
347 if (promoted_len1
> promoted_len2
)
349 unsigned_operation
= is_unsigned1
;
350 result_len
= promoted_len1
;
352 else if (promoted_len2
> promoted_len1
)
354 unsigned_operation
= is_unsigned2
;
355 result_len
= promoted_len2
;
359 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
360 result_len
= promoted_len1
;
363 switch (language
->la_language
)
369 if (result_len
<= TYPE_LENGTH (builtin
->builtin_int
))
371 promoted_type
= (unsigned_operation
372 ? builtin
->builtin_unsigned_int
373 : builtin
->builtin_int
);
375 else if (result_len
<= TYPE_LENGTH (builtin
->builtin_long
))
377 promoted_type
= (unsigned_operation
378 ? builtin
->builtin_unsigned_long
379 : builtin
->builtin_long
);
383 promoted_type
= (unsigned_operation
384 ? builtin
->builtin_unsigned_long_long
385 : builtin
->builtin_long_long
);
388 case language_opencl
:
389 if (result_len
<= TYPE_LENGTH (lookup_signed_typename
394 ? lookup_unsigned_typename (language
, "int")
395 : lookup_signed_typename (language
, "int"));
397 else if (result_len
<= TYPE_LENGTH (lookup_signed_typename
402 ? lookup_unsigned_typename (language
, "long")
403 : lookup_signed_typename (language
,"long"));
407 /* For other languages the result type is unchanged from gdb
408 version 6.7 for backward compatibility.
409 If either arg was long long, make sure that value is also long
410 long. Otherwise use long. */
411 if (unsigned_operation
)
413 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
414 promoted_type
= builtin
->builtin_unsigned_long_long
;
416 promoted_type
= builtin
->builtin_unsigned_long
;
420 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
421 promoted_type
= builtin
->builtin_long_long
;
423 promoted_type
= builtin
->builtin_long
;
431 /* Promote both operands to common type. */
432 *arg1
= value_cast (promoted_type
, *arg1
);
433 *arg2
= value_cast (promoted_type
, *arg2
);
438 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
440 type
= check_typedef (type
);
441 if (TYPE_IS_REFERENCE (type
))
442 type
= TYPE_TARGET_TYPE (type
);
444 switch (type
->code ())
450 case TYPE_CODE_ARRAY
:
451 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
458 /* Represents a fake method with the given parameter types. This is
459 used by the parser to construct a temporary "expected" type for
460 method overload resolution. FLAGS is used as instance flags of the
461 new type, in order to be able to make the new type represent a
462 const/volatile overload. */
467 fake_method (type_instance_flags flags
,
468 int num_types
, struct type
**param_types
);
471 /* The constructed type. */
472 struct type
*type () { return &m_type
; }
475 struct type m_type
{};
476 main_type m_main_type
{};
479 fake_method::fake_method (type_instance_flags flags
,
480 int num_types
, struct type
**param_types
)
482 struct type
*type
= &m_type
;
484 TYPE_MAIN_TYPE (type
) = &m_main_type
;
485 TYPE_LENGTH (type
) = 1;
486 type
->set_code (TYPE_CODE_METHOD
);
487 TYPE_CHAIN (type
) = type
;
488 type
->set_instance_flags (flags
);
491 if (param_types
[num_types
- 1] == NULL
)
494 type
->set_has_varargs (true);
496 else if (check_typedef (param_types
[num_types
- 1])->code ()
500 /* Caller should have ensured this. */
501 gdb_assert (num_types
== 0);
502 type
->set_is_prototyped (true);
506 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
507 neither an objfile nor a gdbarch. As a result we must manually
508 allocate memory for auxiliary fields, and free the memory ourselves
509 when we are done with it. */
510 type
->set_num_fields (num_types
);
512 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
514 while (num_types
-- > 0)
515 type
->field (num_types
).set_type (param_types
[num_types
]);
518 fake_method::~fake_method ()
520 xfree (m_type
.fields ());
527 type_instance_operation::evaluate (struct type
*expect_type
,
528 struct expression
*exp
,
531 type_instance_flags flags
= std::get
<0> (m_storage
);
532 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
534 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
535 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
541 /* Helper for evaluating an OP_VAR_VALUE. */
544 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
546 /* JYG: We used to just return value_zero of the symbol type if
547 we're asked to avoid side effects. Otherwise we return
548 value_of_variable (...). However I'm not sure if
549 value_of_variable () has any side effect. We need a full value
550 object returned here for whatis_exp () to call evaluate_type ()
551 and then pass the full value to value_rtti_target_type () if we
552 are dealing with a pointer or reference to a base class and print
555 struct value
*ret
= NULL
;
559 ret
= value_of_variable (var
, blk
);
562 catch (const gdb_exception_error
&except
)
564 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
567 ret
= value_zero (SYMBOL_TYPE (var
), not_lval
);
578 var_value_operation::evaluate (struct type
*expect_type
,
579 struct expression
*exp
,
582 symbol
*var
= std::get
<0> (m_storage
);
583 if (SYMBOL_TYPE (var
)->code () == TYPE_CODE_ERROR
)
584 error_unknown_type (var
->print_name ());
585 return evaluate_var_value (noside
, std::get
<1> (m_storage
), var
);
588 } /* namespace expr */
590 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
593 evaluate_var_msym_value (enum noside noside
,
594 struct objfile
*objfile
, minimal_symbol
*msymbol
)
597 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
599 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
600 return value_zero (the_type
, not_lval
);
602 return value_at_lazy (the_type
, address
);
605 /* See expression.h. */
608 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
610 gdb::array_view
<value
*> argvec
,
611 const char *function_name
,
612 type
*default_return_type
)
615 error (_("Cannot evaluate function -- may be inlined"));
616 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
618 /* If the return type doesn't look like a function type,
619 call an error. This can happen if somebody tries to turn
620 a variable into a function call. */
622 type
*ftype
= value_type (callee
);
624 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
626 /* We don't know anything about what the internal
627 function might return, but we have to return
629 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
632 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
634 type
*return_type
= result_type_of_xmethod (callee
, argvec
);
636 if (return_type
== NULL
)
637 error (_("Xmethod is missing return type."));
638 return value_zero (return_type
, not_lval
);
640 else if (ftype
->code () == TYPE_CODE_FUNC
641 || ftype
->code () == TYPE_CODE_METHOD
)
643 if (ftype
->is_gnu_ifunc ())
645 CORE_ADDR address
= value_address (callee
);
646 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
648 if (resolved_type
!= NULL
)
649 ftype
= resolved_type
;
652 type
*return_type
= TYPE_TARGET_TYPE (ftype
);
654 if (return_type
== NULL
)
655 return_type
= default_return_type
;
657 if (return_type
== NULL
)
658 error_call_unknown_return_type (function_name
);
660 return allocate_value (return_type
);
663 error (_("Expression of type other than "
664 "\"Function returning ...\" used as function"));
666 switch (value_type (callee
)->code ())
668 case TYPE_CODE_INTERNAL_FUNCTION
:
669 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
670 callee
, argvec
.size (), argvec
.data ());
671 case TYPE_CODE_XMETHOD
:
672 return call_xmethod (callee
, argvec
);
674 return call_function_by_hand (callee
, default_return_type
, argvec
);
682 operation::evaluate_funcall (struct type
*expect_type
,
683 struct expression
*exp
,
685 const char *function_name
,
686 const std::vector
<operation_up
> &args
)
688 std::vector
<value
*> vals (args
.size ());
690 value
*callee
= evaluate_with_coercion (exp
, noside
);
691 struct type
*type
= value_type (callee
);
692 if (type
->code () == TYPE_CODE_PTR
)
693 type
= TYPE_TARGET_TYPE (type
);
694 for (int i
= 0; i
< args
.size (); ++i
)
696 if (i
< type
->num_fields ())
697 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
699 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
702 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
703 function_name
, expect_type
);
707 var_value_operation::evaluate_funcall (struct type
*expect_type
,
708 struct expression
*exp
,
710 const std::vector
<operation_up
> &args
)
712 if (!overload_resolution
713 || exp
->language_defn
->la_language
!= language_cplus
)
714 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
716 std::vector
<value
*> argvec (args
.size ());
717 for (int i
= 0; i
< args
.size (); ++i
)
718 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
721 find_overload_match (argvec
, NULL
, NON_METHOD
,
722 NULL
, std::get
<0> (m_storage
),
723 NULL
, &symp
, NULL
, 0, noside
);
725 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
726 error_unknown_type (symp
->print_name ());
727 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
729 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
730 nullptr, expect_type
);
734 scope_operation::evaluate_funcall (struct type
*expect_type
,
735 struct expression
*exp
,
737 const std::vector
<operation_up
> &args
)
739 if (!overload_resolution
740 || exp
->language_defn
->la_language
!= language_cplus
)
741 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
743 /* Unpack it locally so we can properly handle overload
745 const std::string
&name
= std::get
<1> (m_storage
);
746 struct type
*type
= std::get
<0> (m_storage
);
748 symbol
*function
= NULL
;
749 const char *function_name
= NULL
;
750 std::vector
<value
*> argvec (1 + args
.size ());
751 if (type
->code () == TYPE_CODE_NAMESPACE
)
753 function
= cp_lookup_symbol_namespace (type
->name (),
755 get_selected_block (0),
757 if (function
== NULL
)
758 error (_("No symbol \"%s\" in namespace \"%s\"."),
759 name
.c_str (), type
->name ());
763 gdb_assert (type
->code () == TYPE_CODE_STRUCT
764 || type
->code () == TYPE_CODE_UNION
);
765 function_name
= name
.c_str ();
767 /* We need a properly typed value for method lookup. */
768 argvec
[0] = value_zero (type
, lval_memory
);
771 for (int i
= 0; i
< args
.size (); ++i
)
772 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
773 gdb::array_view
<value
*> arg_view
= argvec
;
775 value
*callee
= nullptr;
776 if (function_name
!= nullptr)
780 find_overload_match (arg_view
, function_name
, METHOD
,
781 &argvec
[0], nullptr, &callee
, nullptr,
782 &static_memfuncp
, 0, noside
);
783 if (!static_memfuncp
)
785 /* For the time being, we don't handle this. */
786 error (_("Call to overloaded function %s requires "
791 arg_view
= arg_view
.slice (1);
796 arg_view
= arg_view
.slice (1);
797 find_overload_match (arg_view
, nullptr,
798 NON_METHOD
, nullptr, function
,
799 nullptr, &symp
, nullptr, 1, noside
);
800 callee
= value_of_variable (symp
, get_selected_block (0));
803 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
804 nullptr, expect_type
);
808 structop_member_base::evaluate_funcall (struct type
*expect_type
,
809 struct expression
*exp
,
811 const std::vector
<operation_up
> &args
)
813 /* First, evaluate the structure into lhs. */
815 if (opcode () == STRUCTOP_MEMBER
)
816 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
818 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
820 std::vector
<value
*> vals (args
.size () + 1);
821 gdb::array_view
<value
*> val_view
= vals
;
822 /* If the function is a virtual function, then the aggregate
823 value (providing the structure) plays its part by providing
824 the vtable. Otherwise, it is just along for the ride: call
825 the function directly. */
826 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
829 type
*a1_type
= check_typedef (value_type (rhs
));
830 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
832 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
833 callee
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
835 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
839 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
841 struct type
*type_ptr
842 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
843 struct type
*target_type_ptr
844 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
846 /* Now, convert this value to an address. */
847 lhs
= value_cast (type_ptr
, lhs
);
849 long mem_offset
= value_as_long (rhs
);
851 callee
= value_from_pointer (target_type_ptr
,
852 value_as_long (lhs
) + mem_offset
);
853 callee
= value_ind (callee
);
855 val_view
= val_view
.slice (1);
858 error (_("Non-pointer-to-member value used in pointer-to-member "
861 for (int i
= 0; i
< args
.size (); ++i
)
862 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
864 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
865 nullptr, expect_type
);
870 structop_base_operation::evaluate_funcall
871 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
872 const std::vector
<operation_up
> &args
)
874 std::vector
<value
*> vals (args
.size () + 1);
875 /* First, evaluate the structure into vals[0]. */
876 enum exp_opcode op
= opcode ();
877 if (op
== STRUCTOP_STRUCT
)
879 /* If v is a variable in a register, and the user types
880 v.method (), this will produce an error, because v has no
883 A possible way around this would be to allocate a copy of
884 the variable on the stack, copy in the contents, call the
885 function, and copy out the contents. I.e. convert this
886 from call by reference to call by copy-return (or
887 whatever it's called). However, this does not work
888 because it is not the same: the method being called could
889 stash a copy of the address, and then future uses through
890 that address (after the method returns) would be expected
891 to use the variable itself, not some copy of it. */
892 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
896 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
897 /* Check to see if the operator '->' has been overloaded.
898 If the operator has been overloaded replace vals[0] with the
899 value returned by the custom operator and continue
901 while (unop_user_defined_p (op
, vals
[0]))
903 struct value
*value
= nullptr;
906 value
= value_x_unop (vals
[0], op
, noside
);
908 catch (const gdb_exception_error
&except
)
910 if (except
.error
== NOT_FOUND_ERROR
)
920 for (int i
= 0; i
< args
.size (); ++i
)
921 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
922 gdb::array_view
<value
*> arg_view
= vals
;
926 const char *tstr
= std::get
<1> (m_storage
).c_str ();
927 if (overload_resolution
928 && exp
->language_defn
->la_language
== language_cplus
)
930 /* Language is C++, do some overload resolution before
932 value
*val0
= vals
[0];
933 find_overload_match (arg_view
, tstr
, METHOD
,
934 &val0
, nullptr, &callee
, nullptr,
935 &static_memfuncp
, 0, noside
);
939 /* Non-C++ case -- or no overload resolution. */
941 struct value
*temp
= vals
[0];
943 callee
= value_struct_elt (&temp
, &vals
[1], tstr
,
945 op
== STRUCTOP_STRUCT
946 ? "structure" : "structure pointer");
947 /* value_struct_elt updates temp with the correct value of the
948 ``this'' pointer if necessary, so modify it to reflect any
950 vals
[0] = value_from_longest (lookup_pointer_type (value_type (temp
)),
952 + value_embedded_offset (temp
));
955 /* Take out `this' if needed. */
957 arg_view
= arg_view
.slice (1);
959 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
960 nullptr, expect_type
);
964 } /* namespace expr */
966 /* Return true if type is integral or reference to integral */
969 is_integral_or_integral_reference (struct type
*type
)
971 if (is_integral_type (type
))
974 type
= check_typedef (type
);
975 return (type
!= nullptr
976 && TYPE_IS_REFERENCE (type
)
977 && is_integral_type (TYPE_TARGET_TYPE (type
)));
980 /* Helper function that implements the body of OP_SCOPE. */
983 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
985 struct type
*type
, const char *string
)
987 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
990 error (_("There is no field named %s"), string
);
994 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
997 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
998 enum noside noside
, symbol
*sym
)
1000 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1001 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
1003 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1004 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1005 error (_("Symbol \"%s\" does not have any specific entry value"),
1006 sym
->print_name ());
1008 struct frame_info
*frame
= get_selected_frame (NULL
);
1009 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1012 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1015 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1016 enum noside noside
, bool outermost_p
,
1017 bound_minimal_symbol msymbol
)
1019 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1022 struct type
*type
= value_type (val
);
1023 if (type
->code () == TYPE_CODE_ERROR
1024 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1025 error_unknown_type (msymbol
.minsym
->print_name ());
1029 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1032 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1034 value
*func
, const char *var
)
1036 CORE_ADDR addr
= value_address (func
);
1037 const block
*blk
= block_for_pc (addr
);
1038 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1039 if (sym
.symbol
== NULL
)
1040 error (_("No symbol \"%s\" in specified context."), var
);
1041 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1044 /* Helper function that implements the body of OP_REGISTER. */
1047 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1048 enum noside noside
, const char *name
)
1053 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1054 name
, strlen (name
));
1056 error (_("Register $%s not available."), name
);
1058 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1059 a value with the appropriate register type. Unfortunately,
1060 we don't have easy access to the type of user registers.
1061 So for these registers, we fetch the register value regardless
1062 of the evaluation mode. */
1063 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1064 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1065 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1067 val
= value_of_register (regno
, get_selected_frame (NULL
));
1069 error (_("Value of register %s not available."), name
);
1074 /* Helper function that implements the body of OP_STRING. */
1077 eval_op_string (struct type
*expect_type
, struct expression
*exp
,
1078 enum noside noside
, int len
, const char *string
)
1080 struct type
*type
= language_string_char_type (exp
->language_defn
,
1082 return value_string (string
, len
, type
);
1085 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1088 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1092 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1093 return value_from_longest (selector_type
,
1094 lookup_child_selector (exp
->gdbarch
, sel
));
1097 /* Helper function that implements the body of BINOP_CONCAT. */
1100 eval_op_concat (struct type
*expect_type
, struct expression
*exp
,
1101 enum noside noside
, struct value
*arg1
, struct value
*arg2
)
1103 if (binop_user_defined_p (BINOP_CONCAT
, arg1
, arg2
))
1104 return value_x_binop (arg1
, arg2
, BINOP_CONCAT
, OP_NULL
, noside
);
1106 return value_concat (arg1
, arg2
);
1109 /* A helper function for TERNOP_SLICE. */
1112 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1114 struct value
*array
, struct value
*low
, struct value
*upper
)
1116 int lowbound
= value_as_long (low
);
1117 int upperbound
= value_as_long (upper
);
1118 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1121 /* A helper function for STRUCTOP_STRUCT. */
1124 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1126 struct value
*arg1
, const char *string
)
1128 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1130 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1131 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1135 /* A helper function for STRUCTOP_PTR. */
1138 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1140 struct value
*arg1
, const char *string
)
1142 /* Check to see if operator '->' has been overloaded. If so replace
1143 arg1 with the value returned by evaluating operator->(). */
1144 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1146 struct value
*value
= NULL
;
1149 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1152 catch (const gdb_exception_error
&except
)
1154 if (except
.error
== NOT_FOUND_ERROR
)
1163 /* JYG: if print object is on we need to replace the base type
1164 with rtti type in order to continue on with successful
1165 lookup of member / method only available in the rtti type. */
1167 struct type
*arg_type
= value_type (arg1
);
1168 struct type
*real_type
;
1169 int full
, using_enc
;
1171 struct value_print_options opts
;
1173 get_user_print_options (&opts
);
1174 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1175 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1177 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1180 arg1
= value_cast (real_type
, arg1
);
1184 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1185 NULL
, "structure pointer");
1186 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1187 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1191 /* A helper function for STRUCTOP_MEMBER. */
1194 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1196 struct value
*arg1
, struct value
*arg2
)
1201 struct type
*type
= check_typedef (value_type (arg2
));
1202 switch (type
->code ())
1204 case TYPE_CODE_METHODPTR
:
1205 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1206 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1209 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1210 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1211 return value_ind (arg2
);
1214 case TYPE_CODE_MEMBERPTR
:
1215 /* Now, convert these values to an address. */
1216 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1219 mem_offset
= value_as_long (arg2
);
1221 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1222 value_as_long (arg1
) + mem_offset
);
1223 return value_ind (arg3
);
1226 error (_("non-pointer-to-member value used "
1227 "in pointer-to-member construct"));
1231 /* A helper function for BINOP_ADD. */
1234 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1236 struct value
*arg1
, struct value
*arg2
)
1238 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1239 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1240 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1241 && is_integral_or_integral_reference (value_type (arg2
)))
1242 return value_ptradd (arg1
, value_as_long (arg2
));
1243 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
1244 && is_integral_or_integral_reference (value_type (arg1
)))
1245 return value_ptradd (arg2
, value_as_long (arg1
));
1248 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1249 return value_binop (arg1
, arg2
, BINOP_ADD
);
1253 /* A helper function for BINOP_SUB. */
1256 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1258 struct value
*arg1
, struct value
*arg2
)
1260 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1261 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1262 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1263 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
1265 /* FIXME -- should be ptrdiff_t */
1266 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1267 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1269 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1270 && is_integral_or_integral_reference (value_type (arg2
)))
1271 return value_ptradd (arg1
, - value_as_long (arg2
));
1274 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1275 return value_binop (arg1
, arg2
, BINOP_SUB
);
1279 /* Helper function for several different binary operations. */
1282 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1283 enum noside noside
, enum exp_opcode op
,
1284 struct value
*arg1
, struct value
*arg2
)
1286 if (binop_user_defined_p (op
, arg1
, arg2
))
1287 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1290 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1291 fudge arg2 to avoid division-by-zero, the caller is
1292 (theoretically) only looking for the type of the result. */
1293 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1294 /* ??? Do we really want to test for BINOP_MOD here?
1295 The implementation of value_binop gives it a well-defined
1298 || op
== BINOP_INTDIV
1301 && value_logical_not (arg2
))
1303 struct value
*v_one
;
1305 v_one
= value_one (value_type (arg2
));
1306 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1307 return value_binop (arg1
, v_one
, op
);
1311 /* For shift and integer exponentiation operations,
1312 only promote the first argument. */
1313 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1314 && is_integral_type (value_type (arg2
)))
1315 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1317 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1319 return value_binop (arg1
, arg2
, op
);
1324 /* A helper function for BINOP_SUBSCRIPT. */
1327 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1328 enum noside noside
, enum exp_opcode op
,
1329 struct value
*arg1
, struct value
*arg2
)
1331 if (binop_user_defined_p (op
, arg1
, arg2
))
1332 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1335 /* If the user attempts to subscript something that is not an
1336 array or pointer type (like a plain int variable for example),
1337 then report this as an error. */
1339 arg1
= coerce_ref (arg1
);
1340 struct type
*type
= check_typedef (value_type (arg1
));
1341 if (type
->code () != TYPE_CODE_ARRAY
1342 && type
->code () != TYPE_CODE_PTR
)
1345 error (_("cannot subscript something of type `%s'"),
1348 error (_("cannot subscript requested type"));
1351 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1352 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1354 return value_subscript (arg1
, value_as_long (arg2
));
1358 /* A helper function for BINOP_EQUAL. */
1361 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1362 enum noside noside
, enum exp_opcode op
,
1363 struct value
*arg1
, struct value
*arg2
)
1365 if (binop_user_defined_p (op
, arg1
, arg2
))
1367 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1371 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1372 int tem
= value_equal (arg1
, arg2
);
1373 struct type
*type
= language_bool_type (exp
->language_defn
,
1375 return value_from_longest (type
, (LONGEST
) tem
);
1379 /* A helper function for BINOP_NOTEQUAL. */
1382 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1383 enum noside noside
, enum exp_opcode op
,
1384 struct value
*arg1
, struct value
*arg2
)
1386 if (binop_user_defined_p (op
, arg1
, arg2
))
1388 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1392 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1393 int tem
= value_equal (arg1
, arg2
);
1394 struct type
*type
= language_bool_type (exp
->language_defn
,
1396 return value_from_longest (type
, (LONGEST
) ! tem
);
1400 /* A helper function for BINOP_LESS. */
1403 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1404 enum noside noside
, enum exp_opcode op
,
1405 struct value
*arg1
, struct value
*arg2
)
1407 if (binop_user_defined_p (op
, arg1
, arg2
))
1409 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1413 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1414 int tem
= value_less (arg1
, arg2
);
1415 struct type
*type
= language_bool_type (exp
->language_defn
,
1417 return value_from_longest (type
, (LONGEST
) tem
);
1421 /* A helper function for BINOP_GTR. */
1424 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1425 enum noside noside
, enum exp_opcode op
,
1426 struct value
*arg1
, struct value
*arg2
)
1428 if (binop_user_defined_p (op
, arg1
, arg2
))
1430 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1434 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1435 int tem
= value_less (arg2
, arg1
);
1436 struct type
*type
= language_bool_type (exp
->language_defn
,
1438 return value_from_longest (type
, (LONGEST
) tem
);
1442 /* A helper function for BINOP_GEQ. */
1445 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1446 enum noside noside
, enum exp_opcode op
,
1447 struct value
*arg1
, struct value
*arg2
)
1449 if (binop_user_defined_p (op
, arg1
, arg2
))
1451 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1455 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1456 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1457 struct type
*type
= language_bool_type (exp
->language_defn
,
1459 return value_from_longest (type
, (LONGEST
) tem
);
1463 /* A helper function for BINOP_LEQ. */
1466 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1467 enum noside noside
, enum exp_opcode op
,
1468 struct value
*arg1
, struct value
*arg2
)
1470 if (binop_user_defined_p (op
, arg1
, arg2
))
1472 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1476 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1477 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1478 struct type
*type
= language_bool_type (exp
->language_defn
,
1480 return value_from_longest (type
, (LONGEST
) tem
);
1484 /* A helper function for BINOP_REPEAT. */
1487 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1488 enum noside noside
, enum exp_opcode op
,
1489 struct value
*arg1
, struct value
*arg2
)
1491 struct type
*type
= check_typedef (value_type (arg2
));
1492 if (type
->code () != TYPE_CODE_INT
1493 && type
->code () != TYPE_CODE_ENUM
)
1494 error (_("Non-integral right operand for \"@\" operator."));
1495 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1497 return allocate_repeat_value (value_type (arg1
),
1498 longest_to_int (value_as_long (arg2
)));
1501 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1504 /* A helper function for UNOP_PLUS. */
1507 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1508 enum noside noside
, enum exp_opcode op
,
1511 if (unop_user_defined_p (op
, arg1
))
1512 return value_x_unop (arg1
, op
, noside
);
1515 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1516 return value_pos (arg1
);
1520 /* A helper function for UNOP_NEG. */
1523 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1524 enum noside noside
, enum exp_opcode op
,
1527 if (unop_user_defined_p (op
, arg1
))
1528 return value_x_unop (arg1
, op
, noside
);
1531 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1532 return value_neg (arg1
);
1536 /* A helper function for UNOP_COMPLEMENT. */
1539 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1540 enum noside noside
, enum exp_opcode op
,
1543 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1544 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1547 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1548 return value_complement (arg1
);
1552 /* A helper function for UNOP_LOGICAL_NOT. */
1555 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1556 enum noside noside
, enum exp_opcode op
,
1559 if (unop_user_defined_p (op
, arg1
))
1560 return value_x_unop (arg1
, op
, noside
);
1563 struct type
*type
= language_bool_type (exp
->language_defn
,
1565 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1569 /* A helper function for UNOP_IND. */
1572 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1576 struct type
*type
= check_typedef (value_type (arg1
));
1577 if (type
->code () == TYPE_CODE_METHODPTR
1578 || type
->code () == TYPE_CODE_MEMBERPTR
)
1579 error (_("Attempt to dereference pointer "
1580 "to member without an object"));
1581 if (unop_user_defined_p (UNOP_IND
, arg1
))
1582 return value_x_unop (arg1
, UNOP_IND
, noside
);
1583 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1585 type
= check_typedef (value_type (arg1
));
1587 /* If the type pointed to is dynamic then in order to resolve the
1588 dynamic properties we must actually dereference the pointer.
1589 There is a risk that this dereference will have side-effects
1590 in the inferior, but being able to print accurate type
1591 information seems worth the risk. */
1592 if ((type
->code () != TYPE_CODE_PTR
1593 && !TYPE_IS_REFERENCE (type
))
1594 || !is_dynamic_type (TYPE_TARGET_TYPE (type
)))
1596 if (type
->code () == TYPE_CODE_PTR
1597 || TYPE_IS_REFERENCE (type
)
1598 /* In C you can dereference an array to get the 1st elt. */
1599 || type
->code () == TYPE_CODE_ARRAY
)
1600 return value_zero (TYPE_TARGET_TYPE (type
),
1602 else if (type
->code () == TYPE_CODE_INT
)
1603 /* GDB allows dereferencing an int. */
1604 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1607 error (_("Attempt to take contents of a non-pointer value."));
1611 /* Allow * on an integer so we can cast it to whatever we want.
1612 This returns an int, which seems like the most C-like thing to
1613 do. "long long" variables are rare enough that
1614 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1615 if (type
->code () == TYPE_CODE_INT
)
1616 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1617 (CORE_ADDR
) value_as_address (arg1
));
1618 return value_ind (arg1
);
1621 /* A helper function for UNOP_ALIGNOF. */
1624 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1628 struct type
*type
= value_type (arg1
);
1629 /* FIXME: This should be size_t. */
1630 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1631 ULONGEST align
= type_align (type
);
1633 error (_("could not determine alignment of type"));
1634 return value_from_longest (size_type
, align
);
1637 /* A helper function for UNOP_MEMVAL. */
1640 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1642 struct value
*arg1
, struct type
*type
)
1644 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1645 return value_zero (type
, lval_memory
);
1647 return value_at_lazy (type
, value_as_address (arg1
));
1650 /* A helper function for UNOP_PREINCREMENT. */
1653 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1654 enum noside noside
, enum exp_opcode op
,
1657 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1659 else if (unop_user_defined_p (op
, arg1
))
1661 return value_x_unop (arg1
, op
, noside
);
1666 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1667 arg2
= value_ptradd (arg1
, 1);
1670 struct value
*tmp
= arg1
;
1672 arg2
= value_one (value_type (arg1
));
1673 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1674 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1677 return value_assign (arg1
, arg2
);
1681 /* A helper function for UNOP_PREDECREMENT. */
1684 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1685 enum noside noside
, enum exp_opcode op
,
1688 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1690 else if (unop_user_defined_p (op
, arg1
))
1692 return value_x_unop (arg1
, op
, noside
);
1697 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1698 arg2
= value_ptradd (arg1
, -1);
1701 struct value
*tmp
= arg1
;
1703 arg2
= value_one (value_type (arg1
));
1704 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1705 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1708 return value_assign (arg1
, arg2
);
1712 /* A helper function for UNOP_POSTINCREMENT. */
1715 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1716 enum noside noside
, enum exp_opcode op
,
1719 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1721 else if (unop_user_defined_p (op
, arg1
))
1723 return value_x_unop (arg1
, op
, noside
);
1727 struct value
*arg3
= value_non_lval (arg1
);
1730 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1731 arg2
= value_ptradd (arg1
, 1);
1734 struct value
*tmp
= arg1
;
1736 arg2
= value_one (value_type (arg1
));
1737 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1738 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1741 value_assign (arg1
, arg2
);
1746 /* A helper function for UNOP_POSTDECREMENT. */
1749 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1750 enum noside noside
, enum exp_opcode op
,
1753 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1755 else if (unop_user_defined_p (op
, arg1
))
1757 return value_x_unop (arg1
, op
, noside
);
1761 struct value
*arg3
= value_non_lval (arg1
);
1764 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1765 arg2
= value_ptradd (arg1
, -1);
1768 struct value
*tmp
= arg1
;
1770 arg2
= value_one (value_type (arg1
));
1771 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1772 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1775 value_assign (arg1
, arg2
);
1780 /* A helper function for OP_TYPE. */
1783 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1784 enum noside noside
, struct type
*type
)
1786 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1787 return allocate_value (type
);
1789 error (_("Attempt to use a type name as an expression"));
1792 /* A helper function for BINOP_ASSIGN_MODIFY. */
1795 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1796 enum noside noside
, enum exp_opcode op
,
1797 struct value
*arg1
, struct value
*arg2
)
1799 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1801 if (binop_user_defined_p (op
, arg1
, arg2
))
1802 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1803 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1805 && is_integral_type (value_type (arg2
)))
1806 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1807 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1809 && is_integral_type (value_type (arg2
)))
1810 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1813 struct value
*tmp
= arg1
;
1815 /* For shift and integer exponentiation operations,
1816 only promote the first argument. */
1817 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1818 && is_integral_type (value_type (arg2
)))
1819 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1821 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1823 arg2
= value_binop (tmp
, arg2
, op
);
1825 return value_assign (arg1
, arg2
);
1828 /* Note that ARGS needs 2 empty slots up front and must end with a
1830 static struct value
*
1831 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1832 enum noside noside
, CORE_ADDR selector
,
1833 value
*target
, gdb::array_view
<value
*> args
)
1835 CORE_ADDR responds_selector
= 0;
1836 CORE_ADDR method_selector
= 0;
1838 int struct_return
= 0;
1840 struct value
*msg_send
= NULL
;
1841 struct value
*msg_send_stret
= NULL
;
1842 int gnu_runtime
= 0;
1844 struct value
*method
= NULL
;
1845 struct value
*called_method
= NULL
;
1847 struct type
*selector_type
= NULL
;
1848 struct type
*long_type
;
1851 struct value
*ret
= NULL
;
1856 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1857 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1859 if (value_as_long (target
) == 0)
1860 return value_from_longest (long_type
, 0);
1862 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1865 /* Find the method dispatch (Apple runtime) or method lookup
1866 (GNU runtime) function for Objective-C. These will be used
1867 to lookup the symbol information for the method. If we
1868 can't find any symbol information, then we'll use these to
1869 call the method, otherwise we can call the method
1870 directly. The msg_send_stret function is used in the special
1871 case of a method that returns a structure (Apple runtime
1875 type
= selector_type
;
1877 type
= lookup_function_type (type
);
1878 type
= lookup_pointer_type (type
);
1879 type
= lookup_function_type (type
);
1880 type
= lookup_pointer_type (type
);
1882 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1884 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1886 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1887 msg_send_stret
= value_from_pointer (type
,
1888 value_as_address (msg_send_stret
));
1892 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1893 /* Special dispatcher for methods returning structs. */
1895 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1898 /* Verify the target object responds to this method. The
1899 standard top-level 'Object' class uses a different name for
1900 the verification method than the non-standard, but more
1901 often used, 'NSObject' class. Make sure we check for both. */
1904 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1905 if (responds_selector
== 0)
1907 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1909 if (responds_selector
== 0)
1910 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1913 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1914 if (method_selector
== 0)
1916 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1918 if (method_selector
== 0)
1919 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1921 /* Call the verification method, to make sure that the target
1922 class implements the desired method. */
1924 argvec
[0] = msg_send
;
1926 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1927 argvec
[3] = value_from_longest (long_type
, selector
);
1930 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1933 /* Function objc_msg_lookup returns a pointer. */
1935 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1937 if (value_as_long (ret
) == 0)
1938 error (_("Target does not respond to this message selector."));
1940 /* Call "methodForSelector:" method, to get the address of a
1941 function method that implements this selector for this
1942 class. If we can find a symbol at that address, then we
1943 know the return type, parameter types etc. (that's a good
1946 argvec
[0] = msg_send
;
1948 argvec
[2] = value_from_longest (long_type
, method_selector
);
1949 argvec
[3] = value_from_longest (long_type
, selector
);
1952 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1956 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1959 /* ret should now be the selector. */
1961 addr
= value_as_long (ret
);
1964 struct symbol
*sym
= NULL
;
1966 /* The address might point to a function descriptor;
1967 resolve it to the actual code address instead. */
1968 addr
= gdbarch_convert_from_func_ptr_addr
1969 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
1971 /* Is it a high_level symbol? */
1972 sym
= find_pc_function (addr
);
1974 method
= value_of_variable (sym
, 0);
1977 /* If we found a method with symbol information, check to see
1978 if it returns a struct. Otherwise assume it doesn't. */
1983 struct type
*val_type
;
1985 funaddr
= find_function_addr (method
, &val_type
);
1987 block_for_pc (funaddr
);
1989 val_type
= check_typedef (val_type
);
1991 if ((val_type
== NULL
)
1992 || (val_type
->code () == TYPE_CODE_ERROR
))
1994 if (expect_type
!= NULL
)
1995 val_type
= expect_type
;
1998 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2001 else if (expect_type
!= NULL
)
2003 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2004 check_typedef (expect_type
));
2007 /* Found a function symbol. Now we will substitute its
2008 value in place of the message dispatcher (obj_msgSend),
2009 so that we call the method directly instead of thru
2010 the dispatcher. The main reason for doing this is that
2011 we can now evaluate the return value and parameter values
2012 according to their known data types, in case we need to
2013 do things like promotion, dereferencing, special handling
2014 of structs and doubles, etc.
2016 We want to use the type signature of 'method', but still
2017 jump to objc_msgSend() or objc_msgSend_stret() to better
2018 mimic the behavior of the runtime. */
2022 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
2023 error (_("method address has symbol information "
2024 "with non-function type; skipping"));
2026 /* Create a function pointer of the appropriate type, and
2027 replace its value with the value of msg_send or
2028 msg_send_stret. We must use a pointer here, as
2029 msg_send and msg_send_stret are of pointer type, and
2030 the representation may be different on systems that use
2031 function descriptors. */
2034 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2035 value_as_address (msg_send_stret
));
2038 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2039 value_as_address (msg_send
));
2044 called_method
= msg_send_stret
;
2046 called_method
= msg_send
;
2050 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2052 /* If the return type doesn't look like a function type,
2053 call an error. This can happen if somebody tries to
2054 turn a variable into a function call. This is here
2055 because people often want to call, eg, strcmp, which
2056 gdb doesn't know is a function. If gdb isn't asked for
2057 it's opinion (ie. through "whatis"), it won't offer
2060 struct type
*callee_type
= value_type (called_method
);
2062 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2063 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2064 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2068 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2069 return allocate_value (expect_type
);
2071 return allocate_value (callee_type
);
2074 error (_("Expression of type other than "
2075 "\"method returning ...\" used as a method"));
2078 /* Now depending on whether we found a symbol for the method,
2079 we will either call the runtime dispatcher or the method
2083 args
[1] = value_from_longest (long_type
, selector
);
2085 if (gnu_runtime
&& (method
!= NULL
))
2087 /* Function objc_msg_lookup returns a pointer. */
2088 struct type
*tem_type
= value_type (called_method
);
2089 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2090 deprecated_set_value_type (called_method
, tem_type
);
2091 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2094 return call_function_by_hand (called_method
, NULL
, args
);
2097 /* Helper function for MULTI_SUBSCRIPT. */
2099 static struct value
*
2100 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2101 enum noside noside
, value
*arg1
,
2102 gdb::array_view
<value
*> args
)
2104 for (value
*arg2
: args
)
2106 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2108 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2112 arg1
= coerce_ref (arg1
);
2113 struct type
*type
= check_typedef (value_type (arg1
));
2115 switch (type
->code ())
2118 case TYPE_CODE_ARRAY
:
2119 case TYPE_CODE_STRING
:
2120 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2125 error (_("cannot subscript something of type `%s'"),
2128 error (_("cannot subscript requested type"));
2139 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2140 struct expression
*exp
,
2143 enum noside sub_no_side
= EVAL_NORMAL
;
2144 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2146 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2147 sub_no_side
= EVAL_NORMAL
;
2149 sub_no_side
= noside
;
2151 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2153 if (value_as_long (target
) == 0)
2154 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2156 sub_no_side
= noside
;
2157 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2158 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2159 argvec
[0] = nullptr;
2160 argvec
[1] = nullptr;
2161 for (int i
= 0; i
< args
.size (); ++i
)
2162 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2163 argvec
[args
.size () + 2] = nullptr;
2165 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2166 get
<0> (m_storage
), target
,
2167 gdb::make_array_view (argvec
,
2172 multi_subscript_operation::evaluate (struct type
*expect_type
,
2173 struct expression
*exp
,
2176 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2177 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2178 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2179 for (int ix
= 0; ix
< values
.size (); ++ix
)
2180 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2181 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2182 gdb::make_array_view (argvec
, values
.size ()));
2186 logical_and_operation::evaluate (struct type
*expect_type
,
2187 struct expression
*exp
,
2190 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2192 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2193 EVAL_AVOID_SIDE_EFFECTS
);
2195 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2197 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2198 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2202 int tem
= value_logical_not (arg1
);
2205 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2206 tem
= value_logical_not (arg2
);
2208 struct type
*type
= language_bool_type (exp
->language_defn
,
2210 return value_from_longest (type
, !tem
);
2215 logical_or_operation::evaluate (struct type
*expect_type
,
2216 struct expression
*exp
,
2219 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2221 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2222 EVAL_AVOID_SIDE_EFFECTS
);
2224 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2226 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2227 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2231 int tem
= value_logical_not (arg1
);
2234 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2235 tem
= value_logical_not (arg2
);
2238 struct type
*type
= language_bool_type (exp
->language_defn
,
2240 return value_from_longest (type
, !tem
);
2245 adl_func_operation::evaluate (struct type
*expect_type
,
2246 struct expression
*exp
,
2249 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2250 std::vector
<value
*> args (arg_ops
.size ());
2251 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2252 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2254 struct symbol
*symp
;
2255 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2258 nullptr, &symp
, nullptr, 0, noside
);
2259 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
2260 error_unknown_type (symp
->print_name ());
2261 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2262 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2263 nullptr, expect_type
);
2267 /* This function evaluates brace-initializers (in C/C++) for
2271 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2272 struct expression
*exp
,
2273 enum noside noside
, int nargs
)
2275 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2276 struct type
*struct_type
= check_typedef (value_type (struct_val
));
2277 struct type
*field_type
;
2281 while (--nargs
>= 0)
2283 struct value
*val
= NULL
;
2284 int bitpos
, bitsize
;
2288 /* Skip static fields. */
2289 while (fieldno
< struct_type
->num_fields ()
2290 && field_is_static (&struct_type
->field (fieldno
)))
2292 if (fieldno
>= struct_type
->num_fields ())
2293 error (_("too many initializers"));
2294 field_type
= struct_type
->field (fieldno
).type ();
2295 if (field_type
->code () == TYPE_CODE_UNION
2296 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
2297 error (_("don't know which variant you want to set"));
2299 /* Here, struct_type is the type of the inner struct,
2300 while substruct_type is the type of the inner struct.
2301 These are the same for normal structures, but a variant struct
2302 contains anonymous union fields that contain substruct fields.
2303 The value fieldno is the index of the top-level (normal or
2304 anonymous union) field in struct_field, while the value
2305 subfieldno is the index of the actual real (named inner) field
2306 in substruct_type. */
2308 field_type
= struct_type
->field (fieldno
).type ();
2310 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2312 /* Now actually set the field in struct_val. */
2314 /* Assign val to field fieldno. */
2315 if (value_type (val
) != field_type
)
2316 val
= value_cast (field_type
, val
);
2318 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2319 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
2320 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
2322 modify_field (struct_type
, addr
,
2323 value_as_long (val
), bitpos
% 8, bitsize
);
2325 memcpy (addr
, value_contents (val
),
2326 TYPE_LENGTH (value_type (val
)));
2333 array_operation::evaluate (struct type
*expect_type
,
2334 struct expression
*exp
,
2338 int tem2
= std::get
<0> (m_storage
);
2339 int tem3
= std::get
<1> (m_storage
);
2340 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2341 int nargs
= tem3
- tem2
+ 1;
2342 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2344 if (expect_type
!= nullptr
2345 && type
->code () == TYPE_CODE_STRUCT
)
2347 struct value
*rec
= allocate_value (expect_type
);
2349 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
2350 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2353 if (expect_type
!= nullptr
2354 && type
->code () == TYPE_CODE_ARRAY
)
2356 struct type
*range_type
= type
->index_type ();
2357 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2358 struct value
*array
= allocate_value (expect_type
);
2359 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
2360 LONGEST low_bound
, high_bound
, index
;
2362 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2365 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
2368 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
2369 for (tem
= nargs
; --nargs
>= 0;)
2371 struct value
*element
;
2373 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2375 if (value_type (element
) != element_type
)
2376 element
= value_cast (element_type
, element
);
2377 if (index
> high_bound
)
2378 /* To avoid memory corruption. */
2379 error (_("Too many array elements"));
2380 memcpy (value_contents_raw (array
)
2381 + (index
- low_bound
) * element_size
,
2382 value_contents (element
),
2389 if (expect_type
!= nullptr
2390 && type
->code () == TYPE_CODE_SET
)
2392 struct value
*set
= allocate_value (expect_type
);
2393 gdb_byte
*valaddr
= value_contents_raw (set
);
2394 struct type
*element_type
= type
->index_type ();
2395 struct type
*check_type
= element_type
;
2396 LONGEST low_bound
, high_bound
;
2398 /* Get targettype of elementtype. */
2399 while (check_type
->code () == TYPE_CODE_RANGE
2400 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2401 check_type
= TYPE_TARGET_TYPE (check_type
);
2403 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2404 error (_("(power)set type with unknown size"));
2405 memset (valaddr
, '\0', TYPE_LENGTH (type
));
2407 for (tem
= 0; tem
< nargs
; tem
++)
2409 LONGEST range_low
, range_high
;
2410 struct type
*range_low_type
, *range_high_type
;
2411 struct value
*elem_val
;
2413 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2414 range_low_type
= range_high_type
= value_type (elem_val
);
2415 range_low
= range_high
= value_as_long (elem_val
);
2417 /* Check types of elements to avoid mixture of elements from
2418 different types. Also check if type of element is "compatible"
2419 with element type of powerset. */
2420 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2421 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
2422 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2423 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
2424 if ((range_low_type
->code () != range_high_type
->code ())
2425 || (range_low_type
->code () == TYPE_CODE_ENUM
2426 && (range_low_type
!= range_high_type
)))
2427 /* different element modes. */
2428 error (_("POWERSET tuple elements of different mode"));
2429 if ((check_type
->code () != range_low_type
->code ())
2430 || (check_type
->code () == TYPE_CODE_ENUM
2431 && range_low_type
!= check_type
))
2432 error (_("incompatible POWERSET tuple elements"));
2433 if (range_low
> range_high
)
2435 warning (_("empty POWERSET tuple range"));
2438 if (range_low
< low_bound
|| range_high
> high_bound
)
2439 error (_("POWERSET tuple element out of range"));
2440 range_low
-= low_bound
;
2441 range_high
-= low_bound
;
2442 for (; range_low
<= range_high
; range_low
++)
2444 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2446 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2447 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2448 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2455 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2456 for (tem
= 0; tem
< nargs
; tem
++)
2458 /* Ensure that array expressions are coerced into pointer
2460 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2462 return value_array (tem2
, tem3
, argvec
);
2468 /* Helper for evaluate_subexp_for_address. */
2471 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2474 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2476 struct type
*type
= check_typedef (value_type (x
));
2478 if (TYPE_IS_REFERENCE (type
))
2479 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2481 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2482 return value_zero (lookup_pointer_type (value_type (x
)),
2485 error (_("Attempt to take address of "
2486 "value not located in memory."));
2488 return value_addr (x
);
2495 operation::evaluate_for_cast (struct type
*expect_type
,
2496 struct expression
*exp
,
2499 value
*val
= evaluate (expect_type
, exp
, noside
);
2500 return value_cast (expect_type
, val
);
2504 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2506 value
*val
= evaluate (nullptr, exp
, noside
);
2507 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2511 scope_operation::evaluate_for_address (struct expression
*exp
,
2514 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2515 std::get
<1> (m_storage
).c_str (),
2518 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2523 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2526 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2528 /* We can't optimize out "&*" if there's a user-defined operator*. */
2529 if (unop_user_defined_p (UNOP_IND
, x
))
2531 x
= value_x_unop (x
, UNOP_IND
, noside
);
2532 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2535 return coerce_array (x
);
2539 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2542 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2543 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2544 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2546 struct type
*type
= lookup_pointer_type (value_type (val
));
2547 return value_zero (type
, not_lval
);
2550 return value_addr (val
);
2554 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2557 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2558 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2562 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2565 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2566 EVAL_AVOID_SIDE_EFFECTS
);
2567 struct type
*type
= value_type (typeval
);
2568 return value_cast (lookup_pointer_type (type
),
2569 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2573 var_value_operation::evaluate_for_address (struct expression
*exp
,
2576 symbol
*var
= std::get
<0> (m_storage
);
2578 /* C++: The "address" of a reference should yield the address
2579 * of the object pointed to. Let value_addr() deal with it. */
2580 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2581 return operation::evaluate_for_address (exp
, noside
);
2583 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2585 struct type
*type
= lookup_pointer_type (SYMBOL_TYPE (var
));
2586 enum address_class sym_class
= SYMBOL_CLASS (var
);
2588 if (sym_class
== LOC_CONST
2589 || sym_class
== LOC_CONST_BYTES
2590 || sym_class
== LOC_REGISTER
)
2591 error (_("Attempt to take address of register or constant."));
2593 return value_zero (type
, not_lval
);
2596 return address_of_variable (var
, std::get
<1> (m_storage
));
2600 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2603 struct symbol
*var
= std::get
<0> (m_storage
);
2604 struct type
*type
= check_typedef (SYMBOL_TYPE (var
));
2605 if (type
->code () == TYPE_CODE_ARRAY
2606 && !type
->is_vector ()
2607 && CAST_IS_CONVERSION (exp
->language_defn
))
2609 struct value
*val
= address_of_variable (var
, std::get
<1> (m_storage
));
2610 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)), val
);
2612 return evaluate (nullptr, exp
, noside
);
2617 /* Helper function for evaluating the size of a type. */
2620 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2622 /* FIXME: This should be size_t. */
2623 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2624 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2625 "When applied to a reference or a reference type, the result is
2626 the size of the referenced type." */
2627 type
= check_typedef (type
);
2628 if (exp
->language_defn
->la_language
== language_cplus
2629 && (TYPE_IS_REFERENCE (type
)))
2630 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2631 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2638 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2640 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2641 return evaluate_subexp_for_sizeof_base (exp
, value_type (val
));
2645 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2649 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2650 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2652 struct type
*type
= value_type (mval
);
2653 if (type
->code () == TYPE_CODE_ERROR
)
2654 error_unknown_type (b
.minsym
->print_name ());
2656 /* FIXME: This should be size_t. */
2657 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2658 return value_from_longest (size_type
, TYPE_LENGTH (type
));
2662 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2665 if (noside
== EVAL_NORMAL
)
2667 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2668 EVAL_AVOID_SIDE_EFFECTS
);
2669 struct type
*type
= check_typedef (value_type (val
));
2670 if (type
->code () == TYPE_CODE_ARRAY
)
2672 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2673 if (type
->code () == TYPE_CODE_ARRAY
)
2675 type
= type
->index_type ();
2676 /* Only re-evaluate the right hand side if the resulting type
2677 is a variable length type. */
2678 if (type
->bounds ()->flag_bound_evaluated
)
2680 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2681 /* FIXME: This should be size_t. */
2682 struct type
*size_type
2683 = builtin_type (exp
->gdbarch
)->builtin_int
;
2684 return value_from_longest
2685 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
2691 return operation::evaluate_for_sizeof (exp
, noside
);
2695 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2698 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2699 EVAL_AVOID_SIDE_EFFECTS
);
2700 struct type
*type
= check_typedef (value_type (val
));
2701 if (type
->code () != TYPE_CODE_PTR
2702 && !TYPE_IS_REFERENCE (type
)
2703 && type
->code () != TYPE_CODE_ARRAY
)
2704 error (_("Attempt to take contents of a non-pointer value."));
2705 type
= TYPE_TARGET_TYPE (type
);
2706 if (is_dynamic_type (type
))
2707 type
= value_type (value_ind (val
));
2708 /* FIXME: This should be size_t. */
2709 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2710 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2714 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2717 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2721 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2724 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2725 EVAL_AVOID_SIDE_EFFECTS
);
2726 return evaluate_subexp_for_sizeof_base (exp
, value_type (typeval
));
2730 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2733 struct type
*type
= SYMBOL_TYPE (std::get
<0> (m_storage
));
2734 if (is_dynamic_type (type
))
2736 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2737 type
= value_type (val
);
2738 if (type
->code () == TYPE_CODE_ARRAY
)
2740 /* FIXME: This should be size_t. */
2741 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2742 if (type_not_allocated (type
) || type_not_associated (type
))
2743 return value_zero (size_type
, not_lval
);
2744 else if (is_dynamic_type (type
->index_type ())
2745 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2746 return allocate_optimized_out_value (size_type
);
2749 return evaluate_subexp_for_sizeof_base (exp
, type
);
2753 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2754 struct expression
*exp
,
2757 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2758 return value_zero (to_type
, not_lval
);
2760 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2761 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2763 val
= value_cast (to_type
, val
);
2765 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2766 if (VALUE_LVAL (val
) == lval_memory
)
2768 if (value_lazy (val
))
2769 value_fetch_lazy (val
);
2770 VALUE_LVAL (val
) = not_lval
;
2776 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2777 struct expression
*exp
,
2780 value
*val
= evaluate_var_value (noside
,
2781 std::get
<1> (m_storage
),
2782 std::get
<0> (m_storage
));
2784 val
= value_cast (to_type
, val
);
2786 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2787 if (VALUE_LVAL (val
) == lval_memory
)
2789 if (value_lazy (val
))
2790 value_fetch_lazy (val
);
2791 VALUE_LVAL (val
) = not_lval
;
2798 /* Parse a type expression in the string [P..P+LENGTH). */
2801 parse_and_eval_type (const char *p
, int length
)
2803 char *tmp
= (char *) alloca (length
+ 4);
2806 memcpy (tmp
+ 1, p
, length
);
2807 tmp
[length
+ 1] = ')';
2808 tmp
[length
+ 2] = '0';
2809 tmp
[length
+ 3] = '\0';
2810 expression_up expr
= parse_expression (tmp
);
2811 expr::unop_cast_operation
*op
2812 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2814 error (_("Internal error in eval_type."));
2815 return op
->get_type ();