home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / gdb / valops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  48.1 KB  |  1,692 lines

  1. /* Perform non-arithmetic operations on values, for GDB.
  2.    Copyright 1986, 1987, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "value.h"
  24. #include "frame.h"
  25. #include "inferior.h"
  26. #include "gdbcore.h"
  27. #include "target.h"
  28. #include "demangle.h"
  29.  
  30. #include <errno.h>
  31.  
  32. /* Local functions.  */
  33.  
  34. static int
  35. typecmp PARAMS ((int staticp, struct type *t1[], value t2[]));
  36.  
  37. static CORE_ADDR
  38. find_function_addr PARAMS ((value, struct type **));
  39.  
  40. static CORE_ADDR
  41. value_push PARAMS ((CORE_ADDR, value));
  42.  
  43. static CORE_ADDR
  44. value_arg_push PARAMS ((CORE_ADDR, value));
  45.  
  46. static value
  47. search_struct_field PARAMS ((char *, value, int, struct type *, int));
  48.  
  49. static value
  50. search_struct_method PARAMS ((char *, value *, value *, int, int *,
  51.                   struct type *));
  52.  
  53. static int
  54. check_field_in PARAMS ((struct type *, const char *));
  55.  
  56. static CORE_ADDR
  57. allocate_space_in_inferior PARAMS ((int));
  58.  
  59.  
  60. /* Allocate NBYTES of space in the inferior using the inferior's malloc
  61.    and return a value that is a pointer to the allocated space. */
  62.  
  63. static CORE_ADDR
  64. allocate_space_in_inferior (len)
  65.      int len;
  66. {
  67.   register value val;
  68.   register struct symbol *sym;
  69.   struct minimal_symbol *msymbol;
  70.   struct type *type;
  71.   value blocklen;
  72.   LONGEST maddr;
  73.  
  74.   /* Find the address of malloc in the inferior.  */
  75.  
  76.   sym = lookup_symbol ("malloc", 0, VAR_NAMESPACE, 0, NULL);
  77.   if (sym != NULL)
  78.     {
  79.       if (SYMBOL_CLASS (sym) != LOC_BLOCK)
  80.     {
  81.       error ("\"malloc\" exists in this program but is not a function.");
  82.     }
  83.       val = value_of_variable (sym);
  84.     }
  85.   else
  86.     {
  87.       msymbol = lookup_minimal_symbol ("malloc", (struct objfile *) NULL);
  88.       if (msymbol != NULL)
  89.     {
  90.       type = lookup_pointer_type (builtin_type_char);
  91.       type = lookup_function_type (type);
  92.       type = lookup_pointer_type (type);
  93.       maddr = (LONGEST) SYMBOL_VALUE_ADDRESS (msymbol);
  94.       val = value_from_longest (type, maddr);
  95.     }
  96.       else
  97.     {
  98.       error ("evaluation of this expression requires the program to have a function \"malloc\".");
  99.     }
  100.     }
  101.  
  102.   blocklen = value_from_longest (builtin_type_int, (LONGEST) len);
  103.   val = call_function_by_hand (val, 1, &blocklen);
  104.   if (value_logical_not (val))
  105.     {
  106.       error ("No memory available to program.");
  107.     }
  108.   return (value_as_long (val));
  109. }
  110.  
  111. /* Cast value ARG2 to type TYPE and return as a value.
  112.    More general than a C cast: accepts any two types of the same length,
  113.    and if ARG2 is an lvalue it can be cast into anything at all.  */
  114. /* In C++, casts may change pointer or object representations.  */
  115.  
  116. value
  117. value_cast (type, arg2)
  118.      struct type *type;
  119.      register value arg2;
  120. {
  121.   register enum type_code code1;
  122.   register enum type_code code2;
  123.   register int scalar;
  124.  
  125.   /* Coerce arrays but not enums.  Enums will work as-is
  126.      and coercing them would cause an infinite recursion.  */
  127.   if (TYPE_CODE (VALUE_TYPE (arg2)) != TYPE_CODE_ENUM)
  128.     COERCE_ARRAY (arg2);
  129.  
  130.   code1 = TYPE_CODE (type);
  131.   code2 = TYPE_CODE (VALUE_TYPE (arg2));
  132.   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
  133.         || code2 == TYPE_CODE_ENUM);
  134.  
  135.   if (   code1 == TYPE_CODE_STRUCT
  136.       && code2 == TYPE_CODE_STRUCT
  137.       && TYPE_NAME (type) != 0)
  138.     {
  139.       /* Look in the type of the source to see if it contains the
  140.      type of the target as a superclass.  If so, we'll need to
  141.      offset the object in addition to changing its type.  */
  142.       value v = search_struct_field (type_name_no_tag (type),
  143.                      arg2, 0, VALUE_TYPE (arg2), 1);
  144.       if (v)
  145.     {
  146.       VALUE_TYPE (v) = type;
  147.       return v;
  148.     }
  149.     }
  150.   if (code1 == TYPE_CODE_FLT && scalar)
  151.     return value_from_double (type, value_as_double (arg2));
  152.   else if ((code1 == TYPE_CODE_INT || code1 == TYPE_CODE_ENUM)
  153.        && (scalar || code2 == TYPE_CODE_PTR))
  154.     return value_from_longest (type, value_as_long (arg2));
  155.   else if (TYPE_LENGTH (type) == TYPE_LENGTH (VALUE_TYPE (arg2)))
  156.     {
  157.       if (code1 == TYPE_CODE_PTR && code2 == TYPE_CODE_PTR)
  158.     {
  159.       /* Look in the type of the source to see if it contains the
  160.          type of the target as a superclass.  If so, we'll need to
  161.          offset the pointer rather than just change its type.  */
  162.       struct type *t1 = TYPE_TARGET_TYPE (type);
  163.       struct type *t2 = TYPE_TARGET_TYPE (VALUE_TYPE (arg2));
  164.       if (   TYPE_CODE (t1) == TYPE_CODE_STRUCT
  165.           && TYPE_CODE (t2) == TYPE_CODE_STRUCT
  166.           && TYPE_NAME (t1) != 0) /* if name unknown, can't have supercl */
  167.         {
  168.           value v = search_struct_field (type_name_no_tag (t1),
  169.                          value_ind (arg2), 0, t2, 1);
  170.           if (v)
  171.         {
  172.           v = value_addr (v);
  173.           VALUE_TYPE (v) = type;
  174.           return v;
  175.         }
  176.         }
  177.       /* No superclass found, just fall through to change ptr type.  */
  178.     }
  179.       VALUE_TYPE (arg2) = type;
  180.       return arg2;
  181.     }
  182.   else if (VALUE_LVAL (arg2) == lval_memory)
  183.     {
  184.       return value_at_lazy (type, VALUE_ADDRESS (arg2) + VALUE_OFFSET (arg2));
  185.     }
  186.   else if (code1 == TYPE_CODE_VOID)
  187.     {
  188.       return value_zero (builtin_type_void, not_lval);
  189.     }
  190.   else
  191.     {
  192.       error ("Invalid cast.");
  193.       return 0;
  194.     }
  195. }
  196.  
  197. /* Create a value of type TYPE that is zero, and return it.  */
  198.  
  199. value
  200. value_zero (type, lv)
  201.      struct type *type;
  202.      enum lval_type lv;
  203. {
  204.   register value val = allocate_value (type);
  205.  
  206.   memset (VALUE_CONTENTS (val), 0, TYPE_LENGTH (type));
  207.   VALUE_LVAL (val) = lv;
  208.  
  209.   return val;
  210. }
  211.  
  212. /* Return a value with type TYPE located at ADDR.  
  213.  
  214.    Call value_at only if the data needs to be fetched immediately;
  215.    if we can be 'lazy' and defer the fetch, perhaps indefinately, call
  216.    value_at_lazy instead.  value_at_lazy simply records the address of
  217.    the data and sets the lazy-evaluation-required flag.  The lazy flag 
  218.    is tested in the VALUE_CONTENTS macro, which is used if and when 
  219.    the contents are actually required.  */
  220.  
  221. value
  222. value_at (type, addr)
  223.      struct type *type;
  224.      CORE_ADDR addr;
  225. {
  226.   register value val = allocate_value (type);
  227.  
  228.   read_memory (addr, VALUE_CONTENTS_RAW (val), TYPE_LENGTH (type));
  229.  
  230.   VALUE_LVAL (val) = lval_memory;
  231.   VALUE_ADDRESS (val) = addr;
  232.  
  233.   return val;
  234. }
  235.  
  236. /* Return a lazy value with type TYPE located at ADDR (cf. value_at).  */
  237.  
  238. value
  239. value_at_lazy (type, addr)
  240.      struct type *type;
  241.      CORE_ADDR addr;
  242. {
  243.   register value val = allocate_value (type);
  244.  
  245.   VALUE_LVAL (val) = lval_memory;
  246.   VALUE_ADDRESS (val) = addr;
  247.   VALUE_LAZY (val) = 1;
  248.  
  249.   return val;
  250. }
  251.  
  252. /* Called only from the VALUE_CONTENTS macro, if the current data for
  253.    a variable needs to be loaded into VALUE_CONTENTS(VAL).  Fetches the
  254.    data from the user's process, and clears the lazy flag to indicate
  255.    that the data in the buffer is valid.
  256.  
  257.    If the value is zero-length, we avoid calling read_memory, which would
  258.    abort.  We mark the value as fetched anyway -- all 0 bytes of it.
  259.  
  260.    This function returns a value because it is used in the VALUE_CONTENTS
  261.    macro as part of an expression, where a void would not work.  The
  262.    value is ignored.  */
  263.  
  264. int
  265. value_fetch_lazy (val)
  266.      register value val;
  267. {
  268.   CORE_ADDR addr = VALUE_ADDRESS (val) + VALUE_OFFSET (val);
  269.  
  270.   if (TYPE_LENGTH (VALUE_TYPE (val)))
  271.     read_memory (addr, VALUE_CONTENTS_RAW (val), 
  272.              TYPE_LENGTH (VALUE_TYPE (val)));
  273.   VALUE_LAZY (val) = 0;
  274.   return 0;
  275. }
  276.  
  277.  
  278. /* Store the contents of FROMVAL into the location of TOVAL.
  279.    Return a new value with the location of TOVAL and contents of FROMVAL.  */
  280.  
  281. value
  282. value_assign (toval, fromval)
  283.      register value toval, fromval;
  284. {
  285.   register struct type *type = VALUE_TYPE (toval);
  286.   register value val;
  287.   char raw_buffer[MAX_REGISTER_RAW_SIZE];
  288.   char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  289.   int use_buffer = 0;
  290.  
  291.   COERCE_ARRAY (fromval);
  292.   COERCE_REF (toval);
  293.  
  294.   if (VALUE_LVAL (toval) != lval_internalvar)
  295.     fromval = value_cast (type, fromval);
  296.  
  297.   /* If TOVAL is a special machine register requiring conversion
  298.      of program values to a special raw format,
  299.      convert FROMVAL's contents now, with result in `raw_buffer',
  300.      and set USE_BUFFER to the number of bytes to write.  */
  301.  
  302.   if (VALUE_REGNO (toval) >= 0
  303.       && REGISTER_CONVERTIBLE (VALUE_REGNO (toval)))
  304.     {
  305.       int regno = VALUE_REGNO (toval);
  306.       if (VALUE_TYPE (fromval) != REGISTER_VIRTUAL_TYPE (regno))
  307.     fromval = value_cast (REGISTER_VIRTUAL_TYPE (regno), fromval);
  308.       memcpy (virtual_buffer, VALUE_CONTENTS (fromval),
  309.          REGISTER_VIRTUAL_SIZE (regno));
  310.       REGISTER_CONVERT_TO_RAW (regno, virtual_buffer, raw_buffer);
  311.       use_buffer = REGISTER_RAW_SIZE (regno);
  312.     }
  313.  
  314.   switch (VALUE_LVAL (toval))
  315.     {
  316.     case lval_internalvar:
  317.       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
  318.       break;
  319.  
  320.     case lval_internalvar_component:
  321.       set_internalvar_component (VALUE_INTERNALVAR (toval),
  322.                  VALUE_OFFSET (toval),
  323.                  VALUE_BITPOS (toval),
  324.                  VALUE_BITSIZE (toval),
  325.                  fromval);
  326.       break;
  327.  
  328.     case lval_memory:
  329.       if (VALUE_BITSIZE (toval))
  330.     {
  331.       int v;        /* FIXME, this won't work for large bitfields */
  332.       read_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  333.                (char *) &v, sizeof v);
  334.       modify_field ((char *) &v, (int) value_as_long (fromval),
  335.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  336.       write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  337.             (char *)&v, sizeof v);
  338.     }
  339.       else if (use_buffer)
  340.     write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  341.               raw_buffer, use_buffer);
  342.       else
  343.     write_memory (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  344.               VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  345.       break;
  346.  
  347.     case lval_register:
  348.       if (VALUE_BITSIZE (toval))
  349.     {
  350.       int v;
  351.  
  352.       read_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  353.                    (char *) &v, sizeof v);
  354.       modify_field ((char *) &v, (int) value_as_long (fromval),
  355.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  356.       write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  357.                 (char *) &v, sizeof v);
  358.     }
  359.       else if (use_buffer)
  360.     write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  361.                   raw_buffer, use_buffer);
  362.       else
  363.     write_register_bytes (VALUE_ADDRESS (toval) + VALUE_OFFSET (toval),
  364.                   VALUE_CONTENTS (fromval), TYPE_LENGTH (type));
  365.       break;
  366.  
  367.     case lval_reg_frame_relative:
  368.       {
  369.     /* value is stored in a series of registers in the frame
  370.        specified by the structure.  Copy that value out, modify
  371.        it, and copy it back in.  */
  372.     int amount_to_copy = (VALUE_BITSIZE (toval) ? 1 : TYPE_LENGTH (type));
  373.     int reg_size = REGISTER_RAW_SIZE (VALUE_FRAME_REGNUM (toval));
  374.     int byte_offset = VALUE_OFFSET (toval) % reg_size;
  375.     int reg_offset = VALUE_OFFSET (toval) / reg_size;
  376.     int amount_copied;
  377.     char *buffer = (char *) alloca (amount_to_copy);
  378.     int regno;
  379.     FRAME frame;
  380.  
  381.     /* Figure out which frame this is in currently.  */
  382.     for (frame = get_current_frame ();
  383.          frame && FRAME_FP (frame) != VALUE_FRAME (toval);
  384.          frame = get_prev_frame (frame))
  385.       ;
  386.  
  387.     if (!frame)
  388.       error ("Value being assigned to is no longer active.");
  389.  
  390.     amount_to_copy += (reg_size - amount_to_copy % reg_size);
  391.  
  392.     /* Copy it out.  */
  393.     for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
  394.           amount_copied = 0);
  395.          amount_copied < amount_to_copy;
  396.          amount_copied += reg_size, regno++)
  397.       {
  398.         get_saved_register (buffer + amount_copied,
  399.                 (int *)NULL, (CORE_ADDR *)NULL,
  400.                 frame, regno, (enum lval_type *)NULL);
  401.       }
  402.  
  403.     /* Modify what needs to be modified.  */
  404.     if (VALUE_BITSIZE (toval))
  405.       modify_field (buffer + byte_offset,
  406.             (int) value_as_long (fromval),
  407.             VALUE_BITPOS (toval), VALUE_BITSIZE (toval));
  408.     else if (use_buffer)
  409.       memcpy (buffer + byte_offset, raw_buffer, use_buffer);
  410.     else
  411.       memcpy (buffer + byte_offset, VALUE_CONTENTS (fromval),
  412.           TYPE_LENGTH (type));
  413.  
  414.     /* Copy it back.  */
  415.     for ((regno = VALUE_FRAME_REGNUM (toval) + reg_offset,
  416.           amount_copied = 0);
  417.          amount_copied < amount_to_copy;
  418.          amount_copied += reg_size, regno++)
  419.       {
  420.         enum lval_type lval;
  421.         CORE_ADDR addr;
  422.         int optim;
  423.  
  424.         /* Just find out where to put it.  */
  425.         get_saved_register ((char *)NULL,
  426.                     &optim, &addr, frame, regno, &lval);
  427.         
  428.         if (optim)
  429.           error ("Attempt to assign to a value that was optimized out.");
  430.         if (lval == lval_memory)
  431.           write_memory (addr, buffer + amount_copied, reg_size);
  432.         else if (lval == lval_register)
  433.           write_register_bytes (addr, buffer + amount_copied, reg_size);
  434.         else
  435.           error ("Attempt to assign to an unmodifiable value.");
  436.       }
  437.       }
  438.       break;
  439.     
  440.  
  441.     default:
  442.       error ("Left side of = operation is not an lvalue.");
  443.     }
  444.  
  445.   /* Return a value just like TOVAL except with the contents of FROMVAL
  446.      (except in the case of the type if TOVAL is an internalvar).  */
  447.  
  448.   if (VALUE_LVAL (toval) == lval_internalvar
  449.       || VALUE_LVAL (toval) == lval_internalvar_component)
  450.     {
  451.       type = VALUE_TYPE (fromval);
  452.     }
  453.  
  454.   val = allocate_value (type);
  455.   memcpy (val, toval, VALUE_CONTENTS_RAW (val) - (char *) val);
  456.   memcpy (VALUE_CONTENTS_RAW (val), VALUE_CONTENTS (fromval),
  457.       TYPE_LENGTH (type));
  458.   VALUE_TYPE (val) = type;
  459.   
  460.   return val;
  461. }
  462.  
  463. /* Extend a value VAL to COUNT repetitions of its type.  */
  464.  
  465. value
  466. value_repeat (arg1, count)
  467.      value arg1;
  468.      int count;
  469. {
  470.   register value val;
  471.  
  472.   if (VALUE_LVAL (arg1) != lval_memory)
  473.     error ("Only values in memory can be extended with '@'.");
  474.   if (count < 1)
  475.     error ("Invalid number %d of repetitions.", count);
  476.  
  477.   val = allocate_repeat_value (VALUE_TYPE (arg1), count);
  478.  
  479.   read_memory (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1),
  480.            VALUE_CONTENTS_RAW (val),
  481.            TYPE_LENGTH (VALUE_TYPE (val)) * count);
  482.   VALUE_LVAL (val) = lval_memory;
  483.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1);
  484.  
  485.   return val;
  486. }
  487.  
  488. value
  489. value_of_variable (var)
  490.      struct symbol *var;
  491. {
  492.   value val;
  493.  
  494.   val = read_var_value (var, (FRAME) 0);
  495.   if (val == 0)
  496.     error ("Address of symbol \"%s\" is unknown.", SYMBOL_SOURCE_NAME (var));
  497.   return val;
  498. }
  499.  
  500. /* Given a value which is an array, return a value which is a pointer to its
  501.    first element, regardless of whether or not the array has a nonzero lower
  502.    bound.
  503.  
  504.    FIXME:  A previous comment here indicated that this routine should be
  505.    substracting the array's lower bound.  It's not clear to me that this
  506.    is correct.  Given an array subscripting operation, it would certainly
  507.    work to do the adjustment here, essentially computing:
  508.  
  509.    (&array[0] - (lowerbound * sizeof array[0])) + (index * sizeof array[0])
  510.  
  511.    However I believe a more appropriate and logical place to account for
  512.    the lower bound is to do so in value_subscript, essentially computing:
  513.  
  514.    (&array[0] + ((index - lowerbound) * sizeof array[0]))
  515.  
  516.    As further evidence consider what would happen with operations other
  517.    than array subscripting, where the caller would get back a value that
  518.    had an address somewhere before the actual first element of the array,
  519.    and the information about the lower bound would be lost because of
  520.    the coercion to pointer type.
  521.    */
  522.  
  523. value
  524. value_coerce_array (arg1)
  525.      value arg1;
  526. {
  527.   register struct type *type;
  528.  
  529.   if (VALUE_LVAL (arg1) != lval_memory)
  530.     error ("Attempt to take address of value not located in memory.");
  531.  
  532.   /* Get type of elements.  */
  533.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_ARRAY)
  534.     type = TYPE_TARGET_TYPE (VALUE_TYPE (arg1));
  535.   else
  536.     /* A phony array made by value_repeat.
  537.        Its type is the type of the elements, not an array type.  */
  538.     type = VALUE_TYPE (arg1);
  539.  
  540.   return value_from_longest (lookup_pointer_type (type),
  541.                (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
  542. }
  543.  
  544. /* Given a value which is a function, return a value which is a pointer
  545.    to it.  */
  546.  
  547. value
  548. value_coerce_function (arg1)
  549.      value arg1;
  550. {
  551.  
  552.   if (VALUE_LVAL (arg1) != lval_memory)
  553.     error ("Attempt to take address of value not located in memory.");
  554.  
  555.   return value_from_longest (lookup_pointer_type (VALUE_TYPE (arg1)),
  556.         (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
  557. }  
  558.  
  559. /* Return a pointer value for the object for which ARG1 is the contents.  */
  560.  
  561. value
  562. value_addr (arg1)
  563.      value arg1;
  564. {
  565.   struct type *type = VALUE_TYPE (arg1);
  566.   if (TYPE_CODE (type) == TYPE_CODE_REF)
  567.     {
  568.       /* Copy the value, but change the type from (T&) to (T*).
  569.      We keep the same location information, which is efficient,
  570.      and allows &(&X) to get the location containing the reference. */
  571.       value arg2 = value_copy (arg1);
  572.       VALUE_TYPE (arg2) = lookup_pointer_type (TYPE_TARGET_TYPE (type));
  573.       return arg2;
  574.     }
  575.   if (VALUE_REPEATED (arg1)
  576.       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  577.     return value_coerce_array (arg1);
  578.   if (TYPE_CODE (type) == TYPE_CODE_FUNC)
  579.     return value_coerce_function (arg1);
  580.  
  581.   if (VALUE_LVAL (arg1) != lval_memory)
  582.     error ("Attempt to take address of value not located in memory.");
  583.  
  584.   return value_from_longest (lookup_pointer_type (type),
  585.         (LONGEST) (VALUE_ADDRESS (arg1) + VALUE_OFFSET (arg1)));
  586. }
  587.  
  588. /* Given a value of a pointer type, apply the C unary * operator to it.  */
  589.  
  590. value
  591. value_ind (arg1)
  592.      value arg1;
  593. {
  594.   COERCE_ARRAY (arg1);
  595.  
  596.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_MEMBER)
  597.     error ("not implemented: member types in value_ind");
  598.  
  599.   /* Allow * on an integer so we can cast it to whatever we want.
  600.      This returns an int, which seems like the most C-like thing
  601.      to do.  "long long" variables are rare enough that
  602.      BUILTIN_TYPE_LONGEST would seem to be a mistake.  */
  603.   if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_INT)
  604.     return value_at (builtin_type_int,
  605.              (CORE_ADDR) value_as_long (arg1));
  606.   else if (TYPE_CODE (VALUE_TYPE (arg1)) == TYPE_CODE_PTR)
  607.     return value_at_lazy (TYPE_TARGET_TYPE (VALUE_TYPE (arg1)),
  608.               value_as_pointer (arg1));
  609.   error ("Attempt to take contents of a non-pointer value.");
  610.   return 0;  /* For lint -- never reached */
  611. }
  612.  
  613. /* Pushing small parts of stack frames.  */
  614.  
  615. /* Push one word (the size of object that a register holds).  */
  616.  
  617. CORE_ADDR
  618. push_word (sp, buffer)
  619.      CORE_ADDR sp;
  620.      REGISTER_TYPE buffer;
  621. {
  622.   register int len = sizeof (REGISTER_TYPE);
  623.  
  624.   SWAP_TARGET_AND_HOST (&buffer, len);
  625. #if 1 INNER_THAN 2
  626.   sp -= len;
  627.   write_memory (sp, (char *)&buffer, len);
  628. #else /* stack grows upward */
  629.   write_memory (sp, (char *)&buffer, len);
  630.   sp += len;
  631. #endif /* stack grows upward */
  632.  
  633.   return sp;
  634. }
  635.  
  636. /* Push LEN bytes with data at BUFFER.  */
  637.  
  638. CORE_ADDR
  639. push_bytes (sp, buffer, len)
  640.      CORE_ADDR sp;
  641.      char *buffer;
  642.      int len;
  643. {
  644. #if 1 INNER_THAN 2
  645.   sp -= len;
  646.   write_memory (sp, buffer, len);
  647. #else /* stack grows upward */
  648.   write_memory (sp, buffer, len);
  649.   sp += len;
  650. #endif /* stack grows upward */
  651.  
  652.   return sp;
  653. }
  654.  
  655. /* Push onto the stack the specified value VALUE.  */
  656.  
  657. static CORE_ADDR
  658. value_push (sp, arg)
  659.      register CORE_ADDR sp;
  660.      value arg;
  661. {
  662.   register int len = TYPE_LENGTH (VALUE_TYPE (arg));
  663.  
  664. #if 1 INNER_THAN 2
  665.   sp -= len;
  666.   write_memory (sp, VALUE_CONTENTS (arg), len);
  667. #else /* stack grows upward */
  668.   write_memory (sp, VALUE_CONTENTS (arg), len);
  669.   sp += len;
  670. #endif /* stack grows upward */
  671.  
  672.   return sp;
  673. }
  674.  
  675. /* Perform the standard coercions that are specified
  676.    for arguments to be passed to C functions.  */
  677.  
  678. value
  679. value_arg_coerce (arg)
  680.      value arg;
  681. {
  682.   register struct type *type;
  683.  
  684.   COERCE_ENUM (arg);
  685. #if 1    /* FIXME:  This is only a temporary patch.  -fnf */
  686.   if (VALUE_REPEATED (arg)
  687.       || TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_ARRAY)
  688.     arg = value_coerce_array (arg);
  689.   if (TYPE_CODE (VALUE_TYPE (arg)) == TYPE_CODE_FUNC)
  690.     arg = value_coerce_function (arg);
  691. #endif
  692.  
  693.   type = VALUE_TYPE (arg);
  694.  
  695.   if (TYPE_CODE (type) == TYPE_CODE_INT
  696.       && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_int))
  697.     return value_cast (builtin_type_int, arg);
  698.  
  699.   if (TYPE_CODE (type) == TYPE_CODE_FLT
  700.       && TYPE_LENGTH (type) < TYPE_LENGTH (builtin_type_double))
  701.     return value_cast (builtin_type_double, arg);
  702.  
  703.   return arg;
  704. }
  705.  
  706. /* Push the value ARG, first coercing it as an argument
  707.    to a C function.  */
  708.  
  709. static CORE_ADDR
  710. value_arg_push (sp, arg)
  711.      register CORE_ADDR sp;
  712.      value arg;
  713. {
  714.   return value_push (sp, value_arg_coerce (arg));
  715. }
  716.  
  717. /* Determine a function's address and its return type from its value. 
  718.    Calls error() if the function is not valid for calling.  */
  719.  
  720. static CORE_ADDR
  721. find_function_addr (function, retval_type)
  722.      value function;
  723.      struct type **retval_type;
  724. {
  725.   register struct type *ftype = VALUE_TYPE (function);
  726.   register enum type_code code = TYPE_CODE (ftype);
  727.   struct type *value_type;
  728.   CORE_ADDR funaddr;
  729.  
  730.   /* If it's a member function, just look at the function
  731.      part of it.  */
  732.  
  733.   /* Determine address to call.  */
  734.   if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
  735.     {
  736.       funaddr = VALUE_ADDRESS (function);
  737.       value_type = TYPE_TARGET_TYPE (ftype);
  738.     }
  739.   else if (code == TYPE_CODE_PTR)
  740.     {
  741.       funaddr = value_as_pointer (function);
  742.       if (TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_FUNC
  743.       || TYPE_CODE (TYPE_TARGET_TYPE (ftype)) == TYPE_CODE_METHOD)
  744.     value_type = TYPE_TARGET_TYPE (TYPE_TARGET_TYPE (ftype));
  745.       else
  746.     value_type = builtin_type_int;
  747.     }
  748.   else if (code == TYPE_CODE_INT)
  749.     {
  750.       /* Handle the case of functions lacking debugging info.
  751.      Their values are characters since their addresses are char */
  752.       if (TYPE_LENGTH (ftype) == 1)
  753.     funaddr = value_as_pointer (value_addr (function));
  754.       else
  755.     /* Handle integer used as address of a function.  */
  756.     funaddr = (CORE_ADDR) value_as_long (function);
  757.  
  758.       value_type = builtin_type_int;
  759.     }
  760.   else
  761.     error ("Invalid data type for function to be called.");
  762.  
  763.   *retval_type = value_type;
  764.   return funaddr;
  765. }
  766.  
  767. #if defined (CALL_DUMMY)
  768. /* All this stuff with a dummy frame may seem unnecessarily complicated
  769.    (why not just save registers in GDB?).  The purpose of pushing a dummy
  770.    frame which looks just like a real frame is so that if you call a
  771.    function and then hit a breakpoint (get a signal, etc), "backtrace"
  772.    will look right.  Whether the backtrace needs to actually show the
  773.    stack at the time the inferior function was called is debatable, but
  774.    it certainly needs to not display garbage.  So if you are contemplating
  775.    making dummy frames be different from normal frames, consider that.  */
  776.  
  777. /* Perform a function call in the inferior.
  778.    ARGS is a vector of values of arguments (NARGS of them).
  779.    FUNCTION is a value, the function to be called.
  780.    Returns a value representing what the function returned.
  781.    May fail to return, if a breakpoint or signal is hit
  782.    during the execution of the function.  */
  783.  
  784. value
  785. call_function_by_hand (function, nargs, args)
  786.      value function;
  787.      int nargs;
  788.      value *args;
  789. {
  790.   register CORE_ADDR sp;
  791.   register int i;
  792.   CORE_ADDR start_sp;
  793.   /* CALL_DUMMY is an array of words (REGISTER_TYPE), but each word
  794.      is in host byte order.  It is switched to target byte order before calling
  795.      FIX_CALL_DUMMY.  */
  796.   static REGISTER_TYPE dummy[] = CALL_DUMMY;
  797.   REGISTER_TYPE dummy1[sizeof dummy / sizeof (REGISTER_TYPE)];
  798.   CORE_ADDR old_sp;
  799.   struct type *value_type;
  800.   unsigned char struct_return;
  801.   CORE_ADDR struct_addr;
  802.   struct inferior_status inf_status;
  803.   struct cleanup *old_chain;
  804.   CORE_ADDR funaddr;
  805.   int using_gcc;
  806.   CORE_ADDR real_pc;
  807.  
  808.   if (!target_has_execution)
  809.     noprocess();
  810.  
  811.   save_inferior_status (&inf_status, 1);
  812.   old_chain = make_cleanup (restore_inferior_status, &inf_status);
  813.  
  814.   /* PUSH_DUMMY_FRAME is responsible for saving the inferior registers
  815.      (and POP_FRAME for restoring them).  (At least on most machines)
  816.      they are saved on the stack in the inferior.  */
  817.   PUSH_DUMMY_FRAME;
  818.  
  819.   old_sp = sp = read_register (SP_REGNUM);
  820.  
  821. #if 1 INNER_THAN 2        /* Stack grows down */
  822.   sp -= sizeof dummy;
  823.   start_sp = sp;
  824. #else                /* Stack grows up */
  825.   start_sp = sp;
  826.   sp += sizeof dummy;
  827. #endif
  828.  
  829.   funaddr = find_function_addr (function, &value_type);
  830.  
  831.   {
  832.     struct block *b = block_for_pc (funaddr);
  833.     /* If compiled without -g, assume GCC.  */
  834.     using_gcc = b == NULL || BLOCK_GCC_COMPILED (b);
  835.   }
  836.  
  837.   /* Are we returning a value using a structure return or a normal
  838.      value return? */
  839.  
  840.   struct_return = using_struct_return (function, funaddr, value_type,
  841.                        using_gcc);
  842.  
  843.   /* Create a call sequence customized for this function
  844.      and the number of arguments for it.  */
  845.   memcpy (dummy1, dummy, sizeof dummy);
  846.   for (i = 0; i < sizeof dummy / sizeof (REGISTER_TYPE); i++)
  847.     SWAP_TARGET_AND_HOST (&dummy1[i], sizeof (REGISTER_TYPE));
  848.  
  849. #ifdef GDB_TARGET_IS_HPPA
  850.   real_pc = FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
  851.                 value_type, using_gcc);
  852. #else
  853.   FIX_CALL_DUMMY (dummy1, start_sp, funaddr, nargs, args,
  854.           value_type, using_gcc);
  855.   real_pc = start_sp;
  856. #endif
  857.  
  858. #if CALL_DUMMY_LOCATION == ON_STACK
  859.   write_memory (start_sp, (char *)dummy1, sizeof dummy);
  860.  
  861. #else /* Not on stack.  */
  862. #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
  863.   /* Convex Unix prohibits executing in the stack segment. */
  864.   /* Hope there is empty room at the top of the text segment. */
  865.   {
  866.     extern CORE_ADDR text_end;
  867.     static checked = 0;
  868.     if (!checked)
  869.       for (start_sp = text_end - sizeof dummy; start_sp < text_end; ++start_sp)
  870.     if (read_memory_integer (start_sp, 1) != 0)
  871.       error ("text segment full -- no place to put call");
  872.     checked = 1;
  873.     sp = old_sp;
  874.     start_sp = text_end - sizeof dummy;
  875.     write_memory (start_sp, (char *)dummy1, sizeof dummy);
  876.   }
  877. #else /* After text_end.  */
  878.   {
  879.     extern CORE_ADDR text_end;
  880.     int errcode;
  881.     sp = old_sp;
  882.     start_sp = text_end;
  883.     errcode = target_write_memory (start_sp, (char *)dummy1, sizeof dummy);
  884.     if (errcode != 0)
  885.       error ("Cannot write text segment -- call_function failed");
  886.   }
  887. #endif /* After text_end.  */
  888. #endif /* Not on stack.  */
  889.  
  890. #ifdef lint
  891.   sp = old_sp;        /* It really is used, for some ifdef's... */
  892. #endif
  893.  
  894. #ifdef STACK_ALIGN
  895.   /* If stack grows down, we must leave a hole at the top. */
  896.   {
  897.     int len = 0;
  898.  
  899.     /* Reserve space for the return structure to be written on the
  900.        stack, if necessary */
  901.  
  902.     if (struct_return)
  903.       len += TYPE_LENGTH (value_type);
  904.     
  905.     for (i = nargs - 1; i >= 0; i--)
  906.       len += TYPE_LENGTH (VALUE_TYPE (value_arg_coerce (args[i])));
  907. #ifdef CALL_DUMMY_STACK_ADJUST
  908.     len += CALL_DUMMY_STACK_ADJUST;
  909. #endif
  910. #if 1 INNER_THAN 2
  911.     sp -= STACK_ALIGN (len) - len;
  912. #else
  913.     sp += STACK_ALIGN (len) - len;
  914. #endif
  915.   }
  916. #endif /* STACK_ALIGN */
  917.  
  918.     /* Reserve space for the return structure to be written on the
  919.        stack, if necessary */
  920.  
  921.     if (struct_return)
  922.       {
  923. #if 1 INNER_THAN 2
  924.     sp -= TYPE_LENGTH (value_type);
  925.     struct_addr = sp;
  926. #else
  927.     struct_addr = sp;
  928.     sp += TYPE_LENGTH (value_type);
  929. #endif
  930.       }
  931.  
  932. #if defined (REG_STRUCT_HAS_ADDR)
  933.   {
  934.     /* This is a machine like the sparc, where we need to pass a pointer
  935.        to the structure, not the structure itself.  */
  936.     if (REG_STRUCT_HAS_ADDR (using_gcc))
  937.       for (i = nargs - 1; i >= 0; i--)
  938.     if (TYPE_CODE (VALUE_TYPE (args[i])) == TYPE_CODE_STRUCT)
  939.       {
  940.         CORE_ADDR addr;
  941. #if !(1 INNER_THAN 2)
  942.         /* The stack grows up, so the address of the thing we push
  943.            is the stack pointer before we push it.  */
  944.         addr = sp;
  945. #endif
  946.         /* Push the structure.  */
  947.         sp = value_push (sp, args[i]);
  948. #if 1 INNER_THAN 2
  949.         /* The stack grows down, so the address of the thing we push
  950.            is the stack pointer after we push it.  */
  951.         addr = sp;
  952. #endif
  953.         /* The value we're going to pass is the address of the thing
  954.            we just pushed.  */
  955.         args[i] = value_from_longest (lookup_pointer_type (value_type),
  956.                        (LONGEST) addr);
  957.       }
  958.   }
  959. #endif /* REG_STRUCT_HAS_ADDR.  */
  960.  
  961. #ifdef PUSH_ARGUMENTS
  962.   PUSH_ARGUMENTS(nargs, args, sp, struct_return, struct_addr);
  963. #else /* !PUSH_ARGUMENTS */
  964.   for (i = nargs - 1; i >= 0; i--)
  965.     sp = value_arg_push (sp, args[i]);
  966. #endif /* !PUSH_ARGUMENTS */
  967.  
  968. #ifdef CALL_DUMMY_STACK_ADJUST
  969. #if 1 INNER_THAN 2
  970.   sp -= CALL_DUMMY_STACK_ADJUST;
  971. #else
  972.   sp += CALL_DUMMY_STACK_ADJUST;
  973. #endif
  974. #endif /* CALL_DUMMY_STACK_ADJUST */
  975.  
  976.   /* Store the address at which the structure is supposed to be
  977.      written.  Note that this (and the code which reserved the space
  978.      above) assumes that gcc was used to compile this function.  Since
  979.      it doesn't cost us anything but space and if the function is pcc
  980.      it will ignore this value, we will make that assumption.
  981.  
  982.      Also note that on some machines (like the sparc) pcc uses a 
  983.      convention like gcc's.  */
  984.  
  985.   if (struct_return)
  986.     STORE_STRUCT_RETURN (struct_addr, sp);
  987.  
  988.   /* Write the stack pointer.  This is here because the statements above
  989.      might fool with it.  On SPARC, this write also stores the register
  990.      window into the right place in the new stack frame, which otherwise
  991.      wouldn't happen.  (See write_inferior_registers in sparc-xdep.c.)  */
  992.   write_register (SP_REGNUM, sp);
  993.  
  994.   /* Figure out the value returned by the function.  */
  995.   {
  996.     char retbuf[REGISTER_BYTES];
  997.  
  998.     /* Execute the stack dummy routine, calling FUNCTION.
  999.        When it is done, discard the empty frame
  1000.        after storing the contents of all regs into retbuf.  */
  1001.     run_stack_dummy (real_pc + CALL_DUMMY_START_OFFSET, retbuf);
  1002.  
  1003.     do_cleanups (old_chain);
  1004.  
  1005.     return value_being_returned (value_type, retbuf, struct_return);
  1006.   }
  1007. }
  1008. #else /* no CALL_DUMMY.  */
  1009. value
  1010. call_function_by_hand (function, nargs, args)
  1011.      value function;
  1012.      int nargs;
  1013.      value *args;
  1014. {
  1015.   error ("Cannot invoke functions on this machine.");
  1016. }
  1017. #endif /* no CALL_DUMMY.  */
  1018.  
  1019.  
  1020. /* Create a value for an array by allocating space in the inferior, copying
  1021.    the data into that space, and then setting up an array value.
  1022.  
  1023.    The array bounds are set from LOWBOUND and HIGHBOUND, and the array is
  1024.    populated from the values passed in ELEMVEC.
  1025.  
  1026.    The element type of the array is inherited from the type of the
  1027.    first element, and all elements must have the same size (though we
  1028.    don't currently enforce any restriction on their types). */
  1029.  
  1030. value
  1031. value_array (lowbound, highbound, elemvec)
  1032.      int lowbound;
  1033.      int highbound;
  1034.      value *elemvec;
  1035. {
  1036.   int nelem;
  1037.   int idx;
  1038.   int typelength;
  1039.   value val;
  1040.   struct type *rangetype;
  1041.   struct type *arraytype;
  1042.   CORE_ADDR addr;
  1043.  
  1044.   /* Validate that the bounds are reasonable and that each of the elements
  1045.      have the same size. */
  1046.  
  1047.   nelem = highbound - lowbound + 1;
  1048.   if (nelem <= 0)
  1049.     {
  1050.       error ("bad array bounds (%d, %d)", lowbound, highbound);
  1051.     }
  1052.   typelength = TYPE_LENGTH (VALUE_TYPE (elemvec[0]));
  1053.   for (idx = 0; idx < nelem; idx++)
  1054.     {
  1055.       if (TYPE_LENGTH (VALUE_TYPE (elemvec[idx])) != typelength)
  1056.     {
  1057.       error ("array elements must all be the same size");
  1058.     }
  1059.     }
  1060.  
  1061.   /* Allocate space to store the array in the inferior, and then initialize
  1062.      it by copying in each element.  FIXME:  Is it worth it to create a
  1063.      local buffer in which to collect each value and then write all the
  1064.      bytes in one operation? */
  1065.  
  1066.   addr = allocate_space_in_inferior (nelem * typelength);
  1067.   for (idx = 0; idx < nelem; idx++)
  1068.     {
  1069.       write_memory (addr + (idx * typelength), VALUE_CONTENTS (elemvec[idx]),
  1070.             typelength);
  1071.     }
  1072.  
  1073.   /* Create the array type and set up an array value to be evaluated lazily. */
  1074.  
  1075.   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
  1076.                  lowbound, highbound);
  1077.   arraytype = create_array_type ((struct type *) NULL, 
  1078.                  VALUE_TYPE (elemvec[0]), rangetype);
  1079.   val = value_at_lazy (arraytype, addr);
  1080.   return (val);
  1081. }
  1082.  
  1083. /* Create a value for a string constant by allocating space in the inferior,
  1084.    copying the data into that space, and returning the address with type
  1085.    TYPE_CODE_STRING.  PTR points to the string constant data; LEN is number
  1086.    of characters.
  1087.    Note that string types are like array of char types with a lower bound of
  1088.    zero and an upper bound of LEN - 1.  Also note that the string may contain
  1089.    embedded null bytes. */
  1090.  
  1091. value
  1092. value_string (ptr, len)
  1093.      char *ptr;
  1094.      int len;
  1095. {
  1096.   value val;
  1097.   struct type *rangetype;
  1098.   struct type *stringtype;
  1099.   CORE_ADDR addr;
  1100.  
  1101.   /* Allocate space to store the string in the inferior, and then
  1102.      copy LEN bytes from PTR in gdb to that address in the inferior. */
  1103.  
  1104.   addr = allocate_space_in_inferior (len);
  1105.   write_memory (addr, ptr, len);
  1106.  
  1107.   /* Create the string type and set up a string value to be evaluated
  1108.      lazily. */
  1109.  
  1110.   rangetype = create_range_type ((struct type *) NULL, builtin_type_int,
  1111.                  0, len - 1);
  1112.   stringtype = create_string_type ((struct type *) NULL, rangetype);
  1113.   val = value_at_lazy (stringtype, addr);
  1114.   return (val);
  1115. }
  1116.  
  1117. /* Compare two argument lists and return the position in which they differ,
  1118.    or zero if equal.
  1119.  
  1120.    STATICP is nonzero if the T1 argument list came from a
  1121.    static member function.
  1122.  
  1123.    For non-static member functions, we ignore the first argument,
  1124.    which is the type of the instance variable.  This is because we want
  1125.    to handle calls with objects from derived classes.  This is not
  1126.    entirely correct: we should actually check to make sure that a
  1127.    requested operation is type secure, shouldn't we?  FIXME.  */
  1128.  
  1129. static int
  1130. typecmp (staticp, t1, t2)
  1131.      int staticp;
  1132.      struct type *t1[];
  1133.      value t2[];
  1134. {
  1135.   int i;
  1136.  
  1137.   if (t2 == 0)
  1138.     return 1;
  1139.   if (staticp && t1 == 0)
  1140.     return t2[1] != 0;
  1141.   if (t1 == 0)
  1142.     return 1;
  1143.   if (TYPE_CODE (t1[0]) == TYPE_CODE_VOID) return 0;
  1144.   if (t1[!staticp] == 0) return 0;
  1145.   for (i = !staticp; t1[i] && TYPE_CODE (t1[i]) != TYPE_CODE_VOID; i++)
  1146.     {
  1147.       if (! t2[i])
  1148.     return i+1;
  1149.       if (TYPE_CODE (t1[i]) == TYPE_CODE_REF
  1150.       && TYPE_TARGET_TYPE (t1[i]) == VALUE_TYPE (t2[i]))
  1151.     continue;
  1152.       if (TYPE_CODE (t1[i]) != TYPE_CODE (VALUE_TYPE (t2[i])))
  1153.     return i+1;
  1154.     }
  1155.   if (!t1[i]) return 0;
  1156.   return t2[i] ? i+1 : 0;
  1157. }
  1158.  
  1159. /* Helper function used by value_struct_elt to recurse through baseclasses.
  1160.    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
  1161.    and search in it assuming it has (class) type TYPE.
  1162.    If found, return value, else return NULL.
  1163.  
  1164.    If LOOKING_FOR_BASECLASS, then instead of looking for struct fields,
  1165.    look for a baseclass named NAME.  */
  1166.  
  1167. static value
  1168. search_struct_field (name, arg1, offset, type, looking_for_baseclass)
  1169.      char *name;
  1170.      register value arg1;
  1171.      int offset;
  1172.      register struct type *type;
  1173.      int looking_for_baseclass;
  1174. {
  1175.   int i;
  1176.  
  1177.   check_stub_type (type);
  1178.  
  1179.   if (! looking_for_baseclass)
  1180.     for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1181.       {
  1182.     char *t_field_name = TYPE_FIELD_NAME (type, i);
  1183.  
  1184.     if (t_field_name && STREQ (t_field_name, name))
  1185.       {
  1186.         value v;
  1187.         if (TYPE_FIELD_STATIC (type, i))
  1188.           {
  1189.         char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (type, i);
  1190.         struct symbol *sym =
  1191.             lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
  1192.         if (sym == NULL)
  1193.             error ("Internal error: could not find physical static variable named %s",
  1194.                phys_name);
  1195.         v = value_at (TYPE_FIELD_TYPE (type, i),
  1196.                   (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  1197.           }
  1198.         else
  1199.           v = value_primitive_field (arg1, offset, i, type);
  1200.         if (v == 0)
  1201.           error("there is no field named %s", name);
  1202.         return v;
  1203.       }
  1204.       }
  1205.  
  1206.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1207.     {
  1208.       value v;
  1209.       /* If we are looking for baseclasses, this is what we get when we
  1210.      hit them.  But it could happen that the base part's member name
  1211.      is not yet filled in.  */
  1212.       int found_baseclass = (looking_for_baseclass
  1213.                  && TYPE_BASECLASS_NAME (type, i) != NULL
  1214.                  && STREQ (name, TYPE_BASECLASS_NAME (type, i)));
  1215.  
  1216.       if (BASETYPE_VIA_VIRTUAL (type, i))
  1217.     {
  1218.       value v2;
  1219.       /* Fix to use baseclass_offset instead. FIXME */
  1220.       baseclass_addr (type, i, VALUE_CONTENTS (arg1) + offset,
  1221.               &v2, (int *)NULL);
  1222.       if (v2 == 0)
  1223.         error ("virtual baseclass botch");
  1224.       if (found_baseclass)
  1225.         return v2;
  1226.       v = search_struct_field (name, v2, 0, TYPE_BASECLASS (type, i),
  1227.                    looking_for_baseclass);
  1228.     }
  1229.       else if (found_baseclass)
  1230.     v = value_primitive_field (arg1, offset, i, type);
  1231.       else
  1232.     v = search_struct_field (name, arg1,
  1233.                  offset + TYPE_BASECLASS_BITPOS (type, i) / 8,
  1234.                  TYPE_BASECLASS (type, i),
  1235.                  looking_for_baseclass);
  1236.       if (v) return v;
  1237.     }
  1238.   return NULL;
  1239. }
  1240.  
  1241. /* Helper function used by value_struct_elt to recurse through baseclasses.
  1242.    Look for a field NAME in ARG1. Adjust the address of ARG1 by OFFSET bytes,
  1243.    and search in it assuming it has (class) type TYPE.
  1244.    If found, return value, else return NULL. */
  1245.  
  1246. static value
  1247. search_struct_method (name, arg1p, args, offset, static_memfuncp, type)
  1248.      char *name;
  1249.      register value *arg1p, *args;
  1250.      int offset, *static_memfuncp;
  1251.      register struct type *type;
  1252. {
  1253.   int i;
  1254.  
  1255.   check_stub_type (type);
  1256.   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; i--)
  1257.     {
  1258.       char *t_field_name = TYPE_FN_FIELDLIST_NAME (type, i);
  1259.       if (t_field_name && STREQ (t_field_name, name))
  1260.     {
  1261.       int j = TYPE_FN_FIELDLIST_LENGTH (type, i) - 1;
  1262.       struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  1263.  
  1264.       if (j > 0 && args == 0)
  1265.         error ("cannot resolve overloaded method `%s'", name);
  1266.       while (j >= 0)
  1267.         {
  1268.           if (TYPE_FN_FIELD_STUB (f, j))
  1269.         check_stub_method (type, i, j);
  1270.           if (!typecmp (TYPE_FN_FIELD_STATIC_P (f, j),
  1271.                 TYPE_FN_FIELD_ARGS (f, j), args))
  1272.         {
  1273.           if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  1274.             return (value)value_virtual_fn_field (arg1p, f, j, type, offset);
  1275.           if (TYPE_FN_FIELD_STATIC_P (f, j) && static_memfuncp)
  1276.             *static_memfuncp = 1;
  1277.           return (value)value_fn_field (arg1p, f, j, type, offset);
  1278.         }
  1279.           j--;
  1280.         }
  1281.     }
  1282.     }
  1283.  
  1284.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1285.     {
  1286.       value v;
  1287.       int base_offset;
  1288.  
  1289.       if (BASETYPE_VIA_VIRTUAL (type, i))
  1290.     {
  1291.       base_offset = baseclass_offset (type, i, *arg1p, offset);
  1292.       if (base_offset == -1)
  1293.         error ("virtual baseclass botch");
  1294.     }
  1295.       else
  1296.     {
  1297.       base_offset = TYPE_BASECLASS_BITPOS (type, i) / 8;
  1298.         }
  1299.       v = search_struct_method (name, arg1p, args, base_offset + offset,
  1300.                 static_memfuncp, TYPE_BASECLASS (type, i));
  1301.       if (v)
  1302.     {
  1303. /* FIXME-bothner:  Why is this commented out?  Why is it here?  */
  1304. /*      *arg1p = arg1_tmp;*/
  1305.       return v;
  1306.         }
  1307.     }
  1308.   return NULL;
  1309. }
  1310.  
  1311. /* Given *ARGP, a value of type (pointer to a)* structure/union,
  1312.    extract the component named NAME from the ultimate target structure/union
  1313.    and return it as a value with its appropriate type.
  1314.    ERR is used in the error message if *ARGP's type is wrong.
  1315.  
  1316.    C++: ARGS is a list of argument types to aid in the selection of
  1317.    an appropriate method. Also, handle derived types.
  1318.  
  1319.    STATIC_MEMFUNCP, if non-NULL, points to a caller-supplied location
  1320.    where the truthvalue of whether the function that was resolved was
  1321.    a static member function or not is stored.
  1322.  
  1323.    ERR is an error message to be printed in case the field is not found.  */
  1324.  
  1325. value
  1326. value_struct_elt (argp, args, name, static_memfuncp, err)
  1327.      register value *argp, *args;
  1328.      char *name;
  1329.      int *static_memfuncp;
  1330.      char *err;
  1331. {
  1332.   register struct type *t;
  1333.   value v;
  1334.  
  1335.   COERCE_ARRAY (*argp);
  1336.  
  1337.   t = VALUE_TYPE (*argp);
  1338.  
  1339.   /* Follow pointers until we get to a non-pointer.  */
  1340.  
  1341.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  1342.     {
  1343.       *argp = value_ind (*argp);
  1344.       /* Don't coerce fn pointer to fn and then back again!  */
  1345.       if (TYPE_CODE (VALUE_TYPE (*argp)) != TYPE_CODE_FUNC)
  1346.     COERCE_ARRAY (*argp);
  1347.       t = VALUE_TYPE (*argp);
  1348.     }
  1349.  
  1350.   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
  1351.     error ("not implemented: member type in value_struct_elt");
  1352.  
  1353.   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
  1354.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1355.     error ("Attempt to extract a component of a value that is not a %s.", err);
  1356.  
  1357.   /* Assume it's not, unless we see that it is.  */
  1358.   if (static_memfuncp)
  1359.     *static_memfuncp =0;
  1360.  
  1361.   if (!args)
  1362.     {
  1363.       /* if there are no arguments ...do this...  */
  1364.  
  1365.       /* Try as a field first, because if we succeed, there
  1366.      is less work to be done.  */
  1367.       v = search_struct_field (name, *argp, 0, t, 0);
  1368.       if (v)
  1369.     return v;
  1370.  
  1371.       /* C++: If it was not found as a data field, then try to
  1372.          return it as a pointer to a method.  */
  1373.  
  1374.       if (destructor_name_p (name, t))
  1375.     error ("Cannot get value of destructor");
  1376.  
  1377.       v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
  1378.  
  1379.       if (v == 0)
  1380.     {
  1381.       if (TYPE_NFN_FIELDS (t))
  1382.         error ("There is no member or method named %s.", name);
  1383.       else
  1384.         error ("There is no member named %s.", name);
  1385.     }
  1386.       return v;
  1387.     }
  1388.  
  1389.   if (destructor_name_p (name, t))
  1390.     {
  1391.       if (!args[1])
  1392.     {
  1393.       /* destructors are a special case.  */
  1394.       return (value)value_fn_field (NULL, TYPE_FN_FIELDLIST1 (t, 0),
  1395.                     TYPE_FN_FIELDLIST_LENGTH (t, 0),
  1396.                     0, 0);
  1397.     }
  1398.       else
  1399.     {
  1400.       error ("destructor should not have any argument");
  1401.     }
  1402.     }
  1403.   else
  1404.     v = search_struct_method (name, argp, args, 0, static_memfuncp, t);
  1405.  
  1406.   if (v == 0)
  1407.     {
  1408.       /* See if user tried to invoke data as function.  If so,
  1409.      hand it back.  If it's not callable (i.e., a pointer to function),
  1410.      gdb should give an error.  */
  1411.       v = search_struct_field (name, *argp, 0, t, 0);
  1412.     }
  1413.  
  1414.   if (!v)
  1415.     error ("Structure has no component named %s.", name);
  1416.   return v;
  1417. }
  1418.  
  1419. /* C++: return 1 is NAME is a legitimate name for the destructor
  1420.    of type TYPE.  If TYPE does not have a destructor, or
  1421.    if NAME is inappropriate for TYPE, an error is signaled.  */
  1422. int
  1423. destructor_name_p (name, type)
  1424.      const char *name;
  1425.      const struct type *type;
  1426. {
  1427.   /* destructors are a special case.  */
  1428.  
  1429.   if (name[0] == '~')
  1430.     {
  1431.       char *dname = type_name_no_tag (type);
  1432.       if (!STREQ (dname, name+1))
  1433.     error ("name of destructor must equal name of class");
  1434.       else
  1435.     return 1;
  1436.     }
  1437.   return 0;
  1438. }
  1439.  
  1440. /* Helper function for check_field: Given TYPE, a structure/union,
  1441.    return 1 if the component named NAME from the ultimate
  1442.    target structure/union is defined, otherwise, return 0. */
  1443.  
  1444. static int
  1445. check_field_in (type, name)
  1446.      register struct type *type;
  1447.      const char *name;
  1448. {
  1449.   register int i;
  1450.  
  1451.   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1452.     {
  1453.       char *t_field_name = TYPE_FIELD_NAME (type, i);
  1454.       if (t_field_name && STREQ (t_field_name, name))
  1455.     return 1;
  1456.     }
  1457.  
  1458.   /* C++: If it was not found as a data field, then try to
  1459.      return it as a pointer to a method.  */
  1460.  
  1461.   /* Destructors are a special case.  */
  1462.   if (destructor_name_p (name, type))
  1463.     return 1;
  1464.  
  1465.   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
  1466.     {
  1467.       if (STREQ (TYPE_FN_FIELDLIST_NAME (type, i), name))
  1468.     return 1;
  1469.     }
  1470.  
  1471.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1472.     if (check_field_in (TYPE_BASECLASS (type, i), name))
  1473.       return 1;
  1474.       
  1475.   return 0;
  1476. }
  1477.  
  1478.  
  1479. /* C++: Given ARG1, a value of type (pointer to a)* structure/union,
  1480.    return 1 if the component named NAME from the ultimate
  1481.    target structure/union is defined, otherwise, return 0.  */
  1482.  
  1483. int
  1484. check_field (arg1, name)
  1485.      register value arg1;
  1486.      const char *name;
  1487. {
  1488.   register struct type *t;
  1489.  
  1490.   COERCE_ARRAY (arg1);
  1491.  
  1492.   t = VALUE_TYPE (arg1);
  1493.  
  1494.   /* Follow pointers until we get to a non-pointer.  */
  1495.  
  1496.   while (TYPE_CODE (t) == TYPE_CODE_PTR || TYPE_CODE (t) == TYPE_CODE_REF)
  1497.     t = TYPE_TARGET_TYPE (t);
  1498.  
  1499.   if (TYPE_CODE (t) == TYPE_CODE_MEMBER)
  1500.     error ("not implemented: member type in check_field");
  1501.  
  1502.   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
  1503.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1504.     error ("Internal error: `this' is not an aggregate");
  1505.  
  1506.   return check_field_in (t, name);
  1507. }
  1508.  
  1509. /* C++: Given an aggregate type CURTYPE, and a member name NAME,
  1510.    return the address of this member as a "pointer to member"
  1511.    type.  If INTYPE is non-null, then it will be the type
  1512.    of the member we are looking for.  This will help us resolve
  1513.    "pointers to member functions".  This function is used
  1514.    to resolve user expressions of the form "DOMAIN::NAME".  */
  1515.  
  1516. value
  1517. value_struct_elt_for_reference (domain, offset, curtype, name, intype)
  1518.      struct type *domain, *curtype, *intype;
  1519.      int offset;
  1520.      char *name;
  1521. {
  1522.   register struct type *t = curtype;
  1523.   register int i;
  1524.   value v;
  1525.  
  1526.   if (   TYPE_CODE (t) != TYPE_CODE_STRUCT
  1527.       && TYPE_CODE (t) != TYPE_CODE_UNION)
  1528.     error ("Internal error: non-aggregate type to value_struct_elt_for_reference");
  1529.  
  1530.   for (i = TYPE_NFIELDS (t) - 1; i >= TYPE_N_BASECLASSES (t); i--)
  1531.     {
  1532.       char *t_field_name = TYPE_FIELD_NAME (t, i);
  1533.       
  1534.       if (t_field_name && STREQ (t_field_name, name))
  1535.     {
  1536.       if (TYPE_FIELD_STATIC (t, i))
  1537.         {
  1538.           char *phys_name = TYPE_FIELD_STATIC_PHYSNAME (t, i);
  1539.           struct symbol *sym =
  1540.         lookup_symbol (phys_name, 0, VAR_NAMESPACE, 0, NULL);
  1541.           if (sym == NULL)
  1542.         error ("Internal error: could not find physical static variable named %s",
  1543.                phys_name);
  1544.           return value_at (SYMBOL_TYPE (sym),
  1545.                    (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  1546.         }
  1547.       if (TYPE_FIELD_PACKED (t, i))
  1548.         error ("pointers to bitfield members not allowed");
  1549.       
  1550.       return value_from_longest
  1551.         (lookup_reference_type (lookup_member_type (TYPE_FIELD_TYPE (t, i),
  1552.                             domain)),
  1553.          offset + (LONGEST) (TYPE_FIELD_BITPOS (t, i) >> 3));
  1554.     }
  1555.     }
  1556.  
  1557.   /* C++: If it was not found as a data field, then try to
  1558.      return it as a pointer to a method.  */
  1559.  
  1560.   /* Destructors are a special case.  */
  1561.   if (destructor_name_p (name, t))
  1562.     {
  1563.       error ("member pointers to destructors not implemented yet");
  1564.     }
  1565.  
  1566.   /* Perform all necessary dereferencing.  */
  1567.   while (intype && TYPE_CODE (intype) == TYPE_CODE_PTR)
  1568.     intype = TYPE_TARGET_TYPE (intype);
  1569.  
  1570.   for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; --i)
  1571.     {
  1572.       if (STREQ (TYPE_FN_FIELDLIST_NAME (t, i), name))
  1573.     {
  1574.       int j = TYPE_FN_FIELDLIST_LENGTH (t, i);
  1575.       struct fn_field *f = TYPE_FN_FIELDLIST1 (t, i);
  1576.       
  1577.       if (intype == 0 && j > 1)
  1578.         error ("non-unique member `%s' requires type instantiation", name);
  1579.       if (intype)
  1580.         {
  1581.           while (j--)
  1582.         if (TYPE_FN_FIELD_TYPE (f, j) == intype)
  1583.           break;
  1584.           if (j < 0)
  1585.         error ("no member function matches that type instantiation");
  1586.         }
  1587.       else
  1588.         j = 0;
  1589.       
  1590.       if (TYPE_FN_FIELD_STUB (f, j))
  1591.         check_stub_method (t, i, j);
  1592.       if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  1593.         {
  1594.           return value_from_longest
  1595.         (lookup_reference_type
  1596.          (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
  1597.                       domain)),
  1598.          (LONGEST) METHOD_PTR_FROM_VOFFSET
  1599.           (TYPE_FN_FIELD_VOFFSET (f, j)));
  1600.         }
  1601.       else
  1602.         {
  1603.           struct symbol *s = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, j),
  1604.                         0, VAR_NAMESPACE, 0, NULL);
  1605.           if (s == NULL)
  1606.         {
  1607.           v = 0;
  1608.         }
  1609.           else
  1610.         {
  1611.           v = read_var_value (s, 0);
  1612. #if 0
  1613.           VALUE_TYPE (v) = lookup_reference_type
  1614.             (lookup_member_type (TYPE_FN_FIELD_TYPE (f, j),
  1615.                      domain));
  1616. #endif
  1617.         }
  1618.           return v;
  1619.         }
  1620.     }
  1621.     }
  1622.   for (i = TYPE_N_BASECLASSES (t) - 1; i >= 0; i--)
  1623.     {
  1624.       value v;
  1625.       int base_offset;
  1626.  
  1627.       if (BASETYPE_VIA_VIRTUAL (t, i))
  1628.     base_offset = 0;
  1629.       else
  1630.     base_offset = TYPE_BASECLASS_BITPOS (t, i) / 8;
  1631.       v = value_struct_elt_for_reference (domain,
  1632.                       offset + base_offset,
  1633.                       TYPE_BASECLASS (t, i),
  1634.                       name,
  1635.                       intype);
  1636.       if (v)
  1637.     return v;
  1638.     }
  1639.   return 0;
  1640. }
  1641.  
  1642. /* C++: return the value of the class instance variable, if one exists.
  1643.    Flag COMPLAIN signals an error if the request is made in an
  1644.    inappropriate context.  */
  1645. value
  1646. value_of_this (complain)
  1647.      int complain;
  1648. {
  1649.   extern FRAME selected_frame;
  1650.   struct symbol *func, *sym;
  1651.   struct block *b;
  1652.   int i;
  1653.   static const char funny_this[] = "this";
  1654.   value this;
  1655.  
  1656.   if (selected_frame == 0)
  1657.     if (complain)
  1658.       error ("no frame selected");
  1659.     else return 0;
  1660.  
  1661.   func = get_frame_function (selected_frame);
  1662.   if (!func)
  1663.     {
  1664.       if (complain)
  1665.     error ("no `this' in nameless context");
  1666.       else return 0;
  1667.     }
  1668.  
  1669.   b = SYMBOL_BLOCK_VALUE (func);
  1670.   i = BLOCK_NSYMS (b);
  1671.   if (i <= 0)
  1672.     if (complain)
  1673.       error ("no args, no `this'");
  1674.     else return 0;
  1675.  
  1676.   /* Calling lookup_block_symbol is necessary to get the LOC_REGISTER
  1677.      symbol instead of the LOC_ARG one (if both exist).  */
  1678.   sym = lookup_block_symbol (b, funny_this, VAR_NAMESPACE);
  1679.   if (sym == NULL)
  1680.     {
  1681.       if (complain)
  1682.     error ("current stack frame not in method");
  1683.       else
  1684.     return NULL;
  1685.     }
  1686.  
  1687.   this = read_var_value (sym, selected_frame);
  1688.   if (this == 0 && complain)
  1689.     error ("`this' argument at unknown address");
  1690.   return this;
  1691. }
  1692.