2004-06-07 Randolph Chung <tausq@debian.org>
[deliverable/binutils-gdb.git] / gdb / varobj.c
index 118feb9742e10c99deb7f71d339774018a43ff82..c662518c82fa439f273bedfa6c03b91f608428b6 100644 (file)
@@ -23,6 +23,7 @@
 #include "language.h"
 #include "wrapper.h"
 #include "gdbcmd.h"
+#include "gdb_string.h"
 #include <math.h>
 
 #include "varobj.h"
@@ -52,7 +53,7 @@ struct varobj_root
   struct block *valid_block;
 
   /* The frame for this expression */
-  CORE_ADDR frame;
+  struct frame_id frame;
 
   /* If 1, "update" always recomputes the frame & valid block
      using the currently selected frame. */
@@ -91,7 +92,7 @@ struct varobj
   struct type *type;
 
   /* The value of this expression or subexpression.  This may be NULL. */
-  value_ptr value;
+  struct value *value;
 
   /* Did an error occur evaluating the expression or getting its value? */
   int error;
@@ -110,6 +111,9 @@ struct varobj
 
   /* The format of the output for this object */
   enum varobj_display_formats format;
+
+  /* Was this variable updated via a varobj_set_value operation */
+  int updated;
 };
 
 /* Every variable keeps a linked list of its children, described
@@ -188,7 +192,7 @@ static struct type *get_target_type (struct type *);
 
 static enum varobj_display_formats variable_default_display (struct varobj *);
 
-static int my_value_equal (value_ptr, value_ptr, int *);
+static int my_value_equal (struct value *, struct value *, int *);
 
 static void vpush (struct vstack **pstack, struct varobj *var);
 
@@ -208,9 +212,9 @@ static char *name_of_variable (struct varobj *);
 
 static char *name_of_child (struct varobj *, int);
 
-static value_ptr value_of_root (struct varobj **var_handle, int *);
+static struct value *value_of_root (struct varobj **var_handle, int *);
 
-static value_ptr value_of_child (struct varobj *parent, int index);
+static struct value *value_of_child (struct varobj *parent, int index);
 
 static struct type *type_of_child (struct varobj *var);
 
@@ -228,9 +232,9 @@ static char *c_name_of_variable (struct varobj *parent);
 
 static char *c_name_of_child (struct varobj *parent, int index);
 
-static value_ptr c_value_of_root (struct varobj **var_handle);
+static struct value *c_value_of_root (struct varobj **var_handle);
 
-static value_ptr c_value_of_child (struct varobj *parent, int index);
+static struct value *c_value_of_child (struct varobj *parent, int index);
 
 static struct type *c_type_of_child (struct varobj *parent, int index);
 
@@ -248,9 +252,9 @@ static char *cplus_name_of_variable (struct varobj *parent);
 
 static char *cplus_name_of_child (struct varobj *parent, int index);
 
-static value_ptr cplus_value_of_root (struct varobj **var_handle);
+static struct value *cplus_value_of_root (struct varobj **var_handle);
 
-static value_ptr cplus_value_of_child (struct varobj *parent, int index);
+static struct value *cplus_value_of_child (struct varobj *parent, int index);
 
 static struct type *cplus_type_of_child (struct varobj *parent, int index);
 
@@ -266,9 +270,9 @@ static char *java_name_of_variable (struct varobj *parent);
 
 static char *java_name_of_child (struct varobj *parent, int index);
 
-static value_ptr java_value_of_root (struct varobj **var_handle);
+static struct value *java_value_of_root (struct varobj **var_handle);
 
-static value_ptr java_value_of_child (struct varobj *parent, int index);
+static struct value *java_value_of_child (struct varobj *parent, int index);
 
 static struct type *java_type_of_child (struct varobj *parent, int index);
 
@@ -293,11 +297,11 @@ struct language_specific
   /* The name of the INDEX'th child of PARENT. */
   char *(*name_of_child) (struct varobj * parent, int index);
 
