* breakpoint.c, breakpoint.h (breakpoint_init_inferior): New function
[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 #include "language.h" /* For CAST_IS_CONVERSION */
28
29 /* Values of NOSIDE argument to eval_subexp. */
30 enum noside
31 { EVAL_NORMAL,
32 EVAL_SKIP, /* Only effect is to increment pos. */
33 EVAL_AVOID_SIDE_EFFECTS /* Don't modify any variables or
34 call any functions. The value
35 returned will have the correct
36 type, and will have an
37 approximately correct lvalue
38 type (inaccuracy: anything that is
39 listed as being in a register in
40 the function in which it was
41 declared will be lval_register). */
42 };
43
44 /* Prototypes for local functions. */
45
46 static value
47 evaluate_subexp_for_sizeof PARAMS ((struct expression *, int *));
48
49 static value
50 evaluate_subexp_with_coercion PARAMS ((struct expression *, int *,
51 enum noside));
52
53 static value
54 evaluate_subexp_for_address PARAMS ((struct expression *, int *,
55 enum noside));
56
57 static value
58 evaluate_subexp PARAMS ((struct type *, struct expression *, int *,
59 enum noside));
60
61 \f
62 /* Parse the string EXP as a C expression, evaluate it,
63 and return the result as a number. */
64
65 CORE_ADDR
66 parse_and_eval_address (exp)
67 char *exp;
68 {
69 struct expression *expr = parse_expression (exp);
70 register CORE_ADDR addr;
71 register struct cleanup *old_chain =
72 make_cleanup (free_current_contents, &expr);
73
74 addr = value_as_pointer (evaluate_expression (expr));
75 do_cleanups (old_chain);
76 return addr;
77 }
78
79 /* Like parse_and_eval_address but takes a pointer to a char * variable
80 and advanced that variable across the characters parsed. */
81
82 CORE_ADDR
83 parse_and_eval_address_1 (expptr)
84 char **expptr;
85 {
86 struct expression *expr = parse_exp_1 (expptr, (struct block *)0, 0);
87 register CORE_ADDR addr;
88 register struct cleanup *old_chain =
89 make_cleanup (free_current_contents, &expr);
90
91 addr = value_as_pointer (evaluate_expression (expr));
92 do_cleanups (old_chain);
93 return addr;
94 }
95
96 value
97 parse_and_eval (exp)
98 char *exp;
99 {
100 struct expression *expr = parse_expression (exp);
101 register value val;
102 register struct cleanup *old_chain
103 = make_cleanup (free_current_contents, &expr);
104
105 val = evaluate_expression (expr);
106 do_cleanups (old_chain);
107 return val;
108 }
109
110 /* Parse up to a comma (or to a closeparen)
111 in the string EXPP as an expression, evaluate it, and return the value.
112 EXPP is advanced to point to the comma. */
113
114 value
115 parse_to_comma_and_eval (expp)
116 char **expp;
117 {
118 struct expression *expr = parse_exp_1 (expp, (struct block *) 0, 1);
119 register value val;
120 register struct cleanup *old_chain
121 = make_cleanup (free_current_contents, &expr);
122
123 val = evaluate_expression (expr);
124 do_cleanups (old_chain);
125 return val;
126 }
127 \f
128 /* Evaluate an expression in internal prefix form
129 such as is constructed by parse.y.
130
131 See expression.h for info on the format of an expression. */
132
133 static value evaluate_subexp ();
134 static value evaluate_subexp_for_address ();
135 static value evaluate_subexp_for_sizeof ();
136 static value evaluate_subexp_with_coercion ();
137
138 value
139 evaluate_expression (exp)
140 struct expression *exp;
141 {
142 int pc = 0;
143 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_NORMAL);
144 }
145
146 /* Evaluate an expression, avoiding all memory references
147 and getting a value whose type alone is correct. */
148
149 value
150 evaluate_type (exp)
151 struct expression *exp;
152 {
153 int pc = 0;
154 return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
155 }
156
157 static value
158 evaluate_subexp (expect_type, exp, pos, noside)
159 struct type *expect_type;
160 register struct expression *exp;
161 register int *pos;
162 enum noside noside;
163 {
164 enum exp_opcode op;
165 int tem, tem2, tem3;
166 register int pc, pc2, oldpos;
167 register value arg1, arg2, arg3;
168 struct type *type;
169 int nargs;
170 value *argvec;
171
172 pc = (*pos)++;
173 op = exp->elts[pc].opcode;
174
175 switch (op)
176 {
177 case OP_SCOPE:
178 tem = longest_to_int (exp->elts[pc + 2].longconst);
179 (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
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) += 3;
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 + 2].symbol,
230 exp->elts[pc + 1].block);
231
232 case OP_LAST:
233 (*pos) += 2;
234 return
235 access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
236
237 case OP_REGISTER:
238 (*pos) += 2;
239 return value_of_register (longest_to_int (exp->elts[pc + 1].longconst));
240
241 case OP_BOOL:
242 (*pos) += 2;
243 return value_from_longest (builtin_type_chill_bool,
244 exp->elts[pc + 1].longconst);
245
246 case OP_INTERNALVAR:
247 (*pos) += 2;
248 return value_of_internalvar (exp->elts[pc + 1].internalvar);
249
250 case OP_STRING:
251 tem = longest_to_int (exp->elts[pc + 1].longconst);
252 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
253 if (noside == EVAL_SKIP)
254 goto nosideret;
255 return value_string (&exp->elts[pc + 2].string, tem);
256
257 case OP_BITSTRING:
258 error ("support for OP_BITSTRING unimplemented");
259 break;
260
261 case OP_ARRAY:
262 (*pos) += 3;
263 tem2 = longest_to_int (exp->elts[pc + 1].longconst);
264 tem3 = longest_to_int (exp->elts[pc + 2].longconst);
265 nargs = tem3 - tem2 + 1;
266 argvec = (value *) alloca (sizeof (value) * nargs);
267 for (tem = 0; tem < nargs; tem++)
268 {
269 /* Ensure that array expressions are coerced into pointer objects. */
270 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
271 }
272 if (noside == EVAL_SKIP)
273 goto nosideret;
274 return (value_array (tem2, tem3, argvec));
275 break;
276
277 case TERNOP_COND:
278 /* Skip third and second args to evaluate the first one. */
279 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
280 if (value_logical_not (arg1))
281 {
282 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
283 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
284 }
285 else
286 {
287 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
288 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
289 return arg2;
290 }
291
292 case OP_FUNCALL:
293 (*pos) += 2;
294 op = exp->elts[*pos].opcode;
295 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
296 {
297 int fnptr;
298
299 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
300 /* First, evaluate the structure into arg2 */
301 pc2 = (*pos)++;
302
303 if (noside == EVAL_SKIP)
304 goto nosideret;
305
306 if (op == STRUCTOP_MEMBER)
307 {
308 arg2 = evaluate_subexp_for_address (exp, pos, noside);
309 }
310 else
311 {
312 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
313 }
314
315 /* If the function is a virtual function, then the
316 aggregate value (providing the structure) plays
317 its part by providing the vtable. Otherwise,
318 it is just along for the ride: call the function
319 directly. */
320
321 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
322
323 fnptr = longest_to_int (value_as_long (arg1));
324
325 if (METHOD_PTR_IS_VIRTUAL(fnptr))
326 {
327 int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
328 struct type *basetype;
329 struct type *domain_type =
330 TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
331 int i, j;
332 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
333 if (domain_type != basetype)
334 arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
335 basetype = TYPE_VPTR_BASETYPE (domain_type);
336 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
337 {
338 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
339 /* If one is virtual, then all are virtual. */
340 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
341 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
342 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
343 {
344 value temp = value_ind (arg2);
345 arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
346 arg2 = value_addr (temp);
347 goto got_it;
348 }
349 }
350 if (i < 0)
351 error ("virtual function at index %d not found", fnoffset);
352 }
353 else
354 {
355 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
356 }
357 got_it:
358
359 /* Now, say which argument to start evaluating from */
360 tem = 2;
361 }
362 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
363 {
364 /* Hair for method invocations */
365 int tem2;
366
367 nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
368 /* First, evaluate the structure into arg2 */
369 pc2 = (*pos)++;
370 tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
371 *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
372 if (noside == EVAL_SKIP)
373 goto nosideret;
374
375 if (op == STRUCTOP_STRUCT)
376 {
377 /* If v is a variable in a register, and the user types
378 v.method (), this will produce an error, because v has
379 no address.
380
381 A possible way around this would be to allocate a
382 copy of the variable on the stack, copy in the
383 contents, call the function, and copy out the
384 contents. I.e. convert this from call by reference
385 to call by copy-return (or whatever it's called).
386 However, this does not work because it is not the
387 same: the method being called could stash a copy of
388 the address, and then future uses through that address
389 (after the method returns) would be expected to
390 use the variable itself, not some copy of it. */
391 arg2 = evaluate_subexp_for_address (exp, pos, noside);
392 }
393 else
394 {
395 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
396 }
397 /* Now, say which argument to start evaluating from */
398 tem = 2;
399 }
400 else
401 {
402 nargs = longest_to_int (exp->elts[pc + 1].longconst);
403 tem = 0;
404 }
405 /* Allocate arg vector, including space for the function to be
406 called in argvec[0] and a terminating NULL */
407 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
408 for (; tem <= nargs; tem++)
409 /* Ensure that array expressions are coerced into pointer objects. */
410 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
411
412 /* signal end of arglist */
413 argvec[tem] = 0;
414
415 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
416 {
417 int static_memfuncp;
418 value temp = arg2;
419
420 argvec[1] = arg2;
421 argvec[0] =
422 value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 2].string,
423 &static_memfuncp,
424 op == STRUCTOP_STRUCT
425 ? "structure" : "structure pointer");
426 if (VALUE_OFFSET (temp))
427 {
428 arg2 = value_from_longest (lookup_pointer_type (VALUE_TYPE (temp)),
429 VALUE_ADDRESS (temp)+VALUE_OFFSET (temp));
430 argvec[1] = arg2;
431 }
432 if (static_memfuncp)
433 {
434 argvec[1] = argvec[0];
435 nargs--;
436 argvec++;
437 }
438 }
439 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
440 {
441 argvec[1] = arg2;
442 argvec[0] = arg1;
443 }
444
445 if (noside == EVAL_SKIP)
446 goto nosideret;
447 if (noside == EVAL_AVOID_SIDE_EFFECTS)
448 {
449 /* If the return type doesn't look like a function type, call an
450 error. This can happen if somebody tries to turn a variable into
451 a function call. This is here because people often want to
452 call, eg, strcmp, which gdb doesn't know is a function. If
453 gdb isn't asked for it's opinion (ie. through "whatis"),
454 it won't offer it. */
455
456 struct type *ftype =
457 TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0]));
458
459 if (ftype)
460 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
461 else
462 error ("Expression of type other than \"Function returning ...\" used as function");
463 }
464 return call_function_by_hand (argvec[0], nargs, argvec + 1);
465
466 case STRUCTOP_STRUCT:
467 tem = longest_to_int (exp->elts[pc + 1].longconst);
468 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
469 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
470 if (noside == EVAL_SKIP)
471 goto nosideret;
472 if (noside == EVAL_AVOID_SIDE_EFFECTS)
473 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
474 &exp->elts[pc + 2].string,
475 0),
476 lval_memory);
477 else
478 {
479 value temp = arg1;
480 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
481 (int *) 0, "structure");
482 }
483
484 case STRUCTOP_PTR:
485 tem = longest_to_int (exp->elts[pc + 1].longconst);
486 (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
487 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
488 if (noside == EVAL_SKIP)
489 goto nosideret;
490 if (noside == EVAL_AVOID_SIDE_EFFECTS)
491 return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
492 &exp->elts[pc + 2].string,
493 0),
494 lval_memory);
495 else
496 {
497 value temp = arg1;
498 return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
499 (int *) 0, "structure pointer");
500 }
501
502 case STRUCTOP_MEMBER:
503 arg1 = evaluate_subexp_for_address (exp, pos, noside);
504 goto handle_pointer_to_member;
505 case STRUCTOP_MPTR:
506 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
507 handle_pointer_to_member:
508 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
509 if (noside == EVAL_SKIP)
510 goto nosideret;
511 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR)
512 goto bad_pointer_to_member;
513 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
514 if (TYPE_CODE (type) == TYPE_CODE_METHOD)
515 error ("not implemented: pointer-to-method in pointer-to-member construct");
516 if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
517 goto bad_pointer_to_member;
518 /* Now, convert these values to an address. */
519 arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
520 arg1);
521 arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
522 value_as_long (arg1) + value_as_long (arg2));
523 return value_ind (arg3);
524 bad_pointer_to_member:
525 error("non-pointer-to-member value used in pointer-to-member construct");
526
527 case BINOP_CONCAT:
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, OP_NULL);
534 else
535 return value_concat (arg1, arg2);
536
537 case BINOP_ASSIGN:
538 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
539 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
540 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
541 return arg1;
542 if (binop_user_defined_p (op, arg1, arg2))
543 return value_x_binop (arg1, arg2, op, OP_NULL);
544 else
545 return value_assign (arg1, arg2);
546
547 case BINOP_ASSIGN_MODIFY:
548 (*pos) += 2;
549 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
550 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
551 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
552 return arg1;
553 op = exp->elts[pc + 1].opcode;
554 if (binop_user_defined_p (op, arg1, arg2))
555 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
556 else if (op == BINOP_ADD)
557 arg2 = value_add (arg1, arg2);
558 else if (op == BINOP_SUB)
559 arg2 = value_sub (arg1, arg2);
560 else
561 arg2 = value_binop (arg1, arg2, op);
562 return value_assign (arg1, arg2);
563
564 case BINOP_ADD:
565 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
566 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
567 if (noside == EVAL_SKIP)
568 goto nosideret;
569 if (binop_user_defined_p (op, arg1, arg2))
570 return value_x_binop (arg1, arg2, op, OP_NULL);
571 else
572 return value_add (arg1, arg2);
573
574 case BINOP_SUB:
575 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
576 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
577 if (noside == EVAL_SKIP)
578 goto nosideret;
579 if (binop_user_defined_p (op, arg1, arg2))
580 return value_x_binop (arg1, arg2, op, OP_NULL);
581 else
582 return value_sub (arg1, arg2);
583
584 case BINOP_MUL:
585 case BINOP_DIV:
586 case BINOP_REM:
587 case BINOP_MOD:
588 case BINOP_LSH:
589 case BINOP_RSH:
590 case BINOP_BITWISE_AND:
591 case BINOP_BITWISE_IOR:
592 case BINOP_BITWISE_XOR:
593 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
594 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
595 if (noside == EVAL_SKIP)
596 goto nosideret;
597 if (binop_user_defined_p (op, arg1, arg2))
598 return value_x_binop (arg1, arg2, op, OP_NULL);
599 else
600 if (noside == EVAL_AVOID_SIDE_EFFECTS
601 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
602 return value_zero (VALUE_TYPE (arg1), not_lval);
603 else
604 return value_binop (arg1, arg2, op);
605
606 case BINOP_SUBSCRIPT:
607 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
608 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
609 if (noside == EVAL_SKIP)
610 goto nosideret;
611 if (noside == EVAL_AVOID_SIDE_EFFECTS)
612 {
613 /* If the user attempts to subscript something that has no target
614 type (like a plain int variable for example), then report this
615 as an error. */
616
617 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
618 if (type)
619 return value_zero (type, VALUE_LVAL (arg1));
620 else
621 error ("cannot subscript something of type `%s'",
622 TYPE_NAME (VALUE_TYPE (arg1)));
623 }
624
625 if (binop_user_defined_p (op, arg1, arg2))
626 return value_x_binop (arg1, arg2, op, OP_NULL);
627 else
628 return value_subscript (arg1, arg2);
629
630 case MULTI_SUBSCRIPT:
631 (*pos) += 2;
632 nargs = longest_to_int (exp->elts[pc + 1].longconst);
633 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
634 while (nargs-- > 0)
635 {
636 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
637 /* FIXME: EVAL_SKIP handling may not be correct. */
638 if (noside == EVAL_SKIP)
639 {
640 if (nargs > 0)
641 {
642 continue;
643 }
644 else
645 {
646 goto nosideret;
647 }
648 }
649 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
650 if (noside == EVAL_AVOID_SIDE_EFFECTS)
651 {
652 /* If the user attempts to subscript something that has no target
653 type (like a plain int variable for example), then report this
654 as an error. */
655
656 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
657 if (type != NULL)
658 {
659 arg1 = value_zero (type, VALUE_LVAL (arg1));
660 noside = EVAL_SKIP;
661 continue;
662 }
663 else
664 {
665 error ("cannot subscript something of type `%s'",
666 TYPE_NAME (VALUE_TYPE (arg1)));
667 }
668 }
669
670 if (binop_user_defined_p (op, arg1, arg2))
671 {
672 arg1 = value_x_binop (arg1, arg2, op, OP_NULL);
673 }
674 else
675 {
676 arg1 = value_subscript (arg1, arg2);
677 }
678 }
679 return (arg1);
680
681 case BINOP_LOGICAL_AND:
682 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
683 if (noside == EVAL_SKIP)
684 {
685 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
686 goto nosideret;
687 }
688
689 oldpos = *pos;
690 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
691 *pos = oldpos;
692
693 if (binop_user_defined_p (op, arg1, arg2))
694 {
695 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
696 return value_x_binop (arg1, arg2, op, OP_NULL);
697 }
698 else
699 {
700 tem = value_logical_not (arg1);
701 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
702 (tem ? EVAL_SKIP : noside));
703 return value_from_longest (builtin_type_int,
704 (LONGEST) (!tem && !value_logical_not (arg2)));
705 }
706
707 case BINOP_LOGICAL_OR:
708 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
709 if (noside == EVAL_SKIP)
710 {
711 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
712 goto nosideret;
713 }
714
715 oldpos = *pos;
716 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
717 *pos = oldpos;
718
719 if (binop_user_defined_p (op, arg1, arg2))
720 {
721 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
722 return value_x_binop (arg1, arg2, op, OP_NULL);
723 }
724 else
725 {
726 tem = value_logical_not (arg1);
727 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
728 (!tem ? EVAL_SKIP : noside));
729 return value_from_longest (builtin_type_int,
730 (LONGEST) (!tem || !value_logical_not (arg2)));
731 }
732
733 case BINOP_EQUAL:
734 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
735 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
736 if (noside == EVAL_SKIP)
737 goto nosideret;
738 if (binop_user_defined_p (op, arg1, arg2))
739 {
740 return value_x_binop (arg1, arg2, op, OP_NULL);
741 }
742 else
743 {
744 tem = value_equal (arg1, arg2);
745 return value_from_longest (builtin_type_int, (LONGEST) tem);
746 }
747
748 case BINOP_NOTEQUAL:
749 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
750 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
751 if (noside == EVAL_SKIP)
752 goto nosideret;
753 if (binop_user_defined_p (op, arg1, arg2))
754 {
755 return value_x_binop (arg1, arg2, op, OP_NULL);
756 }
757 else
758 {
759 tem = value_equal (arg1, arg2);
760 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
761 }
762
763 case BINOP_LESS:
764 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
765 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
766 if (noside == EVAL_SKIP)
767 goto nosideret;
768 if (binop_user_defined_p (op, arg1, arg2))
769 {
770 return value_x_binop (arg1, arg2, op, OP_NULL);
771 }
772 else
773 {
774 tem = value_less (arg1, arg2);
775 return value_from_longest (builtin_type_int, (LONGEST) tem);
776 }
777
778 case BINOP_GTR:
779 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
780 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
781 if (noside == EVAL_SKIP)
782 goto nosideret;
783 if (binop_user_defined_p (op, arg1, arg2))
784 {
785 return value_x_binop (arg1, arg2, op, OP_NULL);
786 }
787 else
788 {
789 tem = value_less (arg2, arg1);
790 return value_from_longest (builtin_type_int, (LONGEST) tem);
791 }
792
793 case BINOP_GEQ:
794 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
795 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
796 if (noside == EVAL_SKIP)
797 goto nosideret;
798 if (binop_user_defined_p (op, arg1, arg2))
799 {
800 return value_x_binop (arg1, arg2, op, OP_NULL);
801 }
802 else
803 {
804 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
805 return value_from_longest (builtin_type_int, (LONGEST) tem);
806 }
807
808 case BINOP_LEQ:
809 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
810 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
811 if (noside == EVAL_SKIP)
812 goto nosideret;
813 if (binop_user_defined_p (op, arg1, arg2))
814 {
815 return value_x_binop (arg1, arg2, op, OP_NULL);
816 }
817 else
818 {
819 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
820 return value_from_longest (builtin_type_int, (LONGEST) tem);
821 }
822
823 case BINOP_REPEAT:
824 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
825 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
826 if (noside == EVAL_SKIP)
827 goto nosideret;
828 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
829 error ("Non-integral right operand for \"@\" operator.");
830 if (noside == EVAL_AVOID_SIDE_EFFECTS)
831 return allocate_repeat_value (VALUE_TYPE (arg1),
832 longest_to_int (value_as_long (arg2)));
833 else
834 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
835
836 case BINOP_COMMA:
837 evaluate_subexp (NULL_TYPE, exp, pos, noside);
838 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
839
840 case UNOP_NEG:
841 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
842 if (noside == EVAL_SKIP)
843 goto nosideret;
844 if (unop_user_defined_p (op, arg1))
845 return value_x_unop (arg1, op);
846 else
847 return value_neg (arg1);
848
849 case UNOP_COMPLEMENT:
850 /* C++: check for and handle destructor names. */
851 op = exp->elts[*pos].opcode;
852
853 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
854 if (noside == EVAL_SKIP)
855 goto nosideret;
856 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
857 return value_x_unop (arg1, UNOP_COMPLEMENT);
858 else
859 return value_complement (arg1);
860
861 case UNOP_LOGICAL_NOT:
862 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
863 if (noside == EVAL_SKIP)
864 goto nosideret;
865 if (unop_user_defined_p (op, arg1))
866 return value_x_unop (arg1, op);
867 else
868 return value_from_longest (builtin_type_int,
869 (LONGEST) value_logical_not (arg1));
870
871 case UNOP_IND:
872 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
873 expect_type = TYPE_TARGET_TYPE (expect_type);
874 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
875 if (noside == EVAL_SKIP)
876 goto nosideret;
877 if (noside == EVAL_AVOID_SIDE_EFFECTS)
878 {
879 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
880 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
881 /* In C you can dereference an array to get the 1st elt. */
882 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
883 )
884 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
885 lval_memory);
886 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
887 /* GDB allows dereferencing an int. */
888 return value_zero (builtin_type_int, lval_memory);
889 else
890 error ("Attempt to take contents of a non-pointer value.");
891 }
892 return value_ind (arg1);
893
894 case UNOP_ADDR:
895 /* C++: check for and handle pointer to members. */
896
897 op = exp->elts[*pos].opcode;
898
899 if (noside == EVAL_SKIP)
900 {
901 if (op == OP_SCOPE)
902 {
903 int temm = longest_to_int (exp->elts[pc+3].longconst);
904 (*pos) += 3 + BYTES_TO_EXP_ELEM (temm + 1);
905 }
906 else
907 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
908 goto nosideret;
909 }
910
911 return evaluate_subexp_for_address (exp, pos, noside);
912
913 case UNOP_SIZEOF:
914 if (noside == EVAL_SKIP)
915 {
916 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
917 goto nosideret;
918 }
919 return evaluate_subexp_for_sizeof (exp, pos);
920
921 case UNOP_CAST:
922 (*pos) += 2;
923 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
924 if (noside == EVAL_SKIP)
925 goto nosideret;
926 return value_cast (exp->elts[pc + 1].type, arg1);
927
928 case UNOP_MEMVAL:
929 (*pos) += 2;
930 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
931 if (noside == EVAL_SKIP)
932 goto nosideret;
933 if (noside == EVAL_AVOID_SIDE_EFFECTS)
934 return value_zero (exp->elts[pc + 1].type, lval_memory);
935 else
936 return value_at_lazy (exp->elts[pc + 1].type,
937 value_as_pointer (arg1));
938
939 case UNOP_PREINCREMENT:
940 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
941 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
942 return arg1;
943 else if (unop_user_defined_p (op, arg1))
944 {
945 return value_x_unop (arg1, op);
946 }
947 else
948 {
949 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
950 (LONGEST) 1));
951 return value_assign (arg1, arg2);
952 }
953
954 case UNOP_PREDECREMENT:
955 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
956 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
957 return arg1;
958 else if (unop_user_defined_p (op, arg1))
959 {
960 return value_x_unop (arg1, op);
961 }
962 else
963 {
964 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
965 (LONGEST) 1));
966 return value_assign (arg1, arg2);
967 }
968
969 case UNOP_POSTINCREMENT:
970 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
971 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
972 return arg1;
973 else if (unop_user_defined_p (op, arg1))
974 {
975 return value_x_unop (arg1, op);
976 }
977 else
978 {
979 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
980 (LONGEST) 1));
981 value_assign (arg1, arg2);
982 return arg1;
983 }
984
985 case UNOP_POSTDECREMENT:
986 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
987 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
988 return arg1;
989 else if (unop_user_defined_p (op, arg1))
990 {
991 return value_x_unop (arg1, op);
992 }
993 else
994 {
995 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
996 (LONGEST) 1));
997 value_assign (arg1, arg2);
998 return arg1;
999 }
1000
1001 case OP_THIS:
1002 (*pos) += 1;
1003 return value_of_this (1);
1004
1005 case OP_TYPE:
1006 error ("Attempt to use a type name as an expression");
1007
1008 default:
1009 /* Removing this case and compiling with gcc -Wall reveals that
1010 a lot of cases are hitting this case. Some of these should
1011 probably be removed from expression.h (e.g. do we need a BINOP_SCOPE
1012 and an OP_SCOPE?); others are legitimate expressions which are
1013 (apparently) not fully implemented.
1014
1015 If there are any cases landing here which mean a user error,
1016 then they should be separate cases, with more descriptive
1017 error messages. */
1018
1019 error ("\
1020 GDB does not (yet) know how to evaluated that kind of expression");
1021 }
1022
1023 nosideret:
1024 return value_from_longest (builtin_type_long, (LONGEST) 1);
1025 }
1026 \f
1027 /* Evaluate a subexpression of EXP, at index *POS,
1028 and return the address of that subexpression.
1029 Advance *POS over the subexpression.
1030 If the subexpression isn't an lvalue, get an error.
1031 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
1032 then only the type of the result need be correct. */
1033
1034 static value
1035 evaluate_subexp_for_address (exp, pos, noside)
1036 register struct expression *exp;
1037 register int *pos;
1038 enum noside noside;
1039 {
1040 enum exp_opcode op;
1041 register int pc;
1042 struct symbol *var;
1043
1044 pc = (*pos);
1045 op = exp->elts[pc].opcode;
1046
1047 switch (op)
1048 {
1049 case UNOP_IND:
1050 (*pos)++;
1051 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1052
1053 case UNOP_MEMVAL:
1054 (*pos) += 3;
1055 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1056 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1057
1058 case OP_VAR_VALUE:
1059 var = exp->elts[pc + 2].symbol;
1060
1061 /* C++: The "address" of a reference should yield the address
1062 * of the object pointed to. Let value_addr() deal with it. */
1063 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
1064 goto default_case;
1065
1066 (*pos) += 4;
1067 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1068 {
1069 struct type *type =
1070 lookup_pointer_type (SYMBOL_TYPE (var));
1071 enum address_class sym_class = SYMBOL_CLASS (var);
1072
1073 if (sym_class == LOC_CONST
1074 || sym_class == LOC_CONST_BYTES
1075 || sym_class == LOC_REGISTER
1076 || sym_class == LOC_REGPARM)
1077 error ("Attempt to take address of register or constant.");
1078
1079 return
1080 value_zero (type, not_lval);
1081 }
1082 else
1083 return
1084 locate_var_value
1085 (var,
1086 block_innermost_frame (exp->elts[pc + 1].block));
1087
1088 default:
1089 default_case:
1090 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1091 {
1092 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1093 if (VALUE_LVAL (x) == lval_memory)
1094 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
1095 not_lval);
1096 else
1097 error ("Attempt to take address of non-lval");
1098 }
1099 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1100 }
1101 }
1102
1103 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1104 When used in contexts where arrays will be coerced anyway, this is
1105 equivalent to `evaluate_subexp' but much faster because it avoids
1106 actually fetching array contents (perhaps obsolete now that we have
1107 VALUE_LAZY).
1108
1109 Note that we currently only do the coercion for C expressions, where
1110 arrays are zero based and the coercion is correct. For other languages,
1111 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1112 to decide if coercion is appropriate.
1113
1114 */
1115
1116 static value
1117 evaluate_subexp_with_coercion (exp, pos, noside)
1118 register struct expression *exp;
1119 register int *pos;
1120 enum noside noside;
1121 {
1122 register enum exp_opcode op;
1123 register int pc;
1124 register value val;
1125 struct symbol *var;
1126
1127 pc = (*pos);
1128 op = exp->elts[pc].opcode;
1129
1130 switch (op)
1131 {
1132 case OP_VAR_VALUE:
1133 var = exp->elts[pc + 2].symbol;
1134 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY
1135 && CAST_IS_CONVERSION)
1136 {
1137 (*pos) += 4;
1138 val =
1139 locate_var_value
1140 (var, block_innermost_frame (exp->elts[pc + 1].block));
1141 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
1142 val);
1143 }
1144 /* FALLTHROUGH */
1145
1146 default:
1147 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1148 }
1149 }
1150
1151 /* Evaluate a subexpression of EXP, at index *POS,
1152 and return a value for the size of that subexpression.
1153 Advance *POS over the subexpression. */
1154
1155 static value
1156 evaluate_subexp_for_sizeof (exp, pos)
1157 register struct expression *exp;
1158 register int *pos;
1159 {
1160 enum exp_opcode op;
1161 register int pc;
1162 value val;
1163
1164 pc = (*pos);
1165 op = exp->elts[pc].opcode;
1166
1167 switch (op)
1168 {
1169 /* This case is handled specially
1170 so that we avoid creating a value for the result type.
1171 If the result type is very big, it's desirable not to
1172 create a value unnecessarily. */
1173 case UNOP_IND:
1174 (*pos)++;
1175 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1176 return value_from_longest (builtin_type_int, (LONGEST)
1177 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1178
1179 case UNOP_MEMVAL:
1180 (*pos) += 3;
1181 return value_from_longest (builtin_type_int,
1182 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1183
1184 case OP_VAR_VALUE:
1185 (*pos) += 4;
1186 return
1187 value_from_longest
1188 (builtin_type_int,
1189 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 2].symbol)));
1190
1191 default:
1192 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1193 return value_from_longest (builtin_type_int,
1194 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1195 }
1196 }
1197
1198 /* Parse a type expression in the string [P..P+LENGTH). */
1199
1200 struct type *
1201 parse_and_eval_type (p, length)
1202 char *p;
1203 int length;
1204 {
1205 char *tmp = (char *)alloca (length + 4);
1206 struct expression *expr;
1207 tmp[0] = '(';
1208 memcpy (tmp+1, p, length);
1209 tmp[length+1] = ')';
1210 tmp[length+2] = '0';
1211 tmp[length+3] = '\0';
1212 expr = parse_expression (tmp);
1213 if (expr->elts[0].opcode != UNOP_CAST)
1214 error ("Internal error in eval_type.");
1215 return expr->elts[1].type;
1216 }
This page took 0.053349 seconds and 4 git commands to generate.