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 + BYTES_TO_EXP_ELEM (tem
+ 1);
180 arg1
= value_struct_elt_for_reference (exp
->elts
[pc
+ 1].type
,
182 exp
->elts
[pc
+ 1].type
,
183 &exp
->elts
[pc
+ 3].string
,
186 error ("There is no field named %s", &exp
->elts
[pc
+ 3].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
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
251 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
252 if (noside
== EVAL_SKIP
)
254 return value_string (&exp
->elts
[pc
+ 2].string
, tem
);
257 error ("support for OP_BITSTRING unimplemented");
262 tem2
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
263 tem3
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
264 nargs
= tem3
- tem2
+ 1;
265 argvec
= (value
*) alloca (sizeof (value
) * nargs
);
266 for (tem
= 0; tem
< nargs
; tem
++)
268 /* Ensure that array expressions are coerced into pointer objects. */
269 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
271 if (noside
== EVAL_SKIP
)
273 return (value_array (tem2
, tem3
, argvec
));
277 /* Skip third and second args to evaluate the first one. */
278 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
279 if (value_logical_not (arg1
))
281 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
282 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
286 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
287 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
293 op
= exp
->elts
[*pos
].opcode
;
294 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
298 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
299 /* First, evaluate the structure into arg2 */
302 if (noside
== EVAL_SKIP
)
305 if (op
== STRUCTOP_MEMBER
)
307 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
311 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
314 /* If the function is a virtual function, then the
315 aggregate value (providing the structure) plays
316 its part by providing the vtable. Otherwise,
317 it is just along for the ride: call the function
320 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
322 fnptr
= longest_to_int (value_as_long (arg1
));
324 if (METHOD_PTR_IS_VIRTUAL(fnptr
))
326 int fnoffset
= METHOD_PTR_TO_VOFFSET(fnptr
);
327 struct type
*basetype
;
328 struct type
*domain_type
=
329 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
331 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
332 if (domain_type
!= basetype
)
333 arg2
= value_cast(lookup_pointer_type (domain_type
), arg2
);
334 basetype
= TYPE_VPTR_BASETYPE (domain_type
);
335 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
337 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
338 /* If one is virtual, then all are virtual. */
339 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
340 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
341 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnoffset
)
343 value temp
= value_ind (arg2
);
344 arg1
= value_virtual_fn_field (&temp
, f
, j
, domain_type
, 0);
345 arg2
= value_addr (temp
);
350 error ("virtual function at index %d not found", fnoffset
);
354 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
358 /* Now, say which argument to start evaluating from */
361 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
363 /* Hair for method invocations */
366 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
367 /* First, evaluate the structure into arg2 */
369 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
370 *pos
+= 3 + BYTES_TO_EXP_ELEM (tem2
+ 1);
371 if (noside
== EVAL_SKIP
)
374 if (op
== STRUCTOP_STRUCT
)
376 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
380 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
382 /* Now, say which argument to start evaluating from */
387 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
390 /* Allocate arg vector, including space for the function to be
391 called in argvec[0] and a terminating NULL */
392 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
393 for (; tem
<= nargs
; tem
++)
394 /* Ensure that array expressions are coerced into pointer objects. */
395 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
397 /* signal end of arglist */
400 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
407 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 2].string
,
409 op
== STRUCTOP_STRUCT
410 ? "structure" : "structure pointer");
411 if (VALUE_OFFSET (temp
))
413 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
414 VALUE_ADDRESS (temp
)+VALUE_OFFSET (temp
));
419 argvec
[1] = argvec
[0];
424 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
430 if (noside
== EVAL_SKIP
)
432 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
434 /* If the return type doesn't look like a function type, call an
435 error. This can happen if somebody tries to turn a variable into
436 a function call. This is here because people often want to
437 call, eg, strcmp, which gdb doesn't know is a function. If
438 gdb isn't asked for it's opinion (ie. through "whatis"),
439 it won't offer it. */
442 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
445 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
447 error ("Expression of type other than \"Function returning ...\" used as function");
449 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
451 case STRUCTOP_STRUCT
:
452 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
453 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
454 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
455 if (noside
== EVAL_SKIP
)
457 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
458 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
459 &exp
->elts
[pc
+ 2].string
,
465 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
466 (int *) 0, "structure");
470 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
471 (*pos
) += 3 + BYTES_TO_EXP_ELEM (tem
+ 1);
472 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
473 if (noside
== EVAL_SKIP
)
475 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
476 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
477 &exp
->elts
[pc
+ 2].string
,
483 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
484 (int *) 0, "structure pointer");
487 case STRUCTOP_MEMBER
:
488 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
489 goto handle_pointer_to_member
;
491 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
492 handle_pointer_to_member
:
493 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
494 if (noside
== EVAL_SKIP
)
496 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
)
497 goto bad_pointer_to_member
;
498 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
499 if (TYPE_CODE (type
) == TYPE_CODE_METHOD
)
500 error ("not implemented: pointer-to-method in pointer-to-member construct");
501 if (TYPE_CODE (type
) != TYPE_CODE_MEMBER
)
502 goto bad_pointer_to_member
;
503 /* Now, convert these values to an address. */
504 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
506 arg3
= value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
507 value_as_long (arg1
) + value_as_long (arg2
));
508 return value_ind (arg3
);
509 bad_pointer_to_member
:
510 error("non-pointer-to-member value used in pointer-to-member construct");
513 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
514 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
515 if (noside
== EVAL_SKIP
)
517 if (binop_user_defined_p (op
, arg1
, arg2
))
518 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
520 return value_concat (arg1
, arg2
);
523 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
524 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
525 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
527 if (binop_user_defined_p (op
, arg1
, arg2
))
528 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
530 return value_assign (arg1
, arg2
);
532 case BINOP_ASSIGN_MODIFY
:
534 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
535 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
536 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
538 op
= exp
->elts
[pc
+ 1].opcode
;
539 if (binop_user_defined_p (op
, arg1
, arg2
))
540 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
541 else if (op
== BINOP_ADD
)
542 arg2
= value_add (arg1
, arg2
);
543 else if (op
== BINOP_SUB
)
544 arg2
= value_sub (arg1
, arg2
);
546 arg2
= value_binop (arg1
, arg2
, op
);
547 return value_assign (arg1
, arg2
);
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 (binop_user_defined_p (op
, arg1
, arg2
))
555 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
557 return value_add (arg1
, arg2
);
560 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
561 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
562 if (noside
== EVAL_SKIP
)
564 if (binop_user_defined_p (op
, arg1
, arg2
))
565 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
567 return value_sub (arg1
, arg2
);
575 case BINOP_BITWISE_AND
:
576 case BINOP_BITWISE_IOR
:
577 case BINOP_BITWISE_XOR
:
578 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
579 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
580 if (noside
== EVAL_SKIP
)
582 if (binop_user_defined_p (op
, arg1
, arg2
))
583 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
585 if (noside
== EVAL_AVOID_SIDE_EFFECTS
586 && (op
== BINOP_DIV
|| op
== BINOP_REM
|| op
== BINOP_MOD
))
587 return value_zero (VALUE_TYPE (arg1
), not_lval
);
589 return value_binop (arg1
, arg2
, op
);
591 case BINOP_SUBSCRIPT
:
592 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
593 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
594 if (noside
== EVAL_SKIP
)
596 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
598 /* If the user attempts to subscript something that has no target
599 type (like a plain int variable for example), then report this
602 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
604 return value_zero (type
, VALUE_LVAL (arg1
));
606 error ("cannot subscript something of type `%s'",
607 TYPE_NAME (VALUE_TYPE (arg1
)));
610 if (binop_user_defined_p (op
, arg1
, arg2
))
611 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
613 return value_subscript (arg1
, arg2
);
615 case MULTI_SUBSCRIPT
:
617 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
618 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
621 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
622 /* FIXME: EVAL_SKIP handling may not be correct. */
623 if (noside
== EVAL_SKIP
)
634 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
635 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
637 /* If the user attempts to subscript something that has no target
638 type (like a plain int variable for example), then report this
641 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
644 arg1
= value_zero (type
, VALUE_LVAL (arg1
));
650 error ("cannot subscript something of type `%s'",
651 TYPE_NAME (VALUE_TYPE (arg1
)));
655 if (binop_user_defined_p (op
, arg1
, arg2
))
657 arg1
= value_x_binop (arg1
, arg2
, op
, OP_NULL
);
661 arg1
= value_subscript (arg1
, arg2
);
666 case BINOP_LOGICAL_AND
:
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
)));
692 case BINOP_LOGICAL_OR
:
693 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
694 if (noside
== EVAL_SKIP
)
696 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
701 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
704 if (binop_user_defined_p (op
, arg1
, arg2
))
706 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
707 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
711 tem
= value_logical_not (arg1
);
712 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
713 (!tem
? EVAL_SKIP
: noside
));
714 return value_from_longest (builtin_type_int
,
715 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
719 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
720 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
721 if (noside
== EVAL_SKIP
)
723 if (binop_user_defined_p (op
, arg1
, arg2
))
725 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
729 tem
= value_equal (arg1
, arg2
);
730 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
734 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
735 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
736 if (noside
== EVAL_SKIP
)
738 if (binop_user_defined_p (op
, arg1
, arg2
))
740 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
744 tem
= value_equal (arg1
, arg2
);
745 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
749 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
750 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
751 if (noside
== EVAL_SKIP
)
753 if (binop_user_defined_p (op
, arg1
, arg2
))
755 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
759 tem
= value_less (arg1
, arg2
);
760 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
764 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
765 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
766 if (noside
== EVAL_SKIP
)
768 if (binop_user_defined_p (op
, arg1
, arg2
))
770 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
774 tem
= value_less (arg2
, arg1
);
775 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
779 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
780 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
781 if (noside
== EVAL_SKIP
)
783 if (binop_user_defined_p (op
, arg1
, arg2
))
785 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
789 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
790 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
794 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
795 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
796 if (noside
== EVAL_SKIP
)
798 if (binop_user_defined_p (op
, arg1
, arg2
))
800 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
804 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
805 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
809 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
810 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
811 if (noside
== EVAL_SKIP
)
813 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
814 error ("Non-integral right operand for \"@\" operator.");
815 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
816 return allocate_repeat_value (VALUE_TYPE (arg1
),
817 longest_to_int (value_as_long (arg2
)));
819 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
822 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
823 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
826 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
827 if (noside
== EVAL_SKIP
)
829 if (unop_user_defined_p (op
, arg1
))
830 return value_x_unop (arg1
, op
);
832 return value_neg (arg1
);
834 case UNOP_COMPLEMENT
:
835 /* C++: check for and handle destructor names. */
836 op
= exp
->elts
[*pos
].opcode
;
838 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
839 if (noside
== EVAL_SKIP
)
841 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
842 return value_x_unop (arg1
, UNOP_COMPLEMENT
);
844 return value_complement (arg1
);
846 case UNOP_LOGICAL_NOT
:
847 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
848 if (noside
== EVAL_SKIP
)
850 if (unop_user_defined_p (op
, arg1
))
851 return value_x_unop (arg1
, op
);
853 return value_from_longest (builtin_type_int
,
854 (LONGEST
) value_logical_not (arg1
));
857 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
858 expect_type
= TYPE_TARGET_TYPE (expect_type
);
859 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
860 if (noside
== EVAL_SKIP
)
862 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
864 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
865 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
866 /* In C you can dereference an array to get the 1st elt. */
867 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
869 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
871 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
872 /* GDB allows dereferencing an int. */
873 return value_zero (builtin_type_int
, lval_memory
);
875 error ("Attempt to take contents of a non-pointer value.");
877 return value_ind (arg1
);
880 /* C++: check for and handle pointer to members. */
882 op
= exp
->elts
[*pos
].opcode
;
884 if (noside
== EVAL_SKIP
)
888 int temm
= longest_to_int (exp
->elts
[pc
+3].longconst
);
889 (*pos
) += 3 + BYTES_TO_EXP_ELEM (temm
+ 1);
892 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
896 return evaluate_subexp_for_address (exp
, pos
, noside
);
899 if (noside
== EVAL_SKIP
)
901 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
904 return evaluate_subexp_for_sizeof (exp
, pos
);
908 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
909 if (noside
== EVAL_SKIP
)
911 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
915 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
916 if (noside
== EVAL_SKIP
)
918 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
919 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
921 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
922 value_as_pointer (arg1
));
924 case UNOP_PREINCREMENT
:
925 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
926 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
928 else if (unop_user_defined_p (op
, arg1
))
930 return value_x_unop (arg1
, op
);
934 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
936 return value_assign (arg1
, arg2
);
939 case UNOP_PREDECREMENT
:
940 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
941 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
943 else if (unop_user_defined_p (op
, arg1
))
945 return value_x_unop (arg1
, op
);
949 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
951 return value_assign (arg1
, arg2
);
954 case UNOP_POSTINCREMENT
:
955 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
956 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
958 else if (unop_user_defined_p (op
, arg1
))
960 return value_x_unop (arg1
, op
);
964 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
966 value_assign (arg1
, arg2
);
970 case UNOP_POSTDECREMENT
:
971 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
972 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
974 else if (unop_user_defined_p (op
, arg1
))
976 return value_x_unop (arg1
, op
);
980 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
982 value_assign (arg1
, arg2
);
988 return value_of_this (1);
991 error ("Attempt to use a type name as an expression");
994 /* Removing this case and compiling with gcc -Wall reveals that
995 a lot of cases are hitting this case. Some of these should
996 probably be removed from expression.h (e.g. do we need a BINOP_SCOPE
997 and an OP_SCOPE?); others are legitimate expressions which are
998 (apparently) not fully implemented.
1000 If there are any cases landing here which mean a user error,
1001 then they should be separate cases, with more descriptive
1005 GDB does not (yet) know how to evaluated that kind of expression");
1009 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
1012 /* Evaluate a subexpression of EXP, at index *POS,
1013 and return the address of that subexpression.
1014 Advance *POS over the subexpression.
1015 If the subexpression isn't an lvalue, get an error.
1016 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1017 then only the type of the result need be correct. */
1020 evaluate_subexp_for_address (exp
, pos
, noside
)
1021 register struct expression
*exp
;
1030 op
= exp
->elts
[pc
].opcode
;
1036 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1040 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
1041 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1044 var
= exp
->elts
[pc
+ 1].symbol
;
1046 /* C++: The "address" of a reference should yield the address
1047 * of the object pointed to. Let value_addr() deal with it. */
1048 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
1052 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1055 lookup_pointer_type (SYMBOL_TYPE (var
));
1056 enum address_class sym_class
= SYMBOL_CLASS (var
);
1058 if (sym_class
== LOC_CONST
1059 || sym_class
== LOC_CONST_BYTES
1060 || sym_class
== LOC_REGISTER
1061 || sym_class
== LOC_REGPARM
)
1062 error ("Attempt to take address of register or constant.");
1065 value_zero (type
, not_lval
);
1068 return locate_var_value (var
, (FRAME
) 0);
1072 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
1074 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1075 if (VALUE_LVAL (x
) == lval_memory
)
1076 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
1079 error ("Attempt to take address of non-lval");
1081 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
1085 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1086 When used in contexts where arrays will be coerced anyway, this is
1087 equivalent to `evaluate_subexp' but much faster because it avoids
1088 actually fetching array contents.
1090 Note that we currently only do the coercion for C expressions, where
1091 arrays are zero based and the coercion is correct. For other languages,
1092 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1093 to decide if coercion is appropriate.
1098 evaluate_subexp_with_coercion (exp
, pos
, noside
)
1099 register struct expression
*exp
;
1103 register enum exp_opcode op
;
1109 op
= exp
->elts
[pc
].opcode
;
1114 var
= exp
->elts
[pc
+ 1].symbol
;
1115 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
1116 && CAST_IS_CONVERSION
)
1119 val
= locate_var_value (var
, (FRAME
) 0);
1120 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1124 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1128 /* Evaluate a subexpression of EXP, at index *POS,
1129 and return a value for the size of that subexpression.
1130 Advance *POS over the subexpression. */
1133 evaluate_subexp_for_sizeof (exp
, pos
)
1134 register struct expression
*exp
;
1142 op
= exp
->elts
[pc
].opcode
;
1146 /* This case is handled specially
1147 so that we avoid creating a value for the result type.
1148 If the result type is very big, it's desirable not to
1149 create a value unnecessarily. */
1152 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1153 return value_from_longest (builtin_type_int
, (LONGEST
)
1154 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1158 return value_from_longest (builtin_type_int
,
1159 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1163 return value_from_longest (builtin_type_int
,
1164 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1167 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1168 return value_from_longest (builtin_type_int
,
1169 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1173 /* Parse a type expression in the string [P..P+LENGTH). */
1176 parse_and_eval_type (p
, length
)
1180 char *tmp
= (char *)alloca (length
+ 4);
1181 struct expression
*expr
;
1183 memcpy (tmp
+1, p
, length
);
1184 tmp
[length
+1] = ')';
1185 tmp
[length
+2] = '0';
1186 tmp
[length
+3] = '\0';
1187 expr
= parse_expression (tmp
);
1188 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1189 error ("Internal error in eval_type.");
1190 return expr
->elts
[1].type
;