-  /* The value_ptr of the root variable ROOT. */
-  value_ptr (*value_of_root) (struct varobj ** root_handle);
+  /* The ``struct value *'' of the root variable ROOT. */
+  struct value *(*value_of_root) (struct varobj ** root_handle);
 
-  /* The value_ptr of the INDEX'th child of PARENT. */
-  value_ptr (*value_of_child) (struct varobj * parent, int index);
+  /* The ``struct value *'' of the INDEX'th child of PARENT. */
+  struct value *(*value_of_child) (struct varobj * parent, int index);
 
   /* The type of the INDEX'th child of PARENT. */
   struct type *(*type_of_child) (struct varobj * parent, int index);
@@ -392,6 +396,27 @@ static struct vlist **varobj_table;
 
 /* Creates a varobj (not its children) */
 
+/* Return the full FRAME which corresponds to the given CORE_ADDR
+   or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
+
+static struct frame_info *
+find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
+{
+  struct frame_info *frame = NULL;
+
+  if (frame_addr == (CORE_ADDR) 0)
+    return NULL;
+
+  while (1)
+    {
+      frame = get_prev_frame (frame);
+      if (frame == NULL)
+       return NULL;
+      if (get_frame_base_address (frame) == frame_addr)
+       return frame;
+    }
+}
+
 struct varobj *
 varobj_create (char *objname,
               char *expression, CORE_ADDR frame, enum varobj_type type)
@@ -416,8 +441,14 @@ varobj_create (char *objname,
 
       /* Allow creator to specify context of variable */
       if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
-       fi = selected_frame;
+       fi = deprecated_selected_frame;
       else
+       /* FIXME: cagney/2002-11-23: This code should be doing a
+          lookup using the frame ID and not just the frame's
+          ``address''.  This, of course, means an interface change.
+          However, with out that interface change ISAs, such as the
+          ia64 with its two stacks, won't work.  Similar goes for the
+          case where there is a frameless function.  */
        fi = find_frame_addr_in_frame_chain (frame);
 
       /* frame = -2 means always use selected frame */
@@ -426,7 +457,7 @@ varobj_create (char *objname,
 
       block = NULL;
       if (fi != NULL)
-       block = get_frame_block (fi);
+       block = get_frame_block (fi, 0);
 
       p = expression;
       innermost_block = NULL;
@@ -456,9 +487,9 @@ varobj_create (char *objname,
          Since select_frame is so benign, just call it for all cases. */
       if (fi != NULL)
        {
-         var->root->frame = FRAME_FP (fi);
-         old_fi = selected_frame;
-         select_frame (fi, -1);
+         var->root->frame = get_frame_id (fi);
+         old_fi = deprecated_selected_frame;
+         select_frame (fi);
        }
 
       /* We definitively need to catch errors here.
@@ -485,7 +516,7 @@ varobj_create (char *objname,
 
       /* Reset the selected frame */
       if (fi != NULL)
-       select_frame (old_fi, -1);
+       select_frame (old_fi);
     }
 
   /* If the variable object name is null, that means this
@@ -514,13 +545,13 @@ char *
 varobj_gen_name (void)
 {
   static int id = 0;
-  char obj_name[31];
+  char *obj_name;
 
   /* generate a name for this object */
   id++;
-  sprintf (obj_name, "var%d", id);
+  xasprintf (&obj_name, "var%d", id);
 
-  return xstrdup (obj_name);
+  return obj_name;
 }
 
 /* Given an "objname", returns the pointer to the corresponding varobj
@@ -696,7 +727,7 @@ varobj_list_children (struct varobj *var, struct varobj ***childlist)
 char *
 varobj_get_type (struct varobj *var)
 {
-  value_ptr val;
+  struct value *val;
   struct cleanup *old_chain;
   struct ui_file *stb;
   char *thetype;
@@ -710,7 +741,7 @@ varobj_get_type (struct varobj *var)
   stb = mem_fileopen ();
   old_chain = make_cleanup_ui_file_delete (stb);
 
-  /* To print the type, we simply create a zero value_ptr and
+  /* To print the type, we simply create a zero ``struct value *'' and
      cast it to our type. We then typeprint this variable. */
   val = value_zero (var->type, not_lval);
   type_print (VALUE_TYPE (val), "", stb, -1);
