1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "expression.h"
28 #define NULL_TYPE ((struct type *)0)
31 /* Parse the string EXP as a C expression, evaluate it,
32 and return the result as a number. */
35 parse_and_eval_address (exp
)
38 struct expression
*expr
= parse_c_expression (exp
);
39 register CORE_ADDR addr
;
40 register struct cleanup
*old_chain
41 = make_cleanup (free_current_contents
, &expr
);
43 addr
= (CORE_ADDR
) value_as_long (evaluate_expression (expr
));
44 do_cleanups (old_chain
);
48 /* Like parse_and_eval_address but takes a pointer to a char * variable
49 and advanced that variable across the characters parsed. */
52 parse_and_eval_address_1 (expptr
)
55 struct expression
*expr
= parse_c_1 (expptr
, 0, 0);
56 register CORE_ADDR addr
;
57 register struct cleanup
*old_chain
58 = make_cleanup (free_current_contents
, &expr
);
60 addr
= (CORE_ADDR
) value_as_long (evaluate_expression (expr
));
61 do_cleanups (old_chain
);
69 struct expression
*expr
= parse_c_expression (exp
);
71 register struct cleanup
*old_chain
72 = make_cleanup (free_current_contents
, &expr
);
74 val
= evaluate_expression (expr
);
75 do_cleanups (old_chain
);
79 /* Parse up to a comma (or to a closeparen)
80 in the string EXPP as an expression, evaluate it, and return the value.
81 EXPP is advanced to point to the comma. */
84 parse_to_comma_and_eval (expp
)
87 struct expression
*expr
= parse_c_1 (expp
, 0, 1);
89 register struct cleanup
*old_chain
90 = make_cleanup (free_current_contents
, &expr
);
92 val
= evaluate_expression (expr
);
93 do_cleanups (old_chain
);
97 /* Evaluate an expression in internal prefix form
98 such as is constructed by expread.y.
100 See expression.h for info on the format of an expression. */
102 static value
evaluate_subexp ();
103 static value
evaluate_subexp_for_address ();
104 static value
evaluate_subexp_for_sizeof ();
105 static value
evaluate_subexp_with_coercion ();
107 /* Values of NOSIDE argument to eval_subexp. */
110 EVAL_SKIP
, /* Only effect is to increment pos. */
111 EVAL_AVOID_SIDE_EFFECTS
, /* Don't modify any variables or
112 call any functions. The value
113 returned will have the correct
114 type, and will have an
115 approximately correct lvalue
116 type (inaccuracy: anything that is
117 listed as being in a register in
118 the function in which it was
119 declared will be lval_register). */
123 evaluate_expression (exp
)
124 struct expression
*exp
;
127 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_NORMAL
);
130 /* Evaluate an expression, avoiding all memory references
131 and getting a value whose type alone is correct. */
135 struct expression
*exp
;
138 return evaluate_subexp (NULL_TYPE
, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
142 evaluate_subexp (expect_type
, exp
, pos
, noside
)
143 struct type
*expect_type
;
144 register struct expression
*exp
;
150 register int pc
, pc2
, oldpos
;
151 register value arg1
, arg2
, arg3
;
156 op
= exp
->elts
[pc
].opcode
;
161 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
162 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
163 / sizeof (union exp_element
));
164 arg1
= value_static_field (exp
->elts
[pc
+ 1].type
,
165 &exp
->elts
[pc
+ 2].string
, -1);
167 error ("There is no field named %s", &exp
->elts
[pc
+ 2].string
);
172 return value_from_long (exp
->elts
[pc
+ 1].type
,
173 exp
->elts
[pc
+ 2].longconst
);
177 return value_from_double (exp
->elts
[pc
+ 1].type
,
178 exp
->elts
[pc
+ 2].doubleconst
);
182 if (noside
== EVAL_SKIP
)
184 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
186 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
189 switch (SYMBOL_CLASS (sym
))
193 case LOC_CONST_BYTES
:
207 return value_zero (SYMBOL_TYPE (sym
), lv
);
210 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
214 return access_value_history ((int) exp
->elts
[pc
+ 1].longconst
);
218 return value_of_register ((int) exp
->elts
[pc
+ 1].longconst
);
222 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
225 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
226 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
227 / sizeof (union exp_element
));
228 if (noside
== EVAL_SKIP
)
230 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
233 /* Skip third and second args to evaluate the first one. */
234 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
235 if (value_zerop (arg1
))
237 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
238 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
242 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
243 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
249 op
= exp
->elts
[*pos
].opcode
;
250 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
254 nargs
= (int) exp
->elts
[pc
+ 1].longconst
+ 1;
255 /* First, evaluate the structure into arg2 */
258 if (noside
== EVAL_SKIP
)
261 if (op
== STRUCTOP_MEMBER
)
263 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
267 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
270 /* If the function is a virtual function, then the
271 aggregate value (providing the structure) plays
272 its part by providing the vtable. Otherwise,
273 it is just along for the ride: call the function
276 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
278 fnptr
= (int) value_as_long (arg1
);
279 /* FIXME-tiemann: this is way obsolete. */
282 struct type
*basetype
;
284 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
285 basetype
= TYPE_VPTR_BASETYPE (basetype
);
286 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
288 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
289 /* If one is virtual, then all are virtual. */
290 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
291 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
292 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
295 value base
= value_ind (arg2
);
296 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
298 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
299 fill_in_vptr_fieldno (basetype
);
301 VALUE_TYPE (base
) = basetype
;
302 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
303 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
304 VALUE_TYPE (arg1
) = builtin_type_int
;
305 arg1
= value_subscript (vtbl
, arg1
);
306 VALUE_TYPE (arg1
) = fntype
;
311 error ("virtual function at index %d not found", fnptr
);
315 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
319 /* Now, say which argument to start evaluating from */
322 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
324 /* Hair for method invocations */
327 nargs
= (int) exp
->elts
[pc
+ 1].longconst
+ 1;
328 /* First, evaluate the structure into arg2 */
330 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
331 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
332 if (noside
== EVAL_SKIP
)
335 if (op
== STRUCTOP_STRUCT
)
337 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
341 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
343 /* Now, say which argument to start evaluating from */
348 nargs
= (int) exp
->elts
[pc
+ 1].longconst
;
351 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
352 for (; tem
<= nargs
; tem
++)
353 /* Ensure that array expressions are coerced into pointer objects. */
354 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
356 /* signal end of arglist */
359 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
366 value_struct_elt (&temp
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
368 op
== STRUCTOP_STRUCT
369 ? "structure" : "structure pointer");
370 if (VALUE_OFFSET (temp
))
372 arg2
= value_from_long (builtin_type_long
,
373 value_as_long (arg2
)+VALUE_OFFSET (temp
));
374 VALUE_TYPE (arg2
) = lookup_pointer_type (VALUE_TYPE (temp
));
379 argvec
[1] = argvec
[0];
384 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
390 if (noside
== EVAL_SKIP
)
392 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
394 /* If the return type doesn't look like a function type, call an
395 error. This can happen if somebody tries to turn a variable into
396 a function call. This is here because people often want to
397 call, eg, strcmp, which gdb doesn't know is a function. If
398 gdb isn't asked for it's opinion (ie. through "whatis"),
399 it won't offer it. */
402 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
405 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
407 error ("Expression of type other than \"Function returning ...\" used as function");
409 return target_call_function (argvec
[0], nargs
, argvec
+ 1);
411 case STRUCTOP_STRUCT
:
412 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
413 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
414 / sizeof (union exp_element
));
415 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
416 if (noside
== EVAL_SKIP
)
418 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
419 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
420 &exp
->elts
[pc
+ 1].string
,
426 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
427 (int *) 0, "structure");
431 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
432 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
433 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
434 if (noside
== EVAL_SKIP
)
436 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
437 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
439 &exp
->elts
[pc
+ 1].string
,
445 return value_struct_elt (&temp
, (value
*)0, &exp
->elts
[pc
+ 1].string
,
446 (int *) 0, "structure pointer");
449 case STRUCTOP_MEMBER
:
450 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
451 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
452 if (noside
== EVAL_SKIP
)
454 /* Now, convert these values to an address. */
455 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
456 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
458 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
459 != TYPE_CODE_METHOD
)))
460 error ("non-pointer-to-member value used in pointer-to-member construct");
461 arg3
= value_from_long (builtin_type_long
,
462 value_as_long (arg1
) + value_as_long (arg2
));
464 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (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_long (builtin_type_long
,
478 value_as_long (arg1
) + value_as_long (arg2
));
480 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))));
481 return value_ind (arg3
);
484 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
485 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
486 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
488 if (binop_user_defined_p (op
, arg1
, arg2
))
489 return value_x_binop (arg1
, arg2
, op
, 0);
491 return value_assign (arg1
, arg2
);
493 case BINOP_ASSIGN_MODIFY
:
495 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
496 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
497 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
499 op
= exp
->elts
[pc
+ 1].opcode
;
500 if (binop_user_defined_p (op
, arg1
, arg2
))
501 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
502 else if (op
== BINOP_ADD
)
503 arg2
= value_add (arg1
, arg2
);
504 else if (op
== BINOP_SUB
)
505 arg2
= value_sub (arg1
, arg2
);
507 arg2
= value_binop (arg1
, arg2
, op
);
508 return value_assign (arg1
, arg2
);
511 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
512 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
513 if (noside
== EVAL_SKIP
)
515 if (binop_user_defined_p (op
, arg1
, arg2
))
516 return value_x_binop (arg1
, arg2
, op
, 0);
518 return value_add (arg1
, arg2
);
521 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
522 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
523 if (noside
== EVAL_SKIP
)
525 if (binop_user_defined_p (op
, arg1
, arg2
))
526 return value_x_binop (arg1
, arg2
, op
, 0);
528 return value_sub (arg1
, arg2
);
538 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
539 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
540 if (noside
== EVAL_SKIP
)
542 if (binop_user_defined_p (op
, arg1
, arg2
))
543 return value_x_binop (arg1
, arg2
, op
, 0);
545 if (noside
== EVAL_AVOID_SIDE_EFFECTS
547 return value_zero (VALUE_TYPE (arg1
), not_lval
);
549 return value_binop (arg1
, arg2
, op
);
551 case BINOP_SUBSCRIPT
:
552 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
553 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
554 if (noside
== EVAL_SKIP
)
556 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
557 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
560 if (binop_user_defined_p (op
, arg1
, arg2
))
561 return value_x_binop (arg1
, arg2
, op
, 0);
563 return value_subscript (arg1
, arg2
);
566 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
567 if (noside
== EVAL_SKIP
)
569 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
574 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
577 if (binop_user_defined_p (op
, arg1
, arg2
))
579 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
580 return value_x_binop (arg1
, arg2
, op
, 0);
584 tem
= value_zerop (arg1
);
585 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
586 (tem
? EVAL_SKIP
: noside
));
587 return value_from_long (builtin_type_int
,
588 (LONGEST
) (!tem
&& !value_zerop (arg2
)));
592 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
593 if (noside
== EVAL_SKIP
)
595 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
600 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
603 if (binop_user_defined_p (op
, arg1
, arg2
))
605 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
606 return value_x_binop (arg1
, arg2
, op
, 0);
610 tem
= value_zerop (arg1
);
611 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
,
612 (!tem
? EVAL_SKIP
: noside
));
613 return value_from_long (builtin_type_int
,
614 (LONGEST
) (!tem
|| !value_zerop (arg2
)));
618 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
619 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
620 if (noside
== EVAL_SKIP
)
622 if (binop_user_defined_p (op
, arg1
, arg2
))
624 return value_x_binop (arg1
, arg2
, op
, 0);
628 tem
= value_equal (arg1
, arg2
);
629 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
633 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
634 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
635 if (noside
== EVAL_SKIP
)
637 if (binop_user_defined_p (op
, arg1
, arg2
))
639 return value_x_binop (arg1
, arg2
, op
, 0);
643 tem
= value_equal (arg1
, arg2
);
644 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
648 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
649 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
650 if (noside
== EVAL_SKIP
)
652 if (binop_user_defined_p (op
, arg1
, arg2
))
654 return value_x_binop (arg1
, arg2
, op
, 0);
658 tem
= value_less (arg1
, arg2
);
659 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
663 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
664 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
665 if (noside
== EVAL_SKIP
)
667 if (binop_user_defined_p (op
, arg1
, arg2
))
669 return value_x_binop (arg1
, arg2
, op
, 0);
673 tem
= value_less (arg2
, arg1
);
674 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
678 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
679 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
680 if (noside
== EVAL_SKIP
)
682 if (binop_user_defined_p (op
, arg1
, arg2
))
684 return value_x_binop (arg1
, arg2
, op
, 0);
688 tem
= value_less (arg1
, arg2
);
689 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
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
, 0);
703 tem
= value_less (arg2
, arg1
);
704 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
708 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
709 arg2
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
710 if (noside
== EVAL_SKIP
)
712 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
713 error ("Non-integral right operand for \"@\" operator.");
714 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
715 return allocate_repeat_value (VALUE_TYPE (arg1
),
716 (int) value_as_long (arg2
));
718 return value_repeat (arg1
, (int) value_as_long (arg2
));
721 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
722 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
725 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
726 if (noside
== EVAL_SKIP
)
728 if (unop_user_defined_p (op
, arg1
))
729 return value_x_unop (arg1
, op
);
731 return value_neg (arg1
);
734 /* C++: check for and handle destructor names. */
735 op
= exp
->elts
[*pos
].opcode
;
737 /* FIXME-tiemann: this is a cop-out. */
739 error ("destructor in eval");
741 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
742 if (noside
== EVAL_SKIP
)
744 if (unop_user_defined_p (UNOP_LOGNOT
, arg1
))
745 return value_x_unop (arg1
, UNOP_LOGNOT
);
747 return value_lognot (arg1
);
750 arg1
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
751 if (noside
== EVAL_SKIP
)
753 if (unop_user_defined_p (op
, arg1
))
754 return value_x_unop (arg1
, op
);
756 return value_from_long (builtin_type_int
,
757 (LONGEST
) value_zerop (arg1
));
760 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
761 expect_type
= TYPE_TARGET_TYPE (expect_type
);
762 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
763 if (noside
== EVAL_SKIP
)
765 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
767 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
768 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
769 /* In C you can dereference an array to get the 1st elt. */
770 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
772 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
774 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
775 /* GDB allows dereferencing an int. */
776 return value_zero (builtin_type_int
, lval_memory
);
778 error ("Attempt to take contents of a non-pointer value.");
780 return value_ind (arg1
);
783 /* C++: check for and handle pointer to members. */
785 op
= exp
->elts
[*pos
].opcode
;
787 if (noside
== EVAL_SKIP
)
791 char *name
= &exp
->elts
[pc
+3].string
;
792 int temm
= strlen (name
);
793 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
796 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
802 char *name
= &exp
->elts
[pc
+3].string
;
803 int temm
= strlen (name
);
804 struct type
*domain
= exp
->elts
[pc
+2].type
;
805 (*pos
) += 2 + (temm
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
806 arg1
= value_struct_elt_for_address (domain
, expect_type
, name
);
809 error ("no field `%s' in structure", name
);
812 return evaluate_subexp_for_address (exp
, pos
, noside
);
815 if (noside
== EVAL_SKIP
)
817 evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_SKIP
);
820 return evaluate_subexp_for_sizeof (exp
, pos
);
824 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
825 if (noside
== EVAL_SKIP
)
827 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
831 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
832 if (noside
== EVAL_SKIP
)
834 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
835 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
837 return value_at_lazy (exp
->elts
[pc
+ 1].type
,
838 (CORE_ADDR
) value_as_long (arg1
));
840 case UNOP_PREINCREMENT
:
841 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
842 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
844 else if (unop_user_defined_p (op
, arg1
))
846 return value_x_unop (arg1
, op
);
850 arg2
= value_add (arg1
, value_from_long (builtin_type_char
,
852 return value_assign (arg1
, arg2
);
855 case UNOP_PREDECREMENT
:
856 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
857 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
859 else if (unop_user_defined_p (op
, arg1
))
861 return value_x_unop (arg1
, op
);
865 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
,
867 return value_assign (arg1
, arg2
);
870 case UNOP_POSTINCREMENT
:
871 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
872 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
874 else if (unop_user_defined_p (op
, arg1
))
876 return value_x_unop (arg1
, op
);
880 arg2
= value_add (arg1
, value_from_long (builtin_type_char
,
882 value_assign (arg1
, arg2
);
886 case UNOP_POSTDECREMENT
:
887 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
888 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
890 else if (unop_user_defined_p (op
, arg1
))
892 return value_x_unop (arg1
, op
);
896 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
,
898 value_assign (arg1
, arg2
);
904 return value_of_this (1);
907 error ("internal error: I do not know how to evaluate what you gave me");
911 return value_from_long (builtin_type_long
, (LONGEST
) 1);
914 /* Evaluate a subexpression of EXP, at index *POS,
915 and return the address of that subexpression.
916 Advance *POS over the subexpression.
917 If the subexpression isn't an lvalue, get an error.
918 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
919 then only the type of the result need be correct. */
922 evaluate_subexp_for_address (exp
, pos
, noside
)
923 register struct expression
*exp
;
931 op
= exp
->elts
[pc
].opcode
;
937 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
941 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
942 evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
946 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
949 lookup_pointer_type (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
));
950 enum address_class sym_class
=
951 SYMBOL_CLASS (exp
->elts
[pc
+ 1].symbol
);
953 if (sym_class
== LOC_CONST
954 || sym_class
== LOC_CONST_BYTES
955 || sym_class
== LOC_REGISTER
956 || sym_class
== LOC_REGPARM
)
957 error ("Attempt to take address of register or constant.");
960 value_zero (type
, not_lval
);
963 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
966 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
968 value x
= evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
969 if (VALUE_LVAL (x
) == lval_memory
)
970 return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x
)),
973 error ("Attempt to take address of non-lval");
975 return value_addr (evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
));
979 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
980 When used in contexts where arrays will be coerced anyway,
981 this is equivalent to `evaluate_subexp'
982 but much faster because it avoids actually fetching array contents. */
985 evaluate_subexp_with_coercion (exp
, pos
, noside
)
986 register struct expression
*exp
;
990 register enum exp_opcode op
;
995 op
= exp
->elts
[pc
].opcode
;
1000 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
1003 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
1004 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
1008 return evaluate_subexp (NULL_TYPE
, exp
, pos
, noside
);
1012 /* Evaluate a subexpression of EXP, at index *POS,
1013 and return a value for the size of that subexpression.
1014 Advance *POS over the subexpression. */
1017 evaluate_subexp_for_sizeof (exp
, pos
)
1018 register struct expression
*exp
;
1026 op
= exp
->elts
[pc
].opcode
;
1030 /* This case is handled specially
1031 so that we avoid creating a value for the result type.
1032 If the result type is very big, it's desirable not to
1033 create a value unnecessarily. */
1036 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1037 return value_from_long (builtin_type_int
, (LONGEST
)
1038 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1042 return value_from_long (builtin_type_int
,
1043 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1047 return value_from_long (builtin_type_int
,
1048 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1051 val
= evaluate_subexp (NULL_TYPE
, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1052 return value_from_long (builtin_type_int
,
1053 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));