* Makefile.in (VERSION): Bump to 4.7.4.
[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 = longest_to_int (exp->elts[pc + 2].longconst);
178 (*pos) += 4 + ((tem + sizeof (union exp_element))
179 / sizeof (union exp_element));
180 arg1 = value_struct_elt_for_reference (exp->elts[pc + 1].type,
181 0,
182 exp->elts[pc + 1].type,
183 &exp->elts[pc + 3].string,
184 expect_type);
185 if (arg1 == NULL)
186 error ("There is no field named %s", &exp->elts[pc + 3].string);
187 return arg1;
188
189 case OP_LONG:
190 (*pos) += 3;
191 return value_from_longest (exp->elts[pc + 1].type,
192 exp->elts[pc + 2].longconst);
193
194 case OP_DOUBLE:
195 (*pos) += 3;
196 return value_from_double (exp->elts[pc + 1].type,
197 exp->elts[pc + 2].doubleconst);
198
199 case OP_VAR_VALUE:
200 (*pos) += 2;
201 if (noside == EVAL_SKIP)
202 goto nosideret;
203 if (noside == EVAL_AVOID_SIDE_EFFECTS)
204 {
205 struct symbol * sym = exp->elts[pc + 1].symbol;
206 enum lval_type lv;
207
208 switch (SYMBOL_CLASS (sym))
209 {
210 case LOC_CONST:
211 case LOC_LABEL:
212 case LOC_CONST_BYTES:
213 lv = not_lval;
214 break;
215
216 case LOC_REGISTER:
217 case LOC_REGPARM:
218 lv = lval_register;
219 break;
220
221 default:
222 lv = lval_memory;
223 break;
224 }
225
226 return value_zero (SYMBOL_TYPE (sym), lv);
227 }
228 else
229 return value_of_variable (exp->elts[pc + 1].symbol);
230
231 case OP_LAST:
232 (*pos) += 2;
233 return
234 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
235
236 case OP_REGISTER:
237 (*pos) += 2;
238 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
239
240 /* start-sanitize-chill */
241 case OP_BOOL:
242 (*pos) += 2;
243 return value_from_longest (builtin_type_chill_bool,
244 exp->elts[pc + 1].longconst);
245 /* end-sanitize-chill */
246
247 case OP_INTERNALVAR:
248 (*pos) += 2;
249 return value_of_internalvar (exp->elts[pc + 1].internalvar);
250
251 case OP_STRING:
252 tem = longest_to_int (exp->elts[pc + 1].longconst);
253 (*pos) += 3 + ((tem + sizeof (union exp_element))
254 / sizeof (union exp_element));
255 if (noside == EVAL_SKIP)
256 goto nosideret;
257 return value_string (&exp->elts[pc + 2].string, tem);
258
259 case TERNOP_COND:
260 /* Skip third and second args to evaluate the first one. */
261 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
262 if (value_logical_not (arg1))
263 {
264 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
265 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
266 }
267 else
268 {
269 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
270 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
271 return arg2;
272 }
273
274 case OP_FUNCALL:
275 (*pos) += 2;
276 op = exp->elts[*pos].opcode;
277 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
278 {
279 int fnptr;
280
281 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
282 /* First, evaluate the structure into arg2 */
283 pc2 = (*pos)++;
284
285 if (noside == EVAL_SKIP)
286 goto nosideret;
287
288 if (op == STRUCTOP_MEMBER)
289 {
290 arg2 = evaluate_subexp_for_address (exp, pos, noside);
291 }
292 else
293 {
294 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
295 }
296
297 /* If the function is a virtual function, then the
298 aggregate value (providing the structure) plays
299 its part by providing the vtable. Otherwise,
300 it is just along for the ride: call the function
301 directly. */
302
303 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
304
305 fnptr = longest_to_int (value_as_long (arg1));
306
307 if (METHOD_PTR_IS_VIRTUAL(fnptr))
308 {
309 int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
310 struct type *basetype;
311 struct type *domain_type =
312 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
313 int i, j;
314 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
315 if (domain_type != basetype)
316 arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
317 basetype = TYPE_VPTR_BASETYPE (domain_type);
318 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
319 {
320 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
321 /* If one is virtual, then all are virtual. */
322 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
323 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
324 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
325 {
326 value temp = value_ind (arg2);
327 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
328 arg2 = value_addr (temp);
329 goto got_it;
330 }
331 }
332 if (i < 0)
333 error ("virtual function at index %d not found", fnoffset);
334 }
335 else
336 {
337 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
338 }
339 got_it:
340
341 /* Now, say which argument to start evaluating from */
342 tem = 2;
343 }
344 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
345 {
346 /* Hair for method invocations */
347 int tem2;
348
349 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
350 /* First, evaluate the structure into arg2 */
351 pc2 = (*pos)++;
352 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
353 *pos += 3 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
354 if (noside == EVAL_SKIP)
355 goto nosideret;
356
357 if (op == STRUCTOP_STRUCT)
358 {
359 arg2 = evaluate_subexp_for_address (exp, pos, noside);
360 }
361 else
362 {
363 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
364 }
365 /* Now, say which argument to start evaluating from */
366 tem = 2;
367 }
368 else
369 {
370 nargs = longest_to_int (exp->elts[pc + 1].longconst);
371 tem = 0;
372 }
373 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
374 for (; tem <= nargs; tem++)
375 /* Ensure that array expressions are coerced into pointer objects. */
376 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
377
378 /* signal end of arglist */
379 argvec[tem] = 0;
380
381 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
382 {
383 int static_memfuncp;
384 value temp = arg2;
385
386 argvec[1] = arg2;
387 argvec[0] =
388 value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 2].string,
389 &static_memfuncp,
390 op == STRUCTOP_STRUCT
391 ? "structure" : "structure pointer");
392 if (VALUE_OFFSET (temp))
393 {
394 arg2 = value_from_longest (lookup_pointer_type (VALUE_TYPE (temp)),
395 value_as_long (arg2)+VALUE_OFFSET (temp));
396 argvec[1] = arg2;
397 }
398 if (static_memfuncp)
399 {
400 argvec[1] = argvec[0];
401 nargs--;
402 argvec++;
403 }
404 }
405 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
406 {
407 argvec[1] = arg2;
408 argvec[0] = arg1;
409 }
410
411 if (noside == EVAL_SKIP)
412 goto nosideret;
413 if (noside == EVAL_AVOID_SIDE_EFFECTS)
414 {
415 /* If the return type doesn't look like a function type, call an
416 error. This can happen if somebody tries to turn a variable into
417 a function call. This is here because people often want to
418 call, eg, strcmp, which gdb doesn't know is a function. If
419 gdb isn't asked for it's opinion (ie. through "whatis"),
420 it won't offer it. */
421
422 struct type *ftype =
423 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
424
425 if (ftype)
426 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
427 else
428 error ("Expression of type other than \"Function returning ...\" used as function");
429 }
430 return call_function_by_hand (argvec[0], nargs, argvec + 1);
431
432 case STRUCTOP_STRUCT:
433 tem = longest_to_int (exp->elts[pc + 1].longconst);
434 (*pos) += 3 + ((tem + sizeof (union exp_element))
435 / sizeof (union exp_element));
436 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
437 if (noside == EVAL_SKIP)
438 goto nosideret;
439 if (noside == EVAL_AVOID_SIDE_EFFECTS)
440 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
441 &exp->elts[pc + 2].string,
442 0),
443 lval_memory);
444 else
445 {
446 value temp = arg1;
447 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
448 (int *) 0, "structure");
449 }
450
451 case STRUCTOP_PTR:
452 tem = longest_to_int (exp->elts[pc + 1].longconst);
453 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
454 arg1 = evaluate_subexp (NULL_TYPE, 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 + 2].string,
461 0),
462 lval_memory);
463 else
464 {
465 value temp = arg1;
466 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
467 (int *) 0, "structure pointer");
468 }
469
470 case STRUCTOP_MEMBER:
471 arg1 = evaluate_subexp_for_address (exp, pos, noside);
472 goto handle_pointer_to_member;
473 case STRUCTOP_MPTR:
474 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
475 handle_pointer_to_member:
476 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
477 if (noside == EVAL_SKIP)
478 goto nosideret;
479 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR)
480 goto bad_pointer_to_member;
481 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
482 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
483 error ("not implemented: pointer-to-method in pointer-to-member construct");
484 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
485 goto bad_pointer_to_member;
486 /* Now, convert these values to an address. */
487 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
488 arg1);
489 arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
490 value_as_long (arg1) + value_as_long (arg2));
491 return value_ind (arg3);
492 bad_pointer_to_member:
493 error("non-pointer-to-member value used in pointer-to-member construct");
494
495 case BINOP_ASSIGN:
496 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
497 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
498 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
499 return arg1;
500 if (binop_user_defined_p (op, arg1, arg2))
501 return value_x_binop (arg1, arg2, op, OP_NULL);
502 else
503 return value_assign (arg1, arg2);
504
505 case BINOP_ASSIGN_MODIFY:
506 (*pos) += 2;
507 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
508 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
509 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
510 return arg1;
511 op = exp->elts[pc + 1].opcode;
512 if (binop_user_defined_p (op, arg1, arg2))
513 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
514 else if (op == BINOP_ADD)
515 arg2 = value_add (arg1, arg2);
516 else if (op == BINOP_SUB)
517 arg2 = value_sub (arg1, arg2);
518 else
519 arg2 = value_binop (arg1, arg2, op);
520 return value_assign (arg1, arg2);
521
522 case BINOP_ADD:
523 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
524 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
525 if (noside == EVAL_SKIP)
526 goto nosideret;
527 if (binop_user_defined_p (op, arg1, arg2))
528 return value_x_binop (arg1, arg2, op, OP_NULL);
529 else
530 return value_add (arg1, arg2);
531
532 case BINOP_SUB:
533 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
534 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
535 if (noside == EVAL_SKIP)
536 goto nosideret;
537 if (binop_user_defined_p (op, arg1, arg2))
538 return value_x_binop (arg1, arg2, op, OP_NULL);
539 else
540 return value_sub (arg1, arg2);
541
542 case BINOP_MUL:
543 case BINOP_DIV:
544 case BINOP_REM:
545 case BINOP_LSH:
546 case BINOP_RSH:
547 case BINOP_BITWISE_AND:
548 case BINOP_BITWISE_IOR:
549 case BINOP_BITWISE_XOR:
550 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
551 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
552 if (noside == EVAL_SKIP)
553 goto nosideret;
554 if (binop_user_defined_p (op, arg1, arg2))
555 return value_x_binop (arg1, arg2, op, OP_NULL);
556 else
557 if (noside == EVAL_AVOID_SIDE_EFFECTS
558 && (op == BINOP_DIV || op == BINOP_REM))
559 return value_zero (VALUE_TYPE (arg1), not_lval);
560 else
561 return value_binop (arg1, arg2, op);
562
563 case BINOP_SUBSCRIPT:
564 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
565 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
566 if (noside == EVAL_SKIP)
567 goto nosideret;
568 if (noside == EVAL_AVOID_SIDE_EFFECTS)
569 {
570 /* If the user attempts to subscript something that has no target
571 type (like a plain int variable for example), then report this
572 as an error. */
573
574 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
575 if (type)
576 return value_zero (type, VALUE_LVAL (arg1));
577 else
578 error ("cannot subscript something of type `%s'",
579 TYPE_NAME (VALUE_TYPE (arg1)));
580 }
581
582 if (binop_user_defined_p (op, arg1, arg2))
583 return value_x_binop (arg1, arg2, op, OP_NULL);
584 else
585 return value_subscript (arg1, arg2);
586
587 case BINOP_LOGICAL_AND:
588 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
589 if (noside == EVAL_SKIP)
590 {
591 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
592 goto nosideret;
593 }
594
595 oldpos = *pos;
596 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
597 *pos = oldpos;
598
599 if (binop_user_defined_p (op, arg1, arg2))
600 {
601 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
602 return value_x_binop (arg1, arg2, op, OP_NULL);
603 }
604 else
605 {
606 tem = value_logical_not (arg1);
607 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
608 (tem ? EVAL_SKIP : noside));
609 return value_from_longest (builtin_type_int,
610 (LONGEST) (!tem && !value_logical_not (arg2)));
611 }
612
613 case BINOP_LOGICAL_OR:
614 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
615 if (noside == EVAL_SKIP)
616 {
617 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
618 goto nosideret;
619 }
620
621 oldpos = *pos;
622 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
623 *pos = oldpos;
624
625 if (binop_user_defined_p (op, arg1, arg2))
626 {
627 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
628 return value_x_binop (arg1, arg2, op, OP_NULL);
629 }
630 else
631 {
632 tem = value_logical_not (arg1);
633 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
634 (!tem ? EVAL_SKIP : noside));
635 return value_from_longest (builtin_type_int,
636 (LONGEST) (!tem || !value_logical_not (arg2)));
637 }
638
639 case BINOP_EQUAL:
640 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
641 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
642 if (noside == EVAL_SKIP)
643 goto nosideret;
644 if (binop_user_defined_p (op, arg1, arg2))
645 {
646 return value_x_binop (arg1, arg2, op, OP_NULL);
647 }
648 else
649 {
650 tem = value_equal (arg1, arg2);
651 return value_from_longest (builtin_type_int, (LONGEST) tem);
652 }
653
654 case BINOP_NOTEQUAL:
655 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
656 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
657 if (noside == EVAL_SKIP)
658 goto nosideret;
659 if (binop_user_defined_p (op, arg1, arg2))
660 {
661 return value_x_binop (arg1, arg2, op, OP_NULL);
662 }
663 else
664 {
665 tem = value_equal (arg1, arg2);
666 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
667 }
668
669 case BINOP_LESS:
670 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
671 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
672 if (noside == EVAL_SKIP)
673 goto nosideret;
674 if (binop_user_defined_p (op, arg1, arg2))
675 {
676 return value_x_binop (arg1, arg2, op, OP_NULL);
677 }
678 else
679 {
680 tem = value_less (arg1, arg2);
681 return value_from_longest (builtin_type_int, (LONGEST) tem);
682 }
683
684 case BINOP_GTR:
685 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
686 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
687 if (noside == EVAL_SKIP)
688 goto nosideret;
689 if (binop_user_defined_p (op, arg1, arg2))
690 {
691 return value_x_binop (arg1, arg2, op, OP_NULL);
692 }
693 else
694 {
695 tem = value_less (arg2, arg1);
696 return value_from_longest (builtin_type_int, (LONGEST) tem);
697 }
698
699 case BINOP_GEQ:
700 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
701 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
702 if (noside == EVAL_SKIP)
703 goto nosideret;
704 if (binop_user_defined_p (op, arg1, arg2))
705 {
706 return value_x_binop (arg1, arg2, op, OP_NULL);
707 }
708 else
709 {
710 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
711 return value_from_longest (builtin_type_int, (LONGEST) tem);
712 }
713
714 case BINOP_LEQ:
715 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
716 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
717 if (noside == EVAL_SKIP)
718 goto nosideret;
719 if (binop_user_defined_p (op, arg1, arg2))
720 {
721 return value_x_binop (arg1, arg2, op, OP_NULL);
722 }
723 else
724 {
725 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
726 return value_from_longest (builtin_type_int, (LONGEST) tem);
727 }
728
729 case BINOP_REPEAT:
730 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
731 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
732 if (noside == EVAL_SKIP)
733 goto nosideret;
734 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
735 error ("Non-integral right operand for \"@\" operator.");
736 if (noside == EVAL_AVOID_SIDE_EFFECTS)
737 return allocate_repeat_value (VALUE_TYPE (arg1),
738 longest_to_int (value_as_long (arg2)));
739 else
740 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
741
742 case BINOP_COMMA:
743 evaluate_subexp (NULL_TYPE, exp, pos, noside);
744 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
745
746 case UNOP_NEG:
747 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
748 if (noside == EVAL_SKIP)
749 goto nosideret;
750 if (unop_user_defined_p (op, arg1))
751 return value_x_unop (arg1, op);
752 else
753 return value_neg (arg1);
754
755 case UNOP_COMPLEMENT:
756 /* C++: check for and handle destructor names. */
757 op = exp->elts[*pos].opcode;
758
759 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
760 if (noside == EVAL_SKIP)
761 goto nosideret;
762 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
763 return value_x_unop (arg1, UNOP_COMPLEMENT);
764 else
765 return value_complement (arg1);
766
767 case UNOP_LOGICAL_NOT:
768 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
769 if (noside == EVAL_SKIP)
770 goto nosideret;
771 if (unop_user_defined_p (op, arg1))
772 return value_x_unop (arg1, op);
773 else
774 return value_from_longest (builtin_type_int,
775 (LONGEST) value_logical_not (arg1));
776
777 case UNOP_IND:
778 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
779 expect_type = TYPE_TARGET_TYPE (expect_type);
780 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
781 if (noside == EVAL_SKIP)
782 goto nosideret;
783 if (noside == EVAL_AVOID_SIDE_EFFECTS)
784 {
785 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
786 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
787 /* In C you can dereference an array to get the 1st elt. */
788 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
789 )
790 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
791 lval_memory);
792 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
793 /* GDB allows dereferencing an int. */
794 return value_zero (builtin_type_int, lval_memory);
795 else
796 error ("Attempt to take contents of a non-pointer value.");
797 }
798 return value_ind (arg1);
799
800 case UNOP_ADDR:
801 /* C++: check for and handle pointer to members. */
802
803 op = exp->elts[*pos].opcode;
804
805 if (noside == EVAL_SKIP)
806 {
807 if (op == OP_SCOPE)
808 {
809 int temm = longest_to_int (exp->elts[pc+3].longconst);
810 (*pos) += 3 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
811 }
812 else
813 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
814 goto nosideret;
815 }
816
817 return evaluate_subexp_for_address (exp, pos, noside);
818
819 case UNOP_SIZEOF:
820 if (noside == EVAL_SKIP)
821 {
822 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
823 goto nosideret;
824 }
825 return evaluate_subexp_for_sizeof (exp, pos);
826
827 case UNOP_CAST:
828 (*pos) += 2;
829 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
830 if (noside == EVAL_SKIP)
831 goto nosideret;
832 return value_cast (exp->elts[pc + 1].type, arg1);
833
834 case UNOP_MEMVAL:
835 (*pos) += 2;
836 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
837 if (noside == EVAL_SKIP)
838 goto nosideret;
839 if (noside == EVAL_AVOID_SIDE_EFFECTS)
840 return value_zero (exp->elts[pc + 1].type, lval_memory);
841 else
842 return value_at_lazy (exp->elts[pc + 1].type,
843 value_as_pointer (arg1));
844
845 case UNOP_PREINCREMENT:
846 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
847 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
848 return arg1;
849 else if (unop_user_defined_p (op, arg1))
850 {
851 return value_x_unop (arg1, op);
852 }
853 else
854 {
855 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
856 (LONGEST) 1));
857 return value_assign (arg1, arg2);
858 }
859
860 case UNOP_PREDECREMENT:
861 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
862 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
863 return arg1;
864 else if (unop_user_defined_p (op, arg1))
865 {
866 return value_x_unop (arg1, op);
867 }
868 else
869 {
870 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
871 (LONGEST) 1));
872 return value_assign (arg1, arg2);
873 }
874
875 case UNOP_POSTINCREMENT:
876 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
877 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
878 return arg1;
879 else if (unop_user_defined_p (op, arg1))
880 {
881 return value_x_unop (arg1, op);
882 }
883 else
884 {
885 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
886 (LONGEST) 1));
887 value_assign (arg1, arg2);
888 return arg1;
889 }
890
891 case UNOP_POSTDECREMENT:
892 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
893 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
894 return arg1;
895 else if (unop_user_defined_p (op, arg1))
896 {
897 return value_x_unop (arg1, op);
898 }
899 else
900 {
901 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
902 (LONGEST) 1));
903 value_assign (arg1, arg2);
904 return arg1;
905 }
906
907 case OP_THIS:
908 (*pos) += 1;
909 return value_of_this (1);
910
911 default:
912 error ("internal error: I do not know how to evaluate what you gave me");
913 }
914
915 nosideret:
916 return value_from_longest (builtin_type_long, (LONGEST) 1);
917 }
918 \f
919 /* Evaluate a subexpression of EXP, at index *POS,
920 and return the address of that subexpression.
921 Advance *POS over the subexpression.
922 If the subexpression isn't an lvalue, get an error.
923 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
924 then only the type of the result need be correct. */
925
926 static value
927 evaluate_subexp_for_address (exp, pos, noside)
928 register struct expression *exp;
929 register int *pos;
930 enum noside noside;
931 {
932 enum exp_opcode op;
933 register int pc;
934 struct symbol *var;
935
936 pc = (*pos);
937 op = exp->elts[pc].opcode;
938
939 switch (op)
940 {
941 case UNOP_IND:
942 (*pos)++;
943 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
944
945 case UNOP_MEMVAL:
946 (*pos) += 3;
947 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
948 evaluate_subexp (NULL_TYPE, exp, pos, noside));
949
950 case OP_VAR_VALUE:
951 var = exp->elts[pc + 1].symbol;
952
953 /* C++: The "address" of a reference should yield the address
954 * of the object pointed to. Let value_addr() deal with it. */
955 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
956 goto default_case;
957
958 (*pos) += 3;
959 if (noside == EVAL_AVOID_SIDE_EFFECTS)
960 {
961 struct type *type =
962 lookup_pointer_type (SYMBOL_TYPE (var));
963 enum address_class sym_class = SYMBOL_CLASS (var);
964
965 if (sym_class == LOC_CONST
966 || sym_class == LOC_CONST_BYTES
967 || sym_class == LOC_REGISTER
968 || sym_class == LOC_REGPARM)
969 error ("Attempt to take address of register or constant.");
970
971 return
972 value_zero (type, not_lval);
973 }
974 else
975 return locate_var_value (var, (FRAME) 0);
976
977 default:
978 default_case:
979 if (noside == EVAL_AVOID_SIDE_EFFECTS)
980 {
981 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
982 if (VALUE_LVAL (x) == lval_memory)
983 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
984 not_lval);
985 else
986 error ("Attempt to take address of non-lval");
987 }
988 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
989 }
990 }
991
992 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
993 When used in contexts where arrays will be coerced anyway,
994 this is equivalent to `evaluate_subexp'
995 but much faster because it avoids actually fetching array contents. */
996
997 static value
998 evaluate_subexp_with_coercion (exp, pos, noside)
999 register struct expression *exp;
1000 register int *pos;
1001 enum noside noside;
1002 {
1003 register enum exp_opcode op;
1004 register int pc;
1005 register value val;
1006 struct symbol *var;
1007
1008 pc = (*pos);
1009 op = exp->elts[pc].opcode;
1010
1011 switch (op)
1012 {
1013 case OP_VAR_VALUE:
1014 var = exp->elts[pc + 1].symbol;
1015 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY)
1016 {
1017 (*pos) += 3;
1018 val = locate_var_value (var, (FRAME) 0);
1019 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
1020 val);
1021 }
1022 default:
1023 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1024 }
1025 }
1026
1027 /* Evaluate a subexpression of EXP, at index *POS,
1028 and return a value for the size of that subexpression.
1029 Advance *POS over the subexpression. */
1030
1031 static value
1032 evaluate_subexp_for_sizeof (exp, pos)
1033 register struct expression *exp;
1034 register int *pos;
1035 {
1036 enum exp_opcode op;
1037 register int pc;
1038 value val;
1039
1040 pc = (*pos);
1041 op = exp->elts[pc].opcode;
1042
1043 switch (op)
1044 {
1045 /* This case is handled specially
1046 so that we avoid creating a value for the result type.
1047 If the result type is very big, it's desirable not to
1048 create a value unnecessarily. */
1049 case UNOP_IND:
1050 (*pos)++;
1051 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1052 return value_from_longest (builtin_type_int, (LONGEST)
1053 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1054
1055 case UNOP_MEMVAL:
1056 (*pos) += 3;
1057 return value_from_longest (builtin_type_int,
1058 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1059
1060 case OP_VAR_VALUE:
1061 (*pos) += 3;
1062 return value_from_longest (builtin_type_int,
1063 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1064
1065 default:
1066 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1067 return value_from_longest (builtin_type_int,
1068 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1069 }
1070 }
1071
1072 /* Parse a type expression in the string [P..P+LENGTH). */
1073
1074 struct type *
1075 parse_and_eval_type (p, length)
1076 char *p;
1077 int length;
1078 {
1079 char *tmp = (char *)alloca (length + 4);
1080 struct expression *expr;
1081 tmp[0] = '(';
1082 memcpy (tmp+1, p, length);
1083 tmp[length+1] = ')';
1084 tmp[length+2] = '0';
1085 tmp[length+3] = '\0';
1086 expr = parse_expression (tmp);
1087 if (expr->elts[0].opcode != UNOP_CAST)
1088 error ("Internal error in eval_type.");
1089 return expr->elts[1].type;
1090 }
This page took 0.051125 seconds and 5 git commands to generate.