1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987, 1989 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. */
25 #include "expression.h"
29 #define NULL_TYPE ((struct type *)0)
32 /* Parse the string EXP as a C expression, evaluate it,
33 and return the result as a number. */
36 parse_and_eval_address (exp
)
39 struct expression
*expr
= parse_expression (exp
);
40 register CORE_ADDR addr
;
41 register struct cleanup
*old_chain
42 = make_cleanup (free_current_contents
, &expr
);
44 addr
= value_as_pointer (evaluate_expression (expr
));
45 do_cleanups (old_chain
);
49 /* Like parse_and_eval_address but takes a pointer to a char * variable
50 and advanced that variable across the characters parsed. */
53 parse_and_eval_address_1 (expptr
)
56 struct expression
*expr
= parse_exp_1 (expptr
, (struct block
*)0, 0);
57 register CORE_ADDR addr
;
58 register struct cleanup
*old_chain
59 = make_cleanup (free_current_contents
, &expr
);
61 addr
= value_as_pointer (evaluate_expression (expr
));
62 do_cleanups (old_chain
);
70 struct expression
*expr
= parse_expression (exp
);
72 register struct cleanup
*old_chain
73 = make_cleanup (free_current_contents
, &expr
);
75 val
= evaluate_expression (expr
);
76 do_cleanups (old_chain
);
80 /* Parse up to a comma (or to a closeparen)
81 in the string EXPP as an expression, evaluate it, and return the value.
82 EXPP is advanced to point to the comma. */
85 parse_to_comma_and_eval (expp
)
88 struct expression
*expr
= parse_exp_1 (expp
, (struct block
*) 0, 1);
90 register struct cleanup
*old_chain
91 = make_cleanup (free_current_contents
, &expr
);
93 val
= evaluate_expression (expr
);
94 do_cleanups (old_chain
);
98 /* Evaluate an expression in internal prefix form
99 such as is constructed by expread.y.
101 See expression.h for info on the format of an expression. */
103 static value
evaluate_subexp ();
104 static value
evaluate_subexp_for_address ();
105 static value
evaluate_subexp_for_sizeof ();
106 static value
evaluate_subexp_with_coercion ();
108 /* Values of NOSIDE argument to eval_subexp. */
111 EVAL_SKIP
, /* Only effect is to increment pos. */
112 EVAL_AVOID_SIDE_EFFECTS
, /* Don't modify any variables or
113 call any functions. The value
114 returned will have the correct
115 type, and will have an
116 approximately correct lvalue
117 type (inaccuracy: anything that is
118 listed as being in a register in
119 the function in which it was
120 declared will be lval_register). */
124 evaluate_expression (exp
)
125 struct expression
*exp
;
128 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_NORMAL
);
131 /* Evaluate an expression, avoiding all memory references
132 and getting a value whose type alone is correct. */
136 struct expression
*exp
;
139 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
143 evaluate_subexp (expect_type
, exp
, pos
, noside
)
144 struct type
*expect_type
;
145 register struct expression
*exp
;
151 register int pc
, pc2
, oldpos
;
152 register value arg1
, arg2
, arg3
;
157 op
= exp
->elts
[pc
].opcode
;
162 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
163 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
164 / sizeof (union exp_element
));
165 arg1
= value_static_field (exp
->elts
[pc
+ 1].type
,
166 &exp
->elts
[pc
+ 2].string
, -1);
168 error ("There is no field named %s", &exp
->elts
[pc
+ 2].string
);
173 return value_from_longest (exp
->elts
[pc
+ 1].type
,
174 exp
->elts
[pc
+ 2].longconst
);
178 return value_from_double (exp
->elts
[pc
+ 1].type
,
179 exp
->elts
[pc
+ 2].doubleconst
);
183 if (noside
== EVAL_SKIP
)
185 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
187 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
190 switch (SYMBOL_CLASS (sym
))
194 case LOC_CONST_BYTES
:
208 return value_zero (SYMBOL_TYPE (sym
), lv
);
211 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
216 access_value_history (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
220 return value_of_register (longest_to_int (exp
->elts
[pc
+ 1].longconst
));
224 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
227 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
228 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
229 / sizeof (union exp_element
));
230 if (noside
== EVAL_SKIP
)
232 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
235 /* Skip third and second args to evaluate the first one. */
236 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
237 if (value_zerop (arg1
))
239 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
240 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
244 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
245 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
251 op
= exp
->elts
[*pos
].opcode
;
252 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
256 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
257 /* First, evaluate the structure into arg2 */
260 if (noside
== EVAL_SKIP
)
263 if (op
== STRUCTOP_MEMBER
)
265 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
269 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
272 /* If the function is a virtual function, then the
273 aggregate value (providing the structure) plays
274 its part by providing the vtable. Otherwise,
275 it is just along for the ride: call the function
278 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
280 fnptr
= longest_to_int (value_as_long (arg1
));
281 /* FIXME-tiemann: this is way obsolete. */
284 struct type
*basetype
;
286 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
287 basetype
= TYPE_VPTR_BASETYPE (basetype
);
288 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
290 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
291 /* If one is virtual, then all are virtual. */
292 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
293 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
294 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
297 value base
= value_ind (arg2
);
298 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
300 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
301 fill_in_vptr_fieldno (basetype
);
303 VALUE_TYPE (base
) = basetype
;
304 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
305 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
306 VALUE_TYPE (arg1
) = builtin_type_int
;
307 arg1
= value_subscript (vtbl
, arg1
);
308 VALUE_TYPE (arg1
) = fntype
;
313 error ("virtual function at index %d not found", fnptr
);
317 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
321 /* Now, say which argument to start evaluating from */
324 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
326 /* Hair for method invocations */
329 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
) + 1;
330 /* First, evaluate the structure into arg2 */
332 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
333 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
334 if (noside
== EVAL_SKIP
)
337 if (op
== STRUCTOP_STRUCT
)
339 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
343 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
345 /* Now, say which argument to start evaluating from */
350 nargs
= longest_to_int (exp
->elts
[pc
+ 1].longconst
);
353 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
354 for (; tem
<= nargs
; tem
++)
355 /* Ensure that array expressions are coerced into pointer objects. */
356 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
358 /* signal end of arglist */
361 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
368 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
370 op
== STRUCTOP_STRUCT
371 ? "structure" : "structure pointer");
372 if (VALUE_OFFSET (temp
))
374 arg2
= value_from_longest (lookup_pointer_type (VALUE_TYPE (temp
)),
375 value_as_long (arg2
)+VALUE_OFFSET (temp
));
380 argvec
[1] = argvec
[0];
385 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
391 if (noside
== EVAL_SKIP
)
393 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
395 /* If the return type doesn't look like a function type, call an
396 error. This can happen if somebody tries to turn a variable into
397 a function call. This is here because people often want to
398 call, eg, strcmp, which gdb doesn't know is a function. If
399 gdb isn't asked for it's opinion (ie. through "whatis"),
400 it won't offer it. */
403 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
406 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
408 error ("Expression of type other than \"Function returning ...\" used as function");
410 return target_call_function (argvec
[0], nargs
, argvec
+ 1);
412 case STRUCTOP_STRUCT
:
413 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
414 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
415 / sizeof (union exp_element
));
416 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
417 if (noside
== EVAL_SKIP
)
419 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
420 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
421 &exp
->elts
[pc
+ 1].string
,
427 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
428 (int *) 0, "structure");
432 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
433 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
434 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
435 if (noside
== EVAL_SKIP
)
437 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
438 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
440 &exp
->elts
[pc
+ 1].string
,
446 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
447 (int *) 0, "structure pointer");
450 case STRUCTOP_MEMBER
:
451 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
452 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
453 if (noside
== EVAL_SKIP
)
455 /* Now, convert these values to an address. */
456 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
457 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
459 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
460 != TYPE_CODE_METHOD
)))
461 error ("non-pointer-to-member value used in pointer-to-member construct");
462 arg3
= value_from_longest (
463 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))),
464 value_as_long (arg1
) + value_as_long (arg2
));
465 return value_ind (arg3
);
468 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
469 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
470 if (noside
== EVAL_SKIP
)
472 /* Now, convert these values to an address. */
473 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
474 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_MEMBER
475 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_METHOD
))
476 error ("non-pointer-to-member value used in pointer-to-member construct");
477 arg3
= value_from_longest (
478 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))),
479 value_as_long (arg1
) + value_as_long (arg2
));
480 return value_ind (arg3
);
483 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
484 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
485 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
487 if (binop_user_defined_p (op
, arg1
, arg2
))
488 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
490 return value_assign (arg1
, arg2
);
492 case BINOP_ASSIGN_MODIFY
:
494 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
495 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
496 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
498 op
= exp
->elts
[pc
+ 1].opcode
;
499 if (binop_user_defined_p (op
, arg1
, arg2
))
500 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
501 else if (op
== BINOP_ADD
)
502 arg2
= value_add (arg1
, arg2
);
503 else if (op
== BINOP_SUB
)
504 arg2
= value_sub (arg1
, arg2
);
506 arg2
= value_binop (arg1
, arg2
, op
);
507 return value_assign (arg1
, arg2
);
510 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
511 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
512 if (noside
== EVAL_SKIP
)
514 if (binop_user_defined_p (op
, arg1
, arg2
))
515 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
517 return value_add (arg1
, arg2
);
520 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
521 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
522 if (noside
== EVAL_SKIP
)
524 if (binop_user_defined_p (op
, arg1
, arg2
))
525 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
527 return value_sub (arg1
, arg2
);
537 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
538 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
539 if (noside
== EVAL_SKIP
)
541 if (binop_user_defined_p (op
, arg1
, arg2
))
542 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
544 if (noside
== EVAL_AVOID_SIDE_EFFECTS
546 return value_zero (VALUE_TYPE (arg1
), not_lval
);
548 return value_binop (arg1
, arg2
, op
);
550 case BINOP_SUBSCRIPT
:
551 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
552 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
553 if (noside
== EVAL_SKIP
)
555 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
556 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
559 if (binop_user_defined_p (op
, arg1
, arg2
))
560 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
562 return value_subscript (arg1
, arg2
);
565 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
566 if (noside
== EVAL_SKIP
)
568 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
573 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
576 if (binop_user_defined_p (op
, arg1
, arg2
))
578 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
579 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
583 tem
= value_zerop (arg1
);
584 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
585 (tem
? EVAL_SKIP
: noside
));
586 return value_from_longest (builtin_type_int
,
587 (LONGEST
) (!tem
&& !value_zerop (arg2
)));
591 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
592 if (noside
== EVAL_SKIP
)
594 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
599 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
602 if (binop_user_defined_p (op
, arg1
, arg2
))
604 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
605 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
609 tem
= value_zerop (arg1
);
610 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
611 (!tem
? EVAL_SKIP
: noside
));
612 return value_from_longest (builtin_type_int
,
613 (LONGEST
) (!tem
|| !value_zerop (arg2
)));
617 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
618 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
619 if (noside
== EVAL_SKIP
)
621 if (binop_user_defined_p (op
, arg1
, arg2
))
623 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
627 tem
= value_equal (arg1
, arg2
);
628 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
632 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
633 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
634 if (noside
== EVAL_SKIP
)
636 if (binop_user_defined_p (op
, arg1
, arg2
))
638 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
642 tem
= value_equal (arg1
, arg2
);
643 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
647 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
648 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
649 if (noside
== EVAL_SKIP
)
651 if (binop_user_defined_p (op
, arg1
, arg2
))
653 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
657 tem
= value_less (arg1
, arg2
);
658 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
662 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
663 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
664 if (noside
== EVAL_SKIP
)
666 if (binop_user_defined_p (op
, arg1
, arg2
))
668 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
672 tem
= value_less (arg2
, arg1
);
673 return value_from_longest (builtin_type_int
, (LONGEST
) tem
);
677 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
678 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
679 if (noside
== EVAL_SKIP
)
681 if (binop_user_defined_p (op
, arg1
, arg2
))
683 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
687 tem
= value_less (arg1
, arg2
);
688 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
692 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
693 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
694 if (noside
== EVAL_SKIP
)
696 if (binop_user_defined_p (op
, arg1
, arg2
))
698 return value_x_binop (arg1
, arg2
, op
, OP_NULL
);
702 tem
= value_less (arg2
, arg1
);
703 return value_from_longest (builtin_type_int
, (LONGEST
) ! tem
);
707 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
708 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
709 if (noside
== EVAL_SKIP
)
711 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
712 error ("Non-integral right operand for \"@\" operator.");
713 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
714 return allocate_repeat_value (VALUE_TYPE (arg1
),
715 longest_to_int (value_as_long (arg2
)));
717 return value_repeat (arg1
, longest_to_int (value_as_long (arg2
)));
720 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
721 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
724 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
725 if (noside
== EVAL_SKIP
)
727 if (unop_user_defined_p (op
, arg1
))
728 return value_x_unop (arg1
, op
);
730 return value_neg (arg1
);
733 /* C++: check for and handle destructor names. */
734 op
= exp
->elts
[*pos
].opcode
;
736 /* FIXME-tiemann: this is a cop-out. */
738 error ("destructor in eval");
740 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
741 if (noside
== EVAL_SKIP
)
743 if (unop_user_defined_p (UNOP_LOGNOT
, arg1
))
744 return value_x_unop (arg1
, UNOP_LOGNOT
);
746 return value_lognot (arg1
);
749 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
750 if (noside
== EVAL_SKIP
)
752 if (unop_user_defined_p (op
, arg1
))
753 return value_x_unop (arg1
, op
);
755 return value_from_longest (builtin_type_int
,
756 (LONGEST
) value_zerop (arg1
));
759 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
760 expect_type
= TYPE_TARGET_TYPE (expect_type
);
761 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
762 if (noside
== EVAL_SKIP
)
764 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
766 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
767 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
768 /* In C you can dereference an array to get the 1st elt. */
769 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
771 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
773 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
774 /* GDB allows dereferencing an int. */
775 return value_zero (builtin_type_int
, lval_memory
);
777 error ("Attempt to take contents of a non-pointer value.");
779 return value_ind (arg1
);
782 /* C++: check for and handle pointer to members. */
784 op
= exp
->elts
[*pos
].opcode
;
786 if (noside
== EVAL_SKIP
)
790 char *name
= &exp
->elts
[pc
+3].string
;
791 int temm
= strlen (name
);
792 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
795 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
801 char *name
= &exp
->elts
[pc
+3].string
;
802 int temm
= strlen (name
);
803 struct type
*domain
= exp
->elts
[pc
+2].type
;
804 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
805 arg1
= value_struct_elt_for_address (domain
, expect_type
, name
);
808 error ("no field `%s' in structure", name
);
811 return evaluate_subexp_for_address (exp
, pos
, noside
);
814 if (noside
== EVAL_SKIP
)
816 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
819 return evaluate_subexp_for_sizeof (exp
, pos
);
823 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
824 if (noside
== EVAL_SKIP
)
826 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
830 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
831 if (noside
== EVAL_SKIP
)
833 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
834 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
836 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
837 value_as_pointer (arg1
));
839 case UNOP_PREINCREMENT
:
840 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
841 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
843 else if (unop_user_defined_p (op
, arg1
))
845 return value_x_unop (arg1
, op
);
849 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
851 return value_assign (arg1
, arg2
);
854 case UNOP_PREDECREMENT
:
855 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
856 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
858 else if (unop_user_defined_p (op
, arg1
))
860 return value_x_unop (arg1
, op
);
864 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
866 return value_assign (arg1
, arg2
);
869 case UNOP_POSTINCREMENT
:
870 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
871 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
873 else if (unop_user_defined_p (op
, arg1
))
875 return value_x_unop (arg1
, op
);
879 arg2
= value_add (arg1
, value_from_longest (builtin_type_char
,
881 value_assign (arg1
, arg2
);
885 case UNOP_POSTDECREMENT
:
886 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
887 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
889 else if (unop_user_defined_p (op
, arg1
))
891 return value_x_unop (arg1
, op
);
895 arg2
= value_sub (arg1
, value_from_longest (builtin_type_char
,
897 value_assign (arg1
, arg2
);
903 return value_of_this (1);
906 error ("internal error: I do not know how to evaluate what you gave me");
910 return value_from_longest (builtin_type_long
, (LONGEST
) 1);
913 /* Evaluate a subexpression of EXP, at index *POS,
914 and return the address of that subexpression.
915 Advance *POS over the subexpression.
916 If the subexpression isn't an lvalue, get an error.
917 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
918 then only the type of the result need be correct. */
921 evaluate_subexp_for_address (exp
, pos
, noside
)
922 register struct expression
*exp
;
930 op
= exp
->elts
[pc
].opcode
;
936 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
940 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
941 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
945 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
948 lookup_pointer_type (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
));
949 enum address_class sym_class
=
950 SYMBOL_CLASS (exp
->elts
[pc
+ 1].symbol
);
952 if (sym_class
== LOC_CONST
953 || sym_class
== LOC_CONST_BYTES
954 || sym_class
== LOC_REGISTER
955 || sym_class
== LOC_REGPARM
)
956 error ("Attempt to take address of register or constant.");
959 value_zero (type
, not_lval
);
962 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (FRAME
) 0);
965 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
967 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
968 if (VALUE_LVAL (x
) == lval_memory
)
969 return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x
)),
972 error ("Attempt to take address of non-lval");
974 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
978 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
979 When used in contexts where arrays will be coerced anyway,
980 this is equivalent to `evaluate_subexp'
981 but much faster because it avoids actually fetching array contents. */
984 evaluate_subexp_with_coercion (exp
, pos
, noside
)
985 register struct expression
*exp
;
989 register enum exp_opcode op
;
994 op
= exp
->elts
[pc
].opcode
;
999 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
1002 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (FRAME
) 0);
1003 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
1007 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1011 /* Evaluate a subexpression of EXP, at index *POS,
1012 and return a value for the size of that subexpression.
1013 Advance *POS over the subexpression. */
1016 evaluate_subexp_for_sizeof (exp
, pos
)
1017 register struct expression
*exp
;
1025 op
= exp
->elts
[pc
].opcode
;
1029 /* This case is handled specially
1030 so that we avoid creating a value for the result type.
1031 If the result type is very big, it's desirable not to
1032 create a value unnecessarily. */
1035 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1036 return value_from_longest (builtin_type_int
, (LONGEST
)
1037 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1041 return value_from_longest (builtin_type_int
,
1042 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1046 return value_from_longest (builtin_type_int
,
1047 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1050 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1051 return value_from_longest (builtin_type_int
,
1052 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
This page took 0.067506 seconds and 4 git commands to generate.