1 /* Evaluate expressions for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "expression.h"
27 #include "language.h" /* For CAST_IS_CONVERSION */
29 /* Values of NOSIDE argument to eval_subexp. */
32 EVAL_SKIP
, /* Only effect is to increment pos. */
33 EVAL_AVOID_SIDE_EFFECTS
/* Don't modify any variables or
34 call any functions. The value
35 returned will have the correct
36 type, and will have an
37 approximately correct lvalue
38 type (inaccuracy: anything that is
39 listed as being in a register in
40 the function in which it was
41 declared will be lval_register). */
44 /* Prototypes for local functions. */
47 evaluate_subexp_for_sizeof
PARAMS ((struct expression
*, int *));
50 evaluate_subexp_with_coercion
PARAMS ((struct expression
*, int *,
54 evaluate_subexp_for_address
PARAMS ((struct expression
*, int *,
58 evaluate_subexp
PARAMS ((struct type
*, struct expression
*, int *,
62 /* Parse the string EXP as a C expression, evaluate it,
63 and return the result as a number. */
66 parse_and_eval_address (exp
)
69 struct expression
*expr
= parse_expression (exp
);
70 register CORE_ADDR addr
;
71 register struct cleanup
*old_chain
=
72 make_cleanup (free_current_contents
, &expr
);
74 addr
= value_as_pointer (evaluate_expression (expr
));
75 do_cleanups (old_chain
);
79 /* Like parse_and_eval_address but takes a pointer to a char * variable
80 and advanced that variable across the characters parsed. */
83 parse_and_eval_address_1 (expptr
)
86 struct expression
*expr
= parse_exp_1 (expptr
, (struct block
*)0, 0);
87 register CORE_ADDR addr
;
88 register struct cleanup
*old_chain
=
89 make_cleanup (free_current_contents
, &expr
);
91 addr
= value_as_pointer (evaluate_expression (expr
));
92 do_cleanups (old_chain
);
100 struct expression
*expr
= parse_expression (exp
);
102 register struct cleanup
*old_chain
103 = make_cleanup (free_current_contents
, &expr
);
105 val
= evaluate_expression (expr
);
106 do_cleanups (old_chain
);
110 /* Parse up to a comma (or to a closeparen)
111 in the string EXPP as an expression, evaluate it, and return the value.
112 EXPP is advanced to point to the comma. */
115 parse_to_comma_and_eval (expp
)
118 struct expression
*expr
= parse_exp_1 (expp
, (struct block
*) 0, 1);
120 register struct cleanup
*old_chain
121 = make_cleanup (free_current_contents
, &expr
);
123 val
= evaluate_expression (expr
);
124 do_cleanups (old_chain
);
128 /* Evaluate an expression in internal prefix form
129 such as is constructed by parse.y.
131 See expression.h for info on the format of an expression. */
133 static value
evaluate_subexp ();
134 static value
evaluate_subexp_for_address ();
135 static value
evaluate_subexp_for_sizeof ();
136 static value
evaluate_subexp_with_coercion ();
139 evaluate_expression (exp
)
140 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. */
151 struct expression
*exp
;
154 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
158 evaluate_subexp (expect_type
, exp
, pos
, noside
)
159 struct type
*expect_type
;
160 register struct expression
*exp
;
166 register int pc
, pc2
, oldpos
;
167 register value arg1
, arg2
, arg3
;
173 op
= exp
->elts
[pc
].opcode
;
178 tem
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
179 (*pos
) += 4 + ((tem
+ sizeof (union exp_element
))
180 / sizeof (union exp_element
));
181 arg1
= value_struct_elt_for_reference (exp
->elts
[pc
+ 1].type
,
183 exp
->elts
[pc
+ 1].type
,
184 &exp
->elts
[pc
+ 3].string
,
187 error ("There is no field named %s", &exp
->elts
[pc
+ 3].string
);
192 return value_from_longest (exp
->elts
[pc
+ 1].type
,
193 exp
->elts
[pc
+ 2].longconst
);
197 return value_from_double (exp
->elts
[pc
+ 1].type
,
198 exp
->elts
[pc
+ 2].doubleconst
);
202 if (noside
== EVAL_SKIP
)
204 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
206 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
209 switch (SYMBOL_CLASS (sym
))
213 case LOC_CONST_BYTES
:
227 return value_zero (SYMBOL_TYPE (sym
), lv
);
230 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
235 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
239 return value_of_register (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
241 /* start-sanitize-chill */
244 return value_from_longest (builtin_type_chill_bool
,
245 exp
->elts
[pc
+ 1].longconst
);
246 /* end-sanitize-chill */
250 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
253 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
254 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
255 / sizeof (union exp_element
));
256 if (noside
== EVAL_SKIP
)
258 return value_string (&exp
->elts
[pc
+ 2].string
, tem
);
261 /* Skip third and second args to evaluate the first one. */
262 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
263 if (value_logical_not (arg1
))
265 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
266 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
270 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
271 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
277 op
= exp
->elts
[*pos
].opcode
;
278 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
282 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
283 /* First, evaluate the structure into arg2 */
286 if (noside
== EVAL_SKIP
)
289 if (op
== STRUCTOP_MEMBER
)
291 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
295 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
298 /* If the function is a virtual function, then the
299 aggregate value (providing the structure) plays
300 its part by providing the vtable. Otherwise,
301 it is just along for the ride: call the function
304 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
306 fnptr
= longest_to_int (value_as_long (arg1
));
308 if (METHOD_PTR_IS_VIRTUAL(fnptr
))
310 int fnoffset
= METHOD_PTR_TO_VOFFSET(fnptr
);
311 struct type
*basetype
;
312 struct type
*domain_type
=
313 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
315 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
316 if (domain_type
!= basetype
)
317 arg2
= value_cast(lookup_pointer_type (domain_type
), arg2
);
318 basetype
= TYPE_VPTR_BASETYPE (domain_type
);
319 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
321 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
322 /* If one is virtual, then all are virtual. */
323 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
324 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
325 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnoffset
)
327 value temp
= value_ind (arg2
);
328 arg1
= value_virtual_fn_field (&temp
, f
, j
, domain_type
, 0);
329 arg2
= value_addr (temp
);
334 error ("virtual function at index %d not found", fnoffset
);
338 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
342 /* Now, say which argument to start evaluating from */
345 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
347 /* Hair for method invocations */
350 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
351 /* First, evaluate the structure into arg2 */
353 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
354 *pos
+= 3 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
355 if (noside
== EVAL_SKIP
)
358 if (op
== STRUCTOP_STRUCT
)
360 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
364 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
366 /* Now, say which argument to start evaluating from */
371 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
374 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
375 for (; tem
<= nargs
; tem
++)
376 /* Ensure that array expressions are coerced into pointer objects. */
377 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
379 /* signal end of arglist */
382 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
389 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 2].string
,
391 op
== STRUCTOP_STRUCT
392 ? "structure" : "structure pointer");
393 if (VALUE_OFFSET (temp
))
395 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
396 value_as_long (arg2
)+VALUE_OFFSET (temp
));
401 argvec
[1] = argvec
[0];
406 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
412 if (noside
== EVAL_SKIP
)
414 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
416 /* If the return type doesn't look like a function type, call an
417 error. This can happen if somebody tries to turn a variable into
418 a function call. This is here because people often want to
419 call, eg, strcmp, which gdb doesn't know is a function. If
420 gdb isn't asked for it's opinion (ie. through "whatis"),
421 it won't offer it. */
424 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
427 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
429 error ("Expression of type other than \"Function returning ...\" used as function");
431 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
433 case STRUCTOP_STRUCT
:
434 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
435 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
436 / sizeof (union exp_element
));
437 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
438 if (noside
== EVAL_SKIP
)
440 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
441 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
442 &exp
->elts
[pc
+ 2].string
,
448 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
449 (int *) 0, "structure");
453 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
454 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
455 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
456 if (noside
== EVAL_SKIP
)
458 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
459 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
461 &exp
->elts
[pc
+ 2].string
,
467 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
468 (int *) 0, "structure pointer");
471 case STRUCTOP_MEMBER
:
472 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
473 goto handle_pointer_to_member
;
475 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
476 handle_pointer_to_member
:
477 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
478 if (noside
== EVAL_SKIP
)
480 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
)
481 goto bad_pointer_to_member
;
482 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
483 if (TYPE_CODE (type
) == TYPE_CODE_METHOD
)
484 error ("not implemented: pointer-to-method in pointer-to-member construct");
485 if (TYPE_CODE (type
) != TYPE_CODE_MEMBER
)
486 goto bad_pointer_to_member
;
487 /* Now, convert these values to an address. */
488 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
490 arg3
= value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
491 value_as_long (arg1
) + value_as_long (arg2
));
492 return value_ind (arg3
);
493 bad_pointer_to_member
:
494 error("non-pointer-to-member value used in pointer-to-member construct");
497 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
498 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
499 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
501 if (binop_user_defined_p (op
, arg1
, arg2
))
502 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
504 return value_assign (arg1
, arg2
);
506 case BINOP_ASSIGN_MODIFY
:
508 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
509 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
510 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
512 op
= exp
->elts
[pc
+ 1].opcode
;
513 if (binop_user_defined_p (op
, arg1
, arg2
))
514 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
515 else if (op
== BINOP_ADD
)
516 arg2
= value_add (arg1
, arg2
);
517 else if (op
== BINOP_SUB
)
518 arg2
= value_sub (arg1
, arg2
);
520 arg2
= value_binop (arg1
, arg2
, op
);
521 return value_assign (arg1
, arg2
);
524 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
525 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
526 if (noside
== EVAL_SKIP
)
528 if (binop_user_defined_p (op
, arg1
, arg2
))
529 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
531 return value_add (arg1
, arg2
);
534 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
535 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
536 if (noside
== EVAL_SKIP
)
538 if (binop_user_defined_p (op
, arg1
, arg2
))
539 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
541 return value_sub (arg1
, arg2
);
548 case BINOP_BITWISE_AND
:
549 case BINOP_BITWISE_IOR
:
550 case BINOP_BITWISE_XOR
:
551 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
552 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
553 if (noside
== EVAL_SKIP
)
555 if (binop_user_defined_p (op
, arg1
, arg2
))
556 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
558 if (noside
== EVAL_AVOID_SIDE_EFFECTS
559 && (op
== BINOP_DIV
|| op
== BINOP_REM
))
560 return value_zero (VALUE_TYPE (arg1
), not_lval
);
562 return value_binop (arg1
, arg2
, op
);
564 case BINOP_SUBSCRIPT
:
565 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
566 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
567 if (noside
== EVAL_SKIP
)
569 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
571 /* If the user attempts to subscript something that has no target
572 type (like a plain int variable for example), then report this
575 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
577 return value_zero (type
, VALUE_LVAL (arg1
));
579 error ("cannot subscript something of type `%s'",
580 TYPE_NAME (VALUE_TYPE (arg1
)));
583 if (binop_user_defined_p (op
, arg1
, arg2
))
584 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
586 return value_subscript (arg1
, arg2
);
588 case MULTI_SUBSCRIPT
:
590 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
591 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
594 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
595 /* FIXME: EVAL_SKIP handling may not be correct. */
596 if (noside
== EVAL_SKIP
)
607 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
608 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
610 /* If the user attempts to subscript something that has no target
611 type (like a plain int variable for example), then report this
614 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
617 arg1
= value_zero (type
, VALUE_LVAL (arg1
));
623 error ("cannot subscript something of type `%s'",
624 TYPE_NAME (VALUE_TYPE (arg1
)));
628 if (binop_user_defined_p (op
, arg1
, arg2
))
630 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
);
634 arg1
= value_subscript (arg1
, arg2
);
639 case BINOP_LOGICAL_AND
:
640 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
641 if (noside
== EVAL_SKIP
)
643 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
648 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
651 if (binop_user_defined_p (op
, arg1
, arg2
))
653 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
654 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
658 tem
= value_logical_not (arg1
);
659 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
660 (tem
? EVAL_SKIP
: noside
));
661 return value_from_longest (builtin_type_int
,
662 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
665 case BINOP_LOGICAL_OR
:
666 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
667 if (noside
== EVAL_SKIP
)
669 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
674 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
677 if (binop_user_defined_p (op
, arg1
, arg2
))
679 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
680 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
684 tem
= value_logical_not (arg1
);
685 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
686 (!tem
? EVAL_SKIP
: noside
));
687 return value_from_longest (builtin_type_int
,
688 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
692 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
693 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
694 if (noside
== EVAL_SKIP
)
696 if (binop_user_defined_p (op
, arg1
, arg2
))
698 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
702 tem
= value_equal (arg1
, arg2
);
703 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
707 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
708 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
709 if (noside
== EVAL_SKIP
)
711 if (binop_user_defined_p (op
, arg1
, arg2
))
713 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
717 tem
= value_equal (arg1
, arg2
);
718 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
722 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
723 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
724 if (noside
== EVAL_SKIP
)
726 if (binop_user_defined_p (op
, arg1
, arg2
))
728 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
732 tem
= value_less (arg1
, arg2
);
733 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
737 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
738 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
739 if (noside
== EVAL_SKIP
)
741 if (binop_user_defined_p (op
, arg1
, arg2
))
743 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
747 tem
= value_less (arg2
, arg1
);
748 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
752 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
753 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
754 if (noside
== EVAL_SKIP
)
756 if (binop_user_defined_p (op
, arg1
, arg2
))
758 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
762 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
763 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
767 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
768 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
769 if (noside
== EVAL_SKIP
)
771 if (binop_user_defined_p (op
, arg1
, arg2
))
773 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
777 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
778 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
782 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
783 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
784 if (noside
== EVAL_SKIP
)
786 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
787 error ("Non-integral right operand for \"@\" operator.");
788 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
789 return allocate_repeat_value (VALUE_TYPE (arg1
),
790 longest_to_int (value_as_long (arg2
)));
792 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
795 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
796 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
799 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
800 if (noside
== EVAL_SKIP
)
802 if (unop_user_defined_p (op
, arg1
))
803 return value_x_unop (arg1
, op
);
805 return value_neg (arg1
);
807 case UNOP_COMPLEMENT
:
808 /* C++: check for and handle destructor names. */
809 op
= exp
->elts
[*pos
].opcode
;
811 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
812 if (noside
== EVAL_SKIP
)
814 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
815 return value_x_unop (arg1
, UNOP_COMPLEMENT
);
817 return value_complement (arg1
);
819 case UNOP_LOGICAL_NOT
:
820 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
821 if (noside
== EVAL_SKIP
)
823 if (unop_user_defined_p (op
, arg1
))
824 return value_x_unop (arg1
, op
);
826 return value_from_longest (builtin_type_int
,
827 (LONGEST
) value_logical_not (arg1
));
830 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
831 expect_type
= TYPE_TARGET_TYPE (expect_type
);
832 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
833 if (noside
== EVAL_SKIP
)
835 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
837 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
838 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
839 /* In C you can dereference an array to get the 1st elt. */
840 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
842 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
844 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
845 /* GDB allows dereferencing an int. */
846 return value_zero (builtin_type_int
, lval_memory
);
848 error ("Attempt to take contents of a non-pointer value.");
850 return value_ind (arg1
);
853 /* C++: check for and handle pointer to members. */
855 op
= exp
->elts
[*pos
].opcode
;
857 if (noside
== EVAL_SKIP
)
861 int temm
= longest_to_int (exp
->elts
[pc
+3].longconst
);
862 (*pos
) += 3 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
865 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
869 return evaluate_subexp_for_address (exp
, pos
, noside
);
872 if (noside
== EVAL_SKIP
)
874 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
877 return evaluate_subexp_for_sizeof (exp
, pos
);
881 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
882 if (noside
== EVAL_SKIP
)
884 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
888 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
889 if (noside
== EVAL_SKIP
)
891 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
892 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
894 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
895 value_as_pointer (arg1
));
897 case UNOP_PREINCREMENT
:
898 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
899 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
901 else if (unop_user_defined_p (op
, arg1
))
903 return value_x_unop (arg1
, op
);
907 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
909 return value_assign (arg1
, arg2
);
912 case UNOP_PREDECREMENT
:
913 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
914 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
916 else if (unop_user_defined_p (op
, arg1
))
918 return value_x_unop (arg1
, op
);
922 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
924 return value_assign (arg1
, arg2
);
927 case UNOP_POSTINCREMENT
:
928 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
929 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
931 else if (unop_user_defined_p (op
, arg1
))
933 return value_x_unop (arg1
, op
);
937 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
939 value_assign (arg1
, arg2
);
943 case UNOP_POSTDECREMENT
:
944 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
945 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
947 else if (unop_user_defined_p (op
, arg1
))
949 return value_x_unop (arg1
, op
);
953 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
955 value_assign (arg1
, arg2
);
961 return value_of_this (1);
964 error ("internal error: I do not know how to evaluate what you gave me");
968 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
971 /* Evaluate a subexpression of EXP, at index *POS,
972 and return the address of that subexpression.
973 Advance *POS over the subexpression.
974 If the subexpression isn't an lvalue, get an error.
975 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
976 then only the type of the result need be correct. */
979 evaluate_subexp_for_address (exp
, pos
, noside
)
980 register struct expression
*exp
;
989 op
= exp
->elts
[pc
].opcode
;
995 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
999 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
1000 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1003 var
= exp
->elts
[pc
+ 1].symbol
;
1005 /* C++: The "address" of a reference should yield the address
1006 * of the object pointed to. Let value_addr() deal with it. */
1007 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
1011 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1014 lookup_pointer_type (SYMBOL_TYPE (var
));
1015 enum address_class sym_class
= SYMBOL_CLASS (var
);
1017 if (sym_class
== LOC_CONST
1018 || sym_class
== LOC_CONST_BYTES
1019 || sym_class
== LOC_REGISTER
1020 || sym_class
== LOC_REGPARM
)
1021 error ("Attempt to take address of register or constant.");
1024 value_zero (type
, not_lval
);
1027 return locate_var_value (var
, (FRAME
) 0);
1031 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1033 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1034 if (VALUE_LVAL (x
) == lval_memory
)
1035 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
1038 error ("Attempt to take address of non-lval");
1040 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1044 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1045 When used in contexts where arrays will be coerced anyway, this is
1046 equivalent to `evaluate_subexp' but much faster because it avoids
1047 actually fetching array contents.
1049 Note that we currently only do the coercion for C expressions, where
1050 arrays are zero based and the coercion is correct. For other languages,
1051 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1052 to decide if coercion is appropriate.
1057 evaluate_subexp_with_coercion (exp
, pos
, noside
)
1058 register struct expression
*exp
;
1062 register enum exp_opcode op
;
1068 op
= exp
->elts
[pc
].opcode
;
1073 var
= exp
->elts
[pc
+ 1].symbol
;
1074 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
1075 && CAST_IS_CONVERSION
)
1078 val
= locate_var_value (var
, (FRAME
) 0);
1079 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1083 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1087 /* Evaluate a subexpression of EXP, at index *POS,
1088 and return a value for the size of that subexpression.
1089 Advance *POS over the subexpression. */
1092 evaluate_subexp_for_sizeof (exp
, pos
)
1093 register struct expression
*exp
;
1101 op
= exp
->elts
[pc
].opcode
;
1105 /* This case is handled specially
1106 so that we avoid creating a value for the result type.
1107 If the result type is very big, it's desirable not to
1108 create a value unnecessarily. */
1111 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1112 return value_from_longest (builtin_type_int
, (LONGEST
)
1113 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1117 return value_from_longest (builtin_type_int
,
1118 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1122 return value_from_longest (builtin_type_int
,
1123 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1126 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1127 return value_from_longest (builtin_type_int
,
1128 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1132 /* Parse a type expression in the string [P..P+LENGTH). */
1135 parse_and_eval_type (p
, length
)
1139 char *tmp
= (char *)alloca (length
+ 4);
1140 struct expression
*expr
;
1142 memcpy (tmp
+1, p
, length
);
1143 tmp
[length
+1] = ')';
1144 tmp
[length
+2] = '0';
1145 tmp
[length
+3] = '\0';
1146 expr
= parse_expression (tmp
);
1147 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1148 error ("Internal error in eval_type.");
1149 return expr
->elts
[1].type
;