Oh GACK! src-clean doesn't quite work that easily since cleandist rebuilds the
[unix-history] / gnu / usr.bin / gdb / eval.c
CommitLineData
04497f0b
NW
1/*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 * Modified 1990 by Van Jacobson at Lawrence Berkeley Laboratory.
7 */
8
9#ifndef lint
10static char sccsid[] = "@(#)eval.c 6.3 (Berkeley) 5/8/91";
11#endif /* not lint */
12
13/* Evaluate expressions for GDB.
14 Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
15
16This file is part of GDB.
17
18GDB is free software; you can redistribute it and/or modify
19it under the terms of the GNU General Public License as published by
20the Free Software Foundation; either version 1, or (at your option)
21any later version.
22
23GDB is distributed in the hope that it will be useful,
24but WITHOUT ANY WARRANTY; without even the implied warranty of
25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26GNU General Public License for more details.
27
28You should have received a copy of the GNU General Public License
29along with GDB; see the file COPYING. If not, write to
30the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
31
32#include "defs.h"
33#include "param.h"
34#include "symtab.h"
35#include "value.h"
36#include "expression.h"
37
38\f
39/* Parse the string EXP as a C expression, evaluate it,
40 and return the result as a number. */
41
42CORE_ADDR
43parse_and_eval_address (exp)
44 char *exp;
45{
46 struct expression *expr = parse_c_expression (exp);
47 register CORE_ADDR addr;
48 register struct cleanup *old_chain
49 = make_cleanup (free_current_contents, &expr);
50
51 addr = (CORE_ADDR) value_as_long (evaluate_expression (expr));
52 do_cleanups (old_chain);
53 return addr;
54}
55
56/* Like parse_and_eval_address but takes a pointer to a char * variable
57 and advanced that variable across the characters parsed. */
58
59CORE_ADDR
60parse_and_eval_address_1 (expptr)
61 char **expptr;
62{
63 struct expression *expr = parse_c_1 (expptr, 0, 0);
64 register CORE_ADDR addr;
65 register struct cleanup *old_chain
66 = make_cleanup (free_current_contents, &expr);
67
68 addr = value_as_long (evaluate_expression (expr));
69 do_cleanups (old_chain);
70 return addr;
71}
72
73value
74parse_and_eval (exp)
75 char *exp;
76{
77 struct expression *expr = parse_c_expression (exp);
78 register value val;
79 register struct cleanup *old_chain
80 = make_cleanup (free_current_contents, &expr);
81
82 val = evaluate_expression (expr);
83 do_cleanups (old_chain);
84 return val;
85}
86
87/* Parse up to a comma (or to a closeparen)
88 in the string EXPP as an expression, evaluate it, and return the value.
89 EXPP is advanced to point to the comma. */
90
91value
92parse_to_comma_and_eval (expp)
93 char **expp;
94{
95 struct expression *expr = parse_c_1 (expp, 0, 1);
96 register value val;
97 register struct cleanup *old_chain
98 = make_cleanup (free_current_contents, &expr);
99
100 val = evaluate_expression (expr);
101 do_cleanups (old_chain);
102 return val;
103}
104\f
105/* Evaluate an expression in internal prefix form
106 such as is constructed by expread.y.
107
108 See expression.h for info on the format of an expression. */
109
110static value evaluate_subexp ();
111static value evaluate_subexp_for_address ();
112static value evaluate_subexp_for_sizeof ();
113static value evaluate_subexp_with_coercion ();
114
115/* return true if 'var' has an address in inferior's memory. */
116static int
117value_has_lval(var)
118 register struct symbol *var;
119{
120 switch (SYMBOL_CLASS(var))
121 {
122 case LOC_STATIC:
123 case LOC_LABEL:
124 case LOC_ARG:
125 case LOC_REF_ARG:
126 case LOC_LOCAL:
127 case LOC_BLOCK:
128 return (1);
129 }
130 return (0);
131}
132
133/* Values of NOSIDE argument to eval_subexp. */
134enum noside
135{ EVAL_NORMAL,
136 EVAL_SKIP, /* Only effect is to increment pos. */
137 EVAL_AVOID_SIDE_EFFECTS, /* Don't modify any variables or
138 call any functions. The value
139 returned will have the correct
140 type, and will have an
141 approximately correct lvalue
142 type (inaccuracy: anything that is
143 listed as being in a register in
144 the function in which it was
145 declared will be lval_register). */
146};
147
148value
149evaluate_expression (exp)
150 struct expression *exp;
151{
152 int pc = 0;
153 return evaluate_subexp (0, exp, &pc, EVAL_NORMAL);
154}
155
156/* Evaluate an expression, avoiding all memory references
157 and getting a value whose type alone is correct. */
158
159value
160evaluate_type (exp)
161 struct expression *exp;
162{
163 int pc = 0;
164 return evaluate_subexp (0, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
165}
166
167static value
168evaluate_subexp (expect_type, exp, pos, noside)
169 struct type *expect_type;
170 register struct expression *exp;
171 register int *pos;
172 enum noside noside;
173{
174 enum exp_opcode op;
175 int tem;
176 register int pc, pc2, oldpos;
177 register value arg1, arg2, arg3;
178 int nargs;
179 value *argvec;
180
181 pc = (*pos)++;
182 op = exp->elts[pc].opcode;
183
184 switch (op)
185 {
186 case OP_SCOPE:
187 tem = strlen (&exp->elts[pc + 2].string);
188 (*pos) += 3 + ((tem + sizeof (union exp_element))
189 / sizeof (union exp_element));
190 return value_static_field (exp->elts[pc + 1].type,
191 &exp->elts[pc + 2].string, -1);
192
193 case OP_LONG:
194 (*pos) += 3;
195 return value_from_long (exp->elts[pc + 1].type,
196 exp->elts[pc + 2].longconst);
197
198 case OP_DOUBLE:
199 (*pos) += 3;
200 return value_from_double (exp->elts[pc + 1].type,
201 exp->elts[pc + 2].doubleconst);
202
203 case OP_VAR_VALUE:
204 (*pos) += 2;
205 if (noside == EVAL_SKIP)
206 goto nosideret;
207 if (noside == EVAL_AVOID_SIDE_EFFECTS)
208 {
209 struct symbol * sym = exp->elts[pc + 1].symbol;
210 enum lval_type lv;
211
212 switch (SYMBOL_CLASS (sym))
213 {
214 case LOC_CONST:
215 case LOC_LABEL:
216 case LOC_CONST_BYTES:
217 lv = not_lval;
218 case LOC_REGISTER:
219 case LOC_REGPARM:
220 lv = lval_register;
221 default:
222 lv = lval_memory;
223 }
224
225 return value_zero (SYMBOL_TYPE (sym), lv);
226 }
227 else
228 return value_of_variable (exp->elts[pc + 1].symbol);
229
230 case OP_LAST:
231 (*pos) += 2;
232 return access_value_history ((int) exp->elts[pc + 1].longconst);
233
234 case OP_REGISTER:
235 (*pos) += 2;
236 return value_of_register ((int) exp->elts[pc + 1].longconst);
237
238 case OP_INTERNALVAR:
239 (*pos) += 2;
240 return value_of_internalvar (exp->elts[pc + 1].internalvar);
241
242 case OP_STRING:
243 tem = strlen (&exp->elts[pc + 1].string);
244 (*pos) += 2 + ((tem + sizeof (union exp_element))
245 / sizeof (union exp_element));
246 if (noside == EVAL_SKIP)
247 goto nosideret;
248 return value_string (&exp->elts[pc + 1].string, tem);
249
250 case TERNOP_COND:
251 /* Skip third and second args to evaluate the first one. */
252 arg1 = evaluate_subexp (0, exp, pos, noside);
253 if (value_zerop (arg1))
254 {
255 evaluate_subexp (0, exp, pos, EVAL_SKIP);
256 return evaluate_subexp (0, exp, pos, noside);
257 }
258 else
259 {
260 arg2 = evaluate_subexp (0, exp, pos, noside);
261 evaluate_subexp (0, exp, pos, EVAL_SKIP);
262 return arg2;
263 }
264
265 case OP_FUNCALL:
266 (*pos) += 2;
267 op = exp->elts[*pos].opcode;
268 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
269 {
270 int fnptr;
271 int tem2;
272
273 nargs = (int) exp->elts[pc + 1].longconst + 1;
274 /* First, evaluate the structure into arg2 */
275 pc2 = (*pos)++;
276
277 if (noside == EVAL_SKIP)
278 goto nosideret;
279
280 if (op == STRUCTOP_MEMBER)
281 {
282 arg2 = evaluate_subexp_for_address (exp, pos, noside);
283 }
284 else
285 {
286 arg2 = evaluate_subexp (0, exp, pos, noside);
287 }
288
289 /* If the function is a virtual function, then the
290 aggregate value (providing the structure) plays
291 its part by providing the vtable. Otherwise,
292 it is just along for the ride: call the function
293 directly. */
294
295 arg1 = evaluate_subexp (0, exp, pos, noside);
296
297 fnptr = (int) value_as_long (arg1);
298 if (fnptr < 128)
299 {
300 struct type *basetype;
301 int i, j;
302 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
303 basetype = TYPE_VPTR_BASETYPE (basetype);
304 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
305 {
306 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
307 /* If one is virtual, then all are virtual. */
308 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
309 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
310 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnptr)
311 {
312 value vtbl;
313 value base = value_ind (arg2);
314 struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
315
316 if (TYPE_VPTR_FIELDNO (basetype) < 0)
317 TYPE_VPTR_FIELDNO (basetype)
318 = fill_in_vptr_fieldno (basetype);
319
320 VALUE_TYPE (base) = basetype;
321 vtbl = value_field (base, TYPE_VPTR_FIELDNO (basetype));
322 VALUE_TYPE (vtbl) = lookup_pointer_type (fntype);
323 VALUE_TYPE (arg1) = builtin_type_int;
324 arg1 = value_subscript (vtbl, arg1);
325 VALUE_TYPE (arg1) = fntype;
326 goto got_it;
327 }
328 }
329 if (i < 0)
330 error ("virtual function at index %d not found", fnptr);
331 }
332 else
333 {
334 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
335 }
336 got_it:
337
338 /* Now, say which argument to start evaluating from */
339 tem = 2;
340 }
341 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
342 {
343 /* Hair for method invocations */
344 int tem2;
345
346 nargs = (int) exp->elts[pc + 1].longconst + 1;
347 /* First, evaluate the structure into arg2 */
348 pc2 = (*pos)++;
349 tem2 = strlen (&exp->elts[pc2 + 1].string);
350 *pos += 2 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
351 if (noside == EVAL_SKIP)
352 goto nosideret;
353
354 if (op == STRUCTOP_STRUCT)
355 {
356 arg2 = evaluate_subexp_for_address (exp, pos, noside);
357 }
358 else
359 {
360 arg2 = evaluate_subexp (0, exp, pos, noside);
361 }
362 /* Now, say which argument to start evaluating from */
363 tem = 2;
364 }
365 else
366 {
367 nargs = (int) exp->elts[pc + 1].longconst;
368 tem = 0;
369 }
370 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
371 for (; tem <= nargs; tem++)
372 /* Ensure that array expressions are coerced into pointer objects. */
373 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
374
375 /* signal end of arglist */
376 argvec[tem] = 0;
377
378 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
379 {
380 int static_memfuncp;
381
382 argvec[1] = arg2;
383 argvec[0] =
384 value_struct_elt (arg2, argvec+1, &exp->elts[pc2 + 1].string,
385 &static_memfuncp,
386 op == STRUCTOP_STRUCT
387 ? "structure" : "structure pointer");
388 if (static_memfuncp)
389 {
390 argvec[1] = argvec[0];
391 nargs--;
392 argvec++;
393 }
394 }
395 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
396 {
397 argvec[1] = arg2;
398 argvec[0] = arg1;
399 }
400
401 if (noside == EVAL_SKIP)
402 goto nosideret;
403 if (noside == EVAL_AVOID_SIDE_EFFECTS)
404 {
405 /* If the return type doesn't look like a function type, call an
406 error. This can happen if somebody tries to turn a variable into
407 a function call. This is here because people often want to
408 call, eg, strcmp, which gdb doesn't know is a function. If
409 gdb isn't asked for it's opinion (ie. through "whatis"),
410 it won't offer it. */
411
412 struct type *ftype =
413 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
414
415 if (ftype)
416 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
417 else
418 error ("Expression of type other than \"Function returning ...\" used as function");
419 }
420 return call_function (argvec[0], nargs, argvec + 1);
421
422 case STRUCTOP_STRUCT:
423 tem = strlen (&exp->elts[pc + 1].string);
424 (*pos) += 2 + ((tem + sizeof (union exp_element))
425 / sizeof (union exp_element));
426
427 /* Try to convert "foo.bar" into "(&foo)->bar" so we won't copy
428 * the entire contents of a large struct just to extract one
429 * value from it. */
430 if (noside == EVAL_NORMAL && exp->elts[*pos].opcode == OP_VAR_VALUE
431 && value_has_lval(exp->elts[*pos + 1].symbol))
432 arg1 = evaluate_subexp_for_address(exp, pos, noside);
433 else
434 arg1 = evaluate_subexp (0, exp, pos, noside);
435
436 if (noside == EVAL_SKIP)
437 goto nosideret;
438 if (noside == EVAL_AVOID_SIDE_EFFECTS)
439 {
440 register struct type *type = VALUE_TYPE (arg1);
441 if (TYPE_CODE (type) == TYPE_CODE_PTR)
442 type = TYPE_TARGET_TYPE (type);
443 return value_zero (lookup_struct_elt_type (type,
444 &exp->elts[pc + 1].string),
445 lval_memory);
446 }
447 else
448 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string, 0,
449 "structure");
450
451 case STRUCTOP_PTR:
452 tem = strlen (&exp->elts[pc + 1].string);
453 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
454 arg1 = evaluate_subexp (0, exp, pos, noside);
455 if (noside == EVAL_SKIP)
456 goto nosideret;
457 if (noside == EVAL_AVOID_SIDE_EFFECTS)
458 return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
459 (VALUE_TYPE (arg1)),
460 &exp->elts[pc + 1].string),
461 lval_memory);
462 else
463 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string, 0,
464 "structure pointer");
465
466 case STRUCTOP_MEMBER:
467 arg1 = evaluate_subexp_for_address (exp, pos, noside);
468 arg2 = evaluate_subexp (0, exp, pos, noside);
469 if (noside == EVAL_SKIP)
470 goto nosideret;
471 /* Now, convert these values to an address. */
472 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
473 || ((TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
474 != TYPE_CODE_MEMBER)
475 && (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)))
476 != TYPE_CODE_METHOD)))
477 error ("non-pointer-to-member value used in pointer-to-member construct");
478 arg3 = value_from_long (builtin_type_long,
479 value_as_long (arg1) + value_as_long (arg2));
480 VALUE_TYPE (arg3) =
481 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
482 return value_ind (arg3);
483
484 case STRUCTOP_MPTR:
485 arg1 = evaluate_subexp (0, exp, pos, noside);
486 arg2 = evaluate_subexp (0, exp, pos, noside);
487 if (noside == EVAL_SKIP)
488 goto nosideret;
489 /* Now, convert these values to an address. */
490 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR
491 || (TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_MEMBER
492 && TYPE_CODE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))) != TYPE_CODE_METHOD))
493 error ("non-pointer-to-member value used in pointer-to-member construct");
494 arg3 = value_from_long (builtin_type_long,
495 value_as_long (arg1) + value_as_long (arg2));
496 VALUE_TYPE (arg3) =
497 lookup_pointer_type (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg2))));
498 return value_ind (arg3);
499
500 case BINOP_ASSIGN:
501 arg1 = evaluate_subexp (0, exp, pos, noside);
502 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
503 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
504 return arg1;
505 if (binop_user_defined_p (op, arg1, arg2))
506 return value_x_binop (arg1, arg2, op, 0);
507 else
508 return value_assign (arg1, arg2);
509
510 case BINOP_ASSIGN_MODIFY:
511 (*pos) += 2;
512 arg1 = evaluate_subexp (0, exp, pos, noside);
513 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
514 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
515 return arg1;
516 op = exp->elts[pc + 1].opcode;
517 if (binop_user_defined_p (op, arg1, arg2))
518 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
519 else if (op == BINOP_ADD)
520 arg2 = value_add (arg1, arg2);
521 else if (op == BINOP_SUB)
522 arg2 = value_sub (arg1, arg2);
523 else
524 arg2 = value_binop (arg1, arg2, op);
525 return value_assign (arg1, arg2);
526
527 case BINOP_ADD:
528 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
529 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
530 if (noside == EVAL_SKIP)
531 goto nosideret;
532 if (binop_user_defined_p (op, arg1, arg2))
533 return value_x_binop (arg1, arg2, op, 0);
534 else
535 return value_add (arg1, arg2);
536
537 case BINOP_SUB:
538 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
539 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
540 if (noside == EVAL_SKIP)
541 goto nosideret;
542 if (binop_user_defined_p (op, arg1, arg2))
543 return value_x_binop (arg1, arg2, op, 0);
544 else
545 return value_sub (arg1, arg2);
546
547 case BINOP_MUL:
548 case BINOP_DIV:
549 case BINOP_REM:
550 case BINOP_LSH:
551 case BINOP_RSH:
552 case BINOP_LOGAND:
553 case BINOP_LOGIOR:
554 case BINOP_LOGXOR:
555 arg1 = evaluate_subexp (0, exp, pos, noside);
556 arg2 = evaluate_subexp (0, exp, pos, noside);
557 if (noside == EVAL_SKIP)
558 goto nosideret;
559 if (binop_user_defined_p (op, arg1, arg2))
560 return value_x_binop (arg1, arg2, op, 0);
561 else
562 if (noside == EVAL_AVOID_SIDE_EFFECTS
563 && op == BINOP_DIV)
564 return value_zero (VALUE_TYPE (arg1), not_lval);
565 else
566 return value_binop (arg1, arg2, op);
567
568 case BINOP_SUBSCRIPT:
569 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
570 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
571 if (noside == EVAL_SKIP)
572 goto nosideret;
573 if (noside == EVAL_AVOID_SIDE_EFFECTS)
574 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
575 VALUE_LVAL (arg1));
576
577 if (binop_user_defined_p (op, arg1, arg2))
578 return value_x_binop (arg1, arg2, op, 0);
579 else
580 return value_subscript (arg1, arg2);
581
582 case BINOP_AND:
583 arg1 = evaluate_subexp (0, exp, pos, noside);
584 if (noside == EVAL_SKIP)
585 {
586 arg2 = evaluate_subexp (0, exp, pos, noside);
587 goto nosideret;
588 }
589
590 oldpos = *pos;
591 arg2 = evaluate_subexp (0, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
592 *pos = oldpos;
593
594 if (binop_user_defined_p (op, arg1, arg2))
595 {
596 arg2 = evaluate_subexp (0, exp, pos, noside);
597 return value_x_binop (arg1, arg2, op, 0);
598 }
599 else
600 {
601 tem = value_zerop (arg1);
602 arg2 = evaluate_subexp (0, exp, pos,
603 (tem ? EVAL_SKIP : noside));
604 return value_from_long (builtin_type_int,
605 (LONGEST) (!tem && !value_zerop (arg2)));
606 }
607
608 case BINOP_OR:
609 arg1 = evaluate_subexp (0, exp, pos, noside);
610 if (noside == EVAL_SKIP)
611 {
612 arg2 = evaluate_subexp (0, exp, pos, noside);
613 goto nosideret;
614 }
615
616 oldpos = *pos;
617 arg2 = evaluate_subexp (0, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
618 *pos = oldpos;
619
620 if (binop_user_defined_p (op, arg1, arg2))
621 {
622 arg2 = evaluate_subexp (0, exp, pos, noside);
623 return value_x_binop (arg1, arg2, op, 0);
624 }
625 else
626 {
627 tem = value_zerop (arg1);
628 arg2 = evaluate_subexp (0, exp, pos,
629 (!tem ? EVAL_SKIP : noside));
630 return value_from_long (builtin_type_int,
631 (LONGEST) (!tem || !value_zerop (arg2)));
632 }
633
634 case BINOP_EQUAL:
635 arg1 = evaluate_subexp (0, exp, pos, noside);
636 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
637 if (noside == EVAL_SKIP)
638 goto nosideret;
639 if (binop_user_defined_p (op, arg1, arg2))
640 {
641 return value_x_binop (arg1, arg2, op, 0);
642 }
643 else
644 {
645 tem = value_equal (arg1, arg2);
646 return value_from_long (builtin_type_int, (LONGEST) tem);
647 }
648
649 case BINOP_NOTEQUAL:
650 arg1 = evaluate_subexp (0, exp, pos, noside);
651 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
652 if (noside == EVAL_SKIP)
653 goto nosideret;
654 if (binop_user_defined_p (op, arg1, arg2))
655 {
656 return value_x_binop (arg1, arg2, op, 0);
657 }
658 else
659 {
660 tem = value_equal (arg1, arg2);
661 return value_from_long (builtin_type_int, (LONGEST) ! tem);
662 }
663
664 case BINOP_LESS:
665 arg1 = evaluate_subexp (0, exp, pos, noside);
666 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
667 if (noside == EVAL_SKIP)
668 goto nosideret;
669 if (binop_user_defined_p (op, arg1, arg2))
670 {
671 return value_x_binop (arg1, arg2, op, 0);
672 }
673 else
674 {
675 tem = value_less (arg1, arg2);
676 return value_from_long (builtin_type_int, (LONGEST) tem);
677 }
678
679 case BINOP_GTR:
680 arg1 = evaluate_subexp (0, exp, pos, noside);
681 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
682 if (noside == EVAL_SKIP)
683 goto nosideret;
684 if (binop_user_defined_p (op, arg1, arg2))
685 {
686 return value_x_binop (arg1, arg2, op, 0);
687 }
688 else
689 {
690 tem = value_less (arg2, arg1);
691 return value_from_long (builtin_type_int, (LONGEST) tem);
692 }
693
694 case BINOP_GEQ:
695 arg1 = evaluate_subexp (0, exp, pos, noside);
696 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
697 if (noside == EVAL_SKIP)
698 goto nosideret;
699 if (binop_user_defined_p (op, arg1, arg2))
700 {
701 return value_x_binop (arg1, arg2, op, 0);
702 }
703 else
704 {
705 tem = value_less (arg1, arg2);
706 return value_from_long (builtin_type_int, (LONGEST) ! tem);
707 }
708
709 case BINOP_LEQ:
710 arg1 = evaluate_subexp (0, exp, pos, noside);
711 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
712 if (noside == EVAL_SKIP)
713 goto nosideret;
714 if (binop_user_defined_p (op, arg1, arg2))
715 {
716 return value_x_binop (arg1, arg2, op, 0);
717 }
718 else
719 {
720 tem = value_less (arg2, arg1);
721 return value_from_long (builtin_type_int, (LONGEST) ! tem);
722 }
723
724 case BINOP_REPEAT:
725 arg1 = evaluate_subexp (0, exp, pos, noside);
726 arg2 = evaluate_subexp (0, exp, pos, noside);
727 if (noside == EVAL_SKIP)
728 goto nosideret;
729 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
730 error ("Non-integral right operand for \"@\" operator.");
731 if (noside == EVAL_AVOID_SIDE_EFFECTS)
732 return allocate_repeat_value (VALUE_TYPE (arg1),
733 (int) value_as_long (arg2));
734 else
735 return value_repeat (arg1, (int) value_as_long (arg2));
736
737 case BINOP_COMMA:
738 evaluate_subexp (0, exp, pos, noside);
739 return evaluate_subexp (0, exp, pos, noside);
740
741 case UNOP_NEG:
742 arg1 = evaluate_subexp (0, exp, pos, noside);
743 if (noside == EVAL_SKIP)
744 goto nosideret;
745 if (unop_user_defined_p (op, arg1))
746 return value_x_unop (arg1, op);
747 else
748 return value_neg (arg1);
749
750 case UNOP_LOGNOT:
751 arg1 = evaluate_subexp (0, exp, pos, noside);
752 if (noside == EVAL_SKIP)
753 goto nosideret;
754 if (unop_user_defined_p (op, arg1))
755 return value_x_unop (arg1, op);
756 else
757 return value_lognot (arg1);
758
759 case UNOP_ZEROP:
760 arg1 = evaluate_subexp (0, exp, pos, noside);
761 if (noside == EVAL_SKIP)
762 goto nosideret;
763 if (unop_user_defined_p (op, arg1))
764 return value_x_unop (arg1, op);
765 else
766 return value_from_long (builtin_type_int,
767 (LONGEST) value_zerop (arg1));
768
769 case UNOP_IND:
770 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
771 expect_type = TYPE_TARGET_TYPE (expect_type);
772 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
773 if (noside == EVAL_SKIP)
774 goto nosideret;
775 if (noside == EVAL_AVOID_SIDE_EFFECTS)
776 {
777 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
778 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
779 /* In C you can dereference an array to get the 1st elt. */
780 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
781 )
782 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
783 lval_memory);
784 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
785 /* GDB allows dereferencing an int. */
786 return value_zero (builtin_type_int, lval_memory);
787 else
788 error ("Attempt to take contents of a non-pointer value.");
789 }
790 return value_ind (arg1);
791
792 case UNOP_ADDR:
793 /* C++: check for and handle pointer to members. */
794
795 op = exp->elts[*pos].opcode;
796
797 if (noside == EVAL_SKIP)
798 {
799 if (op == OP_SCOPE)
800 {
801 char *name = &exp->elts[pc+3].string;
802 int tem = strlen (name);
803 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
804 }
805 else
806 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
807 goto nosideret;
808 }
809
810 if (op == OP_SCOPE)
811 {
812 char *name = &exp->elts[pc+3].string;
813 int tem = strlen (name);
814 struct type *domain = exp->elts[pc+2].type;
815 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
816 arg1 = value_struct_elt_for_address (domain, expect_type, name);
817 if (arg1)
818 return arg1;
819 error ("no field `%s' in structure", name);
820 }
821 else
822 return evaluate_subexp_for_address (exp, pos, noside);
823
824 case UNOP_SIZEOF:
825 if (noside == EVAL_SKIP)
826 {
827 evaluate_subexp (0, exp, pos, EVAL_SKIP);
828 goto nosideret;
829 }
830 return evaluate_subexp_for_sizeof (exp, pos);
831
832 case UNOP_CAST:
833 (*pos) += 2;
834 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
835 if (noside == EVAL_SKIP)
836 goto nosideret;
837 return value_cast (exp->elts[pc + 1].type, arg1);
838
839 case UNOP_MEMVAL:
840 (*pos) += 2;
841 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
842 if (noside == EVAL_SKIP)
843 goto nosideret;
844 if (noside == EVAL_AVOID_SIDE_EFFECTS)
845 return value_zero (exp->elts[pc + 1].type, lval_memory);
846 else
847 return value_at (exp->elts[pc + 1].type,
848 (CORE_ADDR) value_as_long (arg1));
849
850 case UNOP_PREINCREMENT:
851 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
852 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
853 return arg1;
854 else if (unop_user_defined_p (op, arg1))
855 {
856 return value_x_unop (arg1, op);
857 }
858 else
859 {
860 arg2 = value_add (arg1, value_from_long (builtin_type_char,
861 (LONGEST) 1));
862 return value_assign (arg1, arg2);
863 }
864
865 case UNOP_PREDECREMENT:
866 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
867 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
868 return arg1;
869 else if (unop_user_defined_p (op, arg1))
870 {
871 return value_x_unop (arg1, op);
872 }
873 else
874 {
875 arg2 = value_sub (arg1, value_from_long (builtin_type_char,
876 (LONGEST) 1));
877 return value_assign (arg1, arg2);
878 }
879
880 case UNOP_POSTINCREMENT:
881 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
882 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
883 return arg1;
884 else if (unop_user_defined_p (op, arg1))
885 {
886 return value_x_unop (arg1, op);
887 }
888 else
889 {
890 arg2 = value_add (arg1, value_from_long (builtin_type_char,
891 (LONGEST) 1));
892 value_assign (arg1, arg2);
893 return arg1;
894 }
895
896 case UNOP_POSTDECREMENT:
897 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
898 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
899 return arg1;
900 else if (unop_user_defined_p (op, arg1))
901 {
902 return value_x_unop (arg1, op);
903 }
904 else
905 {
906 arg2 = value_sub (arg1, value_from_long (builtin_type_char,
907 (LONGEST) 1));
908 value_assign (arg1, arg2);
909 return arg1;
910 }
911
912 case OP_THIS:
913 (*pos) += 1;
914 return value_of_this (1);
915
916 default:
917 error ("internal error: I do not know how to evaluate what you gave me");
918 }
919
920 nosideret:
921 return value_from_long (builtin_type_long, (LONGEST) 1);
922}
923\f
924/* Evaluate a subexpression of EXP, at index *POS,
925 and return the address of that subexpression.
926 Advance *POS over the subexpression.
927 If the subexpression isn't an lvalue, get an error.
928 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
929 then only the type of the result need be correct. */
930
931static value
932evaluate_subexp_for_address (exp, pos, noside)
933 register struct expression *exp;
934 register int *pos;
935 enum noside noside;
936{
937 enum exp_opcode op;
938 register int pc;
939
940 pc = (*pos);
941 op = exp->elts[pc].opcode;
942
943 switch (op)
944 {
945 case UNOP_IND:
946 (*pos)++;
947 return evaluate_subexp (0, exp, pos, noside);
948
949 case UNOP_MEMVAL:
950 (*pos) += 3;
951 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
952 evaluate_subexp (0, exp, pos, noside));
953
954 case OP_VAR_VALUE:
955 (*pos) += 3;
956 if (noside == EVAL_AVOID_SIDE_EFFECTS)
957 {
958 struct type *type =
959 lookup_pointer_type (SYMBOL_TYPE (exp->elts[pc + 1].symbol));
960 enum address_class sym_class =
961 SYMBOL_CLASS (exp->elts[pc + 1].symbol);
962
963 if (sym_class == LOC_CONST
964 || sym_class == LOC_CONST_BYTES
965 || sym_class == LOC_REGISTER
966 || sym_class == LOC_REGPARM)
967 error ("Attempt to take address of register or constant.");
968
969 return
970 value_zero (type, not_lval);
971 }
972 else
973 return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
974
975 default:
976 if (noside == EVAL_AVOID_SIDE_EFFECTS)
977 {
978 value x = evaluate_subexp (0, exp, pos, noside);
979 if (VALUE_LVAL (x) == lval_memory)
980 return value_zero (TYPE_POINTER_TYPE (VALUE_TYPE (x)),
981 not_lval);
982 else
983 error ("Attempt to take address of non-lval");
984 }
985 return value_addr (evaluate_subexp (0, exp, pos, noside));
986 }
987}
988
989/* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
990 When used in contexts where arrays will be coerced anyway,
991 this is equivalent to `evaluate_subexp'
992 but much faster because it avoids actually fetching array contents. */
993
994static value
995evaluate_subexp_with_coercion (exp, pos, noside)
996 register struct expression *exp;
997 register int *pos;
998 enum noside noside;
999{
1000 register enum exp_opcode op;
1001 register int pc;
1002 register value val;
1003
1004 pc = (*pos);
1005 op = exp->elts[pc].opcode;
1006
1007 switch (op)
1008 {
1009 case OP_VAR_VALUE:
1010 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
1011 {
1012 (*pos) += 3;
1013 val = locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
1014 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
1015 val);
1016 }
1017 }
1018
1019 return evaluate_subexp (0, exp, pos, noside);
1020}
1021
1022/* Evaluate a subexpression of EXP, at index *POS,
1023 and return a value for the size of that subexpression.
1024 Advance *POS over the subexpression. */
1025
1026static value
1027evaluate_subexp_for_sizeof (exp, pos)
1028 register struct expression *exp;
1029 register int *pos;
1030{
1031 enum exp_opcode op;
1032 register int pc;
1033 value val;
1034
1035 pc = (*pos);
1036 op = exp->elts[pc].opcode;
1037
1038 switch (op)
1039 {
1040 /* This case is handled specially
1041 so that we avoid creating a value for the result type.
1042 If the result type is very big, it's desirable not to
1043 create a value unnecessarily. */
1044 case UNOP_IND:
1045 (*pos)++;
1046 val = evaluate_subexp (0, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1047 return value_from_long (builtin_type_int, (LONGEST)
1048 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1049
1050 case UNOP_MEMVAL:
1051 (*pos) += 3;
1052 return value_from_long (builtin_type_int,
1053 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1054
1055 case OP_VAR_VALUE:
1056 (*pos) += 3;
1057 return value_from_long (builtin_type_int,
1058 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1059
1060 default:
1061 val = evaluate_subexp (0, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1062 return value_from_long (builtin_type_int,
1063 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1064 }
1065}