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
);
549 case BINOP_BITWISE_AND
:
550 case BINOP_BITWISE_IOR
:
551 case BINOP_BITWISE_XOR
:
552 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
553 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
554 if (noside
== EVAL_SKIP
)
556 if (binop_user_defined_p (op
, arg1
, arg2
))
557 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
559 if (noside
== EVAL_AVOID_SIDE_EFFECTS
560 && (op
== BINOP_DIV
|| op
== BINOP_REM
|| op
== BINOP_MOD
))
561 return value_zero (VALUE_TYPE (arg1
), not_lval
);
563 return value_binop (arg1
, arg2
, op
);
565 case BINOP_SUBSCRIPT
:
566 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
567 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
568 if (noside
== EVAL_SKIP
)
570 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
572 /* If the user attempts to subscript something that has no target
573 type (like a plain int variable for example), then report this
576 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
578 return value_zero (type
, VALUE_LVAL (arg1
));
580 error ("cannot subscript something of type `%s'",
581 TYPE_NAME (VALUE_TYPE (arg1
)));
584 if (binop_user_defined_p (op
, arg1
, arg2
))
585 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
587 return value_subscript (arg1
, arg2
);
589 case MULTI_SUBSCRIPT
:
591 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
592 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
595 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
596 /* FIXME: EVAL_SKIP handling may not be correct. */
597 if (noside
== EVAL_SKIP
)
608 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
609 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
611 /* If the user attempts to subscript something that has no target
612 type (like a plain int variable for example), then report this
615 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
618 arg1
= value_zero (type
, VALUE_LVAL (arg1
));
624 error ("cannot subscript something of type `%s'",
625 TYPE_NAME (VALUE_TYPE (arg1
)));
629 if (binop_user_defined_p (op
, arg1
, arg2
))
631 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
);
635 arg1
= value_subscript (arg1
, arg2
);
640 case BINOP_LOGICAL_AND
:
641 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
642 if (noside
== EVAL_SKIP
)
644 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
649 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
652 if (binop_user_defined_p (op
, arg1
, arg2
))
654 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
655 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
659 tem
= value_logical_not (arg1
);
660 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
661 (tem
? EVAL_SKIP
: noside
));
662 return value_from_longest (builtin_type_int
,
663 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
666 case BINOP_LOGICAL_OR
:
667 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
668 if (noside
== EVAL_SKIP
)
670 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
675 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
678 if (binop_user_defined_p (op
, arg1
, arg2
))
680 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
681 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
685 tem
= value_logical_not (arg1
);
686 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
687 (!tem
? EVAL_SKIP
: noside
));
688 return value_from_longest (builtin_type_int
,
689 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
693 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
694 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
695 if (noside
== EVAL_SKIP
)
697 if (binop_user_defined_p (op
, arg1
, arg2
))
699 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
703 tem
= value_equal (arg1
, arg2
);
704 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
708 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
709 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
710 if (noside
== EVAL_SKIP
)
712 if (binop_user_defined_p (op
, arg1
, arg2
))
714 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
718 tem
= value_equal (arg1
, arg2
);
719 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
723 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
724 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
725 if (noside
== EVAL_SKIP
)
727 if (binop_user_defined_p (op
, arg1
, arg2
))
729 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
733 tem
= value_less (arg1
, arg2
);
734 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
738 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
739 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
740 if (noside
== EVAL_SKIP
)
742 if (binop_user_defined_p (op
, arg1
, arg2
))
744 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
748 tem
= value_less (arg2
, arg1
);
749 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
753 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
754 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
755 if (noside
== EVAL_SKIP
)
757 if (binop_user_defined_p (op
, arg1
, arg2
))
759 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
763 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
764 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
768 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
769 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
770 if (noside
== EVAL_SKIP
)
772 if (binop_user_defined_p (op
, arg1
, arg2
))
774 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
778 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
779 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
783 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
784 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
785 if (noside
== EVAL_SKIP
)
787 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
788 error ("Non-integral right operand for \"@\" operator.");
789 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
790 return allocate_repeat_value (VALUE_TYPE (arg1
),
791 longest_to_int (value_as_long (arg2
)));
793 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
796 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
797 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
800 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
801 if (noside
== EVAL_SKIP
)
803 if (unop_user_defined_p (op
, arg1
))
804 return value_x_unop (arg1
, op
);
806 return value_neg (arg1
);
808 case UNOP_COMPLEMENT
:
809 /* C++: check for and handle destructor names. */
810 op
= exp
->elts
[*pos
].opcode
;
812 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
813 if (noside
== EVAL_SKIP
)
815 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
816 return value_x_unop (arg1
, UNOP_COMPLEMENT
);
818 return value_complement (arg1
);
820 case UNOP_LOGICAL_NOT
:
821 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
822 if (noside
== EVAL_SKIP
)
824 if (unop_user_defined_p (op
, arg1
))
825 return value_x_unop (arg1
, op
);
827 return value_from_longest (builtin_type_int
,
828 (LONGEST
) value_logical_not (arg1
));
831 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
832 expect_type
= TYPE_TARGET_TYPE (expect_type
);
833 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
834 if (noside
== EVAL_SKIP
)
836 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
838 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
839 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
840 /* In C you can dereference an array to get the 1st elt. */
841 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
843 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
845 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
846 /* GDB allows dereferencing an int. */
847 return value_zero (builtin_type_int
, lval_memory
);
849 error ("Attempt to take contents of a non-pointer value.");
851 return value_ind (arg1
);
854 /* C++: check for and handle pointer to members. */
856 op
= exp
->elts
[*pos
].opcode
;
858 if (noside
== EVAL_SKIP
)
862 int temm
= longest_to_int (exp
->elts
[pc
+3].longconst
);
863 (*pos
) += 3 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
866 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
870 return evaluate_subexp_for_address (exp
, pos
, noside
);
873 if (noside
== EVAL_SKIP
)
875 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
878 return evaluate_subexp_for_sizeof (exp
, pos
);
882 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
883 if (noside
== EVAL_SKIP
)
885 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
889 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
890 if (noside
== EVAL_SKIP
)
892 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
893 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
895 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
896 value_as_pointer (arg1
));
898 case UNOP_PREINCREMENT
:
899 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
900 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
902 else if (unop_user_defined_p (op
, arg1
))
904 return value_x_unop (arg1
, op
);
908 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
910 return value_assign (arg1
, arg2
);
913 case UNOP_PREDECREMENT
:
914 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
915 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
917 else if (unop_user_defined_p (op
, arg1
))
919 return value_x_unop (arg1
, op
);
923 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
925 return value_assign (arg1
, arg2
);
928 case UNOP_POSTINCREMENT
:
929 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
930 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
932 else if (unop_user_defined_p (op
, arg1
))
934 return value_x_unop (arg1
, op
);
938 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
940 value_assign (arg1
, arg2
);
944 case UNOP_POSTDECREMENT
:
945 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
946 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
948 else if (unop_user_defined_p (op
, arg1
))
950 return value_x_unop (arg1
, op
);
954 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
956 value_assign (arg1
, arg2
);
962 return value_of_this (1);
965 error ("internal error: I do not know how to evaluate what you gave me");
969 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
972 /* Evaluate a subexpression of EXP, at index *POS,
973 and return the address of that subexpression.
974 Advance *POS over the subexpression.
975 If the subexpression isn't an lvalue, get an error.
976 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
977 then only the type of the result need be correct. */
980 evaluate_subexp_for_address (exp
, pos
, noside
)
981 register struct expression
*exp
;
990 op
= exp
->elts
[pc
].opcode
;
996 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1000 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
1001 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1004 var
= exp
->elts
[pc
+ 1].symbol
;
1006 /* C++: The "address" of a reference should yield the address
1007 * of the object pointed to. Let value_addr() deal with it. */
1008 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
1012 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1015 lookup_pointer_type (SYMBOL_TYPE (var
));
1016 enum address_class sym_class
= SYMBOL_CLASS (var
);
1018 if (sym_class
== LOC_CONST
1019 || sym_class
== LOC_CONST_BYTES
1020 || sym_class
== LOC_REGISTER
1021 || sym_class
== LOC_REGPARM
)
1022 error ("Attempt to take address of register or constant.");
1025 value_zero (type
, not_lval
);
1028 return locate_var_value (var
, (FRAME
) 0);
1032 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1034 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1035 if (VALUE_LVAL (x
) == lval_memory
)
1036 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
1039 error ("Attempt to take address of non-lval");
1041 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1045 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1046 When used in contexts where arrays will be coerced anyway, this is
1047 equivalent to `evaluate_subexp' but much faster because it avoids
1048 actually fetching array contents.
1050 Note that we currently only do the coercion for C expressions, where
1051 arrays are zero based and the coercion is correct. For other languages,
1052 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1053 to decide if coercion is appropriate.
1058 evaluate_subexp_with_coercion (exp
, pos
, noside
)
1059 register struct expression
*exp
;
1063 register enum exp_opcode op
;
1069 op
= exp
->elts
[pc
].opcode
;
1074 var
= exp
->elts
[pc
+ 1].symbol
;
1075 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
1076 && CAST_IS_CONVERSION
)
1079 val
= locate_var_value (var
, (FRAME
) 0);
1080 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1084 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1088 /* Evaluate a subexpression of EXP, at index *POS,
1089 and return a value for the size of that subexpression.
1090 Advance *POS over the subexpression. */
1093 evaluate_subexp_for_sizeof (exp
, pos
)
1094 register struct expression
*exp
;
1102 op
= exp
->elts
[pc
].opcode
;
1106 /* This case is handled specially
1107 so that we avoid creating a value for the result type.
1108 If the result type is very big, it's desirable not to
1109 create a value unnecessarily. */
1112 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1113 return value_from_longest (builtin_type_int
, (LONGEST
)
1114 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1118 return value_from_longest (builtin_type_int
,
1119 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1123 return value_from_longest (builtin_type_int
,
1124 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1127 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1128 return value_from_longest (builtin_type_int
,
1129 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1133 /* Parse a type expression in the string [P..P+LENGTH). */
1136 parse_and_eval_type (p
, length
)
1140 char *tmp
= (char *)alloca (length
+ 4);
1141 struct expression
*expr
;
1143 memcpy (tmp
+1, p
, length
);
1144 tmp
[length
+1] = ')';
1145 tmp
[length
+2] = '0';
1146 tmp
[length
+3] = '\0';
1147 expr
= parse_expression (tmp
);
1148 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1149 error ("Internal error in eval_type.");
1150 return expr
->elts
[1].type
;