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