1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2025 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/>. */
23 #include "expression.h"
26 #include "gdbthread.h"
30 #include "objc-lang.h"
32 #include "parser-defs.h"
33 #include "cp-support.h"
36 #include "user-regs.h"
38 #include "gdbsupport/gdb_obstack.h"
40 #include "typeprint.h"
47 /* Parse the string EXP as a C expression, evaluate it,
48 and return the result as a number. */
51 parse_and_eval_address (const char *exp
)
53 expression_up expr
= parse_expression (exp
);
55 return value_as_address (expr
->evaluate ());
58 /* Like parse_and_eval_address, but treats the value of the expression
59 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
61 parse_and_eval_long (const char *exp
)
63 expression_up expr
= parse_expression (exp
);
65 return value_as_long (expr
->evaluate ());
69 parse_and_eval (const char *exp
, parser_flags flags
)
71 expression_up expr
= parse_expression (exp
, nullptr, flags
);
73 return expr
->evaluate ();
76 /* Parse up to a comma (or to a closeparen)
77 in the string EXPP as an expression, evaluate it, and return the value.
78 EXPP is advanced to point to the comma. */
81 parse_to_comma_and_eval (const char **expp
)
83 expression_up expr
= parse_exp_1 (expp
, 0, nullptr,
84 PARSER_COMMA_TERMINATES
);
86 return expr
->evaluate ();
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 std::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
= retval
->non_lval ();
119 /* Find the current value of a watchpoint on EXP. Return the value in
120 *VALP and *RESULTP and the chain of intermediate and final values
121 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
124 If PRESERVE_ERRORS is true, then exceptions are passed through.
125 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
126 occurs while evaluating the expression, *RESULTP will be set to
127 NULL. *RESULTP may be a lazy value, if the result could not be
128 read from memory. It is used to determine whether a value is
129 user-specified (we should watch the whole value) or intermediate
130 (we should watch only the bit used to locate the final value).
132 If the final value, or any intermediate value, could not be read
133 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
134 set to any referenced values. *VALP will never be a lazy value.
135 This is the value which we store in struct breakpoint.
137 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
138 released from the value chain. If VAL_CHAIN is NULL, all generated
139 values will be left on the value chain. */
142 fetch_subexp_value (struct expression
*exp
,
144 struct value
**valp
, struct value
**resultp
,
145 std::vector
<value_ref_ptr
> *val_chain
,
146 bool preserve_errors
)
148 struct value
*mark
, *new_mark
, *result
;
156 /* Evaluate the expression. */
157 mark
= value_mark ();
162 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
164 catch (const gdb_exception
&ex
)
166 /* Ignore memory errors if we want watchpoints pointing at
167 inaccessible memory to still be created; otherwise, throw the
168 error to some higher catcher. */
172 if (!preserve_errors
)
181 new_mark
= value_mark ();
182 if (mark
== new_mark
)
187 /* Make sure it's not lazy, so that after the target stops again we
188 have a non-lazy previous value to compare with. */
191 if (!result
->lazy ())
198 result
->fetch_lazy ();
201 catch (const gdb_exception_error
&except
)
209 /* Return the chain of intermediate values. We use this to
210 decide which addresses to watch. */
211 *val_chain
= value_release_to_mark (mark
);
215 /* Promote value ARG1 as appropriate before performing a unary operation
217 If the result is not appropriate for any particular language then it
218 needs to patch this function. */
221 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
226 *arg1
= coerce_ref (*arg1
);
227 type1
= check_typedef ((*arg1
)->type ());
229 if (is_integral_type (type1
))
231 switch (language
->la_language
)
234 /* Perform integral promotion for ANSI C/C++.
235 If not appropriate for any particular language
236 it needs to modify this function. */
238 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
240 if (type1
->length () < builtin_int
->length ())
241 *arg1
= value_cast (builtin_int
, *arg1
);
248 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
249 operation on those two operands.
250 If the result is not appropriate for any particular language then it
251 needs to patch this function. */
254 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
255 struct value
**arg1
, struct value
**arg2
)
257 struct type
*promoted_type
= NULL
;
261 *arg1
= coerce_ref (*arg1
);
262 *arg2
= coerce_ref (*arg2
);
264 type1
= check_typedef ((*arg1
)->type ());
265 type2
= check_typedef ((*arg2
)->type ());
267 if ((type1
->code () != TYPE_CODE_FLT
268 && type1
->code () != TYPE_CODE_DECFLOAT
269 && !is_integral_type (type1
))
270 || (type2
->code () != TYPE_CODE_FLT
271 && type2
->code () != TYPE_CODE_DECFLOAT
272 && !is_integral_type (type2
)))
275 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
278 if (type1
->code () == TYPE_CODE_DECFLOAT
279 || type2
->code () == TYPE_CODE_DECFLOAT
)
281 /* No promotion required. */
283 else if (type1
->code () == TYPE_CODE_FLT
284 || type2
->code () == TYPE_CODE_FLT
)
286 switch (language
->la_language
)
292 case language_opencl
:
293 /* No promotion required. */
297 /* For other languages the result type is unchanged from gdb
298 version 6.7 for backward compatibility.
299 If either arg was long double, make sure that value is also long
300 double. Otherwise use double. */
301 if (type1
->length () * 8 > gdbarch_double_bit (gdbarch
)
302 || type2
->length () * 8 > gdbarch_double_bit (gdbarch
))
303 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
305 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
309 else if (type1
->code () == TYPE_CODE_BOOL
310 && type2
->code () == TYPE_CODE_BOOL
)
312 /* No promotion required. */
315 /* Integral operations here. */
316 /* FIXME: Also mixed integral/booleans, with result an integer. */
318 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
319 unsigned int promoted_len1
= type1
->length ();
320 unsigned int promoted_len2
= type2
->length ();
321 int is_unsigned1
= type1
->is_unsigned ();
322 int is_unsigned2
= type2
->is_unsigned ();
323 unsigned int result_len
;
324 int unsigned_operation
;
326 /* Determine type length and signedness after promotion for
328 if (promoted_len1
< builtin
->builtin_int
->length ())
331 promoted_len1
= builtin
->builtin_int
->length ();
333 if (promoted_len2
< builtin
->builtin_int
->length ())
336 promoted_len2
= builtin
->builtin_int
->length ();
339 if (promoted_len1
> promoted_len2
)
341 unsigned_operation
= is_unsigned1
;
342 result_len
= promoted_len1
;
344 else if (promoted_len2
> promoted_len1
)
346 unsigned_operation
= is_unsigned2
;
347 result_len
= promoted_len2
;
351 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
352 result_len
= promoted_len1
;
355 switch (language
->la_language
)
357 case language_opencl
:
359 <= lookup_signed_typename (language
, "int")->length())
363 ? lookup_unsigned_typename (language
, "int")
364 : lookup_signed_typename (language
, "int"));
367 <= lookup_signed_typename (language
, "long")->length())
371 ? lookup_unsigned_typename (language
, "long")
372 : lookup_signed_typename (language
,"long"));
376 if (result_len
<= builtin
->builtin_int
->length ())
378 promoted_type
= (unsigned_operation
379 ? builtin
->builtin_unsigned_int
380 : builtin
->builtin_int
);
382 else if (result_len
<= builtin
->builtin_long
->length ())
384 promoted_type
= (unsigned_operation
385 ? builtin
->builtin_unsigned_long
386 : builtin
->builtin_long
);
388 else if (result_len
<= builtin
->builtin_long_long
->length ())
390 promoted_type
= (unsigned_operation
391 ? builtin
->builtin_unsigned_long_long
392 : builtin
->builtin_long_long
);
396 promoted_type
= (unsigned_operation
397 ? builtin
->builtin_uint128
398 : builtin
->builtin_int128
);
406 /* Promote both operands to common type. */
407 *arg1
= value_cast (promoted_type
, *arg1
);
408 *arg2
= value_cast (promoted_type
, *arg2
);
413 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
415 type
= check_typedef (type
);
416 if (TYPE_IS_REFERENCE (type
))
417 type
= type
->target_type ();
419 switch (type
->code ())
425 case TYPE_CODE_ARRAY
:
426 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
433 /* Represents a fake method with the given parameter types. This is
434 used by the parser to construct a temporary "expected" type for
435 method overload resolution. FLAGS is used as instance flags of the
436 new type, in order to be able to make the new type represent a
437 const/volatile overload. */
442 fake_method (type_instance_flags flags
,
443 int num_types
, struct type
**param_types
);
446 /* The constructed type. */
447 struct type
*type () { return &m_type
; }
450 struct type m_type
{};
451 main_type m_main_type
{};
454 fake_method::fake_method (type_instance_flags flags
,
455 int num_types
, struct type
**param_types
)
457 struct type
*type
= &m_type
;
459 TYPE_MAIN_TYPE (type
) = &m_main_type
;
460 type
->set_length (1);
461 type
->set_code (TYPE_CODE_METHOD
);
462 TYPE_CHAIN (type
) = type
;
463 type
->set_instance_flags (flags
);
466 if (param_types
[num_types
- 1] == NULL
)
469 type
->set_has_varargs (true);
471 else if (check_typedef (param_types
[num_types
- 1])->code ()
475 /* Caller should have ensured this. */
476 gdb_assert (num_types
== 0);
477 type
->set_is_prototyped (true);
481 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
482 neither an objfile nor a gdbarch. As a result we must manually
483 allocate memory for auxiliary fields, and free the memory ourselves
484 when we are done with it. */
485 type
->set_num_fields (num_types
);
487 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
489 while (num_types
-- > 0)
490 type
->field (num_types
).set_type (param_types
[num_types
]);
493 fake_method::~fake_method ()
495 xfree (m_type
.fields ());
502 type_instance_operation::evaluate (struct type
*expect_type
,
503 struct expression
*exp
,
506 type_instance_flags flags
= std::get
<0> (m_storage
);
507 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
509 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
510 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
516 /* Helper for evaluating an OP_VAR_VALUE. */
519 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
521 /* JYG: We used to just return value::zero of the symbol type if
522 we're asked to avoid side effects. Otherwise we return
523 value_of_variable (...). However I'm not sure if
524 value_of_variable () has any side effect. We need a full value
525 object returned here for whatis_exp () to call evaluate_type ()
526 and then pass the full value to value_rtti_target_type () if we
527 are dealing with a pointer or reference to a base class and print
530 struct value
*ret
= NULL
;
534 ret
= value_of_variable (var
, blk
);
537 catch (const gdb_exception_error
&except
)
539 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
542 ret
= value::zero (var
->type (), not_lval
);
553 var_value_operation::evaluate (struct type
*expect_type
,
554 struct expression
*exp
,
557 symbol
*var
= std::get
<0> (m_storage
).symbol
;
558 if (var
->type ()->code () == TYPE_CODE_ERROR
)
559 error_unknown_type (var
->print_name ());
560 return evaluate_var_value (noside
, std::get
<0> (m_storage
).block
, var
);
563 } /* namespace expr */
565 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
568 evaluate_var_msym_value (enum noside noside
,
569 struct objfile
*objfile
, minimal_symbol
*msymbol
)
572 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
574 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
575 return value::zero (the_type
, not_lval
);
577 return value_at_lazy (the_type
, address
);
580 /* See expression.h. */
583 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
585 gdb::array_view
<value
*> argvec
,
586 const char *function_name
,
587 type
*default_return_type
)
590 error (_("Cannot evaluate function -- may be inlined"));
592 type
*ftype
= callee
->type ();
594 /* If the callee is a struct, there might be a user-defined function call
595 operator that should be used instead. */
596 std::vector
<value
*> vals
;
597 if (overload_resolution
598 && exp
->language_defn
->la_language
== language_cplus
599 && check_typedef (ftype
)->code () == TYPE_CODE_STRUCT
)
601 /* Include space for the `this' pointer at the start. */
602 vals
.resize (argvec
.size () + 1);
604 vals
[0] = value_addr (callee
);
605 for (int i
= 0; i
< argvec
.size (); ++i
)
606 vals
[i
+ 1] = argvec
[i
];
609 find_overload_match (vals
, "operator()", METHOD
, &vals
[0], nullptr,
610 &callee
, nullptr, &static_memfuncp
, 0, noside
);
611 if (!static_memfuncp
)
614 ftype
= callee
->type ();
617 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
619 /* If the return type doesn't look like a function type,
620 call an error. This can happen if somebody tries to turn
621 a variable into a function call. */
623 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
625 /* The call to call_internal_function below handles noside. */
627 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
629 type
*return_type
= callee
->result_type_of_xmethod (argvec
);
631 if (return_type
== NULL
)
632 error (_("Xmethod is missing return type."));
633 return value::zero (return_type
, not_lval
);
635 else if (ftype
->code () == TYPE_CODE_FUNC
636 || ftype
->code () == TYPE_CODE_METHOD
)
638 if (ftype
->is_gnu_ifunc ())
640 CORE_ADDR address
= callee
->address ();
641 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
643 if (resolved_type
!= NULL
)
644 ftype
= resolved_type
;
647 type
*return_type
= ftype
->target_type ();
649 if (return_type
== NULL
)
650 return_type
= default_return_type
;
652 if (return_type
== NULL
)
653 error_call_unknown_return_type (function_name
);
655 return value::allocate (return_type
);
658 error (_("Expression of type other than "
659 "\"Function returning ...\" used as function"));
661 switch (callee
->type ()->code ())
663 case TYPE_CODE_INTERNAL_FUNCTION
:
664 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
665 callee
, argvec
.size (), argvec
.data (),
667 case TYPE_CODE_XMETHOD
:
668 return callee
->call_xmethod (argvec
);
670 return call_function_by_hand (callee
, default_return_type
, argvec
);
678 operation::evaluate_funcall (struct type
*expect_type
,
679 struct expression
*exp
,
681 const char *function_name
,
682 const std::vector
<operation_up
> &args
)
684 std::vector
<value
*> vals (args
.size ());
686 value
*callee
= evaluate_with_coercion (exp
, noside
);
687 struct type
*type
= callee
->type ();
688 if (type
->code () == TYPE_CODE_PTR
)
689 type
= type
->target_type ();
690 /* If type is a struct, num_fields would refer to the number of
691 members in the type, not the number of arguments. */
692 bool type_has_arguments
693 = type
->code () == TYPE_CODE_FUNC
|| type
->code () == TYPE_CODE_METHOD
;
694 for (int i
= 0; i
< args
.size (); ++i
)
696 if (type_has_arguments
&& 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 (symp
->type ()->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),
757 SEARCH_FUNCTION_DOMAIN
).symbol
;
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 (rhs
->type ());
831 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
833 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
834 callee
= value::zero (a1_type
->target_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 (a1_type
->target_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 (temp
->type ()),
959 + temp
->embedded_offset ());
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
);
970 /* Helper for structop_base_operation::complete which recursively adds
971 field and method names from TYPE, a struct or union type, to the
972 OUTPUT list. PREFIX is prepended to each result. */
975 add_struct_fields (struct type
*type
, completion_list
&output
,
976 const char *fieldname
, int namelen
, const char *prefix
)
979 int computed_type_name
= 0;
980 const char *type_name
= NULL
;
982 type
= check_typedef (type
);
983 for (i
= 0; i
< type
->num_fields (); ++i
)
985 if (i
< TYPE_N_BASECLASSES (type
))
986 add_struct_fields (TYPE_BASECLASS (type
, i
),
987 output
, fieldname
, namelen
, prefix
);
988 else if (type
->field (i
).name ())
990 if (type
->field (i
).name ()[0] != '\0')
992 if (! strncmp (type
->field (i
).name (),
994 output
.emplace_back (concat (prefix
, type
->field (i
).name (),
997 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
998 || type
->field (i
).type ()->code () == TYPE_CODE_STRUCT
)
1000 /* Recurse into anonymous unions and structures. */
1001 add_struct_fields (type
->field (i
).type (),
1002 output
, fieldname
, namelen
, prefix
);
1007 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
1009 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
1011 if (name
&& ! strncmp (name
, fieldname
, namelen
))
1013 if (!computed_type_name
)
1015 type_name
= type
->name ();
1016 computed_type_name
= 1;
1018 /* Omit constructors from the completion list. */
1019 if (!type_name
|| strcmp (type_name
, name
))
1020 output
.emplace_back (concat (prefix
, name
, nullptr));
1028 structop_base_operation::complete (struct expression
*exp
,
1029 completion_tracker
&tracker
,
1032 const std::string
&fieldname
= std::get
<1> (m_storage
);
1034 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1035 EVAL_AVOID_SIDE_EFFECTS
);
1036 struct type
*type
= lhs
->type ();
1039 type
= check_typedef (type
);
1040 if (!type
->is_pointer_or_reference ())
1042 type
= type
->target_type ();
1045 if (type
->code () == TYPE_CODE_UNION
1046 || type
->code () == TYPE_CODE_STRUCT
)
1048 completion_list result
;
1050 add_struct_fields (type
, result
, fieldname
.c_str (),
1051 fieldname
.length (), prefix
);
1052 tracker
.add_completions (std::move (result
));
1059 } /* namespace expr */
1061 /* Return true if type is integral or reference to integral */
1064 is_integral_or_integral_reference (struct type
*type
)
1066 if (is_integral_type (type
))
1069 type
= check_typedef (type
);
1070 return (type
!= nullptr
1071 && TYPE_IS_REFERENCE (type
)
1072 && is_integral_type (type
->target_type ()));
1075 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1078 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1079 enum noside noside
, symbol
*sym
)
1081 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1082 return value::zero (sym
->type (), not_lval
);
1084 const symbol_computed_ops
*computed_ops
= sym
->computed_ops ();
1085 if (computed_ops
== nullptr
1086 || computed_ops
->read_variable_at_entry
== nullptr)
1087 error (_("Symbol \"%s\" does not have any specific entry value"),
1088 sym
->print_name ());
1090 frame_info_ptr frame
= get_selected_frame (NULL
);
1091 return computed_ops
->read_variable_at_entry (sym
, frame
);
1094 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1097 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1098 enum noside noside
, bool outermost_p
,
1099 bound_minimal_symbol msymbol
)
1101 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1104 struct type
*type
= val
->type ();
1105 if (type
->code () == TYPE_CODE_ERROR
1106 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1107 error_unknown_type (msymbol
.minsym
->print_name ());
1111 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1114 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1116 value
*func
, const char *var
)
1118 CORE_ADDR addr
= func
->address ();
1119 const block
*blk
= block_for_pc (addr
);
1120 struct block_symbol sym
= lookup_symbol (var
, blk
, SEARCH_VAR_DOMAIN
,
1122 if (sym
.symbol
== NULL
)
1123 error (_("No symbol \"%s\" in specified context."), var
);
1124 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1127 /* Helper function that implements the body of OP_REGISTER. */
1130 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1131 enum noside noside
, const char *name
)
1136 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1137 name
, strlen (name
));
1139 error (_("Register $%s not available."), name
);
1141 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1142 a value with the appropriate register type. Unfortunately,
1143 we don't have easy access to the type of user registers.
1144 So for these registers, we fetch the register value regardless
1145 of the evaluation mode. */
1146 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1147 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1148 val
= value::zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1150 val
= value_of_register
1151 (regno
, get_next_frame_sentinel_okay (get_selected_frame ()));
1153 error (_("Value of register %s not available."), name
);
1162 string_operation::evaluate (struct type
*expect_type
,
1163 struct expression
*exp
,
1166 const std::string
&str
= std::get
<0> (m_storage
);
1167 struct type
*type
= language_string_char_type (exp
->language_defn
,
1169 return value_string (str
.c_str (), str
.size (), type
);
1173 ternop_slice_operation::evaluate (struct type
*expect_type
,
1174 struct expression
*exp
,
1178 = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
1180 = std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
1182 = std::get
<2> (m_storage
)->evaluate (nullptr, exp
, noside
);
1184 int lowbound
= value_as_long (low
);
1185 int upperbound
= value_as_long (upper
);
1186 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1189 } /* namespace expr */
1191 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1194 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1198 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1199 return value_from_longest (selector_type
,
1200 lookup_child_selector (exp
->gdbarch
, sel
));
1203 /* A helper function for STRUCTOP_STRUCT. */
1206 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1208 struct value
*arg1
, const char *string
)
1210 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1212 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1213 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1217 /* A helper function for STRUCTOP_PTR. */
1220 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1222 struct value
*arg1
, const char *string
)
1224 /* Check to see if operator '->' has been overloaded. If so replace
1225 arg1 with the value returned by evaluating operator->(). */
1226 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1228 struct value
*value
= NULL
;
1231 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1234 catch (const gdb_exception_error
&except
)
1236 if (except
.error
== NOT_FOUND_ERROR
)
1245 /* JYG: if print object is on we need to replace the base type
1246 with rtti type in order to continue on with successful
1247 lookup of member / method only available in the rtti type. */
1249 struct type
*arg_type
= arg1
->type ();
1250 struct type
*real_type
;
1251 int full
, using_enc
;
1253 struct value_print_options opts
;
1255 get_user_print_options (&opts
);
1256 if (opts
.objectprint
&& arg_type
->target_type ()
1257 && (arg_type
->target_type ()->code () == TYPE_CODE_STRUCT
))
1259 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1262 arg1
= value_cast (real_type
, arg1
);
1266 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1267 NULL
, "structure pointer");
1268 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1269 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1273 /* A helper function for STRUCTOP_MEMBER. */
1276 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1278 struct value
*arg1
, struct value
*arg2
)
1283 struct type
*type
= check_typedef (arg2
->type ());
1284 switch (type
->code ())
1286 case TYPE_CODE_METHODPTR
:
1287 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1288 return value::zero (type
->target_type (), not_lval
);
1291 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1292 gdb_assert (arg2
->type ()->code () == TYPE_CODE_PTR
);
1293 return value_ind (arg2
);
1296 case TYPE_CODE_MEMBERPTR
:
1297 /* Now, convert these values to an address. */
1298 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_PTR
)
1299 arg1
= value_addr (arg1
);
1300 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1303 mem_offset
= value_as_long (arg2
);
1305 arg3
= value_from_pointer (lookup_pointer_type (type
->target_type ()),
1306 value_as_long (arg1
) + mem_offset
);
1307 return value_ind (arg3
);
1310 error (_("non-pointer-to-member value used "
1311 "in pointer-to-member construct"));
1315 /* A helper function for BINOP_ADD. */
1318 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1320 struct value
*arg1
, struct value
*arg2
)
1322 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1323 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1324 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1325 && is_integral_or_integral_reference (arg2
->type ()))
1326 return value_ptradd (arg1
, value_as_long (arg2
));
1327 else if (ptrmath_type_p (exp
->language_defn
, arg2
->type ())
1328 && is_integral_or_integral_reference (arg1
->type ()))
1329 return value_ptradd (arg2
, value_as_long (arg1
));
1332 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1333 return value_binop (arg1
, arg2
, BINOP_ADD
);
1337 /* A helper function for BINOP_SUB. */
1340 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1342 struct value
*arg1
, struct value
*arg2
)
1344 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1345 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1346 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1347 && ptrmath_type_p (exp
->language_defn
, arg2
->type ()))
1349 /* FIXME -- should be ptrdiff_t */
1350 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1351 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1353 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1354 && is_integral_or_integral_reference (arg2
->type ()))
1355 return value_ptradd (arg1
, - value_as_long (arg2
));
1358 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1359 return value_binop (arg1
, arg2
, BINOP_SUB
);
1363 /* Helper function for several different binary operations. */
1366 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1367 enum noside noside
, enum exp_opcode op
,
1368 struct value
*arg1
, struct value
*arg2
)
1370 if (binop_user_defined_p (op
, arg1
, arg2
))
1371 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1374 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1375 fudge arg2 to avoid division-by-zero, the caller is
1376 (theoretically) only looking for the type of the result. */
1377 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1378 /* ??? Do we really want to test for BINOP_MOD here?
1379 The implementation of value_binop gives it a well-defined
1382 || op
== BINOP_INTDIV
1385 && value_logical_not (arg2
))
1387 struct value
*v_one
;
1389 v_one
= value_one (arg2
->type ());
1390 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1391 return value_binop (arg1
, v_one
, op
);
1395 /* For shift and integer exponentiation operations,
1396 only promote the first argument. */
1397 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1398 && is_integral_type (arg2
->type ()))
1399 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1401 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1403 return value_binop (arg1
, arg2
, op
);
1408 /* A helper function for BINOP_SUBSCRIPT. */
1411 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1412 enum noside noside
, enum exp_opcode op
,
1413 struct value
*arg1
, struct value
*arg2
)
1415 if (binop_user_defined_p (op
, arg1
, arg2
))
1416 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1419 /* If the user attempts to subscript something that is not an
1420 array or pointer type (like a plain int variable for example),
1421 then report this as an error. */
1423 arg1
= coerce_ref (arg1
);
1424 struct type
*type
= check_typedef (arg1
->type ());
1425 if (type
->code () != TYPE_CODE_ARRAY
1426 && type
->code () != TYPE_CODE_PTR
)
1429 error (_("cannot subscript something of type `%s'"),
1432 error (_("cannot subscript requested type"));
1435 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1436 return value::zero (type
->target_type (), arg1
->lval ());
1438 return value_subscript (arg1
, value_as_long (arg2
));
1442 /* A helper function for BINOP_EQUAL. */
1445 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1446 enum noside noside
, enum exp_opcode op
,
1447 struct value
*arg1
, struct value
*arg2
)
1449 if (binop_user_defined_p (op
, arg1
, arg2
))
1451 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1455 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1456 int tem
= value_equal (arg1
, arg2
);
1457 struct type
*type
= language_bool_type (exp
->language_defn
,
1459 return value_from_longest (type
, (LONGEST
) tem
);
1463 /* A helper function for BINOP_NOTEQUAL. */
1466 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1467 enum noside noside
, enum exp_opcode op
,
1468 struct value
*arg1
, struct value
*arg2
)
1470 if (binop_user_defined_p (op
, arg1
, arg2
))
1472 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1476 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1477 int tem
= value_equal (arg1
, arg2
);
1478 struct type
*type
= language_bool_type (exp
->language_defn
,
1480 return value_from_longest (type
, (LONGEST
) ! tem
);
1484 /* A helper function for BINOP_LESS. */
1487 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1488 enum noside noside
, enum exp_opcode op
,
1489 struct value
*arg1
, struct value
*arg2
)
1491 if (binop_user_defined_p (op
, arg1
, arg2
))
1493 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1497 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1498 int tem
= value_less (arg1
, arg2
);
1499 struct type
*type
= language_bool_type (exp
->language_defn
,
1501 return value_from_longest (type
, (LONGEST
) tem
);
1505 /* A helper function for BINOP_GTR. */
1508 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1509 enum noside noside
, enum exp_opcode op
,
1510 struct value
*arg1
, struct value
*arg2
)
1512 if (binop_user_defined_p (op
, arg1
, arg2
))
1514 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1518 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1519 int tem
= value_less (arg2
, arg1
);
1520 struct type
*type
= language_bool_type (exp
->language_defn
,
1522 return value_from_longest (type
, (LONGEST
) tem
);
1526 /* A helper function for BINOP_GEQ. */
1529 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1530 enum noside noside
, enum exp_opcode op
,
1531 struct value
*arg1
, struct value
*arg2
)
1533 if (binop_user_defined_p (op
, arg1
, arg2
))
1535 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1539 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1540 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1541 struct type
*type
= language_bool_type (exp
->language_defn
,
1543 return value_from_longest (type
, (LONGEST
) tem
);
1547 /* A helper function for BINOP_LEQ. */
1550 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1551 enum noside noside
, enum exp_opcode op
,
1552 struct value
*arg1
, struct value
*arg2
)
1554 if (binop_user_defined_p (op
, arg1
, arg2
))
1556 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1560 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1561 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1562 struct type
*type
= language_bool_type (exp
->language_defn
,
1564 return value_from_longest (type
, (LONGEST
) tem
);
1568 /* A helper function for BINOP_REPEAT. */
1571 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1572 enum noside noside
, enum exp_opcode op
,
1573 struct value
*arg1
, struct value
*arg2
)
1575 struct type
*type
= check_typedef (arg2
->type ());
1576 if (type
->code () != TYPE_CODE_INT
1577 && type
->code () != TYPE_CODE_ENUM
)
1578 error (_("Non-integral right operand for \"@\" operator."));
1579 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1581 return allocate_repeat_value (arg1
->type (),
1582 longest_to_int (value_as_long (arg2
)));
1585 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1588 /* A helper function for UNOP_PLUS. */
1591 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1592 enum noside noside
, enum exp_opcode op
,
1595 if (unop_user_defined_p (op
, arg1
))
1596 return value_x_unop (arg1
, op
, noside
);
1599 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1600 return value_pos (arg1
);
1604 /* A helper function for UNOP_NEG. */
1607 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1608 enum noside noside
, enum exp_opcode op
,
1611 if (unop_user_defined_p (op
, arg1
))
1612 return value_x_unop (arg1
, op
, noside
);
1615 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1616 return value_neg (arg1
);
1620 /* A helper function for UNOP_COMPLEMENT. */
1623 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1624 enum noside noside
, enum exp_opcode op
,
1627 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1628 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1631 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1632 return value_complement (arg1
);
1636 /* A helper function for UNOP_LOGICAL_NOT. */
1639 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1640 enum noside noside
, enum exp_opcode op
,
1643 if (unop_user_defined_p (op
, arg1
))
1644 return value_x_unop (arg1
, op
, noside
);
1647 struct type
*type
= language_bool_type (exp
->language_defn
,
1649 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1653 /* A helper function for UNOP_IND. */
1656 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1660 struct type
*type
= check_typedef (arg1
->type ());
1661 if (type
->code () == TYPE_CODE_METHODPTR
1662 || type
->code () == TYPE_CODE_MEMBERPTR
)
1663 error (_("Attempt to dereference pointer "
1664 "to member without an object"));
1665 if (unop_user_defined_p (UNOP_IND
, arg1
))
1666 return value_x_unop (arg1
, UNOP_IND
, noside
);
1667 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1669 type
= check_typedef (arg1
->type ());
1671 /* If the type pointed to is dynamic then in order to resolve the
1672 dynamic properties we must actually dereference the pointer.
1673 There is a risk that this dereference will have side-effects
1674 in the inferior, but being able to print accurate type
1675 information seems worth the risk. */
1676 if (!type
->is_pointer_or_reference ()
1677 || !is_dynamic_type (type
->target_type ()))
1679 if (type
->is_pointer_or_reference ()
1680 /* In C you can dereference an array to get the 1st elt. */
1681 || type
->code () == TYPE_CODE_ARRAY
)
1682 return value::zero (type
->target_type (),
1684 else if (type
->code () == TYPE_CODE_INT
)
1685 /* GDB allows dereferencing an int. */
1686 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1689 error (_("Attempt to take contents of a non-pointer value."));
1693 /* Allow * on an integer so we can cast it to whatever we want.
1694 This returns an int, which seems like the most C-like thing to
1695 do. "long long" variables are rare enough that
1696 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1697 if (type
->code () == TYPE_CODE_INT
)
1698 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1699 value_as_address (arg1
));
1700 return value_ind (arg1
);
1703 /* A helper function for UNOP_ALIGNOF. */
1706 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1710 struct type
*type
= arg1
->type ();
1711 /* FIXME: This should be size_t. */
1712 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1713 ULONGEST align
= type_align (type
);
1715 error (_("could not determine alignment of type"));
1716 return value_from_longest (size_type
, align
);
1719 /* A helper function for UNOP_MEMVAL. */
1722 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1724 struct value
*arg1
, struct type
*type
)
1726 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1727 return value::zero (type
, lval_memory
);
1729 return value_at_lazy (type
, value_as_address (arg1
));
1732 /* A helper function for UNOP_PREINCREMENT. */
1735 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1736 enum noside noside
, enum exp_opcode op
,
1739 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1741 else if (unop_user_defined_p (op
, arg1
))
1743 return value_x_unop (arg1
, op
, noside
);
1748 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1749 arg2
= value_ptradd (arg1
, 1);
1752 struct value
*tmp
= arg1
;
1754 arg2
= value_one (arg1
->type ());
1755 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1756 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1759 return value_assign (arg1
, arg2
);
1763 /* A helper function for UNOP_PREDECREMENT. */
1766 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1767 enum noside noside
, enum exp_opcode op
,
1770 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1772 else if (unop_user_defined_p (op
, arg1
))
1774 return value_x_unop (arg1
, op
, noside
);
1779 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1780 arg2
= value_ptradd (arg1
, -1);
1783 struct value
*tmp
= arg1
;
1785 arg2
= value_one (arg1
->type ());
1786 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1787 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1790 return value_assign (arg1
, arg2
);
1794 /* A helper function for UNOP_POSTINCREMENT. */
1797 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1798 enum noside noside
, enum exp_opcode op
,
1801 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1803 else if (unop_user_defined_p (op
, arg1
))
1805 return value_x_unop (arg1
, op
, noside
);
1809 struct value
*arg3
= arg1
->non_lval ();
1812 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1813 arg2
= value_ptradd (arg1
, 1);
1816 struct value
*tmp
= arg1
;
1818 arg2
= value_one (arg1
->type ());
1819 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1820 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1823 value_assign (arg1
, arg2
);
1828 /* A helper function for UNOP_POSTDECREMENT. */
1831 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1832 enum noside noside
, enum exp_opcode op
,
1835 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1837 else if (unop_user_defined_p (op
, arg1
))
1839 return value_x_unop (arg1
, op
, noside
);
1843 struct value
*arg3
= arg1
->non_lval ();
1846 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1847 arg2
= value_ptradd (arg1
, -1);
1850 struct value
*tmp
= arg1
;
1852 arg2
= value_one (arg1
->type ());
1853 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1854 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1857 value_assign (arg1
, arg2
);
1866 type_operation::evaluate (struct type
*expect_type
, struct expression
*exp
,
1869 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1870 return value::allocate (std::get
<0> (m_storage
));
1872 error (_("Attempt to use a type name as an expression"));
1877 /* A helper function for BINOP_ASSIGN_MODIFY. */
1880 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1881 enum noside noside
, enum exp_opcode op
,
1882 struct value
*arg1
, struct value
*arg2
)
1884 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1886 if (binop_user_defined_p (op
, arg1
, arg2
))
1887 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1888 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1890 && is_integral_type (arg2
->type ()))
1891 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1892 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1894 && is_integral_type (arg2
->type ()))
1895 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1898 struct value
*tmp
= arg1
;
1900 /* For shift and integer exponentiation operations,
1901 only promote the first argument. */
1902 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1903 && is_integral_type (arg2
->type ()))
1904 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1906 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1908 arg2
= value_binop (tmp
, arg2
, op
);
1910 return value_assign (arg1
, arg2
);
1913 /* Note that ARGS needs 2 empty slots up front and must end with a
1915 static struct value
*
1916 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1917 enum noside noside
, CORE_ADDR selector
,
1918 value
*target
, gdb::array_view
<value
*> args
)
1920 CORE_ADDR responds_selector
= 0;
1921 CORE_ADDR method_selector
= 0;
1923 int struct_return
= 0;
1925 struct value
*msg_send
= NULL
;
1926 struct value
*msg_send_stret
= NULL
;
1927 int gnu_runtime
= 0;
1929 struct value
*method
= NULL
;
1930 struct value
*called_method
= NULL
;
1932 struct type
*selector_type
= NULL
;
1933 struct type
*long_type
;
1936 struct value
*ret
= NULL
;
1941 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1942 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1944 if (value_as_long (target
) == 0)
1945 return value_from_longest (long_type
, 0);
1947 if (lookup_minimal_symbol (current_program_space
, "objc_msg_lookup").minsym
1951 /* Find the method dispatch (Apple runtime) or method lookup
1952 (GNU runtime) function for Objective-C. These will be used
1953 to lookup the symbol information for the method. If we
1954 can't find any symbol information, then we'll use these to
1955 call the method, otherwise we can call the method
1956 directly. The msg_send_stret function is used in the special
1957 case of a method that returns a structure (Apple runtime
1961 type
= selector_type
;
1963 type
= lookup_function_type (type
);
1964 type
= lookup_pointer_type (type
);
1965 type
= lookup_function_type (type
);
1966 type
= lookup_pointer_type (type
);
1968 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1970 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1972 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1973 msg_send_stret
= value_from_pointer (type
,
1974 value_as_address (msg_send_stret
));
1978 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1979 /* Special dispatcher for methods returning structs. */
1981 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1984 /* Verify the target object responds to this method. The
1985 standard top-level 'Object' class uses a different name for
1986 the verification method than the non-standard, but more
1987 often used, 'NSObject' class. Make sure we check for both. */
1990 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1991 if (responds_selector
== 0)
1993 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1995 if (responds_selector
== 0)
1996 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1999 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
2000 if (method_selector
== 0)
2002 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
2004 if (method_selector
== 0)
2005 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2007 /* Call the verification method, to make sure that the target
2008 class implements the desired method. */
2010 argvec
[0] = msg_send
;
2012 argvec
[2] = value_from_longest (long_type
, responds_selector
);
2013 argvec
[3] = value_from_longest (long_type
, selector
);
2016 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2019 /* Function objc_msg_lookup returns a pointer. */
2021 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2023 if (value_as_long (ret
) == 0)
2024 error (_("Target does not respond to this message selector."));
2026 /* Call "methodForSelector:" method, to get the address of a
2027 function method that implements this selector for this
2028 class. If we can find a symbol at that address, then we
2029 know the return type, parameter types etc. (that's a good
2032 argvec
[0] = msg_send
;
2034 argvec
[2] = value_from_longest (long_type
, method_selector
);
2035 argvec
[3] = value_from_longest (long_type
, selector
);
2038 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2042 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2045 /* ret should now be the selector. */
2047 addr
= value_as_long (ret
);
2050 struct symbol
*sym
= NULL
;
2052 /* The address might point to a function descriptor;
2053 resolve it to the actual code address instead. */
2054 addr
= gdbarch_convert_from_func_ptr_addr
2055 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
2057 /* Is it a high_level symbol? */
2058 sym
= find_pc_function (addr
);
2060 method
= value_of_variable (sym
, 0);
2063 /* If we found a method with symbol information, check to see
2064 if it returns a struct. Otherwise assume it doesn't. */
2069 struct type
*val_type
;
2071 funaddr
= find_function_addr (method
, &val_type
);
2073 block_for_pc (funaddr
);
2075 val_type
= check_typedef (val_type
);
2077 if ((val_type
== NULL
)
2078 || (val_type
->code () == TYPE_CODE_ERROR
))
2080 if (expect_type
!= NULL
)
2081 val_type
= expect_type
;
2084 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2087 else if (expect_type
!= NULL
)
2089 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2090 check_typedef (expect_type
));
2093 /* Found a function symbol. Now we will substitute its
2094 value in place of the message dispatcher (obj_msgSend),
2095 so that we call the method directly instead of through
2096 the dispatcher. The main reason for doing this is that
2097 we can now evaluate the return value and parameter values
2098 according to their known data types, in case we need to
2099 do things like promotion, dereferencing, special handling
2100 of structs and doubles, etc.
2102 We want to use the type signature of 'method', but still
2103 jump to objc_msgSend() or objc_msgSend_stret() to better
2104 mimic the behavior of the runtime. */
2108 if (method
->type ()->code () != TYPE_CODE_FUNC
)
2109 error (_("method address has symbol information "
2110 "with non-function type; skipping"));
2112 /* Create a function pointer of the appropriate type, and
2113 replace its value with the value of msg_send or
2114 msg_send_stret. We must use a pointer here, as
2115 msg_send and msg_send_stret are of pointer type, and
2116 the representation may be different on systems that use
2117 function descriptors. */
2120 = value_from_pointer (lookup_pointer_type (method
->type ()),
2121 value_as_address (msg_send_stret
));
2124 = value_from_pointer (lookup_pointer_type (method
->type ()),
2125 value_as_address (msg_send
));
2130 called_method
= msg_send_stret
;
2132 called_method
= msg_send
;
2136 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2138 /* If the return type doesn't look like a function type,
2139 call an error. This can happen if somebody tries to
2140 turn a variable into a function call. This is here
2141 because people often want to call, eg, strcmp, which
2142 gdb doesn't know is a function. If gdb isn't asked for
2143 it's opinion (ie. through "whatis"), it won't offer
2146 struct type
*callee_type
= called_method
->type ();
2148 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2149 callee_type
= callee_type
->target_type ();
2150 callee_type
= callee_type
->target_type ();
2154 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2155 return value::allocate (expect_type
);
2157 return value::allocate (callee_type
);
2160 error (_("Expression of type other than "
2161 "\"method returning ...\" used as a method"));
2164 /* Now depending on whether we found a symbol for the method,
2165 we will either call the runtime dispatcher or the method
2169 args
[1] = value_from_longest (long_type
, selector
);
2171 if (gnu_runtime
&& (method
!= NULL
))
2173 /* Function objc_msg_lookup returns a pointer. */
2174 struct type
*tem_type
= called_method
->type ();
2175 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2176 called_method
->deprecated_set_type (tem_type
);
2177 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2180 return call_function_by_hand (called_method
, NULL
, args
);
2183 /* Helper function for MULTI_SUBSCRIPT. */
2185 static struct value
*
2186 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2187 enum noside noside
, value
*arg1
,
2188 gdb::array_view
<value
*> args
)
2190 for (value
*arg2
: args
)
2192 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2194 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2198 arg1
= coerce_ref (arg1
);
2199 struct type
*type
= check_typedef (arg1
->type ());
2201 switch (type
->code ())
2204 case TYPE_CODE_ARRAY
:
2205 case TYPE_CODE_STRING
:
2206 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2211 error (_("cannot subscript something of type `%s'"),
2214 error (_("cannot subscript requested type"));
2225 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2226 struct expression
*exp
,
2229 enum noside sub_no_side
= EVAL_NORMAL
;
2230 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2232 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2233 sub_no_side
= EVAL_NORMAL
;
2235 sub_no_side
= noside
;
2237 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2239 if (value_as_long (target
) == 0)
2240 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2242 sub_no_side
= noside
;
2243 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2244 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2245 argvec
[0] = nullptr;
2246 argvec
[1] = nullptr;
2247 for (int i
= 0; i
< args
.size (); ++i
)
2248 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2249 argvec
[args
.size () + 2] = nullptr;
2251 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2252 get
<0> (m_storage
), target
,
2253 gdb::make_array_view (argvec
,
2258 multi_subscript_operation::evaluate (struct type
*expect_type
,
2259 struct expression
*exp
,
2262 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2263 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2264 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2265 for (int ix
= 0; ix
< values
.size (); ++ix
)
2266 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2267 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2268 gdb::make_array_view (argvec
, values
.size ()));
2272 logical_and_operation::evaluate (struct type
*expect_type
,
2273 struct expression
*exp
,
2276 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2278 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2279 EVAL_AVOID_SIDE_EFFECTS
);
2281 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2283 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2284 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2288 bool tem
= value_logical_not (arg1
);
2291 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2292 tem
= value_logical_not (arg2
);
2294 struct type
*type
= language_bool_type (exp
->language_defn
,
2296 return value_from_longest (type
, !tem
);
2301 logical_or_operation::evaluate (struct type
*expect_type
,
2302 struct expression
*exp
,
2305 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2307 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2308 EVAL_AVOID_SIDE_EFFECTS
);
2310 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2312 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2313 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2317 bool tem
= value_logical_not (arg1
);
2320 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2321 tem
= value_logical_not (arg2
);
2324 struct type
*type
= language_bool_type (exp
->language_defn
,
2326 return value_from_longest (type
, !tem
);
2331 adl_func_operation::evaluate (struct type
*expect_type
,
2332 struct expression
*exp
,
2335 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2336 std::vector
<value
*> args (arg_ops
.size ());
2337 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2338 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2340 struct symbol
*symp
;
2341 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2344 nullptr, &symp
, nullptr, 0, noside
);
2345 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
2346 error_unknown_type (symp
->print_name ());
2347 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2348 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2349 nullptr, expect_type
);
2353 /* This function evaluates brace-initializers (in C/C++) for
2357 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2358 struct expression
*exp
,
2359 enum noside noside
, int nargs
)
2361 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2362 struct type
*struct_type
= check_typedef (struct_val
->type ());
2363 struct type
*field_type
;
2367 while (--nargs
>= 0)
2369 struct value
*val
= NULL
;
2370 int bitpos
, bitsize
;
2374 /* Skip static fields. */
2375 while (fieldno
< struct_type
->num_fields ()
2376 && struct_type
->field (fieldno
).is_static ())
2378 if (fieldno
>= struct_type
->num_fields ())
2379 error (_("too many initializers"));
2380 field_type
= struct_type
->field (fieldno
).type ();
2381 if (field_type
->code () == TYPE_CODE_UNION
2382 && struct_type
->field (fieldno
).name ()[0] == '0')
2383 error (_("don't know which variant you want to set"));
2385 /* Here, struct_type is the type of the inner struct,
2386 while substruct_type is the type of the inner struct.
2387 These are the same for normal structures, but a variant struct
2388 contains anonymous union fields that contain substruct fields.
2389 The value fieldno is the index of the top-level (normal or
2390 anonymous union) field in struct_field, while the value
2391 subfieldno is the index of the actual real (named inner) field
2392 in substruct_type. */
2394 field_type
= struct_type
->field (fieldno
).type ();
2396 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2398 /* Now actually set the field in struct_val. */
2400 /* Assign val to field fieldno. */
2401 if (val
->type () != field_type
)
2402 val
= value_cast (field_type
, val
);
2404 bitsize
= struct_type
->field (fieldno
).bitsize ();
2405 bitpos
= struct_type
->field (fieldno
).loc_bitpos ();
2406 addr
= struct_val
->contents_writeable ().data () + bitpos
/ 8;
2408 modify_field (struct_type
, addr
,
2409 value_as_long (val
), bitpos
% 8, bitsize
);
2411 memcpy (addr
, val
->contents ().data (),
2412 val
->type ()->length ());
2419 array_operation::evaluate (struct type
*expect_type
,
2420 struct expression
*exp
,
2423 const int provided_low_bound
= std::get
<0> (m_storage
);
2424 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2425 const int nargs
= std::get
<1> (m_storage
) - provided_low_bound
+ 1;
2426 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2428 if (expect_type
!= nullptr
2429 && type
->code () == TYPE_CODE_STRUCT
)
2431 struct value
*rec
= value::allocate (expect_type
);
2433 memset (rec
->contents_raw ().data (), '\0', type
->length ());
2434 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2437 if (expect_type
!= nullptr
2438 && type
->code () == TYPE_CODE_ARRAY
)
2440 struct type
*range_type
= type
->index_type ();
2441 struct type
*element_type
= type
->target_type ();
2442 struct value
*array
= value::allocate (expect_type
);
2443 int element_size
= check_typedef (element_type
)->length ();
2444 LONGEST low_bound
, high_bound
;
2446 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2449 high_bound
= (type
->length () / element_size
) - 1;
2451 if (low_bound
+ nargs
- 1 > high_bound
)
2452 error (_("Too many array elements"));
2453 memset (array
->contents_raw ().data (), 0, expect_type
->length ());
2454 for (int idx
= 0; idx
< nargs
; ++idx
)
2456 struct value
*element
;
2458 element
= in_args
[idx
]->evaluate (element_type
, exp
, noside
);
2459 if (element
->type () != element_type
)
2460 element
= value_cast (element_type
, element
);
2461 memcpy (array
->contents_raw ().data () + idx
* element_size
,
2462 element
->contents ().data (),
2468 if (expect_type
!= nullptr
2469 && type
->code () == TYPE_CODE_SET
)
2471 struct value
*set
= value::allocate (expect_type
);
2472 gdb_byte
*valaddr
= set
->contents_raw ().data ();
2473 struct type
*element_type
= type
->index_type ();
2474 struct type
*check_type
= element_type
;
2475 LONGEST low_bound
, high_bound
;
2477 /* Get targettype of elementtype. */
2478 while (check_type
->code () == TYPE_CODE_RANGE
2479 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2480 check_type
= check_type
->target_type ();
2482 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2483 error (_("(power)set type with unknown size"));
2484 memset (valaddr
, '\0', type
->length ());
2485 for (int idx
= 0; idx
< nargs
; idx
++)
2487 LONGEST range_low
, range_high
;
2488 struct type
*range_low_type
, *range_high_type
;
2489 struct value
*elem_val
;
2491 elem_val
= in_args
[idx
]->evaluate (element_type
, exp
, noside
);
2492 range_low_type
= range_high_type
= elem_val
->type ();
2493 range_low
= range_high
= value_as_long (elem_val
);
2495 /* Check types of elements to avoid mixture of elements from
2496 different types. Also check if type of element is "compatible"
2497 with element type of powerset. */
2498 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2499 range_low_type
= range_low_type
->target_type ();
2500 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2501 range_high_type
= range_high_type
->target_type ();
2502 if ((range_low_type
->code () != range_high_type
->code ())
2503 || (range_low_type
->code () == TYPE_CODE_ENUM
2504 && (range_low_type
!= range_high_type
)))
2505 /* different element modes. */
2506 error (_("POWERSET tuple elements of different mode"));
2507 if ((check_type
->code () != range_low_type
->code ())
2508 || (check_type
->code () == TYPE_CODE_ENUM
2509 && range_low_type
!= check_type
))
2510 error (_("incompatible POWERSET tuple elements"));
2511 if (range_low
> range_high
)
2513 warning (_("empty POWERSET tuple range"));
2516 if (range_low
< low_bound
|| range_high
> high_bound
)
2517 error (_("POWERSET tuple element out of range"));
2518 range_low
-= low_bound
;
2519 range_high
-= low_bound
;
2520 for (; range_low
<= range_high
; range_low
++)
2522 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2524 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2525 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2526 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2533 std::vector
<value
*> argvec (nargs
);
2534 for (int tem
= 0; tem
< nargs
; tem
++)
2536 /* Ensure that array expressions are coerced into pointer
2538 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2540 return value_array (provided_low_bound
, argvec
);
2544 unop_extract_operation::evaluate (struct type
*expect_type
,
2545 struct expression
*exp
,
2548 value
*old_value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2549 struct type
*type
= get_type ();
2551 if (type
->length () > old_value
->type ()->length ())
2552 error (_("length type is larger than the value type"));
2554 struct value
*result
= value::allocate (type
);
2555 old_value
->contents_copy (result
, 0, 0, type
->length ());
2561 /* Helper for evaluate_subexp_for_address. */
2564 evaluate_subexp_for_address_base (enum noside noside
, value
*x
)
2566 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2568 struct type
*type
= check_typedef (x
->type ());
2569 enum type_code typecode
= type
->code ();
2571 if (TYPE_IS_REFERENCE (type
))
2572 return value::zero (lookup_pointer_type (type
->target_type ()),
2574 else if (x
->lval () == lval_memory
|| value_must_coerce_to_target (x
)
2575 || typecode
== TYPE_CODE_STRUCT
|| typecode
== TYPE_CODE_UNION
)
2576 return value::zero (lookup_pointer_type (x
->type ()), not_lval
);
2578 error (_("Attempt to take address of value not located in memory."));
2581 return value_addr (x
);
2588 operation::evaluate_for_cast (struct type
*expect_type
,
2589 struct expression
*exp
,
2592 value
*val
= evaluate (expect_type
, exp
, noside
);
2593 return value_cast (expect_type
, val
);
2597 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2599 value
*val
= evaluate (nullptr, exp
, noside
);
2600 return evaluate_subexp_for_address_base (noside
, val
);
2604 scope_operation::evaluate_internal (struct type
*expect_type
,
2605 struct expression
*exp
,
2609 const char *string
= std::get
<1> (m_storage
).c_str ();
2610 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
), string
,
2611 expect_type
, want_address
, noside
);
2613 error (_("There is no field named %s"), string
);
2618 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2621 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2623 /* We can't optimize out "&*" if there's a user-defined operator*. */
2624 if (unop_user_defined_p (UNOP_IND
, x
))
2626 x
= value_x_unop (x
, UNOP_IND
, noside
);
2627 return evaluate_subexp_for_address_base (noside
, x
);
2630 return coerce_array (x
);
2634 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2637 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2638 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2639 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2641 struct type
*type
= lookup_pointer_type (val
->type ());
2642 return value::zero (type
, not_lval
);
2645 return value_addr (val
);
2649 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2652 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2653 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2657 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2660 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2661 EVAL_AVOID_SIDE_EFFECTS
);
2662 struct type
*type
= typeval
->type ();
2663 return value_cast (lookup_pointer_type (type
),
2664 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2668 var_value_operation::evaluate_for_address (struct expression
*exp
,
2671 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2673 /* C++: The "address" of a reference should yield the address
2674 * of the object pointed to. Let value_addr() deal with it. */
2675 if (TYPE_IS_REFERENCE (var
->type ()))
2676 return operation::evaluate_for_address (exp
, noside
);
2678 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2680 struct type
*type
= lookup_pointer_type (var
->type ());
2681 enum address_class sym_class
= var
->aclass ();
2683 if (sym_class
== LOC_CONST
2684 || sym_class
== LOC_CONST_BYTES
2685 || sym_class
== LOC_REGISTER
)
2686 error (_("Attempt to take address of register or constant."));
2688 return value::zero (type
, not_lval
);
2691 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2695 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2698 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2699 struct type
*type
= check_typedef (var
->type ());
2700 if (type
->code () == TYPE_CODE_ARRAY
2701 && !type
->is_vector ()
2702 && CAST_IS_CONVERSION (exp
->language_defn
))
2704 struct value
*val
= address_of_variable (var
,
2705 std::get
<0> (m_storage
).block
);
2706 return value_cast (lookup_pointer_type (type
->target_type ()), val
);
2708 return evaluate (nullptr, exp
, noside
);
2713 /* Helper function for evaluating the size of a type. */
2716 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2718 /* FIXME: This should be size_t. */
2719 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2720 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2721 "When applied to a reference or a reference type, the result is
2722 the size of the referenced type." */
2723 type
= check_typedef (type
);
2724 if (exp
->language_defn
->la_language
== language_cplus
2725 && (TYPE_IS_REFERENCE (type
)))
2726 type
= check_typedef (type
->target_type ());
2727 else if (exp
->language_defn
->la_language
== language_fortran
2728 && type
->code () == TYPE_CODE_PTR
)
2730 /* Dereference Fortran pointer types to allow them for the Fortran
2731 sizeof intrinsic. */
2732 type
= check_typedef (type
->target_type ());
2734 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2741 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2743 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2744 return evaluate_subexp_for_sizeof_base (exp
, val
->type ());
2748 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2752 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2753 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2755 struct type
*type
= mval
->type ();
2756 if (type
->code () == TYPE_CODE_ERROR
)
2757 error_unknown_type (b
.minsym
->print_name ());
2759 /* FIXME: This should be size_t. */
2760 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2761 return value_from_longest (size_type
, type
->length ());
2765 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2768 if (noside
== EVAL_NORMAL
)
2770 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2771 EVAL_AVOID_SIDE_EFFECTS
);
2772 struct type
*type
= check_typedef (val
->type ());
2773 if (type
->code () == TYPE_CODE_ARRAY
)
2775 type
= check_typedef (type
->target_type ());
2776 if (type
->code () == TYPE_CODE_ARRAY
)
2778 type
= type
->index_type ();
2779 /* Only re-evaluate the right hand side if the resulting type
2780 is a variable length type. */
2781 if (type
->bounds ()->flag_bound_evaluated
)
2783 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2784 /* FIXME: This should be size_t. */
2785 struct type
*size_type
2786 = builtin_type (exp
->gdbarch
)->builtin_int
;
2787 return value_from_longest
2788 (size_type
, (LONGEST
) val
->type ()->length ());
2794 return operation::evaluate_for_sizeof (exp
, noside
);
2798 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2801 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2802 EVAL_AVOID_SIDE_EFFECTS
);
2803 struct type
*type
= check_typedef (val
->type ());
2804 if (!type
->is_pointer_or_reference ()
2805 && type
->code () != TYPE_CODE_ARRAY
)
2806 error (_("Attempt to take contents of a non-pointer value."));
2807 type
= type
->target_type ();
2808 if (is_dynamic_type (type
))
2809 type
= value_ind (val
)->type ();
2810 /* FIXME: This should be size_t. */
2811 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2812 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2816 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2819 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2823 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2826 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2827 EVAL_AVOID_SIDE_EFFECTS
);
2828 return evaluate_subexp_for_sizeof_base (exp
, typeval
->type ());
2832 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2835 struct type
*type
= std::get
<0> (m_storage
).symbol
->type ();
2836 if (is_dynamic_type (type
))
2838 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2839 type
= val
->type ();
2840 if (type
->code () == TYPE_CODE_ARRAY
)
2842 /* FIXME: This should be size_t. */
2843 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2844 if (type_not_allocated (type
) || type_not_associated (type
))
2845 return value::zero (size_type
, not_lval
);
2846 else if (is_dynamic_type (type
->index_type ())
2847 && !type
->bounds ()->high
.is_available ())
2848 return value::allocate_optimized_out (size_type
);
2851 return evaluate_subexp_for_sizeof_base (exp
, type
);
2855 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2856 struct expression
*exp
,
2859 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2860 return value::zero (to_type
, not_lval
);
2862 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2863 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2865 val
= value_cast (to_type
, val
);
2867 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2868 if (val
->lval () == lval_memory
)
2872 val
->set_lval (not_lval
);
2878 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2879 struct expression
*exp
,
2882 value
*val
= evaluate_var_value (noside
,
2883 std::get
<0> (m_storage
).block
,
2884 std::get
<0> (m_storage
).symbol
);
2886 val
= value_cast (to_type
, val
);
2888 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2889 if (val
->lval () == lval_memory
)
2893 val
->set_lval (not_lval
);
2900 /* Parse a type expression in the string [P..P+LENGTH). */
2903 parse_and_eval_type (const char *p
, int length
)
2905 char *tmp
= (char *) alloca (length
+ 4);
2908 memcpy (tmp
+ 1, p
, length
);
2909 tmp
[length
+ 1] = ')';
2910 tmp
[length
+ 2] = '0';
2911 tmp
[length
+ 3] = '\0';
2912 expression_up expr
= parse_expression (tmp
);
2913 expr::unop_cast_operation
*op
2914 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2916 error (_("Internal error in eval_type."));
2917 return op
->get_type ();