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
= longest_to_int (exp
->elts
[pc
+ 2].longconst
);
178 (*pos
) += 4 + ((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
+ 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
));
240 /* start-sanitize-chill */
243 return value_from_longest (builtin_type_chill_bool
,
244 exp
->elts
[pc
+ 1].longconst
);
245 /* end-sanitize-chill */
249 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
252 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
253 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
254 / sizeof (union exp_element
));
255 if (noside
== EVAL_SKIP
)
257 return value_string (&exp
->elts
[pc
+ 2].string
, tem
);
260 /* Skip third and second args to evaluate the first one. */
261 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
262 if (value_logical_not (arg1
))
264 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
265 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
269 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
270 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
276 op
= exp
->elts
[*pos
].opcode
;
277 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
281 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
282 /* First, evaluate the structure into arg2 */
285 if (noside
== EVAL_SKIP
)
288 if (op
== STRUCTOP_MEMBER
)
290 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
294 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
297 /* If the function is a virtual function, then the
298 aggregate value (providing the structure) plays
299 its part by providing the vtable. Otherwise,
300 it is just along for the ride: call the function
303 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
305 fnptr
= longest_to_int (value_as_long (arg1
));
307 if (METHOD_PTR_IS_VIRTUAL(fnptr
))
309 int fnoffset
= METHOD_PTR_TO_VOFFSET(fnptr
);
310 struct type
*basetype
;
311 struct type
*domain_type
=
312 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
314 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
315 if (domain_type
!= basetype
)
316 arg2
= value_cast(lookup_pointer_type (domain_type
), arg2
);
317 basetype
= TYPE_VPTR_BASETYPE (domain_type
);
318 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
320 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
321 /* If one is virtual, then all are virtual. */
322 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
323 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
324 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnoffset
)
326 value temp
= value_ind (arg2
);
327 arg1
= value_virtual_fn_field (&temp
, f
, j
, domain_type
, 0);
328 arg2
= value_addr (temp
);
333 error ("virtual function at index %d not found", fnoffset
);
337 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
341 /* Now, say which argument to start evaluating from */
344 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
346 /* Hair for method invocations */
349 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
350 /* First, evaluate the structure into arg2 */
352 tem2
= longest_to_int (exp
->elts
[pc2
+ 1].longconst
);
353 *pos
+= 3 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
354 if (noside
== EVAL_SKIP
)
357 if (op
== STRUCTOP_STRUCT
)
359 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
363 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
365 /* Now, say which argument to start evaluating from */
370 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
373 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
374 for (; tem
<= nargs
; tem
++)
375 /* Ensure that array expressions are coerced into pointer objects. */
376 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
378 /* signal end of arglist */
381 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
388 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 2].string
,
390 op
== STRUCTOP_STRUCT
391 ? "structure" : "structure pointer");
392 if (VALUE_OFFSET (temp
))
394 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
395 value_as_long (arg2
)+VALUE_OFFSET (temp
));
400 argvec
[1] = argvec
[0];
405 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
411 if (noside
== EVAL_SKIP
)
413 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
415 /* If the return type doesn't look like a function type, call an
416 error. This can happen if somebody tries to turn a variable into
417 a function call. This is here because people often want to
418 call, eg, strcmp, which gdb doesn't know is a function. If
419 gdb isn't asked for it's opinion (ie. through "whatis"),
420 it won't offer it. */
423 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
426 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
428 error ("Expression of type other than \"Function returning ...\" used as function");
430 return call_function_by_hand (argvec
[0], nargs
, argvec
+ 1);
432 case STRUCTOP_STRUCT
:
433 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
434 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
435 / sizeof (union exp_element
));
436 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
437 if (noside
== EVAL_SKIP
)
439 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
440 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
441 &exp
->elts
[pc
+ 2].string
,
447 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
448 (int *) 0, "structure");
452 tem
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
453 (*pos
) += 3 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
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 (TYPE_TARGET_TYPE
460 &exp
->elts
[pc
+ 2].string
,
466 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 2].string
,
467 (int *) 0, "structure pointer");
470 case STRUCTOP_MEMBER
:
471 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
472 goto handle_pointer_to_member
;
474 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
475 handle_pointer_to_member
:
476 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
477 if (noside
== EVAL_SKIP
)
479 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
)
480 goto bad_pointer_to_member
;
481 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
482 if (TYPE_CODE (type
) == TYPE_CODE_METHOD
)
483 error ("not implemented: pointer-to-method in pointer-to-member construct");
484 if (TYPE_CODE (type
) != TYPE_CODE_MEMBER
)
485 goto bad_pointer_to_member
;
486 /* Now, convert these values to an address. */
487 arg1
= value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type
)),
489 arg3
= value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type
)),
490 value_as_long (arg1
) + value_as_long (arg2
));
491 return value_ind (arg3
);
492 bad_pointer_to_member
:
493 error("non-pointer-to-member value used in pointer-to-member construct");
496 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
497 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
498 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
500 if (binop_user_defined_p (op
, arg1
, arg2
))
501 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
503 return value_assign (arg1
, arg2
);
505 case BINOP_ASSIGN_MODIFY
:
507 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
508 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
509 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
511 op
= exp
->elts
[pc
+ 1].opcode
;
512 if (binop_user_defined_p (op
, arg1
, arg2
))
513 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
514 else if (op
== BINOP_ADD
)
515 arg2
= value_add (arg1
, arg2
);
516 else if (op
== BINOP_SUB
)
517 arg2
= value_sub (arg1
, arg2
);
519 arg2
= value_binop (arg1
, arg2
, op
);
520 return value_assign (arg1
, arg2
);
523 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
524 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
525 if (noside
== EVAL_SKIP
)
527 if (binop_user_defined_p (op
, arg1
, arg2
))
528 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
530 return value_add (arg1
, arg2
);
533 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
534 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
535 if (noside
== EVAL_SKIP
)
537 if (binop_user_defined_p (op
, arg1
, arg2
))
538 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
540 return value_sub (arg1
, arg2
);
547 case BINOP_BITWISE_AND
:
548 case BINOP_BITWISE_IOR
:
549 case BINOP_BITWISE_XOR
:
550 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
551 arg2
= evaluate_subexp (NULL_TYPE
, 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 if (noside
== EVAL_AVOID_SIDE_EFFECTS
558 && (op
== BINOP_DIV
|| op
== BINOP_REM
))
559 return value_zero (VALUE_TYPE (arg1
), not_lval
);
561 return value_binop (arg1
, arg2
, op
);
563 case BINOP_SUBSCRIPT
:
564 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
565 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
566 if (noside
== EVAL_SKIP
)
568 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
570 /* If the user attempts to subscript something that has no target
571 type (like a plain int variable for example), then report this
574 type
= TYPE_TARGET_TYPE (VALUE_TYPE (arg1
));
576 return value_zero (type
, VALUE_LVAL (arg1
));
578 error ("cannot subscript something of type `%s'",
579 TYPE_NAME (VALUE_TYPE (arg1
)));
582 if (binop_user_defined_p (op
, arg1
, arg2
))
583 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
585 return value_subscript (arg1
, arg2
);
587 case BINOP_LOGICAL_AND
:
588 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
589 if (noside
== EVAL_SKIP
)
591 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
596 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
599 if (binop_user_defined_p (op
, arg1
, arg2
))
601 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
602 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
606 tem
= value_logical_not (arg1
);
607 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
608 (tem
? EVAL_SKIP
: noside
));
609 return value_from_longest (builtin_type_int
,
610 (LONGEST
) (!tem
&& !value_logical_not (arg2
)));
613 case BINOP_LOGICAL_OR
:
614 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
615 if (noside
== EVAL_SKIP
)
617 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
622 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
625 if (binop_user_defined_p (op
, arg1
, arg2
))
627 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
628 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
632 tem
= value_logical_not (arg1
);
633 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
634 (!tem
? EVAL_SKIP
: noside
));
635 return value_from_longest (builtin_type_int
,
636 (LONGEST
) (!tem
|| !value_logical_not (arg2
)));
640 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
641 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
642 if (noside
== EVAL_SKIP
)
644 if (binop_user_defined_p (op
, arg1
, arg2
))
646 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
650 tem
= value_equal (arg1
, arg2
);
651 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
655 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
656 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
657 if (noside
== EVAL_SKIP
)
659 if (binop_user_defined_p (op
, arg1
, arg2
))
661 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
665 tem
= value_equal (arg1
, arg2
);
666 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
670 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
671 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
672 if (noside
== EVAL_SKIP
)
674 if (binop_user_defined_p (op
, arg1
, arg2
))
676 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
680 tem
= value_less (arg1
, arg2
);
681 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
685 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
686 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
687 if (noside
== EVAL_SKIP
)
689 if (binop_user_defined_p (op
, arg1
, arg2
))
691 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
695 tem
= value_less (arg2
, arg1
);
696 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
700 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
701 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
702 if (noside
== EVAL_SKIP
)
704 if (binop_user_defined_p (op
, arg1
, arg2
))
706 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
710 tem
= value_less (arg2
, arg1
) || value_equal (arg1
, arg2
);
711 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
715 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
716 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
717 if (noside
== EVAL_SKIP
)
719 if (binop_user_defined_p (op
, arg1
, arg2
))
721 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
725 tem
= value_less (arg1
, arg2
) || value_equal (arg1
, arg2
);
726 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
730 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
731 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
732 if (noside
== EVAL_SKIP
)
734 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
735 error ("Non-integral right operand for \"@\" operator.");
736 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
737 return allocate_repeat_value (VALUE_TYPE (arg1
),
738 longest_to_int (value_as_long (arg2
)));
740 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
743 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
744 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
747 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
748 if (noside
== EVAL_SKIP
)
750 if (unop_user_defined_p (op
, arg1
))
751 return value_x_unop (arg1
, op
);
753 return value_neg (arg1
);
755 case UNOP_COMPLEMENT
:
756 /* C++: check for and handle destructor names. */
757 op
= exp
->elts
[*pos
].opcode
;
759 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
760 if (noside
== EVAL_SKIP
)
762 if (unop_user_defined_p (UNOP_COMPLEMENT
, arg1
))
763 return value_x_unop (arg1
, UNOP_COMPLEMENT
);
765 return value_complement (arg1
);
767 case UNOP_LOGICAL_NOT
:
768 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
769 if (noside
== EVAL_SKIP
)
771 if (unop_user_defined_p (op
, arg1
))
772 return value_x_unop (arg1
, op
);
774 return value_from_longest (builtin_type_int
,
775 (LONGEST
) value_logical_not (arg1
));
778 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
779 expect_type
= TYPE_TARGET_TYPE (expect_type
);
780 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
781 if (noside
== EVAL_SKIP
)
783 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
785 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
786 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
787 /* In C you can dereference an array to get the 1st elt. */
788 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
790 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
792 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
793 /* GDB allows dereferencing an int. */
794 return value_zero (builtin_type_int
, lval_memory
);
796 error ("Attempt to take contents of a non-pointer value.");
798 return value_ind (arg1
);
801 /* C++: check for and handle pointer to members. */
803 op
= exp
->elts
[*pos
].opcode
;
805 if (noside
== EVAL_SKIP
)
809 int temm
= longest_to_int (exp
->elts
[pc
+3].longconst
);
810 (*pos
) += 3 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
813 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
817 return evaluate_subexp_for_address (exp
, pos
, noside
);
820 if (noside
== EVAL_SKIP
)
822 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
825 return evaluate_subexp_for_sizeof (exp
, pos
);
829 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
830 if (noside
== EVAL_SKIP
)
832 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
836 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
837 if (noside
== EVAL_SKIP
)
839 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
840 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
842 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
843 value_as_pointer (arg1
));
845 case UNOP_PREINCREMENT
:
846 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
847 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
849 else if (unop_user_defined_p (op
, arg1
))
851 return value_x_unop (arg1
, op
);
855 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
857 return value_assign (arg1
, arg2
);
860 case UNOP_PREDECREMENT
:
861 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
862 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
864 else if (unop_user_defined_p (op
, arg1
))
866 return value_x_unop (arg1
, op
);
870 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
872 return value_assign (arg1
, arg2
);
875 case UNOP_POSTINCREMENT
:
876 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
877 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
879 else if (unop_user_defined_p (op
, arg1
))
881 return value_x_unop (arg1
, op
);
885 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
887 value_assign (arg1
, arg2
);
891 case UNOP_POSTDECREMENT
:
892 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
893 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
895 else if (unop_user_defined_p (op
, arg1
))
897 return value_x_unop (arg1
, op
);
901 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
903 value_assign (arg1
, arg2
);
909 return value_of_this (1);
912 error ("internal error: I do not know how to evaluate what you gave me");
916 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
919 /* Evaluate a subexpression of EXP, at index *POS,
920 and return the address of that subexpression.
921 Advance *POS over the subexpression.
922 If the subexpression isn't an lvalue, get an error.
923 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
924 then only the type of the result need be correct. */
927 evaluate_subexp_for_address (exp
, pos
, noside
)
928 register struct expression
*exp
;
937 op
= exp
->elts
[pc
].opcode
;
943 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
947 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
948 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
951 var
= exp
->elts
[pc
+ 1].symbol
;
953 /* C++: The "address" of a reference should yield the address
954 * of the object pointed to. Let value_addr() deal with it. */
955 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_REF
)
959 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
962 lookup_pointer_type (SYMBOL_TYPE (var
));
963 enum address_class sym_class
= SYMBOL_CLASS (var
);
965 if (sym_class
== LOC_CONST
966 || sym_class
== LOC_CONST_BYTES
967 || sym_class
== LOC_REGISTER
968 || sym_class
== LOC_REGPARM
)
969 error ("Attempt to take address of register or constant.");
972 value_zero (type
, not_lval
);
975 return locate_var_value (var
, (FRAME
) 0);
979 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
981 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
982 if (VALUE_LVAL (x
) == lval_memory
)
983 return value_zero (lookup_pointer_type (VALUE_TYPE (x
)),
986 error ("Attempt to take address of non-lval");
988 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
992 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
993 When used in contexts where arrays will be coerced anyway,
994 this is equivalent to `evaluate_subexp'
995 but much faster because it avoids actually fetching array contents. */
998 evaluate_subexp_with_coercion (exp
, pos
, noside
)
999 register struct expression
*exp
;
1003 register enum exp_opcode op
;
1009 op
= exp
->elts
[pc
].opcode
;
1014 var
= exp
->elts
[pc
+ 1].symbol
;
1015 if (TYPE_CODE (SYMBOL_TYPE (var
)) == TYPE_CODE_ARRAY
)
1018 val
= locate_var_value (var
, (FRAME
) 0);
1019 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var
))),
1023 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1027 /* Evaluate a subexpression of EXP, at index *POS,
1028 and return a value for the size of that subexpression.
1029 Advance *POS over the subexpression. */
1032 evaluate_subexp_for_sizeof (exp
, pos
)
1033 register struct expression
*exp
;
1041 op
= exp
->elts
[pc
].opcode
;
1045 /* This case is handled specially
1046 so that we avoid creating a value for the result type.
1047 If the result type is very big, it's desirable not to
1048 create a value unnecessarily. */
1051 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1052 return value_from_longest (builtin_type_int
, (LONGEST
)
1053 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1057 return value_from_longest (builtin_type_int
,
1058 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1062 return value_from_longest (builtin_type_int
,
1063 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1066 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1067 return value_from_longest (builtin_type_int
,
1068 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
1072 /* Parse a type expression in the string [P..P+LENGTH). */
1075 parse_and_eval_type (p
, length
)
1079 char *tmp
= (char *)alloca (length
+ 4);
1080 struct expression
*expr
;
1082 memcpy (tmp
+1, p
, length
);
1083 tmp
[length
+1] = ')';
1084 tmp
[length
+2] = '0';
1085 tmp
[length
+3] = '\0';
1086 expr
= parse_expression (tmp
);
1087 if (expr
->elts
[0].opcode
!= UNOP_CAST
)
1088 error ("Internal error in eval_type.");
1089 return expr
->elts
[1].type
;