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
,
182 exp
->elts
[pc
+ 1].type
,
183 &exp
->elts
[pc
+ 2].string
,
186 error ("There is no field named %s", &exp
->elts
[pc
+ 2].string
);
191 return value_from_longest (exp
->elts
[pc
+ 1].type
,
192 exp
->elts
[pc
+ 2].longconst
);
196 return value_from_double (exp
->elts
[pc
+ 1].type
,
197 exp
->elts
[pc
+ 2].doubleconst
);
201 if (noside
== EVAL_SKIP
)
203 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
205 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
208 switch (SYMBOL_CLASS (sym
))
212 case LOC_CONST_BYTES
:
226 return value_zero (SYMBOL_TYPE (sym
), lv
);
229 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
234 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
238 return value_of_register (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
242 return value_from_longest (builtin_type_chill_bool
,
243 exp
->elts
[pc
+ 1].longconst
);
247 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
250 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
251 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
252 / sizeof (union exp_element
));
253 if (noside
== EVAL_SKIP
)
255 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
258 /* Skip third and second args to evaluate the first one. */
259 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
260 if (value_logical_not (arg1
))
262 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
263 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
267 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
268 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
274 op
= exp
->elts
[*pos
].opcode
;
275 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
279 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
280 /* First, evaluate the structure into arg2 */
283 if (noside
== EVAL_SKIP
)
286 if (op
== STRUCTOP_MEMBER
)
288 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
292 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
295 /* If the function is a virtual function, then the
296 aggregate value (providing the structure) plays
297 its part by providing the vtable. Otherwise,
298 it is just along for the ride: call the function
301 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
303 fnptr
= longest_to_int (value_as_long (arg1
));
305 if (METHOD_PTR_IS_VIRTUAL(fnptr
))
307 int fnoffset
= METHOD_PTR_TO_VOFFSET(fnptr
);
308 struct type
*basetype
;
309 struct type
*domain_type
=
310 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
312 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
313 if (domain_type
!= basetype
)
314 arg2
= value_cast(lookup_pointer_type (domain_type
), arg2
);
315 basetype
= TYPE_VPTR_BASETYPE (domain_type
);
316 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
318 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
319 /* If one is virtual, then all are virtual. */
320 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
321 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
322 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnoffset
)
324 value temp
= value_ind (arg2
);
325 arg1
= value_virtual_fn_field (&temp
, f
, j
, domain_type
, 0);
326 arg2
= value_addr (temp
);
331 error ("virtual function at index %d not found", fnoffset
);
335 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
339 /* Now, say which argument to start evaluating from */
342 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
344 /* Hair for method invocations */
347 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
348 /* First, evaluate the structure into arg2 */
350 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
351 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
352 if (noside
== EVAL_SKIP
)
355 if (op
== STRUCTOP_STRUCT
)
357 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
361 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
363 /* Now, say which argument to start evaluating from */
368 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
371 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
372 for (; tem
<= nargs
; tem
++)
373 /* Ensure that array expressions are coerced into pointer objects. */
374 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
376 /* signal end of arglist */
379 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
386 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
388 op
== STRUCTOP_STRUCT
389 ? "structure" : "structure pointer");
390 if (VALUE_OFFSET (temp
))
392 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
393 value_as_long (arg2
)+VALUE_OFFSET (temp
));
398 argvec
[1] = argvec
[0];
403 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
409 if (noside
== EVAL_SKIP
)
411 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
413 /* If the return type doesn't look like a function type, call an
414 error. This can happen if somebody tries to turn a variable into
415 a function call. This is here because people often want to
416 call, eg, strcmp, which gdb doesn't know is a function. If
417 gdb isn't asked for it's opinion (ie. through "whatis"),
418 it won't offer it. */
421 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
424 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
426 error ("Expression of type other than \"Function returning ...\" used as function");
428 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
430 case STRUCTOP_STRUCT
:
431 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
432 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
433 / sizeof (union exp_element
));
434 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
435 if (noside
== EVAL_SKIP
)
437 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
438 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
439 &exp
->elts
[pc
+ 1].string
,
445 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
446 (int *) 0, "structure");
450 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
451 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
452 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
453 if (noside
== EVAL_SKIP
)
455 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
456 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
458 &exp
->elts
[pc
+ 1].string
,
464 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
465 (int *) 0, "structure pointer");
468 case STRUCTOP_MEMBER
:
469 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
470 goto handle_pointer_to_member
;
472 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
473 handle_pointer_to_member
:
474 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
475 if (noside
== EVAL_SKIP
)
477 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
)
478 goto bad_pointer_to_member
;
479 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
480 if (TYPE_CODE (type
) == TYPE_CODE_METHOD
)
481 error ("not implemented: pointer-to-method in pointer-to-member construct");
482 if (TYPE_CODE (type
) != TYPE_CODE_MEMBER
)
483 goto bad_pointer_to_member
;
484 /* Now, convert these values to an address. */
485 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
487 arg3
= value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
488 value_as_long (arg1
) + value_as_long (arg2
));
489 return value_ind (arg3
);
490 bad_pointer_to_member
:
491 error("non-pointer-to-member value used in pointer-to-member construct");
494 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
495 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
496 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
498 if (binop_user_defined_p (op
, arg1
, arg2
))
499 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
501 return value_assign (arg1
, arg2
);
503 case BINOP_ASSIGN_MODIFY
:
505 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
506 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
507 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
509 op
= exp
->elts
[pc
+ 1].opcode
;
510 if (binop_user_defined_p (op
, arg1
, arg2
))
511 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
512 else if (op
== BINOP_ADD
)
513 arg2
= value_add (arg1
, arg2
);
514 else if (op
== BINOP_SUB
)
515 arg2
= value_sub (arg1
, arg2
);
517 arg2
= value_binop (arg1
, arg2
, op
);
518 return value_assign (arg1
, arg2
);
521 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
522 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
523 if (noside
== EVAL_SKIP
)
525 if (binop_user_defined_p (op
, arg1
, arg2
))
526 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
528 return value_add (arg1
, arg2
);
531 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
532 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
533 if (noside
== EVAL_SKIP
)
535 if (binop_user_defined_p (op
, arg1
, arg2
))
536 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
538 return value_sub (arg1
, arg2
);
545 case BINOP_BITWISE_AND
:
546 case BINOP_BITWISE_IOR
:
547 case BINOP_BITWISE_XOR
:
548 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
549 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
550 if (noside
== EVAL_SKIP
)
552 if (binop_user_defined_p (op
, arg1
, arg2
))
553 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
555 if (noside
== EVAL_AVOID_SIDE_EFFECTS
556 && (op
== BINOP_DIV
|| op
== BINOP_REM
))
557 return value_zero (VALUE_TYPE (arg1
), not_lval
);
559 return value_binop (arg1
, arg2
, op
);
561 case BINOP_SUBSCRIPT
:
562 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
563 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
564 if (noside
== EVAL_SKIP
)
566 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
568 /* If the user attempts to subscript something that has no target
569 type (like a plain int variable for example), then report this
572 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
574 return value_zero (type
, VALUE_LVAL (arg1
));
576 error ("cannot subscript something of type `%s'",
577 TYPE_NAME (VALUE_TYPE (arg1
)));
580 if (binop_user_defined_p (op
, arg1
, arg2
))
581 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
583 return value_subscript (arg1
, arg2
);
585 case BINOP_LOGICAL_AND
:
586 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
587 if (noside
== EVAL_SKIP
)
589 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
594 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
597 if (binop_user_defined_p (op
, arg1
, arg2
))
599 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
600 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
604 tem
= value_logical_not (arg1
);
605 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
606 (tem
? EVAL_SKIP
: noside
));
607 return value_from_longest (builtin_type_int
,
608 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
611 case BINOP_LOGICAL_OR
:
612 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
613 if (noside
== EVAL_SKIP
)
615 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
620 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
623 if (binop_user_defined_p (op
, arg1
, arg2
))
625 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
626 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
630 tem
= value_logical_not (arg1
);
631 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
632 (!tem
? EVAL_SKIP
: noside
));
633 return value_from_longest (builtin_type_int
,
634 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
638 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
639 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
640 if (noside
== EVAL_SKIP
)
642 if (binop_user_defined_p (op
, arg1
, arg2
))
644 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
648 tem
= value_equal (arg1
, arg2
);
649 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
653 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
654 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
655 if (noside
== EVAL_SKIP
)
657 if (binop_user_defined_p (op
, arg1
, arg2
))
659 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
663 tem
= value_equal (arg1
, arg2
);
664 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
668 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
669 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
670 if (noside
== EVAL_SKIP
)
672 if (binop_user_defined_p (op
, arg1
, arg2
))
674 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
678 tem
= value_less (arg1
, arg2
);
679 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
683 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
684 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
685 if (noside
== EVAL_SKIP
)
687 if (binop_user_defined_p (op
, arg1
, arg2
))
689 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
693 tem
= value_less (arg2
, arg1
);
694 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
698 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
699 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
700 if (noside
== EVAL_SKIP
)
702 if (binop_user_defined_p (op
, arg1
, arg2
))
704 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
708 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
709 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
713 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
714 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
715 if (noside
== EVAL_SKIP
)
717 if (binop_user_defined_p (op
, arg1
, arg2
))
719 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
723 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
724 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
728 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
729 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
730 if (noside
== EVAL_SKIP
)
732 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
733 error ("Non-integral right operand for \"@\" operator.");
734 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
735 return allocate_repeat_value (VALUE_TYPE (arg1
),
736 longest_to_int (value_as_long (arg2
)));
738 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
741 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
742 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
745 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
746 if (noside
== EVAL_SKIP
)
748 if (unop_user_defined_p (op
, arg1
))
749 return value_x_unop (arg1
, op
);
751 return value_neg (arg1
);
753 case UNOP_COMPLEMENT
:
754 /* C++: check for and handle destructor names. */
755 op
= exp
->elts
[*pos
].opcode
;
757 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
758 if (noside
== EVAL_SKIP
)
760 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
761 return value_x_unop (arg1
, UNOP_COMPLEMENT
);
763 return value_complement (arg1
);
765 case UNOP_LOGICAL_NOT
:
766 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
767 if (noside
== EVAL_SKIP
)
769 if (unop_user_defined_p (op
, arg1
))
770 return value_x_unop (arg1
, op
);
772 return value_from_longest (builtin_type_int
,
773 (LONGEST
) value_logical_not (arg1
));
776 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
777 expect_type
= TYPE_TARGET_TYPE (expect_type
);
778 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
779 if (noside
== EVAL_SKIP
)
781 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
783 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
784 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
785 /* In C you can dereference an array to get the 1st elt. */
786 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
788 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
790 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
791 /* GDB allows dereferencing an int. */
792 return value_zero (builtin_type_int
, lval_memory
);
794 error ("Attempt to take contents of a non-pointer value.");
796 return value_ind (arg1
);
799 /* C++: check for and handle pointer to members. */
801 op
= exp
->elts
[*pos
].opcode
;
803 if (noside
== EVAL_SKIP
)
807 char *name
= &exp
->elts
[pc
+3].string
;
808 int temm
= strlen (name
);
809 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
812 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
816 return evaluate_subexp_for_address (exp
, pos
, noside
);
819 if (noside
== EVAL_SKIP
)
821 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
824 return evaluate_subexp_for_sizeof (exp
, pos
);
828 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
829 if (noside
== EVAL_SKIP
)
831 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
835 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
836 if (noside
== EVAL_SKIP
)
838 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
839 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
841 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
842 value_as_pointer (arg1
));
844 case UNOP_PREINCREMENT
:
845 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
846 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
848 else if (unop_user_defined_p (op
, arg1
))
850 return value_x_unop (arg1
, op
);
854 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
856 return value_assign (arg1
, arg2
);
859 case UNOP_PREDECREMENT
:
860 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
861 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
863 else if (unop_user_defined_p (op
, arg1
))
865 return value_x_unop (arg1
, op
);
869 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
871 return value_assign (arg1
, arg2
);
874 case UNOP_POSTINCREMENT
:
875 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
876 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
878 else if (unop_user_defined_p (op
, arg1
))
880 return value_x_unop (arg1
, op
);
884 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
886 value_assign (arg1
, arg2
);
890 case UNOP_POSTDECREMENT
:
891 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
892 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
894 else if (unop_user_defined_p (op
, arg1
))
896 return value_x_unop (arg1
, op
);
900 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
902 value_assign (arg1
, arg2
);
908 return value_of_this (1);
911 error ("internal error: I do not know how to evaluate what you gave me");
915 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
918 /* Evaluate a subexpression of EXP, at index *POS,
919 and return the address of that subexpression.
920 Advance *POS over the subexpression.
921 If the subexpression isn't an lvalue, get an error.
922 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
923 then only the type of the result need be correct. */
926 evaluate_subexp_for_address (exp
, pos
, noside
)
927 register struct expression
*exp
;
936 op
= exp
->elts
[pc
].opcode
;
942 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
946 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
947 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
950 var
= exp
->elts
[pc
+ 1].symbol
;
952 /* C++: The "address" of a reference should yield the address
953 * of the object pointed to. Let value_addr() deal with it. */
954 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
958 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
961 lookup_pointer_type (SYMBOL_TYPE (var
));
962 enum address_class sym_class
= SYMBOL_CLASS (var
);
964 if (sym_class
== LOC_CONST
965 || sym_class
== LOC_CONST_BYTES
966 || sym_class
== LOC_REGISTER
967 || sym_class
== LOC_REGPARM
)
968 error ("Attempt to take address of register or constant.");
971 value_zero (type
, not_lval
);
974 return locate_var_value (var
, (FRAME
) 0);
978 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
980 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
981 if (VALUE_LVAL (x
) == lval_memory
)
982 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
985 error ("Attempt to take address of non-lval");
987 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
991 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
992 When used in contexts where arrays will be coerced anyway,
993 this is equivalent to `evaluate_subexp'
994 but much faster because it avoids actually fetching array contents. */
997 evaluate_subexp_with_coercion (exp
, pos
, noside
)
998 register struct expression
*exp
;
1002 register enum exp_opcode op
;
1008 op
= exp
->elts
[pc
].opcode
;
1013 var
= exp
->elts
[pc
+ 1].symbol
;
1014 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
)
1017 val
= locate_var_value (var
, (FRAME
) 0);
1018 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1022 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1026 /* Evaluate a subexpression of EXP, at index *POS,
1027 and return a value for the size of that subexpression.
1028 Advance *POS over the subexpression. */
1031 evaluate_subexp_for_sizeof (exp
, pos
)
1032 register struct expression
*exp
;
1040 op
= exp
->elts
[pc
].opcode
;
1044 /* This case is handled specially
1045 so that we avoid creating a value for the result type.
1046 If the result type is very big, it's desirable not to
1047 create a value unnecessarily. */
1050 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1051 return value_from_longest (builtin_type_int
, (LONGEST
)
1052 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1056 return value_from_longest (builtin_type_int
,
1057 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1061 return value_from_longest (builtin_type_int
,
1062 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1065 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1066 return value_from_longest (builtin_type_int
,
1067 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1071 /* Parse a type expression in the string [P..P+LENGTH). */
1074 parse_and_eval_type (p
, length
)
1078 char *tmp
= (char *)alloca (length
+ 4);
1079 struct expression
*expr
;
1081 memcpy (tmp
+1, p
, length
);
1082 tmp
[length
+1] = ')';
1083 tmp
[length
+2] = '0';
1084 tmp
[length
+3] = '\0';
1085 expr
= parse_expression (tmp
);
1086 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1087 error ("Internal error in eval_type.");
1088 return expr
->elts
[1].type
;