1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2023 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 "gdbsupport/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::uses_objfile (struct objfile
*objfile
) const
95 gdb_assert (objfile
->separate_debug_objfile_backlink
== nullptr);
96 return op
->uses_objfile (objfile
);
99 /* See expression.h. */
102 expression::evaluate (struct type
*expect_type
, enum noside noside
)
104 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
105 if (target_has_execution () && inferior_ptid
!= null_ptid
106 && language_defn
->la_language
== language_cplus
107 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
108 stack_temporaries
.emplace (inferior_thread ());
110 struct value
*retval
= op
->evaluate (expect_type
, this, noside
);
112 if (stack_temporaries
.has_value ()
113 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
114 retval
= value_non_lval (retval
);
122 evaluate_expression (struct expression
*exp
, struct type
*expect_type
)
124 return exp
->evaluate (expect_type
, EVAL_NORMAL
);
127 /* Evaluate an expression, avoiding all memory references
128 and getting a value whose type alone is correct. */
131 evaluate_type (struct expression
*exp
)
133 return exp
->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS
);
136 /* Find the current value of a watchpoint on EXP. Return the value in
137 *VALP and *RESULTP and the chain of intermediate and final values
138 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
141 If PRESERVE_ERRORS is true, then exceptions are passed through.
142 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
143 occurs while evaluating the expression, *RESULTP will be set to
144 NULL. *RESULTP may be a lazy value, if the result could not be
145 read from memory. It is used to determine whether a value is
146 user-specified (we should watch the whole value) or intermediate
147 (we should watch only the bit used to locate the final value).
149 If the final value, or any intermediate value, could not be read
150 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
151 set to any referenced values. *VALP will never be a lazy value.
152 This is the value which we store in struct breakpoint.
154 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
155 released from the value chain. If VAL_CHAIN is NULL, all generated
156 values will be left on the value chain. */
159 fetch_subexp_value (struct expression
*exp
,
161 struct value
**valp
, struct value
**resultp
,
162 std::vector
<value_ref_ptr
> *val_chain
,
163 bool preserve_errors
)
165 struct value
*mark
, *new_mark
, *result
;
173 /* Evaluate the expression. */
174 mark
= value_mark ();
179 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
181 catch (const gdb_exception
&ex
)
183 /* Ignore memory errors if we want watchpoints pointing at
184 inaccessible memory to still be created; otherwise, throw the
185 error to some higher catcher. */
189 if (!preserve_errors
)
198 new_mark
= value_mark ();
199 if (mark
== new_mark
)
204 /* Make sure it's not lazy, so that after the target stops again we
205 have a non-lazy previous value to compare with. */
208 if (!result
->lazy ())
215 value_fetch_lazy (result
);
218 catch (const gdb_exception_error
&except
)
226 /* Return the chain of intermediate values. We use this to
227 decide which addresses to watch. */
228 *val_chain
= value_release_to_mark (mark
);
232 /* Promote value ARG1 as appropriate before performing a unary operation
234 If the result is not appropriate for any particular language then it
235 needs to patch this function. */
238 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
243 *arg1
= coerce_ref (*arg1
);
244 type1
= check_typedef ((*arg1
)->type ());
246 if (is_integral_type (type1
))
248 switch (language
->la_language
)
251 /* Perform integral promotion for ANSI C/C++.
252 If not appropriate for any particular language
253 it needs to modify this function. */
255 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
257 if (type1
->length () < builtin_int
->length ())
258 *arg1
= value_cast (builtin_int
, *arg1
);
265 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
266 operation on those two operands.
267 If the result is not appropriate for any particular language then it
268 needs to patch this function. */
271 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
272 struct value
**arg1
, struct value
**arg2
)
274 struct type
*promoted_type
= NULL
;
278 *arg1
= coerce_ref (*arg1
);
279 *arg2
= coerce_ref (*arg2
);
281 type1
= check_typedef ((*arg1
)->type ());
282 type2
= check_typedef ((*arg2
)->type ());
284 if ((type1
->code () != TYPE_CODE_FLT
285 && type1
->code () != TYPE_CODE_DECFLOAT
286 && !is_integral_type (type1
))
287 || (type2
->code () != TYPE_CODE_FLT
288 && type2
->code () != TYPE_CODE_DECFLOAT
289 && !is_integral_type (type2
)))
292 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
295 if (type1
->code () == TYPE_CODE_DECFLOAT
296 || type2
->code () == TYPE_CODE_DECFLOAT
)
298 /* No promotion required. */
300 else if (type1
->code () == TYPE_CODE_FLT
301 || type2
->code () == TYPE_CODE_FLT
)
303 switch (language
->la_language
)
309 case language_opencl
:
310 /* No promotion required. */
314 /* For other languages the result type is unchanged from gdb
315 version 6.7 for backward compatibility.
316 If either arg was long double, make sure that value is also long
317 double. Otherwise use double. */
318 if (type1
->length () * 8 > gdbarch_double_bit (gdbarch
)
319 || type2
->length () * 8 > gdbarch_double_bit (gdbarch
))
320 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
322 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
326 else if (type1
->code () == TYPE_CODE_BOOL
327 && type2
->code () == TYPE_CODE_BOOL
)
329 /* No promotion required. */
332 /* Integral operations here. */
333 /* FIXME: Also mixed integral/booleans, with result an integer. */
335 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
336 unsigned int promoted_len1
= type1
->length ();
337 unsigned int promoted_len2
= type2
->length ();
338 int is_unsigned1
= type1
->is_unsigned ();
339 int is_unsigned2
= type2
->is_unsigned ();
340 unsigned int result_len
;
341 int unsigned_operation
;
343 /* Determine type length and signedness after promotion for
345 if (promoted_len1
< builtin
->builtin_int
->length ())
348 promoted_len1
= builtin
->builtin_int
->length ();
350 if (promoted_len2
< builtin
->builtin_int
->length ())
353 promoted_len2
= builtin
->builtin_int
->length ();
356 if (promoted_len1
> promoted_len2
)
358 unsigned_operation
= is_unsigned1
;
359 result_len
= promoted_len1
;
361 else if (promoted_len2
> promoted_len1
)
363 unsigned_operation
= is_unsigned2
;
364 result_len
= promoted_len2
;
368 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
369 result_len
= promoted_len1
;
372 switch (language
->la_language
)
378 if (result_len
<= builtin
->builtin_int
->length ())
380 promoted_type
= (unsigned_operation
381 ? builtin
->builtin_unsigned_int
382 : builtin
->builtin_int
);
384 else if (result_len
<= builtin
->builtin_long
->length ())
386 promoted_type
= (unsigned_operation
387 ? builtin
->builtin_unsigned_long
388 : builtin
->builtin_long
);
392 promoted_type
= (unsigned_operation
393 ? builtin
->builtin_unsigned_long_long
394 : builtin
->builtin_long_long
);
397 case language_opencl
:
399 <= lookup_signed_typename (language
, "int")->length())
403 ? lookup_unsigned_typename (language
, "int")
404 : lookup_signed_typename (language
, "int"));
407 <= lookup_signed_typename (language
, "long")->length())
411 ? lookup_unsigned_typename (language
, "long")
412 : lookup_signed_typename (language
,"long"));
416 /* For other languages the result type is unchanged from gdb
417 version 6.7 for backward compatibility.
418 If either arg was long long, make sure that value is also long
419 long. Otherwise use long. */
420 if (unsigned_operation
)
422 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
423 promoted_type
= builtin
->builtin_unsigned_long_long
;
425 promoted_type
= builtin
->builtin_unsigned_long
;
429 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
430 promoted_type
= builtin
->builtin_long_long
;
432 promoted_type
= builtin
->builtin_long
;
440 /* Promote both operands to common type. */
441 *arg1
= value_cast (promoted_type
, *arg1
);
442 *arg2
= value_cast (promoted_type
, *arg2
);
447 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
449 type
= check_typedef (type
);
450 if (TYPE_IS_REFERENCE (type
))
451 type
= type
->target_type ();
453 switch (type
->code ())
459 case TYPE_CODE_ARRAY
:
460 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
467 /* Represents a fake method with the given parameter types. This is
468 used by the parser to construct a temporary "expected" type for
469 method overload resolution. FLAGS is used as instance flags of the
470 new type, in order to be able to make the new type represent a
471 const/volatile overload. */
476 fake_method (type_instance_flags flags
,
477 int num_types
, struct type
**param_types
);
480 /* The constructed type. */
481 struct type
*type () { return &m_type
; }
484 struct type m_type
{};
485 main_type m_main_type
{};
488 fake_method::fake_method (type_instance_flags flags
,
489 int num_types
, struct type
**param_types
)
491 struct type
*type
= &m_type
;
493 TYPE_MAIN_TYPE (type
) = &m_main_type
;
494 type
->set_length (1);
495 type
->set_code (TYPE_CODE_METHOD
);
496 TYPE_CHAIN (type
) = type
;
497 type
->set_instance_flags (flags
);
500 if (param_types
[num_types
- 1] == NULL
)
503 type
->set_has_varargs (true);
505 else if (check_typedef (param_types
[num_types
- 1])->code ()
509 /* Caller should have ensured this. */
510 gdb_assert (num_types
== 0);
511 type
->set_is_prototyped (true);
515 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
516 neither an objfile nor a gdbarch. As a result we must manually
517 allocate memory for auxiliary fields, and free the memory ourselves
518 when we are done with it. */
519 type
->set_num_fields (num_types
);
521 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
523 while (num_types
-- > 0)
524 type
->field (num_types
).set_type (param_types
[num_types
]);
527 fake_method::~fake_method ()
529 xfree (m_type
.fields ());
536 type_instance_operation::evaluate (struct type
*expect_type
,
537 struct expression
*exp
,
540 type_instance_flags flags
= std::get
<0> (m_storage
);
541 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
543 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
544 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
550 /* Helper for evaluating an OP_VAR_VALUE. */
553 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
555 /* JYG: We used to just return value_zero of the symbol type if
556 we're asked to avoid side effects. Otherwise we return
557 value_of_variable (...). However I'm not sure if
558 value_of_variable () has any side effect. We need a full value
559 object returned here for whatis_exp () to call evaluate_type ()
560 and then pass the full value to value_rtti_target_type () if we
561 are dealing with a pointer or reference to a base class and print
564 struct value
*ret
= NULL
;
568 ret
= value_of_variable (var
, blk
);
571 catch (const gdb_exception_error
&except
)
573 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
576 ret
= value_zero (var
->type (), not_lval
);
587 var_value_operation::evaluate (struct type
*expect_type
,
588 struct expression
*exp
,
591 symbol
*var
= std::get
<0> (m_storage
).symbol
;
592 if (var
->type ()->code () == TYPE_CODE_ERROR
)
593 error_unknown_type (var
->print_name ());
594 return evaluate_var_value (noside
, std::get
<0> (m_storage
).block
, var
);
597 } /* namespace expr */
599 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
602 evaluate_var_msym_value (enum noside noside
,
603 struct objfile
*objfile
, minimal_symbol
*msymbol
)
606 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
608 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
609 return value_zero (the_type
, not_lval
);
611 return value_at_lazy (the_type
, address
);
614 /* See expression.h. */
617 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
619 gdb::array_view
<value
*> argvec
,
620 const char *function_name
,
621 type
*default_return_type
)
624 error (_("Cannot evaluate function -- may be inlined"));
625 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
627 /* If the return type doesn't look like a function type,
628 call an error. This can happen if somebody tries to turn
629 a variable into a function call. */
631 type
*ftype
= callee
->type ();
633 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
635 /* We don't know anything about what the internal
636 function might return, but we have to return
638 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
641 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
643 type
*return_type
= result_type_of_xmethod (callee
, argvec
);
645 if (return_type
== NULL
)
646 error (_("Xmethod is missing return type."));
647 return value_zero (return_type
, not_lval
);
649 else if (ftype
->code () == TYPE_CODE_FUNC
650 || ftype
->code () == TYPE_CODE_METHOD
)
652 if (ftype
->is_gnu_ifunc ())
654 CORE_ADDR address
= value_address (callee
);
655 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
657 if (resolved_type
!= NULL
)
658 ftype
= resolved_type
;
661 type
*return_type
= ftype
->target_type ();
663 if (return_type
== NULL
)
664 return_type
= default_return_type
;
666 if (return_type
== NULL
)
667 error_call_unknown_return_type (function_name
);
669 return allocate_value (return_type
);
672 error (_("Expression of type other than "
673 "\"Function returning ...\" used as function"));
675 switch (callee
->type ()->code ())
677 case TYPE_CODE_INTERNAL_FUNCTION
:
678 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
679 callee
, argvec
.size (), argvec
.data ());
680 case TYPE_CODE_XMETHOD
:
681 return call_xmethod (callee
, argvec
);
683 return call_function_by_hand (callee
, default_return_type
, argvec
);
691 operation::evaluate_funcall (struct type
*expect_type
,
692 struct expression
*exp
,
694 const char *function_name
,
695 const std::vector
<operation_up
> &args
)
697 std::vector
<value
*> vals (args
.size ());
699 value
*callee
= evaluate_with_coercion (exp
, noside
);
700 struct type
*type
= callee
->type ();
701 if (type
->code () == TYPE_CODE_PTR
)
702 type
= type
->target_type ();
703 for (int i
= 0; i
< args
.size (); ++i
)
705 if (i
< type
->num_fields ())
706 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
708 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
711 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
712 function_name
, expect_type
);
716 var_value_operation::evaluate_funcall (struct type
*expect_type
,
717 struct expression
*exp
,
719 const std::vector
<operation_up
> &args
)
721 if (!overload_resolution
722 || exp
->language_defn
->la_language
!= language_cplus
)
723 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
725 std::vector
<value
*> argvec (args
.size ());
726 for (int i
= 0; i
< args
.size (); ++i
)
727 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
730 find_overload_match (argvec
, NULL
, NON_METHOD
,
731 NULL
, std::get
<0> (m_storage
).symbol
,
732 NULL
, &symp
, NULL
, 0, noside
);
734 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
735 error_unknown_type (symp
->print_name ());
736 value
*callee
= evaluate_var_value (noside
, std::get
<0> (m_storage
).block
,
739 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
740 nullptr, expect_type
);
744 scope_operation::evaluate_funcall (struct type
*expect_type
,
745 struct expression
*exp
,
747 const std::vector
<operation_up
> &args
)
749 if (!overload_resolution
750 || exp
->language_defn
->la_language
!= language_cplus
)
751 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
753 /* Unpack it locally so we can properly handle overload
755 const std::string
&name
= std::get
<1> (m_storage
);
756 struct type
*type
= std::get
<0> (m_storage
);
758 symbol
*function
= NULL
;
759 const char *function_name
= NULL
;
760 std::vector
<value
*> argvec (1 + args
.size ());
761 if (type
->code () == TYPE_CODE_NAMESPACE
)
763 function
= cp_lookup_symbol_namespace (type
->name (),
765 get_selected_block (0),
767 if (function
== NULL
)
768 error (_("No symbol \"%s\" in namespace \"%s\"."),
769 name
.c_str (), type
->name ());
773 gdb_assert (type
->code () == TYPE_CODE_STRUCT
774 || type
->code () == TYPE_CODE_UNION
);
775 function_name
= name
.c_str ();
777 /* We need a properly typed value for method lookup. */
778 argvec
[0] = value_zero (type
, lval_memory
);
781 for (int i
= 0; i
< args
.size (); ++i
)
782 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
783 gdb::array_view
<value
*> arg_view
= argvec
;
785 value
*callee
= nullptr;
786 if (function_name
!= nullptr)
790 find_overload_match (arg_view
, function_name
, METHOD
,
791 &argvec
[0], nullptr, &callee
, nullptr,
792 &static_memfuncp
, 0, noside
);
793 if (!static_memfuncp
)
795 /* For the time being, we don't handle this. */
796 error (_("Call to overloaded function %s requires "
801 arg_view
= arg_view
.slice (1);
806 arg_view
= arg_view
.slice (1);
807 find_overload_match (arg_view
, nullptr,
808 NON_METHOD
, nullptr, function
,
809 nullptr, &symp
, nullptr, 1, noside
);
810 callee
= value_of_variable (symp
, get_selected_block (0));
813 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
814 nullptr, expect_type
);
818 structop_member_base::evaluate_funcall (struct type
*expect_type
,
819 struct expression
*exp
,
821 const std::vector
<operation_up
> &args
)
823 /* First, evaluate the structure into lhs. */
825 if (opcode () == STRUCTOP_MEMBER
)
826 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
828 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
830 std::vector
<value
*> vals (args
.size () + 1);
831 gdb::array_view
<value
*> val_view
= vals
;
832 /* If the function is a virtual function, then the aggregate
833 value (providing the structure) plays its part by providing
834 the vtable. Otherwise, it is just along for the ride: call
835 the function directly. */
836 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
839 type
*a1_type
= check_typedef (rhs
->type ());
840 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
842 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
843 callee
= value_zero (a1_type
->target_type (), not_lval
);
845 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
849 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
851 struct type
*type_ptr
852 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
853 struct type
*target_type_ptr
854 = lookup_pointer_type (a1_type
->target_type ());
856 /* Now, convert this value to an address. */
857 lhs
= value_cast (type_ptr
, lhs
);
859 long mem_offset
= value_as_long (rhs
);
861 callee
= value_from_pointer (target_type_ptr
,
862 value_as_long (lhs
) + mem_offset
);
863 callee
= value_ind (callee
);
865 val_view
= val_view
.slice (1);
868 error (_("Non-pointer-to-member value used in pointer-to-member "
871 for (int i
= 0; i
< args
.size (); ++i
)
872 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
874 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
875 nullptr, expect_type
);
880 structop_base_operation::evaluate_funcall
881 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
882 const std::vector
<operation_up
> &args
)
884 /* Allocate space for the function call arguments, Including space for a
885 `this' pointer at the start. */
886 std::vector
<value
*> vals (args
.size () + 1);
887 /* First, evaluate the structure into vals[0]. */
888 enum exp_opcode op
= opcode ();
889 if (op
== STRUCTOP_STRUCT
)
891 /* If v is a variable in a register, and the user types
892 v.method (), this will produce an error, because v has no
895 A possible way around this would be to allocate a copy of
896 the variable on the stack, copy in the contents, call the
897 function, and copy out the contents. I.e. convert this
898 from call by reference to call by copy-return (or
899 whatever it's called). However, this does not work
900 because it is not the same: the method being called could
901 stash a copy of the address, and then future uses through
902 that address (after the method returns) would be expected
903 to use the variable itself, not some copy of it. */
904 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
908 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
909 /* Check to see if the operator '->' has been overloaded.
910 If the operator has been overloaded replace vals[0] with the
911 value returned by the custom operator and continue
913 while (unop_user_defined_p (op
, vals
[0]))
915 struct value
*value
= nullptr;
918 value
= value_x_unop (vals
[0], op
, noside
);
920 catch (const gdb_exception_error
&except
)
922 if (except
.error
== NOT_FOUND_ERROR
)
932 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
933 pointer we placed into vals[0]. */
934 for (int i
= 0; i
< args
.size (); ++i
)
935 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
937 /* The array view includes the `this' pointer. */
938 gdb::array_view
<value
*> arg_view (vals
);
942 const char *tstr
= std::get
<1> (m_storage
).c_str ();
943 if (overload_resolution
944 && exp
->language_defn
->la_language
== language_cplus
)
946 /* Language is C++, do some overload resolution before
948 value
*val0
= vals
[0];
949 find_overload_match (arg_view
, tstr
, METHOD
,
950 &val0
, nullptr, &callee
, nullptr,
951 &static_memfuncp
, 0, noside
);
955 /* Non-C++ case -- or no overload resolution. */
957 struct value
*temp
= vals
[0];
959 callee
= value_struct_elt (&temp
, arg_view
, tstr
,
961 op
== STRUCTOP_STRUCT
962 ? "structure" : "structure pointer");
963 /* value_struct_elt updates temp with the correct value of the
964 ``this'' pointer if necessary, so modify it to reflect any
966 vals
[0] = value_from_longest (lookup_pointer_type (temp
->type ()),
968 + temp
->embedded_offset ());
971 /* Take out `this' if needed. */
973 arg_view
= arg_view
.slice (1);
975 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
976 nullptr, expect_type
);
979 /* Helper for structop_base_operation::complete which recursively adds
980 field and method names from TYPE, a struct or union type, to the
981 OUTPUT list. PREFIX is prepended to each result. */
984 add_struct_fields (struct type
*type
, completion_list
&output
,
985 const char *fieldname
, int namelen
, const char *prefix
)
988 int computed_type_name
= 0;
989 const char *type_name
= NULL
;
991 type
= check_typedef (type
);
992 for (i
= 0; i
< type
->num_fields (); ++i
)
994 if (i
< TYPE_N_BASECLASSES (type
))
995 add_struct_fields (TYPE_BASECLASS (type
, i
),
996 output
, fieldname
, namelen
, prefix
);
997 else if (type
->field (i
).name ())
999 if (type
->field (i
).name ()[0] != '\0')
1001 if (! strncmp (type
->field (i
).name (),
1002 fieldname
, namelen
))
1003 output
.emplace_back (concat (prefix
, type
->field (i
).name (),
1006 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
)
1008 /* Recurse into anonymous unions. */
1009 add_struct_fields (type
->field (i
).type (),
1010 output
, fieldname
, namelen
, prefix
);
1015 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1017 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1019 if (name
&& ! strncmp (name
, fieldname
, namelen
))
1021 if (!computed_type_name
)
1023 type_name
= type
->name ();
1024 computed_type_name
= 1;
1026 /* Omit constructors from the completion list. */
1027 if (!type_name
|| strcmp (type_name
, name
))
1028 output
.emplace_back (concat (prefix
, name
, nullptr));
1036 structop_base_operation::complete (struct expression
*exp
,
1037 completion_tracker
&tracker
,
1040 const std::string
&fieldname
= std::get
<1> (m_storage
);
1042 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1043 EVAL_AVOID_SIDE_EFFECTS
);
1044 struct type
*type
= lhs
->type ();
1047 type
= check_typedef (type
);
1048 if (!type
->is_pointer_or_reference ())
1050 type
= type
->target_type ();
1053 if (type
->code () == TYPE_CODE_UNION
1054 || type
->code () == TYPE_CODE_STRUCT
)
1056 completion_list result
;
1058 add_struct_fields (type
, result
, fieldname
.c_str (),
1059 fieldname
.length (), prefix
);
1060 tracker
.add_completions (std::move (result
));
1067 } /* namespace expr */
1069 /* Return true if type is integral or reference to integral */
1072 is_integral_or_integral_reference (struct type
*type
)
1074 if (is_integral_type (type
))
1077 type
= check_typedef (type
);
1078 return (type
!= nullptr
1079 && TYPE_IS_REFERENCE (type
)
1080 && is_integral_type (type
->target_type ()));
1083 /* Helper function that implements the body of OP_SCOPE. */
1086 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
1088 struct type
*type
, const char *string
)
1090 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
1093 error (_("There is no field named %s"), string
);
1097 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1100 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1101 enum noside noside
, symbol
*sym
)
1103 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1104 return value_zero (sym
->type (), not_lval
);
1106 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1107 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1108 error (_("Symbol \"%s\" does not have any specific entry value"),
1109 sym
->print_name ());
1111 frame_info_ptr frame
= get_selected_frame (NULL
);
1112 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1115 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1118 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1119 enum noside noside
, bool outermost_p
,
1120 bound_minimal_symbol msymbol
)
1122 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1125 struct type
*type
= val
->type ();
1126 if (type
->code () == TYPE_CODE_ERROR
1127 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1128 error_unknown_type (msymbol
.minsym
->print_name ());
1132 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1135 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1137 value
*func
, const char *var
)
1139 CORE_ADDR addr
= value_address (func
);
1140 const block
*blk
= block_for_pc (addr
);
1141 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1142 if (sym
.symbol
== NULL
)
1143 error (_("No symbol \"%s\" in specified context."), var
);
1144 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1147 /* Helper function that implements the body of OP_REGISTER. */
1150 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1151 enum noside noside
, const char *name
)
1156 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1157 name
, strlen (name
));
1159 error (_("Register $%s not available."), name
);
1161 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1162 a value with the appropriate register type. Unfortunately,
1163 we don't have easy access to the type of user registers.
1164 So for these registers, we fetch the register value regardless
1165 of the evaluation mode. */
1166 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1167 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1168 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1170 val
= value_of_register (regno
, get_selected_frame (NULL
));
1172 error (_("Value of register %s not available."), name
);
1181 string_operation::evaluate (struct type
*expect_type
,
1182 struct expression
*exp
,
1185 const std::string
&str
= std::get
<0> (m_storage
);
1186 struct type
*type
= language_string_char_type (exp
->language_defn
,
1188 return value_string (str
.c_str (), str
.size (), type
);
1191 } /* namespace expr */
1193 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1196 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1200 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1201 return value_from_longest (selector_type
,
1202 lookup_child_selector (exp
->gdbarch
, sel
));
1205 /* A helper function for TERNOP_SLICE. */
1208 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1210 struct value
*array
, struct value
*low
, struct value
*upper
)
1212 int lowbound
= value_as_long (low
);
1213 int upperbound
= value_as_long (upper
);
1214 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1217 /* A helper function for STRUCTOP_STRUCT. */
1220 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1222 struct value
*arg1
, const char *string
)
1224 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1226 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1227 arg3
= value_zero (arg3
->type (), VALUE_LVAL (arg3
));
1231 /* A helper function for STRUCTOP_PTR. */
1234 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1236 struct value
*arg1
, const char *string
)
1238 /* Check to see if operator '->' has been overloaded. If so replace
1239 arg1 with the value returned by evaluating operator->(). */
1240 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1242 struct value
*value
= NULL
;
1245 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1248 catch (const gdb_exception_error
&except
)
1250 if (except
.error
== NOT_FOUND_ERROR
)
1259 /* JYG: if print object is on we need to replace the base type
1260 with rtti type in order to continue on with successful
1261 lookup of member / method only available in the rtti type. */
1263 struct type
*arg_type
= arg1
->type ();
1264 struct type
*real_type
;
1265 int full
, using_enc
;
1267 struct value_print_options opts
;
1269 get_user_print_options (&opts
);
1270 if (opts
.objectprint
&& arg_type
->target_type ()
1271 && (arg_type
->target_type ()->code () == TYPE_CODE_STRUCT
))
1273 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1276 arg1
= value_cast (real_type
, arg1
);
1280 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1281 NULL
, "structure pointer");
1282 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1283 arg3
= value_zero (arg3
->type (), VALUE_LVAL (arg3
));
1287 /* A helper function for STRUCTOP_MEMBER. */
1290 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1292 struct value
*arg1
, struct value
*arg2
)
1297 struct type
*type
= check_typedef (arg2
->type ());
1298 switch (type
->code ())
1300 case TYPE_CODE_METHODPTR
:
1301 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1302 return value_zero (type
->target_type (), not_lval
);
1305 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1306 gdb_assert (arg2
->type ()->code () == TYPE_CODE_PTR
);
1307 return value_ind (arg2
);
1310 case TYPE_CODE_MEMBERPTR
:
1311 /* Now, convert these values to an address. */
1312 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_PTR
)
1313 arg1
= value_addr (arg1
);
1314 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1317 mem_offset
= value_as_long (arg2
);
1319 arg3
= value_from_pointer (lookup_pointer_type (type
->target_type ()),
1320 value_as_long (arg1
) + mem_offset
);
1321 return value_ind (arg3
);
1324 error (_("non-pointer-to-member value used "
1325 "in pointer-to-member construct"));
1329 /* A helper function for BINOP_ADD. */
1332 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1334 struct value
*arg1
, struct value
*arg2
)
1336 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1337 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1338 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1339 && is_integral_or_integral_reference (arg2
->type ()))
1340 return value_ptradd (arg1
, value_as_long (arg2
));
1341 else if (ptrmath_type_p (exp
->language_defn
, arg2
->type ())
1342 && is_integral_or_integral_reference (arg1
->type ()))
1343 return value_ptradd (arg2
, value_as_long (arg1
));
1346 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1347 return value_binop (arg1
, arg2
, BINOP_ADD
);
1351 /* A helper function for BINOP_SUB. */
1354 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1356 struct value
*arg1
, struct value
*arg2
)
1358 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1359 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1360 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1361 && ptrmath_type_p (exp
->language_defn
, arg2
->type ()))
1363 /* FIXME -- should be ptrdiff_t */
1364 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1365 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1367 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1368 && is_integral_or_integral_reference (arg2
->type ()))
1369 return value_ptradd (arg1
, - value_as_long (arg2
));
1372 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1373 return value_binop (arg1
, arg2
, BINOP_SUB
);
1377 /* Helper function for several different binary operations. */
1380 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1381 enum noside noside
, enum exp_opcode op
,
1382 struct value
*arg1
, struct value
*arg2
)
1384 if (binop_user_defined_p (op
, arg1
, arg2
))
1385 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1388 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1389 fudge arg2 to avoid division-by-zero, the caller is
1390 (theoretically) only looking for the type of the result. */
1391 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1392 /* ??? Do we really want to test for BINOP_MOD here?
1393 The implementation of value_binop gives it a well-defined
1396 || op
== BINOP_INTDIV
1399 && value_logical_not (arg2
))
1401 struct value
*v_one
;
1403 v_one
= value_one (arg2
->type ());
1404 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1405 return value_binop (arg1
, v_one
, op
);
1409 /* For shift and integer exponentiation operations,
1410 only promote the first argument. */
1411 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1412 && is_integral_type (arg2
->type ()))
1413 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1415 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1417 return value_binop (arg1
, arg2
, op
);
1422 /* A helper function for BINOP_SUBSCRIPT. */
1425 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1426 enum noside noside
, enum exp_opcode op
,
1427 struct value
*arg1
, struct value
*arg2
)
1429 if (binop_user_defined_p (op
, arg1
, arg2
))
1430 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1433 /* If the user attempts to subscript something that is not an
1434 array or pointer type (like a plain int variable for example),
1435 then report this as an error. */
1437 arg1
= coerce_ref (arg1
);
1438 struct type
*type
= check_typedef (arg1
->type ());
1439 if (type
->code () != TYPE_CODE_ARRAY
1440 && type
->code () != TYPE_CODE_PTR
)
1443 error (_("cannot subscript something of type `%s'"),
1446 error (_("cannot subscript requested type"));
1449 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1450 return value_zero (type
->target_type (), VALUE_LVAL (arg1
));
1452 return value_subscript (arg1
, value_as_long (arg2
));
1456 /* A helper function for BINOP_EQUAL. */
1459 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1460 enum noside noside
, enum exp_opcode op
,
1461 struct value
*arg1
, struct value
*arg2
)
1463 if (binop_user_defined_p (op
, arg1
, arg2
))
1465 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1469 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1470 int tem
= value_equal (arg1
, arg2
);
1471 struct type
*type
= language_bool_type (exp
->language_defn
,
1473 return value_from_longest (type
, (LONGEST
) tem
);
1477 /* A helper function for BINOP_NOTEQUAL. */
1480 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1481 enum noside noside
, enum exp_opcode op
,
1482 struct value
*arg1
, struct value
*arg2
)
1484 if (binop_user_defined_p (op
, arg1
, arg2
))
1486 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1490 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1491 int tem
= value_equal (arg1
, arg2
);
1492 struct type
*type
= language_bool_type (exp
->language_defn
,
1494 return value_from_longest (type
, (LONGEST
) ! tem
);
1498 /* A helper function for BINOP_LESS. */
1501 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1502 enum noside noside
, enum exp_opcode op
,
1503 struct value
*arg1
, struct value
*arg2
)
1505 if (binop_user_defined_p (op
, arg1
, arg2
))
1507 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1511 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1512 int tem
= value_less (arg1
, arg2
);
1513 struct type
*type
= language_bool_type (exp
->language_defn
,
1515 return value_from_longest (type
, (LONGEST
) tem
);
1519 /* A helper function for BINOP_GTR. */
1522 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1523 enum noside noside
, enum exp_opcode op
,
1524 struct value
*arg1
, struct value
*arg2
)
1526 if (binop_user_defined_p (op
, arg1
, arg2
))
1528 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1532 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1533 int tem
= value_less (arg2
, arg1
);
1534 struct type
*type
= language_bool_type (exp
->language_defn
,
1536 return value_from_longest (type
, (LONGEST
) tem
);
1540 /* A helper function for BINOP_GEQ. */
1543 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1544 enum noside noside
, enum exp_opcode op
,
1545 struct value
*arg1
, struct value
*arg2
)
1547 if (binop_user_defined_p (op
, arg1
, arg2
))
1549 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1553 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1554 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1555 struct type
*type
= language_bool_type (exp
->language_defn
,
1557 return value_from_longest (type
, (LONGEST
) tem
);
1561 /* A helper function for BINOP_LEQ. */
1564 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1565 enum noside noside
, enum exp_opcode op
,
1566 struct value
*arg1
, struct value
*arg2
)
1568 if (binop_user_defined_p (op
, arg1
, arg2
))
1570 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1574 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1575 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1576 struct type
*type
= language_bool_type (exp
->language_defn
,
1578 return value_from_longest (type
, (LONGEST
) tem
);
1582 /* A helper function for BINOP_REPEAT. */
1585 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1586 enum noside noside
, enum exp_opcode op
,
1587 struct value
*arg1
, struct value
*arg2
)
1589 struct type
*type
= check_typedef (arg2
->type ());
1590 if (type
->code () != TYPE_CODE_INT
1591 && type
->code () != TYPE_CODE_ENUM
)
1592 error (_("Non-integral right operand for \"@\" operator."));
1593 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1595 return allocate_repeat_value (arg1
->type (),
1596 longest_to_int (value_as_long (arg2
)));
1599 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1602 /* A helper function for UNOP_PLUS. */
1605 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1606 enum noside noside
, enum exp_opcode op
,
1609 if (unop_user_defined_p (op
, arg1
))
1610 return value_x_unop (arg1
, op
, noside
);
1613 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1614 return value_pos (arg1
);
1618 /* A helper function for UNOP_NEG. */
1621 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1622 enum noside noside
, enum exp_opcode op
,
1625 if (unop_user_defined_p (op
, arg1
))
1626 return value_x_unop (arg1
, op
, noside
);
1629 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1630 return value_neg (arg1
);
1634 /* A helper function for UNOP_COMPLEMENT. */
1637 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1638 enum noside noside
, enum exp_opcode op
,
1641 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1642 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1645 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1646 return value_complement (arg1
);
1650 /* A helper function for UNOP_LOGICAL_NOT. */
1653 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1654 enum noside noside
, enum exp_opcode op
,
1657 if (unop_user_defined_p (op
, arg1
))
1658 return value_x_unop (arg1
, op
, noside
);
1661 struct type
*type
= language_bool_type (exp
->language_defn
,
1663 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1667 /* A helper function for UNOP_IND. */
1670 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1674 struct type
*type
= check_typedef (arg1
->type ());
1675 if (type
->code () == TYPE_CODE_METHODPTR
1676 || type
->code () == TYPE_CODE_MEMBERPTR
)
1677 error (_("Attempt to dereference pointer "
1678 "to member without an object"));
1679 if (unop_user_defined_p (UNOP_IND
, arg1
))
1680 return value_x_unop (arg1
, UNOP_IND
, noside
);
1681 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1683 type
= check_typedef (arg1
->type ());
1685 /* If the type pointed to is dynamic then in order to resolve the
1686 dynamic properties we must actually dereference the pointer.
1687 There is a risk that this dereference will have side-effects
1688 in the inferior, but being able to print accurate type
1689 information seems worth the risk. */
1690 if (!type
->is_pointer_or_reference ()
1691 || !is_dynamic_type (type
->target_type ()))
1693 if (type
->is_pointer_or_reference ()
1694 /* In C you can dereference an array to get the 1st elt. */
1695 || type
->code () == TYPE_CODE_ARRAY
)
1696 return value_zero (type
->target_type (),
1698 else if (type
->code () == TYPE_CODE_INT
)
1699 /* GDB allows dereferencing an int. */
1700 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1703 error (_("Attempt to take contents of a non-pointer value."));
1707 /* Allow * on an integer so we can cast it to whatever we want.
1708 This returns an int, which seems like the most C-like thing to
1709 do. "long long" variables are rare enough that
1710 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1711 if (type
->code () == TYPE_CODE_INT
)
1712 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1713 (CORE_ADDR
) value_as_address (arg1
));
1714 return value_ind (arg1
);
1717 /* A helper function for UNOP_ALIGNOF. */
1720 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1724 struct type
*type
= arg1
->type ();
1725 /* FIXME: This should be size_t. */
1726 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1727 ULONGEST align
= type_align (type
);
1729 error (_("could not determine alignment of type"));
1730 return value_from_longest (size_type
, align
);
1733 /* A helper function for UNOP_MEMVAL. */
1736 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1738 struct value
*arg1
, struct type
*type
)
1740 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1741 return value_zero (type
, lval_memory
);
1743 return value_at_lazy (type
, value_as_address (arg1
));
1746 /* A helper function for UNOP_PREINCREMENT. */
1749 eval_op_preinc (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
);
1762 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1763 arg2
= value_ptradd (arg1
, 1);
1766 struct value
*tmp
= arg1
;
1768 arg2
= value_one (arg1
->type ());
1769 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1770 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1773 return value_assign (arg1
, arg2
);
1777 /* A helper function for UNOP_PREDECREMENT. */
1780 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1781 enum noside noside
, enum exp_opcode op
,
1784 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1786 else if (unop_user_defined_p (op
, arg1
))
1788 return value_x_unop (arg1
, op
, noside
);
1793 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1794 arg2
= value_ptradd (arg1
, -1);
1797 struct value
*tmp
= arg1
;
1799 arg2
= value_one (arg1
->type ());
1800 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1801 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1804 return value_assign (arg1
, arg2
);
1808 /* A helper function for UNOP_POSTINCREMENT. */
1811 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1812 enum noside noside
, enum exp_opcode op
,
1815 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1817 else if (unop_user_defined_p (op
, arg1
))
1819 return value_x_unop (arg1
, op
, noside
);
1823 struct value
*arg3
= value_non_lval (arg1
);
1826 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1827 arg2
= value_ptradd (arg1
, 1);
1830 struct value
*tmp
= arg1
;
1832 arg2
= value_one (arg1
->type ());
1833 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1834 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1837 value_assign (arg1
, arg2
);
1842 /* A helper function for UNOP_POSTDECREMENT. */
1845 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1846 enum noside noside
, enum exp_opcode op
,
1849 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1851 else if (unop_user_defined_p (op
, arg1
))
1853 return value_x_unop (arg1
, op
, noside
);
1857 struct value
*arg3
= value_non_lval (arg1
);
1860 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1861 arg2
= value_ptradd (arg1
, -1);
1864 struct value
*tmp
= arg1
;
1866 arg2
= value_one (arg1
->type ());
1867 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1868 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1871 value_assign (arg1
, arg2
);
1876 /* A helper function for OP_TYPE. */
1879 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1880 enum noside noside
, struct type
*type
)
1882 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1883 return allocate_value (type
);
1885 error (_("Attempt to use a type name as an expression"));
1888 /* A helper function for BINOP_ASSIGN_MODIFY. */
1891 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1892 enum noside noside
, enum exp_opcode op
,
1893 struct value
*arg1
, struct value
*arg2
)
1895 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1897 if (binop_user_defined_p (op
, arg1
, arg2
))
1898 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1899 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1901 && is_integral_type (arg2
->type ()))
1902 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1903 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1905 && is_integral_type (arg2
->type ()))
1906 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1909 struct value
*tmp
= arg1
;
1911 /* For shift and integer exponentiation operations,
1912 only promote the first argument. */
1913 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1914 && is_integral_type (arg2
->type ()))
1915 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1917 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1919 arg2
= value_binop (tmp
, arg2
, op
);
1921 return value_assign (arg1
, arg2
);
1924 /* Note that ARGS needs 2 empty slots up front and must end with a
1926 static struct value
*
1927 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1928 enum noside noside
, CORE_ADDR selector
,
1929 value
*target
, gdb::array_view
<value
*> args
)
1931 CORE_ADDR responds_selector
= 0;
1932 CORE_ADDR method_selector
= 0;
1934 int struct_return
= 0;
1936 struct value
*msg_send
= NULL
;
1937 struct value
*msg_send_stret
= NULL
;
1938 int gnu_runtime
= 0;
1940 struct value
*method
= NULL
;
1941 struct value
*called_method
= NULL
;
1943 struct type
*selector_type
= NULL
;
1944 struct type
*long_type
;
1947 struct value
*ret
= NULL
;
1952 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1953 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1955 if (value_as_long (target
) == 0)
1956 return value_from_longest (long_type
, 0);
1958 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1961 /* Find the method dispatch (Apple runtime) or method lookup
1962 (GNU runtime) function for Objective-C. These will be used
1963 to lookup the symbol information for the method. If we
1964 can't find any symbol information, then we'll use these to
1965 call the method, otherwise we can call the method
1966 directly. The msg_send_stret function is used in the special
1967 case of a method that returns a structure (Apple runtime
1971 type
= selector_type
;
1973 type
= lookup_function_type (type
);
1974 type
= lookup_pointer_type (type
);
1975 type
= lookup_function_type (type
);
1976 type
= lookup_pointer_type (type
);
1978 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1980 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1982 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1983 msg_send_stret
= value_from_pointer (type
,
1984 value_as_address (msg_send_stret
));
1988 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1989 /* Special dispatcher for methods returning structs. */
1991 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1994 /* Verify the target object responds to this method. The
1995 standard top-level 'Object' class uses a different name for
1996 the verification method than the non-standard, but more
1997 often used, 'NSObject' class. Make sure we check for both. */
2000 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
2001 if (responds_selector
== 0)
2003 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
2005 if (responds_selector
== 0)
2006 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
2009 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
2010 if (method_selector
== 0)
2012 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
2014 if (method_selector
== 0)
2015 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2017 /* Call the verification method, to make sure that the target
2018 class implements the desired method. */
2020 argvec
[0] = msg_send
;
2022 argvec
[2] = value_from_longest (long_type
, responds_selector
);
2023 argvec
[3] = value_from_longest (long_type
, selector
);
2026 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2029 /* Function objc_msg_lookup returns a pointer. */
2031 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2033 if (value_as_long (ret
) == 0)
2034 error (_("Target does not respond to this message selector."));
2036 /* Call "methodForSelector:" method, to get the address of a
2037 function method that implements this selector for this
2038 class. If we can find a symbol at that address, then we
2039 know the return type, parameter types etc. (that's a good
2042 argvec
[0] = msg_send
;
2044 argvec
[2] = value_from_longest (long_type
, method_selector
);
2045 argvec
[3] = value_from_longest (long_type
, selector
);
2048 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2052 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2055 /* ret should now be the selector. */
2057 addr
= value_as_long (ret
);
2060 struct symbol
*sym
= NULL
;
2062 /* The address might point to a function descriptor;
2063 resolve it to the actual code address instead. */
2064 addr
= gdbarch_convert_from_func_ptr_addr
2065 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
2067 /* Is it a high_level symbol? */
2068 sym
= find_pc_function (addr
);
2070 method
= value_of_variable (sym
, 0);
2073 /* If we found a method with symbol information, check to see
2074 if it returns a struct. Otherwise assume it doesn't. */
2079 struct type
*val_type
;
2081 funaddr
= find_function_addr (method
, &val_type
);
2083 block_for_pc (funaddr
);
2085 val_type
= check_typedef (val_type
);
2087 if ((val_type
== NULL
)
2088 || (val_type
->code () == TYPE_CODE_ERROR
))
2090 if (expect_type
!= NULL
)
2091 val_type
= expect_type
;
2094 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2097 else if (expect_type
!= NULL
)
2099 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2100 check_typedef (expect_type
));
2103 /* Found a function symbol. Now we will substitute its
2104 value in place of the message dispatcher (obj_msgSend),
2105 so that we call the method directly instead of thru
2106 the dispatcher. The main reason for doing this is that
2107 we can now evaluate the return value and parameter values
2108 according to their known data types, in case we need to
2109 do things like promotion, dereferencing, special handling
2110 of structs and doubles, etc.
2112 We want to use the type signature of 'method', but still
2113 jump to objc_msgSend() or objc_msgSend_stret() to better
2114 mimic the behavior of the runtime. */
2118 if (method
->type ()->code () != TYPE_CODE_FUNC
)
2119 error (_("method address has symbol information "
2120 "with non-function type; skipping"));
2122 /* Create a function pointer of the appropriate type, and
2123 replace its value with the value of msg_send or
2124 msg_send_stret. We must use a pointer here, as
2125 msg_send and msg_send_stret are of pointer type, and
2126 the representation may be different on systems that use
2127 function descriptors. */
2130 = value_from_pointer (lookup_pointer_type (method
->type ()),
2131 value_as_address (msg_send_stret
));
2134 = value_from_pointer (lookup_pointer_type (method
->type ()),
2135 value_as_address (msg_send
));
2140 called_method
= msg_send_stret
;
2142 called_method
= msg_send
;
2146 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2148 /* If the return type doesn't look like a function type,
2149 call an error. This can happen if somebody tries to
2150 turn a variable into a function call. This is here
2151 because people often want to call, eg, strcmp, which
2152 gdb doesn't know is a function. If gdb isn't asked for
2153 it's opinion (ie. through "whatis"), it won't offer
2156 struct type
*callee_type
= called_method
->type ();
2158 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2159 callee_type
= callee_type
->target_type ();
2160 callee_type
= callee_type
->target_type ();
2164 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2165 return allocate_value (expect_type
);
2167 return allocate_value (callee_type
);
2170 error (_("Expression of type other than "
2171 "\"method returning ...\" used as a method"));
2174 /* Now depending on whether we found a symbol for the method,
2175 we will either call the runtime dispatcher or the method
2179 args
[1] = value_from_longest (long_type
, selector
);
2181 if (gnu_runtime
&& (method
!= NULL
))
2183 /* Function objc_msg_lookup returns a pointer. */
2184 struct type
*tem_type
= called_method
->type ();
2185 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2186 called_method
->deprecated_set_type (tem_type
);
2187 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2190 return call_function_by_hand (called_method
, NULL
, args
);
2193 /* Helper function for MULTI_SUBSCRIPT. */
2195 static struct value
*
2196 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2197 enum noside noside
, value
*arg1
,
2198 gdb::array_view
<value
*> args
)
2200 for (value
*arg2
: args
)
2202 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2204 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2208 arg1
= coerce_ref (arg1
);
2209 struct type
*type
= check_typedef (arg1
->type ());
2211 switch (type
->code ())
2214 case TYPE_CODE_ARRAY
:
2215 case TYPE_CODE_STRING
:
2216 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2221 error (_("cannot subscript something of type `%s'"),
2224 error (_("cannot subscript requested type"));
2235 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2236 struct expression
*exp
,
2239 enum noside sub_no_side
= EVAL_NORMAL
;
2240 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2242 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2243 sub_no_side
= EVAL_NORMAL
;
2245 sub_no_side
= noside
;
2247 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2249 if (value_as_long (target
) == 0)
2250 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2252 sub_no_side
= noside
;
2253 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2254 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2255 argvec
[0] = nullptr;
2256 argvec
[1] = nullptr;
2257 for (int i
= 0; i
< args
.size (); ++i
)
2258 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2259 argvec
[args
.size () + 2] = nullptr;
2261 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2262 get
<0> (m_storage
), target
,
2263 gdb::make_array_view (argvec
,
2268 multi_subscript_operation::evaluate (struct type
*expect_type
,
2269 struct expression
*exp
,
2272 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2273 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2274 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2275 for (int ix
= 0; ix
< values
.size (); ++ix
)
2276 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2277 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2278 gdb::make_array_view (argvec
, values
.size ()));
2282 logical_and_operation::evaluate (struct type
*expect_type
,
2283 struct expression
*exp
,
2286 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2288 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2289 EVAL_AVOID_SIDE_EFFECTS
);
2291 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2293 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2294 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2298 bool tem
= value_logical_not (arg1
);
2301 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2302 tem
= value_logical_not (arg2
);
2304 struct type
*type
= language_bool_type (exp
->language_defn
,
2306 return value_from_longest (type
, !tem
);
2311 logical_or_operation::evaluate (struct type
*expect_type
,
2312 struct expression
*exp
,
2315 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2317 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2318 EVAL_AVOID_SIDE_EFFECTS
);
2320 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2322 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2323 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2327 bool tem
= value_logical_not (arg1
);
2330 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2331 tem
= value_logical_not (arg2
);
2334 struct type
*type
= language_bool_type (exp
->language_defn
,
2336 return value_from_longest (type
, !tem
);
2341 adl_func_operation::evaluate (struct type
*expect_type
,
2342 struct expression
*exp
,
2345 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2346 std::vector
<value
*> args (arg_ops
.size ());
2347 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2348 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2350 struct symbol
*symp
;
2351 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2354 nullptr, &symp
, nullptr, 0, noside
);
2355 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
2356 error_unknown_type (symp
->print_name ());
2357 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2358 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2359 nullptr, expect_type
);
2363 /* This function evaluates brace-initializers (in C/C++) for
2367 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2368 struct expression
*exp
,
2369 enum noside noside
, int nargs
)
2371 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2372 struct type
*struct_type
= check_typedef (struct_val
->type ());
2373 struct type
*field_type
;
2377 while (--nargs
>= 0)
2379 struct value
*val
= NULL
;
2380 int bitpos
, bitsize
;
2384 /* Skip static fields. */
2385 while (fieldno
< struct_type
->num_fields ()
2386 && field_is_static (&struct_type
->field (fieldno
)))
2388 if (fieldno
>= struct_type
->num_fields ())
2389 error (_("too many initializers"));
2390 field_type
= struct_type
->field (fieldno
).type ();
2391 if (field_type
->code () == TYPE_CODE_UNION
2392 && struct_type
->field (fieldno
).name ()[0] == '0')
2393 error (_("don't know which variant you want to set"));
2395 /* Here, struct_type is the type of the inner struct,
2396 while substruct_type is the type of the inner struct.
2397 These are the same for normal structures, but a variant struct
2398 contains anonymous union fields that contain substruct fields.
2399 The value fieldno is the index of the top-level (normal or
2400 anonymous union) field in struct_field, while the value
2401 subfieldno is the index of the actual real (named inner) field
2402 in substruct_type. */
2404 field_type
= struct_type
->field (fieldno
).type ();
2406 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2408 /* Now actually set the field in struct_val. */
2410 /* Assign val to field fieldno. */
2411 if (val
->type () != field_type
)
2412 val
= value_cast (field_type
, val
);
2414 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2415 bitpos
= struct_type
->field (fieldno
).loc_bitpos ();
2416 addr
= value_contents_writeable (struct_val
).data () + bitpos
/ 8;
2418 modify_field (struct_type
, addr
,
2419 value_as_long (val
), bitpos
% 8, bitsize
);
2421 memcpy (addr
, value_contents (val
).data (),
2422 val
->type ()->length ());
2429 array_operation::evaluate (struct type
*expect_type
,
2430 struct expression
*exp
,
2434 int tem2
= std::get
<0> (m_storage
);
2435 int tem3
= std::get
<1> (m_storage
);
2436 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2437 int nargs
= tem3
- tem2
+ 1;
2438 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2440 if (expect_type
!= nullptr
2441 && type
->code () == TYPE_CODE_STRUCT
)
2443 struct value
*rec
= allocate_value (expect_type
);
2445 memset (value_contents_raw (rec
).data (), '\0', type
->length ());
2446 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2449 if (expect_type
!= nullptr
2450 && type
->code () == TYPE_CODE_ARRAY
)
2452 struct type
*range_type
= type
->index_type ();
2453 struct type
*element_type
= type
->target_type ();
2454 struct value
*array
= allocate_value (expect_type
);
2455 int element_size
= check_typedef (element_type
)->length ();
2456 LONGEST low_bound
, high_bound
, index
;
2458 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2461 high_bound
= (type
->length () / element_size
) - 1;
2464 memset (value_contents_raw (array
).data (), 0, expect_type
->length ());
2465 for (tem
= nargs
; --nargs
>= 0;)
2467 struct value
*element
;
2469 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2471 if (element
->type () != element_type
)
2472 element
= value_cast (element_type
, element
);
2473 if (index
> high_bound
)
2474 /* To avoid memory corruption. */
2475 error (_("Too many array elements"));
2476 memcpy (value_contents_raw (array
).data ()
2477 + (index
- low_bound
) * element_size
,
2478 value_contents (element
).data (),
2485 if (expect_type
!= nullptr
2486 && type
->code () == TYPE_CODE_SET
)
2488 struct value
*set
= allocate_value (expect_type
);
2489 gdb_byte
*valaddr
= value_contents_raw (set
).data ();
2490 struct type
*element_type
= type
->index_type ();
2491 struct type
*check_type
= element_type
;
2492 LONGEST low_bound
, high_bound
;
2494 /* Get targettype of elementtype. */
2495 while (check_type
->code () == TYPE_CODE_RANGE
2496 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2497 check_type
= check_type
->target_type ();
2499 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2500 error (_("(power)set type with unknown size"));
2501 memset (valaddr
, '\0', type
->length ());
2503 for (tem
= 0; tem
< nargs
; tem
++)
2505 LONGEST range_low
, range_high
;
2506 struct type
*range_low_type
, *range_high_type
;
2507 struct value
*elem_val
;
2509 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2510 range_low_type
= range_high_type
= elem_val
->type ();
2511 range_low
= range_high
= value_as_long (elem_val
);
2513 /* Check types of elements to avoid mixture of elements from
2514 different types. Also check if type of element is "compatible"
2515 with element type of powerset. */
2516 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2517 range_low_type
= range_low_type
->target_type ();
2518 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2519 range_high_type
= range_high_type
->target_type ();
2520 if ((range_low_type
->code () != range_high_type
->code ())
2521 || (range_low_type
->code () == TYPE_CODE_ENUM
2522 && (range_low_type
!= range_high_type
)))
2523 /* different element modes. */
2524 error (_("POWERSET tuple elements of different mode"));
2525 if ((check_type
->code () != range_low_type
->code ())
2526 || (check_type
->code () == TYPE_CODE_ENUM
2527 && range_low_type
!= check_type
))
2528 error (_("incompatible POWERSET tuple elements"));
2529 if (range_low
> range_high
)
2531 warning (_("empty POWERSET tuple range"));
2534 if (range_low
< low_bound
|| range_high
> high_bound
)
2535 error (_("POWERSET tuple element out of range"));
2536 range_low
-= low_bound
;
2537 range_high
-= low_bound
;
2538 for (; range_low
<= range_high
; range_low
++)
2540 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2542 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2543 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2544 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2551 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2552 for (tem
= 0; tem
< nargs
; tem
++)
2554 /* Ensure that array expressions are coerced into pointer
2556 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2558 return value_array (tem2
, tem3
, argvec
);
2562 unop_extract_operation::evaluate (struct type
*expect_type
,
2563 struct expression
*exp
,
2566 value
*old_value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2567 struct type
*type
= get_type ();
2569 if (type
->length () > old_value
->type ()->length ())
2570 error (_("length type is larger than the value type"));
2572 struct value
*result
= allocate_value (type
);
2573 value_contents_copy (result
, 0, old_value
, 0, type
->length ());
2580 /* Helper for evaluate_subexp_for_address. */
2583 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2586 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2588 struct type
*type
= check_typedef (x
->type ());
2590 if (TYPE_IS_REFERENCE (type
))
2591 return value_zero (lookup_pointer_type (type
->target_type ()),
2593 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2594 return value_zero (lookup_pointer_type (x
->type ()),
2597 error (_("Attempt to take address of "
2598 "value not located in memory."));
2600 return value_addr (x
);
2607 operation::evaluate_for_cast (struct type
*expect_type
,
2608 struct expression
*exp
,
2611 value
*val
= evaluate (expect_type
, exp
, noside
);
2612 return value_cast (expect_type
, val
);
2616 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2618 value
*val
= evaluate (nullptr, exp
, noside
);
2619 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2623 scope_operation::evaluate_for_address (struct expression
*exp
,
2626 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2627 std::get
<1> (m_storage
).c_str (),
2630 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2635 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2638 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2640 /* We can't optimize out "&*" if there's a user-defined operator*. */
2641 if (unop_user_defined_p (UNOP_IND
, x
))
2643 x
= value_x_unop (x
, UNOP_IND
, noside
);
2644 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2647 return coerce_array (x
);
2651 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2654 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2655 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2656 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2658 struct type
*type
= lookup_pointer_type (val
->type ());
2659 return value_zero (type
, not_lval
);
2662 return value_addr (val
);
2666 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2669 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2670 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2674 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2677 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2678 EVAL_AVOID_SIDE_EFFECTS
);
2679 struct type
*type
= typeval
->type ();
2680 return value_cast (lookup_pointer_type (type
),
2681 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2685 var_value_operation::evaluate_for_address (struct expression
*exp
,
2688 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2690 /* C++: The "address" of a reference should yield the address
2691 * of the object pointed to. Let value_addr() deal with it. */
2692 if (TYPE_IS_REFERENCE (var
->type ()))
2693 return operation::evaluate_for_address (exp
, noside
);
2695 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2697 struct type
*type
= lookup_pointer_type (var
->type ());
2698 enum address_class sym_class
= var
->aclass ();
2700 if (sym_class
== LOC_CONST
2701 || sym_class
== LOC_CONST_BYTES
2702 || sym_class
== LOC_REGISTER
)
2703 error (_("Attempt to take address of register or constant."));
2705 return value_zero (type
, not_lval
);
2708 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2712 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2715 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2716 struct type
*type
= check_typedef (var
->type ());
2717 if (type
->code () == TYPE_CODE_ARRAY
2718 && !type
->is_vector ()
2719 && CAST_IS_CONVERSION (exp
->language_defn
))
2721 struct value
*val
= address_of_variable (var
,
2722 std::get
<0> (m_storage
).block
);
2723 return value_cast (lookup_pointer_type (type
->target_type ()), val
);
2725 return evaluate (nullptr, exp
, noside
);
2730 /* Helper function for evaluating the size of a type. */
2733 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2735 /* FIXME: This should be size_t. */
2736 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2737 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2738 "When applied to a reference or a reference type, the result is
2739 the size of the referenced type." */
2740 type
= check_typedef (type
);
2741 if (exp
->language_defn
->la_language
== language_cplus
2742 && (TYPE_IS_REFERENCE (type
)))
2743 type
= check_typedef (type
->target_type ());
2744 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2751 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2753 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2754 return evaluate_subexp_for_sizeof_base (exp
, val
->type ());
2758 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2762 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2763 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2765 struct type
*type
= mval
->type ();
2766 if (type
->code () == TYPE_CODE_ERROR
)
2767 error_unknown_type (b
.minsym
->print_name ());
2769 /* FIXME: This should be size_t. */
2770 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2771 return value_from_longest (size_type
, type
->length ());
2775 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2778 if (noside
== EVAL_NORMAL
)
2780 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2781 EVAL_AVOID_SIDE_EFFECTS
);
2782 struct type
*type
= check_typedef (val
->type ());
2783 if (type
->code () == TYPE_CODE_ARRAY
)
2785 type
= check_typedef (type
->target_type ());
2786 if (type
->code () == TYPE_CODE_ARRAY
)
2788 type
= type
->index_type ();
2789 /* Only re-evaluate the right hand side if the resulting type
2790 is a variable length type. */
2791 if (type
->bounds ()->flag_bound_evaluated
)
2793 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2794 /* FIXME: This should be size_t. */
2795 struct type
*size_type
2796 = builtin_type (exp
->gdbarch
)->builtin_int
;
2797 return value_from_longest
2798 (size_type
, (LONGEST
) val
->type ()->length ());
2804 return operation::evaluate_for_sizeof (exp
, noside
);
2808 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2811 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2812 EVAL_AVOID_SIDE_EFFECTS
);
2813 struct type
*type
= check_typedef (val
->type ());
2814 if (!type
->is_pointer_or_reference ()
2815 && type
->code () != TYPE_CODE_ARRAY
)
2816 error (_("Attempt to take contents of a non-pointer value."));
2817 type
= type
->target_type ();
2818 if (is_dynamic_type (type
))
2819 type
= value_ind (val
)->type ();
2820 /* FIXME: This should be size_t. */
2821 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2822 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2826 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2829 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2833 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2836 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2837 EVAL_AVOID_SIDE_EFFECTS
);
2838 return evaluate_subexp_for_sizeof_base (exp
, typeval
->type ());
2842 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2845 struct type
*type
= std::get
<0> (m_storage
).symbol
->type ();
2846 if (is_dynamic_type (type
))
2848 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2849 type
= val
->type ();
2850 if (type
->code () == TYPE_CODE_ARRAY
)
2852 /* FIXME: This should be size_t. */
2853 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2854 if (type_not_allocated (type
) || type_not_associated (type
))
2855 return value_zero (size_type
, not_lval
);
2856 else if (is_dynamic_type (type
->index_type ())
2857 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2858 return allocate_optimized_out_value (size_type
);
2861 return evaluate_subexp_for_sizeof_base (exp
, type
);
2865 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2866 struct expression
*exp
,
2869 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2870 return value_zero (to_type
, not_lval
);
2872 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2873 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2875 val
= value_cast (to_type
, val
);
2877 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2878 if (VALUE_LVAL (val
) == lval_memory
)
2881 value_fetch_lazy (val
);
2882 VALUE_LVAL (val
) = not_lval
;
2888 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2889 struct expression
*exp
,
2892 value
*val
= evaluate_var_value (noside
,
2893 std::get
<0> (m_storage
).block
,
2894 std::get
<0> (m_storage
).symbol
);
2896 val
= value_cast (to_type
, val
);
2898 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2899 if (VALUE_LVAL (val
) == lval_memory
)
2902 value_fetch_lazy (val
);
2903 VALUE_LVAL (val
) = not_lval
;
2910 /* Parse a type expression in the string [P..P+LENGTH). */
2913 parse_and_eval_type (const char *p
, int length
)
2915 char *tmp
= (char *) alloca (length
+ 4);
2918 memcpy (tmp
+ 1, p
, length
);
2919 tmp
[length
+ 1] = ')';
2920 tmp
[length
+ 2] = '0';
2921 tmp
[length
+ 3] = '\0';
2922 expression_up expr
= parse_expression (tmp
);
2923 expr::unop_cast_operation
*op
2924 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2926 error (_("Internal error in eval_type."));
2927 return op
->get_type ();