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