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