* gdb.t06/twice.exp: Don't care about the file name GDB prints.
[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:
76a0ffb4 546 case BINOP_MOD:
bd5635a1
RP
547 case BINOP_LSH:
548 case BINOP_RSH:
e58de8a2
FF
549 case BINOP_BITWISE_AND:
550 case BINOP_BITWISE_IOR:
551 case BINOP_BITWISE_XOR:
bd5635a1
RP
552 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
553 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
554 if (noside == EVAL_SKIP)
555 goto nosideret;
556 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 557 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
558 else
559 if (noside == EVAL_AVOID_SIDE_EFFECTS
76a0ffb4 560 && (op == BINOP_DIV || op == BINOP_REM || op == BINOP_MOD))
bd5635a1
RP
561 return value_zero (VALUE_TYPE (arg1), not_lval);
562 else
563 return value_binop (arg1, arg2, op);
564
565 case BINOP_SUBSCRIPT:
566 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
567 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
568 if (noside == EVAL_SKIP)
569 goto nosideret;
570 if (noside == EVAL_AVOID_SIDE_EFFECTS)
35fcebce
PB
571 {
572 /* If the user attempts to subscript something that has no target
573 type (like a plain int variable for example), then report this
574 as an error. */
575
576 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
577 if (type)
578 return value_zero (type, VALUE_LVAL (arg1));
579 else
580 error ("cannot subscript something of type `%s'",
581 TYPE_NAME (VALUE_TYPE (arg1)));
582 }
bd5635a1
RP
583
584 if (binop_user_defined_p (op, arg1, arg2))
2ccb3837 585 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
586 else
587 return value_subscript (arg1, arg2);
588
54bbbfb4
FF
589 case MULTI_SUBSCRIPT:
590 (*pos) += 2;
591 nargs = longest_to_int (exp->elts[pc + 1].longconst);
592 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
593 while (nargs-- > 0)
594 {
595 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
596 /* FIXME: EVAL_SKIP handling may not be correct. */
597 if (noside == EVAL_SKIP)
598 {
599 if (nargs > 0)
600 {
601 continue;
602 }
603 else
604 {
605 goto nosideret;
606 }
607 }
608 /* FIXME: EVAL_AVOID_SIDE_EFFECTS handling may not be correct. */
609 if (noside == EVAL_AVOID_SIDE_EFFECTS)
610 {
611 /* If the user attempts to subscript something that has no target
612 type (like a plain int variable for example), then report this
613 as an error. */
614
615 type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
616 if (type != NULL)
617 {
618 arg1 = value_zero (type, VALUE_LVAL (arg1));
619 noside = EVAL_SKIP;
620 continue;
621 }
622 else
623 {
624 error ("cannot subscript something of type `%s'",
625 TYPE_NAME (VALUE_TYPE (arg1)));
626 }
627 }
628
629 if (binop_user_defined_p (op, arg1, arg2))
630 {
631 arg1 = value_x_binop (arg1, arg2, op, OP_NULL);
632 }
633 else
634 {
635 arg1 = value_subscript (arg1, arg2);
636 }
637 }
638 return (arg1);
639
e58de8a2 640 case BINOP_LOGICAL_AND:
bd5635a1
RP
641 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
642 if (noside == EVAL_SKIP)
643 {
644 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
645 goto nosideret;
646 }
647
648 oldpos = *pos;
649 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
650 *pos = oldpos;
651
652 if (binop_user_defined_p (op, arg1, arg2))
653 {
654 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2ccb3837 655 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
656 }
657 else
658 {
e58de8a2 659 tem = value_logical_not (arg1);
bd5635a1
RP
660 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
661 (tem ? EVAL_SKIP : noside));
2ccb3837 662 return value_from_longest (builtin_type_int,
e58de8a2 663 (LONGEST) (!tem && !value_logical_not (arg2)));
bd5635a1
RP
664 }
665
e58de8a2 666 case BINOP_LOGICAL_OR:
bd5635a1
RP
667 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
668 if (noside == EVAL_SKIP)
669 {
670 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
671 goto nosideret;
672 }
673
674 oldpos = *pos;
675 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
676 *pos = oldpos;
677
678 if (binop_user_defined_p (op, arg1, arg2))
679 {
680 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2ccb3837 681 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
682 }
683 else
684 {
e58de8a2 685 tem = value_logical_not (arg1);
bd5635a1
RP
686 arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
687 (!tem ? EVAL_SKIP : noside));
2ccb3837 688 return value_from_longest (builtin_type_int,
e58de8a2 689 (LONGEST) (!tem || !value_logical_not (arg2)));
bd5635a1
RP
690 }
691
692 case BINOP_EQUAL:
693 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
694 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
695 if (noside == EVAL_SKIP)
696 goto nosideret;
697 if (binop_user_defined_p (op, arg1, arg2))
698 {
2ccb3837 699 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
700 }
701 else
702 {
703 tem = value_equal (arg1, arg2);
2ccb3837 704 return value_from_longest (builtin_type_int, (LONGEST) tem);
bd5635a1
RP
705 }
706
707 case BINOP_NOTEQUAL:
708 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
709 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
710 if (noside == EVAL_SKIP)
711 goto nosideret;
712 if (binop_user_defined_p (op, arg1, arg2))
713 {
2ccb3837 714 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
715 }
716 else
717 {
718 tem = value_equal (arg1, arg2);
2ccb3837 719 return value_from_longest (builtin_type_int, (LONGEST) ! tem);
bd5635a1
RP
720 }
721
722 case BINOP_LESS:
723 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
724 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
725 if (noside == EVAL_SKIP)
726 goto nosideret;
727 if (binop_user_defined_p (op, arg1, arg2))
728 {
2ccb3837 729 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
730 }
731 else
732 {
733 tem = value_less (arg1, arg2);
2ccb3837 734 return value_from_longest (builtin_type_int, (LONGEST) tem);
bd5635a1
RP
735 }
736
737 case BINOP_GTR:
738 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
739 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
740 if (noside == EVAL_SKIP)
741 goto nosideret;
742 if (binop_user_defined_p (op, arg1, arg2))
743 {
2ccb3837 744 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
745 }
746 else
747 {
748 tem = value_less (arg2, arg1);
2ccb3837 749 return value_from_longest (builtin_type_int, (LONGEST) tem);
bd5635a1
RP
750 }
751
752 case BINOP_GEQ:
753 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
754 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
755 if (noside == EVAL_SKIP)
756 goto nosideret;
757 if (binop_user_defined_p (op, arg1, arg2))
758 {
2ccb3837 759 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
760 }
761 else
762 {
8f86a4e4
JG
763 tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
764 return value_from_longest (builtin_type_int, (LONGEST) tem);
bd5635a1
RP
765 }
766
767 case BINOP_LEQ:
768 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
769 arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
770 if (noside == EVAL_SKIP)
771 goto nosideret;
772 if (binop_user_defined_p (op, arg1, arg2))
773 {
2ccb3837 774 return value_x_binop (arg1, arg2, op, OP_NULL);
bd5635a1
RP
775 }
776 else
777 {
8f86a4e4
JG
778 tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
779 return value_from_longest (builtin_type_int, (LONGEST) tem);
bd5635a1
RP
780 }
781
782 case BINOP_REPEAT:
783 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
784 arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
785 if (noside == EVAL_SKIP)
786 goto nosideret;
787 if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
788 error ("Non-integral right operand for \"@\" operator.");
789 if (noside == EVAL_AVOID_SIDE_EFFECTS)
790 return allocate_repeat_value (VALUE_TYPE (arg1),
2ccb3837 791 longest_to_int (value_as_long (arg2)));
bd5635a1 792 else
2ccb3837 793 return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
bd5635a1
RP
794
795 case BINOP_COMMA:
796 evaluate_subexp (NULL_TYPE, exp, pos, noside);
797 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
798
799 case UNOP_NEG:
800 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
801 if (noside == EVAL_SKIP)
802 goto nosideret;
803 if (unop_user_defined_p (op, arg1))
804 return value_x_unop (arg1, op);
805 else
806 return value_neg (arg1);
807
e58de8a2 808 case UNOP_COMPLEMENT:
5f00ca54
JK
809 /* C++: check for and handle destructor names. */
810 op = exp->elts[*pos].opcode;
811
bd5635a1
RP
812 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
813 if (noside == EVAL_SKIP)
814 goto nosideret;
e58de8a2
FF
815 if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
816 return value_x_unop (arg1, UNOP_COMPLEMENT);
bd5635a1 817 else
e58de8a2 818 return value_complement (arg1);
bd5635a1 819
e58de8a2 820 case UNOP_LOGICAL_NOT:
bd5635a1
RP
821 arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
822 if (noside == EVAL_SKIP)
823 goto nosideret;
824 if (unop_user_defined_p (op, arg1))
825 return value_x_unop (arg1, op);
826 else
2ccb3837 827 return value_from_longest (builtin_type_int,
e58de8a2 828 (LONGEST) value_logical_not (arg1));
bd5635a1
RP
829
830 case UNOP_IND:
831 if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
832 expect_type = TYPE_TARGET_TYPE (expect_type);
833 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
834 if (noside == EVAL_SKIP)
835 goto nosideret;
836 if (noside == EVAL_AVOID_SIDE_EFFECTS)
837 {
838 if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
839 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
840 /* In C you can dereference an array to get the 1st elt. */
841 || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
842 )
843 return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
844 lval_memory);
845 else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
846 /* GDB allows dereferencing an int. */
847 return value_zero (builtin_type_int, lval_memory);
848 else
849 error ("Attempt to take contents of a non-pointer value.");
850 }
851 return value_ind (arg1);
852
853 case UNOP_ADDR:
854 /* C++: check for and handle pointer to members. */
855
856 op = exp->elts[*pos].opcode;
857
858 if (noside == EVAL_SKIP)
859 {
860 if (op == OP_SCOPE)
861 {
a8a69e63
FF
862 int temm = longest_to_int (exp->elts[pc+3].longconst);
863 (*pos) += 3 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
bd5635a1
RP
864 }
865 else
866 evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
867 goto nosideret;
868 }
869
01be6913 870 return evaluate_subexp_for_address (exp, pos, noside);
bd5635a1
RP
871
872 case UNOP_SIZEOF:
873 if (noside == EVAL_SKIP)
874 {
875 evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
876 goto nosideret;
877 }
878 return evaluate_subexp_for_sizeof (exp, pos);
879
880 case UNOP_CAST:
881 (*pos) += 2;
882 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
883 if (noside == EVAL_SKIP)
884 goto nosideret;
885 return value_cast (exp->elts[pc + 1].type, arg1);
886
887 case UNOP_MEMVAL:
888 (*pos) += 2;
889 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
890 if (noside == EVAL_SKIP)
891 goto nosideret;
892 if (noside == EVAL_AVOID_SIDE_EFFECTS)
893 return value_zero (exp->elts[pc + 1].type, lval_memory);
894 else
895 return value_at_lazy (exp->elts[pc + 1].type,
2ccb3837 896 value_as_pointer (arg1));
bd5635a1
RP
897
898 case UNOP_PREINCREMENT:
899 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
900 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
901 return arg1;
902 else if (unop_user_defined_p (op, arg1))
903 {
904 return value_x_unop (arg1, op);
905 }
906 else
907 {
2ccb3837 908 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
909 (LONGEST) 1));
910 return value_assign (arg1, arg2);
911 }
912
913 case UNOP_PREDECREMENT:
914 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
915 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
916 return arg1;
917 else if (unop_user_defined_p (op, arg1))
918 {
919 return value_x_unop (arg1, op);
920 }
921 else
922 {
2ccb3837 923 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
924 (LONGEST) 1));
925 return value_assign (arg1, arg2);
926 }
927
928 case UNOP_POSTINCREMENT:
929 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
930 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
931 return arg1;
932 else if (unop_user_defined_p (op, arg1))
933 {
934 return value_x_unop (arg1, op);
935 }
936 else
937 {
2ccb3837 938 arg2 = value_add (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
939 (LONGEST) 1));
940 value_assign (arg1, arg2);
941 return arg1;
942 }
943
944 case UNOP_POSTDECREMENT:
945 arg1 = evaluate_subexp (expect_type, exp, pos, noside);
946 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
947 return arg1;
948 else if (unop_user_defined_p (op, arg1))
949 {
950 return value_x_unop (arg1, op);
951 }
952 else
953 {
2ccb3837 954 arg2 = value_sub (arg1, value_from_longest (builtin_type_char,
bd5635a1
RP
955 (LONGEST) 1));
956 value_assign (arg1, arg2);
957 return arg1;
958 }
959
960 case OP_THIS:
961 (*pos) += 1;
962 return value_of_this (1);
963
964 default:
965 error ("internal error: I do not know how to evaluate what you gave me");
966 }
967
968 nosideret:
2ccb3837 969 return value_from_longest (builtin_type_long, (LONGEST) 1);
bd5635a1
RP
970}
971\f
972/* Evaluate a subexpression of EXP, at index *POS,
973 and return the address of that subexpression.
974 Advance *POS over the subexpression.
975 If the subexpression isn't an lvalue, get an error.
976 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
977 then only the type of the result need be correct. */
978
979static value
980evaluate_subexp_for_address (exp, pos, noside)
981 register struct expression *exp;
982 register int *pos;
983 enum noside noside;
984{
985 enum exp_opcode op;
986 register int pc;
e17960fb 987 struct symbol *var;
bd5635a1
RP
988
989 pc = (*pos);
990 op = exp->elts[pc].opcode;
991
992 switch (op)
993 {
994 case UNOP_IND:
995 (*pos)++;
996 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
997
998 case UNOP_MEMVAL:
999 (*pos) += 3;
1000 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
1001 evaluate_subexp (NULL_TYPE, exp, pos, noside));
1002
1003 case OP_VAR_VALUE:
e17960fb
JG
1004 var = exp->elts[pc + 1].symbol;
1005
1006 /* C++: The "address" of a reference should yield the address
1007 * of the object pointed to. Let value_addr() deal with it. */
1008 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
1009 goto default_case;
1010
bd5635a1
RP
1011 (*pos) += 3;
1012 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1013 {
1014 struct type *type =
e17960fb
JG
1015 lookup_pointer_type (SYMBOL_TYPE (var));
1016 enum address_class sym_class = SYMBOL_CLASS (var);
bd5635a1
RP
1017
1018 if (sym_class == LOC_CONST
1019 || sym_class == LOC_CONST_BYTES
1020 || sym_class == LOC_REGISTER
1021 || sym_class == LOC_REGPARM)
1022 error ("Attempt to take address of register or constant.");
1023
1024 return
1025 value_zero (type, not_lval);
1026 }
1027 else
e17960fb 1028 return locate_var_value (var, (FRAME) 0);
bd5635a1
RP
1029
1030 default:
e17960fb 1031 default_case:
bd5635a1
RP
1032 if (noside == EVAL_AVOID_SIDE_EFFECTS)
1033 {
1034 value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1035 if (VALUE_LVAL (x) == lval_memory)
0a5d35ed 1036 return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
bd5635a1
RP
1037 not_lval);
1038 else
1039 error ("Attempt to take address of non-lval");
1040 }
1041 return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
1042 }
1043}
1044
1045/* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
fb6e675f
FF
1046 When used in contexts where arrays will be coerced anyway, this is
1047 equivalent to `evaluate_subexp' but much faster because it avoids
1048 actually fetching array contents.
1049
1050 Note that we currently only do the coercion for C expressions, where
1051 arrays are zero based and the coercion is correct. For other languages,
1052 with nonzero based arrays, coercion loses. Use CAST_IS_CONVERSION
1053 to decide if coercion is appropriate.
1054
1055 */
bd5635a1
RP
1056
1057static value
1058evaluate_subexp_with_coercion (exp, pos, noside)
1059 register struct expression *exp;
1060 register int *pos;
1061 enum noside noside;
1062{
1063 register enum exp_opcode op;
1064 register int pc;
1065 register value val;
e17960fb 1066 struct symbol *var;
bd5635a1
RP
1067
1068 pc = (*pos);
1069 op = exp->elts[pc].opcode;
1070
1071 switch (op)
1072 {
1073 case OP_VAR_VALUE:
e17960fb 1074 var = exp->elts[pc + 1].symbol;
fb6e675f
FF
1075 if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY
1076 && CAST_IS_CONVERSION)
bd5635a1
RP
1077 {
1078 (*pos) += 3;
e17960fb
JG
1079 val = locate_var_value (var, (FRAME) 0);
1080 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
bd5635a1
RP
1081 val);
1082 }
1083 default:
1084 return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1085 }
1086}
1087
1088/* Evaluate a subexpression of EXP, at index *POS,
1089 and return a value for the size of that subexpression.
1090 Advance *POS over the subexpression. */
1091
1092static value
1093evaluate_subexp_for_sizeof (exp, pos)
1094 register struct expression *exp;
1095 register int *pos;
1096{
1097 enum exp_opcode op;
1098 register int pc;
1099 value val;
1100
1101 pc = (*pos);
1102 op = exp->elts[pc].opcode;
1103
1104 switch (op)
1105 {
1106 /* This case is handled specially
1107 so that we avoid creating a value for the result type.
1108 If the result type is very big, it's desirable not to
1109 create a value unnecessarily. */
1110 case UNOP_IND:
1111 (*pos)++;
1112 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2ccb3837 1113 return value_from_longest (builtin_type_int, (LONGEST)
bd5635a1
RP
1114 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1115
1116 case UNOP_MEMVAL:
1117 (*pos) += 3;
2ccb3837 1118 return value_from_longest (builtin_type_int,
bd5635a1
RP
1119 (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1120
1121 case OP_VAR_VALUE:
1122 (*pos) += 3;
2ccb3837 1123 return value_from_longest (builtin_type_int,
bd5635a1
RP
1124 (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1125
1126 default:
1127 val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2ccb3837 1128 return value_from_longest (builtin_type_int,
bd5635a1
RP
1129 (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1130 }
1131}
0a5d35ed
SG
1132
1133/* Parse a type expression in the string [P..P+LENGTH). */
1134
1135struct type *
1136parse_and_eval_type (p, length)
1137 char *p;
1138 int length;
1139{
1140 char *tmp = (char *)alloca (length + 4);
1141 struct expression *expr;
1142 tmp[0] = '(';
35fcebce 1143 memcpy (tmp+1, p, length);
0a5d35ed
SG
1144 tmp[length+1] = ')';
1145 tmp[length+2] = '0';
1146 tmp[length+3] = '\0';
1147 expr = parse_expression (tmp);
1148 if (expr->elts[0].opcode != UNOP_CAST)
1149 error ("Internal error in eval_type.");
1150 return expr->elts[1].type;
1151}
This page took 0.143674 seconds and 4 git commands to generate.