@@ -751,21 +782,21 @@ varobj_get_value (struct varobj *var)
 int
 varobj_set_value (struct varobj *var, char *expression)
 {
-  value_ptr val;
+  struct value *val;
+  int error;
   int offset = 0;
 
   /* The argument "expression" contains the variable's new value.
      We need to first construct a legal expression for this -- ugh! */
   /* Does this cover all the bases? */
   struct expression *exp;
-  value_ptr value;
+  struct value *value;
   int saved_input_radix = input_radix;
 
-  if (variable_editable (var) && !var->error)
+  if (var->value != NULL && variable_editable (var) && !var->error)
     {
       char *s = expression;
       int i;
-      value_ptr temp;
 
       input_radix = 10;                /* ALWAYS reset to decimal temporarily */
       if (!gdb_parse_exp_1 (&s, 0, 0, &exp))
@@ -778,36 +809,10 @@ varobj_set_value (struct varobj *var, char *expression)
          return 0;
        }
 
-      /* If our parent is "public", "private", or "protected", we could
-         be asking to modify the value of a baseclass. If so, we need to
-         adjust our address by the offset of our baseclass in the subclass,
-         since VALUE_ADDRESS (var->value) points at the start of the subclass.
-         For some reason, value_cast doesn't take care of this properly. */
-      temp = var->value;
-      if (var->parent != NULL && CPLUS_FAKE_CHILD (var->parent))
-       {
-         struct varobj *super, *sub;
-         struct type *type;
-         super = var->parent->parent;
-         sub = super->parent;
-         if (sub != NULL)
-           {
-             /* Yes, it is a baseclass */
-             type = get_type_deref (sub);
-
-             if (super->index < TYPE_N_BASECLASSES (type))
-               {
-                 temp = value_copy (var->value);
-                 for (i = 0; i < super->index; i++)
-                   offset += TYPE_LENGTH (TYPE_FIELD_TYPE (type, i));
-               }
-           }
-       }
-
-      VALUE_ADDRESS (temp) += offset;
-      if (!gdb_value_assign (temp, value, &val))
+      if (!my_value_equal (var->value, value, &error))
+        var->updated = 1;
+      if (!gdb_value_assign (var->value, value, &val))
        return 0;
-      VALUE_ADDRESS (val) -= offset;
       value_free (var->value);
       release_value (val);
       var->value = val;
@@ -876,10 +881,11 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
   struct varobj *v;
   struct varobj **cv;
   struct varobj **templist = NULL;
-  value_ptr new;
+  struct value *new;
   struct vstack *stack = NULL;
   struct vstack *result = NULL;
-  struct frame_info *old_fi;
+  struct frame_id old_fid;
+  struct frame_info *fi;
 
   /* sanity check: have we been passed a pointer? */
   if (changelist == NULL)
@@ -892,7 +898,7 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
 
   /* Save the selected stack frame, since we will need to change it
      in order to evaluate expressions. */
-  old_fi = selected_frame;
+  old_fid = get_frame_id (deprecated_selected_frame);
 
   /* Update the root variable. value_of_root can return NULL
      if the variable is no longer around, i.e. we stepped out of
@@ -920,10 +926,11 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
   /* If values are not equal, note that it's changed.
      There a couple of exceptions here, though.
      We don't want some types to be reported as "changed". */
-  else if (type_changeable (*varp)
-          && !my_value_equal ((*varp)->value, new, &error2))
+  else if (type_changeable (*varp) &&
+          ((*varp)->updated || !my_value_equal ((*varp)->value, new, &error2)))
     {
       vpush (&result, *varp);
+      (*varp)->updated = 0;
       changed++;
       /* error2 replaces var->error since this new value
          WILL replace the old one. */
@@ -960,10 +967,12 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
 
       /* Update this variable */
       new = value_of_child (v->parent, v->index);
-      if (type_changeable (v) && !my_value_equal (v->value, new, &error2))
+      if (type_changeable (v) && 
+          (v->updated || !my_value_equal (v->value, new, &error2)))
        {
          /* Note that it's changed */
          vpush (&result, v);
+         v->updated = 0;
          changed++;
        }
       /* error2 replaces v->error since this new value
@@ -1012,7 +1021,9 @@ varobj_update (struct varobj **varp, struct varobj ***changelist)
     }
 
   /* Restore selected frame */
-  select_frame (old_fi, -1);
+  fi = frame_find_by_id (old_fid);
+  if (fi)
+    select_frame (fi);
 
   if (type_changed)
     return -2;
@@ -1219,7 +1230,7 @@ child_exists (struct varobj *var, char *name)
 
   for (vc = var->children; vc != NULL; vc = vc->next)
     {
-      if (STREQ (vc->child->name, name))
+      if (strcmp (vc->child->name, name) == 0)
        return vc->child;
     }
 
@@ -1239,14 +1250,11 @@ create_child (struct varobj *parent, int index, char *name)
   child->name = name;
   child->index = index;
   child->value = value_of_child (parent, index);
-  if (child->value == NULL || parent->error)
+  if ((!CPLUS_FAKE_CHILD (child) && child->value == NULL) || parent->error)
     child->error = 1;
   child->parent = parent;
   child->root = parent->root;
-  childs_name =
-    (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2) *
-                     sizeof (char));
-  sprintf (childs_name, "%s.%s", parent->obj_name, name);
+  xasprintf (&childs_name, "%s.%s", parent->obj_name, name);
   child->obj_name = childs_name;
   install_variable (child);
 
@@ -1322,6 +1330,7 @@ new_variable (void)
   var->children = NULL;
   var->format = 0;
   var->root = NULL;
+  var->updated = 0;
 
   return var;
 }
