1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987, 1989, 1991 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 #define NULL_TYPE ((struct type *)0)
31 /* Parse the string EXP as a C expression, evaluate it,
32 and return the result as a number. */
35 parse_and_eval_address (exp
)
38 struct expression
*expr
= parse_expression (exp
);
39 register CORE_ADDR addr
;
40 register struct cleanup
*old_chain
41 = make_cleanup (free_current_contents
, &expr
);
43 addr
= value_as_pointer (evaluate_expression (expr
));
44 do_cleanups (old_chain
);
48 /* Like parse_and_eval_address but takes a pointer to a char * variable
49 and advanced that variable across the characters parsed. */
52 parse_and_eval_address_1 (expptr
)
55 struct expression
*expr
= parse_exp_1 (expptr
, (struct block
*)0, 0);
56 register CORE_ADDR addr
;
57 register struct cleanup
*old_chain
58 = make_cleanup (free_current_contents
, &expr
);
60 addr
= value_as_pointer (evaluate_expression (expr
));
61 do_cleanups (old_chain
);
69 struct expression
*expr
= parse_expression (exp
);
71 register struct cleanup
*old_chain
72 = make_cleanup (free_current_contents
, &expr
);
74 val
= evaluate_expression (expr
);
75 do_cleanups (old_chain
);
79 /* Parse up to a comma (or to a closeparen)
80 in the string EXPP as an expression, evaluate it, and return the value.
81 EXPP is advanced to point to the comma. */
84 parse_to_comma_and_eval (expp
)
87 struct expression
*expr
= parse_exp_1 (expp
, (struct block
*) 0, 1);
89 register struct cleanup
*old_chain
90 = make_cleanup (free_current_contents
, &expr
);
92 val
= evaluate_expression (expr
);
93 do_cleanups (old_chain
);
97 /* Evaluate an expression in internal prefix form
98 such as is constructed by parse.y.
100 See expression.h for info on the format of an expression. */
102 static value
evaluate_subexp ();
103 static value
evaluate_subexp_for_address ();
104 static value
evaluate_subexp_for_sizeof ();
105 static value
evaluate_subexp_with_coercion ();
107 /* Values of NOSIDE argument to eval_subexp. */
110 EVAL_SKIP
, /* Only effect is to increment pos. */
111 EVAL_AVOID_SIDE_EFFECTS
/* Don't modify any variables or
112 call any functions. The value
113 returned will have the correct
114 type, and will have an
115 approximately correct lvalue
116 type (inaccuracy: anything that is
117 listed as being in a register in
118 the function in which it was
119 declared will be lval_register). */
123 evaluate_expression (exp
)
124 struct expression
*exp
;
127 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_NORMAL
);
130 /* Evaluate an expression, avoiding all memory references
131 and getting a value whose type alone is correct. */
135 struct expression
*exp
;
138 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
142 evaluate_subexp (expect_type
, exp
, pos
, noside
)
143 struct type
*expect_type
;
144 register struct expression
*exp
;
150 register int pc
, pc2
, oldpos
;
151 register value arg1
, arg2
, arg3
;
156 op
= exp
->elts
[pc
].opcode
;
161 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
162 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
163 / sizeof (union exp_element
));
164 arg1
= value_static_field (exp
->elts
[pc
+ 1].type
,
165 &exp
->elts
[pc
+ 2].string
, -1);
167 error ("There is no field named %s", &exp
->elts
[pc
+ 2].string
);
172 return value_from_longest (exp
->elts
[pc
+ 1].type
,
173 exp
->elts
[pc
+ 2].longconst
);
177 return value_from_double (exp
->elts
[pc
+ 1].type
,
178 exp
->elts
[pc
+ 2].doubleconst
);
182 if (noside
== EVAL_SKIP
)
184 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
186 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
189 switch (SYMBOL_CLASS (sym
))
193 case LOC_CONST_BYTES
:
207 return value_zero (SYMBOL_TYPE (sym
), lv
);
210 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
215 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
219 return value_of_register (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
223 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
226 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
227 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
228 / sizeof (union exp_element
));
229 if (noside
== EVAL_SKIP
)
231 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
234 /* Skip third and second args to evaluate the first one. */
235 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
236 if (value_zerop (arg1
))
238 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
239 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
243 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
244 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
250 op
= exp
->elts
[*pos
].opcode
;
251 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
255 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
256 /* First, evaluate the structure into arg2 */
259 if (noside
== EVAL_SKIP
)
262 if (op
== STRUCTOP_MEMBER
)
264 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
268 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
271 /* If the function is a virtual function, then the
272 aggregate value (providing the structure) plays
273 its part by providing the vtable. Otherwise,
274 it is just along for the ride: call the function
277 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
279 fnptr
= longest_to_int (value_as_long (arg1
));
280 /* FIXME-tiemann: this is way obsolete. */
283 struct type
*basetype
;
285 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
286 basetype
= TYPE_VPTR_BASETYPE (basetype
);
287 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
289 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
290 /* If one is virtual, then all are virtual. */
291 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
292 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
293 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
296 value base
= value_ind (arg2
);
297 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
299 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
300 fill_in_vptr_fieldno (basetype
);
302 VALUE_TYPE (base
) = basetype
;
303 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
304 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
305 VALUE_TYPE (arg1
) = builtin_type_int
;
306 arg1
= value_subscript (vtbl
, arg1
);
307 VALUE_TYPE (arg1
) = fntype
;
312 error ("virtual function at index %d not found", fnptr
);
316 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
320 /* Now, say which argument to start evaluating from */
323 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
325 /* Hair for method invocations */
328 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
329 /* First, evaluate the structure into arg2 */
331 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
332 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
333 if (noside
== EVAL_SKIP
)
336 if (op
== STRUCTOP_STRUCT
)
338 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
342 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
344 /* Now, say which argument to start evaluating from */
349 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
352 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
353 for (; tem
<= nargs
; tem
++)
354 /* Ensure that array expressions are coerced into pointer objects. */
355 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
357 /* signal end of arglist */
360 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
367 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
369 op
== STRUCTOP_STRUCT
370 ? "structure" : "structure pointer");
371 if (VALUE_OFFSET (temp
))
373 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
374 value_as_long (arg2
)+VALUE_OFFSET (temp
));
379 argvec
[1] = argvec
[0];
384 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
390 if (noside
== EVAL_SKIP
)
392 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
394 /* If the return type doesn't look like a function type, call an
395 error. This can happen if somebody tries to turn a variable into
396 a function call. This is here because people often want to
397 call, eg, strcmp, which gdb doesn't know is a function. If
398 gdb isn't asked for it's opinion (ie. through "whatis"),
399 it won't offer it. */
402 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
405 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
407 error ("Expression of type other than \"Function returning ...\" used as function");
409 return target_call_function (argvec
[0], nargs
, argvec
+ 1);
411 case STRUCTOP_STRUCT
:
412 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
413 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
414 / sizeof (union exp_element
));
415 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
416 if (noside
== EVAL_SKIP
)
418 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
419 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
420 &exp
->elts
[pc
+ 1].string
,
426 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
427 (int *) 0, "structure");
431 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
432 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / 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 (TYPE_TARGET_TYPE
439 &exp
->elts
[pc
+ 1].string
,
445 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
446 (int *) 0, "structure pointer");
449 case STRUCTOP_MEMBER
:
450 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
451 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
452 if (noside
== EVAL_SKIP
)
454 /* Now, convert these values to an address. */
455 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
456 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
458 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
459 != TYPE_CODE_METHOD
)))
460 error ("non-pointer-to-member value used in pointer-to-member construct");
461 arg3
= value_from_longest (
462 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))),
463 value_as_long (arg1
) + value_as_long (arg2
));
464 return value_ind (arg3
);
467 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
468 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
469 if (noside
== EVAL_SKIP
)
471 /* Now, convert these values to an address. */
472 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
473 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_MEMBER
474 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_METHOD
))
475 error ("non-pointer-to-member value used in pointer-to-member construct");
476 arg3
= value_from_longest (
477 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))),
478 value_as_long (arg1
) + value_as_long (arg2
));
479 return value_ind (arg3
);
482 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
483 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
484 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
486 if (binop_user_defined_p (op
, arg1
, arg2
))
487 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
489 return value_assign (arg1
, arg2
);
491 case BINOP_ASSIGN_MODIFY
:
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 op
= exp
->elts
[pc
+ 1].opcode
;
498 if (binop_user_defined_p (op
, arg1
, arg2
))
499 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
500 else if (op
== BINOP_ADD
)
501 arg2
= value_add (arg1
, arg2
);
502 else if (op
== BINOP_SUB
)
503 arg2
= value_sub (arg1
, arg2
);
505 arg2
= value_binop (arg1
, arg2
, op
);
506 return value_assign (arg1
, arg2
);
509 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
510 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
511 if (noside
== EVAL_SKIP
)
513 if (binop_user_defined_p (op
, arg1
, arg2
))
514 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
516 return value_add (arg1
, arg2
);
519 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
520 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
521 if (noside
== EVAL_SKIP
)
523 if (binop_user_defined_p (op
, arg1
, arg2
))
524 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
526 return value_sub (arg1
, arg2
);
536 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
537 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
538 if (noside
== EVAL_SKIP
)
540 if (binop_user_defined_p (op
, arg1
, arg2
))
541 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
543 if (noside
== EVAL_AVOID_SIDE_EFFECTS
545 return value_zero (VALUE_TYPE (arg1
), not_lval
);
547 return value_binop (arg1
, arg2
, op
);
549 case BINOP_SUBSCRIPT
:
550 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
551 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
552 if (noside
== EVAL_SKIP
)
554 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
555 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
558 if (binop_user_defined_p (op
, arg1
, arg2
))
559 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
561 return value_subscript (arg1
, arg2
);
564 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
565 if (noside
== EVAL_SKIP
)
567 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
572 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
575 if (binop_user_defined_p (op
, arg1
, arg2
))
577 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
578 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
582 tem
= value_zerop (arg1
);
583 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
584 (tem
? EVAL_SKIP
: noside
));
585 return value_from_longest (builtin_type_int
,
586 (LONGEST
) (!tem
&& !value_zerop (arg2
)));
590 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
591 if (noside
== EVAL_SKIP
)
593 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
598 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
601 if (binop_user_defined_p (op
, arg1
, arg2
))
603 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
604 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
608 tem
= value_zerop (arg1
);
609 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
610 (!tem
? EVAL_SKIP
: noside
));
611 return value_from_longest (builtin_type_int
,
612 (LONGEST
) (!tem
|| !value_zerop (arg2
)));
616 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
617 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
618 if (noside
== EVAL_SKIP
)
620 if (binop_user_defined_p (op
, arg1
, arg2
))
622 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
626 tem
= value_equal (arg1
, arg2
);
627 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
631 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
632 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
633 if (noside
== EVAL_SKIP
)
635 if (binop_user_defined_p (op
, arg1
, arg2
))
637 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
641 tem
= value_equal (arg1
, arg2
);
642 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
646 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
647 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
648 if (noside
== EVAL_SKIP
)
650 if (binop_user_defined_p (op
, arg1
, arg2
))
652 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
656 tem
= value_less (arg1
, arg2
);
657 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
661 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
662 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
663 if (noside
== EVAL_SKIP
)
665 if (binop_user_defined_p (op
, arg1
, arg2
))
667 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
671 tem
= value_less (arg2
, arg1
);
672 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
676 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
677 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
678 if (noside
== EVAL_SKIP
)
680 if (binop_user_defined_p (op
, arg1
, arg2
))
682 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
686 tem
= value_less (arg1
, arg2
);
687 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
691 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
692 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
693 if (noside
== EVAL_SKIP
)
695 if (binop_user_defined_p (op
, arg1
, arg2
))
697 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
701 tem
= value_less (arg2
, arg1
);
702 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
706 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
707 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
708 if (noside
== EVAL_SKIP
)
710 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
711 error ("Non-integral right operand for \"@\" operator.");
712 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
713 return allocate_repeat_value (VALUE_TYPE (arg1
),
714 longest_to_int (value_as_long (arg2
)));
716 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
719 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
720 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
723 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
724 if (noside
== EVAL_SKIP
)
726 if (unop_user_defined_p (op
, arg1
))
727 return value_x_unop (arg1
, op
);
729 return value_neg (arg1
);
732 /* C++: check for and handle destructor names. */
733 op
= exp
->elts
[*pos
].opcode
;
735 /* FIXME-tiemann: this is a cop-out. */
737 error ("destructor in eval");
739 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
740 if (noside
== EVAL_SKIP
)
742 if (unop_user_defined_p (UNOP_LOGNOT
, arg1
))
743 return value_x_unop (arg1
, UNOP_LOGNOT
);
745 return value_lognot (arg1
);
748 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
749 if (noside
== EVAL_SKIP
)
751 if (unop_user_defined_p (op
, arg1
))
752 return value_x_unop (arg1
, op
);
754 return value_from_longest (builtin_type_int
,
755 (LONGEST
) value_zerop (arg1
));
758 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
759 expect_type
= TYPE_TARGET_TYPE (expect_type
);
760 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
761 if (noside
== EVAL_SKIP
)
763 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
765 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
766 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
767 /* In C you can dereference an array to get the 1st elt. */
768 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
770 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
772 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
773 /* GDB allows dereferencing an int. */
774 return value_zero (builtin_type_int
, lval_memory
);
776 error ("Attempt to take contents of a non-pointer value.");
778 return value_ind (arg1
);
781 /* C++: check for and handle pointer to members. */
783 op
= exp
->elts
[*pos
].opcode
;
785 if (noside
== EVAL_SKIP
)
789 char *name
= &exp
->elts
[pc
+3].string
;
790 int temm
= strlen (name
);
791 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
794 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
800 char *name
= &exp
->elts
[pc
+3].string
;
801 int temm
= strlen (name
);
802 struct type
*domain
= exp
->elts
[pc
+2].type
;
803 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
804 arg1
= value_struct_elt_for_address (domain
, expect_type
, name
);
807 error ("no field `%s' in structure", name
);
810 return evaluate_subexp_for_address (exp
, pos
, noside
);
813 if (noside
== EVAL_SKIP
)
815 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
818 return evaluate_subexp_for_sizeof (exp
, pos
);
822 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
823 if (noside
== EVAL_SKIP
)
825 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
829 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
830 if (noside
== EVAL_SKIP
)
832 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
833 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
835 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
836 value_as_pointer (arg1
));
838 case UNOP_PREINCREMENT
:
839 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
840 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
842 else if (unop_user_defined_p (op
, arg1
))
844 return value_x_unop (arg1
, op
);
848 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
850 return value_assign (arg1
, arg2
);
853 case UNOP_PREDECREMENT
:
854 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
855 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
857 else if (unop_user_defined_p (op
, arg1
))
859 return value_x_unop (arg1
, op
);
863 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
865 return value_assign (arg1
, arg2
);
868 case UNOP_POSTINCREMENT
:
869 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
870 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
872 else if (unop_user_defined_p (op
, arg1
))
874 return value_x_unop (arg1
, op
);
878 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
880 value_assign (arg1
, arg2
);
884 case UNOP_POSTDECREMENT
:
885 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
886 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
888 else if (unop_user_defined_p (op
, arg1
))
890 return value_x_unop (arg1
, op
);
894 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
896 value_assign (arg1
, arg2
);
902 return value_of_this (1);
905 error ("internal error: I do not know how to evaluate what you gave me");
909 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
912 /* Evaluate a subexpression of EXP, at index *POS,
913 and return the address of that subexpression.
914 Advance *POS over the subexpression.
915 If the subexpression isn't an lvalue, get an error.
916 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
917 then only the type of the result need be correct. */
920 evaluate_subexp_for_address (exp
, pos
, noside
)
921 register struct expression
*exp
;
929 op
= exp
->elts
[pc
].opcode
;
935 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
939 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
940 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
944 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
947 lookup_pointer_type (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
));
948 enum address_class sym_class
=
949 SYMBOL_CLASS (exp
->elts
[pc
+ 1].symbol
);
951 if (sym_class
== LOC_CONST
952 || sym_class
== LOC_CONST_BYTES
953 || sym_class
== LOC_REGISTER
954 || sym_class
== LOC_REGPARM
)
955 error ("Attempt to take address of register or constant.");
958 value_zero (type
, not_lval
);
961 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (FRAME
) 0);
964 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
966 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
967 if (VALUE_LVAL (x
) == lval_memory
)
968 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
971 error ("Attempt to take address of non-lval");
973 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
977 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
978 When used in contexts where arrays will be coerced anyway,
979 this is equivalent to `evaluate_subexp'
980 but much faster because it avoids actually fetching array contents. */
983 evaluate_subexp_with_coercion (exp
, pos
, noside
)
984 register struct expression
*exp
;
988 register enum exp_opcode op
;
993 op
= exp
->elts
[pc
].opcode
;
998 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
1001 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (FRAME
) 0);
1002 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
1006 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1010 /* Evaluate a subexpression of EXP, at index *POS,
1011 and return a value for the size of that subexpression.
1012 Advance *POS over the subexpression. */
1015 evaluate_subexp_for_sizeof (exp
, pos
)
1016 register struct expression
*exp
;
1024 op
= exp
->elts
[pc
].opcode
;
1028 /* This case is handled specially
1029 so that we avoid creating a value for the result type.
1030 If the result type is very big, it's desirable not to
1031 create a value unnecessarily. */
1034 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1035 return value_from_longest (builtin_type_int
, (LONGEST
)
1036 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1040 return value_from_longest (builtin_type_int
,
1041 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1045 return value_from_longest (builtin_type_int
,
1046 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1049 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1050 return value_from_longest (builtin_type_int
,
1051 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1055 /* Parse a type expression in the string [P..P+LENGTH). */
1058 parse_and_eval_type (p
, length
)
1062 char *tmp
= (char *)alloca (length
+ 4);
1063 struct expression
*expr
;
1065 bcopy (p
, tmp
+1, length
);
1066 tmp
[length
+1] = ')';
1067 tmp
[length
+2] = '0';
1068 tmp
[length
+3] = '\0';
1069 expr
= parse_expression (tmp
);
1070 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1071 error ("Internal error in eval_type.");
1072 return expr
->elts
[1].type
;