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