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