home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / valops.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  62.8 KB  |  2,118 lines

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