gdb/c-varobj.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* varobj support for C and C++.

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

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

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

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

  13. #include "defs.h"
  14. #include "value.h"
  15. #include "varobj.h"
  16. #include "gdbthread.h"
  17. #include "valprint.h"

  18. static void cplus_class_num_children (struct type *type, int children[3]);

  19. /* The names of varobjs representing anonymous structs or unions.  */
  20. #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
  21. #define ANONYMOUS_UNION_NAME _("<anonymous union>")

  22. /* Does CHILD represent a child with no name?  This happens when
  23.    the child is an anonmous struct or union and it has no field name
  24.    in its parent variable.

  25.    This has already been determined by *_describe_child. The easiest
  26.    thing to do is to compare the child's name with ANONYMOUS_*_NAME.  */

  27. int
  28. varobj_is_anonymous_child (struct varobj *child)
  29. {
  30.   return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
  31.           || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
  32. }

  33. /* Given the value and the type of a variable object,
  34.    adjust the value and type to those necessary
  35.    for getting children of the variable object.
  36.    This includes dereferencing top-level references
  37.    to all types and dereferencing pointers to
  38.    structures.

  39.    If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
  40.    value will be fetched and if it differs from static type
  41.    the value will be casted to it.

  42.    Both TYPE and *TYPE should be non-null.  VALUE
  43.    can be null if we want to only translate type.
  44.    *VALUE can be null as well -- if the parent
  45.    value is not known.

  46.    If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
  47.    depending on whether pointer was dereferenced
  48.    in this function.  */

  49. static void
  50. adjust_value_for_child_access (struct value **value,
  51.                                   struct type **type,
  52.                                   int *was_ptr,
  53.                                   int lookup_actual_type)
  54. {
  55.   gdb_assert (type && *type);

  56.   if (was_ptr)
  57.     *was_ptr = 0;

  58.   *type = check_typedef (*type);

  59.   /* The type of value stored in varobj, that is passed
  60.      to us, is already supposed to be
  61.      reference-stripped.  */

  62.   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);

  63.   /* Pointers to structures are treated just like
  64.      structures when accessing children.  Don't
  65.      dererences pointers to other types.  */
  66.   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
  67.     {
  68.       struct type *target_type = get_target_type (*type);
  69.       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
  70.           || TYPE_CODE (target_type) == TYPE_CODE_UNION)
  71.         {
  72.           if (value && *value)
  73.             {
  74.               volatile struct gdb_exception except;

  75.               TRY_CATCH (except, RETURN_MASK_ERROR)
  76.                 {
  77.                   *value = value_ind (*value);
  78.                 }

  79.               if (except.reason < 0)
  80.                 *value = NULL;
  81.             }
  82.           *type = target_type;
  83.           if (was_ptr)
  84.             *was_ptr = 1;
  85.         }
  86.     }

  87.   /* The 'get_target_type' function calls check_typedef on
  88.      result, so we can immediately check type code.  No
  89.      need to call check_typedef here.  */

  90.   /* Access a real type of the value (if necessary and possible).  */
  91.   if (value && *value && lookup_actual_type)
  92.     {
  93.       struct type *enclosing_type;
  94.       int real_type_found = 0;

  95.       enclosing_type = value_actual_type (*value, 1, &real_type_found);
  96.       if (real_type_found)
  97.         {
  98.           *type = enclosing_type;
  99.           *value = value_cast (enclosing_type, *value);
  100.         }
  101.     }
  102. }

  103. /* Is VAR a path expression parent, i.e., can it be used to construct
  104.    a valid path expression?  */

  105. static int
  106. c_is_path_expr_parent (struct varobj *var)
  107. {
  108.   struct type *type;

  109.   /* "Fake" children are not path_expr parents.  */
  110.   if (CPLUS_FAKE_CHILD (var))
  111.     return 0;

  112.   type = varobj_get_gdb_type (var);

  113.   /* Anonymous unions and structs are also not path_expr parents.  */
  114.   if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
  115.        || TYPE_CODE (type) == TYPE_CODE_UNION)
  116.       && TYPE_NAME (type) == NULL
  117.       && TYPE_TAG_NAME (type) == NULL)
  118.     {
  119.       struct varobj *parent = var->parent;

  120.       while (parent != NULL && CPLUS_FAKE_CHILD (parent))
  121.         parent = parent->parent;

  122.       if (parent != NULL)
  123.         {
  124.           struct type *parent_type;
  125.           int was_ptr;

  126.           parent_type = varobj_get_value_type (parent);
  127.           adjust_value_for_child_access (NULL, &parent_type, &was_ptr, 0);

  128.           if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
  129.               || TYPE_CODE (parent_type) == TYPE_CODE_UNION)
  130.             {
  131.               const char *field_name;

  132.               gdb_assert (var->index < TYPE_NFIELDS (parent_type));
  133.               field_name = TYPE_FIELD_NAME (parent_type, var->index);
  134.               return !(field_name == NULL || *field_name == '\0');
  135.             }
  136.         }

  137.       return 0;
  138.     }

  139.   return 1;
  140. }

  141. /* C */

  142. static int
  143. c_number_of_children (struct varobj *var)
  144. {
  145.   struct type *type = varobj_get_value_type (var);
  146.   int children = 0;
  147.   struct type *target;

  148.   adjust_value_for_child_access (NULL, &type, NULL, 0);
  149.   target = get_target_type (type);

  150.   switch (TYPE_CODE (type))
  151.     {
  152.     case TYPE_CODE_ARRAY:
  153.       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
  154.           && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
  155.         children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
  156.       else
  157.         /* If we don't know how many elements there are, don't display
  158.            any.  */
  159.         children = 0;
  160.       break;

  161.     case TYPE_CODE_STRUCT:
  162.     case TYPE_CODE_UNION:
  163.       children = TYPE_NFIELDS (type);
  164.       break;

  165.     case TYPE_CODE_PTR:
  166.       /* The type here is a pointer to non-struct.  Typically, pointers
  167.          have one child, except for function ptrs, which have no children,
  168.          and except for void*, as we don't know what to show.

  169.          We can show char* so we allow it to be dereferenced.  If you decide
  170.          to test for it, please mind that a little magic is necessary to
  171.          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
  172.          TYPE_NAME == "char".  */
  173.       if (TYPE_CODE (target) == TYPE_CODE_FUNC
  174.           || TYPE_CODE (target) == TYPE_CODE_VOID)
  175.         children = 0;
  176.       else
  177.         children = 1;
  178.       break;

  179.     default:
  180.       /* Other types have no children.  */
  181.       break;
  182.     }

  183.   return children;
  184. }

  185. static char *
  186. c_name_of_variable (struct varobj *parent)
  187. {
  188.   return xstrdup (parent->name);
  189. }

  190. /* Return the value of element TYPE_INDEX of a structure
  191.    value VALUE.  VALUE's type should be a structure,
  192.    or union, or a typedef to struct/union.

  193.    Returns NULL if getting the value fails.  Never throws.  */

  194. static struct value *
  195. value_struct_element_index (struct value *value, int type_index)
  196. {
  197.   struct value *result = NULL;
  198.   volatile struct gdb_exception e;
  199.   struct type *type = value_type (value);

  200.   type = check_typedef (type);

  201.   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
  202.               || TYPE_CODE (type) == TYPE_CODE_UNION);

  203.   TRY_CATCH (e, RETURN_MASK_ERROR)
  204.     {
  205.       if (field_is_static (&TYPE_FIELD (type, type_index)))
  206.         result = value_static_field (type, type_index);
  207.       else
  208.         result = value_primitive_field (value, 0, type_index, type);
  209.     }
  210.   if (e.reason < 0)
  211.     {
  212.       return NULL;
  213.     }
  214.   else
  215.     {
  216.       return result;
  217.     }
  218. }

  219. /* Obtain the information about child INDEX of the variable
  220.    object PARENT.
  221.    If CNAME is not null, sets *CNAME to the name of the child relative
  222.    to the parent.
  223.    If CVALUE is not null, sets *CVALUE to the value of the child.
  224.    If CTYPE is not null, sets *CTYPE to the type of the child.

  225.    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
  226.    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
  227.    to NULL.  */

  228. static void
  229. c_describe_child (struct varobj *parent, int index,
  230.                   char **cname, struct value **cvalue, struct type **ctype,
  231.                   char **cfull_expression)
  232. {
  233.   struct value *value = parent->value;
  234.   struct type *type = varobj_get_value_type (parent);
  235.   char *parent_expression = NULL;
  236.   int was_ptr;
  237.   volatile struct gdb_exception except;

  238.   if (cname)
  239.     *cname = NULL;
  240.   if (cvalue)
  241.     *cvalue = NULL;
  242.   if (ctype)
  243.     *ctype = NULL;
  244.   if (cfull_expression)
  245.     {
  246.       *cfull_expression = NULL;
  247.       parent_expression
  248.         = varobj_get_path_expr (varobj_get_path_expr_parent (parent));
  249.     }
  250.   adjust_value_for_child_access (&value, &type, &was_ptr, 0);

  251.   switch (TYPE_CODE (type))
  252.     {
  253.     case TYPE_CODE_ARRAY:
  254.       if (cname)
  255.         *cname
  256.           = xstrdup (int_string (index
  257.                                  + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
  258.                                  10, 1, 0, 0));

  259.       if (cvalue && value)
  260.         {
  261.           int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));

  262.           TRY_CATCH (except, RETURN_MASK_ERROR)
  263.             {
  264.               *cvalue = value_subscript (value, real_index);
  265.             }
  266.         }

  267.       if (ctype)
  268.         *ctype = get_target_type (type);

  269.       if (cfull_expression)
  270.         *cfull_expression =
  271.           xstrprintf ("(%s)[%s]", parent_expression,
  272.                       int_string (index
  273.                                   + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
  274.                                   10, 1, 0, 0));


  275.       break;

  276.     case TYPE_CODE_STRUCT:
  277.     case TYPE_CODE_UNION:
  278.       {
  279.         const char *field_name;

  280.         /* If the type is anonymous and the field has no name,
  281.            set an appropriate name.  */
  282.         field_name = TYPE_FIELD_NAME (type, index);
  283.         if (field_name == NULL || *field_name == '\0')
  284.           {
  285.             if (cname)
  286.               {
  287.                 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
  288.                     == TYPE_CODE_STRUCT)
  289.                   *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
  290.                 else
  291.                   *cname = xstrdup (ANONYMOUS_UNION_NAME);
  292.               }

  293.             if (cfull_expression)
  294.               *cfull_expression = xstrdup ("");
  295.           }
  296.         else
  297.           {
  298.             if (cname)
  299.               *cname = xstrdup (field_name);

  300.             if (cfull_expression)
  301.               {
  302.                 char *join = was_ptr ? "->" : ".";

  303.                 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
  304.                                                 join, field_name);
  305.               }
  306.           }

  307.         if (cvalue && value)
  308.           {
  309.             /* For C, varobj index is the same as type index.  */
  310.             *cvalue = value_struct_element_index (value, index);
  311.           }

  312.         if (ctype)
  313.           *ctype = TYPE_FIELD_TYPE (type, index);
  314.       }
  315.       break;

  316.     case TYPE_CODE_PTR:
  317.       if (cname)
  318.         *cname = xstrprintf ("*%s", parent->name);

  319.       if (cvalue && value)
  320.         {
  321.           TRY_CATCH (except, RETURN_MASK_ERROR)
  322.             {
  323.               *cvalue = value_ind (value);
  324.             }

  325.           if (except.reason < 0)
  326.             *cvalue = NULL;
  327.         }

  328.       /* Don't use get_target_type because it calls
  329.          check_typedef and here, we want to show the true
  330.          declared type of the variable.  */
  331.       if (ctype)
  332.         *ctype = TYPE_TARGET_TYPE (type);

  333.       if (cfull_expression)
  334.         *cfull_expression = xstrprintf ("*(%s)", parent_expression);

  335.       break;

  336.     default:
  337.       /* This should not happen.  */
  338.       if (cname)
  339.         *cname = xstrdup ("???");
  340.       if (cfull_expression)
  341.         *cfull_expression = xstrdup ("???");
  342.       /* Don't set value and type, we don't know then.  */
  343.     }
  344. }

  345. static char *
  346. c_name_of_child (struct varobj *parent, int index)
  347. {
  348.   char *name;

  349.   c_describe_child (parent, index, &name, NULL, NULL, NULL);
  350.   return name;
  351. }

  352. static char *
  353. c_path_expr_of_child (struct varobj *child)
  354. {
  355.   c_describe_child (child->parent, child->index, NULL, NULL, NULL,
  356.                     &child->path_expr);
  357.   return child->path_expr;
  358. }

  359. static struct value *
  360. c_value_of_child (struct varobj *parent, int index)
  361. {
  362.   struct value *value = NULL;

  363.   c_describe_child (parent, index, NULL, &value, NULL, NULL);
  364.   return value;
  365. }

  366. static struct type *
  367. c_type_of_child (struct varobj *parent, int index)
  368. {
  369.   struct type *type = NULL;

  370.   c_describe_child (parent, index, NULL, NULL, &type, NULL);
  371.   return type;
  372. }

  373. /* This returns the type of the variable.  It also skips past typedefs
  374.    to return the real type of the variable.  */

  375. static struct type *
  376. get_type (struct varobj *var)
  377. {
  378.   struct type *type;

  379.   type = var->type;
  380.   if (type != NULL)
  381.     type = check_typedef (type);

  382.   return type;
  383. }

  384. static char *
  385. c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
  386. {
  387.   /* BOGUS: if val_print sees a struct/class, or a reference to one,
  388.      it will print out its children instead of "{...}".  So we need to
  389.      catch that case explicitly.  */
  390.   struct type *type = get_type (var);

  391.   /* Strip top-level references.  */
  392.   while (TYPE_CODE (type) == TYPE_CODE_REF)
  393.     type = check_typedef (TYPE_TARGET_TYPE (type));

  394.   switch (TYPE_CODE (type))
  395.     {
  396.     case TYPE_CODE_STRUCT:
  397.     case TYPE_CODE_UNION:
  398.       return xstrdup ("{...}");
  399.       /* break; */

  400.     case TYPE_CODE_ARRAY:
  401.       {
  402.         char *number;

  403.         number = xstrprintf ("[%d]", var->num_children);
  404.         return (number);
  405.       }
  406.       /* break; */

  407.     default:
  408.       {
  409.         if (var->value == NULL)
  410.           {
  411.             /* This can happen if we attempt to get the value of a struct
  412.                member when the parent is an invalid pointer.  This is an
  413.                error condition, so we should tell the caller.  */
  414.             return NULL;
  415.           }
  416.         else
  417.           {
  418.             if (var->not_fetched && value_lazy (var->value))
  419.               /* Frozen variable and no value yet.  We don't
  420.                  implicitly fetch the value.  MI response will
  421.                  use empty string for the value, which is OK.  */
  422.               return NULL;

  423.             gdb_assert (varobj_value_is_changeable_p (var));
  424.             gdb_assert (!value_lazy (var->value));

  425.             /* If the specified format is the current one,
  426.                we can reuse print_value.  */
  427.             if (format == var->format)
  428.               return xstrdup (var->print_value);
  429.             else
  430.               return varobj_value_get_print_value (var->value, format, var);
  431.           }
  432.       }
  433.     }
  434. }


  435. /* varobj operations for c.  */

  436. const struct lang_varobj_ops c_varobj_ops =
  437. {
  438.    c_number_of_children,
  439.    c_name_of_variable,
  440.    c_name_of_child,
  441.    c_path_expr_of_child,
  442.    c_value_of_child,
  443.    c_type_of_child,
  444.    c_value_of_variable,
  445.    varobj_default_value_is_changeable_p,
  446.    NULL, /* value_has_mutated */
  447.    c_is_path_expr_parent  /* is_path_expr_parent */
  448. };

  449. /* A little convenience enum for dealing with C++/Java.  */
  450. enum vsections
  451. {
  452.   v_public = 0, v_private, v_protected
  453. };

  454. /* C++ */

  455. static int
  456. cplus_number_of_children (struct varobj *var)
  457. {
  458.   struct value *value = NULL;
  459.   struct type *type;
  460.   int children, dont_know;
  461.   int lookup_actual_type = 0;
  462.   struct value_print_options opts;

  463.   dont_know = 1;
  464.   children = 0;

  465.   get_user_print_options (&opts);

  466.   if (!CPLUS_FAKE_CHILD (var))
  467.     {
  468.       type = varobj_get_value_type (var);

  469.       /* It is necessary to access a real type (via RTTI).  */
  470.       if (opts.objectprint)
  471.         {
  472.           value = var->value;
  473.           lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
  474.                                 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
  475.         }
  476.       adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);

  477.       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT)
  478.           || ((TYPE_CODE (type)) == TYPE_CODE_UNION))
  479.         {
  480.           int kids[3];

  481.           cplus_class_num_children (type, kids);
  482.           if (kids[v_public] != 0)
  483.             children++;
  484.           if (kids[v_private] != 0)
  485.             children++;
  486.           if (kids[v_protected] != 0)
  487.             children++;

  488.           /* Add any baseclasses.  */
  489.           children += TYPE_N_BASECLASSES (type);
  490.           dont_know = 0;

  491.           /* FIXME: save children in var.  */
  492.         }
  493.     }
  494.   else
  495.     {
  496.       int kids[3];

  497.       type = varobj_get_value_type (var->parent);

  498.       /* It is necessary to access a real type (via RTTI).  */
  499.       if (opts.objectprint)
  500.         {
  501.           struct varobj *parent = var->parent;

  502.           value = parent->value;
  503.           lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
  504.                                 || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
  505.         }
  506.       adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);

  507.       cplus_class_num_children (type, kids);
  508.       if (strcmp (var->name, "public") == 0)
  509.         children = kids[v_public];
  510.       else if (strcmp (var->name, "private") == 0)
  511.         children = kids[v_private];
  512.       else
  513.         children = kids[v_protected];
  514.       dont_know = 0;
  515.     }

  516.   if (dont_know)
  517.     children = c_number_of_children (var);

  518.   return children;
  519. }

  520. /* Compute # of public, private, and protected variables in this class.
  521.    That means we need to descend into all baseclasses and find out
  522.    how many are there, too.  */

  523. static void
  524. cplus_class_num_children (struct type *type, int children[3])
  525. {
  526.   int i, vptr_fieldno;
  527.   struct type *basetype = NULL;

  528.   children[v_public] = 0;
  529.   children[v_private] = 0;
  530.   children[v_protected] = 0;

  531.   vptr_fieldno = get_vptr_fieldno (type, &basetype);
  532.   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
  533.     {
  534.       /* If we have a virtual table pointer, omit it.  Even if virtual
  535.          table pointers are not specifically marked in the debug info,
  536.          they should be artificial.  */
  537.       if ((type == basetype && i == vptr_fieldno)
  538.           || TYPE_FIELD_ARTIFICIAL (type, i))
  539.         continue;

  540.       if (TYPE_FIELD_PROTECTED (type, i))
  541.         children[v_protected]++;
  542.       else if (TYPE_FIELD_PRIVATE (type, i))
  543.         children[v_private]++;
  544.       else
  545.         children[v_public]++;
  546.     }
  547. }

  548. static char *
  549. cplus_name_of_variable (struct varobj *parent)
  550. {
  551.   return c_name_of_variable (parent);
  552. }

  553. enum accessibility { private_field, protected_field, public_field };

  554. /* Check if field INDEX of TYPE has the specified accessibility.
  555.    Return 0 if so and 1 otherwise.  */

  556. static int
  557. match_accessibility (struct type *type, int index, enum accessibility acc)
  558. {
  559.   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
  560.     return 1;
  561.   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
  562.     return 1;
  563.   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
  564.            && !TYPE_FIELD_PROTECTED (type, index))
  565.     return 1;
  566.   else
  567.     return 0;
  568. }

  569. static void
  570. cplus_describe_child (struct varobj *parent, int index,
  571.                       char **cname, struct value **cvalue, struct type **ctype,
  572.                       char **cfull_expression)
  573. {
  574.   struct value *value;
  575.   struct type *type;
  576.   int was_ptr;
  577.   int lookup_actual_type = 0;
  578.   char *parent_expression = NULL;
  579.   struct varobj *var;
  580.   struct value_print_options opts;

  581.   if (cname)
  582.     *cname = NULL;
  583.   if (cvalue)
  584.     *cvalue = NULL;
  585.   if (ctype)
  586.     *ctype = NULL;
  587.   if (cfull_expression)
  588.     *cfull_expression = NULL;

  589.   get_user_print_options (&opts);

  590.   var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
  591.   if (opts.objectprint)
  592.     lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
  593.                           || TYPE_CODE (var->type) == TYPE_CODE_PTR);
  594.   value = var->value;
  595.   type = varobj_get_value_type (var);
  596.   if (cfull_expression)
  597.     parent_expression
  598.       = varobj_get_path_expr (varobj_get_path_expr_parent (var));

  599.   adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);

  600.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  601.       || TYPE_CODE (type) == TYPE_CODE_UNION)
  602.     {
  603.       char *join = was_ptr ? "->" : ".";

  604.       if (CPLUS_FAKE_CHILD (parent))
  605.         {
  606.           /* The fields of the class type are ordered as they
  607.              appear in the class.  We are given an index for a
  608.              particular access control type ("public","protected",
  609.              or "private").  We must skip over fields that don't
  610.              have the access control we are looking for to properly
  611.              find the indexed field.  */
  612.           int type_index = TYPE_N_BASECLASSES (type);
  613.           enum accessibility acc = public_field;
  614.           int vptr_fieldno;
  615.           struct type *basetype = NULL;
  616.           const char *field_name;

  617.           vptr_fieldno = get_vptr_fieldno (type, &basetype);
  618.           if (strcmp (parent->name, "private") == 0)
  619.             acc = private_field;
  620.           else if (strcmp (parent->name, "protected") == 0)
  621.             acc = protected_field;

  622.           while (index >= 0)
  623.             {
  624.               if ((type == basetype && type_index == vptr_fieldno)
  625.                   || TYPE_FIELD_ARTIFICIAL (type, type_index))
  626.                 ; /* ignore vptr */
  627.               else if (match_accessibility (type, type_index, acc))
  628.                     --index;
  629.                   ++type_index;
  630.             }
  631.           --type_index;

  632.           /* If the type is anonymous and the field has no name,
  633.              set an appopriate name.  */
  634.           field_name = TYPE_FIELD_NAME (type, type_index);
  635.           if (field_name == NULL || *field_name == '\0')
  636.             {
  637.               if (cname)
  638.                 {
  639.                   if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
  640.                       == TYPE_CODE_STRUCT)
  641.                     *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
  642.                   else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
  643.                            == TYPE_CODE_UNION)
  644.                     *cname = xstrdup (ANONYMOUS_UNION_NAME);
  645.                 }

  646.               if (cfull_expression)
  647.                 *cfull_expression = xstrdup ("");
  648.             }
  649.           else
  650.             {
  651.               if (cname)
  652.                 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));

  653.               if (cfull_expression)
  654.                 *cfull_expression
  655.                   = xstrprintf ("((%s)%s%s)", parent_expression, join,
  656.                                 field_name);
  657.             }

  658.           if (cvalue && value)
  659.             *cvalue = value_struct_element_index (value, type_index);

  660.           if (ctype)
  661.             *ctype = TYPE_FIELD_TYPE (type, type_index);
  662.         }
  663.       else if (index < TYPE_N_BASECLASSES (type))
  664.         {
  665.           /* This is a baseclass.  */
  666.           if (cname)
  667.             *cname = xstrdup (TYPE_FIELD_NAME (type, index));

  668.           if (cvalue && value)
  669.             *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);

  670.           if (ctype)
  671.             {
  672.               *ctype = TYPE_FIELD_TYPE (type, index);
  673.             }

  674.           if (cfull_expression)
  675.             {
  676.               char *ptr = was_ptr ? "*" : "";

  677.               /* Cast the parent to the base' type.  Note that in gdb,
  678.                  expression like
  679.                          (Base1)d
  680.                  will create an lvalue, for all appearences, so we don't
  681.                  need to use more fancy:
  682.                          *(Base1*)(&d)
  683.                  construct.

  684.                  When we are in the scope of the base class or of one
  685.                  of its children, the type field name will be interpreted
  686.                  as a constructor, if it exists.  Therefore, we must
  687.                  indicate that the name is a class name by using the
  688.                  'class' keyword.  See PR mi/11912  */
  689.               *cfull_expression = xstrprintf ("(%s(class %s%s) %s)",
  690.                                               ptr,
  691.                                               TYPE_FIELD_NAME (type, index),
  692.                                               ptr,
  693.                                               parent_expression);
  694.             }
  695.         }
  696.       else
  697.         {
  698.           char *access = NULL;
  699.           int children[3];

  700.           cplus_class_num_children (type, children);

  701.           /* Everything beyond the baseclasses can
  702.              only be "public", "private", or "protected"

  703.              The special "fake" children are always output by varobj in
  704.              this order.  So if INDEX == 2, it MUST be "protected".  */
  705.           index -= TYPE_N_BASECLASSES (type);
  706.           switch (index)
  707.             {
  708.             case 0:
  709.               if (children[v_public] > 0)
  710.                  access = "public";
  711.               else if (children[v_private] > 0)
  712.                  access = "private";
  713.               else
  714.                  access = "protected";
  715.               break;
  716.             case 1:
  717.               if (children[v_public] > 0)
  718.                 {
  719.                   if (children[v_private] > 0)
  720.                     access = "private";
  721.                   else
  722.                     access = "protected";
  723.                 }
  724.               else if (children[v_private] > 0)
  725.                  access = "protected";
  726.               break;
  727.             case 2:
  728.               /* Must be protected.  */
  729.               access = "protected";
  730.               break;
  731.             default:
  732.               /* error!  */
  733.               break;
  734.             }

  735.           gdb_assert (access);
  736.           if (cname)
  737.             *cname = xstrdup (access);

  738.           /* Value and type and full expression are null here.  */
  739.         }
  740.     }
  741.   else
  742.     {
  743.       c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
  744.     }
  745. }

  746. static char *
  747. cplus_name_of_child (struct varobj *parent, int index)
  748. {
  749.   char *name = NULL;

  750.   cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
  751.   return name;
  752. }

  753. static char *
  754. cplus_path_expr_of_child (struct varobj *child)
  755. {
  756.   cplus_describe_child (child->parent, child->index, NULL, NULL, NULL,
  757.                         &child->path_expr);
  758.   return child->path_expr;
  759. }

  760. static struct value *
  761. cplus_value_of_child (struct varobj *parent, int index)
  762. {
  763.   struct value *value = NULL;

  764.   cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
  765.   return value;
  766. }

  767. static struct type *
  768. cplus_type_of_child (struct varobj *parent, int index)
  769. {
  770.   struct type *type = NULL;

  771.   cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
  772.   return type;
  773. }

  774. static char *
  775. cplus_value_of_variable (struct varobj *var,
  776.                          enum varobj_display_formats format)
  777. {

  778.   /* If we have one of our special types, don't print out
  779.      any value.  */
  780.   if (CPLUS_FAKE_CHILD (var))
  781.     return xstrdup ("");

  782.   return c_value_of_variable (var, format);
  783. }


  784. /* varobj operations for c++.  */

  785. const struct lang_varobj_ops cplus_varobj_ops =
  786. {
  787.    cplus_number_of_children,
  788.    cplus_name_of_variable,
  789.    cplus_name_of_child,
  790.    cplus_path_expr_of_child,
  791.    cplus_value_of_child,
  792.    cplus_type_of_child,
  793.    cplus_value_of_variable,
  794.    varobj_default_value_is_changeable_p,
  795.    NULL, /* value_has_mutated */
  796.    c_is_path_expr_parent  /* is_path_expr_parent */
  797. };