varobj.c revision 98944
198944Sobrien/* Implementation of the GDB variable objects API.
298944Sobrien   Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
398944Sobrien
498944Sobrien   This program is free software; you can redistribute it and/or modify
598944Sobrien   it under the terms of the GNU General Public License as published by
698944Sobrien   the Free Software Foundation; either version 2 of the License, or
798944Sobrien   (at your option) any later version.
898944Sobrien
998944Sobrien   This program is distributed in the hope that it will be useful,
1098944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1198944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1298944Sobrien   GNU General Public License for more details.
1398944Sobrien
1498944Sobrien   You should have received a copy of the GNU General Public License
1598944Sobrien   along with this program; if not, write to the Free Software
1698944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
1798944Sobrien   Boston, MA 02111-1307, USA.  */
1898944Sobrien
1998944Sobrien#include "defs.h"
2098944Sobrien#include "value.h"
2198944Sobrien#include "expression.h"
2298944Sobrien#include "frame.h"
2398944Sobrien#include "language.h"
2498944Sobrien#include "wrapper.h"
2598944Sobrien#include "gdbcmd.h"
2698944Sobrien#include <math.h>
2798944Sobrien
2898944Sobrien#include "varobj.h"
2998944Sobrien
3098944Sobrien/* Non-zero if we want to see trace of varobj level stuff.  */
3198944Sobrien
3298944Sobrienint varobjdebug = 0;
3398944Sobrien
3498944Sobrien/* String representations of gdb's format codes */
3598944Sobrienchar *varobj_format_string[] =
3698944Sobrien  { "natural", "binary", "decimal", "hexadecimal", "octal" };
3798944Sobrien
3898944Sobrien/* String representations of gdb's known languages */
3998944Sobrienchar *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
4098944Sobrien
4198944Sobrien/* Data structures */
4298944Sobrien
4398944Sobrien/* Every root variable has one of these structures saved in its
4498944Sobrien   varobj. Members which must be free'd are noted. */
4598944Sobrienstruct varobj_root
4698944Sobrien{
4798944Sobrien
4898944Sobrien  /* Alloc'd expression for this parent. */
4998944Sobrien  struct expression *exp;
5098944Sobrien
5198944Sobrien  /* Block for which this expression is valid */
5298944Sobrien  struct block *valid_block;
5398944Sobrien
5498944Sobrien  /* The frame for this expression */
5598944Sobrien  CORE_ADDR frame;
5698944Sobrien
5798944Sobrien  /* If 1, "update" always recomputes the frame & valid block
5898944Sobrien     using the currently selected frame. */
5998944Sobrien  int use_selected_frame;
6098944Sobrien
6198944Sobrien  /* Language info for this variable and its children */
6298944Sobrien  struct language_specific *lang;
6398944Sobrien
6498944Sobrien  /* The varobj for this root node. */
6598944Sobrien  struct varobj *rootvar;
6698944Sobrien
6798944Sobrien  /* Next root variable */
6898944Sobrien  struct varobj_root *next;
6998944Sobrien};
7098944Sobrien
7198944Sobrien/* Every variable in the system has a structure of this type defined
7298944Sobrien   for it. This structure holds all information necessary to manipulate
7398944Sobrien   a particular object variable. Members which must be freed are noted. */
7498944Sobrienstruct varobj
7598944Sobrien{
7698944Sobrien
7798944Sobrien  /* Alloc'd name of the variable for this object.. If this variable is a
7898944Sobrien     child, then this name will be the child's source name.
7998944Sobrien     (bar, not foo.bar) */
8098944Sobrien  /* NOTE: This is the "expression" */
8198944Sobrien  char *name;
8298944Sobrien
8398944Sobrien  /* The alloc'd name for this variable's object. This is here for
8498944Sobrien     convenience when constructing this object's children. */
8598944Sobrien  char *obj_name;
8698944Sobrien
8798944Sobrien  /* Index of this variable in its parent or -1 */
8898944Sobrien  int index;
8998944Sobrien
9098944Sobrien  /* The type of this variable. This may NEVER be NULL. */
9198944Sobrien  struct type *type;
9298944Sobrien
9398944Sobrien  /* The value of this expression or subexpression.  This may be NULL. */
9498944Sobrien  struct value *value;
9598944Sobrien
9698944Sobrien  /* Did an error occur evaluating the expression or getting its value? */
9798944Sobrien  int error;
9898944Sobrien
9998944Sobrien  /* The number of (immediate) children this variable has */
10098944Sobrien  int num_children;
10198944Sobrien
10298944Sobrien  /* If this object is a child, this points to its immediate parent. */
10398944Sobrien  struct varobj *parent;
10498944Sobrien
10598944Sobrien  /* A list of this object's children */
10698944Sobrien  struct varobj_child *children;
10798944Sobrien
10898944Sobrien  /* Description of the root variable. Points to root variable for children. */
10998944Sobrien  struct varobj_root *root;
11098944Sobrien
11198944Sobrien  /* The format of the output for this object */
11298944Sobrien  enum varobj_display_formats format;
11398944Sobrien};
11498944Sobrien
11598944Sobrien/* Every variable keeps a linked list of its children, described
11698944Sobrien   by the following structure. */
11798944Sobrien/* FIXME: Deprecated.  All should use vlist instead */
11898944Sobrien
11998944Sobrienstruct varobj_child
12098944Sobrien{
12198944Sobrien
12298944Sobrien  /* Pointer to the child's data */
12398944Sobrien  struct varobj *child;
12498944Sobrien
12598944Sobrien  /* Pointer to the next child */
12698944Sobrien  struct varobj_child *next;
12798944Sobrien};
12898944Sobrien
12998944Sobrien/* A stack of varobjs */
13098944Sobrien/* FIXME: Deprecated.  All should use vlist instead */
13198944Sobrien
13298944Sobrienstruct vstack
13398944Sobrien{
13498944Sobrien  struct varobj *var;
13598944Sobrien  struct vstack *next;
13698944Sobrien};
13798944Sobrien
13898944Sobrienstruct cpstack
13998944Sobrien{
14098944Sobrien  char *name;
14198944Sobrien  struct cpstack *next;
14298944Sobrien};
14398944Sobrien
14498944Sobrien/* A list of varobjs */
14598944Sobrien
14698944Sobrienstruct vlist
14798944Sobrien{
14898944Sobrien  struct varobj *var;
14998944Sobrien  struct vlist *next;
15098944Sobrien};
15198944Sobrien
15298944Sobrien/* Private function prototypes */
15398944Sobrien
15498944Sobrien/* Helper functions for the above subcommands. */
15598944Sobrien
15698944Sobrienstatic int delete_variable (struct cpstack **, struct varobj *, int);
15798944Sobrien
15898944Sobrienstatic void delete_variable_1 (struct cpstack **, int *,
15998944Sobrien			       struct varobj *, int, int);
16098944Sobrien
16198944Sobrienstatic int install_variable (struct varobj *);
16298944Sobrien
16398944Sobrienstatic void uninstall_variable (struct varobj *);
16498944Sobrien
16598944Sobrienstatic struct varobj *child_exists (struct varobj *, char *);
16698944Sobrien
16798944Sobrienstatic struct varobj *create_child (struct varobj *, int, char *);
16898944Sobrien
16998944Sobrienstatic void save_child_in_parent (struct varobj *, struct varobj *);
17098944Sobrien
17198944Sobrienstatic void remove_child_from_parent (struct varobj *, struct varobj *);
17298944Sobrien
17398944Sobrien/* Utility routines */
17498944Sobrien
17598944Sobrienstatic struct varobj *new_variable (void);
17698944Sobrien
17798944Sobrienstatic struct varobj *new_root_variable (void);
17898944Sobrien
17998944Sobrienstatic void free_variable (struct varobj *var);
18098944Sobrien
18198944Sobrienstatic struct cleanup *make_cleanup_free_variable (struct varobj *var);
18298944Sobrien
18398944Sobrienstatic struct type *get_type (struct varobj *var);
18498944Sobrien
18598944Sobrienstatic struct type *get_type_deref (struct varobj *var);
18698944Sobrien
18798944Sobrienstatic struct type *get_target_type (struct type *);
18898944Sobrien
18998944Sobrienstatic enum varobj_display_formats variable_default_display (struct varobj *);
19098944Sobrien
19198944Sobrienstatic int my_value_equal (struct value *, struct value *, int *);
19298944Sobrien
19398944Sobrienstatic void vpush (struct vstack **pstack, struct varobj *var);
19498944Sobrien
19598944Sobrienstatic struct varobj *vpop (struct vstack **pstack);
19698944Sobrien
19798944Sobrienstatic void cppush (struct cpstack **pstack, char *name);
19898944Sobrien
19998944Sobrienstatic char *cppop (struct cpstack **pstack);
20098944Sobrien
20198944Sobrien/* Language-specific routines. */
20298944Sobrien
20398944Sobrienstatic enum varobj_languages variable_language (struct varobj *var);
20498944Sobrien
20598944Sobrienstatic int number_of_children (struct varobj *);
20698944Sobrien
20798944Sobrienstatic char *name_of_variable (struct varobj *);
20898944Sobrien
20998944Sobrienstatic char *name_of_child (struct varobj *, int);
21098944Sobrien
21198944Sobrienstatic struct value *value_of_root (struct varobj **var_handle, int *);
21298944Sobrien
21398944Sobrienstatic struct value *value_of_child (struct varobj *parent, int index);
21498944Sobrien
21598944Sobrienstatic struct type *type_of_child (struct varobj *var);
21698944Sobrien
21798944Sobrienstatic int variable_editable (struct varobj *var);
21898944Sobrien
21998944Sobrienstatic char *my_value_of_variable (struct varobj *var);
22098944Sobrien
22198944Sobrienstatic int type_changeable (struct varobj *var);
22298944Sobrien
22398944Sobrien/* C implementation */
22498944Sobrien
22598944Sobrienstatic int c_number_of_children (struct varobj *var);
22698944Sobrien
22798944Sobrienstatic char *c_name_of_variable (struct varobj *parent);
22898944Sobrien
22998944Sobrienstatic char *c_name_of_child (struct varobj *parent, int index);
23098944Sobrien
23198944Sobrienstatic struct value *c_value_of_root (struct varobj **var_handle);
23298944Sobrien
23398944Sobrienstatic struct value *c_value_of_child (struct varobj *parent, int index);
23498944Sobrien
23598944Sobrienstatic struct type *c_type_of_child (struct varobj *parent, int index);
23698944Sobrien
23798944Sobrienstatic int c_variable_editable (struct varobj *var);
23898944Sobrien
23998944Sobrienstatic char *c_value_of_variable (struct varobj *var);
24098944Sobrien
24198944Sobrien/* C++ implementation */
24298944Sobrien
24398944Sobrienstatic int cplus_number_of_children (struct varobj *var);
24498944Sobrien
24598944Sobrienstatic void cplus_class_num_children (struct type *type, int children[3]);
24698944Sobrien
24798944Sobrienstatic char *cplus_name_of_variable (struct varobj *parent);
24898944Sobrien
24998944Sobrienstatic char *cplus_name_of_child (struct varobj *parent, int index);
25098944Sobrien
25198944Sobrienstatic struct value *cplus_value_of_root (struct varobj **var_handle);
25298944Sobrien
25398944Sobrienstatic struct value *cplus_value_of_child (struct varobj *parent, int index);
25498944Sobrien
25598944Sobrienstatic struct type *cplus_type_of_child (struct varobj *parent, int index);
25698944Sobrien
25798944Sobrienstatic int cplus_variable_editable (struct varobj *var);
25898944Sobrien
25998944Sobrienstatic char *cplus_value_of_variable (struct varobj *var);
26098944Sobrien
26198944Sobrien/* Java implementation */
26298944Sobrien
26398944Sobrienstatic int java_number_of_children (struct varobj *var);
26498944Sobrien
26598944Sobrienstatic char *java_name_of_variable (struct varobj *parent);
26698944Sobrien
26798944Sobrienstatic char *java_name_of_child (struct varobj *parent, int index);
26898944Sobrien
26998944Sobrienstatic struct value *java_value_of_root (struct varobj **var_handle);
27098944Sobrien
27198944Sobrienstatic struct value *java_value_of_child (struct varobj *parent, int index);
27298944Sobrien
27398944Sobrienstatic struct type *java_type_of_child (struct varobj *parent, int index);
27498944Sobrien
27598944Sobrienstatic int java_variable_editable (struct varobj *var);
27698944Sobrien
27798944Sobrienstatic char *java_value_of_variable (struct varobj *var);
27898944Sobrien
27998944Sobrien/* The language specific vector */
28098944Sobrien
28198944Sobrienstruct language_specific
28298944Sobrien{
28398944Sobrien
28498944Sobrien  /* The language of this variable */
28598944Sobrien  enum varobj_languages language;
28698944Sobrien
28798944Sobrien  /* The number of children of PARENT. */
28898944Sobrien  int (*number_of_children) (struct varobj * parent);
28998944Sobrien
29098944Sobrien  /* The name (expression) of a root varobj. */
29198944Sobrien  char *(*name_of_variable) (struct varobj * parent);
29298944Sobrien
29398944Sobrien  /* The name of the INDEX'th child of PARENT. */
29498944Sobrien  char *(*name_of_child) (struct varobj * parent, int index);
29598944Sobrien
29698944Sobrien  /* The ``struct value *'' of the root variable ROOT. */
29798944Sobrien  struct value *(*value_of_root) (struct varobj ** root_handle);
29898944Sobrien
29998944Sobrien  /* The ``struct value *'' of the INDEX'th child of PARENT. */
30098944Sobrien  struct value *(*value_of_child) (struct varobj * parent, int index);
30198944Sobrien
30298944Sobrien  /* The type of the INDEX'th child of PARENT. */
30398944Sobrien  struct type *(*type_of_child) (struct varobj * parent, int index);
30498944Sobrien
30598944Sobrien  /* Is VAR editable? */
30698944Sobrien  int (*variable_editable) (struct varobj * var);
30798944Sobrien
30898944Sobrien  /* The current value of VAR. */
30998944Sobrien  char *(*value_of_variable) (struct varobj * var);
31098944Sobrien};
31198944Sobrien
31298944Sobrien/* Array of known source language routines. */
31398944Sobrienstatic struct language_specific
31498944Sobrien  languages[vlang_end][sizeof (struct language_specific)] = {
31598944Sobrien  /* Unknown (try treating as C */
31698944Sobrien  {
31798944Sobrien   vlang_unknown,
31898944Sobrien   c_number_of_children,
31998944Sobrien   c_name_of_variable,
32098944Sobrien   c_name_of_child,
32198944Sobrien   c_value_of_root,
32298944Sobrien   c_value_of_child,
32398944Sobrien   c_type_of_child,
32498944Sobrien   c_variable_editable,
32598944Sobrien   c_value_of_variable}
32698944Sobrien  ,
32798944Sobrien  /* C */
32898944Sobrien  {
32998944Sobrien   vlang_c,
33098944Sobrien   c_number_of_children,
33198944Sobrien   c_name_of_variable,
33298944Sobrien   c_name_of_child,
33398944Sobrien   c_value_of_root,
33498944Sobrien   c_value_of_child,
33598944Sobrien   c_type_of_child,
33698944Sobrien   c_variable_editable,
33798944Sobrien   c_value_of_variable}
33898944Sobrien  ,
33998944Sobrien  /* C++ */
34098944Sobrien  {
34198944Sobrien   vlang_cplus,
34298944Sobrien   cplus_number_of_children,
34398944Sobrien   cplus_name_of_variable,
34498944Sobrien   cplus_name_of_child,
34598944Sobrien   cplus_value_of_root,
34698944Sobrien   cplus_value_of_child,
34798944Sobrien   cplus_type_of_child,
34898944Sobrien   cplus_variable_editable,
34998944Sobrien   cplus_value_of_variable}
35098944Sobrien  ,
35198944Sobrien  /* Java */
35298944Sobrien  {
35398944Sobrien   vlang_java,
35498944Sobrien   java_number_of_children,
35598944Sobrien   java_name_of_variable,
35698944Sobrien   java_name_of_child,
35798944Sobrien   java_value_of_root,
35898944Sobrien   java_value_of_child,
35998944Sobrien   java_type_of_child,
36098944Sobrien   java_variable_editable,
36198944Sobrien   java_value_of_variable}
36298944Sobrien};
36398944Sobrien
36498944Sobrien/* A little convenience enum for dealing with C++/Java */
36598944Sobrienenum vsections
36698944Sobrien{
36798944Sobrien  v_public = 0, v_private, v_protected
36898944Sobrien};
36998944Sobrien
37098944Sobrien/* Private data */
37198944Sobrien
37298944Sobrien/* Mappings of varobj_display_formats enums to gdb's format codes */
37398944Sobrienstatic int format_code[] = { 0, 't', 'd', 'x', 'o' };
37498944Sobrien
37598944Sobrien/* Header of the list of root variable objects */
37698944Sobrienstatic struct varobj_root *rootlist;
37798944Sobrienstatic int rootcount = 0;	/* number of root varobjs in the list */
37898944Sobrien
37998944Sobrien/* Prime number indicating the number of buckets in the hash table */
38098944Sobrien/* A prime large enough to avoid too many colisions */
38198944Sobrien#define VAROBJ_TABLE_SIZE 227
38298944Sobrien
38398944Sobrien/* Pointer to the varobj hash table (built at run time) */
38498944Sobrienstatic struct vlist **varobj_table;
38598944Sobrien
38698944Sobrien/* Is the variable X one of our "fake" children? */
38798944Sobrien#define CPLUS_FAKE_CHILD(x) \
38898944Sobrien((x) != NULL && (x)->type == NULL && (x)->value == NULL)
38998944Sobrien
39098944Sobrien
39198944Sobrien/* API Implementation */
39298944Sobrien
39398944Sobrien/* Creates a varobj (not its children) */
39498944Sobrien
39598944Sobrienstruct varobj *
39698944Sobrienvarobj_create (char *objname,
39798944Sobrien	       char *expression, CORE_ADDR frame, enum varobj_type type)
39898944Sobrien{
39998944Sobrien  struct varobj *var;
40098944Sobrien  struct frame_info *fi;
40198944Sobrien  struct frame_info *old_fi = NULL;
40298944Sobrien  struct block *block;
40398944Sobrien  struct cleanup *old_chain;
40498944Sobrien
40598944Sobrien  /* Fill out a varobj structure for the (root) variable being constructed. */
40698944Sobrien  var = new_root_variable ();
40798944Sobrien  old_chain = make_cleanup_free_variable (var);
40898944Sobrien
40998944Sobrien  if (expression != NULL)
41098944Sobrien    {
41198944Sobrien      char *p;
41298944Sobrien      enum varobj_languages lang;
41398944Sobrien
41498944Sobrien      /* Parse and evaluate the expression, filling in as much
41598944Sobrien         of the variable's data as possible */
41698944Sobrien
41798944Sobrien      /* Allow creator to specify context of variable */
41898944Sobrien      if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
41998944Sobrien	fi = selected_frame;
42098944Sobrien      else
42198944Sobrien	fi = find_frame_addr_in_frame_chain (frame);
42298944Sobrien
42398944Sobrien      /* frame = -2 means always use selected frame */
42498944Sobrien      if (type == USE_SELECTED_FRAME)
42598944Sobrien	var->root->use_selected_frame = 1;
42698944Sobrien
42798944Sobrien      block = NULL;
42898944Sobrien      if (fi != NULL)
42998944Sobrien	block = get_frame_block (fi);
43098944Sobrien
43198944Sobrien      p = expression;
43298944Sobrien      innermost_block = NULL;
43398944Sobrien      /* Wrap the call to parse expression, so we can
43498944Sobrien         return a sensible error. */
43598944Sobrien      if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
43698944Sobrien	{
43798944Sobrien	  return NULL;
43898944Sobrien	}
43998944Sobrien
44098944Sobrien      /* Don't allow variables to be created for types. */
44198944Sobrien      if (var->root->exp->elts[0].opcode == OP_TYPE)
44298944Sobrien	{
44398944Sobrien	  do_cleanups (old_chain);
44498944Sobrien	  fprintf_unfiltered (gdb_stderr,
44598944Sobrien			      "Attempt to use a type name as an expression.");
44698944Sobrien	  return NULL;
44798944Sobrien	}
44898944Sobrien
44998944Sobrien      var->format = variable_default_display (var);
45098944Sobrien      var->root->valid_block = innermost_block;
45198944Sobrien      var->name = savestring (expression, strlen (expression));
45298944Sobrien
45398944Sobrien      /* When the frame is different from the current frame,
45498944Sobrien         we must select the appropriate frame before parsing
45598944Sobrien         the expression, otherwise the value will not be current.
45698944Sobrien         Since select_frame is so benign, just call it for all cases. */
45798944Sobrien      if (fi != NULL)
45898944Sobrien	{
45998944Sobrien	  var->root->frame = FRAME_FP (fi);
46098944Sobrien	  old_fi = selected_frame;
46198944Sobrien	  select_frame (fi, -1);
46298944Sobrien	}
46398944Sobrien
46498944Sobrien      /* We definitively need to catch errors here.
46598944Sobrien         If evaluate_expression succeeds we got the value we wanted.
46698944Sobrien         But if it fails, we still go on with a call to evaluate_type()  */
46798944Sobrien      if (gdb_evaluate_expression (var->root->exp, &var->value))
46898944Sobrien	{
46998944Sobrien	  /* no error */
47098944Sobrien	  release_value (var->value);
47198944Sobrien	  if (VALUE_LAZY (var->value))
47298944Sobrien	    gdb_value_fetch_lazy (var->value);
47398944Sobrien	}
47498944Sobrien      else
47598944Sobrien	var->value = evaluate_type (var->root->exp);
47698944Sobrien
47798944Sobrien      var->type = VALUE_TYPE (var->value);
47898944Sobrien
47998944Sobrien      /* Set language info */
48098944Sobrien      lang = variable_language (var);
48198944Sobrien      var->root->lang = languages[lang];
48298944Sobrien
48398944Sobrien      /* Set ourselves as our root */
48498944Sobrien      var->root->rootvar = var;
48598944Sobrien
48698944Sobrien      /* Reset the selected frame */
48798944Sobrien      if (fi != NULL)
48898944Sobrien	select_frame (old_fi, -1);
48998944Sobrien    }
49098944Sobrien
49198944Sobrien  /* If the variable object name is null, that means this
49298944Sobrien     is a temporary variable, so don't install it. */
49398944Sobrien
49498944Sobrien  if ((var != NULL) && (objname != NULL))
49598944Sobrien    {
49698944Sobrien      var->obj_name = savestring (objname, strlen (objname));
49798944Sobrien
49898944Sobrien      /* If a varobj name is duplicated, the install will fail so
49998944Sobrien         we must clenup */
50098944Sobrien      if (!install_variable (var))
50198944Sobrien	{
50298944Sobrien	  do_cleanups (old_chain);
50398944Sobrien	  return NULL;
50498944Sobrien	}
50598944Sobrien    }
50698944Sobrien
50798944Sobrien  discard_cleanups (old_chain);
50898944Sobrien  return var;
50998944Sobrien}
51098944Sobrien
51198944Sobrien/* Generates an unique name that can be used for a varobj */
51298944Sobrien
51398944Sobrienchar *
51498944Sobrienvarobj_gen_name (void)
51598944Sobrien{
51698944Sobrien  static int id = 0;
51798944Sobrien  char obj_name[31];
51898944Sobrien
51998944Sobrien  /* generate a name for this object */
52098944Sobrien  id++;
52198944Sobrien  sprintf (obj_name, "var%d", id);
52298944Sobrien
52398944Sobrien  return xstrdup (obj_name);
52498944Sobrien}
52598944Sobrien
52698944Sobrien/* Given an "objname", returns the pointer to the corresponding varobj
52798944Sobrien   or NULL if not found */
52898944Sobrien
52998944Sobrienstruct varobj *
53098944Sobrienvarobj_get_handle (char *objname)
53198944Sobrien{
53298944Sobrien  struct vlist *cv;
53398944Sobrien  const char *chp;
53498944Sobrien  unsigned int index = 0;
53598944Sobrien  unsigned int i = 1;
53698944Sobrien
53798944Sobrien  for (chp = objname; *chp; chp++)
53898944Sobrien    {
53998944Sobrien      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
54098944Sobrien    }
54198944Sobrien
54298944Sobrien  cv = *(varobj_table + index);
54398944Sobrien  while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
54498944Sobrien    cv = cv->next;
54598944Sobrien
54698944Sobrien  if (cv == NULL)
54798944Sobrien    error ("Variable object not found");
54898944Sobrien
54998944Sobrien  return cv->var;
55098944Sobrien}
55198944Sobrien
55298944Sobrien/* Given the handle, return the name of the object */
55398944Sobrien
55498944Sobrienchar *
55598944Sobrienvarobj_get_objname (struct varobj *var)
55698944Sobrien{
55798944Sobrien  return var->obj_name;
55898944Sobrien}
55998944Sobrien
56098944Sobrien/* Given the handle, return the expression represented by the object */
56198944Sobrien
56298944Sobrienchar *
56398944Sobrienvarobj_get_expression (struct varobj *var)
56498944Sobrien{
56598944Sobrien  return name_of_variable (var);
56698944Sobrien}
56798944Sobrien
56898944Sobrien/* Deletes a varobj and all its children if only_children == 0,
56998944Sobrien   otherwise deletes only the children; returns a malloc'ed list of all the
57098944Sobrien   (malloc'ed) names of the variables that have been deleted (NULL terminated) */
57198944Sobrien
57298944Sobrienint
57398944Sobrienvarobj_delete (struct varobj *var, char ***dellist, int only_children)
57498944Sobrien{
57598944Sobrien  int delcount;
57698944Sobrien  int mycount;
57798944Sobrien  struct cpstack *result = NULL;
57898944Sobrien  char **cp;
57998944Sobrien
58098944Sobrien  /* Initialize a stack for temporary results */
58198944Sobrien  cppush (&result, NULL);
58298944Sobrien
58398944Sobrien  if (only_children)
58498944Sobrien    /* Delete only the variable children */
58598944Sobrien    delcount = delete_variable (&result, var, 1 /* only the children */ );
58698944Sobrien  else
58798944Sobrien    /* Delete the variable and all its children */
58898944Sobrien    delcount = delete_variable (&result, var, 0 /* parent+children */ );
58998944Sobrien
59098944Sobrien  /* We may have been asked to return a list of what has been deleted */
59198944Sobrien  if (dellist != NULL)
59298944Sobrien    {
59398944Sobrien      *dellist = xmalloc ((delcount + 1) * sizeof (char *));
59498944Sobrien
59598944Sobrien      cp = *dellist;
59698944Sobrien      mycount = delcount;
59798944Sobrien      *cp = cppop (&result);
59898944Sobrien      while ((*cp != NULL) && (mycount > 0))
59998944Sobrien	{
60098944Sobrien	  mycount--;
60198944Sobrien	  cp++;
60298944Sobrien	  *cp = cppop (&result);
60398944Sobrien	}
60498944Sobrien
60598944Sobrien      if (mycount || (*cp != NULL))
60698944Sobrien	warning ("varobj_delete: assertion failed - mycount(=%d) <> 0",
60798944Sobrien		 mycount);
60898944Sobrien    }
60998944Sobrien
61098944Sobrien  return delcount;
61198944Sobrien}
61298944Sobrien
61398944Sobrien/* Set/Get variable object display format */
61498944Sobrien
61598944Sobrienenum varobj_display_formats
61698944Sobrienvarobj_set_display_format (struct varobj *var,
61798944Sobrien			   enum varobj_display_formats format)
61898944Sobrien{
61998944Sobrien  switch (format)
62098944Sobrien    {
62198944Sobrien    case FORMAT_NATURAL:
62298944Sobrien    case FORMAT_BINARY:
62398944Sobrien    case FORMAT_DECIMAL:
62498944Sobrien    case FORMAT_HEXADECIMAL:
62598944Sobrien    case FORMAT_OCTAL:
62698944Sobrien      var->format = format;
62798944Sobrien      break;
62898944Sobrien
62998944Sobrien    default:
63098944Sobrien      var->format = variable_default_display (var);
63198944Sobrien    }
63298944Sobrien
63398944Sobrien  return var->format;
63498944Sobrien}
63598944Sobrien
63698944Sobrienenum varobj_display_formats
63798944Sobrienvarobj_get_display_format (struct varobj *var)
63898944Sobrien{
63998944Sobrien  return var->format;
64098944Sobrien}
64198944Sobrien
64298944Sobrienint
64398944Sobrienvarobj_get_num_children (struct varobj *var)
64498944Sobrien{
64598944Sobrien  if (var->num_children == -1)
64698944Sobrien    var->num_children = number_of_children (var);
64798944Sobrien
64898944Sobrien  return var->num_children;
64998944Sobrien}
65098944Sobrien
65198944Sobrien/* Creates a list of the immediate children of a variable object;
65298944Sobrien   the return code is the number of such children or -1 on error */
65398944Sobrien
65498944Sobrienint
65598944Sobrienvarobj_list_children (struct varobj *var, struct varobj ***childlist)
65698944Sobrien{
65798944Sobrien  struct varobj *child;
65898944Sobrien  char *name;
65998944Sobrien  int i;
66098944Sobrien
66198944Sobrien  /* sanity check: have we been passed a pointer? */
66298944Sobrien  if (childlist == NULL)
66398944Sobrien    return -1;
66498944Sobrien
66598944Sobrien  *childlist = NULL;
66698944Sobrien
66798944Sobrien  if (var->num_children == -1)
66898944Sobrien    var->num_children = number_of_children (var);
66998944Sobrien
67098944Sobrien  /* List of children */
67198944Sobrien  *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
67298944Sobrien
67398944Sobrien  for (i = 0; i < var->num_children; i++)
67498944Sobrien    {
67598944Sobrien      /* Mark as the end in case we bail out */
67698944Sobrien      *((*childlist) + i) = NULL;
67798944Sobrien
67898944Sobrien      /* check if child exists, if not create */
67998944Sobrien      name = name_of_child (var, i);
68098944Sobrien      child = child_exists (var, name);
68198944Sobrien      if (child == NULL)
68298944Sobrien	child = create_child (var, i, name);
68398944Sobrien
68498944Sobrien      *((*childlist) + i) = child;
68598944Sobrien    }
68698944Sobrien
68798944Sobrien  /* End of list is marked by a NULL pointer */
68898944Sobrien  *((*childlist) + i) = NULL;
68998944Sobrien
69098944Sobrien  return var->num_children;
69198944Sobrien}
69298944Sobrien
69398944Sobrien/* Obtain the type of an object Variable as a string similar to the one gdb
69498944Sobrien   prints on the console */
69598944Sobrien
69698944Sobrienchar *
69798944Sobrienvarobj_get_type (struct varobj *var)
69898944Sobrien{
69998944Sobrien  struct value *val;
70098944Sobrien  struct cleanup *old_chain;
70198944Sobrien  struct ui_file *stb;
70298944Sobrien  char *thetype;
70398944Sobrien  long length;
70498944Sobrien
70598944Sobrien  /* For the "fake" variables, do not return a type. (It's type is
70698944Sobrien     NULL, too.) */
70798944Sobrien  if (CPLUS_FAKE_CHILD (var))
70898944Sobrien    return NULL;
70998944Sobrien
71098944Sobrien  stb = mem_fileopen ();
71198944Sobrien  old_chain = make_cleanup_ui_file_delete (stb);
71298944Sobrien
71398944Sobrien  /* To print the type, we simply create a zero ``struct value *'' and
71498944Sobrien     cast it to our type. We then typeprint this variable. */
71598944Sobrien  val = value_zero (var->type, not_lval);
71698944Sobrien  type_print (VALUE_TYPE (val), "", stb, -1);
71798944Sobrien
71898944Sobrien  thetype = ui_file_xstrdup (stb, &length);
71998944Sobrien  do_cleanups (old_chain);
72098944Sobrien  return thetype;
72198944Sobrien}
72298944Sobrien
72398944Sobrienenum varobj_languages
72498944Sobrienvarobj_get_language (struct varobj *var)
72598944Sobrien{
72698944Sobrien  return variable_language (var);
72798944Sobrien}
72898944Sobrien
72998944Sobrienint
73098944Sobrienvarobj_get_attributes (struct varobj *var)
73198944Sobrien{
73298944Sobrien  int attributes = 0;
73398944Sobrien
73498944Sobrien  if (variable_editable (var))
73598944Sobrien    /* FIXME: define masks for attributes */
73698944Sobrien    attributes |= 0x00000001;	/* Editable */
73798944Sobrien
73898944Sobrien  return attributes;
73998944Sobrien}
74098944Sobrien
74198944Sobrienchar *
74298944Sobrienvarobj_get_value (struct varobj *var)
74398944Sobrien{
74498944Sobrien  return my_value_of_variable (var);
74598944Sobrien}
74698944Sobrien
74798944Sobrien/* Set the value of an object variable (if it is editable) to the
74898944Sobrien   value of the given expression */
74998944Sobrien/* Note: Invokes functions that can call error() */
75098944Sobrien
75198944Sobrienint
75298944Sobrienvarobj_set_value (struct varobj *var, char *expression)
75398944Sobrien{
75498944Sobrien  struct value *val;
75598944Sobrien  int offset = 0;
75698944Sobrien
75798944Sobrien  /* The argument "expression" contains the variable's new value.
75898944Sobrien     We need to first construct a legal expression for this -- ugh! */
75998944Sobrien  /* Does this cover all the bases? */
76098944Sobrien  struct expression *exp;
76198944Sobrien  struct value *value;
76298944Sobrien  int saved_input_radix = input_radix;
76398944Sobrien
76498944Sobrien  if (var->value != NULL && variable_editable (var) && !var->error)
76598944Sobrien    {
76698944Sobrien      char *s = expression;
76798944Sobrien      int i;
76898944Sobrien
76998944Sobrien      input_radix = 10;		/* ALWAYS reset to decimal temporarily */
77098944Sobrien      if (!gdb_parse_exp_1 (&s, 0, 0, &exp))
77198944Sobrien	/* We cannot proceed without a well-formed expression. */
77298944Sobrien	return 0;
77398944Sobrien      if (!gdb_evaluate_expression (exp, &value))
77498944Sobrien	{
77598944Sobrien	  /* We cannot proceed without a valid expression. */
77698944Sobrien	  xfree (exp);
77798944Sobrien	  return 0;
77898944Sobrien	}
77998944Sobrien
78098944Sobrien      if (!gdb_value_assign (var->value, value, &val))
78198944Sobrien	return 0;
78298944Sobrien      value_free (var->value);
78398944Sobrien      release_value (val);
78498944Sobrien      var->value = val;
78598944Sobrien      input_radix = saved_input_radix;
78698944Sobrien      return 1;
78798944Sobrien    }
78898944Sobrien
78998944Sobrien  return 0;
79098944Sobrien}
79198944Sobrien
79298944Sobrien/* Returns a malloc'ed list with all root variable objects */
79398944Sobrienint
79498944Sobrienvarobj_list (struct varobj ***varlist)
79598944Sobrien{
79698944Sobrien  struct varobj **cv;
79798944Sobrien  struct varobj_root *croot;
79898944Sobrien  int mycount = rootcount;
79998944Sobrien
80098944Sobrien  /* Alloc (rootcount + 1) entries for the result */
80198944Sobrien  *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
80298944Sobrien
80398944Sobrien  cv = *varlist;
80498944Sobrien  croot = rootlist;
80598944Sobrien  while ((croot != NULL) && (mycount > 0))
80698944Sobrien    {
80798944Sobrien      *cv = croot->rootvar;
80898944Sobrien      mycount--;
80998944Sobrien      cv++;
81098944Sobrien      croot = croot->next;
81198944Sobrien    }
81298944Sobrien  /* Mark the end of the list */
81398944Sobrien  *cv = NULL;
81498944Sobrien
81598944Sobrien  if (mycount || (croot != NULL))
81698944Sobrien    warning
81798944Sobrien      ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
81898944Sobrien       rootcount, mycount);
81998944Sobrien
82098944Sobrien  return rootcount;
82198944Sobrien}
82298944Sobrien
82398944Sobrien/* Update the values for a variable and its children.  This is a
82498944Sobrien   two-pronged attack.  First, re-parse the value for the root's
82598944Sobrien   expression to see if it's changed.  Then go all the way
82698944Sobrien   through its children, reconstructing them and noting if they've
82798944Sobrien   changed.
82898944Sobrien   Return value:
82998944Sobrien    -1 if there was an error updating the varobj
83098944Sobrien    -2 if the type changed
83198944Sobrien    Otherwise it is the number of children + parent changed
83298944Sobrien
83398944Sobrien   Only root variables can be updated...
83498944Sobrien
83598944Sobrien   NOTE: This function may delete the caller's varobj. If it
83698944Sobrien   returns -2, then it has done this and VARP will be modified
83798944Sobrien   to point to the new varobj. */
83898944Sobrien
83998944Sobrienint
84098944Sobrienvarobj_update (struct varobj **varp, struct varobj ***changelist)
84198944Sobrien{
84298944Sobrien  int changed = 0;
84398944Sobrien  int type_changed;
84498944Sobrien  int i;
84598944Sobrien  int vleft;
84698944Sobrien  int error2;
84798944Sobrien  struct varobj *v;
84898944Sobrien  struct varobj **cv;
84998944Sobrien  struct varobj **templist = NULL;
85098944Sobrien  struct value *new;
85198944Sobrien  struct vstack *stack = NULL;
85298944Sobrien  struct vstack *result = NULL;
85398944Sobrien  struct frame_info *old_fi;
85498944Sobrien
85598944Sobrien  /* sanity check: have we been passed a pointer? */
85698944Sobrien  if (changelist == NULL)
85798944Sobrien    return -1;
85898944Sobrien
85998944Sobrien  /*  Only root variables can be updated... */
86098944Sobrien  if ((*varp)->root->rootvar != *varp)
86198944Sobrien    /* Not a root var */
86298944Sobrien    return -1;
86398944Sobrien
86498944Sobrien  /* Save the selected stack frame, since we will need to change it
86598944Sobrien     in order to evaluate expressions. */
86698944Sobrien  old_fi = selected_frame;
86798944Sobrien
86898944Sobrien  /* Update the root variable. value_of_root can return NULL
86998944Sobrien     if the variable is no longer around, i.e. we stepped out of
87098944Sobrien     the frame in which a local existed. We are letting the
87198944Sobrien     value_of_root variable dispose of the varobj if the type
87298944Sobrien     has changed. */
87398944Sobrien  type_changed = 1;
87498944Sobrien  new = value_of_root (varp, &type_changed);
87598944Sobrien  if (new == NULL)
87698944Sobrien    {
87798944Sobrien      (*varp)->error = 1;
87898944Sobrien      return -1;
87998944Sobrien    }
88098944Sobrien
88198944Sobrien  /* Initialize a stack for temporary results */
88298944Sobrien  vpush (&result, NULL);
88398944Sobrien
88498944Sobrien  /* If this is a "use_selected_frame" varobj, and its type has changed,
88598944Sobrien     them note that it's changed. */
88698944Sobrien  if (type_changed)
88798944Sobrien    {
88898944Sobrien      vpush (&result, *varp);
88998944Sobrien      changed++;
89098944Sobrien    }
89198944Sobrien  /* If values are not equal, note that it's changed.
89298944Sobrien     There a couple of exceptions here, though.
89398944Sobrien     We don't want some types to be reported as "changed". */
89498944Sobrien  else if (type_changeable (*varp)
89598944Sobrien	   && !my_value_equal ((*varp)->value, new, &error2))
89698944Sobrien    {
89798944Sobrien      vpush (&result, *varp);
89898944Sobrien      changed++;
89998944Sobrien      /* error2 replaces var->error since this new value
90098944Sobrien         WILL replace the old one. */
90198944Sobrien      (*varp)->error = error2;
90298944Sobrien    }
90398944Sobrien
90498944Sobrien  /* We must always keep around the new value for this root
90598944Sobrien     variable expression, or we lose the updated children! */
90698944Sobrien  value_free ((*varp)->value);
90798944Sobrien  (*varp)->value = new;
90898944Sobrien
90998944Sobrien  /* Initialize a stack */
91098944Sobrien  vpush (&stack, NULL);
91198944Sobrien
91298944Sobrien  /* Push the root's children */
91398944Sobrien  if ((*varp)->children != NULL)
91498944Sobrien    {
91598944Sobrien      struct varobj_child *c;
91698944Sobrien      for (c = (*varp)->children; c != NULL; c = c->next)
91798944Sobrien	vpush (&stack, c->child);
91898944Sobrien    }
91998944Sobrien
92098944Sobrien  /* Walk through the children, reconstructing them all. */
92198944Sobrien  v = vpop (&stack);
92298944Sobrien  while (v != NULL)
92398944Sobrien    {
92498944Sobrien      /* Push any children */
92598944Sobrien      if (v->children != NULL)
92698944Sobrien	{
92798944Sobrien	  struct varobj_child *c;
92898944Sobrien	  for (c = v->children; c != NULL; c = c->next)
92998944Sobrien	    vpush (&stack, c->child);
93098944Sobrien	}
93198944Sobrien
93298944Sobrien      /* Update this variable */
93398944Sobrien      new = value_of_child (v->parent, v->index);
93498944Sobrien      if (type_changeable (v) && !my_value_equal (v->value, new, &error2))
93598944Sobrien	{
93698944Sobrien	  /* Note that it's changed */
93798944Sobrien	  vpush (&result, v);
93898944Sobrien	  changed++;
93998944Sobrien	}
94098944Sobrien      /* error2 replaces v->error since this new value
94198944Sobrien         WILL replace the old one. */
94298944Sobrien      v->error = error2;
94398944Sobrien
94498944Sobrien      /* We must always keep new values, since children depend on it. */
94598944Sobrien      if (v->value != NULL)
94698944Sobrien	value_free (v->value);
94798944Sobrien      v->value = new;
94898944Sobrien
94998944Sobrien      /* Get next child */
95098944Sobrien      v = vpop (&stack);
95198944Sobrien    }
95298944Sobrien
95398944Sobrien  /* Alloc (changed + 1) list entries */
95498944Sobrien  /* FIXME: add a cleanup for the allocated list(s)
95598944Sobrien     because one day the select_frame called below can longjump */
95698944Sobrien  *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
95798944Sobrien  if (changed > 1)
95898944Sobrien    {
95998944Sobrien      templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
96098944Sobrien      cv = templist;
96198944Sobrien    }
96298944Sobrien  else
96398944Sobrien    cv = *changelist;
96498944Sobrien
96598944Sobrien  /* Copy from result stack to list */
96698944Sobrien  vleft = changed;
96798944Sobrien  *cv = vpop (&result);
96898944Sobrien  while ((*cv != NULL) && (vleft > 0))
96998944Sobrien    {
97098944Sobrien      vleft--;
97198944Sobrien      cv++;
97298944Sobrien      *cv = vpop (&result);
97398944Sobrien    }
97498944Sobrien  if (vleft)
97598944Sobrien    warning ("varobj_update: assertion failed - vleft <> 0");
97698944Sobrien
97798944Sobrien  if (changed > 1)
97898944Sobrien    {
97998944Sobrien      /* Now we revert the order. */
98098944Sobrien      for (i = 0; i < changed; i++)
98198944Sobrien	*(*changelist + i) = *(templist + changed - 1 - i);
98298944Sobrien      *(*changelist + changed) = NULL;
98398944Sobrien    }
98498944Sobrien
98598944Sobrien  /* Restore selected frame */
98698944Sobrien  select_frame (old_fi, -1);
98798944Sobrien
98898944Sobrien  if (type_changed)
98998944Sobrien    return -2;
99098944Sobrien  else
99198944Sobrien    return changed;
99298944Sobrien}
99398944Sobrien
99498944Sobrien
99598944Sobrien/* Helper functions */
99698944Sobrien
99798944Sobrien/*
99898944Sobrien * Variable object construction/destruction
99998944Sobrien */
100098944Sobrien
100198944Sobrienstatic int
100298944Sobriendelete_variable (struct cpstack **resultp, struct varobj *var,
100398944Sobrien		 int only_children_p)
100498944Sobrien{
100598944Sobrien  int delcount = 0;
100698944Sobrien
100798944Sobrien  delete_variable_1 (resultp, &delcount, var,
100898944Sobrien		     only_children_p, 1 /* remove_from_parent_p */ );
100998944Sobrien
101098944Sobrien  return delcount;
101198944Sobrien}
101298944Sobrien
101398944Sobrien/* Delete the variable object VAR and its children */
101498944Sobrien/* IMPORTANT NOTE: If we delete a variable which is a child
101598944Sobrien   and the parent is not removed we dump core.  It must be always
101698944Sobrien   initially called with remove_from_parent_p set */
101798944Sobrienstatic void
101898944Sobriendelete_variable_1 (struct cpstack **resultp, int *delcountp,
101998944Sobrien		   struct varobj *var, int only_children_p,
102098944Sobrien		   int remove_from_parent_p)
102198944Sobrien{
102298944Sobrien  struct varobj_child *vc;
102398944Sobrien  struct varobj_child *next;
102498944Sobrien
102598944Sobrien  /* Delete any children of this variable, too. */
102698944Sobrien  for (vc = var->children; vc != NULL; vc = next)
102798944Sobrien    {
102898944Sobrien      if (!remove_from_parent_p)
102998944Sobrien	vc->child->parent = NULL;
103098944Sobrien      delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
103198944Sobrien      next = vc->next;
103298944Sobrien      xfree (vc);
103398944Sobrien    }
103498944Sobrien
103598944Sobrien  /* if we were called to delete only the children we are done here */
103698944Sobrien  if (only_children_p)
103798944Sobrien    return;
103898944Sobrien
103998944Sobrien  /* Otherwise, add it to the list of deleted ones and proceed to do so */
104098944Sobrien  /* If the name is null, this is a temporary variable, that has not
104198944Sobrien     yet been installed, don't report it, it belongs to the caller... */
104298944Sobrien  if (var->obj_name != NULL)
104398944Sobrien    {
104498944Sobrien      cppush (resultp, xstrdup (var->obj_name));
104598944Sobrien      *delcountp = *delcountp + 1;
104698944Sobrien    }
104798944Sobrien
104898944Sobrien  /* If this variable has a parent, remove it from its parent's list */
104998944Sobrien  /* OPTIMIZATION: if the parent of this variable is also being deleted,
105098944Sobrien     (as indicated by remove_from_parent_p) we don't bother doing an
105198944Sobrien     expensive list search to find the element to remove when we are
105298944Sobrien     discarding the list afterwards */
105398944Sobrien  if ((remove_from_parent_p) && (var->parent != NULL))
105498944Sobrien    {
105598944Sobrien      remove_child_from_parent (var->parent, var);
105698944Sobrien    }
105798944Sobrien
105898944Sobrien  if (var->obj_name != NULL)
105998944Sobrien    uninstall_variable (var);
106098944Sobrien
106198944Sobrien  /* Free memory associated with this variable */
106298944Sobrien  free_variable (var);
106398944Sobrien}
106498944Sobrien
106598944Sobrien/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
106698944Sobrienstatic int
106798944Sobrieninstall_variable (struct varobj *var)
106898944Sobrien{
106998944Sobrien  struct vlist *cv;
107098944Sobrien  struct vlist *newvl;
107198944Sobrien  const char *chp;
107298944Sobrien  unsigned int index = 0;
107398944Sobrien  unsigned int i = 1;
107498944Sobrien
107598944Sobrien  for (chp = var->obj_name; *chp; chp++)
107698944Sobrien    {
107798944Sobrien      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
107898944Sobrien    }
107998944Sobrien
108098944Sobrien  cv = *(varobj_table + index);
108198944Sobrien  while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
108298944Sobrien    cv = cv->next;
108398944Sobrien
108498944Sobrien  if (cv != NULL)
108598944Sobrien    error ("Duplicate variable object name");
108698944Sobrien
108798944Sobrien  /* Add varobj to hash table */
108898944Sobrien  newvl = xmalloc (sizeof (struct vlist));
108998944Sobrien  newvl->next = *(varobj_table + index);
109098944Sobrien  newvl->var = var;
109198944Sobrien  *(varobj_table + index) = newvl;
109298944Sobrien
109398944Sobrien  /* If root, add varobj to root list */
109498944Sobrien  if (var->root->rootvar == var)
109598944Sobrien    {
109698944Sobrien      /* Add to list of root variables */
109798944Sobrien      if (rootlist == NULL)
109898944Sobrien	var->root->next = NULL;
109998944Sobrien      else
110098944Sobrien	var->root->next = rootlist;
110198944Sobrien      rootlist = var->root;
110298944Sobrien      rootcount++;
110398944Sobrien    }
110498944Sobrien
110598944Sobrien  return 1;			/* OK */
110698944Sobrien}
110798944Sobrien
110898944Sobrien/* Unistall the object VAR. */
110998944Sobrienstatic void
111098944Sobrienuninstall_variable (struct varobj *var)
111198944Sobrien{
111298944Sobrien  struct vlist *cv;
111398944Sobrien  struct vlist *prev;
111498944Sobrien  struct varobj_root *cr;
111598944Sobrien  struct varobj_root *prer;
111698944Sobrien  const char *chp;
111798944Sobrien  unsigned int index = 0;
111898944Sobrien  unsigned int i = 1;
111998944Sobrien
112098944Sobrien  /* Remove varobj from hash table */
112198944Sobrien  for (chp = var->obj_name; *chp; chp++)
112298944Sobrien    {
112398944Sobrien      index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
112498944Sobrien    }
112598944Sobrien
112698944Sobrien  cv = *(varobj_table + index);
112798944Sobrien  prev = NULL;
112898944Sobrien  while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
112998944Sobrien    {
113098944Sobrien      prev = cv;
113198944Sobrien      cv = cv->next;
113298944Sobrien    }
113398944Sobrien
113498944Sobrien  if (varobjdebug)
113598944Sobrien    fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
113698944Sobrien
113798944Sobrien  if (cv == NULL)
113898944Sobrien    {
113998944Sobrien      warning
114098944Sobrien	("Assertion failed: Could not find variable object \"%s\" to delete",
114198944Sobrien	 var->obj_name);
114298944Sobrien      return;
114398944Sobrien    }
114498944Sobrien
114598944Sobrien  if (prev == NULL)
114698944Sobrien    *(varobj_table + index) = cv->next;
114798944Sobrien  else
114898944Sobrien    prev->next = cv->next;
114998944Sobrien
115098944Sobrien  xfree (cv);
115198944Sobrien
115298944Sobrien  /* If root, remove varobj from root list */
115398944Sobrien  if (var->root->rootvar == var)
115498944Sobrien    {
115598944Sobrien      /* Remove from list of root variables */
115698944Sobrien      if (rootlist == var->root)
115798944Sobrien	rootlist = var->root->next;
115898944Sobrien      else
115998944Sobrien	{
116098944Sobrien	  prer = NULL;
116198944Sobrien	  cr = rootlist;
116298944Sobrien	  while ((cr != NULL) && (cr->rootvar != var))
116398944Sobrien	    {
116498944Sobrien	      prer = cr;
116598944Sobrien	      cr = cr->next;
116698944Sobrien	    }
116798944Sobrien	  if (cr == NULL)
116898944Sobrien	    {
116998944Sobrien	      warning
117098944Sobrien		("Assertion failed: Could not find varobj \"%s\" in root list",
117198944Sobrien		 var->obj_name);
117298944Sobrien	      return;
117398944Sobrien	    }
117498944Sobrien	  if (prer == NULL)
117598944Sobrien	    rootlist = NULL;
117698944Sobrien	  else
117798944Sobrien	    prer->next = cr->next;
117898944Sobrien	}
117998944Sobrien      rootcount--;
118098944Sobrien    }
118198944Sobrien
118298944Sobrien}
118398944Sobrien
118498944Sobrien/* Does a child with the name NAME exist in VAR? If so, return its data.
118598944Sobrien   If not, return NULL. */
118698944Sobrienstatic struct varobj *
118798944Sobrienchild_exists (struct varobj *var, char *name)
118898944Sobrien{
118998944Sobrien  struct varobj_child *vc;
119098944Sobrien
119198944Sobrien  for (vc = var->children; vc != NULL; vc = vc->next)
119298944Sobrien    {
119398944Sobrien      if (STREQ (vc->child->name, name))
119498944Sobrien	return vc->child;
119598944Sobrien    }
119698944Sobrien
119798944Sobrien  return NULL;
119898944Sobrien}
119998944Sobrien
120098944Sobrien/* Create and install a child of the parent of the given name */
120198944Sobrienstatic struct varobj *
120298944Sobriencreate_child (struct varobj *parent, int index, char *name)
120398944Sobrien{
120498944Sobrien  struct varobj *child;
120598944Sobrien  char *childs_name;
120698944Sobrien
120798944Sobrien  child = new_variable ();
120898944Sobrien
120998944Sobrien  /* name is allocated by name_of_child */
121098944Sobrien  child->name = name;
121198944Sobrien  child->index = index;
121298944Sobrien  child->value = value_of_child (parent, index);
121398944Sobrien  if ((!CPLUS_FAKE_CHILD(child) && child->value == NULL) || parent->error)
121498944Sobrien    child->error = 1;
121598944Sobrien  child->parent = parent;
121698944Sobrien  child->root = parent->root;
121798944Sobrien  childs_name =
121898944Sobrien    (char *) xmalloc ((strlen (parent->obj_name) + strlen (name) + 2) *
121998944Sobrien		      sizeof (char));
122098944Sobrien  sprintf (childs_name, "%s.%s", parent->obj_name, name);
122198944Sobrien  child->obj_name = childs_name;
122298944Sobrien  install_variable (child);
122398944Sobrien
122498944Sobrien  /* Save a pointer to this child in the parent */
122598944Sobrien  save_child_in_parent (parent, child);
122698944Sobrien
122798944Sobrien  /* Note the type of this child */
122898944Sobrien  child->type = type_of_child (child);
122998944Sobrien
123098944Sobrien  return child;
123198944Sobrien}
123298944Sobrien
123398944Sobrien/* FIXME: This should be a generic add to list */
123498944Sobrien/* Save CHILD in the PARENT's data. */
123598944Sobrienstatic void
123698944Sobriensave_child_in_parent (struct varobj *parent, struct varobj *child)
123798944Sobrien{
123898944Sobrien  struct varobj_child *vc;
123998944Sobrien
124098944Sobrien  /* Insert the child at the top */
124198944Sobrien  vc = parent->children;
124298944Sobrien  parent->children =
124398944Sobrien    (struct varobj_child *) xmalloc (sizeof (struct varobj_child));
124498944Sobrien
124598944Sobrien  parent->children->next = vc;
124698944Sobrien  parent->children->child = child;
124798944Sobrien}
124898944Sobrien
124998944Sobrien/* FIXME: This should be a generic remove from list */
125098944Sobrien/* Remove the CHILD from the PARENT's list of children. */
125198944Sobrienstatic void
125298944Sobrienremove_child_from_parent (struct varobj *parent, struct varobj *child)
125398944Sobrien{
125498944Sobrien  struct varobj_child *vc, *prev;
125598944Sobrien
125698944Sobrien  /* Find the child in the parent's list */
125798944Sobrien  prev = NULL;
125898944Sobrien  for (vc = parent->children; vc != NULL;)
125998944Sobrien    {
126098944Sobrien      if (vc->child == child)
126198944Sobrien	break;
126298944Sobrien      prev = vc;
126398944Sobrien      vc = vc->next;
126498944Sobrien    }
126598944Sobrien
126698944Sobrien  if (prev == NULL)
126798944Sobrien    parent->children = vc->next;
126898944Sobrien  else
126998944Sobrien    prev->next = vc->next;
127098944Sobrien
127198944Sobrien}
127298944Sobrien
127398944Sobrien
127498944Sobrien/*
127598944Sobrien * Miscellaneous utility functions.
127698944Sobrien */
127798944Sobrien
127898944Sobrien/* Allocate memory and initialize a new variable */
127998944Sobrienstatic struct varobj *
128098944Sobriennew_variable (void)
128198944Sobrien{
128298944Sobrien  struct varobj *var;
128398944Sobrien
128498944Sobrien  var = (struct varobj *) xmalloc (sizeof (struct varobj));
128598944Sobrien  var->name = NULL;
128698944Sobrien  var->obj_name = NULL;
128798944Sobrien  var->index = -1;
128898944Sobrien  var->type = NULL;
128998944Sobrien  var->value = NULL;
129098944Sobrien  var->error = 0;
129198944Sobrien  var->num_children = -1;
129298944Sobrien  var->parent = NULL;
129398944Sobrien  var->children = NULL;
129498944Sobrien  var->format = 0;
129598944Sobrien  var->root = NULL;
129698944Sobrien
129798944Sobrien  return var;
129898944Sobrien}
129998944Sobrien
130098944Sobrien/* Allocate memory and initialize a new root variable */
130198944Sobrienstatic struct varobj *
130298944Sobriennew_root_variable (void)
130398944Sobrien{
130498944Sobrien  struct varobj *var = new_variable ();
130598944Sobrien  var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
130698944Sobrien  var->root->lang = NULL;
130798944Sobrien  var->root->exp = NULL;
130898944Sobrien  var->root->valid_block = NULL;
130998944Sobrien  var->root->frame = (CORE_ADDR) -1;
131098944Sobrien  var->root->use_selected_frame = 0;
131198944Sobrien  var->root->rootvar = NULL;
131298944Sobrien
131398944Sobrien  return var;
131498944Sobrien}
131598944Sobrien
131698944Sobrien/* Free any allocated memory associated with VAR. */
131798944Sobrienstatic void
131898944Sobrienfree_variable (struct varobj *var)
131998944Sobrien{
132098944Sobrien  /* Free the expression if this is a root variable. */
132198944Sobrien  if (var->root->rootvar == var)
132298944Sobrien    {
132398944Sobrien      free_current_contents ((char **) &var->root->exp);
132498944Sobrien      xfree (var->root);
132598944Sobrien    }
132698944Sobrien
132798944Sobrien  xfree (var->name);
132898944Sobrien  xfree (var->obj_name);
132998944Sobrien  xfree (var);
133098944Sobrien}
133198944Sobrien
133298944Sobrienstatic void
133398944Sobriendo_free_variable_cleanup (void *var)
133498944Sobrien{
133598944Sobrien  free_variable (var);
133698944Sobrien}
133798944Sobrien
133898944Sobrienstatic struct cleanup *
133998944Sobrienmake_cleanup_free_variable (struct varobj *var)
134098944Sobrien{
134198944Sobrien  return make_cleanup (do_free_variable_cleanup, var);
134298944Sobrien}
134398944Sobrien
134498944Sobrien/* This returns the type of the variable. This skips past typedefs
134598944Sobrien   and returns the real type of the variable. It also dereferences
134698944Sobrien   pointers and references. */
134798944Sobrienstatic struct type *
134898944Sobrienget_type (struct varobj *var)
134998944Sobrien{
135098944Sobrien  struct type *type;
135198944Sobrien  type = var->type;
135298944Sobrien
135398944Sobrien  while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
135498944Sobrien    type = TYPE_TARGET_TYPE (type);
135598944Sobrien
135698944Sobrien  return type;
135798944Sobrien}
135898944Sobrien
135998944Sobrien/* This returns the type of the variable, dereferencing pointers, too. */
136098944Sobrienstatic struct type *
136198944Sobrienget_type_deref (struct varobj *var)
136298944Sobrien{
136398944Sobrien  struct type *type;
136498944Sobrien
136598944Sobrien  type = get_type (var);
136698944Sobrien
136798944Sobrien  if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR
136898944Sobrien		       || TYPE_CODE (type) == TYPE_CODE_REF))
136998944Sobrien    type = get_target_type (type);
137098944Sobrien
137198944Sobrien  return type;
137298944Sobrien}
137398944Sobrien
137498944Sobrien/* This returns the target type (or NULL) of TYPE, also skipping
137598944Sobrien   past typedefs, just like get_type (). */
137698944Sobrienstatic struct type *
137798944Sobrienget_target_type (struct type *type)
137898944Sobrien{
137998944Sobrien  if (type != NULL)
138098944Sobrien    {
138198944Sobrien      type = TYPE_TARGET_TYPE (type);
138298944Sobrien      while (type != NULL && TYPE_CODE (type) == TYPE_CODE_TYPEDEF)
138398944Sobrien	type = TYPE_TARGET_TYPE (type);
138498944Sobrien    }
138598944Sobrien
138698944Sobrien  return type;
138798944Sobrien}
138898944Sobrien
138998944Sobrien/* What is the default display for this variable? We assume that
139098944Sobrien   everything is "natural". Any exceptions? */
139198944Sobrienstatic enum varobj_display_formats
139298944Sobrienvariable_default_display (struct varobj *var)
139398944Sobrien{
139498944Sobrien  return FORMAT_NATURAL;
139598944Sobrien}
139698944Sobrien
139798944Sobrien/* This function is similar to gdb's value_equal, except that this
139898944Sobrien   one is "safe" -- it NEVER longjmps. It determines if the VAR's
139998944Sobrien   value is the same as VAL2. */
140098944Sobrienstatic int
140198944Sobrienmy_value_equal (struct value *val1, struct value *val2, int *error2)
140298944Sobrien{
140398944Sobrien  int r, err1, err2;
140498944Sobrien
140598944Sobrien  *error2 = 0;
140698944Sobrien  /* Special case: NULL values. If both are null, say
140798944Sobrien     they're equal. */
140898944Sobrien  if (val1 == NULL && val2 == NULL)
140998944Sobrien    return 1;
141098944Sobrien  else if (val1 == NULL || val2 == NULL)
141198944Sobrien    return 0;
141298944Sobrien
141398944Sobrien  /* This is bogus, but unfortunately necessary. We must know
141498944Sobrien     exactly what caused an error -- reading val1 or val2 --  so
141598944Sobrien     that we can really determine if we think that something has changed. */
141698944Sobrien  err1 = 0;
141798944Sobrien  err2 = 0;
141898944Sobrien  /* We do need to catch errors here because the whole purpose
141998944Sobrien     is to test if value_equal() has errored */
142098944Sobrien  if (!gdb_value_equal (val1, val1, &r))
142198944Sobrien    err1 = 1;
142298944Sobrien
142398944Sobrien  if (!gdb_value_equal (val2, val2, &r))
142498944Sobrien    *error2 = err2 = 1;
142598944Sobrien
142698944Sobrien  if (err1 != err2)
142798944Sobrien    return 0;
142898944Sobrien
142998944Sobrien  if (!gdb_value_equal (val1, val2, &r))
143098944Sobrien    {
143198944Sobrien      /* An error occurred, this could have happened if
143298944Sobrien         either val1 or val2 errored. ERR1 and ERR2 tell
143398944Sobrien         us which of these it is. If both errored, then
143498944Sobrien         we assume nothing has changed. If one of them is
143598944Sobrien         valid, though, then something has changed. */
143698944Sobrien      if (err1 == err2)
143798944Sobrien	{
143898944Sobrien	  /* both the old and new values caused errors, so
143998944Sobrien	     we say the value did not change */
144098944Sobrien	  /* This is indeterminate, though. Perhaps we should
144198944Sobrien	     be safe and say, yes, it changed anyway?? */
144298944Sobrien	  return 1;
144398944Sobrien	}
144498944Sobrien      else
144598944Sobrien	{
144698944Sobrien	  return 0;
144798944Sobrien	}
144898944Sobrien    }
144998944Sobrien
145098944Sobrien  return r;
145198944Sobrien}
145298944Sobrien
145398944Sobrien/* FIXME: The following should be generic for any pointer */
145498944Sobrienstatic void
145598944Sobrienvpush (struct vstack **pstack, struct varobj *var)
145698944Sobrien{
145798944Sobrien  struct vstack *s;
145898944Sobrien
145998944Sobrien  s = (struct vstack *) xmalloc (sizeof (struct vstack));
146098944Sobrien  s->var = var;
146198944Sobrien  s->next = *pstack;
146298944Sobrien  *pstack = s;
146398944Sobrien}
146498944Sobrien
146598944Sobrien/* FIXME: The following should be generic for any pointer */
146698944Sobrienstatic struct varobj *
146798944Sobrienvpop (struct vstack **pstack)
146898944Sobrien{
146998944Sobrien  struct vstack *s;
147098944Sobrien  struct varobj *v;
147198944Sobrien
147298944Sobrien  if ((*pstack)->var == NULL && (*pstack)->next == NULL)
147398944Sobrien    return NULL;
147498944Sobrien
147598944Sobrien  s = *pstack;
147698944Sobrien  v = s->var;
147798944Sobrien  *pstack = (*pstack)->next;
147898944Sobrien  xfree (s);
147998944Sobrien
148098944Sobrien  return v;
148198944Sobrien}
148298944Sobrien
148398944Sobrien/* FIXME: The following should be generic for any pointer */
148498944Sobrienstatic void
148598944Sobriencppush (struct cpstack **pstack, char *name)
148698944Sobrien{
148798944Sobrien  struct cpstack *s;
148898944Sobrien
148998944Sobrien  s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
149098944Sobrien  s->name = name;
149198944Sobrien  s->next = *pstack;
149298944Sobrien  *pstack = s;
149398944Sobrien}
149498944Sobrien
149598944Sobrien/* FIXME: The following should be generic for any pointer */
149698944Sobrienstatic char *
149798944Sobriencppop (struct cpstack **pstack)
149898944Sobrien{
149998944Sobrien  struct cpstack *s;
150098944Sobrien  char *v;
150198944Sobrien
150298944Sobrien  if ((*pstack)->name == NULL && (*pstack)->next == NULL)
150398944Sobrien    return NULL;
150498944Sobrien
150598944Sobrien  s = *pstack;
150698944Sobrien  v = s->name;
150798944Sobrien  *pstack = (*pstack)->next;
150898944Sobrien  xfree (s);
150998944Sobrien
151098944Sobrien  return v;
151198944Sobrien}
151298944Sobrien
151398944Sobrien/*
151498944Sobrien * Language-dependencies
151598944Sobrien */
151698944Sobrien
151798944Sobrien/* Common entry points */
151898944Sobrien
151998944Sobrien/* Get the language of variable VAR. */
152098944Sobrienstatic enum varobj_languages
152198944Sobrienvariable_language (struct varobj *var)
152298944Sobrien{
152398944Sobrien  enum varobj_languages lang;
152498944Sobrien
152598944Sobrien  switch (var->root->exp->language_defn->la_language)
152698944Sobrien    {
152798944Sobrien    default:
152898944Sobrien    case language_c:
152998944Sobrien      lang = vlang_c;
153098944Sobrien      break;
153198944Sobrien    case language_cplus:
153298944Sobrien      lang = vlang_cplus;
153398944Sobrien      break;
153498944Sobrien    case language_java:
153598944Sobrien      lang = vlang_java;
153698944Sobrien      break;
153798944Sobrien    }
153898944Sobrien
153998944Sobrien  return lang;
154098944Sobrien}
154198944Sobrien
154298944Sobrien/* Return the number of children for a given variable.
154398944Sobrien   The result of this function is defined by the language
154498944Sobrien   implementation. The number of children returned by this function
154598944Sobrien   is the number of children that the user will see in the variable
154698944Sobrien   display. */
154798944Sobrienstatic int
154898944Sobriennumber_of_children (struct varobj *var)
154998944Sobrien{
155098944Sobrien  return (*var->root->lang->number_of_children) (var);;
155198944Sobrien}
155298944Sobrien
155398944Sobrien/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
155498944Sobrienstatic char *
155598944Sobrienname_of_variable (struct varobj *var)
155698944Sobrien{
155798944Sobrien  return (*var->root->lang->name_of_variable) (var);
155898944Sobrien}
155998944Sobrien
156098944Sobrien/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
156198944Sobrienstatic char *
156298944Sobrienname_of_child (struct varobj *var, int index)
156398944Sobrien{
156498944Sobrien  return (*var->root->lang->name_of_child) (var, index);
156598944Sobrien}
156698944Sobrien
156798944Sobrien/* What is the ``struct value *'' of the root variable VAR?
156898944Sobrien   TYPE_CHANGED controls what to do if the type of a
156998944Sobrien   use_selected_frame = 1 variable changes.  On input,
157098944Sobrien   TYPE_CHANGED = 1 means discard the old varobj, and replace
157198944Sobrien   it with this one.  TYPE_CHANGED = 0 means leave it around.
157298944Sobrien   NB: In both cases, var_handle will point to the new varobj,
157398944Sobrien   so if you use TYPE_CHANGED = 0, you will have to stash the
157498944Sobrien   old varobj pointer away somewhere before calling this.
157598944Sobrien   On return, TYPE_CHANGED will be 1 if the type has changed, and
157698944Sobrien   0 otherwise. */
157798944Sobrienstatic struct value *
157898944Sobrienvalue_of_root (struct varobj **var_handle, int *type_changed)
157998944Sobrien{
158098944Sobrien  struct varobj *var;
158198944Sobrien
158298944Sobrien  if (var_handle == NULL)
158398944Sobrien    return NULL;
158498944Sobrien
158598944Sobrien  var = *var_handle;
158698944Sobrien
158798944Sobrien  /* This should really be an exception, since this should
158898944Sobrien     only get called with a root variable. */
158998944Sobrien
159098944Sobrien  if (var->root->rootvar != var)
159198944Sobrien    return NULL;
159298944Sobrien
159398944Sobrien  if (var->root->use_selected_frame)
159498944Sobrien    {
159598944Sobrien      struct varobj *tmp_var;
159698944Sobrien      char *old_type, *new_type;
159798944Sobrien      old_type = varobj_get_type (var);
159898944Sobrien      tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
159998944Sobrien			       USE_SELECTED_FRAME);
160098944Sobrien      if (tmp_var == NULL)
160198944Sobrien	{
160298944Sobrien	  return NULL;
160398944Sobrien	}
160498944Sobrien      new_type = varobj_get_type (tmp_var);
160598944Sobrien      if (strcmp (old_type, new_type) == 0)
160698944Sobrien	{
160798944Sobrien	  varobj_delete (tmp_var, NULL, 0);
160898944Sobrien	  *type_changed = 0;
160998944Sobrien	}
161098944Sobrien      else
161198944Sobrien	{
161298944Sobrien	  if (*type_changed)
161398944Sobrien	    {
161498944Sobrien	      tmp_var->obj_name =
161598944Sobrien		savestring (var->obj_name, strlen (var->obj_name));
161698944Sobrien	      varobj_delete (var, NULL, 0);
161798944Sobrien	    }
161898944Sobrien	  else
161998944Sobrien	    {
162098944Sobrien	      tmp_var->obj_name = varobj_gen_name ();
162198944Sobrien	    }
162298944Sobrien	  install_variable (tmp_var);
162398944Sobrien	  *var_handle = tmp_var;
162498944Sobrien	  var = *var_handle;
162598944Sobrien	  *type_changed = 1;
162698944Sobrien	}
162798944Sobrien    }
162898944Sobrien  else
162998944Sobrien    {
163098944Sobrien      *type_changed = 0;
163198944Sobrien    }
163298944Sobrien
163398944Sobrien  return (*var->root->lang->value_of_root) (var_handle);
163498944Sobrien}
163598944Sobrien
163698944Sobrien/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
163798944Sobrienstatic struct value *
163898944Sobrienvalue_of_child (struct varobj *parent, int index)
163998944Sobrien{
164098944Sobrien  struct value *value;
164198944Sobrien
164298944Sobrien  value = (*parent->root->lang->value_of_child) (parent, index);
164398944Sobrien
164498944Sobrien  /* If we're being lazy, fetch the real value of the variable. */
164598944Sobrien  if (value != NULL && VALUE_LAZY (value))
164698944Sobrien    {
164798944Sobrien      /* If we fail to fetch the value of the child, return
164898944Sobrien	 NULL so that callers notice that we're leaving an
164998944Sobrien	 error message. */
165098944Sobrien      if (!gdb_value_fetch_lazy (value))
165198944Sobrien	value = NULL;
165298944Sobrien    }
165398944Sobrien
165498944Sobrien  return value;
165598944Sobrien}
165698944Sobrien
165798944Sobrien/* What is the type of VAR? */
165898944Sobrienstatic struct type *
165998944Sobrientype_of_child (struct varobj *var)
166098944Sobrien{
166198944Sobrien
166298944Sobrien  /* If the child had no evaluation errors, var->value
166398944Sobrien     will be non-NULL and contain a valid type. */
166498944Sobrien  if (var->value != NULL)
166598944Sobrien    return VALUE_TYPE (var->value);
166698944Sobrien
166798944Sobrien  /* Otherwise, we must compute the type. */
166898944Sobrien  return (*var->root->lang->type_of_child) (var->parent, var->index);
166998944Sobrien}
167098944Sobrien
167198944Sobrien/* Is this variable editable? Use the variable's type to make
167298944Sobrien   this determination. */
167398944Sobrienstatic int
167498944Sobrienvariable_editable (struct varobj *var)
167598944Sobrien{
167698944Sobrien  return (*var->root->lang->variable_editable) (var);
167798944Sobrien}
167898944Sobrien
167998944Sobrien/* GDB already has a command called "value_of_variable". Sigh. */
168098944Sobrienstatic char *
168198944Sobrienmy_value_of_variable (struct varobj *var)
168298944Sobrien{
168398944Sobrien  return (*var->root->lang->value_of_variable) (var);
168498944Sobrien}
168598944Sobrien
168698944Sobrien/* Is VAR something that can change? Depending on language,
168798944Sobrien   some variable's values never change. For example,
168898944Sobrien   struct and unions never change values. */
168998944Sobrienstatic int
169098944Sobrientype_changeable (struct varobj *var)
169198944Sobrien{
169298944Sobrien  int r;
169398944Sobrien  struct type *type;
169498944Sobrien
169598944Sobrien  if (CPLUS_FAKE_CHILD (var))
169698944Sobrien    return 0;
169798944Sobrien
169898944Sobrien  type = get_type (var);
169998944Sobrien
170098944Sobrien  switch (TYPE_CODE (type))
170198944Sobrien    {
170298944Sobrien    case TYPE_CODE_STRUCT:
170398944Sobrien    case TYPE_CODE_UNION:
170498944Sobrien    case TYPE_CODE_ARRAY:
170598944Sobrien      r = 0;
170698944Sobrien      break;
170798944Sobrien
170898944Sobrien    default:
170998944Sobrien      r = 1;
171098944Sobrien    }
171198944Sobrien
171298944Sobrien  return r;
171398944Sobrien}
171498944Sobrien
171598944Sobrien/* C */
171698944Sobrienstatic int
171798944Sobrienc_number_of_children (struct varobj *var)
171898944Sobrien{
171998944Sobrien  struct type *type;
172098944Sobrien  struct type *target;
172198944Sobrien  int children;
172298944Sobrien
172398944Sobrien  type = get_type (var);
172498944Sobrien  target = get_target_type (type);
172598944Sobrien  children = 0;
172698944Sobrien
172798944Sobrien  switch (TYPE_CODE (type))
172898944Sobrien    {
172998944Sobrien    case TYPE_CODE_ARRAY:
173098944Sobrien      if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
173198944Sobrien	  && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
173298944Sobrien	children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
173398944Sobrien      else
173498944Sobrien	children = -1;
173598944Sobrien      break;
173698944Sobrien
173798944Sobrien    case TYPE_CODE_STRUCT:
173898944Sobrien    case TYPE_CODE_UNION:
173998944Sobrien      children = TYPE_NFIELDS (type);
174098944Sobrien      break;
174198944Sobrien
174298944Sobrien    case TYPE_CODE_PTR:
174398944Sobrien      /* This is where things get compilcated. All pointers have one child.
174498944Sobrien         Except, of course, for struct and union ptr, which we automagically
174598944Sobrien         dereference for the user and function ptrs, which have no children.
174698944Sobrien         We also don't dereference void* as we don't know what to show.
174798944Sobrien         We can show char* so we allow it to be dereferenced.  If you decide
174898944Sobrien         to test for it, please mind that a little magic is necessary to
174998944Sobrien         properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
175098944Sobrien         TYPE_NAME == "char" */
175198944Sobrien
175298944Sobrien      switch (TYPE_CODE (target))
175398944Sobrien	{
175498944Sobrien	case TYPE_CODE_STRUCT:
175598944Sobrien	case TYPE_CODE_UNION:
175698944Sobrien	  children = TYPE_NFIELDS (target);
175798944Sobrien	  break;
175898944Sobrien
175998944Sobrien	case TYPE_CODE_FUNC:
176098944Sobrien	case TYPE_CODE_VOID:
176198944Sobrien	  children = 0;
176298944Sobrien	  break;
176398944Sobrien
176498944Sobrien	default:
176598944Sobrien	  children = 1;
176698944Sobrien	}
176798944Sobrien      break;
176898944Sobrien
176998944Sobrien    default:
177098944Sobrien      /* Other types have no children */
177198944Sobrien      break;
177298944Sobrien    }
177398944Sobrien
177498944Sobrien  return children;
177598944Sobrien}
177698944Sobrien
177798944Sobrienstatic char *
177898944Sobrienc_name_of_variable (struct varobj *parent)
177998944Sobrien{
178098944Sobrien  return savestring (parent->name, strlen (parent->name));
178198944Sobrien}
178298944Sobrien
178398944Sobrienstatic char *
178498944Sobrienc_name_of_child (struct varobj *parent, int index)
178598944Sobrien{
178698944Sobrien  struct type *type;
178798944Sobrien  struct type *target;
178898944Sobrien  char *name;
178998944Sobrien  char *string;
179098944Sobrien
179198944Sobrien  type = get_type (parent);
179298944Sobrien  target = get_target_type (type);
179398944Sobrien
179498944Sobrien  switch (TYPE_CODE (type))
179598944Sobrien    {
179698944Sobrien    case TYPE_CODE_ARRAY:
179798944Sobrien      {
179898944Sobrien	/* We never get here unless parent->num_children is greater than 0... */
179998944Sobrien	int len = 1;
180098944Sobrien	while ((int) pow ((double) 10, (double) len) < index)
180198944Sobrien	  len++;
180298944Sobrien	name = (char *) xmalloc (1 + len * sizeof (char));
180398944Sobrien	sprintf (name, "%d", index);
180498944Sobrien      }
180598944Sobrien      break;
180698944Sobrien
180798944Sobrien    case TYPE_CODE_STRUCT:
180898944Sobrien    case TYPE_CODE_UNION:
180998944Sobrien      string = TYPE_FIELD_NAME (type, index);
181098944Sobrien      name = savestring (string, strlen (string));
181198944Sobrien      break;
181298944Sobrien
181398944Sobrien    case TYPE_CODE_PTR:
181498944Sobrien      switch (TYPE_CODE (target))
181598944Sobrien	{
181698944Sobrien	case TYPE_CODE_STRUCT:
181798944Sobrien	case TYPE_CODE_UNION:
181898944Sobrien	  string = TYPE_FIELD_NAME (target, index);
181998944Sobrien	  name = savestring (string, strlen (string));
182098944Sobrien	  break;
182198944Sobrien
182298944Sobrien	default:
182398944Sobrien	  name =
182498944Sobrien	    (char *) xmalloc ((strlen (parent->name) + 2) * sizeof (char));
182598944Sobrien	  sprintf (name, "*%s", parent->name);
182698944Sobrien	  break;
182798944Sobrien	}
182898944Sobrien      break;
182998944Sobrien
183098944Sobrien    default:
183198944Sobrien      /* This should not happen */
183298944Sobrien      name = xstrdup ("???");
183398944Sobrien    }
183498944Sobrien
183598944Sobrien  return name;
183698944Sobrien}
183798944Sobrien
183898944Sobrienstatic struct value *
183998944Sobrienc_value_of_root (struct varobj **var_handle)
184098944Sobrien{
184198944Sobrien  struct value *new_val;
184298944Sobrien  struct varobj *var = *var_handle;
184398944Sobrien  struct frame_info *fi;
184498944Sobrien  int within_scope;
184598944Sobrien
184698944Sobrien  /*  Only root variables can be updated... */
184798944Sobrien  if (var->root->rootvar != var)
184898944Sobrien    /* Not a root var */
184998944Sobrien    return NULL;
185098944Sobrien
185198944Sobrien
185298944Sobrien  /* Determine whether the variable is still around. */
185398944Sobrien  if (var->root->valid_block == NULL)
185498944Sobrien    within_scope = 1;
185598944Sobrien  else
185698944Sobrien    {
185798944Sobrien      reinit_frame_cache ();
185898944Sobrien
185998944Sobrien
186098944Sobrien      fi = find_frame_addr_in_frame_chain (var->root->frame);
186198944Sobrien
186298944Sobrien      within_scope = fi != NULL;
186398944Sobrien      /* FIXME: select_frame could fail */
186498944Sobrien      if (within_scope)
186598944Sobrien	select_frame (fi, -1);
186698944Sobrien    }
186798944Sobrien
186898944Sobrien  if (within_scope)
186998944Sobrien    {
187098944Sobrien      /* We need to catch errors here, because if evaluate
187198944Sobrien         expression fails we just want to make val->error = 1 and
187298944Sobrien         go on */
187398944Sobrien      if (gdb_evaluate_expression (var->root->exp, &new_val))
187498944Sobrien	{
187598944Sobrien	  if (VALUE_LAZY (new_val))
187698944Sobrien	    {
187798944Sobrien	      /* We need to catch errors because if
187898944Sobrien	         value_fetch_lazy fails we still want to continue
187998944Sobrien	         (after making val->error = 1) */
188098944Sobrien	      /* FIXME: Shouldn't be using VALUE_CONTENTS?  The
188198944Sobrien	         comment on value_fetch_lazy() says it is only
188298944Sobrien	         called from the macro... */
188398944Sobrien	      if (!gdb_value_fetch_lazy (new_val))
188498944Sobrien		var->error = 1;
188598944Sobrien	      else
188698944Sobrien		var->error = 0;
188798944Sobrien	    }
188898944Sobrien	}
188998944Sobrien      else
189098944Sobrien	var->error = 1;
189198944Sobrien
189298944Sobrien      release_value (new_val);
189398944Sobrien      return new_val;
189498944Sobrien    }
189598944Sobrien
189698944Sobrien  return NULL;
189798944Sobrien}
189898944Sobrien
189998944Sobrienstatic struct value *
190098944Sobrienc_value_of_child (struct varobj *parent, int index)
190198944Sobrien{
190298944Sobrien  struct value *value;
190398944Sobrien  struct value *temp;
190498944Sobrien  struct value *indval;
190598944Sobrien  struct type *type, *target;
190698944Sobrien  char *name;
190798944Sobrien
190898944Sobrien  type = get_type (parent);
190998944Sobrien  target = get_target_type (type);
191098944Sobrien  name = name_of_child (parent, index);
191198944Sobrien  temp = parent->value;
191298944Sobrien  value = NULL;
191398944Sobrien
191498944Sobrien  if (temp != NULL)
191598944Sobrien    {
191698944Sobrien      switch (TYPE_CODE (type))
191798944Sobrien	{
191898944Sobrien	case TYPE_CODE_ARRAY:
191998944Sobrien#if 0
192098944Sobrien	  /* This breaks if the array lives in a (vector) register. */
192198944Sobrien	  value = value_slice (temp, index, 1);
192298944Sobrien	  temp = value_coerce_array (value);
192398944Sobrien	  gdb_value_ind (temp, &value);
192498944Sobrien#else
192598944Sobrien	  indval = value_from_longest (builtin_type_int, (LONGEST) index);
192698944Sobrien	  gdb_value_subscript (temp, indval, &value);
192798944Sobrien#endif
192898944Sobrien	  break;
192998944Sobrien
193098944Sobrien	case TYPE_CODE_STRUCT:
193198944Sobrien	case TYPE_CODE_UNION:
193298944Sobrien	  gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL, "vstructure");
193398944Sobrien	  break;
193498944Sobrien
193598944Sobrien	case TYPE_CODE_PTR:
193698944Sobrien	  switch (TYPE_CODE (target))
193798944Sobrien	    {
193898944Sobrien	    case TYPE_CODE_STRUCT:
193998944Sobrien	    case TYPE_CODE_UNION:
194098944Sobrien	      gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL, "vstructure");
194198944Sobrien	      break;
194298944Sobrien
194398944Sobrien	    default:
194498944Sobrien	      gdb_value_ind (temp, &value);
194598944Sobrien	      break;
194698944Sobrien	    }
194798944Sobrien	  break;
194898944Sobrien
194998944Sobrien	default:
195098944Sobrien	  break;
195198944Sobrien	}
195298944Sobrien    }
195398944Sobrien
195498944Sobrien  if (value != NULL)
195598944Sobrien    release_value (value);
195698944Sobrien
195798944Sobrien  xfree (name);
195898944Sobrien  return value;
195998944Sobrien}
196098944Sobrien
196198944Sobrienstatic struct type *
196298944Sobrienc_type_of_child (struct varobj *parent, int index)
196398944Sobrien{
196498944Sobrien  struct type *type;
196598944Sobrien  char *name = name_of_child (parent, index);
196698944Sobrien
196798944Sobrien  switch (TYPE_CODE (parent->type))
196898944Sobrien    {
196998944Sobrien    case TYPE_CODE_ARRAY:
197098944Sobrien      type = TYPE_TARGET_TYPE (parent->type);
197198944Sobrien      break;
197298944Sobrien
197398944Sobrien    case TYPE_CODE_STRUCT:
197498944Sobrien    case TYPE_CODE_UNION:
197598944Sobrien      type = lookup_struct_elt_type (parent->type, name, 0);
197698944Sobrien      break;
197798944Sobrien
197898944Sobrien    case TYPE_CODE_PTR:
197998944Sobrien      switch (TYPE_CODE (TYPE_TARGET_TYPE (parent->type)))
198098944Sobrien	{
198198944Sobrien	case TYPE_CODE_STRUCT:
198298944Sobrien	case TYPE_CODE_UNION:
198398944Sobrien	  type = lookup_struct_elt_type (parent->type, name, 0);
198498944Sobrien	  break;
198598944Sobrien
198698944Sobrien	default:
198798944Sobrien	  type = TYPE_TARGET_TYPE (parent->type);
198898944Sobrien	  break;
198998944Sobrien	}
199098944Sobrien      break;
199198944Sobrien
199298944Sobrien    default:
199398944Sobrien      /* This should not happen as only the above types have children */
199498944Sobrien      warning ("Child of parent whose type does not allow children");
199598944Sobrien      /* FIXME: Can we still go on? */
199698944Sobrien      type = NULL;
199798944Sobrien      break;
199898944Sobrien    }
199998944Sobrien
200098944Sobrien  xfree (name);
200198944Sobrien  return type;
200298944Sobrien}
200398944Sobrien
200498944Sobrienstatic int
200598944Sobrienc_variable_editable (struct varobj *var)
200698944Sobrien{
200798944Sobrien  switch (TYPE_CODE (get_type (var)))
200898944Sobrien    {
200998944Sobrien    case TYPE_CODE_STRUCT:
201098944Sobrien    case TYPE_CODE_UNION:
201198944Sobrien    case TYPE_CODE_ARRAY:
201298944Sobrien    case TYPE_CODE_FUNC:
201398944Sobrien    case TYPE_CODE_MEMBER:
201498944Sobrien    case TYPE_CODE_METHOD:
201598944Sobrien      return 0;
201698944Sobrien      break;
201798944Sobrien
201898944Sobrien    default:
201998944Sobrien      return 1;
202098944Sobrien      break;
202198944Sobrien    }
202298944Sobrien}
202398944Sobrien
202498944Sobrienstatic char *
202598944Sobrienc_value_of_variable (struct varobj *var)
202698944Sobrien{
202798944Sobrien  struct type *type;
202898944Sobrien
202998944Sobrien  /* BOGUS: if val_print sees a struct/class, it will print out its
203098944Sobrien     children instead of "{...}" */
203198944Sobrien  type = get_type (var);
203298944Sobrien  switch (TYPE_CODE (type))
203398944Sobrien    {
203498944Sobrien    case TYPE_CODE_STRUCT:
203598944Sobrien    case TYPE_CODE_UNION:
203698944Sobrien      return xstrdup ("{...}");
203798944Sobrien      /* break; */
203898944Sobrien
203998944Sobrien    case TYPE_CODE_ARRAY:
204098944Sobrien      {
204198944Sobrien	char number[18];
204298944Sobrien	sprintf (number, "[%d]", var->num_children);
204398944Sobrien	return xstrdup (number);
204498944Sobrien      }
204598944Sobrien      /* break; */
204698944Sobrien
204798944Sobrien    default:
204898944Sobrien      {
204998944Sobrien	long dummy;
205098944Sobrien	struct ui_file *stb = mem_fileopen ();
205198944Sobrien	struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
205298944Sobrien	char *thevalue;
205398944Sobrien
205498944Sobrien	if (var->value == NULL)
205598944Sobrien	  {
205698944Sobrien	    /* This can happen if we attempt to get the value of a struct
205798944Sobrien	       member when the parent is an invalid pointer. This is an
205898944Sobrien	       error condition, so we should tell the caller. */
205998944Sobrien	    return NULL;
206098944Sobrien	  }
206198944Sobrien	else
206298944Sobrien	  {
206398944Sobrien	    if (VALUE_LAZY (var->value))
206498944Sobrien	      gdb_value_fetch_lazy (var->value);
206598944Sobrien	    val_print (VALUE_TYPE (var->value), VALUE_CONTENTS_RAW (var->value), 0,
206698944Sobrien		       VALUE_ADDRESS (var->value),
206798944Sobrien		       stb, format_code[(int) var->format], 1, 0, 0);
206898944Sobrien	    thevalue = ui_file_xstrdup (stb, &dummy);
206998944Sobrien	    do_cleanups (old_chain);
207098944Sobrien	  }
207198944Sobrien
207298944Sobrien	return thevalue;
207398944Sobrien      }
207498944Sobrien      /* break; */
207598944Sobrien    }
207698944Sobrien}
207798944Sobrien
207898944Sobrien
207998944Sobrien/* C++ */
208098944Sobrien
208198944Sobrienstatic int
208298944Sobriencplus_number_of_children (struct varobj *var)
208398944Sobrien{
208498944Sobrien  struct type *type;
208598944Sobrien  int children, dont_know;
208698944Sobrien
208798944Sobrien  dont_know = 1;
208898944Sobrien  children = 0;
208998944Sobrien
209098944Sobrien  if (!CPLUS_FAKE_CHILD (var))
209198944Sobrien    {
209298944Sobrien      type = get_type_deref (var);
209398944Sobrien
209498944Sobrien      if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
209598944Sobrien	  ((TYPE_CODE (type)) == TYPE_CODE_UNION))
209698944Sobrien	{
209798944Sobrien	  int kids[3];
209898944Sobrien
209998944Sobrien	  cplus_class_num_children (type, kids);
210098944Sobrien	  if (kids[v_public] != 0)
210198944Sobrien	    children++;
210298944Sobrien	  if (kids[v_private] != 0)
210398944Sobrien	    children++;
210498944Sobrien	  if (kids[v_protected] != 0)
210598944Sobrien	    children++;
210698944Sobrien
210798944Sobrien	  /* Add any baseclasses */
210898944Sobrien	  children += TYPE_N_BASECLASSES (type);
210998944Sobrien	  dont_know = 0;
211098944Sobrien
211198944Sobrien	  /* FIXME: save children in var */
211298944Sobrien	}
211398944Sobrien    }
211498944Sobrien  else
211598944Sobrien    {
211698944Sobrien      int kids[3];
211798944Sobrien
211898944Sobrien      type = get_type_deref (var->parent);
211998944Sobrien
212098944Sobrien      cplus_class_num_children (type, kids);
212198944Sobrien      if (STREQ (var->name, "public"))
212298944Sobrien	children = kids[v_public];
212398944Sobrien      else if (STREQ (var->name, "private"))
212498944Sobrien	children = kids[v_private];
212598944Sobrien      else
212698944Sobrien	children = kids[v_protected];
212798944Sobrien      dont_know = 0;
212898944Sobrien    }
212998944Sobrien
213098944Sobrien  if (dont_know)
213198944Sobrien    children = c_number_of_children (var);
213298944Sobrien
213398944Sobrien  return children;
213498944Sobrien}
213598944Sobrien
213698944Sobrien/* Compute # of public, private, and protected variables in this class.
213798944Sobrien   That means we need to descend into all baseclasses and find out
213898944Sobrien   how many are there, too. */
213998944Sobrienstatic void
214098944Sobriencplus_class_num_children (struct type *type, int children[3])
214198944Sobrien{
214298944Sobrien  int i;
214398944Sobrien
214498944Sobrien  children[v_public] = 0;
214598944Sobrien  children[v_private] = 0;
214698944Sobrien  children[v_protected] = 0;
214798944Sobrien
214898944Sobrien  for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
214998944Sobrien    {
215098944Sobrien      /* If we have a virtual table pointer, omit it. */
215198944Sobrien      if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
215298944Sobrien	continue;
215398944Sobrien
215498944Sobrien      if (TYPE_FIELD_PROTECTED (type, i))
215598944Sobrien	children[v_protected]++;
215698944Sobrien      else if (TYPE_FIELD_PRIVATE (type, i))
215798944Sobrien	children[v_private]++;
215898944Sobrien      else
215998944Sobrien	children[v_public]++;
216098944Sobrien    }
216198944Sobrien}
216298944Sobrien
216398944Sobrienstatic char *
216498944Sobriencplus_name_of_variable (struct varobj *parent)
216598944Sobrien{
216698944Sobrien  return c_name_of_variable (parent);
216798944Sobrien}
216898944Sobrien
216998944Sobrienstatic char *
217098944Sobriencplus_name_of_child (struct varobj *parent, int index)
217198944Sobrien{
217298944Sobrien  char *name;
217398944Sobrien  struct type *type;
217498944Sobrien  int children[3];
217598944Sobrien
217698944Sobrien  if (CPLUS_FAKE_CHILD (parent))
217798944Sobrien    {
217898944Sobrien      /* Looking for children of public, private, or protected. */
217998944Sobrien      type = get_type_deref (parent->parent);
218098944Sobrien    }
218198944Sobrien  else
218298944Sobrien    type = get_type_deref (parent);
218398944Sobrien
218498944Sobrien  name = NULL;
218598944Sobrien  switch (TYPE_CODE (type))
218698944Sobrien    {
218798944Sobrien    case TYPE_CODE_STRUCT:
218898944Sobrien    case TYPE_CODE_UNION:
218998944Sobrien      cplus_class_num_children (type, children);
219098944Sobrien
219198944Sobrien      if (CPLUS_FAKE_CHILD (parent))
219298944Sobrien	{
219398944Sobrien	  int i;
219498944Sobrien
219598944Sobrien	  /* Skip over vptr, if it exists. */
219698944Sobrien	  if (TYPE_VPTR_BASETYPE (type) == type
219798944Sobrien	      && index >= TYPE_VPTR_FIELDNO (type))
219898944Sobrien	    index++;
219998944Sobrien
220098944Sobrien	  /* FIXME: This assumes that type orders
220198944Sobrien	     inherited, public, private, protected */
220298944Sobrien	  i = index + TYPE_N_BASECLASSES (type);
220398944Sobrien	  if (STREQ (parent->name, "private") || STREQ (parent->name, "protected"))
220498944Sobrien	    i += children[v_public];
220598944Sobrien	  if (STREQ (parent->name, "protected"))
220698944Sobrien	    i += children[v_private];
220798944Sobrien
220898944Sobrien	  name = TYPE_FIELD_NAME (type, i);
220998944Sobrien	}
221098944Sobrien      else if (index < TYPE_N_BASECLASSES (type))
221198944Sobrien	name = TYPE_FIELD_NAME (type, index);
221298944Sobrien      else
221398944Sobrien	{
221498944Sobrien	  /* Everything beyond the baseclasses can
221598944Sobrien	     only be "public", "private", or "protected" */
221698944Sobrien	  index -= TYPE_N_BASECLASSES (type);
221798944Sobrien	  switch (index)
221898944Sobrien	    {
221998944Sobrien	    case 0:
222098944Sobrien	      if (children[v_public] != 0)
222198944Sobrien		{
222298944Sobrien		  name = "public";
222398944Sobrien		  break;
222498944Sobrien		}
222598944Sobrien	    case 1:
222698944Sobrien	      if (children[v_private] != 0)
222798944Sobrien		{
222898944Sobrien		  name = "private";
222998944Sobrien		  break;
223098944Sobrien		}
223198944Sobrien	    case 2:
223298944Sobrien	      if (children[v_protected] != 0)
223398944Sobrien		{
223498944Sobrien		  name = "protected";
223598944Sobrien		  break;
223698944Sobrien		}
223798944Sobrien	    default:
223898944Sobrien	      /* error! */
223998944Sobrien	      break;
224098944Sobrien	    }
224198944Sobrien	}
224298944Sobrien      break;
224398944Sobrien
224498944Sobrien    default:
224598944Sobrien      break;
224698944Sobrien    }
224798944Sobrien
224898944Sobrien  if (name == NULL)
224998944Sobrien    return c_name_of_child (parent, index);
225098944Sobrien  else
225198944Sobrien    {
225298944Sobrien      if (name != NULL)
225398944Sobrien	name = savestring (name, strlen (name));
225498944Sobrien    }
225598944Sobrien
225698944Sobrien  return name;
225798944Sobrien}
225898944Sobrien
225998944Sobrienstatic struct value *
226098944Sobriencplus_value_of_root (struct varobj **var_handle)
226198944Sobrien{
226298944Sobrien  return c_value_of_root (var_handle);
226398944Sobrien}
226498944Sobrien
226598944Sobrienstatic struct value *
226698944Sobriencplus_value_of_child (struct varobj *parent, int index)
226798944Sobrien{
226898944Sobrien  struct type *type;
226998944Sobrien  struct value *value;
227098944Sobrien
227198944Sobrien  if (CPLUS_FAKE_CHILD (parent))
227298944Sobrien    type = get_type_deref (parent->parent);
227398944Sobrien  else
227498944Sobrien    type = get_type_deref (parent);
227598944Sobrien
227698944Sobrien  value = NULL;
227798944Sobrien
227898944Sobrien  if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
227998944Sobrien      ((TYPE_CODE (type)) == TYPE_CODE_UNION))
228098944Sobrien    {
228198944Sobrien      if (CPLUS_FAKE_CHILD (parent))
228298944Sobrien	{
228398944Sobrien	  char *name;
228498944Sobrien	  struct value *temp = parent->parent->value;
228598944Sobrien
228698944Sobrien	  if (temp == NULL)
228798944Sobrien	    return NULL;
228898944Sobrien
228998944Sobrien	  name = name_of_child (parent, index);
229098944Sobrien	  gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
229198944Sobrien				"cplus_structure");
229298944Sobrien	  if (value != NULL)
229398944Sobrien	    release_value (value);
229498944Sobrien
229598944Sobrien	  xfree (name);
229698944Sobrien	}
229798944Sobrien      else if (index >= TYPE_N_BASECLASSES (type))
229898944Sobrien	{
229998944Sobrien	  /* public, private, or protected */
230098944Sobrien	  return NULL;
230198944Sobrien	}
230298944Sobrien      else
230398944Sobrien	{
230498944Sobrien	  /* Baseclass */
230598944Sobrien	  if (parent->value != NULL)
230698944Sobrien	    {
230798944Sobrien	      struct value *temp = NULL;
230898944Sobrien
230998944Sobrien	      if (TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_PTR
231098944Sobrien		  || TYPE_CODE (VALUE_TYPE (parent->value)) == TYPE_CODE_REF)
231198944Sobrien		{
231298944Sobrien		  if (!gdb_value_ind (parent->value, &temp))
231398944Sobrien		    return NULL;
231498944Sobrien		}
231598944Sobrien	      else
231698944Sobrien		temp = parent->value;
231798944Sobrien
231898944Sobrien	      if (temp != NULL)
231998944Sobrien		{
232098944Sobrien		  value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
232198944Sobrien		  release_value (value);
232298944Sobrien		}
232398944Sobrien	      else
232498944Sobrien		{
232598944Sobrien		  /* We failed to evaluate the parent's value, so don't even
232698944Sobrien		     bother trying to evaluate this child. */
232798944Sobrien		  return NULL;
232898944Sobrien		}
232998944Sobrien	    }
233098944Sobrien	}
233198944Sobrien    }
233298944Sobrien
233398944Sobrien  if (value == NULL)
233498944Sobrien    return c_value_of_child (parent, index);
233598944Sobrien
233698944Sobrien  return value;
233798944Sobrien}
233898944Sobrien
233998944Sobrienstatic struct type *
234098944Sobriencplus_type_of_child (struct varobj *parent, int index)
234198944Sobrien{
234298944Sobrien  struct type *type, *t;
234398944Sobrien
234498944Sobrien  if (CPLUS_FAKE_CHILD (parent))
234598944Sobrien    {
234698944Sobrien      /* Looking for the type of a child of public, private, or protected. */
234798944Sobrien      t = get_type_deref (parent->parent);
234898944Sobrien    }
234998944Sobrien  else
235098944Sobrien    t = get_type_deref (parent);
235198944Sobrien
235298944Sobrien  type = NULL;
235398944Sobrien  switch (TYPE_CODE (t))
235498944Sobrien    {
235598944Sobrien    case TYPE_CODE_STRUCT:
235698944Sobrien    case TYPE_CODE_UNION:
235798944Sobrien      if (CPLUS_FAKE_CHILD (parent))
235898944Sobrien	{
235998944Sobrien	  char *name = cplus_name_of_child (parent, index);
236098944Sobrien	  type = lookup_struct_elt_type (t, name, 0);
236198944Sobrien	  xfree (name);
236298944Sobrien	}
236398944Sobrien      else if (index < TYPE_N_BASECLASSES (t))
236498944Sobrien	type = TYPE_FIELD_TYPE (t, index);
236598944Sobrien      else
236698944Sobrien	{
236798944Sobrien	  /* special */
236898944Sobrien	  return NULL;
236998944Sobrien	}
237098944Sobrien      break;
237198944Sobrien
237298944Sobrien    default:
237398944Sobrien      break;
237498944Sobrien    }
237598944Sobrien
237698944Sobrien  if (type == NULL)
237798944Sobrien    return c_type_of_child (parent, index);
237898944Sobrien
237998944Sobrien  return type;
238098944Sobrien}
238198944Sobrien
238298944Sobrienstatic int
238398944Sobriencplus_variable_editable (struct varobj *var)
238498944Sobrien{
238598944Sobrien  if (CPLUS_FAKE_CHILD (var))
238698944Sobrien    return 0;
238798944Sobrien
238898944Sobrien  return c_variable_editable (var);
238998944Sobrien}
239098944Sobrien
239198944Sobrienstatic char *
239298944Sobriencplus_value_of_variable (struct varobj *var)
239398944Sobrien{
239498944Sobrien
239598944Sobrien  /* If we have one of our special types, don't print out
239698944Sobrien     any value. */
239798944Sobrien  if (CPLUS_FAKE_CHILD (var))
239898944Sobrien    return xstrdup ("");
239998944Sobrien
240098944Sobrien  return c_value_of_variable (var);
240198944Sobrien}
240298944Sobrien
240398944Sobrien/* Java */
240498944Sobrien
240598944Sobrienstatic int
240698944Sobrienjava_number_of_children (struct varobj *var)
240798944Sobrien{
240898944Sobrien  return cplus_number_of_children (var);
240998944Sobrien}
241098944Sobrien
241198944Sobrienstatic char *
241298944Sobrienjava_name_of_variable (struct varobj *parent)
241398944Sobrien{
241498944Sobrien  char *p, *name;
241598944Sobrien
241698944Sobrien  name = cplus_name_of_variable (parent);
241798944Sobrien  /* If  the name has "-" in it, it is because we
241898944Sobrien     needed to escape periods in the name... */
241998944Sobrien  p = name;
242098944Sobrien
242198944Sobrien  while (*p != '\000')
242298944Sobrien    {
242398944Sobrien      if (*p == '-')
242498944Sobrien	*p = '.';
242598944Sobrien      p++;
242698944Sobrien    }
242798944Sobrien
242898944Sobrien  return name;
242998944Sobrien}
243098944Sobrien
243198944Sobrienstatic char *
243298944Sobrienjava_name_of_child (struct varobj *parent, int index)
243398944Sobrien{
243498944Sobrien  char *name, *p;
243598944Sobrien
243698944Sobrien  name = cplus_name_of_child (parent, index);
243798944Sobrien  /* Escape any periods in the name... */
243898944Sobrien  p = name;
243998944Sobrien
244098944Sobrien  while (*p != '\000')
244198944Sobrien    {
244298944Sobrien      if (*p == '.')
244398944Sobrien	*p = '-';
244498944Sobrien      p++;
244598944Sobrien    }
244698944Sobrien
244798944Sobrien  return name;
244898944Sobrien}
244998944Sobrien
245098944Sobrienstatic struct value *
245198944Sobrienjava_value_of_root (struct varobj **var_handle)
245298944Sobrien{
245398944Sobrien  return cplus_value_of_root (var_handle);
245498944Sobrien}
245598944Sobrien
245698944Sobrienstatic struct value *
245798944Sobrienjava_value_of_child (struct varobj *parent, int index)
245898944Sobrien{
245998944Sobrien  return cplus_value_of_child (parent, index);
246098944Sobrien}
246198944Sobrien
246298944Sobrienstatic struct type *
246398944Sobrienjava_type_of_child (struct varobj *parent, int index)
246498944Sobrien{
246598944Sobrien  return cplus_type_of_child (parent, index);
246698944Sobrien}
246798944Sobrien
246898944Sobrienstatic int
246998944Sobrienjava_variable_editable (struct varobj *var)
247098944Sobrien{
247198944Sobrien  return cplus_variable_editable (var);
247298944Sobrien}
247398944Sobrien
247498944Sobrienstatic char *
247598944Sobrienjava_value_of_variable (struct varobj *var)
247698944Sobrien{
247798944Sobrien  return cplus_value_of_variable (var);
247898944Sobrien}
247998944Sobrien
248098944Sobrienextern void _initialize_varobj (void);
248198944Sobrienvoid
248298944Sobrien_initialize_varobj (void)
248398944Sobrien{
248498944Sobrien  int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
248598944Sobrien
248698944Sobrien  varobj_table = xmalloc (sizeof_table);
248798944Sobrien  memset (varobj_table, 0, sizeof_table);
248898944Sobrien
248998944Sobrien  add_show_from_set (add_set_cmd ("debugvarobj", class_maintenance, var_zinteger, (char *) &varobjdebug, "Set varobj debugging.\n\
249098944SobrienWhen non-zero, varobj debugging is enabled.", &setlist),
249198944Sobrien		     &showlist);
249298944Sobrien}
2493