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