1 /* Evaluate expressions for GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2007
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
25 #include "gdb_string.h"
29 #include "expression.h"
32 #include "language.h" /* For CAST_IS_CONVERSION */
33 #include "f-lang.h" /* for array bound stuff */
36 #include "objc-lang.h"
38 #include "parser-defs.h"
39 #include "cp-support.h"
41 #include "exceptions.h"
44 #include "gdb_assert.h"
46 /* This is defined in valops.c */
47 extern int overload_resolution
;
49 /* JYG: lookup rtti type of STRUCTOP_PTR when this is set to continue
50 on with successful lookup for member/method of the rtti type. */
51 extern int objectprint
;
53 /* Prototypes for local functions. */
55 static struct value
*evaluate_subexp_for_sizeof (struct expression
*, int *);
57 static struct value
*evaluate_subexp_for_address (struct expression
*,
60 static struct value
*evaluate_subexp (struct type
*, struct expression
*,
63 static char *get_label (struct expression
*, int *);
65 static struct value
*evaluate_struct_tuple (struct value
*,
66 struct expression
*, int *,
69 static LONGEST
init_array_element (struct value
*, struct value
*,
70 struct expression
*, int *, enum noside
,
74 evaluate_subexp (struct type
*expect_type
, struct expression
*exp
,
75 int *pos
, enum noside noside
)
77 return (*exp
->language_defn
->la_exp_desc
->evaluate_exp
)
78 (expect_type
, exp
, pos
, noside
);
81 /* Parse the string EXP as a C expression, evaluate it,
82 and return the result as a number. */
85 parse_and_eval_address (char *exp
)
87 struct expression
*expr
= parse_expression (exp
);
89 struct cleanup
*old_chain
=
90 make_cleanup (free_current_contents
, &expr
);
92 addr
= value_as_address (evaluate_expression (expr
));
93 do_cleanups (old_chain
);
97 /* Like parse_and_eval_address but takes a pointer to a char * variable
98 and advanced that variable across the characters parsed. */
101 parse_and_eval_address_1 (char **expptr
)
103 struct expression
*expr
= parse_exp_1 (expptr
, (struct block
*) 0, 0);
105 struct cleanup
*old_chain
=
106 make_cleanup (free_current_contents
, &expr
);
108 addr
= value_as_address (evaluate_expression (expr
));
109 do_cleanups (old_chain
);
113 /* Like parse_and_eval_address, but treats the value of the expression
114 as an integer, not an address, returns a LONGEST, not a CORE_ADDR */
116 parse_and_eval_long (char *exp
)
118 struct expression
*expr
= parse_expression (exp
);
120 struct cleanup
*old_chain
=
121 make_cleanup (free_current_contents
, &expr
);
123 retval
= value_as_long (evaluate_expression (expr
));
124 do_cleanups (old_chain
);
129 parse_and_eval (char *exp
)
131 struct expression
*expr
= parse_expression (exp
);
133 struct cleanup
*old_chain
=
134 make_cleanup (free_current_contents
, &expr
);
136 val
= evaluate_expression (expr
);
137 do_cleanups (old_chain
);
141 /* Parse up to a comma (or to a closeparen)
142 in the string EXPP as an expression, evaluate it, and return the value.
143 EXPP is advanced to point to the comma. */
146 parse_to_comma_and_eval (char **expp
)
148 struct expression
*expr
= parse_exp_1 (expp
, (struct block
*) 0, 1);
150 struct cleanup
*old_chain
=
151 make_cleanup (free_current_contents
, &expr
);
153 val
= evaluate_expression (expr
);
154 do_cleanups (old_chain
);
158 /* Evaluate an expression in internal prefix form
159 such as is constructed by parse.y.
161 See expression.h for info on the format of an expression. */
164 evaluate_expression (struct expression
*exp
)
167 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_NORMAL
);
170 /* Evaluate an expression, avoiding all memory references
171 and getting a value whose type alone is correct. */
174 evaluate_type (struct expression
*exp
)
177 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
180 /* If the next expression is an OP_LABELED, skips past it,
181 returning the label. Otherwise, does nothing and returns NULL. */
184 get_label (struct expression
*exp
, int *pos
)
186 if (exp
->elts
[*pos
].opcode
== OP_LABELED
)
189 char *name
= &exp
->elts
[pc
+ 2].string
;
190 int tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
191 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
198 /* This function evaluates tuples (in (the deleted) Chill) or
199 brace-initializers (in C/C++) for structure types. */
201 static struct value
*
202 evaluate_struct_tuple (struct value
*struct_val
,
203 struct expression
*exp
,
204 int *pos
, enum noside noside
, int nargs
)
206 struct type
*struct_type
= check_typedef (value_type (struct_val
));
207 struct type
*substruct_type
= struct_type
;
208 struct type
*field_type
;
215 struct value
*val
= NULL
;
220 /* Skip past the labels, and count them. */
221 while (get_label (exp
, pos
) != NULL
)
226 char *label
= get_label (exp
, &pc
);
229 for (fieldno
= 0; fieldno
< TYPE_NFIELDS (struct_type
);
232 char *field_name
= TYPE_FIELD_NAME (struct_type
, fieldno
);
233 if (field_name
!= NULL
&& strcmp (field_name
, label
) == 0)
236 subfieldno
= fieldno
;
237 substruct_type
= struct_type
;
241 for (fieldno
= 0; fieldno
< TYPE_NFIELDS (struct_type
);
244 char *field_name
= TYPE_FIELD_NAME (struct_type
, fieldno
);
245 field_type
= TYPE_FIELD_TYPE (struct_type
, fieldno
);
246 if ((field_name
== 0 || *field_name
== '\0')
247 && TYPE_CODE (field_type
) == TYPE_CODE_UNION
)
250 for (; variantno
< TYPE_NFIELDS (field_type
);
254 = TYPE_FIELD_TYPE (field_type
, variantno
);
255 if (TYPE_CODE (substruct_type
) == TYPE_CODE_STRUCT
)
258 subfieldno
< TYPE_NFIELDS (substruct_type
);
261 if (strcmp(TYPE_FIELD_NAME (substruct_type
,
272 error (_("there is no field named %s"), label
);
278 /* Unlabelled tuple element - go to next field. */
282 if (subfieldno
>= TYPE_NFIELDS (substruct_type
))
285 substruct_type
= struct_type
;
291 /* Skip static fields. */
292 while (fieldno
< TYPE_NFIELDS (struct_type
)
293 && TYPE_FIELD_STATIC_KIND (struct_type
, fieldno
))
295 subfieldno
= fieldno
;
296 if (fieldno
>= TYPE_NFIELDS (struct_type
))
297 error (_("too many initializers"));
298 field_type
= TYPE_FIELD_TYPE (struct_type
, fieldno
);
299 if (TYPE_CODE (field_type
) == TYPE_CODE_UNION
300 && TYPE_FIELD_NAME (struct_type
, fieldno
)[0] == '0')
301 error (_("don't know which variant you want to set"));
305 /* Here, struct_type is the type of the inner struct,
306 while substruct_type is the type of the inner struct.
307 These are the same for normal structures, but a variant struct
308 contains anonymous union fields that contain substruct fields.
309 The value fieldno is the index of the top-level (normal or
310 anonymous union) field in struct_field, while the value
311 subfieldno is the index of the actual real (named inner) field
312 in substruct_type. */
314 field_type
= TYPE_FIELD_TYPE (substruct_type
, subfieldno
);
316 val
= evaluate_subexp (field_type
, exp
, pos
, noside
);
318 /* Now actually set the field in struct_val. */
320 /* Assign val to field fieldno. */
321 if (value_type (val
) != field_type
)
322 val
= value_cast (field_type
, val
);
324 bitsize
= TYPE_FIELD_BITSIZE (substruct_type
, subfieldno
);
325 bitpos
= TYPE_FIELD_BITPOS (struct_type
, fieldno
);
327 bitpos
+= TYPE_FIELD_BITPOS (substruct_type
, subfieldno
);
328 addr
= value_contents_writeable (struct_val
) + bitpos
/ 8;
330 modify_field (addr
, value_as_long (val
),
331 bitpos
% 8, bitsize
);
333 memcpy (addr
, value_contents (val
),
334 TYPE_LENGTH (value_type (val
)));
336 while (--nlabels
> 0);
341 /* Recursive helper function for setting elements of array tuples for
342 (the deleted) Chill. The target is ARRAY (which has bounds
343 LOW_BOUND to HIGH_BOUND); the element value is ELEMENT; EXP, POS
344 and NOSIDE are as usual. Evaluates index expresions and sets the
345 specified element(s) of ARRAY to ELEMENT. Returns last index
349 init_array_element (struct value
*array
, struct value
*element
,
350 struct expression
*exp
, int *pos
,
351 enum noside noside
, LONGEST low_bound
, LONGEST high_bound
)
354 int element_size
= TYPE_LENGTH (value_type (element
));
355 if (exp
->elts
[*pos
].opcode
== BINOP_COMMA
)
358 init_array_element (array
, element
, exp
, pos
, noside
,
359 low_bound
, high_bound
);
360 return init_array_element (array
, element
,
361 exp
, pos
, noside
, low_bound
, high_bound
);
363 else if (exp
->elts
[*pos
].opcode
== BINOP_RANGE
)
367 low
= value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
368 high
= value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
369 if (low
< low_bound
|| high
> high_bound
)
370 error (_("tuple range index out of range"));
371 for (index
= low
; index
<= high
; index
++)
373 memcpy (value_contents_raw (array
)
374 + (index
- low_bound
) * element_size
,
375 value_contents (element
), element_size
);
380 index
= value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
381 if (index
< low_bound
|| index
> high_bound
)
382 error (_("tuple index out of range"));
383 memcpy (value_contents_raw (array
) + (index
- low_bound
) * element_size
,
384 value_contents (element
), element_size
);
390 value_f90_subarray (struct value
*array
,
391 struct expression
*exp
, int *pos
, enum noside noside
)
394 LONGEST low_bound
, high_bound
;
395 struct type
*range
= check_typedef (TYPE_INDEX_TYPE (value_type (array
)));
396 enum f90_range_type range_type
= longest_to_int (exp
->elts
[pc
].longconst
);
400 if (range_type
== LOW_BOUND_DEFAULT
|| range_type
== BOTH_BOUND_DEFAULT
)
401 low_bound
= TYPE_LOW_BOUND (range
);
403 low_bound
= value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
405 if (range_type
== HIGH_BOUND_DEFAULT
|| range_type
== BOTH_BOUND_DEFAULT
)
406 high_bound
= TYPE_HIGH_BOUND (range
);
408 high_bound
= value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
410 return value_slice (array
, low_bound
, high_bound
- low_bound
+ 1);
414 evaluate_subexp_standard (struct type
*expect_type
,
415 struct expression
*exp
, int *pos
,
420 int pc
, pc2
= 0, oldpos
;
421 struct value
*arg1
= NULL
;
422 struct value
*arg2
= NULL
;
426 struct value
**argvec
;
427 int upper
, lower
, retcode
;
431 struct type
**arg_types
;
435 op
= exp
->elts
[pc
].opcode
;
440 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
441 (*pos
) += 4 + BYTES_TO_EXP_ELEM (tem
+ 1);
442 if (noside
== EVAL_SKIP
)
444 arg1
= value_aggregate_elt (exp
->elts
[pc
+ 1].type
,
445 &exp
->elts
[pc
+ 3].string
,
448 error (_("There is no field named %s"), &exp
->elts
[pc
+ 3].string
);
453 return value_from_longest (exp
->elts
[pc
+ 1].type
,
454 exp
->elts
[pc
+ 2].longconst
);
458 return value_from_double (exp
->elts
[pc
+ 1].type
,
459 exp
->elts
[pc
+ 2].doubleconst
);
463 if (noside
== EVAL_SKIP
)
466 /* JYG: We used to just return value_zero of the symbol type
467 if we're asked to avoid side effects. Otherwise we return
468 value_of_variable (...). However I'm not sure if
469 value_of_variable () has any side effect.
470 We need a full value object returned here for whatis_exp ()
471 to call evaluate_type () and then pass the full value to
472 value_rtti_target_type () if we are dealing with a pointer
473 or reference to a base class and print object is on. */
476 volatile struct gdb_exception except
;
477 struct value
*ret
= NULL
;
479 TRY_CATCH (except
, RETURN_MASK_ERROR
)
481 ret
= value_of_variable (exp
->elts
[pc
+ 2].symbol
,
482 exp
->elts
[pc
+ 1].block
);
485 if (except
.reason
< 0)
487 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
488 ret
= value_zero (SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
), not_lval
);
490 throw_exception (except
);
499 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
503 int regno
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
506 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
507 val
= value_zero (register_type (current_gdbarch
, regno
), not_lval
);
509 val
= value_of_register (regno
, get_selected_frame (NULL
));
511 error (_("Value of register %s not available."),
512 frame_map_regnum_to_name (get_selected_frame (NULL
), regno
));
518 return value_from_longest (LA_BOOL_TYPE
,
519 exp
->elts
[pc
+ 1].longconst
);
523 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
526 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
527 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
528 if (noside
== EVAL_SKIP
)
530 return value_string (&exp
->elts
[pc
+ 2].string
, tem
);
532 case OP_OBJC_NSSTRING
: /* Objective C Foundation Class NSString constant. */
533 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
534 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
535 if (noside
== EVAL_SKIP
)
539 return (struct value
*) value_nsstring (&exp
->elts
[pc
+ 2].string
, tem
+ 1);
542 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
544 += 3 + BYTES_TO_EXP_ELEM ((tem
+ HOST_CHAR_BIT
- 1) / HOST_CHAR_BIT
);
545 if (noside
== EVAL_SKIP
)
547 return value_bitstring (&exp
->elts
[pc
+ 2].string
, tem
);
552 tem2
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
553 tem3
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
554 nargs
= tem3
- tem2
+ 1;
555 type
= expect_type
? check_typedef (expect_type
) : NULL_TYPE
;
557 if (expect_type
!= NULL_TYPE
&& noside
!= EVAL_SKIP
558 && TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
560 struct value
*rec
= allocate_value (expect_type
);
561 memset (value_contents_raw (rec
), '\0', TYPE_LENGTH (type
));
562 return evaluate_struct_tuple (rec
, exp
, pos
, noside
, nargs
);
565 if (expect_type
!= NULL_TYPE
&& noside
!= EVAL_SKIP
566 && TYPE_CODE (type
) == TYPE_CODE_ARRAY
)
568 struct type
*range_type
= TYPE_FIELD_TYPE (type
, 0);
569 struct type
*element_type
= TYPE_TARGET_TYPE (type
);
570 struct value
*array
= allocate_value (expect_type
);
571 int element_size
= TYPE_LENGTH (check_typedef (element_type
));
572 LONGEST low_bound
, high_bound
, index
;
573 if (get_discrete_bounds (range_type
, &low_bound
, &high_bound
) < 0)
576 high_bound
= (TYPE_LENGTH (type
) / element_size
) - 1;
579 memset (value_contents_raw (array
), 0, TYPE_LENGTH (expect_type
));
580 for (tem
= nargs
; --nargs
>= 0;)
582 struct value
*element
;
584 if (exp
->elts
[*pos
].opcode
== BINOP_RANGE
)
587 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
589 element
= evaluate_subexp (element_type
, exp
, pos
, noside
);
590 if (value_type (element
) != element_type
)
591 element
= value_cast (element_type
, element
);
594 int continue_pc
= *pos
;
596 index
= init_array_element (array
, element
, exp
, pos
, noside
,
597 low_bound
, high_bound
);
602 if (index
> high_bound
)
603 /* to avoid memory corruption */
604 error (_("Too many array elements"));
605 memcpy (value_contents_raw (array
)
606 + (index
- low_bound
) * element_size
,
607 value_contents (element
),
615 if (expect_type
!= NULL_TYPE
&& noside
!= EVAL_SKIP
616 && TYPE_CODE (type
) == TYPE_CODE_SET
)
618 struct value
*set
= allocate_value (expect_type
);
619 gdb_byte
*valaddr
= value_contents_raw (set
);
620 struct type
*element_type
= TYPE_INDEX_TYPE (type
);
621 struct type
*check_type
= element_type
;
622 LONGEST low_bound
, high_bound
;
624 /* get targettype of elementtype */
625 while (TYPE_CODE (check_type
) == TYPE_CODE_RANGE
||
626 TYPE_CODE (check_type
) == TYPE_CODE_TYPEDEF
)
627 check_type
= TYPE_TARGET_TYPE (check_type
);
629 if (get_discrete_bounds (element_type
, &low_bound
, &high_bound
) < 0)
630 error (_("(power)set type with unknown size"));
631 memset (valaddr
, '\0', TYPE_LENGTH (type
));
632 for (tem
= 0; tem
< nargs
; tem
++)
634 LONGEST range_low
, range_high
;
635 struct type
*range_low_type
, *range_high_type
;
636 struct value
*elem_val
;
637 if (exp
->elts
[*pos
].opcode
== BINOP_RANGE
)
640 elem_val
= evaluate_subexp (element_type
, exp
, pos
, noside
);
641 range_low_type
= value_type (elem_val
);
642 range_low
= value_as_long (elem_val
);
643 elem_val
= evaluate_subexp (element_type
, exp
, pos
, noside
);
644 range_high_type
= value_type (elem_val
);
645 range_high
= value_as_long (elem_val
);
649 elem_val
= evaluate_subexp (element_type
, exp
, pos
, noside
);
650 range_low_type
= range_high_type
= value_type (elem_val
);
651 range_low
= range_high
= value_as_long (elem_val
);
653 /* check types of elements to avoid mixture of elements from
654 different types. Also check if type of element is "compatible"
655 with element type of powerset */
656 if (TYPE_CODE (range_low_type
) == TYPE_CODE_RANGE
)
657 range_low_type
= TYPE_TARGET_TYPE (range_low_type
);
658 if (TYPE_CODE (range_high_type
) == TYPE_CODE_RANGE
)
659 range_high_type
= TYPE_TARGET_TYPE (range_high_type
);
660 if ((TYPE_CODE (range_low_type
) != TYPE_CODE (range_high_type
)) ||
661 (TYPE_CODE (range_low_type
) == TYPE_CODE_ENUM
&&
662 (range_low_type
!= range_high_type
)))
663 /* different element modes */
664 error (_("POWERSET tuple elements of different mode"));
665 if ((TYPE_CODE (check_type
) != TYPE_CODE (range_low_type
)) ||
666 (TYPE_CODE (check_type
) == TYPE_CODE_ENUM
&&
667 range_low_type
!= check_type
))
668 error (_("incompatible POWERSET tuple elements"));
669 if (range_low
> range_high
)
671 warning (_("empty POWERSET tuple range"));
674 if (range_low
< low_bound
|| range_high
> high_bound
)
675 error (_("POWERSET tuple element out of range"));
676 range_low
-= low_bound
;
677 range_high
-= low_bound
;
678 for (; range_low
<= range_high
; range_low
++)
680 int bit_index
= (unsigned) range_low
% TARGET_CHAR_BIT
;
682 bit_index
= TARGET_CHAR_BIT
- 1 - bit_index
;
683 valaddr
[(unsigned) range_low
/ TARGET_CHAR_BIT
]
690 argvec
= (struct value
**) alloca (sizeof (struct value
*) * nargs
);
691 for (tem
= 0; tem
< nargs
; tem
++)
693 /* Ensure that array expressions are coerced into pointer objects. */
694 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
696 if (noside
== EVAL_SKIP
)
698 return value_array (tem2
, tem3
, argvec
);
702 struct value
*array
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
704 = value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
706 = value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
707 if (noside
== EVAL_SKIP
)
709 return value_slice (array
, lowbound
, upper
- lowbound
+ 1);
712 case TERNOP_SLICE_COUNT
:
714 struct value
*array
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
716 = value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
718 = value_as_long (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
719 return value_slice (array
, lowbound
, length
);
723 /* Skip third and second args to evaluate the first one. */
724 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
725 if (value_logical_not (arg1
))
727 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
728 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
732 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
733 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
737 case OP_OBJC_SELECTOR
:
738 { /* Objective C @selector operator. */
739 char *sel
= &exp
->elts
[pc
+ 2].string
;
740 int len
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
742 (*pos
) += 3 + BYTES_TO_EXP_ELEM (len
+ 1);
743 if (noside
== EVAL_SKIP
)
747 sel
[len
] = 0; /* Make sure it's terminated. */
748 return value_from_longest (lookup_pointer_type (builtin_type_void
),
749 lookup_child_selector (sel
));
752 case OP_OBJC_MSGCALL
:
753 { /* Objective C message (method) call. */
755 static CORE_ADDR responds_selector
= 0;
756 static CORE_ADDR method_selector
= 0;
758 CORE_ADDR selector
= 0;
761 int struct_return
= 0;
764 static struct value
*msg_send
= NULL
;
765 static struct value
*msg_send_stret
= NULL
;
766 static int gnu_runtime
= 0;
768 struct value
*target
= NULL
;
769 struct value
*method
= NULL
;
770 struct value
*called_method
= NULL
;
772 struct type
*selector_type
= NULL
;
774 struct value
*ret
= NULL
;
777 selector
= exp
->elts
[pc
+ 1].longconst
;
778 nargs
= exp
->elts
[pc
+ 2].longconst
;
779 argvec
= (struct value
**) alloca (sizeof (struct value
*)
784 selector_type
= lookup_pointer_type (builtin_type_void
);
785 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
786 sub_no_side
= EVAL_NORMAL
;
788 sub_no_side
= noside
;
790 target
= evaluate_subexp (selector_type
, exp
, pos
, sub_no_side
);
792 if (value_as_long (target
) == 0)
793 return value_from_longest (builtin_type_long
, 0);
795 if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0))
798 /* Find the method dispatch (Apple runtime) or method lookup
799 (GNU runtime) function for Objective-C. These will be used
800 to lookup the symbol information for the method. If we
801 can't find any symbol information, then we'll use these to
802 call the method, otherwise we can call the method
803 directly. The msg_send_stret function is used in the special
804 case of a method that returns a structure (Apple runtime
809 type
= lookup_pointer_type (builtin_type_void
);
810 type
= lookup_function_type (type
);
811 type
= lookup_pointer_type (type
);
812 type
= lookup_function_type (type
);
813 type
= lookup_pointer_type (type
);
815 msg_send
= find_function_in_inferior ("objc_msg_lookup");
816 msg_send_stret
= find_function_in_inferior ("objc_msg_lookup");
818 msg_send
= value_from_pointer (type
, value_as_address (msg_send
));
819 msg_send_stret
= value_from_pointer (type
,
820 value_as_address (msg_send_stret
));
824 msg_send
= find_function_in_inferior ("objc_msgSend");
825 /* Special dispatcher for methods returning structs */
826 msg_send_stret
= find_function_in_inferior ("objc_msgSend_stret");
829 /* Verify the target object responds to this method. The
830 standard top-level 'Object' class uses a different name for
831 the verification method than the non-standard, but more
832 often used, 'NSObject' class. Make sure we check for both. */
834 responds_selector
= lookup_child_selector ("respondsToSelector:");
835 if (responds_selector
== 0)
836 responds_selector
= lookup_child_selector ("respondsTo:");
838 if (responds_selector
== 0)
839 error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
841 method_selector
= lookup_child_selector ("methodForSelector:");
842 if (method_selector
== 0)
843 method_selector
= lookup_child_selector ("methodFor:");
845 if (method_selector
== 0)
846 error (_("no 'methodFor:' or 'methodForSelector:' method"));
848 /* Call the verification method, to make sure that the target
849 class implements the desired method. */
851 argvec
[0] = msg_send
;
853 argvec
[2] = value_from_longest (builtin_type_long
, responds_selector
);
854 argvec
[3] = value_from_longest (builtin_type_long
, selector
);
857 ret
= call_function_by_hand (argvec
[0], 3, argvec
+ 1);
860 /* Function objc_msg_lookup returns a pointer. */
862 ret
= call_function_by_hand (argvec
[0], 3, argvec
+ 1);
864 if (value_as_long (ret
) == 0)
865 error (_("Target does not respond to this message selector."));
867 /* Call "methodForSelector:" method, to get the address of a
868 function method that implements this selector for this
869 class. If we can find a symbol at that address, then we
870 know the return type, parameter types etc. (that's a good
873 argvec
[0] = msg_send
;
875 argvec
[2] = value_from_longest (builtin_type_long
, method_selector
);
876 argvec
[3] = value_from_longest (builtin_type_long
, selector
);
879 ret
= call_function_by_hand (argvec
[0], 3, argvec
+ 1);
883 ret
= call_function_by_hand (argvec
[0], 3, argvec
+ 1);
886 /* ret should now be the selector. */
888 addr
= value_as_long (ret
);
891 struct symbol
*sym
= NULL
;
892 /* Is it a high_level symbol? */
894 sym
= find_pc_function (addr
);
896 method
= value_of_variable (sym
, 0);
899 /* If we found a method with symbol information, check to see
900 if it returns a struct. Otherwise assume it doesn't. */
906 struct type
*value_type
;
908 funaddr
= find_function_addr (method
, &value_type
);
910 b
= block_for_pc (funaddr
);
912 /* If compiled without -g, assume GCC 2. */
913 using_gcc
= (b
== NULL
? 2 : BLOCK_GCC_COMPILED (b
));
915 CHECK_TYPEDEF (value_type
);
917 if ((value_type
== NULL
)
918 || (TYPE_CODE(value_type
) == TYPE_CODE_ERROR
))
920 if (expect_type
!= NULL
)
921 value_type
= expect_type
;
924 struct_return
= using_struct_return (value_type
, using_gcc
);
926 else if (expect_type
!= NULL
)
928 struct_return
= using_struct_return (check_typedef (expect_type
), using_gcc
);
931 /* Found a function symbol. Now we will substitute its
932 value in place of the message dispatcher (obj_msgSend),
933 so that we call the method directly instead of thru
934 the dispatcher. The main reason for doing this is that
935 we can now evaluate the return value and parameter values
936 according to their known data types, in case we need to
937 do things like promotion, dereferencing, special handling
938 of structs and doubles, etc.
940 We want to use the type signature of 'method', but still
941 jump to objc_msgSend() or objc_msgSend_stret() to better
942 mimic the behavior of the runtime. */
946 if (TYPE_CODE (value_type (method
)) != TYPE_CODE_FUNC
)
947 error (_("method address has symbol information with non-function type; skipping"));
949 VALUE_ADDRESS (method
) = value_as_address (msg_send_stret
);
951 VALUE_ADDRESS (method
) = value_as_address (msg_send
);
952 called_method
= method
;
957 called_method
= msg_send_stret
;
959 called_method
= msg_send
;
962 if (noside
== EVAL_SKIP
)
965 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
967 /* If the return type doesn't look like a function type,
968 call an error. This can happen if somebody tries to
969 turn a variable into a function call. This is here
970 because people often want to call, eg, strcmp, which
971 gdb doesn't know is a function. If gdb isn't asked for
972 it's opinion (ie. through "whatis"), it won't offer
975 struct type
*type
= value_type (called_method
);
976 if (type
&& TYPE_CODE (type
) == TYPE_CODE_PTR
)
977 type
= TYPE_TARGET_TYPE (type
);
978 type
= TYPE_TARGET_TYPE (type
);
982 if ((TYPE_CODE (type
) == TYPE_CODE_ERROR
) && expect_type
)
983 return allocate_value (expect_type
);
985 return allocate_value (type
);
988 error (_("Expression of type other than \"method returning ...\" used as a method"));
991 /* Now depending on whether we found a symbol for the method,
992 we will either call the runtime dispatcher or the method
995 argvec
[0] = called_method
;
997 argvec
[2] = value_from_longest (builtin_type_long
, selector
);
998 /* User-supplied arguments. */
999 for (tem
= 0; tem
< nargs
; tem
++)
1000 argvec
[tem
+ 3] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1001 argvec
[tem
+ 3] = 0;
1003 if (gnu_runtime
&& (method
!= NULL
))
1005 /* Function objc_msg_lookup returns a pointer. */
1006 deprecated_set_value_type (argvec
[0],
1007 lookup_function_type (lookup_pointer_type (value_type (argvec
[0]))));
1008 argvec
[0] = call_function_by_hand (argvec
[0], nargs
+ 2, argvec
+ 1);
1011 ret
= call_function_by_hand (argvec
[0], nargs
+ 2, argvec
+ 1);
1018 op
= exp
->elts
[*pos
].opcode
;
1019 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1020 /* Allocate arg vector, including space for the function to be
1021 called in argvec[0] and a terminating NULL */
1022 argvec
= (struct value
**) alloca (sizeof (struct value
*) * (nargs
+ 3));
1023 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
1026 /* First, evaluate the structure into arg2 */
1029 if (noside
== EVAL_SKIP
)
1032 if (op
== STRUCTOP_MEMBER
)
1034 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
1038 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1041 /* If the function is a virtual function, then the
1042 aggregate value (providing the structure) plays
1043 its part by providing the vtable. Otherwise,
1044 it is just along for the ride: call the function
1047 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1049 if (TYPE_CODE (check_typedef (value_type (arg1
)))
1050 != TYPE_CODE_METHODPTR
)
1051 error (_("Non-pointer-to-member value used in pointer-to-member "
1054 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1056 struct type
*method_type
= check_typedef (value_type (arg1
));
1057 arg1
= value_zero (method_type
, not_lval
);
1060 arg1
= cplus_method_ptr_to_value (&arg2
, arg1
);
1062 /* Now, say which argument to start evaluating from */
1065 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
1067 /* Hair for method invocations */
1071 /* First, evaluate the structure into arg2 */
1073 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
1074 *pos
+= 3 + BYTES_TO_EXP_ELEM (tem2
+ 1);
1075 if (noside
== EVAL_SKIP
)
1078 if (op
== STRUCTOP_STRUCT
)
1080 /* If v is a variable in a register, and the user types
1081 v.method (), this will produce an error, because v has
1084 A possible way around this would be to allocate a
1085 copy of the variable on the stack, copy in the
1086 contents, call the function, and copy out the
1087 contents. I.e. convert this from call by reference
1088 to call by copy-return (or whatever it's called).
1089 However, this does not work because it is not the
1090 same: the method being called could stash a copy of
1091 the address, and then future uses through that address
1092 (after the method returns) would be expected to
1093 use the variable itself, not some copy of it. */
1094 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
1098 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1100 /* Now, say which argument to start evaluating from */
1105 /* Non-method function call */
1107 argvec
[0] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1109 type
= value_type (argvec
[0]);
1110 if (type
&& TYPE_CODE (type
) == TYPE_CODE_PTR
)
1111 type
= TYPE_TARGET_TYPE (type
);
1112 if (type
&& TYPE_CODE (type
) == TYPE_CODE_FUNC
)
1114 for (; tem
<= nargs
&& tem
<= TYPE_NFIELDS (type
); tem
++)
1116 /* pai: FIXME This seems to be coercing arguments before
1117 * overload resolution has been done! */
1118 argvec
[tem
] = evaluate_subexp (TYPE_FIELD_TYPE (type
, tem
- 1),
1124 /* Evaluate arguments */
1125 for (; tem
<= nargs
; tem
++)
1127 /* Ensure that array expressions are coerced into pointer objects. */
1128 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1131 /* signal end of arglist */
1134 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
1136 int static_memfuncp
;
1139 /* Method invocation : stuff "this" as first parameter */
1141 /* Name of method from expression */
1142 strcpy (tstr
, &exp
->elts
[pc2
+ 2].string
);
1144 if (overload_resolution
&& (exp
->language_defn
->la_language
== language_cplus
))
1146 /* Language is C++, do some overload resolution before evaluation */
1147 struct value
*valp
= NULL
;
1149 /* Prepare list of argument types for overload resolution */
1150 arg_types
= (struct type
**) alloca (nargs
* (sizeof (struct type
*)));
1151 for (ix
= 1; ix
<= nargs
; ix
++)
1152 arg_types
[ix
- 1] = value_type (argvec
[ix
]);
1154 (void) find_overload_match (arg_types
, nargs
, tstr
,
1155 1 /* method */ , 0 /* strict match */ ,
1156 &arg2
/* the object */ , NULL
,
1157 &valp
, NULL
, &static_memfuncp
);
1160 argvec
[1] = arg2
; /* the ``this'' pointer */
1161 argvec
[0] = valp
; /* use the method found after overload resolution */
1164 /* Non-C++ case -- or no overload resolution */
1166 struct value
*temp
= arg2
;
1167 argvec
[0] = value_struct_elt (&temp
, argvec
+ 1, tstr
,
1169 op
== STRUCTOP_STRUCT
1170 ? "structure" : "structure pointer");
1171 /* value_struct_elt updates temp with the correct value
1172 of the ``this'' pointer if necessary, so modify argvec[1] to
1173 reflect any ``this'' changes. */
1174 arg2
= value_from_longest (lookup_pointer_type(value_type (temp
)),
1175 VALUE_ADDRESS (temp
) + value_offset (temp
)
1176 + value_embedded_offset (temp
));
1177 argvec
[1] = arg2
; /* the ``this'' pointer */
1180 if (static_memfuncp
)
1182 argvec
[1] = argvec
[0];
1187 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
1192 else if (op
== OP_VAR_VALUE
)
1194 /* Non-member function being called */
1195 /* fn: This can only be done for C++ functions. A C-style function
1196 in a C++ program, for instance, does not have the fields that
1197 are expected here */
1199 if (overload_resolution
&& (exp
->language_defn
->la_language
== language_cplus
))
1201 /* Language is C++, do some overload resolution before evaluation */
1202 struct symbol
*symp
;
1204 /* Prepare list of argument types for overload resolution */
1205 arg_types
= (struct type
**) alloca (nargs
* (sizeof (struct type
*)));
1206 for (ix
= 1; ix
<= nargs
; ix
++)
1207 arg_types
[ix
- 1] = value_type (argvec
[ix
]);
1209 (void) find_overload_match (arg_types
, nargs
, NULL
/* no need for name */ ,
1210 0 /* not method */ , 0 /* strict match */ ,
1211 NULL
, exp
->elts
[save_pos1
+2].symbol
/* the function */ ,
1214 /* Now fix the expression being evaluated */
1215 exp
->elts
[save_pos1
+2].symbol
= symp
;
1216 argvec
[0] = evaluate_subexp_with_coercion (exp
, &save_pos1
, noside
);
1220 /* Not C++, or no overload resolution allowed */
1221 /* nothing to be done; argvec already correctly set up */
1226 /* It is probably a C-style function */
1227 /* nothing to be done; argvec already correctly set up */
1232 if (noside
== EVAL_SKIP
)
1234 if (argvec
[0] == NULL
)
1235 error (_("Cannot evaluate function -- may be inlined"));
1236 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1238 /* If the return type doesn't look like a function type, call an
1239 error. This can happen if somebody tries to turn a variable into
1240 a function call. This is here because people often want to
1241 call, eg, strcmp, which gdb doesn't know is a function. If
1242 gdb isn't asked for it's opinion (ie. through "whatis"),
1243 it won't offer it. */
1245 struct type
*ftype
=
1246 TYPE_TARGET_TYPE (value_type (argvec
[0]));
1249 return allocate_value (TYPE_TARGET_TYPE (value_type (argvec
[0])));
1251 error (_("Expression of type other than \"Function returning ...\" used as function"));
1253 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
1254 /* pai: FIXME save value from call_function_by_hand, then adjust pc by adjust_fn_pc if +ve */
1256 case OP_F77_UNDETERMINED_ARGLIST
:
1258 /* Remember that in F77, functions, substring ops and
1259 array subscript operations cannot be disambiguated
1260 at parse time. We have made all array subscript operations,
1261 substring operations as well as function calls come here
1262 and we now have to discover what the heck this thing actually was.
1263 If it is a function, we process just as if we got an OP_FUNCALL. */
1265 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1268 /* First determine the type code we are dealing with. */
1269 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1270 type
= check_typedef (value_type (arg1
));
1271 code
= TYPE_CODE (type
);
1273 if (code
== TYPE_CODE_PTR
)
1275 /* Fortran always passes variable to subroutines as pointer.
1276 So we need to look into its target type to see if it is
1277 array, string or function. If it is, we need to switch
1278 to the target value the original one points to. */
1279 struct type
*target_type
= check_typedef (TYPE_TARGET_TYPE (type
));
1281 if (TYPE_CODE (target_type
) == TYPE_CODE_ARRAY
1282 || TYPE_CODE (target_type
) == TYPE_CODE_STRING
1283 || TYPE_CODE (target_type
) == TYPE_CODE_FUNC
)
1285 arg1
= value_ind (arg1
);
1286 type
= check_typedef (value_type (arg1
));
1287 code
= TYPE_CODE (type
);
1293 case TYPE_CODE_ARRAY
:
1294 if (exp
->elts
[*pos
].opcode
== OP_F90_RANGE
)
1295 return value_f90_subarray (arg1
, exp
, pos
, noside
);
1297 goto multi_f77_subscript
;
1299 case TYPE_CODE_STRING
:
1300 if (exp
->elts
[*pos
].opcode
== OP_F90_RANGE
)
1301 return value_f90_subarray (arg1
, exp
, pos
, noside
);
1304 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1305 return value_subscript (arg1
, arg2
);
1309 case TYPE_CODE_FUNC
:
1310 /* It's a function call. */
1311 /* Allocate arg vector, including space for the function to be
1312 called in argvec[0] and a terminating NULL */
1313 argvec
= (struct value
**) alloca (sizeof (struct value
*) * (nargs
+ 2));
1316 for (; tem
<= nargs
; tem
++)
1317 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
1318 argvec
[tem
] = 0; /* signal end of arglist */
1322 error (_("Cannot perform substring on this type"));
1326 /* We have a complex number, There should be 2 floating
1327 point numbers that compose it */
1328 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1329 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1331 return value_literal_complex (arg1
, arg2
, builtin_type_f_complex_s16
);
1333 case STRUCTOP_STRUCT
:
1334 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1335 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1336 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1337 if (noside
== EVAL_SKIP
)
1339 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1340 return value_zero (lookup_struct_elt_type (value_type (arg1
),
1341 &exp
->elts
[pc
+ 2].string
,
1346 struct value
*temp
= arg1
;
1347 return value_struct_elt (&temp
, NULL
, &exp
->elts
[pc
+ 2].string
,
1352 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1353 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
1354 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1355 if (noside
== EVAL_SKIP
)
1358 /* JYG: if print object is on we need to replace the base type
1359 with rtti type in order to continue on with successful
1360 lookup of member / method only available in the rtti type. */
1362 struct type
*type
= value_type (arg1
);
1363 struct type
*real_type
;
1364 int full
, top
, using_enc
;
1366 if (objectprint
&& TYPE_TARGET_TYPE(type
) &&
1367 (TYPE_CODE (TYPE_TARGET_TYPE (type
)) == TYPE_CODE_CLASS
))
1369 real_type
= value_rtti_target_type (arg1
, &full
, &top
, &using_enc
);
1372 if (TYPE_CODE (type
) == TYPE_CODE_PTR
)
1373 real_type
= lookup_pointer_type (real_type
);
1375 real_type
= lookup_reference_type (real_type
);
1377 arg1
= value_cast (real_type
, arg1
);
1382 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1383 return value_zero (lookup_struct_elt_type (value_type (arg1
),
1384 &exp
->elts
[pc
+ 2].string
,
1389 struct value
*temp
= arg1
;
1390 return value_struct_elt (&temp
, NULL
, &exp
->elts
[pc
+ 2].string
,
1391 NULL
, "structure pointer");
1394 case STRUCTOP_MEMBER
:
1396 if (op
== STRUCTOP_MEMBER
)
1397 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
1399 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1401 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1403 if (noside
== EVAL_SKIP
)
1406 type
= check_typedef (value_type (arg2
));
1407 switch (TYPE_CODE (type
))
1409 case TYPE_CODE_METHODPTR
:
1410 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1411 return value_zero (TYPE_TARGET_TYPE (type
), not_lval
);
1414 arg2
= cplus_method_ptr_to_value (&arg1
, arg2
);
1415 gdb_assert (TYPE_CODE (value_type (arg2
)) == TYPE_CODE_PTR
);
1416 return value_ind (arg2
);
1419 case TYPE_CODE_MEMBERPTR
:
1420 /* Now, convert these values to an address. */
1421 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
1424 mem_offset
= value_as_long (arg2
);
1426 arg3
= value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
1427 value_as_long (arg1
) + mem_offset
);
1428 return value_ind (arg3
);
1431 error (_("non-pointer-to-member value used in pointer-to-member construct"));
1435 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1436 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1437 if (noside
== EVAL_SKIP
)
1439 if (binop_user_defined_p (op
, arg1
, arg2
))
1440 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1442 return value_concat (arg1
, arg2
);
1445 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1446 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1448 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
1450 if (binop_user_defined_p (op
, arg1
, arg2
))
1451 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1453 return value_assign (arg1
, arg2
);
1455 case BINOP_ASSIGN_MODIFY
:
1457 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1458 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1459 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
1461 op
= exp
->elts
[pc
+ 1].opcode
;
1462 if (binop_user_defined_p (op
, arg1
, arg2
))
1463 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
, noside
);
1464 else if (op
== BINOP_ADD
)
1465 arg2
= value_add (arg1
, arg2
);
1466 else if (op
== BINOP_SUB
)
1467 arg2
= value_sub (arg1
, arg2
);
1469 arg2
= value_binop (arg1
, arg2
, op
);
1470 return value_assign (arg1
, arg2
);
1473 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1474 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1475 if (noside
== EVAL_SKIP
)
1477 if (binop_user_defined_p (op
, arg1
, arg2
))
1478 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1480 return value_add (arg1
, arg2
);
1483 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1484 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1485 if (noside
== EVAL_SKIP
)
1487 if (binop_user_defined_p (op
, arg1
, arg2
))
1488 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1490 return value_sub (arg1
, arg2
);
1499 case BINOP_BITWISE_AND
:
1500 case BINOP_BITWISE_IOR
:
1501 case BINOP_BITWISE_XOR
:
1502 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1503 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1504 if (noside
== EVAL_SKIP
)
1506 if (binop_user_defined_p (op
, arg1
, arg2
))
1507 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1508 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
1509 && (op
== BINOP_DIV
|| op
== BINOP_REM
|| op
== BINOP_MOD
))
1510 return value_zero (value_type (arg1
), not_lval
);
1512 return value_binop (arg1
, arg2
, op
);
1515 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1516 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1517 if (noside
== EVAL_SKIP
)
1519 error (_("':' operator used in invalid context"));
1521 case BINOP_SUBSCRIPT
:
1522 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1523 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1524 if (noside
== EVAL_SKIP
)
1526 if (binop_user_defined_p (op
, arg1
, arg2
))
1527 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1530 /* If the user attempts to subscript something that is not an
1531 array or pointer type (like a plain int variable for example),
1532 then report this as an error. */
1534 arg1
= coerce_ref (arg1
);
1535 type
= check_typedef (value_type (arg1
));
1536 if (TYPE_CODE (type
) != TYPE_CODE_ARRAY
1537 && TYPE_CODE (type
) != TYPE_CODE_PTR
)
1539 if (TYPE_NAME (type
))
1540 error (_("cannot subscript something of type `%s'"),
1543 error (_("cannot subscript requested type"));
1546 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1547 return value_zero (TYPE_TARGET_TYPE (type
), VALUE_LVAL (arg1
));
1549 return value_subscript (arg1
, arg2
);
1553 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1554 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1555 if (noside
== EVAL_SKIP
)
1557 return value_in (arg1
, arg2
);
1559 case MULTI_SUBSCRIPT
:
1561 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
1562 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1565 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1566 /* FIXME: EVAL_SKIP handling may not be correct. */
1567 if (noside
== EVAL_SKIP
)
1578 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
1579 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1581 /* If the user attempts to subscript something that has no target
1582 type (like a plain int variable for example), then report this
1585 type
= TYPE_TARGET_TYPE (check_typedef (value_type (arg1
)));
1588 arg1
= value_zero (type
, VALUE_LVAL (arg1
));
1594 error (_("cannot subscript something of type `%s'"),
1595 TYPE_NAME (value_type (arg1
)));
1599 if (binop_user_defined_p (op
, arg1
, arg2
))
1601 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1605 arg1
= value_subscript (arg1
, arg2
);
1610 multi_f77_subscript
:
1612 int subscript_array
[MAX_FORTRAN_DIMS
];
1613 int array_size_array
[MAX_FORTRAN_DIMS
];
1614 int ndimensions
= 1, i
;
1615 struct type
*tmp_type
;
1616 int offset_item
; /* The array offset where the item lives */
1618 if (nargs
> MAX_FORTRAN_DIMS
)
1619 error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS
);
1621 tmp_type
= check_typedef (value_type (arg1
));
1622 ndimensions
= calc_f77_array_dims (type
);
1624 if (nargs
!= ndimensions
)
1625 error (_("Wrong number of subscripts"));
1627 /* Now that we know we have a legal array subscript expression
1628 let us actually find out where this element exists in the array. */
1631 /* Take array indices left to right */
1632 for (i
= 0; i
< nargs
; i
++)
1634 /* Evaluate each subscript, It must be a legal integer in F77 */
1635 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
1637 /* Fill in the subscript and array size arrays */
1639 subscript_array
[i
] = value_as_long (arg2
);
1642 /* Internal type of array is arranged right to left */
1643 for (i
= 0; i
< nargs
; i
++)
1645 retcode
= f77_get_dynamic_upperbound (tmp_type
, &upper
);
1646 if (retcode
== BOUND_FETCH_ERROR
)
1647 error (_("Cannot obtain dynamic upper bound"));
1649 retcode
= f77_get_dynamic_lowerbound (tmp_type
, &lower
);
1650 if (retcode
== BOUND_FETCH_ERROR
)
1651 error (_("Cannot obtain dynamic lower bound"));
1653 array_size_array
[nargs
- i
- 1] = upper
- lower
+ 1;
1655 /* Zero-normalize subscripts so that offsetting will work. */
1657 subscript_array
[nargs
- i
- 1] -= lower
;
1659 /* If we are at the bottom of a multidimensional
1660 array type then keep a ptr to the last ARRAY
1661 type around for use when calling value_subscript()
1662 below. This is done because we pretend to value_subscript
1663 that we actually have a one-dimensional array
1664 of base element type that we apply a simple
1668 tmp_type
= check_typedef (TYPE_TARGET_TYPE (tmp_type
));
1671 /* Now let us calculate the offset for this item */
1673 offset_item
= subscript_array
[ndimensions
- 1];
1675 for (i
= ndimensions
- 1; i
> 0; --i
)
1677 array_size_array
[i
- 1] * offset_item
+ subscript_array
[i
- 1];
1679 /* Construct a value node with the value of the offset */
1681 arg2
= value_from_longest (builtin_type_f_integer
, offset_item
);
1683 /* Let us now play a dirty trick: we will take arg1
1684 which is a value node pointing to the topmost level
1685 of the multidimensional array-set and pretend
1686 that it is actually a array of the final element
1687 type, this will ensure that value_subscript()
1688 returns the correct type value */
1690 deprecated_set_value_type (arg1
, tmp_type
);
1691 return value_ind (value_add (value_coerce_array (arg1
), arg2
));
1694 case BINOP_LOGICAL_AND
:
1695 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1696 if (noside
== EVAL_SKIP
)
1698 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1703 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1706 if (binop_user_defined_p (op
, arg1
, arg2
))
1708 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1709 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1713 tem
= value_logical_not (arg1
);
1714 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
1715 (tem
? EVAL_SKIP
: noside
));
1716 return value_from_longest (LA_BOOL_TYPE
,
1717 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
1720 case BINOP_LOGICAL_OR
:
1721 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1722 if (noside
== EVAL_SKIP
)
1724 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1729 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1732 if (binop_user_defined_p (op
, arg1
, arg2
))
1734 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1735 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1739 tem
= value_logical_not (arg1
);
1740 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
1741 (!tem
? EVAL_SKIP
: noside
));
1742 return value_from_longest (LA_BOOL_TYPE
,
1743 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
1747 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1748 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1749 if (noside
== EVAL_SKIP
)
1751 if (binop_user_defined_p (op
, arg1
, arg2
))
1753 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1757 tem
= value_equal (arg1
, arg2
);
1758 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) tem
);
1761 case BINOP_NOTEQUAL
:
1762 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1763 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1764 if (noside
== EVAL_SKIP
)
1766 if (binop_user_defined_p (op
, arg1
, arg2
))
1768 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1772 tem
= value_equal (arg1
, arg2
);
1773 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) ! tem
);
1777 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1778 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1779 if (noside
== EVAL_SKIP
)
1781 if (binop_user_defined_p (op
, arg1
, arg2
))
1783 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1787 tem
= value_less (arg1
, arg2
);
1788 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) tem
);
1792 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1793 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1794 if (noside
== EVAL_SKIP
)
1796 if (binop_user_defined_p (op
, arg1
, arg2
))
1798 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1802 tem
= value_less (arg2
, arg1
);
1803 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) tem
);
1807 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1808 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1809 if (noside
== EVAL_SKIP
)
1811 if (binop_user_defined_p (op
, arg1
, arg2
))
1813 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1817 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
1818 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) tem
);
1822 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1823 arg2
= evaluate_subexp (value_type (arg1
), exp
, pos
, noside
);
1824 if (noside
== EVAL_SKIP
)
1826 if (binop_user_defined_p (op
, arg1
, arg2
))
1828 return value_x_binop (arg1
, arg2
, op
, OP_NULL
, noside
);
1832 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
1833 return value_from_longest (LA_BOOL_TYPE
, (LONGEST
) tem
);
1837 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1838 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1839 if (noside
== EVAL_SKIP
)
1841 type
= check_typedef (value_type (arg2
));
1842 if (TYPE_CODE (type
) != TYPE_CODE_INT
)
1843 error (_("Non-integral right operand for \"@\" operator."));
1844 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1846 return allocate_repeat_value (value_type (arg1
),
1847 longest_to_int (value_as_long (arg2
)));
1850 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
1853 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1854 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1857 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1858 if (noside
== EVAL_SKIP
)
1860 if (unop_user_defined_p (op
, arg1
))
1861 return value_x_unop (arg1
, op
, noside
);
1863 return value_pos (arg1
);
1866 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1867 if (noside
== EVAL_SKIP
)
1869 if (unop_user_defined_p (op
, arg1
))
1870 return value_x_unop (arg1
, op
, noside
);
1872 return value_neg (arg1
);
1874 case UNOP_COMPLEMENT
:
1875 /* C++: check for and handle destructor names. */
1876 op
= exp
->elts
[*pos
].opcode
;
1878 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1879 if (noside
== EVAL_SKIP
)
1881 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
1882 return value_x_unop (arg1
, UNOP_COMPLEMENT
, noside
);
1884 return value_complement (arg1
);
1886 case UNOP_LOGICAL_NOT
:
1887 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1888 if (noside
== EVAL_SKIP
)
1890 if (unop_user_defined_p (op
, arg1
))
1891 return value_x_unop (arg1
, op
, noside
);
1893 return value_from_longest (LA_BOOL_TYPE
,
1894 (LONGEST
) value_logical_not (arg1
));
1897 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
1898 expect_type
= TYPE_TARGET_TYPE (check_typedef (expect_type
));
1899 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
1900 type
= check_typedef (value_type (arg1
));
1901 if (TYPE_CODE (type
) == TYPE_CODE_METHODPTR
1902 || TYPE_CODE (type
) == TYPE_CODE_MEMBERPTR
)
1903 error (_("Attempt to dereference pointer to member without an object"));
1904 if (noside
== EVAL_SKIP
)
1906 if (unop_user_defined_p (op
, arg1
))
1907 return value_x_unop (arg1
, op
, noside
);
1908 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1910 type
= check_typedef (value_type (arg1
));
1911 if (TYPE_CODE (type
) == TYPE_CODE_PTR
1912 || TYPE_CODE (type
) == TYPE_CODE_REF
1913 /* In C you can dereference an array to get the 1st elt. */
1914 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1916 return value_zero (TYPE_TARGET_TYPE (type
),
1918 else if (TYPE_CODE (type
) == TYPE_CODE_INT
)
1919 /* GDB allows dereferencing an int. */
1920 return value_zero (builtin_type_int
, lval_memory
);
1922 error (_("Attempt to take contents of a non-pointer value."));
1924 return value_ind (arg1
);
1927 /* C++: check for and handle pointer to members. */
1929 op
= exp
->elts
[*pos
].opcode
;
1931 if (noside
== EVAL_SKIP
)
1933 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
1938 struct value
*retvalp
= evaluate_subexp_for_address (exp
, pos
, noside
);
1943 if (noside
== EVAL_SKIP
)
1945 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
1948 return evaluate_subexp_for_sizeof (exp
, pos
);
1952 type
= exp
->elts
[pc
+ 1].type
;
1953 arg1
= evaluate_subexp (type
, exp
, pos
, noside
);
1954 if (noside
== EVAL_SKIP
)
1956 if (type
!= value_type (arg1
))
1957 arg1
= value_cast (type
, arg1
);
1962 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
1963 if (noside
== EVAL_SKIP
)
1965 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1966 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
1968 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
1969 value_as_address (arg1
));
1971 case UNOP_MEMVAL_TLS
:
1973 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
1974 if (noside
== EVAL_SKIP
)
1976 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1977 return value_zero (exp
->elts
[pc
+ 2].type
, lval_memory
);
1981 tls_addr
= target_translate_tls_address (exp
->elts
[pc
+ 1].objfile
,
1982 value_as_address (arg1
));
1983 return value_at_lazy (exp
->elts
[pc
+ 2].type
, tls_addr
);
1986 case UNOP_PREINCREMENT
:
1987 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
1988 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
1990 else if (unop_user_defined_p (op
, arg1
))
1992 return value_x_unop (arg1
, op
, noside
);
1996 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
1998 return value_assign (arg1
, arg2
);
2001 case UNOP_PREDECREMENT
:
2002 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2003 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2005 else if (unop_user_defined_p (op
, arg1
))
2007 return value_x_unop (arg1
, op
, noside
);
2011 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
2013 return value_assign (arg1
, arg2
);
2016 case UNOP_POSTINCREMENT
:
2017 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2018 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2020 else if (unop_user_defined_p (op
, arg1
))
2022 return value_x_unop (arg1
, op
, noside
);
2026 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
2028 value_assign (arg1
, arg2
);
2032 case UNOP_POSTDECREMENT
:
2033 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
2034 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
2036 else if (unop_user_defined_p (op
, arg1
))
2038 return value_x_unop (arg1
, op
, noside
);
2042 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
2044 value_assign (arg1
, arg2
);
2050 return value_of_this (1);
2054 return value_of_local ("self", 1);
2057 /* The value is not supposed to be used. This is here to make it
2058 easier to accommodate expressions that contain types. */
2060 if (noside
== EVAL_SKIP
)
2062 else if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2063 return allocate_value (exp
->elts
[pc
+ 1].type
);
2065 error (_("Attempt to use a type name as an expression"));
2068 /* Removing this case and compiling with gcc -Wall reveals that
2069 a lot of cases are hitting this case. Some of these should
2070 probably be removed from expression.h; others are legitimate
2071 expressions which are (apparently) not fully implemented.
2073 If there are any cases landing here which mean a user error,
2074 then they should be separate cases, with more descriptive
2078 GDB does not (yet) know how to evaluate that kind of expression"));
2082 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
2085 /* Evaluate a subexpression of EXP, at index *POS,
2086 and return the address of that subexpression.
2087 Advance *POS over the subexpression.
2088 If the subexpression isn't an lvalue, get an error.
2089 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2090 then only the type of the result need be correct. */
2092 static struct value
*
2093 evaluate_subexp_for_address (struct expression
*exp
, int *pos
,
2103 op
= exp
->elts
[pc
].opcode
;
2109 x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
2111 /* We can't optimize out "&*" if there's a user-defined operator*. */
2112 if (unop_user_defined_p (op
, x
))
2114 x
= value_x_unop (x
, op
, noside
);
2115 goto default_case_after_eval
;
2122 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
2123 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
2126 var
= exp
->elts
[pc
+ 2].symbol
;
2128 /* C++: The "address" of a reference should yield the address
2129 * of the object pointed to. Let value_addr() deal with it. */
2130 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
2134 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2137 lookup_pointer_type (SYMBOL_TYPE (var
));
2138 enum address_class sym_class
= SYMBOL_CLASS (var
);
2140 if (sym_class
== LOC_CONST
2141 || sym_class
== LOC_CONST_BYTES
2142 || sym_class
== LOC_REGISTER
2143 || sym_class
== LOC_REGPARM
)
2144 error (_("Attempt to take address of register or constant."));
2147 value_zero (type
, not_lval
);
2153 block_innermost_frame (exp
->elts
[pc
+ 1].block
));
2156 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
2157 (*pos
) += 5 + BYTES_TO_EXP_ELEM (tem
+ 1);
2158 x
= value_aggregate_elt (exp
->elts
[pc
+ 1].type
,
2159 &exp
->elts
[pc
+ 3].string
,
2162 error (_("There is no field named %s"), &exp
->elts
[pc
+ 3].string
);
2167 x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
2168 default_case_after_eval
:
2169 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
2171 struct type
*type
= check_typedef (value_type (x
));
2173 if (VALUE_LVAL (x
) == lval_memory
)
2174 return value_zero (lookup_pointer_type (value_type (x
)),
2176 else if (TYPE_CODE (type
) == TYPE_CODE_REF
)
2177 return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
2180 error (_("Attempt to take address of non-lval"));
2182 return value_addr (x
);
2186 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2187 When used in contexts where arrays will be coerced anyway, this is
2188 equivalent to `evaluate_subexp' but much faster because it avoids
2189 actually fetching array contents (perhaps obsolete now that we have
2192 Note that we currently only do the coercion for C expressions, where
2193 arrays are zero based and the coercion is correct. For other languages,
2194 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
2195 to decide if coercion is appropriate.
2200 evaluate_subexp_with_coercion (struct expression
*exp
,
2201 int *pos
, enum noside noside
)
2209 op
= exp
->elts
[pc
].opcode
;
2214 var
= exp
->elts
[pc
+ 2].symbol
;
2215 if (TYPE_CODE (check_typedef (SYMBOL_TYPE (var
))) == TYPE_CODE_ARRAY
2216 && CAST_IS_CONVERSION
)
2221 (var
, block_innermost_frame (exp
->elts
[pc
+ 1].block
));
2222 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (check_typedef (SYMBOL_TYPE (var
)))),
2228 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
2232 /* Evaluate a subexpression of EXP, at index *POS,
2233 and return a value for the size of that subexpression.
2234 Advance *POS over the subexpression. */
2236 static struct value
*
2237 evaluate_subexp_for_sizeof (struct expression
*exp
, int *pos
)
2245 op
= exp
->elts
[pc
].opcode
;
2249 /* This case is handled specially
2250 so that we avoid creating a value for the result type.
2251 If the result type is very big, it's desirable not to
2252 create a value unnecessarily. */
2255 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2256 type
= check_typedef (value_type (val
));
2257 if (TYPE_CODE (type
) != TYPE_CODE_PTR
2258 && TYPE_CODE (type
) != TYPE_CODE_REF
2259 && TYPE_CODE (type
) != TYPE_CODE_ARRAY
)
2260 error (_("Attempt to take contents of a non-pointer value."));
2261 type
= check_typedef (TYPE_TARGET_TYPE (type
));
2262 return value_from_longest (builtin_type_int
, (LONGEST
)
2263 TYPE_LENGTH (type
));
2267 type
= check_typedef (exp
->elts
[pc
+ 1].type
);
2268 return value_from_longest (builtin_type_int
,
2269 (LONGEST
) TYPE_LENGTH (type
));
2273 type
= check_typedef (SYMBOL_TYPE (exp
->elts
[pc
+ 2].symbol
));
2275 value_from_longest (builtin_type_int
, (LONGEST
) TYPE_LENGTH (type
));
2278 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
2279 return value_from_longest (builtin_type_int
,
2280 (LONGEST
) TYPE_LENGTH (value_type (val
)));
2284 /* Parse a type expression in the string [P..P+LENGTH). */
2287 parse_and_eval_type (char *p
, int length
)
2289 char *tmp
= (char *) alloca (length
+ 4);
2290 struct expression
*expr
;
2292 memcpy (tmp
+ 1, p
, length
);
2293 tmp
[length
+ 1] = ')';
2294 tmp
[length
+ 2] = '0';
2295 tmp
[length
+ 3] = '\0';
2296 expr
= parse_expression (tmp
);
2297 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
2298 error (_("Internal error in eval_type."));
2299 return expr
->elts
[1].type
;
2303 calc_f77_array_dims (struct type
*array_type
)
2306 struct type
*tmp_type
;
2308 if ((TYPE_CODE (array_type
) != TYPE_CODE_ARRAY
))
2309 error (_("Can't get dimensions for a non-array type"));
2311 tmp_type
= array_type
;
2313 while ((tmp_type
= TYPE_TARGET_TYPE (tmp_type
)))
2315 if (TYPE_CODE (tmp_type
) == TYPE_CODE_ARRAY
)