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