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 (expr
->evaluate ());
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 (expr
->evaluate ());
70 parse_and_eval (const char *exp
)
72 expression_up expr
= parse_expression (exp
);
74 return expr
->evaluate ();
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 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 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
= retval
->non_lval ();
119 /* Evaluate an expression, avoiding all memory references
120 and getting a value whose type alone is correct. */
123 evaluate_type (struct expression
*exp
)
125 return exp
->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS
);
128 /* Find the current value of a watchpoint on EXP. Return the value in
129 *VALP and *RESULTP and the chain of intermediate and final values
130 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
133 If PRESERVE_ERRORS is true, then exceptions are passed through.
134 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
135 occurs while evaluating the expression, *RESULTP will be set to
136 NULL. *RESULTP may be a lazy value, if the result could not be
137 read from memory. It is used to determine whether a value is
138 user-specified (we should watch the whole value) or intermediate
139 (we should watch only the bit used to locate the final value).
141 If the final value, or any intermediate value, could not be read
142 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
143 set to any referenced values. *VALP will never be a lazy value.
144 This is the value which we store in struct breakpoint.
146 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
147 released from the value chain. If VAL_CHAIN is NULL, all generated
148 values will be left on the value chain. */
151 fetch_subexp_value (struct expression
*exp
,
153 struct value
**valp
, struct value
**resultp
,
154 std::vector
<value_ref_ptr
> *val_chain
,
155 bool preserve_errors
)
157 struct value
*mark
, *new_mark
, *result
;
165 /* Evaluate the expression. */
166 mark
= value_mark ();
171 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
173 catch (const gdb_exception
&ex
)
175 /* Ignore memory errors if we want watchpoints pointing at
176 inaccessible memory to still be created; otherwise, throw the
177 error to some higher catcher. */
181 if (!preserve_errors
)
190 new_mark
= value_mark ();
191 if (mark
== new_mark
)
196 /* Make sure it's not lazy, so that after the target stops again we
197 have a non-lazy previous value to compare with. */
200 if (!result
->lazy ())
207 result
->fetch_lazy ();
210 catch (const gdb_exception_error
&except
)
218 /* Return the chain of intermediate values. We use this to
219 decide which addresses to watch. */
220 *val_chain
= value_release_to_mark (mark
);
224 /* Promote value ARG1 as appropriate before performing a unary operation
226 If the result is not appropriate for any particular language then it
227 needs to patch this function. */
230 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
235 *arg1
= coerce_ref (*arg1
);
236 type1
= check_typedef ((*arg1
)->type ());
238 if (is_integral_type (type1
))
240 switch (language
->la_language
)
243 /* Perform integral promotion for ANSI C/C++.
244 If not appropriate for any particular language
245 it needs to modify this function. */
247 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
249 if (type1
->length () < builtin_int
->length ())
250 *arg1
= value_cast (builtin_int
, *arg1
);
257 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
258 operation on those two operands.
259 If the result is not appropriate for any particular language then it
260 needs to patch this function. */
263 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
264 struct value
**arg1
, struct value
**arg2
)
266 struct type
*promoted_type
= NULL
;
270 *arg1
= coerce_ref (*arg1
);
271 *arg2
= coerce_ref (*arg2
);
273 type1
= check_typedef ((*arg1
)->type ());
274 type2
= check_typedef ((*arg2
)->type ());
276 if ((type1
->code () != TYPE_CODE_FLT
277 && type1
->code () != TYPE_CODE_DECFLOAT
278 && !is_integral_type (type1
))
279 || (type2
->code () != TYPE_CODE_FLT
280 && type2
->code () != TYPE_CODE_DECFLOAT
281 && !is_integral_type (type2
)))
284 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
287 if (type1
->code () == TYPE_CODE_DECFLOAT
288 || type2
->code () == TYPE_CODE_DECFLOAT
)
290 /* No promotion required. */
292 else if (type1
->code () == TYPE_CODE_FLT
293 || type2
->code () == TYPE_CODE_FLT
)
295 switch (language
->la_language
)
301 case language_opencl
:
302 /* No promotion required. */
306 /* For other languages the result type is unchanged from gdb
307 version 6.7 for backward compatibility.
308 If either arg was long double, make sure that value is also long
309 double. Otherwise use double. */
310 if (type1
->length () * 8 > gdbarch_double_bit (gdbarch
)
311 || type2
->length () * 8 > gdbarch_double_bit (gdbarch
))
312 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
314 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
318 else if (type1
->code () == TYPE_CODE_BOOL
319 && type2
->code () == TYPE_CODE_BOOL
)
321 /* No promotion required. */
324 /* Integral operations here. */
325 /* FIXME: Also mixed integral/booleans, with result an integer. */
327 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
328 unsigned int promoted_len1
= type1
->length ();
329 unsigned int promoted_len2
= type2
->length ();
330 int is_unsigned1
= type1
->is_unsigned ();
331 int is_unsigned2
= type2
->is_unsigned ();
332 unsigned int result_len
;
333 int unsigned_operation
;
335 /* Determine type length and signedness after promotion for
337 if (promoted_len1
< builtin
->builtin_int
->length ())
340 promoted_len1
= builtin
->builtin_int
->length ();
342 if (promoted_len2
< builtin
->builtin_int
->length ())
345 promoted_len2
= builtin
->builtin_int
->length ();
348 if (promoted_len1
> promoted_len2
)
350 unsigned_operation
= is_unsigned1
;
351 result_len
= promoted_len1
;
353 else if (promoted_len2
> promoted_len1
)
355 unsigned_operation
= is_unsigned2
;
356 result_len
= promoted_len2
;
360 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
361 result_len
= promoted_len1
;
364 switch (language
->la_language
)
366 case language_opencl
:
368 <= lookup_signed_typename (language
, "int")->length())
372 ? lookup_unsigned_typename (language
, "int")
373 : lookup_signed_typename (language
, "int"));
376 <= lookup_signed_typename (language
, "long")->length())
380 ? lookup_unsigned_typename (language
, "long")
381 : lookup_signed_typename (language
,"long"));
385 if (result_len
<= builtin
->builtin_int
->length ())
387 promoted_type
= (unsigned_operation
388 ? builtin
->builtin_unsigned_int
389 : builtin
->builtin_int
);
391 else if (result_len
<= builtin
->builtin_long
->length ())
393 promoted_type
= (unsigned_operation
394 ? builtin
->builtin_unsigned_long
395 : builtin
->builtin_long
);
397 else if (result_len
<= builtin
->builtin_long_long
->length ())
399 promoted_type
= (unsigned_operation
400 ? builtin
->builtin_unsigned_long_long
401 : builtin
->builtin_long_long
);
405 promoted_type
= (unsigned_operation
406 ? builtin
->builtin_uint128
407 : builtin
->builtin_int128
);
415 /* Promote both operands to common type. */
416 *arg1
= value_cast (promoted_type
, *arg1
);
417 *arg2
= value_cast (promoted_type
, *arg2
);
422 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
424 type
= check_typedef (type
);
425 if (TYPE_IS_REFERENCE (type
))
426 type
= type
->target_type ();
428 switch (type
->code ())
434 case TYPE_CODE_ARRAY
:
435 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
442 /* Represents a fake method with the given parameter types. This is
443 used by the parser to construct a temporary "expected" type for
444 method overload resolution. FLAGS is used as instance flags of the
445 new type, in order to be able to make the new type represent a
446 const/volatile overload. */
451 fake_method (type_instance_flags flags
,
452 int num_types
, struct type
**param_types
);
455 /* The constructed type. */
456 struct type
*type () { return &m_type
; }
459 struct type m_type
{};
460 main_type m_main_type
{};
463 fake_method::fake_method (type_instance_flags flags
,
464 int num_types
, struct type
**param_types
)
466 struct type
*type
= &m_type
;
468 TYPE_MAIN_TYPE (type
) = &m_main_type
;
469 type
->set_length (1);
470 type
->set_code (TYPE_CODE_METHOD
);
471 TYPE_CHAIN (type
) = type
;
472 type
->set_instance_flags (flags
);
475 if (param_types
[num_types
- 1] == NULL
)
478 type
->set_has_varargs (true);
480 else if (check_typedef (param_types
[num_types
- 1])->code ()
484 /* Caller should have ensured this. */
485 gdb_assert (num_types
== 0);
486 type
->set_is_prototyped (true);
490 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
491 neither an objfile nor a gdbarch. As a result we must manually
492 allocate memory for auxiliary fields, and free the memory ourselves
493 when we are done with it. */
494 type
->set_num_fields (num_types
);
496 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
498 while (num_types
-- > 0)
499 type
->field (num_types
).set_type (param_types
[num_types
]);
502 fake_method::~fake_method ()
504 xfree (m_type
.fields ());
511 type_instance_operation::evaluate (struct type
*expect_type
,
512 struct expression
*exp
,
515 type_instance_flags flags
= std::get
<0> (m_storage
);
516 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
518 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
519 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
525 /* Helper for evaluating an OP_VAR_VALUE. */
528 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
530 /* JYG: We used to just return value::zero of the symbol type if
531 we're asked to avoid side effects. Otherwise we return
532 value_of_variable (...). However I'm not sure if
533 value_of_variable () has any side effect. We need a full value
534 object returned here for whatis_exp () to call evaluate_type ()
535 and then pass the full value to value_rtti_target_type () if we
536 are dealing with a pointer or reference to a base class and print
539 struct value
*ret
= NULL
;
543 ret
= value_of_variable (var
, blk
);
546 catch (const gdb_exception_error
&except
)
548 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
551 ret
= value::zero (var
->type (), not_lval
);
562 var_value_operation::evaluate (struct type
*expect_type
,
563 struct expression
*exp
,
566 symbol
*var
= std::get
<0> (m_storage
).symbol
;
567 if (var
->type ()->code () == TYPE_CODE_ERROR
)
568 error_unknown_type (var
->print_name ());
569 return evaluate_var_value (noside
, std::get
<0> (m_storage
).block
, var
);
572 } /* namespace expr */
574 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
577 evaluate_var_msym_value (enum noside noside
,
578 struct objfile
*objfile
, minimal_symbol
*msymbol
)
581 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
583 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
584 return value::zero (the_type
, not_lval
);
586 return value_at_lazy (the_type
, address
);
589 /* See expression.h. */
592 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
594 gdb::array_view
<value
*> argvec
,
595 const char *function_name
,
596 type
*default_return_type
)
599 error (_("Cannot evaluate function -- may be inlined"));
600 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
602 /* If the return type doesn't look like a function type,
603 call an error. This can happen if somebody tries to turn
604 a variable into a function call. */
606 type
*ftype
= callee
->type ();
608 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
610 /* We don't know anything about what the internal
611 function might return, but we have to return
613 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
616 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
618 type
*return_type
= callee
->result_type_of_xmethod (argvec
);
620 if (return_type
== NULL
)
621 error (_("Xmethod is missing return type."));
622 return value::zero (return_type
, not_lval
);
624 else if (ftype
->code () == TYPE_CODE_FUNC
625 || ftype
->code () == TYPE_CODE_METHOD
)
627 if (ftype
->is_gnu_ifunc ())
629 CORE_ADDR address
= callee
->address ();
630 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
632 if (resolved_type
!= NULL
)
633 ftype
= resolved_type
;
636 type
*return_type
= ftype
->target_type ();
638 if (return_type
== NULL
)
639 return_type
= default_return_type
;
641 if (return_type
== NULL
)
642 error_call_unknown_return_type (function_name
);
644 return value::allocate (return_type
);
647 error (_("Expression of type other than "
648 "\"Function returning ...\" used as function"));
650 switch (callee
->type ()->code ())
652 case TYPE_CODE_INTERNAL_FUNCTION
:
653 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
654 callee
, argvec
.size (), argvec
.data ());
655 case TYPE_CODE_XMETHOD
:
656 return callee
->call_xmethod (argvec
);
658 return call_function_by_hand (callee
, default_return_type
, argvec
);
666 operation::evaluate_funcall (struct type
*expect_type
,
667 struct expression
*exp
,
669 const char *function_name
,
670 const std::vector
<operation_up
> &args
)
672 std::vector
<value
*> vals (args
.size ());
674 value
*callee
= evaluate_with_coercion (exp
, noside
);
675 struct type
*type
= callee
->type ();
676 if (type
->code () == TYPE_CODE_PTR
)
677 type
= type
->target_type ();
678 for (int i
= 0; i
< args
.size (); ++i
)
680 if (i
< type
->num_fields ())
681 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
683 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
686 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
687 function_name
, expect_type
);
691 var_value_operation::evaluate_funcall (struct type
*expect_type
,
692 struct expression
*exp
,
694 const std::vector
<operation_up
> &args
)
696 if (!overload_resolution
697 || exp
->language_defn
->la_language
!= language_cplus
)
698 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
700 std::vector
<value
*> argvec (args
.size ());
701 for (int i
= 0; i
< args
.size (); ++i
)
702 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
705 find_overload_match (argvec
, NULL
, NON_METHOD
,
706 NULL
, std::get
<0> (m_storage
).symbol
,
707 NULL
, &symp
, NULL
, 0, noside
);
709 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
710 error_unknown_type (symp
->print_name ());
711 value
*callee
= evaluate_var_value (noside
, std::get
<0> (m_storage
).block
,
714 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
715 nullptr, expect_type
);
719 scope_operation::evaluate_funcall (struct type
*expect_type
,
720 struct expression
*exp
,
722 const std::vector
<operation_up
> &args
)
724 if (!overload_resolution
725 || exp
->language_defn
->la_language
!= language_cplus
)
726 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
728 /* Unpack it locally so we can properly handle overload
730 const std::string
&name
= std::get
<1> (m_storage
);
731 struct type
*type
= std::get
<0> (m_storage
);
733 symbol
*function
= NULL
;
734 const char *function_name
= NULL
;
735 std::vector
<value
*> argvec (1 + args
.size ());
736 if (type
->code () == TYPE_CODE_NAMESPACE
)
738 function
= cp_lookup_symbol_namespace (type
->name (),
740 get_selected_block (0),
742 if (function
== NULL
)
743 error (_("No symbol \"%s\" in namespace \"%s\"."),
744 name
.c_str (), type
->name ());
748 gdb_assert (type
->code () == TYPE_CODE_STRUCT
749 || type
->code () == TYPE_CODE_UNION
);
750 function_name
= name
.c_str ();
752 /* We need a properly typed value for method lookup. */
753 argvec
[0] = value::zero (type
, lval_memory
);
756 for (int i
= 0; i
< args
.size (); ++i
)
757 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
758 gdb::array_view
<value
*> arg_view
= argvec
;
760 value
*callee
= nullptr;
761 if (function_name
!= nullptr)
765 find_overload_match (arg_view
, function_name
, METHOD
,
766 &argvec
[0], nullptr, &callee
, nullptr,
767 &static_memfuncp
, 0, noside
);
768 if (!static_memfuncp
)
770 /* For the time being, we don't handle this. */
771 error (_("Call to overloaded function %s requires "
776 arg_view
= arg_view
.slice (1);
781 arg_view
= arg_view
.slice (1);
782 find_overload_match (arg_view
, nullptr,
783 NON_METHOD
, nullptr, function
,
784 nullptr, &symp
, nullptr, 1, noside
);
785 callee
= value_of_variable (symp
, get_selected_block (0));
788 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
789 nullptr, expect_type
);
793 structop_member_base::evaluate_funcall (struct type
*expect_type
,
794 struct expression
*exp
,
796 const std::vector
<operation_up
> &args
)
798 /* First, evaluate the structure into lhs. */
800 if (opcode () == STRUCTOP_MEMBER
)
801 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
803 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
805 std::vector
<value
*> vals (args
.size () + 1);
806 gdb::array_view
<value
*> val_view
= vals
;
807 /* If the function is a virtual function, then the aggregate
808 value (providing the structure) plays its part by providing
809 the vtable. Otherwise, it is just along for the ride: call
810 the function directly. */
811 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
814 type
*a1_type
= check_typedef (rhs
->type ());
815 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
817 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
818 callee
= value::zero (a1_type
->target_type (), not_lval
);
820 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
824 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
826 struct type
*type_ptr
827 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
828 struct type
*target_type_ptr
829 = lookup_pointer_type (a1_type
->target_type ());
831 /* Now, convert this value to an address. */
832 lhs
= value_cast (type_ptr
, lhs
);
834 long mem_offset
= value_as_long (rhs
);
836 callee
= value_from_pointer (target_type_ptr
,
837 value_as_long (lhs
) + mem_offset
);
838 callee
= value_ind (callee
);
840 val_view
= val_view
.slice (1);
843 error (_("Non-pointer-to-member value used in pointer-to-member "
846 for (int i
= 0; i
< args
.size (); ++i
)
847 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
849 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
850 nullptr, expect_type
);
855 structop_base_operation::evaluate_funcall
856 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
857 const std::vector
<operation_up
> &args
)
859 /* Allocate space for the function call arguments, Including space for a
860 `this' pointer at the start. */
861 std::vector
<value
*> vals (args
.size () + 1);
862 /* First, evaluate the structure into vals[0]. */
863 enum exp_opcode op
= opcode ();
864 if (op
== STRUCTOP_STRUCT
)
866 /* If v is a variable in a register, and the user types
867 v.method (), this will produce an error, because v has no
870 A possible way around this would be to allocate a copy of
871 the variable on the stack, copy in the contents, call the
872 function, and copy out the contents. I.e. convert this
873 from call by reference to call by copy-return (or
874 whatever it's called). However, this does not work
875 because it is not the same: the method being called could
876 stash a copy of the address, and then future uses through
877 that address (after the method returns) would be expected
878 to use the variable itself, not some copy of it. */
879 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
883 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
884 /* Check to see if the operator '->' has been overloaded.
885 If the operator has been overloaded replace vals[0] with the
886 value returned by the custom operator and continue
888 while (unop_user_defined_p (op
, vals
[0]))
890 struct value
*value
= nullptr;
893 value
= value_x_unop (vals
[0], op
, noside
);
895 catch (const gdb_exception_error
&except
)
897 if (except
.error
== NOT_FOUND_ERROR
)
907 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
908 pointer we placed into vals[0]. */
909 for (int i
= 0; i
< args
.size (); ++i
)
910 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
912 /* The array view includes the `this' pointer. */
913 gdb::array_view
<value
*> arg_view (vals
);
917 const char *tstr
= std::get
<1> (m_storage
).c_str ();
918 if (overload_resolution
919 && exp
->language_defn
->la_language
== language_cplus
)
921 /* Language is C++, do some overload resolution before
923 value
*val0
= vals
[0];
924 find_overload_match (arg_view
, tstr
, METHOD
,
925 &val0
, nullptr, &callee
, nullptr,
926 &static_memfuncp
, 0, noside
);
930 /* Non-C++ case -- or no overload resolution. */
932 struct value
*temp
= vals
[0];
934 callee
= value_struct_elt (&temp
, arg_view
, tstr
,
936 op
== STRUCTOP_STRUCT
937 ? "structure" : "structure pointer");
938 /* value_struct_elt updates temp with the correct value of the
939 ``this'' pointer if necessary, so modify it to reflect any
941 vals
[0] = value_from_longest (lookup_pointer_type (temp
->type ()),
943 + temp
->embedded_offset ());
946 /* Take out `this' if needed. */
948 arg_view
= arg_view
.slice (1);
950 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
951 nullptr, expect_type
);
954 /* Helper for structop_base_operation::complete which recursively adds
955 field and method names from TYPE, a struct or union type, to the
956 OUTPUT list. PREFIX is prepended to each result. */
959 add_struct_fields (struct type
*type
, completion_list
&output
,
960 const char *fieldname
, int namelen
, const char *prefix
)
963 int computed_type_name
= 0;
964 const char *type_name
= NULL
;
966 type
= check_typedef (type
);
967 for (i
= 0; i
< type
->num_fields (); ++i
)
969 if (i
< TYPE_N_BASECLASSES (type
))
970 add_struct_fields (TYPE_BASECLASS (type
, i
),
971 output
, fieldname
, namelen
, prefix
);
972 else if (type
->field (i
).name ())
974 if (type
->field (i
).name ()[0] != '\0')
976 if (! strncmp (type
->field (i
).name (),
978 output
.emplace_back (concat (prefix
, type
->field (i
).name (),
981 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
)
983 /* Recurse into anonymous unions. */
984 add_struct_fields (type
->field (i
).type (),
985 output
, fieldname
, namelen
, prefix
);
990 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
992 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
994 if (name
&& ! strncmp (name
, fieldname
, namelen
))
996 if (!computed_type_name
)
998 type_name
= type
->name ();
999 computed_type_name
= 1;
1001 /* Omit constructors from the completion list. */
1002 if (!type_name
|| strcmp (type_name
, name
))
1003 output
.emplace_back (concat (prefix
, name
, nullptr));
1011 structop_base_operation::complete (struct expression
*exp
,
1012 completion_tracker
&tracker
,
1015 const std::string
&fieldname
= std::get
<1> (m_storage
);
1017 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1018 EVAL_AVOID_SIDE_EFFECTS
);
1019 struct type
*type
= lhs
->type ();
1022 type
= check_typedef (type
);
1023 if (!type
->is_pointer_or_reference ())
1025 type
= type
->target_type ();
1028 if (type
->code () == TYPE_CODE_UNION
1029 || type
->code () == TYPE_CODE_STRUCT
)
1031 completion_list result
;
1033 add_struct_fields (type
, result
, fieldname
.c_str (),
1034 fieldname
.length (), prefix
);
1035 tracker
.add_completions (std::move (result
));
1042 } /* namespace expr */
1044 /* Return true if type is integral or reference to integral */
1047 is_integral_or_integral_reference (struct type
*type
)
1049 if (is_integral_type (type
))
1052 type
= check_typedef (type
);
1053 return (type
!= nullptr
1054 && TYPE_IS_REFERENCE (type
)
1055 && is_integral_type (type
->target_type ()));
1058 /* Helper function that implements the body of OP_SCOPE. */
1061 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
1063 struct type
*type
, const char *string
)
1065 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
1068 error (_("There is no field named %s"), string
);
1072 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1075 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1076 enum noside noside
, symbol
*sym
)
1078 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1079 return value::zero (sym
->type (), not_lval
);
1081 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1082 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1083 error (_("Symbol \"%s\" does not have any specific entry value"),
1084 sym
->print_name ());
1086 frame_info_ptr frame
= get_selected_frame (NULL
);
1087 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1090 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1093 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1094 enum noside noside
, bool outermost_p
,
1095 bound_minimal_symbol msymbol
)
1097 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1100 struct type
*type
= val
->type ();
1101 if (type
->code () == TYPE_CODE_ERROR
1102 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1103 error_unknown_type (msymbol
.minsym
->print_name ());
1107 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1110 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1112 value
*func
, const char *var
)
1114 CORE_ADDR addr
= func
->address ();
1115 const block
*blk
= block_for_pc (addr
);
1116 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1117 if (sym
.symbol
== NULL
)
1118 error (_("No symbol \"%s\" in specified context."), var
);
1119 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1122 /* Helper function that implements the body of OP_REGISTER. */
1125 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1126 enum noside noside
, const char *name
)
1131 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1132 name
, strlen (name
));
1134 error (_("Register $%s not available."), name
);
1136 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1137 a value with the appropriate register type. Unfortunately,
1138 we don't have easy access to the type of user registers.
1139 So for these registers, we fetch the register value regardless
1140 of the evaluation mode. */
1141 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1142 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1143 val
= value::zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1145 val
= value_of_register (regno
, get_selected_frame (NULL
));
1147 error (_("Value of register %s not available."), name
);
1156 string_operation::evaluate (struct type
*expect_type
,
1157 struct expression
*exp
,
1160 const std::string
&str
= std::get
<0> (m_storage
);
1161 struct type
*type
= language_string_char_type (exp
->language_defn
,
1163 return value_string (str
.c_str (), str
.size (), type
);
1166 } /* namespace expr */
1168 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1171 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1175 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1176 return value_from_longest (selector_type
,
1177 lookup_child_selector (exp
->gdbarch
, sel
));
1180 /* A helper function for TERNOP_SLICE. */
1183 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1185 struct value
*array
, struct value
*low
, struct value
*upper
)
1187 int lowbound
= value_as_long (low
);
1188 int upperbound
= value_as_long (upper
);
1189 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1192 /* A helper function for STRUCTOP_STRUCT. */
1195 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1197 struct value
*arg1
, const char *string
)
1199 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1201 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1202 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1206 /* A helper function for STRUCTOP_PTR. */
1209 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1211 struct value
*arg1
, const char *string
)
1213 /* Check to see if operator '->' has been overloaded. If so replace
1214 arg1 with the value returned by evaluating operator->(). */
1215 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1217 struct value
*value
= NULL
;
1220 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1223 catch (const gdb_exception_error
&except
)
1225 if (except
.error
== NOT_FOUND_ERROR
)
1234 /* JYG: if print object is on we need to replace the base type
1235 with rtti type in order to continue on with successful
1236 lookup of member / method only available in the rtti type. */
1238 struct type
*arg_type
= arg1
->type ();
1239 struct type
*real_type
;
1240 int full
, using_enc
;
1242 struct value_print_options opts
;
1244 get_user_print_options (&opts
);
1245 if (opts
.objectprint
&& arg_type
->target_type ()
1246 && (arg_type
->target_type ()->code () == TYPE_CODE_STRUCT
))
1248 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1251 arg1
= value_cast (real_type
, arg1
);
1255 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1256 NULL
, "structure pointer");
1257 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1258 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1262 /* A helper function for STRUCTOP_MEMBER. */
1265 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1267 struct value
*arg1
, struct value
*arg2
)
1272 struct type
*type
= check_typedef (arg2
->type ());
1273 switch (type
->code ())
1275 case TYPE_CODE_METHODPTR
:
1276 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1277 return value::zero (type
->target_type (), not_lval
);
1280 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1281 gdb_assert (arg2
->type ()->code () == TYPE_CODE_PTR
);
1282 return value_ind (arg2
);
1285 case TYPE_CODE_MEMBERPTR
:
1286 /* Now, convert these values to an address. */
1287 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_PTR
)
1288 arg1
= value_addr (arg1
);
1289 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1292 mem_offset
= value_as_long (arg2
);
1294 arg3
= value_from_pointer (lookup_pointer_type (type
->target_type ()),
1295 value_as_long (arg1
) + mem_offset
);
1296 return value_ind (arg3
);
1299 error (_("non-pointer-to-member value used "
1300 "in pointer-to-member construct"));
1304 /* A helper function for BINOP_ADD. */
1307 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1309 struct value
*arg1
, struct value
*arg2
)
1311 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1312 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1313 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1314 && is_integral_or_integral_reference (arg2
->type ()))
1315 return value_ptradd (arg1
, value_as_long (arg2
));
1316 else if (ptrmath_type_p (exp
->language_defn
, arg2
->type ())
1317 && is_integral_or_integral_reference (arg1
->type ()))
1318 return value_ptradd (arg2
, value_as_long (arg1
));
1321 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1322 return value_binop (arg1
, arg2
, BINOP_ADD
);
1326 /* A helper function for BINOP_SUB. */
1329 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1331 struct value
*arg1
, struct value
*arg2
)
1333 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1334 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1335 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1336 && ptrmath_type_p (exp
->language_defn
, arg2
->type ()))
1338 /* FIXME -- should be ptrdiff_t */
1339 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1340 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1342 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1343 && is_integral_or_integral_reference (arg2
->type ()))
1344 return value_ptradd (arg1
, - value_as_long (arg2
));
1347 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1348 return value_binop (arg1
, arg2
, BINOP_SUB
);
1352 /* Helper function for several different binary operations. */
1355 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1356 enum noside noside
, enum exp_opcode op
,
1357 struct value
*arg1
, struct value
*arg2
)
1359 if (binop_user_defined_p (op
, arg1
, arg2
))
1360 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1363 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1364 fudge arg2 to avoid division-by-zero, the caller is
1365 (theoretically) only looking for the type of the result. */
1366 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1367 /* ??? Do we really want to test for BINOP_MOD here?
1368 The implementation of value_binop gives it a well-defined
1371 || op
== BINOP_INTDIV
1374 && value_logical_not (arg2
))
1376 struct value
*v_one
;
1378 v_one
= value_one (arg2
->type ());
1379 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1380 return value_binop (arg1
, v_one
, op
);
1384 /* For shift and integer exponentiation operations,
1385 only promote the first argument. */
1386 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1387 && is_integral_type (arg2
->type ()))
1388 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1390 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1392 return value_binop (arg1
, arg2
, op
);
1397 /* A helper function for BINOP_SUBSCRIPT. */
1400 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1401 enum noside noside
, enum exp_opcode op
,
1402 struct value
*arg1
, struct value
*arg2
)
1404 if (binop_user_defined_p (op
, arg1
, arg2
))
1405 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1408 /* If the user attempts to subscript something that is not an
1409 array or pointer type (like a plain int variable for example),
1410 then report this as an error. */
1412 arg1
= coerce_ref (arg1
);
1413 struct type
*type
= check_typedef (arg1
->type ());
1414 if (type
->code () != TYPE_CODE_ARRAY
1415 && type
->code () != TYPE_CODE_PTR
)
1418 error (_("cannot subscript something of type `%s'"),
1421 error (_("cannot subscript requested type"));
1424 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1425 return value::zero (type
->target_type (), arg1
->lval ());
1427 return value_subscript (arg1
, value_as_long (arg2
));
1431 /* A helper function for BINOP_EQUAL. */
1434 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1435 enum noside noside
, enum exp_opcode op
,
1436 struct value
*arg1
, struct value
*arg2
)
1438 if (binop_user_defined_p (op
, arg1
, arg2
))
1440 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1444 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1445 int tem
= value_equal (arg1
, arg2
);
1446 struct type
*type
= language_bool_type (exp
->language_defn
,
1448 return value_from_longest (type
, (LONGEST
) tem
);
1452 /* A helper function for BINOP_NOTEQUAL. */
1455 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1456 enum noside noside
, enum exp_opcode op
,
1457 struct value
*arg1
, struct value
*arg2
)
1459 if (binop_user_defined_p (op
, arg1
, arg2
))
1461 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1465 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1466 int tem
= value_equal (arg1
, arg2
);
1467 struct type
*type
= language_bool_type (exp
->language_defn
,
1469 return value_from_longest (type
, (LONGEST
) ! tem
);
1473 /* A helper function for BINOP_LESS. */
1476 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1477 enum noside noside
, enum exp_opcode op
,
1478 struct value
*arg1
, struct value
*arg2
)
1480 if (binop_user_defined_p (op
, arg1
, arg2
))
1482 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1486 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1487 int tem
= value_less (arg1
, arg2
);
1488 struct type
*type
= language_bool_type (exp
->language_defn
,
1490 return value_from_longest (type
, (LONGEST
) tem
);
1494 /* A helper function for BINOP_GTR. */
1497 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1498 enum noside noside
, enum exp_opcode op
,
1499 struct value
*arg1
, struct value
*arg2
)
1501 if (binop_user_defined_p (op
, arg1
, arg2
))
1503 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1507 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1508 int tem
= value_less (arg2
, arg1
);
1509 struct type
*type
= language_bool_type (exp
->language_defn
,
1511 return value_from_longest (type
, (LONGEST
) tem
);
1515 /* A helper function for BINOP_GEQ. */
1518 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1519 enum noside noside
, enum exp_opcode op
,
1520 struct value
*arg1
, struct value
*arg2
)
1522 if (binop_user_defined_p (op
, arg1
, arg2
))
1524 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1528 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1529 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1530 struct type
*type
= language_bool_type (exp
->language_defn
,
1532 return value_from_longest (type
, (LONGEST
) tem
);
1536 /* A helper function for BINOP_LEQ. */
1539 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1540 enum noside noside
, enum exp_opcode op
,
1541 struct value
*arg1
, struct value
*arg2
)
1543 if (binop_user_defined_p (op
, arg1
, arg2
))
1545 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1549 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1550 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1551 struct type
*type
= language_bool_type (exp
->language_defn
,
1553 return value_from_longest (type
, (LONGEST
) tem
);
1557 /* A helper function for BINOP_REPEAT. */
1560 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1561 enum noside noside
, enum exp_opcode op
,
1562 struct value
*arg1
, struct value
*arg2
)
1564 struct type
*type
= check_typedef (arg2
->type ());
1565 if (type
->code () != TYPE_CODE_INT
1566 && type
->code () != TYPE_CODE_ENUM
)
1567 error (_("Non-integral right operand for \"@\" operator."));
1568 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1570 return allocate_repeat_value (arg1
->type (),
1571 longest_to_int (value_as_long (arg2
)));
1574 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1577 /* A helper function for UNOP_PLUS. */
1580 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1581 enum noside noside
, enum exp_opcode op
,
1584 if (unop_user_defined_p (op
, arg1
))
1585 return value_x_unop (arg1
, op
, noside
);
1588 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1589 return value_pos (arg1
);
1593 /* A helper function for UNOP_NEG. */
1596 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1597 enum noside noside
, enum exp_opcode op
,
1600 if (unop_user_defined_p (op
, arg1
))
1601 return value_x_unop (arg1
, op
, noside
);
1604 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1605 return value_neg (arg1
);
1609 /* A helper function for UNOP_COMPLEMENT. */
1612 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1613 enum noside noside
, enum exp_opcode op
,
1616 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1617 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1620 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1621 return value_complement (arg1
);
1625 /* A helper function for UNOP_LOGICAL_NOT. */
1628 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1629 enum noside noside
, enum exp_opcode op
,
1632 if (unop_user_defined_p (op
, arg1
))
1633 return value_x_unop (arg1
, op
, noside
);
1636 struct type
*type
= language_bool_type (exp
->language_defn
,
1638 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1642 /* A helper function for UNOP_IND. */
1645 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1649 struct type
*type
= check_typedef (arg1
->type ());
1650 if (type
->code () == TYPE_CODE_METHODPTR
1651 || type
->code () == TYPE_CODE_MEMBERPTR
)
1652 error (_("Attempt to dereference pointer "
1653 "to member without an object"));
1654 if (unop_user_defined_p (UNOP_IND
, arg1
))
1655 return value_x_unop (arg1
, UNOP_IND
, noside
);
1656 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1658 type
= check_typedef (arg1
->type ());
1660 /* If the type pointed to is dynamic then in order to resolve the
1661 dynamic properties we must actually dereference the pointer.
1662 There is a risk that this dereference will have side-effects
1663 in the inferior, but being able to print accurate type
1664 information seems worth the risk. */
1665 if (!type
->is_pointer_or_reference ()
1666 || !is_dynamic_type (type
->target_type ()))
1668 if (type
->is_pointer_or_reference ()
1669 /* In C you can dereference an array to get the 1st elt. */
1670 || type
->code () == TYPE_CODE_ARRAY
)
1671 return value::zero (type
->target_type (),
1673 else if (type
->code () == TYPE_CODE_INT
)
1674 /* GDB allows dereferencing an int. */
1675 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1678 error (_("Attempt to take contents of a non-pointer value."));
1682 /* Allow * on an integer so we can cast it to whatever we want.
1683 This returns an int, which seems like the most C-like thing to
1684 do. "long long" variables are rare enough that
1685 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1686 if (type
->code () == TYPE_CODE_INT
)
1687 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1688 (CORE_ADDR
) value_as_address (arg1
));
1689 return value_ind (arg1
);
1692 /* A helper function for UNOP_ALIGNOF. */
1695 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1699 struct type
*type
= arg1
->type ();
1700 /* FIXME: This should be size_t. */
1701 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1702 ULONGEST align
= type_align (type
);
1704 error (_("could not determine alignment of type"));
1705 return value_from_longest (size_type
, align
);
1708 /* A helper function for UNOP_MEMVAL. */
1711 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1713 struct value
*arg1
, struct type
*type
)
1715 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1716 return value::zero (type
, lval_memory
);
1718 return value_at_lazy (type
, value_as_address (arg1
));
1721 /* A helper function for UNOP_PREINCREMENT. */
1724 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1725 enum noside noside
, enum exp_opcode op
,
1728 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1730 else if (unop_user_defined_p (op
, arg1
))
1732 return value_x_unop (arg1
, op
, noside
);
1737 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1738 arg2
= value_ptradd (arg1
, 1);
1741 struct value
*tmp
= arg1
;
1743 arg2
= value_one (arg1
->type ());
1744 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1745 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1748 return value_assign (arg1
, arg2
);
1752 /* A helper function for UNOP_PREDECREMENT. */
1755 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1756 enum noside noside
, enum exp_opcode op
,
1759 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1761 else if (unop_user_defined_p (op
, arg1
))
1763 return value_x_unop (arg1
, op
, noside
);
1768 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1769 arg2
= value_ptradd (arg1
, -1);
1772 struct value
*tmp
= arg1
;
1774 arg2
= value_one (arg1
->type ());
1775 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1776 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1779 return value_assign (arg1
, arg2
);
1783 /* A helper function for UNOP_POSTINCREMENT. */
1786 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1787 enum noside noside
, enum exp_opcode op
,
1790 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1792 else if (unop_user_defined_p (op
, arg1
))
1794 return value_x_unop (arg1
, op
, noside
);
1798 struct value
*arg3
= arg1
->non_lval ();
1801 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1802 arg2
= value_ptradd (arg1
, 1);
1805 struct value
*tmp
= arg1
;
1807 arg2
= value_one (arg1
->type ());
1808 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1809 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1812 value_assign (arg1
, arg2
);
1817 /* A helper function for UNOP_POSTDECREMENT. */
1820 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1821 enum noside noside
, enum exp_opcode op
,
1824 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1826 else if (unop_user_defined_p (op
, arg1
))
1828 return value_x_unop (arg1
, op
, noside
);
1832 struct value
*arg3
= arg1
->non_lval ();
1835 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1836 arg2
= value_ptradd (arg1
, -1);
1839 struct value
*tmp
= arg1
;
1841 arg2
= value_one (arg1
->type ());
1842 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1843 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1846 value_assign (arg1
, arg2
);
1851 /* A helper function for OP_TYPE. */
1854 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1855 enum noside noside
, struct type
*type
)
1857 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1858 return value::allocate (type
);
1860 error (_("Attempt to use a type name as an expression"));
1863 /* A helper function for BINOP_ASSIGN_MODIFY. */
1866 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1867 enum noside noside
, enum exp_opcode op
,
1868 struct value
*arg1
, struct value
*arg2
)
1870 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1872 if (binop_user_defined_p (op
, arg1
, arg2
))
1873 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1874 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1876 && is_integral_type (arg2
->type ()))
1877 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1878 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1880 && is_integral_type (arg2
->type ()))
1881 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1884 struct value
*tmp
= arg1
;
1886 /* For shift and integer exponentiation operations,
1887 only promote the first argument. */
1888 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1889 && is_integral_type (arg2
->type ()))
1890 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1892 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1894 arg2
= value_binop (tmp
, arg2
, op
);
1896 return value_assign (arg1
, arg2
);
1899 /* Note that ARGS needs 2 empty slots up front and must end with a
1901 static struct value
*
1902 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1903 enum noside noside
, CORE_ADDR selector
,
1904 value
*target
, gdb::array_view
<value
*> args
)
1906 CORE_ADDR responds_selector
= 0;
1907 CORE_ADDR method_selector
= 0;
1909 int struct_return
= 0;
1911 struct value
*msg_send
= NULL
;
1912 struct value
*msg_send_stret
= NULL
;
1913 int gnu_runtime
= 0;
1915 struct value
*method
= NULL
;
1916 struct value
*called_method
= NULL
;
1918 struct type
*selector_type
= NULL
;
1919 struct type
*long_type
;
1922 struct value
*ret
= NULL
;
1927 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1928 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1930 if (value_as_long (target
) == 0)
1931 return value_from_longest (long_type
, 0);
1933 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1936 /* Find the method dispatch (Apple runtime) or method lookup
1937 (GNU runtime) function for Objective-C. These will be used
1938 to lookup the symbol information for the method. If we
1939 can't find any symbol information, then we'll use these to
1940 call the method, otherwise we can call the method
1941 directly. The msg_send_stret function is used in the special
1942 case of a method that returns a structure (Apple runtime
1946 type
= selector_type
;
1948 type
= lookup_function_type (type
);
1949 type
= lookup_pointer_type (type
);
1950 type
= lookup_function_type (type
);
1951 type
= lookup_pointer_type (type
);
1953 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1955 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1957 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1958 msg_send_stret
= value_from_pointer (type
,
1959 value_as_address (msg_send_stret
));
1963 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1964 /* Special dispatcher for methods returning structs. */
1966 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1969 /* Verify the target object responds to this method. The
1970 standard top-level 'Object' class uses a different name for
1971 the verification method than the non-standard, but more
1972 often used, 'NSObject' class. Make sure we check for both. */
1975 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1976 if (responds_selector
== 0)
1978 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1980 if (responds_selector
== 0)
1981 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1984 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1985 if (method_selector
== 0)
1987 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1989 if (method_selector
== 0)
1990 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1992 /* Call the verification method, to make sure that the target
1993 class implements the desired method. */
1995 argvec
[0] = msg_send
;
1997 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1998 argvec
[3] = value_from_longest (long_type
, selector
);
2001 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2004 /* Function objc_msg_lookup returns a pointer. */
2006 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2008 if (value_as_long (ret
) == 0)
2009 error (_("Target does not respond to this message selector."));
2011 /* Call "methodForSelector:" method, to get the address of a
2012 function method that implements this selector for this
2013 class. If we can find a symbol at that address, then we
2014 know the return type, parameter types etc. (that's a good
2017 argvec
[0] = msg_send
;
2019 argvec
[2] = value_from_longest (long_type
, method_selector
);
2020 argvec
[3] = value_from_longest (long_type
, selector
);
2023 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2027 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2030 /* ret should now be the selector. */
2032 addr
= value_as_long (ret
);
2035 struct symbol
*sym
= NULL
;
2037 /* The address might point to a function descriptor;
2038 resolve it to the actual code address instead. */
2039 addr
= gdbarch_convert_from_func_ptr_addr
2040 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
2042 /* Is it a high_level symbol? */
2043 sym
= find_pc_function (addr
);
2045 method
= value_of_variable (sym
, 0);
2048 /* If we found a method with symbol information, check to see
2049 if it returns a struct. Otherwise assume it doesn't. */
2054 struct type
*val_type
;
2056 funaddr
= find_function_addr (method
, &val_type
);
2058 block_for_pc (funaddr
);
2060 val_type
= check_typedef (val_type
);
2062 if ((val_type
== NULL
)
2063 || (val_type
->code () == TYPE_CODE_ERROR
))
2065 if (expect_type
!= NULL
)
2066 val_type
= expect_type
;
2069 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2072 else if (expect_type
!= NULL
)
2074 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2075 check_typedef (expect_type
));
2078 /* Found a function symbol. Now we will substitute its
2079 value in place of the message dispatcher (obj_msgSend),
2080 so that we call the method directly instead of thru
2081 the dispatcher. The main reason for doing this is that
2082 we can now evaluate the return value and parameter values
2083 according to their known data types, in case we need to
2084 do things like promotion, dereferencing, special handling
2085 of structs and doubles, etc.
2087 We want to use the type signature of 'method', but still
2088 jump to objc_msgSend() or objc_msgSend_stret() to better
2089 mimic the behavior of the runtime. */
2093 if (method
->type ()->code () != TYPE_CODE_FUNC
)
2094 error (_("method address has symbol information "
2095 "with non-function type; skipping"));
2097 /* Create a function pointer of the appropriate type, and
2098 replace its value with the value of msg_send or
2099 msg_send_stret. We must use a pointer here, as
2100 msg_send and msg_send_stret are of pointer type, and
2101 the representation may be different on systems that use
2102 function descriptors. */
2105 = value_from_pointer (lookup_pointer_type (method
->type ()),
2106 value_as_address (msg_send_stret
));
2109 = value_from_pointer (lookup_pointer_type (method
->type ()),
2110 value_as_address (msg_send
));
2115 called_method
= msg_send_stret
;
2117 called_method
= msg_send
;
2121 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2123 /* If the return type doesn't look like a function type,
2124 call an error. This can happen if somebody tries to
2125 turn a variable into a function call. This is here
2126 because people often want to call, eg, strcmp, which
2127 gdb doesn't know is a function. If gdb isn't asked for
2128 it's opinion (ie. through "whatis"), it won't offer
2131 struct type
*callee_type
= called_method
->type ();
2133 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2134 callee_type
= callee_type
->target_type ();
2135 callee_type
= callee_type
->target_type ();
2139 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2140 return value::allocate (expect_type
);
2142 return value::allocate (callee_type
);
2145 error (_("Expression of type other than "
2146 "\"method returning ...\" used as a method"));
2149 /* Now depending on whether we found a symbol for the method,
2150 we will either call the runtime dispatcher or the method
2154 args
[1] = value_from_longest (long_type
, selector
);
2156 if (gnu_runtime
&& (method
!= NULL
))
2158 /* Function objc_msg_lookup returns a pointer. */
2159 struct type
*tem_type
= called_method
->type ();
2160 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2161 called_method
->deprecated_set_type (tem_type
);
2162 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2165 return call_function_by_hand (called_method
, NULL
, args
);
2168 /* Helper function for MULTI_SUBSCRIPT. */
2170 static struct value
*
2171 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2172 enum noside noside
, value
*arg1
,
2173 gdb::array_view
<value
*> args
)
2175 for (value
*arg2
: args
)
2177 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2179 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2183 arg1
= coerce_ref (arg1
);
2184 struct type
*type
= check_typedef (arg1
->type ());
2186 switch (type
->code ())
2189 case TYPE_CODE_ARRAY
:
2190 case TYPE_CODE_STRING
:
2191 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2196 error (_("cannot subscript something of type `%s'"),
2199 error (_("cannot subscript requested type"));
2210 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2211 struct expression
*exp
,
2214 enum noside sub_no_side
= EVAL_NORMAL
;
2215 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2217 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2218 sub_no_side
= EVAL_NORMAL
;
2220 sub_no_side
= noside
;
2222 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2224 if (value_as_long (target
) == 0)
2225 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2227 sub_no_side
= noside
;
2228 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2229 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2230 argvec
[0] = nullptr;
2231 argvec
[1] = nullptr;
2232 for (int i
= 0; i
< args
.size (); ++i
)
2233 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2234 argvec
[args
.size () + 2] = nullptr;
2236 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2237 get
<0> (m_storage
), target
,
2238 gdb::make_array_view (argvec
,
2243 multi_subscript_operation::evaluate (struct type
*expect_type
,
2244 struct expression
*exp
,
2247 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2248 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2249 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2250 for (int ix
= 0; ix
< values
.size (); ++ix
)
2251 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2252 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2253 gdb::make_array_view (argvec
, values
.size ()));
2257 logical_and_operation::evaluate (struct type
*expect_type
,
2258 struct expression
*exp
,
2261 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2263 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2264 EVAL_AVOID_SIDE_EFFECTS
);
2266 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2268 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2269 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2273 bool tem
= value_logical_not (arg1
);
2276 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2277 tem
= value_logical_not (arg2
);
2279 struct type
*type
= language_bool_type (exp
->language_defn
,
2281 return value_from_longest (type
, !tem
);
2286 logical_or_operation::evaluate (struct type
*expect_type
,
2287 struct expression
*exp
,
2290 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2292 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2293 EVAL_AVOID_SIDE_EFFECTS
);
2295 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2297 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2298 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2302 bool tem
= value_logical_not (arg1
);
2305 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2306 tem
= value_logical_not (arg2
);
2309 struct type
*type
= language_bool_type (exp
->language_defn
,
2311 return value_from_longest (type
, !tem
);
2316 adl_func_operation::evaluate (struct type
*expect_type
,
2317 struct expression
*exp
,
2320 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2321 std::vector
<value
*> args (arg_ops
.size ());
2322 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2323 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2325 struct symbol
*symp
;
2326 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2329 nullptr, &symp
, nullptr, 0, noside
);
2330 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
2331 error_unknown_type (symp
->print_name ());
2332 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2333 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2334 nullptr, expect_type
);
2338 /* This function evaluates brace-initializers (in C/C++) for
2342 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2343 struct expression
*exp
,
2344 enum noside noside
, int nargs
)
2346 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2347 struct type
*struct_type
= check_typedef (struct_val
->type ());
2348 struct type
*field_type
;
2352 while (--nargs
>= 0)
2354 struct value
*val
= NULL
;
2355 int bitpos
, bitsize
;
2359 /* Skip static fields. */
2360 while (fieldno
< struct_type
->num_fields ()
2361 && struct_type
->field (fieldno
).is_static ())
2363 if (fieldno
>= struct_type
->num_fields ())
2364 error (_("too many initializers"));
2365 field_type
= struct_type
->field (fieldno
).type ();
2366 if (field_type
->code () == TYPE_CODE_UNION
2367 && struct_type
->field (fieldno
).name ()[0] == '0')
2368 error (_("don't know which variant you want to set"));
2370 /* Here, struct_type is the type of the inner struct,
2371 while substruct_type is the type of the inner struct.
2372 These are the same for normal structures, but a variant struct
2373 contains anonymous union fields that contain substruct fields.
2374 The value fieldno is the index of the top-level (normal or
2375 anonymous union) field in struct_field, while the value
2376 subfieldno is the index of the actual real (named inner) field
2377 in substruct_type. */
2379 field_type
= struct_type
->field (fieldno
).type ();
2381 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2383 /* Now actually set the field in struct_val. */
2385 /* Assign val to field fieldno. */
2386 if (val
->type () != field_type
)
2387 val
= value_cast (field_type
, val
);
2389 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2390 bitpos
= struct_type
->field (fieldno
).loc_bitpos ();
2391 addr
= struct_val
->contents_writeable ().data () + bitpos
/ 8;
2393 modify_field (struct_type
, addr
,
2394 value_as_long (val
), bitpos
% 8, bitsize
);
2396 memcpy (addr
, val
->contents ().data (),
2397 val
->type ()->length ());
2404 array_operation::evaluate (struct type
*expect_type
,
2405 struct expression
*exp
,
2409 int tem2
= std::get
<0> (m_storage
);
2410 int tem3
= std::get
<1> (m_storage
);
2411 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2412 int nargs
= tem3
- tem2
+ 1;
2413 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2415 if (expect_type
!= nullptr
2416 && type
->code () == TYPE_CODE_STRUCT
)
2418 struct value
*rec
= value::allocate (expect_type
);
2420 memset (rec
->contents_raw ().data (), '\0', type
->length ());
2421 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2424 if (expect_type
!= nullptr
2425 && type
->code () == TYPE_CODE_ARRAY
)
2427 struct type
*range_type
= type
->index_type ();
2428 struct type
*element_type
= type
->target_type ();
2429 struct value
*array
= value::allocate (expect_type
);
2430 int element_size
= check_typedef (element_type
)->length ();
2431 LONGEST low_bound
, high_bound
, index
;
2433 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2436 high_bound
= (type
->length () / element_size
) - 1;
2439 memset (array
->contents_raw ().data (), 0, expect_type
->length ());
2440 for (tem
= nargs
; --nargs
>= 0;)
2442 struct value
*element
;
2444 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2446 if (element
->type () != element_type
)
2447 element
= value_cast (element_type
, element
);
2448 if (index
> high_bound
)
2449 /* To avoid memory corruption. */
2450 error (_("Too many array elements"));
2451 memcpy (array
->contents_raw ().data ()
2452 + (index
- low_bound
) * element_size
,
2453 element
->contents ().data (),
2460 if (expect_type
!= nullptr
2461 && type
->code () == TYPE_CODE_SET
)
2463 struct value
*set
= value::allocate (expect_type
);
2464 gdb_byte
*valaddr
= set
->contents_raw ().data ();
2465 struct type
*element_type
= type
->index_type ();
2466 struct type
*check_type
= element_type
;
2467 LONGEST low_bound
, high_bound
;
2469 /* Get targettype of elementtype. */
2470 while (check_type
->code () == TYPE_CODE_RANGE
2471 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2472 check_type
= check_type
->target_type ();
2474 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2475 error (_("(power)set type with unknown size"));
2476 memset (valaddr
, '\0', type
->length ());
2478 for (tem
= 0; tem
< nargs
; tem
++)
2480 LONGEST range_low
, range_high
;
2481 struct type
*range_low_type
, *range_high_type
;
2482 struct value
*elem_val
;
2484 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2485 range_low_type
= range_high_type
= elem_val
->type ();
2486 range_low
= range_high
= value_as_long (elem_val
);
2488 /* Check types of elements to avoid mixture of elements from
2489 different types. Also check if type of element is "compatible"
2490 with element type of powerset. */
2491 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2492 range_low_type
= range_low_type
->target_type ();
2493 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2494 range_high_type
= range_high_type
->target_type ();
2495 if ((range_low_type
->code () != range_high_type
->code ())
2496 || (range_low_type
->code () == TYPE_CODE_ENUM
2497 && (range_low_type
!= range_high_type
)))
2498 /* different element modes. */
2499 error (_("POWERSET tuple elements of different mode"));
2500 if ((check_type
->code () != range_low_type
->code ())
2501 || (check_type
->code () == TYPE_CODE_ENUM
2502 && range_low_type
!= check_type
))
2503 error (_("incompatible POWERSET tuple elements"));
2504 if (range_low
> range_high
)
2506 warning (_("empty POWERSET tuple range"));
2509 if (range_low
< low_bound
|| range_high
> high_bound
)
2510 error (_("POWERSET tuple element out of range"));
2511 range_low
-= low_bound
;
2512 range_high
-= low_bound
;
2513 for (; range_low
<= range_high
; range_low
++)
2515 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2517 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2518 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2519 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2526 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2527 for (tem
= 0; tem
< nargs
; tem
++)
2529 /* Ensure that array expressions are coerced into pointer
2531 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2533 return value_array (tem2
, tem3
, argvec
);
2537 unop_extract_operation::evaluate (struct type
*expect_type
,
2538 struct expression
*exp
,
2541 value
*old_value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2542 struct type
*type
= get_type ();
2544 if (type
->length () > old_value
->type ()->length ())
2545 error (_("length type is larger than the value type"));
2547 struct value
*result
= value::allocate (type
);
2548 old_value
->contents_copy (result
, 0, 0, type
->length ());
2555 /* Helper for evaluate_subexp_for_address. */
2558 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2561 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2563 struct type
*type
= check_typedef (x
->type ());
2565 if (TYPE_IS_REFERENCE (type
))
2566 return value::zero (lookup_pointer_type (type
->target_type ()),
2568 else if (x
->lval () == lval_memory
|| value_must_coerce_to_target (x
))
2569 return value::zero (lookup_pointer_type (x
->type ()),
2572 error (_("Attempt to take address of "
2573 "value not located in memory."));
2575 return value_addr (x
);
2582 operation::evaluate_for_cast (struct type
*expect_type
,
2583 struct expression
*exp
,
2586 value
*val
= evaluate (expect_type
, exp
, noside
);
2587 return value_cast (expect_type
, val
);
2591 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2593 value
*val
= evaluate (nullptr, exp
, noside
);
2594 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2598 scope_operation::evaluate_for_address (struct expression
*exp
,
2601 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2602 std::get
<1> (m_storage
).c_str (),
2605 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2610 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2613 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2615 /* We can't optimize out "&*" if there's a user-defined operator*. */
2616 if (unop_user_defined_p (UNOP_IND
, x
))
2618 x
= value_x_unop (x
, UNOP_IND
, noside
);
2619 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2622 return coerce_array (x
);
2626 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2629 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2630 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2631 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2633 struct type
*type
= lookup_pointer_type (val
->type ());
2634 return value::zero (type
, not_lval
);
2637 return value_addr (val
);
2641 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2644 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2645 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2649 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2652 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2653 EVAL_AVOID_SIDE_EFFECTS
);
2654 struct type
*type
= typeval
->type ();
2655 return value_cast (lookup_pointer_type (type
),
2656 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2660 var_value_operation::evaluate_for_address (struct expression
*exp
,
2663 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2665 /* C++: The "address" of a reference should yield the address
2666 * of the object pointed to. Let value_addr() deal with it. */
2667 if (TYPE_IS_REFERENCE (var
->type ()))
2668 return operation::evaluate_for_address (exp
, noside
);
2670 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2672 struct type
*type
= lookup_pointer_type (var
->type ());
2673 enum address_class sym_class
= var
->aclass ();
2675 if (sym_class
== LOC_CONST
2676 || sym_class
== LOC_CONST_BYTES
2677 || sym_class
== LOC_REGISTER
)
2678 error (_("Attempt to take address of register or constant."));
2680 return value::zero (type
, not_lval
);
2683 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2687 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2690 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2691 struct type
*type
= check_typedef (var
->type ());
2692 if (type
->code () == TYPE_CODE_ARRAY
2693 && !type
->is_vector ()
2694 && CAST_IS_CONVERSION (exp
->language_defn
))
2696 struct value
*val
= address_of_variable (var
,
2697 std::get
<0> (m_storage
).block
);
2698 return value_cast (lookup_pointer_type (type
->target_type ()), val
);
2700 return evaluate (nullptr, exp
, noside
);
2705 /* Helper function for evaluating the size of a type. */
2708 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2710 /* FIXME: This should be size_t. */
2711 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2712 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2713 "When applied to a reference or a reference type, the result is
2714 the size of the referenced type." */
2715 type
= check_typedef (type
);
2716 if (exp
->language_defn
->la_language
== language_cplus
2717 && (TYPE_IS_REFERENCE (type
)))
2718 type
= check_typedef (type
->target_type ());
2719 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2726 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2728 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2729 return evaluate_subexp_for_sizeof_base (exp
, val
->type ());
2733 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2737 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2738 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2740 struct type
*type
= mval
->type ();
2741 if (type
->code () == TYPE_CODE_ERROR
)
2742 error_unknown_type (b
.minsym
->print_name ());
2744 /* FIXME: This should be size_t. */
2745 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2746 return value_from_longest (size_type
, type
->length ());
2750 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2753 if (noside
== EVAL_NORMAL
)
2755 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2756 EVAL_AVOID_SIDE_EFFECTS
);
2757 struct type
*type
= check_typedef (val
->type ());
2758 if (type
->code () == TYPE_CODE_ARRAY
)
2760 type
= check_typedef (type
->target_type ());
2761 if (type
->code () == TYPE_CODE_ARRAY
)
2763 type
= type
->index_type ();
2764 /* Only re-evaluate the right hand side if the resulting type
2765 is a variable length type. */
2766 if (type
->bounds ()->flag_bound_evaluated
)
2768 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2769 /* FIXME: This should be size_t. */
2770 struct type
*size_type
2771 = builtin_type (exp
->gdbarch
)->builtin_int
;
2772 return value_from_longest
2773 (size_type
, (LONGEST
) val
->type ()->length ());
2779 return operation::evaluate_for_sizeof (exp
, noside
);
2783 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2786 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2787 EVAL_AVOID_SIDE_EFFECTS
);
2788 struct type
*type
= check_typedef (val
->type ());
2789 if (!type
->is_pointer_or_reference ()
2790 && type
->code () != TYPE_CODE_ARRAY
)
2791 error (_("Attempt to take contents of a non-pointer value."));
2792 type
= type
->target_type ();
2793 if (is_dynamic_type (type
))
2794 type
= value_ind (val
)->type ();
2795 /* FIXME: This should be size_t. */
2796 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2797 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2801 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2804 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2808 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2811 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2812 EVAL_AVOID_SIDE_EFFECTS
);
2813 return evaluate_subexp_for_sizeof_base (exp
, typeval
->type ());
2817 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2820 struct type
*type
= std::get
<0> (m_storage
).symbol
->type ();
2821 if (is_dynamic_type (type
))
2823 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2824 type
= val
->type ();
2825 if (type
->code () == TYPE_CODE_ARRAY
)
2827 /* FIXME: This should be size_t. */
2828 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2829 if (type_not_allocated (type
) || type_not_associated (type
))
2830 return value::zero (size_type
, not_lval
);
2831 else if (is_dynamic_type (type
->index_type ())
2832 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2833 return value::allocate_optimized_out (size_type
);
2836 return evaluate_subexp_for_sizeof_base (exp
, type
);
2840 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2841 struct expression
*exp
,
2844 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2845 return value::zero (to_type
, not_lval
);
2847 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2848 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2850 val
= value_cast (to_type
, val
);
2852 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2853 if (val
->lval () == lval_memory
)
2857 val
->set_lval (not_lval
);
2863 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2864 struct expression
*exp
,
2867 value
*val
= evaluate_var_value (noside
,
2868 std::get
<0> (m_storage
).block
,
2869 std::get
<0> (m_storage
).symbol
);
2871 val
= value_cast (to_type
, val
);
2873 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2874 if (val
->lval () == lval_memory
)
2878 val
->set_lval (not_lval
);
2885 /* Parse a type expression in the string [P..P+LENGTH). */
2888 parse_and_eval_type (const char *p
, int length
)
2890 char *tmp
= (char *) alloca (length
+ 4);
2893 memcpy (tmp
+ 1, p
, length
);
2894 tmp
[length
+ 1] = ')';
2895 tmp
[length
+ 2] = '0';
2896 tmp
[length
+ 3] = '\0';
2897 expression_up expr
= parse_expression (tmp
);
2898 expr::unop_cast_operation
*op
2899 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2901 error (_("Internal error in eval_type."));
2902 return op
->get_type ();