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