1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2021 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 *,
61 evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
62 int *pos
, enum noside noside
)
64 return ((*exp
->language_defn
->expression_ops ()->evaluate_exp
)
65 (expect_type
, exp
, pos
, noside
));
68 /* Parse the string EXP as a C expression, evaluate it,
69 and return the result as a number. */
72 parse_and_eval_address (const char *exp
)
74 expression_up expr
= parse_expression (exp
);
76 return value_as_address (evaluate_expression (expr
.get ()));
79 /* Like parse_and_eval_address, but treats the value of the expression
80 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
82 parse_and_eval_long (const char *exp
)
84 expression_up expr
= parse_expression (exp
);
86 return value_as_long (evaluate_expression (expr
.get ()));
90 parse_and_eval (const char *exp
)
92 expression_up expr
= parse_expression (exp
);
94 return evaluate_expression (expr
.get ());
97 /* Parse up to a comma (or to a closeparen)
98 in the string EXPP as an expression, evaluate it, and return the value.
99 EXPP is advanced to point to the comma. */
102 parse_to_comma_and_eval (const char **expp
)
104 expression_up expr
= parse_exp_1 (expp
, 0, nullptr, 1);
106 return evaluate_expression (expr
.get ());
110 /* See expression.h. */
113 expression::evaluate (struct type
*expect_type
, enum noside noside
)
115 gdb::optional
<enable_thread_stack_temporaries
> stack_temporaries
;
116 if (target_has_execution ()
117 && language_defn
->la_language
== language_cplus
118 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
119 stack_temporaries
.emplace (inferior_thread ());
122 struct value
*retval
= evaluate_subexp (expect_type
, this, &pos
, noside
);
124 if (stack_temporaries
.has_value ()
125 && value_in_thread_stack_temporaries (retval
, inferior_thread ()))
126 retval
= value_non_lval (retval
);
134 evaluate_expression (struct expression
*exp
, struct type
*expect_type
)
136 return exp
->evaluate (expect_type
, EVAL_NORMAL
);
139 /* Evaluate an expression, avoiding all memory references
140 and getting a value whose type alone is correct. */
143 evaluate_type (struct expression
*exp
)
145 return exp
->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS
);
148 /* Evaluate a subexpression, avoiding all memory references and
149 getting a value whose type alone is correct. */
152 evaluate_subexpression_type (struct expression
*exp
, int subexp
)
154 return evaluate_subexp (nullptr, exp
, &subexp
, EVAL_AVOID_SIDE_EFFECTS
);
157 /* Find the current value of a watchpoint on EXP. Return the value in
158 *VALP and *RESULTP and the chain of intermediate and final values
159 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
162 If PRESERVE_ERRORS is true, then exceptions are passed through.
163 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
164 occurs while evaluating the expression, *RESULTP will be set to
165 NULL. *RESULTP may be a lazy value, if the result could not be
166 read from memory. It is used to determine whether a value is
167 user-specified (we should watch the whole value) or intermediate
168 (we should watch only the bit used to locate the final value).
170 If the final value, or any intermediate value, could not be read
171 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
172 set to any referenced values. *VALP will never be a lazy value.
173 This is the value which we store in struct breakpoint.
175 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
176 released from the value chain. If VAL_CHAIN is NULL, all generated
177 values will be left on the value chain. */
180 fetch_subexp_value (struct expression
*exp
, int *pc
, struct value
**valp
,
181 struct value
**resultp
,
182 std::vector
<value_ref_ptr
> *val_chain
,
183 bool preserve_errors
)
185 struct value
*mark
, *new_mark
, *result
;
193 /* Evaluate the expression. */
194 mark
= value_mark ();
199 result
= evaluate_subexp (nullptr, exp
, pc
, EVAL_NORMAL
);
201 catch (const gdb_exception
&ex
)
203 /* Ignore memory errors if we want watchpoints pointing at
204 inaccessible memory to still be created; otherwise, throw the
205 error to some higher catcher. */
209 if (!preserve_errors
)
218 new_mark
= value_mark ();
219 if (mark
== new_mark
)
224 /* Make sure it's not lazy, so that after the target stops again we
225 have a non-lazy previous value to compare with. */
228 if (!value_lazy (result
))
235 value_fetch_lazy (result
);
238 catch (const gdb_exception_error
&except
)
246 /* Return the chain of intermediate values. We use this to
247 decide which addresses to watch. */
248 *val_chain
= value_release_to_mark (mark
);
252 /* Extract a field operation from an expression. If the subexpression
253 of EXP starting at *SUBEXP is not a structure dereference
254 operation, return NULL. Otherwise, return the name of the
255 dereferenced field, and advance *SUBEXP to point to the
256 subexpression of the left-hand-side of the dereference. This is
257 used when completing field names. */
260 extract_field_op (struct expression
*exp
, int *subexp
)
265 if (exp
->elts
[*subexp
].opcode
!= STRUCTOP_STRUCT
266 && exp
->elts
[*subexp
].opcode
!= STRUCTOP_PTR
)
268 tem
= longest_to_int (exp
->elts
[*subexp
+ 1].longconst
);
269 result
= &exp
->elts
[*subexp
+ 2].string
;
270 (*subexp
) += 1 + 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
274 /* This function evaluates brace-initializers (in C/C++) for
277 static struct value
*
278 evaluate_struct_tuple (struct value
*struct_val
,
279 struct expression
*exp
,
280 int *pos
, enum noside noside
, int nargs
)
282 struct type
*struct_type
= check_typedef (value_type (struct_val
));
283 struct type
*field_type
;
288 struct value
*val
= NULL
;
293 /* Skip static fields. */
294 while (fieldno
< struct_type
->num_fields ()
295 && field_is_static (&struct_type
->field (fieldno
)))
297 if (fieldno
>= struct_type
->num_fields ())
298 error (_("too many initializers"));
299 field_type
= struct_type
->field (fieldno
).type ();
300 if (field_type
->code () == TYPE_CODE_UNION
301 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
302 error (_("don't know which variant you want to set"));
304 /* Here, struct_type is the type of the inner struct,
305 while substruct_type is the type of the inner struct.
306 These are the same for normal structures, but a variant struct
307 contains anonymous union fields that contain substruct fields.
308 The value fieldno is the index of the top-level (normal or
309 anonymous union) field in struct_field, while the value
310 subfieldno is the index of the actual real (named inner) field
311 in substruct_type. */
313 field_type
= struct_type
->field (fieldno
).type ();
315 val
= evaluate_subexp (field_type
, exp
, pos
, noside
);
317 /* Now actually set the field in struct_val. */
319 /* Assign val to field fieldno. */
320 if (value_type (val
) != field_type
)
321 val
= value_cast (field_type
, val
);
323 bitsize
= TYPE_FIELD_BITSIZE (struct_type
, fieldno
);
324 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
325 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
327 modify_field (struct_type
, addr
,
328 value_as_long (val
), bitpos
% 8, bitsize
);
330 memcpy (addr
, value_contents (val
),
331 TYPE_LENGTH (value_type (val
)));
337 /* Promote value ARG1 as appropriate before performing a unary operation
339 If the result is not appropriate for any particular language then it
340 needs to patch this function. */
343 unop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
348 *arg1
= coerce_ref (*arg1
);
349 type1
= check_typedef (value_type (*arg1
));
351 if (is_integral_type (type1
))
353 switch (language
->la_language
)
356 /* Perform integral promotion for ANSI C/C++.
357 If not appropriate for any particular language
358 it needs to modify this function. */
360 struct type
*builtin_int
= builtin_type (gdbarch
)->builtin_int
;
362 if (TYPE_LENGTH (type1
) < TYPE_LENGTH (builtin_int
))
363 *arg1
= value_cast (builtin_int
, *arg1
);
370 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
371 operation on those two operands.
372 If the result is not appropriate for any particular language then it
373 needs to patch this function. */
376 binop_promote (const struct language_defn
*language
, struct gdbarch
*gdbarch
,
377 struct value
**arg1
, struct value
**arg2
)
379 struct type
*promoted_type
= NULL
;
383 *arg1
= coerce_ref (*arg1
);
384 *arg2
= coerce_ref (*arg2
);
386 type1
= check_typedef (value_type (*arg1
));
387 type2
= check_typedef (value_type (*arg2
));
389 if ((type1
->code () != TYPE_CODE_FLT
390 && type1
->code () != TYPE_CODE_DECFLOAT
391 && !is_integral_type (type1
))
392 || (type2
->code () != TYPE_CODE_FLT
393 && type2
->code () != TYPE_CODE_DECFLOAT
394 && !is_integral_type (type2
)))
397 if (is_fixed_point_type (type1
) || is_fixed_point_type (type2
))
400 if (type1
->code () == TYPE_CODE_DECFLOAT
401 || type2
->code () == TYPE_CODE_DECFLOAT
)
403 /* No promotion required. */
405 else if (type1
->code () == TYPE_CODE_FLT
406 || type2
->code () == TYPE_CODE_FLT
)
408 switch (language
->la_language
)
414 case language_opencl
:
415 /* No promotion required. */
419 /* For other languages the result type is unchanged from gdb
420 version 6.7 for backward compatibility.
421 If either arg was long double, make sure that value is also long
422 double. Otherwise use double. */
423 if (TYPE_LENGTH (type1
) * 8 > gdbarch_double_bit (gdbarch
)
424 || TYPE_LENGTH (type2
) * 8 > gdbarch_double_bit (gdbarch
))
425 promoted_type
= builtin_type (gdbarch
)->builtin_long_double
;
427 promoted_type
= builtin_type (gdbarch
)->builtin_double
;
431 else if (type1
->code () == TYPE_CODE_BOOL
432 && type2
->code () == TYPE_CODE_BOOL
)
434 /* No promotion required. */
437 /* Integral operations here. */
438 /* FIXME: Also mixed integral/booleans, with result an integer. */
440 const struct builtin_type
*builtin
= builtin_type (gdbarch
);
441 unsigned int promoted_len1
= TYPE_LENGTH (type1
);
442 unsigned int promoted_len2
= TYPE_LENGTH (type2
);
443 int is_unsigned1
= type1
->is_unsigned ();
444 int is_unsigned2
= type2
->is_unsigned ();
445 unsigned int result_len
;
446 int unsigned_operation
;
448 /* Determine type length and signedness after promotion for
450 if (promoted_len1
< TYPE_LENGTH (builtin
->builtin_int
))
453 promoted_len1
= TYPE_LENGTH (builtin
->builtin_int
);
455 if (promoted_len2
< TYPE_LENGTH (builtin
->builtin_int
))
458 promoted_len2
= TYPE_LENGTH (builtin
->builtin_int
);
461 if (promoted_len1
> promoted_len2
)
463 unsigned_operation
= is_unsigned1
;
464 result_len
= promoted_len1
;
466 else if (promoted_len2
> promoted_len1
)
468 unsigned_operation
= is_unsigned2
;
469 result_len
= promoted_len2
;
473 unsigned_operation
= is_unsigned1
|| is_unsigned2
;
474 result_len
= promoted_len1
;
477 switch (language
->la_language
)
483 if (result_len
<= TYPE_LENGTH (builtin
->builtin_int
))
485 promoted_type
= (unsigned_operation
486 ? builtin
->builtin_unsigned_int
487 : builtin
->builtin_int
);
489 else if (result_len
<= TYPE_LENGTH (builtin
->builtin_long
))
491 promoted_type
= (unsigned_operation
492 ? builtin
->builtin_unsigned_long
493 : builtin
->builtin_long
);
497 promoted_type
= (unsigned_operation
498 ? builtin
->builtin_unsigned_long_long
499 : builtin
->builtin_long_long
);
502 case language_opencl
:
503 if (result_len
<= TYPE_LENGTH (lookup_signed_typename
508 ? lookup_unsigned_typename (language
, "int")
509 : lookup_signed_typename (language
, "int"));
511 else if (result_len
<= TYPE_LENGTH (lookup_signed_typename
516 ? lookup_unsigned_typename (language
, "long")
517 : lookup_signed_typename (language
,"long"));
521 /* For other languages the result type is unchanged from gdb
522 version 6.7 for backward compatibility.
523 If either arg was long long, make sure that value is also long
524 long. Otherwise use long. */
525 if (unsigned_operation
)
527 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
528 promoted_type
= builtin
->builtin_unsigned_long_long
;
530 promoted_type
= builtin
->builtin_unsigned_long
;
534 if (result_len
> gdbarch_long_bit (gdbarch
) / HOST_CHAR_BIT
)
535 promoted_type
= builtin
->builtin_long_long
;
537 promoted_type
= builtin
->builtin_long
;
545 /* Promote both operands to common type. */
546 *arg1
= value_cast (promoted_type
, *arg1
);
547 *arg2
= value_cast (promoted_type
, *arg2
);
552 ptrmath_type_p (const struct language_defn
*lang
, struct type
*type
)
554 type
= check_typedef (type
);
555 if (TYPE_IS_REFERENCE (type
))
556 type
= TYPE_TARGET_TYPE (type
);
558 switch (type
->code ())
564 case TYPE_CODE_ARRAY
:
565 return type
->is_vector () ? 0 : lang
->c_style_arrays_p ();
572 /* Represents a fake method with the given parameter types. This is
573 used by the parser to construct a temporary "expected" type for
574 method overload resolution. FLAGS is used as instance flags of the
575 new type, in order to be able to make the new type represent a
576 const/volatile overload. */
581 fake_method (type_instance_flags flags
,
582 int num_types
, struct type
**param_types
);
585 /* The constructed type. */
586 struct type
*type () { return &m_type
; }
589 struct type m_type
{};
590 main_type m_main_type
{};
593 fake_method::fake_method (type_instance_flags flags
,
594 int num_types
, struct type
**param_types
)
596 struct type
*type
= &m_type
;
598 TYPE_MAIN_TYPE (type
) = &m_main_type
;
599 TYPE_LENGTH (type
) = 1;
600 type
->set_code (TYPE_CODE_METHOD
);
601 TYPE_CHAIN (type
) = type
;
602 type
->set_instance_flags (flags
);
605 if (param_types
[num_types
- 1] == NULL
)
608 type
->set_has_varargs (true);
610 else if (check_typedef (param_types
[num_types
- 1])->code ()
614 /* Caller should have ensured this. */
615 gdb_assert (num_types
== 0);
616 type
->set_is_prototyped (true);
620 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
621 neither an objfile nor a gdbarch. As a result we must manually
622 allocate memory for auxiliary fields, and free the memory ourselves
623 when we are done with it. */
624 type
->set_num_fields (num_types
);
626 ((struct field
*) xzalloc (sizeof (struct field
) * num_types
));
628 while (num_types
-- > 0)
629 type
->field (num_types
).set_type (param_types
[num_types
]);
632 fake_method::~fake_method ()
634 xfree (m_type
.fields ());
637 /* Helper for evaluating an OP_VAR_VALUE. */
640 evaluate_var_value (enum noside noside
, const block
*blk
, symbol
*var
)
642 /* JYG: We used to just return value_zero of the symbol type if
643 we're asked to avoid side effects. Otherwise we return
644 value_of_variable (...). However I'm not sure if
645 value_of_variable () has any side effect. We need a full value
646 object returned here for whatis_exp () to call evaluate_type ()
647 and then pass the full value to value_rtti_target_type () if we
648 are dealing with a pointer or reference to a base class and print
651 struct value
*ret
= NULL
;
655 ret
= value_of_variable (var
, blk
);
658 catch (const gdb_exception_error
&except
)
660 if (noside
!= EVAL_AVOID_SIDE_EFFECTS
)
663 ret
= value_zero (SYMBOL_TYPE (var
), not_lval
);
669 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
672 evaluate_var_msym_value (enum noside noside
,
673 struct objfile
*objfile
, minimal_symbol
*msymbol
)
676 type
*the_type
= find_minsym_type_and_address (msymbol
, objfile
, &address
);
678 if (noside
== EVAL_AVOID_SIDE_EFFECTS
&& !the_type
->is_gnu_ifunc ())
679 return value_zero (the_type
, not_lval
);
681 return value_at_lazy (the_type
, address
);
684 /* Helper for returning a value when handling EVAL_SKIP. */
687 eval_skip_value (expression
*exp
)
689 return value_from_longest (builtin_type (exp
->gdbarch
)->builtin_int
, 1);
692 /* See expression.h. */
695 evaluate_subexp_do_call (expression
*exp
, enum noside noside
,
697 gdb::array_view
<value
*> argvec
,
698 const char *function_name
,
699 type
*default_return_type
)
702 error (_("Cannot evaluate function -- may be inlined"));
703 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
705 /* If the return type doesn't look like a function type,
706 call an error. This can happen if somebody tries to turn
707 a variable into a function call. */
709 type
*ftype
= value_type (callee
);
711 if (ftype
->code () == TYPE_CODE_INTERNAL_FUNCTION
)
713 /* We don't know anything about what the internal
714 function might return, but we have to return
716 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
719 else if (ftype
->code () == TYPE_CODE_XMETHOD
)
721 type
*return_type
= result_type_of_xmethod (callee
, argvec
);
723 if (return_type
== NULL
)
724 error (_("Xmethod is missing return type."));
725 return value_zero (return_type
, not_lval
);
727 else if (ftype
->code () == TYPE_CODE_FUNC
728 || ftype
->code () == TYPE_CODE_METHOD
)
730 if (ftype
->is_gnu_ifunc ())
732 CORE_ADDR address
= value_address (callee
);
733 type
*resolved_type
= find_gnu_ifunc_target_type (address
);
735 if (resolved_type
!= NULL
)
736 ftype
= resolved_type
;
739 type
*return_type
= TYPE_TARGET_TYPE (ftype
);
741 if (return_type
== NULL
)
742 return_type
= default_return_type
;
744 if (return_type
== NULL
)
745 error_call_unknown_return_type (function_name
);
747 return allocate_value (return_type
);
750 error (_("Expression of type other than "
751 "\"Function returning ...\" used as function"));
753 switch (value_type (callee
)->code ())
755 case TYPE_CODE_INTERNAL_FUNCTION
:
756 return call_internal_function (exp
->gdbarch
, exp
->language_defn
,
757 callee
, argvec
.size (), argvec
.data ());
758 case TYPE_CODE_XMETHOD
:
759 return call_xmethod (callee
, argvec
);
761 return call_function_by_hand (callee
, default_return_type
, argvec
);
765 /* Helper for evaluating an OP_FUNCALL. */
768 evaluate_funcall (type
*expect_type
, expression
*exp
, int *pos
,
776 symbol
*function
= NULL
;
777 char *function_name
= NULL
;
778 const char *var_func_name
= NULL
;
783 exp_opcode op
= exp
->elts
[*pos
].opcode
;
784 int nargs
= longest_to_int (exp
->elts
[pc
].longconst
);
785 /* Allocate arg vector, including space for the function to be
786 called in argvec[0], a potential `this', and a terminating
788 value
**argvec
= (value
**) alloca (sizeof (value
*) * (nargs
+ 3));
789 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
791 /* First, evaluate the structure into arg2. */
794 if (op
== STRUCTOP_MEMBER
)
796 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
800 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
803 /* If the function is a virtual function, then the aggregate
804 value (providing the structure) plays its part by providing
805 the vtable. Otherwise, it is just along for the ride: call
806 the function directly. */
808 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
810 type
*a1_type
= check_typedef (value_type (arg1
));
811 if (noside
== EVAL_SKIP
)
812 tem
= 1; /* Set it to the right arg index so that all
813 arguments can also be skipped. */
814 else if (a1_type
->code () == TYPE_CODE_METHODPTR
)
816 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
817 arg1
= value_zero (TYPE_TARGET_TYPE (a1_type
), not_lval
);
819 arg1
= cplus_method_ptr_to_value (&arg2
, arg1
);
821 /* Now, say which argument to start evaluating from. */
826 else if (a1_type
->code () == TYPE_CODE_MEMBERPTR
)
828 struct type
*type_ptr
829 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type
));
830 struct type
*target_type_ptr
831 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type
));
833 /* Now, convert these values to an address. */
834 arg2
= value_cast (type_ptr
, arg2
);
836 long mem_offset
= value_as_long (arg1
);
838 arg1
= value_from_pointer (target_type_ptr
,
839 value_as_long (arg2
) + mem_offset
);
840 arg1
= value_ind (arg1
);
844 error (_("Non-pointer-to-member value used in pointer-to-member "
847 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
849 /* Hair for method invocations. */
853 /* First, evaluate the structure into arg2. */
855 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
856 *pos
+= 3 + BYTES_TO_EXP_ELEM (tem2
+ 1);
858 if (op
== STRUCTOP_STRUCT
)
860 /* If v is a variable in a register, and the user types
861 v.method (), this will produce an error, because v has no
864 A possible way around this would be to allocate a copy of
865 the variable on the stack, copy in the contents, call the
866 function, and copy out the contents. I.e. convert this
867 from call by reference to call by copy-return (or
868 whatever it's called). However, this does not work
869 because it is not the same: the method being called could
870 stash a copy of the address, and then future uses through
871 that address (after the method returns) would be expected
872 to use the variable itself, not some copy of it. */
873 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
877 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
879 /* Check to see if the operator '->' has been overloaded.
880 If the operator has been overloaded replace arg2 with the
881 value returned by the custom operator and continue
883 while (unop_user_defined_p (op
, arg2
))
885 struct value
*value
= NULL
;
888 value
= value_x_unop (arg2
, op
, noside
);
891 catch (const gdb_exception_error
&except
)
893 if (except
.error
== NOT_FOUND_ERROR
)
902 /* Now, say which argument to start evaluating from. */
905 else if (op
== OP_SCOPE
906 && overload_resolution
907 && (exp
->language_defn
->la_language
== language_cplus
))
909 /* Unpack it locally so we can properly handle overload
915 local_tem
= longest_to_int (exp
->elts
[pc2
+ 2].longconst
);
916 (*pos
) += 4 + BYTES_TO_EXP_ELEM (local_tem
+ 1);
917 struct type
*type
= exp
->elts
[pc2
+ 1].type
;
918 name
= &exp
->elts
[pc2
+ 3].string
;
921 function_name
= NULL
;
922 if (type
->code () == TYPE_CODE_NAMESPACE
)
924 function
= cp_lookup_symbol_namespace (type
->name (),
926 get_selected_block (0),
928 if (function
== NULL
)
929 error (_("No symbol \"%s\" in namespace \"%s\"."),
930 name
, type
->name ());
933 /* arg2 is left as NULL on purpose. */
937 gdb_assert (type
->code () == TYPE_CODE_STRUCT
938 || type
->code () == TYPE_CODE_UNION
);
939 function_name
= name
;
941 /* We need a properly typed value for method lookup. For
942 static methods arg2 is otherwise unused. */
943 arg2
= value_zero (type
, lval_memory
);
948 else if (op
== OP_ADL_FUNC
)
950 /* Save the function position and move pos so that the arguments
957 func_name_len
= longest_to_int (exp
->elts
[save_pos1
+ 3].longconst
);
958 (*pos
) += 6 + BYTES_TO_EXP_ELEM (func_name_len
+ 1);
962 /* Non-method function call. */
966 /* If this is a C++ function wait until overload resolution. */
967 if (op
== OP_VAR_VALUE
968 && overload_resolution
969 && (exp
->language_defn
->la_language
== language_cplus
))
971 (*pos
) += 4; /* Skip the evaluation of the symbol. */
976 if (op
== OP_VAR_MSYM_VALUE
)
978 minimal_symbol
*msym
= exp
->elts
[*pos
+ 2].msymbol
;
979 var_func_name
= msym
->print_name ();
981 else if (op
== OP_VAR_VALUE
)
983 symbol
*sym
= exp
->elts
[*pos
+ 2].symbol
;
984 var_func_name
= sym
->print_name ();
987 argvec
[0] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
988 type
*type
= value_type (argvec
[0]);
989 if (type
&& type
->code () == TYPE_CODE_PTR
)
990 type
= TYPE_TARGET_TYPE (type
);
991 if (type
&& type
->code () == TYPE_CODE_FUNC
)
993 for (; tem
<= nargs
&& tem
<= type
->num_fields (); tem
++)
995 argvec
[tem
] = evaluate_subexp (type
->field (tem
- 1).type (),
1002 /* Evaluate arguments (if not already done, e.g., namespace::func()
1003 and overload-resolution is off). */
1004 for (; tem
<= nargs
; tem
++)
1006 /* Ensure that array expressions are coerced into pointer
1008 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1011 /* Signal end of arglist. */
1014 if (noside
== EVAL_SKIP
)
1015 return eval_skip_value (exp
);
1017 if (op
== OP_ADL_FUNC
)
1019 struct symbol
*symp
;
1022 int string_pc
= save_pos1
+ 3;
1024 /* Extract the function name. */
1025 name_len
= longest_to_int (exp
->elts
[string_pc
].longconst
);
1026 func_name
= (char *) alloca (name_len
+ 1);
1027 strcpy (func_name
, &exp
->elts
[string_pc
+ 1].string
);
1029 find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1031 NON_METHOD
, /* not method */
1032 NULL
, NULL
, /* pass NULL symbol since
1033 symbol is unknown */
1034 NULL
, &symp
, NULL
, 0, noside
);
1036 /* Now fix the expression being evaluated. */
1037 exp
->elts
[save_pos1
+ 2].symbol
= symp
;
1038 argvec
[0] = evaluate_subexp_with_coercion (exp
, &save_pos1
, noside
);
1041 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
1042 || (op
== OP_SCOPE
&& function_name
!= NULL
))
1044 int static_memfuncp
;
1047 /* Method invocation: stuff "this" as first parameter. If the
1048 method turns out to be static we undo this below. */
1053 /* Name of method from expression. */
1054 tstr
= &exp
->elts
[pc2
+ 2].string
;
1057 tstr
= function_name
;
1059 if (overload_resolution
&& (exp
->language_defn
->la_language
1062 /* Language is C++, do some overload resolution before
1064 struct value
*valp
= NULL
;
1066 (void) find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1068 METHOD
, /* method */
1069 &arg2
, /* the object */
1071 &static_memfuncp
, 0, noside
);
1073 if (op
== OP_SCOPE
&& !static_memfuncp
)
1075 /* For the time being, we don't handle this. */
1076 error (_("Call to overloaded function %s requires "
1080 argvec
[1] = arg2
; /* the ``this'' pointer */
1081 argvec
[0] = valp
; /* Use the method found after overload
1085 /* Non-C++ case -- or no overload resolution. */
1087 struct value
*temp
= arg2
;
1089 argvec
[0] = value_struct_elt (&temp
, argvec
+ 1, tstr
,
1091 op
== STRUCTOP_STRUCT
1092 ? "structure" : "structure pointer");
1093 /* value_struct_elt updates temp with the correct value of
1094 the ``this'' pointer if necessary, so modify argvec[1] to
1095 reflect any ``this'' changes. */
1097 = value_from_longest (lookup_pointer_type(value_type (temp
)),
1098 value_address (temp
)
1099 + value_embedded_offset (temp
));
1100 argvec
[1] = arg2
; /* the ``this'' pointer */
1103 /* Take out `this' if needed. */
1104 if (static_memfuncp
)
1106 argvec
[1] = argvec
[0];
1111 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
1113 /* Pointer to member. argvec[1] is already set up. */
1116 else if (op
== OP_VAR_VALUE
|| (op
== OP_SCOPE
&& function
!= NULL
))
1118 /* Non-member function being called. */
1119 /* fn: This can only be done for C++ functions. A C-style
1120 function in a C++ program, for instance, does not have the
1121 fields that are expected here. */
1123 if (overload_resolution
&& (exp
->language_defn
->la_language
1126 /* Language is C++, do some overload resolution before
1128 struct symbol
*symp
;
1131 /* If a scope has been specified disable ADL. */
1135 if (op
== OP_VAR_VALUE
)
1136 function
= exp
->elts
[save_pos1
+2].symbol
;
1138 (void) find_overload_match (gdb::make_array_view (&argvec
[1], nargs
),
1139 NULL
, /* no need for name */
1140 NON_METHOD
, /* not method */
1141 NULL
, function
, /* the function */
1142 NULL
, &symp
, NULL
, no_adl
, noside
);
1144 if (op
== OP_VAR_VALUE
)
1146 /* Now fix the expression being evaluated. */
1147 exp
->elts
[save_pos1
+2].symbol
= symp
;
1148 argvec
[0] = evaluate_subexp_with_coercion (exp
, &save_pos1
,
1152 argvec
[0] = value_of_variable (symp
, get_selected_block (0));
1156 /* Not C++, or no overload resolution allowed. */
1157 /* Nothing to be done; argvec already correctly set up. */
1162 /* It is probably a C-style function. */
1163 /* Nothing to be done; argvec already correctly set up. */
1166 return evaluate_subexp_do_call (exp
, noside
, argvec
[0],
1167 gdb::make_array_view (argvec
+ 1, nargs
),
1168 var_func_name
, expect_type
);
1171 /* Return true if type is integral or reference to integral */
1174 is_integral_or_integral_reference (struct type
*type
)
1176 if (is_integral_type (type
))
1179 type
= check_typedef (type
);
1180 return (type
!= nullptr
1181 && TYPE_IS_REFERENCE (type
)
1182 && is_integral_type (TYPE_TARGET_TYPE (type
)));
1185 /* Helper function that implements the body of OP_SCOPE. */
1187 static struct value
*
1188 eval_op_scope (struct type
*expect_type
, struct expression
*exp
,
1190 struct type
*type
, const char *string
)
1192 if (noside
== EVAL_SKIP
)
1193 return eval_skip_value (exp
);
1194 struct value
*arg1
= value_aggregate_elt (type
, string
, expect_type
,
1197 error (_("There is no field named %s"), string
);
1201 /* Helper function that implements the body of OP_VAR_ENTRY_VALUE. */
1203 static struct value
*
1204 eval_op_var_entry_value (struct type
*expect_type
, struct expression
*exp
,
1205 enum noside noside
, symbol
*sym
)
1207 if (noside
== EVAL_SKIP
)
1208 return eval_skip_value (exp
);
1209 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1210 return value_zero (SYMBOL_TYPE (sym
), not_lval
);
1212 if (SYMBOL_COMPUTED_OPS (sym
) == NULL
1213 || SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry
== NULL
)
1214 error (_("Symbol \"%s\" does not have any specific entry value"),
1215 sym
->print_name ());
1217 struct frame_info
*frame
= get_selected_frame (NULL
);
1218 return SYMBOL_COMPUTED_OPS (sym
)->read_variable_at_entry (sym
, frame
);
1221 /* Helper function that implements the body of OP_VAR_MSYM_VALUE. */
1223 static struct value
*
1224 eval_op_var_msym_value (struct type
*expect_type
, struct expression
*exp
,
1225 enum noside noside
, bool outermost_p
,
1226 minimal_symbol
*msymbol
, struct objfile
*objfile
)
1228 value
*val
= evaluate_var_msym_value (noside
, objfile
, msymbol
);
1230 struct type
*type
= value_type (val
);
1231 if (type
->code () == TYPE_CODE_ERROR
1232 && (noside
!= EVAL_AVOID_SIDE_EFFECTS
|| !outermost_p
))
1233 error_unknown_type (msymbol
->print_name ());
1237 /* Helper function that implements the body of OP_FUNC_STATIC_VAR. */
1239 static struct value
*
1240 eval_op_func_static_var (struct type
*expect_type
, struct expression
*exp
,
1242 value
*func
, const char *var
)
1244 if (noside
== EVAL_SKIP
)
1245 return eval_skip_value (exp
);
1246 CORE_ADDR addr
= value_address (func
);
1247 const block
*blk
= block_for_pc (addr
);
1248 struct block_symbol sym
= lookup_symbol (var
, blk
, VAR_DOMAIN
, NULL
);
1249 if (sym
.symbol
== NULL
)
1250 error (_("No symbol \"%s\" in specified context."), var
);
1251 return evaluate_var_value (noside
, sym
.block
, sym
.symbol
);
1254 /* Helper function that implements the body of OP_REGISTER. */
1256 static struct value
*
1257 eval_op_register (struct type
*expect_type
, struct expression
*exp
,
1258 enum noside noside
, const char *name
)
1263 regno
= user_reg_map_name_to_regnum (exp
->gdbarch
,
1264 name
, strlen (name
));
1266 error (_("Register $%s not available."), name
);
1268 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1269 a value with the appropriate register type. Unfortunately,
1270 we don't have easy access to the type of user registers.
1271 So for these registers, we fetch the register value regardless
1272 of the evaluation mode. */
1273 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1274 && regno
< gdbarch_num_cooked_regs (exp
->gdbarch
))
1275 val
= value_zero (register_type (exp
->gdbarch
, regno
), not_lval
);
1277 val
= value_of_register (regno
, get_selected_frame (NULL
));
1279 error (_("Value of register %s not available."), name
);
1284 /* Helper function that implements the body of OP_STRING. */
1286 static struct value
*
1287 eval_op_string (struct type
*expect_type
, struct expression
*exp
,
1288 enum noside noside
, int len
, const char *string
)
1290 if (noside
== EVAL_SKIP
)
1291 return eval_skip_value (exp
);
1292 struct type
*type
= language_string_char_type (exp
->language_defn
,
1294 return value_string (string
, len
, type
);
1297 /* Helper function that implements the body of OP_OBJC_SELECTOR. */
1299 static struct value
*
1300 eval_op_objc_selector (struct type
*expect_type
, struct expression
*exp
,
1304 if (noside
== EVAL_SKIP
)
1305 return eval_skip_value (exp
);
1307 struct type
*selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1308 return value_from_longest (selector_type
,
1309 lookup_child_selector (exp
->gdbarch
, sel
));
1312 /* Helper function that implements the body of BINOP_CONCAT. */
1314 static struct value
*
1315 eval_op_concat (struct type
*expect_type
, struct expression
*exp
,
1317 enum exp_opcode op
, struct value
*arg1
, struct value
*arg2
)
1319 if (noside
== EVAL_SKIP
)
1320 return eval_skip_value (exp
);
1321 if (binop_user_defined_p (op
, arg1
, arg2
))
1322 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1324 return value_concat (arg1
, arg2
);
1327 /* A helper function for TERNOP_SLICE. */
1329 static struct value
*
1330 eval_op_ternop (struct type
*expect_type
, struct expression
*exp
,
1332 struct value
*array
, struct value
*low
, struct value
*upper
)
1334 if (noside
== EVAL_SKIP
)
1335 return eval_skip_value (exp
);
1336 int lowbound
= value_as_long (low
);
1337 int upperbound
= value_as_long (upper
);
1338 return value_slice (array
, lowbound
, upperbound
- lowbound
+ 1);
1341 /* A helper function for STRUCTOP_STRUCT. */
1343 static struct value
*
1344 eval_op_structop_struct (struct type
*expect_type
, struct expression
*exp
,
1346 struct value
*arg1
, const char *string
)
1348 if (noside
== EVAL_SKIP
)
1349 return eval_skip_value (exp
);
1350 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1352 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1353 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1357 /* A helper function for STRUCTOP_PTR. */
1359 static struct value
*
1360 eval_op_structop_ptr (struct type
*expect_type
, struct expression
*exp
,
1361 enum noside noside
, enum exp_opcode op
,
1362 struct value
*arg1
, const char *string
)
1364 if (noside
== EVAL_SKIP
)
1365 return eval_skip_value (exp
);
1367 /* Check to see if operator '->' has been overloaded. If so replace
1368 arg1 with the value returned by evaluating operator->(). */
1369 while (unop_user_defined_p (op
, arg1
))
1371 struct value
*value
= NULL
;
1374 value
= value_x_unop (arg1
, op
, noside
);
1377 catch (const gdb_exception_error
&except
)
1379 if (except
.error
== NOT_FOUND_ERROR
)
1388 /* JYG: if print object is on we need to replace the base type
1389 with rtti type in order to continue on with successful
1390 lookup of member / method only available in the rtti type. */
1392 struct type
*arg_type
= value_type (arg1
);
1393 struct type
*real_type
;
1394 int full
, using_enc
;
1396 struct value_print_options opts
;
1398 get_user_print_options (&opts
);
1399 if (opts
.objectprint
&& TYPE_TARGET_TYPE (arg_type
)
1400 && (TYPE_TARGET_TYPE (arg_type
)->code () == TYPE_CODE_STRUCT
))
1402 real_type
= value_rtti_indirect_type (arg1
, &full
, &top
,
1405 arg1
= value_cast (real_type
, arg1
);
1409 struct value
*arg3
= value_struct_elt (&arg1
, NULL
, string
,
1410 NULL
, "structure pointer");
1411 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1412 arg3
= value_zero (value_type (arg3
), VALUE_LVAL (arg3
));
1416 /* A helper function for STRUCTOP_MEMBER. */
1418 static struct value
*
1419 eval_op_member (struct type
*expect_type
, struct expression
*exp
,
1421 struct value
*arg1
, struct value
*arg2
)
1425 if (noside
== EVAL_SKIP
)
1426 return eval_skip_value (exp
);
1429 struct type
*type
= check_typedef (value_type (arg2
));
1430 switch (type
->code ())
1432 case TYPE_CODE_METHODPTR
:
1433 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1434 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1437 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1438 gdb_assert (value_type (arg2
)->code () == TYPE_CODE_PTR
);
1439 return value_ind (arg2
);
1442 case TYPE_CODE_MEMBERPTR
:
1443 /* Now, convert these values to an address. */
1444 arg1
= value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type
)),
1447 mem_offset
= value_as_long (arg2
);
1449 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1450 value_as_long (arg1
) + mem_offset
);
1451 return value_ind (arg3
);
1454 error (_("non-pointer-to-member value used "
1455 "in pointer-to-member construct"));
1459 /* A helper function for BINOP_ADD. */
1461 static struct value
*
1462 eval_op_add (struct type
*expect_type
, struct expression
*exp
,
1463 enum noside noside
, enum exp_opcode op
,
1464 struct value
*arg1
, struct value
*arg2
)
1466 if (noside
== EVAL_SKIP
)
1467 return eval_skip_value (exp
);
1468 if (binop_user_defined_p (op
, arg1
, arg2
))
1469 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1470 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1471 && is_integral_or_integral_reference (value_type (arg2
)))
1472 return value_ptradd (arg1
, value_as_long (arg2
));
1473 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg2
))
1474 && is_integral_or_integral_reference (value_type (arg1
)))
1475 return value_ptradd (arg2
, value_as_long (arg1
));
1478 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1479 return value_binop (arg1
, arg2
, BINOP_ADD
);
1483 /* A helper function for BINOP_SUB. */
1485 static struct value
*
1486 eval_op_sub (struct type
*expect_type
, struct expression
*exp
,
1487 enum noside noside
, enum exp_opcode op
,
1488 struct value
*arg1
, struct value
*arg2
)
1490 if (noside
== EVAL_SKIP
)
1491 return eval_skip_value (exp
);
1492 if (binop_user_defined_p (op
, arg1
, arg2
))
1493 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1494 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1495 && ptrmath_type_p (exp
->language_defn
, value_type (arg2
)))
1497 /* FIXME -- should be ptrdiff_t */
1498 struct type
*type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1499 return value_from_longest (type
, value_ptrdiff (arg1
, arg2
));
1501 else if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
))
1502 && is_integral_or_integral_reference (value_type (arg2
)))
1503 return value_ptradd (arg1
, - value_as_long (arg2
));
1506 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1507 return value_binop (arg1
, arg2
, BINOP_SUB
);
1511 /* Helper function for several different binary operations. */
1513 static struct value
*
1514 eval_op_binary (struct type
*expect_type
, struct expression
*exp
,
1515 enum noside noside
, enum exp_opcode op
,
1516 struct value
*arg1
, struct value
*arg2
)
1518 if (noside
== EVAL_SKIP
)
1519 return eval_skip_value (exp
);
1520 if (binop_user_defined_p (op
, arg1
, arg2
))
1521 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1524 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
1525 fudge arg2 to avoid division-by-zero, the caller is
1526 (theoretically) only looking for the type of the result. */
1527 if (noside
== EVAL_AVOID_SIDE_EFFECTS
1528 /* ??? Do we really want to test for BINOP_MOD here?
1529 The implementation of value_binop gives it a well-defined
1532 || op
== BINOP_INTDIV
1535 && value_logical_not (arg2
))
1537 struct value
*v_one
;
1539 v_one
= value_one (value_type (arg2
));
1540 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &v_one
);
1541 return value_binop (arg1
, v_one
, op
);
1545 /* For shift and integer exponentiation operations,
1546 only promote the first argument. */
1547 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
1548 && is_integral_type (value_type (arg2
)))
1549 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
1551 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1553 return value_binop (arg1
, arg2
, op
);
1558 /* A helper function for BINOP_SUBSCRIPT. */
1560 static struct value
*
1561 eval_op_subscript (struct type
*expect_type
, struct expression
*exp
,
1562 enum noside noside
, enum exp_opcode op
,
1563 struct value
*arg1
, struct value
*arg2
)
1565 if (noside
== EVAL_SKIP
)
1566 return eval_skip_value (exp
);
1567 if (binop_user_defined_p (op
, arg1
, arg2
))
1568 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1571 /* If the user attempts to subscript something that is not an
1572 array or pointer type (like a plain int variable for example),
1573 then report this as an error. */
1575 arg1
= coerce_ref (arg1
);
1576 struct type
*type
= check_typedef (value_type (arg1
));
1577 if (type
->code () != TYPE_CODE_ARRAY
1578 && type
->code () != TYPE_CODE_PTR
)
1581 error (_("cannot subscript something of type `%s'"),
1584 error (_("cannot subscript requested type"));
1587 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1588 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1590 return value_subscript (arg1
, value_as_long (arg2
));
1594 /* A helper function for BINOP_EQUAL. */
1596 static struct value
*
1597 eval_op_equal (struct type
*expect_type
, struct expression
*exp
,
1598 enum noside noside
, enum exp_opcode op
,
1599 struct value
*arg1
, struct value
*arg2
)
1601 if (noside
== EVAL_SKIP
)
1602 return eval_skip_value (exp
);
1603 if (binop_user_defined_p (op
, arg1
, arg2
))
1605 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1609 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1610 int tem
= value_equal (arg1
, arg2
);
1611 struct type
*type
= language_bool_type (exp
->language_defn
,
1613 return value_from_longest (type
, (LONGEST
) tem
);
1617 /* A helper function for BINOP_NOTEQUAL. */
1619 static struct value
*
1620 eval_op_notequal (struct type
*expect_type
, struct expression
*exp
,
1621 enum noside noside
, enum exp_opcode op
,
1622 struct value
*arg1
, struct value
*arg2
)
1624 if (noside
== EVAL_SKIP
)
1625 return eval_skip_value (exp
);
1626 if (binop_user_defined_p (op
, arg1
, arg2
))
1628 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1632 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1633 int tem
= value_equal (arg1
, arg2
);
1634 struct type
*type
= language_bool_type (exp
->language_defn
,
1636 return value_from_longest (type
, (LONGEST
) ! tem
);
1640 /* A helper function for BINOP_LESS. */
1642 static struct value
*
1643 eval_op_less (struct type
*expect_type
, struct expression
*exp
,
1644 enum noside noside
, enum exp_opcode op
,
1645 struct value
*arg1
, struct value
*arg2
)
1647 if (noside
== EVAL_SKIP
)
1648 return eval_skip_value (exp
);
1649 if (binop_user_defined_p (op
, arg1
, arg2
))
1651 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1655 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
1656 int tem
= value_less (arg1
, arg2
);
1657 struct type
*type
= language_bool_type (exp
->language_defn
,
1659 return value_from_longest (type
, (LONGEST
) tem
);
1664 evaluate_subexp_standard (struct type
*expect_type
,
1665 struct expression
*exp
, int *pos
,
1669 int tem
, tem2
, tem3
;
1671 struct value
*arg1
= NULL
;
1672 struct value
*arg2
= NULL
;
1676 struct value
**argvec
;
1678 struct type
**arg_types
;
1681 op
= exp
->elts
[pc
].opcode
;
1686 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1687 (*pos
) += 4 + BYTES_TO_EXP_ELEM (tem
+ 1);
1688 return eval_op_scope (expect_type
, exp
, noside
,
1689 exp
->elts
[pc
+ 1].type
,
1690 &exp
->elts
[pc
+ 3].string
);
1694 return value_from_longest (exp
->elts
[pc
+ 1].type
,
1695 exp
->elts
[pc
+ 2].longconst
);
1699 return value_from_contents (exp
->elts
[pc
+ 1].type
,
1700 exp
->elts
[pc
+ 2].floatconst
);
1706 symbol
*var
= exp
->elts
[pc
+ 2].symbol
;
1707 if (SYMBOL_TYPE (var
)->code () == TYPE_CODE_ERROR
)
1708 error_unknown_type (var
->print_name ());
1709 if (noside
!= EVAL_SKIP
)
1710 return evaluate_var_value (noside
, exp
->elts
[pc
+ 1].block
, var
);
1713 /* Return a dummy value of the correct type when skipping, so
1714 that parent functions know what is to be skipped. */
1715 return allocate_value (SYMBOL_TYPE (var
));
1719 case OP_VAR_MSYM_VALUE
:
1723 minimal_symbol
*msymbol
= exp
->elts
[pc
+ 2].msymbol
;
1724 return eval_op_var_msym_value (expect_type
, exp
, noside
,
1726 exp
->elts
[pc
+ 1].objfile
);
1729 case OP_VAR_ENTRY_VALUE
:
1733 struct symbol
*sym
= exp
->elts
[pc
+ 1].symbol
;
1735 return eval_op_var_entry_value (expect_type
, exp
, noside
, sym
);
1738 case OP_FUNC_STATIC_VAR
:
1739 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1740 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1741 if (noside
== EVAL_SKIP
)
1742 return eval_skip_value (exp
);
1745 value
*func
= evaluate_subexp_standard (NULL
, exp
, pos
, noside
);
1747 return eval_op_func_static_var (expect_type
, exp
, noside
, func
,
1748 &exp
->elts
[pc
+ 2].string
);
1754 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
1758 const char *name
= &exp
->elts
[pc
+ 2].string
;
1760 (*pos
) += 3 + BYTES_TO_EXP_ELEM (exp
->elts
[pc
+ 1].longconst
+ 1);
1761 return eval_op_register (expect_type
, exp
, noside
, name
);
1765 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
1766 return value_from_longest (type
, exp
->elts
[pc
+ 1].longconst
);
1768 case OP_INTERNALVAR
:
1770 return value_of_internalvar (exp
->gdbarch
,
1771 exp
->elts
[pc
+ 1].internalvar
);
1774 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1775 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1776 return eval_op_string (expect_type
, exp
, noside
, tem
,
1777 &exp
->elts
[pc
+ 2].string
);
1779 case OP_OBJC_NSSTRING
: /* Objective C Foundation Class
1780 NSString constant. */
1781 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1782 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1783 if (noside
== EVAL_SKIP
)
1784 return eval_skip_value (exp
);
1785 return value_nsstring (exp
->gdbarch
, &exp
->elts
[pc
+ 2].string
, tem
+ 1);
1789 tem2
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1790 tem3
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
1791 nargs
= tem3
- tem2
+ 1;
1792 type
= expect_type
? check_typedef (expect_type
) : nullptr;
1794 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1795 && type
->code () == TYPE_CODE_STRUCT
)
1797 struct value
*rec
= allocate_value (expect_type
);
1799 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
1800 return evaluate_struct_tuple (rec
, exp
, pos
, noside
, nargs
);
1803 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1804 && type
->code () == TYPE_CODE_ARRAY
)
1806 struct type
*range_type
= type
->index_type ();
1807 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
1808 struct value
*array
= allocate_value (expect_type
);
1809 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
1810 LONGEST low_bound
, high_bound
, index
;
1812 if (!get_discrete_bounds (range_type
, &low_bound
, &high_bound
))
1815 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
1818 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
1819 for (tem
= nargs
; --nargs
>= 0;)
1821 struct value
*element
;
1823 element
= evaluate_subexp (element_type
, exp
, pos
, noside
);
1824 if (value_type (element
) != element_type
)
1825 element
= value_cast (element_type
, element
);
1826 if (index
> high_bound
)
1827 /* To avoid memory corruption. */
1828 error (_("Too many array elements"));
1829 memcpy (value_contents_raw (array
)
1830 + (index
- low_bound
) * element_size
,
1831 value_contents (element
),
1838 if (expect_type
!= nullptr && noside
!= EVAL_SKIP
1839 && type
->code () == TYPE_CODE_SET
)
1841 struct value
*set
= allocate_value (expect_type
);
1842 gdb_byte
*valaddr
= value_contents_raw (set
);
1843 struct type
*element_type
= type
->index_type ();
1844 struct type
*check_type
= element_type
;
1845 LONGEST low_bound
, high_bound
;
1847 /* Get targettype of elementtype. */
1848 while (check_type
->code () == TYPE_CODE_RANGE
1849 || check_type
->code () == TYPE_CODE_TYPEDEF
)
1850 check_type
= TYPE_TARGET_TYPE (check_type
);
1852 if (!get_discrete_bounds (element_type
, &low_bound
, &high_bound
))
1853 error (_("(power)set type with unknown size"));
1854 memset (valaddr
, '\0', TYPE_LENGTH (type
));
1855 for (tem
= 0; tem
< nargs
; tem
++)
1857 LONGEST range_low
, range_high
;
1858 struct type
*range_low_type
, *range_high_type
;
1859 struct value
*elem_val
;
1861 elem_val
= evaluate_subexp (element_type
, exp
, pos
, noside
);
1862 range_low_type
= range_high_type
= value_type (elem_val
);
1863 range_low
= range_high
= value_as_long (elem_val
);
1865 /* Check types of elements to avoid mixture of elements from
1866 different types. Also check if type of element is "compatible"
1867 with element type of powerset. */
1868 if (range_low_type
->code () == TYPE_CODE_RANGE
)
1869 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
1870 if (range_high_type
->code () == TYPE_CODE_RANGE
)
1871 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
1872 if ((range_low_type
->code () != range_high_type
->code ())
1873 || (range_low_type
->code () == TYPE_CODE_ENUM
1874 && (range_low_type
!= range_high_type
)))
1875 /* different element modes. */
1876 error (_("POWERSET tuple elements of different mode"));
1877 if ((check_type
->code () != range_low_type
->code ())
1878 || (check_type
->code () == TYPE_CODE_ENUM
1879 && range_low_type
!= check_type
))
1880 error (_("incompatible POWERSET tuple elements"));
1881 if (range_low
> range_high
)
1883 warning (_("empty POWERSET tuple range"));
1886 if (range_low
< low_bound
|| range_high
> high_bound
)
1887 error (_("POWERSET tuple element out of range"));
1888 range_low
-= low_bound
;
1889 range_high
-= low_bound
;
1890 for (; range_low
<= range_high
; range_low
++)
1892 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
1894 if (gdbarch_byte_order (exp
->gdbarch
) == BFD_ENDIAN_BIG
)
1895 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
1896 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
1903 argvec
= XALLOCAVEC (struct value
*, nargs
);
1904 for (tem
= 0; tem
< nargs
; tem
++)
1906 /* Ensure that array expressions are coerced into pointer
1908 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1910 if (noside
== EVAL_SKIP
)
1911 return eval_skip_value (exp
);
1912 return value_array (tem2
, tem3
, argvec
);
1916 struct value
*array
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1917 struct value
*low
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1918 struct value
*upper
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1919 return eval_op_ternop (expect_type
, exp
, noside
, array
, low
, upper
);
1923 /* Skip third and second args to evaluate the first one. */
1924 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1925 if (value_logical_not (arg1
))
1927 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
1928 return evaluate_subexp (nullptr, exp
, pos
, noside
);
1932 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
1933 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
1937 case OP_OBJC_SELECTOR
:
1938 { /* Objective C @selector operator. */
1939 char *sel
= &exp
->elts
[pc
+ 2].string
;
1940 int len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1942 (*pos
) += 3 + BYTES_TO_EXP_ELEM (len
+ 1);
1944 sel
[len
] = 0; /* Make sure it's terminated. */
1946 return eval_op_objc_selector (expect_type
, exp
, noside
, sel
);
1949 case OP_OBJC_MSGCALL
:
1950 { /* Objective C message (method) call. */
1952 CORE_ADDR responds_selector
= 0;
1953 CORE_ADDR method_selector
= 0;
1955 CORE_ADDR selector
= 0;
1957 int struct_return
= 0;
1958 enum noside sub_no_side
= EVAL_NORMAL
;
1960 struct value
*msg_send
= NULL
;
1961 struct value
*msg_send_stret
= NULL
;
1962 int gnu_runtime
= 0;
1964 struct value
*target
= NULL
;
1965 struct value
*method
= NULL
;
1966 struct value
*called_method
= NULL
;
1968 struct type
*selector_type
= NULL
;
1969 struct type
*long_type
;
1971 struct value
*ret
= NULL
;
1974 selector
= exp
->elts
[pc
+ 1].longconst
;
1975 nargs
= exp
->elts
[pc
+ 2].longconst
;
1976 argvec
= XALLOCAVEC (struct value
*, nargs
+ 5);
1980 long_type
= builtin_type (exp
->gdbarch
)->builtin_long
;
1981 selector_type
= builtin_type (exp
->gdbarch
)->builtin_data_ptr
;
1983 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1984 sub_no_side
= EVAL_NORMAL
;
1986 sub_no_side
= noside
;
1988 target
= evaluate_subexp (selector_type
, exp
, pos
, sub_no_side
);
1990 if (value_as_long (target
) == 0)
1991 return value_from_longest (long_type
, 0);
1993 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym
)
1996 /* Find the method dispatch (Apple runtime) or method lookup
1997 (GNU runtime) function for Objective-C. These will be used
1998 to lookup the symbol information for the method. If we
1999 can't find any symbol information, then we'll use these to
2000 call the method, otherwise we can call the method
2001 directly. The msg_send_stret function is used in the special
2002 case of a method that returns a structure (Apple runtime
2006 type
= selector_type
;
2008 type
= lookup_function_type (type
);
2009 type
= lookup_pointer_type (type
);
2010 type
= lookup_function_type (type
);
2011 type
= lookup_pointer_type (type
);
2013 msg_send
= find_function_in_inferior ("objc_msg_lookup", NULL
);
2015 = find_function_in_inferior ("objc_msg_lookup", NULL
);
2017 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
2018 msg_send_stret
= value_from_pointer (type
,
2019 value_as_address (msg_send_stret
));
2023 msg_send
= find_function_in_inferior ("objc_msgSend", NULL
);
2024 /* Special dispatcher for methods returning structs. */
2026 = find_function_in_inferior ("objc_msgSend_stret", NULL
);
2029 /* Verify the target object responds to this method. The
2030 standard top-level 'Object' class uses a different name for
2031 the verification method than the non-standard, but more
2032 often used, 'NSObject' class. Make sure we check for both. */
2035 = lookup_child_selector (exp
->gdbarch
, "respondsToSelector:");
2036 if (responds_selector
== 0)
2038 = lookup_child_selector (exp
->gdbarch
, "respondsTo:");
2040 if (responds_selector
== 0)
2041 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
2044 = lookup_child_selector (exp
->gdbarch
, "methodForSelector:");
2045 if (method_selector
== 0)
2047 = lookup_child_selector (exp
->gdbarch
, "methodFor:");
2049 if (method_selector
== 0)
2050 error (_("no 'methodFor:' or 'methodForSelector:' method"));
2052 /* Call the verification method, to make sure that the target
2053 class implements the desired method. */
2055 argvec
[0] = msg_send
;
2057 argvec
[2] = value_from_longest (long_type
, responds_selector
);
2058 argvec
[3] = value_from_longest (long_type
, selector
);
2061 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2064 /* Function objc_msg_lookup returns a pointer. */
2066 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2068 if (value_as_long (ret
) == 0)
2069 error (_("Target does not respond to this message selector."));
2071 /* Call "methodForSelector:" method, to get the address of a
2072 function method that implements this selector for this
2073 class. If we can find a symbol at that address, then we
2074 know the return type, parameter types etc. (that's a good
2077 argvec
[0] = msg_send
;
2079 argvec
[2] = value_from_longest (long_type
, method_selector
);
2080 argvec
[3] = value_from_longest (long_type
, selector
);
2083 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2087 ret
= call_function_by_hand (argvec
[0], NULL
, {argvec
+ 1, 3});
2090 /* ret should now be the selector. */
2092 addr
= value_as_long (ret
);
2095 struct symbol
*sym
= NULL
;
2097 /* The address might point to a function descriptor;
2098 resolve it to the actual code address instead. */
2099 addr
= gdbarch_convert_from_func_ptr_addr (exp
->gdbarch
, addr
,
2100 current_top_target ());
2102 /* Is it a high_level symbol? */
2103 sym
= find_pc_function (addr
);
2105 method
= value_of_variable (sym
, 0);
2108 /* If we found a method with symbol information, check to see
2109 if it returns a struct. Otherwise assume it doesn't. */
2114 struct type
*val_type
;
2116 funaddr
= find_function_addr (method
, &val_type
);
2118 block_for_pc (funaddr
);
2120 val_type
= check_typedef (val_type
);
2122 if ((val_type
== NULL
)
2123 || (val_type
->code () == TYPE_CODE_ERROR
))
2125 if (expect_type
!= NULL
)
2126 val_type
= expect_type
;
2129 struct_return
= using_struct_return (exp
->gdbarch
, method
,
2132 else if (expect_type
!= NULL
)
2134 struct_return
= using_struct_return (exp
->gdbarch
, NULL
,
2135 check_typedef (expect_type
));
2138 /* Found a function symbol. Now we will substitute its
2139 value in place of the message dispatcher (obj_msgSend),
2140 so that we call the method directly instead of thru
2141 the dispatcher. The main reason for doing this is that
2142 we can now evaluate the return value and parameter values
2143 according to their known data types, in case we need to
2144 do things like promotion, dereferencing, special handling
2145 of structs and doubles, etc.
2147 We want to use the type signature of 'method', but still
2148 jump to objc_msgSend() or objc_msgSend_stret() to better
2149 mimic the behavior of the runtime. */
2153 if (value_type (method
)->code () != TYPE_CODE_FUNC
)
2154 error (_("method address has symbol information "
2155 "with non-function type; skipping"));
2157 /* Create a function pointer of the appropriate type, and
2158 replace its value with the value of msg_send or
2159 msg_send_stret. We must use a pointer here, as
2160 msg_send and msg_send_stret are of pointer type, and
2161 the representation may be different on systems that use
2162 function descriptors. */
2165 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2166 value_as_address (msg_send_stret
));
2169 = value_from_pointer (lookup_pointer_type (value_type (method
)),
2170 value_as_address (msg_send
));
2175 called_method
= msg_send_stret
;
2177 called_method
= msg_send
;
2180 if (noside
== EVAL_SKIP
)
2181 return eval_skip_value (exp
);
2183 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2185 /* If the return type doesn't look like a function type,
2186 call an error. This can happen if somebody tries to
2187 turn a variable into a function call. This is here
2188 because people often want to call, eg, strcmp, which
2189 gdb doesn't know is a function. If gdb isn't asked for
2190 it's opinion (ie. through "whatis"), it won't offer
2193 struct type
*callee_type
= value_type (called_method
);
2195 if (callee_type
&& callee_type
->code () == TYPE_CODE_PTR
)
2196 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2197 callee_type
= TYPE_TARGET_TYPE (callee_type
);
2201 if ((callee_type
->code () == TYPE_CODE_ERROR
) && expect_type
)
2202 return allocate_value (expect_type
);
2204 return allocate_value (callee_type
);
2207 error (_("Expression of type other than "
2208 "\"method returning ...\" used as a method"));
2211 /* Now depending on whether we found a symbol for the method,
2212 we will either call the runtime dispatcher or the method
2215 argvec
[0] = called_method
;
2217 argvec
[2] = value_from_longest (long_type
, selector
);
2218 /* User-supplied arguments. */
2219 for (tem
= 0; tem
< nargs
; tem
++)
2220 argvec
[tem
+ 3] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
2221 argvec
[tem
+ 3] = 0;
2223 auto call_args
= gdb::make_array_view (argvec
+ 1, nargs
+ 2);
2225 if (gnu_runtime
&& (method
!= NULL
))
2227 /* Function objc_msg_lookup returns a pointer. */
2228 deprecated_set_value_type (argvec
[0],
2229 lookup_pointer_type (lookup_function_type (value_type (argvec
[0]))));
2230 argvec
[0] = call_function_by_hand (argvec
[0], NULL
, call_args
);
2233 return call_function_by_hand (argvec
[0], NULL
, call_args
);
2238 return evaluate_funcall (expect_type
, exp
, pos
, noside
);
2241 /* We have a complex number, There should be 2 floating
2242 point numbers that compose it. */
2244 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2245 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2247 return value_literal_complex (arg1
, arg2
, exp
->elts
[pc
+ 1].type
);
2249 case STRUCTOP_STRUCT
:
2250 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
2251 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
2252 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2253 return eval_op_structop_struct (expect_type
, exp
, noside
, arg1
,
2254 &exp
->elts
[pc
+ 2].string
);
2257 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
2258 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
2259 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2260 return eval_op_structop_ptr (expect_type
, exp
, noside
, op
, arg1
,
2261 &exp
->elts
[pc
+ 2].string
);
2263 case STRUCTOP_MEMBER
:
2265 if (op
== STRUCTOP_MEMBER
)
2266 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
2268 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2270 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2272 return eval_op_member (expect_type
, exp
, noside
, arg1
, arg2
);
2276 type_instance_flags flags
2277 = (type_instance_flag_value
) longest_to_int (exp
->elts
[pc
+ 1].longconst
);
2278 nargs
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
2279 arg_types
= (struct type
**) alloca (nargs
* sizeof (struct type
*));
2280 for (ix
= 0; ix
< nargs
; ++ix
)
2281 arg_types
[ix
] = exp
->elts
[pc
+ 2 + ix
+ 1].type
;
2283 fake_method
fake_expect_type (flags
, nargs
, arg_types
);
2284 *(pos
) += 4 + nargs
;
2285 return evaluate_subexp_standard (fake_expect_type
.type (), exp
, pos
,
2290 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2291 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2292 return eval_op_concat (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2295 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2296 /* Special-case assignments where the left-hand-side is a
2297 convenience variable -- in these, don't bother setting an
2298 expected type. This avoids a weird case where re-assigning a
2299 string or array to an internal variable could error with "Too
2300 many array elements". */
2301 arg2
= evaluate_subexp (VALUE_LVAL (arg1
) == lval_internalvar
2303 : value_type (arg1
),
2306 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2308 if (binop_user_defined_p (op
, arg1
, arg2
))
2309 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2311 return value_assign (arg1
, arg2
);
2313 case BINOP_ASSIGN_MODIFY
:
2315 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2316 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2317 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2319 op
= exp
->elts
[pc
+ 1].opcode
;
2320 if (binop_user_defined_p (op
, arg1
, arg2
))
2321 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
2322 else if (op
== BINOP_ADD
&& ptrmath_type_p (exp
->language_defn
,
2324 && is_integral_type (value_type (arg2
)))
2325 arg2
= value_ptradd (arg1
, value_as_long (arg2
));
2326 else if (op
== BINOP_SUB
&& ptrmath_type_p (exp
->language_defn
,
2328 && is_integral_type (value_type (arg2
)))
2329 arg2
= value_ptradd (arg1
, - value_as_long (arg2
));
2332 struct value
*tmp
= arg1
;
2334 /* For shift and integer exponentiation operations,
2335 only promote the first argument. */
2336 if ((op
== BINOP_LSH
|| op
== BINOP_RSH
|| op
== BINOP_EXP
)
2337 && is_integral_type (value_type (arg2
)))
2338 unop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
);
2340 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2342 arg2
= value_binop (tmp
, arg2
, op
);
2344 return value_assign (arg1
, arg2
);
2347 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2348 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2349 return eval_op_add (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2352 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2353 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2354 return eval_op_sub (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2364 case BINOP_BITWISE_AND
:
2365 case BINOP_BITWISE_IOR
:
2366 case BINOP_BITWISE_XOR
:
2367 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2368 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2369 return eval_op_binary (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2371 case BINOP_SUBSCRIPT
:
2372 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2373 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2374 return eval_op_subscript (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2376 case MULTI_SUBSCRIPT
:
2378 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
2379 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
2380 argvec
= XALLOCAVEC (struct value
*, nargs
);
2381 for (ix
= 0; ix
< nargs
; ++ix
)
2382 argvec
[ix
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
2383 if (noside
== EVAL_SKIP
)
2385 for (ix
= 0; ix
< nargs
; ++ix
)
2389 if (binop_user_defined_p (op
, arg1
, arg2
))
2391 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2395 arg1
= coerce_ref (arg1
);
2396 type
= check_typedef (value_type (arg1
));
2398 switch (type
->code ())
2401 case TYPE_CODE_ARRAY
:
2402 case TYPE_CODE_STRING
:
2403 arg1
= value_subscript (arg1
, value_as_long (arg2
));
2408 error (_("cannot subscript something of type `%s'"),
2411 error (_("cannot subscript requested type"));
2417 case BINOP_LOGICAL_AND
:
2418 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2419 if (noside
== EVAL_SKIP
)
2421 evaluate_subexp (nullptr, exp
, pos
, noside
);
2422 return eval_skip_value (exp
);
2426 arg2
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2429 if (binop_user_defined_p (op
, arg1
, arg2
))
2431 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2432 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2436 tem
= value_logical_not (arg1
);
2438 = evaluate_subexp (nullptr, exp
, pos
, (tem
? EVAL_SKIP
: noside
));
2439 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2440 return value_from_longest (type
,
2441 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
2444 case BINOP_LOGICAL_OR
:
2445 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2446 if (noside
== EVAL_SKIP
)
2448 evaluate_subexp (nullptr, exp
, pos
, noside
);
2449 return eval_skip_value (exp
);
2453 arg2
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2456 if (binop_user_defined_p (op
, arg1
, arg2
))
2458 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2459 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2463 tem
= value_logical_not (arg1
);
2465 = evaluate_subexp (nullptr, exp
, pos
, (!tem
? EVAL_SKIP
: noside
));
2466 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2467 return value_from_longest (type
,
2468 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
2472 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2473 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2474 return eval_op_equal (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2476 case BINOP_NOTEQUAL
:
2477 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2478 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2479 return eval_op_notequal (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2482 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2483 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2484 return eval_op_less (expect_type
, exp
, noside
, op
, arg1
, arg2
);
2487 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2488 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2489 if (noside
== EVAL_SKIP
)
2490 return eval_skip_value (exp
);
2491 if (binop_user_defined_p (op
, arg1
, arg2
))
2493 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2497 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2498 tem
= value_less (arg2
, arg1
);
2499 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2500 return value_from_longest (type
, (LONGEST
) tem
);
2504 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2505 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2506 if (noside
== EVAL_SKIP
)
2507 return eval_skip_value (exp
);
2508 if (binop_user_defined_p (op
, arg1
, arg2
))
2510 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2514 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2515 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
2516 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2517 return value_from_longest (type
, (LONGEST
) tem
);
2521 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2522 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
2523 if (noside
== EVAL_SKIP
)
2524 return eval_skip_value (exp
);
2525 if (binop_user_defined_p (op
, arg1
, arg2
))
2527 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
2531 binop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
, &arg2
);
2532 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
2533 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2534 return value_from_longest (type
, (LONGEST
) tem
);
2538 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2539 arg2
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2540 if (noside
== EVAL_SKIP
)
2541 return eval_skip_value (exp
);
2542 type
= check_typedef (value_type (arg2
));
2543 if (type
->code () != TYPE_CODE_INT
2544 && type
->code () != TYPE_CODE_ENUM
)
2545 error (_("Non-integral right operand for \"@\" operator."));
2546 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2548 return allocate_repeat_value (value_type (arg1
),
2549 longest_to_int (value_as_long (arg2
)));
2552 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
2555 evaluate_subexp (nullptr, exp
, pos
, noside
);
2556 return evaluate_subexp (nullptr, exp
, pos
, noside
);
2559 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2560 if (noside
== EVAL_SKIP
)
2561 return eval_skip_value (exp
);
2562 if (unop_user_defined_p (op
, arg1
))
2563 return value_x_unop (arg1
, op
, noside
);
2566 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2567 return value_pos (arg1
);
2571 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2572 if (noside
== EVAL_SKIP
)
2573 return eval_skip_value (exp
);
2574 if (unop_user_defined_p (op
, arg1
))
2575 return value_x_unop (arg1
, op
, noside
);
2578 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2579 return value_neg (arg1
);
2582 case UNOP_COMPLEMENT
:
2583 /* C++: check for and handle destructor names. */
2585 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2586 if (noside
== EVAL_SKIP
)
2587 return eval_skip_value (exp
);
2588 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
2589 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
2592 unop_promote (exp
->language_defn
, exp
->gdbarch
, &arg1
);
2593 return value_complement (arg1
);
2596 case UNOP_LOGICAL_NOT
:
2597 arg1
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2598 if (noside
== EVAL_SKIP
)
2599 return eval_skip_value (exp
);
2600 if (unop_user_defined_p (op
, arg1
))
2601 return value_x_unop (arg1
, op
, noside
);
2604 type
= language_bool_type (exp
->language_defn
, exp
->gdbarch
);
2605 return value_from_longest (type
, (LONGEST
) value_logical_not (arg1
));
2609 if (expect_type
&& expect_type
->code () == TYPE_CODE_PTR
)
2610 expect_type
= TYPE_TARGET_TYPE (check_typedef (expect_type
));
2611 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2612 type
= check_typedef (value_type (arg1
));
2613 if (type
->code () == TYPE_CODE_METHODPTR
2614 || type
->code () == TYPE_CODE_MEMBERPTR
)
2615 error (_("Attempt to dereference pointer "
2616 "to member without an object"));
2617 if (noside
== EVAL_SKIP
)
2618 return eval_skip_value (exp
);
2619 if (unop_user_defined_p (op
, arg1
))
2620 return value_x_unop (arg1
, op
, noside
);
2621 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2623 type
= check_typedef (value_type (arg1
));
2625 /* If the type pointed to is dynamic then in order to resolve the
2626 dynamic properties we must actually dereference the pointer.
2627 There is a risk that this dereference will have side-effects
2628 in the inferior, but being able to print accurate type
2629 information seems worth the risk. */
2630 if ((type
->code () != TYPE_CODE_PTR
2631 && !TYPE_IS_REFERENCE (type
))
2632 || !is_dynamic_type (TYPE_TARGET_TYPE (type
)))
2634 if (type
->code () == TYPE_CODE_PTR
2635 || TYPE_IS_REFERENCE (type
)
2636 /* In C you can dereference an array to get the 1st elt. */
2637 || type
->code () == TYPE_CODE_ARRAY
)
2638 return value_zero (TYPE_TARGET_TYPE (type
),
2640 else if (type
->code () == TYPE_CODE_INT
)
2641 /* GDB allows dereferencing an int. */
2642 return value_zero (builtin_type (exp
->gdbarch
)->builtin_int
,
2645 error (_("Attempt to take contents of a non-pointer value."));
2649 /* Allow * on an integer so we can cast it to whatever we want.
2650 This returns an int, which seems like the most C-like thing to
2651 do. "long long" variables are rare enough that
2652 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2653 if (type
->code () == TYPE_CODE_INT
)
2654 return value_at_lazy (builtin_type (exp
->gdbarch
)->builtin_int
,
2655 (CORE_ADDR
) value_as_address (arg1
));
2656 return value_ind (arg1
);
2659 /* C++: check for and handle pointer to members. */
2661 if (noside
== EVAL_SKIP
)
2663 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2664 return eval_skip_value (exp
);
2667 return evaluate_subexp_for_address (exp
, pos
, noside
);
2670 if (noside
== EVAL_SKIP
)
2672 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2673 return eval_skip_value (exp
);
2675 return evaluate_subexp_for_sizeof (exp
, pos
, noside
);
2680 evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
));
2681 /* FIXME: This should be size_t. */
2682 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
2683 ULONGEST align
= type_align (type
);
2685 error (_("could not determine alignment of type"));
2686 return value_from_longest (size_type
, align
);
2691 type
= exp
->elts
[pc
+ 1].type
;
2692 return evaluate_subexp_for_cast (exp
, pos
, noside
, type
);
2694 case UNOP_CAST_TYPE
:
2695 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2696 type
= value_type (arg1
);
2697 return evaluate_subexp_for_cast (exp
, pos
, noside
, type
);
2699 case UNOP_DYNAMIC_CAST
:
2700 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2701 type
= value_type (arg1
);
2702 arg1
= evaluate_subexp (type
, exp
, pos
, noside
);
2703 if (noside
== EVAL_SKIP
)
2704 return eval_skip_value (exp
);
2705 return value_dynamic_cast (type
, arg1
);
2707 case UNOP_REINTERPRET_CAST
:
2708 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2709 type
= value_type (arg1
);
2710 arg1
= evaluate_subexp (type
, exp
, pos
, noside
);
2711 if (noside
== EVAL_SKIP
)
2712 return eval_skip_value (exp
);
2713 return value_reinterpret_cast (type
, arg1
);
2717 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2718 if (noside
== EVAL_SKIP
)
2719 return eval_skip_value (exp
);
2720 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2721 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
2723 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
2724 value_as_address (arg1
));
2726 case UNOP_MEMVAL_TYPE
:
2727 arg1
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2728 type
= value_type (arg1
);
2729 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2730 if (noside
== EVAL_SKIP
)
2731 return eval_skip_value (exp
);
2732 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2733 return value_zero (type
, lval_memory
);
2735 return value_at_lazy (type
, value_as_address (arg1
));
2737 case UNOP_PREINCREMENT
:
2738 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2739 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2741 else if (unop_user_defined_p (op
, arg1
))
2743 return value_x_unop (arg1
, op
, noside
);
2747 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2748 arg2
= value_ptradd (arg1
, 1);
2751 struct value
*tmp
= arg1
;
2753 arg2
= value_one (value_type (arg1
));
2754 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2755 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
2758 return value_assign (arg1
, arg2
);
2761 case UNOP_PREDECREMENT
:
2762 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2763 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2765 else if (unop_user_defined_p (op
, arg1
))
2767 return value_x_unop (arg1
, op
, noside
);
2771 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2772 arg2
= value_ptradd (arg1
, -1);
2775 struct value
*tmp
= arg1
;
2777 arg2
= value_one (value_type (arg1
));
2778 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2779 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
2782 return value_assign (arg1
, arg2
);
2785 case UNOP_POSTINCREMENT
:
2786 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2787 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2789 else if (unop_user_defined_p (op
, arg1
))
2791 return value_x_unop (arg1
, op
, noside
);
2795 arg3
= value_non_lval (arg1
);
2797 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2798 arg2
= value_ptradd (arg1
, 1);
2801 struct value
*tmp
= arg1
;
2803 arg2
= value_one (value_type (arg1
));
2804 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2805 arg2
= value_binop (tmp
, arg2
, BINOP_ADD
);
2808 value_assign (arg1
, arg2
);
2812 case UNOP_POSTDECREMENT
:
2813 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2814 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2816 else if (unop_user_defined_p (op
, arg1
))
2818 return value_x_unop (arg1
, op
, noside
);
2822 arg3
= value_non_lval (arg1
);
2824 if (ptrmath_type_p (exp
->language_defn
, value_type (arg1
)))
2825 arg2
= value_ptradd (arg1
, -1);
2828 struct value
*tmp
= arg1
;
2830 arg2
= value_one (value_type (arg1
));
2831 binop_promote (exp
->language_defn
, exp
->gdbarch
, &tmp
, &arg2
);
2832 arg2
= value_binop (tmp
, arg2
, BINOP_SUB
);
2835 value_assign (arg1
, arg2
);
2841 return value_of_this (exp
->language_defn
);
2844 /* The value is not supposed to be used. This is here to make it
2845 easier to accommodate expressions that contain types. */
2847 if (noside
== EVAL_SKIP
)
2848 return eval_skip_value (exp
);
2849 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2850 return allocate_value (exp
->elts
[pc
+ 1].type
);
2852 error (_("Attempt to use a type name as an expression"));
2856 if (noside
== EVAL_SKIP
)
2858 evaluate_subexp (nullptr, exp
, pos
, EVAL_SKIP
);
2859 return eval_skip_value (exp
);
2861 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2863 enum exp_opcode sub_op
= exp
->elts
[*pos
].opcode
;
2864 struct value
*result
;
2866 result
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2868 /* 'decltype' has special semantics for lvalues. */
2869 if (op
== OP_DECLTYPE
2870 && (sub_op
== BINOP_SUBSCRIPT
2871 || sub_op
== STRUCTOP_MEMBER
2872 || sub_op
== STRUCTOP_MPTR
2873 || sub_op
== UNOP_IND
2874 || sub_op
== STRUCTOP_STRUCT
2875 || sub_op
== STRUCTOP_PTR
2876 || sub_op
== OP_SCOPE
))
2878 type
= value_type (result
);
2880 if (!TYPE_IS_REFERENCE (type
))
2882 type
= lookup_lvalue_reference_type (type
);
2883 result
= allocate_value (type
);
2890 error (_("Attempt to use a type as an expression"));
2894 struct value
*result
;
2895 enum exp_opcode sub_op
= exp
->elts
[*pos
].opcode
;
2897 if (sub_op
== OP_TYPE
|| sub_op
== OP_DECLTYPE
|| sub_op
== OP_TYPEOF
)
2898 result
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2900 result
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2902 if (noside
!= EVAL_NORMAL
)
2903 return allocate_value (cplus_typeid_type (exp
->gdbarch
));
2905 return cplus_typeid (result
);
2909 /* Removing this case and compiling with gcc -Wall reveals that
2910 a lot of cases are hitting this case. Some of these should
2911 probably be removed from expression.h; others are legitimate
2912 expressions which are (apparently) not fully implemented.
2914 If there are any cases landing here which mean a user error,
2915 then they should be separate cases, with more descriptive
2918 error (_("GDB does not (yet) know how to "
2919 "evaluate that kind of expression"));
2922 gdb_assert_not_reached ("missed return?");
2925 /* Evaluate a subexpression of EXP, at index *POS,
2926 and return the address of that subexpression.
2927 Advance *POS over the subexpression.
2928 If the subexpression isn't an lvalue, get an error.
2929 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2930 then only the type of the result need be correct. */
2932 static struct value
*
2933 evaluate_subexp_for_address (struct expression
*exp
, int *pos
,
2943 op
= exp
->elts
[pc
].opcode
;
2949 x
= evaluate_subexp (nullptr, exp
, pos
, noside
);
2951 /* We can't optimize out "&*" if there's a user-defined operator*. */
2952 if (unop_user_defined_p (op
, x
))
2954 x
= value_x_unop (x
, op
, noside
);
2955 goto default_case_after_eval
;
2958 return coerce_array (x
);
2962 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
2963 evaluate_subexp (nullptr, exp
, pos
, noside
));
2965 case UNOP_MEMVAL_TYPE
:
2970 x
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2971 type
= value_type (x
);
2972 return value_cast (lookup_pointer_type (type
),
2973 evaluate_subexp (nullptr, exp
, pos
, noside
));
2977 var
= exp
->elts
[pc
+ 2].symbol
;
2979 /* C++: The "address" of a reference should yield the address
2980 * of the object pointed to. Let value_addr() deal with it. */
2981 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var
)))
2985 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2988 lookup_pointer_type (SYMBOL_TYPE (var
));
2989 enum address_class sym_class
= SYMBOL_CLASS (var
);
2991 if (sym_class
== LOC_CONST
2992 || sym_class
== LOC_CONST_BYTES
2993 || sym_class
== LOC_REGISTER
)
2994 error (_("Attempt to take address of register or constant."));
2997 value_zero (type
, not_lval
);
3000 return address_of_variable (var
, exp
->elts
[pc
+ 1].block
);
3002 case OP_VAR_MSYM_VALUE
:
3006 value
*val
= evaluate_var_msym_value (noside
,
3007 exp
->elts
[pc
+ 1].objfile
,
3008 exp
->elts
[pc
+ 2].msymbol
);
3009 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
3011 struct type
*type
= lookup_pointer_type (value_type (val
));
3012 return value_zero (type
, not_lval
);
3015 return value_addr (val
);
3019 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
3020 (*pos
) += 5 + BYTES_TO_EXP_ELEM (tem
+ 1);
3021 x
= value_aggregate_elt (exp
->elts
[pc
+ 1].type
,
3022 &exp
->elts
[pc
+ 3].string
,
3025 error (_("There is no field named %s"), &exp
->elts
[pc
+ 3].string
);
3030 x
= evaluate_subexp (nullptr, exp
, pos
, noside
);
3031 default_case_after_eval
:
3032 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
3034 struct type
*type
= check_typedef (value_type (x
));
3036 if (TYPE_IS_REFERENCE (type
))
3037 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
3039 else if (VALUE_LVAL (x
) == lval_memory
|| value_must_coerce_to_target (x
))
3040 return value_zero (lookup_pointer_type (value_type (x
)),
3043 error (_("Attempt to take address of "
3044 "value not located in memory."));
3046 return value_addr (x
);
3050 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
3051 When used in contexts where arrays will be coerced anyway, this is
3052 equivalent to `evaluate_subexp' but much faster because it avoids
3053 actually fetching array contents (perhaps obsolete now that we have
3056 Note that we currently only do the coercion for C expressions, where
3057 arrays are zero based and the coercion is correct. For other languages,
3058 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
3059 to decide if coercion is appropriate. */
3062 evaluate_subexp_with_coercion (struct expression
*exp
,
3063 int *pos
, enum noside noside
)
3072 op
= exp
->elts
[pc
].opcode
;
3077 var
= exp
->elts
[pc
+ 2].symbol
;
3078 type
= check_typedef (SYMBOL_TYPE (var
));
3079 if (type
->code () == TYPE_CODE_ARRAY
3080 && !type
->is_vector ()
3081 && CAST_IS_CONVERSION (exp
->language_defn
))
3084 val
= address_of_variable (var
, exp
->elts
[pc
+ 1].block
);
3085 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
3091 return evaluate_subexp (nullptr, exp
, pos
, noside
);
3095 /* Evaluate a subexpression of EXP, at index *POS,
3096 and return a value for the size of that subexpression.
3097 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
3098 we allow side-effects on the operand if its type is a variable
3101 static struct value
*
3102 evaluate_subexp_for_sizeof (struct expression
*exp
, int *pos
,
3105 /* FIXME: This should be size_t. */
3106 struct type
*size_type
= builtin_type (exp
->gdbarch
)->builtin_int
;
3113 op
= exp
->elts
[pc
].opcode
;
3117 /* This case is handled specially
3118 so that we avoid creating a value for the result type.
3119 If the result type is very big, it's desirable not to
3120 create a value unnecessarily. */
3123 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
3124 type
= check_typedef (value_type (val
));
3125 if (type
->code () != TYPE_CODE_PTR
3126 && !TYPE_IS_REFERENCE (type
)
3127 && type
->code () != TYPE_CODE_ARRAY
)
3128 error (_("Attempt to take contents of a non-pointer value."));
3129 type
= TYPE_TARGET_TYPE (type
);
3130 if (is_dynamic_type (type
))
3131 type
= value_type (value_ind (val
));
3132 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
3136 type
= exp
->elts
[pc
+ 1].type
;
3139 case UNOP_MEMVAL_TYPE
:
3141 val
= evaluate_subexp (NULL
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
3142 type
= value_type (val
);
3146 type
= SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
);
3147 if (is_dynamic_type (type
))
3149 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_NORMAL
);
3150 type
= value_type (val
);
3151 if (type
->code () == TYPE_CODE_ARRAY
)
3153 if (type_not_allocated (type
) || type_not_associated (type
))
3154 return value_zero (size_type
, not_lval
);
3155 else if (is_dynamic_type (type
->index_type ())
3156 && type
->bounds ()->high
.kind () == PROP_UNDEFINED
)
3157 return allocate_optimized_out_value (size_type
);
3164 case OP_VAR_MSYM_VALUE
:
3168 minimal_symbol
*msymbol
= exp
->elts
[pc
+ 2].msymbol
;
3169 value
*mval
= evaluate_var_msym_value (noside
,
3170 exp
->elts
[pc
+ 1].objfile
,
3173 type
= value_type (mval
);
3174 if (type
->code () == TYPE_CODE_ERROR
)
3175 error_unknown_type (msymbol
->print_name ());
3177 return value_from_longest (size_type
, TYPE_LENGTH (type
));
3181 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3182 type of the subscript is a variable length array type. In this case we
3183 must re-evaluate the right hand side of the subscription to allow
3185 case BINOP_SUBSCRIPT
:
3186 if (noside
== EVAL_NORMAL
)
3188 int npc
= (*pos
) + 1;
3190 val
= evaluate_subexp (nullptr, exp
, &npc
, EVAL_AVOID_SIDE_EFFECTS
);
3191 type
= check_typedef (value_type (val
));
3192 if (type
->code () == TYPE_CODE_ARRAY
)
3194 type
= check_typedef (TYPE_TARGET_TYPE (type
));
3195 if (type
->code () == TYPE_CODE_ARRAY
)
3197 type
= type
->index_type ();
3198 /* Only re-evaluate the right hand side if the resulting type
3199 is a variable length type. */
3200 if (type
->bounds ()->flag_bound_evaluated
)
3202 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_NORMAL
);
3203 return value_from_longest
3204 (size_type
, (LONGEST
) TYPE_LENGTH (value_type (val
)));
3213 val
= evaluate_subexp (nullptr, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
3214 type
= value_type (val
);
3218 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3219 "When applied to a reference or a reference type, the result is
3220 the size of the referenced type." */
3221 type
= check_typedef (type
);
3222 if (exp
->language_defn
->la_language
== language_cplus
3223 && (TYPE_IS_REFERENCE (type
)))
3224 type
= check_typedef (TYPE_TARGET_TYPE (type
));
3225 return value_from_longest (size_type
, (LONGEST
) TYPE_LENGTH (type
));
3228 /* Evaluate a subexpression of EXP, at index *POS, and return a value
3229 for that subexpression cast to TO_TYPE. Advance *POS over the
3233 evaluate_subexp_for_cast (expression
*exp
, int *pos
,
3235 struct type
*to_type
)
3239 /* Don't let symbols be evaluated with evaluate_subexp because that
3240 throws an "unknown type" error for no-debug data symbols.
3241 Instead, we want the cast to reinterpret the symbol. */
3242 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
3243 || exp
->elts
[pc
].opcode
== OP_VAR_VALUE
)
3248 if (exp
->elts
[pc
].opcode
== OP_VAR_MSYM_VALUE
)
3250 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
3251 return value_zero (to_type
, not_lval
);
3253 val
= evaluate_var_msym_value (noside
,
3254 exp
->elts
[pc
+ 1].objfile
,
3255 exp
->elts
[pc
+ 2].msymbol
);
3258 val
= evaluate_var_value (noside
,
3259 exp
->elts
[pc
+ 1].block
,
3260 exp
->elts
[pc
+ 2].symbol
);
3262 if (noside
== EVAL_SKIP
)
3263 return eval_skip_value (exp
);
3265 val
= value_cast (to_type
, val
);
3267 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
3268 if (VALUE_LVAL (val
) == lval_memory
)
3270 if (value_lazy (val
))
3271 value_fetch_lazy (val
);
3272 VALUE_LVAL (val
) = not_lval
;
3277 value
*val
= evaluate_subexp (to_type
, exp
, pos
, noside
);
3278 if (noside
== EVAL_SKIP
)
3279 return eval_skip_value (exp
);
3280 return value_cast (to_type
, val
);
3283 /* Parse a type expression in the string [P..P+LENGTH). */
3286 parse_and_eval_type (const char *p
, int length
)
3288 char *tmp
= (char *) alloca (length
+ 4);
3291 memcpy (tmp
+ 1, p
, length
);
3292 tmp
[length
+ 1] = ')';
3293 tmp
[length
+ 2] = '0';
3294 tmp
[length
+ 3] = '\0';
3295 expression_up expr
= parse_expression (tmp
);
3296 if (expr
->first_opcode () != UNOP_CAST
)
3297 error (_("Internal error in eval_type."));
3298 return expr
->elts
[1].type
;