GDB (API)
/home/stan/gdb/src/gdb/valops.c
Go to the documentation of this file.
00001 /* Perform non-arithmetic operations on values, for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "value.h"
00024 #include "frame.h"
00025 #include "inferior.h"
00026 #include "gdbcore.h"
00027 #include "target.h"
00028 #include "demangle.h"
00029 #include "language.h"
00030 #include "gdbcmd.h"
00031 #include "regcache.h"
00032 #include "cp-abi.h"
00033 #include "block.h"
00034 #include "infcall.h"
00035 #include "dictionary.h"
00036 #include "cp-support.h"
00037 #include "dfp.h"
00038 #include "tracepoint.h"
00039 #include <errno.h>
00040 #include "gdb_string.h"
00041 #include "gdb_assert.h"
00042 #include "cp-support.h"
00043 #include "observer.h"
00044 #include "objfiles.h"
00045 #include "symtab.h"
00046 #include "exceptions.h"
00047 
00048 extern unsigned int overload_debug;
00049 /* Local functions.  */
00050 
00051 static int typecmp (int staticp, int varargs, int nargs,
00052                     struct field t1[], struct value *t2[]);
00053 
00054 static struct value *search_struct_field (const char *, struct value *, 
00055                                           int, struct type *, int);
00056 
00057 static struct value *search_struct_method (const char *, struct value **,
00058                                            struct value **,
00059                                            int, int *, struct type *);
00060 
00061 static int find_oload_champ_namespace (struct value **, int,
00062                                        const char *, const char *,
00063                                        struct symbol ***,
00064                                        struct badness_vector **,
00065                                        const int no_adl);
00066 
00067 static
00068 int find_oload_champ_namespace_loop (struct value **, int,
00069                                      const char *, const char *,
00070                                      int, struct symbol ***,
00071                                      struct badness_vector **, int *,
00072                                      const int no_adl);
00073 
00074 static int find_oload_champ (struct value **, int, int, int,
00075                              struct fn_field *, struct symbol **,
00076                              struct badness_vector **);
00077 
00078 static int oload_method_static (int, struct fn_field *, int);
00079 
00080 enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };
00081 
00082 static enum
00083 oload_classification classify_oload_match (struct badness_vector *,
00084                                            int, int);
00085 
00086 static struct value *value_struct_elt_for_reference (struct type *,
00087                                                      int, struct type *,
00088                                                      char *,
00089                                                      struct type *,
00090                                                      int, enum noside);
00091 
00092 static struct value *value_namespace_elt (const struct type *,
00093                                           char *, int , enum noside);
00094 
00095 static struct value *value_maybe_namespace_elt (const struct type *,
00096                                                 char *, int,
00097                                                 enum noside);
00098 
00099 static CORE_ADDR allocate_space_in_inferior (int);
00100 
00101 static struct value *cast_into_complex (struct type *, struct value *);
00102 
00103 static struct fn_field *find_method_list (struct value **, const char *,
00104                                           int, struct type *, int *,
00105                                           struct type **, int *);
00106 
00107 void _initialize_valops (void);
00108 
00109 #if 0
00110 /* Flag for whether we want to abandon failed expression evals by
00111    default.  */
00112 
00113 static int auto_abandon = 0;
00114 #endif
00115 
00116 int overload_resolution = 0;
00117 static void
00118 show_overload_resolution (struct ui_file *file, int from_tty,
00119                           struct cmd_list_element *c, 
00120                           const char *value)
00121 {
00122   fprintf_filtered (file, _("Overload resolution in evaluating "
00123                             "C++ functions is %s.\n"),
00124                     value);
00125 }
00126 
00127 /* Find the address of function name NAME in the inferior.  If OBJF_P
00128    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
00129    is defined.  */
00130 
00131 struct value *
00132 find_function_in_inferior (const char *name, struct objfile **objf_p)
00133 {
00134   struct symbol *sym;
00135 
00136   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
00137   if (sym != NULL)
00138     {
00139       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
00140         {
00141           error (_("\"%s\" exists in this program but is not a function."),
00142                  name);
00143         }
00144 
00145       if (objf_p)
00146         *objf_p = SYMBOL_SYMTAB (sym)->objfile;
00147 
00148       return value_of_variable (sym, NULL);
00149     }
00150   else
00151     {
00152       struct bound_minimal_symbol msymbol = 
00153         lookup_bound_minimal_symbol (name);
00154 
00155       if (msymbol.minsym != NULL)
00156         {
00157           struct objfile *objfile = msymbol.objfile;
00158           struct gdbarch *gdbarch = get_objfile_arch (objfile);
00159 
00160           struct type *type;
00161           CORE_ADDR maddr;
00162           type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
00163           type = lookup_function_type (type);
00164           type = lookup_pointer_type (type);
00165           maddr = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
00166 
00167           if (objf_p)
00168             *objf_p = objfile;
00169 
00170           return value_from_pointer (type, maddr);
00171         }
00172       else
00173         {
00174           if (!target_has_execution)
00175             error (_("evaluation of this expression "
00176                      "requires the target program to be active"));
00177           else
00178             error (_("evaluation of this expression requires the "
00179                      "program to have a function \"%s\"."),
00180                    name);
00181         }
00182     }
00183 }
00184 
00185 /* Allocate NBYTES of space in the inferior using the inferior's
00186    malloc and return a value that is a pointer to the allocated
00187    space.  */
00188 
00189 struct value *
00190 value_allocate_space_in_inferior (int len)
00191 {
00192   struct objfile *objf;
00193   struct value *val = find_function_in_inferior ("malloc", &objf);
00194   struct gdbarch *gdbarch = get_objfile_arch (objf);
00195   struct value *blocklen;
00196 
00197   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
00198   val = call_function_by_hand (val, 1, &blocklen);
00199   if (value_logical_not (val))
00200     {
00201       if (!target_has_execution)
00202         error (_("No memory available to program now: "
00203                  "you need to start the target first"));
00204       else
00205         error (_("No memory available to program: call to malloc failed"));
00206     }
00207   return val;
00208 }
00209 
00210 static CORE_ADDR
00211 allocate_space_in_inferior (int len)
00212 {
00213   return value_as_long (value_allocate_space_in_inferior (len));
00214 }
00215 
00216 /* Cast struct value VAL to type TYPE and return as a value.
00217    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
00218    for this to work.  Typedef to one of the codes is permitted.
00219    Returns NULL if the cast is neither an upcast nor a downcast.  */
00220 
00221 static struct value *
00222 value_cast_structs (struct type *type, struct value *v2)
00223 {
00224   struct type *t1;
00225   struct type *t2;
00226   struct value *v;
00227 
00228   gdb_assert (type != NULL && v2 != NULL);
00229 
00230   t1 = check_typedef (type);
00231   t2 = check_typedef (value_type (v2));
00232 
00233   /* Check preconditions.  */
00234   gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
00235                || TYPE_CODE (t1) == TYPE_CODE_UNION)
00236               && !!"Precondition is that type is of STRUCT or UNION kind.");
00237   gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
00238                || TYPE_CODE (t2) == TYPE_CODE_UNION)
00239               && !!"Precondition is that value is of STRUCT or UNION kind");
00240 
00241   if (TYPE_NAME (t1) != NULL
00242       && TYPE_NAME (t2) != NULL
00243       && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
00244     return NULL;
00245 
00246   /* Upcasting: look in the type of the source to see if it contains the
00247      type of the target as a superclass.  If so, we'll need to
00248      offset the pointer rather than just change its type.  */
00249   if (TYPE_NAME (t1) != NULL)
00250     {
00251       v = search_struct_field (type_name_no_tag (t1),
00252                                v2, 0, t2, 1);
00253       if (v)
00254         return v;
00255     }
00256 
00257   /* Downcasting: look in the type of the target to see if it contains the
00258      type of the source as a superclass.  If so, we'll need to
00259      offset the pointer rather than just change its type.  */
00260   if (TYPE_NAME (t2) != NULL)
00261     {
00262       /* Try downcasting using the run-time type of the value.  */
00263       int full, top, using_enc;
00264       struct type *real_type;
00265 
00266       real_type = value_rtti_type (v2, &full, &top, &using_enc);
00267       if (real_type)
00268         {
00269           v = value_full_object (v2, real_type, full, top, using_enc);
00270           v = value_at_lazy (real_type, value_address (v));
00271 
00272           /* We might be trying to cast to the outermost enclosing
00273              type, in which case search_struct_field won't work.  */
00274           if (TYPE_NAME (real_type) != NULL
00275               && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
00276             return v;
00277 
00278           v = search_struct_field (type_name_no_tag (t2), v, 0, real_type, 1);
00279           if (v)
00280             return v;
00281         }
00282 
00283       /* Try downcasting using information from the destination type
00284          T2.  This wouldn't work properly for classes with virtual
00285          bases, but those were handled above.  */
00286       v = search_struct_field (type_name_no_tag (t2),
00287                                value_zero (t1, not_lval), 0, t1, 1);
00288       if (v)
00289         {
00290           /* Downcasting is possible (t1 is superclass of v2).  */
00291           CORE_ADDR addr2 = value_address (v2);
00292 
00293           addr2 -= value_address (v) + value_embedded_offset (v);
00294           return value_at (type, addr2);
00295         }
00296     }
00297 
00298   return NULL;
00299 }
00300 
00301 /* Cast one pointer or reference type to another.  Both TYPE and
00302    the type of ARG2 should be pointer types, or else both should be
00303    reference types.  If SUBCLASS_CHECK is non-zero, this will force a
00304    check to see whether TYPE is a superclass of ARG2's type.  If
00305    SUBCLASS_CHECK is zero, then the subclass check is done only when
00306    ARG2 is itself non-zero.  Returns the new pointer or reference.  */
00307 
00308 struct value *
00309 value_cast_pointers (struct type *type, struct value *arg2,
00310                      int subclass_check)
00311 {
00312   struct type *type1 = check_typedef (type);
00313   struct type *type2 = check_typedef (value_type (arg2));
00314   struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
00315   struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));
00316 
00317   if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
00318       && TYPE_CODE (t2) == TYPE_CODE_STRUCT
00319       && (subclass_check || !value_logical_not (arg2)))
00320     {
00321       struct value *v2;
00322 
00323       if (TYPE_CODE (type2) == TYPE_CODE_REF)
00324         v2 = coerce_ref (arg2);
00325       else
00326         v2 = value_ind (arg2);
00327       gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
00328                   == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
00329       v2 = value_cast_structs (t1, v2);
00330       /* At this point we have what we can have, un-dereference if needed.  */
00331       if (v2)
00332         {
00333           struct value *v = value_addr (v2);
00334 
00335           deprecated_set_value_type (v, type);
00336           return v;
00337         }
00338    }
00339 
00340   /* No superclass found, just change the pointer type.  */
00341   arg2 = value_copy (arg2);
00342   deprecated_set_value_type (arg2, type);
00343   set_value_enclosing_type (arg2, type);
00344   set_value_pointed_to_offset (arg2, 0);        /* pai: chk_val */
00345   return arg2;
00346 }
00347 
00348 /* Cast value ARG2 to type TYPE and return as a value.
00349    More general than a C cast: accepts any two types of the same length,
00350    and if ARG2 is an lvalue it can be cast into anything at all.  */
00351 /* In C++, casts may change pointer or object representations.  */
00352 
00353 struct value *
00354 value_cast (struct type *type, struct value *arg2)
00355 {
00356   enum type_code code1;
00357   enum type_code code2;
00358   int scalar;
00359   struct type *type2;
00360 
00361   int convert_to_boolean = 0;
00362 
00363   if (value_type (arg2) == type)
00364     return arg2;
00365 
00366   code1 = TYPE_CODE (check_typedef (type));
00367 
00368   /* Check if we are casting struct reference to struct reference.  */
00369   if (code1 == TYPE_CODE_REF)
00370     {
00371       /* We dereference type; then we recurse and finally
00372          we generate value of the given reference.  Nothing wrong with 
00373          that.  */
00374       struct type *t1 = check_typedef (type);
00375       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
00376       struct value *val =  value_cast (dereftype, arg2);
00377 
00378       return value_ref (val); 
00379     }
00380 
00381   code2 = TYPE_CODE (check_typedef (value_type (arg2)));
00382 
00383   if (code2 == TYPE_CODE_REF)
00384     /* We deref the value and then do the cast.  */
00385     return value_cast (type, coerce_ref (arg2)); 
00386 
00387   CHECK_TYPEDEF (type);
00388   code1 = TYPE_CODE (type);
00389   arg2 = coerce_ref (arg2);
00390   type2 = check_typedef (value_type (arg2));
00391 
00392   /* You can't cast to a reference type.  See value_cast_pointers
00393      instead.  */
00394   gdb_assert (code1 != TYPE_CODE_REF);
00395 
00396   /* A cast to an undetermined-length array_type, such as 
00397      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
00398      where N is sizeof(OBJECT)/sizeof(TYPE).  */
00399   if (code1 == TYPE_CODE_ARRAY)
00400     {
00401       struct type *element_type = TYPE_TARGET_TYPE (type);
00402       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));
00403 
00404       if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
00405         {
00406           struct type *range_type = TYPE_INDEX_TYPE (type);
00407           int val_length = TYPE_LENGTH (type2);
00408           LONGEST low_bound, high_bound, new_length;
00409 
00410           if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
00411             low_bound = 0, high_bound = 0;
00412           new_length = val_length / element_length;
00413           if (val_length % element_length != 0)
00414             warning (_("array element type size does not "
00415                        "divide object size in cast"));
00416           /* FIXME-type-allocation: need a way to free this type when
00417              we are done with it.  */
00418           range_type = create_range_type ((struct type *) NULL,
00419                                           TYPE_TARGET_TYPE (range_type),
00420                                           low_bound,
00421                                           new_length + low_bound - 1);
00422           deprecated_set_value_type (arg2, 
00423                                      create_array_type ((struct type *) NULL,
00424                                                         element_type, 
00425                                                         range_type));
00426           return arg2;
00427         }
00428     }
00429 
00430   if (current_language->c_style_arrays
00431       && TYPE_CODE (type2) == TYPE_CODE_ARRAY
00432       && !TYPE_VECTOR (type2))
00433     arg2 = value_coerce_array (arg2);
00434 
00435   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
00436     arg2 = value_coerce_function (arg2);
00437 
00438   type2 = check_typedef (value_type (arg2));
00439   code2 = TYPE_CODE (type2);
00440 
00441   if (code1 == TYPE_CODE_COMPLEX)
00442     return cast_into_complex (type, arg2);
00443   if (code1 == TYPE_CODE_BOOL)
00444     {
00445       code1 = TYPE_CODE_INT;
00446       convert_to_boolean = 1;
00447     }
00448   if (code1 == TYPE_CODE_CHAR)
00449     code1 = TYPE_CODE_INT;
00450   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
00451     code2 = TYPE_CODE_INT;
00452 
00453   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
00454             || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
00455             || code2 == TYPE_CODE_RANGE);
00456 
00457   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
00458       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
00459       && TYPE_NAME (type) != 0)
00460     {
00461       struct value *v = value_cast_structs (type, arg2);
00462 
00463       if (v)
00464         return v;
00465     }
00466 
00467   if (code1 == TYPE_CODE_FLT && scalar)
00468     return value_from_double (type, value_as_double (arg2));
00469   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
00470     {
00471       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
00472       int dec_len = TYPE_LENGTH (type);
00473       gdb_byte dec[16];
00474 
00475       if (code2 == TYPE_CODE_FLT)
00476         decimal_from_floating (arg2, dec, dec_len, byte_order);
00477       else if (code2 == TYPE_CODE_DECFLOAT)
00478         decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
00479                          byte_order, dec, dec_len, byte_order);
00480       else
00481         /* The only option left is an integral type.  */
00482         decimal_from_integral (arg2, dec, dec_len, byte_order);
00483 
00484       return value_from_decfloat (type, dec);
00485     }
00486   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
00487             || code1 == TYPE_CODE_RANGE)
00488            && (scalar || code2 == TYPE_CODE_PTR
00489                || code2 == TYPE_CODE_MEMBERPTR))
00490     {
00491       LONGEST longest;
00492 
00493       /* When we cast pointers to integers, we mustn't use
00494          gdbarch_pointer_to_address to find the address the pointer
00495          represents, as value_as_long would.  GDB should evaluate
00496          expressions just as the compiler would --- and the compiler
00497          sees a cast as a simple reinterpretation of the pointer's
00498          bits.  */
00499       if (code2 == TYPE_CODE_PTR)
00500         longest = extract_unsigned_integer
00501                     (value_contents (arg2), TYPE_LENGTH (type2),
00502                      gdbarch_byte_order (get_type_arch (type2)));
00503       else
00504         longest = value_as_long (arg2);
00505       return value_from_longest (type, convert_to_boolean ?
00506                                  (LONGEST) (longest ? 1 : 0) : longest);
00507     }
00508   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT  
00509                                       || code2 == TYPE_CODE_ENUM 
00510                                       || code2 == TYPE_CODE_RANGE))
00511     {
00512       /* TYPE_LENGTH (type) is the length of a pointer, but we really
00513          want the length of an address! -- we are really dealing with
00514          addresses (i.e., gdb representations) not pointers (i.e.,
00515          target representations) here.
00516 
00517          This allows things like "print *(int *)0x01000234" to work
00518          without printing a misleading message -- which would
00519          otherwise occur when dealing with a target having two byte
00520          pointers and four byte addresses.  */
00521 
00522       int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
00523       LONGEST longest = value_as_long (arg2);
00524 
00525       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
00526         {
00527           if (longest >= ((LONGEST) 1 << addr_bit)
00528               || longest <= -((LONGEST) 1 << addr_bit))
00529             warning (_("value truncated"));
00530         }
00531       return value_from_longest (type, longest);
00532     }
00533   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
00534            && value_as_long (arg2) == 0)
00535     {
00536       struct value *result = allocate_value (type);
00537 
00538       cplus_make_method_ptr (type, value_contents_writeable (result), 0, 0);
00539       return result;
00540     }
00541   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
00542            && value_as_long (arg2) == 0)
00543     {
00544       /* The Itanium C++ ABI represents NULL pointers to members as
00545          minus one, instead of biasing the normal case.  */
00546       return value_from_longest (type, -1);
00547     }
00548   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
00549            && code2 == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)
00550            && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
00551     error (_("Cannot convert between vector values of different sizes"));
00552   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar
00553            && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
00554     error (_("can only cast scalar to vector of same size"));
00555   else if (code1 == TYPE_CODE_VOID)
00556     {
00557       return value_zero (type, not_lval);
00558     }
00559   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
00560     {
00561       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
00562         return value_cast_pointers (type, arg2, 0);
00563 
00564       arg2 = value_copy (arg2);
00565       deprecated_set_value_type (arg2, type);
00566       set_value_enclosing_type (arg2, type);
00567       set_value_pointed_to_offset (arg2, 0);    /* pai: chk_val */
00568       return arg2;
00569     }
00570   else if (VALUE_LVAL (arg2) == lval_memory)
00571     return value_at_lazy (type, value_address (arg2));
00572   else
00573     {
00574       error (_("Invalid cast."));
00575       return 0;
00576     }
00577 }
00578 
00579 /* The C++ reinterpret_cast operator.  */
00580 
00581 struct value *
00582 value_reinterpret_cast (struct type *type, struct value *arg)
00583 {
00584   struct value *result;
00585   struct type *real_type = check_typedef (type);
00586   struct type *arg_type, *dest_type;
00587   int is_ref = 0;
00588   enum type_code dest_code, arg_code;
00589 
00590   /* Do reference, function, and array conversion.  */
00591   arg = coerce_array (arg);
00592 
00593   /* Attempt to preserve the type the user asked for.  */
00594   dest_type = type;
00595 
00596   /* If we are casting to a reference type, transform
00597      reinterpret_cast<T&>(V) to *reinterpret_cast<T*>(&V).  */
00598   if (TYPE_CODE (real_type) == TYPE_CODE_REF)
00599     {
00600       is_ref = 1;
00601       arg = value_addr (arg);
00602       dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
00603       real_type = lookup_pointer_type (real_type);
00604     }
00605 
00606   arg_type = value_type (arg);
00607 
00608   dest_code = TYPE_CODE (real_type);
00609   arg_code = TYPE_CODE (arg_type);
00610 
00611   /* We can convert pointer types, or any pointer type to int, or int
00612      type to pointer.  */
00613   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
00614       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
00615       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
00616       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
00617       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
00618       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
00619       || (dest_code == arg_code
00620           && (dest_code == TYPE_CODE_PTR
00621               || dest_code == TYPE_CODE_METHODPTR
00622               || dest_code == TYPE_CODE_MEMBERPTR)))
00623     result = value_cast (dest_type, arg);
00624   else
00625     error (_("Invalid reinterpret_cast"));
00626 
00627   if (is_ref)
00628     result = value_cast (type, value_ref (value_ind (result)));
00629 
00630   return result;
00631 }
00632 
00633 /* A helper for value_dynamic_cast.  This implements the first of two
00634    runtime checks: we iterate over all the base classes of the value's
00635    class which are equal to the desired class; if only one of these
00636    holds the value, then it is the answer.  */
00637 
00638 static int
00639 dynamic_cast_check_1 (struct type *desired_type,
00640                       const gdb_byte *valaddr,
00641                       int embedded_offset,
00642                       CORE_ADDR address,
00643                       struct value *val,
00644                       struct type *search_type,
00645                       CORE_ADDR arg_addr,
00646                       struct type *arg_type,
00647                       struct value **result)
00648 {
00649   int i, result_count = 0;
00650 
00651   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
00652     {
00653       int offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
00654                                      address, val);
00655 
00656       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
00657         {
00658           if (address + embedded_offset + offset >= arg_addr
00659               && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
00660             {
00661               ++result_count;
00662               if (!*result)
00663                 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
00664                                          address + embedded_offset + offset);
00665             }
00666         }
00667       else
00668         result_count += dynamic_cast_check_1 (desired_type,
00669                                               valaddr,
00670                                               embedded_offset + offset,
00671                                               address, val,
00672                                               TYPE_BASECLASS (search_type, i),
00673                                               arg_addr,
00674                                               arg_type,
00675                                               result);
00676     }
00677 
00678   return result_count;
00679 }
00680 
00681 /* A helper for value_dynamic_cast.  This implements the second of two
00682    runtime checks: we look for a unique public sibling class of the
00683    argument's declared class.  */
00684 
00685 static int
00686 dynamic_cast_check_2 (struct type *desired_type,
00687                       const gdb_byte *valaddr,
00688                       int embedded_offset,
00689                       CORE_ADDR address,
00690                       struct value *val,
00691                       struct type *search_type,
00692                       struct value **result)
00693 {
00694   int i, result_count = 0;
00695 
00696   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
00697     {
00698       int offset;
00699 
00700       if (! BASETYPE_VIA_PUBLIC (search_type, i))
00701         continue;
00702 
00703       offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
00704                                  address, val);
00705       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
00706         {
00707           ++result_count;
00708           if (*result == NULL)
00709             *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
00710                                      address + embedded_offset + offset);
00711         }
00712       else
00713         result_count += dynamic_cast_check_2 (desired_type,
00714                                               valaddr,
00715                                               embedded_offset + offset,
00716                                               address, val,
00717                                               TYPE_BASECLASS (search_type, i),
00718                                               result);
00719     }
00720 
00721   return result_count;
00722 }
00723 
00724 /* The C++ dynamic_cast operator.  */
00725 
00726 struct value *
00727 value_dynamic_cast (struct type *type, struct value *arg)
00728 {
00729   int full, top, using_enc;
00730   struct type *resolved_type = check_typedef (type);
00731   struct type *arg_type = check_typedef (value_type (arg));
00732   struct type *class_type, *rtti_type;
00733   struct value *result, *tem, *original_arg = arg;
00734   CORE_ADDR addr;
00735   int is_ref = TYPE_CODE (resolved_type) == TYPE_CODE_REF;
00736 
00737   if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
00738       && TYPE_CODE (resolved_type) != TYPE_CODE_REF)
00739     error (_("Argument to dynamic_cast must be a pointer or reference type"));
00740   if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
00741       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_CLASS)
00742     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));
00743 
00744   class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
00745   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
00746     {
00747       if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
00748           && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
00749                 && value_as_long (arg) == 0))
00750         error (_("Argument to dynamic_cast does not have pointer type"));
00751       if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
00752         {
00753           arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
00754           if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
00755             error (_("Argument to dynamic_cast does "
00756                      "not have pointer to class type"));
00757         }
00758 
00759       /* Handle NULL pointers.  */
00760       if (value_as_long (arg) == 0)
00761         return value_zero (type, not_lval);
00762 
00763       arg = value_ind (arg);
00764     }
00765   else
00766     {
00767       if (TYPE_CODE (arg_type) != TYPE_CODE_CLASS)
00768         error (_("Argument to dynamic_cast does not have class type"));
00769     }
00770 
00771   /* If the classes are the same, just return the argument.  */
00772   if (class_types_same_p (class_type, arg_type))
00773     return value_cast (type, arg);
00774 
00775   /* If the target type is a unique base class of the argument's
00776      declared type, just cast it.  */
00777   if (is_ancestor (class_type, arg_type))
00778     {
00779       if (is_unique_ancestor (class_type, arg))
00780         return value_cast (type, original_arg);
00781       error (_("Ambiguous dynamic_cast"));
00782     }
00783 
00784   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
00785   if (! rtti_type)
00786     error (_("Couldn't determine value's most derived type for dynamic_cast"));
00787 
00788   /* Compute the most derived object's address.  */
00789   addr = value_address (arg);
00790   if (full)
00791     {
00792       /* Done.  */
00793     }
00794   else if (using_enc)
00795     addr += top;
00796   else
00797     addr += top + value_embedded_offset (arg);
00798 
00799   /* dynamic_cast<void *> means to return a pointer to the
00800      most-derived object.  */
00801   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
00802       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
00803     return value_at_lazy (type, addr);
00804 
00805   tem = value_at (type, addr);
00806 
00807   /* The first dynamic check specified in 5.2.7.  */
00808   if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
00809     {
00810       if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
00811         return tem;
00812       result = NULL;
00813       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
00814                                 value_contents_for_printing (tem),
00815                                 value_embedded_offset (tem),
00816                                 value_address (tem), tem,
00817                                 rtti_type, addr,
00818                                 arg_type,
00819                                 &result) == 1)
00820         return value_cast (type,
00821                            is_ref ? value_ref (result) : value_addr (result));
00822     }
00823 
00824   /* The second dynamic check specified in 5.2.7.  */
00825   result = NULL;
00826   if (is_public_ancestor (arg_type, rtti_type)
00827       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
00828                                value_contents_for_printing (tem),
00829                                value_embedded_offset (tem),
00830                                value_address (tem), tem,
00831                                rtti_type, &result) == 1)
00832     return value_cast (type,
00833                        is_ref ? value_ref (result) : value_addr (result));
00834 
00835   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
00836     return value_zero (type, not_lval);
00837 
00838   error (_("dynamic_cast failed"));
00839 }
00840 
00841 /* Create a value of type TYPE that is zero, and return it.  */
00842 
00843 struct value *
00844 value_zero (struct type *type, enum lval_type lv)
00845 {
00846   struct value *val = allocate_value (type);
00847 
00848   VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
00849   return val;
00850 }
00851 
00852 /* Create a not_lval value of numeric type TYPE that is one, and return it.  */
00853 
00854 struct value *
00855 value_one (struct type *type)
00856 {
00857   struct type *type1 = check_typedef (type);
00858   struct value *val;
00859 
00860   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
00861     {
00862       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
00863       gdb_byte v[16];
00864 
00865       decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
00866       val = value_from_decfloat (type, v);
00867     }
00868   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
00869     {
00870       val = value_from_double (type, (DOUBLEST) 1);
00871     }
00872   else if (is_integral_type (type1))
00873     {
00874       val = value_from_longest (type, (LONGEST) 1);
00875     }
00876   else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
00877     {
00878       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
00879       int i;
00880       LONGEST low_bound, high_bound;
00881       struct value *tmp;
00882 
00883       if (!get_array_bounds (type1, &low_bound, &high_bound))
00884         error (_("Could not determine the vector bounds"));
00885 
00886       val = allocate_value (type);
00887       for (i = 0; i < high_bound - low_bound + 1; i++)
00888         {
00889           tmp = value_one (eltype);
00890           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
00891                   value_contents_all (tmp), TYPE_LENGTH (eltype));
00892         }
00893     }
00894   else
00895     {
00896       error (_("Not a numeric type."));
00897     }
00898 
00899   /* value_one result is never used for assignments to.  */
00900   gdb_assert (VALUE_LVAL (val) == not_lval);
00901 
00902   return val;
00903 }
00904 
00905 /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.  */
00906 
00907 static struct value *
00908 get_value_at (struct type *type, CORE_ADDR addr, int lazy)
00909 {
00910   struct value *val;
00911 
00912   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
00913     error (_("Attempt to dereference a generic pointer."));
00914 
00915   val = value_from_contents_and_address (type, NULL, addr);
00916 
00917   if (!lazy)
00918     value_fetch_lazy (val);
00919 
00920   return val;
00921 }
00922 
00923 /* Return a value with type TYPE located at ADDR.
00924 
00925    Call value_at only if the data needs to be fetched immediately;
00926    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
00927    value_at_lazy instead.  value_at_lazy simply records the address of
00928    the data and sets the lazy-evaluation-required flag.  The lazy flag
00929    is tested in the value_contents macro, which is used if and when
00930    the contents are actually required.
00931 
00932    Note: value_at does *NOT* handle embedded offsets; perform such
00933    adjustments before or after calling it.  */
00934 
00935 struct value *
00936 value_at (struct type *type, CORE_ADDR addr)
00937 {
00938   return get_value_at (type, addr, 0);
00939 }
00940 
00941 /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
00942 
00943 struct value *
00944 value_at_lazy (struct type *type, CORE_ADDR addr)
00945 {
00946   return get_value_at (type, addr, 1);
00947 }
00948 
00949 void
00950 read_value_memory (struct value *val, int embedded_offset,
00951                    int stack, CORE_ADDR memaddr,
00952                    gdb_byte *buffer, size_t length)
00953 {
00954   if (length)
00955     {
00956       VEC(mem_range_s) *available_memory;
00957 
00958       if (!traceframe_available_memory (&available_memory, memaddr, length))
00959         {
00960           if (stack)
00961             read_stack (memaddr, buffer, length);
00962           else
00963             read_memory (memaddr, buffer, length);
00964         }
00965       else
00966         {
00967           struct target_section_table *table;
00968           struct cleanup *old_chain;
00969           CORE_ADDR unavail;
00970           mem_range_s *r;
00971           int i;
00972 
00973           /* Fallback to reading from read-only sections.  */
00974           table = target_get_section_table (&exec_ops);
00975           available_memory =
00976             section_table_available_memory (available_memory,
00977                                             memaddr, length,
00978                                             table->sections,
00979                                             table->sections_end);
00980 
00981           old_chain = make_cleanup (VEC_cleanup(mem_range_s),
00982                                     &available_memory);
00983 
00984           normalize_mem_ranges (available_memory);
00985 
00986           /* Mark which bytes are unavailable, and read those which
00987              are available.  */
00988 
00989           unavail = memaddr;
00990 
00991           for (i = 0;
00992                VEC_iterate (mem_range_s, available_memory, i, r);
00993                i++)
00994             {
00995               if (mem_ranges_overlap (r->start, r->length,
00996                                       memaddr, length))
00997                 {
00998                   CORE_ADDR lo1, hi1, lo2, hi2;
00999                   CORE_ADDR start, end;
01000 
01001                   /* Get the intersection window.  */
01002                   lo1 = memaddr;
01003                   hi1 = memaddr + length;
01004                   lo2 = r->start;
01005                   hi2 = r->start + r->length;
01006                   start = max (lo1, lo2);
01007                   end = min (hi1, hi2);
01008 
01009                   gdb_assert (end - memaddr <= length);
01010 
01011                   if (start > unavail)
01012                     mark_value_bytes_unavailable (val,
01013                                                   (embedded_offset
01014                                                    + unavail - memaddr),
01015                                                   start - unavail);
01016                   unavail = end;
01017 
01018                   read_memory (start, buffer + start - memaddr, end - start);
01019                 }
01020             }
01021 
01022           if (unavail != memaddr + length)
01023             mark_value_bytes_unavailable (val,
01024                                           embedded_offset + unavail - memaddr,
01025                                           (memaddr + length) - unavail);
01026 
01027           do_cleanups (old_chain);
01028         }
01029     }
01030 }
01031 
01032 /* Store the contents of FROMVAL into the location of TOVAL.
01033    Return a new value with the location of TOVAL and contents of FROMVAL.  */
01034 
01035 struct value *
01036 value_assign (struct value *toval, struct value *fromval)
01037 {
01038   struct type *type;
01039   struct value *val;
01040   struct frame_id old_frame;
01041 
01042   if (!deprecated_value_modifiable (toval))
01043     error (_("Left operand of assignment is not a modifiable lvalue."));
01044 
01045   toval = coerce_ref (toval);
01046 
01047   type = value_type (toval);
01048   if (VALUE_LVAL (toval) != lval_internalvar)
01049     fromval = value_cast (type, fromval);
01050   else
01051     {
01052       /* Coerce arrays and functions to pointers, except for arrays
01053          which only live in GDB's storage.  */
01054       if (!value_must_coerce_to_target (fromval))
01055         fromval = coerce_array (fromval);
01056     }
01057 
01058   CHECK_TYPEDEF (type);
01059 
01060   /* Since modifying a register can trash the frame chain, and
01061      modifying memory can trash the frame cache, we save the old frame
01062      and then restore the new frame afterwards.  */
01063   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());
01064 
01065   switch (VALUE_LVAL (toval))
01066     {
01067     case lval_internalvar:
01068       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
01069       return value_of_internalvar (get_type_arch (type),
01070                                    VALUE_INTERNALVAR (toval));
01071 
01072     case lval_internalvar_component:
01073       {
01074         int offset = value_offset (toval);
01075 
01076         /* Are we dealing with a bitfield?
01077 
01078            It is important to mention that `value_parent (toval)' is
01079            non-NULL iff `value_bitsize (toval)' is non-zero.  */
01080         if (value_bitsize (toval))
01081           {
01082             /* VALUE_INTERNALVAR below refers to the parent value, while
01083                the offset is relative to this parent value.  */
01084             gdb_assert (value_parent (value_parent (toval)) == NULL);
01085             offset += value_offset (value_parent (toval));
01086           }
01087 
01088         set_internalvar_component (VALUE_INTERNALVAR (toval),
01089                                    offset,
01090                                    value_bitpos (toval),
01091                                    value_bitsize (toval),
01092                                    fromval);
01093       }
01094       break;
01095 
01096     case lval_memory:
01097       {
01098         const gdb_byte *dest_buffer;
01099         CORE_ADDR changed_addr;
01100         int changed_len;
01101         gdb_byte buffer[sizeof (LONGEST)];
01102 
01103         if (value_bitsize (toval))
01104           {
01105             struct value *parent = value_parent (toval);
01106 
01107             changed_addr = value_address (parent) + value_offset (toval);
01108             changed_len = (value_bitpos (toval)
01109                            + value_bitsize (toval)
01110                            + HOST_CHAR_BIT - 1)
01111               / HOST_CHAR_BIT;
01112 
01113             /* If we can read-modify-write exactly the size of the
01114                containing type (e.g. short or int) then do so.  This
01115                is safer for volatile bitfields mapped to hardware
01116                registers.  */
01117             if (changed_len < TYPE_LENGTH (type)
01118                 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
01119                 && ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
01120               changed_len = TYPE_LENGTH (type);
01121 
01122             if (changed_len > (int) sizeof (LONGEST))
01123               error (_("Can't handle bitfields which "
01124                        "don't fit in a %d bit word."),
01125                      (int) sizeof (LONGEST) * HOST_CHAR_BIT);
01126 
01127             read_memory (changed_addr, buffer, changed_len);
01128             modify_field (type, buffer, value_as_long (fromval),
01129                           value_bitpos (toval), value_bitsize (toval));
01130             dest_buffer = buffer;
01131           }
01132         else
01133           {
01134             changed_addr = value_address (toval);
01135             changed_len = TYPE_LENGTH (type);
01136             dest_buffer = value_contents (fromval);
01137           }
01138 
01139         write_memory_with_notification (changed_addr, dest_buffer, changed_len);
01140       }
01141       break;
01142 
01143     case lval_register:
01144       {
01145         struct frame_info *frame;
01146         struct gdbarch *gdbarch;
01147         int value_reg;
01148 
01149         /* Figure out which frame this is in currently.  */
01150         frame = frame_find_by_id (VALUE_FRAME_ID (toval));
01151         value_reg = VALUE_REGNUM (toval);
01152 
01153         if (!frame)
01154           error (_("Value being assigned to is no longer active."));
01155 
01156         gdbarch = get_frame_arch (frame);
01157         if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval), type))
01158           {
01159             /* If TOVAL is a special machine register requiring
01160                conversion of program values to a special raw
01161                format.  */
01162             gdbarch_value_to_register (gdbarch, frame,
01163                                        VALUE_REGNUM (toval), type,
01164                                        value_contents (fromval));
01165           }
01166         else
01167           {
01168             if (value_bitsize (toval))
01169               {
01170                 struct value *parent = value_parent (toval);
01171                 int offset = value_offset (parent) + value_offset (toval);
01172                 int changed_len;
01173                 gdb_byte buffer[sizeof (LONGEST)];
01174                 int optim, unavail;
01175 
01176                 changed_len = (value_bitpos (toval)
01177                                + value_bitsize (toval)
01178                                + HOST_CHAR_BIT - 1)
01179                   / HOST_CHAR_BIT;
01180 
01181                 if (changed_len > (int) sizeof (LONGEST))
01182                   error (_("Can't handle bitfields which "
01183                            "don't fit in a %d bit word."),
01184                          (int) sizeof (LONGEST) * HOST_CHAR_BIT);
01185 
01186                 if (!get_frame_register_bytes (frame, value_reg, offset,
01187                                                changed_len, buffer,
01188                                                &optim, &unavail))
01189                   {
01190                     if (optim)
01191                       error (_("value has been optimized out"));
01192                     if (unavail)
01193                       throw_error (NOT_AVAILABLE_ERROR,
01194                                    _("value is not available"));
01195                   }
01196 
01197                 modify_field (type, buffer, value_as_long (fromval),
01198                               value_bitpos (toval), value_bitsize (toval));
01199 
01200                 put_frame_register_bytes (frame, value_reg, offset,
01201                                           changed_len, buffer);
01202               }
01203             else
01204               {
01205                 put_frame_register_bytes (frame, value_reg,
01206                                           value_offset (toval),
01207                                           TYPE_LENGTH (type),
01208                                           value_contents (fromval));
01209               }
01210           }
01211 
01212         if (deprecated_register_changed_hook)
01213           deprecated_register_changed_hook (-1);
01214         break;
01215       }
01216 
01217     case lval_computed:
01218       {
01219         const struct lval_funcs *funcs = value_computed_funcs (toval);
01220 
01221         if (funcs->write != NULL)
01222           {
01223             funcs->write (toval, fromval);
01224             break;
01225           }
01226       }
01227       /* Fall through.  */
01228 
01229     default:
01230       error (_("Left operand of assignment is not an lvalue."));
01231     }
01232 
01233   /* Assigning to the stack pointer, frame pointer, and other
01234      (architecture and calling convention specific) registers may
01235      cause the frame cache and regcache to be out of date.  Assigning to memory
01236      also can.  We just do this on all assignments to registers or
01237      memory, for simplicity's sake; I doubt the slowdown matters.  */
01238   switch (VALUE_LVAL (toval))
01239     {
01240     case lval_memory:
01241     case lval_register:
01242     case lval_computed:
01243 
01244       observer_notify_target_changed (&current_target);
01245 
01246       /* Having destroyed the frame cache, restore the selected
01247          frame.  */
01248 
01249       /* FIXME: cagney/2002-11-02: There has to be a better way of
01250          doing this.  Instead of constantly saving/restoring the
01251          frame.  Why not create a get_selected_frame() function that,
01252          having saved the selected frame's ID can automatically
01253          re-find the previously selected frame automatically.  */
01254 
01255       {
01256         struct frame_info *fi = frame_find_by_id (old_frame);
01257 
01258         if (fi != NULL)
01259           select_frame (fi);
01260       }
01261 
01262       break;
01263     default:
01264       break;
01265     }
01266   
01267   /* If the field does not entirely fill a LONGEST, then zero the sign
01268      bits.  If the field is signed, and is negative, then sign
01269      extend.  */
01270   if ((value_bitsize (toval) > 0)
01271       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
01272     {
01273       LONGEST fieldval = value_as_long (fromval);
01274       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;
01275 
01276       fieldval &= valmask;
01277       if (!TYPE_UNSIGNED (type) 
01278           && (fieldval & (valmask ^ (valmask >> 1))))
01279         fieldval |= ~valmask;
01280 
01281       fromval = value_from_longest (type, fieldval);
01282     }
01283 
01284   /* The return value is a copy of TOVAL so it shares its location
01285      information, but its contents are updated from FROMVAL.  This
01286      implies the returned value is not lazy, even if TOVAL was.  */
01287   val = value_copy (toval);
01288   set_value_lazy (val, 0);
01289   memcpy (value_contents_raw (val), value_contents (fromval),
01290           TYPE_LENGTH (type));
01291 
01292   /* We copy over the enclosing type and pointed-to offset from FROMVAL
01293      in the case of pointer types.  For object types, the enclosing type
01294      and embedded offset must *not* be copied: the target object refered
01295      to by TOVAL retains its original dynamic type after assignment.  */
01296   if (TYPE_CODE (type) == TYPE_CODE_PTR)
01297     {
01298       set_value_enclosing_type (val, value_enclosing_type (fromval));
01299       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
01300     }
01301 
01302   return val;
01303 }
01304 
01305 /* Extend a value VAL to COUNT repetitions of its type.  */
01306 
01307 struct value *
01308 value_repeat (struct value *arg1, int count)
01309 {
01310   struct value *val;
01311 
01312   if (VALUE_LVAL (arg1) != lval_memory)
01313     error (_("Only values in memory can be extended with '@'."));
01314   if (count < 1)
01315     error (_("Invalid number %d of repetitions."), count);
01316 
01317   val = allocate_repeat_value (value_enclosing_type (arg1), count);
01318 
01319   VALUE_LVAL (val) = lval_memory;
01320   set_value_address (val, value_address (arg1));
01321 
01322   read_value_memory (val, 0, value_stack (val), value_address (val),
01323                      value_contents_all_raw (val),
01324                      TYPE_LENGTH (value_enclosing_type (val)));
01325 
01326   return val;
01327 }
01328 
01329 struct value *
01330 value_of_variable (struct symbol *var, const struct block *b)
01331 {
01332   struct frame_info *frame;
01333 
01334   if (!symbol_read_needs_frame (var))
01335     frame = NULL;
01336   else if (!b)
01337     frame = get_selected_frame (_("No frame selected."));
01338   else
01339     {
01340       frame = block_innermost_frame (b);
01341       if (!frame)
01342         {
01343           if (BLOCK_FUNCTION (b) && !block_inlined_p (b)
01344               && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
01345             error (_("No frame is currently executing in block %s."),
01346                    SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
01347           else
01348             error (_("No frame is currently executing in specified block"));
01349         }
01350     }
01351 
01352   return read_var_value (var, frame);
01353 }
01354 
01355 struct value *
01356 address_of_variable (struct symbol *var, const struct block *b)
01357 {
01358   struct type *type = SYMBOL_TYPE (var);
01359   struct value *val;
01360 
01361   /* Evaluate it first; if the result is a memory address, we're fine.
01362      Lazy evaluation pays off here.  */
01363 
01364   val = value_of_variable (var, b);
01365 
01366   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
01367       || TYPE_CODE (type) == TYPE_CODE_FUNC)
01368     {
01369       CORE_ADDR addr = value_address (val);
01370 
01371       return value_from_pointer (lookup_pointer_type (type), addr);
01372     }
01373 
01374   /* Not a memory address; check what the problem was.  */
01375   switch (VALUE_LVAL (val))
01376     {
01377     case lval_register:
01378       {
01379         struct frame_info *frame;
01380         const char *regname;
01381 
01382         frame = frame_find_by_id (VALUE_FRAME_ID (val));
01383         gdb_assert (frame);
01384 
01385         regname = gdbarch_register_name (get_frame_arch (frame),
01386                                          VALUE_REGNUM (val));
01387         gdb_assert (regname && *regname);
01388 
01389         error (_("Address requested for identifier "
01390                  "\"%s\" which is in register $%s"),
01391                SYMBOL_PRINT_NAME (var), regname);
01392         break;
01393       }
01394 
01395     default:
01396       error (_("Can't take address of \"%s\" which isn't an lvalue."),
01397              SYMBOL_PRINT_NAME (var));
01398       break;
01399     }
01400 
01401   return val;
01402 }
01403 
01404 /* Return one if VAL does not live in target memory, but should in order
01405    to operate on it.  Otherwise return zero.  */
01406 
01407 int
01408 value_must_coerce_to_target (struct value *val)
01409 {
01410   struct type *valtype;
01411 
01412   /* The only lval kinds which do not live in target memory.  */
01413   if (VALUE_LVAL (val) != not_lval
01414       && VALUE_LVAL (val) != lval_internalvar)
01415     return 0;
01416 
01417   valtype = check_typedef (value_type (val));
01418 
01419   switch (TYPE_CODE (valtype))
01420     {
01421     case TYPE_CODE_ARRAY:
01422       return TYPE_VECTOR (valtype) ? 0 : 1;
01423     case TYPE_CODE_STRING:
01424       return 1;
01425     default:
01426       return 0;
01427     }
01428 }
01429 
01430 /* Make sure that VAL lives in target memory if it's supposed to.  For
01431    instance, strings are constructed as character arrays in GDB's
01432    storage, and this function copies them to the target.  */
01433 
01434 struct value *
01435 value_coerce_to_target (struct value *val)
01436 {
01437   LONGEST length;
01438   CORE_ADDR addr;
01439 
01440   if (!value_must_coerce_to_target (val))
01441     return val;
01442 
01443   length = TYPE_LENGTH (check_typedef (value_type (val)));
01444   addr = allocate_space_in_inferior (length);
01445   write_memory (addr, value_contents (val), length);
01446   return value_at_lazy (value_type (val), addr);
01447 }
01448 
01449 /* Given a value which is an array, return a value which is a pointer
01450    to its first element, regardless of whether or not the array has a
01451    nonzero lower bound.
01452 
01453    FIXME: A previous comment here indicated that this routine should
01454    be substracting the array's lower bound.  It's not clear to me that
01455    this is correct.  Given an array subscripting operation, it would
01456    certainly work to do the adjustment here, essentially computing:
01457 
01458    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
01459 
01460    However I believe a more appropriate and logical place to account
01461    for the lower bound is to do so in value_subscript, essentially
01462    computing:
01463 
01464    (&array[0] + ((index - lowerbound) * sizeof array[0]))
01465 
01466    As further evidence consider what would happen with operations
01467    other than array subscripting, where the caller would get back a
01468    value that had an address somewhere before the actual first element
01469    of the array, and the information about the lower bound would be
01470    lost because of the coercion to pointer type.  */
01471 
01472 struct value *
01473 value_coerce_array (struct value *arg1)
01474 {
01475   struct type *type = check_typedef (value_type (arg1));
01476 
01477   /* If the user tries to do something requiring a pointer with an
01478      array that has not yet been pushed to the target, then this would
01479      be a good time to do so.  */
01480   arg1 = value_coerce_to_target (arg1);
01481 
01482   if (VALUE_LVAL (arg1) != lval_memory)
01483     error (_("Attempt to take address of value not located in memory."));
01484 
01485   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
01486                              value_address (arg1));
01487 }
01488 
01489 /* Given a value which is a function, return a value which is a pointer
01490    to it.  */
01491 
01492 struct value *
01493 value_coerce_function (struct value *arg1)
01494 {
01495   struct value *retval;
01496 
01497   if (VALUE_LVAL (arg1) != lval_memory)
01498     error (_("Attempt to take address of value not located in memory."));
01499 
01500   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
01501                                value_address (arg1));
01502   return retval;
01503 }
01504 
01505 /* Return a pointer value for the object for which ARG1 is the
01506    contents.  */
01507 
01508 struct value *
01509 value_addr (struct value *arg1)
01510 {
01511   struct value *arg2;
01512   struct type *type = check_typedef (value_type (arg1));
01513 
01514   if (TYPE_CODE (type) == TYPE_CODE_REF)
01515     {
01516       /* Copy the value, but change the type from (T&) to (T*).  We
01517          keep the same location information, which is efficient, and
01518          allows &(&X) to get the location containing the reference.  */
01519       arg2 = value_copy (arg1);
01520       deprecated_set_value_type (arg2, 
01521                                  lookup_pointer_type (TYPE_TARGET_TYPE (type)));
01522       return arg2;
01523     }
01524   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
01525     return value_coerce_function (arg1);
01526 
01527   /* If this is an array that has not yet been pushed to the target,
01528      then this would be a good time to force it to memory.  */
01529   arg1 = value_coerce_to_target (arg1);
01530 
01531   if (VALUE_LVAL (arg1) != lval_memory)
01532     error (_("Attempt to take address of value not located in memory."));
01533 
01534   /* Get target memory address.  */
01535   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
01536                              (value_address (arg1)
01537                               + value_embedded_offset (arg1)));
01538 
01539   /* This may be a pointer to a base subobject; so remember the
01540      full derived object's type ...  */
01541   set_value_enclosing_type (arg2,
01542                             lookup_pointer_type (value_enclosing_type (arg1)));
01543   /* ... and also the relative position of the subobject in the full
01544      object.  */
01545   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
01546   return arg2;
01547 }
01548 
01549 /* Return a reference value for the object for which ARG1 is the
01550    contents.  */
01551 
01552 struct value *
01553 value_ref (struct value *arg1)
01554 {
01555   struct value *arg2;
01556   struct type *type = check_typedef (value_type (arg1));
01557 
01558   if (TYPE_CODE (type) == TYPE_CODE_REF)
01559     return arg1;
01560 
01561   arg2 = value_addr (arg1);
01562   deprecated_set_value_type (arg2, lookup_reference_type (type));
01563   return arg2;
01564 }
01565 
01566 /* Given a value of a pointer type, apply the C unary * operator to
01567    it.  */
01568 
01569 struct value *
01570 value_ind (struct value *arg1)
01571 {
01572   struct type *base_type;
01573   struct value *arg2;
01574 
01575   arg1 = coerce_array (arg1);
01576 
01577   base_type = check_typedef (value_type (arg1));
01578 
01579   if (VALUE_LVAL (arg1) == lval_computed)
01580     {
01581       const struct lval_funcs *funcs = value_computed_funcs (arg1);
01582 
01583       if (funcs->indirect)
01584         {
01585           struct value *result = funcs->indirect (arg1);
01586 
01587           if (result)
01588             return result;
01589         }
01590     }
01591 
01592   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
01593     {
01594       struct type *enc_type;
01595 
01596       /* We may be pointing to something embedded in a larger object.
01597          Get the real type of the enclosing object.  */
01598       enc_type = check_typedef (value_enclosing_type (arg1));
01599       enc_type = TYPE_TARGET_TYPE (enc_type);
01600 
01601       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
01602           || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
01603         /* For functions, go through find_function_addr, which knows
01604            how to handle function descriptors.  */
01605         arg2 = value_at_lazy (enc_type, 
01606                               find_function_addr (arg1, NULL));
01607       else
01608         /* Retrieve the enclosing object pointed to.  */
01609         arg2 = value_at_lazy (enc_type, 
01610                               (value_as_address (arg1)
01611                                - value_pointed_to_offset (arg1)));
01612 
01613       return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
01614     }
01615 
01616   error (_("Attempt to take contents of a non-pointer value."));
01617   return 0;                     /* For lint -- never reached.  */
01618 }
01619 
01620 /* Create a value for an array by allocating space in GDB, copying the
01621    data into that space, and then setting up an array value.
01622 
01623    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
01624    is populated from the values passed in ELEMVEC.
01625 
01626    The element type of the array is inherited from the type of the
01627    first element, and all elements must have the same size (though we
01628    don't currently enforce any restriction on their types).  */
01629 
01630 struct value *
01631 value_array (int lowbound, int highbound, struct value **elemvec)
01632 {
01633   int nelem;
01634   int idx;
01635   unsigned int typelength;
01636   struct value *val;
01637   struct type *arraytype;
01638 
01639   /* Validate that the bounds are reasonable and that each of the
01640      elements have the same size.  */
01641 
01642   nelem = highbound - lowbound + 1;
01643   if (nelem <= 0)
01644     {
01645       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
01646     }
01647   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
01648   for (idx = 1; idx < nelem; idx++)
01649     {
01650       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
01651         {
01652           error (_("array elements must all be the same size"));
01653         }
01654     }
01655 
01656   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
01657                                        lowbound, highbound);
01658 
01659   if (!current_language->c_style_arrays)
01660     {
01661       val = allocate_value (arraytype);
01662       for (idx = 0; idx < nelem; idx++)
01663         value_contents_copy (val, idx * typelength, elemvec[idx], 0,
01664                              typelength);
01665       return val;
01666     }
01667 
01668   /* Allocate space to store the array, and then initialize it by
01669      copying in each element.  */
01670 
01671   val = allocate_value (arraytype);
01672   for (idx = 0; idx < nelem; idx++)
01673     value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
01674   return val;
01675 }
01676 
01677 struct value *
01678 value_cstring (char *ptr, ssize_t len, struct type *char_type)
01679 {
01680   struct value *val;
01681   int lowbound = current_language->string_lower_bound;
01682   ssize_t highbound = len / TYPE_LENGTH (char_type);
01683   struct type *stringtype
01684     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);
01685 
01686   val = allocate_value (stringtype);
01687   memcpy (value_contents_raw (val), ptr, len);
01688   return val;
01689 }
01690 
01691 /* Create a value for a string constant by allocating space in the
01692    inferior, copying the data into that space, and returning the
01693    address with type TYPE_CODE_STRING.  PTR points to the string
01694    constant data; LEN is number of characters.
01695 
01696    Note that string types are like array of char types with a lower
01697    bound of zero and an upper bound of LEN - 1.  Also note that the
01698    string may contain embedded null bytes.  */
01699 
01700 struct value *
01701 value_string (char *ptr, ssize_t len, struct type *char_type)
01702 {
01703   struct value *val;
01704   int lowbound = current_language->string_lower_bound;
01705   ssize_t highbound = len / TYPE_LENGTH (char_type);
01706   struct type *stringtype
01707     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);
01708 
01709   val = allocate_value (stringtype);
01710   memcpy (value_contents_raw (val), ptr, len);
01711   return val;
01712 }
01713 
01714 
01715 /* See if we can pass arguments in T2 to a function which takes
01716    arguments of types T1.  T1 is a list of NARGS arguments, and T2 is
01717    a NULL-terminated vector.  If some arguments need coercion of some
01718    sort, then the coerced values are written into T2.  Return value is
01719    0 if the arguments could be matched, or the position at which they
01720    differ if not.
01721 
01722    STATICP is nonzero if the T1 argument list came from a static
01723    member function.  T2 will still include the ``this'' pointer, but
01724    it will be skipped.
01725 
01726    For non-static member functions, we ignore the first argument,
01727    which is the type of the instance variable.  This is because we
01728    want to handle calls with objects from derived classes.  This is
01729    not entirely correct: we should actually check to make sure that a
01730    requested operation is type secure, shouldn't we?  FIXME.  */
01731 
01732 static int
01733 typecmp (int staticp, int varargs, int nargs,
01734          struct field t1[], struct value *t2[])
01735 {
01736   int i;
01737 
01738   if (t2 == 0)
01739     internal_error (__FILE__, __LINE__, 
01740                     _("typecmp: no argument list"));
01741 
01742   /* Skip ``this'' argument if applicable.  T2 will always include
01743      THIS.  */
01744   if (staticp)
01745     t2 ++;
01746 
01747   for (i = 0;
01748        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
01749        i++)
01750     {
01751       struct type *tt1, *tt2;
01752 
01753       if (!t2[i])
01754         return i + 1;
01755 
01756       tt1 = check_typedef (t1[i].type);
01757       tt2 = check_typedef (value_type (t2[i]));
01758 
01759       if (TYPE_CODE (tt1) == TYPE_CODE_REF
01760       /* We should be doing hairy argument matching, as below.  */
01761           && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
01762               == TYPE_CODE (tt2)))
01763         {
01764           if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
01765             t2[i] = value_coerce_array (t2[i]);
01766           else
01767             t2[i] = value_ref (t2[i]);
01768           continue;
01769         }
01770 
01771       /* djb - 20000715 - Until the new type structure is in the
01772          place, and we can attempt things like implicit conversions,
01773          we need to do this so you can take something like a map<const
01774          char *>, and properly access map["hello"], because the
01775          argument to [] will be a reference to a pointer to a char,
01776          and the argument will be a pointer to a char.  */
01777       while (TYPE_CODE(tt1) == TYPE_CODE_REF
01778              || TYPE_CODE (tt1) == TYPE_CODE_PTR)
01779         {
01780           tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
01781         }
01782       while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
01783              || TYPE_CODE(tt2) == TYPE_CODE_PTR
01784              || TYPE_CODE(tt2) == TYPE_CODE_REF)
01785         {
01786           tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
01787         }
01788       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
01789         continue;
01790       /* Array to pointer is a `trivial conversion' according to the
01791          ARM.  */
01792 
01793       /* We should be doing much hairier argument matching (see
01794          section 13.2 of the ARM), but as a quick kludge, just check
01795          for the same type code.  */
01796       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
01797         return i + 1;
01798     }
01799   if (varargs || t2[i] == NULL)
01800     return 0;
01801   return i + 1;
01802 }
01803 
01804 /* Helper class for do_search_struct_field that updates *RESULT_PTR
01805    and *LAST_BOFFSET, and possibly throws an exception if the field
01806    search has yielded ambiguous results.  */
01807 
01808 static void
01809 update_search_result (struct value **result_ptr, struct value *v,
01810                       int *last_boffset, int boffset,
01811                       const char *name, struct type *type)
01812 {
01813   if (v != NULL)
01814     {
01815       if (*result_ptr != NULL
01816           /* The result is not ambiguous if all the classes that are
01817              found occupy the same space.  */
01818           && *last_boffset != boffset)
01819         error (_("base class '%s' is ambiguous in type '%s'"),
01820                name, TYPE_SAFE_NAME (type));
01821       *result_ptr = v;
01822       *last_boffset = boffset;
01823     }
01824 }
01825 
01826 /* A helper for search_struct_field.  This does all the work; most
01827    arguments are as passed to search_struct_field.  The result is
01828    stored in *RESULT_PTR, which must be initialized to NULL.
01829    OUTERMOST_TYPE is the type of the initial type passed to
01830    search_struct_field; this is used for error reporting when the
01831    lookup is ambiguous.  */
01832 
01833 static void
01834 do_search_struct_field (const char *name, struct value *arg1, int offset,
01835                         struct type *type, int looking_for_baseclass,
01836                         struct value **result_ptr,
01837                         int *last_boffset,
01838                         struct type *outermost_type)
01839 {
01840   int i;
01841   int nbases;
01842 
01843   CHECK_TYPEDEF (type);
01844   nbases = TYPE_N_BASECLASSES (type);
01845 
01846   if (!looking_for_baseclass)
01847     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
01848       {
01849         const char *t_field_name = TYPE_FIELD_NAME (type, i);
01850 
01851         if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
01852           {
01853             struct value *v;
01854 
01855             if (field_is_static (&TYPE_FIELD (type, i)))
01856               {
01857                 v = value_static_field (type, i);
01858                 if (v == 0)
01859                   error (_("field %s is nonexistent or "
01860                            "has been optimized out"),
01861                          name);
01862               }
01863             else
01864               v = value_primitive_field (arg1, offset, i, type);
01865             *result_ptr = v;
01866             return;
01867           }
01868 
01869         if (t_field_name
01870             && (t_field_name[0] == '\0'
01871                 || (TYPE_CODE (type) == TYPE_CODE_UNION
01872                     && (strcmp_iw (t_field_name, "else") == 0))))
01873           {
01874             struct type *field_type = TYPE_FIELD_TYPE (type, i);
01875 
01876             if (TYPE_CODE (field_type) == TYPE_CODE_UNION
01877                 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
01878               {
01879                 /* Look for a match through the fields of an anonymous
01880                    union, or anonymous struct.  C++ provides anonymous
01881                    unions.
01882 
01883                    In the GNU Chill (now deleted from GDB)
01884                    implementation of variant record types, each
01885                    <alternative field> has an (anonymous) union type,
01886                    each member of the union represents a <variant
01887                    alternative>.  Each <variant alternative> is
01888                    represented as a struct, with a member for each
01889                    <variant field>.  */
01890 
01891                 struct value *v = NULL;
01892                 int new_offset = offset;
01893 
01894                 /* This is pretty gross.  In G++, the offset in an
01895                    anonymous union is relative to the beginning of the
01896                    enclosing struct.  In the GNU Chill (now deleted
01897                    from GDB) implementation of variant records, the
01898                    bitpos is zero in an anonymous union field, so we
01899                    have to add the offset of the union here.  */
01900                 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
01901                     || (TYPE_NFIELDS (field_type) > 0
01902                         && TYPE_FIELD_BITPOS (field_type, 0) == 0))
01903                   new_offset += TYPE_FIELD_BITPOS (type, i) / 8;
01904 
01905                 do_search_struct_field (name, arg1, new_offset, 
01906                                         field_type,
01907                                         looking_for_baseclass, &v,
01908                                         last_boffset,
01909                                         outermost_type);
01910                 if (v)
01911                   {
01912                     *result_ptr = v;
01913                     return;
01914                   }
01915               }
01916           }
01917       }
01918 
01919   for (i = 0; i < nbases; i++)
01920     {
01921       struct value *v = NULL;
01922       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
01923       /* If we are looking for baseclasses, this is what we get when
01924          we hit them.  But it could happen that the base part's member
01925          name is not yet filled in.  */
01926       int found_baseclass = (looking_for_baseclass
01927                              && TYPE_BASECLASS_NAME (type, i) != NULL
01928                              && (strcmp_iw (name, 
01929                                             TYPE_BASECLASS_NAME (type, 
01930                                                                  i)) == 0));
01931       int boffset = value_embedded_offset (arg1) + offset;
01932 
01933       if (BASETYPE_VIA_VIRTUAL (type, i))
01934         {
01935           struct value *v2;
01936 
01937           boffset = baseclass_offset (type, i,
01938                                       value_contents_for_printing (arg1),
01939                                       value_embedded_offset (arg1) + offset,
01940                                       value_address (arg1),
01941                                       arg1);
01942 
01943           /* The virtual base class pointer might have been clobbered
01944              by the user program.  Make sure that it still points to a
01945              valid memory location.  */
01946 
01947           boffset += value_embedded_offset (arg1) + offset;
01948           if (boffset < 0
01949               || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
01950             {
01951               CORE_ADDR base_addr;
01952 
01953               base_addr = value_address (arg1) + boffset;
01954               v2 = value_at_lazy (basetype, base_addr);
01955               if (target_read_memory (base_addr, 
01956                                       value_contents_raw (v2),
01957                                       TYPE_LENGTH (value_type (v2))) != 0)
01958                 error (_("virtual baseclass botch"));
01959             }
01960           else
01961             {
01962               v2 = value_copy (arg1);
01963               deprecated_set_value_type (v2, basetype);
01964               set_value_embedded_offset (v2, boffset);
01965             }
01966 
01967           if (found_baseclass)
01968             v = v2;
01969           else
01970             {
01971               do_search_struct_field (name, v2, 0,
01972                                       TYPE_BASECLASS (type, i),
01973                                       looking_for_baseclass,
01974                                       result_ptr, last_boffset,
01975                                       outermost_type);
01976             }
01977         }
01978       else if (found_baseclass)
01979         v = value_primitive_field (arg1, offset, i, type);
01980       else
01981         {
01982           do_search_struct_field (name, arg1,
01983                                   offset + TYPE_BASECLASS_BITPOS (type, 
01984                                                                   i) / 8,
01985                                   basetype, looking_for_baseclass,
01986                                   result_ptr, last_boffset,
01987                                   outermost_type);
01988         }
01989 
01990       update_search_result (result_ptr, v, last_boffset,
01991                             boffset, name, outermost_type);
01992     }
01993 }
01994 
01995 /* Helper function used by value_struct_elt to recurse through
01996    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
01997    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
01998    TYPE.  If found, return value, else return NULL.
01999 
02000    If LOOKING_FOR_BASECLASS, then instead of looking for struct
02001    fields, look for a baseclass named NAME.  */
02002 
02003 static struct value *
02004 search_struct_field (const char *name, struct value *arg1, int offset,
02005                      struct type *type, int looking_for_baseclass)
02006 {
02007   struct value *result = NULL;
02008   int boffset = 0;
02009 
02010   do_search_struct_field (name, arg1, offset, type, looking_for_baseclass,
02011                           &result, &boffset, type);
02012   return result;
02013 }
02014 
02015 /* Helper function used by value_struct_elt to recurse through
02016    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
02017    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
02018    TYPE.
02019 
02020    If found, return value, else if name matched and args not return
02021    (value) -1, else return NULL.  */
02022 
02023 static struct value *
02024 search_struct_method (const char *name, struct value **arg1p,
02025                       struct value **args, int offset,
02026                       int *static_memfuncp, struct type *type)
02027 {
02028   int i;
02029   struct value *v;
02030   int name_matched = 0;
02031   char dem_opname[64];
02032 
02033   CHECK_TYPEDEF (type);
02034   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
02035     {
02036       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
02037 
02038       /* FIXME!  May need to check for ARM demangling here.  */
02039       if (strncmp (t_field_name, "__", 2) == 0 ||
02040           strncmp (t_field_name, "op", 2) == 0 ||
02041           strncmp (t_field_name, "type", 4) == 0)
02042         {
02043           if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
02044             t_field_name = dem_opname;
02045           else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
02046             t_field_name = dem_opname;
02047         }
02048       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
02049         {
02050           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
02051           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
02052 
02053           name_matched = 1;
02054           check_stub_method_group (type, i);
02055           if (j > 0 && args == 0)
02056             error (_("cannot resolve overloaded method "
02057                      "`%s': no arguments supplied"), name);
02058           else if (j == 0 && args == 0)
02059             {
02060               v = value_fn_field (arg1p, f, j, type, offset);
02061               if (v != NULL)
02062                 return v;
02063             }
02064           else
02065             while (j >= 0)
02066               {
02067                 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
02068                               TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
02069                               TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
02070                               TYPE_FN_FIELD_ARGS (f, j), args))
02071                   {
02072                     if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
02073                       return value_virtual_fn_field (arg1p, f, j, 
02074                                                      type, offset);
02075                     if (TYPE_FN_FIELD_STATIC_P (f, j) 
02076                         && static_memfuncp)
02077                       *static_memfuncp = 1;
02078                     v = value_fn_field (arg1p, f, j, type, offset);
02079                     if (v != NULL)
02080                       return v;       
02081                   }
02082                 j--;
02083               }
02084         }
02085     }
02086 
02087   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
02088     {
02089       int base_offset;
02090       int this_offset;
02091 
02092       if (BASETYPE_VIA_VIRTUAL (type, i))
02093         {
02094           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
02095           struct value *base_val;
02096           const gdb_byte *base_valaddr;
02097 
02098           /* The virtual base class pointer might have been
02099              clobbered by the user program.  Make sure that it
02100             still points to a valid memory location.  */
02101 
02102           if (offset < 0 || offset >= TYPE_LENGTH (type))
02103             {
02104               gdb_byte *tmp;
02105               struct cleanup *back_to;
02106               CORE_ADDR address;
02107 
02108               tmp = xmalloc (TYPE_LENGTH (baseclass));
02109               back_to = make_cleanup (xfree, tmp);
02110               address = value_address (*arg1p);
02111 
02112               if (target_read_memory (address + offset,
02113                                       tmp, TYPE_LENGTH (baseclass)) != 0)
02114                 error (_("virtual baseclass botch"));
02115 
02116               base_val = value_from_contents_and_address (baseclass,
02117                                                           tmp,
02118                                                           address + offset);
02119               base_valaddr = value_contents_for_printing (base_val);
02120               this_offset = 0;
02121               do_cleanups (back_to);
02122             }
02123           else
02124             {
02125               base_val = *arg1p;
02126               base_valaddr = value_contents_for_printing (*arg1p);
02127               this_offset = offset;
02128             }
02129 
02130           base_offset = baseclass_offset (type, i, base_valaddr,
02131                                           this_offset, value_address (base_val),
02132                                           base_val);
02133         }
02134       else
02135         {
02136           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
02137         }
02138       v = search_struct_method (name, arg1p, args, base_offset + offset,
02139                                 static_memfuncp, TYPE_BASECLASS (type, i));
02140       if (v == (struct value *) - 1)
02141         {
02142           name_matched = 1;
02143         }
02144       else if (v)
02145         {
02146           /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
02147           /* *arg1p = arg1_tmp; */
02148           return v;
02149         }
02150     }
02151   if (name_matched)
02152     return (struct value *) - 1;
02153   else
02154     return NULL;
02155 }
02156 
02157 /* Given *ARGP, a value of type (pointer to a)* structure/union,
02158    extract the component named NAME from the ultimate target
02159    structure/union and return it as a value with its appropriate type.
02160    ERR is used in the error message if *ARGP's type is wrong.
02161 
02162    C++: ARGS is a list of argument types to aid in the selection of
02163    an appropriate method.  Also, handle derived types.
02164 
02165    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
02166    where the truthvalue of whether the function that was resolved was
02167    a static member function or not is stored.
02168 
02169    ERR is an error message to be printed in case the field is not
02170    found.  */
02171 
02172 struct value *
02173 value_struct_elt (struct value **argp, struct value **args,
02174                   const char *name, int *static_memfuncp, const char *err)
02175 {
02176   struct type *t;
02177   struct value *v;
02178 
02179   *argp = coerce_array (*argp);
02180 
02181   t = check_typedef (value_type (*argp));
02182 
02183   /* Follow pointers until we get to a non-pointer.  */
02184 
02185   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
02186     {
02187       *argp = value_ind (*argp);
02188       /* Don't coerce fn pointer to fn and then back again!  */
02189       if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
02190         *argp = coerce_array (*argp);
02191       t = check_typedef (value_type (*argp));
02192     }
02193 
02194   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
02195       && TYPE_CODE (t) != TYPE_CODE_UNION)
02196     error (_("Attempt to extract a component of a value that is not a %s."),
02197            err);
02198 
02199   /* Assume it's not, unless we see that it is.  */
02200   if (static_memfuncp)
02201     *static_memfuncp = 0;
02202 
02203   if (!args)
02204     {
02205       /* if there are no arguments ...do this...  */
02206 
02207       /* Try as a field first, because if we succeed, there is less
02208          work to be done.  */
02209       v = search_struct_field (name, *argp, 0, t, 0);
02210       if (v)
02211         return v;
02212 
02213       /* C++: If it was not found as a data field, then try to
02214          return it as a pointer to a method.  */
02215       v = search_struct_method (name, argp, args, 0, 
02216                                 static_memfuncp, t);
02217 
02218       if (v == (struct value *) - 1)
02219         error (_("Cannot take address of method %s."), name);
02220       else if (v == 0)
02221         {
02222           if (TYPE_NFN_FIELDS (t))
02223             error (_("There is no member or method named %s."), name);
02224           else
02225             error (_("There is no member named %s."), name);
02226         }
02227       return v;
02228     }
02229 
02230     v = search_struct_method (name, argp, args, 0, 
02231                               static_memfuncp, t);
02232   
02233   if (v == (struct value *) - 1)
02234     {
02235       error (_("One of the arguments you tried to pass to %s could not "
02236                "be converted to what the function wants."), name);
02237     }
02238   else if (v == 0)
02239     {
02240       /* See if user tried to invoke data as function.  If so, hand it
02241          back.  If it's not callable (i.e., a pointer to function),
02242          gdb should give an error.  */
02243       v = search_struct_field (name, *argp, 0, t, 0);
02244       /* If we found an ordinary field, then it is not a method call.
02245          So, treat it as if it were a static member function.  */
02246       if (v && static_memfuncp)
02247         *static_memfuncp = 1;
02248     }
02249 
02250   if (!v)
02251     throw_error (NOT_FOUND_ERROR,
02252                  _("Structure has no component named %s."), name);
02253   return v;
02254 }
02255 
02256 /* Search through the methods of an object (and its bases) to find a
02257    specified method.  Return the pointer to the fn_field list of
02258    overloaded instances.
02259 
02260    Helper function for value_find_oload_list.
02261    ARGP is a pointer to a pointer to a value (the object).
02262    METHOD is a string containing the method name.
02263    OFFSET is the offset within the value.
02264    TYPE is the assumed type of the object.
02265    NUM_FNS is the number of overloaded instances.
02266    BASETYPE is set to the actual type of the subobject where the
02267       method is found.
02268    BOFFSET is the offset of the base subobject where the method is found.  */
02269 
02270 static struct fn_field *
02271 find_method_list (struct value **argp, const char *method,
02272                   int offset, struct type *type, int *num_fns,
02273                   struct type **basetype, int *boffset)
02274 {
02275   int i;
02276   struct fn_field *f;
02277   CHECK_TYPEDEF (type);
02278 
02279   *num_fns = 0;
02280 
02281   /* First check in object itself.  */
02282   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
02283     {
02284       /* pai: FIXME What about operators and type conversions?  */
02285       const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
02286 
02287       if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
02288         {
02289           int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
02290           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
02291 
02292           *num_fns = len;
02293           *basetype = type;
02294           *boffset = offset;
02295 
02296           /* Resolve any stub methods.  */
02297           check_stub_method_group (type, i);
02298 
02299           return f;
02300         }
02301     }
02302 
02303   /* Not found in object, check in base subobjects.  */
02304   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
02305     {
02306       int base_offset;
02307 
02308       if (BASETYPE_VIA_VIRTUAL (type, i))
02309         {
02310           base_offset = baseclass_offset (type, i,
02311                                           value_contents_for_printing (*argp),
02312                                           value_offset (*argp) + offset,
02313                                           value_address (*argp), *argp);
02314         }
02315       else /* Non-virtual base, simply use bit position from debug
02316               info.  */
02317         {
02318           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
02319         }
02320       f = find_method_list (argp, method, base_offset + offset,
02321                             TYPE_BASECLASS (type, i), num_fns, 
02322                             basetype, boffset);
02323       if (f)
02324         return f;
02325     }
02326   return NULL;
02327 }
02328 
02329 /* Return the list of overloaded methods of a specified name.
02330 
02331    ARGP is a pointer to a pointer to a value (the object).
02332    METHOD is the method name.
02333    OFFSET is the offset within the value contents.
02334    NUM_FNS is the number of overloaded instances.
02335    BASETYPE is set to the type of the base subobject that defines the
02336       method.
02337    BOFFSET is the offset of the base subobject which defines the method.  */
02338 
02339 static struct fn_field *
02340 value_find_oload_method_list (struct value **argp, const char *method,
02341                               int offset, int *num_fns, 
02342                               struct type **basetype, int *boffset)
02343 {
02344   struct type *t;
02345 
02346   t = check_typedef (value_type (*argp));
02347 
02348   /* Code snarfed from value_struct_elt.  */
02349   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
02350     {
02351       *argp = value_ind (*argp);
02352       /* Don't coerce fn pointer to fn and then back again!  */
02353       if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
02354         *argp = coerce_array (*argp);
02355       t = check_typedef (value_type (*argp));
02356     }
02357 
02358   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
02359       && TYPE_CODE (t) != TYPE_CODE_UNION)
02360     error (_("Attempt to extract a component of a "
02361              "value that is not a struct or union"));
02362 
02363   return find_method_list (argp, method, 0, t, num_fns, 
02364                            basetype, boffset);
02365 }
02366 
02367 /* Given an array of arguments (ARGS) (which includes an
02368    entry for "this" in the case of C++ methods), the number of
02369    arguments NARGS, the NAME of a function, and whether it's a method or
02370    not (METHOD), find the best function that matches on the argument types
02371    according to the overload resolution rules.
02372 
02373    METHOD can be one of three values:
02374      NON_METHOD for non-member functions.
02375      METHOD: for member functions.
02376      BOTH: used for overload resolution of operators where the
02377        candidates are expected to be either member or non member
02378        functions.  In this case the first argument ARGTYPES
02379        (representing 'this') is expected to be a reference to the
02380        target object, and will be dereferenced when attempting the
02381        non-member search.
02382 
02383    In the case of class methods, the parameter OBJ is an object value
02384    in which to search for overloaded methods.
02385 
02386    In the case of non-method functions, the parameter FSYM is a symbol
02387    corresponding to one of the overloaded functions.
02388 
02389    Return value is an integer: 0 -> good match, 10 -> debugger applied
02390    non-standard coercions, 100 -> incompatible.
02391 
02392    If a method is being searched for, VALP will hold the value.
02393    If a non-method is being searched for, SYMP will hold the symbol 
02394    for it.
02395 
02396    If a method is being searched for, and it is a static method,
02397    then STATICP will point to a non-zero value.
02398 
02399    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
02400    ADL overload candidates when performing overload resolution for a fully
02401    qualified name.
02402 
02403    Note: This function does *not* check the value of
02404    overload_resolution.  Caller must check it to see whether overload
02405    resolution is permitted.  */
02406 
02407 int
02408 find_overload_match (struct value **args, int nargs,
02409                      const char *name, enum oload_search_type method,
02410                      struct value **objp, struct symbol *fsym,
02411                      struct value **valp, struct symbol **symp, 
02412                      int *staticp, const int no_adl)
02413 {
02414   struct value *obj = (objp ? *objp : NULL);
02415   struct type *obj_type = obj ? value_type (obj) : NULL;
02416   /* Index of best overloaded function.  */
02417   int func_oload_champ = -1;
02418   int method_oload_champ = -1;
02419 
02420   /* The measure for the current best match.  */
02421   struct badness_vector *method_badness = NULL;
02422   struct badness_vector *func_badness = NULL;
02423 
02424   struct value *temp = obj;
02425   /* For methods, the list of overloaded methods.  */
02426   struct fn_field *fns_ptr = NULL;
02427   /* For non-methods, the list of overloaded function symbols.  */
02428   struct symbol **oload_syms = NULL;
02429   /* Number of overloaded instances being considered.  */
02430   int num_fns = 0;
02431   struct type *basetype = NULL;
02432   int boffset;
02433 
02434   struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);
02435 
02436   const char *obj_type_name = NULL;
02437   const char *func_name = NULL;
02438   enum oload_classification match_quality;
02439   enum oload_classification method_match_quality = INCOMPATIBLE;
02440   enum oload_classification func_match_quality = INCOMPATIBLE;
02441 
02442   /* Get the list of overloaded methods or functions.  */
02443   if (method == METHOD || method == BOTH)
02444     {
02445       gdb_assert (obj);
02446 
02447       /* OBJ may be a pointer value rather than the object itself.  */
02448       obj = coerce_ref (obj);
02449       while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
02450         obj = coerce_ref (value_ind (obj));
02451       obj_type_name = TYPE_NAME (value_type (obj));
02452 
02453       /* First check whether this is a data member, e.g. a pointer to
02454          a function.  */
02455       if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
02456         {
02457           *valp = search_struct_field (name, obj, 0,
02458                                        check_typedef (value_type (obj)), 0);
02459           if (*valp)
02460             {
02461               *staticp = 1;
02462               do_cleanups (all_cleanups);
02463               return 0;
02464             }
02465         }
02466 
02467       /* Retrieve the list of methods with the name NAME.  */
02468       fns_ptr = value_find_oload_method_list (&temp, name, 
02469                                               0, &num_fns, 
02470                                               &basetype, &boffset);
02471       /* If this is a method only search, and no methods were found
02472          the search has faild.  */
02473       if (method == METHOD && (!fns_ptr || !num_fns))
02474         error (_("Couldn't find method %s%s%s"),
02475                obj_type_name,
02476                (obj_type_name && *obj_type_name) ? "::" : "",
02477                name);
02478       /* If we are dealing with stub method types, they should have
02479          been resolved by find_method_list via
02480          value_find_oload_method_list above.  */
02481       if (fns_ptr)
02482         {
02483           gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);
02484           method_oload_champ = find_oload_champ (args, nargs, method,
02485                                                  num_fns, fns_ptr,
02486                                                  oload_syms, &method_badness);
02487 
02488           method_match_quality =
02489               classify_oload_match (method_badness, nargs,
02490                                     oload_method_static (method, fns_ptr,
02491                                                          method_oload_champ));
02492 
02493           make_cleanup (xfree, method_badness);
02494         }
02495 
02496     }
02497 
02498   if (method == NON_METHOD || method == BOTH)
02499     {
02500       const char *qualified_name = NULL;
02501 
02502       /* If the overload match is being search for both as a method
02503          and non member function, the first argument must now be
02504          dereferenced.  */
02505       if (method == BOTH)
02506         args[0] = value_ind (args[0]);
02507 
02508       if (fsym)
02509         {
02510           qualified_name = SYMBOL_NATURAL_NAME (fsym);
02511 
02512           /* If we have a function with a C++ name, try to extract just
02513              the function part.  Do not try this for non-functions (e.g.
02514              function pointers).  */
02515           if (qualified_name
02516               && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
02517               == TYPE_CODE_FUNC)
02518             {
02519               char *temp;
02520 
02521               temp = cp_func_name (qualified_name);
02522 
02523               /* If cp_func_name did not remove anything, the name of the
02524                  symbol did not include scope or argument types - it was
02525                  probably a C-style function.  */
02526               if (temp)
02527                 {
02528                   make_cleanup (xfree, temp);
02529                   if (strcmp (temp, qualified_name) == 0)
02530                     func_name = NULL;
02531                   else
02532                     func_name = temp;
02533                 }
02534             }
02535         }
02536       else
02537         {
02538           func_name = name;
02539           qualified_name = name;
02540         }
02541 
02542       /* If there was no C++ name, this must be a C-style function or
02543          not a function at all.  Just return the same symbol.  Do the
02544          same if cp_func_name fails for some reason.  */
02545       if (func_name == NULL)
02546         {
02547           *symp = fsym;
02548           do_cleanups (all_cleanups);
02549           return 0;
02550         }
02551 
02552       func_oload_champ = find_oload_champ_namespace (args, nargs,
02553                                                      func_name,
02554                                                      qualified_name,
02555                                                      &oload_syms,
02556                                                      &func_badness,
02557                                                      no_adl);
02558 
02559       if (func_oload_champ >= 0)
02560         func_match_quality = classify_oload_match (func_badness, nargs, 0);
02561 
02562       make_cleanup (xfree, oload_syms);
02563       make_cleanup (xfree, func_badness);
02564     }
02565 
02566   /* Did we find a match ?  */
02567   if (method_oload_champ == -1 && func_oload_champ == -1)
02568     throw_error (NOT_FOUND_ERROR,
02569                  _("No symbol \"%s\" in current context."),
02570                  name);
02571 
02572   /* If we have found both a method match and a function
02573      match, find out which one is better, and calculate match
02574      quality.  */
02575   if (method_oload_champ >= 0 && func_oload_champ >= 0)
02576     {
02577       switch (compare_badness (func_badness, method_badness))
02578         {
02579           case 0: /* Top two contenders are equally good.  */
02580             /* FIXME: GDB does not support the general ambiguous case.
02581              All candidates should be collected and presented the
02582              user.  */
02583             error (_("Ambiguous overload resolution"));
02584             break;
02585           case 1: /* Incomparable top contenders.  */
02586             /* This is an error incompatible candidates
02587                should not have been proposed.  */
02588             error (_("Internal error: incompatible "
02589                      "overload candidates proposed"));
02590             break;
02591           case 2: /* Function champion.  */
02592             method_oload_champ = -1;
02593             match_quality = func_match_quality;
02594             break;
02595           case 3: /* Method champion.  */
02596             func_oload_champ = -1;
02597             match_quality = method_match_quality;
02598             break;
02599           default:
02600             error (_("Internal error: unexpected overload comparison result"));
02601             break;
02602         }
02603     }
02604   else
02605     {
02606       /* We have either a method match or a function match.  */
02607       if (method_oload_champ >= 0)
02608         match_quality = method_match_quality;
02609       else
02610         match_quality = func_match_quality;
02611     }
02612 
02613   if (match_quality == INCOMPATIBLE)
02614     {
02615       if (method == METHOD)
02616         error (_("Cannot resolve method %s%s%s to any overloaded instance"),
02617                obj_type_name,
02618                (obj_type_name && *obj_type_name) ? "::" : "",
02619                name);
02620       else
02621         error (_("Cannot resolve function %s to any overloaded instance"),
02622                func_name);
02623     }
02624   else if (match_quality == NON_STANDARD)
02625     {
02626       if (method == METHOD)
02627         warning (_("Using non-standard conversion to match "
02628                    "method %s%s%s to supplied arguments"),
02629                  obj_type_name,
02630                  (obj_type_name && *obj_type_name) ? "::" : "",
02631                  name);
02632       else
02633         warning (_("Using non-standard conversion to match "
02634                    "function %s to supplied arguments"),
02635                  func_name);
02636     }
02637 
02638   if (staticp != NULL)
02639     *staticp = oload_method_static (method, fns_ptr, method_oload_champ);
02640 
02641   if (method_oload_champ >= 0)
02642     {
02643       if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ))
02644         *valp = value_virtual_fn_field (&temp, fns_ptr, method_oload_champ,
02645                                         basetype, boffset);
02646       else
02647         *valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
02648                                 basetype, boffset);
02649     }
02650   else
02651     *symp = oload_syms[func_oload_champ];
02652 
02653   if (objp)
02654     {
02655       struct type *temp_type = check_typedef (value_type (temp));
02656       struct type *objtype = check_typedef (obj_type);
02657 
02658       if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
02659           && (TYPE_CODE (objtype) == TYPE_CODE_PTR
02660               || TYPE_CODE (objtype) == TYPE_CODE_REF))
02661         {
02662           temp = value_addr (temp);
02663         }
02664       *objp = temp;
02665     }
02666 
02667   do_cleanups (all_cleanups);
02668 
02669   switch (match_quality)
02670     {
02671     case INCOMPATIBLE:
02672       return 100;
02673     case NON_STANDARD:
02674       return 10;
02675     default:                            /* STANDARD */
02676       return 0;
02677     }
02678 }
02679 
02680 /* Find the best overload match, searching for FUNC_NAME in namespaces
02681    contained in QUALIFIED_NAME until it either finds a good match or
02682    runs out of namespaces.  It stores the overloaded functions in
02683    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
02684    calling function is responsible for freeing *OLOAD_SYMS and
02685    *OLOAD_CHAMP_BV.  If NO_ADL, argument dependent lookup is not 
02686    performned.  */
02687 
02688 static int
02689 find_oload_champ_namespace (struct value **args, int nargs,
02690                             const char *func_name,
02691                             const char *qualified_name,
02692                             struct symbol ***oload_syms,
02693                             struct badness_vector **oload_champ_bv,
02694                             const int no_adl)
02695 {
02696   int oload_champ;
02697 
02698   find_oload_champ_namespace_loop (args, nargs,
02699                                    func_name,
02700                                    qualified_name, 0,
02701                                    oload_syms, oload_champ_bv,
02702                                    &oload_champ,
02703                                    no_adl);
02704 
02705   return oload_champ;
02706 }
02707 
02708 /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
02709    how deep we've looked for namespaces, and the champ is stored in
02710    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
02711    if it isn't.  Other arguments are the same as in
02712    find_oload_champ_namespace
02713 
02714    It is the caller's responsibility to free *OLOAD_SYMS and
02715    *OLOAD_CHAMP_BV.  */
02716 
02717 static int
02718 find_oload_champ_namespace_loop (struct value **args, int nargs,
02719                                  const char *func_name,
02720                                  const char *qualified_name,
02721                                  int namespace_len,
02722                                  struct symbol ***oload_syms,
02723                                  struct badness_vector **oload_champ_bv,
02724                                  int *oload_champ,
02725                                  const int no_adl)
02726 {
02727   int next_namespace_len = namespace_len;
02728   int searched_deeper = 0;
02729   int num_fns = 0;
02730   struct cleanup *old_cleanups;
02731   int new_oload_champ;
02732   struct symbol **new_oload_syms;
02733   struct badness_vector *new_oload_champ_bv;
02734   char *new_namespace;
02735 
02736   if (next_namespace_len != 0)
02737     {
02738       gdb_assert (qualified_name[next_namespace_len] == ':');
02739       next_namespace_len +=  2;
02740     }
02741   next_namespace_len +=
02742     cp_find_first_component (qualified_name + next_namespace_len);
02743 
02744   /* Initialize these to values that can safely be xfree'd.  */
02745   *oload_syms = NULL;
02746   *oload_champ_bv = NULL;
02747 
02748   /* First, see if we have a deeper namespace we can search in.
02749      If we get a good match there, use it.  */
02750 
02751   if (qualified_name[next_namespace_len] == ':')
02752     {
02753       searched_deeper = 1;
02754 
02755       if (find_oload_champ_namespace_loop (args, nargs,
02756                                            func_name, qualified_name,
02757                                            next_namespace_len,
02758                                            oload_syms, oload_champ_bv,
02759                                            oload_champ, no_adl))
02760         {
02761           return 1;
02762         }
02763     };
02764 
02765   /* If we reach here, either we're in the deepest namespace or we
02766      didn't find a good match in a deeper namespace.  But, in the
02767      latter case, we still have a bad match in a deeper namespace;
02768      note that we might not find any match at all in the current
02769      namespace.  (There's always a match in the deepest namespace,
02770      because this overload mechanism only gets called if there's a
02771      function symbol to start off with.)  */
02772 
02773   old_cleanups = make_cleanup (xfree, *oload_syms);
02774   make_cleanup (xfree, *oload_champ_bv);
02775   new_namespace = alloca (namespace_len + 1);
02776   strncpy (new_namespace, qualified_name, namespace_len);
02777   new_namespace[namespace_len] = '\0';
02778   new_oload_syms = make_symbol_overload_list (func_name,
02779                                               new_namespace);
02780 
02781   /* If we have reached the deepest level perform argument
02782      determined lookup.  */
02783   if (!searched_deeper && !no_adl)
02784     {
02785       int ix;
02786       struct type **arg_types;
02787 
02788       /* Prepare list of argument types for overload resolution.  */
02789       arg_types = (struct type **)
02790         alloca (nargs * (sizeof (struct type *)));
02791       for (ix = 0; ix < nargs; ix++)
02792         arg_types[ix] = value_type (args[ix]);
02793       make_symbol_overload_list_adl (arg_types, nargs, func_name);
02794     }
02795 
02796   while (new_oload_syms[num_fns])
02797     ++num_fns;
02798 
02799   new_oload_champ = find_oload_champ (args, nargs, 0, num_fns,
02800                                       NULL, new_oload_syms,
02801                                       &new_oload_champ_bv);
02802 
02803   /* Case 1: We found a good match.  Free earlier matches (if any),
02804      and return it.  Case 2: We didn't find a good match, but we're
02805      not the deepest function.  Then go with the bad match that the
02806      deeper function found.  Case 3: We found a bad match, and we're
02807      the deepest function.  Then return what we found, even though
02808      it's a bad match.  */
02809 
02810   if (new_oload_champ != -1
02811       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
02812     {
02813       *oload_syms = new_oload_syms;
02814       *oload_champ = new_oload_champ;
02815       *oload_champ_bv = new_oload_champ_bv;
02816       do_cleanups (old_cleanups);
02817       return 1;
02818     }
02819   else if (searched_deeper)
02820     {
02821       xfree (new_oload_syms);
02822       xfree (new_oload_champ_bv);
02823       discard_cleanups (old_cleanups);
02824       return 0;
02825     }
02826   else
02827     {
02828       *oload_syms = new_oload_syms;
02829       *oload_champ = new_oload_champ;
02830       *oload_champ_bv = new_oload_champ_bv;
02831       do_cleanups (old_cleanups);
02832       return 0;
02833     }
02834 }
02835 
02836 /* Look for a function to take NARGS args of ARGS.  Find
02837    the best match from among the overloaded methods or functions
02838    (depending on METHOD) given by FNS_PTR or OLOAD_SYMS, respectively.
02839    The number of methods/functions in the list is given by NUM_FNS.
02840    Return the index of the best match; store an indication of the
02841    quality of the match in OLOAD_CHAMP_BV.
02842 
02843    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */
02844 
02845 static int
02846 find_oload_champ (struct value **args, int nargs, int method,
02847                   int num_fns, struct fn_field *fns_ptr,
02848                   struct symbol **oload_syms,
02849                   struct badness_vector **oload_champ_bv)
02850 {
02851   int ix;
02852   /* A measure of how good an overloaded instance is.  */
02853   struct badness_vector *bv;
02854   /* Index of best overloaded function.  */
02855   int oload_champ = -1;
02856   /* Current ambiguity state for overload resolution.  */
02857   int oload_ambiguous = 0;
02858   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */
02859 
02860   *oload_champ_bv = NULL;
02861 
02862   /* Consider each candidate in turn.  */
02863   for (ix = 0; ix < num_fns; ix++)
02864     {
02865       int jj;
02866       int static_offset = oload_method_static (method, fns_ptr, ix);
02867       int nparms;
02868       struct type **parm_types;
02869 
02870       if (method)
02871         {
02872           nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
02873         }
02874       else
02875         {
02876           /* If it's not a method, this is the proper place.  */
02877           nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));
02878         }
02879 
02880       /* Prepare array of parameter types.  */
02881       parm_types = (struct type **) 
02882         xmalloc (nparms * (sizeof (struct type *)));
02883       for (jj = 0; jj < nparms; jj++)
02884         parm_types[jj] = (method
02885                           ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
02886                           : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]), 
02887                                              jj));
02888 
02889       /* Compare parameter types to supplied argument types.  Skip
02890          THIS for static methods.  */
02891       bv = rank_function (parm_types, nparms, 
02892                           args + static_offset,
02893                           nargs - static_offset);
02894 
02895       if (!*oload_champ_bv)
02896         {
02897           *oload_champ_bv = bv;
02898           oload_champ = 0;
02899         }
02900       else /* See whether current candidate is better or worse than
02901               previous best.  */
02902         switch (compare_badness (bv, *oload_champ_bv))
02903           {
02904           case 0:               /* Top two contenders are equally good.  */
02905             oload_ambiguous = 1;
02906             break;
02907           case 1:               /* Incomparable top contenders.  */
02908             oload_ambiguous = 2;
02909             break;
02910           case 2:               /* New champion, record details.  */
02911             *oload_champ_bv = bv;
02912             oload_ambiguous = 0;
02913             oload_champ = ix;
02914             break;
02915           case 3:
02916           default:
02917             break;
02918           }
02919       xfree (parm_types);
02920       if (overload_debug)
02921         {
02922           if (method)
02923             fprintf_filtered (gdb_stderr,
02924                               "Overloaded method instance %s, # of parms %d\n",
02925                               fns_ptr[ix].physname, nparms);
02926           else
02927             fprintf_filtered (gdb_stderr,
02928                               "Overloaded function instance "
02929                               "%s # of parms %d\n",
02930                               SYMBOL_DEMANGLED_NAME (oload_syms[ix]), 
02931                               nparms);
02932           for (jj = 0; jj < nargs - static_offset; jj++)
02933             fprintf_filtered (gdb_stderr,
02934                               "...Badness @ %d : %d\n", 
02935                               jj, bv->rank[jj].rank);
02936           fprintf_filtered (gdb_stderr, "Overload resolution "
02937                             "champion is %d, ambiguous? %d\n", 
02938                             oload_champ, oload_ambiguous);
02939         }
02940     }
02941 
02942   return oload_champ;
02943 }
02944 
02945 /* Return 1 if we're looking at a static method, 0 if we're looking at
02946    a non-static method or a function that isn't a method.  */
02947 
02948 static int
02949 oload_method_static (int method, struct fn_field *fns_ptr, int index)
02950 {
02951   if (method && fns_ptr && index >= 0
02952       && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
02953     return 1;
02954   else
02955     return 0;
02956 }
02957 
02958 /* Check how good an overload match OLOAD_CHAMP_BV represents.  */
02959 
02960 static enum oload_classification
02961 classify_oload_match (struct badness_vector *oload_champ_bv,
02962                       int nargs,
02963                       int static_offset)
02964 {
02965   int ix;
02966   enum oload_classification worst = STANDARD;
02967 
02968   for (ix = 1; ix <= nargs - static_offset; ix++)
02969     {
02970       /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
02971          or worse return INCOMPATIBLE.  */
02972       if (compare_ranks (oload_champ_bv->rank[ix],
02973                          INCOMPATIBLE_TYPE_BADNESS) <= 0)
02974         return INCOMPATIBLE;    /* Truly mismatched types.  */
02975       /* Otherwise If this conversion is as bad as
02976          NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD.  */
02977       else if (compare_ranks (oload_champ_bv->rank[ix],
02978                               NS_POINTER_CONVERSION_BADNESS) <= 0)
02979         worst = NON_STANDARD;   /* Non-standard type conversions
02980                                    needed.  */
02981     }
02982 
02983   /* If no INCOMPATIBLE classification was found, return the worst one
02984      that was found (if any).  */
02985   return worst;
02986 }
02987 
02988 /* C++: return 1 is NAME is a legitimate name for the destructor of
02989    type TYPE.  If TYPE does not have a destructor, or if NAME is
02990    inappropriate for TYPE, an error is signaled.  Parameter TYPE should not yet
02991    have CHECK_TYPEDEF applied, this function will apply it itself.  */
02992 
02993 int
02994 destructor_name_p (const char *name, struct type *type)
02995 {
02996   if (name[0] == '~')
02997     {
02998       const char *dname = type_name_no_tag_or_error (type);
02999       const char *cp = strchr (dname, '<');
03000       unsigned int len;
03001 
03002       /* Do not compare the template part for template classes.  */
03003       if (cp == NULL)
03004         len = strlen (dname);
03005       else
03006         len = cp - dname;
03007       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
03008         error (_("name of destructor must equal name of class"));
03009       else
03010         return 1;
03011     }
03012   return 0;
03013 }
03014 
03015 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
03016    return the appropriate member (or the address of the member, if
03017    WANT_ADDRESS).  This function is used to resolve user expressions
03018    of the form "DOMAIN::NAME".  For more details on what happens, see
03019    the comment before value_struct_elt_for_reference.  */
03020 
03021 struct value *
03022 value_aggregate_elt (struct type *curtype, char *name,
03023                      struct type *expect_type, int want_address,
03024                      enum noside noside)
03025 {
03026   switch (TYPE_CODE (curtype))
03027     {
03028     case TYPE_CODE_STRUCT:
03029     case TYPE_CODE_UNION:
03030       return value_struct_elt_for_reference (curtype, 0, curtype, 
03031                                              name, expect_type,
03032                                              want_address, noside);
03033     case TYPE_CODE_NAMESPACE:
03034       return value_namespace_elt (curtype, name, 
03035                                   want_address, noside);
03036     default:
03037       internal_error (__FILE__, __LINE__,
03038                       _("non-aggregate type in value_aggregate_elt"));
03039     }
03040 }
03041 
03042 /* Compares the two method/function types T1 and T2 for "equality" 
03043    with respect to the methods' parameters.  If the types of the
03044    two parameter lists are the same, returns 1; 0 otherwise.  This
03045    comparison may ignore any artificial parameters in T1 if
03046    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
03047    the first artificial parameter in T1, assumed to be a 'this' pointer.
03048 
03049    The type T2 is expected to have come from make_params (in eval.c).  */
03050 
03051 static int
03052 compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
03053 {
03054   int start = 0;
03055 
03056   if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
03057     ++start;
03058 
03059   /* If skipping artificial fields, find the first real field
03060      in T1.  */
03061   if (skip_artificial)
03062     {
03063       while (start < TYPE_NFIELDS (t1)
03064              && TYPE_FIELD_ARTIFICIAL (t1, start))
03065         ++start;
03066     }
03067 
03068   /* Now compare parameters.  */
03069 
03070   /* Special case: a method taking void.  T1 will contain no
03071      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
03072   if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
03073       && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
03074     return 1;
03075 
03076   if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
03077     {
03078       int i;
03079 
03080       for (i = 0; i < TYPE_NFIELDS (t2); ++i)
03081         {
03082           if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
03083                                             TYPE_FIELD_TYPE (t2, i), NULL),
03084                              EXACT_MATCH_BADNESS) != 0)
03085             return 0;
03086         }
03087 
03088       return 1;
03089     }
03090 
03091   return 0;
03092 }
03093 
03094 /* C++: Given an aggregate type CURTYPE, and a member name NAME,
03095    return the address of this member as a "pointer to member" type.
03096    If INTYPE is non-null, then it will be the type of the member we
03097    are looking for.  This will help us resolve "pointers to member
03098    functions".  This function is used to resolve user expressions of
03099    the form "DOMAIN::NAME".  */
03100 
03101 static struct value *
03102 value_struct_elt_for_reference (struct type *domain, int offset,
03103                                 struct type *curtype, char *name,
03104                                 struct type *intype, 
03105                                 int want_address,
03106                                 enum noside noside)
03107 {
03108   struct type *t = curtype;
03109   int i;
03110   struct value *v, *result;
03111 
03112   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
03113       && TYPE_CODE (t) != TYPE_CODE_UNION)
03114     error (_("Internal error: non-aggregate type "
03115              "to value_struct_elt_for_reference"));
03116 
03117   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
03118     {
03119       const char *t_field_name = TYPE_FIELD_NAME (t, i);
03120 
03121       if (t_field_name && strcmp (t_field_name, name) == 0)
03122         {
03123           if (field_is_static (&TYPE_FIELD (t, i)))
03124             {
03125               v = value_static_field (t, i);
03126               if (v == NULL)
03127                 error (_("static field %s has been optimized out"),
03128                        name);
03129               if (want_address)
03130                 v = value_addr (v);
03131               return v;
03132             }
03133           if (TYPE_FIELD_PACKED (t, i))
03134             error (_("pointers to bitfield members not allowed"));
03135 
03136           if (want_address)
03137             return value_from_longest
03138               (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
03139                offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
03140           else if (noside == EVAL_AVOID_SIDE_EFFECTS)
03141             return allocate_value (TYPE_FIELD_TYPE (t, i));
03142           else
03143             error (_("Cannot reference non-static field \"%s\""), name);
03144         }
03145     }
03146 
03147   /* C++: If it was not found as a data field, then try to return it
03148      as a pointer to a method.  */
03149 
03150   /* Perform all necessary dereferencing.  */
03151   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
03152     intype = TYPE_TARGET_TYPE (intype);
03153 
03154   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
03155     {
03156       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
03157       char dem_opname[64];
03158 
03159       if (strncmp (t_field_name, "__", 2) == 0 
03160           || strncmp (t_field_name, "op", 2) == 0 
03161           || strncmp (t_field_name, "type", 4) == 0)
03162         {
03163           if (cplus_demangle_opname (t_field_name, 
03164                                      dem_opname, DMGL_ANSI))
03165             t_field_name = dem_opname;
03166           else if (cplus_demangle_opname (t_field_name, 
03167                                           dem_opname, 0))
03168             t_field_name = dem_opname;
03169         }
03170       if (t_field_name && strcmp (t_field_name, name) == 0)
03171         {
03172           int j;
03173           int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
03174           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
03175 
03176           check_stub_method_group (t, i);
03177 
03178           if (intype)
03179             {
03180               for (j = 0; j < len; ++j)
03181                 {
03182                   if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
03183                       || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
03184                                              intype, 1))
03185                     break;
03186                 }
03187 
03188               if (j == len)
03189                 error (_("no member function matches "
03190                          "that type instantiation"));
03191             }
03192           else
03193             {
03194               int ii;
03195 
03196               j = -1;
03197               for (ii = 0; ii < len; ++ii)
03198                 {
03199                   /* Skip artificial methods.  This is necessary if,
03200                      for example, the user wants to "print
03201                      subclass::subclass" with only one user-defined
03202                      constructor.  There is no ambiguity in this case.
03203                      We are careful here to allow artificial methods
03204                      if they are the unique result.  */
03205                   if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
03206                     {
03207                       if (j == -1)
03208                         j = ii;
03209                       continue;
03210                     }
03211 
03212                   /* Desired method is ambiguous if more than one
03213                      method is defined.  */
03214                   if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
03215                     error (_("non-unique member `%s' requires "
03216                              "type instantiation"), name);
03217 
03218                   j = ii;
03219                 }
03220 
03221               if (j == -1)
03222                 error (_("no matching member function"));
03223             }
03224 
03225           if (TYPE_FN_FIELD_STATIC_P (f, j))
03226             {
03227               struct symbol *s = 
03228                 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
03229                                0, VAR_DOMAIN, 0);
03230 
03231               if (s == NULL)
03232                 return NULL;
03233 
03234               if (want_address)
03235                 return value_addr (read_var_value (s, 0));
03236               else
03237                 return read_var_value (s, 0);
03238             }
03239 
03240           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
03241             {
03242               if (want_address)
03243                 {
03244                   result = allocate_value
03245                     (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
03246                   cplus_make_method_ptr (value_type (result),
03247                                          value_contents_writeable (result),
03248                                          TYPE_FN_FIELD_VOFFSET (f, j), 1);
03249                 }
03250               else if (noside == EVAL_AVOID_SIDE_EFFECTS)
03251                 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
03252               else
03253                 error (_("Cannot reference virtual member function \"%s\""),
03254                        name);
03255             }
03256           else
03257             {
03258               struct symbol *s = 
03259                 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
03260                                0, VAR_DOMAIN, 0);
03261 
03262               if (s == NULL)
03263                 return NULL;
03264 
03265               v = read_var_value (s, 0);
03266               if (!want_address)
03267                 result = v;
03268               else
03269                 {
03270                   result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
03271                   cplus_make_method_ptr (value_type (result),
03272                                          value_contents_writeable (result),
03273                                          value_address (v), 0);
03274                 }
03275             }
03276           return result;
03277         }
03278     }
03279   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
03280     {
03281       struct value *v;
03282       int base_offset;
03283 
03284       if (BASETYPE_VIA_VIRTUAL (t, i))
03285         base_offset = 0;
03286       else
03287         base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
03288       v = value_struct_elt_for_reference (domain,
03289                                           offset + base_offset,
03290                                           TYPE_BASECLASS (t, i),
03291                                           name, intype, 
03292                                           want_address, noside);
03293       if (v)
03294         return v;
03295     }
03296 
03297   /* As a last chance, pretend that CURTYPE is a namespace, and look
03298      it up that way; this (frequently) works for types nested inside
03299      classes.  */
03300 
03301   return value_maybe_namespace_elt (curtype, name, 
03302                                     want_address, noside);
03303 }
03304 
03305 /* C++: Return the member NAME of the namespace given by the type
03306    CURTYPE.  */
03307 
03308 static struct value *
03309 value_namespace_elt (const struct type *curtype,
03310                      char *name, int want_address,
03311                      enum noside noside)
03312 {
03313   struct value *retval = value_maybe_namespace_elt (curtype, name,
03314                                                     want_address, 
03315                                                     noside);
03316 
03317   if (retval == NULL)
03318     error (_("No symbol \"%s\" in namespace \"%s\"."), 
03319            name, TYPE_TAG_NAME (curtype));
03320 
03321   return retval;
03322 }
03323 
03324 /* A helper function used by value_namespace_elt and
03325    value_struct_elt_for_reference.  It looks up NAME inside the
03326    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
03327    is a class and NAME refers to a type in CURTYPE itself (as opposed
03328    to, say, some base class of CURTYPE).  */
03329 
03330 static struct value *
03331 value_maybe_namespace_elt (const struct type *curtype,
03332                            char *name, int want_address,
03333                            enum noside noside)
03334 {
03335   const char *namespace_name = TYPE_TAG_NAME (curtype);
03336   struct symbol *sym;
03337   struct value *result;
03338 
03339   sym = cp_lookup_symbol_namespace (namespace_name, name,
03340                                     get_selected_block (0), VAR_DOMAIN);
03341 
03342   if (sym == NULL)
03343     {
03344       char *concatenated_name = alloca (strlen (namespace_name) + 2
03345                                         + strlen (name) + 1);
03346 
03347       sprintf (concatenated_name, "%s::%s", namespace_name, name);
03348       sym = lookup_static_symbol_aux (concatenated_name, VAR_DOMAIN);
03349     }
03350 
03351   if (sym == NULL)
03352     return NULL;
03353   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
03354            && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
03355     result = allocate_value (SYMBOL_TYPE (sym));
03356   else
03357     result = value_of_variable (sym, get_selected_block (0));
03358 
03359   if (result && want_address)
03360     result = value_addr (result);
03361 
03362   return result;
03363 }
03364 
03365 /* Given a pointer or a reference value V, find its real (RTTI) type.
03366 
03367    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
03368    and refer to the values computed for the object pointed to.  */
03369 
03370 struct type *
03371 value_rtti_indirect_type (struct value *v, int *full, 
03372                           int *top, int *using_enc)
03373 {
03374   struct value *target;
03375   struct type *type, *real_type, *target_type;
03376 
03377   type = value_type (v);
03378   type = check_typedef (type);
03379   if (TYPE_CODE (type) == TYPE_CODE_REF)
03380     target = coerce_ref (v);
03381   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
03382     target = value_ind (v);
03383   else
03384     return NULL;
03385 
03386   real_type = value_rtti_type (target, full, top, using_enc);
03387 
03388   if (real_type)
03389     {
03390       /* Copy qualifiers to the referenced object.  */
03391       target_type = value_type (target);
03392       real_type = make_cv_type (TYPE_CONST (target_type),
03393                                 TYPE_VOLATILE (target_type), real_type, NULL);
03394       if (TYPE_CODE (type) == TYPE_CODE_REF)
03395         real_type = lookup_reference_type (real_type);
03396       else if (TYPE_CODE (type) == TYPE_CODE_PTR)
03397         real_type = lookup_pointer_type (real_type);
03398       else
03399         internal_error (__FILE__, __LINE__, _("Unexpected value type."));
03400 
03401       /* Copy qualifiers to the pointer/reference.  */
03402       real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
03403                                 real_type, NULL);
03404     }
03405 
03406   return real_type;
03407 }
03408 
03409 /* Given a value pointed to by ARGP, check its real run-time type, and
03410    if that is different from the enclosing type, create a new value
03411    using the real run-time type as the enclosing type (and of the same
03412    type as ARGP) and return it, with the embedded offset adjusted to
03413    be the correct offset to the enclosed object.  RTYPE is the type,
03414    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
03415    by value_rtti_type().  If these are available, they can be supplied
03416    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
03417    NULL if they're not available.  */
03418 
03419 struct value *
03420 value_full_object (struct value *argp, 
03421                    struct type *rtype, 
03422                    int xfull, int xtop,
03423                    int xusing_enc)
03424 {
03425   struct type *real_type;
03426   int full = 0;
03427   int top = -1;
03428   int using_enc = 0;
03429   struct value *new_val;
03430 
03431   if (rtype)
03432     {
03433       real_type = rtype;
03434       full = xfull;
03435       top = xtop;
03436       using_enc = xusing_enc;
03437     }
03438   else
03439     real_type = value_rtti_type (argp, &full, &top, &using_enc);
03440 
03441   /* If no RTTI data, or if object is already complete, do nothing.  */
03442   if (!real_type || real_type == value_enclosing_type (argp))
03443     return argp;
03444 
03445   /* In a destructor we might see a real type that is a superclass of
03446      the object's type.  In this case it is better to leave the object
03447      as-is.  */
03448   if (full
03449       && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
03450     return argp;
03451 
03452   /* If we have the full object, but for some reason the enclosing
03453      type is wrong, set it.  */
03454   /* pai: FIXME -- sounds iffy */
03455   if (full)
03456     {
03457       argp = value_copy (argp);
03458       set_value_enclosing_type (argp, real_type);
03459       return argp;
03460     }
03461 
03462   /* Check if object is in memory.  */
03463   if (VALUE_LVAL (argp) != lval_memory)
03464     {
03465       warning (_("Couldn't retrieve complete object of RTTI "
03466                  "type %s; object may be in register(s)."), 
03467                TYPE_NAME (real_type));
03468 
03469       return argp;
03470     }
03471 
03472   /* All other cases -- retrieve the complete object.  */
03473   /* Go back by the computed top_offset from the beginning of the
03474      object, adjusting for the embedded offset of argp if that's what
03475      value_rtti_type used for its computation.  */
03476   new_val = value_at_lazy (real_type, value_address (argp) - top +
03477                            (using_enc ? 0 : value_embedded_offset (argp)));
03478   deprecated_set_value_type (new_val, value_type (argp));
03479   set_value_embedded_offset (new_val, (using_enc
03480                                        ? top + value_embedded_offset (argp)
03481                                        : top));
03482   return new_val;
03483 }
03484 
03485 
03486 /* Return the value of the local variable, if one exists.  Throw error
03487    otherwise, such as if the request is made in an inappropriate context.  */
03488 
03489 struct value *
03490 value_of_this (const struct language_defn *lang)
03491 {
03492   struct symbol *sym;
03493   struct block *b;
03494   struct frame_info *frame;
03495 
03496   if (!lang->la_name_of_this)
03497     error (_("no `this' in current language"));
03498 
03499   frame = get_selected_frame (_("no frame selected"));
03500 
03501   b = get_frame_block (frame, NULL);
03502 
03503   sym = lookup_language_this (lang, b);
03504   if (sym == NULL)
03505     error (_("current stack frame does not contain a variable named `%s'"),
03506            lang->la_name_of_this);
03507 
03508   return read_var_value (sym, frame);
03509 }
03510 
03511 /* Return the value of the local variable, if one exists.  Return NULL
03512    otherwise.  Never throw error.  */
03513 
03514 struct value *
03515 value_of_this_silent (const struct language_defn *lang)
03516 {
03517   struct value *ret = NULL;
03518   volatile struct gdb_exception except;
03519 
03520   TRY_CATCH (except, RETURN_MASK_ERROR)
03521     {
03522       ret = value_of_this (lang);
03523     }
03524 
03525   return ret;
03526 }
03527 
03528 /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
03529    elements long, starting at LOWBOUND.  The result has the same lower
03530    bound as the original ARRAY.  */
03531 
03532 struct value *
03533 value_slice (struct value *array, int lowbound, int length)
03534 {
03535   struct type *slice_range_type, *slice_type, *range_type;
03536   LONGEST lowerbound, upperbound;
03537   struct value *slice;
03538   struct type *array_type;
03539 
03540   array_type = check_typedef (value_type (array));
03541   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
03542       && TYPE_CODE (array_type) != TYPE_CODE_STRING)
03543     error (_("cannot take slice of non-array"));
03544 
03545   range_type = TYPE_INDEX_TYPE (array_type);
03546   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
03547     error (_("slice from bad array or bitstring"));
03548 
03549   if (lowbound < lowerbound || length < 0
03550       || lowbound + length - 1 > upperbound)
03551     error (_("slice out of range"));
03552 
03553   /* FIXME-type-allocation: need a way to free this type when we are
03554      done with it.  */
03555   slice_range_type = create_range_type ((struct type *) NULL,
03556                                         TYPE_TARGET_TYPE (range_type),
03557                                         lowbound, 
03558                                         lowbound + length - 1);
03559 
03560     {
03561       struct type *element_type = TYPE_TARGET_TYPE (array_type);
03562       LONGEST offset =
03563         (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));
03564 
03565       slice_type = create_array_type ((struct type *) NULL, 
03566                                       element_type,
03567                                       slice_range_type);
03568       TYPE_CODE (slice_type) = TYPE_CODE (array_type);
03569 
03570       if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
03571         slice = allocate_value_lazy (slice_type);
03572       else
03573         {
03574           slice = allocate_value (slice_type);
03575           value_contents_copy (slice, 0, array, offset,
03576                                TYPE_LENGTH (slice_type));
03577         }
03578 
03579       set_value_component_location (slice, array);
03580       VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
03581       set_value_offset (slice, value_offset (array) + offset);
03582     }
03583   return slice;
03584 }
03585 
03586 /* Create a value for a FORTRAN complex number.  Currently most of the
03587    time values are coerced to COMPLEX*16 (i.e. a complex number
03588    composed of 2 doubles.  This really should be a smarter routine
03589    that figures out precision inteligently as opposed to assuming
03590    doubles.  FIXME: fmb  */
03591 
03592 struct value *
03593 value_literal_complex (struct value *arg1, 
03594                        struct value *arg2,
03595                        struct type *type)
03596 {
03597   struct value *val;
03598   struct type *real_type = TYPE_TARGET_TYPE (type);
03599 
03600   val = allocate_value (type);
03601   arg1 = value_cast (real_type, arg1);
03602   arg2 = value_cast (real_type, arg2);
03603 
03604   memcpy (value_contents_raw (val),
03605           value_contents (arg1), TYPE_LENGTH (real_type));
03606   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
03607           value_contents (arg2), TYPE_LENGTH (real_type));
03608   return val;
03609 }
03610 
03611 /* Cast a value into the appropriate complex data type.  */
03612 
03613 static struct value *
03614 cast_into_complex (struct type *type, struct value *val)
03615 {
03616   struct type *real_type = TYPE_TARGET_TYPE (type);
03617 
03618   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
03619     {
03620       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
03621       struct value *re_val = allocate_value (val_real_type);
03622       struct value *im_val = allocate_value (val_real_type);
03623 
03624       memcpy (value_contents_raw (re_val),
03625               value_contents (val), TYPE_LENGTH (val_real_type));
03626       memcpy (value_contents_raw (im_val),
03627               value_contents (val) + TYPE_LENGTH (val_real_type),
03628               TYPE_LENGTH (val_real_type));
03629 
03630       return value_literal_complex (re_val, im_val, type);
03631     }
03632   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
03633            || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
03634     return value_literal_complex (val, 
03635                                   value_zero (real_type, not_lval), 
03636                                   type);
03637   else
03638     error (_("cannot cast non-number to complex"));
03639 }
03640 
03641 void
03642 _initialize_valops (void)
03643 {
03644   add_setshow_boolean_cmd ("overload-resolution", class_support,
03645                            &overload_resolution, _("\
03646 Set overload resolution in evaluating C++ functions."), _("\
03647 Show overload resolution in evaluating C++ functions."), 
03648                            NULL, NULL,
03649                            show_overload_resolution,
03650                            &setlist, &showlist);
03651   overload_resolution = 1;
03652 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines