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 #include "language.h" /* For CAST_IS_CONVERSION */
30 /* Values of NOSIDE argument to eval_subexp. */
33 EVAL_SKIP, /* Only effect is to increment pos. */
34 EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
35 call any functions. The value
36 returned will have the correct
37 type, and will have an
38 approximately correct lvalue
39 type (inaccuracy: anything that is
40 listed as being in a register in
41 the function in which it was
42 declared will be lval_register). */
45 /* Prototypes for local functions. */
48 evaluate_subexp_for_sizeof PARAMS ((struct expression *, int *));
51 evaluate_subexp_with_coercion PARAMS ((struct expression *, int *,
55 evaluate_subexp_for_address PARAMS ((struct expression *, int *,
59 evaluate_subexp PARAMS ((struct type *, struct expression *, int *,
63 /* Parse the string EXP as a C expression, evaluate it,
64 and return the result as a number. */
67 parse_and_eval_address (exp)
70 struct expression *expr = parse_expression (exp);
71 register CORE_ADDR addr;
72 register struct cleanup *old_chain =
73 make_cleanup (free_current_contents, &expr);
75 addr = value_as_pointer (evaluate_expression (expr));
76 do_cleanups (old_chain);
80 /* Like parse_and_eval_address but takes a pointer to a char * variable
81 and advanced that variable across the characters parsed. */
84 parse_and_eval_address_1 (expptr)
87 struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0);
88 register CORE_ADDR addr;
89 register struct cleanup *old_chain =
90 make_cleanup (free_current_contents, &expr);
92 addr = value_as_pointer (evaluate_expression (expr));
93 do_cleanups (old_chain);
101 struct expression *expr = parse_expression (exp);
103 register struct cleanup *old_chain
104 = make_cleanup (free_current_contents, &expr);
106 val = evaluate_expression (expr);
107 do_cleanups (old_chain);
111 /* Parse up to a comma (or to a closeparen)
112 in the string EXPP as an expression, evaluate it, and return the value.
113 EXPP is advanced to point to the comma. */
116 parse_to_comma_and_eval (expp)
119 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
121 register struct cleanup *old_chain
122 = make_cleanup (free_current_contents, &expr);
124 val = evaluate_expression (expr);
125 do_cleanups (old_chain);
129 /* Evaluate an expression in internal prefix form
130 such as is constructed by parse.y.
132 See expression.h for info on the format of an expression. */
134 static value evaluate_subexp ();
135 static value evaluate_subexp_for_address ();
136 static value evaluate_subexp_for_sizeof ();
137 static value evaluate_subexp_with_coercion ();
140 evaluate_expression (exp)
141 struct expression *exp;
144 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
147 /* Evaluate an expression, avoiding all memory references
148 and getting a value whose type alone is correct. */
152 struct expression *exp;
155 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
159 evaluate_subexp (expect_type, exp, pos, noside)
160 struct type *expect_type;
161 register struct expression *exp;
167 register int pc, pc2 = 0, oldpos;
168 register value arg1 = NULL, arg2 = NULL, arg3;
174 op = exp->elts[pc].opcode;
179 tem = longest_to_int (exp->elts[pc + 2].longconst);
180 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
181 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
183 exp->elts[pc + 1].type,
184 &exp->elts[pc + 3].string,
187 error ("There is no field named %s", &exp->elts[pc + 3].string);
192 return value_from_longest (exp->elts[pc + 1].type,
193 exp->elts[pc + 2].longconst);
197 return value_from_double (exp->elts[pc + 1].type,
198 exp->elts[pc + 2].doubleconst);
202 if (noside == EVAL_SKIP)
204 if (noside == EVAL_AVOID_SIDE_EFFECTS)
206 struct symbol * sym = exp->elts[pc + 2].symbol;
209 switch (SYMBOL_CLASS (sym))
213 case LOC_CONST_BYTES:
227 return value_zero (SYMBOL_TYPE (sym), lv);
230 return value_of_variable (exp->elts[pc + 2].symbol,
231 exp->elts[pc + 1].block);
236 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
240 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
244 return value_from_longest (builtin_type_chill_bool,
245 exp->elts[pc + 1].longconst);
249 return value_of_internalvar (exp->elts[pc + 1].internalvar);
252 tem = longest_to_int (exp->elts[pc + 1].longconst);
253 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
254 if (noside == EVAL_SKIP)
256 return value_string (&exp->elts[pc + 2].string, tem);
259 error ("support for OP_BITSTRING unimplemented");
264 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
265 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
266 nargs = tem3 - tem2 + 1;
267 argvec = (value *) alloca (sizeof (value) * nargs);
268 for (tem = 0; tem < nargs; tem++)
270 /* Ensure that array expressions are coerced into pointer objects. */
271 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
273 if (noside == EVAL_SKIP)
275 return (value_array (tem2, tem3, argvec));
279 /* Skip third and second args to evaluate the first one. */
280 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
281 if (value_logical_not (arg1))
283 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
284 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
288 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
289 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
295 op = exp->elts[*pos].opcode;
296 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
300 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
301 /* First, evaluate the structure into arg2 */
304 if (noside == EVAL_SKIP)
307 if (op == STRUCTOP_MEMBER)
309 arg2 = evaluate_subexp_for_address (exp, pos, noside);
313 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
316 /* If the function is a virtual function, then the
317 aggregate value (providing the structure) plays
318 its part by providing the vtable. Otherwise,
319 it is just along for the ride: call the function
322 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
324 fnptr = longest_to_int (value_as_long (arg1));
326 if (METHOD_PTR_IS_VIRTUAL(fnptr))
328 int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
329 struct type *basetype;
330 struct type *domain_type =
331 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
333 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
334 if (domain_type != basetype)
335 arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
336 basetype = TYPE_VPTR_BASETYPE (domain_type);
337 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
339 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
340 /* If one is virtual, then all are virtual. */
341 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
342 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
343 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
345 value temp = value_ind (arg2);
346 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
347 arg2 = value_addr (temp);
352 error ("virtual function at index %d not found", fnoffset);
356 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
360 /* Now, say which argument to start evaluating from */
363 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
365 /* Hair for method invocations */
368 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
369 /* First, evaluate the structure into arg2 */
371 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
372 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
373 if (noside == EVAL_SKIP)
376 if (op == STRUCTOP_STRUCT)
378 /* If v is a variable in a register, and the user types
379 v.method (), this will produce an error, because v has
382 A possible way around this would be to allocate a
383 copy of the variable on the stack, copy in the
384 contents, call the function, and copy out the
385 contents. I.e. convert this from call by reference
386 to call by copy-return (or whatever it's called).
387 However, this does not work because it is not the
388 same: the method being called could stash a copy of
389 the address, and then future uses through that address
390 (after the method returns) would be expected to
391 use the variable itself, not some copy of it. */
392 arg2 = evaluate_subexp_for_address (exp, pos, noside);
396 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
398 /* Now, say which argument to start evaluating from */
403 nargs = longest_to_int (exp->elts[pc + 1].longconst);
406 /* Allocate arg vector, including space for the function to be
407 called in argvec[0] and a terminating NULL */
408 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
409 for (; tem <= nargs; tem++)
410 /* Ensure that array expressions are coerced into pointer objects. */
411 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
413 /* signal end of arglist */
416 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
420 char tstr[15], mangle_tstr[15], *ptr, *mangle_ptr;
425 strcpy(tstr, &exp->elts[pc2+2].string);
426 if (!strncmp(tstr, "operator", 8))
429 strcpy(mangle_tstr, "__");
430 mangle_ptr = &mangle_tstr[2];
431 pp = cplus_mangle_opname(ptr, DMGL_ANSI);
433 strcpy(mangle_ptr, pp);
435 strcpy(mangle_ptr, ptr);
437 value_struct_elt (&temp, argvec+1, mangle_tstr,
439 op == STRUCTOP_STRUCT
440 ? "structure" : "structure pointer");
443 pp = cplus_mangle_opname(ptr, DMGL_NO_OPTS);
445 strcpy(mangle_ptr, pp);
447 strcpy(mangle_ptr, ptr);
448 strcpy(tstr, mangle_tstr);
455 value_struct_elt (&temp, argvec+1, tstr,
457 op == STRUCTOP_STRUCT
458 ? "structure" : "structure pointer");
460 arg2 = value_from_longest (lookup_pointer_type(VALUE_TYPE (temp)),
461 VALUE_ADDRESS (temp)+VALUE_OFFSET (temp));
466 argvec[1] = argvec[0];
471 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
477 if (noside == EVAL_SKIP)
479 if (noside == EVAL_AVOID_SIDE_EFFECTS)
481 /* If the return type doesn't look like a function type, call an
482 error. This can happen if somebody tries to turn a variable into
483 a function call. This is here because people often want to
484 call, eg, strcmp, which gdb doesn't know is a function. If
485 gdb isn't asked for it's opinion (ie. through "whatis"),
486 it won't offer it. */
489 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
492 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
494 error ("Expression of type other than \"Function returning ...\" used as function");
496 return call_function_by_hand (argvec[0], nargs, argvec + 1);
498 case STRUCTOP_STRUCT:
499 tem = longest_to_int (exp->elts[pc + 1].longconst);
500 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
501 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
502 if (noside == EVAL_SKIP)
504 if (noside == EVAL_AVOID_SIDE_EFFECTS)
505 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
506 &exp->elts[pc + 2].string,
512 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
513 (int *) 0, "structure");
517 tem = longest_to_int (exp->elts[pc + 1].longconst);
518 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
519 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
520 if (noside == EVAL_SKIP)
522 if (noside == EVAL_AVOID_SIDE_EFFECTS)
523 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
524 &exp->elts[pc + 2].string,
530 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
531 (int *) 0, "structure pointer");
534 case STRUCTOP_MEMBER:
535 arg1 = evaluate_subexp_for_address (exp, pos, noside);
536 goto handle_pointer_to_member;
538 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
539 handle_pointer_to_member:
540 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
541 if (noside == EVAL_SKIP)
543 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR)
544 goto bad_pointer_to_member;
545 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
546 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
547 error ("not implemented: pointer-to-method in pointer-to-member construct");
548 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
549 goto bad_pointer_to_member;
550 /* Now, convert these values to an address. */
551 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
553 arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
554 value_as_long (arg1) + value_as_long (arg2));
555 return value_ind (arg3);
556 bad_pointer_to_member:
557 error("non-pointer-to-member value used in pointer-to-member construct");
560 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
561 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
562 if (noside == EVAL_SKIP)
564 if (binop_user_defined_p (op, arg1, arg2))
565 return value_x_binop (arg1, arg2, op, OP_NULL);
567 return value_concat (arg1, arg2);
570 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
571 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
572 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
574 if (binop_user_defined_p (op, arg1, arg2))
575 return value_x_binop (arg1, arg2, op, OP_NULL);
577 return value_assign (arg1, arg2);
579 case BINOP_ASSIGN_MODIFY:
581 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
582 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
583 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
585 op = exp->elts[pc + 1].opcode;
586 if (binop_user_defined_p (op, arg1, arg2))
587 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
588 else if (op == BINOP_ADD)
589 arg2 = value_add (arg1, arg2);
590 else if (op == BINOP_SUB)
591 arg2 = value_sub (arg1, arg2);
593 arg2 = value_binop (arg1, arg2, op);
594 return value_assign (arg1, arg2);
597 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
598 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
599 if (noside == EVAL_SKIP)
601 if (binop_user_defined_p (op, arg1, arg2))
602 return value_x_binop (arg1, arg2, op, OP_NULL);
604 return value_add (arg1, arg2);
607 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
608 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
609 if (noside == EVAL_SKIP)
611 if (binop_user_defined_p (op, arg1, arg2))
612 return value_x_binop (arg1, arg2, op, OP_NULL);
614 return value_sub (arg1, arg2);
622 case BINOP_BITWISE_AND:
623 case BINOP_BITWISE_IOR:
624 case BINOP_BITWISE_XOR:
625 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
626 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
627 if (noside == EVAL_SKIP)
629 if (binop_user_defined_p (op, arg1, arg2))
630 return value_x_binop (arg1, arg2, op, OP_NULL);
632 if (noside == EVAL_AVOID_SIDE_EFFECTS
633 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
634 return value_zero (VALUE_TYPE (arg1), not_lval);
636 return value_binop (arg1, arg2, op);
638 case BINOP_SUBSCRIPT:
639 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
640 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
641 if (noside == EVAL_SKIP)
643 if (noside == EVAL_AVOID_SIDE_EFFECTS)
645 /* If the user attempts to subscript something that has no target
646 type (like a plain int variable for example), then report this
649 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
651 return value_zero (type, VALUE_LVAL (arg1));
653 error ("cannot subscript something of type `%s'",
654 TYPE_NAME (VALUE_TYPE (arg1)));
657 if (binop_user_defined_p (op, arg1, arg2))
658 return value_x_binop (arg1, arg2, op, OP_NULL);
660 return value_subscript (arg1, arg2);
662 case MULTI_SUBSCRIPT:
664 nargs = longest_to_int (exp->elts[pc + 1].longconst);
665 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
668 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
669 /* FIXME: EVAL_SKIP handling may not be correct. */
670 if (noside == EVAL_SKIP)
681 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
682 if (noside == EVAL_AVOID_SIDE_EFFECTS)
684 /* If the user attempts to subscript something that has no target
685 type (like a plain int variable for example), then report this
688 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
691 arg1 = value_zero (type, VALUE_LVAL (arg1));
697 error ("cannot subscript something of type `%s'",
698 TYPE_NAME (VALUE_TYPE (arg1)));
702 if (binop_user_defined_p (op, arg1, arg2))
704 arg1 = value_x_binop (arg1, arg2, op, OP_NULL);
708 arg1 = value_subscript (arg1, arg2);
713 case BINOP_LOGICAL_AND:
714 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
715 if (noside == EVAL_SKIP)
717 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
722 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
725 if (binop_user_defined_p (op, arg1, arg2))
727 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
728 return value_x_binop (arg1, arg2, op, OP_NULL);
732 tem = value_logical_not (arg1);
733 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
734 (tem ? EVAL_SKIP : noside));
735 return value_from_longest (builtin_type_int,
736 (LONGEST) (!tem && !value_logical_not (arg2)));
739 case BINOP_LOGICAL_OR:
740 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
741 if (noside == EVAL_SKIP)
743 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
748 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
751 if (binop_user_defined_p (op, arg1, arg2))
753 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
754 return value_x_binop (arg1, arg2, op, OP_NULL);
758 tem = value_logical_not (arg1);
759 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
760 (!tem ? EVAL_SKIP : noside));
761 return value_from_longest (builtin_type_int,
762 (LONGEST) (!tem || !value_logical_not (arg2)));
766 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
767 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
768 if (noside == EVAL_SKIP)
770 if (binop_user_defined_p (op, arg1, arg2))
772 return value_x_binop (arg1, arg2, op, OP_NULL);
776 tem = value_equal (arg1, arg2);
777 return value_from_longest (builtin_type_int, (LONGEST) tem);
781 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
782 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
783 if (noside == EVAL_SKIP)
785 if (binop_user_defined_p (op, arg1, arg2))
787 return value_x_binop (arg1, arg2, op, OP_NULL);
791 tem = value_equal (arg1, arg2);
792 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
796 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
797 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
798 if (noside == EVAL_SKIP)
800 if (binop_user_defined_p (op, arg1, arg2))
802 return value_x_binop (arg1, arg2, op, OP_NULL);
806 tem = value_less (arg1, arg2);
807 return value_from_longest (builtin_type_int, (LONGEST) tem);
811 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
812 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
813 if (noside == EVAL_SKIP)
815 if (binop_user_defined_p (op, arg1, arg2))
817 return value_x_binop (arg1, arg2, op, OP_NULL);
821 tem = value_less (arg2, arg1);
822 return value_from_longest (builtin_type_int, (LONGEST) tem);
826 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
827 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
828 if (noside == EVAL_SKIP)
830 if (binop_user_defined_p (op, arg1, arg2))
832 return value_x_binop (arg1, arg2, op, OP_NULL);
836 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
837 return value_from_longest (builtin_type_int, (LONGEST) tem);
841 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
842 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
843 if (noside == EVAL_SKIP)
845 if (binop_user_defined_p (op, arg1, arg2))
847 return value_x_binop (arg1, arg2, op, OP_NULL);
851 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
852 return value_from_longest (builtin_type_int, (LONGEST) tem);
856 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
857 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
858 if (noside == EVAL_SKIP)
860 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
861 error ("Non-integral right operand for \"@\" operator.");
862 if (noside == EVAL_AVOID_SIDE_EFFECTS)
863 return allocate_repeat_value (VALUE_TYPE (arg1),
864 longest_to_int (value_as_long (arg2)));
866 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
869 evaluate_subexp (NULL_TYPE, exp, pos, noside);
870 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
873 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
874 if (noside == EVAL_SKIP)
876 if (unop_user_defined_p (op, arg1))
877 return value_x_unop (arg1, op);
879 return value_neg (arg1);
881 case UNOP_COMPLEMENT:
882 /* C++: check for and handle destructor names. */
883 op = exp->elts[*pos].opcode;
885 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
886 if (noside == EVAL_SKIP)
888 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
889 return value_x_unop (arg1, UNOP_COMPLEMENT);
891 return value_complement (arg1);
893 case UNOP_LOGICAL_NOT:
894 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
895 if (noside == EVAL_SKIP)
897 if (unop_user_defined_p (op, arg1))
898 return value_x_unop (arg1, op);
900 return value_from_longest (builtin_type_int,
901 (LONGEST) value_logical_not (arg1));
904 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
905 expect_type = TYPE_TARGET_TYPE (expect_type);
906 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
907 if (noside == EVAL_SKIP)
909 if (noside == EVAL_AVOID_SIDE_EFFECTS)
911 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
912 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
913 /* In C you can dereference an array to get the 1st elt. */
914 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
916 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
918 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
919 /* GDB allows dereferencing an int. */
920 return value_zero (builtin_type_int, lval_memory);
922 error ("Attempt to take contents of a non-pointer value.");
924 return value_ind (arg1);
927 /* C++: check for and handle pointer to members. */
929 op = exp->elts[*pos].opcode;
931 if (noside == EVAL_SKIP)
935 int temm = longest_to_int (exp->elts[pc+3].longconst);
936 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
939 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
943 return evaluate_subexp_for_address (exp, pos, noside);
946 if (noside == EVAL_SKIP)
948 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
951 return evaluate_subexp_for_sizeof (exp, pos);
955 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
956 if (noside == EVAL_SKIP)
958 return value_cast (exp->elts[pc + 1].type, arg1);
962 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
963 if (noside == EVAL_SKIP)
965 if (noside == EVAL_AVOID_SIDE_EFFECTS)
966 return value_zero (exp->elts[pc + 1].type, lval_memory);
968 return value_at_lazy (exp->elts[pc + 1].type,
969 value_as_pointer (arg1));
971 case UNOP_PREINCREMENT:
972 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
973 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
975 else if (unop_user_defined_p (op, arg1))
977 return value_x_unop (arg1, op);
981 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
983 return value_assign (arg1, arg2);
986 case UNOP_PREDECREMENT:
987 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
988 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
990 else if (unop_user_defined_p (op, arg1))
992 return value_x_unop (arg1, op);
996 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
998 return value_assign (arg1, arg2);
1001 case UNOP_POSTINCREMENT:
1002 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1003 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1005 else if (unop_user_defined_p (op, arg1))
1007 return value_x_unop (arg1, op);
1011 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
1013 value_assign (arg1, arg2);
1017 case UNOP_POSTDECREMENT:
1018 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
1019 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1021 else if (unop_user_defined_p (op, arg1))
1023 return value_x_unop (arg1, op);
1027 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
1029 value_assign (arg1, arg2);
1035 return value_of_this (1);
1038 error ("Attempt to use a type name as an expression");
1041 /* Removing this case and compiling with gcc -Wall reveals that
1042 a lot of cases are hitting this case. Some of these should
1043 probably be removed from expression.h (e.g. do we need a BINOP_SCOPE
1044 and an OP_SCOPE?); others are legitimate expressions which are
1045 (apparently) not fully implemented.
1047 If there are any cases landing here which mean a user error,
1048 then they should be separate cases, with more descriptive
1052 GDB does not (yet) know how to evaluated that kind of expression");
1056 return value_from_longest (builtin_type_long, (LONGEST) 1);
1059 /* Evaluate a subexpression of EXP, at index *POS,
1060 and return the address of that subexpression.
1061 Advance *POS over the subexpression.
1062 If the subexpression isn't an lvalue, get an error.
1063 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1064 then only the type of the result need be correct. */
1067 evaluate_subexp_for_address (exp, pos, noside)
1068 register struct expression *exp;
1077 op = exp->elts[pc].opcode;
1083 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1087 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1088 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1091 var = exp->elts[pc + 2].symbol;
1093 /* C++: The "address" of a reference should yield the address
1094 * of the object pointed to. Let value_addr() deal with it. */
1095 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
1099 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1102 lookup_pointer_type (SYMBOL_TYPE (var));
1103 enum address_class sym_class = SYMBOL_CLASS (var);
1105 if (sym_class == LOC_CONST
1106 || sym_class == LOC_CONST_BYTES
1107 || sym_class == LOC_REGISTER
1108 || sym_class == LOC_REGPARM)
1109 error ("Attempt to take address of register or constant.");
1112 value_zero (type, not_lval);
1118 block_innermost_frame (exp->elts[pc + 1].block));
1122 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1124 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1125 if (VALUE_LVAL (x) == lval_memory)
1126 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
1129 error ("Attempt to take address of non-lval");
1131 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1135 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1136 When used in contexts where arrays will be coerced anyway, this is
1137 equivalent to `evaluate_subexp' but much faster because it avoids
1138 actually fetching array contents (perhaps obsolete now that we have
1141 Note that we currently only do the coercion for C expressions, where
1142 arrays are zero based and the coercion is correct. For other languages,
1143 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1144 to decide if coercion is appropriate.
1149 evaluate_subexp_with_coercion (exp, pos, noside)
1150 register struct expression *exp;
1154 register enum exp_opcode op;
1160 op = exp->elts[pc].opcode;
1165 var = exp->elts[pc + 2].symbol;
1166 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY
1167 && CAST_IS_CONVERSION)
1172 (var, block_innermost_frame (exp->elts[pc + 1].block));
1173 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
1179 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1183 /* Evaluate a subexpression of EXP, at index *POS,
1184 and return a value for the size of that subexpression.
1185 Advance *POS over the subexpression. */
1188 evaluate_subexp_for_sizeof (exp, pos)
1189 register struct expression *exp;
1197 op = exp->elts[pc].opcode;
1201 /* This case is handled specially
1202 so that we avoid creating a value for the result type.
1203 If the result type is very big, it's desirable not to
1204 create a value unnecessarily. */
1207 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1208 return value_from_longest (builtin_type_int, (LONGEST)
1209 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1213 return value_from_longest (builtin_type_int,
1214 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1221 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 2].symbol)));
1224 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1225 return value_from_longest (builtin_type_int,
1226 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1230 /* Parse a type expression in the string [P..P+LENGTH). */
1233 parse_and_eval_type (p, length)
1237 char *tmp = (char *)alloca (length + 4);
1238 struct expression *expr;
1240 memcpy (tmp+1, p, length);
1241 tmp[length+1] = ')';
1242 tmp[length+2] = '0';
1243 tmp[length+3] = '\0';
1244 expr = parse_expression (tmp);
1245 if (expr->elts[0].opcode != UNOP_CAST)
1246 error ("Internal error in eval_type.");
1247 return expr->elts[1].type;