Include gdb_assert.h in common-defs.h
[external/binutils.git] / gdb / eval.c
1 /* Evaluate expressions for GDB.
2
3    Copyright (C) 1986-2014 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include <string.h>
22 #include "symtab.h"
23 #include "gdbtypes.h"
24 #include "value.h"
25 #include "expression.h"
26 #include "target.h"
27 #include "frame.h"
28 #include "language.h"           /* For CAST_IS_CONVERSION.  */
29 #include "f-lang.h"             /* For array bound stuff.  */
30 #include "cp-abi.h"
31 #include "infcall.h"
32 #include "objc-lang.h"
33 #include "block.h"
34 #include "parser-defs.h"
35 #include "cp-support.h"
36 #include "ui-out.h"
37 #include "exceptions.h"
38 #include "regcache.h"
39 #include "user-regs.h"
40 #include "valprint.h"
41 #include "gdb_obstack.h"
42 #include "objfiles.h"
43 #include <ctype.h>
44
45 /* This is defined in valops.c */
46 extern int overload_resolution;
47
48 /* Prototypes for local functions.  */
49
50 static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
51                                                  enum noside);
52
53 static struct value *evaluate_subexp_for_address (struct expression *,
54                                                   int *, enum noside);
55
56 static struct value *evaluate_struct_tuple (struct value *,
57                                             struct expression *, int *,
58                                             enum noside, int);
59
60 static LONGEST init_array_element (struct value *, struct value *,
61                                    struct expression *, int *, enum noside,
62                                    LONGEST, LONGEST);
63
64 struct value *
65 evaluate_subexp (struct type *expect_type, struct expression *exp,
66                  int *pos, enum noside noside)
67 {
68   return (*exp->language_defn->la_exp_desc->evaluate_exp) 
69     (expect_type, exp, pos, noside);
70 }
71 \f
72 /* Parse the string EXP as a C expression, evaluate it,
73    and return the result as a number.  */
74
75 CORE_ADDR
76 parse_and_eval_address (const char *exp)
77 {
78   struct expression *expr = parse_expression (exp);
79   CORE_ADDR addr;
80   struct cleanup *old_chain =
81     make_cleanup (free_current_contents, &expr);
82
83   addr = value_as_address (evaluate_expression (expr));
84   do_cleanups (old_chain);
85   return addr;
86 }
87
88 /* Like parse_and_eval_address, but treats the value of the expression
89    as an integer, not an address, returns a LONGEST, not a CORE_ADDR.  */
90 LONGEST
91 parse_and_eval_long (const char *exp)
92 {
93   struct expression *expr = parse_expression (exp);
94   LONGEST retval;
95   struct cleanup *old_chain =
96     make_cleanup (free_current_contents, &expr);
97
98   retval = value_as_long (evaluate_expression (expr));
99   do_cleanups (old_chain);
100   return (retval);
101 }
102
103 struct value *
104 parse_and_eval (const char *exp)
105 {
106   struct expression *expr = parse_expression (exp);
107   struct value *val;
108   struct cleanup *old_chain =
109     make_cleanup (free_current_contents, &expr);
110
111   val = evaluate_expression (expr);
112   do_cleanups (old_chain);
113   return val;
114 }
115
116 /* Parse up to a comma (or to a closeparen)
117    in the string EXPP as an expression, evaluate it, and return the value.
118    EXPP is advanced to point to the comma.  */
119
120 struct value *
121 parse_to_comma_and_eval (const char **expp)
122 {
123   struct expression *expr = parse_exp_1 (expp, 0, (struct block *) 0, 1);
124   struct value *val;
125   struct cleanup *old_chain =
126     make_cleanup (free_current_contents, &expr);
127
128   val = evaluate_expression (expr);
129   do_cleanups (old_chain);
130   return val;
131 }
132 \f
133 /* Evaluate an expression in internal prefix form
134    such as is constructed by parse.y.
135
136    See expression.h for info on the format of an expression.  */
137
138 struct value *
139 evaluate_expression (struct expression *exp)
140 {
141   int pc = 0;
142
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 struct value *
150 evaluate_type (struct expression *exp)
151 {
152   int pc = 0;
153
154   return evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
155 }
156
157 /* Evaluate a subexpression, avoiding all memory references and
158    getting a value whose type alone is correct.  */
159
160 struct value *
161 evaluate_subexpression_type (struct expression *exp, int subexp)
162 {
163   return evaluate_subexp (NULL_TYPE, exp, &subexp, EVAL_AVOID_SIDE_EFFECTS);
164 }
165
166 /* Find the current value of a watchpoint on EXP.  Return the value in
167    *VALP and *RESULTP and the chain of intermediate and final values
168    in *VAL_CHAIN.  RESULTP and VAL_CHAIN may be NULL if the caller does
169    not need them.
170
171    If PRESERVE_ERRORS is true, then exceptions are passed through.
172    Otherwise, if PRESERVE_ERRORS is false, then if a memory error
173    occurs while evaluating the expression, *RESULTP will be set to
174    NULL.  *RESULTP may be a lazy value, if the result could not be
175    read from memory.  It is used to determine whether a value is
176    user-specified (we should watch the whole value) or intermediate
177    (we should watch only the bit used to locate the final value).
178
179    If the final value, or any intermediate value, could not be read
180    from memory, *VALP will be set to NULL.  *VAL_CHAIN will still be
181    set to any referenced values.  *VALP will never be a lazy value.
182    This is the value which we store in struct breakpoint.
183
184    If VAL_CHAIN is non-NULL, *VAL_CHAIN will be released from the
185    value chain.  The caller must free the values individually.  If
186    VAL_CHAIN is NULL, all generated values will be left on the value
187    chain.  */
188
189 void
190 fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
191                     struct value **resultp, struct value **val_chain,
192                     int preserve_errors)
193 {
194   struct value *mark, *new_mark, *result;
195   volatile struct gdb_exception ex;
196
197   *valp = NULL;
198   if (resultp)
199     *resultp = NULL;
200   if (val_chain)
201     *val_chain = NULL;
202
203   /* Evaluate the expression.  */
204   mark = value_mark ();
205   result = NULL;
206
207   TRY_CATCH (ex, RETURN_MASK_ALL)
208     {
209       result = evaluate_subexp (NULL_TYPE, exp, pc, EVAL_NORMAL);
210     }
211   if (ex.reason < 0)
212     {
213       /* Ignore memory errors if we want watchpoints pointing at
214          inaccessible memory to still be created; otherwise, throw the
215          error to some higher catcher.  */
216       switch (ex.error)
217         {
218         case MEMORY_ERROR:
219           if (!preserve_errors)
220             break;
221         default:
222           throw_exception (ex);
223           break;
224         }
225     }
226
227   new_mark = value_mark ();
228   if (mark == new_mark)
229     return;
230   if (resultp)
231     *resultp = result;
232
233   /* Make sure it's not lazy, so that after the target stops again we
234      have a non-lazy previous value to compare with.  */
235   if (result != NULL)
236     {
237       if (!value_lazy (result))
238         *valp = result;
239       else
240         {
241           volatile struct gdb_exception except;
242
243           TRY_CATCH (except, RETURN_MASK_ERROR)
244             {
245               value_fetch_lazy (result);
246               *valp = result;
247             }
248         }
249     }
250
251   if (val_chain)
252     {
253       /* Return the chain of intermediate values.  We use this to
254          decide which addresses to watch.  */
255       *val_chain = new_mark;
256       value_release_to_mark (mark);
257     }
258 }
259
260 /* Extract a field operation from an expression.  If the subexpression
261    of EXP starting at *SUBEXP is not a structure dereference
262    operation, return NULL.  Otherwise, return the name of the
263    dereferenced field, and advance *SUBEXP to point to the
264    subexpression of the left-hand-side of the dereference.  This is
265    used when completing field names.  */
266
267 char *
268 extract_field_op (struct expression *exp, int *subexp)
269 {
270   int tem;
271   char *result;
272
273   if (exp->elts[*subexp].opcode != STRUCTOP_STRUCT
274       && exp->elts[*subexp].opcode != STRUCTOP_PTR)
275     return NULL;
276   tem = longest_to_int (exp->elts[*subexp + 1].longconst);
277   result = &exp->elts[*subexp + 2].string;
278   (*subexp) += 1 + 3 + BYTES_TO_EXP_ELEM (tem + 1);
279   return result;
280 }
281
282 /* This function evaluates brace-initializers (in C/C++) for
283    structure types.  */
284
285 static struct value *
286 evaluate_struct_tuple (struct value *struct_val,
287                        struct expression *exp,
288                        int *pos, enum noside noside, int nargs)
289 {
290   struct type *struct_type = check_typedef (value_type (struct_val));
291   struct type *field_type;
292   int fieldno = -1;
293
294   while (--nargs >= 0)
295     {
296       struct value *val = NULL;
297       int bitpos, bitsize;
298       bfd_byte *addr;
299
300       fieldno++;
301       /* Skip static fields.  */
302       while (fieldno < TYPE_NFIELDS (struct_type)
303              && field_is_static (&TYPE_FIELD (struct_type,
304                                               fieldno)))
305         fieldno++;
306       if (fieldno >= TYPE_NFIELDS (struct_type))
307         error (_("too many initializers"));
308       field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
309       if (TYPE_CODE (field_type) == TYPE_CODE_UNION
310           && TYPE_FIELD_NAME (struct_type, fieldno)[0] == '0')
311         error (_("don't know which variant you want to set"));
312
313       /* Here, struct_type is the type of the inner struct,
314          while substruct_type is the type of the inner struct.
315          These are the same for normal structures, but a variant struct
316          contains anonymous union fields that contain substruct fields.
317          The value fieldno is the index of the top-level (normal or
318          anonymous union) field in struct_field, while the value
319          subfieldno is the index of the actual real (named inner) field
320          in substruct_type.  */
321
322       field_type = TYPE_FIELD_TYPE (struct_type, fieldno);
323       if (val == 0)
324         val = evaluate_subexp (field_type, exp, pos, noside);
325
326       /* Now actually set the field in struct_val.  */
327
328       /* Assign val to field fieldno.  */
329       if (value_type (val) != field_type)
330         val = value_cast (field_type, val);
331
332       bitsize = TYPE_FIELD_BITSIZE (struct_type, fieldno);
333       bitpos = TYPE_FIELD_BITPOS (struct_type, fieldno);
334       addr = value_contents_writeable (struct_val) + bitpos / 8;
335       if (bitsize)
336         modify_field (struct_type, addr,
337                       value_as_long (val), bitpos % 8, bitsize);
338       else
339         memcpy (addr, value_contents (val),
340                 TYPE_LENGTH (value_type (val)));
341
342     }
343   return struct_val;
344 }
345
346 /* Recursive helper function for setting elements of array tuples.
347    The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND); the
348    element value is ELEMENT; EXP, POS and NOSIDE are as usual.
349    Evaluates index expresions and sets the specified element(s) of
350    ARRAY to ELEMENT.  Returns last index value.  */
351
352 static LONGEST
353 init_array_element (struct value *array, struct value *element,
354                     struct expression *exp, int *pos,
355                     enum noside noside, LONGEST low_bound, LONGEST high_bound)
356 {
357   LONGEST index;
358   int element_size = TYPE_LENGTH (value_type (element));
359
360   if (exp->elts[*pos].opcode == BINOP_COMMA)
361     {
362       (*pos)++;
363       init_array_element (array, element, exp, pos, noside,
364                           low_bound, high_bound);
365       return init_array_element (array, element,
366                                  exp, pos, noside, low_bound, high_bound);
367     }
368   else
369     {
370       index = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
371       if (index < low_bound || index > high_bound)
372         error (_("tuple index out of range"));
373       memcpy (value_contents_raw (array) + (index - low_bound) * element_size,
374               value_contents (element), element_size);
375     }
376   return index;
377 }
378
379 static struct value *
380 value_f90_subarray (struct value *array,
381                     struct expression *exp, int *pos, enum noside noside)
382 {
383   int pc = (*pos) + 1;
384   LONGEST low_bound, high_bound;
385   struct type *range = check_typedef (TYPE_INDEX_TYPE (value_type (array)));
386   enum f90_range_type range_type = longest_to_int (exp->elts[pc].longconst);
387  
388   *pos += 3;
389
390   if (range_type == LOW_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
391     low_bound = TYPE_LOW_BOUND (range);
392   else
393     low_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
394
395   if (range_type == HIGH_BOUND_DEFAULT || range_type == BOTH_BOUND_DEFAULT)
396     high_bound = TYPE_HIGH_BOUND (range);
397   else
398     high_bound = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
399
400   return value_slice (array, low_bound, high_bound - low_bound + 1);
401 }
402
403
404 /* Promote value ARG1 as appropriate before performing a unary operation
405    on this argument.
406    If the result is not appropriate for any particular language then it
407    needs to patch this function.  */
408
409 void
410 unop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
411               struct value **arg1)
412 {
413   struct type *type1;
414
415   *arg1 = coerce_ref (*arg1);
416   type1 = check_typedef (value_type (*arg1));
417
418   if (is_integral_type (type1))
419     {
420       switch (language->la_language)
421         {
422         default:
423           /* Perform integral promotion for ANSI C/C++.
424              If not appropropriate for any particular language
425              it needs to modify this function.  */
426           {
427             struct type *builtin_int = builtin_type (gdbarch)->builtin_int;
428
429             if (TYPE_LENGTH (type1) < TYPE_LENGTH (builtin_int))
430               *arg1 = value_cast (builtin_int, *arg1);
431           }
432           break;
433         }
434     }
435 }
436
437 /* Promote values ARG1 and ARG2 as appropriate before performing a binary
438    operation on those two operands.
439    If the result is not appropriate for any particular language then it
440    needs to patch this function.  */
441
442 void
443 binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
444                struct value **arg1, struct value **arg2)
445 {
446   struct type *promoted_type = NULL;
447   struct type *type1;
448   struct type *type2;
449
450   *arg1 = coerce_ref (*arg1);
451   *arg2 = coerce_ref (*arg2);
452
453   type1 = check_typedef (value_type (*arg1));
454   type2 = check_typedef (value_type (*arg2));
455
456   if ((TYPE_CODE (type1) != TYPE_CODE_FLT
457        && TYPE_CODE (type1) != TYPE_CODE_DECFLOAT
458        && !is_integral_type (type1))
459       || (TYPE_CODE (type2) != TYPE_CODE_FLT
460           && TYPE_CODE (type2) != TYPE_CODE_DECFLOAT
461           && !is_integral_type (type2)))
462     return;
463
464   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT
465       || TYPE_CODE (type2) == TYPE_CODE_DECFLOAT)
466     {
467       /* No promotion required.  */
468     }
469   else if (TYPE_CODE (type1) == TYPE_CODE_FLT
470            || TYPE_CODE (type2) == TYPE_CODE_FLT)
471     {
472       switch (language->la_language)
473         {
474         case language_c:
475         case language_cplus:
476         case language_asm:
477         case language_objc:
478         case language_opencl:
479           /* No promotion required.  */
480           break;
481
482         default:
483           /* For other languages the result type is unchanged from gdb
484              version 6.7 for backward compatibility.
485              If either arg was long double, make sure that value is also long
486              double.  Otherwise use double.  */
487           if (TYPE_LENGTH (type1) * 8 > gdbarch_double_bit (gdbarch)
488               || TYPE_LENGTH (type2) * 8 > gdbarch_double_bit (gdbarch))
489             promoted_type = builtin_type (gdbarch)->builtin_long_double;
490           else
491             promoted_type = builtin_type (gdbarch)->builtin_double;
492           break;
493         }
494     }
495   else if (TYPE_CODE (type1) == TYPE_CODE_BOOL
496            && TYPE_CODE (type2) == TYPE_CODE_BOOL)
497     {
498       /* No promotion required.  */
499     }
500   else
501     /* Integral operations here.  */
502     /* FIXME: Also mixed integral/booleans, with result an integer.  */
503     {
504       const struct builtin_type *builtin = builtin_type (gdbarch);
505       unsigned int promoted_len1 = TYPE_LENGTH (type1);
506       unsigned int promoted_len2 = TYPE_LENGTH (type2);
507       int is_unsigned1 = TYPE_UNSIGNED (type1);
508       int is_unsigned2 = TYPE_UNSIGNED (type2);
509       unsigned int result_len;
510       int unsigned_operation;
511
512       /* Determine type length and signedness after promotion for
513          both operands.  */
514       if (promoted_len1 < TYPE_LENGTH (builtin->builtin_int))
515         {
516           is_unsigned1 = 0;
517           promoted_len1 = TYPE_LENGTH (builtin->builtin_int);
518         }
519       if (promoted_len2 < TYPE_LENGTH (builtin->builtin_int))
520         {
521           is_unsigned2 = 0;
522           promoted_len2 = TYPE_LENGTH (builtin->builtin_int);
523         }
524
525       if (promoted_len1 > promoted_len2)
526         {
527           unsigned_operation = is_unsigned1;
528           result_len = promoted_len1;
529         }
530       else if (promoted_len2 > promoted_len1)
531         {
532           unsigned_operation = is_unsigned2;
533           result_len = promoted_len2;
534         }
535       else
536         {
537           unsigned_operation = is_unsigned1 || is_unsigned2;
538           result_len = promoted_len1;
539         }
540
541       switch (language->la_language)
542         {
543         case language_c:
544         case language_cplus:
545         case language_asm:
546         case language_objc:
547           if (result_len <= TYPE_LENGTH (builtin->builtin_int))
548             {
549               promoted_type = (unsigned_operation
550                                ? builtin->builtin_unsigned_int
551                                : builtin->builtin_int);
552             }
553           else if (result_len <= TYPE_LENGTH (builtin->builtin_long))
554             {
555               promoted_type = (unsigned_operation
556                                ? builtin->builtin_unsigned_long
557                                : builtin->builtin_long);
558             }
559           else
560             {
561               promoted_type = (unsigned_operation
562                                ? builtin->builtin_unsigned_long_long
563                                : builtin->builtin_long_long);
564             }
565           break;
566         case language_opencl:
567           if (result_len <= TYPE_LENGTH (lookup_signed_typename
568                                          (language, gdbarch, "int")))
569             {
570               promoted_type =
571                 (unsigned_operation
572                  ? lookup_unsigned_typename (language, gdbarch, "int")
573                  : lookup_signed_typename (language, gdbarch, "int"));
574             }
575           else if (result_len <= TYPE_LENGTH (lookup_signed_typename
576                                               (language, gdbarch, "long")))
577             {
578               promoted_type =
579                 (unsigned_operation
580                  ? lookup_unsigned_typename (language, gdbarch, "long")
581                  : lookup_signed_typename (language, gdbarch,"long"));
582             }
583           break;
584         default:
585           /* For other languages the result type is unchanged from gdb
586              version 6.7 for backward compatibility.
587              If either arg was long long, make sure that value is also long
588              long.  Otherwise use long.  */
589           if (unsigned_operation)
590             {
591               if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
592                 promoted_type = builtin->builtin_unsigned_long_long;
593               else
594                 promoted_type = builtin->builtin_unsigned_long;
595             }
596           else
597             {
598               if (result_len > gdbarch_long_bit (gdbarch) / HOST_CHAR_BIT)
599                 promoted_type = builtin->builtin_long_long;
600               else
601                 promoted_type = builtin->builtin_long;
602             }
603           break;
604         }
605     }
606
607   if (promoted_type)
608     {
609       /* Promote both operands to common type.  */
610       *arg1 = value_cast (promoted_type, *arg1);
611       *arg2 = value_cast (promoted_type, *arg2);
612     }
613 }
614
615 static int
616 ptrmath_type_p (const struct language_defn *lang, struct type *type)
617 {
618   type = check_typedef (type);
619   if (TYPE_CODE (type) == TYPE_CODE_REF)
620     type = TYPE_TARGET_TYPE (type);
621
622   switch (TYPE_CODE (type))
623     {
624     case TYPE_CODE_PTR:
625     case TYPE_CODE_FUNC:
626       return 1;
627
628     case TYPE_CODE_ARRAY:
629       return TYPE_VECTOR (type) ? 0 : lang->c_style_arrays;
630
631     default:
632       return 0;
633     }
634 }
635
636 /* Constructs a fake method with the given parameter types.
637    This function is used by the parser to construct an "expected"
638    type for method overload resolution.  */
639
640 static struct type *
641 make_params (int num_types, struct type **param_types)
642 {
643   struct type *type = XCNEW (struct type);
644   TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
645   TYPE_LENGTH (type) = 1;
646   TYPE_CODE (type) = TYPE_CODE_METHOD;
647   TYPE_VPTR_FIELDNO (type) = -1;
648   TYPE_CHAIN (type) = type;
649   if (num_types > 0)
650     {
651       if (param_types[num_types - 1] == NULL)
652         {
653           --num_types;
654           TYPE_VARARGS (type) = 1;
655         }
656       else if (TYPE_CODE (check_typedef (param_types[num_types - 1]))
657                == TYPE_CODE_VOID)
658         {
659           --num_types;
660           /* Caller should have ensured this.  */
661           gdb_assert (num_types == 0);
662           TYPE_PROTOTYPED (type) = 1;
663         }
664     }
665
666   TYPE_NFIELDS (type) = num_types;
667   TYPE_FIELDS (type) = (struct field *)
668     TYPE_ZALLOC (type, sizeof (struct field) * num_types);
669
670   while (num_types-- > 0)
671     TYPE_FIELD_TYPE (type, num_types) = param_types[num_types];
672
673   return type;
674 }
675
676 struct value *
677 evaluate_subexp_standard (struct type *expect_type,
678                           struct expression *exp, int *pos,
679                           enum noside noside)
680 {
681   enum exp_opcode op;
682   int tem, tem2, tem3;
683   int pc, pc2 = 0, oldpos;
684   struct value *arg1 = NULL;
685   struct value *arg2 = NULL;
686   struct value *arg3;
687   struct type *type;
688   int nargs;
689   struct value **argvec;
690   int code;
691   int ix;
692   long mem_offset;
693   struct type **arg_types;
694   int save_pos1;
695   struct symbol *function = NULL;
696   char *function_name = NULL;
697
698   pc = (*pos)++;
699   op = exp->elts[pc].opcode;
700
701   switch (op)
702     {
703     case OP_SCOPE:
704       tem = longest_to_int (exp->elts[pc + 2].longconst);
705       (*pos) += 4 + BYTES_TO_EXP_ELEM (tem + 1);
706       if (noside == EVAL_SKIP)
707         goto nosideret;
708       arg1 = value_aggregate_elt (exp->elts[pc + 1].type,
709                                   &exp->elts[pc + 3].string,
710                                   expect_type, 0, noside);
711       if (arg1 == NULL)
712         error (_("There is no field named %s"), &exp->elts[pc + 3].string);
713       return arg1;
714
715     case OP_LONG:
716       (*pos) += 3;
717       return value_from_longest (exp->elts[pc + 1].type,
718                                  exp->elts[pc + 2].longconst);
719
720     case OP_DOUBLE:
721       (*pos) += 3;
722       return value_from_double (exp->elts[pc + 1].type,
723                                 exp->elts[pc + 2].doubleconst);
724
725     case OP_DECFLOAT:
726       (*pos) += 3;
727       return value_from_decfloat (exp->elts[pc + 1].type,
728                                   exp->elts[pc + 2].decfloatconst);
729
730     case OP_ADL_FUNC:
731     case OP_VAR_VALUE:
732       (*pos) += 3;
733       if (noside == EVAL_SKIP)
734         goto nosideret;
735
736       /* JYG: We used to just return value_zero of the symbol type
737          if we're asked to avoid side effects.  Otherwise we return
738          value_of_variable (...).  However I'm not sure if
739          value_of_variable () has any side effect.
740          We need a full value object returned here for whatis_exp ()
741          to call evaluate_type () and then pass the full value to
742          value_rtti_target_type () if we are dealing with a pointer
743          or reference to a base class and print object is on.  */
744
745       {
746         volatile struct gdb_exception except;
747         struct value *ret = NULL;
748
749         TRY_CATCH (except, RETURN_MASK_ERROR)
750           {
751             ret = value_of_variable (exp->elts[pc + 2].symbol,
752                                      exp->elts[pc + 1].block);
753           }
754
755         if (except.reason < 0)
756           {
757             if (noside == EVAL_AVOID_SIDE_EFFECTS)
758               ret = value_zero (SYMBOL_TYPE (exp->elts[pc + 2].symbol),
759                                 not_lval);
760             else
761               throw_exception (except);
762           }
763
764         return ret;
765       }
766
767     case OP_VAR_ENTRY_VALUE:
768       (*pos) += 2;
769       if (noside == EVAL_SKIP)
770         goto nosideret;
771
772       {
773         struct symbol *sym = exp->elts[pc + 1].symbol;
774         struct frame_info *frame;
775
776         if (noside == EVAL_AVOID_SIDE_EFFECTS)
777           return value_zero (SYMBOL_TYPE (sym), not_lval);
778
779         if (SYMBOL_COMPUTED_OPS (sym) == NULL
780             || SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
781           error (_("Symbol \"%s\" does not have any specific entry value"),
782                  SYMBOL_PRINT_NAME (sym));
783
784         frame = get_selected_frame (NULL);
785         return SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry (sym, frame);
786       }
787
788     case OP_LAST:
789       (*pos) += 2;
790       return
791         access_value_history (longest_to_int (exp->elts[pc + 1].longconst));
792
793     case OP_REGISTER:
794       {
795         const char *name = &exp->elts[pc + 2].string;
796         int regno;
797         struct value *val;
798
799         (*pos) += 3 + BYTES_TO_EXP_ELEM (exp->elts[pc + 1].longconst + 1);
800         regno = user_reg_map_name_to_regnum (exp->gdbarch,
801                                              name, strlen (name));
802         if (regno == -1)
803           error (_("Register $%s not available."), name);
804
805         /* In EVAL_AVOID_SIDE_EFFECTS mode, we only need to return
806            a value with the appropriate register type.  Unfortunately,
807            we don't have easy access to the type of user registers.
808            So for these registers, we fetch the register value regardless
809            of the evaluation mode.  */
810         if (noside == EVAL_AVOID_SIDE_EFFECTS
811             && regno < gdbarch_num_regs (exp->gdbarch)
812                         + gdbarch_num_pseudo_regs (exp->gdbarch))
813           val = value_zero (register_type (exp->gdbarch, regno), not_lval);
814         else
815           val = value_of_register (regno, get_selected_frame (NULL));
816         if (val == NULL)
817           error (_("Value of register %s not available."), name);
818         else
819           return val;
820       }
821     case OP_BOOL:
822       (*pos) += 2;
823       type = language_bool_type (exp->language_defn, exp->gdbarch);
824       return value_from_longest (type, exp->elts[pc + 1].longconst);
825
826     case OP_INTERNALVAR:
827       (*pos) += 2;
828       return value_of_internalvar (exp->gdbarch,
829                                    exp->elts[pc + 1].internalvar);
830
831     case OP_STRING:
832       tem = longest_to_int (exp->elts[pc + 1].longconst);
833       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
834       if (noside == EVAL_SKIP)
835         goto nosideret;
836       type = language_string_char_type (exp->language_defn, exp->gdbarch);
837       return value_string (&exp->elts[pc + 2].string, tem, type);
838
839     case OP_OBJC_NSSTRING:              /* Objective C Foundation Class
840                                            NSString constant.  */
841       tem = longest_to_int (exp->elts[pc + 1].longconst);
842       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
843       if (noside == EVAL_SKIP)
844         {
845           goto nosideret;
846         }
847       return value_nsstring (exp->gdbarch, &exp->elts[pc + 2].string, tem + 1);
848
849     case OP_ARRAY:
850       (*pos) += 3;
851       tem2 = longest_to_int (exp->elts[pc + 1].longconst);
852       tem3 = longest_to_int (exp->elts[pc + 2].longconst);
853       nargs = tem3 - tem2 + 1;
854       type = expect_type ? check_typedef (expect_type) : NULL_TYPE;
855
856       if (expect_type != NULL_TYPE && noside != EVAL_SKIP
857           && TYPE_CODE (type) == TYPE_CODE_STRUCT)
858         {
859           struct value *rec = allocate_value (expect_type);
860
861           memset (value_contents_raw (rec), '\0', TYPE_LENGTH (type));
862           return evaluate_struct_tuple (rec, exp, pos, noside, nargs);
863         }
864
865       if (expect_type != NULL_TYPE && noside != EVAL_SKIP
866           && TYPE_CODE (type) == TYPE_CODE_ARRAY)
867         {
868           struct type *range_type = TYPE_INDEX_TYPE (type);
869           struct type *element_type = TYPE_TARGET_TYPE (type);
870           struct value *array = allocate_value (expect_type);
871           int element_size = TYPE_LENGTH (check_typedef (element_type));
872           LONGEST low_bound, high_bound, index;
873
874           if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
875             {
876               low_bound = 0;
877               high_bound = (TYPE_LENGTH (type) / element_size) - 1;
878             }
879           index = low_bound;
880           memset (value_contents_raw (array), 0, TYPE_LENGTH (expect_type));
881           for (tem = nargs; --nargs >= 0;)
882             {
883               struct value *element;
884               int index_pc = 0;
885
886               element = evaluate_subexp (element_type, exp, pos, noside);
887               if (value_type (element) != element_type)
888                 element = value_cast (element_type, element);
889               if (index_pc)
890                 {
891                   int continue_pc = *pos;
892
893                   *pos = index_pc;
894                   index = init_array_element (array, element, exp, pos, noside,
895                                               low_bound, high_bound);
896                   *pos = continue_pc;
897                 }
898               else
899                 {
900                   if (index > high_bound)
901                     /* To avoid memory corruption.  */
902                     error (_("Too many array elements"));
903                   memcpy (value_contents_raw (array)
904                           + (index - low_bound) * element_size,
905                           value_contents (element),
906                           element_size);
907                 }
908               index++;
909             }
910           return array;
911         }
912
913       if (expect_type != NULL_TYPE && noside != EVAL_SKIP
914           && TYPE_CODE (type) == TYPE_CODE_SET)
915         {
916           struct value *set = allocate_value (expect_type);
917           gdb_byte *valaddr = value_contents_raw (set);
918           struct type *element_type = TYPE_INDEX_TYPE (type);
919           struct type *check_type = element_type;
920           LONGEST low_bound, high_bound;
921
922           /* Get targettype of elementtype.  */
923           while (TYPE_CODE (check_type) == TYPE_CODE_RANGE
924                  || TYPE_CODE (check_type) == TYPE_CODE_TYPEDEF)
925             check_type = TYPE_TARGET_TYPE (check_type);
926
927           if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
928             error (_("(power)set type with unknown size"));
929           memset (valaddr, '\0', TYPE_LENGTH (type));
930           for (tem = 0; tem < nargs; tem++)
931             {
932               LONGEST range_low, range_high;
933               struct type *range_low_type, *range_high_type;
934               struct value *elem_val;
935
936               elem_val = evaluate_subexp (element_type, exp, pos, noside);
937               range_low_type = range_high_type = value_type (elem_val);
938               range_low = range_high = value_as_long (elem_val);
939
940               /* Check types of elements to avoid mixture of elements from
941                  different types. Also check if type of element is "compatible"
942                  with element type of powerset.  */
943               if (TYPE_CODE (range_low_type) == TYPE_CODE_RANGE)
944                 range_low_type = TYPE_TARGET_TYPE (range_low_type);
945               if (TYPE_CODE (range_high_type) == TYPE_CODE_RANGE)
946                 range_high_type = TYPE_TARGET_TYPE (range_high_type);
947               if ((TYPE_CODE (range_low_type) != TYPE_CODE (range_high_type))
948                   || (TYPE_CODE (range_low_type) == TYPE_CODE_ENUM
949                       && (range_low_type != range_high_type)))
950                 /* different element modes.  */
951                 error (_("POWERSET tuple elements of different mode"));
952               if ((TYPE_CODE (check_type) != TYPE_CODE (range_low_type))
953                   || (TYPE_CODE (check_type) == TYPE_CODE_ENUM
954                       && range_low_type != check_type))
955                 error (_("incompatible POWERSET tuple elements"));
956               if (range_low > range_high)
957                 {
958                   warning (_("empty POWERSET tuple range"));
959                   continue;
960                 }
961               if (range_low < low_bound || range_high > high_bound)
962                 error (_("POWERSET tuple element out of range"));
963               range_low -= low_bound;
964               range_high -= low_bound;
965               for (; range_low <= range_high; range_low++)
966                 {
967                   int bit_index = (unsigned) range_low % TARGET_CHAR_BIT;
968
969                   if (gdbarch_bits_big_endian (exp->gdbarch))
970                     bit_index = TARGET_CHAR_BIT - 1 - bit_index;
971                   valaddr[(unsigned) range_low / TARGET_CHAR_BIT]
972                     |= 1 << bit_index;
973                 }
974             }
975           return set;
976         }
977
978       argvec = (struct value **) alloca (sizeof (struct value *) * nargs);
979       for (tem = 0; tem < nargs; tem++)
980         {
981           /* Ensure that array expressions are coerced into pointer
982              objects.  */
983           argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
984         }
985       if (noside == EVAL_SKIP)
986         goto nosideret;
987       return value_array (tem2, tem3, argvec);
988
989     case TERNOP_SLICE:
990       {
991         struct value *array = evaluate_subexp (NULL_TYPE, exp, pos, noside);
992         int lowbound
993           = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
994         int upper
995           = value_as_long (evaluate_subexp (NULL_TYPE, exp, pos, noside));
996
997         if (noside == EVAL_SKIP)
998           goto nosideret;
999         return value_slice (array, lowbound, upper - lowbound + 1);
1000       }
1001
1002     case TERNOP_COND:
1003       /* Skip third and second args to evaluate the first one.  */
1004       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1005       if (value_logical_not (arg1))
1006         {
1007           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1008           return evaluate_subexp (NULL_TYPE, exp, pos, noside);
1009         }
1010       else
1011         {
1012           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1013           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
1014           return arg2;
1015         }
1016
1017     case OP_OBJC_SELECTOR:
1018       {                         /* Objective C @selector operator.  */
1019         char *sel = &exp->elts[pc + 2].string;
1020         int len = longest_to_int (exp->elts[pc + 1].longconst);
1021         struct type *selector_type;
1022
1023         (*pos) += 3 + BYTES_TO_EXP_ELEM (len + 1);
1024         if (noside == EVAL_SKIP)
1025           goto nosideret;
1026
1027         if (sel[len] != 0)
1028           sel[len] = 0;         /* Make sure it's terminated.  */
1029
1030         selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1031         return value_from_longest (selector_type,
1032                                    lookup_child_selector (exp->gdbarch, sel));
1033       }
1034
1035     case OP_OBJC_MSGCALL:
1036       {                         /* Objective C message (method) call.  */
1037
1038         CORE_ADDR responds_selector = 0;
1039         CORE_ADDR method_selector = 0;
1040
1041         CORE_ADDR selector = 0;
1042
1043         int struct_return = 0;
1044         int sub_no_side = 0;
1045
1046         struct value *msg_send = NULL;
1047         struct value *msg_send_stret = NULL;
1048         int gnu_runtime = 0;
1049
1050         struct value *target = NULL;
1051         struct value *method = NULL;
1052         struct value *called_method = NULL; 
1053
1054         struct type *selector_type = NULL;
1055         struct type *long_type;
1056
1057         struct value *ret = NULL;
1058         CORE_ADDR addr = 0;
1059
1060         selector = exp->elts[pc + 1].longconst;
1061         nargs = exp->elts[pc + 2].longconst;
1062         argvec = (struct value **) alloca (sizeof (struct value *) 
1063                                            * (nargs + 5));
1064
1065         (*pos) += 3;
1066
1067         long_type = builtin_type (exp->gdbarch)->builtin_long;
1068         selector_type = builtin_type (exp->gdbarch)->builtin_data_ptr;
1069
1070         if (noside == EVAL_AVOID_SIDE_EFFECTS)
1071           sub_no_side = EVAL_NORMAL;
1072         else
1073           sub_no_side = noside;
1074
1075         target = evaluate_subexp (selector_type, exp, pos, sub_no_side);
1076
1077         if (value_as_long (target) == 0)
1078           return value_from_longest (long_type, 0);
1079         
1080         if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
1081           gnu_runtime = 1;
1082         
1083         /* Find the method dispatch (Apple runtime) or method lookup
1084            (GNU runtime) function for Objective-C.  These will be used
1085            to lookup the symbol information for the method.  If we
1086            can't find any symbol information, then we'll use these to
1087            call the method, otherwise we can call the method
1088            directly.  The msg_send_stret function is used in the special
1089            case of a method that returns a structure (Apple runtime 
1090            only).  */
1091         if (gnu_runtime)
1092           {
1093             struct type *type = selector_type;
1094
1095             type = lookup_function_type (type);
1096             type = lookup_pointer_type (type);
1097             type = lookup_function_type (type);
1098             type = lookup_pointer_type (type);
1099
1100             msg_send = find_function_in_inferior ("objc_msg_lookup", NULL);
1101             msg_send_stret
1102               = find_function_in_inferior ("objc_msg_lookup", NULL);
1103
1104             msg_send = value_from_pointer (type, value_as_address (msg_send));
1105             msg_send_stret = value_from_pointer (type, 
1106                                         value_as_address (msg_send_stret));
1107           }
1108         else
1109           {
1110             msg_send = find_function_in_inferior ("objc_msgSend", NULL);
1111             /* Special dispatcher for methods returning structs.  */
1112             msg_send_stret
1113               = find_function_in_inferior ("objc_msgSend_stret", NULL);
1114           }
1115
1116         /* Verify the target object responds to this method.  The
1117            standard top-level 'Object' class uses a different name for
1118            the verification method than the non-standard, but more
1119            often used, 'NSObject' class.  Make sure we check for both.  */
1120
1121         responds_selector
1122           = lookup_child_selector (exp->gdbarch, "respondsToSelector:");
1123         if (responds_selector == 0)
1124           responds_selector
1125             = lookup_child_selector (exp->gdbarch, "respondsTo:");
1126         
1127         if (responds_selector == 0)
1128           error (_("no 'respondsTo:' or 'respondsToSelector:' method"));
1129         
1130         method_selector
1131           = lookup_child_selector (exp->gdbarch, "methodForSelector:");
1132         if (method_selector == 0)
1133           method_selector
1134             = lookup_child_selector (exp->gdbarch, "methodFor:");
1135         
1136         if (method_selector == 0)
1137           error (_("no 'methodFor:' or 'methodForSelector:' method"));
1138
1139         /* Call the verification method, to make sure that the target
1140          class implements the desired method.  */
1141
1142         argvec[0] = msg_send;
1143         argvec[1] = target;
1144         argvec[2] = value_from_longest (long_type, responds_selector);
1145         argvec[3] = value_from_longest (long_type, selector);
1146         argvec[4] = 0;
1147
1148         ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1149         if (gnu_runtime)
1150           {
1151             /* Function objc_msg_lookup returns a pointer.  */
1152             argvec[0] = ret;
1153             ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1154           }
1155         if (value_as_long (ret) == 0)
1156           error (_("Target does not respond to this message selector."));
1157
1158         /* Call "methodForSelector:" method, to get the address of a
1159            function method that implements this selector for this
1160            class.  If we can find a symbol at that address, then we
1161            know the return type, parameter types etc.  (that's a good
1162            thing).  */
1163
1164         argvec[0] = msg_send;
1165         argvec[1] = target;
1166         argvec[2] = value_from_longest (long_type, method_selector);
1167         argvec[3] = value_from_longest (long_type, selector);
1168         argvec[4] = 0;
1169
1170         ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1171         if (gnu_runtime)
1172           {
1173             argvec[0] = ret;
1174             ret = call_function_by_hand (argvec[0], 3, argvec + 1);
1175           }
1176
1177         /* ret should now be the selector.  */
1178
1179         addr = value_as_long (ret);
1180         if (addr)
1181           {
1182             struct symbol *sym = NULL;
1183
1184             /* The address might point to a function descriptor;
1185                resolve it to the actual code address instead.  */
1186             addr = gdbarch_convert_from_func_ptr_addr (exp->gdbarch, addr,
1187                                                        &current_target);
1188
1189             /* Is it a high_level symbol?  */
1190             sym = find_pc_function (addr);
1191             if (sym != NULL) 
1192               method = value_of_variable (sym, 0);
1193           }
1194
1195         /* If we found a method with symbol information, check to see
1196            if it returns a struct.  Otherwise assume it doesn't.  */
1197
1198         if (method)
1199           {
1200             CORE_ADDR funaddr;
1201             struct type *val_type;
1202
1203             funaddr = find_function_addr (method, &val_type);
1204
1205             block_for_pc (funaddr);
1206
1207             CHECK_TYPEDEF (val_type);
1208           
1209             if ((val_type == NULL) 
1210                 || (TYPE_CODE(val_type) == TYPE_CODE_ERROR))
1211               {
1212                 if (expect_type != NULL)
1213                   val_type = expect_type;
1214               }
1215
1216             struct_return = using_struct_return (exp->gdbarch, method,
1217                                                  val_type);
1218           }
1219         else if (expect_type != NULL)
1220           {
1221             struct_return = using_struct_return (exp->gdbarch, NULL,
1222                                                  check_typedef (expect_type));
1223           }
1224         
1225         /* Found a function symbol.  Now we will substitute its
1226            value in place of the message dispatcher (obj_msgSend),
1227            so that we call the method directly instead of thru
1228            the dispatcher.  The main reason for doing this is that
1229            we can now evaluate the return value and parameter values
1230            according to their known data types, in case we need to
1231            do things like promotion, dereferencing, special handling
1232            of structs and doubles, etc.
1233           
1234            We want to use the type signature of 'method', but still
1235            jump to objc_msgSend() or objc_msgSend_stret() to better
1236            mimic the behavior of the runtime.  */
1237         
1238         if (method)
1239           {
1240             if (TYPE_CODE (value_type (method)) != TYPE_CODE_FUNC)
1241               error (_("method address has symbol information "
1242                        "with non-function type; skipping"));
1243
1244             /* Create a function pointer of the appropriate type, and
1245                replace its value with the value of msg_send or
1246                msg_send_stret.  We must use a pointer here, as
1247                msg_send and msg_send_stret are of pointer type, and
1248                the representation may be different on systems that use
1249                function descriptors.  */
1250             if (struct_return)
1251               called_method
1252                 = value_from_pointer (lookup_pointer_type (value_type (method)),
1253                                       value_as_address (msg_send_stret));
1254             else
1255               called_method
1256                 = value_from_pointer (lookup_pointer_type (value_type (method)),
1257                                       value_as_address (msg_send));
1258           }
1259         else
1260           {
1261             if (struct_return)
1262               called_method = msg_send_stret;
1263             else
1264               called_method = msg_send;
1265           }
1266
1267         if (noside == EVAL_SKIP)
1268           goto nosideret;
1269
1270         if (noside == EVAL_AVOID_SIDE_EFFECTS)
1271           {
1272             /* If the return type doesn't look like a function type,
1273                call an error.  This can happen if somebody tries to
1274                turn a variable into a function call.  This is here
1275                because people often want to call, eg, strcmp, which
1276                gdb doesn't know is a function.  If gdb isn't asked for
1277                it's opinion (ie. through "whatis"), it won't offer
1278                it.  */
1279
1280             struct type *type = value_type (called_method);
1281
1282             if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1283               type = TYPE_TARGET_TYPE (type);
1284             type = TYPE_TARGET_TYPE (type);
1285
1286             if (type)
1287             {
1288               if ((TYPE_CODE (type) == TYPE_CODE_ERROR) && expect_type)
1289                 return allocate_value (expect_type);
1290               else
1291                 return allocate_value (type);
1292             }
1293             else
1294               error (_("Expression of type other than "
1295                        "\"method returning ...\" used as a method"));
1296           }
1297
1298         /* Now depending on whether we found a symbol for the method,
1299            we will either call the runtime dispatcher or the method
1300            directly.  */
1301
1302         argvec[0] = called_method;
1303         argvec[1] = target;
1304         argvec[2] = value_from_longest (long_type, selector);
1305         /* User-supplied arguments.  */
1306         for (tem = 0; tem < nargs; tem++)
1307           argvec[tem + 3] = evaluate_subexp_with_coercion (exp, pos, noside);
1308         argvec[tem + 3] = 0;
1309
1310         if (gnu_runtime && (method != NULL))
1311           {
1312             /* Function objc_msg_lookup returns a pointer.  */
1313             deprecated_set_value_type (argvec[0],
1314                                        lookup_pointer_type (lookup_function_type (value_type (argvec[0]))));
1315             argvec[0]
1316               = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1317           }
1318
1319         ret = call_function_by_hand (argvec[0], nargs + 2, argvec + 1);
1320         return ret;
1321       }
1322       break;
1323
1324     case OP_FUNCALL:
1325       (*pos) += 2;
1326       op = exp->elts[*pos].opcode;
1327       nargs = longest_to_int (exp->elts[pc + 1].longconst);
1328       /* Allocate arg vector, including space for the function to be
1329          called in argvec[0], a potential `this', and a terminating NULL.  */
1330       argvec = (struct value **)
1331         alloca (sizeof (struct value *) * (nargs + 3));
1332       if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1333         {
1334           /* First, evaluate the structure into arg2.  */
1335           pc2 = (*pos)++;
1336
1337           if (noside == EVAL_SKIP)
1338             goto nosideret;
1339
1340           if (op == STRUCTOP_MEMBER)
1341             {
1342               arg2 = evaluate_subexp_for_address (exp, pos, noside);
1343             }
1344           else
1345             {
1346               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1347             }
1348
1349           /* If the function is a virtual function, then the
1350              aggregate value (providing the structure) plays
1351              its part by providing the vtable.  Otherwise,
1352              it is just along for the ride: call the function
1353              directly.  */
1354
1355           arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1356
1357           type = check_typedef (value_type (arg1));
1358           if (TYPE_CODE (type) == TYPE_CODE_METHODPTR)
1359             {
1360               if (noside == EVAL_AVOID_SIDE_EFFECTS)
1361                 arg1 = value_zero (TYPE_TARGET_TYPE (type), not_lval);
1362               else
1363                 arg1 = cplus_method_ptr_to_value (&arg2, arg1);
1364
1365               /* Now, say which argument to start evaluating from.  */
1366               nargs++;
1367               tem = 2;
1368               argvec[1] = arg2;
1369             }
1370           else if (TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
1371             {
1372               struct type *type_ptr
1373                 = lookup_pointer_type (TYPE_DOMAIN_TYPE (type));
1374               struct type *target_type_ptr
1375                 = lookup_pointer_type (TYPE_TARGET_TYPE (type));
1376
1377               /* Now, convert these values to an address.  */
1378               arg2 = value_cast (type_ptr, arg2);
1379
1380               mem_offset = value_as_long (arg1);
1381
1382               arg1 = value_from_pointer (target_type_ptr,
1383                                          value_as_long (arg2) + mem_offset);
1384               arg1 = value_ind (arg1);
1385               tem = 1;
1386             }
1387           else
1388             error (_("Non-pointer-to-member value used in pointer-to-member "
1389                      "construct"));
1390         }
1391       else if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR)
1392         {
1393           /* Hair for method invocations.  */
1394           int tem2;
1395
1396           nargs++;
1397           /* First, evaluate the structure into arg2.  */
1398           pc2 = (*pos)++;
1399           tem2 = longest_to_int (exp->elts[pc2 + 1].longconst);
1400           *pos += 3 + BYTES_TO_EXP_ELEM (tem2 + 1);
1401           if (noside == EVAL_SKIP)
1402             goto nosideret;
1403
1404           if (op == STRUCTOP_STRUCT)
1405             {
1406               /* If v is a variable in a register, and the user types
1407                  v.method (), this will produce an error, because v has
1408                  no address.
1409
1410                  A possible way around this would be to allocate a
1411                  copy of the variable on the stack, copy in the
1412                  contents, call the function, and copy out the
1413                  contents.  I.e. convert this from call by reference
1414                  to call by copy-return (or whatever it's called).
1415                  However, this does not work because it is not the
1416                  same: the method being called could stash a copy of
1417                  the address, and then future uses through that address
1418                  (after the method returns) would be expected to
1419                  use the variable itself, not some copy of it.  */
1420               arg2 = evaluate_subexp_for_address (exp, pos, noside);
1421             }
1422           else
1423             {
1424               arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1425
1426               /* Check to see if the operator '->' has been
1427                  overloaded.  If the operator has been overloaded
1428                  replace arg2 with the value returned by the custom
1429                  operator and continue evaluation.  */
1430               while (unop_user_defined_p (op, arg2))
1431                 {
1432                   volatile struct gdb_exception except;
1433                   struct value *value = NULL;
1434                   TRY_CATCH (except, RETURN_MASK_ERROR)
1435                     {
1436                       value = value_x_unop (arg2, op, noside);
1437                     }
1438
1439                   if (except.reason < 0)
1440                     {
1441                       if (except.error == NOT_FOUND_ERROR)
1442                         break;
1443                       else
1444                         throw_exception (except);
1445                     }
1446                   arg2 = value;
1447                 }
1448             }
1449           /* Now, say which argument to start evaluating from.  */
1450           tem = 2;
1451         }
1452       else if (op == OP_SCOPE
1453                && overload_resolution
1454                && (exp->language_defn->la_language == language_cplus))
1455         {
1456           /* Unpack it locally so we can properly handle overload
1457              resolution.  */
1458           char *name;
1459           int local_tem;
1460
1461           pc2 = (*pos)++;
1462           local_tem = longest_to_int (exp->elts[pc2 + 2].longconst);
1463           (*pos) += 4 + BYTES_TO_EXP_ELEM (local_tem + 1);
1464           type = exp->elts[pc2 + 1].type;
1465           name = &exp->elts[pc2 + 3].string;
1466
1467           function = NULL;
1468           function_name = NULL;
1469           if (TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
1470             {
1471               function = cp_lookup_symbol_namespace (TYPE_TAG_NAME (type),
1472                                                      name,
1473                                                      get_selected_block (0),
1474                                                      VAR_DOMAIN);
1475               if (function == NULL)
1476                 error (_("No symbol \"%s\" in namespace \"%s\"."), 
1477                        name, TYPE_TAG_NAME (type));
1478
1479               tem = 1;
1480               /* arg2 is left as NULL on purpose.  */
1481             }
1482           else
1483             {
1484               gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
1485                           || TYPE_CODE (type) == TYPE_CODE_UNION);
1486               function_name = name;
1487
1488               /* We need a properly typed value for method lookup.  For
1489                  static methods arg2 is otherwise unused.  */
1490               arg2 = value_zero (type, lval_memory);
1491               ++nargs;
1492               tem = 2;
1493             }
1494         }
1495       else if (op == OP_ADL_FUNC)
1496         {
1497           /* Save the function position and move pos so that the arguments
1498              can be evaluated.  */
1499           int func_name_len;
1500
1501           save_pos1 = *pos;
1502           tem = 1;
1503
1504           func_name_len = longest_to_int (exp->elts[save_pos1 + 3].longconst);
1505           (*pos) += 6 + BYTES_TO_EXP_ELEM (func_name_len + 1);
1506         }
1507       else
1508         {
1509           /* Non-method function call.  */
1510           save_pos1 = *pos;
1511           tem = 1;
1512
1513           /* If this is a C++ function wait until overload resolution.  */
1514           if (op == OP_VAR_VALUE
1515               && overload_resolution
1516               && (exp->language_defn->la_language == language_cplus))
1517             {
1518               (*pos) += 4; /* Skip the evaluation of the symbol.  */
1519               argvec[0] = NULL;
1520             }
1521           else
1522             {
1523               argvec[0] = evaluate_subexp_with_coercion (exp, pos, noside);
1524               type = value_type (argvec[0]);
1525               if (type && TYPE_CODE (type) == TYPE_CODE_PTR)
1526                 type = TYPE_TARGET_TYPE (type);
1527               if (type && TYPE_CODE (type) == TYPE_CODE_FUNC)
1528                 {
1529                   for (; tem <= nargs && tem <= TYPE_NFIELDS (type); tem++)
1530                     {
1531                       argvec[tem] = evaluate_subexp (TYPE_FIELD_TYPE (type,
1532                                                                       tem - 1),
1533                                                      exp, pos, noside);
1534                     }
1535                 }
1536             }
1537         }
1538
1539       /* Evaluate arguments (if not already done, e.g., namespace::func()
1540          and overload-resolution is off).  */
1541       for (; tem <= nargs; tem++)
1542         {
1543           /* Ensure that array expressions are coerced into pointer
1544              objects.  */
1545           argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1546         }
1547
1548       /* Signal end of arglist.  */
1549       argvec[tem] = 0;
1550
1551       if (op == OP_ADL_FUNC)
1552         {
1553           struct symbol *symp;
1554           char *func_name;
1555           int  name_len;
1556           int string_pc = save_pos1 + 3;
1557
1558           /* Extract the function name.  */
1559           name_len = longest_to_int (exp->elts[string_pc].longconst);
1560           func_name = (char *) alloca (name_len + 1);
1561           strcpy (func_name, &exp->elts[string_pc + 1].string);
1562
1563           find_overload_match (&argvec[1], nargs, func_name,
1564                                NON_METHOD, /* not method */
1565                                NULL, NULL, /* pass NULL symbol since
1566                                               symbol is unknown */
1567                                NULL, &symp, NULL, 0);
1568
1569           /* Now fix the expression being evaluated.  */
1570           exp->elts[save_pos1 + 2].symbol = symp;
1571           argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1, noside);
1572         }
1573
1574       if (op == STRUCTOP_STRUCT || op == STRUCTOP_PTR
1575           || (op == OP_SCOPE && function_name != NULL))
1576         {
1577           int static_memfuncp;
1578           char *tstr;
1579
1580           /* Method invocation: stuff "this" as first parameter.
1581              If the method turns out to be static we undo this below.  */
1582           argvec[1] = arg2;
1583
1584           if (op != OP_SCOPE)
1585             {
1586               /* Name of method from expression.  */
1587               tstr = &exp->elts[pc2 + 2].string;
1588             }
1589           else
1590             tstr = function_name;
1591
1592           if (overload_resolution && (exp->language_defn->la_language
1593                                       == language_cplus))
1594             {
1595               /* Language is C++, do some overload resolution before
1596                  evaluation.  */
1597               struct value *valp = NULL;
1598
1599               (void) find_overload_match (&argvec[1], nargs, tstr,
1600                                           METHOD, /* method */
1601                                           &arg2,  /* the object */
1602                                           NULL, &valp, NULL,
1603                                           &static_memfuncp, 0);
1604
1605               if (op == OP_SCOPE && !static_memfuncp)
1606                 {
1607                   /* For the time being, we don't handle this.  */
1608                   error (_("Call to overloaded function %s requires "
1609                            "`this' pointer"),
1610                          function_name);
1611                 }
1612               argvec[1] = arg2; /* the ``this'' pointer */
1613               argvec[0] = valp; /* Use the method found after overload
1614                                    resolution.  */
1615             }
1616           else
1617             /* Non-C++ case -- or no overload resolution.  */
1618             {
1619               struct value *temp = arg2;
1620
1621               argvec[0] = value_struct_elt (&temp, argvec + 1, tstr,
1622                                             &static_memfuncp,
1623                                             op == STRUCTOP_STRUCT
1624                                        ? "structure" : "structure pointer");
1625               /* value_struct_elt updates temp with the correct value
1626                  of the ``this'' pointer if necessary, so modify argvec[1] to
1627                  reflect any ``this'' changes.  */
1628               arg2
1629                 = value_from_longest (lookup_pointer_type(value_type (temp)),
1630                                       value_address (temp)
1631                                       + value_embedded_offset (temp));
1632               argvec[1] = arg2; /* the ``this'' pointer */
1633             }
1634
1635           /* Take out `this' if needed.  */
1636           if (static_memfuncp)
1637             {
1638               argvec[1] = argvec[0];
1639               nargs--;
1640               argvec++;
1641             }
1642         }
1643       else if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
1644         {
1645           /* Pointer to member.  argvec[1] is already set up.  */
1646           argvec[0] = arg1;
1647         }
1648       else if (op == OP_VAR_VALUE || (op == OP_SCOPE && function != NULL))
1649         {
1650           /* Non-member function being called.  */
1651           /* fn: This can only be done for C++ functions.  A C-style function
1652              in a C++ program, for instance, does not have the fields that 
1653              are expected here.  */
1654
1655           if (overload_resolution && (exp->language_defn->la_language
1656                                       == language_cplus))
1657             {
1658               /* Language is C++, do some overload resolution before
1659                  evaluation.  */
1660               struct symbol *symp;
1661               int no_adl = 0;
1662
1663               /* If a scope has been specified disable ADL.  */
1664               if (op == OP_SCOPE)
1665                 no_adl = 1;
1666
1667               if (op == OP_VAR_VALUE)
1668                 function = exp->elts[save_pos1+2].symbol;
1669
1670               (void) find_overload_match (&argvec[1], nargs,
1671                                           NULL,        /* no need for name */
1672                                           NON_METHOD,  /* not method */
1673                                           NULL, function, /* the function */
1674                                           NULL, &symp, NULL, no_adl);
1675
1676               if (op == OP_VAR_VALUE)
1677                 {
1678                   /* Now fix the expression being evaluated.  */
1679                   exp->elts[save_pos1+2].symbol = symp;
1680                   argvec[0] = evaluate_subexp_with_coercion (exp, &save_pos1,
1681                                                              noside);
1682                 }
1683               else
1684                 argvec[0] = value_of_variable (symp, get_selected_block (0));
1685             }
1686           else
1687             {
1688               /* Not C++, or no overload resolution allowed.  */
1689               /* Nothing to be done; argvec already correctly set up.  */
1690             }
1691         }
1692       else
1693         {
1694           /* It is probably a C-style function.  */
1695           /* Nothing to be done; argvec already correctly set up.  */
1696         }
1697
1698     do_call_it:
1699
1700       if (noside == EVAL_SKIP)
1701         goto nosideret;
1702       if (argvec[0] == NULL)
1703         error (_("Cannot evaluate function -- may be inlined"));
1704       if (noside == EVAL_AVOID_SIDE_EFFECTS)
1705         {
1706           /* If the return type doesn't look like a function type, call an
1707              error.  This can happen if somebody tries to turn a variable into
1708              a function call.  This is here because people often want to
1709              call, eg, strcmp, which gdb doesn't know is a function.  If
1710              gdb isn't asked for it's opinion (ie. through "whatis"),
1711              it won't offer it.  */
1712
1713           struct type *ftype = value_type (argvec[0]);
1714
1715           if (TYPE_CODE (ftype) == TYPE_CODE_INTERNAL_FUNCTION)
1716             {
1717               /* We don't know anything about what the internal
1718                  function might return, but we have to return
1719                  something.  */
1720               return value_zero (builtin_type (exp->gdbarch)->builtin_int,
1721                                  not_lval);
1722             }
1723           else if (TYPE_GNU_IFUNC (ftype))
1724             return allocate_value (TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype)));
1725           else if (TYPE_TARGET_TYPE (ftype))
1726             return allocate_value (TYPE_TARGET_TYPE (ftype));
1727           else
1728             error (_("Expression of type other than "
1729                      "\"Function returning ...\" used as function"));
1730         }
1731       switch (TYPE_CODE (value_type (argvec[0])))
1732         {
1733         case TYPE_CODE_INTERNAL_FUNCTION:
1734           return call_internal_function (exp->gdbarch, exp->language_defn,
1735                                          argvec[0], nargs, argvec + 1);
1736         case TYPE_CODE_XMETHOD:
1737           return call_xmethod (argvec[0], nargs, argvec + 1);
1738         default:
1739           return call_function_by_hand (argvec[0], nargs, argvec + 1);
1740         }
1741       /* pai: FIXME save value from call_function_by_hand, then adjust
1742          pc by adjust_fn_pc if +ve.  */
1743
1744     case OP_F77_UNDETERMINED_ARGLIST:
1745
1746       /* Remember that in F77, functions, substring ops and 
1747          array subscript operations cannot be disambiguated 
1748          at parse time.  We have made all array subscript operations, 
1749          substring operations as well as function calls  come here 
1750          and we now have to discover what the heck this thing actually was.
1751          If it is a function, we process just as if we got an OP_FUNCALL.  */
1752
1753       nargs = longest_to_int (exp->elts[pc + 1].longconst);
1754       (*pos) += 2;
1755
1756       /* First determine the type code we are dealing with.  */
1757       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1758       type = check_typedef (value_type (arg1));
1759       code = TYPE_CODE (type);
1760
1761       if (code == TYPE_CODE_PTR)
1762         {
1763           /* Fortran always passes variable to subroutines as pointer.
1764              So we need to look into its target type to see if it is
1765              array, string or function.  If it is, we need to switch
1766              to the target value the original one points to.  */ 
1767           struct type *target_type = check_typedef (TYPE_TARGET_TYPE (type));
1768
1769           if (TYPE_CODE (target_type) == TYPE_CODE_ARRAY
1770               || TYPE_CODE (target_type) == TYPE_CODE_STRING
1771               || TYPE_CODE (target_type) == TYPE_CODE_FUNC)
1772             {
1773               arg1 = value_ind (arg1);
1774               type = check_typedef (value_type (arg1));
1775               code = TYPE_CODE (type);
1776             }
1777         } 
1778
1779       switch (code)
1780         {
1781         case TYPE_CODE_ARRAY:
1782           if (exp->elts[*pos].opcode == OP_F90_RANGE)
1783             return value_f90_subarray (arg1, exp, pos, noside);
1784           else
1785             goto multi_f77_subscript;
1786
1787         case TYPE_CODE_STRING:
1788           if (exp->elts[*pos].opcode == OP_F90_RANGE)
1789             return value_f90_subarray (arg1, exp, pos, noside);
1790           else
1791             {
1792               arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1793               return value_subscript (arg1, value_as_long (arg2));
1794             }
1795
1796         case TYPE_CODE_PTR:
1797         case TYPE_CODE_FUNC:
1798           /* It's a function call.  */
1799           /* Allocate arg vector, including space for the function to be
1800              called in argvec[0] and a terminating NULL.  */
1801           argvec = (struct value **)
1802             alloca (sizeof (struct value *) * (nargs + 2));
1803           argvec[0] = arg1;
1804           tem = 1;
1805           for (; tem <= nargs; tem++)
1806             argvec[tem] = evaluate_subexp_with_coercion (exp, pos, noside);
1807           argvec[tem] = 0;      /* signal end of arglist */
1808           goto do_call_it;
1809
1810         default:
1811           error (_("Cannot perform substring on this type"));
1812         }
1813
1814     case OP_COMPLEX:
1815       /* We have a complex number, There should be 2 floating 
1816          point numbers that compose it.  */
1817       (*pos) += 2;
1818       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1819       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1820
1821       return value_literal_complex (arg1, arg2, exp->elts[pc + 1].type);
1822
1823     case STRUCTOP_STRUCT:
1824       tem = longest_to_int (exp->elts[pc + 1].longconst);
1825       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1826       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1827       if (noside == EVAL_SKIP)
1828         goto nosideret;
1829       arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1830                                NULL, "structure");
1831       if (noside == EVAL_AVOID_SIDE_EFFECTS)
1832         arg3 = value_zero (value_type (arg3), not_lval);
1833       return arg3;
1834
1835     case STRUCTOP_PTR:
1836       tem = longest_to_int (exp->elts[pc + 1].longconst);
1837       (*pos) += 3 + BYTES_TO_EXP_ELEM (tem + 1);
1838       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1839       if (noside == EVAL_SKIP)
1840         goto nosideret;
1841
1842       /* Check to see if operator '->' has been overloaded.  If so replace
1843          arg1 with the value returned by evaluating operator->().  */
1844       while (unop_user_defined_p (op, arg1))
1845         {
1846           volatile struct gdb_exception except;
1847           struct value *value = NULL;
1848           TRY_CATCH (except, RETURN_MASK_ERROR)
1849             {
1850               value = value_x_unop (arg1, op, noside);
1851             }
1852
1853           if (except.reason < 0)
1854             {
1855               if (except.error == NOT_FOUND_ERROR)
1856                 break;
1857               else
1858                 throw_exception (except);
1859             }
1860           arg1 = value;
1861         }
1862
1863       /* JYG: if print object is on we need to replace the base type
1864          with rtti type in order to continue on with successful
1865          lookup of member / method only available in the rtti type.  */
1866       {
1867         struct type *type = value_type (arg1);
1868         struct type *real_type;
1869         int full, top, using_enc;
1870         struct value_print_options opts;
1871
1872         get_user_print_options (&opts);
1873         if (opts.objectprint && TYPE_TARGET_TYPE(type)
1874             && (TYPE_CODE (TYPE_TARGET_TYPE (type)) == TYPE_CODE_CLASS))
1875           {
1876             real_type = value_rtti_indirect_type (arg1, &full, &top,
1877                                                   &using_enc);
1878             if (real_type)
1879                 arg1 = value_cast (real_type, arg1);
1880           }
1881       }
1882
1883       arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
1884                                NULL, "structure pointer");
1885       if (noside == EVAL_AVOID_SIDE_EFFECTS)
1886         arg3 = value_zero (value_type (arg3), not_lval);
1887       return arg3;
1888
1889     case STRUCTOP_MEMBER:
1890     case STRUCTOP_MPTR:
1891       if (op == STRUCTOP_MEMBER)
1892         arg1 = evaluate_subexp_for_address (exp, pos, noside);
1893       else
1894         arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1895
1896       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1897
1898       if (noside == EVAL_SKIP)
1899         goto nosideret;
1900
1901       type = check_typedef (value_type (arg2));
1902       switch (TYPE_CODE (type))
1903         {
1904         case TYPE_CODE_METHODPTR:
1905           if (noside == EVAL_AVOID_SIDE_EFFECTS)
1906             return value_zero (TYPE_TARGET_TYPE (type), not_lval);
1907           else
1908             {
1909               arg2 = cplus_method_ptr_to_value (&arg1, arg2);
1910               gdb_assert (TYPE_CODE (value_type (arg2)) == TYPE_CODE_PTR);
1911               return value_ind (arg2);
1912             }
1913
1914         case TYPE_CODE_MEMBERPTR:
1915           /* Now, convert these values to an address.  */
1916           arg1 = value_cast_pointers (lookup_pointer_type (TYPE_DOMAIN_TYPE (type)),
1917                                       arg1, 1);
1918
1919           mem_offset = value_as_long (arg2);
1920
1921           arg3 = value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
1922                                      value_as_long (arg1) + mem_offset);
1923           return value_ind (arg3);
1924
1925         default:
1926           error (_("non-pointer-to-member value used "
1927                    "in pointer-to-member construct"));
1928         }
1929
1930     case TYPE_INSTANCE:
1931       nargs = longest_to_int (exp->elts[pc + 1].longconst);
1932       arg_types = (struct type **) alloca (nargs * sizeof (struct type *));
1933       for (ix = 0; ix < nargs; ++ix)
1934         arg_types[ix] = exp->elts[pc + 1 + ix + 1].type;
1935
1936       expect_type = make_params (nargs, arg_types);
1937       *(pos) += 3 + nargs;
1938       arg1 = evaluate_subexp_standard (expect_type, exp, pos, noside);
1939       xfree (TYPE_FIELDS (expect_type));
1940       xfree (TYPE_MAIN_TYPE (expect_type));
1941       xfree (expect_type);
1942       return arg1;
1943
1944     case BINOP_CONCAT:
1945       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
1946       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
1947       if (noside == EVAL_SKIP)
1948         goto nosideret;
1949       if (binop_user_defined_p (op, arg1, arg2))
1950         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1951       else
1952         return value_concat (arg1, arg2);
1953
1954     case BINOP_ASSIGN:
1955       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1956       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1957
1958       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1959         return arg1;
1960       if (binop_user_defined_p (op, arg1, arg2))
1961         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
1962       else
1963         return value_assign (arg1, arg2);
1964
1965     case BINOP_ASSIGN_MODIFY:
1966       (*pos) += 2;
1967       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
1968       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
1969       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
1970         return arg1;
1971       op = exp->elts[pc + 1].opcode;
1972       if (binop_user_defined_p (op, arg1, arg2))
1973         return value_x_binop (arg1, arg2, BINOP_ASSIGN_MODIFY, op, noside);
1974       else if (op == BINOP_ADD && ptrmath_type_p (exp->language_defn,
1975                                                   value_type (arg1))
1976                && is_integral_type (value_type (arg2)))
1977         arg2 = value_ptradd (arg1, value_as_long (arg2));
1978       else if (op == BINOP_SUB && ptrmath_type_p (exp->language_defn,
1979                                                   value_type (arg1))
1980                && is_integral_type (value_type (arg2)))
1981         arg2 = value_ptradd (arg1, - value_as_long (arg2));
1982       else
1983         {
1984           struct value *tmp = arg1;
1985
1986           /* For shift and integer exponentiation operations,
1987              only promote the first argument.  */
1988           if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
1989               && is_integral_type (value_type (arg2)))
1990             unop_promote (exp->language_defn, exp->gdbarch, &tmp);
1991           else
1992             binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
1993
1994           arg2 = value_binop (tmp, arg2, op);
1995         }
1996       return value_assign (arg1, arg2);
1997
1998     case BINOP_ADD:
1999       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2000       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2001       if (noside == EVAL_SKIP)
2002         goto nosideret;
2003       if (binop_user_defined_p (op, arg1, arg2))
2004         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2005       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2006                && is_integral_type (value_type (arg2)))
2007         return value_ptradd (arg1, value_as_long (arg2));
2008       else if (ptrmath_type_p (exp->language_defn, value_type (arg2))
2009                && is_integral_type (value_type (arg1)))
2010         return value_ptradd (arg2, value_as_long (arg1));
2011       else
2012         {
2013           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2014           return value_binop (arg1, arg2, BINOP_ADD);
2015         }
2016
2017     case BINOP_SUB:
2018       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2019       arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2020       if (noside == EVAL_SKIP)
2021         goto nosideret;
2022       if (binop_user_defined_p (op, arg1, arg2))
2023         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2024       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2025                && ptrmath_type_p (exp->language_defn, value_type (arg2)))
2026         {
2027           /* FIXME -- should be ptrdiff_t */
2028           type = builtin_type (exp->gdbarch)->builtin_long;
2029           return value_from_longest (type, value_ptrdiff (arg1, arg2));
2030         }
2031       else if (ptrmath_type_p (exp->language_defn, value_type (arg1))
2032                && is_integral_type (value_type (arg2)))
2033         return value_ptradd (arg1, - value_as_long (arg2));
2034       else
2035         {
2036           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2037           return value_binop (arg1, arg2, BINOP_SUB);
2038         }
2039
2040     case BINOP_EXP:
2041     case BINOP_MUL:
2042     case BINOP_DIV:
2043     case BINOP_INTDIV:
2044     case BINOP_REM:
2045     case BINOP_MOD:
2046     case BINOP_LSH:
2047     case BINOP_RSH:
2048     case BINOP_BITWISE_AND:
2049     case BINOP_BITWISE_IOR:
2050     case BINOP_BITWISE_XOR:
2051       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2052       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2053       if (noside == EVAL_SKIP)
2054         goto nosideret;
2055       if (binop_user_defined_p (op, arg1, arg2))
2056         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2057       else
2058         {
2059           /* If EVAL_AVOID_SIDE_EFFECTS and we're dividing by zero,
2060              fudge arg2 to avoid division-by-zero, the caller is
2061              (theoretically) only looking for the type of the result.  */
2062           if (noside == EVAL_AVOID_SIDE_EFFECTS
2063               /* ??? Do we really want to test for BINOP_MOD here?
2064                  The implementation of value_binop gives it a well-defined
2065                  value.  */
2066               && (op == BINOP_DIV
2067                   || op == BINOP_INTDIV
2068                   || op == BINOP_REM
2069                   || op == BINOP_MOD)
2070               && value_logical_not (arg2))
2071             {
2072               struct value *v_one, *retval;
2073
2074               v_one = value_one (value_type (arg2));
2075               binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
2076               retval = value_binop (arg1, v_one, op);
2077               return retval;
2078             }
2079           else
2080             {
2081               /* For shift and integer exponentiation operations,
2082                  only promote the first argument.  */
2083               if ((op == BINOP_LSH || op == BINOP_RSH || op == BINOP_EXP)
2084                   && is_integral_type (value_type (arg2)))
2085                 unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2086               else
2087                 binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2088
2089               return value_binop (arg1, arg2, op);
2090             }
2091         }
2092
2093     case BINOP_SUBSCRIPT:
2094       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2095       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2096       if (noside == EVAL_SKIP)
2097         goto nosideret;
2098       if (binop_user_defined_p (op, arg1, arg2))
2099         return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2100       else
2101         {
2102           /* If the user attempts to subscript something that is not an
2103              array or pointer type (like a plain int variable for example),
2104              then report this as an error.  */
2105
2106           arg1 = coerce_ref (arg1);
2107           type = check_typedef (value_type (arg1));
2108           if (TYPE_CODE (type) != TYPE_CODE_ARRAY
2109               && TYPE_CODE (type) != TYPE_CODE_PTR)
2110             {
2111               if (TYPE_NAME (type))
2112                 error (_("cannot subscript something of type `%s'"),
2113                        TYPE_NAME (type));
2114               else
2115                 error (_("cannot subscript requested type"));
2116             }
2117
2118           if (noside == EVAL_AVOID_SIDE_EFFECTS)
2119             return value_zero (TYPE_TARGET_TYPE (type), VALUE_LVAL (arg1));
2120           else
2121             return value_subscript (arg1, value_as_long (arg2));
2122         }
2123     case MULTI_SUBSCRIPT:
2124       (*pos) += 2;
2125       nargs = longest_to_int (exp->elts[pc + 1].longconst);
2126       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
2127       while (nargs-- > 0)
2128         {
2129           arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2130           /* FIXME:  EVAL_SKIP handling may not be correct.  */
2131           if (noside == EVAL_SKIP)
2132             {
2133               if (nargs > 0)
2134                 {
2135                   continue;
2136                 }
2137               else
2138                 {
2139                   goto nosideret;
2140                 }
2141             }
2142           /* FIXME:  EVAL_AVOID_SIDE_EFFECTS handling may not be correct.  */
2143           if (noside == EVAL_AVOID_SIDE_EFFECTS)
2144             {
2145               /* If the user attempts to subscript something that has no target
2146                  type (like a plain int variable for example), then report this
2147                  as an error.  */
2148
2149               type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
2150               if (type != NULL)
2151                 {
2152                   arg1 = value_zero (type, VALUE_LVAL (arg1));
2153                   noside = EVAL_SKIP;
2154                   continue;
2155                 }
2156               else
2157                 {
2158                   error (_("cannot subscript something of type `%s'"),
2159                          TYPE_NAME (value_type (arg1)));
2160                 }
2161             }
2162
2163           if (binop_user_defined_p (op, arg1, arg2))
2164             {
2165               arg1 = value_x_binop (arg1, arg2, op, OP_NULL, noside);
2166             }
2167           else
2168             {
2169               arg1 = coerce_ref (arg1);
2170               type = check_typedef (value_type (arg1));
2171
2172               switch (TYPE_CODE (type))
2173                 {
2174                 case TYPE_CODE_PTR:
2175                 case TYPE_CODE_ARRAY:
2176                 case TYPE_CODE_STRING:
2177                   arg1 = value_subscript (arg1, value_as_long (arg2));
2178                   break;
2179
2180                 default:
2181                   if (TYPE_NAME (type))
2182                     error (_("cannot subscript something of type `%s'"),
2183                            TYPE_NAME (type));
2184                   else
2185                     error (_("cannot subscript requested type"));
2186                 }
2187             }
2188         }
2189       return (arg1);
2190
2191     multi_f77_subscript:
2192       {
2193         LONGEST subscript_array[MAX_FORTRAN_DIMS];
2194         int ndimensions = 1, i;
2195         struct value *array = arg1;
2196
2197         if (nargs > MAX_FORTRAN_DIMS)
2198           error (_("Too many subscripts for F77 (%d Max)"), MAX_FORTRAN_DIMS);
2199
2200         ndimensions = calc_f77_array_dims (type);
2201
2202         if (nargs != ndimensions)
2203           error (_("Wrong number of subscripts"));
2204
2205         gdb_assert (nargs > 0);
2206
2207         /* Now that we know we have a legal array subscript expression 
2208            let us actually find out where this element exists in the array.  */
2209
2210         /* Take array indices left to right.  */
2211         for (i = 0; i < nargs; i++)
2212           {
2213             /* Evaluate each subscript; it must be a legal integer in F77.  */
2214             arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
2215
2216             /* Fill in the subscript array.  */
2217
2218             subscript_array[i] = value_as_long (arg2);
2219           }
2220
2221         /* Internal type of array is arranged right to left.  */
2222         for (i = nargs; i > 0; i--)
2223           {
2224             struct type *array_type = check_typedef (value_type (array));
2225             LONGEST index = subscript_array[i - 1];
2226
2227             array = value_subscripted_rvalue (array, index,
2228                                               f77_get_lowerbound (array_type));
2229           }
2230
2231         return array;
2232       }
2233
2234     case BINOP_LOGICAL_AND:
2235       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2236       if (noside == EVAL_SKIP)
2237         {
2238           evaluate_subexp (NULL_TYPE, exp, pos, noside);
2239           goto nosideret;
2240         }
2241
2242       oldpos = *pos;
2243       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2244       *pos = oldpos;
2245
2246       if (binop_user_defined_p (op, arg1, arg2))
2247         {
2248           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2249           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2250         }
2251       else
2252         {
2253           tem = value_logical_not (arg1);
2254           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2255                                   (tem ? EVAL_SKIP : noside));
2256           type = language_bool_type (exp->language_defn, exp->gdbarch);
2257           return value_from_longest (type,
2258                              (LONGEST) (!tem && !value_logical_not (arg2)));
2259         }
2260
2261     case BINOP_LOGICAL_OR:
2262       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2263       if (noside == EVAL_SKIP)
2264         {
2265           evaluate_subexp (NULL_TYPE, exp, pos, noside);
2266           goto nosideret;
2267         }
2268
2269       oldpos = *pos;
2270       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2271       *pos = oldpos;
2272
2273       if (binop_user_defined_p (op, arg1, arg2))
2274         {
2275           arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2276           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2277         }
2278       else
2279         {
2280           tem = value_logical_not (arg1);
2281           arg2 = evaluate_subexp (NULL_TYPE, exp, pos,
2282                                   (!tem ? EVAL_SKIP : noside));
2283           type = language_bool_type (exp->language_defn, exp->gdbarch);
2284           return value_from_longest (type,
2285                              (LONGEST) (!tem || !value_logical_not (arg2)));
2286         }
2287
2288     case BINOP_EQUAL:
2289       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2290       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2291       if (noside == EVAL_SKIP)
2292         goto nosideret;
2293       if (binop_user_defined_p (op, arg1, arg2))
2294         {
2295           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2296         }
2297       else
2298         {
2299           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2300           tem = value_equal (arg1, arg2);
2301           type = language_bool_type (exp->language_defn, exp->gdbarch);
2302           return value_from_longest (type, (LONGEST) tem);
2303         }
2304
2305     case BINOP_NOTEQUAL:
2306       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2307       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2308       if (noside == EVAL_SKIP)
2309         goto nosideret;
2310       if (binop_user_defined_p (op, arg1, arg2))
2311         {
2312           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2313         }
2314       else
2315         {
2316           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2317           tem = value_equal (arg1, arg2);
2318           type = language_bool_type (exp->language_defn, exp->gdbarch);
2319           return value_from_longest (type, (LONGEST) ! tem);
2320         }
2321
2322     case BINOP_LESS:
2323       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2324       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2325       if (noside == EVAL_SKIP)
2326         goto nosideret;
2327       if (binop_user_defined_p (op, arg1, arg2))
2328         {
2329           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2330         }
2331       else
2332         {
2333           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2334           tem = value_less (arg1, arg2);
2335           type = language_bool_type (exp->language_defn, exp->gdbarch);
2336           return value_from_longest (type, (LONGEST) tem);
2337         }
2338
2339     case BINOP_GTR:
2340       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2341       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2342       if (noside == EVAL_SKIP)
2343         goto nosideret;
2344       if (binop_user_defined_p (op, arg1, arg2))
2345         {
2346           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2347         }
2348       else
2349         {
2350           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2351           tem = value_less (arg2, arg1);
2352           type = language_bool_type (exp->language_defn, exp->gdbarch);
2353           return value_from_longest (type, (LONGEST) tem);
2354         }
2355
2356     case BINOP_GEQ:
2357       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2358       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2359       if (noside == EVAL_SKIP)
2360         goto nosideret;
2361       if (binop_user_defined_p (op, arg1, arg2))
2362         {
2363           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2364         }
2365       else
2366         {
2367           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2368           tem = value_less (arg2, arg1) || value_equal (arg1, arg2);
2369           type = language_bool_type (exp->language_defn, exp->gdbarch);
2370           return value_from_longest (type, (LONGEST) tem);
2371         }
2372
2373     case BINOP_LEQ:
2374       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2375       arg2 = evaluate_subexp (value_type (arg1), exp, pos, noside);
2376       if (noside == EVAL_SKIP)
2377         goto nosideret;
2378       if (binop_user_defined_p (op, arg1, arg2))
2379         {
2380           return value_x_binop (arg1, arg2, op, OP_NULL, noside);
2381         }
2382       else
2383         {
2384           binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
2385           tem = value_less (arg1, arg2) || value_equal (arg1, arg2);
2386           type = language_bool_type (exp->language_defn, exp->gdbarch);
2387           return value_from_longest (type, (LONGEST) tem);
2388         }
2389
2390     case BINOP_REPEAT:
2391       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2392       arg2 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2393       if (noside == EVAL_SKIP)
2394         goto nosideret;
2395       type = check_typedef (value_type (arg2));
2396       if (TYPE_CODE (type) != TYPE_CODE_INT)
2397         error (_("Non-integral right operand for \"@\" operator."));
2398       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2399         {
2400           return allocate_repeat_value (value_type (arg1),
2401                                      longest_to_int (value_as_long (arg2)));
2402         }
2403       else
2404         return value_repeat (arg1, longest_to_int (value_as_long (arg2)));
2405
2406     case BINOP_COMMA:
2407       evaluate_subexp (NULL_TYPE, exp, pos, noside);
2408       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2409
2410     case UNOP_PLUS:
2411       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2412       if (noside == EVAL_SKIP)
2413         goto nosideret;
2414       if (unop_user_defined_p (op, arg1))
2415         return value_x_unop (arg1, op, noside);
2416       else
2417         {
2418           unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2419           return value_pos (arg1);
2420         }
2421       
2422     case UNOP_NEG:
2423       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2424       if (noside == EVAL_SKIP)
2425         goto nosideret;
2426       if (unop_user_defined_p (op, arg1))
2427         return value_x_unop (arg1, op, noside);
2428       else
2429         {
2430           unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2431           return value_neg (arg1);
2432         }
2433
2434     case UNOP_COMPLEMENT:
2435       /* C++: check for and handle destructor names.  */
2436       op = exp->elts[*pos].opcode;
2437
2438       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2439       if (noside == EVAL_SKIP)
2440         goto nosideret;
2441       if (unop_user_defined_p (UNOP_COMPLEMENT, arg1))
2442         return value_x_unop (arg1, UNOP_COMPLEMENT, noside);
2443       else
2444         {
2445           unop_promote (exp->language_defn, exp->gdbarch, &arg1);
2446           return value_complement (arg1);
2447         }
2448
2449     case UNOP_LOGICAL_NOT:
2450       arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2451       if (noside == EVAL_SKIP)
2452         goto nosideret;
2453       if (unop_user_defined_p (op, arg1))
2454         return value_x_unop (arg1, op, noside);
2455       else
2456         {
2457           type = language_bool_type (exp->language_defn, exp->gdbarch);
2458           return value_from_longest (type, (LONGEST) value_logical_not (arg1));
2459         }
2460
2461     case UNOP_IND:
2462       if (expect_type && TYPE_CODE (expect_type) == TYPE_CODE_PTR)
2463         expect_type = TYPE_TARGET_TYPE (check_typedef (expect_type));
2464       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2465       type = check_typedef (value_type (arg1));
2466       if (TYPE_CODE (type) == TYPE_CODE_METHODPTR
2467           || TYPE_CODE (type) == TYPE_CODE_MEMBERPTR)
2468         error (_("Attempt to dereference pointer "
2469                  "to member without an object"));
2470       if (noside == EVAL_SKIP)
2471         goto nosideret;
2472       if (unop_user_defined_p (op, arg1))
2473         return value_x_unop (arg1, op, noside);
2474       else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2475         {
2476           type = check_typedef (value_type (arg1));
2477           if (TYPE_CODE (type) == TYPE_CODE_PTR
2478               || TYPE_CODE (type) == TYPE_CODE_REF
2479           /* In C you can dereference an array to get the 1st elt.  */
2480               || TYPE_CODE (type) == TYPE_CODE_ARRAY
2481             )
2482             return value_zero (TYPE_TARGET_TYPE (type),
2483                                lval_memory);
2484           else if (TYPE_CODE (type) == TYPE_CODE_INT)
2485             /* GDB allows dereferencing an int.  */
2486             return value_zero (builtin_type (exp->gdbarch)->builtin_int,
2487                                lval_memory);
2488           else
2489             error (_("Attempt to take contents of a non-pointer value."));
2490         }
2491
2492       /* Allow * on an integer so we can cast it to whatever we want.
2493          This returns an int, which seems like the most C-like thing to
2494          do.  "long long" variables are rare enough that
2495          BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
2496       if (TYPE_CODE (type) == TYPE_CODE_INT)
2497         return value_at_lazy (builtin_type (exp->gdbarch)->builtin_int,
2498                               (CORE_ADDR) value_as_address (arg1));
2499       return value_ind (arg1);
2500
2501     case UNOP_ADDR:
2502       /* C++: check for and handle pointer to members.  */
2503
2504       op = exp->elts[*pos].opcode;
2505
2506       if (noside == EVAL_SKIP)
2507         {
2508           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2509           goto nosideret;
2510         }
2511       else
2512         {
2513           struct value *retvalp = evaluate_subexp_for_address (exp, pos,
2514                                                                noside);
2515
2516           return retvalp;
2517         }
2518
2519     case UNOP_SIZEOF:
2520       if (noside == EVAL_SKIP)
2521         {
2522           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2523           goto nosideret;
2524         }
2525       return evaluate_subexp_for_sizeof (exp, pos, noside);
2526
2527     case UNOP_CAST:
2528       (*pos) += 2;
2529       type = exp->elts[pc + 1].type;
2530       arg1 = evaluate_subexp (type, exp, pos, noside);
2531       if (noside == EVAL_SKIP)
2532         goto nosideret;
2533       if (type != value_type (arg1))
2534         arg1 = value_cast (type, arg1);
2535       return arg1;
2536
2537     case UNOP_CAST_TYPE:
2538       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2539       type = value_type (arg1);
2540       arg1 = evaluate_subexp (type, exp, pos, noside);
2541       if (noside == EVAL_SKIP)
2542         goto nosideret;
2543       if (type != value_type (arg1))
2544         arg1 = value_cast (type, arg1);
2545       return arg1;
2546
2547     case UNOP_DYNAMIC_CAST:
2548       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2549       type = value_type (arg1);
2550       arg1 = evaluate_subexp (type, exp, pos, noside);
2551       if (noside == EVAL_SKIP)
2552         goto nosideret;
2553       return value_dynamic_cast (type, arg1);
2554
2555     case UNOP_REINTERPRET_CAST:
2556       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2557       type = value_type (arg1);
2558       arg1 = evaluate_subexp (type, exp, pos, noside);
2559       if (noside == EVAL_SKIP)
2560         goto nosideret;
2561       return value_reinterpret_cast (type, arg1);
2562
2563     case UNOP_MEMVAL:
2564       (*pos) += 2;
2565       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2566       if (noside == EVAL_SKIP)
2567         goto nosideret;
2568       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2569         return value_zero (exp->elts[pc + 1].type, lval_memory);
2570       else
2571         return value_at_lazy (exp->elts[pc + 1].type,
2572                               value_as_address (arg1));
2573
2574     case UNOP_MEMVAL_TYPE:
2575       arg1 = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2576       type = value_type (arg1);
2577       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2578       if (noside == EVAL_SKIP)
2579         goto nosideret;
2580       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2581         return value_zero (type, lval_memory);
2582       else
2583         return value_at_lazy (type, value_as_address (arg1));
2584
2585     case UNOP_MEMVAL_TLS:
2586       (*pos) += 3;
2587       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2588       if (noside == EVAL_SKIP)
2589         goto nosideret;
2590       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2591         return value_zero (exp->elts[pc + 2].type, lval_memory);
2592       else
2593         {
2594           CORE_ADDR tls_addr;
2595
2596           tls_addr = target_translate_tls_address (exp->elts[pc + 1].objfile,
2597                                                    value_as_address (arg1));
2598           return value_at_lazy (exp->elts[pc + 2].type, tls_addr);
2599         }
2600
2601     case UNOP_PREINCREMENT:
2602       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2603       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2604         return arg1;
2605       else if (unop_user_defined_p (op, arg1))
2606         {
2607           return value_x_unop (arg1, op, noside);
2608         }
2609       else
2610         {
2611           if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2612             arg2 = value_ptradd (arg1, 1);
2613           else
2614             {
2615               struct value *tmp = arg1;
2616
2617               arg2 = value_one (value_type (arg1));
2618               binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2619               arg2 = value_binop (tmp, arg2, BINOP_ADD);
2620             }
2621
2622           return value_assign (arg1, arg2);
2623         }
2624
2625     case UNOP_PREDECREMENT:
2626       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2627       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2628         return arg1;
2629       else if (unop_user_defined_p (op, arg1))
2630         {
2631           return value_x_unop (arg1, op, noside);
2632         }
2633       else
2634         {
2635           if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2636             arg2 = value_ptradd (arg1, -1);
2637           else
2638             {
2639               struct value *tmp = arg1;
2640
2641               arg2 = value_one (value_type (arg1));
2642               binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2643               arg2 = value_binop (tmp, arg2, BINOP_SUB);
2644             }
2645
2646           return value_assign (arg1, arg2);
2647         }
2648
2649     case UNOP_POSTINCREMENT:
2650       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2651       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2652         return arg1;
2653       else if (unop_user_defined_p (op, arg1))
2654         {
2655           return value_x_unop (arg1, op, noside);
2656         }
2657       else
2658         {
2659           arg3 = value_non_lval (arg1);
2660
2661           if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2662             arg2 = value_ptradd (arg1, 1);
2663           else
2664             {
2665               struct value *tmp = arg1;
2666
2667               arg2 = value_one (value_type (arg1));
2668               binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2669               arg2 = value_binop (tmp, arg2, BINOP_ADD);
2670             }
2671
2672           value_assign (arg1, arg2);
2673           return arg3;
2674         }
2675
2676     case UNOP_POSTDECREMENT:
2677       arg1 = evaluate_subexp (expect_type, exp, pos, noside);
2678       if (noside == EVAL_SKIP || noside == EVAL_AVOID_SIDE_EFFECTS)
2679         return arg1;
2680       else if (unop_user_defined_p (op, arg1))
2681         {
2682           return value_x_unop (arg1, op, noside);
2683         }
2684       else
2685         {
2686           arg3 = value_non_lval (arg1);
2687
2688           if (ptrmath_type_p (exp->language_defn, value_type (arg1)))
2689             arg2 = value_ptradd (arg1, -1);
2690           else
2691             {
2692               struct value *tmp = arg1;
2693
2694               arg2 = value_one (value_type (arg1));
2695               binop_promote (exp->language_defn, exp->gdbarch, &tmp, &arg2);
2696               arg2 = value_binop (tmp, arg2, BINOP_SUB);
2697             }
2698
2699           value_assign (arg1, arg2);
2700           return arg3;
2701         }
2702
2703     case OP_THIS:
2704       (*pos) += 1;
2705       return value_of_this (exp->language_defn);
2706
2707     case OP_TYPE:
2708       /* The value is not supposed to be used.  This is here to make it
2709          easier to accommodate expressions that contain types.  */
2710       (*pos) += 2;
2711       if (noside == EVAL_SKIP)
2712         goto nosideret;
2713       else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2714         {
2715           struct type *type = exp->elts[pc + 1].type;
2716
2717           /* If this is a typedef, then find its immediate target.  We
2718              use check_typedef to resolve stubs, but we ignore its
2719              result because we do not want to dig past all
2720              typedefs.  */
2721           check_typedef (type);
2722           if (TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
2723             type = TYPE_TARGET_TYPE (type);
2724           return allocate_value (type);
2725         }
2726       else
2727         error (_("Attempt to use a type name as an expression"));
2728
2729     case OP_TYPEOF:
2730     case OP_DECLTYPE:
2731       if (noside == EVAL_SKIP)
2732         {
2733           evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
2734           goto nosideret;
2735         }
2736       else if (noside == EVAL_AVOID_SIDE_EFFECTS)
2737         {
2738           enum exp_opcode sub_op = exp->elts[*pos].opcode;
2739           struct value *result;
2740
2741           result = evaluate_subexp (NULL_TYPE, exp, pos,
2742                                     EVAL_AVOID_SIDE_EFFECTS);
2743
2744           /* 'decltype' has special semantics for lvalues.  */
2745           if (op == OP_DECLTYPE
2746               && (sub_op == BINOP_SUBSCRIPT
2747                   || sub_op == STRUCTOP_MEMBER
2748                   || sub_op == STRUCTOP_MPTR
2749                   || sub_op == UNOP_IND
2750                   || sub_op == STRUCTOP_STRUCT
2751                   || sub_op == STRUCTOP_PTR
2752                   || sub_op == OP_SCOPE))
2753             {
2754               struct type *type = value_type (result);
2755
2756               if (TYPE_CODE (check_typedef (type)) != TYPE_CODE_REF)
2757                 {
2758                   type = lookup_reference_type (type);
2759                   result = allocate_value (type);
2760                 }
2761             }
2762
2763           return result;
2764         }
2765       else
2766         error (_("Attempt to use a type as an expression"));
2767
2768     case OP_TYPEID:
2769       {
2770         struct value *result;
2771         enum exp_opcode sub_op = exp->elts[*pos].opcode;
2772
2773         if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
2774           result = evaluate_subexp (NULL_TYPE, exp, pos,
2775                                     EVAL_AVOID_SIDE_EFFECTS);
2776         else
2777           result = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2778
2779         if (noside != EVAL_NORMAL)
2780           return allocate_value (cplus_typeid_type (exp->gdbarch));
2781
2782         return cplus_typeid (result);
2783       }
2784
2785     default:
2786       /* Removing this case and compiling with gcc -Wall reveals that
2787          a lot of cases are hitting this case.  Some of these should
2788          probably be removed from expression.h; others are legitimate
2789          expressions which are (apparently) not fully implemented.
2790
2791          If there are any cases landing here which mean a user error,
2792          then they should be separate cases, with more descriptive
2793          error messages.  */
2794
2795       error (_("GDB does not (yet) know how to "
2796                "evaluate that kind of expression"));
2797     }
2798
2799 nosideret:
2800   return value_from_longest (builtin_type (exp->gdbarch)->builtin_int, 1);
2801 }
2802 \f
2803 /* Evaluate a subexpression of EXP, at index *POS,
2804    and return the address of that subexpression.
2805    Advance *POS over the subexpression.
2806    If the subexpression isn't an lvalue, get an error.
2807    NOSIDE may be EVAL_AVOID_SIDE_EFFECTS;
2808    then only the type of the result need be correct.  */
2809
2810 static struct value *
2811 evaluate_subexp_for_address (struct expression *exp, int *pos,
2812                              enum noside noside)
2813 {
2814   enum exp_opcode op;
2815   int pc;
2816   struct symbol *var;
2817   struct value *x;
2818   int tem;
2819
2820   pc = (*pos);
2821   op = exp->elts[pc].opcode;
2822
2823   switch (op)
2824     {
2825     case UNOP_IND:
2826       (*pos)++;
2827       x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2828
2829       /* We can't optimize out "&*" if there's a user-defined operator*.  */
2830       if (unop_user_defined_p (op, x))
2831         {
2832           x = value_x_unop (x, op, noside);
2833           goto default_case_after_eval;
2834         }
2835
2836       return coerce_array (x);
2837
2838     case UNOP_MEMVAL:
2839       (*pos) += 3;
2840       return value_cast (lookup_pointer_type (exp->elts[pc + 1].type),
2841                          evaluate_subexp (NULL_TYPE, exp, pos, noside));
2842
2843     case UNOP_MEMVAL_TYPE:
2844       {
2845         struct type *type;
2846
2847         (*pos) += 1;
2848         x = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2849         type = value_type (x);
2850         return value_cast (lookup_pointer_type (type),
2851                            evaluate_subexp (NULL_TYPE, exp, pos, noside));
2852       }
2853
2854     case OP_VAR_VALUE:
2855       var = exp->elts[pc + 2].symbol;
2856
2857       /* C++: The "address" of a reference should yield the address
2858        * of the object pointed to.  Let value_addr() deal with it.  */
2859       if (TYPE_CODE (SYMBOL_TYPE (var)) == TYPE_CODE_REF)
2860         goto default_case;
2861
2862       (*pos) += 4;
2863       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2864         {
2865           struct type *type =
2866             lookup_pointer_type (SYMBOL_TYPE (var));
2867           enum address_class sym_class = SYMBOL_CLASS (var);
2868
2869           if (sym_class == LOC_CONST
2870               || sym_class == LOC_CONST_BYTES
2871               || sym_class == LOC_REGISTER)
2872             error (_("Attempt to take address of register or constant."));
2873
2874           return
2875             value_zero (type, not_lval);
2876         }
2877       else
2878         return address_of_variable (var, exp->elts[pc + 1].block);
2879
2880     case OP_SCOPE:
2881       tem = longest_to_int (exp->elts[pc + 2].longconst);
2882       (*pos) += 5 + BYTES_TO_EXP_ELEM (tem + 1);
2883       x = value_aggregate_elt (exp->elts[pc + 1].type,
2884                                &exp->elts[pc + 3].string,
2885                                NULL, 1, noside);
2886       if (x == NULL)
2887         error (_("There is no field named %s"), &exp->elts[pc + 3].string);
2888       return x;
2889
2890     default:
2891     default_case:
2892       x = evaluate_subexp (NULL_TYPE, exp, pos, noside);
2893     default_case_after_eval:
2894       if (noside == EVAL_AVOID_SIDE_EFFECTS)
2895         {
2896           struct type *type = check_typedef (value_type (x));
2897
2898           if (TYPE_CODE (type) == TYPE_CODE_REF)
2899             return value_zero (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2900                                not_lval);
2901           else if (VALUE_LVAL (x) == lval_memory || value_must_coerce_to_target (x))
2902             return value_zero (lookup_pointer_type (value_type (x)),
2903                                not_lval);
2904           else
2905             error (_("Attempt to take address of "
2906                      "value not located in memory."));
2907         }
2908       return value_addr (x);
2909     }
2910 }
2911
2912 /* Evaluate like `evaluate_subexp' except coercing arrays to pointers.
2913    When used in contexts where arrays will be coerced anyway, this is
2914    equivalent to `evaluate_subexp' but much faster because it avoids
2915    actually fetching array contents (perhaps obsolete now that we have
2916    value_lazy()).
2917
2918    Note that we currently only do the coercion for C expressions, where
2919    arrays are zero based and the coercion is correct.  For other languages,
2920    with nonzero based arrays, coercion loses.  Use CAST_IS_CONVERSION
2921    to decide if coercion is appropriate.  */
2922
2923 struct value *
2924 evaluate_subexp_with_coercion (struct expression *exp,
2925                                int *pos, enum noside noside)
2926 {
2927   enum exp_opcode op;
2928   int pc;
2929   struct value *val;
2930   struct symbol *var;
2931   struct type *type;
2932
2933   pc = (*pos);
2934   op = exp->elts[pc].opcode;
2935
2936   switch (op)
2937     {
2938     case OP_VAR_VALUE:
2939       var = exp->elts[pc + 2].symbol;
2940       type = check_typedef (SYMBOL_TYPE (var));
2941       if (TYPE_CODE (type) == TYPE_CODE_ARRAY
2942           && !TYPE_VECTOR (type)
2943           && CAST_IS_CONVERSION (exp->language_defn))
2944         {
2945           (*pos) += 4;
2946           val = address_of_variable (var, exp->elts[pc + 1].block);
2947           return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
2948                              val);
2949         }
2950       /* FALLTHROUGH */
2951
2952     default:
2953       return evaluate_subexp (NULL_TYPE, exp, pos, noside);
2954     }
2955 }
2956
2957 /* Evaluate a subexpression of EXP, at index *POS,
2958    and return a value for the size of that subexpression.
2959    Advance *POS over the subexpression.  If NOSIDE is EVAL_NORMAL
2960    we allow side-effects on the operand if its type is a variable
2961    length array.   */
2962
2963 static struct value *
2964 evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
2965                             enum noside noside)
2966 {
2967   /* FIXME: This should be size_t.  */
2968   struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
2969   enum exp_opcode op;
2970   int pc;
2971   struct type *type;
2972   struct value *val;
2973
2974   pc = (*pos);
2975   op = exp->elts[pc].opcode;
2976
2977   switch (op)
2978     {
2979       /* This case is handled specially
2980          so that we avoid creating a value for the result type.
2981          If the result type is very big, it's desirable not to
2982          create a value unnecessarily.  */
2983     case UNOP_IND:
2984       (*pos)++;
2985       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
2986       type = check_typedef (value_type (val));
2987       if (TYPE_CODE (type) != TYPE_CODE_PTR
2988           && TYPE_CODE (type) != TYPE_CODE_REF
2989           && TYPE_CODE (type) != TYPE_CODE_ARRAY)
2990         error (_("Attempt to take contents of a non-pointer value."));
2991       type = TYPE_TARGET_TYPE (type);
2992       if (is_dynamic_type (type))
2993         type = value_type (value_ind (val));
2994       return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
2995
2996     case UNOP_MEMVAL:
2997       (*pos) += 3;
2998       type = exp->elts[pc + 1].type;
2999       break;
3000
3001     case UNOP_MEMVAL_TYPE:
3002       (*pos) += 1;
3003       val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3004       type = value_type (val);
3005       break;
3006
3007     case OP_VAR_VALUE:
3008       type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
3009       if (is_dynamic_type (type))
3010         {
3011           val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3012           type = value_type (val);
3013         }
3014       else
3015         (*pos) += 4;
3016       break;
3017
3018       /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
3019          type of the subscript is a variable length array type. In this case we
3020          must re-evaluate the right hand side of the subcription to allow
3021          side-effects. */
3022     case BINOP_SUBSCRIPT:
3023       if (noside == EVAL_NORMAL)
3024         {
3025           int pc = (*pos) + 1;
3026
3027           val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
3028           type = check_typedef (value_type (val));
3029           if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3030             {
3031               type = check_typedef (TYPE_TARGET_TYPE (type));
3032               if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
3033                 {
3034                   type = TYPE_INDEX_TYPE (type);
3035                   /* Only re-evaluate the right hand side if the resulting type
3036                      is a variable length type.  */
3037                   if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
3038                     {
3039                       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
3040                       return value_from_longest
3041                         (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
3042                     }
3043                 }
3044             }
3045         }
3046
3047       /* Fall through.  */
3048
3049     default:
3050       val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
3051       type = value_type (val);
3052       break;
3053     }
3054
3055   /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
3056      "When applied to a reference or a reference type, the result is
3057      the size of the referenced type."  */
3058   CHECK_TYPEDEF (type);
3059   if (exp->language_defn->la_language == language_cplus
3060       && TYPE_CODE (type) == TYPE_CODE_REF)
3061     type = check_typedef (TYPE_TARGET_TYPE (type));
3062   return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
3063 }
3064
3065 /* Parse a type expression in the string [P..P+LENGTH).  */
3066
3067 struct type *
3068 parse_and_eval_type (char *p, int length)
3069 {
3070   char *tmp = (char *) alloca (length + 4);
3071   struct expression *expr;
3072
3073   tmp[0] = '(';
3074   memcpy (tmp + 1, p, length);
3075   tmp[length + 1] = ')';
3076   tmp[length + 2] = '0';
3077   tmp[length + 3] = '\0';
3078   expr = parse_expression (tmp);
3079   if (expr->elts[0].opcode != UNOP_CAST)
3080     error (_("Internal error in eval_type."));
3081   return expr->elts[1].type;
3082 }
3083
3084 int
3085 calc_f77_array_dims (struct type *array_type)
3086 {
3087   int ndimen = 1;
3088   struct type *tmp_type;
3089
3090   if ((TYPE_CODE (array_type) != TYPE_CODE_ARRAY))
3091     error (_("Can't get dimensions for a non-array type"));
3092
3093   tmp_type = array_type;
3094
3095   while ((tmp_type = TYPE_TARGET_TYPE (tmp_type)))
3096     {
3097       if (TYPE_CODE (tmp_type) == TYPE_CODE_ARRAY)
3098         ++ndimen;
3099     }
3100   return ndimen;
3101 }