1806e5218d7e2f017ca7af41d366693ac7d8af81
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. */
24 #include "expression.h"
27 /* Parse the string EXP as a C expression, evaluate it,
28 and return the result as a number. */
31 parse_and_eval_address (exp
)
34 struct expression
*expr
= parse_c_expression (exp
);
35 register CORE_ADDR addr
;
36 register struct cleanup
*old_chain
37 = make_cleanup (free_current_contents
, &expr
);
39 addr
= (CORE_ADDR
) value_as_long (evaluate_expression (expr
));
40 do_cleanups (old_chain
);
44 /* Like parse_and_eval_address but takes a pointer to a char * variable
45 and advanced that variable across the characters parsed. */
48 parse_and_eval_address_1 (expptr
)
51 struct expression
*expr
= parse_c_1 (expptr
, 0, 0);
52 register CORE_ADDR addr
;
53 register struct cleanup
*old_chain
54 = make_cleanup (free_current_contents
, &expr
);
56 addr
= value_as_long (evaluate_expression (expr
));
57 do_cleanups (old_chain
);
65 struct expression
*expr
= parse_c_expression (exp
);
67 register struct cleanup
*old_chain
68 = make_cleanup (free_current_contents
, &expr
);
70 val
= evaluate_expression (expr
);
71 do_cleanups (old_chain
);
75 /* Parse up to a comma (or to a closeparen)
76 in the string EXPP as an expression, evaluate it, and return the value.
77 EXPP is advanced to point to the comma. */
80 parse_to_comma_and_eval (expp
)
83 struct expression
*expr
= parse_c_1 (expp
, 0, 1);
85 register struct cleanup
*old_chain
86 = make_cleanup (free_current_contents
, &expr
);
88 val
= evaluate_expression (expr
);
89 do_cleanups (old_chain
);
93 /* Evaluate an expression in internal prefix form
94 such as is constructed by expread.y.
96 See expression.h for info on the format of an expression. */
98 static value
evaluate_subexp ();
99 static value
evaluate_subexp_for_address ();
100 static value
evaluate_subexp_for_sizeof ();
101 static value
evaluate_subexp_with_coercion ();
103 /* Values of NOSIDE argument to eval_subexp. */
106 EVAL_SKIP
, /* Only effect is to increment pos. */
107 EVAL_AVOID_SIDE_EFFECTS
, /* Don't modify any variables or
108 call any functions. The value
109 returned will have the correct
110 type, and will have an
111 approximately correct lvalue
112 type (inaccuracy: anything that is
113 listed as being in a register in
114 the function in which it was
115 declared will be lval_register). */
119 evaluate_expression (exp
)
120 struct expression
*exp
;
123 return evaluate_subexp (0, exp
, &pc
, EVAL_NORMAL
);
126 /* Evaluate an expression, avoiding all memory references
127 and getting a value whose type alone is correct. */
131 struct expression
*exp
;
134 return evaluate_subexp (0, exp
, &pc
, EVAL_AVOID_SIDE_EFFECTS
);
138 evaluate_subexp (expect_type
, exp
, pos
, noside
)
139 struct type
*expect_type
;
140 register struct expression
*exp
;
146 register int pc
, pc2
, oldpos
;
147 register value arg1
, arg2
, arg3
;
152 op
= exp
->elts
[pc
].opcode
;
157 tem
= strlen (&exp
->elts
[pc
+ 2].string
);
158 (*pos
) += 3 + ((tem
+ sizeof (union exp_element
))
159 / sizeof (union exp_element
));
160 return value_static_field (exp
->elts
[pc
+ 1].type
,
161 &exp
->elts
[pc
+ 2].string
, -1);
165 return value_from_long (exp
->elts
[pc
+ 1].type
,
166 exp
->elts
[pc
+ 2].longconst
);
170 return value_from_double (exp
->elts
[pc
+ 1].type
,
171 exp
->elts
[pc
+ 2].doubleconst
);
175 if (noside
== EVAL_SKIP
)
177 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
179 struct symbol
* sym
= exp
->elts
[pc
+ 1].symbol
;
182 switch (SYMBOL_CLASS (sym
))
186 case LOC_CONST_BYTES
:
195 return value_zero (SYMBOL_TYPE (sym
), lv
);
198 return value_of_variable (exp
->elts
[pc
+ 1].symbol
);
202 return access_value_history ((int) exp
->elts
[pc
+ 1].longconst
);
206 return value_of_register ((int) exp
->elts
[pc
+ 1].longconst
);
210 return value_of_internalvar (exp
->elts
[pc
+ 1].internalvar
);
213 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
214 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
215 / sizeof (union exp_element
));
216 if (noside
== EVAL_SKIP
)
218 return value_string (&exp
->elts
[pc
+ 1].string
, tem
);
221 /* Skip third and second args to evaluate the first one. */
222 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
223 if (value_zerop (arg1
))
225 evaluate_subexp (0, exp
, pos
, EVAL_SKIP
);
226 return evaluate_subexp (0, exp
, pos
, noside
);
230 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
231 evaluate_subexp (0, exp
, pos
, EVAL_SKIP
);
237 op
= exp
->elts
[*pos
].opcode
;
238 if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
243 nargs
= (int) exp
->elts
[pc
+ 1].longconst
+ 1;
244 /* First, evaluate the structure into arg2 */
247 if (noside
== EVAL_SKIP
)
250 if (op
== STRUCTOP_MEMBER
)
252 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
256 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
259 /* If the function is a virtual function, then the
260 aggregate value (providing the structure) plays
261 its part by providing the vtable. Otherwise,
262 it is just along for the ride: call the function
265 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
267 fnptr
= (int) value_as_long (arg1
);
270 struct type
*basetype
;
272 basetype
= TYPE_TARGET_TYPE (VALUE_TYPE (arg2
));
273 basetype
= TYPE_VPTR_BASETYPE (basetype
);
274 for (i
= TYPE_NFN_FIELDS (basetype
) - 1; i
>= 0; i
--)
276 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (basetype
, i
);
277 /* If one is virtual, then all are virtual. */
278 if (TYPE_FN_FIELD_VIRTUAL_P (f
, 0))
279 for (j
= TYPE_FN_FIELDLIST_LENGTH (basetype
, i
) - 1; j
>= 0; --j
)
280 if (TYPE_FN_FIELD_VOFFSET (f
, j
) == fnptr
)
283 value base
= value_ind (arg2
);
284 struct type
*fntype
= lookup_pointer_type (TYPE_FN_FIELD_TYPE (f
, j
));
286 if (TYPE_VPTR_FIELDNO (basetype
) < 0)
287 TYPE_VPTR_FIELDNO (basetype
)
288 = fill_in_vptr_fieldno (basetype
);
290 VALUE_TYPE (base
) = basetype
;
291 vtbl
= value_field (base
, TYPE_VPTR_FIELDNO (basetype
));
292 VALUE_TYPE (vtbl
) = lookup_pointer_type (fntype
);
293 VALUE_TYPE (arg1
) = builtin_type_int
;
294 arg1
= value_subscript (vtbl
, arg1
);
295 VALUE_TYPE (arg1
) = fntype
;
300 error ("virtual function at index %d not found", fnptr
);
304 VALUE_TYPE (arg1
) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)));
308 /* Now, say which argument to start evaluating from */
311 else if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
313 /* Hair for method invocations */
316 nargs
= (int) exp
->elts
[pc
+ 1].longconst
+ 1;
317 /* First, evaluate the structure into arg2 */
319 tem2
= strlen (&exp
->elts
[pc2
+ 1].string
);
320 *pos
+= 2 + (tem2
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
321 if (noside
== EVAL_SKIP
)
324 if (op
== STRUCTOP_STRUCT
)
326 arg2
= evaluate_subexp_for_address (exp
, pos
, noside
);
330 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
332 /* Now, say which argument to start evaluating from */
337 nargs
= (int) exp
->elts
[pc
+ 1].longconst
;
340 argvec
= (value
*) alloca (sizeof (value
) * (nargs
+ 2));
341 for (; tem
<= nargs
; tem
++)
342 /* Ensure that array expressions are coerced into pointer objects. */
343 argvec
[tem
] = evaluate_subexp_with_coercion (exp
, pos
, noside
);
345 /* signal end of arglist */
348 if (op
== STRUCTOP_STRUCT
|| op
== STRUCTOP_PTR
)
354 value_struct_elt (arg2
, argvec
+1, &exp
->elts
[pc2
+ 1].string
,
356 op
== STRUCTOP_STRUCT
357 ? "structure" : "structure pointer");
360 argvec
[1] = argvec
[0];
365 else if (op
== STRUCTOP_MEMBER
|| op
== STRUCTOP_MPTR
)
371 if (noside
== EVAL_SKIP
)
373 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
375 /* If the return type doesn't look like a function type, call an
376 error. This can happen if somebody tries to turn a variable into
377 a function call. This is here because people often want to
378 call, eg, strcmp, which gdb doesn't know is a function. If
379 gdb isn't asked for it's opinion (ie. through "whatis"),
380 it won't offer it. */
383 TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0]));
386 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec
[0])));
388 error ("Expression of type other than \"Function returning ...\" used as function");
390 return call_function (argvec
[0], nargs
, argvec
+ 1);
392 case STRUCTOP_STRUCT
:
393 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
394 (*pos
) += 2 + ((tem
+ sizeof (union exp_element
))
395 / sizeof (union exp_element
));
396 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
397 if (noside
== EVAL_SKIP
)
399 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
400 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1
),
401 &exp
->elts
[pc
+ 1].string
),
404 return value_struct_elt (arg1
, 0, &exp
->elts
[pc
+ 1].string
, 0,
408 tem
= strlen (&exp
->elts
[pc
+ 1].string
);
409 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
410 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
411 if (noside
== EVAL_SKIP
)
413 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
414 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
416 &exp
->elts
[pc
+ 1].string
),
419 return value_struct_elt (arg1
, 0, &exp
->elts
[pc
+ 1].string
, 0,
420 "structure pointer");
422 case STRUCTOP_MEMBER
:
423 arg1
= evaluate_subexp_for_address (exp
, pos
, noside
);
424 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
425 if (noside
== EVAL_SKIP
)
427 /* Now, convert these values to an address. */
428 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
429 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
431 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
)))
432 != TYPE_CODE_METHOD
)))
433 error ("non-pointer-to-member value used in pointer-to-member construct");
434 arg3
= value_from_long (builtin_type_long
,
435 value_as_long (arg1
) + value_as_long (arg2
));
437 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))));
438 return value_ind (arg3
);
441 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
442 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
443 if (noside
== EVAL_SKIP
)
445 /* Now, convert these values to an address. */
446 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_PTR
447 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_MEMBER
448 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))) != TYPE_CODE_METHOD
))
449 error ("non-pointer-to-member value used in pointer-to-member construct");
450 arg3
= value_from_long (builtin_type_long
,
451 value_as_long (arg1
) + value_as_long (arg2
));
453 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2
))));
454 return value_ind (arg3
);
457 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
458 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
459 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
461 if (binop_user_defined_p (op
, arg1
, arg2
))
462 return value_x_binop (arg1
, arg2
, op
, 0);
464 return value_assign (arg1
, arg2
);
466 case BINOP_ASSIGN_MODIFY
:
468 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
469 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
470 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
472 op
= exp
->elts
[pc
+ 1].opcode
;
473 if (binop_user_defined_p (op
, arg1
, arg2
))
474 return value_x_binop (arg1
, arg2
, BINOP_ASSIGN_MODIFY
, op
);
475 else if (op
== BINOP_ADD
)
476 arg2
= value_add (arg1
, arg2
);
477 else if (op
== BINOP_SUB
)
478 arg2
= value_sub (arg1
, arg2
);
480 arg2
= value_binop (arg1
, arg2
, op
);
481 return value_assign (arg1
, arg2
);
484 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
485 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
486 if (noside
== EVAL_SKIP
)
488 if (binop_user_defined_p (op
, arg1
, arg2
))
489 return value_x_binop (arg1
, arg2
, op
, 0);
491 return value_add (arg1
, arg2
);
494 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
495 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
496 if (noside
== EVAL_SKIP
)
498 if (binop_user_defined_p (op
, arg1
, arg2
))
499 return value_x_binop (arg1
, arg2
, op
, 0);
501 return value_sub (arg1
, arg2
);
511 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
512 arg2
= evaluate_subexp (0, 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 if (noside
== EVAL_AVOID_SIDE_EFFECTS
520 return value_zero (VALUE_TYPE (arg1
), not_lval
);
522 return value_binop (arg1
, arg2
, op
);
524 case BINOP_SUBSCRIPT
:
525 arg1
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
526 arg2
= evaluate_subexp_with_coercion (exp
, pos
, noside
);
527 if (noside
== EVAL_SKIP
)
529 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
530 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
533 if (binop_user_defined_p (op
, arg1
, arg2
))
534 return value_x_binop (arg1
, arg2
, op
, 0);
536 return value_subscript (arg1
, arg2
);
539 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
540 if (noside
== EVAL_SKIP
)
542 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
547 arg2
= evaluate_subexp (0, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
550 if (binop_user_defined_p (op
, arg1
, arg2
))
552 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
553 return value_x_binop (arg1
, arg2
, op
, 0);
557 tem
= value_zerop (arg1
);
558 arg2
= evaluate_subexp (0, exp
, pos
,
559 (tem
? EVAL_SKIP
: noside
));
560 return value_from_long (builtin_type_int
,
561 (LONGEST
) (!tem
&& !value_zerop (arg2
)));
565 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
566 if (noside
== EVAL_SKIP
)
568 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
573 arg2
= evaluate_subexp (0, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
576 if (binop_user_defined_p (op
, arg1
, arg2
))
578 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
579 return value_x_binop (arg1
, arg2
, op
, 0);
583 tem
= value_zerop (arg1
);
584 arg2
= evaluate_subexp (0, exp
, pos
,
585 (!tem
? EVAL_SKIP
: noside
));
586 return value_from_long (builtin_type_int
,
587 (LONGEST
) (!tem
|| !value_zerop (arg2
)));
591 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
592 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
593 if (noside
== EVAL_SKIP
)
595 if (binop_user_defined_p (op
, arg1
, arg2
))
597 return value_x_binop (arg1
, arg2
, op
, 0);
601 tem
= value_equal (arg1
, arg2
);
602 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
606 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
607 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
608 if (noside
== EVAL_SKIP
)
610 if (binop_user_defined_p (op
, arg1
, arg2
))
612 return value_x_binop (arg1
, arg2
, op
, 0);
616 tem
= value_equal (arg1
, arg2
);
617 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
621 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
622 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
623 if (noside
== EVAL_SKIP
)
625 if (binop_user_defined_p (op
, arg1
, arg2
))
627 return value_x_binop (arg1
, arg2
, op
, 0);
631 tem
= value_less (arg1
, arg2
);
632 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
636 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
637 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
638 if (noside
== EVAL_SKIP
)
640 if (binop_user_defined_p (op
, arg1
, arg2
))
642 return value_x_binop (arg1
, arg2
, op
, 0);
646 tem
= value_less (arg2
, arg1
);
647 return value_from_long (builtin_type_int
, (LONGEST
) tem
);
651 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
652 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
653 if (noside
== EVAL_SKIP
)
655 if (binop_user_defined_p (op
, arg1
, arg2
))
657 return value_x_binop (arg1
, arg2
, op
, 0);
661 tem
= value_less (arg1
, arg2
);
662 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
666 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
667 arg2
= evaluate_subexp (VALUE_TYPE (arg1
), exp
, pos
, noside
);
668 if (noside
== EVAL_SKIP
)
670 if (binop_user_defined_p (op
, arg1
, arg2
))
672 return value_x_binop (arg1
, arg2
, op
, 0);
676 tem
= value_less (arg2
, arg1
);
677 return value_from_long (builtin_type_int
, (LONGEST
) ! tem
);
681 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
682 arg2
= evaluate_subexp (0, exp
, pos
, noside
);
683 if (noside
== EVAL_SKIP
)
685 if (TYPE_CODE (VALUE_TYPE (arg2
)) != TYPE_CODE_INT
)
686 error ("Non-integral right operand for \"@\" operator.");
687 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
688 return allocate_repeat_value (VALUE_TYPE (arg1
),
689 (int) value_as_long (arg2
));
691 return value_repeat (arg1
, (int) value_as_long (arg2
));
694 evaluate_subexp (0, exp
, pos
, noside
);
695 return evaluate_subexp (0, exp
, pos
, noside
);
698 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
699 if (noside
== EVAL_SKIP
)
701 if (unop_user_defined_p (op
, arg1
))
702 return value_x_unop (arg1
, op
);
704 return value_neg (arg1
);
707 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
708 if (noside
== EVAL_SKIP
)
710 if (unop_user_defined_p (op
, arg1
))
711 return value_x_unop (arg1
, op
);
713 return value_lognot (arg1
);
716 arg1
= evaluate_subexp (0, exp
, pos
, noside
);
717 if (noside
== EVAL_SKIP
)
719 if (unop_user_defined_p (op
, arg1
))
720 return value_x_unop (arg1
, op
);
722 return value_from_long (builtin_type_int
,
723 (LONGEST
) value_zerop (arg1
));
726 if (expect_type
&& TYPE_CODE (expect_type
) == TYPE_CODE_PTR
)
727 expect_type
= TYPE_TARGET_TYPE (expect_type
);
728 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
729 if (noside
== EVAL_SKIP
)
731 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
733 if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_PTR
734 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_REF
735 /* In C you can dereference an array to get the 1st elt. */
736 || TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_ARRAY
738 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1
)),
740 else if (TYPE_CODE (VALUE_TYPE (arg1
)) == TYPE_CODE_INT
)
741 /* GDB allows dereferencing an int. */
742 return value_zero (BUILTIN_TYPE_LONGEST
, lval_memory
);
744 error ("Attempt to take contents of a non-pointer value.");
746 return value_ind (arg1
);
749 /* C++: check for and handle pointer to members. */
751 op
= exp
->elts
[*pos
].opcode
;
753 if (noside
== EVAL_SKIP
)
757 char *name
= &exp
->elts
[pc
+3].string
;
758 int tem
= strlen (name
);
759 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
762 evaluate_subexp (expect_type
, exp
, pos
, EVAL_SKIP
);
768 char *name
= &exp
->elts
[pc
+3].string
;
769 int tem
= strlen (name
);
770 struct type
*domain
= exp
->elts
[pc
+2].type
;
771 (*pos
) += 2 + (tem
+ sizeof (union exp_element
)) / sizeof (union exp_element
);
772 arg1
= value_struct_elt_for_address (domain
, expect_type
, name
);
775 error ("no field `%s' in structure", name
);
778 return evaluate_subexp_for_address (exp
, pos
, noside
);
781 if (noside
== EVAL_SKIP
)
783 evaluate_subexp (0, exp
, pos
, EVAL_SKIP
);
786 return evaluate_subexp_for_sizeof (exp
, pos
);
790 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
791 if (noside
== EVAL_SKIP
)
793 return value_cast (exp
->elts
[pc
+ 1].type
, arg1
);
797 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
798 if (noside
== EVAL_SKIP
)
800 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
801 return value_zero (exp
->elts
[pc
+ 1].type
, lval_memory
);
803 return value_at (exp
->elts
[pc
+ 1].type
,
804 (CORE_ADDR
) value_as_long (arg1
));
806 case UNOP_PREINCREMENT
:
807 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
808 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
810 else if (unop_user_defined_p (op
, arg1
))
812 return value_x_unop (arg1
, op
);
816 arg2
= value_add (arg1
, value_from_long (builtin_type_char
,
818 return value_assign (arg1
, arg2
);
821 case UNOP_PREDECREMENT
:
822 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
823 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
825 else if (unop_user_defined_p (op
, arg1
))
827 return value_x_unop (arg1
, op
);
831 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
,
833 return value_assign (arg1
, arg2
);
836 case UNOP_POSTINCREMENT
:
837 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
838 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
840 else if (unop_user_defined_p (op
, arg1
))
842 return value_x_unop (arg1
, op
);
846 arg2
= value_add (arg1
, value_from_long (builtin_type_char
,
848 value_assign (arg1
, arg2
);
852 case UNOP_POSTDECREMENT
:
853 arg1
= evaluate_subexp (expect_type
, exp
, pos
, noside
);
854 if (noside
== EVAL_SKIP
|| noside
== EVAL_AVOID_SIDE_EFFECTS
)
856 else if (unop_user_defined_p (op
, arg1
))
858 return value_x_unop (arg1
, op
);
862 arg2
= value_sub (arg1
, value_from_long (builtin_type_char
,
864 value_assign (arg1
, arg2
);
870 return value_of_this (1);
873 error ("internal error: I do not know how to evaluate what you gave me");
877 return value_from_long (builtin_type_long
, (LONGEST
) 1);
880 /* Evaluate a subexpression of EXP, at index *POS,
881 and return the address of that subexpression.
882 Advance *POS over the subexpression.
883 If the subexpression isn't an lvalue, get an error.
884 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
885 then only the type of the result need be correct. */
888 evaluate_subexp_for_address (exp
, pos
, noside
)
889 register struct expression
*exp
;
897 op
= exp
->elts
[pc
].opcode
;
903 return evaluate_subexp (0, exp
, pos
, noside
);
907 return value_cast (lookup_pointer_type (exp
->elts
[pc
+ 1].type
),
908 evaluate_subexp (0, exp
, pos
, noside
));
912 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
915 lookup_pointer_type (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
));
916 enum address_class sym_class
=
917 SYMBOL_CLASS (exp
->elts
[pc
+ 1].symbol
);
919 if (sym_class
== LOC_CONST
920 || sym_class
== LOC_CONST_BYTES
921 || sym_class
== LOC_REGISTER
922 || sym_class
== LOC_REGPARM
)
923 error ("Attempt to take address of register or constant.");
926 value_zero (type
, not_lval
);
929 return locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
932 if (noside
== EVAL_AVOID_SIDE_EFFECTS
)
934 value x
= evaluate_subexp (0, exp
, pos
, noside
);
935 if (VALUE_LVAL (x
) == lval_memory
)
936 return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x
)),
939 error ("Attempt to take address of non-lval");
941 return value_addr (evaluate_subexp (0, exp
, pos
, noside
));
945 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
946 When used in contexts where arrays will be coerced anyway,
947 this is equivalent to `evaluate_subexp'
948 but much faster because it avoids actually fetching array contents. */
951 evaluate_subexp_with_coercion (exp
, pos
, noside
)
952 register struct expression
*exp
;
956 register enum exp_opcode op
;
961 op
= exp
->elts
[pc
].opcode
;
966 if (TYPE_CODE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)) == TYPE_CODE_ARRAY
)
969 val
= locate_var_value (exp
->elts
[pc
+ 1].symbol
, (CORE_ADDR
) 0);
970 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
))),
975 return evaluate_subexp (0, exp
, pos
, noside
);
978 /* Evaluate a subexpression of EXP, at index *POS,
979 and return a value for the size of that subexpression.
980 Advance *POS over the subexpression. */
983 evaluate_subexp_for_sizeof (exp
, pos
)
984 register struct expression
*exp
;
992 op
= exp
->elts
[pc
].opcode
;
996 /* This case is handled specially
997 so that we avoid creating a value for the result type.
998 If the result type is very big, it's desirable not to
999 create a value unnecessarily. */
1002 val
= evaluate_subexp (0, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1003 return value_from_long (builtin_type_int
, (LONGEST
)
1004 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val
))));
1008 return value_from_long (builtin_type_int
,
1009 (LONGEST
) TYPE_LENGTH (exp
->elts
[pc
+ 1].type
));
1013 return value_from_long (builtin_type_int
,
1014 (LONGEST
) TYPE_LENGTH (SYMBOL_TYPE (exp
->elts
[pc
+ 1].symbol
)));
1017 val
= evaluate_subexp (0, exp
, pos
, EVAL_AVOID_SIDE_EFFECTS
);
1018 return value_from_long (builtin_type_int
,
1019 (LONGEST
) TYPE_LENGTH (VALUE_TYPE (val
)));
This page took 0.050442 seconds and 4 git commands to generate.