1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986-2014 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/>. */
25 #include "expression.h"
28 #include "language.h" /* For CAST_IS_CONVERSION. */
29 #include "f-lang.h" /* For array bound stuff. */
32 #include "objc-lang.h"
34 #include "parser-defs.h"
35 #include "cp-support.h"
37 #include "exceptions.h"
39 #include "user-regs.h"
41 #include "gdb_obstack.h"
45 /* This is defined in valops.c */
46 extern int overload_resolution;
48 /* Prototypes for local functions. */
50 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
53 static struct value *evaluate_subexp_for_address (struct expression *,
56 static struct value *evaluate_struct_tuple (struct value *,
57 struct expression *, int *,
60 static LONGEST init_array_element (struct value *, struct value *,
61 struct expression *, int *, enum noside,
65 evaluate_subexp (struct type *expect_type, struct expression *exp,
66 int *pos, enum noside noside)
68 return (*exp->language_defn->la_exp_desc->evaluate_exp)
69 (expect_type, exp, pos, noside);
72 /* Parse the string EXP as a C expression, evaluate it,
73 and return the result as a number. */
76 parse_and_eval_address (const char *exp)
78 struct expression *expr = parse_expression (exp);
80 struct cleanup *old_chain =
81 make_cleanup (free_current_contents, &expr);
83 addr = value_as_address (evaluate_expression (expr));
84 do_cleanups (old_chain);
88 /* Like parse_and_eval_address, but treats the value of the expression
89 as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
91 parse_and_eval_long (const char *exp)
93 struct expression *expr = parse_expression (exp);
95 struct cleanup *old_chain =
96 make_cleanup (free_current_contents, &expr);
98 retval = value_as_long (evaluate_expression (expr));
99 do_cleanups (old_chain);
104 parse_and_eval (const char *exp)
106 struct expression *expr = parse_expression (exp);
108 struct cleanup *old_chain =
109 make_cleanup (free_current_contents, &expr);
111 val = evaluate_expression (expr);
112 do_cleanups (old_chain);
116 /* Parse up to a comma (or to a closeparen)
117 in the string EXPP as an expression, evaluate it, and return the value.
118 EXPP is advanced to point to the comma. */
121 parse_to_comma_and_eval (const char **expp)
123 struct expression *expr = parse_exp_1 (expp, 0, (struct block *) 0, 1);
125 struct cleanup *old_chain =
126 make_cleanup (free_current_contents, &expr);
128 val = evaluate_expression (expr);
129 do_cleanups (old_chain);
133 /* Evaluate an expression in internal prefix form
134 such as is constructed by parse.y.
136 See expression.h for info on the format of an expression. */
139 evaluate_expression (struct expression *exp)
143 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
146 /* Evaluate an expression, avoiding all memory references
147 and getting a value whose type alone is correct. */
150 evaluate_type (struct expression *exp)
154 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
157 /* Evaluate a subexpression, avoiding all memory references and
158 getting a value whose type alone is correct. */
161 evaluate_subexpression_type (struct expression *exp, int subexp)
163 return evaluate_subexp (NULL_TYPE, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS);
166 /* Find the current value of a watchpoint on EXP. Return the value in
167 *VALP and *RESULTP and the chain of intermediate and final values
168 in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
171 If PRESERVE_ERRORS is true, then exceptions are passed through.
172 Otherwise, if PRESERVE_ERRORS is false, then if a memory error
173 occurs while evaluating the expression, *RESULTP will be set to
174 NULL. *RESULTP may be a lazy value, if the result could not be
175 read from memory. It is used to determine whether a value is
176 user-specified (we should watch the whole value) or intermediate
177 (we should watch only the bit used to locate the final value).
179 If the final value, or any intermediate value, could not be read
180 from memory, *VALP will be set to NULL. *VAL_CHAIN will still be
181 set to any referenced values. *VALP will never be a lazy value.
182 This is the value which we store in struct breakpoint.
184 If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
185 value chain. The caller must free the values individually. If
186 VAL_CHAIN is NULL, all generated values will be left on the value
190 fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
191 struct value **resultp, struct value **val_chain,
194 struct value *mark, *new_mark, *result;
195 volatile struct gdb_exception ex;
203 /* Evaluate the expression. */
204 mark = value_mark ();
207 TRY_CATCH (ex, RETURN_MASK_ALL)
209 result = evaluate_subexp (NULL_TYPE, exp, pc, EVAL_NORMAL);
213 /* Ignore memory errors if we want watchpoints pointing at
214 inaccessible memory to still be created; otherwise, throw the
215 error to some higher catcher. */
219 if (!preserve_errors)
222 throw_exception (ex);
227 new_mark = value_mark ();
228 if (mark == new_mark)
233 /* Make sure it's not lazy, so that after the target stops again we
234 have a non-lazy previous value to compare with. */
237 if (!value_lazy (result))
241 volatile struct gdb_exception except;
243 TRY_CATCH (except, RETURN_MASK_ERROR)
245 value_fetch_lazy (result);
253 /* Return the chain of intermediate values. We use this to
254 decide which addresses to watch. */
255 *val_chain = new_mark;
256 value_release_to_mark (mark);
260 /* Extract a field operation from an expression. If the subexpression
261 of EXP starting at *SUBEXP is not a structure dereference
262 operation, return NULL. Otherwise, return the name of the
263 dereferenced field, and advance *SUBEXP to point to the
264 subexpression of the left-hand-side of the dereference. This is
265 used when completing field names. */
268 extract_field_op (struct expression *exp, int *subexp)
273 if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT
274 && exp->elts[*subexp].opcode != STRUCTOP_PTR)
276 tem = longest_to_int (exp->elts[*subexp + 1].longconst);
277 result = &exp->elts[*subexp + 2].string;
278 (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1);
282 /* This function evaluates brace-initializers (in C/C++) for
285 static struct value *
286 evaluate_struct_tuple (struct value *struct_val,
287 struct expression *exp,
288 int *pos, enum noside noside, int nargs)
290 struct type *struct_type = check_typedef (value_type (struct_val));
291 struct type *field_type;
296 struct value *val = NULL;
301 /* Skip static fields. */
302 while (fieldno < TYPE_NFIELDS (struct_type)
303 && field_is_static (&TYPE_FIELD (struct_type,
306 if (fieldno >= TYPE_NFIELDS (struct_type))
307 error (_("too many initializers"));
308 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
309 if (TYPE_CODE (field_type) == TYPE_CODE_UNION
310 && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
311 error (_("don't know which variant you want to set"));
313 /* Here, struct_type is the type of the inner struct,
314 while substruct_type is the type of the inner struct.
315 These are the same for normal structures, but a variant struct
316 contains anonymous union fields that contain substruct fields.
317 The value fieldno is the index of the top-level (normal or
318 anonymous union) field in struct_field, while the value
319 subfieldno is the index of the actual real (named inner) field
320 in substruct_type. */
322 field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
324 val = evaluate_subexp (field_type, exp, pos, noside);
326 /* Now actually set the field in struct_val. */
328 /* Assign val to field fieldno. */
329 if (value_type (val) != field_type)
330 val = value_cast (field_type, val);
332 bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
333 bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
334 addr = value_contents_writeable (struct_val) + bitpos / 8;
336 modify_field (struct_type, addr,
337 value_as_long (val), bitpos % 8, bitsize);
339 memcpy (addr, value_contents (val),
340 TYPE_LENGTH (value_type (val)));
346 /* Recursive helper function for setting elements of array tuples.
347 The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND); the
348 element value is ELEMENT; EXP, POS and NOSIDE are as usual.
349 Evaluates index expresions and sets the specified element(s) of
350 ARRAY to ELEMENT. Returns last index value. */
353 init_array_element (struct value *array, struct value *element,
354 struct expression *exp, int *pos,
355 enum noside noside, LONGEST low_bound, LONGEST high_bound)
358 int element_size = TYPE_LENGTH (value_type (element));
360 if (exp->elts[*pos].opcode == BINOP_COMMA)
363 init_array_element (array, element, exp, pos, noside,
364 low_bound, high_bound);
365 return init_array_element (array, element,
366 exp, pos, noside, low_bound, high_bound);
370 index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
371 if (index < low_bound || index > high_bound)
372 error (_("tuple index out of range"));
373 memcpy (value_contents_raw (array) + (index - low_bound) * element_size,
374 value_contents (element), element_size);
379 static struct value *
380 value_f90_subarray (struct value *array,
381 struct expression *exp, int *pos, enum noside noside)
384 LONGEST low_bound, high_bound;
385 struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array)));
386 enum f90_range_type range_type = longest_to_int (exp->elts[pc].longconst);
390 if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
391 low_bound = TYPE_LOW_BOUND (range);
393 low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
395 if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
396 high_bound = TYPE_HIGH_BOUND (range);
398 high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
400 return value_slice (array, low_bound, high_bound - low_bound + 1);
404 /* Promote value ARG1 as appropriate before performing a unary operation
406 If the result is not appropriate for any particular language then it
407 needs to patch this function. */
410 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
415 *arg1 = coerce_ref (*arg1);
416 type1 = check_typedef (value_type (*arg1));
418 if (is_integral_type (type1))
420 switch (language->la_language)
423 /* Perform integral promotion for ANSI C/C++.
424 If not appropropriate for any particular language
425 it needs to modify this function. */
427 struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
429 if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int))
430 *arg1 = value_cast (builtin_int, *arg1);
437 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
438 operation on those two operands.
439 If the result is not appropriate for any particular language then it
440 needs to patch this function. */
443 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
444 struct value **arg1, struct value **arg2)
446 struct type *promoted_type = NULL;
450 *arg1 = coerce_ref (*arg1);
451 *arg2 = coerce_ref (*arg2);
453 type1 = check_typedef (value_type (*arg1));
454 type2 = check_typedef (value_type (*arg2));
456 if ((TYPE_CODE (type1) != TYPE_CODE_FLT
457 && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
458 && !is_integral_type (type1))
459 || (TYPE_CODE (type2) != TYPE_CODE_FLT
460 && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
461 && !is_integral_type (type2)))
464 if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
465 || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
467 /* No promotion required. */
469 else if (TYPE_CODE (type1) == TYPE_CODE_FLT
470 || TYPE_CODE (type2) == TYPE_CODE_FLT)
472 switch (language->la_language)
478 case language_opencl:
479 /* No promotion required. */
483 /* For other languages the result type is unchanged from gdb
484 version 6.7 for backward compatibility.
485 If either arg was long double, make sure that value is also long
486 double. Otherwise use double. */
487 if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch)
488 || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch))
489 promoted_type = builtin_type (gdbarch)->builtin_long_double;
491 promoted_type = builtin_type (gdbarch)->builtin_double;
495 else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
496 && TYPE_CODE (type2) == TYPE_CODE_BOOL)
498 /* No promotion required. */
501 /* Integral operations here. */
502 /* FIXME: Also mixed integral/booleans, with result an integer. */
504 const struct builtin_type *builtin = builtin_type (gdbarch);
505 unsigned int promoted_len1 = TYPE_LENGTH (type1);
506 unsigned int promoted_len2 = TYPE_LENGTH (type2);
507 int is_unsigned1 = TYPE_UNSIGNED (type1);
508 int is_unsigned2 = TYPE_UNSIGNED (type2);
509 unsigned int result_len;
510 int unsigned_operation;
512 /* Determine type length and signedness after promotion for
514 if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int))
517 promoted_len1 = TYPE_LENGTH (builtin->builtin_int);
519 if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int))
522 promoted_len2 = TYPE_LENGTH (builtin->builtin_int);
525 if (promoted_len1 > promoted_len2)
527 unsigned_operation = is_unsigned1;
528 result_len = promoted_len1;
530 else if (promoted_len2 > promoted_len1)
532 unsigned_operation = is_unsigned2;
533 result_len = promoted_len2;
537 unsigned_operation = is_unsigned1 || is_unsigned2;
538 result_len = promoted_len1;
541 switch (language->la_language)
547 if (result_len <= TYPE_LENGTH (builtin->builtin_int))
549 promoted_type = (unsigned_operation
550 ? builtin->builtin_unsigned_int
551 : builtin->builtin_int);
553 else if (result_len <= TYPE_LENGTH (builtin->builtin_long))
555 promoted_type = (unsigned_operation
556 ? builtin->builtin_unsigned_long
557 : builtin->builtin_long);
561 promoted_type = (unsigned_operation
562 ? builtin->builtin_unsigned_long_long
563 : builtin->builtin_long_long);
566 case language_opencl:
567 if (result_len <= TYPE_LENGTH (lookup_signed_typename
568 (language, gdbarch, "int")))
572 ? lookup_unsigned_typename (language, gdbarch, "int")
573 : lookup_signed_typename (language, gdbarch, "int"));
575 else if (result_len <= TYPE_LENGTH (lookup_signed_typename
576 (language, gdbarch, "long")))
580 ? lookup_unsigned_typename (language, gdbarch, "long")
581 : lookup_signed_typename (language, gdbarch,"long"));
585 /* For other languages the result type is unchanged from gdb
586 version 6.7 for backward compatibility.
587 If either arg was long long, make sure that value is also long
588 long. Otherwise use long. */
589 if (unsigned_operation)
591 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
592 promoted_type = builtin->builtin_unsigned_long_long;
594 promoted_type = builtin->builtin_unsigned_long;
598 if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
599 promoted_type = builtin->builtin_long_long;
601 promoted_type = builtin->builtin_long;
609 /* Promote both operands to common type. */
610 *arg1 = value_cast (promoted_type, *arg1);
611 *arg2 = value_cast (promoted_type, *arg2);
616 ptrmath_type_p (const struct language_defn *lang, struct type *type)
618 type = check_typedef (type);
619 if (TYPE_CODE (type) == TYPE_CODE_REF)
620 type = TYPE_TARGET_TYPE (type);
622 switch (TYPE_CODE (type))
628 case TYPE_CODE_ARRAY:
629 return TYPE_VECTOR (type) ? 0 : lang->c_style_arrays;
636 /* Constructs a fake method with the given parameter types.
637 This function is used by the parser to construct an "expected"
638 type for method overload resolution. */
641 make_params (int num_types, struct type **param_types)
643 struct type *type = XCNEW (struct type);
644 TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
645 TYPE_LENGTH (type) = 1;
646 TYPE_CODE (type) = TYPE_CODE_METHOD;
647 TYPE_VPTR_FIELDNO (type) = -1;
648 TYPE_CHAIN (type) = type;
651 if (param_types[num_types - 1] == NULL)
654 TYPE_VARARGS (type) = 1;
656 else if (TYPE_CODE (check_typedef (param_types[num_types - 1]))
660 /* Caller should have ensured this. */
661 gdb_assert (num_types == 0);
662 TYPE_PROTOTYPED (type) = 1;
666 TYPE_NFIELDS (type) = num_types;
667 TYPE_FIELDS (type) = (struct field *)
668 TYPE_ZALLOC (type, sizeof (struct field) * num_types);
670 while (num_types-- > 0)
671 TYPE_FIELD_TYPE (type, num_types) = param_types[num_types];
677 evaluate_subexp_standard (struct type *expect_type,
678 struct expression *exp, int *pos,
683 int pc, pc2 = 0, oldpos;
684 struct value *arg1 = NULL;
685 struct value *arg2 = NULL;
689 struct value **argvec;
693 struct type **arg_types;
695 struct symbol *function = NULL;
696 char *function_name = NULL;
699 op = exp->elts[pc].opcode;
704 tem = longest_to_int (exp->elts[pc + 2].longconst);
705 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
706 if (noside == EVAL_SKIP)
708 arg1 = value_aggregate_elt (exp->elts[pc + 1].type,
709 &exp->elts[pc + 3].string,
710 expect_type, 0, noside);
712 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
717 return value_from_longest (exp->elts[pc + 1].type,
718 exp->elts[pc + 2].longconst);
722 return value_from_double (exp->elts[pc + 1].type,
723 exp->elts[pc + 2].doubleconst);
727 return value_from_decfloat (exp->elts[pc + 1].type,
728 exp->elts[pc + 2].decfloatconst);
733 if (noside == EVAL_SKIP)
736 /* JYG: We used to just return value_zero of the symbol type
737 if we're asked to avoid side effects. Otherwise we return
738 value_of_variable (...). However I'm not sure if
739 value_of_variable () has any side effect.
740 We need a full value object returned here for whatis_exp ()
741 to call evaluate_type () and then pass the full value to
742 value_rtti_target_type () if we are dealing with a pointer
743 or reference to a base class and print object is on. */
746 volatile struct gdb_exception except;
747 struct value *ret = NULL;
749 TRY_CATCH (except, RETURN_MASK_ERROR)
751 ret = value_of_variable (exp->elts[pc + 2].symbol,
752 exp->elts[pc + 1].block);
755 if (except.reason < 0)
757 if (noside == EVAL_AVOID_SIDE_EFFECTS)
758 ret = value_zero (SYMBOL_TYPE (exp->elts[pc + 2].symbol),
761 throw_exception (except);
767 case OP_VAR_ENTRY_VALUE:
769 if (noside == EVAL_SKIP)
773 struct symbol *sym = exp->elts[pc + 1].symbol;
774 struct frame_info *frame;
776 if (noside == EVAL_AVOID_SIDE_EFFECTS)
777 return value_zero (SYMBOL_TYPE (sym), not_lval);
779 if (SYMBOL_COMPUTED_OPS (sym) == NULL
780 || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
781 error (_("Symbol \"%s\" does not have any specific entry value"),
782 SYMBOL_PRINT_NAME (sym));
784 frame = get_selected_frame (NULL);
785 return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
791 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
795 const char *name = &exp->elts[pc + 2].string;
799 (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
800 regno = user_reg_map_name_to_regnum (exp->gdbarch,
801 name, strlen (name));
803 error (_("Register $%s not available."), name);
805 /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
806 a value with the appropriate register type. Unfortunately,
807 we don't have easy access to the type of user registers.
808 So for these registers, we fetch the register value regardless
809 of the evaluation mode. */
810 if (noside == EVAL_AVOID_SIDE_EFFECTS
811 && regno < gdbarch_num_regs (exp->gdbarch)
812 + gdbarch_num_pseudo_regs (exp->gdbarch))
813 val = value_zero (register_type (exp->gdbarch, regno), not_lval);
815 val = value_of_register (regno, get_selected_frame (NULL));
817 error (_("Value of register %s not available."), name);
823 type = language_bool_type (exp->language_defn, exp->gdbarch);
824 return value_from_longest (type, exp->elts[pc + 1].longconst);
828 return value_of_internalvar (exp->gdbarch,
829 exp->elts[pc + 1].internalvar);
832 tem = longest_to_int (exp->elts[pc + 1].longconst);
833 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
834 if (noside == EVAL_SKIP)
836 type = language_string_char_type (exp->language_defn, exp->gdbarch);
837 return value_string (&exp->elts[pc + 2].string, tem, type);
839 case OP_OBJC_NSSTRING: /* Objective C Foundation Class
840 NSString constant. */
841 tem = longest_to_int (exp->elts[pc + 1].longconst);
842 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
843 if (noside == EVAL_SKIP)
847 return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
851 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
852 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
853 nargs = tem3 - tem2 + 1;
854 type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
856 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
857 && TYPE_CODE (type) == TYPE_CODE_STRUCT)
859 struct value *rec = allocate_value (expect_type);
861 memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
862 return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
865 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
866 && TYPE_CODE (type) == TYPE_CODE_ARRAY)
868 struct type *range_type = TYPE_INDEX_TYPE (type);
869 struct type *element_type = TYPE_TARGET_TYPE (type);
870 struct value *array = allocate_value (expect_type);
871 int element_size = TYPE_LENGTH (check_typedef (element_type));
872 LONGEST low_bound, high_bound, index;
874 if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
877 high_bound = (TYPE_LENGTH (type) / element_size) - 1;
880 memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
881 for (tem = nargs; --nargs >= 0;)
883 struct value *element;
886 element = evaluate_subexp (element_type, exp, pos, noside);
887 if (value_type (element) != element_type)
888 element = value_cast (element_type, element);
891 int continue_pc = *pos;
894 index = init_array_element (array, element, exp, pos, noside,
895 low_bound, high_bound);
900 if (index > high_bound)
901 /* To avoid memory corruption. */
902 error (_("Too many array elements"));
903 memcpy (value_contents_raw (array)
904 + (index - low_bound) * element_size,
905 value_contents (element),
913 if (expect_type != NULL_TYPE && noside != EVAL_SKIP
914 && TYPE_CODE (type) == TYPE_CODE_SET)
916 struct value *set = allocate_value (expect_type);
917 gdb_byte *valaddr = value_contents_raw (set);
918 struct type *element_type = TYPE_INDEX_TYPE (type);
919 struct type *check_type = element_type;
920 LONGEST low_bound, high_bound;
922 /* Get targettype of elementtype. */
923 while (TYPE_CODE (check_type) == TYPE_CODE_RANGE
924 || TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
925 check_type = TYPE_TARGET_TYPE (check_type);
927 if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
928 error (_("(power)set type with unknown size"));
929 memset (valaddr, '\0', TYPE_LENGTH (type));
930 for (tem = 0; tem < nargs; tem++)
932 LONGEST range_low, range_high;
933 struct type *range_low_type, *range_high_type;
934 struct value *elem_val;
936 elem_val = evaluate_subexp (element_type, exp, pos, noside);
937 range_low_type = range_high_type = value_type (elem_val);
938 range_low = range_high = value_as_long (elem_val);
940 /* Check types of elements to avoid mixture of elements from
941 different types. Also check if type of element is "compatible"
942 with element type of powerset. */
943 if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
944 range_low_type = TYPE_TARGET_TYPE (range_low_type);
945 if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
946 range_high_type = TYPE_TARGET_TYPE (range_high_type);
947 if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type))
948 || (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM
949 && (range_low_type != range_high_type)))
950 /* different element modes. */
951 error (_("POWERSET tuple elements of different mode"));
952 if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type))
953 || (TYPE_CODE (check_type) == TYPE_CODE_ENUM
954 && range_low_type != check_type))
955 error (_("incompatible POWERSET tuple elements"));
956 if (range_low > range_high)
958 warning (_("empty POWERSET tuple range"));
961 if (range_low < low_bound || range_high > high_bound)
962 error (_("POWERSET tuple element out of range"));
963 range_low -= low_bound;
964 range_high -= low_bound;
965 for (; range_low <= range_high; range_low++)
967 int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
969 if (gdbarch_bits_big_endian (exp->gdbarch))
970 bit_index = TARGET_CHAR_BIT - 1 - bit_index;
971 valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
978 argvec = (struct value **) alloca (sizeof (struct value *) * nargs);
979 for (tem = 0; tem < nargs; tem++)
981 /* Ensure that array expressions are coerced into pointer
983 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
985 if (noside == EVAL_SKIP)
987 return value_array (tem2, tem3, argvec);
991 struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
993 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
995 = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
997 if (noside == EVAL_SKIP)
999 return value_slice (array, lowbound, upper - lowbound + 1);
1003 /* Skip third and second args to evaluate the first one. */
1004 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1005 if (value_logical_not (arg1))
1007 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1008 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1012 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1013 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1017 case OP_OBJC_SELECTOR:
1018 { /* Objective C @selector operator. */
1019 char *sel = &exp->elts[pc + 2].string;
1020 int len = longest_to_int (exp->elts[pc + 1].longconst);
1021 struct type *selector_type;
1023 (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
1024 if (noside == EVAL_SKIP)
1028 sel[len] = 0; /* Make sure it's terminated. */
1030 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1031 return value_from_longest (selector_type,
1032 lookup_child_selector (exp->gdbarch, sel));
1035 case OP_OBJC_MSGCALL:
1036 { /* Objective C message (method) call. */
1038 CORE_ADDR responds_selector = 0;
1039 CORE_ADDR method_selector = 0;
1041 CORE_ADDR selector = 0;
1043 int struct_return = 0;
1044 int sub_no_side = 0;
1046 struct value *msg_send = NULL;
1047 struct value *msg_send_stret = NULL;
1048 int gnu_runtime = 0;
1050 struct value *target = NULL;
1051 struct value *method = NULL;
1052 struct value *called_method = NULL;
1054 struct type *selector_type = NULL;
1055 struct type *long_type;
1057 struct value *ret = NULL;
1060 selector = exp->elts[pc + 1].longconst;
1061 nargs = exp->elts[pc + 2].longconst;
1062 argvec = (struct value **) alloca (sizeof (struct value *)
1067 long_type = builtin_type (exp->gdbarch)->builtin_long;
1068 selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1070 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1071 sub_no_side = EVAL_NORMAL;
1073 sub_no_side = noside;
1075 target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
1077 if (value_as_long (target) == 0)
1078 return value_from_longest (long_type, 0);
1080 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1083 /* Find the method dispatch (Apple runtime) or method lookup
1084 (GNU runtime) function for Objective-C. These will be used
1085 to lookup the symbol information for the method. If we
1086 can't find any symbol information, then we'll use these to
1087 call the method, otherwise we can call the method
1088 directly. The msg_send_stret function is used in the special
1089 case of a method that returns a structure (Apple runtime
1093 struct type *type = selector_type;
1095 type = lookup_function_type (type);
1096 type = lookup_pointer_type (type);
1097 type = lookup_function_type (type);
1098 type = lookup_pointer_type (type);
1100 msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1102 = find_function_in_inferior ("objc_msg_lookup", NULL);
1104 msg_send = value_from_pointer (type, value_as_address (msg_send));
1105 msg_send_stret = value_from_pointer (type,
1106 value_as_address (msg_send_stret));
1110 msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1111 /* Special dispatcher for methods returning structs. */
1113 = find_function_in_inferior ("objc_msgSend_stret", NULL);
1116 /* Verify the target object responds to this method. The
1117 standard top-level 'Object' class uses a different name for
1118 the verification method than the non-standard, but more
1119 often used, 'NSObject' class. Make sure we check for both. */
1122 = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1123 if (responds_selector == 0)
1125 = lookup_child_selector (exp->gdbarch, "respondsTo:");
1127 if (responds_selector == 0)
1128 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1131 = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1132 if (method_selector == 0)
1134 = lookup_child_selector (exp->gdbarch, "methodFor:");
1136 if (method_selector == 0)
1137 error (_("no 'methodFor:' or 'methodForSelector:' method"));
1139 /* Call the verification method, to make sure that the target
1140 class implements the desired method. */
1142 argvec[0] = msg_send;
1144 argvec[2] = value_from_longest (long_type, responds_selector);
1145 argvec[3] = value_from_longest (long_type, selector);
1148 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1151 /* Function objc_msg_lookup returns a pointer. */
1153 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1155 if (value_as_long (ret) == 0)
1156 error (_("Target does not respond to this message selector."));
1158 /* Call "methodForSelector:" method, to get the address of a
1159 function method that implements this selector for this
1160 class. If we can find a symbol at that address, then we
1161 know the return type, parameter types etc. (that's a good
1164 argvec[0] = msg_send;
1166 argvec[2] = value_from_longest (long_type, method_selector);
1167 argvec[3] = value_from_longest (long_type, selector);
1170 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1174 ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1177 /* ret should now be the selector. */
1179 addr = value_as_long (ret);
1182 struct symbol *sym = NULL;
1184 /* The address might point to a function descriptor;
1185 resolve it to the actual code address instead. */
1186 addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1189 /* Is it a high_level symbol? */
1190 sym = find_pc_function (addr);
1192 method = value_of_variable (sym, 0);
1195 /* If we found a method with symbol information, check to see
1196 if it returns a struct. Otherwise assume it doesn't. */
1201 struct type *val_type;
1203 funaddr = find_function_addr (method, &val_type);
1205 block_for_pc (funaddr);
1207 CHECK_TYPEDEF (val_type);
1209 if ((val_type == NULL)
1210 || (TYPE_CODE(val_type) == TYPE_CODE_ERROR))
1212 if (expect_type != NULL)
1213 val_type = expect_type;
1216 struct_return = using_struct_return (exp->gdbarch, method,
1219 else if (expect_type != NULL)
1221 struct_return = using_struct_return (exp->gdbarch, NULL,
1222 check_typedef (expect_type));
1225 /* Found a function symbol. Now we will substitute its
1226 value in place of the message dispatcher (obj_msgSend),
1227 so that we call the method directly instead of thru
1228 the dispatcher. The main reason for doing this is that
1229 we can now evaluate the return value and parameter values
1230 according to their known data types, in case we need to
1231 do things like promotion, dereferencing, special handling
1232 of structs and doubles, etc.
1234 We want to use the type signature of 'method', but still
1235 jump to objc_msgSend() or objc_msgSend_stret() to better
1236 mimic the behavior of the runtime. */
1240 if (TYPE_CODE (value_type (method)) != TYPE_CODE_FUNC)
1241 error (_("method address has symbol information "
1242 "with non-function type; skipping"));
1244 /* Create a function pointer of the appropriate type, and
1245 replace its value with the value of msg_send or
1246 msg_send_stret. We must use a pointer here, as
1247 msg_send and msg_send_stret are of pointer type, and
1248 the representation may be different on systems that use
1249 function descriptors. */
1252 = value_from_pointer (lookup_pointer_type (value_type (method)),
1253 value_as_address (msg_send_stret));
1256 = value_from_pointer (lookup_pointer_type (value_type (method)),
1257 value_as_address (msg_send));
1262 called_method = msg_send_stret;
1264 called_method = msg_send;
1267 if (noside == EVAL_SKIP)
1270 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1272 /* If the return type doesn't look like a function type,
1273 call an error. This can happen if somebody tries to
1274 turn a variable into a function call. This is here
1275 because people often want to call, eg, strcmp, which
1276 gdb doesn't know is a function. If gdb isn't asked for
1277 it's opinion (ie. through "whatis"), it won't offer
1280 struct type *type = value_type (called_method);
1282 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1283 type = TYPE_TARGET_TYPE (type);
1284 type = TYPE_TARGET_TYPE (type);
1288 if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type)
1289 return allocate_value (expect_type);
1291 return allocate_value (type);
1294 error (_("Expression of type other than "
1295 "\"method returning ...\" used as a method"));
1298 /* Now depending on whether we found a symbol for the method,
1299 we will either call the runtime dispatcher or the method
1302 argvec[0] = called_method;
1304 argvec[2] = value_from_longest (long_type, selector);
1305 /* User-supplied arguments. */
1306 for (tem = 0; tem < nargs; tem++)
1307 argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
1308 argvec[tem + 3] = 0;
1310 if (gnu_runtime && (method != NULL))
1312 /* Function objc_msg_lookup returns a pointer. */
1313 deprecated_set_value_type (argvec[0],
1314 lookup_pointer_type (lookup_function_type (value_type (argvec[0]))));
1316 = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1319 ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1326 op = exp->elts[*pos].opcode;
1327 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1328 /* Allocate arg vector, including space for the function to be
1329 called in argvec[0], a potential `this', and a terminating NULL. */
1330 argvec = (struct value **)
1331 alloca (sizeof (struct value *) * (nargs + 3));
1332 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1334 /* First, evaluate the structure into arg2. */
1337 if (noside == EVAL_SKIP)
1340 if (op == STRUCTOP_MEMBER)
1342 arg2 = evaluate_subexp_for_address (exp, pos, noside);
1346 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1349 /* If the function is a virtual function, then the
1350 aggregate value (providing the structure) plays
1351 its part by providing the vtable. Otherwise,
1352 it is just along for the ride: call the function
1355 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1357 type = check_typedef (value_type (arg1));
1358 if (TYPE_CODE (type) == TYPE_CODE_METHODPTR)
1360 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1361 arg1 = value_zero (TYPE_TARGET_TYPE (type), not_lval);
1363 arg1 = cplus_method_ptr_to_value (&arg2, arg1);
1365 /* Now, say which argument to start evaluating from. */
1370 else if (TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
1372 struct type *type_ptr
1373 = lookup_pointer_type (TYPE_DOMAIN_TYPE (type));
1374 struct type *target_type_ptr
1375 = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1377 /* Now, convert these values to an address. */
1378 arg2 = value_cast (type_ptr, arg2);
1380 mem_offset = value_as_long (arg1);
1382 arg1 = value_from_pointer (target_type_ptr,
1383 value_as_long (arg2) + mem_offset);
1384 arg1 = value_ind (arg1);
1388 error (_("Non-pointer-to-member value used in pointer-to-member "
1391 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
1393 /* Hair for method invocations. */
1397 /* First, evaluate the structure into arg2. */
1399 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
1400 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
1401 if (noside == EVAL_SKIP)
1404 if (op == STRUCTOP_STRUCT)
1406 /* If v is a variable in a register, and the user types
1407 v.method (), this will produce an error, because v has
1410 A possible way around this would be to allocate a
1411 copy of the variable on the stack, copy in the
1412 contents, call the function, and copy out the
1413 contents. I.e. convert this from call by reference
1414 to call by copy-return (or whatever it's called).
1415 However, this does not work because it is not the
1416 same: the method being called could stash a copy of
1417 the address, and then future uses through that address
1418 (after the method returns) would be expected to
1419 use the variable itself, not some copy of it. */
1420 arg2 = evaluate_subexp_for_address (exp, pos, noside);
1424 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1426 /* Check to see if the operator '->' has been
1427 overloaded. If the operator has been overloaded
1428 replace arg2 with the value returned by the custom
1429 operator and continue evaluation. */
1430 while (unop_user_defined_p (op, arg2))
1432 volatile struct gdb_exception except;
1433 struct value *value = NULL;
1434 TRY_CATCH (except, RETURN_MASK_ERROR)
1436 value = value_x_unop (arg2, op, noside);
1439 if (except.reason < 0)
1441 if (except.error == NOT_FOUND_ERROR)
1444 throw_exception (except);
1449 /* Now, say which argument to start evaluating from. */
1452 else if (op == OP_SCOPE
1453 && overload_resolution
1454 && (exp->language_defn->la_language == language_cplus))
1456 /* Unpack it locally so we can properly handle overload
1462 local_tem = longest_to_int (exp->elts[pc2 + 2].longconst);
1463 (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1);
1464 type = exp->elts[pc2 + 1].type;
1465 name = &exp->elts[pc2 + 3].string;
1468 function_name = NULL;
1469 if (TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
1471 function = cp_lookup_symbol_namespace (TYPE_TAG_NAME (type),
1473 get_selected_block (0),
1475 if (function == NULL)
1476 error (_("No symbol \"%s\" in namespace \"%s\"."),
1477 name, TYPE_TAG_NAME (type));
1480 /* arg2 is left as NULL on purpose. */
1484 gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1485 || TYPE_CODE (type) == TYPE_CODE_UNION);
1486 function_name = name;
1488 /* We need a properly typed value for method lookup. For
1489 static methods arg2 is otherwise unused. */
1490 arg2 = value_zero (type, lval_memory);
1495 else if (op == OP_ADL_FUNC)
1497 /* Save the function position and move pos so that the arguments
1498 can be evaluated. */
1504 func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst);
1505 (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1);
1509 /* Non-method function call. */
1513 /* If this is a C++ function wait until overload resolution. */
1514 if (op == OP_VAR_VALUE
1515 && overload_resolution
1516 && (exp->language_defn->la_language == language_cplus))
1518 (*pos) += 4; /* Skip the evaluation of the symbol. */
1523 argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
1524 type = value_type (argvec[0]);
1525 if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1526 type = TYPE_TARGET_TYPE (type);
1527 if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
1529 for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
1531 argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type,
1539 /* Evaluate arguments (if not already done, e.g., namespace::func()
1540 and overload-resolution is off). */
1541 for (; tem <= nargs; tem++)
1543 /* Ensure that array expressions are coerced into pointer
1545 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1548 /* Signal end of arglist. */
1551 if (op == OP_ADL_FUNC)
1553 struct symbol *symp;
1556 int string_pc = save_pos1 + 3;
1558 /* Extract the function name. */
1559 name_len = longest_to_int (exp->elts[string_pc].longconst);
1560 func_name = (char *) alloca (name_len + 1);
1561 strcpy (func_name, &exp->elts[string_pc + 1].string);
1563 find_overload_match (&argvec[1], nargs, func_name,
1564 NON_METHOD, /* not method */
1565 NULL, NULL, /* pass NULL symbol since
1566 symbol is unknown */
1567 NULL, &symp, NULL, 0);
1569 /* Now fix the expression being evaluated. */
1570 exp->elts[save_pos1 + 2].symbol = symp;
1571 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1574 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
1575 || (op == OP_SCOPE && function_name != NULL))
1577 int static_memfuncp;
1580 /* Method invocation: stuff "this" as first parameter.
1581 If the method turns out to be static we undo this below. */
1586 /* Name of method from expression. */
1587 tstr = &exp->elts[pc2 + 2].string;
1590 tstr = function_name;
1592 if (overload_resolution && (exp->language_defn->la_language
1595 /* Language is C++, do some overload resolution before
1597 struct value *valp = NULL;
1599 (void) find_overload_match (&argvec[1], nargs, tstr,
1600 METHOD, /* method */
1601 &arg2, /* the object */
1603 &static_memfuncp, 0);
1605 if (op == OP_SCOPE && !static_memfuncp)
1607 /* For the time being, we don't handle this. */
1608 error (_("Call to overloaded function %s requires "
1612 argvec[1] = arg2; /* the ``this'' pointer */
1613 argvec[0] = valp; /* Use the method found after overload
1617 /* Non-C++ case -- or no overload resolution. */
1619 struct value *temp = arg2;
1621 argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1623 op == STRUCTOP_STRUCT
1624 ? "structure" : "structure pointer");
1625 /* value_struct_elt updates temp with the correct value
1626 of the ``this'' pointer if necessary, so modify argvec[1] to
1627 reflect any ``this'' changes. */
1629 = value_from_longest (lookup_pointer_type(value_type (temp)),
1630 value_address (temp)
1631 + value_embedded_offset (temp));
1632 argvec[1] = arg2; /* the ``this'' pointer */
1635 /* Take out `this' if needed. */
1636 if (static_memfuncp)
1638 argvec[1] = argvec[0];
1643 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1645 /* Pointer to member. argvec[1] is already set up. */
1648 else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL))
1650 /* Non-member function being called. */
1651 /* fn: This can only be done for C++ functions. A C-style function
1652 in a C++ program, for instance, does not have the fields that
1653 are expected here. */
1655 if (overload_resolution && (exp->language_defn->la_language
1658 /* Language is C++, do some overload resolution before
1660 struct symbol *symp;
1663 /* If a scope has been specified disable ADL. */
1667 if (op == OP_VAR_VALUE)
1668 function = exp->elts[save_pos1+2].symbol;
1670 (void) find_overload_match (&argvec[1], nargs,
1671 NULL, /* no need for name */
1672 NON_METHOD, /* not method */
1673 NULL, function, /* the function */
1674 NULL, &symp, NULL, no_adl);
1676 if (op == OP_VAR_VALUE)
1678 /* Now fix the expression being evaluated. */
1679 exp->elts[save_pos1+2].symbol = symp;
1680 argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1,
1684 argvec[0] = value_of_variable (symp, get_selected_block (0));
1688 /* Not C++, or no overload resolution allowed. */
1689 /* Nothing to be done; argvec already correctly set up. */
1694 /* It is probably a C-style function. */
1695 /* Nothing to be done; argvec already correctly set up. */
1700 if (noside == EVAL_SKIP)
1702 if (argvec[0] == NULL)
1703 error (_("Cannot evaluate function -- may be inlined"));
1704 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1706 /* If the return type doesn't look like a function type, call an
1707 error. This can happen if somebody tries to turn a variable into
1708 a function call. This is here because people often want to
1709 call, eg, strcmp, which gdb doesn't know is a function. If
1710 gdb isn't asked for it's opinion (ie. through "whatis"),
1711 it won't offer it. */
1713 struct type *ftype = value_type (argvec[0]);
1715 if (TYPE_CODE (ftype) == TYPE_CODE_INTERNAL_FUNCTION)
1717 /* We don't know anything about what the internal
1718 function might return, but we have to return
1720 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
1723 else if (TYPE_GNU_IFUNC (ftype))
1724 return allocate_value (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)));
1725 else if (TYPE_TARGET_TYPE (ftype))
1726 return allocate_value (TYPE_TARGET_TYPE (ftype));
1728 error (_("Expression of type other than "
1729 "\"Function returning ...\" used as function"));
1731 switch (TYPE_CODE (value_type (argvec[0])))
1733 case TYPE_CODE_INTERNAL_FUNCTION:
1734 return call_internal_function (exp->gdbarch, exp->language_defn,
1735 argvec[0], nargs, argvec + 1);
1736 case TYPE_CODE_XMETHOD:
1737 return call_xmethod (argvec[0], nargs, argvec + 1);
1739 return call_function_by_hand (argvec[0], nargs, argvec + 1);
1741 /* pai: FIXME save value from call_function_by_hand, then adjust
1742 pc by adjust_fn_pc if +ve. */
1744 case OP_F77_UNDETERMINED_ARGLIST:
1746 /* Remember that in F77, functions, substring ops and
1747 array subscript operations cannot be disambiguated
1748 at parse time. We have made all array subscript operations,
1749 substring operations as well as function calls come here
1750 and we now have to discover what the heck this thing actually was.
1751 If it is a function, we process just as if we got an OP_FUNCALL. */
1753 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1756 /* First determine the type code we are dealing with. */
1757 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1758 type = check_typedef (value_type (arg1));
1759 code = TYPE_CODE (type);
1761 if (code == TYPE_CODE_PTR)
1763 /* Fortran always passes variable to subroutines as pointer.
1764 So we need to look into its target type to see if it is
1765 array, string or function. If it is, we need to switch
1766 to the target value the original one points to. */
1767 struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1769 if (TYPE_CODE (target_type) == TYPE_CODE_ARRAY
1770 || TYPE_CODE (target_type) == TYPE_CODE_STRING
1771 || TYPE_CODE (target_type) == TYPE_CODE_FUNC)
1773 arg1 = value_ind (arg1);
1774 type = check_typedef (value_type (arg1));
1775 code = TYPE_CODE (type);
1781 case TYPE_CODE_ARRAY:
1782 if (exp->elts[*pos].opcode == OP_F90_RANGE)
1783 return value_f90_subarray (arg1, exp, pos, noside);
1785 goto multi_f77_subscript;
1787 case TYPE_CODE_STRING:
1788 if (exp->elts[*pos].opcode == OP_F90_RANGE)
1789 return value_f90_subarray (arg1, exp, pos, noside);
1792 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1793 return value_subscript (arg1, value_as_long (arg2));
1797 case TYPE_CODE_FUNC:
1798 /* It's a function call. */
1799 /* Allocate arg vector, including space for the function to be
1800 called in argvec[0] and a terminating NULL. */
1801 argvec = (struct value **)
1802 alloca (sizeof (struct value *) * (nargs + 2));
1805 for (; tem <= nargs; tem++)
1806 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1807 argvec[tem] = 0; /* signal end of arglist */
1811 error (_("Cannot perform substring on this type"));
1815 /* We have a complex number, There should be 2 floating
1816 point numbers that compose it. */
1818 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1819 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1821 return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
1823 case STRUCTOP_STRUCT:
1824 tem = longest_to_int (exp->elts[pc + 1].longconst);
1825 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1826 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1827 if (noside == EVAL_SKIP)
1829 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1831 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1832 arg3 = value_zero (value_type (arg3), not_lval);
1836 tem = longest_to_int (exp->elts[pc + 1].longconst);
1837 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1838 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1839 if (noside == EVAL_SKIP)
1842 /* Check to see if operator '->' has been overloaded. If so replace
1843 arg1 with the value returned by evaluating operator->(). */
1844 while (unop_user_defined_p (op, arg1))
1846 volatile struct gdb_exception except;
1847 struct value *value = NULL;
1848 TRY_CATCH (except, RETURN_MASK_ERROR)
1850 value = value_x_unop (arg1, op, noside);
1853 if (except.reason < 0)
1855 if (except.error == NOT_FOUND_ERROR)
1858 throw_exception (except);
1863 /* JYG: if print object is on we need to replace the base type
1864 with rtti type in order to continue on with successful
1865 lookup of member / method only available in the rtti type. */
1867 struct type *type = value_type (arg1);
1868 struct type *real_type;
1869 int full, top, using_enc;
1870 struct value_print_options opts;
1872 get_user_print_options (&opts);
1873 if (opts.objectprint && TYPE_TARGET_TYPE(type)
1874 && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
1876 real_type = value_rtti_indirect_type (arg1, &full, &top,
1879 arg1 = value_cast (real_type, arg1);
1883 arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1884 NULL, "structure pointer");
1885 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1886 arg3 = value_zero (value_type (arg3), not_lval);
1889 case STRUCTOP_MEMBER:
1891 if (op == STRUCTOP_MEMBER)
1892 arg1 = evaluate_subexp_for_address (exp, pos, noside);
1894 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1896 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1898 if (noside == EVAL_SKIP)
1901 type = check_typedef (value_type (arg2));
1902 switch (TYPE_CODE (type))
1904 case TYPE_CODE_METHODPTR:
1905 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1906 return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1909 arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1910 gdb_assert (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR);
1911 return value_ind (arg2);
1914 case TYPE_CODE_MEMBERPTR:
1915 /* Now, convert these values to an address. */
1916 arg1 = value_cast_pointers (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
1919 mem_offset = value_as_long (arg2);
1921 arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1922 value_as_long (arg1) + mem_offset);
1923 return value_ind (arg3);
1926 error (_("non-pointer-to-member value used "
1927 "in pointer-to-member construct"));
1931 nargs = longest_to_int (exp->elts[pc + 1].longconst);
1932 arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
1933 for (ix = 0; ix < nargs; ++ix)
1934 arg_types[ix] = exp->elts[pc + 1 + ix + 1].type;
1936 expect_type = make_params (nargs, arg_types);
1937 *(pos) += 3 + nargs;
1938 arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
1939 xfree (TYPE_FIELDS (expect_type));
1940 xfree (TYPE_MAIN_TYPE (expect_type));
1941 xfree (expect_type);
1945 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1946 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1947 if (noside == EVAL_SKIP)
1949 if (binop_user_defined_p (op, arg1, arg2))
1950 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1952 return value_concat (arg1, arg2);
1955 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1956 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1958 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1960 if (binop_user_defined_p (op, arg1, arg2))
1961 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1963 return value_assign (arg1, arg2);
1965 case BINOP_ASSIGN_MODIFY:
1967 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1968 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1969 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1971 op = exp->elts[pc + 1].opcode;
1972 if (binop_user_defined_p (op, arg1, arg2))
1973 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1974 else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1976 && is_integral_type (value_type (arg2)))
1977 arg2 = value_ptradd (arg1, value_as_long (arg2));
1978 else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1980 && is_integral_type (value_type (arg2)))
1981 arg2 = value_ptradd (arg1, - value_as_long (arg2));
1984 struct value *tmp = arg1;
1986 /* For shift and integer exponentiation operations,
1987 only promote the first argument. */
1988 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1989 && is_integral_type (value_type (arg2)))
1990 unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1992 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1994 arg2 = value_binop (tmp, arg2, op);
1996 return value_assign (arg1, arg2);
1999 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2000 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2001 if (noside == EVAL_SKIP)
2003 if (binop_user_defined_p (op, arg1, arg2))
2004 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2005 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2006 && is_integral_type (value_type (arg2)))
2007 return value_ptradd (arg1, value_as_long (arg2));
2008 else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
2009 && is_integral_type (value_type (arg1)))
2010 return value_ptradd (arg2, value_as_long (arg1));
2013 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2014 return value_binop (arg1, arg2, BINOP_ADD);
2018 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2019 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2020 if (noside == EVAL_SKIP)
2022 if (binop_user_defined_p (op, arg1, arg2))
2023 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2024 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2025 && ptrmath_type_p (exp->language_defn, value_type (arg2)))
2027 /* FIXME -- should be ptrdiff_t */
2028 type = builtin_type (exp->gdbarch)->builtin_long;
2029 return value_from_longest (type, value_ptrdiff (arg1, arg2));
2031 else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2032 && is_integral_type (value_type (arg2)))
2033 return value_ptradd (arg1, - value_as_long (arg2));
2036 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2037 return value_binop (arg1, arg2, BINOP_SUB);
2048 case BINOP_BITWISE_AND:
2049 case BINOP_BITWISE_IOR:
2050 case BINOP_BITWISE_XOR:
2051 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2052 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2053 if (noside == EVAL_SKIP)
2055 if (binop_user_defined_p (op, arg1, arg2))
2056 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2059 /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2060 fudge arg2 to avoid division-by-zero, the caller is
2061 (theoretically) only looking for the type of the result. */
2062 if (noside == EVAL_AVOID_SIDE_EFFECTS
2063 /* ??? Do we really want to test for BINOP_MOD here?
2064 The implementation of value_binop gives it a well-defined
2067 || op == BINOP_INTDIV
2070 && value_logical_not (arg2))
2072 struct value *v_one, *retval;
2074 v_one = value_one (value_type (arg2));
2075 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
2076 retval = value_binop (arg1, v_one, op);
2081 /* For shift and integer exponentiation operations,
2082 only promote the first argument. */
2083 if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2084 && is_integral_type (value_type (arg2)))
2085 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2087 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2089 return value_binop (arg1, arg2, op);
2093 case BINOP_SUBSCRIPT:
2094 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2095 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2096 if (noside == EVAL_SKIP)
2098 if (binop_user_defined_p (op, arg1, arg2))
2099 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2102 /* If the user attempts to subscript something that is not an
2103 array or pointer type (like a plain int variable for example),
2104 then report this as an error. */
2106 arg1 = coerce_ref (arg1);
2107 type = check_typedef (value_type (arg1));
2108 if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2109 && TYPE_CODE (type) != TYPE_CODE_PTR)
2111 if (TYPE_NAME (type))
2112 error (_("cannot subscript something of type `%s'"),
2115 error (_("cannot subscript requested type"));
2118 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2119 return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
2121 return value_subscript (arg1, value_as_long (arg2));
2123 case MULTI_SUBSCRIPT:
2125 nargs = longest_to_int (exp->elts[pc + 1].longconst);
2126 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2129 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2130 /* FIXME: EVAL_SKIP handling may not be correct. */
2131 if (noside == EVAL_SKIP)
2142 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
2143 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2145 /* If the user attempts to subscript something that has no target
2146 type (like a plain int variable for example), then report this
2149 type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
2152 arg1 = value_zero (type, VALUE_LVAL (arg1));
2158 error (_("cannot subscript something of type `%s'"),
2159 TYPE_NAME (value_type (arg1)));
2163 if (binop_user_defined_p (op, arg1, arg2))
2165 arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2169 arg1 = coerce_ref (arg1);
2170 type = check_typedef (value_type (arg1));
2172 switch (TYPE_CODE (type))
2175 case TYPE_CODE_ARRAY:
2176 case TYPE_CODE_STRING:
2177 arg1 = value_subscript (arg1, value_as_long (arg2));
2181 if (TYPE_NAME (type))
2182 error (_("cannot subscript something of type `%s'"),
2185 error (_("cannot subscript requested type"));
2191 multi_f77_subscript:
2193 LONGEST subscript_array[MAX_FORTRAN_DIMS];
2194 int ndimensions = 1, i;
2195 struct value *array = arg1;
2197 if (nargs > MAX_FORTRAN_DIMS)
2198 error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
2200 ndimensions = calc_f77_array_dims (type);
2202 if (nargs != ndimensions)
2203 error (_("Wrong number of subscripts"));
2205 gdb_assert (nargs > 0);
2207 /* Now that we know we have a legal array subscript expression
2208 let us actually find out where this element exists in the array. */
2210 /* Take array indices left to right. */
2211 for (i = 0; i < nargs; i++)
2213 /* Evaluate each subscript; it must be a legal integer in F77. */
2214 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2216 /* Fill in the subscript array. */
2218 subscript_array[i] = value_as_long (arg2);
2221 /* Internal type of array is arranged right to left. */
2222 for (i = nargs; i > 0; i--)
2224 struct type *array_type = check_typedef (value_type (array));
2225 LONGEST index = subscript_array[i - 1];
2227 array = value_subscripted_rvalue (array, index,
2228 f77_get_lowerbound (array_type));
2234 case BINOP_LOGICAL_AND:
2235 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2236 if (noside == EVAL_SKIP)
2238 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2243 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2246 if (binop_user_defined_p (op, arg1, arg2))
2248 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2249 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2253 tem = value_logical_not (arg1);
2254 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2255 (tem ? EVAL_SKIP : noside));
2256 type = language_bool_type (exp->language_defn, exp->gdbarch);
2257 return value_from_longest (type,
2258 (LONGEST) (!tem && !value_logical_not (arg2)));
2261 case BINOP_LOGICAL_OR:
2262 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2263 if (noside == EVAL_SKIP)
2265 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2270 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2273 if (binop_user_defined_p (op, arg1, arg2))
2275 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2276 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2280 tem = value_logical_not (arg1);
2281 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2282 (!tem ? EVAL_SKIP : noside));
2283 type = language_bool_type (exp->language_defn, exp->gdbarch);
2284 return value_from_longest (type,
2285 (LONGEST) (!tem || !value_logical_not (arg2)));
2289 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2290 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2291 if (noside == EVAL_SKIP)
2293 if (binop_user_defined_p (op, arg1, arg2))
2295 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2299 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2300 tem = value_equal (arg1, arg2);
2301 type = language_bool_type (exp->language_defn, exp->gdbarch);
2302 return value_from_longest (type, (LONGEST) tem);
2305 case BINOP_NOTEQUAL:
2306 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2307 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2308 if (noside == EVAL_SKIP)
2310 if (binop_user_defined_p (op, arg1, arg2))
2312 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2316 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2317 tem = value_equal (arg1, arg2);
2318 type = language_bool_type (exp->language_defn, exp->gdbarch);
2319 return value_from_longest (type, (LONGEST) ! tem);
2323 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2324 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2325 if (noside == EVAL_SKIP)
2327 if (binop_user_defined_p (op, arg1, arg2))
2329 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2333 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2334 tem = value_less (arg1, arg2);
2335 type = language_bool_type (exp->language_defn, exp->gdbarch);
2336 return value_from_longest (type, (LONGEST) tem);
2340 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2341 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2342 if (noside == EVAL_SKIP)
2344 if (binop_user_defined_p (op, arg1, arg2))
2346 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2350 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2351 tem = value_less (arg2, arg1);
2352 type = language_bool_type (exp->language_defn, exp->gdbarch);
2353 return value_from_longest (type, (LONGEST) tem);
2357 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2358 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2359 if (noside == EVAL_SKIP)
2361 if (binop_user_defined_p (op, arg1, arg2))
2363 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2367 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2368 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2369 type = language_bool_type (exp->language_defn, exp->gdbarch);
2370 return value_from_longest (type, (LONGEST) tem);
2374 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2375 arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2376 if (noside == EVAL_SKIP)
2378 if (binop_user_defined_p (op, arg1, arg2))
2380 return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2384 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2385 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2386 type = language_bool_type (exp->language_defn, exp->gdbarch);
2387 return value_from_longest (type, (LONGEST) tem);
2391 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2392 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2393 if (noside == EVAL_SKIP)
2395 type = check_typedef (value_type (arg2));
2396 if (TYPE_CODE (type) != TYPE_CODE_INT)
2397 error (_("Non-integral right operand for \"@\" operator."));
2398 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2400 return allocate_repeat_value (value_type (arg1),
2401 longest_to_int (value_as_long (arg2)));
2404 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2407 evaluate_subexp (NULL_TYPE, exp, pos, noside);
2408 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2411 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2412 if (noside == EVAL_SKIP)
2414 if (unop_user_defined_p (op, arg1))
2415 return value_x_unop (arg1, op, noside);
2418 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2419 return value_pos (arg1);
2423 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2424 if (noside == EVAL_SKIP)
2426 if (unop_user_defined_p (op, arg1))
2427 return value_x_unop (arg1, op, noside);
2430 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2431 return value_neg (arg1);
2434 case UNOP_COMPLEMENT:
2435 /* C++: check for and handle destructor names. */
2436 op = exp->elts[*pos].opcode;
2438 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2439 if (noside == EVAL_SKIP)
2441 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2442 return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2445 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2446 return value_complement (arg1);
2449 case UNOP_LOGICAL_NOT:
2450 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2451 if (noside == EVAL_SKIP)
2453 if (unop_user_defined_p (op, arg1))
2454 return value_x_unop (arg1, op, noside);
2457 type = language_bool_type (exp->language_defn, exp->gdbarch);
2458 return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2462 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
2463 expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2464 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2465 type = check_typedef (value_type (arg1));
2466 if (TYPE_CODE (type) == TYPE_CODE_METHODPTR
2467 || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
2468 error (_("Attempt to dereference pointer "
2469 "to member without an object"));
2470 if (noside == EVAL_SKIP)
2472 if (unop_user_defined_p (op, arg1))
2473 return value_x_unop (arg1, op, noside);
2474 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2476 type = check_typedef (value_type (arg1));
2477 if (TYPE_CODE (type) == TYPE_CODE_PTR
2478 || TYPE_CODE (type) == TYPE_CODE_REF
2479 /* In C you can dereference an array to get the 1st elt. */
2480 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2482 return value_zero (TYPE_TARGET_TYPE (type),
2484 else if (TYPE_CODE (type) == TYPE_CODE_INT)
2485 /* GDB allows dereferencing an int. */
2486 return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2489 error (_("Attempt to take contents of a non-pointer value."));
2492 /* Allow * on an integer so we can cast it to whatever we want.
2493 This returns an int, which seems like the most C-like thing to
2494 do. "long long" variables are rare enough that
2495 BUILTIN_TYPE_LONGEST would seem to be a mistake. */
2496 if (TYPE_CODE (type) == TYPE_CODE_INT)
2497 return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2498 (CORE_ADDR) value_as_address (arg1));
2499 return value_ind (arg1);
2502 /* C++: check for and handle pointer to members. */
2504 op = exp->elts[*pos].opcode;
2506 if (noside == EVAL_SKIP)
2508 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2513 struct value *retvalp = evaluate_subexp_for_address (exp, pos,
2520 if (noside == EVAL_SKIP)
2522 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2525 return evaluate_subexp_for_sizeof (exp, pos, noside);
2529 type = exp->elts[pc + 1].type;
2530 arg1 = evaluate_subexp (type, exp, pos, noside);
2531 if (noside == EVAL_SKIP)
2533 if (type != value_type (arg1))
2534 arg1 = value_cast (type, arg1);
2537 case UNOP_CAST_TYPE:
2538 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2539 type = value_type (arg1);
2540 arg1 = evaluate_subexp (type, exp, pos, noside);
2541 if (noside == EVAL_SKIP)
2543 if (type != value_type (arg1))
2544 arg1 = value_cast (type, arg1);
2547 case UNOP_DYNAMIC_CAST:
2548 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2549 type = value_type (arg1);
2550 arg1 = evaluate_subexp (type, exp, pos, noside);
2551 if (noside == EVAL_SKIP)
2553 return value_dynamic_cast (type, arg1);
2555 case UNOP_REINTERPRET_CAST:
2556 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2557 type = value_type (arg1);
2558 arg1 = evaluate_subexp (type, exp, pos, noside);
2559 if (noside == EVAL_SKIP)
2561 return value_reinterpret_cast (type, arg1);
2565 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2566 if (noside == EVAL_SKIP)
2568 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2569 return value_zero (exp->elts[pc + 1].type, lval_memory);
2571 return value_at_lazy (exp->elts[pc + 1].type,
2572 value_as_address (arg1));
2574 case UNOP_MEMVAL_TYPE:
2575 arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2576 type = value_type (arg1);
2577 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2578 if (noside == EVAL_SKIP)
2580 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2581 return value_zero (type, lval_memory);
2583 return value_at_lazy (type, value_as_address (arg1));
2585 case UNOP_MEMVAL_TLS:
2587 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2588 if (noside == EVAL_SKIP)
2590 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2591 return value_zero (exp->elts[pc + 2].type, lval_memory);
2596 tls_addr = target_translate_tls_address (exp->elts[pc + 1].objfile,
2597 value_as_address (arg1));
2598 return value_at_lazy (exp->elts[pc + 2].type, tls_addr);
2601 case UNOP_PREINCREMENT:
2602 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2603 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2605 else if (unop_user_defined_p (op, arg1))
2607 return value_x_unop (arg1, op, noside);
2611 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2612 arg2 = value_ptradd (arg1, 1);
2615 struct value *tmp = arg1;
2617 arg2 = value_one (value_type (arg1));
2618 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2619 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2622 return value_assign (arg1, arg2);
2625 case UNOP_PREDECREMENT:
2626 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2627 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2629 else if (unop_user_defined_p (op, arg1))
2631 return value_x_unop (arg1, op, noside);
2635 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2636 arg2 = value_ptradd (arg1, -1);
2639 struct value *tmp = arg1;
2641 arg2 = value_one (value_type (arg1));
2642 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2643 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2646 return value_assign (arg1, arg2);
2649 case UNOP_POSTINCREMENT:
2650 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2651 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2653 else if (unop_user_defined_p (op, arg1))
2655 return value_x_unop (arg1, op, noside);
2659 arg3 = value_non_lval (arg1);
2661 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2662 arg2 = value_ptradd (arg1, 1);
2665 struct value *tmp = arg1;
2667 arg2 = value_one (value_type (arg1));
2668 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2669 arg2 = value_binop (tmp, arg2, BINOP_ADD);
2672 value_assign (arg1, arg2);
2676 case UNOP_POSTDECREMENT:
2677 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2678 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2680 else if (unop_user_defined_p (op, arg1))
2682 return value_x_unop (arg1, op, noside);
2686 arg3 = value_non_lval (arg1);
2688 if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2689 arg2 = value_ptradd (arg1, -1);
2692 struct value *tmp = arg1;
2694 arg2 = value_one (value_type (arg1));
2695 binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2696 arg2 = value_binop (tmp, arg2, BINOP_SUB);
2699 value_assign (arg1, arg2);
2705 return value_of_this (exp->language_defn);
2708 /* The value is not supposed to be used. This is here to make it
2709 easier to accommodate expressions that contain types. */
2711 if (noside == EVAL_SKIP)
2713 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2715 struct type *type = exp->elts[pc + 1].type;
2717 /* If this is a typedef, then find its immediate target. We
2718 use check_typedef to resolve stubs, but we ignore its
2719 result because we do not want to dig past all
2721 check_typedef (type);
2722 if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2723 type = TYPE_TARGET_TYPE (type);
2724 return allocate_value (type);
2727 error (_("Attempt to use a type name as an expression"));
2731 if (noside == EVAL_SKIP)
2733 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2736 else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2738 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2739 struct value *result;
2741 result = evaluate_subexp (NULL_TYPE, exp, pos,
2742 EVAL_AVOID_SIDE_EFFECTS);
2744 /* 'decltype' has special semantics for lvalues. */
2745 if (op == OP_DECLTYPE
2746 && (sub_op == BINOP_SUBSCRIPT
2747 || sub_op == STRUCTOP_MEMBER
2748 || sub_op == STRUCTOP_MPTR
2749 || sub_op == UNOP_IND
2750 || sub_op == STRUCTOP_STRUCT
2751 || sub_op == STRUCTOP_PTR
2752 || sub_op == OP_SCOPE))
2754 struct type *type = value_type (result);
2756 if (TYPE_CODE (check_typedef (type)) != TYPE_CODE_REF)
2758 type = lookup_reference_type (type);
2759 result = allocate_value (type);
2766 error (_("Attempt to use a type as an expression"));
2770 struct value *result;
2771 enum exp_opcode sub_op = exp->elts[*pos].opcode;
2773 if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2774 result = evaluate_subexp (NULL_TYPE, exp, pos,
2775 EVAL_AVOID_SIDE_EFFECTS);
2777 result = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2779 if (noside != EVAL_NORMAL)
2780 return allocate_value (cplus_typeid_type (exp->gdbarch));
2782 return cplus_typeid (result);
2786 /* Removing this case and compiling with gcc -Wall reveals that
2787 a lot of cases are hitting this case. Some of these should
2788 probably be removed from expression.h; others are legitimate
2789 expressions which are (apparently) not fully implemented.
2791 If there are any cases landing here which mean a user error,
2792 then they should be separate cases, with more descriptive
2795 error (_("GDB does not (yet) know how to "
2796 "evaluate that kind of expression"));
2800 return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
2803 /* Evaluate a subexpression of EXP, at index *POS,
2804 and return the address of that subexpression.
2805 Advance *POS over the subexpression.
2806 If the subexpression isn't an lvalue, get an error.
2807 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2808 then only the type of the result need be correct. */
2810 static struct value *
2811 evaluate_subexp_for_address (struct expression *exp, int *pos,
2821 op = exp->elts[pc].opcode;
2827 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2829 /* We can't optimize out "&*" if there's a user-defined operator*. */
2830 if (unop_user_defined_p (op, x))
2832 x = value_x_unop (x, op, noside);
2833 goto default_case_after_eval;
2836 return coerce_array (x);
2840 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2841 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2843 case UNOP_MEMVAL_TYPE:
2848 x = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2849 type = value_type (x);
2850 return value_cast (lookup_pointer_type (type),
2851 evaluate_subexp (NULL_TYPE, exp, pos, noside));
2855 var = exp->elts[pc + 2].symbol;
2857 /* C++: The "address" of a reference should yield the address
2858 * of the object pointed to. Let value_addr() deal with it. */
2859 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
2863 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2866 lookup_pointer_type (SYMBOL_TYPE (var));
2867 enum address_class sym_class = SYMBOL_CLASS (var);
2869 if (sym_class == LOC_CONST
2870 || sym_class == LOC_CONST_BYTES
2871 || sym_class == LOC_REGISTER)
2872 error (_("Attempt to take address of register or constant."));
2875 value_zero (type, not_lval);
2878 return address_of_variable (var, exp->elts[pc + 1].block);
2881 tem = longest_to_int (exp->elts[pc + 2].longconst);
2882 (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
2883 x = value_aggregate_elt (exp->elts[pc + 1].type,
2884 &exp->elts[pc + 3].string,
2887 error (_("There is no field named %s"), &exp->elts[pc + 3].string);
2892 x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2893 default_case_after_eval:
2894 if (noside == EVAL_AVOID_SIDE_EFFECTS)
2896 struct type *type = check_typedef (value_type (x));
2898 if (TYPE_CODE (type) == TYPE_CODE_REF)
2899 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2901 else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2902 return value_zero (lookup_pointer_type (value_type (x)),
2905 error (_("Attempt to take address of "
2906 "value not located in memory."));
2908 return value_addr (x);
2912 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2913 When used in contexts where arrays will be coerced anyway, this is
2914 equivalent to `evaluate_subexp' but much faster because it avoids
2915 actually fetching array contents (perhaps obsolete now that we have
2918 Note that we currently only do the coercion for C expressions, where
2919 arrays are zero based and the coercion is correct. For other languages,
2920 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2921 to decide if coercion is appropriate. */
2924 evaluate_subexp_with_coercion (struct expression *exp,
2925 int *pos, enum noside noside)
2934 op = exp->elts[pc].opcode;
2939 var = exp->elts[pc + 2].symbol;
2940 type = check_typedef (SYMBOL_TYPE (var));
2941 if (TYPE_CODE (type) == TYPE_CODE_ARRAY
2942 && !TYPE_VECTOR (type)
2943 && CAST_IS_CONVERSION (exp->language_defn))
2946 val = address_of_variable (var, exp->elts[pc + 1].block);
2947 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2953 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2957 /* Evaluate a subexpression of EXP, at index *POS,
2958 and return a value for the size of that subexpression.
2959 Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
2960 we allow side-effects on the operand if its type is a variable
2963 static struct value *
2964 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
2967 /* FIXME: This should be size_t. */
2968 struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2975 op = exp->elts[pc].opcode;
2979 /* This case is handled specially
2980 so that we avoid creating a value for the result type.
2981 If the result type is very big, it's desirable not to
2982 create a value unnecessarily. */
2985 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2986 type = check_typedef (value_type (val));
2987 if (TYPE_CODE (type) != TYPE_CODE_PTR
2988 && TYPE_CODE (type) != TYPE_CODE_REF
2989 && TYPE_CODE (type) != TYPE_CODE_ARRAY)
2990 error (_("Attempt to take contents of a non-pointer value."));
2991 type = TYPE_TARGET_TYPE (type);
2992 if (is_dynamic_type (type))
2993 type = value_type (value_ind (val));
2994 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2998 type = exp->elts[pc + 1].type;
3001 case UNOP_MEMVAL_TYPE:
3003 val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3004 type = value_type (val);
3008 type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
3009 if (is_dynamic_type (type))
3011 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3012 type = value_type (val);
3018 /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3019 type of the subscript is a variable length array type. In this case we
3020 must re-evaluate the right hand side of the subcription to allow
3022 case BINOP_SUBSCRIPT:
3023 if (noside == EVAL_NORMAL)
3025 int pc = (*pos) + 1;
3027 val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
3028 type = check_typedef (value_type (val));
3029 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3031 type = check_typedef (TYPE_TARGET_TYPE (type));
3032 if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3034 type = TYPE_INDEX_TYPE (type);
3035 /* Only re-evaluate the right hand side if the resulting type
3036 is a variable length type. */
3037 if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
3039 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3040 return value_from_longest
3041 (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
3050 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3051 type = value_type (val);
3055 /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3056 "When applied to a reference or a reference type, the result is
3057 the size of the referenced type." */
3058 CHECK_TYPEDEF (type);
3059 if (exp->language_defn->la_language == language_cplus
3060 && TYPE_CODE (type) == TYPE_CODE_REF)
3061 type = check_typedef (TYPE_TARGET_TYPE (type));
3062 return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3065 /* Parse a type expression in the string [P..P+LENGTH). */
3068 parse_and_eval_type (char *p, int length)
3070 char *tmp = (char *) alloca (length + 4);
3071 struct expression *expr;
3074 memcpy (tmp + 1, p, length);
3075 tmp[length + 1] = ')';
3076 tmp[length + 2] = '0';
3077 tmp[length + 3] = '\0';
3078 expr = parse_expression (tmp);
3079 if (expr->elts[0].opcode != UNOP_CAST)
3080 error (_("Internal error in eval_type."));
3081 return expr->elts[1].type;
3085 calc_f77_array_dims (struct type *array_type)
3088 struct type *tmp_type;
3090 if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
3091 error (_("Can't get dimensions for a non-array type"));
3093 tmp_type = array_type;
3095 while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
3097 if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)