gdb/ada-varobj.c - gdb

Global variables defined

Functions defined

Source code

  1. /* varobj support for Ada.

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

  3.    This file is part of GDB.

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

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

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

  14. #include "defs.h"
  15. #include "ada-lang.h"
  16. #include "varobj.h"
  17. #include "language.h"
  18. #include "valprint.h"

  19. /* Implementation principle used in this unit:

  20.    For our purposes, the meat of the varobj object is made of two
  21.    elements: The varobj's (struct) value, and the varobj's (struct)
  22.    type.  In most situations, the varobj has a non-NULL value, and
  23.    the type becomes redundant, as it can be directly derived from
  24.    the value.  In the initial implementation of this unit, most
  25.    routines would only take a value, and return a value.

  26.    But there are many situations where it is possible for a varobj
  27.    to have a NULL value.  For instance, if the varobj becomes out of
  28.    scope.  Or better yet, when the varobj is the child of another
  29.    NULL pointer varobj.  In that situation, we must rely on the type
  30.    instead of the value to create the child varobj.

  31.    That's why most functions below work with a (value, type) pair.
  32.    The value may or may not be NULL.  But the type is always expected
  33.    to be set.  When the value is NULL, then we work with the type
  34.    alone, and keep the value NULL.  But when the value is not NULL,
  35.    then we work using the value, because it provides more information.
  36.    But we still always set the type as well, even if that type could
  37.    easily be derived from the value.  The reason behind this is that
  38.    it allows the code to use the type without having to worry about
  39.    it being set or not.  It makes the code clearer.  */

  40. static int ada_varobj_get_number_of_children (struct value *parent_value,
  41.                                               struct type *parent_type);

  42. /* A convenience function that decodes the VALUE_PTR/TYPE_PTR couple:
  43.    If there is a value (*VALUE_PTR not NULL), then perform the decoding
  44.    using it, and compute the associated type from the resulting value.
  45.    Otherwise, compute a static approximation of *TYPE_PTR, leaving
  46.    *VALUE_PTR unchanged.

  47.    The results are written in place.  */

  48. static void
  49. ada_varobj_decode_var (struct value **value_ptr, struct type **type_ptr)
  50. {
  51.   if (*value_ptr)
  52.     {
  53.       *value_ptr = ada_get_decoded_value (*value_ptr);
  54.       *type_ptr = ada_check_typedef (value_type (*value_ptr));
  55.     }
  56.   else
  57.     *type_ptr = ada_get_decoded_type (*type_ptr);
  58. }

  59. /* Return a string containing an image of the given scalar value.
  60.    VAL is the numeric value, while TYPE is the value's type.
  61.    This is useful for plain integers, of course, but even more
  62.    so for enumerated types.

  63.    The result should be deallocated by xfree after use.  */

  64. static char *
  65. ada_varobj_scalar_image (struct type *type, LONGEST val)
  66. {
  67.   struct ui_file *buf = mem_fileopen ();
  68.   struct cleanup *cleanups = make_cleanup_ui_file_delete (buf);
  69.   char *result;

  70.   ada_print_scalar (type, val, buf);
  71.   result = ui_file_xstrdup (buf, NULL);
  72.   do_cleanups (cleanups);

  73.   return result;
  74. }

  75. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates
  76.    a struct or union, compute the (CHILD_VALUE, CHILD_TYPE) couple
  77.    corresponding to the field number FIELDNO.  */

  78. static void
  79. ada_varobj_struct_elt (struct value *parent_value,
  80.                        struct type *parent_type,
  81.                        int fieldno,
  82.                        struct value **child_value,
  83.                        struct type **child_type)
  84. {
  85.   struct value *value = NULL;
  86.   struct type *type = NULL;

  87.   if (parent_value)
  88.     {
  89.       value = value_field (parent_value, fieldno);
  90.       type = value_type (value);
  91.     }
  92.   else
  93.     type = TYPE_FIELD_TYPE (parent_type, fieldno);

  94.   if (child_value)
  95.     *child_value = value;
  96.   if (child_type)
  97.     *child_type = type;
  98. }

  99. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a pointer or
  100.    reference, return a (CHILD_VALUE, CHILD_TYPE) couple corresponding
  101.    to the dereferenced value.  */

  102. static void
  103. ada_varobj_ind (struct value *parent_value,
  104.                 struct type *parent_type,
  105.                 struct value **child_value,
  106.                 struct type **child_type)
  107. {
  108.   struct value *value = NULL;
  109.   struct type *type = NULL;

  110.   if (ada_is_array_descriptor_type (parent_type))
  111.     {
  112.       /* This can only happen when PARENT_VALUE is NULL.  Otherwise,
  113.          ada_get_decoded_value would have transformed our parent_type
  114.          into a simple array pointer type.  */
  115.       gdb_assert (parent_value == NULL);
  116.       gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF);

  117.       /* Decode parent_type by the equivalent pointer to (decoded)
  118.          array.  */
  119.       while (TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF)
  120.         parent_type = TYPE_TARGET_TYPE (parent_type);
  121.       parent_type = ada_coerce_to_simple_array_type (parent_type);
  122.       parent_type = lookup_pointer_type (parent_type);
  123.     }

  124.   /* If parent_value is a null pointer, then only perform static
  125.      dereferencing.  We cannot dereference null pointers.  */
  126.   if (parent_value && value_as_address (parent_value) == 0)
  127.     parent_value = NULL;

  128.   if (parent_value)
  129.     {
  130.       value = ada_value_ind (parent_value);
  131.       type = value_type (value);
  132.     }
  133.   else
  134.     type = TYPE_TARGET_TYPE (parent_type);

  135.   if (child_value)
  136.     *child_value = value;
  137.   if (child_type)
  138.     *child_type = type;
  139. }

  140. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a simple
  141.    array (TYPE_CODE_ARRAY), return the (CHILD_VALUE, CHILD_TYPE)
  142.    pair corresponding to the element at ELT_INDEX.  */

  143. static void
  144. ada_varobj_simple_array_elt (struct value *parent_value,
  145.                              struct type *parent_type,
  146.                              int elt_index,
  147.                              struct value **child_value,
  148.                              struct type **child_type)
  149. {
  150.   struct value *value = NULL;
  151.   struct type *type = NULL;

  152.   if (parent_value)
  153.     {
  154.       struct value *index_value =
  155.         value_from_longest (TYPE_INDEX_TYPE (parent_type), elt_index);

  156.       value = ada_value_subscript (parent_value, 1, &index_value);
  157.       type = value_type (value);
  158.     }
  159.   else
  160.     type = TYPE_TARGET_TYPE (parent_type);

  161.   if (child_value)
  162.     *child_value = value;
  163.   if (child_type)
  164.     *child_type = type;
  165. }

  166. /* Given the decoded value and decoded type of a variable object,
  167.    adjust the value and type to those necessary for getting children
  168.    of the variable object.

  169.    The replacement is performed in place.  */

  170. static void
  171. ada_varobj_adjust_for_child_access (struct value **value,
  172.                                     struct type **type)
  173. {
  174.    /* Pointers to struct/union types are special: Instead of having
  175.       one child (the struct), their children are the components of
  176.       the struct/union type.  We handle this situation by dereferencing
  177.       the (value, type) couple.  */
  178.   if (TYPE_CODE (*type) == TYPE_CODE_PTR
  179.       && (TYPE_CODE (TYPE_TARGET_TYPE (*type)) == TYPE_CODE_STRUCT
  180.           || TYPE_CODE (TYPE_TARGET_TYPE (*type)) == TYPE_CODE_UNION)
  181.       && !ada_is_array_descriptor_type (TYPE_TARGET_TYPE (*type))
  182.       && !ada_is_constrained_packed_array_type (TYPE_TARGET_TYPE (*type)))
  183.     ada_varobj_ind (*value, *type, value, type);

  184.   /* If this is a tagged type, we need to transform it a bit in order
  185.      to be able to fetch its full view.  As always with tagged types,
  186.      we can only do that if we have a value.  */
  187.   if (*value != NULL && ada_is_tagged_type (*type, 1))
  188.     {
  189.       *value = ada_tag_value_at_base_address (*value);
  190.       *type = value_type (*value);
  191.     }
  192. }

  193. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is an array
  194.    (any type of array, "simple" or not), return the number of children
  195.    that this array contains.  */

  196. static int
  197. ada_varobj_get_array_number_of_children (struct value *parent_value,
  198.                                          struct type *parent_type)
  199. {
  200.   LONGEST lo, hi;

  201.   if (!get_array_bounds (parent_type, &lo, &hi))
  202.     {
  203.       /* Could not get the array bounds.  Pretend this is an empty array.  */
  204.       warning (_("unable to get bounds of array, assuming null array"));
  205.       return 0;
  206.     }

  207.   /* Ada allows the upper bound to be less than the lower bound,
  208.      in order to specify empty arrays...  */
  209.   if (hi < lo)
  210.     return 0;

  211.   return hi - lo + 1;
  212. }

  213. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a struct or
  214.    union, return the number of children this struct contains.  */

  215. static int
  216. ada_varobj_get_struct_number_of_children (struct value *parent_value,
  217.                                           struct type *parent_type)
  218. {
  219.   int n_children = 0;
  220.   int i;

  221.   gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
  222.               || TYPE_CODE (parent_type) == TYPE_CODE_UNION);

  223.   for (i = 0; i < TYPE_NFIELDS (parent_type); i++)
  224.     {
  225.       if (ada_is_ignored_field (parent_type, i))
  226.         continue;

  227.       if (ada_is_wrapper_field (parent_type, i))
  228.         {
  229.           struct value *elt_value;
  230.           struct type *elt_type;

  231.           ada_varobj_struct_elt (parent_value, parent_type, i,
  232.                                  &elt_value, &elt_type);
  233.           if (ada_is_tagged_type (elt_type, 0))
  234.             {
  235.               /* We must not use ada_varobj_get_number_of_children
  236.                  to determine is element's number of children, because
  237.                  this function first calls ada_varobj_decode_var,
  238.                  which "fixes" the element.  For tagged types, this
  239.                  includes reading the object's tag to determine its
  240.                  real type, which happens to be the parent_type, and
  241.                  leads to an infinite loop (because the element gets
  242.                  fixed back into the parent).  */
  243.               n_children += ada_varobj_get_struct_number_of_children
  244.                 (elt_value, elt_type);
  245.             }
  246.           else
  247.             n_children += ada_varobj_get_number_of_children (elt_value, elt_type);
  248.         }
  249.       else if (ada_is_variant_part (parent_type, i))
  250.         {
  251.           /* In normal situations, the variant part of the record should
  252.              have been "fixed". Or, in other words, it should have been
  253.              replaced by the branch of the variant part that is relevant
  254.              for our value.  But there are still situations where this
  255.              can happen, however (Eg. when our parent is a NULL pointer).
  256.              We do not support showing this part of the record for now,
  257.              so just pretend this field does not exist.  */
  258.         }
  259.       else
  260.         n_children++;
  261.     }

  262.   return n_children;
  263. }

  264. /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates
  265.    a pointer, return the number of children this pointer has.  */

  266. static int
  267. ada_varobj_get_ptr_number_of_children (struct value *parent_value,
  268.                                        struct type *parent_type)
  269. {
  270.   struct type *child_type = TYPE_TARGET_TYPE (parent_type);

  271.   /* Pointer to functions and to void do not have a child, since
  272.      you cannot print what they point to.  */
  273.   if (TYPE_CODE (child_type) == TYPE_CODE_FUNC
  274.       || TYPE_CODE (child_type) == TYPE_CODE_VOID)
  275.     return 0;

  276.   /* All other types have 1 child.  */
  277.   return 1;
  278. }

  279. /* Return the number of children for the (PARENT_VALUE, PARENT_TYPE)
  280.    pair.  */

  281. static int
  282. ada_varobj_get_number_of_children (struct value *parent_value,
  283.                                    struct type *parent_type)
  284. {
  285.   ada_varobj_decode_var (&parent_value, &parent_type);
  286.   ada_varobj_adjust_for_child_access (&parent_value, &parent_type);

  287.   /* A typedef to an array descriptor in fact represents a pointer
  288.      to an unconstrained array.  These types always have one child
  289.      (the unconstrained array).  */
  290.   if (ada_is_array_descriptor_type (parent_type)
  291.       && TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF)
  292.     return 1;

  293.   if (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY)
  294.     return ada_varobj_get_array_number_of_children (parent_value,
  295.                                                     parent_type);

  296.   if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
  297.       || TYPE_CODE (parent_type) == TYPE_CODE_UNION)
  298.     return ada_varobj_get_struct_number_of_children (parent_value,
  299.                                                      parent_type);

  300.   if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
  301.     return ada_varobj_get_ptr_number_of_children (parent_value,
  302.                                                   parent_type);

  303.   /* All other types have no child.  */
  304.   return 0;
  305. }

  306. /* Describe the child of the (PARENT_VALUE, PARENT_TYPE) pair
  307.    whose index is CHILD_INDEX:

  308.      - If CHILD_NAME is not NULL, then a copy of the child's name
  309.        is saved in *CHILD_NAME.  This copy must be deallocated
  310.        with xfree after use.

  311.      - If CHILD_VALUE is not NULL, then save the child's value
  312.        in *CHILD_VALUE. Same thing for the child's type with
  313.        CHILD_TYPE if not NULL.

  314.      - If CHILD_PATH_EXPR is not NULL, then compute the child's
  315.        path expression.  The resulting string must be deallocated
  316.        after use with xfree.

  317.        Computing the child's path expression requires the PARENT_PATH_EXPR
  318.        to be non-NULL.  Otherwise, PARENT_PATH_EXPR may be null if
  319.        CHILD_PATH_EXPR is NULL.

  320.   PARENT_NAME is the name of the parent, and should never be NULL.  */

  321. static void ada_varobj_describe_child (struct value *parent_value,
  322.                                        struct type *parent_type,
  323.                                        const char *parent_name,
  324.                                        const char *parent_path_expr,
  325.                                        int child_index,
  326.                                        char **child_name,
  327.                                        struct value **child_value,
  328.                                        struct type **child_type,
  329.                                        char **child_path_expr);

  330. /* Same as ada_varobj_describe_child, but limited to struct/union
  331.    objects.  */

  332. static void
  333. ada_varobj_describe_struct_child (struct value *parent_value,
  334.                                   struct type *parent_type,
  335.                                   const char *parent_name,
  336.                                   const char *parent_path_expr,
  337.                                   int child_index,
  338.                                   char **child_name,
  339.                                   struct value **child_value,
  340.                                   struct type **child_type,
  341.                                   char **child_path_expr)
  342. {
  343.   int fieldno;
  344.   int childno = 0;

  345.   gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT);

  346.   for (fieldno = 0; fieldno < TYPE_NFIELDS (parent_type); fieldno++)
  347.     {
  348.       if (ada_is_ignored_field (parent_type, fieldno))
  349.         continue;

  350.       if (ada_is_wrapper_field (parent_type, fieldno))
  351.         {
  352.           struct value *elt_value;
  353.           struct type *elt_type;
  354.           int elt_n_children;

  355.           ada_varobj_struct_elt (parent_value, parent_type, fieldno,
  356.                                  &elt_value, &elt_type);
  357.           if (ada_is_tagged_type (elt_type, 0))
  358.             {
  359.               /* Same as in ada_varobj_get_struct_number_of_children:
  360.                  For tagged types, we must be careful to not call
  361.                  ada_varobj_get_number_of_children, to prevent our
  362.                  element from being fixed back into the parent.  */
  363.               elt_n_children = ada_varobj_get_struct_number_of_children
  364.                 (elt_value, elt_type);
  365.             }
  366.           else
  367.             elt_n_children =
  368.               ada_varobj_get_number_of_children (elt_value, elt_type);

  369.           /* Is the child we're looking for one of the children
  370.              of this wrapper field?  */
  371.           if (child_index - childno < elt_n_children)
  372.             {
  373.               if (ada_is_tagged_type (elt_type, 0))
  374.                 {
  375.                   /* Same as in ada_varobj_get_struct_number_of_children:
  376.                      For tagged types, we must be careful to not call
  377.                      ada_varobj_describe_child, to prevent our element
  378.                      from being fixed back into the parent.  */
  379.                   ada_varobj_describe_struct_child
  380.                     (elt_value, elt_type, parent_name, parent_path_expr,
  381.                      child_index - childno, child_name, child_value,
  382.                      child_type, child_path_expr);
  383.                 }
  384.               else
  385.                 ada_varobj_describe_child (elt_value, elt_type,
  386.                                            parent_name, parent_path_expr,
  387.                                            child_index - childno,
  388.                                            child_name, child_value,
  389.                                            child_type, child_path_expr);
  390.               return;
  391.             }

  392.           /* The child we're looking for is beyond this wrapper
  393.              field, so skip all its children.  */
  394.           childno += elt_n_children;
  395.           continue;
  396.         }
  397.       else if (ada_is_variant_part (parent_type, fieldno))
  398.         {
  399.           /* In normal situations, the variant part of the record should
  400.              have been "fixed". Or, in other words, it should have been
  401.              replaced by the branch of the variant part that is relevant
  402.              for our value.  But there are still situations where this
  403.              can happen, however (Eg. when our parent is a NULL pointer).
  404.              We do not support showing this part of the record for now,
  405.              so just pretend this field does not exist.  */
  406.           continue;
  407.         }

  408.       if (childno == child_index)
  409.         {
  410.           if (child_name)
  411.             {
  412.               /* The name of the child is none other than the field's
  413.                  name, except that we need to strip suffixes from it.
  414.                  For instance, fields with alignment constraints will
  415.                  have an __XVA suffix added to them.  */
  416.               const char *field_name = TYPE_FIELD_NAME (parent_type, fieldno);
  417.               int child_name_len = ada_name_prefix_len (field_name);

  418.               *child_name = xstrprintf ("%.*s", child_name_len, field_name);
  419.             }

  420.           if (child_value && parent_value)
  421.             ada_varobj_struct_elt (parent_value, parent_type, fieldno,
  422.                                    child_value, NULL);

  423.           if (child_type)
  424.             ada_varobj_struct_elt (parent_value, parent_type, fieldno,
  425.                                    NULL, child_type);

  426.           if (child_path_expr)
  427.             {
  428.               /* The name of the child is none other than the field's
  429.                  name, except that we need to strip suffixes from it.
  430.                  For instance, fields with alignment constraints will
  431.                  have an __XVA suffix added to them.  */
  432.               const char *field_name = TYPE_FIELD_NAME (parent_type, fieldno);
  433.               int child_name_len = ada_name_prefix_len (field_name);

  434.               *child_path_expr =
  435.                 xstrprintf ("(%s).%.*s", parent_path_expr,
  436.                             child_name_len, field_name);
  437.             }

  438.           return;
  439.         }

  440.       childno++;
  441.     }

  442.   /* Something went wrong.  Either we miscounted the number of
  443.      children, or CHILD_INDEX was too high.  But we should never
  444.      reach here.  We don't have enough information to recover
  445.      nicely, so just raise an assertion failure.  */
  446.   gdb_assert_not_reached ("unexpected code path");
  447. }

  448. /* Same as ada_varobj_describe_child, but limited to pointer objects.

  449.    Note that CHILD_INDEX is unused in this situation, but still provided
  450.    for consistency of interface with other routines describing an object's
  451.    child.  */

  452. static void
  453. ada_varobj_describe_ptr_child (struct value *parent_value,
  454.                                struct type *parent_type,
  455.                                const char *parent_name,
  456.                                const char *parent_path_expr,
  457.                                int child_index,
  458.                                char **child_name,
  459.                                struct value **child_value,
  460.                                struct type **child_type,
  461.                                char **child_path_expr)
  462. {
  463.   if (child_name)
  464.     *child_name = xstrprintf ("%s.all", parent_name);

  465.   if (child_value && parent_value)
  466.     ada_varobj_ind (parent_value, parent_type, child_value, NULL);

  467.   if (child_type)
  468.     ada_varobj_ind (parent_value, parent_type, NULL, child_type);

  469.   if (child_path_expr)
  470.     *child_path_expr = xstrprintf ("(%s).all", parent_path_expr);
  471. }

  472. /* Same as ada_varobj_describe_child, limited to simple array objects
  473.    (TYPE_CODE_ARRAY only).

  474.    Assumes that the (PARENT_VALUE, PARENT_TYPE) pair is properly decoded.
  475.    This is done by ada_varobj_describe_child before calling us.  */

  476. static void
  477. ada_varobj_describe_simple_array_child (struct value *parent_value,
  478.                                         struct type *parent_type,
  479.                                         const char *parent_name,
  480.                                         const char *parent_path_expr,
  481.                                         int child_index,
  482.                                         char **child_name,
  483.                                         struct value **child_value,
  484.                                         struct type **child_type,
  485.                                         char **child_path_expr)
  486. {
  487.   struct type *index_type;
  488.   int real_index;

  489.   gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY);

  490.   index_type = TYPE_INDEX_TYPE (parent_type);
  491.   real_index = child_index + ada_discrete_type_low_bound (index_type);

  492.   if (child_name)
  493.     *child_name = ada_varobj_scalar_image (index_type, real_index);

  494.   if (child_value && parent_value)
  495.     ada_varobj_simple_array_elt (parent_value, parent_type, real_index,
  496.                                  child_value, NULL);

  497.   if (child_type)
  498.     ada_varobj_simple_array_elt (parent_value, parent_type, real_index,
  499.                                  NULL, child_type);

  500.   if (child_path_expr)
  501.     {
  502.       char *index_img = ada_varobj_scalar_image (index_type, real_index);
  503.       struct cleanup *cleanups = make_cleanup (xfree, index_img);

  504.       /* Enumeration litterals by themselves are potentially ambiguous.
  505.          For instance, consider the following package spec:

  506.             package Pck is
  507.                type Color is (Red, Green, Blue, White);
  508.                type Blood_Cells is (White, Red);
  509.             end Pck;

  510.          In this case, the litteral "red" for instance, or even
  511.          the fully-qualified litteral "pck.red" cannot be resolved
  512.          by itself.  Type qualification is needed to determine which
  513.          enumeration litterals should be used.

  514.          The following variable will be used to contain the name
  515.          of the array index type when such type qualification is
  516.          needed.  */
  517.       const char *index_type_name = NULL;

  518.       /* If the index type is a range type, find the base type.  */
  519.       while (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
  520.         index_type = TYPE_TARGET_TYPE (index_type);

  521.       if (TYPE_CODE (index_type) == TYPE_CODE_ENUM
  522.           || TYPE_CODE (index_type) == TYPE_CODE_BOOL)
  523.         {
  524.           index_type_name = ada_type_name (index_type);
  525.           if (index_type_name)
  526.             index_type_name = ada_decode (index_type_name);
  527.         }

  528.       if (index_type_name != NULL)
  529.         *child_path_expr =
  530.           xstrprintf ("(%s)(%.*s'(%s))", parent_path_expr,
  531.                       ada_name_prefix_len (index_type_name),
  532.                       index_type_name, index_img);
  533.       else
  534.         *child_path_expr =
  535.           xstrprintf ("(%s)(%s)", parent_path_expr, index_img);
  536.       do_cleanups (cleanups);
  537.     }
  538. }

  539. /* See description at declaration above.  */

  540. static void
  541. ada_varobj_describe_child (struct value *parent_value,
  542.                            struct type *parent_type,
  543.                            const char *parent_name,
  544.                            const char *parent_path_expr,
  545.                            int child_index,
  546.                            char **child_name,
  547.                            struct value **child_value,
  548.                            struct type **child_type,
  549.                            char **child_path_expr)
  550. {
  551.   /* We cannot compute the child's path expression without
  552.      the parent's path expression.  This is a pre-condition
  553.      for calling this function.  */
  554.   if (child_path_expr)
  555.     gdb_assert (parent_path_expr != NULL);

  556.   ada_varobj_decode_var (&parent_value, &parent_type);
  557.   ada_varobj_adjust_for_child_access (&parent_value, &parent_type);

  558.   if (child_name)
  559.     *child_name = NULL;
  560.   if (child_value)
  561.     *child_value = NULL;
  562.   if (child_type)
  563.     *child_type = NULL;
  564.   if (child_path_expr)
  565.     *child_path_expr = NULL;

  566.   if (ada_is_array_descriptor_type (parent_type)
  567.       && TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF)
  568.     {
  569.       ada_varobj_describe_ptr_child (parent_value, parent_type,
  570.                                      parent_name, parent_path_expr,
  571.                                      child_index, child_name,
  572.                                      child_value, child_type,
  573.                                      child_path_expr);
  574.       return;
  575.     }

  576.   if (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY)
  577.     {
  578.       ada_varobj_describe_simple_array_child
  579.         (parent_value, parent_type, parent_name, parent_path_expr,
  580.          child_index, child_name, child_value, child_type,
  581.          child_path_expr);
  582.       return;
  583.     }

  584.   if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT)
  585.     {
  586.       ada_varobj_describe_struct_child (parent_value, parent_type,
  587.                                         parent_name, parent_path_expr,
  588.                                         child_index, child_name,
  589.                                         child_value, child_type,
  590.                                         child_path_expr);
  591.       return;
  592.     }

  593.   if (TYPE_CODE (parent_type) == TYPE_CODE_PTR)
  594.     {
  595.       ada_varobj_describe_ptr_child (parent_value, parent_type,
  596.                                      parent_name, parent_path_expr,
  597.                                      child_index, child_name,
  598.                                      child_value, child_type,
  599.                                      child_path_expr);
  600.       return;
  601.     }

  602.   /* It should never happen.  But rather than crash, report dummy names
  603.      and return a NULL child_value.  */
  604.   if (child_name)
  605.     *child_name = xstrdup ("???");
  606. }

  607. /* Return the name of the child number CHILD_INDEX of the (PARENT_VALUE,
  608.    PARENT_TYPE) pair.  PARENT_NAME is the name of the PARENT.

  609.    The result should be deallocated after use with xfree.  */

  610. static char *
  611. ada_varobj_get_name_of_child (struct value *parent_value,
  612.                               struct type *parent_type,
  613.                               const char *parent_name, int child_index)
  614. {
  615.   char *child_name;

  616.   ada_varobj_describe_child (parent_value, parent_type, parent_name,
  617.                              NULL, child_index, &child_name, NULL,
  618.                              NULL, NULL);
  619.   return child_name;
  620. }

  621. /* Return the path expression of the child number CHILD_INDEX of
  622.    the (PARENT_VALUE, PARENT_TYPE) pair.  PARENT_NAME is the name
  623.    of the parent, and PARENT_PATH_EXPR is the parent's path expression.
  624.    Both must be non-NULL.

  625.    The result must be deallocated after use with xfree.  */

  626. static char *
  627. ada_varobj_get_path_expr_of_child (struct value *parent_value,
  628.                                    struct type *parent_type,
  629.                                    const char *parent_name,
  630.                                    const char *parent_path_expr,
  631.                                    int child_index)
  632. {
  633.   char *child_path_expr;

  634.   ada_varobj_describe_child (parent_value, parent_type, parent_name,
  635.                              parent_path_expr, child_index, NULL,
  636.                              NULL, NULL, &child_path_expr);

  637.   return child_path_expr;
  638. }

  639. /* Return the value of child number CHILD_INDEX of the (PARENT_VALUE,
  640.    PARENT_TYPE) pair.  PARENT_NAME is the name of the parent.  */

  641. static struct value *
  642. ada_varobj_get_value_of_child (struct value *parent_value,
  643.                                struct type *parent_type,
  644.                                const char *parent_name, int child_index)
  645. {
  646.   struct value *child_value;

  647.   ada_varobj_describe_child (parent_value, parent_type, parent_name,
  648.                              NULL, child_index, NULL, &child_value,
  649.                              NULL, NULL);

  650.   return child_value;
  651. }

  652. /* Return the type of child number CHILD_INDEX of the (PARENT_VALUE,
  653.    PARENT_TYPE) pair.  */

  654. static struct type *
  655. ada_varobj_get_type_of_child (struct value *parent_value,
  656.                               struct type *parent_type,
  657.                               int child_index)
  658. {
  659.   struct type *child_type;

  660.   ada_varobj_describe_child (parent_value, parent_type, NULL, NULL,
  661.                              child_index, NULL, NULL, &child_type, NULL);

  662.   return child_type;
  663. }

  664. /* Return a string that contains the image of the given VALUE, using
  665.    the print options OPTS as the options for formatting the result.

  666.    The resulting string must be deallocated after use with xfree.  */

  667. static char *
  668. ada_varobj_get_value_image (struct value *value,
  669.                             struct value_print_options *opts)
  670. {
  671.   char *result;
  672.   struct ui_file *buffer;
  673.   struct cleanup *old_chain;

  674.   buffer = mem_fileopen ();
  675.   old_chain = make_cleanup_ui_file_delete (buffer);

  676.   common_val_print (value, buffer, 0, opts, current_language);
  677.   result = ui_file_xstrdup (buffer, NULL);

  678.   do_cleanups (old_chain);
  679.   return result;
  680. }

  681. /* Assuming that the (VALUE, TYPE) pair designates an array varobj,
  682.    return a string that is suitable for use in the "value" field of
  683.    the varobj output.  Most of the time, this is the number of elements
  684.    in the array inside square brackets, but there are situations where
  685.    it's useful to add more info.

  686.    OPTS are the print options used when formatting the result.

  687.    The result should be deallocated after use using xfree.  */

  688. static char *
  689. ada_varobj_get_value_of_array_variable (struct value *value,
  690.                                         struct type *type,
  691.                                         struct value_print_options *opts)
  692. {
  693.   char *result;
  694.   const int numchild = ada_varobj_get_array_number_of_children (value, type);

  695.   /* If we have a string, provide its contents in the "value" field.
  696.      Otherwise, the only other way to inspect the contents of the string
  697.      is by looking at the value of each element, as in any other array,
  698.      which is not very convenient...  */
  699.   if (value
  700.       && ada_is_string_type (type)
  701.       && (opts->format == 0 || opts->format == 's'))
  702.     {
  703.       char *str;
  704.       struct cleanup *old_chain;

  705.       str = ada_varobj_get_value_image (value, opts);
  706.       old_chain = make_cleanup (xfree, str);
  707.       result = xstrprintf ("[%d] %s", numchild, str);
  708.       do_cleanups (old_chain);
  709.     }
  710.   else
  711.     result = xstrprintf ("[%d]", numchild);

  712.   return result;
  713. }

  714. /* Return a string representation of the (VALUE, TYPE) pair, using
  715.    the given print options OPTS as our formatting options.  */

  716. static char *
  717. ada_varobj_get_value_of_variable (struct value *value,
  718.                                   struct type *type,
  719.                                   struct value_print_options *opts)
  720. {
  721.   char *result = NULL;

  722.   ada_varobj_decode_var (&value, &type);

  723.   switch (TYPE_CODE (type))
  724.     {
  725.     case TYPE_CODE_STRUCT:
  726.     case TYPE_CODE_UNION:
  727.       result = xstrdup ("{...}");
  728.       break;
  729.     case TYPE_CODE_ARRAY:
  730.       result = ada_varobj_get_value_of_array_variable (value, type, opts);
  731.       break;
  732.     default:
  733.       if (!value)
  734.         result = xstrdup ("");
  735.       else
  736.         result = ada_varobj_get_value_image (value, opts);
  737.       break;
  738.     }

  739.   return result;
  740. }

  741. /* Ada specific callbacks for VAROBJs.  */

  742. static int
  743. ada_number_of_children (struct varobj *var)
  744. {
  745.   return ada_varobj_get_number_of_children (var->value, var->type);
  746. }

  747. static char *
  748. ada_name_of_variable (struct varobj *parent)
  749. {
  750.   return c_varobj_ops.name_of_variable (parent);
  751. }

  752. static char *
  753. ada_name_of_child (struct varobj *parent, int index)
  754. {
  755.   return ada_varobj_get_name_of_child (parent->value, parent->type,
  756.                                        parent->name, index);
  757. }

  758. static char*
  759. ada_path_expr_of_child (struct varobj *child)
  760. {
  761.   struct varobj *parent = child->parent;
  762.   const char *parent_path_expr = varobj_get_path_expr (parent);

  763.   return ada_varobj_get_path_expr_of_child (parent->value,
  764.                                             parent->type,
  765.                                             parent->name,
  766.                                             parent_path_expr,
  767.                                             child->index);
  768. }

  769. static struct value *
  770. ada_value_of_child (struct varobj *parent, int index)
  771. {
  772.   return ada_varobj_get_value_of_child (parent->value, parent->type,
  773.                                         parent->name, index);
  774. }

  775. static struct type *
  776. ada_type_of_child (struct varobj *parent, int index)
  777. {
  778.   return ada_varobj_get_type_of_child (parent->value, parent->type,
  779.                                        index);
  780. }

  781. static char *
  782. ada_value_of_variable (struct varobj *var, enum varobj_display_formats format)
  783. {
  784.   struct value_print_options opts;

  785.   varobj_formatted_print_options (&opts, format);

  786.   return ada_varobj_get_value_of_variable (var->value, var->type, &opts);
  787. }

  788. /* Implement the "value_is_changeable_p" routine for Ada.  */

  789. static int
  790. ada_value_is_changeable_p (struct varobj *var)
  791. {
  792.   struct type *type = var->value ? value_type (var->value) : var->type;

  793.   if (ada_is_array_descriptor_type (type)
  794.       && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
  795.     {
  796.       /* This is in reality a pointer to an unconstrained array.
  797.          its value is changeable.  */
  798.       return 1;
  799.     }

  800.   if (ada_is_string_type (type))
  801.     {
  802.       /* We display the contents of the string in the array's
  803.          "value" field.  The contents can change, so consider
  804.          that the array is changeable.  */
  805.       return 1;
  806.     }

  807.   return varobj_default_value_is_changeable_p (var);
  808. }

  809. /* Implement the "value_has_mutated" routine for Ada.  */

  810. static int
  811. ada_value_has_mutated (struct varobj *var, struct value *new_val,
  812.                        struct type *new_type)
  813. {
  814.   int i;
  815.   int from = -1;
  816.   int to = -1;

  817.   /* If the number of fields have changed, then for sure the type
  818.      has mutated.  */
  819.   if (ada_varobj_get_number_of_children (new_val, new_type)
  820.       != var->num_children)
  821.     return 1;

  822.   /* If the number of fields have remained the same, then we need
  823.      to check the name of each field.  If they remain the same,
  824.      then chances are the type hasn't mutated.  This is technically
  825.      an incomplete test, as the child's type might have changed
  826.      despite the fact that the name remains the same.  But we'll
  827.      handle this situation by saying that the child has mutated,
  828.      not this value.

  829.      If only part (or none!) of the children have been fetched,
  830.      then only check the ones we fetched.  It does not matter
  831.      to the frontend whether a child that it has not fetched yet
  832.      has mutated or not. So just assume it hasn't.  */

  833.   varobj_restrict_range (var->children, &from, &to);
  834.   for (i = from; i < to; i++)
  835.     if (strcmp (ada_varobj_get_name_of_child (new_val, new_type,
  836.                                               var->name, i),
  837.                 VEC_index (varobj_p, var->children, i)->name) != 0)
  838.       return 1;

  839.   return 0;
  840. }

  841. /* varobj operations for ada.  */

  842. const struct lang_varobj_ops ada_varobj_ops =
  843. {
  844.   ada_number_of_children,
  845.   ada_name_of_variable,
  846.   ada_name_of_child,
  847.   ada_path_expr_of_child,
  848.   ada_value_of_child,
  849.   ada_type_of_child,
  850.   ada_value_of_variable,
  851.   ada_value_is_changeable_p,
  852.   ada_value_has_mutated,
  853.   varobj_default_is_path_expr_parent
  854. };