A ton of changes to improve C++ debugging. See ChangeLog.
[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
300           if (METHOD_PTR_IS_VIRTUAL(fnptr))
301             {
302               int fnoffset = METHOD_PTR_TO_VOFFSET(fnptr);
303               struct type *basetype;
304               struct type *domain_type =
305                   TYPE_DOMAIN_TYPE (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
306               int i, j;
307               basetype = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
308               if (domain_type != basetype)
309                   arg2 = value_cast(lookup_pointer_type (domain_type), arg2);
310               basetype = TYPE_VPTR_BASETYPE (domain_type);
311               for (i = TYPE_NFN_FIELDS (basetype) - 1; i >= 0; i--)
312                 {
313                   struct fn_field *f = TYPE_FN_FIELDLIST1 (basetype, i);
314                   /* If one is virtual, then all are virtual.  */
315                   if (TYPE_FN_FIELD_VIRTUAL_P (f, 0))
316                     for (j = TYPE_FN_FIELDLIST_LENGTH (basetype, i) - 1; j >= 0; --j)
317                       if (TYPE_FN_FIELD_VOFFSET (f, j) == fnoffset)
318                         {
319                           value temp = value_ind (arg2);
320                           arg1 = value_virtual_fn_field (&temp, f, j, domain_type, 0);
321                           arg2 = value_addr (temp);
322                           goto got_it;
323                         }
324                 }
325               if (i < 0)
326                 error ("virtual function at index %d not found", fnoffset);
327             }
328           else
329             {
330               VALUE_TYPE (arg1) = lookup_pointer_type (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)));
331             }
332         got_it:
333
334           /* Now, say which argument to start evaluating from */
335           tem = 2;
336         }
337       else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
338         {
339           /* Hair for method invocations */
340           int tem2;
341
342           nargs = longest_to_int (exp->elts[pc + 1].longconst) + 1;
343           /* First, evaluate the structure into arg2 */
344           pc2 = (*pos)++;
345           /* type = exp->elts[pc2 + 1].type; */
346           tem2 = strlen (&exp->elts[pc2 + 2].string);
347           *pos += 3 + (tem2 + sizeof (union exp_element)) / sizeof (union exp_element);
348           if (noside == EVAL_SKIP)
349             goto nosideret;
350
351           if (op == STRUCTOP_STRUCT)
352             {
353               arg2 = evaluate_subexp_for_address (exp, pos, noside);
354             }
355           else
356             {
357               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
358             }
359           /* Now, say which argument to start evaluating from */
360           tem = 2;
361         }
362       else
363         {
364           nargs = longest_to_int (exp->elts[pc + 1].longconst);
365           tem = 0;
366         }
367       argvec = (value *) alloca (sizeof (value) * (nargs + 2));
368       for (; tem <= nargs; tem++)
369         /* Ensure that array expressions are coerced into pointer objects. */
370         argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
371
372       /* signal end of arglist */
373       argvec[tem] = 0;
374
375       if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
376         {
377           int static_memfuncp;
378           value temp = arg2;
379
380           /* argvec[0] gets the method;
381              argvec[1] gets the 'this' pointer (unless static) (from arg2);
382              the remaining args go into the rest of argvec. */
383
384           argvec[1] = arg2;
385           argvec[0] =
386             value_struct_elt (&temp, argvec+1, &exp->elts[pc2 + 2].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 + 2].string);
432       (*pos) += 3 + ((tem + sizeof (union exp_element))
433                      / sizeof (union exp_element));
434       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
435       if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
436         goto handle_structop_ptr;
437       type = exp->elts[pc + 1].type;
438       if (type)
439         arg1 = value_ind (value_cast (lookup_pointer_type (type),
440                                       value_addr (arg1)));
441       if (noside == EVAL_SKIP)
442         goto nosideret;
443       if (noside == EVAL_AVOID_SIDE_EFFECTS)
444         return value_zero (lookup_struct_elt_type (VALUE_TYPE (arg1),
445                                                    &exp->elts[pc + 2].string,
446                                                    0),
447                            lval_memory);
448       else
449         {
450           value temp = arg1;
451           return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
452                                    (int *) 0, "structure");
453         }
454
455     case STRUCTOP_PTR:
456       tem = strlen (&exp->elts[pc + 2].string);
457       (*pos) += 3 + (tem + sizeof (union exp_element)) / sizeof (union exp_element);
458       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
459     handle_structop_ptr:
460       type = exp->elts[pc + 1].type;
461       if (type)
462         arg1 = value_cast (lookup_pointer_type (type), arg1);
463       if (noside == EVAL_SKIP)
464         goto nosideret;
465       if (noside == EVAL_AVOID_SIDE_EFFECTS)
466         return value_zero (lookup_struct_elt_type (TYPE_TARGET_TYPE
467                                                    (VALUE_TYPE (arg1)),
468                                                    &exp->elts[pc + 2].string,
469                                                    0),
470                            lval_memory);
471       else
472         {
473           value temp = arg1;
474           return value_struct_elt (&temp, (value *)0, &exp->elts[pc + 2].string,
475                                    (int *) 0, "structure pointer");
476         }
477
478     case STRUCTOP_MEMBER:
479       arg1 = evaluate_subexp_for_address (exp, pos, noside);
480       goto handle_pointer_to_member;
481     case STRUCTOP_MPTR:
482       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
483     handle_pointer_to_member:
484       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
485       if (noside == EVAL_SKIP)
486         goto nosideret;
487       if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_PTR)
488         goto bad_pointer_to_member;
489       type = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
490       if (TYPE_CODE (type) == TYPE_CODE_METHOD)
491         error ("not implemented: pointer-to-method in pointer-to-member construct");
492       if (TYPE_CODE (type) != TYPE_CODE_MEMBER)
493         goto bad_pointer_to_member;
494       /* Now, convert these values to an address.  */
495       arg1 = value_cast (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
496                          arg1);
497       arg3 = value_from_longest (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
498                                  value_as_long (arg1) + value_as_long (arg2));
499       return value_ind (arg3);
500     bad_pointer_to_member:
501       error("non-pointer-to-member value used in pointer-to-member construct");
502
503     case BINOP_ASSIGN:
504       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
505       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
506       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
507         return arg1;
508       if (binop_user_defined_p (op, arg1, arg2))
509         return value_x_binop (arg1, arg2, op, OP_NULL);
510       else
511         return value_assign (arg1, arg2);
512
513     case BINOP_ASSIGN_MODIFY:
514       (*pos) += 2;
515       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
516       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
517       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
518         return arg1;
519       op = exp->elts[pc + 1].opcode;
520       if (binop_user_defined_p (op, arg1, arg2))
521         return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op);
522       else if (op == BINOP_ADD)
523         arg2 = value_add (arg1, arg2);
524       else if (op == BINOP_SUB)
525         arg2 = value_sub (arg1, arg2);
526       else
527         arg2 = value_binop (arg1, arg2, op);
528       return value_assign (arg1, arg2);
529
530     case BINOP_ADD:
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_add (arg1, arg2);
539
540     case BINOP_SUB:
541       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
542       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
543       if (noside == EVAL_SKIP)
544         goto nosideret;
545       if (binop_user_defined_p (op, arg1, arg2))
546         return value_x_binop (arg1, arg2, op, OP_NULL);
547       else
548         return value_sub (arg1, arg2);
549
550     case BINOP_MUL:
551     case BINOP_DIV:
552     case BINOP_REM:
553     case BINOP_LSH:
554     case BINOP_RSH:
555     case BINOP_LOGAND:
556     case BINOP_LOGIOR:
557     case BINOP_LOGXOR:
558       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
559       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
560       if (noside == EVAL_SKIP)
561         goto nosideret;
562       if (binop_user_defined_p (op, arg1, arg2))
563         return value_x_binop (arg1, arg2, op, OP_NULL);
564       else
565         if (noside == EVAL_AVOID_SIDE_EFFECTS
566             && (op == BINOP_DIV || op == BINOP_REM))
567           return value_zero (VALUE_TYPE (arg1), not_lval);
568       else
569         return value_binop (arg1, arg2, op);
570
571     case BINOP_SUBSCRIPT:
572       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
573       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
574       if (noside == EVAL_SKIP)
575         goto nosideret;
576       if (noside == EVAL_AVOID_SIDE_EFFECTS)
577         {
578           /* If the user attempts to subscript something that has no target
579              type (like a plain int variable for example), then report this
580              as an error. */
581
582           type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
583           if (type)
584             return value_zero (type, VALUE_LVAL (arg1));
585           else
586             error ("cannot subscript something of type `%s'",
587                    TYPE_NAME (VALUE_TYPE (arg1)));
588         }
589                            
590       if (binop_user_defined_p (op, arg1, arg2))
591         return value_x_binop (arg1, arg2, op, OP_NULL);
592       else
593         return value_subscript (arg1, arg2);
594       
595     case BINOP_AND:
596       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
597       if (noside == EVAL_SKIP)
598         {
599           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
600           goto nosideret;
601         }
602       
603       oldpos = *pos;
604       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
605       *pos = oldpos;
606       
607       if (binop_user_defined_p (op, arg1, arg2)) 
608         {
609           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
610           return value_x_binop (arg1, arg2, op, OP_NULL);
611         }
612       else
613         {
614           tem = value_zerop (arg1);
615           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
616                                   (tem ? EVAL_SKIP : noside));
617           return value_from_longest (builtin_type_int,
618                                   (LONGEST) (!tem && !value_zerop (arg2)));
619         }
620
621     case BINOP_OR:
622       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
623       if (noside == EVAL_SKIP)
624         {
625           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
626           goto nosideret;
627         }
628       
629       oldpos = *pos;
630       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
631       *pos = oldpos;
632       
633       if (binop_user_defined_p (op, arg1, arg2)) 
634         {
635           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
636           return value_x_binop (arg1, arg2, op, OP_NULL);
637         }
638       else
639         {
640           tem = value_zerop (arg1);
641           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
642                                   (!tem ? EVAL_SKIP : noside));
643           return value_from_longest (builtin_type_int,
644                                   (LONGEST) (!tem || !value_zerop (arg2)));
645         }
646
647     case BINOP_EQUAL:
648       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
649       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
650       if (noside == EVAL_SKIP)
651         goto nosideret;
652       if (binop_user_defined_p (op, arg1, arg2))
653         {
654           return value_x_binop (arg1, arg2, op, OP_NULL);
655         }
656       else
657         {
658           tem = value_equal (arg1, arg2);
659           return value_from_longest (builtin_type_int, (LONGEST) tem);
660         }
661
662     case BINOP_NOTEQUAL:
663       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
664       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
665       if (noside == EVAL_SKIP)
666         goto nosideret;
667       if (binop_user_defined_p (op, arg1, arg2))
668         {
669           return value_x_binop (arg1, arg2, op, OP_NULL);
670         }
671       else
672         {
673           tem = value_equal (arg1, arg2);
674           return value_from_longest (builtin_type_int, (LONGEST) ! tem);
675         }
676
677     case BINOP_LESS:
678       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
679       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
680       if (noside == EVAL_SKIP)
681         goto nosideret;
682       if (binop_user_defined_p (op, arg1, arg2))
683         {
684           return value_x_binop (arg1, arg2, op, OP_NULL);
685         }
686       else
687         {
688           tem = value_less (arg1, arg2);
689           return value_from_longest (builtin_type_int, (LONGEST) tem);
690         }
691
692     case BINOP_GTR:
693       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
694       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
695       if (noside == EVAL_SKIP)
696         goto nosideret;
697       if (binop_user_defined_p (op, arg1, arg2))
698         {
699           return value_x_binop (arg1, arg2, op, OP_NULL);
700         }
701       else
702         {
703           tem = value_less (arg2, arg1);
704           return value_from_longest (builtin_type_int, (LONGEST) tem);
705         }
706
707     case BINOP_GEQ:
708       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
709       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
710       if (noside == EVAL_SKIP)
711         goto nosideret;
712       if (binop_user_defined_p (op, arg1, arg2))
713         {
714           return value_x_binop (arg1, arg2, op, OP_NULL);
715         }
716       else
717         {
718           tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
719           return value_from_longest (builtin_type_int, (LONGEST) tem);
720         }
721
722     case BINOP_LEQ:
723       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
724       arg2 = evaluate_subexp (VALUE_TYPE (arg1), exp, pos, noside);
725       if (noside == EVAL_SKIP)
726         goto nosideret;
727       if (binop_user_defined_p (op, arg1, arg2))
728         {
729           return value_x_binop (arg1, arg2, op, OP_NULL);
730         }
731       else 
732         {
733           tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
734           return value_from_longest (builtin_type_int, (LONGEST) tem);
735         }
736
737     case BINOP_REPEAT:
738       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
739       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
740       if (noside == EVAL_SKIP)
741         goto nosideret;
742       if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_INT)
743         error ("Non-integral right operand for \"@\" operator.");
744       if (noside == EVAL_AVOID_SIDE_EFFECTS)
745         return allocate_repeat_value (VALUE_TYPE (arg1),
746                                       longest_to_int (value_as_long (arg2)));
747       else
748         return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
749
750     case BINOP_COMMA:
751       evaluate_subexp (NULL_TYPE, exp, pos, noside);
752       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
753
754     case UNOP_NEG:
755       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
756       if (noside == EVAL_SKIP)
757         goto nosideret;
758       if (unop_user_defined_p (op, arg1))
759         return value_x_unop (arg1, op);
760       else
761         return value_neg (arg1);
762
763     case UNOP_LOGNOT:
764       /* C++: check for and handle destructor names.  */
765       op = exp->elts[*pos].opcode;
766
767       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
768       if (noside == EVAL_SKIP)
769         goto nosideret;
770       if (unop_user_defined_p (UNOP_LOGNOT, arg1))
771         return value_x_unop (arg1, UNOP_LOGNOT);
772       else
773         return value_lognot (arg1);
774
775     case UNOP_ZEROP:
776       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
777       if (noside == EVAL_SKIP)
778         goto nosideret;
779       if (unop_user_defined_p (op, arg1))
780         return value_x_unop (arg1, op);
781       else
782         return value_from_longest (builtin_type_int,
783                                 (LONGEST) value_zerop (arg1));
784
785     case UNOP_IND:
786       if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
787         expect_type = TYPE_TARGET_TYPE (expect_type);
788       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
789       if (noside == EVAL_SKIP)
790         goto nosideret;
791       if (noside == EVAL_AVOID_SIDE_EFFECTS)
792         {
793           if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR
794               || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_REF
795               /* In C you can dereference an array to get the 1st elt.  */
796               || TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY
797               )
798             return value_zero (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
799                                lval_memory);
800           else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
801             /* GDB allows dereferencing an int.  */
802             return value_zero (builtin_type_int, lval_memory);
803           else
804             error ("Attempt to take contents of a non-pointer value.");
805         }
806       return value_ind (arg1);
807
808     case UNOP_ADDR:
809       /* C++: check for and handle pointer to members.  */
810       
811       op = exp->elts[*pos].opcode;
812
813       if (noside == EVAL_SKIP)
814         {
815           if (op == OP_SCOPE)
816             {
817               char *name = &exp->elts[pc+3].string;
818               int temm = strlen (name);
819               (*pos) += 2 + (temm + sizeof (union exp_element)) / sizeof (union exp_element);
820             }
821           else
822             evaluate_subexp (expect_type, exp, pos, EVAL_SKIP);
823           goto nosideret;
824         }
825
826       return evaluate_subexp_for_address (exp, pos, noside);
827
828     case UNOP_SIZEOF:
829       if (noside == EVAL_SKIP)
830         {
831           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
832           goto nosideret;
833         }
834       return evaluate_subexp_for_sizeof (exp, pos);
835
836     case UNOP_CAST:
837       (*pos) += 2;
838       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
839       if (noside == EVAL_SKIP)
840         goto nosideret;
841       return value_cast (exp->elts[pc + 1].type, arg1);
842
843     case UNOP_MEMVAL:
844       (*pos) += 2;
845       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
846       if (noside == EVAL_SKIP)
847         goto nosideret;
848       if (noside == EVAL_AVOID_SIDE_EFFECTS)
849         return value_zero (exp->elts[pc + 1].type, lval_memory);
850       else
851         return value_at_lazy (exp->elts[pc + 1].type,
852                               value_as_pointer (arg1));
853
854     case UNOP_PREINCREMENT:
855       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
856       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
857         return arg1;
858       else if (unop_user_defined_p (op, arg1))
859         {
860           return value_x_unop (arg1, op);
861         }
862       else
863         {
864           arg2 = value_add (arg1, value_from_longest (builtin_type_char, 
865                                                    (LONGEST) 1));
866           return value_assign (arg1, arg2);
867         }
868
869     case UNOP_PREDECREMENT:
870       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
871       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
872         return arg1;
873       else if (unop_user_defined_p (op, arg1))
874         {
875           return value_x_unop (arg1, op);
876         }
877       else
878         {
879           arg2 = value_sub (arg1, value_from_longest (builtin_type_char, 
880                                                    (LONGEST) 1));
881           return value_assign (arg1, arg2);
882         }
883
884     case UNOP_POSTINCREMENT:
885       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
886       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
887         return arg1;
888       else if (unop_user_defined_p (op, arg1))
889         {
890           return value_x_unop (arg1, op);
891         }
892       else
893         {
894           arg2 = value_add (arg1, value_from_longest (builtin_type_char, 
895                                                    (LONGEST) 1));
896           value_assign (arg1, arg2);
897           return arg1;
898         }
899
900     case UNOP_POSTDECREMENT:
901       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
902       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
903         return arg1;
904       else if (unop_user_defined_p (op, arg1))
905         {
906           return value_x_unop (arg1, op);
907         }
908       else
909         {
910           arg2 = value_sub (arg1, value_from_longest (builtin_type_char, 
911                                                    (LONGEST) 1));
912           value_assign (arg1, arg2);
913           return arg1;
914         }
915         
916     case OP_THIS:
917       (*pos) += 1;
918       return value_of_this (1);
919
920     default:
921       error ("internal error: I do not know how to evaluate what you gave me");
922     }
923
924  nosideret:
925   return value_from_longest (builtin_type_long, (LONGEST) 1);
926 }
927 \f
928 /* Evaluate a subexpression of EXP, at index *POS,
929    and return the address of that subexpression.
930    Advance *POS over the subexpression.
931    If the subexpression isn't an lvalue, get an error.
932    NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
933    then only the type of the result need be correct.  */
934
935 static value
936 evaluate_subexp_for_address (exp, pos, noside)
937      register struct expression *exp;
938      register int *pos;
939      enum noside noside;
940 {
941   enum exp_opcode op;
942   register int pc;
943   struct symbol *var;
944
945   pc = (*pos);
946   op = exp->elts[pc].opcode;
947
948   switch (op)
949     {
950     case UNOP_IND:
951       (*pos)++;
952       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
953
954     case UNOP_MEMVAL:
955       (*pos) += 3;
956       return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
957                          evaluate_subexp (NULL_TYPE, exp, pos, noside));
958
959     case OP_VAR_VALUE:
960       var = exp->elts[pc + 1].symbol;
961
962       /* C++: The "address" of a reference should yield the address
963        * of the object pointed to. Let value_addr() deal with it. */
964       if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
965         goto default_case;
966
967       (*pos) += 3;
968       if (noside == EVAL_AVOID_SIDE_EFFECTS)
969         {
970           struct type *type =
971             lookup_pointer_type (SYMBOL_TYPE (var));
972           enum address_class sym_class = SYMBOL_CLASS (var);
973
974           if (sym_class == LOC_CONST
975               || sym_class == LOC_CONST_BYTES
976               || sym_class == LOC_REGISTER
977               || sym_class == LOC_REGPARM)
978             error ("Attempt to take address of register or constant.");
979
980         return
981           value_zero (type, not_lval);
982         }
983       else
984         return locate_var_value (var, (FRAME) 0);
985
986     default:
987     default_case:
988       if (noside == EVAL_AVOID_SIDE_EFFECTS)
989         {
990           value x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
991           if (VALUE_LVAL (x) == lval_memory)
992             return value_zero (lookup_pointer_type (VALUE_TYPE (x)),
993                                not_lval);
994           else
995             error ("Attempt to take address of non-lval");
996         }
997       return value_addr (evaluate_subexp (NULL_TYPE, exp, pos, noside));
998     }
999 }
1000
1001 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
1002    When used in contexts where arrays will be coerced anyway,
1003    this is equivalent to `evaluate_subexp'
1004    but much faster because it avoids actually fetching array contents.  */
1005
1006 static value
1007 evaluate_subexp_with_coercion (exp, pos, noside)
1008      register struct expression *exp;
1009      register int *pos;
1010      enum noside noside;
1011 {
1012   register enum exp_opcode op;
1013   register int pc;
1014   register value val;
1015   struct symbol *var;
1016
1017   pc = (*pos);
1018   op = exp->elts[pc].opcode;
1019
1020   switch (op)
1021     {
1022     case OP_VAR_VALUE:
1023       var = exp->elts[pc + 1].symbol;
1024       if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_ARRAY)
1025         {
1026           (*pos) += 3;
1027           val = locate_var_value (var, (FRAME) 0);
1028           return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (SYMBOL_TYPE (var))),
1029                              val);
1030         }
1031       default:
1032         return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1033     }
1034 }
1035
1036 /* Evaluate a subexpression of EXP, at index *POS,
1037    and return a value for the size of that subexpression.
1038    Advance *POS over the subexpression.  */
1039
1040 static value
1041 evaluate_subexp_for_sizeof (exp, pos)
1042      register struct expression *exp;
1043      register int *pos;
1044 {
1045   enum exp_opcode op;
1046   register int pc;
1047   value val;
1048
1049   pc = (*pos);
1050   op = exp->elts[pc].opcode;
1051
1052   switch (op)
1053     {
1054       /* This case is handled specially
1055          so that we avoid creating a value for the result type.
1056          If the result type is very big, it's desirable not to
1057          create a value unnecessarily.  */
1058     case UNOP_IND:
1059       (*pos)++;
1060       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1061       return value_from_longest (builtin_type_int, (LONGEST)
1062                       TYPE_LENGTH (TYPE_TARGET_TYPE (VALUE_TYPE (val))));
1063
1064     case UNOP_MEMVAL:
1065       (*pos) += 3;
1066       return value_from_longest (builtin_type_int, 
1067                               (LONGEST) TYPE_LENGTH (exp->elts[pc + 1].type));
1068
1069     case OP_VAR_VALUE:
1070       (*pos) += 3;
1071       return value_from_longest (builtin_type_int,
1072          (LONGEST) TYPE_LENGTH (SYMBOL_TYPE (exp->elts[pc + 1].symbol)));
1073
1074     default:
1075       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
1076       return value_from_longest (builtin_type_int,
1077                               (LONGEST) TYPE_LENGTH (VALUE_TYPE (val)));
1078     }
1079 }
1080
1081 /* Parse a type expression in the string [P..P+LENGTH). */
1082
1083 struct type *
1084 parse_and_eval_type (p, length)
1085      char *p;
1086      int length;
1087 {
1088     char *tmp = (char *)alloca (length + 4);
1089     struct expression *expr;
1090     tmp[0] = '(';
1091     memcpy (tmp+1, p, length);
1092     tmp[length+1] = ')';
1093     tmp[length+2] = '0';
1094     tmp[length+3] = '\0';
1095     expr = parse_expression (tmp);
1096     if (expr->elts[0].opcode != UNOP_CAST)
1097         error ("Internal error in eval_type.");
1098     return expr->elts[1].type;
1099 }