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"
28 /* Values of NOSIDE argument to eval_subexp. */
31 EVAL_SKIP
, /* Only effect is to increment pos. */
32 EVAL_AVOID_SIDE_EFFECTS
/* Don't modify any variables or
33 call any functions. The value
34 returned will have the correct
35 type, and will have an
36 approximately correct lvalue
37 type (inaccuracy: anything that is
38 listed as being in a register in
39 the function in which it was
40 declared will be lval_register). */
43 /* Prototypes for local functions. */
46 evaluate_subexp_for_sizeof
PARAMS ((struct expression
*, int *));
49 evaluate_subexp_with_coercion
PARAMS ((struct expression
*, int *,
53 evaluate_subexp_for_address
PARAMS ((struct expression
*, int *,
57 evaluate_subexp
PARAMS ((struct type
*, struct expression
*, int *,
61 /* Parse the string EXP as a C expression, evaluate it,
62 and return the result as a number. */
65 parse_and_eval_address (exp
)
68 struct expression
*expr
= parse_expression (exp
);
69 register CORE_ADDR addr
;
70 register struct cleanup
*old_chain
=
71 make_cleanup (free_current_contents
, &expr
);
73 addr
= value_as_pointer (evaluate_expression (expr
));
74 do_cleanups (old_chain
);
78 /* Like parse_and_eval_address but takes a pointer to a char * variable
79 and advanced that variable across the characters parsed. */
82 parse_and_eval_address_1 (expptr
)
85 struct expression
*expr
= parse_exp_1 (expptr
, (struct block
*)0, 0);
86 register CORE_ADDR addr
;
87 register struct cleanup
*old_chain
=
88 make_cleanup (free_current_contents
, &expr
);
90 addr
= value_as_pointer (evaluate_expression (expr
));
91 do_cleanups (old_chain
);
99 struct expression
*expr
= parse_expression (exp
);
101 register struct cleanup
*old_chain
102 = make_cleanup (free_current_contents
, &expr
);
104 val
= evaluate_expression (expr
);
105 do_cleanups (old_chain
);
109 /* Parse up to a comma (or to a closeparen)
110 in the string EXPP as an expression, evaluate it, and return the value.
111 EXPP is advanced to point to the comma. */
114 parse_to_comma_and_eval (expp
)
117 struct expression
*expr
= parse_exp_1 (expp
, (struct block
*) 0, 1);
119 register struct cleanup
*old_chain
120 = make_cleanup (free_current_contents
, &expr
);
122 val
= evaluate_expression (expr
);
123 do_cleanups (old_chain
);
127 /* Evaluate an expression in internal prefix form
128 such as is constructed by parse.y.
130 See expression.h for info on the format of an expression. */
132 static value
evaluate_subexp ();
133 static value
evaluate_subexp_for_address ();
134 static value
evaluate_subexp_for_sizeof ();
135 static value
evaluate_subexp_with_coercion ();
138 evaluate_expression (exp
)
139 struct expression
*exp
;
142 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_NORMAL
);
145 /* Evaluate an expression, avoiding all memory references
146 and getting a value whose type alone is correct. */
150 struct expression
*exp
;
153 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
157 evaluate_subexp (expect_type
, exp
, pos
, noside
)
158 struct type
*expect_type
;
159 register struct expression
*exp
;
165 register int pc
, pc2
, oldpos
;
166 register value arg1
, arg2
, arg3
;
172 op
= exp
->elts
[pc
].opcode
;
177 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
178 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
179 / sizeof (union exp_element
));
180 arg1
= value_struct_elt_for_reference (exp
->elts
[pc
+ 1].type
,
181 exp
->elts
[pc
+ 1].type
,
182 &exp
->elts
[pc
+ 2].string
,
185 error ("There is no field named %s", &exp
->elts
[pc
+ 2].string
);
190 return value_from_longest (exp
->elts
[pc
+ 1].type
,
191 exp
->elts
[pc
+ 2].longconst
);
195 return value_from_double (exp
->elts
[pc
+ 1].type
,
196 exp
->elts
[pc
+ 2].doubleconst
);
200 if (noside
== EVAL_SKIP
)
202 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
204 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
207 switch (SYMBOL_CLASS (sym
))
211 case LOC_CONST_BYTES
:
225 return value_zero (SYMBOL_TYPE (sym
), lv
);
228 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
233 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
237 return value_of_register (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
241 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
244 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
245 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
246 / sizeof (union exp_element
));
247 if (noside
== EVAL_SKIP
)
249 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
252 /* Skip third and second args to evaluate the first one. */
253 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
254 if (value_zerop (arg1
))
256 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
257 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
261 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
262 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
268 op
= exp
->elts
[*pos
].opcode
;
269 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
273 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
274 /* First, evaluate the structure into arg2 */
277 if (noside
== EVAL_SKIP
)
280 if (op
== STRUCTOP_MEMBER
)
282 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
286 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
289 /* If the function is a virtual function, then the
290 aggregate value (providing the structure) plays
291 its part by providing the vtable. Otherwise,
292 it is just along for the ride: call the function
295 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
297 fnptr
= longest_to_int (value_as_long (arg1
));
298 /* FIXME-tiemann: this is way obsolete. */
301 struct type
*basetype
;
303 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
304 basetype
= TYPE_VPTR_BASETYPE (basetype
);
305 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
307 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
308 /* If one is virtual, then all are virtual. */
309 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
310 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
311 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
314 value base
= value_ind (arg2
);
315 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
317 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
318 fill_in_vptr_fieldno (basetype
);
320 VALUE_TYPE (base
) = basetype
;
321 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
322 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
323 VALUE_TYPE (arg1
) = builtin_type_int
;
324 arg1
= value_subscript (vtbl
, arg1
);
325 VALUE_TYPE (arg1
) = fntype
;
330 error ("virtual function at index %d not found", fnptr
);
334 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
338 /* Now, say which argument to start evaluating from */
341 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
343 /* Hair for method invocations */
346 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
347 /* First, evaluate the structure into arg2 */
349 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
350 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
351 if (noside
== EVAL_SKIP
)
354 if (op
== STRUCTOP_STRUCT
)
356 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
360 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
362 /* Now, say which argument to start evaluating from */
367 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
370 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
371 for (; tem
<= nargs
; tem
++)
372 /* Ensure that array expressions are coerced into pointer objects. */
373 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
375 /* signal end of arglist */
378 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
385 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
387 op
== STRUCTOP_STRUCT
388 ? "structure" : "structure pointer");
389 if (VALUE_OFFSET (temp
))
391 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
392 value_as_long (arg2
)+VALUE_OFFSET (temp
));
397 argvec
[1] = argvec
[0];
402 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
408 if (noside
== EVAL_SKIP
)
410 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
412 /* If the return type doesn't look like a function type, call an
413 error. This can happen if somebody tries to turn a variable into
414 a function call. This is here because people often want to
415 call, eg, strcmp, which gdb doesn't know is a function. If
416 gdb isn't asked for it's opinion (ie. through "whatis"),
417 it won't offer it. */
420 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
423 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
425 error ("Expression of type other than \"Function returning ...\" used as function");
427 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
429 case STRUCTOP_STRUCT
:
430 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
431 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
432 / sizeof (union exp_element
));
433 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
434 if (noside
== EVAL_SKIP
)
436 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
437 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
438 &exp
->elts
[pc
+ 1].string
,
444 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
445 (int *) 0, "structure");
449 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
450 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
451 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
452 if (noside
== EVAL_SKIP
)
454 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
455 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
457 &exp
->elts
[pc
+ 1].string
,
463 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
464 (int *) 0, "structure pointer");
467 case STRUCTOP_MEMBER
:
468 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
469 goto handle_pointer_to_member
;
471 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
472 handle_pointer_to_member
:
473 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
474 if (noside
== EVAL_SKIP
)
476 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
)
477 goto bad_pointer_to_member
;
478 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
479 if (TYPE_CODE (type
) == TYPE_CODE_METHOD
)
480 error ("not implemented: pointer-to-method in pointer-to-member construct");
481 if (TYPE_CODE (type
) != TYPE_CODE_MEMBER
)
482 goto bad_pointer_to_member
;
483 /* Now, convert these values to an address. */
484 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
486 arg3
= value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
487 value_as_long (arg1
) + value_as_long (arg2
));
488 return value_ind (arg3
);
489 bad_pointer_to_member
:
490 error("non-pointer-to-member value used in pointer-to-member construct");
493 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
494 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
495 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
497 if (binop_user_defined_p (op
, arg1
, arg2
))
498 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
500 return value_assign (arg1
, arg2
);
502 case BINOP_ASSIGN_MODIFY
:
504 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
505 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
506 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
508 op
= exp
->elts
[pc
+ 1].opcode
;
509 if (binop_user_defined_p (op
, arg1
, arg2
))
510 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
511 else if (op
== BINOP_ADD
)
512 arg2
= value_add (arg1
, arg2
);
513 else if (op
== BINOP_SUB
)
514 arg2
= value_sub (arg1
, arg2
);
516 arg2
= value_binop (arg1
, arg2
, op
);
517 return value_assign (arg1
, arg2
);
520 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
521 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
522 if (noside
== EVAL_SKIP
)
524 if (binop_user_defined_p (op
, arg1
, arg2
))
525 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
527 return value_add (arg1
, arg2
);
530 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
531 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
532 if (noside
== EVAL_SKIP
)
534 if (binop_user_defined_p (op
, arg1
, arg2
))
535 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
537 return value_sub (arg1
, arg2
);
547 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
548 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
549 if (noside
== EVAL_SKIP
)
551 if (binop_user_defined_p (op
, arg1
, arg2
))
552 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
554 if (noside
== EVAL_AVOID_SIDE_EFFECTS
556 return value_zero (VALUE_TYPE (arg1
), not_lval
);
558 return value_binop (arg1
, arg2
, op
);
560 case BINOP_SUBSCRIPT
:
561 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
562 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
563 if (noside
== EVAL_SKIP
)
565 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
566 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
569 if (binop_user_defined_p (op
, arg1
, arg2
))
570 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
572 return value_subscript (arg1
, arg2
);
575 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
576 if (noside
== EVAL_SKIP
)
578 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
583 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
586 if (binop_user_defined_p (op
, arg1
, arg2
))
588 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
589 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
593 tem
= value_zerop (arg1
);
594 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
595 (tem
? EVAL_SKIP
: noside
));
596 return value_from_longest (builtin_type_int
,
597 (LONGEST
) (!tem
&& !value_zerop (arg2
)));
601 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
602 if (noside
== EVAL_SKIP
)
604 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
609 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
612 if (binop_user_defined_p (op
, arg1
, arg2
))
614 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
615 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
619 tem
= value_zerop (arg1
);
620 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
621 (!tem
? EVAL_SKIP
: noside
));
622 return value_from_longest (builtin_type_int
,
623 (LONGEST
) (!tem
|| !value_zerop (arg2
)));
627 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
628 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
629 if (noside
== EVAL_SKIP
)
631 if (binop_user_defined_p (op
, arg1
, arg2
))
633 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
637 tem
= value_equal (arg1
, arg2
);
638 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
642 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
643 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
644 if (noside
== EVAL_SKIP
)
646 if (binop_user_defined_p (op
, arg1
, arg2
))
648 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
652 tem
= value_equal (arg1
, arg2
);
653 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
657 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
658 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
659 if (noside
== EVAL_SKIP
)
661 if (binop_user_defined_p (op
, arg1
, arg2
))
663 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
667 tem
= value_less (arg1
, arg2
);
668 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
672 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
673 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
674 if (noside
== EVAL_SKIP
)
676 if (binop_user_defined_p (op
, arg1
, arg2
))
678 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
682 tem
= value_less (arg2
, arg1
);
683 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
687 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
688 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
689 if (noside
== EVAL_SKIP
)
691 if (binop_user_defined_p (op
, arg1
, arg2
))
693 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
697 tem
= value_less (arg1
, arg2
);
698 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
702 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
703 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
704 if (noside
== EVAL_SKIP
)
706 if (binop_user_defined_p (op
, arg1
, arg2
))
708 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
712 tem
= value_less (arg2
, arg1
);
713 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
717 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
718 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
719 if (noside
== EVAL_SKIP
)
721 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
722 error ("Non-integral right operand for \"@\" operator.");
723 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
724 return allocate_repeat_value (VALUE_TYPE (arg1
),
725 longest_to_int (value_as_long (arg2
)));
727 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
730 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
731 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
734 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
735 if (noside
== EVAL_SKIP
)
737 if (unop_user_defined_p (op
, arg1
))
738 return value_x_unop (arg1
, op
);
740 return value_neg (arg1
);
743 /* C++: check for and handle destructor names. */
744 op
= exp
->elts
[*pos
].opcode
;
746 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
747 if (noside
== EVAL_SKIP
)
749 if (unop_user_defined_p (UNOP_LOGNOT
, arg1
))
750 return value_x_unop (arg1
, UNOP_LOGNOT
);
752 return value_lognot (arg1
);
755 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
756 if (noside
== EVAL_SKIP
)
758 if (unop_user_defined_p (op
, arg1
))
759 return value_x_unop (arg1
, op
);
761 return value_from_longest (builtin_type_int
,
762 (LONGEST
) value_zerop (arg1
));
765 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
766 expect_type
= TYPE_TARGET_TYPE (expect_type
);
767 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
768 if (noside
== EVAL_SKIP
)
770 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
772 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
773 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
774 /* In C you can dereference an array to get the 1st elt. */
775 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
777 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
779 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
780 /* GDB allows dereferencing an int. */
781 return value_zero (builtin_type_int
, lval_memory
);
783 error ("Attempt to take contents of a non-pointer value.");
785 return value_ind (arg1
);
788 /* C++: check for and handle pointer to members. */
790 op
= exp
->elts
[*pos
].opcode
;
792 if (noside
== EVAL_SKIP
)
796 char *name
= &exp
->elts
[pc
+3].string
;
797 int temm
= strlen (name
);
798 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
801 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
805 return evaluate_subexp_for_address (exp
, pos
, noside
);
808 if (noside
== EVAL_SKIP
)
810 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
813 return evaluate_subexp_for_sizeof (exp
, pos
);
817 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
818 if (noside
== EVAL_SKIP
)
820 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
824 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
825 if (noside
== EVAL_SKIP
)
827 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
828 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
830 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
831 value_as_pointer (arg1
));
833 case UNOP_PREINCREMENT
:
834 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
835 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
837 else if (unop_user_defined_p (op
, arg1
))
839 return value_x_unop (arg1
, op
);
843 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
845 return value_assign (arg1
, arg2
);
848 case UNOP_PREDECREMENT
:
849 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
850 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
852 else if (unop_user_defined_p (op
, arg1
))
854 return value_x_unop (arg1
, op
);
858 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
860 return value_assign (arg1
, arg2
);
863 case UNOP_POSTINCREMENT
:
864 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
865 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
867 else if (unop_user_defined_p (op
, arg1
))
869 return value_x_unop (arg1
, op
);
873 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
875 value_assign (arg1
, arg2
);
879 case UNOP_POSTDECREMENT
:
880 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
881 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
883 else if (unop_user_defined_p (op
, arg1
))
885 return value_x_unop (arg1
, op
);
889 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
891 value_assign (arg1
, arg2
);
897 return value_of_this (1);
900 error ("internal error: I do not know how to evaluate what you gave me");
904 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
907 /* Evaluate a subexpression of EXP, at index *POS,
908 and return the address of that subexpression.
909 Advance *POS over the subexpression.
910 If the subexpression isn't an lvalue, get an error.
911 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
912 then only the type of the result need be correct. */
915 evaluate_subexp_for_address (exp
, pos
, noside
)
916 register struct expression
*exp
;
925 op
= exp
->elts
[pc
].opcode
;
931 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
935 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
936 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
939 var
= exp
->elts
[pc
+ 1].symbol
;
941 /* C++: The "address" of a reference should yield the address
942 * of the object pointed to. Let value_addr() deal with it. */
943 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
947 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
950 lookup_pointer_type (SYMBOL_TYPE (var
));
951 enum address_class sym_class
= SYMBOL_CLASS (var
);
953 if (sym_class
== LOC_CONST
954 || sym_class
== LOC_CONST_BYTES
955 || sym_class
== LOC_REGISTER
956 || sym_class
== LOC_REGPARM
)
957 error ("Attempt to take address of register or constant.");
960 value_zero (type
, not_lval
);
963 return locate_var_value (var
, (FRAME
) 0);
967 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
969 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
970 if (VALUE_LVAL (x
) == lval_memory
)
971 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
974 error ("Attempt to take address of non-lval");
976 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
980 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
981 When used in contexts where arrays will be coerced anyway,
982 this is equivalent to `evaluate_subexp'
983 but much faster because it avoids actually fetching array contents. */
986 evaluate_subexp_with_coercion (exp
, pos
, noside
)
987 register struct expression
*exp
;
991 register enum exp_opcode op
;
997 op
= exp
->elts
[pc
].opcode
;
1002 var
= exp
->elts
[pc
+ 1].symbol
;
1003 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
)
1006 val
= locate_var_value (var
, (FRAME
) 0);
1007 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1011 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1015 /* Evaluate a subexpression of EXP, at index *POS,
1016 and return a value for the size of that subexpression.
1017 Advance *POS over the subexpression. */
1020 evaluate_subexp_for_sizeof (exp
, pos
)
1021 register struct expression
*exp
;
1029 op
= exp
->elts
[pc
].opcode
;
1033 /* This case is handled specially
1034 so that we avoid creating a value for the result type.
1035 If the result type is very big, it's desirable not to
1036 create a value unnecessarily. */
1039 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1040 return value_from_longest (builtin_type_int
, (LONGEST
)
1041 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1045 return value_from_longest (builtin_type_int
,
1046 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1050 return value_from_longest (builtin_type_int
,
1051 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1054 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1055 return value_from_longest (builtin_type_int
,
1056 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1060 /* Parse a type expression in the string [P..P+LENGTH). */
1063 parse_and_eval_type (p
, length
)
1067 char *tmp
= (char *)alloca (length
+ 4);
1068 struct expression
*expr
;
1070 bcopy (p
, tmp
+1, length
);
1071 tmp
[length
+1] = ')';
1072 tmp
[length
+2] = '0';
1073 tmp
[length
+3] = '\0';
1074 expr
= parse_expression (tmp
);
1075 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1076 error ("Internal error in eval_type.");
1077 return expr
->elts
[1].type
;