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 /* 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"));
591 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
593 /* If the return type doesn't look like a function type,
594 call an error. This can happen if somebody tries to turn
595 a variable into a function call. */
597 type
*ftype
= callee
->type ();
599 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
601 /* We don't know anything about what the internal
602 function might return, but we have to return
604 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
607 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
609 type
*return_type
= callee
->result_type_of_xmethod (argvec
);
611 if (return_type
== NULL
)
612 error (_("Xmethod is missing return type."));
613 return value::zero (return_type
, not_lval
);
615 else if (ftype
->code () == TYPE_CODE_FUNC
616 || ftype
->code () == TYPE_CODE_METHOD
)
618 if (ftype
->is_gnu_ifunc ())
620 CORE_ADDR address
= callee
->address ();
621 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
623 if (resolved_type
!= NULL
)
624 ftype
= resolved_type
;
627 type
*return_type
= ftype
->target_type ();
629 if (return_type
== NULL
)
630 return_type
= default_return_type
;
632 if (return_type
== NULL
)
633 error_call_unknown_return_type (function_name
);
635 return value::allocate (return_type
);
638 error (_("Expression of type other than "
639 "\"Function returning ...\" used as function"));
641 switch (callee
->type ()->code ())
643 case TYPE_CODE_INTERNAL_FUNCTION
:
644 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
645 callee
, argvec
.size (), argvec
.data ());
646 case TYPE_CODE_XMETHOD
:
647 return callee
->call_xmethod (argvec
);
649 return call_function_by_hand (callee
, default_return_type
, argvec
);
657 operation::evaluate_funcall (struct type
*expect_type
,
658 struct expression
*exp
,
660 const char *function_name
,
661 const std::vector
<operation_up
> &args
)
663 std::vector
<value
*> vals (args
.size ());
665 value
*callee
= evaluate_with_coercion (exp
, noside
);
666 struct type
*type
= callee
->type ();
667 if (type
->code () == TYPE_CODE_PTR
)
668 type
= type
->target_type ();
669 for (int i
= 0; i
< args
.size (); ++i
)
671 if (i
< type
->num_fields ())
672 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
674 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
677 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
678 function_name
, expect_type
);
682 var_value_operation::evaluate_funcall (struct type
*expect_type
,
683 struct expression
*exp
,
685 const std::vector
<operation_up
> &args
)
687 if (!overload_resolution
688 || exp
->language_defn
->la_language
!= language_cplus
)
689 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
691 std::vector
<value
*> argvec (args
.size ());
692 for (int i
= 0; i
< args
.size (); ++i
)
693 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
696 find_overload_match (argvec
, NULL
, NON_METHOD
,
697 NULL
, std::get
<0> (m_storage
).symbol
,
698 NULL
, &symp
, NULL
, 0, noside
);
700 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
701 error_unknown_type (symp
->print_name ());
702 value
*callee
= evaluate_var_value (noside
, std::get
<0> (m_storage
).block
,
705 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
706 nullptr, expect_type
);
710 scope_operation::evaluate_funcall (struct type
*expect_type
,
711 struct expression
*exp
,
713 const std::vector
<operation_up
> &args
)
715 if (!overload_resolution
716 || exp
->language_defn
->la_language
!= language_cplus
)
717 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
719 /* Unpack it locally so we can properly handle overload
721 const std::string
&name
= std::get
<1> (m_storage
);
722 struct type
*type
= std::get
<0> (m_storage
);
724 symbol
*function
= NULL
;
725 const char *function_name
= NULL
;
726 std::vector
<value
*> argvec (1 + args
.size ());
727 if (type
->code () == TYPE_CODE_NAMESPACE
)
729 function
= cp_lookup_symbol_namespace (type
->name (),
731 get_selected_block (0),
733 if (function
== NULL
)
734 error (_("No symbol \"%s\" in namespace \"%s\"."),
735 name
.c_str (), type
->name ());
739 gdb_assert (type
->code () == TYPE_CODE_STRUCT
740 || type
->code () == TYPE_CODE_UNION
);
741 function_name
= name
.c_str ();
743 /* We need a properly typed value for method lookup. */
744 argvec
[0] = value::zero (type
, lval_memory
);
747 for (int i
= 0; i
< args
.size (); ++i
)
748 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
749 gdb::array_view
<value
*> arg_view
= argvec
;
751 value
*callee
= nullptr;
752 if (function_name
!= nullptr)
756 find_overload_match (arg_view
, function_name
, METHOD
,
757 &argvec
[0], nullptr, &callee
, nullptr,
758 &static_memfuncp
, 0, noside
);
759 if (!static_memfuncp
)
761 /* For the time being, we don't handle this. */
762 error (_("Call to overloaded function %s requires "
767 arg_view
= arg_view
.slice (1);
772 arg_view
= arg_view
.slice (1);
773 find_overload_match (arg_view
, nullptr,
774 NON_METHOD
, nullptr, function
,
775 nullptr, &symp
, nullptr, 1, noside
);
776 callee
= value_of_variable (symp
, get_selected_block (0));
779 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
780 nullptr, expect_type
);
784 structop_member_base::evaluate_funcall (struct type
*expect_type
,
785 struct expression
*exp
,
787 const std::vector
<operation_up
> &args
)
789 /* First, evaluate the structure into lhs. */
791 if (opcode () == STRUCTOP_MEMBER
)
792 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
794 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
796 std::vector
<value
*> vals (args
.size () + 1);
797 gdb::array_view
<value
*> val_view
= vals
;
798 /* If the function is a virtual function, then the aggregate
799 value (providing the structure) plays its part by providing
800 the vtable. Otherwise, it is just along for the ride: call
801 the function directly. */
802 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
805 type
*a1_type
= check_typedef (rhs
->type ());
806 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
808 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
809 callee
= value::zero (a1_type
->target_type (), not_lval
);
811 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
815 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
817 struct type
*type_ptr
818 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
819 struct type
*target_type_ptr
820 = lookup_pointer_type (a1_type
->target_type ());
822 /* Now, convert this value to an address. */
823 lhs
= value_cast (type_ptr
, lhs
);
825 long mem_offset
= value_as_long (rhs
);
827 callee
= value_from_pointer (target_type_ptr
,
828 value_as_long (lhs
) + mem_offset
);
829 callee
= value_ind (callee
);
831 val_view
= val_view
.slice (1);
834 error (_("Non-pointer-to-member value used in pointer-to-member "
837 for (int i
= 0; i
< args
.size (); ++i
)
838 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
840 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
841 nullptr, expect_type
);
846 structop_base_operation::evaluate_funcall
847 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
848 const std::vector
<operation_up
> &args
)
850 /* Allocate space for the function call arguments, Including space for a
851 `this' pointer at the start. */
852 std::vector
<value
*> vals (args
.size () + 1);
853 /* First, evaluate the structure into vals[0]. */
854 enum exp_opcode op
= opcode ();
855 if (op
== STRUCTOP_STRUCT
)
857 /* If v is a variable in a register, and the user types
858 v.method (), this will produce an error, because v has no
861 A possible way around this would be to allocate a copy of
862 the variable on the stack, copy in the contents, call the
863 function, and copy out the contents. I.e. convert this
864 from call by reference to call by copy-return (or
865 whatever it's called). However, this does not work
866 because it is not the same: the method being called could
867 stash a copy of the address, and then future uses through
868 that address (after the method returns) would be expected
869 to use the variable itself, not some copy of it. */
870 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
874 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
875 /* Check to see if the operator '->' has been overloaded.
876 If the operator has been overloaded replace vals[0] with the
877 value returned by the custom operator and continue
879 while (unop_user_defined_p (op
, vals
[0]))
881 struct value
*value
= nullptr;
884 value
= value_x_unop (vals
[0], op
, noside
);
886 catch (const gdb_exception_error
&except
)
888 if (except
.error
== NOT_FOUND_ERROR
)
898 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
899 pointer we placed into vals[0]. */
900 for (int i
= 0; i
< args
.size (); ++i
)
901 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
903 /* The array view includes the `this' pointer. */
904 gdb::array_view
<value
*> arg_view (vals
);
908 const char *tstr
= std::get
<1> (m_storage
).c_str ();
909 if (overload_resolution
910 && exp
->language_defn
->la_language
== language_cplus
)
912 /* Language is C++, do some overload resolution before
914 value
*val0
= vals
[0];
915 find_overload_match (arg_view
, tstr
, METHOD
,
916 &val0
, nullptr, &callee
, nullptr,
917 &static_memfuncp
, 0, noside
);
921 /* Non-C++ case -- or no overload resolution. */
923 struct value
*temp
= vals
[0];
925 callee
= value_struct_elt (&temp
, arg_view
, tstr
,
927 op
== STRUCTOP_STRUCT
928 ? "structure" : "structure pointer");
929 /* value_struct_elt updates temp with the correct value of the
930 ``this'' pointer if necessary, so modify it to reflect any
932 vals
[0] = value_from_longest (lookup_pointer_type (temp
->type ()),
934 + temp
->embedded_offset ());
937 /* Take out `this' if needed. */
939 arg_view
= arg_view
.slice (1);
941 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
942 nullptr, expect_type
);
945 /* Helper for structop_base_operation::complete which recursively adds
946 field and method names from TYPE, a struct or union type, to the
947 OUTPUT list. PREFIX is prepended to each result. */
950 add_struct_fields (struct type
*type
, completion_list
&output
,
951 const char *fieldname
, int namelen
, const char *prefix
)
954 int computed_type_name
= 0;
955 const char *type_name
= NULL
;
957 type
= check_typedef (type
);
958 for (i
= 0; i
< type
->num_fields (); ++i
)
960 if (i
< TYPE_N_BASECLASSES (type
))
961 add_struct_fields (TYPE_BASECLASS (type
, i
),
962 output
, fieldname
, namelen
, prefix
);
963 else if (type
->field (i
).name ())
965 if (type
->field (i
).name ()[0] != '\0')
967 if (! strncmp (type
->field (i
).name (),
969 output
.emplace_back (concat (prefix
, type
->field (i
).name (),
972 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
)
974 /* Recurse into anonymous unions. */
975 add_struct_fields (type
->field (i
).type (),
976 output
, fieldname
, namelen
, prefix
);
981 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
983 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
985 if (name
&& ! strncmp (name
, fieldname
, namelen
))
987 if (!computed_type_name
)
989 type_name
= type
->name ();
990 computed_type_name
= 1;
992 /* Omit constructors from the completion list. */
993 if (!type_name
|| strcmp (type_name
, name
))
994 output
.emplace_back (concat (prefix
, name
, nullptr));
1002 structop_base_operation::complete (struct expression
*exp
,
1003 completion_tracker
&tracker
,
1006 const std::string
&fieldname
= std::get
<1> (m_storage
);
1008 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1009 EVAL_AVOID_SIDE_EFFECTS
);
1010 struct type
*type
= lhs
->type ();
1013 type
= check_typedef (type
);
1014 if (!type
->is_pointer_or_reference ())
1016 type
= type
->target_type ();
1019 if (type
->code () == TYPE_CODE_UNION
1020 || type
->code () == TYPE_CODE_STRUCT
)
1022 completion_list result
;
1024 add_struct_fields (type
, result
, fieldname
.c_str (),
1025 fieldname
.length (), prefix
);
1026 tracker
.add_completions (std::move (result
));
1033 } /* namespace expr */
1035 /* Return true if type is integral or reference to integral */
1038 is_integral_or_integral_reference (struct type
*type
)
1040 if (is_integral_type (type
))
1043 type
= check_typedef (type
);
1044 return (type
!= nullptr
1045 && TYPE_IS_REFERENCE (type
)
1046 && is_integral_type (type
->target_type ()));
1049 /* Helper function that implements the body of OP_SCOPE. */
1052 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
1054 struct type
*type
, const char *string
)
1056 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
1059 error (_("There is no field named %s"), string
);
1063 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1066 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1067 enum noside noside
, symbol
*sym
)
1069 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1070 return value::zero (sym
->type (), not_lval
);
1072 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1073 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1074 error (_("Symbol \"%s\" does not have any specific entry value"),
1075 sym
->print_name ());
1077 frame_info_ptr frame
= get_selected_frame (NULL
);
1078 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1081 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1084 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1085 enum noside noside
, bool outermost_p
,
1086 bound_minimal_symbol msymbol
)
1088 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1091 struct type
*type
= val
->type ();
1092 if (type
->code () == TYPE_CODE_ERROR
1093 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1094 error_unknown_type (msymbol
.minsym
->print_name ());
1098 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1101 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1103 value
*func
, const char *var
)
1105 CORE_ADDR addr
= func
->address ();
1106 const block
*blk
= block_for_pc (addr
);
1107 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1108 if (sym
.symbol
== NULL
)
1109 error (_("No symbol \"%s\" in specified context."), var
);
1110 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1113 /* Helper function that implements the body of OP_REGISTER. */
1116 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1117 enum noside noside
, const char *name
)
1122 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1123 name
, strlen (name
));
1125 error (_("Register $%s not available."), name
);
1127 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1128 a value with the appropriate register type. Unfortunately,
1129 we don't have easy access to the type of user registers.
1130 So for these registers, we fetch the register value regardless
1131 of the evaluation mode. */
1132 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1133 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1134 val
= value::zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1136 val
= value_of_register (regno
, get_selected_frame (NULL
));
1138 error (_("Value of register %s not available."), name
);
1147 string_operation::evaluate (struct type
*expect_type
,
1148 struct expression
*exp
,
1151 const std::string
&str
= std::get
<0> (m_storage
);
1152 struct type
*type
= language_string_char_type (exp
->language_defn
,
1154 return value_string (str
.c_str (), str
.size (), type
);
1157 } /* namespace expr */
1159 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1162 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1166 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1167 return value_from_longest (selector_type
,
1168 lookup_child_selector (exp
->gdbarch
, sel
));
1171 /* A helper function for TERNOP_SLICE. */
1174 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1176 struct value
*array
, struct value
*low
, struct value
*upper
)
1178 int lowbound
= value_as_long (low
);
1179 int upperbound
= value_as_long (upper
);
1180 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1183 /* A helper function for STRUCTOP_STRUCT. */
1186 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1188 struct value
*arg1
, const char *string
)
1190 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1192 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1193 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1197 /* A helper function for STRUCTOP_PTR. */
1200 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1202 struct value
*arg1
, const char *string
)
1204 /* Check to see if operator '->' has been overloaded. If so replace
1205 arg1 with the value returned by evaluating operator->(). */
1206 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1208 struct value
*value
= NULL
;
1211 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1214 catch (const gdb_exception_error
&except
)
1216 if (except
.error
== NOT_FOUND_ERROR
)
1225 /* JYG: if print object is on we need to replace the base type
1226 with rtti type in order to continue on with successful
1227 lookup of member / method only available in the rtti type. */
1229 struct type
*arg_type
= arg1
->type ();
1230 struct type
*real_type
;
1231 int full
, using_enc
;
1233 struct value_print_options opts
;
1235 get_user_print_options (&opts
);
1236 if (opts
.objectprint
&& arg_type
->target_type ()
1237 && (arg_type
->target_type ()->code () == TYPE_CODE_STRUCT
))
1239 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1242 arg1
= value_cast (real_type
, arg1
);
1246 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1247 NULL
, "structure pointer");
1248 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1249 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1253 /* A helper function for STRUCTOP_MEMBER. */
1256 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1258 struct value
*arg1
, struct value
*arg2
)
1263 struct type
*type
= check_typedef (arg2
->type ());
1264 switch (type
->code ())
1266 case TYPE_CODE_METHODPTR
:
1267 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1268 return value::zero (type
->target_type (), not_lval
);
1271 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1272 gdb_assert (arg2
->type ()->code () == TYPE_CODE_PTR
);
1273 return value_ind (arg2
);
1276 case TYPE_CODE_MEMBERPTR
:
1277 /* Now, convert these values to an address. */
1278 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_PTR
)
1279 arg1
= value_addr (arg1
);
1280 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1283 mem_offset
= value_as_long (arg2
);
1285 arg3
= value_from_pointer (lookup_pointer_type (type
->target_type ()),
1286 value_as_long (arg1
) + mem_offset
);
1287 return value_ind (arg3
);
1290 error (_("non-pointer-to-member value used "
1291 "in pointer-to-member construct"));
1295 /* A helper function for BINOP_ADD. */
1298 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1300 struct value
*arg1
, struct value
*arg2
)
1302 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1303 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1304 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1305 && is_integral_or_integral_reference (arg2
->type ()))
1306 return value_ptradd (arg1
, value_as_long (arg2
));
1307 else if (ptrmath_type_p (exp
->language_defn
, arg2
->type ())
1308 && is_integral_or_integral_reference (arg1
->type ()))
1309 return value_ptradd (arg2
, value_as_long (arg1
));
1312 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1313 return value_binop (arg1
, arg2
, BINOP_ADD
);
1317 /* A helper function for BINOP_SUB. */
1320 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1322 struct value
*arg1
, struct value
*arg2
)
1324 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1325 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1326 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1327 && ptrmath_type_p (exp
->language_defn
, arg2
->type ()))
1329 /* FIXME -- should be ptrdiff_t */
1330 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1331 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1333 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1334 && is_integral_or_integral_reference (arg2
->type ()))
1335 return value_ptradd (arg1
, - value_as_long (arg2
));
1338 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1339 return value_binop (arg1
, arg2
, BINOP_SUB
);
1343 /* Helper function for several different binary operations. */
1346 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1347 enum noside noside
, enum exp_opcode op
,
1348 struct value
*arg1
, struct value
*arg2
)
1350 if (binop_user_defined_p (op
, arg1
, arg2
))
1351 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1354 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1355 fudge arg2 to avoid division-by-zero, the caller is
1356 (theoretically) only looking for the type of the result. */
1357 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1358 /* ??? Do we really want to test for BINOP_MOD here?
1359 The implementation of value_binop gives it a well-defined
1362 || op
== BINOP_INTDIV
1365 && value_logical_not (arg2
))
1367 struct value
*v_one
;
1369 v_one
= value_one (arg2
->type ());
1370 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1371 return value_binop (arg1
, v_one
, op
);
1375 /* For shift and integer exponentiation operations,
1376 only promote the first argument. */
1377 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1378 && is_integral_type (arg2
->type ()))
1379 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1381 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1383 return value_binop (arg1
, arg2
, op
);
1388 /* A helper function for BINOP_SUBSCRIPT. */
1391 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1392 enum noside noside
, enum exp_opcode op
,
1393 struct value
*arg1
, struct value
*arg2
)
1395 if (binop_user_defined_p (op
, arg1
, arg2
))
1396 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1399 /* If the user attempts to subscript something that is not an
1400 array or pointer type (like a plain int variable for example),
1401 then report this as an error. */
1403 arg1
= coerce_ref (arg1
);
1404 struct type
*type
= check_typedef (arg1
->type ());
1405 if (type
->code () != TYPE_CODE_ARRAY
1406 && type
->code () != TYPE_CODE_PTR
)
1409 error (_("cannot subscript something of type `%s'"),
1412 error (_("cannot subscript requested type"));
1415 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1416 return value::zero (type
->target_type (), arg1
->lval ());
1418 return value_subscript (arg1
, value_as_long (arg2
));
1422 /* A helper function for BINOP_EQUAL. */
1425 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1426 enum noside noside
, enum exp_opcode op
,
1427 struct value
*arg1
, struct value
*arg2
)
1429 if (binop_user_defined_p (op
, arg1
, arg2
))
1431 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1435 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1436 int tem
= value_equal (arg1
, arg2
);
1437 struct type
*type
= language_bool_type (exp
->language_defn
,
1439 return value_from_longest (type
, (LONGEST
) tem
);
1443 /* A helper function for BINOP_NOTEQUAL. */
1446 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1447 enum noside noside
, enum exp_opcode op
,
1448 struct value
*arg1
, struct value
*arg2
)
1450 if (binop_user_defined_p (op
, arg1
, arg2
))
1452 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1456 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1457 int tem
= value_equal (arg1
, arg2
);
1458 struct type
*type
= language_bool_type (exp
->language_defn
,
1460 return value_from_longest (type
, (LONGEST
) ! tem
);
1464 /* A helper function for BINOP_LESS. */
1467 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1468 enum noside noside
, enum exp_opcode op
,
1469 struct value
*arg1
, struct value
*arg2
)
1471 if (binop_user_defined_p (op
, arg1
, arg2
))
1473 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1477 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1478 int tem
= value_less (arg1
, arg2
);
1479 struct type
*type
= language_bool_type (exp
->language_defn
,
1481 return value_from_longest (type
, (LONGEST
) tem
);
1485 /* A helper function for BINOP_GTR. */
1488 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1489 enum noside noside
, enum exp_opcode op
,
1490 struct value
*arg1
, struct value
*arg2
)
1492 if (binop_user_defined_p (op
, arg1
, arg2
))
1494 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1498 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1499 int tem
= value_less (arg2
, arg1
);
1500 struct type
*type
= language_bool_type (exp
->language_defn
,
1502 return value_from_longest (type
, (LONGEST
) tem
);
1506 /* A helper function for BINOP_GEQ. */
1509 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1510 enum noside noside
, enum exp_opcode op
,
1511 struct value
*arg1
, struct value
*arg2
)
1513 if (binop_user_defined_p (op
, arg1
, arg2
))
1515 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1519 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1520 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1521 struct type
*type
= language_bool_type (exp
->language_defn
,
1523 return value_from_longest (type
, (LONGEST
) tem
);
1527 /* A helper function for BINOP_LEQ. */
1530 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1531 enum noside noside
, enum exp_opcode op
,
1532 struct value
*arg1
, struct value
*arg2
)
1534 if (binop_user_defined_p (op
, arg1
, arg2
))
1536 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1540 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1541 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1542 struct type
*type
= language_bool_type (exp
->language_defn
,
1544 return value_from_longest (type
, (LONGEST
) tem
);
1548 /* A helper function for BINOP_REPEAT. */
1551 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1552 enum noside noside
, enum exp_opcode op
,
1553 struct value
*arg1
, struct value
*arg2
)
1555 struct type
*type
= check_typedef (arg2
->type ());
1556 if (type
->code () != TYPE_CODE_INT
1557 && type
->code () != TYPE_CODE_ENUM
)
1558 error (_("Non-integral right operand for \"@\" operator."));
1559 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1561 return allocate_repeat_value (arg1
->type (),
1562 longest_to_int (value_as_long (arg2
)));
1565 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1568 /* A helper function for UNOP_PLUS. */
1571 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1572 enum noside noside
, enum exp_opcode op
,
1575 if (unop_user_defined_p (op
, arg1
))
1576 return value_x_unop (arg1
, op
, noside
);
1579 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1580 return value_pos (arg1
);
1584 /* A helper function for UNOP_NEG. */
1587 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1588 enum noside noside
, enum exp_opcode op
,
1591 if (unop_user_defined_p (op
, arg1
))
1592 return value_x_unop (arg1
, op
, noside
);
1595 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1596 return value_neg (arg1
);
1600 /* A helper function for UNOP_COMPLEMENT. */
1603 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1604 enum noside noside
, enum exp_opcode op
,
1607 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1608 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1611 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1612 return value_complement (arg1
);
1616 /* A helper function for UNOP_LOGICAL_NOT. */
1619 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1620 enum noside noside
, enum exp_opcode op
,
1623 if (unop_user_defined_p (op
, arg1
))
1624 return value_x_unop (arg1
, op
, noside
);
1627 struct type
*type
= language_bool_type (exp
->language_defn
,
1629 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1633 /* A helper function for UNOP_IND. */
1636 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1640 struct type
*type
= check_typedef (arg1
->type ());
1641 if (type
->code () == TYPE_CODE_METHODPTR
1642 || type
->code () == TYPE_CODE_MEMBERPTR
)
1643 error (_("Attempt to dereference pointer "
1644 "to member without an object"));
1645 if (unop_user_defined_p (UNOP_IND
, arg1
))
1646 return value_x_unop (arg1
, UNOP_IND
, noside
);
1647 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1649 type
= check_typedef (arg1
->type ());
1651 /* If the type pointed to is dynamic then in order to resolve the
1652 dynamic properties we must actually dereference the pointer.
1653 There is a risk that this dereference will have side-effects
1654 in the inferior, but being able to print accurate type
1655 information seems worth the risk. */
1656 if (!type
->is_pointer_or_reference ()
1657 || !is_dynamic_type (type
->target_type ()))
1659 if (type
->is_pointer_or_reference ()
1660 /* In C you can dereference an array to get the 1st elt. */
1661 || type
->code () == TYPE_CODE_ARRAY
)
1662 return value::zero (type
->target_type (),
1664 else if (type
->code () == TYPE_CODE_INT
)
1665 /* GDB allows dereferencing an int. */
1666 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1669 error (_("Attempt to take contents of a non-pointer value."));
1673 /* Allow * on an integer so we can cast it to whatever we want.
1674 This returns an int, which seems like the most C-like thing to
1675 do. "long long" variables are rare enough that
1676 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1677 if (type
->code () == TYPE_CODE_INT
)
1678 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1679 (CORE_ADDR
) value_as_address (arg1
));
1680 return value_ind (arg1
);
1683 /* A helper function for UNOP_ALIGNOF. */
1686 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1690 struct type
*type
= arg1
->type ();
1691 /* FIXME: This should be size_t. */
1692 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1693 ULONGEST align
= type_align (type
);
1695 error (_("could not determine alignment of type"));
1696 return value_from_longest (size_type
, align
);
1699 /* A helper function for UNOP_MEMVAL. */
1702 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1704 struct value
*arg1
, struct type
*type
)
1706 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1707 return value::zero (type
, lval_memory
);
1709 return value_at_lazy (type
, value_as_address (arg1
));
1712 /* A helper function for UNOP_PREINCREMENT. */
1715 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1716 enum noside noside
, enum exp_opcode op
,
1719 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1721 else if (unop_user_defined_p (op
, arg1
))
1723 return value_x_unop (arg1
, op
, noside
);
1728 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1729 arg2
= value_ptradd (arg1
, 1);
1732 struct value
*tmp
= arg1
;
1734 arg2
= value_one (arg1
->type ());
1735 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1736 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1739 return value_assign (arg1
, arg2
);
1743 /* A helper function for UNOP_PREDECREMENT. */
1746 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1747 enum noside noside
, enum exp_opcode op
,
1750 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1752 else if (unop_user_defined_p (op
, arg1
))
1754 return value_x_unop (arg1
, op
, noside
);
1759 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1760 arg2
= value_ptradd (arg1
, -1);
1763 struct value
*tmp
= arg1
;
1765 arg2
= value_one (arg1
->type ());
1766 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1767 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1770 return value_assign (arg1
, arg2
);
1774 /* A helper function for UNOP_POSTINCREMENT. */
1777 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1778 enum noside noside
, enum exp_opcode op
,
1781 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1783 else if (unop_user_defined_p (op
, arg1
))
1785 return value_x_unop (arg1
, op
, noside
);
1789 struct value
*arg3
= arg1
->non_lval ();
1792 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1793 arg2
= value_ptradd (arg1
, 1);
1796 struct value
*tmp
= arg1
;
1798 arg2
= value_one (arg1
->type ());
1799 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1800 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1803 value_assign (arg1
, arg2
);
1808 /* A helper function for UNOP_POSTDECREMENT. */
1811 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1812 enum noside noside
, enum exp_opcode op
,
1815 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1817 else if (unop_user_defined_p (op
, arg1
))
1819 return value_x_unop (arg1
, op
, noside
);
1823 struct value
*arg3
= arg1
->non_lval ();
1826 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1827 arg2
= value_ptradd (arg1
, -1);
1830 struct value
*tmp
= arg1
;
1832 arg2
= value_one (arg1
->type ());
1833 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1834 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1837 value_assign (arg1
, arg2
);
1842 /* A helper function for OP_TYPE. */
1845 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1846 enum noside noside
, struct type
*type
)
1848 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1849 return value::allocate (type
);
1851 error (_("Attempt to use a type name as an expression"));
1854 /* A helper function for BINOP_ASSIGN_MODIFY. */
1857 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1858 enum noside noside
, enum exp_opcode op
,
1859 struct value
*arg1
, struct value
*arg2
)
1861 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1863 if (binop_user_defined_p (op
, arg1
, arg2
))
1864 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1865 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1867 && is_integral_type (arg2
->type ()))
1868 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1869 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1871 && is_integral_type (arg2
->type ()))
1872 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1875 struct value
*tmp
= arg1
;
1877 /* For shift and integer exponentiation operations,
1878 only promote the first argument. */
1879 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1880 && is_integral_type (arg2
->type ()))
1881 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1883 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1885 arg2
= value_binop (tmp
, arg2
, op
);
1887 return value_assign (arg1
, arg2
);
1890 /* Note that ARGS needs 2 empty slots up front and must end with a
1892 static struct value
*
1893 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1894 enum noside noside
, CORE_ADDR selector
,
1895 value
*target
, gdb::array_view
<value
*> args
)
1897 CORE_ADDR responds_selector
= 0;
1898 CORE_ADDR method_selector
= 0;
1900 int struct_return
= 0;
1902 struct value
*msg_send
= NULL
;
1903 struct value
*msg_send_stret
= NULL
;
1904 int gnu_runtime
= 0;
1906 struct value
*method
= NULL
;
1907 struct value
*called_method
= NULL
;
1909 struct type
*selector_type
= NULL
;
1910 struct type
*long_type
;
1913 struct value
*ret
= NULL
;
1918 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1919 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1921 if (value_as_long (target
) == 0)
1922 return value_from_longest (long_type
, 0);
1924 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1927 /* Find the method dispatch (Apple runtime) or method lookup
1928 (GNU runtime) function for Objective-C. These will be used
1929 to lookup the symbol information for the method. If we
1930 can't find any symbol information, then we'll use these to
1931 call the method, otherwise we can call the method
1932 directly. The msg_send_stret function is used in the special
1933 case of a method that returns a structure (Apple runtime
1937 type
= selector_type
;
1939 type
= lookup_function_type (type
);
1940 type
= lookup_pointer_type (type
);
1941 type
= lookup_function_type (type
);
1942 type
= lookup_pointer_type (type
);
1944 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1946 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1948 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1949 msg_send_stret
= value_from_pointer (type
,
1950 value_as_address (msg_send_stret
));
1954 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1955 /* Special dispatcher for methods returning structs. */
1957 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1960 /* Verify the target object responds to this method. The
1961 standard top-level 'Object' class uses a different name for
1962 the verification method than the non-standard, but more
1963 often used, 'NSObject' class. Make sure we check for both. */
1966 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1967 if (responds_selector
== 0)
1969 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1971 if (responds_selector
== 0)
1972 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1975 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1976 if (method_selector
== 0)
1978 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1980 if (method_selector
== 0)
1981 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1983 /* Call the verification method, to make sure that the target
1984 class implements the desired method. */
1986 argvec
[0] = msg_send
;
1988 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1989 argvec
[3] = value_from_longest (long_type
, selector
);
1992 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1995 /* Function objc_msg_lookup returns a pointer. */
1997 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1999 if (value_as_long (ret
) == 0)
2000 error (_("Target does not respond to this message selector."));
2002 /* Call "methodForSelector:" method, to get the address of a
2003 function method that implements this selector for this
2004 class. If we can find a symbol at that address, then we
2005 know the return type, parameter types etc. (that's a good
2008 argvec
[0] = msg_send
;
2010 argvec
[2] = value_from_longest (long_type
, method_selector
);
2011 argvec
[3] = value_from_longest (long_type
, selector
);
2014 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2018 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2021 /* ret should now be the selector. */
2023 addr
= value_as_long (ret
);
2026 struct symbol
*sym
= NULL
;
2028 /* The address might point to a function descriptor;
2029 resolve it to the actual code address instead. */
2030 addr
= gdbarch_convert_from_func_ptr_addr
2031 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
2033 /* Is it a high_level symbol? */
2034 sym
= find_pc_function (addr
);
2036 method
= value_of_variable (sym
, 0);
2039 /* If we found a method with symbol information, check to see
2040 if it returns a struct. Otherwise assume it doesn't. */
2045 struct type
*val_type
;
2047 funaddr
= find_function_addr (method
, &val_type
);
2049 block_for_pc (funaddr
);
2051 val_type
= check_typedef (val_type
);
2053 if ((val_type
== NULL
)
2054 || (val_type
->code () == TYPE_CODE_ERROR
))
2056 if (expect_type
!= NULL
)
2057 val_type
= expect_type
;
2060 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2063 else if (expect_type
!= NULL
)
2065 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2066 check_typedef (expect_type
));
2069 /* Found a function symbol. Now we will substitute its
2070 value in place of the message dispatcher (obj_msgSend),
2071 so that we call the method directly instead of thru
2072 the dispatcher. The main reason for doing this is that
2073 we can now evaluate the return value and parameter values
2074 according to their known data types, in case we need to
2075 do things like promotion, dereferencing, special handling
2076 of structs and doubles, etc.
2078 We want to use the type signature of 'method', but still
2079 jump to objc_msgSend() or objc_msgSend_stret() to better
2080 mimic the behavior of the runtime. */
2084 if (method
->type ()->code () != TYPE_CODE_FUNC
)
2085 error (_("method address has symbol information "
2086 "with non-function type; skipping"));
2088 /* Create a function pointer of the appropriate type, and
2089 replace its value with the value of msg_send or
2090 msg_send_stret. We must use a pointer here, as
2091 msg_send and msg_send_stret are of pointer type, and
2092 the representation may be different on systems that use
2093 function descriptors. */
2096 = value_from_pointer (lookup_pointer_type (method
->type ()),
2097 value_as_address (msg_send_stret
));
2100 = value_from_pointer (lookup_pointer_type (method
->type ()),
2101 value_as_address (msg_send
));
2106 called_method
= msg_send_stret
;
2108 called_method
= msg_send
;
2112 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2114 /* If the return type doesn't look like a function type,
2115 call an error. This can happen if somebody tries to
2116 turn a variable into a function call. This is here
2117 because people often want to call, eg, strcmp, which
2118 gdb doesn't know is a function. If gdb isn't asked for
2119 it's opinion (ie. through "whatis"), it won't offer
2122 struct type
*callee_type
= called_method
->type ();
2124 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2125 callee_type
= callee_type
->target_type ();
2126 callee_type
= callee_type
->target_type ();
2130 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2131 return value::allocate (expect_type
);
2133 return value::allocate (callee_type
);
2136 error (_("Expression of type other than "
2137 "\"method returning ...\" used as a method"));
2140 /* Now depending on whether we found a symbol for the method,
2141 we will either call the runtime dispatcher or the method
2145 args
[1] = value_from_longest (long_type
, selector
);
2147 if (gnu_runtime
&& (method
!= NULL
))
2149 /* Function objc_msg_lookup returns a pointer. */
2150 struct type
*tem_type
= called_method
->type ();
2151 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2152 called_method
->deprecated_set_type (tem_type
);
2153 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2156 return call_function_by_hand (called_method
, NULL
, args
);
2159 /* Helper function for MULTI_SUBSCRIPT. */
2161 static struct value
*
2162 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2163 enum noside noside
, value
*arg1
,
2164 gdb::array_view
<value
*> args
)
2166 for (value
*arg2
: args
)
2168 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2170 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2174 arg1
= coerce_ref (arg1
);
2175 struct type
*type
= check_typedef (arg1
->type ());
2177 switch (type
->code ())
2180 case TYPE_CODE_ARRAY
:
2181 case TYPE_CODE_STRING
:
2182 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2187 error (_("cannot subscript something of type `%s'"),
2190 error (_("cannot subscript requested type"));
2201 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2202 struct expression
*exp
,
2205 enum noside sub_no_side
= EVAL_NORMAL
;
2206 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2208 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2209 sub_no_side
= EVAL_NORMAL
;
2211 sub_no_side
= noside
;
2213 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2215 if (value_as_long (target
) == 0)
2216 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2218 sub_no_side
= noside
;
2219 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2220 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2221 argvec
[0] = nullptr;
2222 argvec
[1] = nullptr;
2223 for (int i
= 0; i
< args
.size (); ++i
)
2224 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2225 argvec
[args
.size () + 2] = nullptr;
2227 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2228 get
<0> (m_storage
), target
,
2229 gdb::make_array_view (argvec
,
2234 multi_subscript_operation::evaluate (struct type
*expect_type
,
2235 struct expression
*exp
,
2238 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2239 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2240 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2241 for (int ix
= 0; ix
< values
.size (); ++ix
)
2242 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2243 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2244 gdb::make_array_view (argvec
, values
.size ()));
2248 logical_and_operation::evaluate (struct type
*expect_type
,
2249 struct expression
*exp
,
2252 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2254 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2255 EVAL_AVOID_SIDE_EFFECTS
);
2257 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2259 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2260 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2264 bool tem
= value_logical_not (arg1
);
2267 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2268 tem
= value_logical_not (arg2
);
2270 struct type
*type
= language_bool_type (exp
->language_defn
,
2272 return value_from_longest (type
, !tem
);
2277 logical_or_operation::evaluate (struct type
*expect_type
,
2278 struct expression
*exp
,
2281 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2283 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2284 EVAL_AVOID_SIDE_EFFECTS
);
2286 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2288 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2289 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2293 bool tem
= value_logical_not (arg1
);
2296 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2297 tem
= value_logical_not (arg2
);
2300 struct type
*type
= language_bool_type (exp
->language_defn
,
2302 return value_from_longest (type
, !tem
);
2307 adl_func_operation::evaluate (struct type
*expect_type
,
2308 struct expression
*exp
,
2311 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2312 std::vector
<value
*> args (arg_ops
.size ());
2313 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2314 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2316 struct symbol
*symp
;
2317 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2320 nullptr, &symp
, nullptr, 0, noside
);
2321 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
2322 error_unknown_type (symp
->print_name ());
2323 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2324 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2325 nullptr, expect_type
);
2329 /* This function evaluates brace-initializers (in C/C++) for
2333 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2334 struct expression
*exp
,
2335 enum noside noside
, int nargs
)
2337 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2338 struct type
*struct_type
= check_typedef (struct_val
->type ());
2339 struct type
*field_type
;
2343 while (--nargs
>= 0)
2345 struct value
*val
= NULL
;
2346 int bitpos
, bitsize
;
2350 /* Skip static fields. */
2351 while (fieldno
< struct_type
->num_fields ()
2352 && struct_type
->field (fieldno
).is_static ())
2354 if (fieldno
>= struct_type
->num_fields ())
2355 error (_("too many initializers"));
2356 field_type
= struct_type
->field (fieldno
).type ();
2357 if (field_type
->code () == TYPE_CODE_UNION
2358 && struct_type
->field (fieldno
).name ()[0] == '0')
2359 error (_("don't know which variant you want to set"));
2361 /* Here, struct_type is the type of the inner struct,
2362 while substruct_type is the type of the inner struct.
2363 These are the same for normal structures, but a variant struct
2364 contains anonymous union fields that contain substruct fields.
2365 The value fieldno is the index of the top-level (normal or
2366 anonymous union) field in struct_field, while the value
2367 subfieldno is the index of the actual real (named inner) field
2368 in substruct_type. */
2370 field_type
= struct_type
->field (fieldno
).type ();
2372 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2374 /* Now actually set the field in struct_val. */
2376 /* Assign val to field fieldno. */
2377 if (val
->type () != field_type
)
2378 val
= value_cast (field_type
, val
);
2380 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2381 bitpos
= struct_type
->field (fieldno
).loc_bitpos ();
2382 addr
= struct_val
->contents_writeable ().data () + bitpos
/ 8;
2384 modify_field (struct_type
, addr
,
2385 value_as_long (val
), bitpos
% 8, bitsize
);
2387 memcpy (addr
, val
->contents ().data (),
2388 val
->type ()->length ());
2395 array_operation::evaluate (struct type
*expect_type
,
2396 struct expression
*exp
,
2400 int tem2
= std::get
<0> (m_storage
);
2401 int tem3
= std::get
<1> (m_storage
);
2402 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2403 int nargs
= tem3
- tem2
+ 1;
2404 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2406 if (expect_type
!= nullptr
2407 && type
->code () == TYPE_CODE_STRUCT
)
2409 struct value
*rec
= value::allocate (expect_type
);
2411 memset (rec
->contents_raw ().data (), '\0', type
->length ());
2412 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2415 if (expect_type
!= nullptr
2416 && type
->code () == TYPE_CODE_ARRAY
)
2418 struct type
*range_type
= type
->index_type ();
2419 struct type
*element_type
= type
->target_type ();
2420 struct value
*array
= value::allocate (expect_type
);
2421 int element_size
= check_typedef (element_type
)->length ();
2422 LONGEST low_bound
, high_bound
, index
;
2424 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2427 high_bound
= (type
->length () / element_size
) - 1;
2430 memset (array
->contents_raw ().data (), 0, expect_type
->length ());
2431 for (tem
= nargs
; --nargs
>= 0;)
2433 struct value
*element
;
2435 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2437 if (element
->type () != element_type
)
2438 element
= value_cast (element_type
, element
);
2439 if (index
> high_bound
)
2440 /* To avoid memory corruption. */
2441 error (_("Too many array elements"));
2442 memcpy (array
->contents_raw ().data ()
2443 + (index
- low_bound
) * element_size
,
2444 element
->contents ().data (),
2451 if (expect_type
!= nullptr
2452 && type
->code () == TYPE_CODE_SET
)
2454 struct value
*set
= value::allocate (expect_type
);
2455 gdb_byte
*valaddr
= set
->contents_raw ().data ();
2456 struct type
*element_type
= type
->index_type ();
2457 struct type
*check_type
= element_type
;
2458 LONGEST low_bound
, high_bound
;
2460 /* Get targettype of elementtype. */
2461 while (check_type
->code () == TYPE_CODE_RANGE
2462 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2463 check_type
= check_type
->target_type ();
2465 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2466 error (_("(power)set type with unknown size"));
2467 memset (valaddr
, '\0', type
->length ());
2469 for (tem
= 0; tem
< nargs
; tem
++)
2471 LONGEST range_low
, range_high
;
2472 struct type
*range_low_type
, *range_high_type
;
2473 struct value
*elem_val
;
2475 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2476 range_low_type
= range_high_type
= elem_val
->type ();
2477 range_low
= range_high
= value_as_long (elem_val
);
2479 /* Check types of elements to avoid mixture of elements from
2480 different types. Also check if type of element is "compatible"
2481 with element type of powerset. */
2482 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2483 range_low_type
= range_low_type
->target_type ();
2484 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2485 range_high_type
= range_high_type
->target_type ();
2486 if ((range_low_type
->code () != range_high_type
->code ())
2487 || (range_low_type
->code () == TYPE_CODE_ENUM
2488 && (range_low_type
!= range_high_type
)))
2489 /* different element modes. */
2490 error (_("POWERSET tuple elements of different mode"));
2491 if ((check_type
->code () != range_low_type
->code ())
2492 || (check_type
->code () == TYPE_CODE_ENUM
2493 && range_low_type
!= check_type
))
2494 error (_("incompatible POWERSET tuple elements"));
2495 if (range_low
> range_high
)
2497 warning (_("empty POWERSET tuple range"));
2500 if (range_low
< low_bound
|| range_high
> high_bound
)
2501 error (_("POWERSET tuple element out of range"));
2502 range_low
-= low_bound
;
2503 range_high
-= low_bound
;
2504 for (; range_low
<= range_high
; range_low
++)
2506 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2508 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2509 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2510 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2517 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2518 for (tem
= 0; tem
< nargs
; tem
++)
2520 /* Ensure that array expressions are coerced into pointer
2522 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2524 return value_array (tem2
, tem3
, argvec
);
2528 unop_extract_operation::evaluate (struct type
*expect_type
,
2529 struct expression
*exp
,
2532 value
*old_value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2533 struct type
*type
= get_type ();
2535 if (type
->length () > old_value
->type ()->length ())
2536 error (_("length type is larger than the value type"));
2538 struct value
*result
= value::allocate (type
);
2539 old_value
->contents_copy (result
, 0, 0, type
->length ());
2546 /* Helper for evaluate_subexp_for_address. */
2549 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2552 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2554 struct type
*type
= check_typedef (x
->type ());
2556 if (TYPE_IS_REFERENCE (type
))
2557 return value::zero (lookup_pointer_type (type
->target_type ()),
2559 else if (x
->lval () == lval_memory
|| value_must_coerce_to_target (x
))
2560 return value::zero (lookup_pointer_type (x
->type ()),
2563 error (_("Attempt to take address of "
2564 "value not located in memory."));
2566 return value_addr (x
);
2573 operation::evaluate_for_cast (struct type
*expect_type
,
2574 struct expression
*exp
,
2577 value
*val
= evaluate (expect_type
, exp
, noside
);
2578 return value_cast (expect_type
, val
);
2582 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2584 value
*val
= evaluate (nullptr, exp
, noside
);
2585 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2589 scope_operation::evaluate_for_address (struct expression
*exp
,
2592 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2593 std::get
<1> (m_storage
).c_str (),
2596 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2601 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2604 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2606 /* We can't optimize out "&*" if there's a user-defined operator*. */
2607 if (unop_user_defined_p (UNOP_IND
, x
))
2609 x
= value_x_unop (x
, UNOP_IND
, noside
);
2610 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2613 return coerce_array (x
);
2617 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2620 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2621 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2622 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2624 struct type
*type
= lookup_pointer_type (val
->type ());
2625 return value::zero (type
, not_lval
);
2628 return value_addr (val
);
2632 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2635 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2636 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2640 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2643 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2644 EVAL_AVOID_SIDE_EFFECTS
);
2645 struct type
*type
= typeval
->type ();
2646 return value_cast (lookup_pointer_type (type
),
2647 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2651 var_value_operation::evaluate_for_address (struct expression
*exp
,
2654 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2656 /* C++: The "address" of a reference should yield the address
2657 * of the object pointed to. Let value_addr() deal with it. */
2658 if (TYPE_IS_REFERENCE (var
->type ()))
2659 return operation::evaluate_for_address (exp
, noside
);
2661 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2663 struct type
*type
= lookup_pointer_type (var
->type ());
2664 enum address_class sym_class
= var
->aclass ();
2666 if (sym_class
== LOC_CONST
2667 || sym_class
== LOC_CONST_BYTES
2668 || sym_class
== LOC_REGISTER
)
2669 error (_("Attempt to take address of register or constant."));
2671 return value::zero (type
, not_lval
);
2674 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2678 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2681 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2682 struct type
*type
= check_typedef (var
->type ());
2683 if (type
->code () == TYPE_CODE_ARRAY
2684 && !type
->is_vector ()
2685 && CAST_IS_CONVERSION (exp
->language_defn
))
2687 struct value
*val
= address_of_variable (var
,
2688 std::get
<0> (m_storage
).block
);
2689 return value_cast (lookup_pointer_type (type
->target_type ()), val
);
2691 return evaluate (nullptr, exp
, noside
);
2696 /* Helper function for evaluating the size of a type. */
2699 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2701 /* FIXME: This should be size_t. */
2702 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2703 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2704 "When applied to a reference or a reference type, the result is
2705 the size of the referenced type." */
2706 type
= check_typedef (type
);
2707 if (exp
->language_defn
->la_language
== language_cplus
2708 && (TYPE_IS_REFERENCE (type
)))
2709 type
= check_typedef (type
->target_type ());
2710 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2717 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2719 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2720 return evaluate_subexp_for_sizeof_base (exp
, val
->type ());
2724 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2728 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2729 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2731 struct type
*type
= mval
->type ();
2732 if (type
->code () == TYPE_CODE_ERROR
)
2733 error_unknown_type (b
.minsym
->print_name ());
2735 /* FIXME: This should be size_t. */
2736 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2737 return value_from_longest (size_type
, type
->length ());
2741 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2744 if (noside
== EVAL_NORMAL
)
2746 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2747 EVAL_AVOID_SIDE_EFFECTS
);
2748 struct type
*type
= check_typedef (val
->type ());
2749 if (type
->code () == TYPE_CODE_ARRAY
)
2751 type
= check_typedef (type
->target_type ());
2752 if (type
->code () == TYPE_CODE_ARRAY
)
2754 type
= type
->index_type ();
2755 /* Only re-evaluate the right hand side if the resulting type
2756 is a variable length type. */
2757 if (type
->bounds ()->flag_bound_evaluated
)
2759 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2760 /* FIXME: This should be size_t. */
2761 struct type
*size_type
2762 = builtin_type (exp
->gdbarch
)->builtin_int
;
2763 return value_from_longest
2764 (size_type
, (LONGEST
) val
->type ()->length ());
2770 return operation::evaluate_for_sizeof (exp
, noside
);
2774 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2777 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2778 EVAL_AVOID_SIDE_EFFECTS
);
2779 struct type
*type
= check_typedef (val
->type ());
2780 if (!type
->is_pointer_or_reference ()
2781 && type
->code () != TYPE_CODE_ARRAY
)
2782 error (_("Attempt to take contents of a non-pointer value."));
2783 type
= type
->target_type ();
2784 if (is_dynamic_type (type
))
2785 type
= value_ind (val
)->type ();
2786 /* FIXME: This should be size_t. */
2787 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2788 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2792 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2795 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2799 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2802 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2803 EVAL_AVOID_SIDE_EFFECTS
);
2804 return evaluate_subexp_for_sizeof_base (exp
, typeval
->type ());
2808 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2811 struct type
*type
= std::get
<0> (m_storage
).symbol
->type ();
2812 if (is_dynamic_type (type
))
2814 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2815 type
= val
->type ();
2816 if (type
->code () == TYPE_CODE_ARRAY
)
2818 /* FIXME: This should be size_t. */
2819 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2820 if (type_not_allocated (type
) || type_not_associated (type
))
2821 return value::zero (size_type
, not_lval
);
2822 else if (is_dynamic_type (type
->index_type ())
2823 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2824 return value::allocate_optimized_out (size_type
);
2827 return evaluate_subexp_for_sizeof_base (exp
, type
);
2831 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2832 struct expression
*exp
,
2835 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2836 return value::zero (to_type
, not_lval
);
2838 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2839 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2841 val
= value_cast (to_type
, val
);
2843 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2844 if (val
->lval () == lval_memory
)
2848 val
->set_lval (not_lval
);
2854 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2855 struct expression
*exp
,
2858 value
*val
= evaluate_var_value (noside
,
2859 std::get
<0> (m_storage
).block
,
2860 std::get
<0> (m_storage
).symbol
);
2862 val
= value_cast (to_type
, val
);
2864 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2865 if (val
->lval () == lval_memory
)
2869 val
->set_lval (not_lval
);
2876 /* Parse a type expression in the string [P..P+LENGTH). */
2879 parse_and_eval_type (const char *p
, int length
)
2881 char *tmp
= (char *) alloca (length
+ 4);
2884 memcpy (tmp
+ 1, p
, length
);
2885 tmp
[length
+ 1] = ')';
2886 tmp
[length
+ 2] = '0';
2887 tmp
[length
+ 3] = '\0';
2888 expression_up expr
= parse_expression (tmp
);
2889 expr::unop_cast_operation
*op
2890 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2892 error (_("Internal error in eval_type."));
2893 return op
->get_type ();