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