gdb/valops.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Perform non-arithmetic operations on values, for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "value.h"
  18. #include "frame.h"
  19. #include "inferior.h"
  20. #include "gdbcore.h"
  21. #include "target.h"
  22. #include "demangle.h"
  23. #include "language.h"
  24. #include "gdbcmd.h"
  25. #include "regcache.h"
  26. #include "cp-abi.h"
  27. #include "block.h"
  28. #include "infcall.h"
  29. #include "dictionary.h"
  30. #include "cp-support.h"
  31. #include "dfp.h"
  32. #include "tracepoint.h"
  33. #include "observer.h"
  34. #include "objfiles.h"
  35. #include "extension.h"

  36. extern unsigned int overload_debug;
  37. /* Local functions.  */

  38. static int typecmp (int staticp, int varargs, int nargs,
  39.                     struct field t1[], struct value *t2[]);

  40. static struct value *search_struct_field (const char *, struct value *,
  41.                                           int, struct type *, int);

  42. static struct value *search_struct_method (const char *, struct value **,
  43.                                            struct value **,
  44.                                            int, int *, struct type *);

  45. static int find_oload_champ_namespace (struct value **, int,
  46.                                        const char *, const char *,
  47.                                        struct symbol ***,
  48.                                        struct badness_vector **,
  49.                                        const int no_adl);

  50. static
  51. int find_oload_champ_namespace_loop (struct value **, int,
  52.                                      const char *, const char *,
  53.                                      int, struct symbol ***,
  54.                                      struct badness_vector **, int *,
  55.                                      const int no_adl);

  56. static int find_oload_champ (struct value **, int, int,
  57.                              struct fn_field *, VEC (xmethod_worker_ptr) *,
  58.                              struct symbol **, struct badness_vector **);

  59. static int oload_method_static_p (struct fn_field *, int);

  60. enum oload_classification { STANDARD, NON_STANDARD, INCOMPATIBLE };

  61. static enum
  62. oload_classification classify_oload_match (struct badness_vector *,
  63.                                            int, int);

  64. static struct value *value_struct_elt_for_reference (struct type *,
  65.                                                      int, struct type *,
  66.                                                      const char *,
  67.                                                      struct type *,
  68.                                                      int, enum noside);

  69. static struct value *value_namespace_elt (const struct type *,
  70.                                           const char *, int , enum noside);

  71. static struct value *value_maybe_namespace_elt (const struct type *,
  72.                                                 const char *, int,
  73.                                                 enum noside);

  74. static CORE_ADDR allocate_space_in_inferior (int);

  75. static struct value *cast_into_complex (struct type *, struct value *);

  76. static void find_method_list (struct value **, const char *,
  77.                               int, struct type *, struct fn_field **, int *,
  78.                               VEC (xmethod_worker_ptr) **,
  79.                               struct type **, int *);

  80. void _initialize_valops (void);

  81. #if 0
  82. /* Flag for whether we want to abandon failed expression evals by
  83.    default.  */

  84. static int auto_abandon = 0;
  85. #endif

  86. int overload_resolution = 0;
  87. static void
  88. show_overload_resolution (struct ui_file *file, int from_tty,
  89.                           struct cmd_list_element *c,
  90.                           const char *value)
  91. {
  92.   fprintf_filtered (file, _("Overload resolution in evaluating "
  93.                             "C++ functions is %s.\n"),
  94.                     value);
  95. }

  96. /* Find the address of function name NAME in the inferior.  If OBJF_P
  97.    is non-NULL, *OBJF_P will be set to the OBJFILE where the function
  98.    is defined.  */

  99. struct value *
  100. find_function_in_inferior (const char *name, struct objfile **objf_p)
  101. {
  102.   struct symbol *sym;

  103.   sym = lookup_symbol (name, 0, VAR_DOMAIN, 0);
  104.   if (sym != NULL)
  105.     {
  106.       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
  107.         {
  108.           error (_("\"%s\" exists in this program but is not a function."),
  109.                  name);
  110.         }

  111.       if (objf_p)
  112.         *objf_p = symbol_objfile (sym);

  113.       return value_of_variable (sym, NULL);
  114.     }
  115.   else
  116.     {
  117.       struct bound_minimal_symbol msymbol =
  118.         lookup_bound_minimal_symbol (name);

  119.       if (msymbol.minsym != NULL)
  120.         {
  121.           struct objfile *objfile = msymbol.objfile;
  122.           struct gdbarch *gdbarch = get_objfile_arch (objfile);

  123.           struct type *type;
  124.           CORE_ADDR maddr;
  125.           type = lookup_pointer_type (builtin_type (gdbarch)->builtin_char);
  126.           type = lookup_function_type (type);
  127.           type = lookup_pointer_type (type);
  128.           maddr = BMSYMBOL_VALUE_ADDRESS (msymbol);

  129.           if (objf_p)
  130.             *objf_p = objfile;

  131.           return value_from_pointer (type, maddr);
  132.         }
  133.       else
  134.         {
  135.           if (!target_has_execution)
  136.             error (_("evaluation of this expression "
  137.                      "requires the target program to be active"));
  138.           else
  139.             error (_("evaluation of this expression requires the "
  140.                      "program to have a function \"%s\"."),
  141.                    name);
  142.         }
  143.     }
  144. }

  145. /* Allocate NBYTES of space in the inferior using the inferior's
  146.    malloc and return a value that is a pointer to the allocated
  147.    space.  */

  148. struct value *
  149. value_allocate_space_in_inferior (int len)
  150. {
  151.   struct objfile *objf;
  152.   struct value *val = find_function_in_inferior ("malloc", &objf);
  153.   struct gdbarch *gdbarch = get_objfile_arch (objf);
  154.   struct value *blocklen;

  155.   blocklen = value_from_longest (builtin_type (gdbarch)->builtin_int, len);
  156.   val = call_function_by_hand (val, 1, &blocklen);
  157.   if (value_logical_not (val))
  158.     {
  159.       if (!target_has_execution)
  160.         error (_("No memory available to program now: "
  161.                  "you need to start the target first"));
  162.       else
  163.         error (_("No memory available to program: call to malloc failed"));
  164.     }
  165.   return val;
  166. }

  167. static CORE_ADDR
  168. allocate_space_in_inferior (int len)
  169. {
  170.   return value_as_long (value_allocate_space_in_inferior (len));
  171. }

  172. /* Cast struct value VAL to type TYPE and return as a value.
  173.    Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
  174.    for this to work.  Typedef to one of the codes is permitted.
  175.    Returns NULL if the cast is neither an upcast nor a downcast.  */

  176. static struct value *
  177. value_cast_structs (struct type *type, struct value *v2)
  178. {
  179.   struct type *t1;
  180.   struct type *t2;
  181.   struct value *v;

  182.   gdb_assert (type != NULL && v2 != NULL);

  183.   t1 = check_typedef (type);
  184.   t2 = check_typedef (value_type (v2));

  185.   /* Check preconditions.  */
  186.   gdb_assert ((TYPE_CODE (t1) == TYPE_CODE_STRUCT
  187.                || TYPE_CODE (t1) == TYPE_CODE_UNION)
  188.               && !!"Precondition is that type is of STRUCT or UNION kind.");
  189.   gdb_assert ((TYPE_CODE (t2) == TYPE_CODE_STRUCT
  190.                || TYPE_CODE (t2) == TYPE_CODE_UNION)
  191.               && !!"Precondition is that value is of STRUCT or UNION kind");

  192.   if (TYPE_NAME (t1) != NULL
  193.       && TYPE_NAME (t2) != NULL
  194.       && !strcmp (TYPE_NAME (t1), TYPE_NAME (t2)))
  195.     return NULL;

  196.   /* Upcasting: look in the type of the source to see if it contains the
  197.      type of the target as a superclass.  If so, we'll need to
  198.      offset the pointer rather than just change its type.  */
  199.   if (TYPE_NAME (t1) != NULL)
  200.     {
  201.       v = search_struct_field (type_name_no_tag (t1),
  202.                                v2, 0, t2, 1);
  203.       if (v)
  204.         return v;
  205.     }

  206.   /* Downcasting: look in the type of the target to see if it contains the
  207.      type of the source as a superclass.  If so, we'll need to
  208.      offset the pointer rather than just change its type.  */
  209.   if (TYPE_NAME (t2) != NULL)
  210.     {
  211.       /* Try downcasting using the run-time type of the value.  */
  212.       int full, top, using_enc;
  213.       struct type *real_type;

  214.       real_type = value_rtti_type (v2, &full, &top, &using_enc);
  215.       if (real_type)
  216.         {
  217.           v = value_full_object (v2, real_type, full, top, using_enc);
  218.           v = value_at_lazy (real_type, value_address (v));
  219.           real_type = value_type (v);

  220.           /* We might be trying to cast to the outermost enclosing
  221.              type, in which case search_struct_field won't work.  */
  222.           if (TYPE_NAME (real_type) != NULL
  223.               && !strcmp (TYPE_NAME (real_type), TYPE_NAME (t1)))
  224.             return v;

  225.           v = search_struct_field (type_name_no_tag (t2), v, 0, real_type, 1);
  226.           if (v)
  227.             return v;
  228.         }

  229.       /* Try downcasting using information from the destination type
  230.          T2.  This wouldn't work properly for classes with virtual
  231.          bases, but those were handled above.  */
  232.       v = search_struct_field (type_name_no_tag (t2),
  233.                                value_zero (t1, not_lval), 0, t1, 1);
  234.       if (v)
  235.         {
  236.           /* Downcasting is possible (t1 is superclass of v2).  */
  237.           CORE_ADDR addr2 = value_address (v2);

  238.           addr2 -= value_address (v) + value_embedded_offset (v);
  239.           return value_at (type, addr2);
  240.         }
  241.     }

  242.   return NULL;
  243. }

  244. /* Cast one pointer or reference type to another.  Both TYPE and
  245.    the type of ARG2 should be pointer types, or else both should be
  246.    reference types.  If SUBCLASS_CHECK is non-zero, this will force a
  247.    check to see whether TYPE is a superclass of ARG2's type.  If
  248.    SUBCLASS_CHECK is zero, then the subclass check is done only when
  249.    ARG2 is itself non-zero.  Returns the new pointer or reference.  */

  250. struct value *
  251. value_cast_pointers (struct type *type, struct value *arg2,
  252.                      int subclass_check)
  253. {
  254.   struct type *type1 = check_typedef (type);
  255.   struct type *type2 = check_typedef (value_type (arg2));
  256.   struct type *t1 = check_typedef (TYPE_TARGET_TYPE (type1));
  257.   struct type *t2 = check_typedef (TYPE_TARGET_TYPE (type2));

  258.   if (TYPE_CODE (t1) == TYPE_CODE_STRUCT
  259.       && TYPE_CODE (t2) == TYPE_CODE_STRUCT
  260.       && (subclass_check || !value_logical_not (arg2)))
  261.     {
  262.       struct value *v2;

  263.       if (TYPE_CODE (type2) == TYPE_CODE_REF)
  264.         v2 = coerce_ref (arg2);
  265.       else
  266.         v2 = value_ind (arg2);
  267.       gdb_assert (TYPE_CODE (check_typedef (value_type (v2)))
  268.                   == TYPE_CODE_STRUCT && !!"Why did coercion fail?");
  269.       v2 = value_cast_structs (t1, v2);
  270.       /* At this point we have what we can have, un-dereference if needed.  */
  271.       if (v2)
  272.         {
  273.           struct value *v = value_addr (v2);

  274.           deprecated_set_value_type (v, type);
  275.           return v;
  276.         }
  277.     }

  278.   /* No superclass found, just change the pointer type.  */
  279.   arg2 = value_copy (arg2);
  280.   deprecated_set_value_type (arg2, type);
  281.   set_value_enclosing_type (arg2, type);
  282.   set_value_pointed_to_offset (arg2, 0);        /* pai: chk_val */
  283.   return arg2;
  284. }

  285. /* Cast value ARG2 to type TYPE and return as a value.
  286.    More general than a C cast: accepts any two types of the same length,
  287.    and if ARG2 is an lvalue it can be cast into anything at all.  */
  288. /* In C++, casts may change pointer or object representations.  */

  289. struct value *
  290. value_cast (struct type *type, struct value *arg2)
  291. {
  292.   enum type_code code1;
  293.   enum type_code code2;
  294.   int scalar;
  295.   struct type *type2;

  296.   int convert_to_boolean = 0;

  297.   if (value_type (arg2) == type)
  298.     return arg2;

  299.   code1 = TYPE_CODE (check_typedef (type));

  300.   /* Check if we are casting struct reference to struct reference.  */
  301.   if (code1 == TYPE_CODE_REF)
  302.     {
  303.       /* We dereference type; then we recurse and finally
  304.          we generate value of the given reference.  Nothing wrong with
  305.          that.  */
  306.       struct type *t1 = check_typedef (type);
  307.       struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
  308.       struct value *val =  value_cast (dereftype, arg2);

  309.       return value_ref (val);
  310.     }

  311.   code2 = TYPE_CODE (check_typedef (value_type (arg2)));

  312.   if (code2 == TYPE_CODE_REF)
  313.     /* We deref the value and then do the cast.  */
  314.     return value_cast (type, coerce_ref (arg2));

  315.   CHECK_TYPEDEF (type);
  316.   code1 = TYPE_CODE (type);
  317.   arg2 = coerce_ref (arg2);
  318.   type2 = check_typedef (value_type (arg2));

  319.   /* You can't cast to a reference type.  See value_cast_pointers
  320.      instead.  */
  321.   gdb_assert (code1 != TYPE_CODE_REF);

  322.   /* A cast to an undetermined-length array_type, such as
  323.      (TYPE [])OBJECT, is treated like a cast to (TYPE [N])OBJECT,
  324.      where N is sizeof(OBJECT)/sizeof(TYPE).  */
  325.   if (code1 == TYPE_CODE_ARRAY)
  326.     {
  327.       struct type *element_type = TYPE_TARGET_TYPE (type);
  328.       unsigned element_length = TYPE_LENGTH (check_typedef (element_type));

  329.       if (element_length > 0 && TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
  330.         {
  331.           struct type *range_type = TYPE_INDEX_TYPE (type);
  332.           int val_length = TYPE_LENGTH (type2);
  333.           LONGEST low_bound, high_bound, new_length;

  334.           if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
  335.             low_bound = 0, high_bound = 0;
  336.           new_length = val_length / element_length;
  337.           if (val_length % element_length != 0)
  338.             warning (_("array element type size does not "
  339.                        "divide object size in cast"));
  340.           /* FIXME-type-allocation: need a way to free this type when
  341.              we are done with it.  */
  342.           range_type = create_static_range_type ((struct type *) NULL,
  343.                                                  TYPE_TARGET_TYPE (range_type),
  344.                                                  low_bound,
  345.                                                  new_length + low_bound - 1);
  346.           deprecated_set_value_type (arg2,
  347.                                      create_array_type ((struct type *) NULL,
  348.                                                         element_type,
  349.                                                         range_type));
  350.           return arg2;
  351.         }
  352.     }

  353.   if (current_language->c_style_arrays
  354.       && TYPE_CODE (type2) == TYPE_CODE_ARRAY
  355.       && !TYPE_VECTOR (type2))
  356.     arg2 = value_coerce_array (arg2);

  357.   if (TYPE_CODE (type2) == TYPE_CODE_FUNC)
  358.     arg2 = value_coerce_function (arg2);

  359.   type2 = check_typedef (value_type (arg2));
  360.   code2 = TYPE_CODE (type2);

  361.   if (code1 == TYPE_CODE_COMPLEX)
  362.     return cast_into_complex (type, arg2);
  363.   if (code1 == TYPE_CODE_BOOL)
  364.     {
  365.       code1 = TYPE_CODE_INT;
  366.       convert_to_boolean = 1;
  367.     }
  368.   if (code1 == TYPE_CODE_CHAR)
  369.     code1 = TYPE_CODE_INT;
  370.   if (code2 == TYPE_CODE_BOOL || code2 == TYPE_CODE_CHAR)
  371.     code2 = TYPE_CODE_INT;

  372.   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
  373.             || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
  374.             || code2 == TYPE_CODE_RANGE);

  375.   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
  376.       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
  377.       && TYPE_NAME (type) != 0)
  378.     {
  379.       struct value *v = value_cast_structs (type, arg2);

  380.       if (v)
  381.         return v;
  382.     }

  383.   if (code1 == TYPE_CODE_FLT && scalar)
  384.     return value_from_double (type, value_as_double (arg2));
  385.   else if (code1 == TYPE_CODE_DECFLOAT && scalar)
  386.     {
  387.       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  388.       int dec_len = TYPE_LENGTH (type);
  389.       gdb_byte dec[16];

  390.       if (code2 == TYPE_CODE_FLT)
  391.         decimal_from_floating (arg2, dec, dec_len, byte_order);
  392.       else if (code2 == TYPE_CODE_DECFLOAT)
  393.         decimal_convert (value_contents (arg2), TYPE_LENGTH (type2),
  394.                          byte_order, dec, dec_len, byte_order);
  395.       else
  396.         /* The only option left is an integral type.  */
  397.         decimal_from_integral (arg2, dec, dec_len, byte_order);

  398.       return value_from_decfloat (type, dec);
  399.     }
  400.   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM
  401.             || code1 == TYPE_CODE_RANGE)
  402.            && (scalar || code2 == TYPE_CODE_PTR
  403.                || code2 == TYPE_CODE_MEMBERPTR))
  404.     {
  405.       LONGEST longest;

  406.       /* When we cast pointers to integers, we mustn't use
  407.          gdbarch_pointer_to_address to find the address the pointer
  408.          represents, as value_as_long would.  GDB should evaluate
  409.          expressions just as the compiler would --- and the compiler
  410.          sees a cast as a simple reinterpretation of the pointer's
  411.          bits.  */
  412.       if (code2 == TYPE_CODE_PTR)
  413.         longest = extract_unsigned_integer
  414.                     (value_contents (arg2), TYPE_LENGTH (type2),
  415.                      gdbarch_byte_order (get_type_arch (type2)));
  416.       else
  417.         longest = value_as_long (arg2);
  418.       return value_from_longest (type, convert_to_boolean ?
  419.                                  (LONGEST) (longest ? 1 : 0) : longest);
  420.     }
  421.   else if (code1 == TYPE_CODE_PTR && (code2 == TYPE_CODE_INT
  422.                                       || code2 == TYPE_CODE_ENUM
  423.                                       || code2 == TYPE_CODE_RANGE))
  424.     {
  425.       /* TYPE_LENGTH (type) is the length of a pointer, but we really
  426.          want the length of an address! -- we are really dealing with
  427.          addresses (i.e., gdb representations) not pointers (i.e.,
  428.          target representations) here.

  429.          This allows things like "print *(int *)0x01000234" to work
  430.          without printing a misleading message -- which would
  431.          otherwise occur when dealing with a target having two byte
  432.          pointers and four byte addresses.  */

  433.       int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
  434.       LONGEST longest = value_as_long (arg2);

  435.       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
  436.         {
  437.           if (longest >= ((LONGEST) 1 << addr_bit)
  438.               || longest <= -((LONGEST) 1 << addr_bit))
  439.             warning (_("value truncated"));
  440.         }
  441.       return value_from_longest (type, longest);
  442.     }
  443.   else if (code1 == TYPE_CODE_METHODPTR && code2 == TYPE_CODE_INT
  444.            && value_as_long (arg2) == 0)
  445.     {
  446.       struct value *result = allocate_value (type);

  447.       cplus_make_method_ptr (type, value_contents_writeable (result), 0, 0);
  448.       return result;
  449.     }
  450.   else if (code1 == TYPE_CODE_MEMBERPTR && code2 == TYPE_CODE_INT
  451.            && value_as_long (arg2) == 0)
  452.     {
  453.       /* The Itanium C++ ABI represents NULL pointers to members as
  454.          minus one, instead of biasing the normal case.  */
  455.       return value_from_longest (type, -1);
  456.     }
  457.   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type)
  458.            && code2 == TYPE_CODE_ARRAY && TYPE_VECTOR (type2)
  459.            && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
  460.     error (_("Cannot convert between vector values of different sizes"));
  461.   else if (code1 == TYPE_CODE_ARRAY && TYPE_VECTOR (type) && scalar
  462.            && TYPE_LENGTH (type) != TYPE_LENGTH (type2))
  463.     error (_("can only cast scalar to vector of same size"));
  464.   else if (code1 == TYPE_CODE_VOID)
  465.     {
  466.       return value_zero (type, not_lval);
  467.     }
  468.   else if (TYPE_LENGTH (type) == TYPE_LENGTH (type2))
  469.     {
  470.       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  471.         return value_cast_pointers (type, arg2, 0);

  472.       arg2 = value_copy (arg2);
  473.       deprecated_set_value_type (arg2, type);
  474.       set_value_enclosing_type (arg2, type);
  475.       set_value_pointed_to_offset (arg2, 0);        /* pai: chk_val */
  476.       return arg2;
  477.     }
  478.   else if (VALUE_LVAL (arg2) == lval_memory)
  479.     return value_at_lazy (type, value_address (arg2));
  480.   else
  481.     {
  482.       error (_("Invalid cast."));
  483.       return 0;
  484.     }
  485. }

  486. /* The C++ reinterpret_cast operator.  */

  487. struct value *
  488. value_reinterpret_cast (struct type *type, struct value *arg)
  489. {
  490.   struct value *result;
  491.   struct type *real_type = check_typedef (type);
  492.   struct type *arg_type, *dest_type;
  493.   int is_ref = 0;
  494.   enum type_code dest_code, arg_code;

  495.   /* Do reference, function, and array conversion.  */
  496.   arg = coerce_array (arg);

  497.   /* Attempt to preserve the type the user asked for.  */
  498.   dest_type = type;

  499.   /* If we are casting to a reference type, transform
  500.      reinterpret_cast<T&>(V) to *reinterpret_cast<T*>(&V).  */
  501.   if (TYPE_CODE (real_type) == TYPE_CODE_REF)
  502.     {
  503.       is_ref = 1;
  504.       arg = value_addr (arg);
  505.       dest_type = lookup_pointer_type (TYPE_TARGET_TYPE (dest_type));
  506.       real_type = lookup_pointer_type (real_type);
  507.     }

  508.   arg_type = value_type (arg);

  509.   dest_code = TYPE_CODE (real_type);
  510.   arg_code = TYPE_CODE (arg_type);

  511.   /* We can convert pointer types, or any pointer type to int, or int
  512.      type to pointer.  */
  513.   if ((dest_code == TYPE_CODE_PTR && arg_code == TYPE_CODE_INT)
  514.       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_PTR)
  515.       || (dest_code == TYPE_CODE_METHODPTR && arg_code == TYPE_CODE_INT)
  516.       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_METHODPTR)
  517.       || (dest_code == TYPE_CODE_MEMBERPTR && arg_code == TYPE_CODE_INT)
  518.       || (dest_code == TYPE_CODE_INT && arg_code == TYPE_CODE_MEMBERPTR)
  519.       || (dest_code == arg_code
  520.           && (dest_code == TYPE_CODE_PTR
  521.               || dest_code == TYPE_CODE_METHODPTR
  522.               || dest_code == TYPE_CODE_MEMBERPTR)))
  523.     result = value_cast (dest_type, arg);
  524.   else
  525.     error (_("Invalid reinterpret_cast"));

  526.   if (is_ref)
  527.     result = value_cast (type, value_ref (value_ind (result)));

  528.   return result;
  529. }

  530. /* A helper for value_dynamic_cast.  This implements the first of two
  531.    runtime checks: we iterate over all the base classes of the value's
  532.    class which are equal to the desired class; if only one of these
  533.    holds the value, then it is the answer.  */

  534. static int
  535. dynamic_cast_check_1 (struct type *desired_type,
  536.                       const gdb_byte *valaddr,
  537.                       int embedded_offset,
  538.                       CORE_ADDR address,
  539.                       struct value *val,
  540.                       struct type *search_type,
  541.                       CORE_ADDR arg_addr,
  542.                       struct type *arg_type,
  543.                       struct value **result)
  544. {
  545.   int i, result_count = 0;

  546.   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
  547.     {
  548.       int offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
  549.                                      address, val);

  550.       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
  551.         {
  552.           if (address + embedded_offset + offset >= arg_addr
  553.               && address + embedded_offset + offset < arg_addr + TYPE_LENGTH (arg_type))
  554.             {
  555.               ++result_count;
  556.               if (!*result)
  557.                 *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
  558.                                          address + embedded_offset + offset);
  559.             }
  560.         }
  561.       else
  562.         result_count += dynamic_cast_check_1 (desired_type,
  563.                                               valaddr,
  564.                                               embedded_offset + offset,
  565.                                               address, val,
  566.                                               TYPE_BASECLASS (search_type, i),
  567.                                               arg_addr,
  568.                                               arg_type,
  569.                                               result);
  570.     }

  571.   return result_count;
  572. }

  573. /* A helper for value_dynamic_cast.  This implements the second of two
  574.    runtime checks: we look for a unique public sibling class of the
  575.    argument's declared class.  */

  576. static int
  577. dynamic_cast_check_2 (struct type *desired_type,
  578.                       const gdb_byte *valaddr,
  579.                       int embedded_offset,
  580.                       CORE_ADDR address,
  581.                       struct value *val,
  582.                       struct type *search_type,
  583.                       struct value **result)
  584. {
  585.   int i, result_count = 0;

  586.   for (i = 0; i < TYPE_N_BASECLASSES (search_type) && result_count < 2; ++i)
  587.     {
  588.       int offset;

  589.       if (! BASETYPE_VIA_PUBLIC (search_type, i))
  590.         continue;

  591.       offset = baseclass_offset (search_type, i, valaddr, embedded_offset,
  592.                                  address, val);
  593.       if (class_types_same_p (desired_type, TYPE_BASECLASS (search_type, i)))
  594.         {
  595.           ++result_count;
  596.           if (*result == NULL)
  597.             *result = value_at_lazy (TYPE_BASECLASS (search_type, i),
  598.                                      address + embedded_offset + offset);
  599.         }
  600.       else
  601.         result_count += dynamic_cast_check_2 (desired_type,
  602.                                               valaddr,
  603.                                               embedded_offset + offset,
  604.                                               address, val,
  605.                                               TYPE_BASECLASS (search_type, i),
  606.                                               result);
  607.     }

  608.   return result_count;
  609. }

  610. /* The C++ dynamic_cast operator.  */

  611. struct value *
  612. value_dynamic_cast (struct type *type, struct value *arg)
  613. {
  614.   int full, top, using_enc;
  615.   struct type *resolved_type = check_typedef (type);
  616.   struct type *arg_type = check_typedef (value_type (arg));
  617.   struct type *class_type, *rtti_type;
  618.   struct value *result, *tem, *original_arg = arg;
  619.   CORE_ADDR addr;
  620.   int is_ref = TYPE_CODE (resolved_type) == TYPE_CODE_REF;

  621.   if (TYPE_CODE (resolved_type) != TYPE_CODE_PTR
  622.       && TYPE_CODE (resolved_type) != TYPE_CODE_REF)
  623.     error (_("Argument to dynamic_cast must be a pointer or reference type"));
  624.   if (TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_VOID
  625.       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) != TYPE_CODE_STRUCT)
  626.     error (_("Argument to dynamic_cast must be pointer to class or `void *'"));

  627.   class_type = check_typedef (TYPE_TARGET_TYPE (resolved_type));
  628.   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
  629.     {
  630.       if (TYPE_CODE (arg_type) != TYPE_CODE_PTR
  631.           && ! (TYPE_CODE (arg_type) == TYPE_CODE_INT
  632.                 && value_as_long (arg) == 0))
  633.         error (_("Argument to dynamic_cast does not have pointer type"));
  634.       if (TYPE_CODE (arg_type) == TYPE_CODE_PTR)
  635.         {
  636.           arg_type = check_typedef (TYPE_TARGET_TYPE (arg_type));
  637.           if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
  638.             error (_("Argument to dynamic_cast does "
  639.                      "not have pointer to class type"));
  640.         }

  641.       /* Handle NULL pointers.  */
  642.       if (value_as_long (arg) == 0)
  643.         return value_zero (type, not_lval);

  644.       arg = value_ind (arg);
  645.     }
  646.   else
  647.     {
  648.       if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT)
  649.         error (_("Argument to dynamic_cast does not have class type"));
  650.     }

  651.   /* If the classes are the same, just return the argument.  */
  652.   if (class_types_same_p (class_type, arg_type))
  653.     return value_cast (type, arg);

  654.   /* If the target type is a unique base class of the argument's
  655.      declared type, just cast it.  */
  656.   if (is_ancestor (class_type, arg_type))
  657.     {
  658.       if (is_unique_ancestor (class_type, arg))
  659.         return value_cast (type, original_arg);
  660.       error (_("Ambiguous dynamic_cast"));
  661.     }

  662.   rtti_type = value_rtti_type (arg, &full, &top, &using_enc);
  663.   if (! rtti_type)
  664.     error (_("Couldn't determine value's most derived type for dynamic_cast"));

  665.   /* Compute the most derived object's address.  */
  666.   addr = value_address (arg);
  667.   if (full)
  668.     {
  669.       /* Done.  */
  670.     }
  671.   else if (using_enc)
  672.     addr += top;
  673.   else
  674.     addr += top + value_embedded_offset (arg);

  675.   /* dynamic_cast<void *> means to return a pointer to the
  676.      most-derived object.  */
  677.   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR
  678.       && TYPE_CODE (TYPE_TARGET_TYPE (resolved_type)) == TYPE_CODE_VOID)
  679.     return value_at_lazy (type, addr);

  680.   tem = value_at (type, addr);
  681.   type = value_type (tem);

  682.   /* The first dynamic check specified in 5.2.7.  */
  683.   if (is_public_ancestor (arg_type, TYPE_TARGET_TYPE (resolved_type)))
  684.     {
  685.       if (class_types_same_p (rtti_type, TYPE_TARGET_TYPE (resolved_type)))
  686.         return tem;
  687.       result = NULL;
  688.       if (dynamic_cast_check_1 (TYPE_TARGET_TYPE (resolved_type),
  689.                                 value_contents_for_printing (tem),
  690.                                 value_embedded_offset (tem),
  691.                                 value_address (tem), tem,
  692.                                 rtti_type, addr,
  693.                                 arg_type,
  694.                                 &result) == 1)
  695.         return value_cast (type,
  696.                            is_ref ? value_ref (result) : value_addr (result));
  697.     }

  698.   /* The second dynamic check specified in 5.2.7.  */
  699.   result = NULL;
  700.   if (is_public_ancestor (arg_type, rtti_type)
  701.       && dynamic_cast_check_2 (TYPE_TARGET_TYPE (resolved_type),
  702.                                value_contents_for_printing (tem),
  703.                                value_embedded_offset (tem),
  704.                                value_address (tem), tem,
  705.                                rtti_type, &result) == 1)
  706.     return value_cast (type,
  707.                        is_ref ? value_ref (result) : value_addr (result));

  708.   if (TYPE_CODE (resolved_type) == TYPE_CODE_PTR)
  709.     return value_zero (type, not_lval);

  710.   error (_("dynamic_cast failed"));
  711. }

  712. /* Create a value of type TYPE that is zero, and return it.  */

  713. struct value *
  714. value_zero (struct type *type, enum lval_type lv)
  715. {
  716.   struct value *val = allocate_value (type);

  717.   VALUE_LVAL (val) = (lv == lval_computed ? not_lval : lv);
  718.   return val;
  719. }

  720. /* Create a not_lval value of numeric type TYPE that is one, and return it.  */

  721. struct value *
  722. value_one (struct type *type)
  723. {
  724.   struct type *type1 = check_typedef (type);
  725.   struct value *val;

  726.   if (TYPE_CODE (type1) == TYPE_CODE_DECFLOAT)
  727.     {
  728.       enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (type));
  729.       gdb_byte v[16];

  730.       decimal_from_string (v, TYPE_LENGTH (type), byte_order, "1");
  731.       val = value_from_decfloat (type, v);
  732.     }
  733.   else if (TYPE_CODE (type1) == TYPE_CODE_FLT)
  734.     {
  735.       val = value_from_double (type, (DOUBLEST) 1);
  736.     }
  737.   else if (is_integral_type (type1))
  738.     {
  739.       val = value_from_longest (type, (LONGEST) 1);
  740.     }
  741.   else if (TYPE_CODE (type1) == TYPE_CODE_ARRAY && TYPE_VECTOR (type1))
  742.     {
  743.       struct type *eltype = check_typedef (TYPE_TARGET_TYPE (type1));
  744.       int i;
  745.       LONGEST low_bound, high_bound;
  746.       struct value *tmp;

  747.       if (!get_array_bounds (type1, &low_bound, &high_bound))
  748.         error (_("Could not determine the vector bounds"));

  749.       val = allocate_value (type);
  750.       for (i = 0; i < high_bound - low_bound + 1; i++)
  751.         {
  752.           tmp = value_one (eltype);
  753.           memcpy (value_contents_writeable (val) + i * TYPE_LENGTH (eltype),
  754.                   value_contents_all (tmp), TYPE_LENGTH (eltype));
  755.         }
  756.     }
  757.   else
  758.     {
  759.       error (_("Not a numeric type."));
  760.     }

  761.   /* value_one result is never used for assignments to.  */
  762.   gdb_assert (VALUE_LVAL (val) == not_lval);

  763.   return val;
  764. }

  765. /* Helper function for value_at, value_at_lazy, and value_at_lazy_stack.
  766.    The type of the created value may differ from the passed type TYPE.
  767.    Make sure to retrieve the returned values's new type after this call
  768.    e.g. in case the type is a variable length array.  */

  769. static struct value *
  770. get_value_at (struct type *type, CORE_ADDR addr, int lazy)
  771. {
  772.   struct value *val;

  773.   if (TYPE_CODE (check_typedef (type)) == TYPE_CODE_VOID)
  774.     error (_("Attempt to dereference a generic pointer."));

  775.   val = value_from_contents_and_address (type, NULL, addr);

  776.   if (!lazy)
  777.     value_fetch_lazy (val);

  778.   return val;
  779. }

  780. /* Return a value with type TYPE located at ADDR.

  781.    Call value_at only if the data needs to be fetched immediately;
  782.    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
  783.    value_at_lazy instead.  value_at_lazy simply records the address of
  784.    the data and sets the lazy-evaluation-required flag.  The lazy flag
  785.    is tested in the value_contents macro, which is used if and when
  786.    the contents are actually required.  The type of the created value
  787.    may differ from the passed type TYPE.  Make sure to retrieve the
  788.    returned values's new type after this call e.g. in case the type
  789.    is a variable length array.

  790.    Note: value_at does *NOT* handle embedded offsets; perform such
  791.    adjustments before or after calling it.  */

  792. struct value *
  793. value_at (struct type *type, CORE_ADDR addr)
  794. {
  795.   return get_value_at (type, addr, 0);
  796. }

  797. /* Return a lazy value with type TYPE located at ADDR (cf. value_at).
  798.    The type of the created value may differ from the passed type TYPE.
  799.    Make sure to retrieve the returned values's new type after this call
  800.    e.g. in case the type is a variable length array.  */

  801. struct value *
  802. value_at_lazy (struct type *type, CORE_ADDR addr)
  803. {
  804.   return get_value_at (type, addr, 1);
  805. }

  806. void
  807. read_value_memory (struct value *val, int embedded_offset,
  808.                    int stack, CORE_ADDR memaddr,
  809.                    gdb_byte *buffer, size_t length)
  810. {
  811.   ULONGEST xfered = 0;

  812.   while (xfered < length)
  813.     {
  814.       enum target_xfer_status status;
  815.       ULONGEST xfered_len;

  816.       status = target_xfer_partial (current_target.beneath,
  817.                                     TARGET_OBJECT_MEMORY, NULL,
  818.                                     buffer + xfered, NULL,
  819.                                     memaddr + xfered, length - xfered,
  820.                                     &xfered_len);

  821.       if (status == TARGET_XFER_OK)
  822.         /* nothing */;
  823.       else if (status == TARGET_XFER_UNAVAILABLE)
  824.         mark_value_bytes_unavailable (val, embedded_offset + xfered,
  825.                                       xfered_len);
  826.       else if (status == TARGET_XFER_EOF)
  827.         memory_error (TARGET_XFER_E_IO, memaddr + xfered);
  828.       else
  829.         memory_error (status, memaddr + xfered);

  830.       xfered += xfered_len;
  831.       QUIT;
  832.     }
  833. }

  834. /* Store the contents of FROMVAL into the location of TOVAL.
  835.    Return a new value with the location of TOVAL and contents of FROMVAL.  */

  836. struct value *
  837. value_assign (struct value *toval, struct value *fromval)
  838. {
  839.   struct type *type;
  840.   struct value *val;
  841.   struct frame_id old_frame;

  842.   if (!deprecated_value_modifiable (toval))
  843.     error (_("Left operand of assignment is not a modifiable lvalue."));

  844.   toval = coerce_ref (toval);

  845.   type = value_type (toval);
  846.   if (VALUE_LVAL (toval) != lval_internalvar)
  847.     fromval = value_cast (type, fromval);
  848.   else
  849.     {
  850.       /* Coerce arrays and functions to pointers, except for arrays
  851.          which only live in GDB's storage.  */
  852.       if (!value_must_coerce_to_target (fromval))
  853.         fromval = coerce_array (fromval);
  854.     }

  855.   CHECK_TYPEDEF (type);

  856.   /* Since modifying a register can trash the frame chain, and
  857.      modifying memory can trash the frame cache, we save the old frame
  858.      and then restore the new frame afterwards.  */
  859.   old_frame = get_frame_id (deprecated_safe_get_selected_frame ());

  860.   switch (VALUE_LVAL (toval))
  861.     {
  862.     case lval_internalvar:
  863.       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
  864.       return value_of_internalvar (get_type_arch (type),
  865.                                    VALUE_INTERNALVAR (toval));

  866.     case lval_internalvar_component:
  867.       {
  868.         int offset = value_offset (toval);

  869.         /* Are we dealing with a bitfield?

  870.            It is important to mention that `value_parent (toval)' is
  871.            non-NULL iff `value_bitsize (toval)' is non-zero.  */
  872.         if (value_bitsize (toval))
  873.           {
  874.             /* VALUE_INTERNALVAR below refers to the parent value, while
  875.                the offset is relative to this parent value.  */
  876.             gdb_assert (value_parent (value_parent (toval)) == NULL);
  877.             offset += value_offset (value_parent (toval));
  878.           }

  879.         set_internalvar_component (VALUE_INTERNALVAR (toval),
  880.                                    offset,
  881.                                    value_bitpos (toval),
  882.                                    value_bitsize (toval),
  883.                                    fromval);
  884.       }
  885.       break;

  886.     case lval_memory:
  887.       {
  888.         const gdb_byte *dest_buffer;
  889.         CORE_ADDR changed_addr;
  890.         int changed_len;
  891.         gdb_byte buffer[sizeof (LONGEST)];

  892.         if (value_bitsize (toval))
  893.           {
  894.             struct value *parent = value_parent (toval);

  895.             changed_addr = value_address (parent) + value_offset (toval);
  896.             changed_len = (value_bitpos (toval)
  897.                            + value_bitsize (toval)
  898.                            + HOST_CHAR_BIT - 1)
  899.               / HOST_CHAR_BIT;

  900.             /* If we can read-modify-write exactly the size of the
  901.                containing type (e.g. short or int) then do so.  This
  902.                is safer for volatile bitfields mapped to hardware
  903.                registers.  */
  904.             if (changed_len < TYPE_LENGTH (type)
  905.                 && TYPE_LENGTH (type) <= (int) sizeof (LONGEST)
  906.                 && ((LONGEST) changed_addr % TYPE_LENGTH (type)) == 0)
  907.               changed_len = TYPE_LENGTH (type);

  908.             if (changed_len > (int) sizeof (LONGEST))
  909.               error (_("Can't handle bitfields which "
  910.                        "don't fit in a %d bit word."),
  911.                      (int) sizeof (LONGEST) * HOST_CHAR_BIT);

  912.             read_memory (changed_addr, buffer, changed_len);
  913.             modify_field (type, buffer, value_as_long (fromval),
  914.                           value_bitpos (toval), value_bitsize (toval));
  915.             dest_buffer = buffer;
  916.           }
  917.         else
  918.           {
  919.             changed_addr = value_address (toval);
  920.             changed_len = TYPE_LENGTH (type);
  921.             dest_buffer = value_contents (fromval);
  922.           }

  923.         write_memory_with_notification (changed_addr, dest_buffer, changed_len);
  924.       }
  925.       break;

  926.     case lval_register:
  927.       {
  928.         struct frame_info *frame;
  929.         struct gdbarch *gdbarch;
  930.         int value_reg;

  931.         /* Figure out which frame this is in currently.  */
  932.         frame = frame_find_by_id (VALUE_FRAME_ID (toval));
  933.         value_reg = VALUE_REGNUM (toval);

  934.         if (!frame)
  935.           error (_("Value being assigned to is no longer active."));

  936.         gdbarch = get_frame_arch (frame);

  937.         if (value_bitsize (toval))
  938.           {
  939.             struct value *parent = value_parent (toval);
  940.             int offset = value_offset (parent) + value_offset (toval);
  941.             int changed_len;
  942.             gdb_byte buffer[sizeof (LONGEST)];
  943.             int optim, unavail;

  944.             changed_len = (value_bitpos (toval)
  945.                            + value_bitsize (toval)
  946.                            + HOST_CHAR_BIT - 1)
  947.                           / HOST_CHAR_BIT;

  948.             if (changed_len > (int) sizeof (LONGEST))
  949.               error (_("Can't handle bitfields which "
  950.                        "don't fit in a %d bit word."),
  951.                      (int) sizeof (LONGEST) * HOST_CHAR_BIT);

  952.             if (!get_frame_register_bytes (frame, value_reg, offset,
  953.                                            changed_len, buffer,
  954.                                            &optim, &unavail))
  955.               {
  956.                 if (optim)
  957.                   throw_error (OPTIMIZED_OUT_ERROR,
  958.                                _("value has been optimized out"));
  959.                 if (unavail)
  960.                   throw_error (NOT_AVAILABLE_ERROR,
  961.                                _("value is not available"));
  962.               }

  963.             modify_field (type, buffer, value_as_long (fromval),
  964.                           value_bitpos (toval), value_bitsize (toval));

  965.             put_frame_register_bytes (frame, value_reg, offset,
  966.                                       changed_len, buffer);
  967.           }
  968.         else
  969.           {
  970.             if (gdbarch_convert_register_p (gdbarch, VALUE_REGNUM (toval),
  971.                                             type))
  972.               {
  973.                 /* If TOVAL is a special machine register requiring
  974.                    conversion of program values to a special raw
  975.                    format.  */
  976.                 gdbarch_value_to_register (gdbarch, frame,
  977.                                            VALUE_REGNUM (toval), type,
  978.                                            value_contents (fromval));
  979.               }
  980.             else
  981.               {
  982.                 put_frame_register_bytes (frame, value_reg,
  983.                                           value_offset (toval),
  984.                                           TYPE_LENGTH (type),
  985.                                           value_contents (fromval));
  986.               }
  987.           }

  988.         observer_notify_register_changed (frame, value_reg);
  989.         if (deprecated_register_changed_hook)
  990.           deprecated_register_changed_hook (-1);
  991.         break;
  992.       }

  993.     case lval_computed:
  994.       {
  995.         const struct lval_funcs *funcs = value_computed_funcs (toval);

  996.         if (funcs->write != NULL)
  997.           {
  998.             funcs->write (toval, fromval);
  999.             break;
  1000.           }
  1001.       }
  1002.       /* Fall through.  */

  1003.     default:
  1004.       error (_("Left operand of assignment is not an lvalue."));
  1005.     }

  1006.   /* Assigning to the stack pointer, frame pointer, and other
  1007.      (architecture and calling convention specific) registers may
  1008.      cause the frame cache and regcache to be out of date.  Assigning to memory
  1009.      also can.  We just do this on all assignments to registers or
  1010.      memory, for simplicity's sake; I doubt the slowdown matters.  */
  1011.   switch (VALUE_LVAL (toval))
  1012.     {
  1013.     case lval_memory:
  1014.     case lval_register:
  1015.     case lval_computed:

  1016.       observer_notify_target_changed (&current_target);

  1017.       /* Having destroyed the frame cache, restore the selected
  1018.          frame.  */

  1019.       /* FIXME: cagney/2002-11-02: There has to be a better way of
  1020.          doing this.  Instead of constantly saving/restoring the
  1021.          frame.  Why not create a get_selected_frame() function that,
  1022.          having saved the selected frame's ID can automatically
  1023.          re-find the previously selected frame automatically.  */

  1024.       {
  1025.         struct frame_info *fi = frame_find_by_id (old_frame);

  1026.         if (fi != NULL)
  1027.           select_frame (fi);
  1028.       }

  1029.       break;
  1030.     default:
  1031.       break;
  1032.     }

  1033.   /* If the field does not entirely fill a LONGEST, then zero the sign
  1034.      bits.  If the field is signed, and is negative, then sign
  1035.      extend.  */
  1036.   if ((value_bitsize (toval) > 0)
  1037.       && (value_bitsize (toval) < 8 * (int) sizeof (LONGEST)))
  1038.     {
  1039.       LONGEST fieldval = value_as_long (fromval);
  1040.       LONGEST valmask = (((ULONGEST) 1) << value_bitsize (toval)) - 1;

  1041.       fieldval &= valmask;
  1042.       if (!TYPE_UNSIGNED (type)
  1043.           && (fieldval & (valmask ^ (valmask >> 1))))
  1044.         fieldval |= ~valmask;

  1045.       fromval = value_from_longest (type, fieldval);
  1046.     }

  1047.   /* The return value is a copy of TOVAL so it shares its location
  1048.      information, but its contents are updated from FROMVAL.  This
  1049.      implies the returned value is not lazy, even if TOVAL was.  */
  1050.   val = value_copy (toval);
  1051.   set_value_lazy (val, 0);
  1052.   memcpy (value_contents_raw (val), value_contents (fromval),
  1053.           TYPE_LENGTH (type));

  1054.   /* We copy over the enclosing type and pointed-to offset from FROMVAL
  1055.      in the case of pointer types.  For object types, the enclosing type
  1056.      and embedded offset must *not* be copied: the target object refered
  1057.      to by TOVAL retains its original dynamic type after assignment.  */
  1058.   if (TYPE_CODE (type) == TYPE_CODE_PTR)
  1059.     {
  1060.       set_value_enclosing_type (val, value_enclosing_type (fromval));
  1061.       set_value_pointed_to_offset (val, value_pointed_to_offset (fromval));
  1062.     }

  1063.   return val;
  1064. }

  1065. /* Extend a value VAL to COUNT repetitions of its type.  */

  1066. struct value *
  1067. value_repeat (struct value *arg1, int count)
  1068. {
  1069.   struct value *val;

  1070.   if (VALUE_LVAL (arg1) != lval_memory)
  1071.     error (_("Only values in memory can be extended with '@'."));
  1072.   if (count < 1)
  1073.     error (_("Invalid number %d of repetitions."), count);

  1074.   val = allocate_repeat_value (value_enclosing_type (arg1), count);

  1075.   VALUE_LVAL (val) = lval_memory;
  1076.   set_value_address (val, value_address (arg1));

  1077.   read_value_memory (val, 0, value_stack (val), value_address (val),
  1078.                      value_contents_all_raw (val),
  1079.                      TYPE_LENGTH (value_enclosing_type (val)));

  1080.   return val;
  1081. }

  1082. struct value *
  1083. value_of_variable (struct symbol *var, const struct block *b)
  1084. {
  1085.   struct frame_info *frame;

  1086.   if (!symbol_read_needs_frame (var))
  1087.     frame = NULL;
  1088.   else if (!b)
  1089.     frame = get_selected_frame (_("No frame selected."));
  1090.   else
  1091.     {
  1092.       frame = block_innermost_frame (b);
  1093.       if (!frame)
  1094.         {
  1095.           if (BLOCK_FUNCTION (b) && !block_inlined_p (b)
  1096.               && SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)))
  1097.             error (_("No frame is currently executing in block %s."),
  1098.                    SYMBOL_PRINT_NAME (BLOCK_FUNCTION (b)));
  1099.           else
  1100.             error (_("No frame is currently executing in specified block"));
  1101.         }
  1102.     }

  1103.   return read_var_value (var, frame);
  1104. }

  1105. struct value *
  1106. address_of_variable (struct symbol *var, const struct block *b)
  1107. {
  1108.   struct type *type = SYMBOL_TYPE (var);
  1109.   struct value *val;

  1110.   /* Evaluate it first; if the result is a memory address, we're fine.
  1111.      Lazy evaluation pays off here.  */

  1112.   val = value_of_variable (var, b);
  1113.   type = value_type (val);

  1114.   if ((VALUE_LVAL (val) == lval_memory && value_lazy (val))
  1115.       || TYPE_CODE (type) == TYPE_CODE_FUNC)
  1116.     {
  1117.       CORE_ADDR addr = value_address (val);

  1118.       return value_from_pointer (lookup_pointer_type (type), addr);
  1119.     }

  1120.   /* Not a memory address; check what the problem was.  */
  1121.   switch (VALUE_LVAL (val))
  1122.     {
  1123.     case lval_register:
  1124.       {
  1125.         struct frame_info *frame;
  1126.         const char *regname;

  1127.         frame = frame_find_by_id (VALUE_FRAME_ID (val));
  1128.         gdb_assert (frame);

  1129.         regname = gdbarch_register_name (get_frame_arch (frame),
  1130.                                          VALUE_REGNUM (val));
  1131.         gdb_assert (regname && *regname);

  1132.         error (_("Address requested for identifier "
  1133.                  "\"%s\" which is in register $%s"),
  1134.                SYMBOL_PRINT_NAME (var), regname);
  1135.         break;
  1136.       }

  1137.     default:
  1138.       error (_("Can't take address of \"%s\" which isn't an lvalue."),
  1139.              SYMBOL_PRINT_NAME (var));
  1140.       break;
  1141.     }

  1142.   return val;
  1143. }

  1144. /* Return one if VAL does not live in target memory, but should in order
  1145.    to operate on it.  Otherwise return zero.  */

  1146. int
  1147. value_must_coerce_to_target (struct value *val)
  1148. {
  1149.   struct type *valtype;

  1150.   /* The only lval kinds which do not live in target memory.  */
  1151.   if (VALUE_LVAL (val) != not_lval
  1152.       && VALUE_LVAL (val) != lval_internalvar
  1153.       && VALUE_LVAL (val) != lval_xcallable)
  1154.     return 0;

  1155.   valtype = check_typedef (value_type (val));

  1156.   switch (TYPE_CODE (valtype))
  1157.     {
  1158.     case TYPE_CODE_ARRAY:
  1159.       return TYPE_VECTOR (valtype) ? 0 : 1;
  1160.     case TYPE_CODE_STRING:
  1161.       return 1;
  1162.     default:
  1163.       return 0;
  1164.     }
  1165. }

  1166. /* Make sure that VAL lives in target memory if it's supposed to.  For
  1167.    instance, strings are constructed as character arrays in GDB's
  1168.    storage, and this function copies them to the target.  */

  1169. struct value *
  1170. value_coerce_to_target (struct value *val)
  1171. {
  1172.   LONGEST length;
  1173.   CORE_ADDR addr;

  1174.   if (!value_must_coerce_to_target (val))
  1175.     return val;

  1176.   length = TYPE_LENGTH (check_typedef (value_type (val)));
  1177.   addr = allocate_space_in_inferior (length);
  1178.   write_memory (addr, value_contents (val), length);
  1179.   return value_at_lazy (value_type (val), addr);
  1180. }

  1181. /* Given a value which is an array, return a value which is a pointer
  1182.    to its first element, regardless of whether or not the array has a
  1183.    nonzero lower bound.

  1184.    FIXME: A previous comment here indicated that this routine should
  1185.    be substracting the array's lower bound.  It's not clear to me that
  1186.    this is correct.  Given an array subscripting operation, it would
  1187.    certainly work to do the adjustment here, essentially computing:

  1188.    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])

  1189.    However I believe a more appropriate and logical place to account
  1190.    for the lower bound is to do so in value_subscript, essentially
  1191.    computing:

  1192.    (&array[0] + ((index - lowerbound) * sizeof array[0]))

  1193.    As further evidence consider what would happen with operations
  1194.    other than array subscripting, where the caller would get back a
  1195.    value that had an address somewhere before the actual first element
  1196.    of the array, and the information about the lower bound would be
  1197.    lost because of the coercion to pointer type.  */

  1198. struct value *
  1199. value_coerce_array (struct value *arg1)
  1200. {
  1201.   struct type *type = check_typedef (value_type (arg1));

  1202.   /* If the user tries to do something requiring a pointer with an
  1203.      array that has not yet been pushed to the target, then this would
  1204.      be a good time to do so.  */
  1205.   arg1 = value_coerce_to_target (arg1);

  1206.   if (VALUE_LVAL (arg1) != lval_memory)
  1207.     error (_("Attempt to take address of value not located in memory."));

  1208.   return value_from_pointer (lookup_pointer_type (TYPE_TARGET_TYPE (type)),
  1209.                              value_address (arg1));
  1210. }

  1211. /* Given a value which is a function, return a value which is a pointer
  1212.    to it.  */

  1213. struct value *
  1214. value_coerce_function (struct value *arg1)
  1215. {
  1216.   struct value *retval;

  1217.   if (VALUE_LVAL (arg1) != lval_memory)
  1218.     error (_("Attempt to take address of value not located in memory."));

  1219.   retval = value_from_pointer (lookup_pointer_type (value_type (arg1)),
  1220.                                value_address (arg1));
  1221.   return retval;
  1222. }

  1223. /* Return a pointer value for the object for which ARG1 is the
  1224.    contents.  */

  1225. struct value *
  1226. value_addr (struct value *arg1)
  1227. {
  1228.   struct value *arg2;
  1229.   struct type *type = check_typedef (value_type (arg1));

  1230.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  1231.     {
  1232.       /* Copy the value, but change the type from (T&) to (T*).  We
  1233.          keep the same location information, which is efficient, and
  1234.          allows &(&X) to get the location containing the reference.  */
  1235.       arg2 = value_copy (arg1);
  1236.       deprecated_set_value_type (arg2,
  1237.                                  lookup_pointer_type (TYPE_TARGET_TYPE (type)));
  1238.       return arg2;
  1239.     }
  1240.   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
  1241.     return value_coerce_function (arg1);

  1242.   /* If this is an array that has not yet been pushed to the target,
  1243.      then this would be a good time to force it to memory.  */
  1244.   arg1 = value_coerce_to_target (arg1);

  1245.   if (VALUE_LVAL (arg1) != lval_memory)
  1246.     error (_("Attempt to take address of value not located in memory."));

  1247.   /* Get target memory address.  */
  1248.   arg2 = value_from_pointer (lookup_pointer_type (value_type (arg1)),
  1249.                              (value_address (arg1)
  1250.                               + value_embedded_offset (arg1)));

  1251.   /* This may be a pointer to a base subobject; so remember the
  1252.      full derived object's type ...  */
  1253.   set_value_enclosing_type (arg2,
  1254.                             lookup_pointer_type (value_enclosing_type (arg1)));
  1255.   /* ... and also the relative position of the subobject in the full
  1256.      object.  */
  1257.   set_value_pointed_to_offset (arg2, value_embedded_offset (arg1));
  1258.   return arg2;
  1259. }

  1260. /* Return a reference value for the object for which ARG1 is the
  1261.    contents.  */

  1262. struct value *
  1263. value_ref (struct value *arg1)
  1264. {
  1265.   struct value *arg2;
  1266.   struct type *type = check_typedef (value_type (arg1));

  1267.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  1268.     return arg1;

  1269.   arg2 = value_addr (arg1);
  1270.   deprecated_set_value_type (arg2, lookup_reference_type (type));
  1271.   return arg2;
  1272. }

  1273. /* Given a value of a pointer type, apply the C unary * operator to
  1274.    it.  */

  1275. struct value *
  1276. value_ind (struct value *arg1)
  1277. {
  1278.   struct type *base_type;
  1279.   struct value *arg2;

  1280.   arg1 = coerce_array (arg1);

  1281.   base_type = check_typedef (value_type (arg1));

  1282.   if (VALUE_LVAL (arg1) == lval_computed)
  1283.     {
  1284.       const struct lval_funcs *funcs = value_computed_funcs (arg1);

  1285.       if (funcs->indirect)
  1286.         {
  1287.           struct value *result = funcs->indirect (arg1);

  1288.           if (result)
  1289.             return result;
  1290.         }
  1291.     }

  1292.   if (TYPE_CODE (base_type) == TYPE_CODE_PTR)
  1293.     {
  1294.       struct type *enc_type;

  1295.       /* We may be pointing to something embedded in a larger object.
  1296.          Get the real type of the enclosing object.  */
  1297.       enc_type = check_typedef (value_enclosing_type (arg1));
  1298.       enc_type = TYPE_TARGET_TYPE (enc_type);

  1299.       if (TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_FUNC
  1300.           || TYPE_CODE (check_typedef (enc_type)) == TYPE_CODE_METHOD)
  1301.         /* For functions, go through find_function_addr, which knows
  1302.            how to handle function descriptors.  */
  1303.         arg2 = value_at_lazy (enc_type,
  1304.                               find_function_addr (arg1, NULL));
  1305.       else
  1306.         /* Retrieve the enclosing object pointed to.  */
  1307.         arg2 = value_at_lazy (enc_type,
  1308.                               (value_as_address (arg1)
  1309.                                - value_pointed_to_offset (arg1)));

  1310.       enc_type = value_type (arg2);
  1311.       return readjust_indirect_value_type (arg2, enc_type, base_type, arg1);
  1312.     }

  1313.   error (_("Attempt to take contents of a non-pointer value."));
  1314.   return 0;                        /* For lint -- never reached.  */
  1315. }

  1316. /* Create a value for an array by allocating space in GDB, copying the
  1317.    data into that space, and then setting up an array value.

  1318.    The array bounds are set from LOWBOUND and HIGHBOUND, and the array
  1319.    is populated from the values passed in ELEMVEC.

  1320.    The element type of the array is inherited from the type of the
  1321.    first element, and all elements must have the same size (though we
  1322.    don't currently enforce any restriction on their types).  */

  1323. struct value *
  1324. value_array (int lowbound, int highbound, struct value **elemvec)
  1325. {
  1326.   int nelem;
  1327.   int idx;
  1328.   unsigned int typelength;
  1329.   struct value *val;
  1330.   struct type *arraytype;

  1331.   /* Validate that the bounds are reasonable and that each of the
  1332.      elements have the same size.  */

  1333.   nelem = highbound - lowbound + 1;
  1334.   if (nelem <= 0)
  1335.     {
  1336.       error (_("bad array bounds (%d, %d)"), lowbound, highbound);
  1337.     }
  1338.   typelength = TYPE_LENGTH (value_enclosing_type (elemvec[0]));
  1339.   for (idx = 1; idx < nelem; idx++)
  1340.     {
  1341.       if (TYPE_LENGTH (value_enclosing_type (elemvec[idx])) != typelength)
  1342.         {
  1343.           error (_("array elements must all be the same size"));
  1344.         }
  1345.     }

  1346.   arraytype = lookup_array_range_type (value_enclosing_type (elemvec[0]),
  1347.                                        lowbound, highbound);

  1348.   if (!current_language->c_style_arrays)
  1349.     {
  1350.       val = allocate_value (arraytype);
  1351.       for (idx = 0; idx < nelem; idx++)
  1352.         value_contents_copy (val, idx * typelength, elemvec[idx], 0,
  1353.                              typelength);
  1354.       return val;
  1355.     }

  1356.   /* Allocate space to store the array, and then initialize it by
  1357.      copying in each element.  */

  1358.   val = allocate_value (arraytype);
  1359.   for (idx = 0; idx < nelem; idx++)
  1360.     value_contents_copy (val, idx * typelength, elemvec[idx], 0, typelength);
  1361.   return val;
  1362. }

  1363. struct value *
  1364. value_cstring (char *ptr, ssize_t len, struct type *char_type)
  1365. {
  1366.   struct value *val;
  1367.   int lowbound = current_language->string_lower_bound;
  1368.   ssize_t highbound = len / TYPE_LENGTH (char_type);
  1369.   struct type *stringtype
  1370.     = lookup_array_range_type (char_type, lowbound, highbound + lowbound - 1);

  1371.   val = allocate_value (stringtype);
  1372.   memcpy (value_contents_raw (val), ptr, len);
  1373.   return val;
  1374. }

  1375. /* Create a value for a string constant by allocating space in the
  1376.    inferior, copying the data into that space, and returning the
  1377.    address with type TYPE_CODE_STRING.  PTR points to the string
  1378.    constant data; LEN is number of characters.

  1379.    Note that string types are like array of char types with a lower
  1380.    bound of zero and an upper bound of LEN - 1.  Also note that the
  1381.    string may contain embedded null bytes.  */

  1382. struct value *
  1383. value_string (char *ptr, ssize_t len, struct type *char_type)
  1384. {
  1385.   struct value *val;
  1386.   int lowbound = current_language->string_lower_bound;
  1387.   ssize_t highbound = len / TYPE_LENGTH (char_type);
  1388.   struct type *stringtype
  1389.     = lookup_string_range_type (char_type, lowbound, highbound + lowbound - 1);

  1390.   val = allocate_value (stringtype);
  1391.   memcpy (value_contents_raw (val), ptr, len);
  1392.   return val;
  1393. }


  1394. /* See if we can pass arguments in T2 to a function which takes
  1395.    arguments of types T1.  T1 is a list of NARGS arguments, and T2 is
  1396.    a NULL-terminated vector.  If some arguments need coercion of some
  1397.    sort, then the coerced values are written into T2.  Return value is
  1398.    0 if the arguments could be matched, or the position at which they
  1399.    differ if not.

  1400.    STATICP is nonzero if the T1 argument list came from a static
  1401.    member functionT2 will still include the ``this'' pointer, but
  1402.    it will be skipped.

  1403.    For non-static member functions, we ignore the first argument,
  1404.    which is the type of the instance variable.  This is because we
  1405.    want to handle calls with objects from derived classes.  This is
  1406.    not entirely correct: we should actually check to make sure that a
  1407.    requested operation is type secure, shouldn't we?  FIXME.  */

  1408. static int
  1409. typecmp (int staticp, int varargs, int nargs,
  1410.          struct field t1[], struct value *t2[])
  1411. {
  1412.   int i;

  1413.   if (t2 == 0)
  1414.     internal_error (__FILE__, __LINE__,
  1415.                     _("typecmp: no argument list"));

  1416.   /* Skip ``this'' argument if applicable.  T2 will always include
  1417.      THIS.  */
  1418.   if (staticp)
  1419.     t2 ++;

  1420.   for (i = 0;
  1421.        (i < nargs) && TYPE_CODE (t1[i].type) != TYPE_CODE_VOID;
  1422.        i++)
  1423.     {
  1424.       struct type *tt1, *tt2;

  1425.       if (!t2[i])
  1426.         return i + 1;

  1427.       tt1 = check_typedef (t1[i].type);
  1428.       tt2 = check_typedef (value_type (t2[i]));

  1429.       if (TYPE_CODE (tt1) == TYPE_CODE_REF
  1430.           /* We should be doing hairy argument matching, as below.  */
  1431.           && (TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (tt1)))
  1432.               == TYPE_CODE (tt2)))
  1433.         {
  1434.           if (TYPE_CODE (tt2) == TYPE_CODE_ARRAY)
  1435.             t2[i] = value_coerce_array (t2[i]);
  1436.           else
  1437.             t2[i] = value_ref (t2[i]);
  1438.           continue;
  1439.         }

  1440.       /* djb - 20000715 - Until the new type structure is in the
  1441.          place, and we can attempt things like implicit conversions,
  1442.          we need to do this so you can take something like a map<const
  1443.          char *>, and properly access map["hello"], because the
  1444.          argument to [] will be a reference to a pointer to a char,
  1445.          and the argument will be a pointer to a char.  */
  1446.       while (TYPE_CODE(tt1) == TYPE_CODE_REF
  1447.              || TYPE_CODE (tt1) == TYPE_CODE_PTR)
  1448.         {
  1449.           tt1 = check_typedef( TYPE_TARGET_TYPE(tt1) );
  1450.         }
  1451.       while (TYPE_CODE(tt2) == TYPE_CODE_ARRAY
  1452.              || TYPE_CODE(tt2) == TYPE_CODE_PTR
  1453.              || TYPE_CODE(tt2) == TYPE_CODE_REF)
  1454.         {
  1455.           tt2 = check_typedef (TYPE_TARGET_TYPE(tt2));
  1456.         }
  1457.       if (TYPE_CODE (tt1) == TYPE_CODE (tt2))
  1458.         continue;
  1459.       /* Array to pointer is a `trivial conversion' according to the
  1460.          ARM.  */

  1461.       /* We should be doing much hairier argument matching (see
  1462.          section 13.2 of the ARM), but as a quick kludge, just check
  1463.          for the same type code.  */
  1464.       if (TYPE_CODE (t1[i].type) != TYPE_CODE (value_type (t2[i])))
  1465.         return i + 1;
  1466.     }
  1467.   if (varargs || t2[i] == NULL)
  1468.     return 0;
  1469.   return i + 1;
  1470. }

  1471. /* Helper class for do_search_struct_field that updates *RESULT_PTR
  1472.    and *LAST_BOFFSET, and possibly throws an exception if the field
  1473.    search has yielded ambiguous results.  */

  1474. static void
  1475. update_search_result (struct value **result_ptr, struct value *v,
  1476.                       int *last_boffset, int boffset,
  1477.                       const char *name, struct type *type)
  1478. {
  1479.   if (v != NULL)
  1480.     {
  1481.       if (*result_ptr != NULL
  1482.           /* The result is not ambiguous if all the classes that are
  1483.              found occupy the same space.  */
  1484.           && *last_boffset != boffset)
  1485.         error (_("base class '%s' is ambiguous in type '%s'"),
  1486.                name, TYPE_SAFE_NAME (type));
  1487.       *result_ptr = v;
  1488.       *last_boffset = boffset;
  1489.     }
  1490. }

  1491. /* A helper for search_struct_field.  This does all the work; most
  1492.    arguments are as passed to search_struct_field.  The result is
  1493.    stored in *RESULT_PTR, which must be initialized to NULL.
  1494.    OUTERMOST_TYPE is the type of the initial type passed to
  1495.    search_struct_field; this is used for error reporting when the
  1496.    lookup is ambiguous.  */

  1497. static void
  1498. do_search_struct_field (const char *name, struct value *arg1, int offset,
  1499.                         struct type *type, int looking_for_baseclass,
  1500.                         struct value **result_ptr,
  1501.                         int *last_boffset,
  1502.                         struct type *outermost_type)
  1503. {
  1504.   int i;
  1505.   int nbases;

  1506.   CHECK_TYPEDEF (type);
  1507.   nbases = TYPE_N_BASECLASSES (type);

  1508.   if (!looking_for_baseclass)
  1509.     for (i = TYPE_NFIELDS (type) - 1; i >= nbases; i--)
  1510.       {
  1511.         const char *t_field_name = TYPE_FIELD_NAME (type, i);

  1512.         if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1513.           {
  1514.             struct value *v;

  1515.             if (field_is_static (&TYPE_FIELD (type, i)))
  1516.               v = value_static_field (type, i);
  1517.             else
  1518.               v = value_primitive_field (arg1, offset, i, type);
  1519.             *result_ptr = v;
  1520.             return;
  1521.           }

  1522.         if (t_field_name
  1523.             && t_field_name[0] == '\0')
  1524.           {
  1525.             struct type *field_type = TYPE_FIELD_TYPE (type, i);

  1526.             if (TYPE_CODE (field_type) == TYPE_CODE_UNION
  1527.                 || TYPE_CODE (field_type) == TYPE_CODE_STRUCT)
  1528.               {
  1529.                 /* Look for a match through the fields of an anonymous
  1530.                    union, or anonymous struct.  C++ provides anonymous
  1531.                    unions.

  1532.                    In the GNU Chill (now deleted from GDB)
  1533.                    implementation of variant record types, each
  1534.                    <alternative field> has an (anonymous) union type,
  1535.                    each member of the union represents a <variant
  1536.                    alternative>.  Each <variant alternative> is
  1537.                    represented as a struct, with a member for each
  1538.                    <variant field>.  */

  1539.                 struct value *v = NULL;
  1540.                 int new_offset = offset;

  1541.                 /* This is pretty gross.  In G++, the offset in an
  1542.                    anonymous union is relative to the beginning of the
  1543.                    enclosing struct.  In the GNU Chill (now deleted
  1544.                    from GDB) implementation of variant records, the
  1545.                    bitpos is zero in an anonymous union field, so we
  1546.                    have to add the offset of the union here.  */
  1547.                 if (TYPE_CODE (field_type) == TYPE_CODE_STRUCT
  1548.                     || (TYPE_NFIELDS (field_type) > 0
  1549.                         && TYPE_FIELD_BITPOS (field_type, 0) == 0))
  1550.                   new_offset += TYPE_FIELD_BITPOS (type, i) / 8;

  1551.                 do_search_struct_field (name, arg1, new_offset,
  1552.                                         field_type,
  1553.                                         looking_for_baseclass, &v,
  1554.                                         last_boffset,
  1555.                                         outermost_type);
  1556.                 if (v)
  1557.                   {
  1558.                     *result_ptr = v;
  1559.                     return;
  1560.                   }
  1561.               }
  1562.           }
  1563.       }

  1564.   for (i = 0; i < nbases; i++)
  1565.     {
  1566.       struct value *v = NULL;
  1567.       struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
  1568.       /* If we are looking for baseclasses, this is what we get when
  1569.          we hit them.  But it could happen that the base part's member
  1570.          name is not yet filled in.  */
  1571.       int found_baseclass = (looking_for_baseclass
  1572.                              && TYPE_BASECLASS_NAME (type, i) != NULL
  1573.                              && (strcmp_iw (name,
  1574.                                             TYPE_BASECLASS_NAME (type,
  1575.                                                                  i)) == 0));
  1576.       int boffset = value_embedded_offset (arg1) + offset;

  1577.       if (BASETYPE_VIA_VIRTUAL (type, i))
  1578.         {
  1579.           struct value *v2;

  1580.           boffset = baseclass_offset (type, i,
  1581.                                       value_contents_for_printing (arg1),
  1582.                                       value_embedded_offset (arg1) + offset,
  1583.                                       value_address (arg1),
  1584.                                       arg1);

  1585.           /* The virtual base class pointer might have been clobbered
  1586.              by the user program.  Make sure that it still points to a
  1587.              valid memory location.  */

  1588.           boffset += value_embedded_offset (arg1) + offset;
  1589.           if (boffset < 0
  1590.               || boffset >= TYPE_LENGTH (value_enclosing_type (arg1)))
  1591.             {
  1592.               CORE_ADDR base_addr;

  1593.               base_addr = value_address (arg1) + boffset;
  1594.               v2 = value_at_lazy (basetype, base_addr);
  1595.               if (target_read_memory (base_addr,
  1596.                                       value_contents_raw (v2),
  1597.                                       TYPE_LENGTH (value_type (v2))) != 0)
  1598.                 error (_("virtual baseclass botch"));
  1599.             }
  1600.           else
  1601.             {
  1602.               v2 = value_copy (arg1);
  1603.               deprecated_set_value_type (v2, basetype);
  1604.               set_value_embedded_offset (v2, boffset);
  1605.             }

  1606.           if (found_baseclass)
  1607.             v = v2;
  1608.           else
  1609.             {
  1610.               do_search_struct_field (name, v2, 0,
  1611.                                       TYPE_BASECLASS (type, i),
  1612.                                       looking_for_baseclass,
  1613.                                       result_ptr, last_boffset,
  1614.                                       outermost_type);
  1615.             }
  1616.         }
  1617.       else if (found_baseclass)
  1618.         v = value_primitive_field (arg1, offset, i, type);
  1619.       else
  1620.         {
  1621.           do_search_struct_field (name, arg1,
  1622.                                   offset + TYPE_BASECLASS_BITPOS (type,
  1623.                                                                   i) / 8,
  1624.                                   basetype, looking_for_baseclass,
  1625.                                   result_ptr, last_boffset,
  1626.                                   outermost_type);
  1627.         }

  1628.       update_search_result (result_ptr, v, last_boffset,
  1629.                             boffset, name, outermost_type);
  1630.     }
  1631. }

  1632. /* Helper function used by value_struct_elt to recurse through
  1633.    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
  1634.    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
  1635.    TYPE.  If found, return value, else return NULL.

  1636.    If LOOKING_FOR_BASECLASS, then instead of looking for struct
  1637.    fields, look for a baseclass named NAME.  */

  1638. static struct value *
  1639. search_struct_field (const char *name, struct value *arg1, int offset,
  1640.                      struct type *type, int looking_for_baseclass)
  1641. {
  1642.   struct value *result = NULL;
  1643.   int boffset = 0;

  1644.   do_search_struct_field (name, arg1, offset, type, looking_for_baseclass,
  1645.                           &result, &boffset, type);
  1646.   return result;
  1647. }

  1648. /* Helper function used by value_struct_elt to recurse through
  1649.    baseclasses.  Look for a field NAME in ARG1.  Adjust the address of
  1650.    ARG1 by OFFSET bytes, and search in it assuming it has (class) type
  1651.    TYPE.

  1652.    If found, return value, else if name matched and args not return
  1653.    (value) -1, else return NULL.  */

  1654. static struct value *
  1655. search_struct_method (const char *name, struct value **arg1p,
  1656.                       struct value **args, int offset,
  1657.                       int *static_memfuncp, struct type *type)
  1658. {
  1659.   int i;
  1660.   struct value *v;
  1661.   int name_matched = 0;
  1662.   char dem_opname[64];

  1663.   CHECK_TYPEDEF (type);
  1664.   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
  1665.     {
  1666.       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);

  1667.       /* FIXME!  May need to check for ARM demangling here.  */
  1668.       if (strncmp (t_field_name, "__", 2) == 0 ||
  1669.           strncmp (t_field_name, "op", 2) == 0 ||
  1670.           strncmp (t_field_name, "type", 4) == 0)
  1671.         {
  1672.           if (cplus_demangle_opname (t_field_name, dem_opname, DMGL_ANSI))
  1673.             t_field_name = dem_opname;
  1674.           else if (cplus_demangle_opname (t_field_name, dem_opname, 0))
  1675.             t_field_name = dem_opname;
  1676.         }
  1677.       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1678.         {
  1679.           int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
  1680.           struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);

  1681.           name_matched = 1;
  1682.           check_stub_method_group (type, i);
  1683.           if (j > 0 && args == 0)
  1684.             error (_("cannot resolve overloaded method "
  1685.                      "`%s': no arguments supplied"), name);
  1686.           else if (j == 0 && args == 0)
  1687.             {
  1688.               v = value_fn_field (arg1p, f, j, type, offset);
  1689.               if (v != NULL)
  1690.                 return v;
  1691.             }
  1692.           else
  1693.             while (j >= 0)
  1694.               {
  1695.                 if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
  1696.                               TYPE_VARARGS (TYPE_FN_FIELD_TYPE (f, j)),
  1697.                               TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (f, j)),
  1698.                               TYPE_FN_FIELD_ARGS (f, j), args))
  1699.                   {
  1700.                     if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  1701.                       return value_virtual_fn_field (arg1p, f, j,
  1702.                                                      type, offset);
  1703.                     if (TYPE_FN_FIELD_STATIC_P (f, j)
  1704.                         && static_memfuncp)
  1705.                       *static_memfuncp = 1;
  1706.                     v = value_fn_field (arg1p, f, j, type, offset);
  1707.                     if (v != NULL)
  1708.                       return v;
  1709.                   }
  1710.                 j--;
  1711.               }
  1712.         }
  1713.     }

  1714.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1715.     {
  1716.       int base_offset;
  1717.       int this_offset;

  1718.       if (BASETYPE_VIA_VIRTUAL (type, i))
  1719.         {
  1720.           struct type *baseclass = check_typedef (TYPE_BASECLASS (type, i));
  1721.           struct value *base_val;
  1722.           const gdb_byte *base_valaddr;

  1723.           /* The virtual base class pointer might have been
  1724.              clobbered by the user program.  Make sure that it
  1725.              still points to a valid memory location.  */

  1726.           if (offset < 0 || offset >= TYPE_LENGTH (type))
  1727.             {
  1728.               gdb_byte *tmp;
  1729.               struct cleanup *back_to;
  1730.               CORE_ADDR address;

  1731.               tmp = xmalloc (TYPE_LENGTH (baseclass));
  1732.               back_to = make_cleanup (xfree, tmp);
  1733.               address = value_address (*arg1p);

  1734.               if (target_read_memory (address + offset,
  1735.                                       tmp, TYPE_LENGTH (baseclass)) != 0)
  1736.                 error (_("virtual baseclass botch"));

  1737.               base_val = value_from_contents_and_address (baseclass,
  1738.                                                           tmp,
  1739.                                                           address + offset);
  1740.               base_valaddr = value_contents_for_printing (base_val);
  1741.               this_offset = 0;
  1742.               do_cleanups (back_to);
  1743.             }
  1744.           else
  1745.             {
  1746.               base_val = *arg1p;
  1747.               base_valaddr = value_contents_for_printing (*arg1p);
  1748.               this_offset = offset;
  1749.             }

  1750.           base_offset = baseclass_offset (type, i, base_valaddr,
  1751.                                           this_offset, value_address (base_val),
  1752.                                           base_val);
  1753.         }
  1754.       else
  1755.         {
  1756.           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
  1757.         }
  1758.       v = search_struct_method (name, arg1p, args, base_offset + offset,
  1759.                                 static_memfuncp, TYPE_BASECLASS (type, i));
  1760.       if (v == (struct value *) - 1)
  1761.         {
  1762.           name_matched = 1;
  1763.         }
  1764.       else if (v)
  1765.         {
  1766.           /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
  1767.           /* *arg1p = arg1_tmp; */
  1768.           return v;
  1769.         }
  1770.     }
  1771.   if (name_matched)
  1772.     return (struct value *) - 1;
  1773.   else
  1774.     return NULL;
  1775. }

  1776. /* Given *ARGP, a value of type (pointer to a)* structure/union,
  1777.    extract the component named NAME from the ultimate target
  1778.    structure/union and return it as a value with its appropriate type.
  1779.    ERR is used in the error message if *ARGP's type is wrong.

  1780.    C++: ARGS is a list of argument types to aid in the selection of
  1781.    an appropriate method.  Also, handle derived types.

  1782.    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
  1783.    where the truthvalue of whether the function that was resolved was
  1784.    a static member function or not is stored.

  1785.    ERR is an error message to be printed in case the field is not
  1786.    found.  */

  1787. struct value *
  1788. value_struct_elt (struct value **argp, struct value **args,
  1789.                   const char *name, int *static_memfuncp, const char *err)
  1790. {
  1791.   struct type *t;
  1792.   struct value *v;

  1793.   *argp = coerce_array (*argp);

  1794.   t = check_typedef (value_type (*argp));

  1795.   /* Follow pointers until we get to a non-pointer.  */

  1796.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  1797.     {
  1798.       *argp = value_ind (*argp);
  1799.       /* Don't coerce fn pointer to fn and then back again!  */
  1800.       if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
  1801.         *argp = coerce_array (*argp);
  1802.       t = check_typedef (value_type (*argp));
  1803.     }

  1804.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1805.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1806.     error (_("Attempt to extract a component of a value that is not a %s."),
  1807.            err);

  1808.   /* Assume it's not, unless we see that it is.  */
  1809.   if (static_memfuncp)
  1810.     *static_memfuncp = 0;

  1811.   if (!args)
  1812.     {
  1813.       /* if there are no arguments ...do this...  */

  1814.       /* Try as a field first, because if we succeed, there is less
  1815.          work to be done.  */
  1816.       v = search_struct_field (name, *argp, 0, t, 0);
  1817.       if (v)
  1818.         return v;

  1819.       /* C++: If it was not found as a data field, then try to
  1820.          return it as a pointer to a method.  */
  1821.       v = search_struct_method (name, argp, args, 0,
  1822.                                 static_memfuncp, t);

  1823.       if (v == (struct value *) - 1)
  1824.         error (_("Cannot take address of method %s."), name);
  1825.       else if (v == 0)
  1826.         {
  1827.           if (TYPE_NFN_FIELDS (t))
  1828.             error (_("There is no member or method named %s."), name);
  1829.           else
  1830.             error (_("There is no member named %s."), name);
  1831.         }
  1832.       return v;
  1833.     }

  1834.   v = search_struct_method (name, argp, args, 0,
  1835.                             static_memfuncp, t);

  1836.   if (v == (struct value *) - 1)
  1837.     {
  1838.       error (_("One of the arguments you tried to pass to %s could not "
  1839.                "be converted to what the function wants."), name);
  1840.     }
  1841.   else if (v == 0)
  1842.     {
  1843.       /* See if user tried to invoke data as function.  If so, hand it
  1844.          back.  If it's not callable (i.e., a pointer to function),
  1845.          gdb should give an error.  */
  1846.       v = search_struct_field (name, *argp, 0, t, 0);
  1847.       /* If we found an ordinary field, then it is not a method call.
  1848.          So, treat it as if it were a static member function.  */
  1849.       if (v && static_memfuncp)
  1850.         *static_memfuncp = 1;
  1851.     }

  1852.   if (!v)
  1853.     throw_error (NOT_FOUND_ERROR,
  1854.                  _("Structure has no component named %s."), name);
  1855.   return v;
  1856. }

  1857. /* Given *ARGP, a value of type structure or union, or a pointer/reference
  1858.    to a structure or union, extract and return its component (field) of
  1859.    type FTYPE at the specified BITPOS.
  1860.    Throw an exception on error.  */

  1861. struct value *
  1862. value_struct_elt_bitpos (struct value **argp, int bitpos, struct type *ftype,
  1863.                          const char *err)
  1864. {
  1865.   struct type *t;
  1866.   struct value *v;
  1867.   int i;
  1868.   int nbases;

  1869.   *argp = coerce_array (*argp);

  1870.   t = check_typedef (value_type (*argp));

  1871.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  1872.     {
  1873.       *argp = value_ind (*argp);
  1874.       if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
  1875.         *argp = coerce_array (*argp);
  1876.       t = check_typedef (value_type (*argp));
  1877.     }

  1878.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1879.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1880.     error (_("Attempt to extract a component of a value that is not a %s."),
  1881.            err);

  1882.   for (i = TYPE_N_BASECLASSES (t); i < TYPE_NFIELDS (t); i++)
  1883.     {
  1884.       if (!field_is_static (&TYPE_FIELD (t, i))
  1885.           && bitpos == TYPE_FIELD_BITPOS (t, i)
  1886.           && types_equal (ftype, TYPE_FIELD_TYPE (t, i)))
  1887.         return value_primitive_field (*argp, 0, i, t);
  1888.     }

  1889.   error (_("No field with matching bitpos and type."));

  1890.   /* Never hit.  */
  1891.   return NULL;
  1892. }

  1893. /* Search through the methods of an object (and its bases) to find a
  1894.    specified method.  Return the pointer to the fn_field list FN_LIST of
  1895.    overloaded instances defined in the source language.  If available
  1896.    and matching, a vector of matching xmethods defined in extension
  1897.    languages are also returned in XM_WORKER_VEC

  1898.    Helper function for value_find_oload_list.
  1899.    ARGP is a pointer to a pointer to a value (the object).
  1900.    METHOD is a string containing the method name.
  1901.    OFFSET is the offset within the value.
  1902.    TYPE is the assumed type of the object.
  1903.    FN_LIST is the pointer to matching overloaded instances defined in
  1904.       source language.  Since this is a recursive function, *FN_LIST
  1905.       should be set to NULL when calling this function.
  1906.    NUM_FNS is the number of overloaded instances.  *NUM_FNS should be set to
  1907.       0 when calling this function.
  1908.    XM_WORKER_VEC is the vector of matching xmethod workers.  *XM_WORKER_VEC
  1909.       should also be set to NULL when calling this function.
  1910.    BASETYPE is set to the actual type of the subobject where the
  1911.       method is found.
  1912.    BOFFSET is the offset of the base subobject where the method is found.  */

  1913. static void
  1914. find_method_list (struct value **argp, const char *method,
  1915.                   int offset, struct type *type,
  1916.                   struct fn_field **fn_list, int *num_fns,
  1917.                   VEC (xmethod_worker_ptr) **xm_worker_vec,
  1918.                   struct type **basetype, int *boffset)
  1919. {
  1920.   int i;
  1921.   struct fn_field *f = NULL;
  1922.   VEC (xmethod_worker_ptr) *worker_vec = NULL, *new_vec = NULL;

  1923.   gdb_assert (fn_list != NULL && xm_worker_vec != NULL);
  1924.   CHECK_TYPEDEF (type);

  1925.   /* First check in object itself.
  1926.      This function is called recursively to search through base classes.
  1927.      If there is a source method match found at some stage, then we need not
  1928.      look for source methods in consequent recursive calls.  */
  1929.   if ((*fn_list) == NULL)
  1930.     {
  1931.       for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
  1932.         {
  1933.           /* pai: FIXME What about operators and type conversions?  */
  1934.           const char *fn_field_name = TYPE_FN_FIELDLIST_NAME (type, i);

  1935.           if (fn_field_name && (strcmp_iw (fn_field_name, method) == 0))
  1936.             {
  1937.               int len = TYPE_FN_FIELDLIST_LENGTH (type, i);
  1938.               f = TYPE_FN_FIELDLIST1 (type, i);
  1939.               *fn_list = f;

  1940.               *num_fns = len;
  1941.               *basetype = type;
  1942.               *boffset = offset;

  1943.               /* Resolve any stub methods.  */
  1944.               check_stub_method_group (type, i);

  1945.               break;
  1946.             }
  1947.         }
  1948.     }

  1949.   /* Unlike source methods, xmethods can be accumulated over successive
  1950.      recursive calls.  In other words, an xmethod named 'm' in a class
  1951.      will not hide an xmethod named 'm' in its base class(es).  We want
  1952.      it to be this way because xmethods are after all convenience functions
  1953.      and hence there is no point restricting them with something like method
  1954.      hiding.  Moreover, if hiding is done for xmethods as well, then we will
  1955.      have to provide a mechanism to un-hide (like the 'using' construct).  */
  1956.   worker_vec = get_matching_xmethod_workers (type, method);
  1957.   new_vec = VEC_merge (xmethod_worker_ptr, *xm_worker_vec, worker_vec);

  1958.   VEC_free (xmethod_worker_ptr, *xm_worker_vec);
  1959.   VEC_free (xmethod_worker_ptr, worker_vec);
  1960.   *xm_worker_vec = new_vec;

  1961.   /* If source methods are not found in current class, look for them in the
  1962.      base classes.  We also have to go through the base classes to gather
  1963.      extension methods.  */
  1964.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1965.     {
  1966.       int base_offset;

  1967.       if (BASETYPE_VIA_VIRTUAL (type, i))
  1968.         {
  1969.           base_offset = baseclass_offset (type, i,
  1970.                                           value_contents_for_printing (*argp),
  1971.                                           value_offset (*argp) + offset,
  1972.                                           value_address (*argp), *argp);
  1973.         }
  1974.       else /* Non-virtual base, simply use bit position from debug
  1975.               info.  */
  1976.         {
  1977.           base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
  1978.         }

  1979.       find_method_list (argp, method, base_offset + offset,
  1980.                         TYPE_BASECLASS (type, i), fn_list, num_fns,
  1981.                         xm_worker_vec, basetype, boffset);
  1982.     }
  1983. }

  1984. /* Return the list of overloaded methods of a specified name.  The methods
  1985.    could be those GDB finds in the binary, or xmethod.  Methods found in
  1986.    the binary are returned in FN_LIST, and xmethods are returned in
  1987.    XM_WORKER_VEC.

  1988.    ARGP is a pointer to a pointer to a value (the object).
  1989.    METHOD is the method name.
  1990.    OFFSET is the offset within the value contents.
  1991.    FN_LIST is the pointer to matching overloaded instances defined in
  1992.       source language.
  1993.    NUM_FNS is the number of overloaded instances.
  1994.    XM_WORKER_VEC is the vector of matching xmethod workers defined in
  1995.       extension languages.
  1996.    BASETYPE is set to the type of the base subobject that defines the
  1997.       method.
  1998.    BOFFSET is the offset of the base subobject which defines the method.  */

  1999. static void
  2000. value_find_oload_method_list (struct value **argp, const char *method,
  2001.                               int offset, struct fn_field **fn_list,
  2002.                               int *num_fns,
  2003.                               VEC (xmethod_worker_ptr) **xm_worker_vec,
  2004.                               struct type **basetype, int *boffset)
  2005. {
  2006.   struct type *t;

  2007.   t = check_typedef (value_type (*argp));

  2008.   /* Code snarfed from value_struct_elt.  */
  2009.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  2010.     {
  2011.       *argp = value_ind (*argp);
  2012.       /* Don't coerce fn pointer to fn and then back again!  */
  2013.       if (TYPE_CODE (check_typedef (value_type (*argp))) != TYPE_CODE_FUNC)
  2014.         *argp = coerce_array (*argp);
  2015.       t = check_typedef (value_type (*argp));
  2016.     }

  2017.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  2018.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  2019.     error (_("Attempt to extract a component of a "
  2020.              "value that is not a struct or union"));

  2021.   gdb_assert (fn_list != NULL && xm_worker_vec != NULL);

  2022.   /* Clear the lists.  */
  2023.   *fn_list = NULL;
  2024.   *num_fns = 0;
  2025.   *xm_worker_vec = NULL;

  2026.   find_method_list (argp, method, 0, t, fn_list, num_fns, xm_worker_vec,
  2027.                     basetype, boffset);
  2028. }

  2029. /* Given an array of arguments (ARGS) (which includes an
  2030.    entry for "this" in the case of C++ methods), the number of
  2031.    arguments NARGS, the NAME of a function, and whether it's a method or
  2032.    not (METHOD), find the best function that matches on the argument types
  2033.    according to the overload resolution rules.

  2034.    METHOD can be one of three values:
  2035.      NON_METHOD for non-member functions.
  2036.      METHOD: for member functions.
  2037.      BOTH: used for overload resolution of operators where the
  2038.        candidates are expected to be either member or non member
  2039.        functions.  In this case the first argument ARGTYPES
  2040.        (representing 'this') is expected to be a reference to the
  2041.        target object, and will be dereferenced when attempting the
  2042.        non-member search.

  2043.    In the case of class methods, the parameter OBJ is an object value
  2044.    in which to search for overloaded methods.

  2045.    In the case of non-method functions, the parameter FSYM is a symbol
  2046.    corresponding to one of the overloaded functions.

  2047.    Return value is an integer: 0 -> good match, 10 -> debugger applied
  2048.    non-standard coercions, 100 -> incompatible.

  2049.    If a method is being searched for, VALP will hold the value.
  2050.    If a non-method is being searched for, SYMP will hold the symbol
  2051.    for it.

  2052.    If a method is being searched for, and it is a static method,
  2053.    then STATICP will point to a non-zero value.

  2054.    If NO_ADL argument dependent lookup is disabled.  This is used to prevent
  2055.    ADL overload candidates when performing overload resolution for a fully
  2056.    qualified name.

  2057.    If NOSIDE is EVAL_AVOID_SIDE_EFFECTS, then OBJP's memory cannot be
  2058.    read while picking the best overload match (it may be all zeroes and thus
  2059.    not have a vtable pointer), in which case skip virtual function lookup.
  2060.    This is ok as typically EVAL_AVOID_SIDE_EFFECTS is only used to determine
  2061.    the result type.

  2062.    Note: This function does *not* check the value of
  2063.    overload_resolution.  Caller must check it to see whether overload
  2064.    resolution is permitted.  */

  2065. int
  2066. find_overload_match (struct value **args, int nargs,
  2067.                      const char *name, enum oload_search_type method,
  2068.                      struct value **objp, struct symbol *fsym,
  2069.                      struct value **valp, struct symbol **symp,
  2070.                      int *staticp, const int no_adl,
  2071.                      const enum noside noside)
  2072. {
  2073.   struct value *obj = (objp ? *objp : NULL);
  2074.   struct type *obj_type = obj ? value_type (obj) : NULL;
  2075.   /* Index of best overloaded function.  */
  2076.   int func_oload_champ = -1;
  2077.   int method_oload_champ = -1;
  2078.   int src_method_oload_champ = -1;
  2079.   int ext_method_oload_champ = -1;
  2080.   int src_and_ext_equal = 0;

  2081.   /* The measure for the current best match.  */
  2082.   struct badness_vector *method_badness = NULL;
  2083.   struct badness_vector *func_badness = NULL;
  2084.   struct badness_vector *ext_method_badness = NULL;
  2085.   struct badness_vector *src_method_badness = NULL;

  2086.   struct value *temp = obj;
  2087.   /* For methods, the list of overloaded methods.  */
  2088.   struct fn_field *fns_ptr = NULL;
  2089.   /* For non-methods, the list of overloaded function symbols.  */
  2090.   struct symbol **oload_syms = NULL;
  2091.   /* For xmethods, the VEC of xmethod workers.  */
  2092.   VEC (xmethod_worker_ptr) *xm_worker_vec = NULL;
  2093.   /* Number of overloaded instances being considered.  */
  2094.   int num_fns = 0;
  2095.   struct type *basetype = NULL;
  2096.   int boffset;

  2097.   struct cleanup *all_cleanups = make_cleanup (null_cleanup, NULL);

  2098.   const char *obj_type_name = NULL;
  2099.   const char *func_name = NULL;
  2100.   enum oload_classification match_quality;
  2101.   enum oload_classification method_match_quality = INCOMPATIBLE;
  2102.   enum oload_classification src_method_match_quality = INCOMPATIBLE;
  2103.   enum oload_classification ext_method_match_quality = INCOMPATIBLE;
  2104.   enum oload_classification func_match_quality = INCOMPATIBLE;

  2105.   /* Get the list of overloaded methods or functions.  */
  2106.   if (method == METHOD || method == BOTH)
  2107.     {
  2108.       gdb_assert (obj);

  2109.       /* OBJ may be a pointer value rather than the object itself.  */
  2110.       obj = coerce_ref (obj);
  2111.       while (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_PTR)
  2112.         obj = coerce_ref (value_ind (obj));
  2113.       obj_type_name = TYPE_NAME (value_type (obj));

  2114.       /* First check whether this is a data member, e.g. a pointer to
  2115.          a function.  */
  2116.       if (TYPE_CODE (check_typedef (value_type (obj))) == TYPE_CODE_STRUCT)
  2117.         {
  2118.           *valp = search_struct_field (name, obj, 0,
  2119.                                        check_typedef (value_type (obj)), 0);
  2120.           if (*valp)
  2121.             {
  2122.               *staticp = 1;
  2123.               do_cleanups (all_cleanups);
  2124.               return 0;
  2125.             }
  2126.         }

  2127.       /* Retrieve the list of methods with the name NAME.  */
  2128.       value_find_oload_method_list (&temp, name, 0, &fns_ptr, &num_fns,
  2129.                                     &xm_worker_vec, &basetype, &boffset);
  2130.       /* If this is a method only search, and no methods were found
  2131.          the search has faild.  */
  2132.       if (method == METHOD && (!fns_ptr || !num_fns) && !xm_worker_vec)
  2133.         error (_("Couldn't find method %s%s%s"),
  2134.                obj_type_name,
  2135.                (obj_type_name && *obj_type_name) ? "::" : "",
  2136.                name);
  2137.       /* If we are dealing with stub method types, they should have
  2138.          been resolved by find_method_list via
  2139.          value_find_oload_method_list above.  */
  2140.       if (fns_ptr)
  2141.         {
  2142.           gdb_assert (TYPE_DOMAIN_TYPE (fns_ptr[0].type) != NULL);

  2143.           src_method_oload_champ = find_oload_champ (args, nargs,
  2144.                                                      num_fns, fns_ptr, NULL,
  2145.                                                      NULL, &src_method_badness);

  2146.           src_method_match_quality = classify_oload_match
  2147.             (src_method_badness, nargs,
  2148.              oload_method_static_p (fns_ptr, src_method_oload_champ));

  2149.           make_cleanup (xfree, src_method_badness);
  2150.         }

  2151.       if (VEC_length (xmethod_worker_ptr, xm_worker_vec) > 0)
  2152.         {
  2153.           ext_method_oload_champ = find_oload_champ (args, nargs,
  2154.                                                      0, NULL, xm_worker_vec,
  2155.                                                      NULL, &ext_method_badness);
  2156.           ext_method_match_quality = classify_oload_match (ext_method_badness,
  2157.                                                            nargs, 0);
  2158.           make_cleanup (xfree, ext_method_badness);
  2159.           make_cleanup (free_xmethod_worker_vec, xm_worker_vec);
  2160.         }

  2161.       if (src_method_oload_champ >= 0 && ext_method_oload_champ >= 0)
  2162.         {
  2163.           switch (compare_badness (ext_method_badness, src_method_badness))
  2164.             {
  2165.               case 0: /* Src method and xmethod are equally good.  */
  2166.                 src_and_ext_equal = 1;
  2167.                 /* If src method and xmethod are equally good, then
  2168.                    xmethod should be the winner.  Hence, fall through to the
  2169.                    case where a xmethod is better than the source
  2170.                    method, except when the xmethod match quality is
  2171.                    non-standard.  */
  2172.                 /* FALLTHROUGH */
  2173.               case 1: /* Src method and ext method are incompatible.  */
  2174.                 /* If ext method match is not standard, then let source method
  2175.                    win.  Otherwise, fallthrough to let xmethod win.  */
  2176.                 if (ext_method_match_quality != STANDARD)
  2177.                   {
  2178.                     method_oload_champ = src_method_oload_champ;
  2179.                     method_badness = src_method_badness;
  2180.                     ext_method_oload_champ = -1;
  2181.                     method_match_quality = src_method_match_quality;
  2182.                     break;
  2183.                   }
  2184.                 /* FALLTHROUGH */
  2185.               case 2: /* Ext method is champion.  */
  2186.                 method_oload_champ = ext_method_oload_champ;
  2187.                 method_badness = ext_method_badness;
  2188.                 src_method_oload_champ = -1;
  2189.                 method_match_quality = ext_method_match_quality;
  2190.                 break;
  2191.               case 3: /* Src method is champion.  */
  2192.                 method_oload_champ = src_method_oload_champ;
  2193.                 method_badness = src_method_badness;
  2194.                 ext_method_oload_champ = -1;
  2195.                 method_match_quality = src_method_match_quality;
  2196.                 break;
  2197.               default:
  2198.                 gdb_assert_not_reached ("Unexpected overload comparison "
  2199.                                         "result");
  2200.                 break;
  2201.             }
  2202.         }
  2203.       else if (src_method_oload_champ >= 0)
  2204.         {
  2205.           method_oload_champ = src_method_oload_champ;
  2206.           method_badness = src_method_badness;
  2207.           method_match_quality = src_method_match_quality;
  2208.         }
  2209.       else if (ext_method_oload_champ >= 0)
  2210.         {
  2211.           method_oload_champ = ext_method_oload_champ;
  2212.           method_badness = ext_method_badness;
  2213.           method_match_quality = ext_method_match_quality;
  2214.         }
  2215.     }

  2216.   if (method == NON_METHOD || method == BOTH)
  2217.     {
  2218.       const char *qualified_name = NULL;

  2219.       /* If the overload match is being search for both as a method
  2220.          and non member function, the first argument must now be
  2221.          dereferenced.  */
  2222.       if (method == BOTH)
  2223.         args[0] = value_ind (args[0]);

  2224.       if (fsym)
  2225.         {
  2226.           qualified_name = SYMBOL_NATURAL_NAME (fsym);

  2227.           /* If we have a function with a C++ name, try to extract just
  2228.              the function part.  Do not try this for non-functions (e.g.
  2229.              function pointers).  */
  2230.           if (qualified_name
  2231.               && TYPE_CODE (check_typedef (SYMBOL_TYPE (fsym)))
  2232.               == TYPE_CODE_FUNC)
  2233.             {
  2234.               char *temp;

  2235.               temp = cp_func_name (qualified_name);

  2236.               /* If cp_func_name did not remove anything, the name of the
  2237.                  symbol did not include scope or argument types - it was
  2238.                  probably a C-style function.  */
  2239.               if (temp)
  2240.                 {
  2241.                   make_cleanup (xfree, temp);
  2242.                   if (strcmp (temp, qualified_name) == 0)
  2243.                     func_name = NULL;
  2244.                   else
  2245.                     func_name = temp;
  2246.                 }
  2247.             }
  2248.         }
  2249.       else
  2250.         {
  2251.           func_name = name;
  2252.           qualified_name = name;
  2253.         }

  2254.       /* If there was no C++ name, this must be a C-style function or
  2255.          not a function at all.  Just return the same symbol.  Do the
  2256.          same if cp_func_name fails for some reason.  */
  2257.       if (func_name == NULL)
  2258.         {
  2259.           *symp = fsym;
  2260.           do_cleanups (all_cleanups);
  2261.           return 0;
  2262.         }

  2263.       func_oload_champ = find_oload_champ_namespace (args, nargs,
  2264.                                                      func_name,
  2265.                                                      qualified_name,
  2266.                                                      &oload_syms,
  2267.                                                      &func_badness,
  2268.                                                      no_adl);

  2269.       if (func_oload_champ >= 0)
  2270.         func_match_quality = classify_oload_match (func_badness, nargs, 0);

  2271.       make_cleanup (xfree, oload_syms);
  2272.       make_cleanup (xfree, func_badness);
  2273.     }

  2274.   /* Did we find a match ?  */
  2275.   if (method_oload_champ == -1 && func_oload_champ == -1)
  2276.     throw_error (NOT_FOUND_ERROR,
  2277.                  _("No symbol \"%s\" in current context."),
  2278.                  name);

  2279.   /* If we have found both a method match and a function
  2280.      match, find out which one is better, and calculate match
  2281.      quality.  */
  2282.   if (method_oload_champ >= 0 && func_oload_champ >= 0)
  2283.     {
  2284.       switch (compare_badness (func_badness, method_badness))
  2285.         {
  2286.           case 0: /* Top two contenders are equally good.  */
  2287.             /* FIXME: GDB does not support the general ambiguous case.
  2288.              All candidates should be collected and presented the
  2289.              user.  */
  2290.             error (_("Ambiguous overload resolution"));
  2291.             break;
  2292.           case 1: /* Incomparable top contenders.  */
  2293.             /* This is an error incompatible candidates
  2294.                should not have been proposed.  */
  2295.             error (_("Internal error: incompatible "
  2296.                      "overload candidates proposed"));
  2297.             break;
  2298.           case 2: /* Function champion.  */
  2299.             method_oload_champ = -1;
  2300.             match_quality = func_match_quality;
  2301.             break;
  2302.           case 3: /* Method champion.  */
  2303.             func_oload_champ = -1;
  2304.             match_quality = method_match_quality;
  2305.             break;
  2306.           default:
  2307.             error (_("Internal error: unexpected overload comparison result"));
  2308.             break;
  2309.         }
  2310.     }
  2311.   else
  2312.     {
  2313.       /* We have either a method match or a function match.  */
  2314.       if (method_oload_champ >= 0)
  2315.         match_quality = method_match_quality;
  2316.       else
  2317.         match_quality = func_match_quality;
  2318.     }

  2319.   if (match_quality == INCOMPATIBLE)
  2320.     {
  2321.       if (method == METHOD)
  2322.         error (_("Cannot resolve method %s%s%s to any overloaded instance"),
  2323.                obj_type_name,
  2324.                (obj_type_name && *obj_type_name) ? "::" : "",
  2325.                name);
  2326.       else
  2327.         error (_("Cannot resolve function %s to any overloaded instance"),
  2328.                func_name);
  2329.     }
  2330.   else if (match_quality == NON_STANDARD)
  2331.     {
  2332.       if (method == METHOD)
  2333.         warning (_("Using non-standard conversion to match "
  2334.                    "method %s%s%s to supplied arguments"),
  2335.                  obj_type_name,
  2336.                  (obj_type_name && *obj_type_name) ? "::" : "",
  2337.                  name);
  2338.       else
  2339.         warning (_("Using non-standard conversion to match "
  2340.                    "function %s to supplied arguments"),
  2341.                  func_name);
  2342.     }

  2343.   if (staticp != NULL)
  2344.     *staticp = oload_method_static_p (fns_ptr, method_oload_champ);

  2345.   if (method_oload_champ >= 0)
  2346.     {
  2347.       if (src_method_oload_champ >= 0)
  2348.         {
  2349.           if (TYPE_FN_FIELD_VIRTUAL_P (fns_ptr, method_oload_champ)
  2350.               && noside != EVAL_AVOID_SIDE_EFFECTS)
  2351.             {
  2352.               *valp = value_virtual_fn_field (&temp, fns_ptr,
  2353.                                               method_oload_champ, basetype,
  2354.                                               boffset);
  2355.             }
  2356.           else
  2357.             *valp = value_fn_field (&temp, fns_ptr, method_oload_champ,
  2358.                                     basetype, boffset);
  2359.         }
  2360.       else
  2361.         {
  2362.           *valp = value_of_xmethod (clone_xmethod_worker
  2363.             (VEC_index (xmethod_worker_ptr, xm_worker_vec,
  2364.                         ext_method_oload_champ)));
  2365.         }
  2366.     }
  2367.   else
  2368.     *symp = oload_syms[func_oload_champ];

  2369.   if (objp)
  2370.     {
  2371.       struct type *temp_type = check_typedef (value_type (temp));
  2372.       struct type *objtype = check_typedef (obj_type);

  2373.       if (TYPE_CODE (temp_type) != TYPE_CODE_PTR
  2374.           && (TYPE_CODE (objtype) == TYPE_CODE_PTR
  2375.               || TYPE_CODE (objtype) == TYPE_CODE_REF))
  2376.         {
  2377.           temp = value_addr (temp);
  2378.         }
  2379.       *objp = temp;
  2380.     }

  2381.   do_cleanups (all_cleanups);

  2382.   switch (match_quality)
  2383.     {
  2384.     case INCOMPATIBLE:
  2385.       return 100;
  2386.     case NON_STANDARD:
  2387.       return 10;
  2388.     default:                                /* STANDARD */
  2389.       return 0;
  2390.     }
  2391. }

  2392. /* Find the best overload match, searching for FUNC_NAME in namespaces
  2393.    contained in QUALIFIED_NAME until it either finds a good match or
  2394.    runs out of namespaces.  It stores the overloaded functions in
  2395.    *OLOAD_SYMS, and the badness vector in *OLOAD_CHAMP_BV.  The
  2396.    calling function is responsible for freeing *OLOAD_SYMS and
  2397.    *OLOAD_CHAMP_BV.  If NO_ADL, argument dependent lookup is not
  2398.    performned.  */

  2399. static int
  2400. find_oload_champ_namespace (struct value **args, int nargs,
  2401.                             const char *func_name,
  2402.                             const char *qualified_name,
  2403.                             struct symbol ***oload_syms,
  2404.                             struct badness_vector **oload_champ_bv,
  2405.                             const int no_adl)
  2406. {
  2407.   int oload_champ;

  2408.   find_oload_champ_namespace_loop (args, nargs,
  2409.                                    func_name,
  2410.                                    qualified_name, 0,
  2411.                                    oload_syms, oload_champ_bv,
  2412.                                    &oload_champ,
  2413.                                    no_adl);

  2414.   return oload_champ;
  2415. }

  2416. /* Helper function for find_oload_champ_namespace; NAMESPACE_LEN is
  2417.    how deep we've looked for namespaces, and the champ is stored in
  2418.    OLOAD_CHAMP.  The return value is 1 if the champ is a good one, 0
  2419.    if it isn't.  Other arguments are the same as in
  2420.    find_oload_champ_namespace

  2421.    It is the caller's responsibility to free *OLOAD_SYMS and
  2422.    *OLOAD_CHAMP_BV.  */

  2423. static int
  2424. find_oload_champ_namespace_loop (struct value **args, int nargs,
  2425.                                  const char *func_name,
  2426.                                  const char *qualified_name,
  2427.                                  int namespace_len,
  2428.                                  struct symbol ***oload_syms,
  2429.                                  struct badness_vector **oload_champ_bv,
  2430.                                  int *oload_champ,
  2431.                                  const int no_adl)
  2432. {
  2433.   int next_namespace_len = namespace_len;
  2434.   int searched_deeper = 0;
  2435.   int num_fns = 0;
  2436.   struct cleanup *old_cleanups;
  2437.   int new_oload_champ;
  2438.   struct symbol **new_oload_syms;
  2439.   struct badness_vector *new_oload_champ_bv;
  2440.   char *new_namespace;

  2441.   if (next_namespace_len != 0)
  2442.     {
  2443.       gdb_assert (qualified_name[next_namespace_len] == ':');
  2444.       next_namespace_len +=  2;
  2445.     }
  2446.   next_namespace_len +=
  2447.     cp_find_first_component (qualified_name + next_namespace_len);

  2448.   /* Initialize these to values that can safely be xfree'd.  */
  2449.   *oload_syms = NULL;
  2450.   *oload_champ_bv = NULL;

  2451.   /* First, see if we have a deeper namespace we can search in.
  2452.      If we get a good match there, use it.  */

  2453.   if (qualified_name[next_namespace_len] == ':')
  2454.     {
  2455.       searched_deeper = 1;

  2456.       if (find_oload_champ_namespace_loop (args, nargs,
  2457.                                            func_name, qualified_name,
  2458.                                            next_namespace_len,
  2459.                                            oload_syms, oload_champ_bv,
  2460.                                            oload_champ, no_adl))
  2461.         {
  2462.           return 1;
  2463.         }
  2464.     };

  2465.   /* If we reach here, either we're in the deepest namespace or we
  2466.      didn't find a good match in a deeper namespace.  But, in the
  2467.      latter case, we still have a bad match in a deeper namespace;
  2468.      note that we might not find any match at all in the current
  2469.      namespace.  (There's always a match in the deepest namespace,
  2470.      because this overload mechanism only gets called if there's a
  2471.      function symbol to start off with.)  */

  2472.   old_cleanups = make_cleanup (xfree, *oload_syms);
  2473.   make_cleanup (xfree, *oload_champ_bv);
  2474.   new_namespace = alloca (namespace_len + 1);
  2475.   strncpy (new_namespace, qualified_name, namespace_len);
  2476.   new_namespace[namespace_len] = '\0';
  2477.   new_oload_syms = make_symbol_overload_list (func_name,
  2478.                                               new_namespace);

  2479.   /* If we have reached the deepest level perform argument
  2480.      determined lookup.  */
  2481.   if (!searched_deeper && !no_adl)
  2482.     {
  2483.       int ix;
  2484.       struct type **arg_types;

  2485.       /* Prepare list of argument types for overload resolution.  */
  2486.       arg_types = (struct type **)
  2487.         alloca (nargs * (sizeof (struct type *)));
  2488.       for (ix = 0; ix < nargs; ix++)
  2489.         arg_types[ix] = value_type (args[ix]);
  2490.       make_symbol_overload_list_adl (arg_types, nargs, func_name);
  2491.     }

  2492.   while (new_oload_syms[num_fns])
  2493.     ++num_fns;

  2494.   new_oload_champ = find_oload_champ (args, nargs, num_fns,
  2495.                                       NULL, NULL, new_oload_syms,
  2496.                                       &new_oload_champ_bv);

  2497.   /* Case 1: We found a good match.  Free earlier matches (if any),
  2498.      and return it.  Case 2: We didn't find a good match, but we're
  2499.      not the deepest function.  Then go with the bad match that the
  2500.      deeper function found.  Case 3: We found a bad match, and we're
  2501.      the deepest function.  Then return what we found, even though
  2502.      it's a bad match.  */

  2503.   if (new_oload_champ != -1
  2504.       && classify_oload_match (new_oload_champ_bv, nargs, 0) == STANDARD)
  2505.     {
  2506.       *oload_syms = new_oload_syms;
  2507.       *oload_champ = new_oload_champ;
  2508.       *oload_champ_bv = new_oload_champ_bv;
  2509.       do_cleanups (old_cleanups);
  2510.       return 1;
  2511.     }
  2512.   else if (searched_deeper)
  2513.     {
  2514.       xfree (new_oload_syms);
  2515.       xfree (new_oload_champ_bv);
  2516.       discard_cleanups (old_cleanups);
  2517.       return 0;
  2518.     }
  2519.   else
  2520.     {
  2521.       *oload_syms = new_oload_syms;
  2522.       *oload_champ = new_oload_champ;
  2523.       *oload_champ_bv = new_oload_champ_bv;
  2524.       do_cleanups (old_cleanups);
  2525.       return 0;
  2526.     }
  2527. }

  2528. /* Look for a function to take NARGS args of ARGS.  Find
  2529.    the best match from among the overloaded methods or functions
  2530.    given by FNS_PTR or OLOAD_SYMS or XM_WORKER_VEC, respectively.
  2531.    One, and only one of FNS_PTR, OLOAD_SYMS and XM_WORKER_VEC can be
  2532.    non-NULL.

  2533.    If XM_WORKER_VEC is NULL, then the length of the arrays FNS_PTR
  2534.    or OLOAD_SYMS (whichever is non-NULL) is specified in NUM_FNS.

  2535.    Return the index of the best match; store an indication of the
  2536.    quality of the match in OLOAD_CHAMP_BV.

  2537.    It is the caller's responsibility to free *OLOAD_CHAMP_BV.  */

  2538. static int
  2539. find_oload_champ (struct value **args, int nargs,
  2540.                   int num_fns, struct fn_field *fns_ptr,
  2541.                   VEC (xmethod_worker_ptr) *xm_worker_vec,
  2542.                   struct symbol **oload_syms,
  2543.                   struct badness_vector **oload_champ_bv)
  2544. {
  2545.   int ix;
  2546.   int fn_count;
  2547.   int xm_worker_vec_n = VEC_length (xmethod_worker_ptr, xm_worker_vec);
  2548.   /* A measure of how good an overloaded instance is.  */
  2549.   struct badness_vector *bv;
  2550.   /* Index of best overloaded function.  */
  2551.   int oload_champ = -1;
  2552.   /* Current ambiguity state for overload resolution.  */
  2553.   int oload_ambiguous = 0;
  2554.   /* 0 => no ambiguity, 1 => two good funcs, 2 => incomparable funcs.  */

  2555.   /* A champion can be found among methods alone, or among functions
  2556.      alone, or in xmethods alone, but not in more than one of these
  2557.      groups.  */
  2558.   gdb_assert ((fns_ptr != NULL) + (oload_syms != NULL) + (xm_worker_vec != NULL)
  2559.               == 1);

  2560.   *oload_champ_bv = NULL;

  2561.   fn_count = (xm_worker_vec != NULL
  2562.               ? VEC_length (xmethod_worker_ptr, xm_worker_vec)
  2563.               : num_fns);
  2564.   /* Consider each candidate in turn.  */
  2565.   for (ix = 0; ix < fn_count; ix++)
  2566.     {
  2567.       int jj;
  2568.       int static_offset = 0;
  2569.       int nparms;
  2570.       struct type **parm_types;
  2571.       struct xmethod_worker *worker = NULL;

  2572.       if (xm_worker_vec != NULL)
  2573.         {
  2574.           worker = VEC_index (xmethod_worker_ptr, xm_worker_vec, ix);
  2575.           parm_types = get_xmethod_arg_types (worker, &nparms);
  2576.         }
  2577.       else
  2578.         {
  2579.           if (fns_ptr != NULL)
  2580.             {
  2581.               nparms = TYPE_NFIELDS (TYPE_FN_FIELD_TYPE (fns_ptr, ix));
  2582.               static_offset = oload_method_static_p (fns_ptr, ix);
  2583.             }
  2584.           else
  2585.             nparms = TYPE_NFIELDS (SYMBOL_TYPE (oload_syms[ix]));

  2586.           parm_types = (struct type **)
  2587.             xmalloc (nparms * (sizeof (struct type *)));
  2588.           for (jj = 0; jj < nparms; jj++)
  2589.             parm_types[jj] = (fns_ptr != NULL
  2590.                               ? (TYPE_FN_FIELD_ARGS (fns_ptr, ix)[jj].type)
  2591.                               : TYPE_FIELD_TYPE (SYMBOL_TYPE (oload_syms[ix]),
  2592.                                                  jj));
  2593.         }

  2594.       /* Compare parameter types to supplied argument types.  Skip
  2595.          THIS for static methods.  */
  2596.       bv = rank_function (parm_types, nparms,
  2597.                           args + static_offset,
  2598.                           nargs - static_offset);

  2599.       if (!*oload_champ_bv)
  2600.         {
  2601.           *oload_champ_bv = bv;
  2602.           oload_champ = 0;
  2603.         }
  2604.       else /* See whether current candidate is better or worse than
  2605.               previous best.  */
  2606.         switch (compare_badness (bv, *oload_champ_bv))
  2607.           {
  2608.           case 0:                /* Top two contenders are equally good.  */
  2609.             oload_ambiguous = 1;
  2610.             break;
  2611.           case 1:                /* Incomparable top contenders.  */
  2612.             oload_ambiguous = 2;
  2613.             break;
  2614.           case 2:                /* New champion, record details.  */
  2615.             *oload_champ_bv = bv;
  2616.             oload_ambiguous = 0;
  2617.             oload_champ = ix;
  2618.             break;
  2619.           case 3:
  2620.           default:
  2621.             break;
  2622.           }
  2623.       xfree (parm_types);
  2624.       if (overload_debug)
  2625.         {
  2626.           if (fns_ptr != NULL)
  2627.             fprintf_filtered (gdb_stderr,
  2628.                               "Overloaded method instance %s, # of parms %d\n",
  2629.                               fns_ptr[ix].physname, nparms);
  2630.           else if (xm_worker_vec != NULL)
  2631.             fprintf_filtered (gdb_stderr,
  2632.                               "Xmethod worker, # of parms %d\n",
  2633.                               nparms);
  2634.           else
  2635.             fprintf_filtered (gdb_stderr,
  2636.                               "Overloaded function instance "
  2637.                               "%s # of parms %d\n",
  2638.                               SYMBOL_DEMANGLED_NAME (oload_syms[ix]),
  2639.                               nparms);
  2640.           for (jj = 0; jj < nargs - static_offset; jj++)
  2641.             fprintf_filtered (gdb_stderr,
  2642.                               "...Badness @ %d : %d\n",
  2643.                               jj, bv->rank[jj].rank);
  2644.           fprintf_filtered (gdb_stderr, "Overload resolution "
  2645.                             "champion is %d, ambiguous? %d\n",
  2646.                             oload_champ, oload_ambiguous);
  2647.         }
  2648.     }

  2649.   return oload_champ;
  2650. }

  2651. /* Return 1 if we're looking at a static method, 0 if we're looking at
  2652.    a non-static method or a function that isn't a method.  */

  2653. static int
  2654. oload_method_static_p (struct fn_field *fns_ptr, int index)
  2655. {
  2656.   if (fns_ptr && index >= 0 && TYPE_FN_FIELD_STATIC_P (fns_ptr, index))
  2657.     return 1;
  2658.   else
  2659.     return 0;
  2660. }

  2661. /* Check how good an overload match OLOAD_CHAMP_BV represents.  */

  2662. static enum oload_classification
  2663. classify_oload_match (struct badness_vector *oload_champ_bv,
  2664.                       int nargs,
  2665.                       int static_offset)
  2666. {
  2667.   int ix;
  2668.   enum oload_classification worst = STANDARD;

  2669.   for (ix = 1; ix <= nargs - static_offset; ix++)
  2670.     {
  2671.       /* If this conversion is as bad as INCOMPATIBLE_TYPE_BADNESS
  2672.          or worse return INCOMPATIBLE.  */
  2673.       if (compare_ranks (oload_champ_bv->rank[ix],
  2674.                          INCOMPATIBLE_TYPE_BADNESS) <= 0)
  2675.         return INCOMPATIBLE;        /* Truly mismatched types.  */
  2676.       /* Otherwise If this conversion is as bad as
  2677.          NS_POINTER_CONVERSION_BADNESS or worse return NON_STANDARD.  */
  2678.       else if (compare_ranks (oload_champ_bv->rank[ix],
  2679.                               NS_POINTER_CONVERSION_BADNESS) <= 0)
  2680.         worst = NON_STANDARD;        /* Non-standard type conversions
  2681.                                    needed.  */
  2682.     }

  2683.   /* If no INCOMPATIBLE classification was found, return the worst one
  2684.      that was found (if any).  */
  2685.   return worst;
  2686. }

  2687. /* C++: return 1 is NAME is a legitimate name for the destructor of
  2688.    type TYPE.  If TYPE does not have a destructor, or if NAME is
  2689.    inappropriate for TYPE, an error is signaled.  Parameter TYPE should not yet
  2690.    have CHECK_TYPEDEF applied, this function will apply it itself.  */

  2691. int
  2692. destructor_name_p (const char *name, struct type *type)
  2693. {
  2694.   if (name[0] == '~')
  2695.     {
  2696.       const char *dname = type_name_no_tag_or_error (type);
  2697.       const char *cp = strchr (dname, '<');
  2698.       unsigned int len;

  2699.       /* Do not compare the template part for template classes.  */
  2700.       if (cp == NULL)
  2701.         len = strlen (dname);
  2702.       else
  2703.         len = cp - dname;
  2704.       if (strlen (name + 1) != len || strncmp (dname, name + 1, len) != 0)
  2705.         error (_("name of destructor must equal name of class"));
  2706.       else
  2707.         return 1;
  2708.     }
  2709.   return 0;
  2710. }

  2711. /* Find an enum constant named NAME in TYPETYPE must be an "enum
  2712.    class".  If the name is found, return a value representing it;
  2713.    otherwise throw an exception.  */

  2714. static struct value *
  2715. enum_constant_from_type (struct type *type, const char *name)
  2716. {
  2717.   int i;
  2718.   int name_len = strlen (name);

  2719.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_ENUM
  2720.               && TYPE_DECLARED_CLASS (type));

  2721.   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); ++i)
  2722.     {
  2723.       const char *fname = TYPE_FIELD_NAME (type, i);
  2724.       int len;

  2725.       if (TYPE_FIELD_LOC_KIND (type, i) != FIELD_LOC_KIND_ENUMVAL
  2726.           || fname == NULL)
  2727.         continue;

  2728.       /* Look for the trailing "::NAME", since enum class constant
  2729.          names are qualified here.  */
  2730.       len = strlen (fname);
  2731.       if (len + 2 >= name_len
  2732.           && fname[len - name_len - 2] == ':'
  2733.           && fname[len - name_len - 1] == ':'
  2734.           && strcmp (&fname[len - name_len], name) == 0)
  2735.         return value_from_longest (type, TYPE_FIELD_ENUMVAL (type, i));
  2736.     }

  2737.   error (_("no constant named \"%s\" in enum \"%s\""),
  2738.          name, TYPE_TAG_NAME (type));
  2739. }

  2740. /* C++: Given an aggregate type CURTYPE, and a member name NAME,
  2741.    return the appropriate member (or the address of the member, if
  2742.    WANT_ADDRESS).  This function is used to resolve user expressions
  2743.    of the form "DOMAIN::NAME".  For more details on what happens, see
  2744.    the comment before value_struct_elt_for_reference.  */

  2745. struct value *
  2746. value_aggregate_elt (struct type *curtype, const char *name,
  2747.                      struct type *expect_type, int want_address,
  2748.                      enum noside noside)
  2749. {
  2750.   switch (TYPE_CODE (curtype))
  2751.     {
  2752.     case TYPE_CODE_STRUCT:
  2753.     case TYPE_CODE_UNION:
  2754.       return value_struct_elt_for_reference (curtype, 0, curtype,
  2755.                                              name, expect_type,
  2756.                                              want_address, noside);
  2757.     case TYPE_CODE_NAMESPACE:
  2758.       return value_namespace_elt (curtype, name,
  2759.                                   want_address, noside);

  2760.     case TYPE_CODE_ENUM:
  2761.       return enum_constant_from_type (curtype, name);

  2762.     default:
  2763.       internal_error (__FILE__, __LINE__,
  2764.                       _("non-aggregate type in value_aggregate_elt"));
  2765.     }
  2766. }

  2767. /* Compares the two method/function types T1 and T2 for "equality"
  2768.    with respect to the methods' parameters.  If the types of the
  2769.    two parameter lists are the same, returns 1; 0 otherwise.  This
  2770.    comparison may ignore any artificial parameters in T1 if
  2771.    SKIP_ARTIFICIAL is non-zero.  This function will ALWAYS skip
  2772.    the first artificial parameter in T1, assumed to be a 'this' pointer.

  2773.    The type T2 is expected to have come from make_params (in eval.c).  */

  2774. static int
  2775. compare_parameters (struct type *t1, struct type *t2, int skip_artificial)
  2776. {
  2777.   int start = 0;

  2778.   if (TYPE_NFIELDS (t1) > 0 && TYPE_FIELD_ARTIFICIAL (t1, 0))
  2779.     ++start;

  2780.   /* If skipping artificial fields, find the first real field
  2781.      in T1.  */
  2782.   if (skip_artificial)
  2783.     {
  2784.       while (start < TYPE_NFIELDS (t1)
  2785.              && TYPE_FIELD_ARTIFICIAL (t1, start))
  2786.         ++start;
  2787.     }

  2788.   /* Now compare parameters.  */

  2789.   /* Special case: a method taking void.  T1 will contain no
  2790.      non-artificial fields, and T2 will contain TYPE_CODE_VOID.  */
  2791.   if ((TYPE_NFIELDS (t1) - start) == 0 && TYPE_NFIELDS (t2) == 1
  2792.       && TYPE_CODE (TYPE_FIELD_TYPE (t2, 0)) == TYPE_CODE_VOID)
  2793.     return 1;

  2794.   if ((TYPE_NFIELDS (t1) - start) == TYPE_NFIELDS (t2))
  2795.     {
  2796.       int i;

  2797.       for (i = 0; i < TYPE_NFIELDS (t2); ++i)
  2798.         {
  2799.           if (compare_ranks (rank_one_type (TYPE_FIELD_TYPE (t1, start + i),
  2800.                                             TYPE_FIELD_TYPE (t2, i), NULL),
  2801.                              EXACT_MATCH_BADNESS) != 0)
  2802.             return 0;
  2803.         }

  2804.       return 1;
  2805.     }

  2806.   return 0;
  2807. }

  2808. /* C++: Given an aggregate type CURTYPE, and a member name NAME,
  2809.    return the address of this member as a "pointer to member" type.
  2810.    If INTYPE is non-null, then it will be the type of the member we
  2811.    are looking for.  This will help us resolve "pointers to member
  2812.    functions".  This function is used to resolve user expressions of
  2813.    the form "DOMAIN::NAME".  */

  2814. static struct value *
  2815. value_struct_elt_for_reference (struct type *domain, int offset,
  2816.                                 struct type *curtype, const char *name,
  2817.                                 struct type *intype,
  2818.                                 int want_address,
  2819.                                 enum noside noside)
  2820. {
  2821.   struct type *t = curtype;
  2822.   int i;
  2823.   struct value *v, *result;

  2824.   if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  2825.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  2826.     error (_("Internal error: non-aggregate type "
  2827.              "to value_struct_elt_for_reference"));

  2828.   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
  2829.     {
  2830.       const char *t_field_name = TYPE_FIELD_NAME (t, i);

  2831.       if (t_field_name && strcmp (t_field_name, name) == 0)
  2832.         {
  2833.           if (field_is_static (&TYPE_FIELD (t, i)))
  2834.             {
  2835.               v = value_static_field (t, i);
  2836.               if (want_address)
  2837.                 v = value_addr (v);
  2838.               return v;
  2839.             }
  2840.           if (TYPE_FIELD_PACKED (t, i))
  2841.             error (_("pointers to bitfield members not allowed"));

  2842.           if (want_address)
  2843.             return value_from_longest
  2844.               (lookup_memberptr_type (TYPE_FIELD_TYPE (t, i), domain),
  2845.                offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
  2846.           else if (noside != EVAL_NORMAL)
  2847.             return allocate_value (TYPE_FIELD_TYPE (t, i));
  2848.           else
  2849.             {
  2850.               /* Try to evaluate NAME as a qualified name with implicit
  2851.                  this pointer.  In this case, attempt to return the
  2852.                  equivalent to `this->*(&TYPE::NAME)'.  */
  2853.               v = value_of_this_silent (current_language);
  2854.               if (v != NULL)
  2855.                 {
  2856.                   struct value *ptr;
  2857.                   long mem_offset;
  2858.                   struct type *type, *tmp;

  2859.                   ptr = value_aggregate_elt (domain, name, NULL, 1, noside);
  2860.                   type = check_typedef (value_type (ptr));
  2861.                   gdb_assert (type != NULL
  2862.                               && TYPE_CODE (type) == TYPE_CODE_MEMBERPTR);
  2863.                   tmp = lookup_pointer_type (TYPE_DOMAIN_TYPE (type));
  2864.                   v = value_cast_pointers (tmp, v, 1);
  2865.                   mem_offset = value_as_long (ptr);
  2866.                   tmp = lookup_pointer_type (TYPE_TARGET_TYPE (type));
  2867.                   result = value_from_pointer (tmp,
  2868.                                                value_as_long (v) + mem_offset);
  2869.                   return value_ind (result);
  2870.                 }

  2871.               error (_("Cannot reference non-static field \"%s\""), name);
  2872.             }
  2873.         }
  2874.     }

  2875.   /* C++: If it was not found as a data field, then try to return it
  2876.      as a pointer to a method.  */

  2877.   /* Perform all necessary dereferencing.  */
  2878.   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
  2879.     intype = TYPE_TARGET_TYPE (intype);

  2880.   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
  2881.     {
  2882.       const char *t_field_name = TYPE_FN_FIELDLIST_NAME (t, i);
  2883.       char dem_opname[64];

  2884.       if (strncmp (t_field_name, "__", 2) == 0
  2885.           || strncmp (t_field_name, "op", 2) == 0
  2886.           || strncmp (t_field_name, "type", 4) == 0)
  2887.         {
  2888.           if (cplus_demangle_opname (t_field_name,
  2889.                                      dem_opname, DMGL_ANSI))
  2890.             t_field_name = dem_opname;
  2891.           else if (cplus_demangle_opname (t_field_name,
  2892.                                           dem_opname, 0))
  2893.             t_field_name = dem_opname;
  2894.         }
  2895.       if (t_field_name && strcmp (t_field_name, name) == 0)
  2896.         {
  2897.           int j;
  2898.           int len = TYPE_FN_FIELDLIST_LENGTH (t, i);
  2899.           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);

  2900.           check_stub_method_group (t, i);

  2901.           if (intype)
  2902.             {
  2903.               for (j = 0; j < len; ++j)
  2904.                 {
  2905.                   if (compare_parameters (TYPE_FN_FIELD_TYPE (f, j), intype, 0)
  2906.                       || compare_parameters (TYPE_FN_FIELD_TYPE (f, j),
  2907.                                              intype, 1))
  2908.                     break;
  2909.                 }

  2910.               if (j == len)
  2911.                 error (_("no member function matches "
  2912.                          "that type instantiation"));
  2913.             }
  2914.           else
  2915.             {
  2916.               int ii;

  2917.               j = -1;
  2918.               for (ii = 0; ii < len; ++ii)
  2919.                 {
  2920.                   /* Skip artificial methods.  This is necessary if,
  2921.                      for example, the user wants to "print
  2922.                      subclass::subclass" with only one user-defined
  2923.                      constructor.  There is no ambiguity in this case.
  2924.                      We are careful here to allow artificial methods
  2925.                      if they are the unique result.  */
  2926.                   if (TYPE_FN_FIELD_ARTIFICIAL (f, ii))
  2927.                     {
  2928.                       if (j == -1)
  2929.                         j = ii;
  2930.                       continue;
  2931.                     }

  2932.                   /* Desired method is ambiguous if more than one
  2933.                      method is defined.  */
  2934.                   if (j != -1 && !TYPE_FN_FIELD_ARTIFICIAL (f, j))
  2935.                     error (_("non-unique member `%s' requires "
  2936.                              "type instantiation"), name);

  2937.                   j = ii;
  2938.                 }

  2939.               if (j == -1)
  2940.                 error (_("no matching member function"));
  2941.             }

  2942.           if (TYPE_FN_FIELD_STATIC_P (f, j))
  2943.             {
  2944.               struct symbol *s =
  2945.                 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  2946.                                0, VAR_DOMAIN, 0);

  2947.               if (s == NULL)
  2948.                 return NULL;

  2949.               if (want_address)
  2950.                 return value_addr (read_var_value (s, 0));
  2951.               else
  2952.                 return read_var_value (s, 0);
  2953.             }

  2954.           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  2955.             {
  2956.               if (want_address)
  2957.                 {
  2958.                   result = allocate_value
  2959.                     (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
  2960.                   cplus_make_method_ptr (value_type (result),
  2961.                                          value_contents_writeable (result),
  2962.                                          TYPE_FN_FIELD_VOFFSET (f, j), 1);
  2963.                 }
  2964.               else if (noside == EVAL_AVOID_SIDE_EFFECTS)
  2965.                 return allocate_value (TYPE_FN_FIELD_TYPE (f, j));
  2966.               else
  2967.                 error (_("Cannot reference virtual member function \"%s\""),
  2968.                        name);
  2969.             }
  2970.           else
  2971.             {
  2972.               struct symbol *s =
  2973.                 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  2974.                                0, VAR_DOMAIN, 0);

  2975.               if (s == NULL)
  2976.                 return NULL;

  2977.               v = read_var_value (s, 0);
  2978.               if (!want_address)
  2979.                 result = v;
  2980.               else
  2981.                 {
  2982.                   result = allocate_value (lookup_methodptr_type (TYPE_FN_FIELD_TYPE (f, j)));
  2983.                   cplus_make_method_ptr (value_type (result),
  2984.                                          value_contents_writeable (result),
  2985.                                          value_address (v), 0);
  2986.                 }
  2987.             }
  2988.           return result;
  2989.         }
  2990.     }
  2991.   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
  2992.     {
  2993.       struct value *v;
  2994.       int base_offset;

  2995.       if (BASETYPE_VIA_VIRTUAL (t, i))
  2996.         base_offset = 0;
  2997.       else
  2998.         base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
  2999.       v = value_struct_elt_for_reference (domain,
  3000.                                           offset + base_offset,
  3001.                                           TYPE_BASECLASS (t, i),
  3002.                                           name, intype,
  3003.                                           want_address, noside);
  3004.       if (v)
  3005.         return v;
  3006.     }

  3007.   /* As a last chance, pretend that CURTYPE is a namespace, and look
  3008.      it up that way; this (frequently) works for types nested inside
  3009.      classes.  */

  3010.   return value_maybe_namespace_elt (curtype, name,
  3011.                                     want_address, noside);
  3012. }

  3013. /* C++: Return the member NAME of the namespace given by the type
  3014.    CURTYPE.  */

  3015. static struct value *
  3016. value_namespace_elt (const struct type *curtype,
  3017.                      const char *name, int want_address,
  3018.                      enum noside noside)
  3019. {
  3020.   struct value *retval = value_maybe_namespace_elt (curtype, name,
  3021.                                                     want_address,
  3022.                                                     noside);

  3023.   if (retval == NULL)
  3024.     error (_("No symbol \"%s\" in namespace \"%s\"."),
  3025.            name, TYPE_TAG_NAME (curtype));

  3026.   return retval;
  3027. }

  3028. /* A helper function used by value_namespace_elt and
  3029.    value_struct_elt_for_reference.  It looks up NAME inside the
  3030.    context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
  3031.    is a class and NAME refers to a type in CURTYPE itself (as opposed
  3032.    to, say, some base class of CURTYPE).  */

  3033. static struct value *
  3034. value_maybe_namespace_elt (const struct type *curtype,
  3035.                            const char *name, int want_address,
  3036.                            enum noside noside)
  3037. {
  3038.   const char *namespace_name = TYPE_TAG_NAME (curtype);
  3039.   struct symbol *sym;
  3040.   struct value *result;

  3041.   sym = cp_lookup_symbol_namespace (namespace_name, name,
  3042.                                     get_selected_block (0), VAR_DOMAIN);

  3043.   if (sym == NULL)
  3044.     return NULL;
  3045.   else if ((noside == EVAL_AVOID_SIDE_EFFECTS)
  3046.            && (SYMBOL_CLASS (sym) == LOC_TYPEDEF))
  3047.     result = allocate_value (SYMBOL_TYPE (sym));
  3048.   else
  3049.     result = value_of_variable (sym, get_selected_block (0));

  3050.   if (want_address)
  3051.     result = value_addr (result);

  3052.   return result;
  3053. }

  3054. /* Given a pointer or a reference value V, find its real (RTTI) type.

  3055.    Other parameters FULL, TOP, USING_ENC as with value_rtti_type()
  3056.    and refer to the values computed for the object pointed to.  */

  3057. struct type *
  3058. value_rtti_indirect_type (struct value *v, int *full,
  3059.                           int *top, int *using_enc)
  3060. {
  3061.   struct value *target;
  3062.   struct type *type, *real_type, *target_type;

  3063.   type = value_type (v);
  3064.   type = check_typedef (type);
  3065.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  3066.     target = coerce_ref (v);
  3067.   else if (TYPE_CODE (type) == TYPE_CODE_PTR)
  3068.     target = value_ind (v);
  3069.   else
  3070.     return NULL;

  3071.   real_type = value_rtti_type (target, full, top, using_enc);

  3072.   if (real_type)
  3073.     {
  3074.       /* Copy qualifiers to the referenced object.  */
  3075.       target_type = value_type (target);
  3076.       real_type = make_cv_type (TYPE_CONST (target_type),
  3077.                                 TYPE_VOLATILE (target_type), real_type, NULL);
  3078.       if (TYPE_CODE (type) == TYPE_CODE_REF)
  3079.         real_type = lookup_reference_type (real_type);
  3080.       else if (TYPE_CODE (type) == TYPE_CODE_PTR)
  3081.         real_type = lookup_pointer_type (real_type);
  3082.       else
  3083.         internal_error (__FILE__, __LINE__, _("Unexpected value type."));

  3084.       /* Copy qualifiers to the pointer/reference.  */
  3085.       real_type = make_cv_type (TYPE_CONST (type), TYPE_VOLATILE (type),
  3086.                                 real_type, NULL);
  3087.     }

  3088.   return real_type;
  3089. }

  3090. /* Given a value pointed to by ARGP, check its real run-time type, and
  3091.    if that is different from the enclosing type, create a new value
  3092.    using the real run-time type as the enclosing type (and of the same
  3093.    type as ARGP) and return it, with the embedded offset adjusted to
  3094.    be the correct offset to the enclosed object.  RTYPE is the type,
  3095.    and XFULL, XTOP, and XUSING_ENC are the other parameters, computed
  3096.    by value_rtti_type().  If these are available, they can be supplied
  3097.    and a second call to value_rtti_type() is avoided.  (Pass RTYPE ==
  3098.    NULL if they're not available.  */

  3099. struct value *
  3100. value_full_object (struct value *argp,
  3101.                    struct type *rtype,
  3102.                    int xfull, int xtop,
  3103.                    int xusing_enc)
  3104. {
  3105.   struct type *real_type;
  3106.   int full = 0;
  3107.   int top = -1;
  3108.   int using_enc = 0;
  3109.   struct value *new_val;

  3110.   if (rtype)
  3111.     {
  3112.       real_type = rtype;
  3113.       full = xfull;
  3114.       top = xtop;
  3115.       using_enc = xusing_enc;
  3116.     }
  3117.   else
  3118.     real_type = value_rtti_type (argp, &full, &top, &using_enc);

  3119.   /* If no RTTI data, or if object is already complete, do nothing.  */
  3120.   if (!real_type || real_type == value_enclosing_type (argp))
  3121.     return argp;

  3122.   /* In a destructor we might see a real type that is a superclass of
  3123.      the object's type.  In this case it is better to leave the object
  3124.      as-is.  */
  3125.   if (full
  3126.       && TYPE_LENGTH (real_type) < TYPE_LENGTH (value_enclosing_type (argp)))
  3127.     return argp;

  3128.   /* If we have the full object, but for some reason the enclosing
  3129.      type is wrong, set it.  */
  3130.   /* pai: FIXME -- sounds iffy */
  3131.   if (full)
  3132.     {
  3133.       argp = value_copy (argp);
  3134.       set_value_enclosing_type (argp, real_type);
  3135.       return argp;
  3136.     }

  3137.   /* Check if object is in memory.  */
  3138.   if (VALUE_LVAL (argp) != lval_memory)
  3139.     {
  3140.       warning (_("Couldn't retrieve complete object of RTTI "
  3141.                  "type %s; object may be in register(s)."),
  3142.                TYPE_NAME (real_type));

  3143.       return argp;
  3144.     }

  3145.   /* All other cases -- retrieve the complete object.  */
  3146.   /* Go back by the computed top_offset from the beginning of the
  3147.      object, adjusting for the embedded offset of argp if that's what
  3148.      value_rtti_type used for its computation.  */
  3149.   new_val = value_at_lazy (real_type, value_address (argp) - top +
  3150.                            (using_enc ? 0 : value_embedded_offset (argp)));
  3151.   deprecated_set_value_type (new_val, value_type (argp));
  3152.   set_value_embedded_offset (new_val, (using_enc
  3153.                                        ? top + value_embedded_offset (argp)
  3154.                                        : top));
  3155.   return new_val;
  3156. }


  3157. /* Return the value of the local variable, if one exists.  Throw error
  3158.    otherwise, such as if the request is made in an inappropriate context.  */

  3159. struct value *
  3160. value_of_this (const struct language_defn *lang)
  3161. {
  3162.   struct symbol *sym;
  3163.   const struct block *b;
  3164.   struct frame_info *frame;

  3165.   if (!lang->la_name_of_this)
  3166.     error (_("no `this' in current language"));

  3167.   frame = get_selected_frame (_("no frame selected"));

  3168.   b = get_frame_block (frame, NULL);

  3169.   sym = lookup_language_this (lang, b);
  3170.   if (sym == NULL)
  3171.     error (_("current stack frame does not contain a variable named `%s'"),
  3172.            lang->la_name_of_this);

  3173.   return read_var_value (sym, frame);
  3174. }

  3175. /* Return the value of the local variable, if one exists.  Return NULL
  3176.    otherwise.  Never throw error.  */

  3177. struct value *
  3178. value_of_this_silent (const struct language_defn *lang)
  3179. {
  3180.   struct value *ret = NULL;
  3181.   volatile struct gdb_exception except;

  3182.   TRY_CATCH (except, RETURN_MASK_ERROR)
  3183.     {
  3184.       ret = value_of_this (lang);
  3185.     }

  3186.   return ret;
  3187. }

  3188. /* Create a slice (sub-string, sub-array) of ARRAY, that is LENGTH
  3189.    elements long, starting at LOWBOUND.  The result has the same lower
  3190.    bound as the original ARRAY.  */

  3191. struct value *
  3192. value_slice (struct value *array, int lowbound, int length)
  3193. {
  3194.   struct type *slice_range_type, *slice_type, *range_type;
  3195.   LONGEST lowerbound, upperbound;
  3196.   struct value *slice;
  3197.   struct type *array_type;

  3198.   array_type = check_typedef (value_type (array));
  3199.   if (TYPE_CODE (array_type) != TYPE_CODE_ARRAY
  3200.       && TYPE_CODE (array_type) != TYPE_CODE_STRING)
  3201.     error (_("cannot take slice of non-array"));

  3202.   range_type = TYPE_INDEX_TYPE (array_type);
  3203.   if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
  3204.     error (_("slice from bad array or bitstring"));

  3205.   if (lowbound < lowerbound || length < 0
  3206.       || lowbound + length - 1 > upperbound)
  3207.     error (_("slice out of range"));

  3208.   /* FIXME-type-allocation: need a way to free this type when we are
  3209.      done with it.  */
  3210.   slice_range_type = create_static_range_type ((struct type *) NULL,
  3211.                                                TYPE_TARGET_TYPE (range_type),
  3212.                                                lowbound,
  3213.                                                lowbound + length - 1);

  3214.   {
  3215.     struct type *element_type = TYPE_TARGET_TYPE (array_type);
  3216.     LONGEST offset
  3217.       = (lowbound - lowerbound) * TYPE_LENGTH (check_typedef (element_type));

  3218.     slice_type = create_array_type ((struct type *) NULL,
  3219.                                     element_type,
  3220.                                     slice_range_type);
  3221.     TYPE_CODE (slice_type) = TYPE_CODE (array_type);

  3222.     if (VALUE_LVAL (array) == lval_memory && value_lazy (array))
  3223.       slice = allocate_value_lazy (slice_type);
  3224.     else
  3225.       {
  3226.         slice = allocate_value (slice_type);
  3227.         value_contents_copy (slice, 0, array, offset,
  3228.                              TYPE_LENGTH (slice_type));
  3229.       }

  3230.     set_value_component_location (slice, array);
  3231.     VALUE_FRAME_ID (slice) = VALUE_FRAME_ID (array);
  3232.     set_value_offset (slice, value_offset (array) + offset);
  3233.   }

  3234.   return slice;
  3235. }

  3236. /* Create a value for a FORTRAN complex number.  Currently most of the
  3237.    time values are coerced to COMPLEX*16 (i.e. a complex number
  3238.    composed of 2 doubles.  This really should be a smarter routine
  3239.    that figures out precision inteligently as opposed to assuming
  3240.    doubles.  FIXME: fmb  */

  3241. struct value *
  3242. value_literal_complex (struct value *arg1,
  3243.                        struct value *arg2,
  3244.                        struct type *type)
  3245. {
  3246.   struct value *val;
  3247.   struct type *real_type = TYPE_TARGET_TYPE (type);

  3248.   val = allocate_value (type);
  3249.   arg1 = value_cast (real_type, arg1);
  3250.   arg2 = value_cast (real_type, arg2);

  3251.   memcpy (value_contents_raw (val),
  3252.           value_contents (arg1), TYPE_LENGTH (real_type));
  3253.   memcpy (value_contents_raw (val) + TYPE_LENGTH (real_type),
  3254.           value_contents (arg2), TYPE_LENGTH (real_type));
  3255.   return val;
  3256. }

  3257. /* Cast a value into the appropriate complex data type.  */

  3258. static struct value *
  3259. cast_into_complex (struct type *type, struct value *val)
  3260. {
  3261.   struct type *real_type = TYPE_TARGET_TYPE (type);

  3262.   if (TYPE_CODE (value_type (val)) == TYPE_CODE_COMPLEX)
  3263.     {
  3264.       struct type *val_real_type = TYPE_TARGET_TYPE (value_type (val));
  3265.       struct value *re_val = allocate_value (val_real_type);
  3266.       struct value *im_val = allocate_value (val_real_type);

  3267.       memcpy (value_contents_raw (re_val),
  3268.               value_contents (val), TYPE_LENGTH (val_real_type));
  3269.       memcpy (value_contents_raw (im_val),
  3270.               value_contents (val) + TYPE_LENGTH (val_real_type),
  3271.               TYPE_LENGTH (val_real_type));

  3272.       return value_literal_complex (re_val, im_val, type);
  3273.     }
  3274.   else if (TYPE_CODE (value_type (val)) == TYPE_CODE_FLT
  3275.            || TYPE_CODE (value_type (val)) == TYPE_CODE_INT)
  3276.     return value_literal_complex (val,
  3277.                                   value_zero (real_type, not_lval),
  3278.                                   type);
  3279.   else
  3280.     error (_("cannot cast non-number to complex"));
  3281. }

  3282. void
  3283. _initialize_valops (void)
  3284. {
  3285.   add_setshow_boolean_cmd ("overload-resolution", class_support,
  3286.                            &overload_resolution, _("\
  3287. Set overload resolution in evaluating C++ functions."), _("\
  3288. Show overload resolution in evaluating C++ functions."),
  3289.                            NULL, NULL,
  3290.                            show_overload_resolution,
  3291.                            &setlist, &showlist);
  3292.   overload_resolution = 1;
  3293. }