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
, parser_flags flags
)
72 expression_up expr
= parse_expression (exp
, nullptr, flags
);
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,
85 PARSER_COMMA_TERMINATES
);
87 return expr
->evaluate ();
91 /* See expression.h. */
94 expression::uses_objfile (struct objfile
*objfile
) const
96 gdb_assert (objfile
->separate_debug_objfile_backlink
== nullptr);
97 return op
->uses_objfile (objfile
);
100 /* See expression.h. */
103 expression::evaluate (struct type
*expect_type
, enum noside noside
)
105 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
106 if (target_has_execution () && inferior_ptid
!= null_ptid
107 && language_defn
->la_language
== language_cplus
108 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
109 stack_temporaries
.emplace (inferior_thread ());
111 struct value
*retval
= op
->evaluate (expect_type
, this, noside
);
113 if (stack_temporaries
.has_value ()
114 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
115 retval
= retval
->non_lval ();
120 /* Find the current value of a watchpoint on EXP. Return the value in
121 *VALP and *RESULTP and the chain of intermediate and final values
122 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
125 If PRESERVE_ERRORS is true, then exceptions are passed through.
126 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
127 occurs while evaluating the expression, *RESULTP will be set to
128 NULL. *RESULTP may be a lazy value, if the result could not be
129 read from memory. It is used to determine whether a value is
130 user-specified (we should watch the whole value) or intermediate
131 (we should watch only the bit used to locate the final value).
133 If the final value, or any intermediate value, could not be read
134 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
135 set to any referenced values. *VALP will never be a lazy value.
136 This is the value which we store in struct breakpoint.
138 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
139 released from the value chain. If VAL_CHAIN is NULL, all generated
140 values will be left on the value chain. */
143 fetch_subexp_value (struct expression
*exp
,
145 struct value
**valp
, struct value
**resultp
,
146 std::vector
<value_ref_ptr
> *val_chain
,
147 bool preserve_errors
)
149 struct value
*mark
, *new_mark
, *result
;
157 /* Evaluate the expression. */
158 mark
= value_mark ();
163 result
= op
->evaluate (nullptr, exp
, EVAL_NORMAL
);
165 catch (const gdb_exception
&ex
)
167 /* Ignore memory errors if we want watchpoints pointing at
168 inaccessible memory to still be created; otherwise, throw the
169 error to some higher catcher. */
173 if (!preserve_errors
)
182 new_mark
= value_mark ();
183 if (mark
== new_mark
)
188 /* Make sure it's not lazy, so that after the target stops again we
189 have a non-lazy previous value to compare with. */
192 if (!result
->lazy ())
199 result
->fetch_lazy ();
202 catch (const gdb_exception_error
&except
)
210 /* Return the chain of intermediate values. We use this to
211 decide which addresses to watch. */
212 *val_chain
= value_release_to_mark (mark
);
216 /* Promote value ARG1 as appropriate before performing a unary operation
218 If the result is not appropriate for any particular language then it
219 needs to patch this function. */
222 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
227 *arg1
= coerce_ref (*arg1
);
228 type1
= check_typedef ((*arg1
)->type ());
230 if (is_integral_type (type1
))
232 switch (language
->la_language
)
235 /* Perform integral promotion for ANSI C/C++.
236 If not appropriate for any particular language
237 it needs to modify this function. */
239 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
241 if (type1
->length () < builtin_int
->length ())
242 *arg1
= value_cast (builtin_int
, *arg1
);
249 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
250 operation on those two operands.
251 If the result is not appropriate for any particular language then it
252 needs to patch this function. */
255 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
256 struct value
**arg1
, struct value
**arg2
)
258 struct type
*promoted_type
= NULL
;
262 *arg1
= coerce_ref (*arg1
);
263 *arg2
= coerce_ref (*arg2
);
265 type1
= check_typedef ((*arg1
)->type ());
266 type2
= check_typedef ((*arg2
)->type ());
268 if ((type1
->code () != TYPE_CODE_FLT
269 && type1
->code () != TYPE_CODE_DECFLOAT
270 && !is_integral_type (type1
))
271 || (type2
->code () != TYPE_CODE_FLT
272 && type2
->code () != TYPE_CODE_DECFLOAT
273 && !is_integral_type (type2
)))
276 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
279 if (type1
->code () == TYPE_CODE_DECFLOAT
280 || type2
->code () == TYPE_CODE_DECFLOAT
)
282 /* No promotion required. */
284 else if (type1
->code () == TYPE_CODE_FLT
285 || type2
->code () == TYPE_CODE_FLT
)
287 switch (language
->la_language
)
293 case language_opencl
:
294 /* No promotion required. */
298 /* For other languages the result type is unchanged from gdb
299 version 6.7 for backward compatibility.
300 If either arg was long double, make sure that value is also long
301 double. Otherwise use double. */
302 if (type1
->length () * 8 > gdbarch_double_bit (gdbarch
)
303 || type2
->length () * 8 > gdbarch_double_bit (gdbarch
))
304 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
306 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
310 else if (type1
->code () == TYPE_CODE_BOOL
311 && type2
->code () == TYPE_CODE_BOOL
)
313 /* No promotion required. */
316 /* Integral operations here. */
317 /* FIXME: Also mixed integral/booleans, with result an integer. */
319 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
320 unsigned int promoted_len1
= type1
->length ();
321 unsigned int promoted_len2
= type2
->length ();
322 int is_unsigned1
= type1
->is_unsigned ();
323 int is_unsigned2
= type2
->is_unsigned ();
324 unsigned int result_len
;
325 int unsigned_operation
;
327 /* Determine type length and signedness after promotion for
329 if (promoted_len1
< builtin
->builtin_int
->length ())
332 promoted_len1
= builtin
->builtin_int
->length ();
334 if (promoted_len2
< builtin
->builtin_int
->length ())
337 promoted_len2
= builtin
->builtin_int
->length ();
340 if (promoted_len1
> promoted_len2
)
342 unsigned_operation
= is_unsigned1
;
343 result_len
= promoted_len1
;
345 else if (promoted_len2
> promoted_len1
)
347 unsigned_operation
= is_unsigned2
;
348 result_len
= promoted_len2
;
352 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
353 result_len
= promoted_len1
;
356 switch (language
->la_language
)
358 case language_opencl
:
360 <= lookup_signed_typename (language
, "int")->length())
364 ? lookup_unsigned_typename (language
, "int")
365 : lookup_signed_typename (language
, "int"));
368 <= lookup_signed_typename (language
, "long")->length())
372 ? lookup_unsigned_typename (language
, "long")
373 : lookup_signed_typename (language
,"long"));
377 if (result_len
<= builtin
->builtin_int
->length ())
379 promoted_type
= (unsigned_operation
380 ? builtin
->builtin_unsigned_int
381 : builtin
->builtin_int
);
383 else if (result_len
<= builtin
->builtin_long
->length ())
385 promoted_type
= (unsigned_operation
386 ? builtin
->builtin_unsigned_long
387 : builtin
->builtin_long
);
389 else if (result_len
<= builtin
->builtin_long_long
->length ())
391 promoted_type
= (unsigned_operation
392 ? builtin
->builtin_unsigned_long_long
393 : builtin
->builtin_long_long
);
397 promoted_type
= (unsigned_operation
398 ? builtin
->builtin_uint128
399 : builtin
->builtin_int128
);
407 /* Promote both operands to common type. */
408 *arg1
= value_cast (promoted_type
, *arg1
);
409 *arg2
= value_cast (promoted_type
, *arg2
);
414 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
416 type
= check_typedef (type
);
417 if (TYPE_IS_REFERENCE (type
))
418 type
= type
->target_type ();
420 switch (type
->code ())
426 case TYPE_CODE_ARRAY
:
427 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
434 /* Represents a fake method with the given parameter types. This is
435 used by the parser to construct a temporary "expected" type for
436 method overload resolution. FLAGS is used as instance flags of the
437 new type, in order to be able to make the new type represent a
438 const/volatile overload. */
443 fake_method (type_instance_flags flags
,
444 int num_types
, struct type
**param_types
);
447 /* The constructed type. */
448 struct type
*type () { return &m_type
; }
451 struct type m_type
{};
452 main_type m_main_type
{};
455 fake_method::fake_method (type_instance_flags flags
,
456 int num_types
, struct type
**param_types
)
458 struct type
*type
= &m_type
;
460 TYPE_MAIN_TYPE (type
) = &m_main_type
;
461 type
->set_length (1);
462 type
->set_code (TYPE_CODE_METHOD
);
463 TYPE_CHAIN (type
) = type
;
464 type
->set_instance_flags (flags
);
467 if (param_types
[num_types
- 1] == NULL
)
470 type
->set_has_varargs (true);
472 else if (check_typedef (param_types
[num_types
- 1])->code ()
476 /* Caller should have ensured this. */
477 gdb_assert (num_types
== 0);
478 type
->set_is_prototyped (true);
482 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
483 neither an objfile nor a gdbarch. As a result we must manually
484 allocate memory for auxiliary fields, and free the memory ourselves
485 when we are done with it. */
486 type
->set_num_fields (num_types
);
488 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
490 while (num_types
-- > 0)
491 type
->field (num_types
).set_type (param_types
[num_types
]);
494 fake_method::~fake_method ()
496 xfree (m_type
.fields ());
503 type_instance_operation::evaluate (struct type
*expect_type
,
504 struct expression
*exp
,
507 type_instance_flags flags
= std::get
<0> (m_storage
);
508 std::vector
<type
*> &types
= std::get
<1> (m_storage
);
510 fake_method
fake_expect_type (flags
, types
.size (), types
.data ());
511 return std::get
<2> (m_storage
)->evaluate (fake_expect_type
.type (),
517 /* Helper for evaluating an OP_VAR_VALUE. */
520 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
522 /* JYG: We used to just return value::zero of the symbol type if
523 we're asked to avoid side effects. Otherwise we return
524 value_of_variable (...). However I'm not sure if
525 value_of_variable () has any side effect. We need a full value
526 object returned here for whatis_exp () to call evaluate_type ()
527 and then pass the full value to value_rtti_target_type () if we
528 are dealing with a pointer or reference to a base class and print
531 struct value
*ret
= NULL
;
535 ret
= value_of_variable (var
, blk
);
538 catch (const gdb_exception_error
&except
)
540 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
543 ret
= value::zero (var
->type (), not_lval
);
554 var_value_operation::evaluate (struct type
*expect_type
,
555 struct expression
*exp
,
558 symbol
*var
= std::get
<0> (m_storage
).symbol
;
559 if (var
->type ()->code () == TYPE_CODE_ERROR
)
560 error_unknown_type (var
->print_name ());
561 return evaluate_var_value (noside
, std::get
<0> (m_storage
).block
, var
);
564 } /* namespace expr */
566 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
569 evaluate_var_msym_value (enum noside noside
,
570 struct objfile
*objfile
, minimal_symbol
*msymbol
)
573 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
575 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
576 return value::zero (the_type
, not_lval
);
578 return value_at_lazy (the_type
, address
);
581 /* See expression.h. */
584 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
586 gdb::array_view
<value
*> argvec
,
587 const char *function_name
,
588 type
*default_return_type
)
591 error (_("Cannot evaluate function -- may be inlined"));
592 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
594 /* If the return type doesn't look like a function type,
595 call an error. This can happen if somebody tries to turn
596 a variable into a function call. */
598 type
*ftype
= callee
->type ();
600 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
602 /* We don't know anything about what the internal
603 function might return, but we have to return
605 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
608 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
610 type
*return_type
= callee
->result_type_of_xmethod (argvec
);
612 if (return_type
== NULL
)
613 error (_("Xmethod is missing return type."));
614 return value::zero (return_type
, not_lval
);
616 else if (ftype
->code () == TYPE_CODE_FUNC
617 || ftype
->code () == TYPE_CODE_METHOD
)
619 if (ftype
->is_gnu_ifunc ())
621 CORE_ADDR address
= callee
->address ();
622 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
624 if (resolved_type
!= NULL
)
625 ftype
= resolved_type
;
628 type
*return_type
= ftype
->target_type ();
630 if (return_type
== NULL
)
631 return_type
= default_return_type
;
633 if (return_type
== NULL
)
634 error_call_unknown_return_type (function_name
);
636 return value::allocate (return_type
);
639 error (_("Expression of type other than "
640 "\"Function returning ...\" used as function"));
642 switch (callee
->type ()->code ())
644 case TYPE_CODE_INTERNAL_FUNCTION
:
645 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
646 callee
, argvec
.size (), argvec
.data ());
647 case TYPE_CODE_XMETHOD
:
648 return callee
->call_xmethod (argvec
);
650 return call_function_by_hand (callee
, default_return_type
, argvec
);
658 operation::evaluate_funcall (struct type
*expect_type
,
659 struct expression
*exp
,
661 const char *function_name
,
662 const std::vector
<operation_up
> &args
)
664 std::vector
<value
*> vals (args
.size ());
666 value
*callee
= evaluate_with_coercion (exp
, noside
);
667 struct type
*type
= callee
->type ();
668 if (type
->code () == TYPE_CODE_PTR
)
669 type
= type
->target_type ();
670 for (int i
= 0; i
< args
.size (); ++i
)
672 if (i
< type
->num_fields ())
673 vals
[i
] = args
[i
]->evaluate (type
->field (i
).type (), exp
, noside
);
675 vals
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
678 return evaluate_subexp_do_call (exp
, noside
, callee
, vals
,
679 function_name
, expect_type
);
683 var_value_operation::evaluate_funcall (struct type
*expect_type
,
684 struct expression
*exp
,
686 const std::vector
<operation_up
> &args
)
688 if (!overload_resolution
689 || exp
->language_defn
->la_language
!= language_cplus
)
690 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
692 std::vector
<value
*> argvec (args
.size ());
693 for (int i
= 0; i
< args
.size (); ++i
)
694 argvec
[i
] = args
[i
]->evaluate_with_coercion (exp
, noside
);
697 find_overload_match (argvec
, NULL
, NON_METHOD
,
698 NULL
, std::get
<0> (m_storage
).symbol
,
699 NULL
, &symp
, NULL
, 0, noside
);
701 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
702 error_unknown_type (symp
->print_name ());
703 value
*callee
= evaluate_var_value (noside
, std::get
<0> (m_storage
).block
,
706 return evaluate_subexp_do_call (exp
, noside
, callee
, argvec
,
707 nullptr, expect_type
);
711 scope_operation::evaluate_funcall (struct type
*expect_type
,
712 struct expression
*exp
,
714 const std::vector
<operation_up
> &args
)
716 if (!overload_resolution
717 || exp
->language_defn
->la_language
!= language_cplus
)
718 return operation::evaluate_funcall (expect_type
, exp
, noside
, args
);
720 /* Unpack it locally so we can properly handle overload
722 const std::string
&name
= std::get
<1> (m_storage
);
723 struct type
*type
= std::get
<0> (m_storage
);
725 symbol
*function
= NULL
;
726 const char *function_name
= NULL
;
727 std::vector
<value
*> argvec (1 + args
.size ());
728 if (type
->code () == TYPE_CODE_NAMESPACE
)
730 function
= cp_lookup_symbol_namespace (type
->name (),
732 get_selected_block (0),
734 if (function
== NULL
)
735 error (_("No symbol \"%s\" in namespace \"%s\"."),
736 name
.c_str (), type
->name ());
740 gdb_assert (type
->code () == TYPE_CODE_STRUCT
741 || type
->code () == TYPE_CODE_UNION
);
742 function_name
= name
.c_str ();
744 /* We need a properly typed value for method lookup. */
745 argvec
[0] = value::zero (type
, lval_memory
);
748 for (int i
= 0; i
< args
.size (); ++i
)
749 argvec
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
750 gdb::array_view
<value
*> arg_view
= argvec
;
752 value
*callee
= nullptr;
753 if (function_name
!= nullptr)
757 find_overload_match (arg_view
, function_name
, METHOD
,
758 &argvec
[0], nullptr, &callee
, nullptr,
759 &static_memfuncp
, 0, noside
);
760 if (!static_memfuncp
)
762 /* For the time being, we don't handle this. */
763 error (_("Call to overloaded function %s requires "
768 arg_view
= arg_view
.slice (1);
773 arg_view
= arg_view
.slice (1);
774 find_overload_match (arg_view
, nullptr,
775 NON_METHOD
, nullptr, function
,
776 nullptr, &symp
, nullptr, 1, noside
);
777 callee
= value_of_variable (symp
, get_selected_block (0));
780 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
781 nullptr, expect_type
);
785 structop_member_base::evaluate_funcall (struct type
*expect_type
,
786 struct expression
*exp
,
788 const std::vector
<operation_up
> &args
)
790 /* First, evaluate the structure into lhs. */
792 if (opcode () == STRUCTOP_MEMBER
)
793 lhs
= std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
795 lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
797 std::vector
<value
*> vals (args
.size () + 1);
798 gdb::array_view
<value
*> val_view
= vals
;
799 /* If the function is a virtual function, then the aggregate
800 value (providing the structure) plays its part by providing
801 the vtable. Otherwise, it is just along for the ride: call
802 the function directly. */
803 value
*rhs
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
806 type
*a1_type
= check_typedef (rhs
->type ());
807 if (a1_type
->code () == TYPE_CODE_METHODPTR
)
809 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
810 callee
= value::zero (a1_type
->target_type (), not_lval
);
812 callee
= cplus_method_ptr_to_value (&lhs
, rhs
);
816 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
818 struct type
*type_ptr
819 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
820 struct type
*target_type_ptr
821 = lookup_pointer_type (a1_type
->target_type ());
823 /* Now, convert this value to an address. */
824 lhs
= value_cast (type_ptr
, lhs
);
826 long mem_offset
= value_as_long (rhs
);
828 callee
= value_from_pointer (target_type_ptr
,
829 value_as_long (lhs
) + mem_offset
);
830 callee
= value_ind (callee
);
832 val_view
= val_view
.slice (1);
835 error (_("Non-pointer-to-member value used in pointer-to-member "
838 for (int i
= 0; i
< args
.size (); ++i
)
839 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
841 return evaluate_subexp_do_call (exp
, noside
, callee
, val_view
,
842 nullptr, expect_type
);
847 structop_base_operation::evaluate_funcall
848 (struct type
*expect_type
, struct expression
*exp
, enum noside noside
,
849 const std::vector
<operation_up
> &args
)
851 /* Allocate space for the function call arguments, Including space for a
852 `this' pointer at the start. */
853 std::vector
<value
*> vals (args
.size () + 1);
854 /* First, evaluate the structure into vals[0]. */
855 enum exp_opcode op
= opcode ();
856 if (op
== STRUCTOP_STRUCT
)
858 /* If v is a variable in a register, and the user types
859 v.method (), this will produce an error, because v has no
862 A possible way around this would be to allocate a copy of
863 the variable on the stack, copy in the contents, call the
864 function, and copy out the contents. I.e. convert this
865 from call by reference to call by copy-return (or
866 whatever it's called). However, this does not work
867 because it is not the same: the method being called could
868 stash a copy of the address, and then future uses through
869 that address (after the method returns) would be expected
870 to use the variable itself, not some copy of it. */
871 vals
[0] = std::get
<0> (m_storage
)->evaluate_for_address (exp
, noside
);
875 vals
[0] = std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
876 /* Check to see if the operator '->' has been overloaded.
877 If the operator has been overloaded replace vals[0] with the
878 value returned by the custom operator and continue
880 while (unop_user_defined_p (op
, vals
[0]))
882 struct value
*value
= nullptr;
885 value
= value_x_unop (vals
[0], op
, noside
);
887 catch (const gdb_exception_error
&except
)
889 if (except
.error
== NOT_FOUND_ERROR
)
899 /* Evaluate the arguments. The '+ 1' here is to allow for the `this'
900 pointer we placed into vals[0]. */
901 for (int i
= 0; i
< args
.size (); ++i
)
902 vals
[i
+ 1] = args
[i
]->evaluate_with_coercion (exp
, noside
);
904 /* The array view includes the `this' pointer. */
905 gdb::array_view
<value
*> arg_view (vals
);
909 const char *tstr
= std::get
<1> (m_storage
).c_str ();
910 if (overload_resolution
911 && exp
->language_defn
->la_language
== language_cplus
)
913 /* Language is C++, do some overload resolution before
915 value
*val0
= vals
[0];
916 find_overload_match (arg_view
, tstr
, METHOD
,
917 &val0
, nullptr, &callee
, nullptr,
918 &static_memfuncp
, 0, noside
);
922 /* Non-C++ case -- or no overload resolution. */
924 struct value
*temp
= vals
[0];
926 callee
= value_struct_elt (&temp
, arg_view
, tstr
,
928 op
== STRUCTOP_STRUCT
929 ? "structure" : "structure pointer");
930 /* value_struct_elt updates temp with the correct value of the
931 ``this'' pointer if necessary, so modify it to reflect any
933 vals
[0] = value_from_longest (lookup_pointer_type (temp
->type ()),
935 + temp
->embedded_offset ());
938 /* Take out `this' if needed. */
940 arg_view
= arg_view
.slice (1);
942 return evaluate_subexp_do_call (exp
, noside
, callee
, arg_view
,
943 nullptr, expect_type
);
946 /* Helper for structop_base_operation::complete which recursively adds
947 field and method names from TYPE, a struct or union type, to the
948 OUTPUT list. PREFIX is prepended to each result. */
951 add_struct_fields (struct type
*type
, completion_list
&output
,
952 const char *fieldname
, int namelen
, const char *prefix
)
955 int computed_type_name
= 0;
956 const char *type_name
= NULL
;
958 type
= check_typedef (type
);
959 for (i
= 0; i
< type
->num_fields (); ++i
)
961 if (i
< TYPE_N_BASECLASSES (type
))
962 add_struct_fields (TYPE_BASECLASS (type
, i
),
963 output
, fieldname
, namelen
, prefix
);
964 else if (type
->field (i
).name ())
966 if (type
->field (i
).name ()[0] != '\0')
968 if (! strncmp (type
->field (i
).name (),
970 output
.emplace_back (concat (prefix
, type
->field (i
).name (),
973 else if (type
->field (i
).type ()->code () == TYPE_CODE_UNION
)
975 /* Recurse into anonymous unions. */
976 add_struct_fields (type
->field (i
).type (),
977 output
, fieldname
, namelen
, prefix
);
982 for (i
= TYPE_NFN_FIELDS (type
) - 1; i
>= 0; --i
)
984 const char *name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
986 if (name
&& ! strncmp (name
, fieldname
, namelen
))
988 if (!computed_type_name
)
990 type_name
= type
->name ();
991 computed_type_name
= 1;
993 /* Omit constructors from the completion list. */
994 if (!type_name
|| strcmp (type_name
, name
))
995 output
.emplace_back (concat (prefix
, name
, nullptr));
1003 structop_base_operation::complete (struct expression
*exp
,
1004 completion_tracker
&tracker
,
1007 const std::string
&fieldname
= std::get
<1> (m_storage
);
1009 value
*lhs
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
1010 EVAL_AVOID_SIDE_EFFECTS
);
1011 struct type
*type
= lhs
->type ();
1014 type
= check_typedef (type
);
1015 if (!type
->is_pointer_or_reference ())
1017 type
= type
->target_type ();
1020 if (type
->code () == TYPE_CODE_UNION
1021 || type
->code () == TYPE_CODE_STRUCT
)
1023 completion_list result
;
1025 add_struct_fields (type
, result
, fieldname
.c_str (),
1026 fieldname
.length (), prefix
);
1027 tracker
.add_completions (std::move (result
));
1034 } /* namespace expr */
1036 /* Return true if type is integral or reference to integral */
1039 is_integral_or_integral_reference (struct type
*type
)
1041 if (is_integral_type (type
))
1044 type
= check_typedef (type
);
1045 return (type
!= nullptr
1046 && TYPE_IS_REFERENCE (type
)
1047 && is_integral_type (type
->target_type ()));
1050 /* Helper function that implements the body of OP_SCOPE. */
1053 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
1055 struct type
*type
, const char *string
)
1057 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
1060 error (_("There is no field named %s"), string
);
1064 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1067 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1068 enum noside noside
, symbol
*sym
)
1070 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1071 return value::zero (sym
->type (), not_lval
);
1073 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1074 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1075 error (_("Symbol \"%s\" does not have any specific entry value"),
1076 sym
->print_name ());
1078 frame_info_ptr frame
= get_selected_frame (NULL
);
1079 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1082 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1085 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1086 enum noside noside
, bool outermost_p
,
1087 bound_minimal_symbol msymbol
)
1089 value
*val
= evaluate_var_msym_value (noside
, msymbol
.objfile
,
1092 struct type
*type
= val
->type ();
1093 if (type
->code () == TYPE_CODE_ERROR
1094 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1095 error_unknown_type (msymbol
.minsym
->print_name ());
1099 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1102 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1104 value
*func
, const char *var
)
1106 CORE_ADDR addr
= func
->address ();
1107 const block
*blk
= block_for_pc (addr
);
1108 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1109 if (sym
.symbol
== NULL
)
1110 error (_("No symbol \"%s\" in specified context."), var
);
1111 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1114 /* Helper function that implements the body of OP_REGISTER. */
1117 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1118 enum noside noside
, const char *name
)
1123 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1124 name
, strlen (name
));
1126 error (_("Register $%s not available."), name
);
1128 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1129 a value with the appropriate register type. Unfortunately,
1130 we don't have easy access to the type of user registers.
1131 So for these registers, we fetch the register value regardless
1132 of the evaluation mode. */
1133 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1134 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1135 val
= value::zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1137 val
= value_of_register (regno
, get_selected_frame (NULL
));
1139 error (_("Value of register %s not available."), name
);
1148 string_operation::evaluate (struct type
*expect_type
,
1149 struct expression
*exp
,
1152 const std::string
&str
= std::get
<0> (m_storage
);
1153 struct type
*type
= language_string_char_type (exp
->language_defn
,
1155 return value_string (str
.c_str (), str
.size (), type
);
1158 } /* namespace expr */
1160 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1163 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1167 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1168 return value_from_longest (selector_type
,
1169 lookup_child_selector (exp
->gdbarch
, sel
));
1172 /* A helper function for TERNOP_SLICE. */
1175 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1177 struct value
*array
, struct value
*low
, struct value
*upper
)
1179 int lowbound
= value_as_long (low
);
1180 int upperbound
= value_as_long (upper
);
1181 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1184 /* A helper function for STRUCTOP_STRUCT. */
1187 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1189 struct value
*arg1
, const char *string
)
1191 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1193 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1194 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1198 /* A helper function for STRUCTOP_PTR. */
1201 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1203 struct value
*arg1
, const char *string
)
1205 /* Check to see if operator '->' has been overloaded. If so replace
1206 arg1 with the value returned by evaluating operator->(). */
1207 while (unop_user_defined_p (STRUCTOP_PTR
, arg1
))
1209 struct value
*value
= NULL
;
1212 value
= value_x_unop (arg1
, STRUCTOP_PTR
, noside
);
1215 catch (const gdb_exception_error
&except
)
1217 if (except
.error
== NOT_FOUND_ERROR
)
1226 /* JYG: if print object is on we need to replace the base type
1227 with rtti type in order to continue on with successful
1228 lookup of member / method only available in the rtti type. */
1230 struct type
*arg_type
= arg1
->type ();
1231 struct type
*real_type
;
1232 int full
, using_enc
;
1234 struct value_print_options opts
;
1236 get_user_print_options (&opts
);
1237 if (opts
.objectprint
&& arg_type
->target_type ()
1238 && (arg_type
->target_type ()->code () == TYPE_CODE_STRUCT
))
1240 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1243 arg1
= value_cast (real_type
, arg1
);
1247 struct value
*arg3
= value_struct_elt (&arg1
, {}, string
,
1248 NULL
, "structure pointer");
1249 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1250 arg3
= value::zero (arg3
->type (), arg3
->lval ());
1254 /* A helper function for STRUCTOP_MEMBER. */
1257 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1259 struct value
*arg1
, struct value
*arg2
)
1264 struct type
*type
= check_typedef (arg2
->type ());
1265 switch (type
->code ())
1267 case TYPE_CODE_METHODPTR
:
1268 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1269 return value::zero (type
->target_type (), not_lval
);
1272 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1273 gdb_assert (arg2
->type ()->code () == TYPE_CODE_PTR
);
1274 return value_ind (arg2
);
1277 case TYPE_CODE_MEMBERPTR
:
1278 /* Now, convert these values to an address. */
1279 if (check_typedef (arg1
->type ())->code () != TYPE_CODE_PTR
)
1280 arg1
= value_addr (arg1
);
1281 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1284 mem_offset
= value_as_long (arg2
);
1286 arg3
= value_from_pointer (lookup_pointer_type (type
->target_type ()),
1287 value_as_long (arg1
) + mem_offset
);
1288 return value_ind (arg3
);
1291 error (_("non-pointer-to-member value used "
1292 "in pointer-to-member construct"));
1296 /* A helper function for BINOP_ADD. */
1299 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1301 struct value
*arg1
, struct value
*arg2
)
1303 if (binop_user_defined_p (BINOP_ADD
, arg1
, arg2
))
1304 return value_x_binop (arg1
, arg2
, BINOP_ADD
, OP_NULL
, noside
);
1305 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1306 && is_integral_or_integral_reference (arg2
->type ()))
1307 return value_ptradd (arg1
, value_as_long (arg2
));
1308 else if (ptrmath_type_p (exp
->language_defn
, arg2
->type ())
1309 && is_integral_or_integral_reference (arg1
->type ()))
1310 return value_ptradd (arg2
, value_as_long (arg1
));
1313 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1314 return value_binop (arg1
, arg2
, BINOP_ADD
);
1318 /* A helper function for BINOP_SUB. */
1321 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1323 struct value
*arg1
, struct value
*arg2
)
1325 if (binop_user_defined_p (BINOP_SUB
, arg1
, arg2
))
1326 return value_x_binop (arg1
, arg2
, BINOP_SUB
, OP_NULL
, noside
);
1327 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1328 && ptrmath_type_p (exp
->language_defn
, arg2
->type ()))
1330 /* FIXME -- should be ptrdiff_t */
1331 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1332 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1334 else if (ptrmath_type_p (exp
->language_defn
, arg1
->type ())
1335 && is_integral_or_integral_reference (arg2
->type ()))
1336 return value_ptradd (arg1
, - value_as_long (arg2
));
1339 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1340 return value_binop (arg1
, arg2
, BINOP_SUB
);
1344 /* Helper function for several different binary operations. */
1347 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1348 enum noside noside
, enum exp_opcode op
,
1349 struct value
*arg1
, struct value
*arg2
)
1351 if (binop_user_defined_p (op
, arg1
, arg2
))
1352 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1355 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1356 fudge arg2 to avoid division-by-zero, the caller is
1357 (theoretically) only looking for the type of the result. */
1358 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1359 /* ??? Do we really want to test for BINOP_MOD here?
1360 The implementation of value_binop gives it a well-defined
1363 || op
== BINOP_INTDIV
1366 && value_logical_not (arg2
))
1368 struct value
*v_one
;
1370 v_one
= value_one (arg2
->type ());
1371 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1372 return value_binop (arg1
, v_one
, op
);
1376 /* For shift and integer exponentiation operations,
1377 only promote the first argument. */
1378 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1379 && is_integral_type (arg2
->type ()))
1380 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1382 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1384 return value_binop (arg1
, arg2
, op
);
1389 /* A helper function for BINOP_SUBSCRIPT. */
1392 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1393 enum noside noside
, enum exp_opcode op
,
1394 struct value
*arg1
, struct value
*arg2
)
1396 if (binop_user_defined_p (op
, arg1
, arg2
))
1397 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1400 /* If the user attempts to subscript something that is not an
1401 array or pointer type (like a plain int variable for example),
1402 then report this as an error. */
1404 arg1
= coerce_ref (arg1
);
1405 struct type
*type
= check_typedef (arg1
->type ());
1406 if (type
->code () != TYPE_CODE_ARRAY
1407 && type
->code () != TYPE_CODE_PTR
)
1410 error (_("cannot subscript something of type `%s'"),
1413 error (_("cannot subscript requested type"));
1416 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1417 return value::zero (type
->target_type (), arg1
->lval ());
1419 return value_subscript (arg1
, value_as_long (arg2
));
1423 /* A helper function for BINOP_EQUAL. */
1426 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1427 enum noside noside
, enum exp_opcode op
,
1428 struct value
*arg1
, struct value
*arg2
)
1430 if (binop_user_defined_p (op
, arg1
, arg2
))
1432 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1436 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1437 int tem
= value_equal (arg1
, arg2
);
1438 struct type
*type
= language_bool_type (exp
->language_defn
,
1440 return value_from_longest (type
, (LONGEST
) tem
);
1444 /* A helper function for BINOP_NOTEQUAL. */
1447 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1448 enum noside noside
, enum exp_opcode op
,
1449 struct value
*arg1
, struct value
*arg2
)
1451 if (binop_user_defined_p (op
, arg1
, arg2
))
1453 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1457 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1458 int tem
= value_equal (arg1
, arg2
);
1459 struct type
*type
= language_bool_type (exp
->language_defn
,
1461 return value_from_longest (type
, (LONGEST
) ! tem
);
1465 /* A helper function for BINOP_LESS. */
1468 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1469 enum noside noside
, enum exp_opcode op
,
1470 struct value
*arg1
, struct value
*arg2
)
1472 if (binop_user_defined_p (op
, arg1
, arg2
))
1474 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1478 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1479 int tem
= value_less (arg1
, arg2
);
1480 struct type
*type
= language_bool_type (exp
->language_defn
,
1482 return value_from_longest (type
, (LONGEST
) tem
);
1486 /* A helper function for BINOP_GTR. */
1489 eval_op_gtr (struct type
*expect_type
, struct expression
*exp
,
1490 enum noside noside
, enum exp_opcode op
,
1491 struct value
*arg1
, struct value
*arg2
)
1493 if (binop_user_defined_p (op
, arg1
, arg2
))
1495 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1499 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1500 int tem
= value_less (arg2
, arg1
);
1501 struct type
*type
= language_bool_type (exp
->language_defn
,
1503 return value_from_longest (type
, (LONGEST
) tem
);
1507 /* A helper function for BINOP_GEQ. */
1510 eval_op_geq (struct type
*expect_type
, struct expression
*exp
,
1511 enum noside noside
, enum exp_opcode op
,
1512 struct value
*arg1
, struct value
*arg2
)
1514 if (binop_user_defined_p (op
, arg1
, arg2
))
1516 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1520 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1521 int tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1522 struct type
*type
= language_bool_type (exp
->language_defn
,
1524 return value_from_longest (type
, (LONGEST
) tem
);
1528 /* A helper function for BINOP_LEQ. */
1531 eval_op_leq (struct type
*expect_type
, struct expression
*exp
,
1532 enum noside noside
, enum exp_opcode op
,
1533 struct value
*arg1
, struct value
*arg2
)
1535 if (binop_user_defined_p (op
, arg1
, arg2
))
1537 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1541 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1542 int tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1543 struct type
*type
= language_bool_type (exp
->language_defn
,
1545 return value_from_longest (type
, (LONGEST
) tem
);
1549 /* A helper function for BINOP_REPEAT. */
1552 eval_op_repeat (struct type
*expect_type
, struct expression
*exp
,
1553 enum noside noside
, enum exp_opcode op
,
1554 struct value
*arg1
, struct value
*arg2
)
1556 struct type
*type
= check_typedef (arg2
->type ());
1557 if (type
->code () != TYPE_CODE_INT
1558 && type
->code () != TYPE_CODE_ENUM
)
1559 error (_("Non-integral right operand for \"@\" operator."));
1560 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1562 return allocate_repeat_value (arg1
->type (),
1563 longest_to_int (value_as_long (arg2
)));
1566 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1569 /* A helper function for UNOP_PLUS. */
1572 eval_op_plus (struct type
*expect_type
, struct expression
*exp
,
1573 enum noside noside
, enum exp_opcode op
,
1576 if (unop_user_defined_p (op
, arg1
))
1577 return value_x_unop (arg1
, op
, noside
);
1580 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1581 return value_pos (arg1
);
1585 /* A helper function for UNOP_NEG. */
1588 eval_op_neg (struct type
*expect_type
, struct expression
*exp
,
1589 enum noside noside
, enum exp_opcode op
,
1592 if (unop_user_defined_p (op
, arg1
))
1593 return value_x_unop (arg1
, op
, noside
);
1596 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1597 return value_neg (arg1
);
1601 /* A helper function for UNOP_COMPLEMENT. */
1604 eval_op_complement (struct type
*expect_type
, struct expression
*exp
,
1605 enum noside noside
, enum exp_opcode op
,
1608 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1609 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1612 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1613 return value_complement (arg1
);
1617 /* A helper function for UNOP_LOGICAL_NOT. */
1620 eval_op_lognot (struct type
*expect_type
, struct expression
*exp
,
1621 enum noside noside
, enum exp_opcode op
,
1624 if (unop_user_defined_p (op
, arg1
))
1625 return value_x_unop (arg1
, op
, noside
);
1628 struct type
*type
= language_bool_type (exp
->language_defn
,
1630 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
1634 /* A helper function for UNOP_IND. */
1637 eval_op_ind (struct type
*expect_type
, struct expression
*exp
,
1641 struct type
*type
= check_typedef (arg1
->type ());
1642 if (type
->code () == TYPE_CODE_METHODPTR
1643 || type
->code () == TYPE_CODE_MEMBERPTR
)
1644 error (_("Attempt to dereference pointer "
1645 "to member without an object"));
1646 if (unop_user_defined_p (UNOP_IND
, arg1
))
1647 return value_x_unop (arg1
, UNOP_IND
, noside
);
1648 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1650 type
= check_typedef (arg1
->type ());
1652 /* If the type pointed to is dynamic then in order to resolve the
1653 dynamic properties we must actually dereference the pointer.
1654 There is a risk that this dereference will have side-effects
1655 in the inferior, but being able to print accurate type
1656 information seems worth the risk. */
1657 if (!type
->is_pointer_or_reference ()
1658 || !is_dynamic_type (type
->target_type ()))
1660 if (type
->is_pointer_or_reference ()
1661 /* In C you can dereference an array to get the 1st elt. */
1662 || type
->code () == TYPE_CODE_ARRAY
)
1663 return value::zero (type
->target_type (),
1665 else if (type
->code () == TYPE_CODE_INT
)
1666 /* GDB allows dereferencing an int. */
1667 return value::zero (builtin_type (exp
->gdbarch
)->builtin_int
,
1670 error (_("Attempt to take contents of a non-pointer value."));
1674 /* Allow * on an integer so we can cast it to whatever we want.
1675 This returns an int, which seems like the most C-like thing to
1676 do. "long long" variables are rare enough that
1677 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
1678 if (type
->code () == TYPE_CODE_INT
)
1679 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
1680 (CORE_ADDR
) value_as_address (arg1
));
1681 return value_ind (arg1
);
1684 /* A helper function for UNOP_ALIGNOF. */
1687 eval_op_alignof (struct type
*expect_type
, struct expression
*exp
,
1691 struct type
*type
= arg1
->type ();
1692 /* FIXME: This should be size_t. */
1693 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
1694 ULONGEST align
= type_align (type
);
1696 error (_("could not determine alignment of type"));
1697 return value_from_longest (size_type
, align
);
1700 /* A helper function for UNOP_MEMVAL. */
1703 eval_op_memval (struct type
*expect_type
, struct expression
*exp
,
1705 struct value
*arg1
, struct type
*type
)
1707 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1708 return value::zero (type
, lval_memory
);
1710 return value_at_lazy (type
, value_as_address (arg1
));
1713 /* A helper function for UNOP_PREINCREMENT. */
1716 eval_op_preinc (struct type
*expect_type
, struct expression
*exp
,
1717 enum noside noside
, enum exp_opcode op
,
1720 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1722 else if (unop_user_defined_p (op
, arg1
))
1724 return value_x_unop (arg1
, op
, noside
);
1729 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1730 arg2
= value_ptradd (arg1
, 1);
1733 struct value
*tmp
= arg1
;
1735 arg2
= value_one (arg1
->type ());
1736 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1737 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1740 return value_assign (arg1
, arg2
);
1744 /* A helper function for UNOP_PREDECREMENT. */
1747 eval_op_predec (struct type
*expect_type
, struct expression
*exp
,
1748 enum noside noside
, enum exp_opcode op
,
1751 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1753 else if (unop_user_defined_p (op
, arg1
))
1755 return value_x_unop (arg1
, op
, noside
);
1760 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1761 arg2
= value_ptradd (arg1
, -1);
1764 struct value
*tmp
= arg1
;
1766 arg2
= value_one (arg1
->type ());
1767 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1768 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1771 return value_assign (arg1
, arg2
);
1775 /* A helper function for UNOP_POSTINCREMENT. */
1778 eval_op_postinc (struct type
*expect_type
, struct expression
*exp
,
1779 enum noside noside
, enum exp_opcode op
,
1782 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1784 else if (unop_user_defined_p (op
, arg1
))
1786 return value_x_unop (arg1
, op
, noside
);
1790 struct value
*arg3
= arg1
->non_lval ();
1793 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1794 arg2
= value_ptradd (arg1
, 1);
1797 struct value
*tmp
= arg1
;
1799 arg2
= value_one (arg1
->type ());
1800 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1801 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
1804 value_assign (arg1
, arg2
);
1809 /* A helper function for UNOP_POSTDECREMENT. */
1812 eval_op_postdec (struct type
*expect_type
, struct expression
*exp
,
1813 enum noside noside
, enum exp_opcode op
,
1816 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1818 else if (unop_user_defined_p (op
, arg1
))
1820 return value_x_unop (arg1
, op
, noside
);
1824 struct value
*arg3
= arg1
->non_lval ();
1827 if (ptrmath_type_p (exp
->language_defn
, arg1
->type ()))
1828 arg2
= value_ptradd (arg1
, -1);
1831 struct value
*tmp
= arg1
;
1833 arg2
= value_one (arg1
->type ());
1834 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1835 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
1838 value_assign (arg1
, arg2
);
1843 /* A helper function for OP_TYPE. */
1846 eval_op_type (struct type
*expect_type
, struct expression
*exp
,
1847 enum noside noside
, struct type
*type
)
1849 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1850 return value::allocate (type
);
1852 error (_("Attempt to use a type name as an expression"));
1855 /* A helper function for BINOP_ASSIGN_MODIFY. */
1858 eval_binop_assign_modify (struct type
*expect_type
, struct expression
*exp
,
1859 enum noside noside
, enum exp_opcode op
,
1860 struct value
*arg1
, struct value
*arg2
)
1862 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1864 if (binop_user_defined_p (op
, arg1
, arg2
))
1865 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1866 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
1868 && is_integral_type (arg2
->type ()))
1869 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
1870 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
1872 && is_integral_type (arg2
->type ()))
1873 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
1876 struct value
*tmp
= arg1
;
1878 /* For shift and integer exponentiation operations,
1879 only promote the first argument. */
1880 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1881 && is_integral_type (arg2
->type ()))
1882 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
1884 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
1886 arg2
= value_binop (tmp
, arg2
, op
);
1888 return value_assign (arg1
, arg2
);
1891 /* Note that ARGS needs 2 empty slots up front and must end with a
1893 static struct value
*
1894 eval_op_objc_msgcall (struct type
*expect_type
, struct expression
*exp
,
1895 enum noside noside
, CORE_ADDR selector
,
1896 value
*target
, gdb::array_view
<value
*> args
)
1898 CORE_ADDR responds_selector
= 0;
1899 CORE_ADDR method_selector
= 0;
1901 int struct_return
= 0;
1903 struct value
*msg_send
= NULL
;
1904 struct value
*msg_send_stret
= NULL
;
1905 int gnu_runtime
= 0;
1907 struct value
*method
= NULL
;
1908 struct value
*called_method
= NULL
;
1910 struct type
*selector_type
= NULL
;
1911 struct type
*long_type
;
1914 struct value
*ret
= NULL
;
1919 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1920 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1922 if (value_as_long (target
) == 0)
1923 return value_from_longest (long_type
, 0);
1925 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1928 /* Find the method dispatch (Apple runtime) or method lookup
1929 (GNU runtime) function for Objective-C. These will be used
1930 to lookup the symbol information for the method. If we
1931 can't find any symbol information, then we'll use these to
1932 call the method, otherwise we can call the method
1933 directly. The msg_send_stret function is used in the special
1934 case of a method that returns a structure (Apple runtime
1938 type
= selector_type
;
1940 type
= lookup_function_type (type
);
1941 type
= lookup_pointer_type (type
);
1942 type
= lookup_function_type (type
);
1943 type
= lookup_pointer_type (type
);
1945 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1947 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1949 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1950 msg_send_stret
= value_from_pointer (type
,
1951 value_as_address (msg_send_stret
));
1955 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1956 /* Special dispatcher for methods returning structs. */
1958 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1961 /* Verify the target object responds to this method. The
1962 standard top-level 'Object' class uses a different name for
1963 the verification method than the non-standard, but more
1964 often used, 'NSObject' class. Make sure we check for both. */
1967 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1968 if (responds_selector
== 0)
1970 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1972 if (responds_selector
== 0)
1973 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1976 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1977 if (method_selector
== 0)
1979 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1981 if (method_selector
== 0)
1982 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1984 /* Call the verification method, to make sure that the target
1985 class implements the desired method. */
1987 argvec
[0] = msg_send
;
1989 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1990 argvec
[3] = value_from_longest (long_type
, selector
);
1993 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1996 /* Function objc_msg_lookup returns a pointer. */
1998 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2000 if (value_as_long (ret
) == 0)
2001 error (_("Target does not respond to this message selector."));
2003 /* Call "methodForSelector:" method, to get the address of a
2004 function method that implements this selector for this
2005 class. If we can find a symbol at that address, then we
2006 know the return type, parameter types etc. (that's a good
2009 argvec
[0] = msg_send
;
2011 argvec
[2] = value_from_longest (long_type
, method_selector
);
2012 argvec
[3] = value_from_longest (long_type
, selector
);
2015 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2019 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2022 /* ret should now be the selector. */
2024 addr
= value_as_long (ret
);
2027 struct symbol
*sym
= NULL
;
2029 /* The address might point to a function descriptor;
2030 resolve it to the actual code address instead. */
2031 addr
= gdbarch_convert_from_func_ptr_addr
2032 (exp
->gdbarch
, addr
, current_inferior ()->top_target ());
2034 /* Is it a high_level symbol? */
2035 sym
= find_pc_function (addr
);
2037 method
= value_of_variable (sym
, 0);
2040 /* If we found a method with symbol information, check to see
2041 if it returns a struct. Otherwise assume it doesn't. */
2046 struct type
*val_type
;
2048 funaddr
= find_function_addr (method
, &val_type
);
2050 block_for_pc (funaddr
);
2052 val_type
= check_typedef (val_type
);
2054 if ((val_type
== NULL
)
2055 || (val_type
->code () == TYPE_CODE_ERROR
))
2057 if (expect_type
!= NULL
)
2058 val_type
= expect_type
;
2061 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2064 else if (expect_type
!= NULL
)
2066 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2067 check_typedef (expect_type
));
2070 /* Found a function symbol. Now we will substitute its
2071 value in place of the message dispatcher (obj_msgSend),
2072 so that we call the method directly instead of thru
2073 the dispatcher. The main reason for doing this is that
2074 we can now evaluate the return value and parameter values
2075 according to their known data types, in case we need to
2076 do things like promotion, dereferencing, special handling
2077 of structs and doubles, etc.
2079 We want to use the type signature of 'method', but still
2080 jump to objc_msgSend() or objc_msgSend_stret() to better
2081 mimic the behavior of the runtime. */
2085 if (method
->type ()->code () != TYPE_CODE_FUNC
)
2086 error (_("method address has symbol information "
2087 "with non-function type; skipping"));
2089 /* Create a function pointer of the appropriate type, and
2090 replace its value with the value of msg_send or
2091 msg_send_stret. We must use a pointer here, as
2092 msg_send and msg_send_stret are of pointer type, and
2093 the representation may be different on systems that use
2094 function descriptors. */
2097 = value_from_pointer (lookup_pointer_type (method
->type ()),
2098 value_as_address (msg_send_stret
));
2101 = value_from_pointer (lookup_pointer_type (method
->type ()),
2102 value_as_address (msg_send
));
2107 called_method
= msg_send_stret
;
2109 called_method
= msg_send
;
2113 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2115 /* If the return type doesn't look like a function type,
2116 call an error. This can happen if somebody tries to
2117 turn a variable into a function call. This is here
2118 because people often want to call, eg, strcmp, which
2119 gdb doesn't know is a function. If gdb isn't asked for
2120 it's opinion (ie. through "whatis"), it won't offer
2123 struct type
*callee_type
= called_method
->type ();
2125 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2126 callee_type
= callee_type
->target_type ();
2127 callee_type
= callee_type
->target_type ();
2131 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2132 return value::allocate (expect_type
);
2134 return value::allocate (callee_type
);
2137 error (_("Expression of type other than "
2138 "\"method returning ...\" used as a method"));
2141 /* Now depending on whether we found a symbol for the method,
2142 we will either call the runtime dispatcher or the method
2146 args
[1] = value_from_longest (long_type
, selector
);
2148 if (gnu_runtime
&& (method
!= NULL
))
2150 /* Function objc_msg_lookup returns a pointer. */
2151 struct type
*tem_type
= called_method
->type ();
2152 tem_type
= lookup_pointer_type (lookup_function_type (tem_type
));
2153 called_method
->deprecated_set_type (tem_type
);
2154 called_method
= call_function_by_hand (called_method
, NULL
, args
);
2157 return call_function_by_hand (called_method
, NULL
, args
);
2160 /* Helper function for MULTI_SUBSCRIPT. */
2162 static struct value
*
2163 eval_multi_subscript (struct type
*expect_type
, struct expression
*exp
,
2164 enum noside noside
, value
*arg1
,
2165 gdb::array_view
<value
*> args
)
2167 for (value
*arg2
: args
)
2169 if (binop_user_defined_p (MULTI_SUBSCRIPT
, arg1
, arg2
))
2171 arg1
= value_x_binop (arg1
, arg2
, MULTI_SUBSCRIPT
, OP_NULL
, noside
);
2175 arg1
= coerce_ref (arg1
);
2176 struct type
*type
= check_typedef (arg1
->type ());
2178 switch (type
->code ())
2181 case TYPE_CODE_ARRAY
:
2182 case TYPE_CODE_STRING
:
2183 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2188 error (_("cannot subscript something of type `%s'"),
2191 error (_("cannot subscript requested type"));
2202 objc_msgcall_operation::evaluate (struct type
*expect_type
,
2203 struct expression
*exp
,
2206 enum noside sub_no_side
= EVAL_NORMAL
;
2207 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
2209 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2210 sub_no_side
= EVAL_NORMAL
;
2212 sub_no_side
= noside
;
2214 = std::get
<1> (m_storage
)->evaluate (selector_type
, exp
, sub_no_side
);
2216 if (value_as_long (target
) == 0)
2217 sub_no_side
= EVAL_AVOID_SIDE_EFFECTS
;
2219 sub_no_side
= noside
;
2220 std::vector
<operation_up
> &args
= std::get
<2> (m_storage
);
2221 value
**argvec
= XALLOCAVEC (struct value
*, args
.size () + 3);
2222 argvec
[0] = nullptr;
2223 argvec
[1] = nullptr;
2224 for (int i
= 0; i
< args
.size (); ++i
)
2225 argvec
[i
+ 2] = args
[i
]->evaluate_with_coercion (exp
, sub_no_side
);
2226 argvec
[args
.size () + 2] = nullptr;
2228 return eval_op_objc_msgcall (expect_type
, exp
, noside
, std::
2229 get
<0> (m_storage
), target
,
2230 gdb::make_array_view (argvec
,
2235 multi_subscript_operation::evaluate (struct type
*expect_type
,
2236 struct expression
*exp
,
2239 value
*arg1
= std::get
<0> (m_storage
)->evaluate_with_coercion (exp
, noside
);
2240 std::vector
<operation_up
> &values
= std::get
<1> (m_storage
);
2241 value
**argvec
= XALLOCAVEC (struct value
*, values
.size ());
2242 for (int ix
= 0; ix
< values
.size (); ++ix
)
2243 argvec
[ix
] = values
[ix
]->evaluate_with_coercion (exp
, noside
);
2244 return eval_multi_subscript (expect_type
, exp
, noside
, arg1
,
2245 gdb::make_array_view (argvec
, values
.size ()));
2249 logical_and_operation::evaluate (struct type
*expect_type
,
2250 struct expression
*exp
,
2253 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2255 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2256 EVAL_AVOID_SIDE_EFFECTS
);
2258 if (binop_user_defined_p (BINOP_LOGICAL_AND
, arg1
, arg2
))
2260 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2261 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_AND
, OP_NULL
, noside
);
2265 bool tem
= value_logical_not (arg1
);
2268 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2269 tem
= value_logical_not (arg2
);
2271 struct type
*type
= language_bool_type (exp
->language_defn
,
2273 return value_from_longest (type
, !tem
);
2278 logical_or_operation::evaluate (struct type
*expect_type
,
2279 struct expression
*exp
,
2282 value
*arg1
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2284 value
*arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
,
2285 EVAL_AVOID_SIDE_EFFECTS
);
2287 if (binop_user_defined_p (BINOP_LOGICAL_OR
, arg1
, arg2
))
2289 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2290 return value_x_binop (arg1
, arg2
, BINOP_LOGICAL_OR
, OP_NULL
, noside
);
2294 bool tem
= value_logical_not (arg1
);
2297 arg2
= std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
);
2298 tem
= value_logical_not (arg2
);
2301 struct type
*type
= language_bool_type (exp
->language_defn
,
2303 return value_from_longest (type
, !tem
);
2308 adl_func_operation::evaluate (struct type
*expect_type
,
2309 struct expression
*exp
,
2312 std::vector
<operation_up
> &arg_ops
= std::get
<2> (m_storage
);
2313 std::vector
<value
*> args (arg_ops
.size ());
2314 for (int i
= 0; i
< arg_ops
.size (); ++i
)
2315 args
[i
] = arg_ops
[i
]->evaluate_with_coercion (exp
, noside
);
2317 struct symbol
*symp
;
2318 find_overload_match (args
, std::get
<0> (m_storage
).c_str (),
2321 nullptr, &symp
, nullptr, 0, noside
);
2322 if (symp
->type ()->code () == TYPE_CODE_ERROR
)
2323 error_unknown_type (symp
->print_name ());
2324 value
*callee
= evaluate_var_value (noside
, std::get
<1> (m_storage
), symp
);
2325 return evaluate_subexp_do_call (exp
, noside
, callee
, args
,
2326 nullptr, expect_type
);
2330 /* This function evaluates brace-initializers (in C/C++) for
2334 array_operation::evaluate_struct_tuple (struct value
*struct_val
,
2335 struct expression
*exp
,
2336 enum noside noside
, int nargs
)
2338 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2339 struct type
*struct_type
= check_typedef (struct_val
->type ());
2340 struct type
*field_type
;
2344 while (--nargs
>= 0)
2346 struct value
*val
= NULL
;
2347 int bitpos
, bitsize
;
2351 /* Skip static fields. */
2352 while (fieldno
< struct_type
->num_fields ()
2353 && struct_type
->field (fieldno
).is_static ())
2355 if (fieldno
>= struct_type
->num_fields ())
2356 error (_("too many initializers"));
2357 field_type
= struct_type
->field (fieldno
).type ();
2358 if (field_type
->code () == TYPE_CODE_UNION
2359 && struct_type
->field (fieldno
).name ()[0] == '0')
2360 error (_("don't know which variant you want to set"));
2362 /* Here, struct_type is the type of the inner struct,
2363 while substruct_type is the type of the inner struct.
2364 These are the same for normal structures, but a variant struct
2365 contains anonymous union fields that contain substruct fields.
2366 The value fieldno is the index of the top-level (normal or
2367 anonymous union) field in struct_field, while the value
2368 subfieldno is the index of the actual real (named inner) field
2369 in substruct_type. */
2371 field_type
= struct_type
->field (fieldno
).type ();
2373 val
= in_args
[idx
++]->evaluate (field_type
, exp
, noside
);
2375 /* Now actually set the field in struct_val. */
2377 /* Assign val to field fieldno. */
2378 if (val
->type () != field_type
)
2379 val
= value_cast (field_type
, val
);
2381 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
2382 bitpos
= struct_type
->field (fieldno
).loc_bitpos ();
2383 addr
= struct_val
->contents_writeable ().data () + bitpos
/ 8;
2385 modify_field (struct_type
, addr
,
2386 value_as_long (val
), bitpos
% 8, bitsize
);
2388 memcpy (addr
, val
->contents ().data (),
2389 val
->type ()->length ());
2396 array_operation::evaluate (struct type
*expect_type
,
2397 struct expression
*exp
,
2401 int tem2
= std::get
<0> (m_storage
);
2402 int tem3
= std::get
<1> (m_storage
);
2403 const std::vector
<operation_up
> &in_args
= std::get
<2> (m_storage
);
2404 int nargs
= tem3
- tem2
+ 1;
2405 struct type
*type
= expect_type
? check_typedef (expect_type
) : nullptr;
2407 if (expect_type
!= nullptr
2408 && type
->code () == TYPE_CODE_STRUCT
)
2410 struct value
*rec
= value::allocate (expect_type
);
2412 memset (rec
->contents_raw ().data (), '\0', type
->length ());
2413 return evaluate_struct_tuple (rec
, exp
, noside
, nargs
);
2416 if (expect_type
!= nullptr
2417 && type
->code () == TYPE_CODE_ARRAY
)
2419 struct type
*range_type
= type
->index_type ();
2420 struct type
*element_type
= type
->target_type ();
2421 struct value
*array
= value::allocate (expect_type
);
2422 int element_size
= check_typedef (element_type
)->length ();
2423 LONGEST low_bound
, high_bound
, index
;
2425 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
2428 high_bound
= (type
->length () / element_size
) - 1;
2431 memset (array
->contents_raw ().data (), 0, expect_type
->length ());
2432 for (tem
= nargs
; --nargs
>= 0;)
2434 struct value
*element
;
2436 element
= in_args
[index
- low_bound
]->evaluate (element_type
,
2438 if (element
->type () != element_type
)
2439 element
= value_cast (element_type
, element
);
2440 if (index
> high_bound
)
2441 /* To avoid memory corruption. */
2442 error (_("Too many array elements"));
2443 memcpy (array
->contents_raw ().data ()
2444 + (index
- low_bound
) * element_size
,
2445 element
->contents ().data (),
2452 if (expect_type
!= nullptr
2453 && type
->code () == TYPE_CODE_SET
)
2455 struct value
*set
= value::allocate (expect_type
);
2456 gdb_byte
*valaddr
= set
->contents_raw ().data ();
2457 struct type
*element_type
= type
->index_type ();
2458 struct type
*check_type
= element_type
;
2459 LONGEST low_bound
, high_bound
;
2461 /* Get targettype of elementtype. */
2462 while (check_type
->code () == TYPE_CODE_RANGE
2463 || check_type
->code () == TYPE_CODE_TYPEDEF
)
2464 check_type
= check_type
->target_type ();
2466 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
2467 error (_("(power)set type with unknown size"));
2468 memset (valaddr
, '\0', type
->length ());
2470 for (tem
= 0; tem
< nargs
; tem
++)
2472 LONGEST range_low
, range_high
;
2473 struct type
*range_low_type
, *range_high_type
;
2474 struct value
*elem_val
;
2476 elem_val
= in_args
[idx
++]->evaluate (element_type
, exp
, noside
);
2477 range_low_type
= range_high_type
= elem_val
->type ();
2478 range_low
= range_high
= value_as_long (elem_val
);
2480 /* Check types of elements to avoid mixture of elements from
2481 different types. Also check if type of element is "compatible"
2482 with element type of powerset. */
2483 if (range_low_type
->code () == TYPE_CODE_RANGE
)
2484 range_low_type
= range_low_type
->target_type ();
2485 if (range_high_type
->code () == TYPE_CODE_RANGE
)
2486 range_high_type
= range_high_type
->target_type ();
2487 if ((range_low_type
->code () != range_high_type
->code ())
2488 || (range_low_type
->code () == TYPE_CODE_ENUM
2489 && (range_low_type
!= range_high_type
)))
2490 /* different element modes. */
2491 error (_("POWERSET tuple elements of different mode"));
2492 if ((check_type
->code () != range_low_type
->code ())
2493 || (check_type
->code () == TYPE_CODE_ENUM
2494 && range_low_type
!= check_type
))
2495 error (_("incompatible POWERSET tuple elements"));
2496 if (range_low
> range_high
)
2498 warning (_("empty POWERSET tuple range"));
2501 if (range_low
< low_bound
|| range_high
> high_bound
)
2502 error (_("POWERSET tuple element out of range"));
2503 range_low
-= low_bound
;
2504 range_high
-= low_bound
;
2505 for (; range_low
<= range_high
; range_low
++)
2507 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
2509 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
2510 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
2511 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
2518 value
**argvec
= XALLOCAVEC (struct value
*, nargs
);
2519 for (tem
= 0; tem
< nargs
; tem
++)
2521 /* Ensure that array expressions are coerced into pointer
2523 argvec
[tem
] = in_args
[tem
]->evaluate_with_coercion (exp
, noside
);
2525 return value_array (tem2
, tem3
, argvec
);
2529 unop_extract_operation::evaluate (struct type
*expect_type
,
2530 struct expression
*exp
,
2533 value
*old_value
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2534 struct type
*type
= get_type ();
2536 if (type
->length () > old_value
->type ()->length ())
2537 error (_("length type is larger than the value type"));
2539 struct value
*result
= value::allocate (type
);
2540 old_value
->contents_copy (result
, 0, 0, type
->length ());
2547 /* Helper for evaluate_subexp_for_address. */
2550 evaluate_subexp_for_address_base (struct expression
*exp
, enum noside noside
,
2553 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2555 struct type
*type
= check_typedef (x
->type ());
2557 if (TYPE_IS_REFERENCE (type
))
2558 return value::zero (lookup_pointer_type (type
->target_type ()),
2560 else if (x
->lval () == lval_memory
|| value_must_coerce_to_target (x
))
2561 return value::zero (lookup_pointer_type (x
->type ()),
2564 error (_("Attempt to take address of "
2565 "value not located in memory."));
2567 return value_addr (x
);
2574 operation::evaluate_for_cast (struct type
*expect_type
,
2575 struct expression
*exp
,
2578 value
*val
= evaluate (expect_type
, exp
, noside
);
2579 return value_cast (expect_type
, val
);
2583 operation::evaluate_for_address (struct expression
*exp
, enum noside noside
)
2585 value
*val
= evaluate (nullptr, exp
, noside
);
2586 return evaluate_subexp_for_address_base (exp
, noside
, val
);
2590 scope_operation::evaluate_for_address (struct expression
*exp
,
2593 value
*x
= value_aggregate_elt (std::get
<0> (m_storage
),
2594 std::get
<1> (m_storage
).c_str (),
2597 error (_("There is no field named %s"), std::get
<1> (m_storage
).c_str ());
2602 unop_ind_base_operation::evaluate_for_address (struct expression
*exp
,
2605 value
*x
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
);
2607 /* We can't optimize out "&*" if there's a user-defined operator*. */
2608 if (unop_user_defined_p (UNOP_IND
, x
))
2610 x
= value_x_unop (x
, UNOP_IND
, noside
);
2611 return evaluate_subexp_for_address_base (exp
, noside
, x
);
2614 return coerce_array (x
);
2618 var_msym_value_operation::evaluate_for_address (struct expression
*exp
,
2621 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2622 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2623 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2625 struct type
*type
= lookup_pointer_type (val
->type ());
2626 return value::zero (type
, not_lval
);
2629 return value_addr (val
);
2633 unop_memval_operation::evaluate_for_address (struct expression
*exp
,
2636 return value_cast (lookup_pointer_type (std::get
<1> (m_storage
)),
2637 std::get
<0> (m_storage
)->evaluate (nullptr, exp
, noside
));
2641 unop_memval_type_operation::evaluate_for_address (struct expression
*exp
,
2644 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2645 EVAL_AVOID_SIDE_EFFECTS
);
2646 struct type
*type
= typeval
->type ();
2647 return value_cast (lookup_pointer_type (type
),
2648 std::get
<1> (m_storage
)->evaluate (nullptr, exp
, noside
));
2652 var_value_operation::evaluate_for_address (struct expression
*exp
,
2655 symbol
*var
= std::get
<0> (m_storage
).symbol
;
2657 /* C++: The "address" of a reference should yield the address
2658 * of the object pointed to. Let value_addr() deal with it. */
2659 if (TYPE_IS_REFERENCE (var
->type ()))
2660 return operation::evaluate_for_address (exp
, noside
);
2662 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2664 struct type
*type
= lookup_pointer_type (var
->type ());
2665 enum address_class sym_class
= var
->aclass ();
2667 if (sym_class
== LOC_CONST
2668 || sym_class
== LOC_CONST_BYTES
2669 || sym_class
== LOC_REGISTER
)
2670 error (_("Attempt to take address of register or constant."));
2672 return value::zero (type
, not_lval
);
2675 return address_of_variable (var
, std::get
<0> (m_storage
).block
);
2679 var_value_operation::evaluate_with_coercion (struct expression
*exp
,
2682 struct symbol
*var
= std::get
<0> (m_storage
).symbol
;
2683 struct type
*type
= check_typedef (var
->type ());
2684 if (type
->code () == TYPE_CODE_ARRAY
2685 && !type
->is_vector ()
2686 && CAST_IS_CONVERSION (exp
->language_defn
))
2688 struct value
*val
= address_of_variable (var
,
2689 std::get
<0> (m_storage
).block
);
2690 return value_cast (lookup_pointer_type (type
->target_type ()), val
);
2692 return evaluate (nullptr, exp
, noside
);
2697 /* Helper function for evaluating the size of a type. */
2700 evaluate_subexp_for_sizeof_base (struct expression
*exp
, struct type
*type
)
2702 /* FIXME: This should be size_t. */
2703 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2704 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
2705 "When applied to a reference or a reference type, the result is
2706 the size of the referenced type." */
2707 type
= check_typedef (type
);
2708 if (exp
->language_defn
->la_language
== language_cplus
2709 && (TYPE_IS_REFERENCE (type
)))
2710 type
= check_typedef (type
->target_type ());
2711 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2718 operation::evaluate_for_sizeof (struct expression
*exp
, enum noside noside
)
2720 value
*val
= evaluate (nullptr, exp
, EVAL_AVOID_SIDE_EFFECTS
);
2721 return evaluate_subexp_for_sizeof_base (exp
, val
->type ());
2725 var_msym_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2729 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2730 value
*mval
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2732 struct type
*type
= mval
->type ();
2733 if (type
->code () == TYPE_CODE_ERROR
)
2734 error_unknown_type (b
.minsym
->print_name ());
2736 /* FIXME: This should be size_t. */
2737 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2738 return value_from_longest (size_type
, type
->length ());
2742 subscript_operation::evaluate_for_sizeof (struct expression
*exp
,
2745 if (noside
== EVAL_NORMAL
)
2747 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2748 EVAL_AVOID_SIDE_EFFECTS
);
2749 struct type
*type
= check_typedef (val
->type ());
2750 if (type
->code () == TYPE_CODE_ARRAY
)
2752 type
= check_typedef (type
->target_type ());
2753 if (type
->code () == TYPE_CODE_ARRAY
)
2755 type
= type
->index_type ();
2756 /* Only re-evaluate the right hand side if the resulting type
2757 is a variable length type. */
2758 if (type
->bounds ()->flag_bound_evaluated
)
2760 val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2761 /* FIXME: This should be size_t. */
2762 struct type
*size_type
2763 = builtin_type (exp
->gdbarch
)->builtin_int
;
2764 return value_from_longest
2765 (size_type
, (LONGEST
) val
->type ()->length ());
2771 return operation::evaluate_for_sizeof (exp
, noside
);
2775 unop_ind_base_operation::evaluate_for_sizeof (struct expression
*exp
,
2778 value
*val
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2779 EVAL_AVOID_SIDE_EFFECTS
);
2780 struct type
*type
= check_typedef (val
->type ());
2781 if (!type
->is_pointer_or_reference ()
2782 && type
->code () != TYPE_CODE_ARRAY
)
2783 error (_("Attempt to take contents of a non-pointer value."));
2784 type
= type
->target_type ();
2785 if (is_dynamic_type (type
))
2786 type
= value_ind (val
)->type ();
2787 /* FIXME: This should be size_t. */
2788 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2789 return value_from_longest (size_type
, (LONGEST
) type
->length ());
2793 unop_memval_operation::evaluate_for_sizeof (struct expression
*exp
,
2796 return evaluate_subexp_for_sizeof_base (exp
, std::get
<1> (m_storage
));
2800 unop_memval_type_operation::evaluate_for_sizeof (struct expression
*exp
,
2803 value
*typeval
= std::get
<0> (m_storage
)->evaluate (nullptr, exp
,
2804 EVAL_AVOID_SIDE_EFFECTS
);
2805 return evaluate_subexp_for_sizeof_base (exp
, typeval
->type ());
2809 var_value_operation::evaluate_for_sizeof (struct expression
*exp
,
2812 struct type
*type
= std::get
<0> (m_storage
).symbol
->type ();
2813 if (is_dynamic_type (type
))
2815 value
*val
= evaluate (nullptr, exp
, EVAL_NORMAL
);
2816 type
= val
->type ();
2817 if (type
->code () == TYPE_CODE_ARRAY
)
2819 /* FIXME: This should be size_t. */
2820 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2821 if (type_not_allocated (type
) || type_not_associated (type
))
2822 return value::zero (size_type
, not_lval
);
2823 else if (is_dynamic_type (type
->index_type ())
2824 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
2825 return value::allocate_optimized_out (size_type
);
2828 return evaluate_subexp_for_sizeof_base (exp
, type
);
2832 var_msym_value_operation::evaluate_for_cast (struct type
*to_type
,
2833 struct expression
*exp
,
2836 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2837 return value::zero (to_type
, not_lval
);
2839 const bound_minimal_symbol
&b
= std::get
<0> (m_storage
);
2840 value
*val
= evaluate_var_msym_value (noside
, b
.objfile
, b
.minsym
);
2842 val
= value_cast (to_type
, val
);
2844 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2845 if (val
->lval () == lval_memory
)
2849 val
->set_lval (not_lval
);
2855 var_value_operation::evaluate_for_cast (struct type
*to_type
,
2856 struct expression
*exp
,
2859 value
*val
= evaluate_var_value (noside
,
2860 std::get
<0> (m_storage
).block
,
2861 std::get
<0> (m_storage
).symbol
);
2863 val
= value_cast (to_type
, val
);
2865 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
2866 if (val
->lval () == lval_memory
)
2870 val
->set_lval (not_lval
);
2877 /* Parse a type expression in the string [P..P+LENGTH). */
2880 parse_and_eval_type (const char *p
, int length
)
2882 char *tmp
= (char *) alloca (length
+ 4);
2885 memcpy (tmp
+ 1, p
, length
);
2886 tmp
[length
+ 1] = ')';
2887 tmp
[length
+ 2] = '0';
2888 tmp
[length
+ 3] = '\0';
2889 expression_up expr
= parse_expression (tmp
);
2890 expr::unop_cast_operation
*op
2891 = dynamic_cast<expr::unop_cast_operation
*> (expr
->op
.get ());
2893 error (_("Internal error in eval_type."));
2894 return op
->get_type ();