gdb-2.5.3
[binutils-gdb.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20
21 #include "defs.h"
22 #include "initialize.h"
23 #include "symtab.h"
24 #include "value.h"
25 #include "expression.h"
26
27 START_FILE
28 \f
29 /* Parse the string EXP as a C expression, evaluate it,
30 and return the result as a number. */
31
32 CORE_ADDR
33 parse_and_eval_address (exp)
34 char *exp;
35 {
36 struct expression *expr = parse_c_expression (exp);
37 register CORE_ADDR addr;
38 register struct cleanup *old_chain
39 = make_cleanup (free_current_contents, &expr);
40
41 addr = value_as_long (evaluate_expression (expr));
42 do_cleanups (old_chain);
43 return addr;
44 }
45
46 /* Like parse_and_eval_address but takes a pointer to a char * variable
47 and advanced that variable across the characters parsed. */
48
49 CORE_ADDR
50 parse_and_eval_address_1 (expptr)
51 char **expptr;
52 {
53 struct expression *expr = parse_c_1 (expptr, 0, 0);
54 register CORE_ADDR addr;
55 register struct cleanup *old_chain
56 = make_cleanup (free_current_contents, &expr);
57
58 addr = value_as_long (evaluate_expression (expr));
59 do_cleanups (old_chain);
60 return addr;
61 }
62
63 value
64 parse_and_eval (exp)
65 char *exp;
66 {
67 struct expression *expr = parse_c_expression (exp);
68 register value val;
69 register struct cleanup *old_chain
70 = make_cleanup (free_current_contents, &expr);
71
72 val = evaluate_expression (expr);
73 do_cleanups (old_chain);
74 return val;
75 }
76
77 /* Parse up to a comma (or to a closeparen)
78 in the string EXPP as an expression, evaluate it, and return the value.
79 EXPP is advanced to point to the comma. */
80
81 value
82 parse_to_comma_and_eval (expp)
83 char **expp;
84 {
85 struct expression *expr = parse_c_1 (expp, 0, 1);
86 register value val;
87 register struct cleanup *old_chain
88 = make_cleanup (free_current_contents, &expr);
89
90 val = evaluate_expression (expr);
91 do_cleanups (old_chain);
92 return val;
93 }
94 \f
95 /* Evaluate an expression in internal prefix form
96 such as is constructed by expread.y.
97
98 See expression.h for info on the format of an expression. */
99
100 static value evaluate_subexp ();
101 static value evaluate_subexp_for_address ();
102 static value evaluate_subexp_for_sizeof ();
103 static value evaluate_subexp_with_coercion ();
104
105 /* Values of NOSIDE argument to eval_subexp. */
106 enum noside
107 { EVAL_NORMAL,
108 EVAL_SKIP,
109 EVAL_AVOID_SIDE_EFFECTS,
110 };
111
112 value
113 evaluate_expression (exp)
114 struct expression *exp;
115 {
116 int pc = 0;
117 return evaluate_subexp (exp, &pc, EVAL_NORMAL);
118 }
119
120 /* Evaluate an expression, avoiding all memory references
121 and getting a value whose type alone is correct. */
122
123 value
124 evaluate_type (exp)
125 struct expression *exp;
126 {
127 int pc = 0;
128 return evaluate_subexp (exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
129 }
130
131 static value
132 evaluate_subexp (exp, pos, noside)
133 register struct expression *exp;
134 register int *pos;
135 enum noside noside;
136 {
137 enum exp_opcode op;
138 int tem;
139 register int pc, pc2, *oldpos;
140 register value arg1, arg2, arg3;
141 int nargs;
142 value *argvec;
143
144 pc = (*pos)++;
145 op = exp->elts[pc].opcode;
146
147 switch (op)
148 {
149 case OP_SCOPE:
150 tem = strlen (&exp->elts[pc + 2].string);
151 (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
152 return value_static_field (exp->elts[pc + 1].type,
153 &exp->elts[pc + 2].string, -1);
154
155 case OP_LONG:
156 (*pos) += 3;
157 return value_from_long (exp->elts[pc + 1].type,
158 exp->elts[pc + 2].longconst);
159
160 case OP_DOUBLE:
161 (*pos) += 3;
162 return value_from_double (exp->elts[pc + 1].type,
163 exp->elts[pc + 2].doubleconst);
164
165 case OP_VAR_VALUE:
166 (*pos) += 2;
167 if (noside == EVAL_SKIP)
168 goto nosideret;
169 return value_of_variable (exp->elts[pc + 1].symbol);
170
171 case OP_LAST:
172 (*pos) += 2;
173 return access_value_history (exp->elts[pc + 1].longconst);
174
175 case OP_REGISTER:
176 (*pos) += 2;
177 return value_of_register (exp->elts[pc + 1].longconst);
178
179 case OP_INTERNALVAR:
180 (*pos) += 2;
181 return value_of_internalvar (exp->elts[pc + 1].internalvar);
182
183 case OP_STRING:
184 tem = strlen (&exp->elts[pc + 1].string);
185 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
186 if (noside == EVAL_SKIP)
187 goto nosideret;
188 return value_string (&exp->elts[pc + 1].string, tem);
189
190 case TERNOP_COND:
191 /* Skip third and second args to evaluate the first one. */
192 arg1 = evaluate_subexp (exp, pos, noside);
193 if (value_zerop (arg1))
194 {
195 evaluate_subexp (exp, pos, EVAL_SKIP);
196 return evaluate_subexp (exp, pos, noside);
197 }
198 else
199 {
200 arg2 = evaluate_subexp (exp, pos, noside);
201 evaluate_subexp (exp, pos, EVAL_SKIP);
202 return arg2;
203 }
204
205 case OP_FUNCALL:
206 (*pos) += 2;
207 op = exp->elts[*pos].opcode;
208 if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
209 {
210 int fnptr;
211 int tem2;
212
213 nargs = exp->elts[pc + 1].longconst + 1;
214 /* First, evaluate the structure into arg2 */
215 pc2 = (*pos)++;
216
217 if (noside == EVAL_SKIP)
218 goto nosideret;
219
220 if (op == STRUCTOP_MEMBER)
221 {
222 arg2 = evaluate_subexp_for_address (exp, pos, noside);
223 }
224 else
225 {
226 arg2 = evaluate_subexp (exp, pos, noside);
227 }
228
229 /* If the function is a virtual function, then the
230 aggregate value (providing the structure) plays
231 its part by providing the vtable. Otherwise,
232 it is just along for the ride: call the function
233 directly. */
234
235 arg1 = evaluate_subexp (exp, pos, noside);
236
237 fnptr = value_as_long (arg1);
238 if (fnptr < 128)
239 {
240 struct type *basetype;
241 int i, j;
242 basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
243 basetype = TYPE_VPTR_BASETYPE (basetype);
244 for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
245 {
246 struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
247 /* If one is virtual, then all are virtual. */
248 if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
249 for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
250 if (TYPE_FN_FIELD_VOFFSET (f, j) == fnptr)
251 {
252 value vtbl;
253 value base = value_ind (arg2);
254 struct type *fntype = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
255
256 if (TYPE_VPTR_FIELDNO (basetype) < 0)
257 TYPE_VPTR_FIELDNO (basetype)
258 = fill_in_vptr_fieldno (basetype);
259
260 VALUE_TYPE (base) = basetype;
261 vtbl = value_field (base, TYPE_VPTR_FIELDNO (basetype));
262 VALUE_TYPE (vtbl) = lookup_pointer_type (fntype);
263 VALUE_TYPE (arg1) = builtin_type_int;
264 arg1 = value_subscript (vtbl, arg1);
265 VALUE_TYPE (arg1) = fntype;
266 goto got_it;
267 }
268 }
269 if (i < 0)
270 error ("virtual function at index %d not found", fnptr);
271 }
272 else
273 {
274 VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
275 }
276 got_it:
277
278 /* Now, say which argument to start evaluating from */
279 tem = 2;
280 }
281 else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
282 {
283 /* Hair for method invocations */
284 int tem2;
285
286 nargs = exp->elts[pc + 1].longconst + 1;
287 /* First, evaluate the structure into arg2 */
288 pc2 = (*pos)++;
289 tem2 = strlen (&exp->elts[pc2 + 1].string);
290 *pos += 2 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
291 if (noside == EVAL_SKIP)
292 goto nosideret;
293
294 if (op == STRUCTOP_STRUCT)
295 {
296 arg2 = evaluate_subexp_for_address (exp, pos, noside);
297 }
298 else
299 {
300 arg2 = evaluate_subexp (exp, pos, noside);
301 }
302 /* Now, say which argument to start evaluating from */
303 tem = 2;
304 }
305 else
306 {
307 nargs = exp->elts[pc + 1].longconst;
308 tem = 0;
309 }
310 argvec = (value *) alloca (sizeof (value) * (nargs + 2));
311 for (; tem <= nargs; tem++)
312 /* Ensure that array expressions are coerced into pointer objects. */
313 argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
314
315 /* signal end of arglist */
316 argvec[tem] = 0;
317
318 if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
319 {
320 argvec[1] = arg2;
321 argvec[0] =
322 value_struct_elt (arg2, argvec+1, &exp->elts[pc2 + 1].string,
323 op == STRUCTOP_STRUCT
324 ? "structure" : "structure pointer");
325 }
326 else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
327 {
328 argvec[1] = arg2;
329 argvec[0] = arg1;
330 }
331
332 if (noside == EVAL_SKIP)
333 goto nosideret;
334 if (noside == EVAL_AVOID_SIDE_EFFECTS)
335 return allocate_value (TYPE_TARGET_TYPE (VALUE_TYPE (argvec[0])));
336 return call_function (argvec[0], nargs, argvec + 1);
337
338 case STRUCTOP_STRUCT:
339 tem = strlen (&exp->elts[pc + 1].string);
340 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
341 arg1 = evaluate_subexp (exp, pos, noside);
342 if (noside == EVAL_SKIP)
343 goto nosideret;
344 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string,
345 "structure");
346
347 case STRUCTOP_PTR:
348 tem = strlen (&exp->elts[pc + 1].string);
349 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
350 arg1 = evaluate_subexp (exp, pos, noside);
351 if (noside == EVAL_SKIP)
352 goto nosideret;
353 return value_struct_elt (arg1, 0, &exp->elts[pc + 1].string,
354 "structure pointer");
355
356 case STRUCTOP_MEMBER:
357 arg1 = evaluate_subexp_for_address (exp, pos, noside);
358 arg2 = evaluate_subexp (exp, pos, noside);
359 if (noside == EVAL_SKIP)
360 goto nosideret;
361 /* Now, convert these values to an address.
362 @@ We do not know what type we are looking for,
363 @@ so we must assume that the value requested is a
364 @@ member address (as opposed to a member function address). */
365 arg3 = value_from_long (builtin_type_long,
366 value_as_long (arg1) + value_as_long (arg2));
367 VALUE_TYPE (arg3) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)));
368 return value_ind (arg3);
369
370 case STRUCTOP_MPTR:
371 arg1 = evaluate_subexp (exp, pos, noside);
372 arg2 = evaluate_subexp (exp, pos, noside);
373 if (noside == EVAL_SKIP)
374 goto nosideret;
375 /* Now, convert these values to an address.
376 @@ We do not know what type we are looking for,
377 @@ so we must assume that the value requested is a
378 @@ member address (as opposed to a member function address). */
379 arg3 = value_from_long (builtin_type_long,
380 value_as_long (arg1) + value_as_long (arg2));
381 VALUE_TYPE (arg3) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg2)));
382 return value_ind (arg3);
383
384 case BINOP_ASSIGN:
385 arg1 = evaluate_subexp (exp, pos, noside);
386 arg2 = evaluate_subexp (exp, pos, noside);
387 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
388 return arg1;
389 if (binop_must_be_user_defined (arg1, arg2))
390 return value_x_binop (arg1, arg2, op, 0);
391 else
392 return value_assign (arg1, arg2);
393
394 case BINOP_ASSIGN_MODIFY:
395 (*pos) += 2;
396 arg1 = evaluate_subexp (exp, pos, noside);
397 arg2 = evaluate_subexp (exp, pos, noside);
398 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
399 return arg1;
400 op = exp->elts[pc + 1].opcode;
401 if (binop_must_be_user_defined (arg1, arg2))
402 return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
403 else if (op == BINOP_ADD)
404 arg2 = value_add (arg1, arg2);
405 else if (op == BINOP_SUB)
406 arg2 = value_sub (arg1, arg2);
407 else
408 arg2 = value_binop (arg1, arg2, op);
409 return value_assign (arg1, arg2);
410
411 case BINOP_ADD:
412 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
413 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
414 if (noside == EVAL_SKIP)
415 goto nosideret;
416 if (binop_must_be_user_defined (arg1, arg2))
417 return value_x_binop (arg1, arg2, op, 0);
418 else
419 return value_add (arg1, arg2);
420
421 case BINOP_SUB:
422 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
423 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
424 if (noside == EVAL_SKIP)
425 goto nosideret;
426 if (binop_must_be_user_defined (arg1, arg2))
427 return value_x_binop (arg1, arg2, op, 0);
428 else
429 return value_sub (arg1, arg2);
430
431 case BINOP_MUL:
432 case BINOP_DIV:
433 case BINOP_REM:
434 case BINOP_LSH:
435 case BINOP_RSH:
436 case BINOP_LOGAND:
437 case BINOP_LOGIOR:
438 case BINOP_LOGXOR:
439 arg1 = evaluate_subexp (exp, pos, noside);
440 arg2 = evaluate_subexp (exp, pos, noside);
441 if (noside == EVAL_SKIP)
442 goto nosideret;
443 if (binop_must_be_user_defined (arg1, arg2))
444 return value_x_binop (arg1, arg2, op, 0);
445 else
446 return value_binop (arg1, arg2, op);
447
448 case BINOP_SUBSCRIPT:
449 arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
450 arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
451 if (noside == EVAL_SKIP)
452 goto nosideret;
453 if (binop_must_be_user_defined (arg1, arg2))
454 return value_x_binop (arg1, arg2, op, 0);
455 else
456 return value_subscript (arg1, arg2, op);
457
458 case BINOP_AND:
459 arg1 = evaluate_subexp (exp, pos, noside);
460 if (binop_must_be_user_defined (arg1, arg2))
461 {
462 arg2 = evaluate_subexp (exp, pos, noside);
463 return value_x_binop (arg1, arg2, op, 0);
464 }
465 else
466 {
467 tem = value_zerop (arg1);
468 arg2 = evaluate_subexp (exp, pos,
469 (tem ? EVAL_SKIP : noside));
470 return value_from_long (builtin_type_int,
471 !tem && !value_zerop (arg2));
472 }
473
474 case BINOP_OR:
475 arg1 = evaluate_subexp (exp, pos, noside);
476 if (binop_must_be_user_defined (arg1, arg2))
477 {
478 arg2 = evaluate_subexp (exp, pos, noside);
479 return value_x_binop (arg1, arg2, op, 0);
480 }
481 else
482 {
483 tem = value_zerop (arg1);
484 arg2 = evaluate_subexp (exp, pos,
485 (!tem ? EVAL_SKIP : noside));
486 return value_from_long (builtin_type_int,
487 !tem || !value_zerop (arg2));
488 }
489
490 case BINOP_EQUAL:
491 arg1 = evaluate_subexp (exp, pos, noside);
492 arg2 = evaluate_subexp (exp, pos, noside);
493 if (noside == EVAL_SKIP)
494 goto nosideret;
495 if (binop_must_be_user_defined (arg1, arg2))
496 {
497 return value_x_binop (arg1, arg2, op, 0);
498 }
499 else
500 {
501 tem = value_equal (arg1, arg2);
502 return value_from_long (builtin_type_int, tem);
503 }
504
505 case BINOP_NOTEQUAL:
506 arg1 = evaluate_subexp (exp, pos, noside);
507 arg2 = evaluate_subexp (exp, pos, noside);
508 if (noside == EVAL_SKIP)
509 goto nosideret;
510 if (binop_must_be_user_defined (arg1, arg2))
511 {
512 return value_x_binop (arg1, arg2, op, 0);
513 }
514 else
515 {
516 tem = value_equal (arg1, arg2);
517 return value_from_long (builtin_type_int, ! tem);
518 }
519
520 case BINOP_LESS:
521 arg1 = evaluate_subexp (exp, pos, noside);
522 arg2 = evaluate_subexp (exp, pos, noside);
523 if (noside == EVAL_SKIP)
524 goto nosideret;
525 if (binop_must_be_user_defined (arg1, arg2))
526 {
527 return value_x_binop (arg1, arg2, op, 0);
528 }
529 else
530 {
531 tem = value_less (arg1, arg2);
532 return value_from_long (builtin_type_int, tem);
533 }
534
535 case BINOP_GTR:
536 arg1 = evaluate_subexp (exp, pos, noside);
537 arg2 = evaluate_subexp (exp, pos, noside);
538 if (noside == EVAL_SKIP)
539 goto nosideret;
540 if (binop_must_be_user_defined (arg1, arg2))
541 {
542 return value_x_binop (arg1, arg2, op, 0);
543 }
544 else
545 {
546 tem = value_less (arg2, arg1);
547 return value_from_long (builtin_type_int, tem);
548 }
549
550 case BINOP_GEQ:
551 arg1 = evaluate_subexp (exp, pos, noside);
552 arg2 = evaluate_subexp (exp, pos, noside);
553 if (noside == EVAL_SKIP)
554 goto nosideret;
555 if (binop_must_be_user_defined (arg1, arg2))
556 {
557 return value_x_binop (arg1, arg2, op, 0);
558 }
559 else
560 {
561 tem = value_less (arg1, arg2);
562 return value_from_long (builtin_type_int, ! tem);
563 }
564
565 case BINOP_LEQ:
566 arg1 = evaluate_subexp (exp, pos, noside);
567 arg2 = evaluate_subexp (exp, pos, noside);
568 if (noside == EVAL_SKIP)
569 goto nosideret;
570 if (binop_must_be_user_defined (arg1, arg2))
571 {
572 return value_x_binop (arg1, arg2, op, 0);
573 }
574 else
575 {
576 tem = value_less (arg2, arg1);
577 return value_from_long (builtin_type_int, ! tem);
578 }
579
580 case BINOP_REPEAT:
581 arg1 = evaluate_subexp (exp, pos, noside);
582 arg2 = evaluate_subexp (exp, pos, noside);
583 if (noside == EVAL_SKIP)
584 goto nosideret;
585 return value_repeat (arg1, value_as_long (arg2));
586
587 case BINOP_COMMA:
588 evaluate_subexp (exp, pos, noside);
589 return evaluate_subexp (exp, pos, noside);
590
591 case UNOP_NEG:
592 arg1 = evaluate_subexp (exp, pos, noside);
593 if (noside == EVAL_SKIP)
594 goto nosideret;
595 if (unop_must_be_user_defined (arg1))
596 return value_x_unop (arg1, op, 0);
597 else
598 return value_neg (arg1);
599
600 case UNOP_LOGNOT:
601 arg1 = evaluate_subexp (exp, pos, noside);
602 if (noside == EVAL_SKIP)
603 goto nosideret;
604 if (unop_must_be_user_defined (arg1))
605 return value_x_unop (arg1, op, 0);
606 else
607 return value_lognot (arg1);
608
609 case UNOP_ZEROP:
610 arg1 = evaluate_subexp (exp, pos, noside);
611 if (noside == EVAL_SKIP)
612 goto nosideret;
613 if (unop_must_be_user_defined (arg1))
614 return value_x_unop (arg1, op, 0);
615 else
616 return value_from_long (builtin_type_int, value_zerop (arg1));
617
618 case UNOP_IND:
619 arg1 = evaluate_subexp (exp, pos, noside);
620 if (noside == EVAL_SKIP)
621 goto nosideret;
622 return value_ind (arg1);
623
624 case UNOP_ADDR:
625 if (noside == EVAL_SKIP)
626 {
627 evaluate_subexp (exp, pos, EVAL_SKIP);
628 goto nosideret;
629 }
630 /* C++: check for and handle pointer to members. */
631
632 op = exp->elts[*pos].opcode;
633 if (op == OP_SCOPE)
634 {
635 char *name = &exp->elts[pc+3].string;
636 int tem = strlen (name);
637 struct type *domain = exp->elts[pc+2].type;
638 (*pos) += 2 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
639 arg1 = value_struct_elt_for_address (domain, 0, name);
640 if (arg1)
641 return arg1;
642 error ("no field `%s' in structure", name);
643 }
644 else
645 return evaluate_subexp_for_address (exp, pos, noside);
646
647 case UNOP_SIZEOF:
648 if (noside == EVAL_SKIP)
649 {
650 evaluate_subexp (exp, pos, EVAL_SKIP);
651 goto nosideret;
652 }
653 return evaluate_subexp_for_sizeof (exp, pos);
654
655 case UNOP_CAST:
656 (*pos) += 2;
657 arg1 = evaluate_subexp (exp, pos, noside);
658 if (noside == EVAL_SKIP)
659 goto nosideret;
660 return value_cast (exp->elts[pc + 1].type, arg1);
661
662 case UNOP_MEMVAL:
663 (*pos) += 2;
664 arg1 = evaluate_subexp (exp, pos, noside);
665 if (noside == EVAL_SKIP)
666 goto nosideret;
667 return value_at (exp->elts[pc + 1].type, value_as_long (arg1));
668
669 case UNOP_PREINCREMENT:
670 arg1 = evaluate_subexp (exp, pos, noside);
671 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
672 return arg1;
673 else if (unop_must_be_user_defined (arg1))
674 {
675 return value_x_unop (arg1, op, 0);
676 }
677 else
678 {
679 arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
680 return value_assign (arg1, arg2);
681 }
682
683 case UNOP_PREDECREMENT:
684 arg1 = evaluate_subexp (exp, pos, noside);
685 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
686 return arg1;
687 else if (unop_must_be_user_defined (arg1))
688 {
689 return value_x_unop (arg1, op, 0);
690 }
691 else
692 {
693 arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
694 return value_assign (arg1, arg2);
695 }
696
697 case UNOP_POSTINCREMENT:
698 arg1 = evaluate_subexp (exp, pos, noside);
699 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
700 return arg1;
701 else if (unop_must_be_user_defined (arg1))
702 {
703 return value_x_unop (arg1, op, 0);
704 }
705 else
706 {
707 arg2 = value_add (arg1, value_from_long (builtin_type_char, 1));
708 value_assign (arg1, arg2);
709 return arg1;
710 }
711
712 case UNOP_POSTDECREMENT:
713 arg1 = evaluate_subexp (exp, pos, noside);
714 if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
715 return arg1;
716 else if (unop_must_be_user_defined (arg1))
717 {
718 return value_x_unop (arg1, op, 0);
719 }
720 else
721 {
722 arg2 = value_sub (arg1, value_from_long (builtin_type_char, 1));
723 value_assign (arg1, arg2);
724 return arg1;
725 }
726
727 case OP_THIS:
728 (*pos) += 1;
729 return value_of_this (1);
730
731 default:
732 error ("internal error: I dont know how to evaluation what you gave me");
733 }
734
735 nosideret:
736 return value_from_long (builtin_type_long, 1);
737 }
738 \f
739 /* Evaluate a subexpression of EXP, at index *POS,
740 and return the address of that subexpression.
741 Advance *POS over the subexpression.
742 If the subexpression isn't an lvalue, get an error.
743 NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
744 then only the type of the result need be correct. */
745
746 static value
747 evaluate_subexp_for_address (exp, pos, noside)
748 register struct expression *exp;
749 register int *pos;
750 enum noside noside;
751 {
752 enum exp_opcode op;
753 register int pc;
754
755 pc = (*pos);
756 op = exp->elts[pc].opcode;
757
758 switch (op)
759 {
760 case UNOP_IND:
761 (*pos)++;
762 return evaluate_subexp (exp, pos, noside);
763
764 case UNOP_MEMVAL:
765 (*pos) += 3;
766 return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
767 evaluate_subexp (exp, pos, noside));
768
769 case OP_VAR_VALUE:
770 (*pos) += 3;
771 return locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
772
773 default:
774 return value_addr (evaluate_subexp (exp, pos, noside));
775 }
776 }
777
778 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
779 When used in contexts where arrays will be coerced anyway,
780 this is equivalent to `evaluate_subexp'
781 but much faster because it avoids actually fetching array contents. */
782
783 static value
784 evaluate_subexp_with_coercion (exp, pos, noside)
785 register struct expression *exp;
786 register int *pos;
787 enum noside noside;
788 {
789 register enum exp_opcode op;
790 register int pc;
791 register value val;
792
793 pc = (*pos);
794 op = exp->elts[pc].opcode;
795
796 switch (op)
797 {
798 case OP_VAR_VALUE:
799 if (TYPE_CODE (SYMBOL_TYPE (exp->elts[pc + 1].symbol)) == TYPE_CODE_ARRAY)
800 {
801 (*pos) += 3;
802 val = locate_var_value (exp->elts[pc + 1].symbol, (CORE_ADDR) 0);
803 return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (exp->elts[pc + 1].symbol))),
804 val);
805 }
806 }
807
808 return evaluate_subexp (exp, pos, noside);
809 }
810
811 /* Evaluate a subexpression of EXP, at index *POS,
812 and return a value for the size of that subexpression.
813 Advance *POS over the subexpression. */
814
815 static value
816 evaluate_subexp_for_sizeof (exp, pos)
817 register struct expression *exp;
818 register int *pos;
819 {
820 enum exp_opcode op;
821 register int pc;
822 value val;
823
824 pc = (*pos);
825 op = exp->elts[pc].opcode;
826
827 switch (op)
828 {
829 /* This case is handled specially
830 so that we avoid creating a value for the result type.
831 If the result type is very big, it's desirable not to
832 create a value unnecessarily. */
833 case UNOP_IND:
834 (*pos)++;
835 val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
836 return value_from_long (builtin_type_int,
837 TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
838
839 case UNOP_MEMVAL:
840 (*pos) += 3;
841 return value_from_long (builtin_type_int,
842 TYPE_LENGTH (exp->elts[pc + 1].type));
843
844 case OP_VAR_VALUE:
845 (*pos) += 3;
846 return value_from_long (builtin_type_int,
847 TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
848
849 default:
850 val = evaluate_subexp (exp, pos, EVAL_AVOID_SIDE_EFFECTS);
851 return value_from_long (builtin_type_int,
852 TYPE_LENGTH (VALUE_TYPE (val)));
853 }
854 }
855 \f
856 static
857 initialize ()
858 { }
859
860 END_FILE