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 *,
61 evaluate_subexp (struct type *expect_type, struct expression *exp,
62 int *pos, enum noside noside)
66 gdb::optional<enable_thread_stack_temporaries> stack_temporaries;
67 if (*pos == 0 && target_has_execution ()
68 && exp->language_defn->la_language == language_cplus
69 && !thread_stack_temporaries_enabled_p (inferior_thread ()))
70 stack_temporaries.emplace (inferior_thread ());
72 retval = (*exp->language_defn->expression_ops ()->evaluate_exp)
73 (expect_type, exp, pos, noside);
75 if (stack_temporaries.has_value ()
76 && value_in_thread_stack_temporaries (retval, inferior_thread ()))
77 retval = value_non_lval (retval);
82 /* Parse the string EXP as a C expression, evaluate it,
83 and return the result as a number. */
86 parse_and_eval_address (const char *exp)
88 expression_up expr = parse_expression (exp);
90 return value_as_address (evaluate_expression (expr.get ()));
93 /* Like parse_and_eval_address, but treats the value of the expression
94 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
96 parse_and_eval_long (const char *exp)
98 expression_up expr = parse_expression (exp);
100 return value_as_long (evaluate_expression (expr.get ()));
104 parse_and_eval (const char *exp)
106 expression_up expr = parse_expression (exp);
108 return evaluate_expression (expr.get ());
111 /* Parse up to a comma (or to a closeparen)
112 in the string EXPP as an expression, evaluate it, and return the value.
113 EXPP is advanced to point to the comma. */
116 parse_to_comma_and_eval (const char **expp)
118 expression_up expr = parse_exp_1 (expp, 0, nullptr, 1);
120 return evaluate_expression (expr.get ());
123 /* Evaluate an expression in internal prefix form
124 such as is constructed by parse.y.
126 See expression.h for info on the format of an expression. */
129 evaluate_expression (struct expression *exp)
133 return evaluate_subexp (nullptr, exp, &pc, EVAL_NORMAL);
136 /* Evaluate an expression, avoiding all memory references
137 and getting a value whose type alone is correct. */
140 evaluate_type (struct expression *exp)
144 return evaluate_subexp (nullptr, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
147 /* Evaluate a subexpression, avoiding all memory references and
148 getting a value whose type alone is correct. */
151 evaluate_subexpression_type (struct expression *exp, int subexp)
153 return evaluate_subexp (nullptr, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS);
156 /* Find the current value of a watchpoint on EXP. Return the value in
157 *VALP and *RESULTP and the chain of intermediate and final values
158 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
161 If PRESERVE_ERRORS is true, then exceptions are passed through.
162 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
163 occurs while evaluating the expression, *RESULTP will be set to
164 NULL. *RESULTP may be a lazy value, if the result could not be
165 read from memory. It is used to determine whether a value is
166 user-specified (we should watch the whole value) or intermediate
167 (we should watch only the bit used to locate the final value).
169 If the final value, or any intermediate value, could not be read
170 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
171 set to any referenced values. *VALP will never be a lazy value.
172 This is the value which we store in struct breakpoint.
174 If VAL_CHAIN is non-NULL, the values put into *VAL_CHAIN will be
175 released from the value chain. If VAL_CHAIN is NULL, all generated
176 values will be left on the value chain. */
179 fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
180 struct value **resultp,
181 std::vector<value_ref_ptr> *val_chain,
182 bool preserve_errors)
184 struct value *mark, *new_mark, *result;
192 /* Evaluate the expression. */
193 mark = value_mark ();
198 result = evaluate_subexp (nullptr, exp, pc, EVAL_NORMAL);
200 catch (const gdb_exception &ex)
202 /* Ignore memory errors if we want watchpoints pointing at
203 inaccessible memory to still be created; otherwise, throw the
204 error to some higher catcher. */
208 if (!preserve_errors)
217 new_mark = value_mark ();
218 if (mark == new_mark)
223 /* Make sure it's not lazy, so that after the target stops again we
224 have a non-lazy previous value to compare with. */
227 if (!value_lazy (result))
234 value_fetch_lazy (result);
237 catch (const gdb_exception_error &except)
245 /* Return the chain of intermediate values. We use this to
246 decide which addresses to watch. */
247 *val_chain = value_release_to_mark (mark);
251 /* Extract a field operation from an expression. If the subexpression
252 of EXP starting at *SUBEXP is not a structure dereference
253 operation, return NULL. Otherwise, return the name of the
254 dereferenced field, and advance *SUBEXP to point to the
255 subexpression of the left-hand-side of the dereference. This is
256 used when completing field names. */
259 extract_field_op (struct expression *exp, int *subexp)
264 if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT
265 && exp->elts[*subexp].opcode != STRUCTOP_PTR)
267 tem = longest_to_int (exp->elts[*subexp + 1].longconst);
268 result = &exp->elts[*subexp + 2].string;
269 (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1);
273 /* This function evaluates brace-initializers (in C/C++) for
276 static struct value *
277 evaluate_struct_tuple (struct value *struct_val,
278 struct expression *exp,
279 int *pos, enum noside noside, int nargs)
281 struct type *struct_type = check_typedef (value_type (struct_val));
282 struct type *field_type;
287 struct value *val = NULL;
292 /* Skip static fields. */
293 while (fieldno < struct_type->num_fields ()
294 && field_is_static (&struct_type->field (fieldno)))
296 if (fieldno >= struct_type->num_fields ())
297 error (_("too many initializers"));
298 field_type = struct_type->field (fieldno).type ();
299 if (field_type->code () == TYPE_CODE_UNION
300 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
301 error (_("don't know which variant you want to set"));
303 /* Here, struct_type is the type of the inner struct,
304 while substruct_type is the type of the inner struct.
305 These are the same for normal structures, but a variant struct
306 contains anonymous union fields that contain substruct fields.
307 The value fieldno is the index of the top-level (normal or
308 anonymous union) field in struct_field, while the value
309 subfieldno is the index of the actual real (named inner) field
310 in substruct_type. */
312 field_type = struct_type->field (fieldno).type ();
314 val = evaluate_subexp (field_type, exp, pos, noside);
316 /* Now actually set the field in struct_val. */
318 /* Assign val to field fieldno. */
319 if (value_type (val) != field_type)
320 val = value_cast (field_type, val);
322 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
323 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
324 addr = value_contents_writeable (struct_val) + bitpos / 8;
326 modify_field (struct_type, addr,
327 value_as_long (val), bitpos % 8, bitsize);
329 memcpy (addr, value_contents (val),
330 TYPE_LENGTH (value_type (val)));
336 /* Promote value ARG1 as appropriate before performing a unary operation
338 If the result is not appropriate for any particular language then it
339 needs to patch this function. */
342 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
347 *arg1 = coerce_ref (*arg1);
348 type1 = check_typedef (value_type (*arg1));
350 if (is_integral_type (type1))
352 switch (language->la_language)
355 /* Perform integral promotion for ANSI C/C++.
356 If not appropriate for any particular language
357 it needs to modify this function. */
359 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
361 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int))
362 *arg1 = value_cast (builtin_int, *arg1);
369 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
370 operation on those two operands.
371 If the result is not appropriate for any particular language then it
372 needs to patch this function. */
375 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
376 struct value **arg1, struct value **arg2)
378 struct type *promoted_type = NULL;
382 *arg1 = coerce_ref (*arg1);
383 *arg2 = coerce_ref (*arg2);
385 type1 = check_typedef (value_type (*arg1));
386 type2 = check_typedef (value_type (*arg2));
388 if ((type1->code () != TYPE_CODE_FLT
389 && type1->code () != TYPE_CODE_DECFLOAT
390 && !is_integral_type (type1))
391 || (type2->code () != TYPE_CODE_FLT
392 && type2->code () != TYPE_CODE_DECFLOAT
393 && !is_integral_type (type2)))
396 if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
399 if (type1->code () == TYPE_CODE_DECFLOAT
400 || type2->code () == TYPE_CODE_DECFLOAT)
402 /* No promotion required. */
404 else if (type1->code () == TYPE_CODE_FLT
405 || type2->code () == TYPE_CODE_FLT)
407 switch (language->la_language)
413 case language_opencl:
414 /* No promotion required. */
418 /* For other languages the result type is unchanged from gdb
419 version 6.7 for backward compatibility.
420 If either arg was long double, make sure that value is also long
421 double. Otherwise use double. */
422 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch)
423 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch))
424 promoted_type = builtin_type (gdbarch)->builtin_long_double;
426 promoted_type = builtin_type (gdbarch)->builtin_double;
430 else if (type1->code () == TYPE_CODE_BOOL
431 && type2->code () == TYPE_CODE_BOOL)
433 /* No promotion required. */
436 /* Integral operations here. */
437 /* FIXME: Also mixed integral/booleans, with result an integer. */
439 const struct builtin_type *builtin = builtin_type (gdbarch);
440 unsigned int promoted_len1 = TYPE_LENGTH (type1);
441 unsigned int promoted_len2 = TYPE_LENGTH (type2);
442 int is_unsigned1 = type1->is_unsigned ();
443 int is_unsigned2 = type2->is_unsigned ();
444 unsigned int result_len;
445 int unsigned_operation;
447 /* Determine type length and signedness after promotion for
449 if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int))
452 promoted_len1 = TYPE_LENGTH (builtin->builtin_int);
454 if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int))
457 promoted_len2 = TYPE_LENGTH (builtin->builtin_int);
460 if (promoted_len1 > promoted_len2)
462 unsigned_operation = is_unsigned1;
463 result_len = promoted_len1;
465 else if (promoted_len2 > promoted_len1)
467 unsigned_operation = is_unsigned2;
468 result_len = promoted_len2;
472 unsigned_operation = is_unsigned1 || is_unsigned2;
473 result_len = promoted_len1;
476 switch (language->la_language)
482 if (result_len <= TYPE_LENGTH (builtin->builtin_int))
484 promoted_type = (unsigned_operation
485 ? builtin->builtin_unsigned_int
486 : builtin->builtin_int);
488 else if (result_len <= TYPE_LENGTH (builtin->builtin_long))
490 promoted_type = (unsigned_operation
491 ? builtin->builtin_unsigned_long
492 : builtin->builtin_long);
496 promoted_type = (unsigned_operation
497 ? builtin->builtin_unsigned_long_long
498 : builtin->builtin_long_long);
501 case language_opencl:
502 if (result_len <= TYPE_LENGTH (lookup_signed_typename
507 ? lookup_unsigned_typename (language, "int")
508 : lookup_signed_typename (language, "int"));
510 else if (result_len <= TYPE_LENGTH (lookup_signed_typename
515 ? lookup_unsigned_typename (language, "long")
516 : lookup_signed_typename (language,"long"));
520 /* For other languages the result type is unchanged from gdb
521 version 6.7 for backward compatibility.
522 If either arg was long long, make sure that value is also long
523 long. Otherwise use long. */
524 if (unsigned_operation)
526 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
527 promoted_type = builtin->builtin_unsigned_long_long;
529 promoted_type = builtin->builtin_unsigned_long;
533 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
534 promoted_type = builtin->builtin_long_long;
536 promoted_type = builtin->builtin_long;
544 /* Promote both operands to common type. */
545 *arg1 = value_cast (promoted_type, *arg1);
546 *arg2 = value_cast (promoted_type, *arg2);
551 ptrmath_type_p (const struct language_defn *lang, struct type *type)
553 type = check_typedef (type);
554 if (TYPE_IS_REFERENCE (type))
555 type = TYPE_TARGET_TYPE (type);
557 switch (type->code ())
563 case TYPE_CODE_ARRAY:
564 return type->is_vector () ? 0 : lang->c_style_arrays_p ();
571 /* Represents a fake method with the given parameter types. This is
572 used by the parser to construct a temporary "expected" type for
573 method overload resolution. FLAGS is used as instance flags of the
574 new type, in order to be able to make the new type represent a
575 const/volatile overload. */
580 fake_method (type_instance_flags flags,
581 int num_types, struct type **param_types);
584 /* The constructed type. */
585 struct type *type () { return &m_type; }
588 struct type m_type {};
589 main_type m_main_type {};
592 fake_method::fake_method (type_instance_flags flags,
593 int num_types, struct type **param_types)
595 struct type *type = &m_type;
597 TYPE_MAIN_TYPE (type) = &m_main_type;
598 TYPE_LENGTH (type) = 1;
599 type->set_code (TYPE_CODE_METHOD);
600 TYPE_CHAIN (type) = type;
601 type->set_instance_flags (flags);
604 if (param_types[num_types - 1] == NULL)
607 type->set_has_varargs (true);
609 else if (check_typedef (param_types[num_types - 1])->code ()
613 /* Caller should have ensured this. */
614 gdb_assert (num_types == 0);
615 type->set_is_prototyped (true);
619 /* We don't use TYPE_ZALLOC here to allocate space as TYPE is owned by
620 neither an objfile nor a gdbarch. As a result we must manually
621 allocate memory for auxiliary fields, and free the memory ourselves
622 when we are done with it. */
623 type->set_num_fields (num_types);
625 ((struct field *) xzalloc (sizeof (struct field) * num_types));
627 while (num_types-- > 0)
628 type->field (num_types).set_type (param_types[num_types]);
631 fake_method::~fake_method ()
633 xfree (m_type.fields ());
636 /* Helper for evaluating an OP_VAR_VALUE. */
639 evaluate_var_value (enum noside noside, const block *blk, symbol *var)
641 /* JYG: We used to just return value_zero of the symbol type if
642 we're asked to avoid side effects. Otherwise we return
643 value_of_variable (...). However I'm not sure if
644 value_of_variable () has any side effect. We need a full value
645 object returned here for whatis_exp () to call evaluate_type ()
646 and then pass the full value to value_rtti_target_type () if we
647 are dealing with a pointer or reference to a base class and print
650 struct value *ret = NULL;
654 ret = value_of_variable (var, blk);
657 catch (const gdb_exception_error &except)
659 if (noside != EVAL_AVOID_SIDE_EFFECTS)
662 ret = value_zero (SYMBOL_TYPE (var), not_lval);
668 /* Helper for evaluating an OP_VAR_MSYM_VALUE. */
671 evaluate_var_msym_value (enum noside noside,
672 struct objfile *objfile, minimal_symbol *msymbol)
675 type *the_type = find_minsym_type_and_address (msymbol, objfile, &address);
677 if (noside == EVAL_AVOID_SIDE_EFFECTS && !the_type->is_gnu_ifunc ())
678 return value_zero (the_type, not_lval);
680 return value_at_lazy (the_type, address);
683 /* Helper for returning a value when handling EVAL_SKIP. */
686 eval_skip_value (expression *exp)
688 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
691 /* See expression.h. */
694 evaluate_subexp_do_call (expression *exp, enum noside noside,
695 int nargs, value **argvec,
696 const char *function_name,
697 type *default_return_type)
699 if (argvec[0] == NULL)
700 error (_("Cannot evaluate function -- may be inlined"));
701 if (noside == EVAL_AVOID_SIDE_EFFECTS)
703 /* If the return type doesn't look like a function type,
704 call an error. This can happen if somebody tries to turn
705 a variable into a function call. */
707 type *ftype = value_type (argvec[0]);
709 if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
711 /* We don't know anything about what the internal
712 function might return, but we have to return
714 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
717 else if (ftype->code () == TYPE_CODE_XMETHOD)
720 = result_type_of_xmethod (argvec[0],
721 gdb::make_array_view (argvec + 1,
724 if (return_type == NULL)
725 error (_("Xmethod is missing return type."));
726 return value_zero (return_type, not_lval);
728 else if (ftype->code () == TYPE_CODE_FUNC
729 || ftype->code () == TYPE_CODE_METHOD)
731 if (ftype->is_gnu_ifunc ())
733 CORE_ADDR address = value_address (argvec[0]);
734 type *resolved_type = find_gnu_ifunc_target_type (address);
736 if (resolved_type != NULL)
737 ftype = resolved_type;
740 type *return_type = TYPE_TARGET_TYPE (ftype);
742 if (return_type == NULL)
743 return_type = default_return_type;
745 if (return_type == NULL)
746 error_call_unknown_return_type (function_name);
748 return allocate_value (return_type);
751 error (_("Expression of type other than "
752 "\"Function returning ...\" used as function"));
754 switch (value_type (argvec[0])->code ())
756 case TYPE_CODE_INTERNAL_FUNCTION:
757 return call_internal_function (exp->gdbarch, exp->language_defn,
758 argvec[0], nargs, argvec + 1);
759 case TYPE_CODE_XMETHOD:
760 return call_xmethod (argvec[0], gdb::make_array_view (argvec + 1, nargs));
762 return call_function_by_hand (argvec[0], default_return_type,
763 gdb::make_array_view (argvec + 1, nargs));
767 /* Helper for evaluating an OP_FUNCALL. */
770 evaluate_funcall (type *expect_type, expression *exp, int *pos,
778 symbol *function = NULL;
779 char *function_name = NULL;
780 const char *var_func_name = NULL;
785 exp_opcode op = exp->elts[*pos].opcode;
786 int nargs = longest_to_int (exp->elts[pc].longconst);
787 /* Allocate arg vector, including space for the function to be
788 called in argvec[0], a potential `this', and a terminating
790 value **argvec = (value **) alloca (sizeof (value *) * (nargs + 3));
791 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
793 /* First, evaluate the structure into arg2. */
796 if (op == STRUCTOP_MEMBER)
798 arg2 = evaluate_subexp_for_address (exp, pos, noside);
802 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
805 /* If the function is a virtual function, then the aggregate
806 value (providing the structure) plays its part by providing
807 the vtable. Otherwise, it is just along for the ride: call
808 the function directly. */
810 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
812 type *a1_type = check_typedef (value_type (arg1));
813 if (noside == EVAL_SKIP)
814 tem = 1; /* Set it to the right arg index so that all
815 arguments can also be skipped. */
816 else if (a1_type->code () == TYPE_CODE_METHODPTR)
818 if (noside == EVAL_AVOID_SIDE_EFFECTS)
819 arg1 = value_zero (TYPE_TARGET_TYPE (a1_type), not_lval);
821 arg1 = cplus_method_ptr_to_value (&arg2, arg1);
823 /* Now, say which argument to start evaluating from. */
828 else if (a1_type->code () == TYPE_CODE_MEMBERPTR)
830 struct type *type_ptr
831 = lookup_pointer_type (TYPE_SELF_TYPE (a1_type));
832 struct type *target_type_ptr
833 = lookup_pointer_type (TYPE_TARGET_TYPE (a1_type));
835 /* Now, convert these values to an address. */
836 arg2 = value_cast (type_ptr, arg2);
838 long mem_offset = value_as_long (arg1);
840 arg1 = value_from_pointer (target_type_ptr,
841 value_as_long (arg2) + mem_offset);
842 arg1 = value_ind (arg1);
846 error (_("Non-pointer-to-member value used in pointer-to-member "
849 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
851 /* Hair for method invocations. */
855 /* First, evaluate the structure into arg2. */
857 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
858 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
860 if (op == STRUCTOP_STRUCT)
862 /* If v is a variable in a register, and the user types
863 v.method (), this will produce an error, because v has no
866 A possible way around this would be to allocate a copy of
867 the variable on the stack, copy in the contents, call the
868 function, and copy out the contents. I.e. convert this
869 from call by reference to call by copy-return (or
870 whatever it's called). However, this does not work
871 because it is not the same: the method being called could
872 stash a copy of the address, and then future uses through
873 that address (after the method returns) would be expected
874 to use the variable itself, not some copy of it. */
875 arg2 = evaluate_subexp_for_address (exp, pos, noside);
879 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
881 /* Check to see if the operator '->' has been overloaded.
882 If the operator has been overloaded replace arg2 with the
883 value returned by the custom operator and continue
885 while (unop_user_defined_p (op, arg2))
887 struct value *value = NULL;
890 value = value_x_unop (arg2, op, noside);
893 catch (const gdb_exception_error &except)
895 if (except.error == NOT_FOUND_ERROR)
904 /* Now, say which argument to start evaluating from. */
907 else if (op == OP_SCOPE
908 && overload_resolution
909 && (exp->language_defn->la_language == language_cplus))
911 /* Unpack it locally so we can properly handle overload
917 local_tem = longest_to_int (exp->elts[pc2 + 2].longconst);
918 (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1);
919 struct type *type = exp->elts[pc2 + 1].type;
920 name = &exp->elts[pc2 + 3].string;
923 function_name = NULL;
924 if (type->code () == TYPE_CODE_NAMESPACE)
926 function = cp_lookup_symbol_namespace (type->name (),
928 get_selected_block (0),
930 if (function == NULL)
931 error (_("No symbol \"%s\" in namespace \"%s\"."),
932 name, type->name ());
935 /* arg2 is left as NULL on purpose. */
939 gdb_assert (type->code () == TYPE_CODE_STRUCT
940 || type->code () == TYPE_CODE_UNION);
941 function_name = name;
943 /* We need a properly typed value for method lookup. For
944 static methods arg2 is otherwise unused. */
945 arg2 = value_zero (type, lval_memory);
950 else if (op == OP_ADL_FUNC)
952 /* Save the function position and move pos so that the arguments
959 func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst);
960 (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1);
964 /* Non-method function call. */
968 /* If this is a C++ function wait until overload resolution. */
969 if (op == OP_VAR_VALUE
970 && overload_resolution
971 && (exp->language_defn->la_language == language_cplus))
973 (*pos) += 4; /* Skip the evaluation of the symbol. */
978 if (op == OP_VAR_MSYM_VALUE)
980 minimal_symbol *msym = exp->elts[*pos + 2].msymbol;
981 var_func_name = msym->print_name ();
983 else if (op == OP_VAR_VALUE)
985 symbol *sym = exp->elts[*pos + 2].symbol;
986 var_func_name = sym->print_name ();
989 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
990 type *type = value_type (argvec[0]);
991 if (type && type->code () == TYPE_CODE_PTR)
992 type = TYPE_TARGET_TYPE (type);
993 if (type && type->code () == TYPE_CODE_FUNC)
995 for (; tem <= nargs && tem <= type->num_fields (); tem++)
997 argvec[tem] = evaluate_subexp (type->field (tem - 1).type (),
1004 /* Evaluate arguments (if not already done, e.g., namespace::func()
1005 and overload-resolution is off). */
1006 for (; tem <= nargs; tem++)
1008 /* Ensure that array expressions are coerced into pointer
1010 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1013 /* Signal end of arglist. */
1016 if (noside == EVAL_SKIP)
1017 return eval_skip_value (exp);
1019 if (op == OP_ADL_FUNC)
1021 struct symbol *symp;
1024 int string_pc = save_pos1 + 3;
1026 /* Extract the function name. */
1027 name_len = longest_to_int (exp->elts[string_pc].longconst);
1028 func_name = (char *) alloca (name_len + 1);
1029 strcpy (func_name, &exp->elts[string_pc + 1].string);
1031 find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1033 NON_METHOD, /* not method */
1034 NULL, NULL, /* pass NULL symbol since
1035 symbol is unknown */
1036 NULL, &symp, NULL, 0, noside);
1038 /* Now fix the expression being evaluated. */
1039 exp->elts[save_pos1 + 2].symbol = symp;
1040 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1043 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
1044 || (op == OP_SCOPE && function_name != NULL))
1046 int static_memfuncp;
1049 /* Method invocation: stuff "this" as first parameter. If the
1050 method turns out to be static we undo this below. */
1055 /* Name of method from expression. */
1056 tstr = &exp->elts[pc2 + 2].string;
1059 tstr = function_name;
1061 if (overload_resolution && (exp->language_defn->la_language
1064 /* Language is C++, do some overload resolution before
1066 struct value *valp = NULL;
1068 (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1070 METHOD, /* method */
1071 &arg2, /* the object */
1073 &static_memfuncp, 0, noside);
1075 if (op == OP_SCOPE && !static_memfuncp)
1077 /* For the time being, we don't handle this. */
1078 error (_("Call to overloaded function %s requires "
1082 argvec[1] = arg2; /* the ``this'' pointer */
1083 argvec[0] = valp; /* Use the method found after overload
1087 /* Non-C++ case -- or no overload resolution. */
1089 struct value *temp = arg2;
1091 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1093 op == STRUCTOP_STRUCT
1094 ? "structure" : "structure pointer");
1095 /* value_struct_elt updates temp with the correct value of
1096 the ``this'' pointer if necessary, so modify argvec[1] to
1097 reflect any ``this'' changes. */
1099 = value_from_longest (lookup_pointer_type(value_type (temp)),
1100 value_address (temp)
1101 + value_embedded_offset (temp));
1102 argvec[1] = arg2; /* the ``this'' pointer */
1105 /* Take out `this' if needed. */
1106 if (static_memfuncp)
1108 argvec[1] = argvec[0];
1113 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1115 /* Pointer to member. argvec[1] is already set up. */
1118 else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL))
1120 /* Non-member function being called. */
1121 /* fn: This can only be done for C++ functions. A C-style
1122 function in a C++ program, for instance, does not have the
1123 fields that are expected here. */
1125 if (overload_resolution && (exp->language_defn->la_language
1128 /* Language is C++, do some overload resolution before
1130 struct symbol *symp;
1133 /* If a scope has been specified disable ADL. */
1137 if (op == OP_VAR_VALUE)
1138 function = exp->elts[save_pos1+2].symbol;
1140 (void) find_overload_match (gdb::make_array_view (&argvec[1], nargs),
1141 NULL, /* no need for name */
1142 NON_METHOD, /* not method */
1143 NULL, function, /* the function */
1144 NULL, &symp, NULL, no_adl, noside);
1146 if (op == OP_VAR_VALUE)
1148 /* Now fix the expression being evaluated. */
1149 exp->elts[save_pos1+2].symbol = symp;
1150 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1,
1154 argvec[0] = value_of_variable (symp, get_selected_block (0));
1158 /* Not C++, or no overload resolution allowed. */
1159 /* Nothing to be done; argvec already correctly set up. */
1164 /* It is probably a C-style function. */
1165 /* Nothing to be done; argvec already correctly set up. */
1168 return evaluate_subexp_do_call (exp, noside, nargs, argvec,
1169 var_func_name, expect_type);
1172 /* Return true if type is integral or reference to integral */
1175 is_integral_or_integral_reference (struct type *type)
1177 if (is_integral_type (type))
1180 type = check_typedef (type);
1181 return (type != nullptr
1182 && TYPE_IS_REFERENCE (type)
1183 && is_integral_type (TYPE_TARGET_TYPE (type)));
1187 evaluate_subexp_standard (struct type *expect_type,
1188 struct expression *exp, int *pos,
1192 int tem, tem2, tem3;
1194 struct value *arg1 = NULL;
1195 struct value *arg2 = NULL;
1199 struct value **argvec;
1202 struct type **arg_types;
1205 op = exp->elts[pc].opcode;
1210 tem = longest_to_int (exp->elts[pc + 2].longconst);
1211 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
1212 if (noside == EVAL_SKIP)
1213 return eval_skip_value (exp);
1214 arg1 = value_aggregate_elt (exp->elts[pc + 1].type,
1215 &exp->elts[pc + 3].string,
1216 expect_type, 0, noside);
1218 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
1223 return value_from_longest (exp->elts[pc + 1].type,
1224 exp->elts[pc + 2].longconst);
1228 return value_from_contents (exp->elts[pc + 1].type,
1229 exp->elts[pc + 2].floatconst);
1235 symbol *var = exp->elts[pc + 2].symbol;
1236 if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR)
1237 error_unknown_type (var->print_name ());
1238 if (noside != EVAL_SKIP)
1239 return evaluate_var_value (noside, exp->elts[pc + 1].block, var);
1242 /* Return a dummy value of the correct type when skipping, so
1243 that parent functions know what is to be skipped. */
1244 return allocate_value (SYMBOL_TYPE (var));
1248 case OP_VAR_MSYM_VALUE:
1252 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
1253 value *val = evaluate_var_msym_value (noside,
1254 exp->elts[pc + 1].objfile,
1257 type = value_type (val);
1258 if (type->code () == TYPE_CODE_ERROR
1259 && (noside != EVAL_AVOID_SIDE_EFFECTS || pc != 0))
1260 error_unknown_type (msymbol->print_name ());
1264 case OP_VAR_ENTRY_VALUE:
1266 if (noside == EVAL_SKIP)
1267 return eval_skip_value (exp);
1270 struct symbol *sym = exp->elts[pc + 1].symbol;
1271 struct frame_info *frame;
1273 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1274 return value_zero (SYMBOL_TYPE (sym), not_lval);
1276 if (SYMBOL_COMPUTED_OPS (sym) == NULL
1277 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
1278 error (_("Symbol \"%s\" does not have any specific entry value"),
1279 sym->print_name ());
1281 frame = get_selected_frame (NULL);
1282 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
1285 case OP_FUNC_STATIC_VAR:
1286 tem = longest_to_int (exp->elts[pc + 1].longconst);
1287 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1288 if (noside == EVAL_SKIP)
1289 return eval_skip_value (exp);
1292 value *func = evaluate_subexp_standard (NULL, exp, pos, noside);
1293 CORE_ADDR addr = value_address (func);
1295 const block *blk = block_for_pc (addr);
1296 const char *var = &exp->elts[pc + 2].string;
1298 struct block_symbol sym = lookup_symbol (var, blk, VAR_DOMAIN, NULL);
1300 if (sym.symbol == NULL)
1301 error (_("No symbol \"%s\" in specified context."), var);
1303 return evaluate_var_value (noside, sym.block, sym.symbol);
1309 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
1313 const char *name = &exp->elts[pc + 2].string;
1317 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
1318 regno = user_reg_map_name_to_regnum (exp->gdbarch,
1319 name, strlen (name));
1321 error (_("Register $%s not available."), name);
1323 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
1324 a value with the appropriate register type. Unfortunately,
1325 we don't have easy access to the type of user registers.
1326 So for these registers, we fetch the register value regardless
1327 of the evaluation mode. */
1328 if (noside == EVAL_AVOID_SIDE_EFFECTS
1329 && regno < gdbarch_num_cooked_regs (exp->gdbarch))
1330 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
1332 val = value_of_register (regno, get_selected_frame (NULL));
1334 error (_("Value of register %s not available."), name);
1340 type = language_bool_type (exp->language_defn, exp->gdbarch);
1341 return value_from_longest (type, exp->elts[pc + 1].longconst);
1343 case OP_INTERNALVAR:
1345 return value_of_internalvar (exp->gdbarch,
1346 exp->elts[pc + 1].internalvar);
1349 tem = longest_to_int (exp->elts[pc + 1].longconst);
1350 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1351 if (noside == EVAL_SKIP)
1352 return eval_skip_value (exp);
1353 type = language_string_char_type (exp->language_defn, exp->gdbarch);
1354 return value_string (&exp->elts[pc + 2].string, tem, type);
1356 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
1357 NSString constant. */
1358 tem = longest_to_int (exp->elts[pc + 1].longconst);
1359 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1360 if (noside == EVAL_SKIP)
1361 return eval_skip_value (exp);
1362 return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
1366 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
1367 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
1368 nargs = tem3 - tem2 + 1;
1369 type = expect_type ? check_typedef (expect_type) : nullptr;
1371 if (expect_type != nullptr && noside != EVAL_SKIP
1372 && type->code () == TYPE_CODE_STRUCT)
1374 struct value *rec = allocate_value (expect_type);
1376 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
1377 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
1380 if (expect_type != nullptr && noside != EVAL_SKIP
1381 && type->code () == TYPE_CODE_ARRAY)
1383 struct type *range_type = type->index_type ();
1384 struct type *element_type = TYPE_TARGET_TYPE (type);
1385 struct value *array = allocate_value (expect_type);
1386 int element_size = TYPE_LENGTH (check_typedef (element_type));
1387 LONGEST low_bound, high_bound, index;
1389 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
1392 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
1395 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
1396 for (tem = nargs; --nargs >= 0;)
1398 struct value *element;
1400 element = evaluate_subexp (element_type, exp, pos, noside);
1401 if (value_type (element) != element_type)
1402 element = value_cast (element_type, element);
1403 if (index > high_bound)
1404 /* To avoid memory corruption. */
1405 error (_("Too many array elements"));
1406 memcpy (value_contents_raw (array)
1407 + (index - low_bound) * element_size,
1408 value_contents (element),
1415 if (expect_type != nullptr && noside != EVAL_SKIP
1416 && type->code () == TYPE_CODE_SET)
1418 struct value *set = allocate_value (expect_type);
1419 gdb_byte *valaddr = value_contents_raw (set);
1420 struct type *element_type = type->index_type ();
1421 struct type *check_type = element_type;
1422 LONGEST low_bound, high_bound;
1424 /* Get targettype of elementtype. */
1425 while (check_type->code () == TYPE_CODE_RANGE
1426 || check_type->code () == TYPE_CODE_TYPEDEF)
1427 check_type = TYPE_TARGET_TYPE (check_type);
1429 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
1430 error (_("(power)set type with unknown size"));
1431 memset (valaddr, '\0', TYPE_LENGTH (type));
1432 for (tem = 0; tem < nargs; tem++)
1434 LONGEST range_low, range_high;
1435 struct type *range_low_type, *range_high_type;
1436 struct value *elem_val;
1438 elem_val = evaluate_subexp (element_type, exp, pos, noside);
1439 range_low_type = range_high_type = value_type (elem_val);
1440 range_low = range_high = value_as_long (elem_val);
1442 /* Check types of elements to avoid mixture of elements from
1443 different types. Also check if type of element is "compatible"
1444 with element type of powerset. */
1445 if (range_low_type->code () == TYPE_CODE_RANGE)
1446 range_low_type = TYPE_TARGET_TYPE (range_low_type);
1447 if (range_high_type->code () == TYPE_CODE_RANGE)
1448 range_high_type = TYPE_TARGET_TYPE (range_high_type);
1449 if ((range_low_type->code () != range_high_type->code ())
1450 || (range_low_type->code () == TYPE_CODE_ENUM
1451 && (range_low_type != range_high_type)))
1452 /* different element modes. */
1453 error (_("POWERSET tuple elements of different mode"));
1454 if ((check_type->code () != range_low_type->code ())
1455 || (check_type->code () == TYPE_CODE_ENUM
1456 && range_low_type != check_type))
1457 error (_("incompatible POWERSET tuple elements"));
1458 if (range_low > range_high)
1460 warning (_("empty POWERSET tuple range"));
1463 if (range_low < low_bound || range_high > high_bound)
1464 error (_("POWERSET tuple element out of range"));
1465 range_low -= low_bound;
1466 range_high -= low_bound;
1467 for (; range_low <= range_high; range_low++)
1469 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
1471 if (gdbarch_byte_order (exp->gdbarch) == BFD_ENDIAN_BIG)
1472 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
1473 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
1480 argvec = XALLOCAVEC (struct value *, nargs);
1481 for (tem = 0; tem < nargs; tem++)
1483 /* Ensure that array expressions are coerced into pointer
1485 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1487 if (noside == EVAL_SKIP)
1488 return eval_skip_value (exp);
1489 return value_array (tem2, tem3, argvec);
1493 struct value *array = evaluate_subexp (nullptr, exp, pos, noside);
1495 = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
1496 int upper = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
1498 if (noside == EVAL_SKIP)
1499 return eval_skip_value (exp);
1500 return value_slice (array, lowbound, upper - lowbound + 1);
1504 /* Skip third and second args to evaluate the first one. */
1505 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1506 if (value_logical_not (arg1))
1508 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
1509 return evaluate_subexp (nullptr, exp, pos, noside);
1513 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
1514 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
1518 case OP_OBJC_SELECTOR:
1519 { /* Objective C @selector operator. */
1520 char *sel = &exp->elts[pc + 2].string;
1521 int len = longest_to_int (exp->elts[pc + 1].longconst);
1522 struct type *selector_type;
1524 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
1525 if (noside == EVAL_SKIP)
1526 return eval_skip_value (exp);
1529 sel[len] = 0; /* Make sure it's terminated. */
1531 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1532 return value_from_longest (selector_type,
1533 lookup_child_selector (exp->gdbarch, sel));
1536 case OP_OBJC_MSGCALL:
1537 { /* Objective C message (method) call. */
1539 CORE_ADDR responds_selector = 0;
1540 CORE_ADDR method_selector = 0;
1542 CORE_ADDR selector = 0;
1544 int struct_return = 0;
1545 enum noside sub_no_side = EVAL_NORMAL;
1547 struct value *msg_send = NULL;
1548 struct value *msg_send_stret = NULL;
1549 int gnu_runtime = 0;
1551 struct value *target = NULL;
1552 struct value *method = NULL;
1553 struct value *called_method = NULL;
1555 struct type *selector_type = NULL;
1556 struct type *long_type;
1558 struct value *ret = NULL;
1561 selector = exp->elts[pc + 1].longconst;
1562 nargs = exp->elts[pc + 2].longconst;
1563 argvec = XALLOCAVEC (struct value *, nargs + 5);
1567 long_type = builtin_type (exp->gdbarch)->builtin_long;
1568 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1570 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1571 sub_no_side = EVAL_NORMAL;
1573 sub_no_side = noside;
1575 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
1577 if (value_as_long (target) == 0)
1578 return value_from_longest (long_type, 0);
1580 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1583 /* Find the method dispatch (Apple runtime) or method lookup
1584 (GNU runtime) function for Objective-C. These will be used
1585 to lookup the symbol information for the method. If we
1586 can't find any symbol information, then we'll use these to
1587 call the method, otherwise we can call the method
1588 directly. The msg_send_stret function is used in the special
1589 case of a method that returns a structure (Apple runtime
1593 type = selector_type;
1595 type = lookup_function_type (type);
1596 type = lookup_pointer_type (type);
1597 type = lookup_function_type (type);
1598 type = lookup_pointer_type (type);
1600 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1602 = find_function_in_inferior ("objc_msg_lookup", NULL);
1604 msg_send = value_from_pointer (type, value_as_address (msg_send));
1605 msg_send_stret = value_from_pointer (type,
1606 value_as_address (msg_send_stret));
1610 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1611 /* Special dispatcher for methods returning structs. */
1613 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1616 /* Verify the target object responds to this method. The
1617 standard top-level 'Object' class uses a different name for
1618 the verification method than the non-standard, but more
1619 often used, 'NSObject' class. Make sure we check for both. */
1622 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1623 if (responds_selector == 0)
1625 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1627 if (responds_selector == 0)
1628 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1631 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1632 if (method_selector == 0)
1634 = lookup_child_selector (exp->gdbarch, "methodFor:");
1636 if (method_selector == 0)
1637 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1639 /* Call the verification method, to make sure that the target
1640 class implements the desired method. */
1642 argvec[0] = msg_send;
1644 argvec[2] = value_from_longest (long_type, responds_selector);
1645 argvec[3] = value_from_longest (long_type, selector);
1648 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1651 /* Function objc_msg_lookup returns a pointer. */
1653 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1655 if (value_as_long (ret) == 0)
1656 error (_("Target does not respond to this message selector."));
1658 /* Call "methodForSelector:" method, to get the address of a
1659 function method that implements this selector for this
1660 class. If we can find a symbol at that address, then we
1661 know the return type, parameter types etc. (that's a good
1664 argvec[0] = msg_send;
1666 argvec[2] = value_from_longest (long_type, method_selector);
1667 argvec[3] = value_from_longest (long_type, selector);
1670 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1674 ret = call_function_by_hand (argvec[0], NULL, {argvec + 1, 3});
1677 /* ret should now be the selector. */
1679 addr = value_as_long (ret);
1682 struct symbol *sym = NULL;
1684 /* The address might point to a function descriptor;
1685 resolve it to the actual code address instead. */
1686 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1687 current_top_target ());
1689 /* Is it a high_level symbol? */
1690 sym = find_pc_function (addr);
1692 method = value_of_variable (sym, 0);
1695 /* If we found a method with symbol information, check to see
1696 if it returns a struct. Otherwise assume it doesn't. */
1701 struct type *val_type;
1703 funaddr = find_function_addr (method, &val_type);
1705 block_for_pc (funaddr);
1707 val_type = check_typedef (val_type);
1709 if ((val_type == NULL)
1710 || (val_type->code () == TYPE_CODE_ERROR))
1712 if (expect_type != NULL)
1713 val_type = expect_type;
1716 struct_return = using_struct_return (exp->gdbarch, method,
1719 else if (expect_type != NULL)
1721 struct_return = using_struct_return (exp->gdbarch, NULL,
1722 check_typedef (expect_type));
1725 /* Found a function symbol. Now we will substitute its
1726 value in place of the message dispatcher (obj_msgSend),
1727 so that we call the method directly instead of thru
1728 the dispatcher. The main reason for doing this is that
1729 we can now evaluate the return value and parameter values
1730 according to their known data types, in case we need to
1731 do things like promotion, dereferencing, special handling
1732 of structs and doubles, etc.
1734 We want to use the type signature of 'method', but still
1735 jump to objc_msgSend() or objc_msgSend_stret() to better
1736 mimic the behavior of the runtime. */
1740 if (value_type (method)->code () != TYPE_CODE_FUNC)
1741 error (_("method address has symbol information "
1742 "with non-function type; skipping"));
1744 /* Create a function pointer of the appropriate type, and
1745 replace its value with the value of msg_send or
1746 msg_send_stret. We must use a pointer here, as
1747 msg_send and msg_send_stret are of pointer type, and
1748 the representation may be different on systems that use
1749 function descriptors. */
1752 = value_from_pointer (lookup_pointer_type (value_type (method)),
1753 value_as_address (msg_send_stret));
1756 = value_from_pointer (lookup_pointer_type (value_type (method)),
1757 value_as_address (msg_send));
1762 called_method = msg_send_stret;
1764 called_method = msg_send;
1767 if (noside == EVAL_SKIP)
1768 return eval_skip_value (exp);
1770 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1772 /* If the return type doesn't look like a function type,
1773 call an error. This can happen if somebody tries to
1774 turn a variable into a function call. This is here
1775 because people often want to call, eg, strcmp, which
1776 gdb doesn't know is a function. If gdb isn't asked for
1777 it's opinion (ie. through "whatis"), it won't offer
1780 struct type *callee_type = value_type (called_method);
1782 if (callee_type && callee_type->code () == TYPE_CODE_PTR)
1783 callee_type = TYPE_TARGET_TYPE (callee_type);
1784 callee_type = TYPE_TARGET_TYPE (callee_type);
1788 if ((callee_type->code () == TYPE_CODE_ERROR) && expect_type)
1789 return allocate_value (expect_type);
1791 return allocate_value (callee_type);
1794 error (_("Expression of type other than "
1795 "\"method returning ...\" used as a method"));
1798 /* Now depending on whether we found a symbol for the method,
1799 we will either call the runtime dispatcher or the method
1802 argvec[0] = called_method;
1804 argvec[2] = value_from_longest (long_type, selector);
1805 /* User-supplied arguments. */
1806 for (tem = 0; tem < nargs; tem++)
1807 argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
1808 argvec[tem + 3] = 0;
1810 auto call_args = gdb::make_array_view (argvec + 1, nargs + 2);
1812 if (gnu_runtime && (method != NULL))
1814 /* Function objc_msg_lookup returns a pointer. */
1815 deprecated_set_value_type (argvec[0],
1816 lookup_pointer_type (lookup_function_type (value_type (argvec[0]))));
1817 argvec[0] = call_function_by_hand (argvec[0], NULL, call_args);
1820 return call_function_by_hand (argvec[0], NULL, call_args);
1825 return evaluate_funcall (expect_type, exp, pos, noside);
1828 /* We have a complex number, There should be 2 floating
1829 point numbers that compose it. */
1831 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1832 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
1834 return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
1836 case STRUCTOP_STRUCT:
1837 tem = longest_to_int (exp->elts[pc + 1].longconst);
1838 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1839 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1840 if (noside == EVAL_SKIP)
1841 return eval_skip_value (exp);
1842 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1844 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1845 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1849 tem = longest_to_int (exp->elts[pc + 1].longconst);
1850 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1851 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1852 if (noside == EVAL_SKIP)
1853 return eval_skip_value (exp);
1855 /* Check to see if operator '->' has been overloaded. If so replace
1856 arg1 with the value returned by evaluating operator->(). */
1857 while (unop_user_defined_p (op, arg1))
1859 struct value *value = NULL;
1862 value = value_x_unop (arg1, op, noside);
1865 catch (const gdb_exception_error &except)
1867 if (except.error == NOT_FOUND_ERROR)
1876 /* JYG: if print object is on we need to replace the base type
1877 with rtti type in order to continue on with successful
1878 lookup of member / method only available in the rtti type. */
1880 struct type *arg_type = value_type (arg1);
1881 struct type *real_type;
1882 int full, using_enc;
1884 struct value_print_options opts;
1886 get_user_print_options (&opts);
1887 if (opts.objectprint && TYPE_TARGET_TYPE (arg_type)
1888 && (TYPE_TARGET_TYPE (arg_type)->code () == TYPE_CODE_STRUCT))
1890 real_type = value_rtti_indirect_type (arg1, &full, &top,
1893 arg1 = value_cast (real_type, arg1);
1897 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1898 NULL, "structure pointer");
1899 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1900 arg3 = value_zero (value_type (arg3), VALUE_LVAL (arg3));
1903 case STRUCTOP_MEMBER:
1905 if (op == STRUCTOP_MEMBER)
1906 arg1 = evaluate_subexp_for_address (exp, pos, noside);
1908 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1910 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
1912 if (noside == EVAL_SKIP)
1913 return eval_skip_value (exp);
1915 type = check_typedef (value_type (arg2));
1916 switch (type->code ())
1918 case TYPE_CODE_METHODPTR:
1919 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1920 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1923 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1924 gdb_assert (value_type (arg2)->code () == TYPE_CODE_PTR);
1925 return value_ind (arg2);
1928 case TYPE_CODE_MEMBERPTR:
1929 /* Now, convert these values to an address. */
1930 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_SELF_TYPE (type)),
1933 mem_offset = value_as_long (arg2);
1935 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1936 value_as_long (arg1) + mem_offset);
1937 return value_ind (arg3);
1940 error (_("non-pointer-to-member value used "
1941 "in pointer-to-member construct"));
1946 type_instance_flags flags
1947 = (type_instance_flag_value) longest_to_int (exp->elts[pc + 1].longconst);
1948 nargs = longest_to_int (exp->elts[pc + 2].longconst);
1949 arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
1950 for (ix = 0; ix < nargs; ++ix)
1951 arg_types[ix] = exp->elts[pc + 2 + ix + 1].type;
1953 fake_method fake_expect_type (flags, nargs, arg_types);
1954 *(pos) += 4 + nargs;
1955 return evaluate_subexp_standard (fake_expect_type.type (), exp, pos,
1960 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1961 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1962 if (noside == EVAL_SKIP)
1963 return eval_skip_value (exp);
1964 if (binop_user_defined_p (op, arg1, arg2))
1965 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1967 return value_concat (arg1, arg2);
1970 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1971 /* Special-case assignments where the left-hand-side is a
1972 convenience variable -- in these, don't bother setting an
1973 expected type. This avoids a weird case where re-assigning a
1974 string or array to an internal variable could error with "Too
1975 many array elements". */
1976 arg2 = evaluate_subexp (VALUE_LVAL (arg1) == lval_internalvar
1978 : value_type (arg1),
1981 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1983 if (binop_user_defined_p (op, arg1, arg2))
1984 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1986 return value_assign (arg1, arg2);
1988 case BINOP_ASSIGN_MODIFY:
1990 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
1991 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1992 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1994 op = exp->elts[pc + 1].opcode;
1995 if (binop_user_defined_p (op, arg1, arg2))
1996 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1997 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1999 && is_integral_type (value_type (arg2)))
2000 arg2 = value_ptradd (arg1, value_as_long (arg2));
2001 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
2003 && is_integral_type (value_type (arg2)))
2004 arg2 = value_ptradd (arg1, - value_as_long (arg2));
2007 struct value *tmp = arg1;
2009 /* For shift and integer exponentiation operations,
2010 only promote the first argument. */
2011 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2012 && is_integral_type (value_type (arg2)))
2013 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
2015 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2017 arg2 = value_binop (tmp, arg2, op);
2019 return value_assign (arg1, arg2);
2022 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2023 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2024 if (noside == EVAL_SKIP)
2025 return eval_skip_value (exp);
2026 if (binop_user_defined_p (op, arg1, arg2))
2027 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2028 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2029 && is_integral_or_integral_reference (value_type (arg2)))
2030 return value_ptradd (arg1, value_as_long (arg2));
2031 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
2032 && is_integral_or_integral_reference (value_type (arg1)))
2033 return value_ptradd (arg2, value_as_long (arg1));
2036 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2037 return value_binop (arg1, arg2, BINOP_ADD);
2041 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2042 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2043 if (noside == EVAL_SKIP)
2044 return eval_skip_value (exp);
2045 if (binop_user_defined_p (op, arg1, arg2))
2046 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2047 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2048 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
2050 /* FIXME -- should be ptrdiff_t */
2051 type = builtin_type (exp->gdbarch)->builtin_long;
2052 return value_from_longest (type, value_ptrdiff (arg1, arg2));
2054 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2055 && is_integral_or_integral_reference (value_type (arg2)))
2056 return value_ptradd (arg1, - value_as_long (arg2));
2059 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2060 return value_binop (arg1, arg2, BINOP_SUB);
2071 case BINOP_BITWISE_AND:
2072 case BINOP_BITWISE_IOR:
2073 case BINOP_BITWISE_XOR:
2074 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2075 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2076 if (noside == EVAL_SKIP)
2077 return eval_skip_value (exp);
2078 if (binop_user_defined_p (op, arg1, arg2))
2079 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2082 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2083 fudge arg2 to avoid division-by-zero, the caller is
2084 (theoretically) only looking for the type of the result. */
2085 if (noside == EVAL_AVOID_SIDE_EFFECTS
2086 /* ??? Do we really want to test for BINOP_MOD here?
2087 The implementation of value_binop gives it a well-defined
2090 || op == BINOP_INTDIV
2093 && value_logical_not (arg2))
2095 struct value *v_one;
2097 v_one = value_one (value_type (arg2));
2098 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
2099 return value_binop (arg1, v_one, op);
2103 /* For shift and integer exponentiation operations,
2104 only promote the first argument. */
2105 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2106 && is_integral_type (value_type (arg2)))
2107 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2109 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2111 return value_binop (arg1, arg2, op);
2115 case BINOP_SUBSCRIPT:
2116 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2117 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2118 if (noside == EVAL_SKIP)
2119 return eval_skip_value (exp);
2120 if (binop_user_defined_p (op, arg1, arg2))
2121 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2124 /* If the user attempts to subscript something that is not an
2125 array or pointer type (like a plain int variable for example),
2126 then report this as an error. */
2128 arg1 = coerce_ref (arg1);
2129 type = check_typedef (value_type (arg1));
2130 if (type->code () != TYPE_CODE_ARRAY
2131 && type->code () != TYPE_CODE_PTR)
2134 error (_("cannot subscript something of type `%s'"),
2137 error (_("cannot subscript requested type"));
2140 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2141 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
2143 return value_subscript (arg1, value_as_long (arg2));
2145 case MULTI_SUBSCRIPT:
2147 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2148 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2151 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2152 /* FIXME: EVAL_SKIP handling may not be correct. */
2153 if (noside == EVAL_SKIP)
2157 return eval_skip_value (exp);
2159 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
2160 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2162 /* If the user attempts to subscript something that has no target
2163 type (like a plain int variable for example), then report this
2166 type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
2169 arg1 = value_zero (type, VALUE_LVAL (arg1));
2175 error (_("cannot subscript something of type `%s'"),
2176 value_type (arg1)->name ());
2180 if (binop_user_defined_p (op, arg1, arg2))
2182 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2186 arg1 = coerce_ref (arg1);
2187 type = check_typedef (value_type (arg1));
2189 switch (type->code ())
2192 case TYPE_CODE_ARRAY:
2193 case TYPE_CODE_STRING:
2194 arg1 = value_subscript (arg1, value_as_long (arg2));
2199 error (_("cannot subscript something of type `%s'"),
2202 error (_("cannot subscript requested type"));
2208 case BINOP_LOGICAL_AND:
2209 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2210 if (noside == EVAL_SKIP)
2212 evaluate_subexp (nullptr, exp, pos, noside);
2213 return eval_skip_value (exp);
2217 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2220 if (binop_user_defined_p (op, arg1, arg2))
2222 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2223 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2227 tem = value_logical_not (arg1);
2229 = evaluate_subexp (nullptr, exp, pos, (tem ? EVAL_SKIP : noside));
2230 type = language_bool_type (exp->language_defn, exp->gdbarch);
2231 return value_from_longest (type,
2232 (LONGEST) (!tem && !value_logical_not (arg2)));
2235 case BINOP_LOGICAL_OR:
2236 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2237 if (noside == EVAL_SKIP)
2239 evaluate_subexp (nullptr, exp, pos, noside);
2240 return eval_skip_value (exp);
2244 arg2 = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2247 if (binop_user_defined_p (op, arg1, arg2))
2249 arg2 = evaluate_subexp (nullptr, exp, pos, noside);
2250 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2254 tem = value_logical_not (arg1);
2256 = evaluate_subexp (nullptr, exp, pos, (!tem ? EVAL_SKIP : noside));
2257 type = language_bool_type (exp->language_defn, exp->gdbarch);
2258 return value_from_longest (type,
2259 (LONGEST) (!tem || !value_logical_not (arg2)));
2263 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2264 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2265 if (noside == EVAL_SKIP)
2266 return eval_skip_value (exp);
2267 if (binop_user_defined_p (op, arg1, arg2))
2269 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2273 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2274 tem = value_equal (arg1, arg2);
2275 type = language_bool_type (exp->language_defn, exp->gdbarch);
2276 return value_from_longest (type, (LONGEST) tem);
2279 case BINOP_NOTEQUAL:
2280 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2281 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2282 if (noside == EVAL_SKIP)
2283 return eval_skip_value (exp);
2284 if (binop_user_defined_p (op, arg1, arg2))
2286 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2290 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2291 tem = value_equal (arg1, arg2);
2292 type = language_bool_type (exp->language_defn, exp->gdbarch);
2293 return value_from_longest (type, (LONGEST) ! tem);
2297 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2298 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2299 if (noside == EVAL_SKIP)
2300 return eval_skip_value (exp);
2301 if (binop_user_defined_p (op, arg1, arg2))
2303 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2307 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2308 tem = value_less (arg1, arg2);
2309 type = language_bool_type (exp->language_defn, exp->gdbarch);
2310 return value_from_longest (type, (LONGEST) tem);
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_less (arg2, arg1);
2326 type = language_bool_type (exp->language_defn, exp->gdbarch);
2327 return value_from_longest (type, (LONGEST) tem);
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_less (arg2, arg1) || 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) || value_equal (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 (nullptr, exp, pos, noside);
2367 if (noside == EVAL_SKIP)
2368 return eval_skip_value (exp);
2369 type = check_typedef (value_type (arg2));
2370 if (type->code () != TYPE_CODE_INT
2371 && type->code () != TYPE_CODE_ENUM)
2372 error (_("Non-integral right operand for \"@\" operator."));
2373 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2375 return allocate_repeat_value (value_type (arg1),
2376 longest_to_int (value_as_long (arg2)));
2379 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2382 evaluate_subexp (nullptr, exp, pos, noside);
2383 return evaluate_subexp (nullptr, exp, pos, noside);
2386 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2387 if (noside == EVAL_SKIP)
2388 return eval_skip_value (exp);
2389 if (unop_user_defined_p (op, arg1))
2390 return value_x_unop (arg1, op, noside);
2393 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2394 return value_pos (arg1);
2398 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2399 if (noside == EVAL_SKIP)
2400 return eval_skip_value (exp);
2401 if (unop_user_defined_p (op, arg1))
2402 return value_x_unop (arg1, op, noside);
2405 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2406 return value_neg (arg1);
2409 case UNOP_COMPLEMENT:
2410 /* C++: check for and handle destructor names. */
2412 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2413 if (noside == EVAL_SKIP)
2414 return eval_skip_value (exp);
2415 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2416 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2419 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2420 return value_complement (arg1);
2423 case UNOP_LOGICAL_NOT:
2424 arg1 = evaluate_subexp (nullptr, exp, pos, noside);
2425 if (noside == EVAL_SKIP)
2426 return eval_skip_value (exp);
2427 if (unop_user_defined_p (op, arg1))
2428 return value_x_unop (arg1, op, noside);
2431 type = language_bool_type (exp->language_defn, exp->gdbarch);
2432 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2436 if (expect_type && expect_type->code () == TYPE_CODE_PTR)
2437 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2438 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2439 type = check_typedef (value_type (arg1));
2440 if (type->code () == TYPE_CODE_METHODPTR
2441 || type->code () == TYPE_CODE_MEMBERPTR)
2442 error (_("Attempt to dereference pointer "
2443 "to member without an object"));
2444 if (noside == EVAL_SKIP)
2445 return eval_skip_value (exp);
2446 if (unop_user_defined_p (op, arg1))
2447 return value_x_unop (arg1, op, noside);
2448 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2450 type = check_typedef (value_type (arg1));
2451 if (type->code () == TYPE_CODE_PTR
2452 || TYPE_IS_REFERENCE (type)
2453 /* In C you can dereference an array to get the 1st elt. */
2454 || type->code () == TYPE_CODE_ARRAY
2456 return value_zero (TYPE_TARGET_TYPE (type),
2458 else if (type->code () == TYPE_CODE_INT)
2459 /* GDB allows dereferencing an int. */
2460 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2463 error (_("Attempt to take contents of a non-pointer value."));
2466 /* Allow * on an integer so we can cast it to whatever we want.
2467 This returns an int, which seems like the most C-like thing to
2468 do. "long long" variables are rare enough that
2469 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2470 if (type->code () == TYPE_CODE_INT)
2471 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2472 (CORE_ADDR) value_as_address (arg1));
2473 return value_ind (arg1);
2476 /* C++: check for and handle pointer to members. */
2478 if (noside == EVAL_SKIP)
2480 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2481 return eval_skip_value (exp);
2484 return evaluate_subexp_for_address (exp, pos, noside);
2487 if (noside == EVAL_SKIP)
2489 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2490 return eval_skip_value (exp);
2492 return evaluate_subexp_for_sizeof (exp, pos, noside);
2497 evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS));
2498 /* FIXME: This should be size_t. */
2499 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2500 ULONGEST align = type_align (type);
2502 error (_("could not determine alignment of type"));
2503 return value_from_longest (size_type, align);
2508 type = exp->elts[pc + 1].type;
2509 return evaluate_subexp_for_cast (exp, pos, noside, type);
2511 case UNOP_CAST_TYPE:
2512 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2513 type = value_type (arg1);
2514 return evaluate_subexp_for_cast (exp, pos, noside, type);
2516 case UNOP_DYNAMIC_CAST:
2517 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2518 type = value_type (arg1);
2519 arg1 = evaluate_subexp (type, exp, pos, noside);
2520 if (noside == EVAL_SKIP)
2521 return eval_skip_value (exp);
2522 return value_dynamic_cast (type, arg1);
2524 case UNOP_REINTERPRET_CAST:
2525 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2526 type = value_type (arg1);
2527 arg1 = evaluate_subexp (type, exp, pos, noside);
2528 if (noside == EVAL_SKIP)
2529 return eval_skip_value (exp);
2530 return value_reinterpret_cast (type, arg1);
2534 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2535 if (noside == EVAL_SKIP)
2536 return eval_skip_value (exp);
2537 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2538 return value_zero (exp->elts[pc + 1].type, lval_memory);
2540 return value_at_lazy (exp->elts[pc + 1].type,
2541 value_as_address (arg1));
2543 case UNOP_MEMVAL_TYPE:
2544 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2545 type = value_type (arg1);
2546 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2547 if (noside == EVAL_SKIP)
2548 return eval_skip_value (exp);
2549 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2550 return value_zero (type, lval_memory);
2552 return value_at_lazy (type, value_as_address (arg1));
2554 case UNOP_PREINCREMENT:
2555 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2556 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2558 else if (unop_user_defined_p (op, arg1))
2560 return value_x_unop (arg1, op, noside);
2564 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2565 arg2 = value_ptradd (arg1, 1);
2568 struct value *tmp = arg1;
2570 arg2 = value_one (value_type (arg1));
2571 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2572 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2575 return value_assign (arg1, arg2);
2578 case UNOP_PREDECREMENT:
2579 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2580 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2582 else if (unop_user_defined_p (op, arg1))
2584 return value_x_unop (arg1, op, noside);
2588 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2589 arg2 = value_ptradd (arg1, -1);
2592 struct value *tmp = arg1;
2594 arg2 = value_one (value_type (arg1));
2595 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2596 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2599 return value_assign (arg1, arg2);
2602 case UNOP_POSTINCREMENT:
2603 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2604 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2606 else if (unop_user_defined_p (op, arg1))
2608 return value_x_unop (arg1, op, noside);
2612 arg3 = value_non_lval (arg1);
2614 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2615 arg2 = value_ptradd (arg1, 1);
2618 struct value *tmp = arg1;
2620 arg2 = value_one (value_type (arg1));
2621 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2622 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2625 value_assign (arg1, arg2);
2629 case UNOP_POSTDECREMENT:
2630 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2631 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2633 else if (unop_user_defined_p (op, arg1))
2635 return value_x_unop (arg1, op, noside);
2639 arg3 = value_non_lval (arg1);
2641 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2642 arg2 = value_ptradd (arg1, -1);
2645 struct value *tmp = arg1;
2647 arg2 = value_one (value_type (arg1));
2648 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2649 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2652 value_assign (arg1, arg2);
2658 return value_of_this (exp->language_defn);
2661 /* The value is not supposed to be used. This is here to make it
2662 easier to accommodate expressions that contain types. */
2664 if (noside == EVAL_SKIP)
2665 return eval_skip_value (exp);
2666 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2667 return allocate_value (exp->elts[pc + 1].type);
2669 error (_("Attempt to use a type name as an expression"));
2673 if (noside == EVAL_SKIP)
2675 evaluate_subexp (nullptr, exp, pos, EVAL_SKIP);
2676 return eval_skip_value (exp);
2678 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2680 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2681 struct value *result;
2683 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2685 /* 'decltype' has special semantics for lvalues. */
2686 if (op == OP_DECLTYPE
2687 && (sub_op == BINOP_SUBSCRIPT
2688 || sub_op == STRUCTOP_MEMBER
2689 || sub_op == STRUCTOP_MPTR
2690 || sub_op == UNOP_IND
2691 || sub_op == STRUCTOP_STRUCT
2692 || sub_op == STRUCTOP_PTR
2693 || sub_op == OP_SCOPE))
2695 type = value_type (result);
2697 if (!TYPE_IS_REFERENCE (type))
2699 type = lookup_lvalue_reference_type (type);
2700 result = allocate_value (type);
2707 error (_("Attempt to use a type as an expression"));
2711 struct value *result;
2712 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2714 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2715 result = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2717 result = evaluate_subexp (nullptr, exp, pos, noside);
2719 if (noside != EVAL_NORMAL)
2720 return allocate_value (cplus_typeid_type (exp->gdbarch));
2722 return cplus_typeid (result);
2726 /* Removing this case and compiling with gcc -Wall reveals that
2727 a lot of cases are hitting this case. Some of these should
2728 probably be removed from expression.h; others are legitimate
2729 expressions which are (apparently) not fully implemented.
2731 If there are any cases landing here which mean a user error,
2732 then they should be separate cases, with more descriptive
2735 error (_("GDB does not (yet) know how to "
2736 "evaluate that kind of expression"));
2739 gdb_assert_not_reached ("missed return?");
2742 /* Evaluate a subexpression of EXP, at index *POS,
2743 and return the address of that subexpression.
2744 Advance *POS over the subexpression.
2745 If the subexpression isn't an lvalue, get an error.
2746 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2747 then only the type of the result need be correct. */
2749 static struct value *
2750 evaluate_subexp_for_address (struct expression *exp, int *pos,
2760 op = exp->elts[pc].opcode;
2766 x = evaluate_subexp (nullptr, exp, pos, noside);
2768 /* We can't optimize out "&*" if there's a user-defined operator*. */
2769 if (unop_user_defined_p (op, x))
2771 x = value_x_unop (x, op, noside);
2772 goto default_case_after_eval;
2775 return coerce_array (x);
2779 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2780 evaluate_subexp (nullptr, exp, pos, noside));
2782 case UNOP_MEMVAL_TYPE:
2787 x = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2788 type = value_type (x);
2789 return value_cast (lookup_pointer_type (type),
2790 evaluate_subexp (nullptr, exp, pos, noside));
2794 var = exp->elts[pc + 2].symbol;
2796 /* C++: The "address" of a reference should yield the address
2797 * of the object pointed to. Let value_addr() deal with it. */
2798 if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var)))
2802 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2805 lookup_pointer_type (SYMBOL_TYPE (var));
2806 enum address_class sym_class = SYMBOL_CLASS (var);
2808 if (sym_class == LOC_CONST
2809 || sym_class == LOC_CONST_BYTES
2810 || sym_class == LOC_REGISTER)
2811 error (_("Attempt to take address of register or constant."));
2814 value_zero (type, not_lval);
2817 return address_of_variable (var, exp->elts[pc + 1].block);
2819 case OP_VAR_MSYM_VALUE:
2823 value *val = evaluate_var_msym_value (noside,
2824 exp->elts[pc + 1].objfile,
2825 exp->elts[pc + 2].msymbol);
2826 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2828 struct type *type = lookup_pointer_type (value_type (val));
2829 return value_zero (type, not_lval);
2832 return value_addr (val);
2836 tem = longest_to_int (exp->elts[pc + 2].longconst);
2837 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
2838 x = value_aggregate_elt (exp->elts[pc + 1].type,
2839 &exp->elts[pc + 3].string,
2842 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
2847 x = evaluate_subexp (nullptr, exp, pos, noside);
2848 default_case_after_eval:
2849 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2851 struct type *type = check_typedef (value_type (x));
2853 if (TYPE_IS_REFERENCE (type))
2854 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2856 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2857 return value_zero (lookup_pointer_type (value_type (x)),
2860 error (_("Attempt to take address of "
2861 "value not located in memory."));
2863 return value_addr (x);
2867 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2868 When used in contexts where arrays will be coerced anyway, this is
2869 equivalent to `evaluate_subexp' but much faster because it avoids
2870 actually fetching array contents (perhaps obsolete now that we have
2873 Note that we currently only do the coercion for C expressions, where
2874 arrays are zero based and the coercion is correct. For other languages,
2875 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2876 to decide if coercion is appropriate. */
2879 evaluate_subexp_with_coercion (struct expression *exp,
2880 int *pos, enum noside noside)
2889 op = exp->elts[pc].opcode;
2894 var = exp->elts[pc + 2].symbol;
2895 type = check_typedef (SYMBOL_TYPE (var));
2896 if (type->code () == TYPE_CODE_ARRAY
2897 && !type->is_vector ()
2898 && CAST_IS_CONVERSION (exp->language_defn))
2901 val = address_of_variable (var, exp->elts[pc + 1].block);
2902 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2908 return evaluate_subexp (nullptr, exp, pos, noside);
2912 /* Evaluate a subexpression of EXP, at index *POS,
2913 and return a value for the size of that subexpression.
2914 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
2915 we allow side-effects on the operand if its type is a variable
2918 static struct value *
2919 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
2922 /* FIXME: This should be size_t. */
2923 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2930 op = exp->elts[pc].opcode;
2934 /* This case is handled specially
2935 so that we avoid creating a value for the result type.
2936 If the result type is very big, it's desirable not to
2937 create a value unnecessarily. */
2940 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2941 type = check_typedef (value_type (val));
2942 if (type->code () != TYPE_CODE_PTR
2943 && !TYPE_IS_REFERENCE (type)
2944 && type->code () != TYPE_CODE_ARRAY)
2945 error (_("Attempt to take contents of a non-pointer value."));
2946 type = TYPE_TARGET_TYPE (type);
2947 if (is_dynamic_type (type))
2948 type = value_type (value_ind (val));
2949 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2953 type = exp->elts[pc + 1].type;
2956 case UNOP_MEMVAL_TYPE:
2958 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2959 type = value_type (val);
2963 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
2964 if (is_dynamic_type (type))
2966 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
2967 type = value_type (val);
2968 if (type->code () == TYPE_CODE_ARRAY
2969 && is_dynamic_type (type->index_type ())
2970 && type->bounds ()->high.kind () == PROP_UNDEFINED)
2971 return allocate_optimized_out_value (size_type);
2977 case OP_VAR_MSYM_VALUE:
2981 minimal_symbol *msymbol = exp->elts[pc + 2].msymbol;
2982 value *mval = evaluate_var_msym_value (noside,
2983 exp->elts[pc + 1].objfile,
2986 type = value_type (mval);
2987 if (type->code () == TYPE_CODE_ERROR)
2988 error_unknown_type (msymbol->print_name ());
2990 return value_from_longest (size_type, TYPE_LENGTH (type));
2994 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
2995 type of the subscript is a variable length array type. In this case we
2996 must re-evaluate the right hand side of the subscription to allow
2998 case BINOP_SUBSCRIPT:
2999 if (noside == EVAL_NORMAL)
3001 int npc = (*pos) + 1;
3003 val = evaluate_subexp (nullptr, exp, &npc, EVAL_AVOID_SIDE_EFFECTS);
3004 type = check_typedef (value_type (val));
3005 if (type->code () == TYPE_CODE_ARRAY)
3007 type = check_typedef (TYPE_TARGET_TYPE (type));
3008 if (type->code () == TYPE_CODE_ARRAY)
3010 type = type->index_type ();
3011 /* Only re-evaluate the right hand side if the resulting type
3012 is a variable length type. */
3013 if (type->bounds ()->flag_bound_evaluated)
3015 val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
3016 return value_from_longest
3017 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
3026 val = evaluate_subexp (nullptr, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3027 type = value_type (val);
3031 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3032 "When applied to a reference or a reference type, the result is
3033 the size of the referenced type." */
3034 type = check_typedef (type);
3035 if (exp->language_defn->la_language == language_cplus
3036 && (TYPE_IS_REFERENCE (type)))
3037 type = check_typedef (TYPE_TARGET_TYPE (type));
3038 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3041 /* Evaluate a subexpression of EXP, at index *POS, and return a value
3042 for that subexpression cast to TO_TYPE. Advance *POS over the
3046 evaluate_subexp_for_cast (expression *exp, int *pos,
3048 struct type *to_type)
3052 /* Don't let symbols be evaluated with evaluate_subexp because that
3053 throws an "unknown type" error for no-debug data symbols.
3054 Instead, we want the cast to reinterpret the symbol. */
3055 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE
3056 || exp->elts[pc].opcode == OP_VAR_VALUE)
3061 if (exp->elts[pc].opcode == OP_VAR_MSYM_VALUE)
3063 if (noside == EVAL_AVOID_SIDE_EFFECTS)
3064 return value_zero (to_type, not_lval);
3066 val = evaluate_var_msym_value (noside,
3067 exp->elts[pc + 1].objfile,
3068 exp->elts[pc + 2].msymbol);
3071 val = evaluate_var_value (noside,
3072 exp->elts[pc + 1].block,
3073 exp->elts[pc + 2].symbol);
3075 if (noside == EVAL_SKIP)
3076 return eval_skip_value (exp);
3078 val = value_cast (to_type, val);
3080 /* Don't allow e.g. '&(int)var_with_no_debug_info'. */
3081 if (VALUE_LVAL (val) == lval_memory)
3083 if (value_lazy (val))
3084 value_fetch_lazy (val);
3085 VALUE_LVAL (val) = not_lval;
3090 value *val = evaluate_subexp (to_type, exp, pos, noside);
3091 if (noside == EVAL_SKIP)
3092 return eval_skip_value (exp);
3093 return value_cast (to_type, val);
3096 /* Parse a type expression in the string [P..P+LENGTH). */
3099 parse_and_eval_type (char *p, int length)
3101 char *tmp = (char *) alloca (length + 4);
3104 memcpy (tmp + 1, p, length);
3105 tmp[length + 1] = ')';
3106 tmp[length + 2] = '0';
3107 tmp[length + 3] = '\0';
3108 expression_up expr = parse_expression (tmp);
3109 if (expr->elts[0].opcode != UNOP_CAST)
3110 error (_("Internal error in eval_type."));
3111 return expr->elts[1].type;