@@ -1335,7 +1344,7 @@ new_root_variable (void)
   var->root->lang = NULL;
   var->root->exp = NULL;
   var->root->valid_block = NULL;
-  var->root->frame = (CORE_ADDR) -1;
+  var->root->frame = null_frame_id;
   var->root->use_selected_frame = 0;
   var->root->rootvar = NULL;
 
@@ -1370,17 +1379,19 @@ make_cleanup_free_variable (struct varobj *var)
   return make_cleanup (do_free_variable_cleanup, var);
 }
 
-/* This returns the type of the variable. This skips past typedefs
-   and returns the real type of the variable. It also dereferences
-   pointers and references. */
+/* This returns the type of the variable. It also skips past typedefs
+   to return the real type of the variable.
+
+   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
+   except within get_target_type and get_type. */
 static struct type *
 get_type (struct varobj *var)
 {
   struct type *type;
   type = var->type;
 
-  while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
-    type = TYPE_TARGET_TYPE (type);
+  if (type != NULL)
+    type = check_typedef (type);
 
   return type;
 }
@@ -1401,15 +1412,18 @@ get_type_deref (struct varobj *var)
 }
 
 /* This returns the target type (or NULL) of TYPE, also skipping
-   past typedefs, just like get_type (). */
+   past typedefs, just like get_type ().
+
+   NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
+   except within get_target_type and get_type. */
 static struct type *
 get_target_type (struct type *type)
 {
   if (type != NULL)
     {
       type = TYPE_TARGET_TYPE (type);
-      while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
-       type = TYPE_TARGET_TYPE (type);
+      if (type != NULL)
+       type = check_typedef (type);
     }
 
   return type;
@@ -1427,7 +1441,7 @@ variable_default_display (struct varobj *var)
    one is "safe" -- it NEVER longjmps. It determines if the VAR's
    value is the same as VAL2. */
 static int
-my_value_equal (value_ptr val1, value_ptr val2, int *error2)
+my_value_equal (struct value *val1, struct value *val2, int *error2)
 {
   int r, err1, err2;
 
@@ -1593,7 +1607,7 @@ name_of_child (struct varobj *var, int index)
   return (*var->root->lang->name_of_child) (var, index);
 }
 
-/* What is the value_ptr of the root variable VAR? 
+/* What is the ``struct value *'' of the root variable VAR? 
    TYPE_CHANGED controls what to do if the type of a
    use_selected_frame = 1 variable changes.  On input,
    TYPE_CHANGED = 1 means discard the old varobj, and replace
@@ -1603,7 +1617,7 @@ name_of_child (struct varobj *var, int index)
    old varobj pointer away somewhere before calling this.
    On return, TYPE_CHANGED will be 1 if the type has changed, and 
    0 otherwise. */
-static value_ptr
+static struct value *
 value_of_root (struct varobj **var_handle, int *type_changed)
 {
   struct varobj *var;
@@ -1662,17 +1676,23 @@ value_of_root (struct varobj **var_handle, int *type_changed)
   return (*var->root->lang->value_of_root) (var_handle);
 }
 
-/* What is the value_ptr for the INDEX'th child of PARENT? */
-static value_ptr
+/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
+static struct value *
 value_of_child (struct varobj *parent, int index)
 {
-  value_ptr value;
+  struct value *value;
 
   value = (*parent->root->lang->value_of_child) (parent, index);
 
   /* If we're being lazy, fetch the real value of the variable. */
   if (value != NULL && VALUE_LAZY (value))
-    gdb_value_fetch_lazy (value);
+    {
+      /* If we fail to fetch the value of the child, return
+         NULL so that callers notice that we're leaving an
+         error message. */
+      if (!gdb_value_fetch_lazy (value))
+       value = NULL;
+    }
 
   return value;
 }
@@ -1817,14 +1837,7 @@ c_name_of_child (struct varobj *parent, int index)
   switch (TYPE_CODE (type))
     {
     case TYPE_CODE_ARRAY:
-      {
-       /* We never get here unless parent->num_children is greater than 0... */
-       int len = 1;
-       while ((int) pow ((double) 10, (double) len) < index)
-         len++;
-       name = (char *) xmalloc (1 + len * sizeof (char));
-       sprintf (name, "%d", index);
-      }
+      xasprintf (&name, "%d", index);
       break;
 
     case TYPE_CODE_STRUCT:
@@ -1843,9 +1856,7 @@ c_name_of_child (struct varobj *parent, int index)
          break;
 
        default:
-         name =
-           (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char));
-         sprintf (name, "*%s", parent->name);
+         xasprintf (&name, "*%s", parent->name);
          break;
        }
       break;
