1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2020 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 "gdb_obstack.h"
41 #include "typeprint.h"
44 /* Prototypes for local functions. */
46 static struct value
*evaluate_subexp_for_sizeof (struct expression
*, int *,
49 static struct value
*evaluate_subexp_for_address (struct expression
*,
52 static value
*evaluate_subexp_for_cast (expression
*exp
, int *pos
,
56 static struct value
*evaluate_struct_tuple (struct value
*,
57 struct expression
*, int *,
60 static LONGEST
init_array_element (struct value
*, struct value
*,
61 struct expression
*, int *, enum noside
,
65 evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
66 int *pos
, enum noside noside
)
70 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
71 if (*pos
== 0 && target_has_execution ()
72 && exp
->language_defn
->la_language
== language_cplus
73 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
74 stack_temporaries
.emplace (inferior_thread ());
76 retval
= (*exp
->language_defn
->expression_ops ()->evaluate_exp
)
77 (expect_type
, exp
, pos
, noside
);
79 if (stack_temporaries
.has_value ()
80 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
81 retval
= value_non_lval (retval
);
86 /* Parse the string EXP as a C expression, evaluate it,
87 and return the result as a number. */
90 parse_and_eval_address (const char *exp
)
92 expression_up expr
= parse_expression (exp
);
94 return value_as_address (evaluate_expression (expr
.get ()));
97 /* Like parse_and_eval_address, but treats the value of the expression
98 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
100 parse_and_eval_long (const char *exp
)
102 expression_up expr
= parse_expression (exp
);
104 return value_as_long (evaluate_expression (expr
.get ()));
108 parse_and_eval (const char *exp
)
110 expression_up expr
= parse_expression (exp
);
112 return evaluate_expression (expr
.get ());
115 /* Parse up to a comma (or to a closeparen)
116 in the string EXPP as an expression, evaluate it, and return the value.
117 EXPP is advanced to point to the comma. */
120 parse_to_comma_and_eval (const char **expp
)
122 expression_up expr
= parse_exp_1 (expp
, 0, nullptr, 1);
124 return evaluate_expression (expr
.get ());
127 /* Evaluate an expression in internal prefix form
128 such as is constructed by parse.y.
130 See expression.h for info on the format of an expression. */
133 evaluate_expression (struct expression
*exp
)
137 return evaluate_subexp (nullptr, exp
, &pc
, EVAL_NORMAL
);
140 /* Evaluate an expression, avoiding all memory references
141 and getting a value whose type alone is correct. */
144 evaluate_type (struct expression
*exp
)
148 return evaluate_subexp (nullptr, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
151 /* Evaluate a subexpression, avoiding all memory references and
152 getting a value whose type alone is correct. */
155 evaluate_subexpression_type (struct expression
*exp
, int subexp
)
157 return evaluate_subexp (nullptr, exp
, &subexp
, EVAL_AVOID_SIDE_EFFECTS
);
160 /* Find the current value of a watchpoint on EXP. Return the value in
161 *VALP and *RESULTP and the chain of intermediate and final values
162 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
165 If PRESERVE_ERRORS is true, then exceptions are passed through.
166 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
167 occurs while evaluating the expression, *RESULTP will be set to
168 NULL. *RESULTP may be a lazy value, if the result could not be
169 read from memory. It is used to determine whether a value is
170 user-specified (we should watch the whole value) or intermediate
171 (we should watch only the bit used to locate the final value).
173 If the final value, or any intermediate value, could not be read
174 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
175 set to any referenced values. *VALP will never be a lazy value.
176 This is the value which we store in struct breakpoint.
178 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
179 released from the value chain. If VAL_CHAIN is NULL, all generated
180 values will be left on the value chain. */
183 fetch_subexp_value (struct expression
*exp
, int *pc
, struct value
**valp
,
184 struct value
**resultp
,
185 std::vector
<value_ref_ptr
> *val_chain
,
188 struct value
*mark
, *new_mark
, *result
;
196 /* Evaluate the expression. */
197 mark
= value_mark ();
202 result
= evaluate_subexp (nullptr, exp
, pc
, EVAL_NORMAL
);
204 catch (const gdb_exception
&ex
)
206 /* Ignore memory errors if we want watchpoints pointing at
207 inaccessible memory to still be created; otherwise, throw the
208 error to some higher catcher. */
212 if (!preserve_errors
)
221 new_mark
= value_mark ();
222 if (mark
== new_mark
)
227 /* Make sure it's not lazy, so that after the target stops again we
228 have a non-lazy previous value to compare with. */
231 if (!value_lazy (result
))
238 value_fetch_lazy (result
);
241 catch (const gdb_exception_error
&except
)
249 /* Return the chain of intermediate values. We use this to
250 decide which addresses to watch. */
251 *val_chain
= value_release_to_mark (mark
);
255 /* Extract a field operation from an expression. If the subexpression
256 of EXP starting at *SUBEXP is not a structure dereference
257 operation, return NULL. Otherwise, return the name of the
258 dereferenced field, and advance *SUBEXP to point to the
259 subexpression of the left-hand-side of the dereference. This is
260 used when completing field names. */
263 extract_field_op (struct expression
*exp
, int *subexp
)
268 if (exp
->elts
[*subexp
].opcode
!= STRUCTOP_STRUCT
269 && exp
->elts
[*subexp
].opcode
!= STRUCTOP_PTR
)
271 tem
= longest_to_int (exp
->elts
[*subexp
+ 1].longconst
);
272 result
= &exp
->elts
[*subexp
+ 2].string
;
273 (*subexp
) += 1 + 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
277 /* This function evaluates brace-initializers (in C/C++) for
280 static struct value
*
281 evaluate_struct_tuple (struct value
*struct_val
,
282 struct expression
*exp
,
283 int *pos
, enum noside noside
, int nargs
)
285 struct type
*struct_type
= check_typedef (value_type (struct_val
));
286 struct type
*field_type
;
291 struct value
*val
= NULL
;
296 /* Skip static fields. */
297 while (fieldno
< struct_type
->num_fields ()
298 && field_is_static (&struct_type
->field (fieldno
)))
300 if (fieldno
>= struct_type
->num_fields ())
301 error (_("too many initializers"));
302 field_type
= struct_type
->field (fieldno
).type ();
303 if (field_type
->code () == TYPE_CODE_UNION
304 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
305 error (_("don't know which variant you want to set"));
307 /* Here, struct_type is the type of the inner struct,
308 while substruct_type is the type of the inner struct.
309 These are the same for normal structures, but a variant struct
310 contains anonymous union fields that contain substruct fields.
311 The value fieldno is the index of the top-level (normal or
312 anonymous union) field in struct_field, while the value
313 subfieldno is the index of the actual real (named inner) field
314 in substruct_type. */
316 field_type
= struct_type
->field (fieldno
).type ();
318 val
= evaluate_subexp (field_type
, exp
, pos
, noside
);
320 /* Now actually set the field in struct_val. */
322 /* Assign val to field fieldno. */
323 if (value_type (val
) != field_type
)
324 val
= value_cast (field_type
, val
);
326 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
327 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
328 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
330 modify_field (struct_type
, addr
,
331 value_as_long (val
), bitpos
% 8, bitsize
);
333 memcpy (addr
, value_contents (val
),
334 TYPE_LENGTH (value_type (val
)));
340 /* Recursive helper function for setting elements of array tuples.
341 The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND); the
342 element value is ELEMENT; EXP, POS and NOSIDE are as usual.
343 Evaluates index expressions and sets the specified element(s) of
344 ARRAY to ELEMENT. Returns last index value. */
347 init_array_element (struct value
*array
, struct value
*element
,
348 struct expression
*exp
, int *pos
,
349 enum noside noside
, LONGEST low_bound
, LONGEST high_bound
)
352 int element_size
= TYPE_LENGTH (value_type (element
));
354 if (exp
->elts
[*pos
].opcode
== BINOP_COMMA
)
357 init_array_element (array
, element
, exp
, pos
, noside
,
358 low_bound
, high_bound
);
359 return init_array_element (array
, element
,
360 exp
, pos
, noside
, low_bound
, high_bound
);
364 index
= value_as_long (evaluate_subexp (nullptr, exp
, pos
, noside
));
365 if (index
< low_bound
|| index
> high_bound
)
366 error (_("tuple index out of range"));
367 memcpy (value_contents_raw (array
) + (index
- low_bound
) * element_size
,
368 value_contents (element
), element_size
);
373 /* Promote value ARG1 as appropriate before performing a unary operation
375 If the result is not appropriate for any particular language then it
376 needs to patch this function. */
379 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
384 *arg1
= coerce_ref (*arg1
);
385 type1
= check_typedef (value_type (*arg1
));
387 if (is_integral_type (type1
))
389 switch (language
->la_language
)
392 /* Perform integral promotion for ANSI C/C++.
393 If not appropriate for any particular language
394 it needs to modify this function. */
396 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
398 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_int
))
399 *arg1
= value_cast (builtin_int
, *arg1
);
406 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
407 operation on those two operands.
408 If the result is not appropriate for any particular language then it
409 needs to patch this function. */
412 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
413 struct value
**arg1
, struct value
**arg2
)
415 struct type
*promoted_type
= NULL
;
419 *arg1
= coerce_ref (*arg1
);
420 *arg2
= coerce_ref (*arg2
);
422 type1
= check_typedef (value_type (*arg1
));
423 type2
= check_typedef (value_type (*arg2
));
425 if ((type1
->code () != TYPE_CODE_FLT
426 && type1
->code () != TYPE_CODE_DECFLOAT
427 && !is_integral_type (type1
))
428 || (type2
->code () != TYPE_CODE_FLT
429 && type2
->code () != TYPE_CODE_DECFLOAT
430 && !is_integral_type (type2
)))
433 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
436 if (type1
->code () == TYPE_CODE_DECFLOAT
437 || type2
->code () == TYPE_CODE_DECFLOAT
)
439 /* No promotion required. */
441 else if (type1
->code () == TYPE_CODE_FLT
442 || type2
->code () == TYPE_CODE_FLT
)
444 switch (language
->la_language
)
450 case language_opencl
:
451 /* No promotion required. */
455 /* For other languages the result type is unchanged from gdb
456 version 6.7 for backward compatibility.
457 If either arg was long double, make sure that value is also long
458 double. Otherwise use double. */
459 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (gdbarch
)
460 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (gdbarch
))
461 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
463 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
467 else if (type1
->code () == TYPE_CODE_BOOL
468 && type2
->code () == TYPE_CODE_BOOL
)
470 /* No promotion required. */
473 /* Integral operations here. */
474 /* FIXME: Also mixed integral/booleans, with result an integer. */
476 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
477 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
478 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
479 int is_unsigned1
= type1
->is_unsigned ();
480 int is_unsigned2
= type2
->is_unsigned ();
481 unsigned int result_len
;
482 int unsigned_operation
;
484 /* Determine type length and signedness after promotion for
486 if (promoted_len1
< TYPE_LENGTH (builtin
->builtin_int
))
489 promoted_len1
= TYPE_LENGTH (builtin
->builtin_int
);
491 if (promoted_len2
< TYPE_LENGTH (builtin
->builtin_int
))
494 promoted_len2
= TYPE_LENGTH (builtin
->builtin_int
);
497 if (promoted_len1
> promoted_len2
)
499 unsigned_operation
= is_unsigned1
;
500 result_len
= promoted_len1
;
502 else if (promoted_len2
> promoted_len1
)
504 unsigned_operation
= is_unsigned2
;
505 result_len
= promoted_len2
;
509 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
510 result_len
= promoted_len1
;
513 switch (language
->la_language
)
519 if (result_len
<= TYPE_LENGTH (builtin
->builtin_int
))
521 promoted_type
= (unsigned_operation
522 ? builtin
->builtin_unsigned_int
523 : builtin
->builtin_int
);
525 else if (result_len
<= TYPE_LENGTH (builtin
->builtin_long
))
527 promoted_type
= (unsigned_operation
528 ? builtin
->builtin_unsigned_long
529 : builtin
->builtin_long
);
533 promoted_type
= (unsigned_operation
534 ? builtin
->builtin_unsigned_long_long
535 : builtin
->builtin_long_long
);
538 case language_opencl
:
539 if (result_len
<= TYPE_LENGTH (lookup_signed_typename
544 ? lookup_unsigned_typename (language
, "int")
545 : lookup_signed_typename (language
, "int"));
547 else if (result_len
<= TYPE_LENGTH (lookup_signed_typename
552 ? lookup_unsigned_typename (language
, "long")
553 : lookup_signed_typename (language
,"long"));
557 /* For other languages the result type is unchanged from gdb
558 version 6.7 for backward compatibility.
559 If either arg was long long, make sure that value is also long
560 long. Otherwise use long. */
561 if (unsigned_operation
)
563 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
564 promoted_type
= builtin
->builtin_unsigned_long_long
;
566 promoted_type
= builtin
->builtin_unsigned_long
;
570 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
571 promoted_type
= builtin
->builtin_long_long
;
573 promoted_type
= builtin
->builtin_long
;
581 /* Promote both operands to common type. */
582 *arg1
= value_cast (promoted_type
, *arg1
);
583 *arg2
= value_cast (promoted_type
, *arg2
);
588 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
590 type
= check_typedef (type
);
591 if (TYPE_IS_REFERENCE (type
))
592 type
= TYPE_TARGET_TYPE (type
);
594 switch (type
->code ())
600 case TYPE_CODE_ARRAY
:
601 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
608 /* Represents a fake method with the given parameter types. This is
609 used by the parser to construct a temporary "expected" type for
610 method overload resolution. FLAGS is used as instance flags of the
611 new type, in order to be able to make the new type represent a
612 const/volatile overload. */
617 fake_method (type_instance_flags flags
,
618 int num_types
, struct type
**param_types
);
621 /* The constructed type. */
622 struct type
*type () { return &m_type
; }
625 struct type m_type
{};
626 main_type m_main_type
{};
629 fake_method::fake_method (type_instance_flags flags
,
630 int num_types
, struct type
**param_types
)
632 struct type
*type
= &m_type
;
634 TYPE_MAIN_TYPE (type
) = &m_main_type
;
635 TYPE_LENGTH (type
) = 1;
636 type
->set_code (TYPE_CODE_METHOD
);
637 TYPE_CHAIN (type
) = type
;
638 type
->set_instance_flags (flags
);
641 if (param_types
[num_types
- 1] == NULL
)
644 type
->set_has_varargs (true);
646 else if (check_typedef (param_types
[num_types
- 1])->code ()
650 /* Caller should have ensured this. */
651 gdb_assert (num_types
== 0);
652 type
->set_is_prototyped (true);
656 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
657 neither an objfile nor a gdbarch. As a result we must manually
658 allocate memory for auxiliary fields, and free the memory ourselves
659 when we are done with it. */
660 type
->set_num_fields (num_types
);
662 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
664 while (num_types
-- > 0)
665 type
->field (num_types
).set_type (param_types
[num_types
]);
668 fake_method::~fake_method ()
670 xfree (m_type
.fields ());
673 /* Helper for evaluating an OP_VAR_VALUE. */
676 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
678 /* JYG: We used to just return value_zero of the symbol type if
679 we're asked to avoid side effects. Otherwise we return
680 value_of_variable (...). However I'm not sure if
681 value_of_variable () has any side effect. We need a full value
682 object returned here for whatis_exp () to call evaluate_type ()
683 and then pass the full value to value_rtti_target_type () if we
684 are dealing with a pointer or reference to a base class and print
687 struct value
*ret
= NULL
;
691 ret
= value_of_variable (var
, blk
);
694 catch (const gdb_exception_error
&except
)
696 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
699 ret
= value_zero (SYMBOL_TYPE (var
), not_lval
);
705 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
708 evaluate_var_msym_value (enum noside noside
,
709 struct objfile
*objfile
, minimal_symbol
*msymbol
)
712 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
714 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
715 return value_zero (the_type
, not_lval
);
717 return value_at_lazy (the_type
, address
);
720 /* Helper for returning a value when handling EVAL_SKIP. */
723 eval_skip_value (expression
*exp
)
725 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
728 /* See expression.h. */
731 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
732 int nargs
, value
**argvec
,
733 const char *function_name
,
734 type
*default_return_type
)
736 if (argvec
[0] == NULL
)
737 error (_("Cannot evaluate function -- may be inlined"));
738 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
740 /* If the return type doesn't look like a function type,
741 call an error. This can happen if somebody tries to turn
742 a variable into a function call. */
744 type
*ftype
= value_type (argvec
[0]);
746 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
748 /* We don't know anything about what the internal
749 function might return, but we have to return
751 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
754 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
757 = result_type_of_xmethod (argvec
[0],
758 gdb::make_array_view (argvec
+ 1,
761 if (return_type
== NULL
)
762 error (_("Xmethod is missing return type."));
763 return value_zero (return_type
, not_lval
);
765 else if (ftype
->code () == TYPE_CODE_FUNC
766 || ftype
->code () == TYPE_CODE_METHOD
)
768 if (ftype
->is_gnu_ifunc ())
770 CORE_ADDR address
= value_address (argvec
[0]);
771 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
773 if (resolved_type
!= NULL
)
774 ftype
= resolved_type
;
777 type
*return_type
= TYPE_TARGET_TYPE (ftype
);
779 if (return_type
== NULL
)
780 return_type
= default_return_type
;
782 if (return_type
== NULL
)
783 error_call_unknown_return_type (function_name
);
785 return allocate_value (return_type
);
788 error (_("Expression of type other than "
789 "\"Function returning ...\" used as function"));
791 switch (value_type (argvec
[0])->code ())
793 case TYPE_CODE_INTERNAL_FUNCTION
:
794 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
795 argvec
[0], nargs
, argvec
+ 1);
796 case TYPE_CODE_XMETHOD
:
797 return call_xmethod (argvec
[0], gdb::make_array_view (argvec
+ 1, nargs
));
799 return call_function_by_hand (argvec
[0], default_return_type
,
800 gdb::make_array_view (argvec
+ 1, nargs
));
804 /* Helper for evaluating an OP_FUNCALL. */
807 evaluate_funcall (type
*expect_type
, expression
*exp
, int *pos
,
815 symbol
*function
= NULL
;
816 char *function_name
= NULL
;
817 const char *var_func_name
= NULL
;
822 exp_opcode op
= exp
->elts
[*pos
].opcode
;
823 int nargs
= longest_to_int (exp
->elts
[pc
].longconst
);
824 /* Allocate arg vector, including space for the function to be
825 called in argvec[0], a potential `this', and a terminating
827 value
**argvec
= (value
**) alloca (sizeof (value
*) * (nargs
+ 3));
828 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
830 /* First, evaluate the structure into arg2. */
833 if (op
== STRUCTOP_MEMBER
)
835 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
839 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
842 /* If the function is a virtual function, then the aggregate
843 value (providing the structure) plays its part by providing
844 the vtable. Otherwise, it is just along for the ride: call
845 the function directly. */
847 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
849 type
*a1_type
= check_typedef (value_type (arg1
));
850 if (noside
== EVAL_SKIP
)
851 tem
= 1; /* Set it to the right arg index so that all
852 arguments can also be skipped. */
853 else if (a1_type
->code () == TYPE_CODE_METHODPTR
)
855 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
856 arg1
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
858 arg1
= cplus_method_ptr_to_value (&arg2
, arg1
);
860 /* Now, say which argument to start evaluating from. */
865 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
867 struct type
*type_ptr
868 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
869 struct type
*target_type_ptr
870 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
872 /* Now, convert these values to an address. */
873 arg2
= value_cast (type_ptr
, arg2
);
875 long mem_offset
= value_as_long (arg1
);
877 arg1
= value_from_pointer (target_type_ptr
,
878 value_as_long (arg2
) + mem_offset
);
879 arg1
= value_ind (arg1
);
883 error (_("Non-pointer-to-member value used in pointer-to-member "
886 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
888 /* Hair for method invocations. */
892 /* First, evaluate the structure into arg2. */
894 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
895 *pos
+= 3 + BYTES_TO_EXP_ELEM (tem2
+ 1);
897 if (op
== STRUCTOP_STRUCT
)
899 /* If v is a variable in a register, and the user types
900 v.method (), this will produce an error, because v has no
903 A possible way around this would be to allocate a copy of
904 the variable on the stack, copy in the contents, call the
905 function, and copy out the contents. I.e. convert this
906 from call by reference to call by copy-return (or
907 whatever it's called). However, this does not work
908 because it is not the same: the method being called could
909 stash a copy of the address, and then future uses through
910 that address (after the method returns) would be expected
911 to use the variable itself, not some copy of it. */
912 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
916 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
918 /* Check to see if the operator '->' has been overloaded.
919 If the operator has been overloaded replace arg2 with the
920 value returned by the custom operator and continue
922 while (unop_user_defined_p (op
, arg2
))
924 struct value
*value
= NULL
;
927 value
= value_x_unop (arg2
, op
, noside
);
930 catch (const gdb_exception_error
&except
)
932 if (except
.error
== NOT_FOUND_ERROR
)
941 /* Now, say which argument to start evaluating from. */
944 else if (op
== OP_SCOPE
945 && overload_resolution
946 && (exp
->language_defn
->la_language
== language_cplus
))
948 /* Unpack it locally so we can properly handle overload
954 local_tem
= longest_to_int (exp
->elts
[pc2
+ 2].longconst
);
955 (*pos
) += 4 + BYTES_TO_EXP_ELEM (local_tem
+ 1);
956 struct type
*type
= exp
->elts
[pc2
+ 1].type
;
957 name
= &exp
->elts
[pc2
+ 3].string
;
960 function_name
= NULL
;
961 if (type
->code () == TYPE_CODE_NAMESPACE
)
963 function
= cp_lookup_symbol_namespace (type
->name (),
965 get_selected_block (0),
967 if (function
== NULL
)
968 error (_("No symbol \"%s\" in namespace \"%s\"."),
969 name
, type
->name ());
972 /* arg2 is left as NULL on purpose. */
976 gdb_assert (type
->code () == TYPE_CODE_STRUCT
977 || type
->code () == TYPE_CODE_UNION
);
978 function_name
= name
;
980 /* We need a properly typed value for method lookup. For
981 static methods arg2 is otherwise unused. */
982 arg2
= value_zero (type
, lval_memory
);
987 else if (op
== OP_ADL_FUNC
)
989 /* Save the function position and move pos so that the arguments
996 func_name_len
= longest_to_int (exp
->elts
[save_pos1
+ 3].longconst
);
997 (*pos
) += 6 + BYTES_TO_EXP_ELEM (func_name_len
+ 1);
1001 /* Non-method function call. */
1005 /* If this is a C++ function wait until overload resolution. */
1006 if (op
== OP_VAR_VALUE
1007 && overload_resolution
1008 && (exp
->language_defn
->la_language
== language_cplus
))
1010 (*pos
) += 4; /* Skip the evaluation of the symbol. */
1015 if (op
== OP_VAR_MSYM_VALUE
)
1017 minimal_symbol
*msym
= exp
->elts
[*pos
+ 2].msymbol
;
1018 var_func_name
= msym
->print_name ();
1020 else if (op
== OP_VAR_VALUE
)
1022 symbol
*sym
= exp
->elts
[*pos
+ 2].symbol
;
1023 var_func_name
= sym
->print_name ();
1026 argvec
[0] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1027 type
*type
= value_type (argvec
[0]);
1028 if (type
&& type
->code () == TYPE_CODE_PTR
)
1029 type
= TYPE_TARGET_TYPE (type
);
1030 if (type
&& type
->code () == TYPE_CODE_FUNC
)
1032 for (; tem
<= nargs
&& tem
<= type
->num_fields (); tem
++)
1034 argvec
[tem
] = evaluate_subexp (type
->field (tem
- 1).type (),
1041 /* Evaluate arguments (if not already done, e.g., namespace::func()
1042 and overload-resolution is off). */
1043 for (; tem
<= nargs
; tem
++)
1045 /* Ensure that array expressions are coerced into pointer
1047 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1050 /* Signal end of arglist. */
1053 if (noside
== EVAL_SKIP
)
1054 return eval_skip_value (exp
);
1056 if (op
== OP_ADL_FUNC
)
1058 struct symbol
*symp
;
1061 int string_pc
= save_pos1
+ 3;
1063 /* Extract the function name. */
1064 name_len
= longest_to_int (exp
->elts
[string_pc
].longconst
);
1065 func_name
= (char *) alloca (name_len
+ 1);
1066 strcpy (func_name
, &exp
->elts
[string_pc
+ 1].string
);
1068 find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1070 NON_METHOD
, /* not method */
1071 NULL
, NULL
, /* pass NULL symbol since
1072 symbol is unknown */
1073 NULL
, &symp
, NULL
, 0, noside
);
1075 /* Now fix the expression being evaluated. */
1076 exp
->elts
[save_pos1
+ 2].symbol
= symp
;
1077 argvec
[0] = evaluate_subexp_with_coercion (exp
, &save_pos1
, noside
);
1080 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
1081 || (op
== OP_SCOPE
&& function_name
!= NULL
))
1083 int static_memfuncp
;
1086 /* Method invocation: stuff "this" as first parameter. If the
1087 method turns out to be static we undo this below. */
1092 /* Name of method from expression. */
1093 tstr
= &exp
->elts
[pc2
+ 2].string
;
1096 tstr
= function_name
;
1098 if (overload_resolution
&& (exp
->language_defn
->la_language
1101 /* Language is C++, do some overload resolution before
1103 struct value
*valp
= NULL
;
1105 (void) find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1107 METHOD
, /* method */
1108 &arg2
, /* the object */
1110 &static_memfuncp
, 0, noside
);
1112 if (op
== OP_SCOPE
&& !static_memfuncp
)
1114 /* For the time being, we don't handle this. */
1115 error (_("Call to overloaded function %s requires "
1119 argvec
[1] = arg2
; /* the ``this'' pointer */
1120 argvec
[0] = valp
; /* Use the method found after overload
1124 /* Non-C++ case -- or no overload resolution. */
1126 struct value
*temp
= arg2
;
1128 argvec
[0] = value_struct_elt (&temp
, argvec
+ 1, tstr
,
1130 op
== STRUCTOP_STRUCT
1131 ? "structure" : "structure pointer");
1132 /* value_struct_elt updates temp with the correct value of
1133 the ``this'' pointer if necessary, so modify argvec[1] to
1134 reflect any ``this'' changes. */
1136 = value_from_longest (lookup_pointer_type(value_type (temp
)),
1137 value_address (temp
)
1138 + value_embedded_offset (temp
));
1139 argvec
[1] = arg2
; /* the ``this'' pointer */
1142 /* Take out `this' if needed. */
1143 if (static_memfuncp
)
1145 argvec
[1] = argvec
[0];
1150 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
1152 /* Pointer to member. argvec[1] is already set up. */
1155 else if (op
== OP_VAR_VALUE
|| (op
== OP_SCOPE
&& function
!= NULL
))
1157 /* Non-member function being called. */
1158 /* fn: This can only be done for C++ functions. A C-style
1159 function in a C++ program, for instance, does not have the
1160 fields that are expected here. */
1162 if (overload_resolution
&& (exp
->language_defn
->la_language
1165 /* Language is C++, do some overload resolution before
1167 struct symbol
*symp
;
1170 /* If a scope has been specified disable ADL. */
1174 if (op
== OP_VAR_VALUE
)
1175 function
= exp
->elts
[save_pos1
+2].symbol
;
1177 (void) find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1178 NULL
, /* no need for name */
1179 NON_METHOD
, /* not method */
1180 NULL
, function
, /* the function */
1181 NULL
, &symp
, NULL
, no_adl
, noside
);
1183 if (op
== OP_VAR_VALUE
)
1185 /* Now fix the expression being evaluated. */
1186 exp
->elts
[save_pos1
+2].symbol
= symp
;
1187 argvec
[0] = evaluate_subexp_with_coercion (exp
, &save_pos1
,
1191 argvec
[0] = value_of_variable (symp
, get_selected_block (0));
1195 /* Not C++, or no overload resolution allowed. */
1196 /* Nothing to be done; argvec already correctly set up. */
1201 /* It is probably a C-style function. */
1202 /* Nothing to be done; argvec already correctly set up. */
1205 return evaluate_subexp_do_call (exp
, noside
, nargs
, argvec
,
1206 var_func_name
, expect_type
);
1209 /* Return true if type is integral or reference to integral */
1212 is_integral_or_integral_reference (struct type
*type
)
1214 if (is_integral_type (type
))
1217 type
= check_typedef (type
);
1218 return (type
!= nullptr
1219 && TYPE_IS_REFERENCE (type
)
1220 && is_integral_type (TYPE_TARGET_TYPE (type
)));
1224 evaluate_subexp_standard (struct type
*expect_type
,
1225 struct expression
*exp
, int *pos
,
1229 int tem
, tem2
, tem3
;
1231 struct value
*arg1
= NULL
;
1232 struct value
*arg2
= NULL
;
1236 struct value
**argvec
;
1239 struct type
**arg_types
;
1242 op
= exp
->elts
[pc
].opcode
;
1247 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1248 (*pos
) += 4 + BYTES_TO_EXP_ELEM (tem
+ 1);
1249 if (noside
== EVAL_SKIP
)
1250 return eval_skip_value (exp
);
1251 arg1
= value_aggregate_elt (exp
->elts
[pc
+ 1].type
,
1252 &exp
->elts
[pc
+ 3].string
,
1253 expect_type
, 0, noside
);
1255 error (_("There is no field named %s"), &exp
->elts
[pc
+ 3].string
);
1260 return value_from_longest (exp
->elts
[pc
+ 1].type
,
1261 exp
->elts
[pc
+ 2].longconst
);
1265 return value_from_contents (exp
->elts
[pc
+ 1].type
,
1266 exp
->elts
[pc
+ 2].floatconst
);
1272 symbol
*var
= exp
->elts
[pc
+ 2].symbol
;
1273 if (SYMBOL_TYPE (var
)->code () == TYPE_CODE_ERROR
)
1274 error_unknown_type (var
->print_name ());
1275 if (noside
!= EVAL_SKIP
)
1276 return evaluate_var_value (noside
, exp
->elts
[pc
+ 1].block
, var
);
1279 /* Return a dummy value of the correct type when skipping, so
1280 that parent functions know what is to be skipped. */
1281 return allocate_value (SYMBOL_TYPE (var
));
1285 case OP_VAR_MSYM_VALUE
:
1289 minimal_symbol
*msymbol
= exp
->elts
[pc
+ 2].msymbol
;
1290 value
*val
= evaluate_var_msym_value (noside
,
1291 exp
->elts
[pc
+ 1].objfile
,
1294 type
= value_type (val
);
1295 if (type
->code () == TYPE_CODE_ERROR
1296 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| pc
!= 0))
1297 error_unknown_type (msymbol
->print_name ());
1301 case OP_VAR_ENTRY_VALUE
:
1303 if (noside
== EVAL_SKIP
)
1304 return eval_skip_value (exp
);
1307 struct symbol
*sym
= exp
->elts
[pc
+ 1].symbol
;
1308 struct frame_info
*frame
;
1310 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1311 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
1313 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1314 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1315 error (_("Symbol \"%s\" does not have any specific entry value"),
1316 sym
->print_name ());
1318 frame
= get_selected_frame (NULL
);
1319 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1322 case OP_FUNC_STATIC_VAR
:
1323 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1324 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1325 if (noside
== EVAL_SKIP
)
1326 return eval_skip_value (exp
);
1329 value
*func
= evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1330 CORE_ADDR addr
= value_address (func
);
1332 const block
*blk
= block_for_pc (addr
);
1333 const char *var
= &exp
->elts
[pc
+ 2].string
;
1335 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1337 if (sym
.symbol
== NULL
)
1338 error (_("No symbol \"%s\" in specified context."), var
);
1340 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1346 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
1350 const char *name
= &exp
->elts
[pc
+ 2].string
;
1354 (*pos
) += 3 + BYTES_TO_EXP_ELEM (exp
->elts
[pc
+ 1].longconst
+ 1);
1355 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1356 name
, strlen (name
));
1358 error (_("Register $%s not available."), name
);
1360 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1361 a value with the appropriate register type. Unfortunately,
1362 we don't have easy access to the type of user registers.
1363 So for these registers, we fetch the register value regardless
1364 of the evaluation mode. */
1365 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1366 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1367 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1369 val
= value_of_register (regno
, get_selected_frame (NULL
));
1371 error (_("Value of register %s not available."), name
);
1377 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
1378 return value_from_longest (type
, exp
->elts
[pc
+ 1].longconst
);
1380 case OP_INTERNALVAR
:
1382 return value_of_internalvar (exp
->gdbarch
,
1383 exp
->elts
[pc
+ 1].internalvar
);
1386 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1387 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1388 if (noside
== EVAL_SKIP
)
1389 return eval_skip_value (exp
);
1390 type
= language_string_char_type (exp
->language_defn
, exp
->gdbarch
);
1391 return value_string (&exp
->elts
[pc
+ 2].string
, tem
, type
);
1393 case OP_OBJC_NSSTRING
: /* Objective C Foundation Class
1394 NSString constant. */
1395 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1396 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1397 if (noside
== EVAL_SKIP
)
1398 return eval_skip_value (exp
);
1399 return value_nsstring (exp
->gdbarch
, &exp
->elts
[pc
+ 2].string
, tem
+ 1);
1403 tem2
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1404 tem3
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1405 nargs
= tem3
- tem2
+ 1;
1406 type
= expect_type
? check_typedef (expect_type
) : nullptr;
1408 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1409 && type
->code () == TYPE_CODE_STRUCT
)
1411 struct value
*rec
= allocate_value (expect_type
);
1413 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
1414 return evaluate_struct_tuple (rec
, exp
, pos
, noside
, nargs
);
1417 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1418 && type
->code () == TYPE_CODE_ARRAY
)
1420 struct type
*range_type
= type
->index_type ();
1421 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
1422 struct value
*array
= allocate_value (expect_type
);
1423 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
1424 LONGEST low_bound
, high_bound
, index
;
1426 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
1429 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
1432 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
1433 for (tem
= nargs
; --nargs
>= 0;)
1435 struct value
*element
;
1438 element
= evaluate_subexp (element_type
, exp
, pos
, noside
);
1439 if (value_type (element
) != element_type
)
1440 element
= value_cast (element_type
, element
);
1443 int continue_pc
= *pos
;
1446 index
= init_array_element (array
, element
, exp
, pos
, noside
,
1447 low_bound
, high_bound
);
1452 if (index
> high_bound
)
1453 /* To avoid memory corruption. */
1454 error (_("Too many array elements"));
1455 memcpy (value_contents_raw (array
)
1456 + (index
- low_bound
) * element_size
,
1457 value_contents (element
),
1465 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1466 && type
->code () == TYPE_CODE_SET
)
1468 struct value
*set
= allocate_value (expect_type
);
1469 gdb_byte
*valaddr
= value_contents_raw (set
);
1470 struct type
*element_type
= type
->index_type ();
1471 struct type
*check_type
= element_type
;
1472 LONGEST low_bound
, high_bound
;
1474 /* Get targettype of elementtype. */
1475 while (check_type
->code () == TYPE_CODE_RANGE
1476 || check_type
->code () == TYPE_CODE_TYPEDEF
)
1477 check_type
= TYPE_TARGET_TYPE (check_type
);
1479 if (get_discrete_bounds (element_type
, &low_bound
, &high_bound
) < 0)
1480 error (_("(power)set type with unknown size"));
1481 memset (valaddr
, '\0', TYPE_LENGTH (type
));
1482 for (tem
= 0; tem
< nargs
; tem
++)
1484 LONGEST range_low
, range_high
;
1485 struct type
*range_low_type
, *range_high_type
;
1486 struct value
*elem_val
;
1488 elem_val
= evaluate_subexp (element_type
, exp
, pos
, noside
);
1489 range_low_type
= range_high_type
= value_type (elem_val
);
1490 range_low
= range_high
= value_as_long (elem_val
);
1492 /* Check types of elements to avoid mixture of elements from
1493 different types. Also check if type of element is "compatible"
1494 with element type of powerset. */
1495 if (range_low_type
->code () == TYPE_CODE_RANGE
)
1496 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
1497 if (range_high_type
->code () == TYPE_CODE_RANGE
)
1498 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
1499 if ((range_low_type
->code () != range_high_type
->code ())
1500 || (range_low_type
->code () == TYPE_CODE_ENUM
1501 && (range_low_type
!= range_high_type
)))
1502 /* different element modes. */
1503 error (_("POWERSET tuple elements of different mode"));
1504 if ((check_type
->code () != range_low_type
->code ())
1505 || (check_type
->code () == TYPE_CODE_ENUM
1506 && range_low_type
!= check_type
))
1507 error (_("incompatible POWERSET tuple elements"));
1508 if (range_low
> range_high
)
1510 warning (_("empty POWERSET tuple range"));
1513 if (range_low
< low_bound
|| range_high
> high_bound
)
1514 error (_("POWERSET tuple element out of range"));
1515 range_low
-= low_bound
;
1516 range_high
-= low_bound
;
1517 for (; range_low
<= range_high
; range_low
++)
1519 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
1521 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
1522 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
1523 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
1530 argvec
= XALLOCAVEC (struct value
*, nargs
);
1531 for (tem
= 0; tem
< nargs
; tem
++)
1533 /* Ensure that array expressions are coerced into pointer
1535 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1537 if (noside
== EVAL_SKIP
)
1538 return eval_skip_value (exp
);
1539 return value_array (tem2
, tem3
, argvec
);
1543 struct value
*array
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1545 = value_as_long (evaluate_subexp (nullptr, exp
, pos
, noside
));
1546 int upper
= value_as_long (evaluate_subexp (nullptr, exp
, pos
, noside
));
1548 if (noside
== EVAL_SKIP
)
1549 return eval_skip_value (exp
);
1550 return value_slice (array
, lowbound
, upper
- lowbound
+ 1);
1554 /* Skip third and second args to evaluate the first one. */
1555 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1556 if (value_logical_not (arg1
))
1558 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
1559 return evaluate_subexp (nullptr, exp
, pos
, noside
);
1563 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1564 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
1568 case OP_OBJC_SELECTOR
:
1569 { /* Objective C @selector operator. */
1570 char *sel
= &exp
->elts
[pc
+ 2].string
;
1571 int len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1572 struct type
*selector_type
;
1574 (*pos
) += 3 + BYTES_TO_EXP_ELEM (len
+ 1);
1575 if (noside
== EVAL_SKIP
)
1576 return eval_skip_value (exp
);
1579 sel
[len
] = 0; /* Make sure it's terminated. */
1581 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1582 return value_from_longest (selector_type
,
1583 lookup_child_selector (exp
->gdbarch
, sel
));
1586 case OP_OBJC_MSGCALL
:
1587 { /* Objective C message (method) call. */
1589 CORE_ADDR responds_selector
= 0;
1590 CORE_ADDR method_selector
= 0;
1592 CORE_ADDR selector
= 0;
1594 int struct_return
= 0;
1595 enum noside sub_no_side
= EVAL_NORMAL
;
1597 struct value
*msg_send
= NULL
;
1598 struct value
*msg_send_stret
= NULL
;
1599 int gnu_runtime
= 0;
1601 struct value
*target
= NULL
;
1602 struct value
*method
= NULL
;
1603 struct value
*called_method
= NULL
;
1605 struct type
*selector_type
= NULL
;
1606 struct type
*long_type
;
1608 struct value
*ret
= NULL
;
1611 selector
= exp
->elts
[pc
+ 1].longconst
;
1612 nargs
= exp
->elts
[pc
+ 2].longconst
;
1613 argvec
= XALLOCAVEC (struct value
*, nargs
+ 5);
1617 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1618 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1620 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1621 sub_no_side
= EVAL_NORMAL
;
1623 sub_no_side
= noside
;
1625 target
= evaluate_subexp (selector_type
, exp
, pos
, sub_no_side
);
1627 if (value_as_long (target
) == 0)
1628 return value_from_longest (long_type
, 0);
1630 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1633 /* Find the method dispatch (Apple runtime) or method lookup
1634 (GNU runtime) function for Objective-C. These will be used
1635 to lookup the symbol information for the method. If we
1636 can't find any symbol information, then we'll use these to
1637 call the method, otherwise we can call the method
1638 directly. The msg_send_stret function is used in the special
1639 case of a method that returns a structure (Apple runtime
1643 type
= selector_type
;
1645 type
= lookup_function_type (type
);
1646 type
= lookup_pointer_type (type
);
1647 type
= lookup_function_type (type
);
1648 type
= lookup_pointer_type (type
);
1650 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
1652 = find_function_in_inferior ("objc_msg_lookup", NULL
);
1654 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
1655 msg_send_stret
= value_from_pointer (type
,
1656 value_as_address (msg_send_stret
));
1660 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
1661 /* Special dispatcher for methods returning structs. */
1663 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
1666 /* Verify the target object responds to this method. The
1667 standard top-level 'Object' class uses a different name for
1668 the verification method than the non-standard, but more
1669 often used, 'NSObject' class. Make sure we check for both. */
1672 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
1673 if (responds_selector
== 0)
1675 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
1677 if (responds_selector
== 0)
1678 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1681 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
1682 if (method_selector
== 0)
1684 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
1686 if (method_selector
== 0)
1687 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1689 /* Call the verification method, to make sure that the target
1690 class implements the desired method. */
1692 argvec
[0] = msg_send
;
1694 argvec
[2] = value_from_longest (long_type
, responds_selector
);
1695 argvec
[3] = value_from_longest (long_type
, selector
);
1698 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1701 /* Function objc_msg_lookup returns a pointer. */
1703 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1705 if (value_as_long (ret
) == 0)
1706 error (_("Target does not respond to this message selector."));
1708 /* Call "methodForSelector:" method, to get the address of a
1709 function method that implements this selector for this
1710 class. If we can find a symbol at that address, then we
1711 know the return type, parameter types etc. (that's a good
1714 argvec
[0] = msg_send
;
1716 argvec
[2] = value_from_longest (long_type
, method_selector
);
1717 argvec
[3] = value_from_longest (long_type
, selector
);
1720 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1724 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
1727 /* ret should now be the selector. */
1729 addr
= value_as_long (ret
);
1732 struct symbol
*sym
= NULL
;
1734 /* The address might point to a function descriptor;
1735 resolve it to the actual code address instead. */
1736 addr
= gdbarch_convert_from_func_ptr_addr (exp
->gdbarch
, addr
,
1737 current_top_target ());
1739 /* Is it a high_level symbol? */
1740 sym
= find_pc_function (addr
);
1742 method
= value_of_variable (sym
, 0);
1745 /* If we found a method with symbol information, check to see
1746 if it returns a struct. Otherwise assume it doesn't. */
1751 struct type
*val_type
;
1753 funaddr
= find_function_addr (method
, &val_type
);
1755 block_for_pc (funaddr
);
1757 val_type
= check_typedef (val_type
);
1759 if ((val_type
== NULL
)
1760 || (val_type
->code () == TYPE_CODE_ERROR
))
1762 if (expect_type
!= NULL
)
1763 val_type
= expect_type
;
1766 struct_return
= using_struct_return (exp
->gdbarch
, method
,
1769 else if (expect_type
!= NULL
)
1771 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
1772 check_typedef (expect_type
));
1775 /* Found a function symbol. Now we will substitute its
1776 value in place of the message dispatcher (obj_msgSend),
1777 so that we call the method directly instead of thru
1778 the dispatcher. The main reason for doing this is that
1779 we can now evaluate the return value and parameter values
1780 according to their known data types, in case we need to
1781 do things like promotion, dereferencing, special handling
1782 of structs and doubles, etc.
1784 We want to use the type signature of 'method', but still
1785 jump to objc_msgSend() or objc_msgSend_stret() to better
1786 mimic the behavior of the runtime. */
1790 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
1791 error (_("method address has symbol information "
1792 "with non-function type; skipping"));
1794 /* Create a function pointer of the appropriate type, and
1795 replace its value with the value of msg_send or
1796 msg_send_stret. We must use a pointer here, as
1797 msg_send and msg_send_stret are of pointer type, and
1798 the representation may be different on systems that use
1799 function descriptors. */
1802 = value_from_pointer (lookup_pointer_type (value_type (method
)),
1803 value_as_address (msg_send_stret
));
1806 = value_from_pointer (lookup_pointer_type (value_type (method
)),
1807 value_as_address (msg_send
));
1812 called_method
= msg_send_stret
;
1814 called_method
= msg_send
;
1817 if (noside
== EVAL_SKIP
)
1818 return eval_skip_value (exp
);
1820 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1822 /* If the return type doesn't look like a function type,
1823 call an error. This can happen if somebody tries to
1824 turn a variable into a function call. This is here
1825 because people often want to call, eg, strcmp, which
1826 gdb doesn't know is a function. If gdb isn't asked for
1827 it's opinion (ie. through "whatis"), it won't offer
1830 struct type
*callee_type
= value_type (called_method
);
1832 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
1833 callee_type
= TYPE_TARGET_TYPE (callee_type
);
1834 callee_type
= TYPE_TARGET_TYPE (callee_type
);
1838 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
1839 return allocate_value (expect_type
);
1841 return allocate_value (callee_type
);
1844 error (_("Expression of type other than "
1845 "\"method returning ...\" used as a method"));
1848 /* Now depending on whether we found a symbol for the method,
1849 we will either call the runtime dispatcher or the method
1852 argvec
[0] = called_method
;
1854 argvec
[2] = value_from_longest (long_type
, selector
);
1855 /* User-supplied arguments. */
1856 for (tem
= 0; tem
< nargs
; tem
++)
1857 argvec
[tem
+ 3] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1858 argvec
[tem
+ 3] = 0;
1860 auto call_args
= gdb::make_array_view (argvec
+ 1, nargs
+ 2);
1862 if (gnu_runtime
&& (method
!= NULL
))
1864 /* Function objc_msg_lookup returns a pointer. */
1865 deprecated_set_value_type (argvec
[0],
1866 lookup_pointer_type (lookup_function_type (value_type (argvec
[0]))));
1867 argvec
[0] = call_function_by_hand (argvec
[0], NULL
, call_args
);
1870 return call_function_by_hand (argvec
[0], NULL
, call_args
);
1875 return evaluate_funcall (expect_type
, exp
, pos
, noside
);
1878 /* We have a complex number, There should be 2 floating
1879 point numbers that compose it. */
1881 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1882 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1884 return value_literal_complex (arg1
, arg2
, exp
->elts
[pc
+ 1].type
);
1886 case STRUCTOP_STRUCT
:
1887 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1888 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1889 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1890 if (noside
== EVAL_SKIP
)
1891 return eval_skip_value (exp
);
1892 arg3
= value_struct_elt (&arg1
, NULL
, &exp
->elts
[pc
+ 2].string
,
1894 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1895 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1899 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1900 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1901 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1902 if (noside
== EVAL_SKIP
)
1903 return eval_skip_value (exp
);
1905 /* Check to see if operator '->' has been overloaded. If so replace
1906 arg1 with the value returned by evaluating operator->(). */
1907 while (unop_user_defined_p (op
, arg1
))
1909 struct value
*value
= NULL
;
1912 value
= value_x_unop (arg1
, op
, noside
);
1915 catch (const gdb_exception_error
&except
)
1917 if (except
.error
== NOT_FOUND_ERROR
)
1926 /* JYG: if print object is on we need to replace the base type
1927 with rtti type in order to continue on with successful
1928 lookup of member / method only available in the rtti type. */
1930 struct type
*arg_type
= value_type (arg1
);
1931 struct type
*real_type
;
1932 int full
, using_enc
;
1934 struct value_print_options opts
;
1936 get_user_print_options (&opts
);
1937 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1938 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1940 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1943 arg1
= value_cast (real_type
, arg1
);
1947 arg3
= value_struct_elt (&arg1
, NULL
, &exp
->elts
[pc
+ 2].string
,
1948 NULL
, "structure pointer");
1949 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1950 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1953 case STRUCTOP_MEMBER
:
1955 if (op
== STRUCTOP_MEMBER
)
1956 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
1958 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1960 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1962 if (noside
== EVAL_SKIP
)
1963 return eval_skip_value (exp
);
1965 type
= check_typedef (value_type (arg2
));
1966 switch (type
->code ())
1968 case TYPE_CODE_METHODPTR
:
1969 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1970 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1973 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1974 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1975 return value_ind (arg2
);
1978 case TYPE_CODE_MEMBERPTR
:
1979 /* Now, convert these values to an address. */
1980 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1983 mem_offset
= value_as_long (arg2
);
1985 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1986 value_as_long (arg1
) + mem_offset
);
1987 return value_ind (arg3
);
1990 error (_("non-pointer-to-member value used "
1991 "in pointer-to-member construct"));
1996 type_instance_flags flags
1997 = (type_instance_flag_value
) longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1998 nargs
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1999 arg_types
= (struct type
**) alloca (nargs
* sizeof (struct type
*));
2000 for (ix
= 0; ix
< nargs
; ++ix
)
2001 arg_types
[ix
] = exp
->elts
[pc
+ 2 + ix
+ 1].type
;
2003 fake_method
fake_expect_type (flags
, nargs
, arg_types
);
2004 *(pos
) += 4 + nargs
;
2005 return evaluate_subexp_standard (fake_expect_type
.type (), exp
, pos
,
2010 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2011 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2012 if (noside
== EVAL_SKIP
)
2013 return eval_skip_value (exp
);
2014 if (binop_user_defined_p (op
, arg1
, arg2
))
2015 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2017 return value_concat (arg1
, arg2
);
2020 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2021 /* Special-case assignments where the left-hand-side is a
2022 convenience variable -- in these, don't bother setting an
2023 expected type. This avoids a weird case where re-assigning a
2024 string or array to an internal variable could error with "Too
2025 many array elements". */
2026 arg2
= evaluate_subexp (VALUE_LVAL (arg1
) == lval_internalvar
2028 : value_type (arg1
),
2031 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2033 if (binop_user_defined_p (op
, arg1
, arg2
))
2034 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2036 return value_assign (arg1
, arg2
);
2038 case BINOP_ASSIGN_MODIFY
:
2040 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2041 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2042 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2044 op
= exp
->elts
[pc
+ 1].opcode
;
2045 if (binop_user_defined_p (op
, arg1
, arg2
))
2046 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
2047 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
2049 && is_integral_type (value_type (arg2
)))
2050 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
2051 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
2053 && is_integral_type (value_type (arg2
)))
2054 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
2057 struct value
*tmp
= arg1
;
2059 /* For shift and integer exponentiation operations,
2060 only promote the first argument. */
2061 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
2062 && is_integral_type (value_type (arg2
)))
2063 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
2065 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2067 arg2
= value_binop (tmp
, arg2
, op
);
2069 return value_assign (arg1
, arg2
);
2072 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2073 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2074 if (noside
== EVAL_SKIP
)
2075 return eval_skip_value (exp
);
2076 if (binop_user_defined_p (op
, arg1
, arg2
))
2077 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2078 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
2079 && is_integral_or_integral_reference (value_type (arg2
)))
2080 return value_ptradd (arg1
, value_as_long (arg2
));
2081 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
2082 && is_integral_or_integral_reference (value_type (arg1
)))
2083 return value_ptradd (arg2
, value_as_long (arg1
));
2086 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2087 return value_binop (arg1
, arg2
, BINOP_ADD
);
2091 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2092 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2093 if (noside
== EVAL_SKIP
)
2094 return eval_skip_value (exp
);
2095 if (binop_user_defined_p (op
, arg1
, arg2
))
2096 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2097 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
2098 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
2100 /* FIXME -- should be ptrdiff_t */
2101 type
= builtin_type (exp
->gdbarch
)->builtin_long
;
2102 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
2104 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
2105 && is_integral_or_integral_reference (value_type (arg2
)))
2106 return value_ptradd (arg1
, - value_as_long (arg2
));
2109 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2110 return value_binop (arg1
, arg2
, BINOP_SUB
);
2121 case BINOP_BITWISE_AND
:
2122 case BINOP_BITWISE_IOR
:
2123 case BINOP_BITWISE_XOR
:
2124 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2125 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2126 if (noside
== EVAL_SKIP
)
2127 return eval_skip_value (exp
);
2128 if (binop_user_defined_p (op
, arg1
, arg2
))
2129 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2132 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2133 fudge arg2 to avoid division-by-zero, the caller is
2134 (theoretically) only looking for the type of the result. */
2135 if (noside
== EVAL_AVOID_SIDE_EFFECTS
2136 /* ??? Do we really want to test for BINOP_MOD here?
2137 The implementation of value_binop gives it a well-defined
2140 || op
== BINOP_INTDIV
2143 && value_logical_not (arg2
))
2145 struct value
*v_one
, *retval
;
2147 v_one
= value_one (value_type (arg2
));
2148 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
2149 retval
= value_binop (arg1
, v_one
, op
);
2154 /* For shift and integer exponentiation operations,
2155 only promote the first argument. */
2156 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
2157 && is_integral_type (value_type (arg2
)))
2158 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2160 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2162 return value_binop (arg1
, arg2
, op
);
2166 case BINOP_SUBSCRIPT
:
2167 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2168 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2169 if (noside
== EVAL_SKIP
)
2170 return eval_skip_value (exp
);
2171 if (binop_user_defined_p (op
, arg1
, arg2
))
2172 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2175 /* If the user attempts to subscript something that is not an
2176 array or pointer type (like a plain int variable for example),
2177 then report this as an error. */
2179 arg1
= coerce_ref (arg1
);
2180 type
= check_typedef (value_type (arg1
));
2181 if (type
->code () != TYPE_CODE_ARRAY
2182 && type
->code () != TYPE_CODE_PTR
)
2185 error (_("cannot subscript something of type `%s'"),
2188 error (_("cannot subscript requested type"));
2191 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2192 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
2194 return value_subscript (arg1
, value_as_long (arg2
));
2196 case MULTI_SUBSCRIPT
:
2198 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
2199 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2202 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2203 /* FIXME: EVAL_SKIP handling may not be correct. */
2204 if (noside
== EVAL_SKIP
)
2208 return eval_skip_value (exp
);
2210 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
2211 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2213 /* If the user attempts to subscript something that has no target
2214 type (like a plain int variable for example), then report this
2217 type
= TYPE_TARGET_TYPE (check_typedef (value_type (arg1
)));
2220 arg1
= value_zero (type
, VALUE_LVAL (arg1
));
2226 error (_("cannot subscript something of type `%s'"),
2227 value_type (arg1
)->name ());
2231 if (binop_user_defined_p (op
, arg1
, arg2
))
2233 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2237 arg1
= coerce_ref (arg1
);
2238 type
= check_typedef (value_type (arg1
));
2240 switch (type
->code ())
2243 case TYPE_CODE_ARRAY
:
2244 case TYPE_CODE_STRING
:
2245 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2250 error (_("cannot subscript something of type `%s'"),
2253 error (_("cannot subscript requested type"));
2259 case BINOP_LOGICAL_AND
:
2260 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2261 if (noside
== EVAL_SKIP
)
2263 evaluate_subexp (nullptr, exp
, pos
, noside
);
2264 return eval_skip_value (exp
);
2268 arg2
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2271 if (binop_user_defined_p (op
, arg1
, arg2
))
2273 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2274 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2278 tem
= value_logical_not (arg1
);
2280 = evaluate_subexp (nullptr, exp
, pos
, (tem
? EVAL_SKIP
: noside
));
2281 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2282 return value_from_longest (type
,
2283 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
2286 case BINOP_LOGICAL_OR
:
2287 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2288 if (noside
== EVAL_SKIP
)
2290 evaluate_subexp (nullptr, exp
, pos
, noside
);
2291 return eval_skip_value (exp
);
2295 arg2
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2298 if (binop_user_defined_p (op
, arg1
, arg2
))
2300 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2301 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2305 tem
= value_logical_not (arg1
);
2307 = evaluate_subexp (nullptr, exp
, pos
, (!tem
? EVAL_SKIP
: noside
));
2308 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2309 return value_from_longest (type
,
2310 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
2314 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2315 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2316 if (noside
== EVAL_SKIP
)
2317 return eval_skip_value (exp
);
2318 if (binop_user_defined_p (op
, arg1
, arg2
))
2320 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2324 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2325 tem
= value_equal (arg1
, arg2
);
2326 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2327 return value_from_longest (type
, (LONGEST
) tem
);
2330 case BINOP_NOTEQUAL
:
2331 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2332 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2333 if (noside
== EVAL_SKIP
)
2334 return eval_skip_value (exp
);
2335 if (binop_user_defined_p (op
, arg1
, arg2
))
2337 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2341 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2342 tem
= value_equal (arg1
, arg2
);
2343 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2344 return value_from_longest (type
, (LONGEST
) ! tem
);
2348 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2349 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2350 if (noside
== EVAL_SKIP
)
2351 return eval_skip_value (exp
);
2352 if (binop_user_defined_p (op
, arg1
, arg2
))
2354 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2358 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2359 tem
= value_less (arg1
, arg2
);
2360 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2361 return value_from_longest (type
, (LONGEST
) tem
);
2365 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2366 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2367 if (noside
== EVAL_SKIP
)
2368 return eval_skip_value (exp
);
2369 if (binop_user_defined_p (op
, arg1
, arg2
))
2371 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2375 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2376 tem
= value_less (arg2
, arg1
);
2377 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2378 return value_from_longest (type
, (LONGEST
) tem
);
2382 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2383 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2384 if (noside
== EVAL_SKIP
)
2385 return eval_skip_value (exp
);
2386 if (binop_user_defined_p (op
, arg1
, arg2
))
2388 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2392 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2393 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
2394 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2395 return value_from_longest (type
, (LONGEST
) tem
);
2399 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2400 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2401 if (noside
== EVAL_SKIP
)
2402 return eval_skip_value (exp
);
2403 if (binop_user_defined_p (op
, arg1
, arg2
))
2405 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2409 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2410 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
2411 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2412 return value_from_longest (type
, (LONGEST
) tem
);
2416 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2417 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2418 if (noside
== EVAL_SKIP
)
2419 return eval_skip_value (exp
);
2420 type
= check_typedef (value_type (arg2
));
2421 if (type
->code () != TYPE_CODE_INT
2422 && type
->code () != TYPE_CODE_ENUM
)
2423 error (_("Non-integral right operand for \"@\" operator."));
2424 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2426 return allocate_repeat_value (value_type (arg1
),
2427 longest_to_int (value_as_long (arg2
)));
2430 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
2433 evaluate_subexp (nullptr, exp
, pos
, noside
);
2434 return evaluate_subexp (nullptr, exp
, pos
, noside
);
2437 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2438 if (noside
== EVAL_SKIP
)
2439 return eval_skip_value (exp
);
2440 if (unop_user_defined_p (op
, arg1
))
2441 return value_x_unop (arg1
, op
, noside
);
2444 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2445 return value_pos (arg1
);
2449 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2450 if (noside
== EVAL_SKIP
)
2451 return eval_skip_value (exp
);
2452 if (unop_user_defined_p (op
, arg1
))
2453 return value_x_unop (arg1
, op
, noside
);
2456 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2457 return value_neg (arg1
);
2460 case UNOP_COMPLEMENT
:
2461 /* C++: check for and handle destructor names. */
2463 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2464 if (noside
== EVAL_SKIP
)
2465 return eval_skip_value (exp
);
2466 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
2467 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
2470 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2471 return value_complement (arg1
);
2474 case UNOP_LOGICAL_NOT
:
2475 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2476 if (noside
== EVAL_SKIP
)
2477 return eval_skip_value (exp
);
2478 if (unop_user_defined_p (op
, arg1
))
2479 return value_x_unop (arg1
, op
, noside
);
2482 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2483 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
2487 if (expect_type
&& expect_type
->code () == TYPE_CODE_PTR
)
2488 expect_type
= TYPE_TARGET_TYPE (check_typedef (expect_type
));
2489 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2490 type
= check_typedef (value_type (arg1
));
2491 if (type
->code () == TYPE_CODE_METHODPTR
2492 || type
->code () == TYPE_CODE_MEMBERPTR
)
2493 error (_("Attempt to dereference pointer "
2494 "to member without an object"));
2495 if (noside
== EVAL_SKIP
)
2496 return eval_skip_value (exp
);
2497 if (unop_user_defined_p (op
, arg1
))
2498 return value_x_unop (arg1
, op
, noside
);
2499 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2501 type
= check_typedef (value_type (arg1
));
2502 if (type
->code () == TYPE_CODE_PTR
2503 || TYPE_IS_REFERENCE (type
)
2504 /* In C you can dereference an array to get the 1st elt. */
2505 || type
->code () == TYPE_CODE_ARRAY
2507 return value_zero (TYPE_TARGET_TYPE (type
),
2509 else if (type
->code () == TYPE_CODE_INT
)
2510 /* GDB allows dereferencing an int. */
2511 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
2514 error (_("Attempt to take contents of a non-pointer value."));
2517 /* Allow * on an integer so we can cast it to whatever we want.
2518 This returns an int, which seems like the most C-like thing to
2519 do. "long long" variables are rare enough that
2520 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2521 if (type
->code () == TYPE_CODE_INT
)
2522 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
2523 (CORE_ADDR
) value_as_address (arg1
));
2524 return value_ind (arg1
);
2527 /* C++: check for and handle pointer to members. */
2529 if (noside
== EVAL_SKIP
)
2531 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2532 return eval_skip_value (exp
);
2536 struct value
*retvalp
= evaluate_subexp_for_address (exp
, pos
,
2543 if (noside
== EVAL_SKIP
)
2545 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2546 return eval_skip_value (exp
);
2548 return evaluate_subexp_for_sizeof (exp
, pos
, noside
);
2553 evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
));
2554 /* FIXME: This should be size_t. */
2555 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2556 ULONGEST align
= type_align (type
);
2558 error (_("could not determine alignment of type"));
2559 return value_from_longest (size_type
, align
);
2564 type
= exp
->elts
[pc
+ 1].type
;
2565 return evaluate_subexp_for_cast (exp
, pos
, noside
, type
);
2567 case UNOP_CAST_TYPE
:
2568 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2569 type
= value_type (arg1
);
2570 return evaluate_subexp_for_cast (exp
, pos
, noside
, type
);
2572 case UNOP_DYNAMIC_CAST
:
2573 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2574 type
= value_type (arg1
);
2575 arg1
= evaluate_subexp (type
, exp
, pos
, noside
);
2576 if (noside
== EVAL_SKIP
)
2577 return eval_skip_value (exp
);
2578 return value_dynamic_cast (type
, arg1
);
2580 case UNOP_REINTERPRET_CAST
:
2581 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2582 type
= value_type (arg1
);
2583 arg1
= evaluate_subexp (type
, exp
, pos
, noside
);
2584 if (noside
== EVAL_SKIP
)
2585 return eval_skip_value (exp
);
2586 return value_reinterpret_cast (type
, arg1
);
2590 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2591 if (noside
== EVAL_SKIP
)
2592 return eval_skip_value (exp
);
2593 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2594 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
2596 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
2597 value_as_address (arg1
));
2599 case UNOP_MEMVAL_TYPE
:
2600 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2601 type
= value_type (arg1
);
2602 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2603 if (noside
== EVAL_SKIP
)
2604 return eval_skip_value (exp
);
2605 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2606 return value_zero (type
, lval_memory
);
2608 return value_at_lazy (type
, value_as_address (arg1
));
2610 case UNOP_PREINCREMENT
:
2611 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2612 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2614 else if (unop_user_defined_p (op
, arg1
))
2616 return value_x_unop (arg1
, op
, noside
);
2620 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2621 arg2
= value_ptradd (arg1
, 1);
2624 struct value
*tmp
= arg1
;
2626 arg2
= value_one (value_type (arg1
));
2627 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2628 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
2631 return value_assign (arg1
, arg2
);
2634 case UNOP_PREDECREMENT
:
2635 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2636 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2638 else if (unop_user_defined_p (op
, arg1
))
2640 return value_x_unop (arg1
, op
, noside
);
2644 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2645 arg2
= value_ptradd (arg1
, -1);
2648 struct value
*tmp
= arg1
;
2650 arg2
= value_one (value_type (arg1
));
2651 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2652 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
2655 return value_assign (arg1
, arg2
);
2658 case UNOP_POSTINCREMENT
:
2659 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2660 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2662 else if (unop_user_defined_p (op
, arg1
))
2664 return value_x_unop (arg1
, op
, noside
);
2668 arg3
= value_non_lval (arg1
);
2670 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2671 arg2
= value_ptradd (arg1
, 1);
2674 struct value
*tmp
= arg1
;
2676 arg2
= value_one (value_type (arg1
));
2677 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2678 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
2681 value_assign (arg1
, arg2
);
2685 case UNOP_POSTDECREMENT
:
2686 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2687 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2689 else if (unop_user_defined_p (op
, arg1
))
2691 return value_x_unop (arg1
, op
, noside
);
2695 arg3
= value_non_lval (arg1
);
2697 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2698 arg2
= value_ptradd (arg1
, -1);
2701 struct value
*tmp
= arg1
;
2703 arg2
= value_one (value_type (arg1
));
2704 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2705 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
2708 value_assign (arg1
, arg2
);
2714 return value_of_this (exp
->language_defn
);
2717 /* The value is not supposed to be used. This is here to make it
2718 easier to accommodate expressions that contain types. */
2720 if (noside
== EVAL_SKIP
)
2721 return eval_skip_value (exp
);
2722 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2723 return allocate_value (exp
->elts
[pc
+ 1].type
);
2725 error (_("Attempt to use a type name as an expression"));
2729 if (noside
== EVAL_SKIP
)
2731 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2732 return eval_skip_value (exp
);
2734 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2736 enum exp_opcode sub_op
= exp
->elts
[*pos
].opcode
;
2737 struct value
*result
;
2739 result
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2741 /* 'decltype' has special semantics for lvalues. */
2742 if (op
== OP_DECLTYPE
2743 && (sub_op
== BINOP_SUBSCRIPT
2744 || sub_op
== STRUCTOP_MEMBER
2745 || sub_op
== STRUCTOP_MPTR
2746 || sub_op
== UNOP_IND
2747 || sub_op
== STRUCTOP_STRUCT
2748 || sub_op
== STRUCTOP_PTR
2749 || sub_op
== OP_SCOPE
))
2751 type
= value_type (result
);
2753 if (!TYPE_IS_REFERENCE (type
))
2755 type
= lookup_lvalue_reference_type (type
);
2756 result
= allocate_value (type
);
2763 error (_("Attempt to use a type as an expression"));
2767 struct value
*result
;
2768 enum exp_opcode sub_op
= exp
->elts
[*pos
].opcode
;
2770 if (sub_op
== OP_TYPE
|| sub_op
== OP_DECLTYPE
|| sub_op
== OP_TYPEOF
)
2771 result
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2773 result
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2775 if (noside
!= EVAL_NORMAL
)
2776 return allocate_value (cplus_typeid_type (exp
->gdbarch
));
2778 return cplus_typeid (result
);
2782 /* Removing this case and compiling with gcc -Wall reveals that
2783 a lot of cases are hitting this case. Some of these should
2784 probably be removed from expression.h; others are legitimate
2785 expressions which are (apparently) not fully implemented.
2787 If there are any cases landing here which mean a user error,
2788 then they should be separate cases, with more descriptive
2791 error (_("GDB does not (yet) know how to "
2792 "evaluate that kind of expression"));
2795 gdb_assert_not_reached ("missed return?");
2798 /* Evaluate a subexpression of EXP, at index *POS,
2799 and return the address of that subexpression.
2800 Advance *POS over the subexpression.
2801 If the subexpression isn't an lvalue, get an error.
2802 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2803 then only the type of the result need be correct. */
2805 static struct value
*
2806 evaluate_subexp_for_address (struct expression
*exp
, int *pos
,
2816 op
= exp
->elts
[pc
].opcode
;
2822 x
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2824 /* We can't optimize out "&*" if there's a user-defined operator*. */
2825 if (unop_user_defined_p (op
, x
))
2827 x
= value_x_unop (x
, op
, noside
);
2828 goto default_case_after_eval
;
2831 return coerce_array (x
);
2835 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
2836 evaluate_subexp (nullptr, exp
, pos
, noside
));
2838 case UNOP_MEMVAL_TYPE
:
2843 x
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2844 type
= value_type (x
);
2845 return value_cast (lookup_pointer_type (type
),
2846 evaluate_subexp (nullptr, exp
, pos
, noside
));
2850 var
= exp
->elts
[pc
+ 2].symbol
;
2852 /* C++: The "address" of a reference should yield the address
2853 * of the object pointed to. Let value_addr() deal with it. */
2854 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2858 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2861 lookup_pointer_type (SYMBOL_TYPE (var
));
2862 enum address_class sym_class
= SYMBOL_CLASS (var
);
2864 if (sym_class
== LOC_CONST
2865 || sym_class
== LOC_CONST_BYTES
2866 || sym_class
== LOC_REGISTER
)
2867 error (_("Attempt to take address of register or constant."));
2870 value_zero (type
, not_lval
);
2873 return address_of_variable (var
, exp
->elts
[pc
+ 1].block
);
2875 case OP_VAR_MSYM_VALUE
:
2879 value
*val
= evaluate_var_msym_value (noside
,
2880 exp
->elts
[pc
+ 1].objfile
,
2881 exp
->elts
[pc
+ 2].msymbol
);
2882 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2884 struct type
*type
= lookup_pointer_type (value_type (val
));
2885 return value_zero (type
, not_lval
);
2888 return value_addr (val
);
2892 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
2893 (*pos
) += 5 + BYTES_TO_EXP_ELEM (tem
+ 1);
2894 x
= value_aggregate_elt (exp
->elts
[pc
+ 1].type
,
2895 &exp
->elts
[pc
+ 3].string
,
2898 error (_("There is no field named %s"), &exp
->elts
[pc
+ 3].string
);
2903 x
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2904 default_case_after_eval
:
2905 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2907 struct type
*type
= check_typedef (value_type (x
));
2909 if (TYPE_IS_REFERENCE (type
))
2910 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2912 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
2913 return value_zero (lookup_pointer_type (value_type (x
)),
2916 error (_("Attempt to take address of "
2917 "value not located in memory."));
2919 return value_addr (x
);
2923 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2924 When used in contexts where arrays will be coerced anyway, this is
2925 equivalent to `evaluate_subexp' but much faster because it avoids
2926 actually fetching array contents (perhaps obsolete now that we have
2929 Note that we currently only do the coercion for C expressions, where
2930 arrays are zero based and the coercion is correct. For other languages,
2931 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2932 to decide if coercion is appropriate. */
2935 evaluate_subexp_with_coercion (struct expression
*exp
,
2936 int *pos
, enum noside noside
)
2945 op
= exp
->elts
[pc
].opcode
;
2950 var
= exp
->elts
[pc
+ 2].symbol
;
2951 type
= check_typedef (SYMBOL_TYPE (var
));
2952 if (type
->code () == TYPE_CODE_ARRAY
2953 && !type
->is_vector ()
2954 && CAST_IS_CONVERSION (exp
->language_defn
))
2957 val
= address_of_variable (var
, exp
->elts
[pc
+ 1].block
);
2958 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2964 return evaluate_subexp (nullptr, exp
, pos
, noside
);
2968 /* Evaluate a subexpression of EXP, at index *POS,
2969 and return a value for the size of that subexpression.
2970 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
2971 we allow side-effects on the operand if its type is a variable
2974 static struct value
*
2975 evaluate_subexp_for_sizeof (struct expression
*exp
, int *pos
,
2978 /* FIXME: This should be size_t. */
2979 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2986 op
= exp
->elts
[pc
].opcode
;
2990 /* This case is handled specially
2991 so that we avoid creating a value for the result type.
2992 If the result type is very big, it's desirable not to
2993 create a value unnecessarily. */
2996 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2997 type
= check_typedef (value_type (val
));
2998 if (type
->code () != TYPE_CODE_PTR
2999 && !TYPE_IS_REFERENCE (type
)
3000 && type
->code () != TYPE_CODE_ARRAY
)
3001 error (_("Attempt to take contents of a non-pointer value."));
3002 type
= TYPE_TARGET_TYPE (type
);
3003 if (is_dynamic_type (type
))
3004 type
= value_type (value_ind (val
));
3005 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
3009 type
= exp
->elts
[pc
+ 1].type
;
3012 case UNOP_MEMVAL_TYPE
:
3014 val
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
3015 type
= value_type (val
);
3019 type
= SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
);
3020 if (is_dynamic_type (type
))
3022 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_NORMAL
);
3023 type
= value_type (val
);
3024 if (type
->code () == TYPE_CODE_ARRAY
3025 && is_dynamic_type (type
->index_type ())
3026 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
3027 return allocate_optimized_out_value (size_type
);
3033 case OP_VAR_MSYM_VALUE
:
3037 minimal_symbol
*msymbol
= exp
->elts
[pc
+ 2].msymbol
;
3038 value
*mval
= evaluate_var_msym_value (noside
,
3039 exp
->elts
[pc
+ 1].objfile
,
3042 type
= value_type (mval
);
3043 if (type
->code () == TYPE_CODE_ERROR
)
3044 error_unknown_type (msymbol
->print_name ());
3046 return value_from_longest (size_type
, TYPE_LENGTH (type
));
3050 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3051 type of the subscript is a variable length array type. In this case we
3052 must re-evaluate the right hand side of the subscription to allow
3054 case BINOP_SUBSCRIPT
:
3055 if (noside
== EVAL_NORMAL
)
3057 int npc
= (*pos
) + 1;
3059 val
= evaluate_subexp (nullptr, exp
, &npc
, EVAL_AVOID_SIDE_EFFECTS
);
3060 type
= check_typedef (value_type (val
));
3061 if (type
->code () == TYPE_CODE_ARRAY
)
3063 type
= check_typedef (TYPE_TARGET_TYPE (type
));
3064 if (type
->code () == TYPE_CODE_ARRAY
)
3066 type
= type
->index_type ();
3067 /* Only re-evaluate the right hand side if the resulting type
3068 is a variable length type. */
3069 if (type
->bounds ()->flag_bound_evaluated
)
3071 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_NORMAL
);
3072 return value_from_longest
3073 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
3082 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
3083 type
= value_type (val
);
3087 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3088 "When applied to a reference or a reference type, the result is
3089 the size of the referenced type." */
3090 type
= check_typedef (type
);
3091 if (exp
->language_defn
->la_language
== language_cplus
3092 && (TYPE_IS_REFERENCE (type
)))
3093 type
= check_typedef (TYPE_TARGET_TYPE (type
));
3094 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
3097 /* Evaluate a subexpression of EXP, at index *POS, and return a value
3098 for that subexpression cast to TO_TYPE. Advance *POS over the
3102 evaluate_subexp_for_cast (expression
*exp
, int *pos
,
3104 struct type
*to_type
)
3108 /* Don't let symbols be evaluated with evaluate_subexp because that
3109 throws an "unknown type" error for no-debug data symbols.
3110 Instead, we want the cast to reinterpret the symbol. */
3111 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
3112 || exp
->elts
[pc
].opcode
== OP_VAR_VALUE
)
3117 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
)
3119 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
3120 return value_zero (to_type
, not_lval
);
3122 val
= evaluate_var_msym_value (noside
,
3123 exp
->elts
[pc
+ 1].objfile
,
3124 exp
->elts
[pc
+ 2].msymbol
);
3127 val
= evaluate_var_value (noside
,
3128 exp
->elts
[pc
+ 1].block
,
3129 exp
->elts
[pc
+ 2].symbol
);
3131 if (noside
== EVAL_SKIP
)
3132 return eval_skip_value (exp
);
3134 val
= value_cast (to_type
, val
);
3136 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
3137 if (VALUE_LVAL (val
) == lval_memory
)
3139 if (value_lazy (val
))
3140 value_fetch_lazy (val
);
3141 VALUE_LVAL (val
) = not_lval
;
3146 value
*val
= evaluate_subexp (to_type
, exp
, pos
, noside
);
3147 if (noside
== EVAL_SKIP
)
3148 return eval_skip_value (exp
);
3149 return value_cast (to_type
, val
);
3152 /* Parse a type expression in the string [P..P+LENGTH). */
3155 parse_and_eval_type (char *p
, int length
)
3157 char *tmp
= (char *) alloca (length
+ 4);
3160 memcpy (tmp
+ 1, p
, length
);
3161 tmp
[length
+ 1] = ')';
3162 tmp
[length
+ 2] = '0';
3163 tmp
[length
+ 3] = '\0';
3164 expression_up expr
= parse_expression (tmp
);
3165 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
3166 error (_("Internal error in eval_type."));
3167 return expr
->elts
[1].type
;