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 () && inferior_ptid
!= null_ptid
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
).symbol
;
583 if (SYMBOL_TYPE (var
)->code () == TYPE_CODE_ERROR
)
584 error_unknown_type (var
->print_name ());
585 return evaluate_var_value (noside
, std::get
<0> (m_storage
).block
, 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
).symbol
,
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
<0> (m_storage
).block
,
730 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
731 nullptr, expect_type
);
735 scope_operation::evaluate_funcall (struct type
*expect_type
,
736 struct expression
*exp
,
738 const std::vector
<operation_up
> &args
)
740 if (!overload_resolution
741 || exp
->language_defn
->la_language
!= language_cplus
)
742 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
744 /* Unpack it locally so we can properly handle overload
746 const std::string
&name
= std::get
<1> (m_storage
);
747 struct type
*type
= std::get
<0> (m_storage
);
749 symbol
*function
= NULL
;
750 const char *function_name
= NULL
;
751 std::vector
<value
*> argvec (1 + args
.size ());
752 if (type
->code () == TYPE_CODE_NAMESPACE
)
754 function
= cp_lookup_symbol_namespace (type
->name (),
756 get_selected_block (0),
758 if (function
== NULL
)
759 error (_("No symbol \"%s\" in namespace \"%s\"."),
760 name
.c_str (), type
->name ());
764 gdb_assert (type
->code () == TYPE_CODE_STRUCT
765 || type
->code () == TYPE_CODE_UNION
);
766 function_name
= name
.c_str ();
768 /* We need a properly typed value for method lookup. */
769 argvec
[0] = value_zero (type
, lval_memory
);
772 for (int i
= 0; i
< args
.size (); ++i
)
773 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
774 gdb::array_view
<value
*> arg_view
= argvec
;
776 value
*callee
= nullptr;
777 if (function_name
!= nullptr)
781 find_overload_match (arg_view
, function_name
, METHOD
,
782 &argvec
[0], nullptr, &callee
, nullptr,
783 &static_memfuncp
, 0, noside
);
784 if (!static_memfuncp
)
786 /* For the time being, we don't handle this. */
787 error (_("Call to overloaded function %s requires "
792 arg_view
= arg_view
.slice (1);
797 arg_view
= arg_view
.slice (1);
798 find_overload_match (arg_view
, nullptr,
799 NON_METHOD
, nullptr, function
,
800 nullptr, &symp
, nullptr, 1, noside
);
801 callee
= value_of_variable (symp
, get_selected_block (0));
804 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
805 nullptr, expect_type
);
809 structop_member_base::evaluate_funcall (struct type
*expect_type
,
810 struct expression
*exp
,
812 const std::vector
<operation_up
> &args
)
814 /* First, evaluate the structure into lhs. */
816 if (opcode () == STRUCTOP_MEMBER
)
817 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
819 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
821 std::vector
<value
*> vals (args
.size () + 1);
822 gdb::array_view
<value
*> val_view
= vals
;
823 /* If the function is a virtual function, then the aggregate
824 value (providing the structure) plays its part by providing
825 the vtable. Otherwise, it is just along for the ride: call
826 the function directly. */
827 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
830 type
*a1_type
= check_typedef (value_type (rhs
));
831 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
833 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
834 callee
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
836 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
840 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
842 struct type
*type_ptr
843 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
844 struct type
*target_type_ptr
845 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
847 /* Now, convert this value to an address. */
848 lhs
= value_cast (type_ptr
, lhs
);
850 long mem_offset
= value_as_long (rhs
);
852 callee
= value_from_pointer (target_type_ptr
,
853 value_as_long (lhs
) + mem_offset
);
854 callee
= value_ind (callee
);
856 val_view
= val_view
.slice (1);
859 error (_("Non-pointer-to-member value used in pointer-to-member "
862 for (int i
= 0; i
< args
.size (); ++i
)
863 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
865 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
866 nullptr, expect_type
);
871 structop_base_operation::evaluate_funcall
872 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
873 const std::vector
<operation_up
> &args
)
875 /* Allocate space for the function call arguments, Including space for a
876 `this' pointer at the start. */
877 std::vector
<value
*> vals (args
.size () + 1);
878 /* First, evaluate the structure into vals[0]. */
879 enum exp_opcode op
= opcode ();
880 if (op
== STRUCTOP_STRUCT
)
882 /* If v is a variable in a register, and the user types
883 v.method (), this will produce an error, because v has no
886 A possible way around this would be to allocate a copy of
887 the variable on the stack, copy in the contents, call the
888 function, and copy out the contents. I.e. convert this
889 from call by reference to call by copy-return (or
890 whatever it's called). However, this does not work
891 because it is not the same: the method being called could
892 stash a copy of the address, and then future uses through
893 that address (after the method returns) would be expected
894 to use the variable itself, not some copy of it. */
895 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
899 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
900 /* Check to see if the operator '->' has been overloaded.
901 If the operator has been overloaded replace vals[0] with the
902 value returned by the custom operator and continue
904 while (unop_user_defined_p (op
, vals
[0]))
906 struct value
*value
= nullptr;
909 value
= value_x_unop (vals
[0], op
, noside
);
911 catch (const gdb_exception_error
&except
)
913 if (except
.error
== NOT_FOUND_ERROR
)
923 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
924 pointer we placed into vals[0]. */
925 for (int i
= 0; i
< args
.size (); ++i
)
926 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
928 /* The array view includes the `this' pointer. */
929 gdb::array_view
<value
*> arg_view (vals
);
933 const char *tstr
= std::get
<1> (m_storage
).c_str ();
934 if (overload_resolution
935 && exp
->language_defn
->la_language
== language_cplus
)
937 /* Language is C++, do some overload resolution before
939 value
*val0
= vals
[0];
940 find_overload_match (arg_view
, tstr
, METHOD
,
941 &val0
, nullptr, &callee
, nullptr,
942 &static_memfuncp
, 0, noside
);
946 /* Non-C++ case -- or no overload resolution. */
948 struct value
*temp
= vals
[0];
950 callee
= value_struct_elt (&temp
, arg_view
, tstr
,
952 op
== STRUCTOP_STRUCT
953 ? "structure" : "structure pointer");
954 /* value_struct_elt updates temp with the correct value of the
955 ``this'' pointer if necessary, so modify it to reflect any
957 vals
[0] = value_from_longest (lookup_pointer_type (value_type (temp
)),
959 + value_embedded_offset (temp
));
962 /* Take out `this' if needed. */
964 arg_view
= arg_view
.slice (1);
966 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
967 nullptr, expect_type
);
971 } /* namespace expr */
973 /* Return true if type is integral or reference to integral */
976 is_integral_or_integral_reference (struct type
*type
)
978 if (is_integral_type (type
))
981 type
= check_typedef (type
);
982 return (type
!= nullptr
983 && TYPE_IS_REFERENCE (type
)
984 && is_integral_type (TYPE_TARGET_TYPE (type
)));
987 /* Helper function that implements the body of OP_SCOPE. */
990 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
992 struct type
*type
, const char *string
)
994 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
997 error (_("There is no field named %s"), string
);
1001 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1004 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1005 enum noside noside
, symbol
*sym
)
1007 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1008 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
1010 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1011 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1012 error (_("Symbol \"%s\" does not have any specific entry value"),
1013 sym
->print_name ());
1015 struct frame_info
*frame
= get_selected_frame (NULL
);
1016 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1019 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1022 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1023 enum noside noside
, bool outermost_p
,
1024 bound_minimal_symbol msymbol
)
1026 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1029 struct type
*type
= value_type (val
);
1030 if (type
->code () == TYPE_CODE_ERROR
1031 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1032 error_unknown_type (msymbol
.minsym
->print_name ());
1036 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1039 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1041 value
*func
, const char *var
)
1043 CORE_ADDR addr
= value_address (func
);
1044 const block
*blk
= block_for_pc (addr
);
1045 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1046 if (sym
.symbol
== NULL
)
1047 error (_("No symbol \"%s\" in specified context."), var
);
1048 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1051 /* Helper function that implements the body of OP_REGISTER. */
1054 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1055 enum noside noside
, const char *name
)
1060 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1061 name
, strlen (name
));
1063 error (_("Register $%s not available."), name
);
1065 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1066 a value with the appropriate register type. Unfortunately,
1067 we don't have easy access to the type of user registers.
1068 So for these registers, we fetch the register value regardless
1069 of the evaluation mode. */
1070 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1071 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1072 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1074 val
= value_of_register (regno
, get_selected_frame (NULL
));
1076 error (_("Value of register %s not available."), name
);
1081 /* Helper function that implements the body of OP_STRING. */
1084 eval_op_string (struct type
*expect_type
, struct expression
*exp
,
1085 enum noside noside
, int len
, const char *string
)
1087 struct type
*type
= language_string_char_type (exp
->language_defn
,
1089 return value_string (string
, len
, type
);
1092 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1095 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1099 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1100 return value_from_longest (selector_type
,
1101 lookup_child_selector (exp
->gdbarch
, sel
));
1104 /* Helper function that implements the body of BINOP_CONCAT. */
1107 eval_op_concat (struct type
*expect_type
, struct expression
*exp
,
1108 enum noside noside
, struct value
*arg1
, struct value
*arg2
)
1110 if (binop_user_defined_p (BINOP_CONCAT
, arg1
, arg2
))
1111 return value_x_binop (arg1
, arg2
, BINOP_CONCAT
, OP_NULL
, noside
);
1113 return value_concat (arg1
, arg2
);
1116 /* A helper function for TERNOP_SLICE. */
1119 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1121 struct value
*array
, struct value
*low
, struct value
*upper
)
1123 int lowbound
= value_as_long (low
);
1124 int upperbound
= value_as_long (upper
);
1125 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1128 /* A helper function for STRUCTOP_STRUCT. */
1131 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1133 struct value
*arg1
, const char *string
)
1135 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1137 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1138 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1142 /* A helper function for STRUCTOP_PTR. */
1145 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1147 struct value
*arg1
, const char *string
)
1149 /* Check to see if operator '->' has been overloaded. If so replace
1150 arg1 with the value returned by evaluating operator->(). */
1151 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1153 struct value
*value
= NULL
;
1156 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1159 catch (const gdb_exception_error
&except
)
1161 if (except
.error
== NOT_FOUND_ERROR
)
1170 /* JYG: if print object is on we need to replace the base type
1171 with rtti type in order to continue on with successful
1172 lookup of member / method only available in the rtti type. */
1174 struct type
*arg_type
= value_type (arg1
);
1175 struct type
*real_type
;
1176 int full
, using_enc
;
1178 struct value_print_options opts
;
1180 get_user_print_options (&opts
);
1181 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1182 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1184 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1187 arg1
= value_cast (real_type
, arg1
);
1191 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1192 NULL
, "structure pointer");
1193 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1194 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1198 /* A helper function for STRUCTOP_MEMBER. */
1201 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1203 struct value
*arg1
, struct value
*arg2
)
1208 struct type
*type
= check_typedef (value_type (arg2
));
1209 switch (type
->code ())
1211 case TYPE_CODE_METHODPTR
:
1212 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1213 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1216 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1217 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1218 return value_ind (arg2
);
1221 case TYPE_CODE_MEMBERPTR
:
1222 /* Now, convert these values to an address. */
1223 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1226 mem_offset
= value_as_long (arg2
);
1228 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1229 value_as_long (arg1
) + mem_offset
);
1230 return value_ind (arg3
);
1233 error (_("non-pointer-to-member value used "
1234 "in pointer-to-member construct"));
1238 /* A helper function for BINOP_ADD. */
1241 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1243 struct value
*arg1
, struct value
*arg2
)
1245 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1246 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1247 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1248 && is_integral_or_integral_reference (value_type (arg2
)))
1249 return value_ptradd (arg1
, value_as_long (arg2
));
1250 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
1251 && is_integral_or_integral_reference (value_type (arg1
)))
1252 return value_ptradd (arg2
, value_as_long (arg1
));
1255 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1256 return value_binop (arg1
, arg2
, BINOP_ADD
);
1260 /* A helper function for BINOP_SUB. */
1263 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1265 struct value
*arg1
, struct value
*arg2
)
1267 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1268 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1269 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1270 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
1272 /* FIXME -- should be ptrdiff_t */
1273 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1274 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1276 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1277 && is_integral_or_integral_reference (value_type (arg2
)))
1278 return value_ptradd (arg1
, - value_as_long (arg2
));
1281 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1282 return value_binop (arg1
, arg2
, BINOP_SUB
);
1286 /* Helper function for several different binary operations. */
1289 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1290 enum noside noside
, enum exp_opcode op
,
1291 struct value
*arg1
, struct value
*arg2
)
1293 if (binop_user_defined_p (op
, arg1
, arg2
))
1294 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1297 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1298 fudge arg2 to avoid division-by-zero, the caller is
1299 (theoretically) only looking for the type of the result. */
1300 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1301 /* ??? Do we really want to test for BINOP_MOD here?
1302 The implementation of value_binop gives it a well-defined
1305 || op
== BINOP_INTDIV
1308 && value_logical_not (arg2
))
1310 struct value
*v_one
;
1312 v_one
= value_one (value_type (arg2
));
1313 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1314 return value_binop (arg1
, v_one
, op
);
1318 /* For shift and integer exponentiation operations,
1319 only promote the first argument. */
1320 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1321 && is_integral_type (value_type (arg2
)))
1322 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1324 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1326 return value_binop (arg1
, arg2
, op
);
1331 /* A helper function for BINOP_SUBSCRIPT. */
1334 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1335 enum noside noside
, enum exp_opcode op
,
1336 struct value
*arg1
, struct value
*arg2
)
1338 if (binop_user_defined_p (op
, arg1
, arg2
))
1339 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1342 /* If the user attempts to subscript something that is not an
1343 array or pointer type (like a plain int variable for example),
1344 then report this as an error. */
1346 arg1
= coerce_ref (arg1
);
1347 struct type
*type
= check_typedef (value_type (arg1
));
1348 if (type
->code () != TYPE_CODE_ARRAY
1349 && type
->code () != TYPE_CODE_PTR
)
1352 error (_("cannot subscript something of type `%s'"),
1355 error (_("cannot subscript requested type"));
1358 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1359 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1361 return value_subscript (arg1
, value_as_long (arg2
));
1365 /* A helper function for BINOP_EQUAL. */
1368 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1369 enum noside noside
, enum exp_opcode op
,
1370 struct value
*arg1
, struct value
*arg2
)
1372 if (binop_user_defined_p (op
, arg1
, arg2
))
1374 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1378 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1379 int tem
= value_equal (arg1
, arg2
);
1380 struct type
*type
= language_bool_type (exp
->language_defn
,
1382 return value_from_longest (type
, (LONGEST
) tem
);
1386 /* A helper function for BINOP_NOTEQUAL. */
1389 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1390 enum noside noside
, enum exp_opcode op
,
1391 struct value
*arg1
, struct value
*arg2
)
1393 if (binop_user_defined_p (op
, arg1
, arg2
))
1395 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1399 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1400 int tem
= value_equal (arg1
, arg2
);
1401 struct type
*type
= language_bool_type (exp
->language_defn
,
1403 return value_from_longest (type
, (LONGEST
) ! tem
);
1407 /* A helper function for BINOP_LESS. */
1410 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1411 enum noside noside
, enum exp_opcode op
,
1412 struct value
*arg1
, struct value
*arg2
)
1414 if (binop_user_defined_p (op
, arg1
, arg2
))
1416 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1420 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1421 int tem
= value_less (arg1
, arg2
);
1422 struct type
*type
= language_bool_type (exp
->language_defn
,
1424 return value_from_longest (type
, (LONGEST
) tem
);
1428 /* A helper function for BINOP_GTR. */
1431 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1432 enum noside noside
, enum exp_opcode op
,
1433 struct value
*arg1
, struct value
*arg2
)
1435 if (binop_user_defined_p (op
, arg1
, arg2
))
1437 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1441 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1442 int tem
= value_less (arg2
, arg1
);
1443 struct type
*type
= language_bool_type (exp
->language_defn
,
1445 return value_from_longest (type
, (LONGEST
) tem
);
1449 /* A helper function for BINOP_GEQ. */
1452 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1453 enum noside noside
, enum exp_opcode op
,
1454 struct value
*arg1
, struct value
*arg2
)
1456 if (binop_user_defined_p (op
, arg1
, arg2
))
1458 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1462 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1463 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1464 struct type
*type
= language_bool_type (exp
->language_defn
,
1466 return value_from_longest (type
, (LONGEST
) tem
);
1470 /* A helper function for BINOP_LEQ. */
1473 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1474 enum noside noside
, enum exp_opcode op
,
1475 struct value
*arg1
, struct value
*arg2
)
1477 if (binop_user_defined_p (op
, arg1
, arg2
))
1479 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1483 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1484 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1485 struct type
*type
= language_bool_type (exp
->language_defn
,
1487 return value_from_longest (type
, (LONGEST
) tem
);
1491 /* A helper function for BINOP_REPEAT. */
1494 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1495 enum noside noside
, enum exp_opcode op
,
1496 struct value
*arg1
, struct value
*arg2
)
1498 struct type
*type
= check_typedef (value_type (arg2
));
1499 if (type
->code () != TYPE_CODE_INT
1500 && type
->code () != TYPE_CODE_ENUM
)
1501 error (_("Non-integral right operand for \"@\" operator."));
1502 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1504 return allocate_repeat_value (value_type (arg1
),
1505 longest_to_int (value_as_long (arg2
)));
1508 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1511 /* A helper function for UNOP_PLUS. */
1514 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1515 enum noside noside
, enum exp_opcode op
,
1518 if (unop_user_defined_p (op
, arg1
))
1519 return value_x_unop (arg1
, op
, noside
);
1522 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1523 return value_pos (arg1
);
1527 /* A helper function for UNOP_NEG. */
1530 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1531 enum noside noside
, enum exp_opcode op
,
1534 if (unop_user_defined_p (op
, arg1
))
1535 return value_x_unop (arg1
, op
, noside
);
1538 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1539 return value_neg (arg1
);
1543 /* A helper function for UNOP_COMPLEMENT. */
1546 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1547 enum noside noside
, enum exp_opcode op
,
1550 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1551 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1554 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1555 return value_complement (arg1
);
1559 /* A helper function for UNOP_LOGICAL_NOT. */
1562 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1563 enum noside noside
, enum exp_opcode op
,
1566 if (unop_user_defined_p (op
, arg1
))
1567 return value_x_unop (arg1
, op
, noside
);
1570 struct type
*type
= language_bool_type (exp
->language_defn
,
1572 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1576 /* A helper function for UNOP_IND. */
1579 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1583 struct type
*type
= check_typedef (value_type (arg1
));
1584 if (type
->code () == TYPE_CODE_METHODPTR
1585 || type
->code () == TYPE_CODE_MEMBERPTR
)
1586 error (_("Attempt to dereference pointer "
1587 "to member without an object"));
1588 if (unop_user_defined_p (UNOP_IND
, arg1
))
1589 return value_x_unop (arg1
, UNOP_IND
, noside
);
1590 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1592 type
= check_typedef (value_type (arg1
));
1594 /* If the type pointed to is dynamic then in order to resolve the
1595 dynamic properties we must actually dereference the pointer.
1596 There is a risk that this dereference will have side-effects
1597 in the inferior, but being able to print accurate type
1598 information seems worth the risk. */
1599 if ((type
->code () != TYPE_CODE_PTR
1600 && !TYPE_IS_REFERENCE (type
))
1601 || !is_dynamic_type (TYPE_TARGET_TYPE (type
)))
1603 if (type
->code () == TYPE_CODE_PTR
1604 || TYPE_IS_REFERENCE (type
)
1605 /* In C you can dereference an array to get the 1st elt. */
1606 || type
->code () == TYPE_CODE_ARRAY
)
1607 return value_zero (TYPE_TARGET_TYPE (type
),
1609 else if (type
->code () == TYPE_CODE_INT
)
1610 /* GDB allows dereferencing an int. */
1611 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1614 error (_("Attempt to take contents of a non-pointer value."));
1618 /* Allow * on an integer so we can cast it to whatever we want.
1619 This returns an int, which seems like the most C-like thing to
1620 do. "long long" variables are rare enough that
1621 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1622 if (type
->code () == TYPE_CODE_INT
)
1623 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1624 (CORE_ADDR
) value_as_address (arg1
));
1625 return value_ind (arg1
);
1628 /* A helper function for UNOP_ALIGNOF. */
1631 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1635 struct type
*type
= value_type (arg1
);
1636 /* FIXME: This should be size_t. */
1637 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1638 ULONGEST align
= type_align (type
);
1640 error (_("could not determine alignment of type"));
1641 return value_from_longest (size_type
, align
);
1644 /* A helper function for UNOP_MEMVAL. */
1647 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1649 struct value
*arg1
, struct type
*type
)
1651 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1652 return value_zero (type
, lval_memory
);
1654 return value_at_lazy (type
, value_as_address (arg1
));
1657 /* A helper function for UNOP_PREINCREMENT. */
1660 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1661 enum noside noside
, enum exp_opcode op
,
1664 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1666 else if (unop_user_defined_p (op
, arg1
))
1668 return value_x_unop (arg1
, op
, noside
);
1673 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1674 arg2
= value_ptradd (arg1
, 1);
1677 struct value
*tmp
= arg1
;
1679 arg2
= value_one (value_type (arg1
));
1680 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1681 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1684 return value_assign (arg1
, arg2
);
1688 /* A helper function for UNOP_PREDECREMENT. */
1691 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1692 enum noside noside
, enum exp_opcode op
,
1695 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1697 else if (unop_user_defined_p (op
, arg1
))
1699 return value_x_unop (arg1
, op
, noside
);
1704 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1705 arg2
= value_ptradd (arg1
, -1);
1708 struct value
*tmp
= arg1
;
1710 arg2
= value_one (value_type (arg1
));
1711 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1712 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1715 return value_assign (arg1
, arg2
);
1719 /* A helper function for UNOP_POSTINCREMENT. */
1722 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1723 enum noside noside
, enum exp_opcode op
,
1726 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1728 else if (unop_user_defined_p (op
, arg1
))
1730 return value_x_unop (arg1
, op
, noside
);
1734 struct value
*arg3
= value_non_lval (arg1
);
1737 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1738 arg2
= value_ptradd (arg1
, 1);
1741 struct value
*tmp
= arg1
;
1743 arg2
= value_one (value_type (arg1
));
1744 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1745 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1748 value_assign (arg1
, arg2
);
1753 /* A helper function for UNOP_POSTDECREMENT. */
1756 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1757 enum noside noside
, enum exp_opcode op
,
1760 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1762 else if (unop_user_defined_p (op
, arg1
))
1764 return value_x_unop (arg1
, op
, noside
);
1768 struct value
*arg3
= value_non_lval (arg1
);
1771 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
1772 arg2
= value_ptradd (arg1
, -1);
1775 struct value
*tmp
= arg1
;
1777 arg2
= value_one (value_type (arg1
));
1778 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1779 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1782 value_assign (arg1
, arg2
);
1787 /* A helper function for OP_TYPE. */
1790 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1791 enum noside noside
, struct type
*type
)
1793 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1794 return allocate_value (type
);
1796 error (_("Attempt to use a type name as an expression"));
1799 /* A helper function for BINOP_ASSIGN_MODIFY. */
1802 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1803 enum noside noside
, enum exp_opcode op
,
1804 struct value
*arg1
, struct value
*arg2
)
1806 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1808 if (binop_user_defined_p (op
, arg1
, arg2
))
1809 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1810 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1812 && is_integral_type (value_type (arg2
)))
1813 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1814 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1816 && is_integral_type (value_type (arg2
)))
1817 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1820 struct value
*tmp
= arg1
;
1822 /* For shift and integer exponentiation operations,
1823 only promote the first argument. */
1824 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1825 && is_integral_type (value_type (arg2
)))
1826 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1828 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1830 arg2
= value_binop (tmp
, arg2
, op
);
1832 return value_assign (arg1
, arg2
);
1835 /* Note that ARGS needs 2 empty slots up front and must end with a
1837 static struct value
*
1838 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1839 enum noside noside
, CORE_ADDR selector
,
1840 value
*target
, gdb::array_view
<value
*> args
)
1842 CORE_ADDR responds_selector
= 0;
1843 CORE_ADDR method_selector
= 0;
1845 int struct_return
= 0;
1847 struct value
*msg_send
= NULL
;
1848 struct value
*msg_send_stret
= NULL
;
1849 int gnu_runtime
= 0;
1851 struct value
*method
= NULL
;
1852 struct value
*called_method
= NULL
;
1854 struct type
*selector_type
= NULL
;
1855 struct type
*long_type
;
1858 struct value
*ret
= NULL
;
1863 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1864 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1866 if (value_as_long (target
) == 0)
1867 return value_from_longest (long_type
, 0);
1869 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1872 /* Find the method dispatch (Apple runtime) or method lookup
1873 (GNU runtime) function for Objective-C. These will be used
1874 to lookup the symbol information for the method. If we
1875 can't find any symbol information, then we'll use these to
1876 call the method, otherwise we can call the method
1877 directly. The msg_send_stret function is used in the special
1878 case of a method that returns a structure (Apple runtime
1882 type
= selector_type
;
1884 type
= lookup_function_type (type
);
1885 type
= lookup_pointer_type (type
);
1886 type
= lookup_function_type (type
);
1887 type
= lookup_pointer_type (type
);
1889 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1891 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1893 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1894 msg_send_stret
= value_from_pointer (type
,
1895 value_as_address (msg_send_stret
));
1899 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1900 /* Special dispatcher for methods returning structs. */
1902 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1905 /* Verify the target object responds to this method. The
1906 standard top-level 'Object' class uses a different name for
1907 the verification method than the non-standard, but more
1908 often used, 'NSObject' class. Make sure we check for both. */
1911 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1912 if (responds_selector
== 0)
1914 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1916 if (responds_selector
== 0)
1917 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1920 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1921 if (method_selector
== 0)
1923 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1925 if (method_selector
== 0)
1926 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1928 /* Call the verification method, to make sure that the target
1929 class implements the desired method. */
1931 argvec
[0] = msg_send
;
1933 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1934 argvec
[3] = value_from_longest (long_type
, selector
);
1937 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1940 /* Function objc_msg_lookup returns a pointer. */
1942 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1944 if (value_as_long (ret
) == 0)
1945 error (_("Target does not respond to this message selector."));
1947 /* Call "methodForSelector:" method, to get the address of a
1948 function method that implements this selector for this
1949 class. If we can find a symbol at that address, then we
1950 know the return type, parameter types etc. (that's a good
1953 argvec
[0] = msg_send
;
1955 argvec
[2] = value_from_longest (long_type
, method_selector
);
1956 argvec
[3] = value_from_longest (long_type
, selector
);
1959 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1963 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1966 /* ret should now be the selector. */
1968 addr
= value_as_long (ret
);
1971 struct symbol
*sym
= NULL
;
1973 /* The address might point to a function descriptor;
1974 resolve it to the actual code address instead. */
1975 addr
= gdbarch_convert_from_func_ptr_addr
1976 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
1978 /* Is it a high_level symbol? */
1979 sym
= find_pc_function (addr
);
1981 method
= value_of_variable (sym
, 0);
1984 /* If we found a method with symbol information, check to see
1985 if it returns a struct. Otherwise assume it doesn't. */
1990 struct type
*val_type
;
1992 funaddr
= find_function_addr (method
, &val_type
);
1994 block_for_pc (funaddr
);
1996 val_type
= check_typedef (val_type
);
1998 if ((val_type
== NULL
)
1999 || (val_type
->code () == TYPE_CODE_ERROR
))
2001 if (expect_type
!= NULL
)
2002 val_type
= expect_type
;
2005 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2008 else if (expect_type
!= NULL
)
2010 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2011 check_typedef (expect_type
));
2014 /* Found a function symbol. Now we will substitute its
2015 value in place of the message dispatcher (obj_msgSend),
2016 so that we call the method directly instead of thru
2017 the dispatcher. The main reason for doing this is that
2018 we can now evaluate the return value and parameter values
2019 according to their known data types, in case we need to
2020 do things like promotion, dereferencing, special handling
2021 of structs and doubles, etc.
2023 We want to use the type signature of 'method', but still
2024 jump to objc_msgSend() or objc_msgSend_stret() to better
2025 mimic the behavior of the runtime. */
2029 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
2030 error (_("method address has symbol information "
2031 "with non-function type; skipping"));
2033 /* Create a function pointer of the appropriate type, and
2034 replace its value with the value of msg_send or
2035 msg_send_stret. We must use a pointer here, as
2036 msg_send and msg_send_stret are of pointer type, and
2037 the representation may be different on systems that use
2038 function descriptors. */
2041 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2042 value_as_address (msg_send_stret
));
2045 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2046 value_as_address (msg_send
));
2051 called_method
= msg_send_stret
;
2053 called_method
= msg_send
;
2057 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2059 /* If the return type doesn't look like a function type,
2060 call an error. This can happen if somebody tries to
2061 turn a variable into a function call. This is here
2062 because people often want to call, eg, strcmp, which
2063 gdb doesn't know is a function. If gdb isn't asked for
2064 it's opinion (ie. through "whatis"), it won't offer
2067 struct type
*callee_type
= value_type (called_method
);
2069 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2070 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2071 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2075 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2076 return allocate_value (expect_type
);
2078 return allocate_value (callee_type
);
2081 error (_("Expression of type other than "
2082 "\"method returning ...\" used as a method"));
2085 /* Now depending on whether we found a symbol for the method,
2086 we will either call the runtime dispatcher or the method
2090 args
[1] = value_from_longest (long_type
, selector
);
2092 if (gnu_runtime
&& (method
!= NULL
))
2094 /* Function objc_msg_lookup returns a pointer. */
2095 struct type
*tem_type
= value_type (called_method
);
2096 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2097 deprecated_set_value_type (called_method
, tem_type
);
2098 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2101 return call_function_by_hand (called_method
, NULL
, args
);
2104 /* Helper function for MULTI_SUBSCRIPT. */
2106 static struct value
*
2107 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2108 enum noside noside
, value
*arg1
,
2109 gdb::array_view
<value
*> args
)
2111 for (value
*arg2
: args
)
2113 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2115 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2119 arg1
= coerce_ref (arg1
);
2120 struct type
*type
= check_typedef (value_type (arg1
));
2122 switch (type
->code ())
2125 case TYPE_CODE_ARRAY
:
2126 case TYPE_CODE_STRING
:
2127 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2132 error (_("cannot subscript something of type `%s'"),
2135 error (_("cannot subscript requested type"));
2146 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2147 struct expression
*exp
,
2150 enum noside sub_no_side
= EVAL_NORMAL
;
2151 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2153 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2154 sub_no_side
= EVAL_NORMAL
;
2156 sub_no_side
= noside
;
2158 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2160 if (value_as_long (target
) == 0)
2161 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2163 sub_no_side
= noside
;
2164 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2165 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2166 argvec
[0] = nullptr;
2167 argvec
[1] = nullptr;
2168 for (int i
= 0; i
< args
.size (); ++i
)
2169 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2170 argvec
[args
.size () + 2] = nullptr;
2172 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2173 get
<0> (m_storage
), target
,
2174 gdb::make_array_view (argvec
,
2179 multi_subscript_operation::evaluate (struct type
*expect_type
,
2180 struct expression
*exp
,
2183 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2184 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2185 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2186 for (int ix
= 0; ix
< values
.size (); ++ix
)
2187 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2188 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2189 gdb::make_array_view (argvec
, values
.size ()));
2193 logical_and_operation::evaluate (struct type
*expect_type
,
2194 struct expression
*exp
,
2197 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2199 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2200 EVAL_AVOID_SIDE_EFFECTS
);
2202 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2204 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2205 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2209 int tem
= value_logical_not (arg1
);
2212 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2213 tem
= value_logical_not (arg2
);
2215 struct type
*type
= language_bool_type (exp
->language_defn
,
2217 return value_from_longest (type
, !tem
);
2222 logical_or_operation::evaluate (struct type
*expect_type
,
2223 struct expression
*exp
,
2226 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2228 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2229 EVAL_AVOID_SIDE_EFFECTS
);
2231 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2233 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2234 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2238 int tem
= value_logical_not (arg1
);
2241 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2242 tem
= value_logical_not (arg2
);
2245 struct type
*type
= language_bool_type (exp
->language_defn
,
2247 return value_from_longest (type
, !tem
);
2252 adl_func_operation::evaluate (struct type
*expect_type
,
2253 struct expression
*exp
,
2256 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2257 std::vector
<value
*> args (arg_ops
.size ());
2258 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2259 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2261 struct symbol
*symp
;
2262 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2265 nullptr, &symp
, nullptr, 0, noside
);
2266 if (SYMBOL_TYPE (symp
)->code () == TYPE_CODE_ERROR
)
2267 error_unknown_type (symp
->print_name ());
2268 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2269 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2270 nullptr, expect_type
);
2274 /* This function evaluates brace-initializers (in C/C++) for
2278 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2279 struct expression
*exp
,
2280 enum noside noside
, int nargs
)
2282 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2283 struct type
*struct_type
= check_typedef (value_type (struct_val
));
2284 struct type
*field_type
;
2288 while (--nargs
>= 0)
2290 struct value
*val
= NULL
;
2291 int bitpos
, bitsize
;
2295 /* Skip static fields. */
2296 while (fieldno
< struct_type
->num_fields ()
2297 && field_is_static (&struct_type
->field (fieldno
)))
2299 if (fieldno
>= struct_type
->num_fields ())
2300 error (_("too many initializers"));
2301 field_type
= struct_type
->field (fieldno
).type ();
2302 if (field_type
->code () == TYPE_CODE_UNION
2303 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
2304 error (_("don't know which variant you want to set"));
2306 /* Here, struct_type is the type of the inner struct,
2307 while substruct_type is the type of the inner struct.
2308 These are the same for normal structures, but a variant struct
2309 contains anonymous union fields that contain substruct fields.
2310 The value fieldno is the index of the top-level (normal or
2311 anonymous union) field in struct_field, while the value
2312 subfieldno is the index of the actual real (named inner) field
2313 in substruct_type. */
2315 field_type
= struct_type
->field (fieldno
).type ();
2317 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2319 /* Now actually set the field in struct_val. */
2321 /* Assign val to field fieldno. */
2322 if (value_type (val
) != field_type
)
2323 val
= value_cast (field_type
, val
);
2325 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2326 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
2327 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
2329 modify_field (struct_type
, addr
,
2330 value_as_long (val
), bitpos
% 8, bitsize
);
2332 memcpy (addr
, value_contents (val
),
2333 TYPE_LENGTH (value_type (val
)));
2340 array_operation::evaluate (struct type
*expect_type
,
2341 struct expression
*exp
,
2345 int tem2
= std::get
<0> (m_storage
);
2346 int tem3
= std::get
<1> (m_storage
);
2347 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2348 int nargs
= tem3
- tem2
+ 1;
2349 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2351 if (expect_type
!= nullptr
2352 && type
->code () == TYPE_CODE_STRUCT
)
2354 struct value
*rec
= allocate_value (expect_type
);
2356 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
2357 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2360 if (expect_type
!= nullptr
2361 && type
->code () == TYPE_CODE_ARRAY
)
2363 struct type
*range_type
= type
->index_type ();
2364 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
2365 struct value
*array
= allocate_value (expect_type
);
2366 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
2367 LONGEST low_bound
, high_bound
, index
;
2369 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2372 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
2375 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
2376 for (tem
= nargs
; --nargs
>= 0;)
2378 struct value
*element
;
2380 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2382 if (value_type (element
) != element_type
)
2383 element
= value_cast (element_type
, element
);
2384 if (index
> high_bound
)
2385 /* To avoid memory corruption. */
2386 error (_("Too many array elements"));
2387 memcpy (value_contents_raw (array
)
2388 + (index
- low_bound
) * element_size
,
2389 value_contents (element
),
2396 if (expect_type
!= nullptr
2397 && type
->code () == TYPE_CODE_SET
)
2399 struct value
*set
= allocate_value (expect_type
);
2400 gdb_byte
*valaddr
= value_contents_raw (set
);
2401 struct type
*element_type
= type
->index_type ();
2402 struct type
*check_type
= element_type
;
2403 LONGEST low_bound
, high_bound
;
2405 /* Get targettype of elementtype. */
2406 while (check_type
->code () == TYPE_CODE_RANGE
2407 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2408 check_type
= TYPE_TARGET_TYPE (check_type
);
2410 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2411 error (_("(power)set type with unknown size"));
2412 memset (valaddr
, '\0', TYPE_LENGTH (type
));
2414 for (tem
= 0; tem
< nargs
; tem
++)
2416 LONGEST range_low
, range_high
;
2417 struct type
*range_low_type
, *range_high_type
;
2418 struct value
*elem_val
;
2420 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2421 range_low_type
= range_high_type
= value_type (elem_val
);
2422 range_low
= range_high
= value_as_long (elem_val
);
2424 /* Check types of elements to avoid mixture of elements from
2425 different types. Also check if type of element is "compatible"
2426 with element type of powerset. */
2427 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2428 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
2429 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2430 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
2431 if ((range_low_type
->code () != range_high_type
->code ())
2432 || (range_low_type
->code () == TYPE_CODE_ENUM
2433 && (range_low_type
!= range_high_type
)))
2434 /* different element modes. */
2435 error (_("POWERSET tuple elements of different mode"));
2436 if ((check_type
->code () != range_low_type
->code ())
2437 || (check_type
->code () == TYPE_CODE_ENUM
2438 && range_low_type
!= check_type
))
2439 error (_("incompatible POWERSET tuple elements"));
2440 if (range_low
> range_high
)
2442 warning (_("empty POWERSET tuple range"));
2445 if (range_low
< low_bound
|| range_high
> high_bound
)
2446 error (_("POWERSET tuple element out of range"));
2447 range_low
-= low_bound
;
2448 range_high
-= low_bound
;
2449 for (; range_low
<= range_high
; range_low
++)
2451 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2453 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2454 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2455 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2462 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2463 for (tem
= 0; tem
< nargs
; tem
++)
2465 /* Ensure that array expressions are coerced into pointer
2467 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2469 return value_array (tem2
, tem3
, argvec
);
2475 /* Helper for evaluate_subexp_for_address. */
2478 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2481 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2483 struct type
*type
= check_typedef (value_type (x
));
2485 if (TYPE_IS_REFERENCE (type
))
2486 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2488 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2489 return value_zero (lookup_pointer_type (value_type (x
)),
2492 error (_("Attempt to take address of "
2493 "value not located in memory."));
2495 return value_addr (x
);
2502 operation::evaluate_for_cast (struct type
*expect_type
,
2503 struct expression
*exp
,
2506 value
*val
= evaluate (expect_type
, exp
, noside
);
2507 return value_cast (expect_type
, val
);
2511 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2513 value
*val
= evaluate (nullptr, exp
, noside
);
2514 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2518 scope_operation::evaluate_for_address (struct expression
*exp
,
2521 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2522 std::get
<1> (m_storage
).c_str (),
2525 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2530 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2533 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2535 /* We can't optimize out "&*" if there's a user-defined operator*. */
2536 if (unop_user_defined_p (UNOP_IND
, x
))
2538 x
= value_x_unop (x
, UNOP_IND
, noside
);
2539 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2542 return coerce_array (x
);
2546 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2549 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2550 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2551 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2553 struct type
*type
= lookup_pointer_type (value_type (val
));
2554 return value_zero (type
, not_lval
);
2557 return value_addr (val
);
2561 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2564 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2565 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2569 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2572 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2573 EVAL_AVOID_SIDE_EFFECTS
);
2574 struct type
*type
= value_type (typeval
);
2575 return value_cast (lookup_pointer_type (type
),
2576 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2580 var_value_operation::evaluate_for_address (struct expression
*exp
,
2583 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2585 /* C++: The "address" of a reference should yield the address
2586 * of the object pointed to. Let value_addr() deal with it. */
2587 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2588 return operation::evaluate_for_address (exp
, noside
);
2590 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2592 struct type
*type
= lookup_pointer_type (SYMBOL_TYPE (var
));
2593 enum address_class sym_class
= SYMBOL_CLASS (var
);
2595 if (sym_class
== LOC_CONST
2596 || sym_class
== LOC_CONST_BYTES
2597 || sym_class
== LOC_REGISTER
)
2598 error (_("Attempt to take address of register or constant."));
2600 return value_zero (type
, not_lval
);
2603 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2607 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2610 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2611 struct type
*type
= check_typedef (SYMBOL_TYPE (var
));
2612 if (type
->code () == TYPE_CODE_ARRAY
2613 && !type
->is_vector ()
2614 && CAST_IS_CONVERSION (exp
->language_defn
))
2616 struct value
*val
= address_of_variable (var
,
2617 std::get
<0> (m_storage
).block
);
2618 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)), val
);
2620 return evaluate (nullptr, exp
, noside
);
2625 /* Helper function for evaluating the size of a type. */
2628 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2630 /* FIXME: This should be size_t. */
2631 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2632 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2633 "When applied to a reference or a reference type, the result is
2634 the size of the referenced type." */
2635 type
= check_typedef (type
);
2636 if (exp
->language_defn
->la_language
== language_cplus
2637 && (TYPE_IS_REFERENCE (type
)))
2638 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2639 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2646 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2648 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2649 return evaluate_subexp_for_sizeof_base (exp
, value_type (val
));
2653 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2657 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2658 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2660 struct type
*type
= value_type (mval
);
2661 if (type
->code () == TYPE_CODE_ERROR
)
2662 error_unknown_type (b
.minsym
->print_name ());
2664 /* FIXME: This should be size_t. */
2665 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2666 return value_from_longest (size_type
, TYPE_LENGTH (type
));
2670 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2673 if (noside
== EVAL_NORMAL
)
2675 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2676 EVAL_AVOID_SIDE_EFFECTS
);
2677 struct type
*type
= check_typedef (value_type (val
));
2678 if (type
->code () == TYPE_CODE_ARRAY
)
2680 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2681 if (type
->code () == TYPE_CODE_ARRAY
)
2683 type
= type
->index_type ();
2684 /* Only re-evaluate the right hand side if the resulting type
2685 is a variable length type. */
2686 if (type
->bounds ()->flag_bound_evaluated
)
2688 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2689 /* FIXME: This should be size_t. */
2690 struct type
*size_type
2691 = builtin_type (exp
->gdbarch
)->builtin_int
;
2692 return value_from_longest
2693 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
2699 return operation::evaluate_for_sizeof (exp
, noside
);
2703 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2706 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2707 EVAL_AVOID_SIDE_EFFECTS
);
2708 struct type
*type
= check_typedef (value_type (val
));
2709 if (type
->code () != TYPE_CODE_PTR
2710 && !TYPE_IS_REFERENCE (type
)
2711 && type
->code () != TYPE_CODE_ARRAY
)
2712 error (_("Attempt to take contents of a non-pointer value."));
2713 type
= TYPE_TARGET_TYPE (type
);
2714 if (is_dynamic_type (type
))
2715 type
= value_type (value_ind (val
));
2716 /* FIXME: This should be size_t. */
2717 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2718 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
2722 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2725 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2729 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2732 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2733 EVAL_AVOID_SIDE_EFFECTS
);
2734 return evaluate_subexp_for_sizeof_base (exp
, value_type (typeval
));
2738 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2741 struct type
*type
= SYMBOL_TYPE (std::get
<0> (m_storage
).symbol
);
2742 if (is_dynamic_type (type
))
2744 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2745 type
= value_type (val
);
2746 if (type
->code () == TYPE_CODE_ARRAY
)
2748 /* FIXME: This should be size_t. */
2749 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2750 if (type_not_allocated (type
) || type_not_associated (type
))
2751 return value_zero (size_type
, not_lval
);
2752 else if (is_dynamic_type (type
->index_type ())
2753 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2754 return allocate_optimized_out_value (size_type
);
2757 return evaluate_subexp_for_sizeof_base (exp
, type
);
2761 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2762 struct expression
*exp
,
2765 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2766 return value_zero (to_type
, not_lval
);
2768 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2769 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2771 val
= value_cast (to_type
, val
);
2773 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2774 if (VALUE_LVAL (val
) == lval_memory
)
2776 if (value_lazy (val
))
2777 value_fetch_lazy (val
);
2778 VALUE_LVAL (val
) = not_lval
;
2784 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2785 struct expression
*exp
,
2788 value
*val
= evaluate_var_value (noside
,
2789 std::get
<0> (m_storage
).block
,
2790 std::get
<0> (m_storage
).symbol
);
2792 val
= value_cast (to_type
, val
);
2794 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2795 if (VALUE_LVAL (val
) == lval_memory
)
2797 if (value_lazy (val
))
2798 value_fetch_lazy (val
);
2799 VALUE_LVAL (val
) = not_lval
;
2806 /* Parse a type expression in the string [P..P+LENGTH). */
2809 parse_and_eval_type (const char *p
, int length
)
2811 char *tmp
= (char *) alloca (length
+ 4);
2814 memcpy (tmp
+ 1, p
, length
);
2815 tmp
[length
+ 1] = ')';
2816 tmp
[length
+ 2] = '0';
2817 tmp
[length
+ 3] = '\0';
2818 expression_up expr
= parse_expression (tmp
);
2819 expr::unop_cast_operation
*op
2820 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2822 error (_("Internal error in eval_type."));
2823 return op
->get_type ();