@@ -1858,10 +1869,10 @@ c_name_of_child (struct varobj *parent, int index)
   return name;
 }
 
-static value_ptr
+static struct value *
 c_value_of_root (struct varobj **var_handle)
 {
-  value_ptr new_val;
+  struct value *new_val;
   struct varobj *var = *var_handle;
   struct frame_info *fi;
   int within_scope;
@@ -1878,14 +1889,11 @@ c_value_of_root (struct varobj **var_handle)
   else
     {
       reinit_frame_cache ();
-
-
-      fi = find_frame_addr_in_frame_chain (var->root->frame);
-
+      fi = frame_find_by_id (var->root->frame);
       within_scope = fi != NULL;
       /* FIXME: select_frame could fail */
       if (within_scope)
-       select_frame (fi, -1);
+       select_frame (fi);
     }
 
   if (within_scope)
@@ -1919,10 +1927,12 @@ c_value_of_root (struct varobj **var_handle)
   return NULL;
 }
 
-static value_ptr
+static struct value *
 c_value_of_child (struct varobj *parent, int index)
 {
-  value_ptr value, temp, indval;
+  struct value *value;
+  struct value *temp;
+  struct value *indval;
   struct type *type, *target;
   char *name;
 
@@ -1950,7 +1960,8 @@ c_value_of_child (struct varobj *parent, int index)
 
        case TYPE_CODE_STRUCT:
        case TYPE_CODE_UNION:
-         value = value_struct_elt (&temp, NULL, name, NULL, "vstructure");
+         gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
+                               "vstructure");
          break;
 
        case TYPE_CODE_PTR:
@@ -1958,8 +1969,8 @@ c_value_of_child (struct varobj *parent, int index)
            {
            case TYPE_CODE_STRUCT:
            case TYPE_CODE_UNION:
-             value =
-               value_struct_elt (&temp, NULL, name, NULL, "vstructure");
+             gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
+                                   "vstructure");
              break;
 
            default:
@@ -1976,6 +1987,7 @@ c_value_of_child (struct varobj *parent, int index)
   if (value != NULL)
     release_value (value);
 
+  xfree (name);
   return value;
 }
 
@@ -1988,7 +2000,7 @@ c_type_of_child (struct varobj *parent, int index)
   switch (TYPE_CODE (parent->type))
     {
     case TYPE_CODE_ARRAY:
-      type = TYPE_TARGET_TYPE (parent->type);
+      type = get_target_type (parent->type);
       break;
 
     case TYPE_CODE_STRUCT:
@@ -1997,7 +2009,7 @@ c_type_of_child (struct varobj *parent, int index)
       break;
 
     case TYPE_CODE_PTR:
-      switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type)))
+      switch (TYPE_CODE (get_target_type (parent->type)))
        {
        case TYPE_CODE_STRUCT:
        case TYPE_CODE_UNION:
@@ -2005,7 +2017,7 @@ c_type_of_child (struct varobj *parent, int index)
          break;
 
        default:
-         type = TYPE_TARGET_TYPE (parent->type);
+         type = get_target_type (parent->type);
          break;
        }
       break;
@@ -2018,6 +2030,7 @@ c_type_of_child (struct varobj *parent, int index)
       break;
     }
 
+  xfree (name);
   return type;
 }
 
@@ -2044,22 +2057,10 @@ c_variable_editable (struct varobj *var)
 static char *
 c_value_of_variable (struct varobj *var)
 {
-  struct type *type;
-  value_ptr val;
-
-  if (var->value != NULL)
-    val = var->value;
-  else
-    {
-      /* This can happen if we attempt to get the value of a struct
-         member when the parent is an invalid pointer. */
-      return xstrdup ("???");
-    }
-
   /* BOGUS: if val_print sees a struct/class, it will print out its
      children instead of "{...}" */
-  type = get_type (var);
-  switch (TYPE_CODE (type))
+
+  switch (TYPE_CODE (get_type (var)))
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
@@ -2068,29 +2069,39 @@ c_value_of_variable (struct varobj *var)
 
     case TYPE_CODE_ARRAY:
       {
-       char number[18];
-       sprintf (number, "[%d]", var->num_children);
-       return xstrdup (number);
+       char *number;
+       xasprintf (&number, "[%d]", var->num_children);
+       return (number);
       }
       /* break; */
 
     default:
       {
-       long dummy;
-       struct ui_file *stb = mem_fileopen ();
-       struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
-       char *thevalue;
-
-       if (VALUE_LAZY (val))
-         gdb_value_fetch_lazy (val);
-       val_print (VALUE_TYPE (val), VALUE_CONTENTS_RAW (val), 0,
-                  VALUE_ADDRESS (val),
-                  stb, format_code[(int) var->format], 1, 0, 0);
-       thevalue = ui_file_xstrdup (stb, &dummy);
-       do_cleanups (old_chain);
+       if (var->value == NULL)
+         {
+           /* This can happen if we attempt to get the value of a struct
+              member when the parent is an invalid pointer. This is an
+              error condition, so we should tell the caller. */
+           return NULL;
+         }
+       else
+         {
+           long dummy;
+           struct ui_file *stb = mem_fileopen ();
+           struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
+           char *thevalue;
+
+           if (VALUE_LAZY (var->value))
+             gdb_value_fetch_lazy (var->value);
+           val_print (VALUE_TYPE (var->value),
+                      VALUE_CONTENTS_RAW (var->value), 0,
+                      VALUE_ADDRESS (var->value), stb,
+                      format_code[(int) var->format], 1, 0, 0);
+           thevalue = ui_file_xstrdup (stb, &dummy);
+           do_cleanups (old_chain);
        return thevalue;
       }
-      /* break; */
+      }
     }
 }
 \f
@@ -2137,9 +2148,9 @@ cplus_number_of_children (struct varobj *var)
       type = get_type_deref (var->parent);
 
       cplus_class_num_children (type, kids);
-      if (STREQ (var->name, "public"))
+      if (strcmp (var->name, "public") == 0)
        children = kids[v_public];
-      else if (STREQ (var->name, "private"))
+      else if (strcmp (var->name, "private") == 0)
        children = kids[v_private];
       else
        children = kids[v_protected];
@@ -2190,7 +2201,6 @@ cplus_name_of_child (struct varobj *parent, int index)
 {
   char *name;
   struct type *type;
-  int children[3];
 
   if (CPLUS_FAKE_CHILD (parent))
     {
@@ -2205,48 +2215,97 @@ cplus_name_of_child (struct varobj *parent, int index)
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
-      cplus_class_num_children (type, children);
-
       if (CPLUS_FAKE_CHILD (parent))
        {
-         /* FIXME: This assumes that type orders
-            inherited, public, private, protected */
-         int i = index + TYPE_N_BASECLASSES (type);
-         if (STREQ (parent->name, "private")
-             || STREQ (parent->name, "protected"))
-           i += children[v_public];
-         if (STREQ (parent->name, "protected"))
-           i += children[v_private];
-
-         name = TYPE_FIELD_NAME (type, i);
+         /* The fields of the class type are ordered as they
+            appear in the class.  We are given an index for a
+            particular access control type ("public","protected",
+            or "private").  We must skip over fields that don't
+            have the access control we are looking for to properly
+            find the indexed field. */
+         int type_index = TYPE_N_BASECLASSES (type);
+         if (strcmp (parent->name, "private") == 0)
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (TYPE_FIELD_PRIVATE (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+         else if (strcmp (parent->name, "protected") == 0)
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (TYPE_FIELD_PROTECTED (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+         else
+           {
+             while (index >= 0)
+               {
+                 if (TYPE_VPTR_BASETYPE (type) == type
+                     && type_index == TYPE_VPTR_FIELDNO (type))
+                   ; /* ignore vptr */
+                 else if (!TYPE_FIELD_PRIVATE (type, type_index) &&
+                     !TYPE_FIELD_PROTECTED (type, type_index))
+                   --index;
+                 ++type_index;
+               }
+             --type_index;
+           }
+
+         name = TYPE_FIELD_NAME (type, type_index);
        }
       else if (index < TYPE_N_BASECLASSES (type))
+       /* We are looking up the name of a base class */
        name = TYPE_FIELD_NAME (type, index);
       else
        {
+         int children[3];
+         cplus_class_num_children(type, children);
+
          /* Everything beyond the baseclasses can
-            only be "public", "private", or "protected" */
+            only be "public", "private", or "protected"
+
+            The special "fake" children are always output by varobj in
+            this order. So if INDEX == 2, it MUST be "protected". */
          index -= TYPE_N_BASECLASSES (type);
          switch (index)
            {
            case 0:
-             if (children[v_public] != 0)
-               {
-                 name = "public";
-                 break;
-               }
+             if (children[v_public] > 0)
+               name = "public";
+             else if (children[v_private] > 0)
+               name = "private";
+             else 
+               name = "protected";
+             break;
            case 1:
-             if (children[v_private] != 0)
+             if (children[v_public] > 0)
                {
-                 name = "private";
-                 break;
+                 if (children[v_private] > 0)
+                   name = "private";
+                 else
+                   name = "protected";
                }
+             else if (children[v_private] > 0)
+               name = "protected";
+             break;
            case 2:
-             if (children[v_protected] != 0)
-               {
-                 name = "protected";
-                 break;
-               }
+             /* Must be protected */
+             name = "protected";
+             break;
            default:
              /* error! */
              break;
@@ -2269,18 +2328,17 @@ cplus_name_of_child (struct varobj *parent, int index)
   return name;
 }
 
-static value_ptr
+static struct value *
 cplus_value_of_root (struct varobj **var_handle)
 {
   return c_value_of_root (var_handle);
 }
 
-static value_ptr
+static struct value *
 cplus_value_of_child (struct varobj *parent, int index)
 {
   struct type *type;
-  value_ptr value;
-  char *name;
+  struct value *value;
 
   if (CPLUS_FAKE_CHILD (parent))
     type = get_type_deref (parent->parent);
@@ -2288,17 +2346,25 @@ cplus_value_of_child (struct varobj *parent, int index)
     type = get_type_deref (parent);
 
   value = NULL;
-  name = name_of_child (parent, index);
 
   if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
       ((TYPE_CODE (type)) == TYPE_CODE_UNION))
     {
       if (CPLUS_FAKE_CHILD (parent))
        {
-         value_ptr temp = parent->parent->value;
-         value = value_struct_elt (&temp, NULL, name,
-                                   NULL, "cplus_structure");
-         release_value (value);
+         char *name;
+         struct value *temp = parent->parent->value;
+
+         if (temp == NULL)
+           return NULL;
+
+         name = name_of_child (parent, index);
+         gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
+                               "cplus_structure");
+         if (value != NULL)
+           release_value (value);
+
+         xfree (name);
        }
       else if (index >= TYPE_N_BASECLASSES (type))
        {
@@ -2310,7 +2376,7 @@ cplus_value_of_child (struct varobj *parent, int index)
          /* Baseclass */
          if (parent->value != NULL)
            {
-             value_ptr temp;
+             struct value *temp = NULL;
 
              if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
                  || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
@@ -2321,8 +2387,17 @@ cplus_value_of_child (struct varobj *parent, int index)
              else
                temp = parent->value;
 
-             value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
-             release_value (value);
+             if (temp != NULL)
+               {
+                 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
+                 release_value (value);
+               }
+             else
+               {
+                 /* We failed to evaluate the parent's value, so don't even
+                    bother trying to evaluate this child. */
+                 return NULL;
+               }
            }
        }
     }
@@ -2338,21 +2413,31 @@ cplus_type_of_child (struct varobj *parent, int index)
 {
   struct type *type, *t;
 
-  t = get_type_deref (parent);
+  if (CPLUS_FAKE_CHILD (parent))
+    {
+      /* Looking for the type of a child of public, private, or protected. */
+      t = get_type_deref (parent->parent);
+    }
+  else
+    t = get_type_deref (parent);
+
   type = NULL;
   switch (TYPE_CODE (t))
     {
     case TYPE_CODE_STRUCT:
     case TYPE_CODE_UNION:
-      if (index >= TYPE_N_BASECLASSES (t))
+      if (CPLUS_FAKE_CHILD (parent))
        {
-         /* special */
-         return NULL;
+         char *name = cplus_name_of_child (parent, index);
+         type = lookup_struct_elt_type (t, name, 0);
+         xfree (name);
        }
+      else if (index < TYPE_N_BASECLASSES (t))
+       type = TYPE_FIELD_TYPE (t, index);
       else
        {
-         /* Baseclass */
-         type = TYPE_FIELD_TYPE (t, index);
+         /* special */
+         return NULL;
        }
       break;
 
@@ -2434,13 +2519,13 @@ java_name_of_child (struct varobj *parent, int index)
   return name;
 }
 
-static value_ptr
+static struct value *
 java_value_of_root (struct varobj **var_handle)
 {
   return cplus_value_of_root (var_handle);
 }
 
-static value_ptr
+static struct value *
 java_value_of_child (struct varobj *parent, int index)
 {
   return cplus_value_of_child (parent, index);
This page took 0.036787 seconds and 4 git commands to generate.