be18a1a9c2a924faed6dd7c08d1db956bea3e185
[external/binutils.git] / gdb / varobj.c
1 /* Implementation of the GDB variable objects API.
2
3    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4    Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor,
19    Boston, MA 02110-1301, USA.  */
20
21 #include "defs.h"
22 #include "exceptions.h"
23 #include "value.h"
24 #include "expression.h"
25 #include "frame.h"
26 #include "language.h"
27 #include "wrapper.h"
28 #include "gdbcmd.h"
29 #include "block.h"
30
31 #include "gdb_assert.h"
32 #include "gdb_string.h"
33
34 #include "varobj.h"
35
36 /* Non-zero if we want to see trace of varobj level stuff.  */
37
38 int varobjdebug = 0;
39 static void
40 show_varobjdebug (struct ui_file *file, int from_tty,
41                   struct cmd_list_element *c, const char *value)
42 {
43   fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
44 }
45
46 /* String representations of gdb's format codes */
47 char *varobj_format_string[] =
48   { "natural", "binary", "decimal", "hexadecimal", "octal" };
49
50 /* String representations of gdb's known languages */
51 char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
52
53 /* Data structures */
54
55 /* Every root variable has one of these structures saved in its
56    varobj. Members which must be free'd are noted. */
57 struct varobj_root
58 {
59
60   /* Alloc'd expression for this parent. */
61   struct expression *exp;
62
63   /* Block for which this expression is valid */
64   struct block *valid_block;
65
66   /* The frame for this expression */
67   struct frame_id frame;
68
69   /* If 1, "update" always recomputes the frame & valid block
70      using the currently selected frame. */
71   int use_selected_frame;
72
73   /* Language info for this variable and its children */
74   struct language_specific *lang;
75
76   /* The varobj for this root node. */
77   struct varobj *rootvar;
78
79   /* Next root variable */
80   struct varobj_root *next;
81 };
82
83 /* Every variable in the system has a structure of this type defined
84    for it. This structure holds all information necessary to manipulate
85    a particular object variable. Members which must be freed are noted. */
86 struct varobj
87 {
88
89   /* Alloc'd name of the variable for this object.. If this variable is a
90      child, then this name will be the child's source name.
91      (bar, not foo.bar) */
92   /* NOTE: This is the "expression" */
93   char *name;
94
95   /* The alloc'd name for this variable's object. This is here for
96      convenience when constructing this object's children. */
97   char *obj_name;
98
99   /* Index of this variable in its parent or -1 */
100   int index;
101
102   /* The type of this variable. This may NEVER be NULL. */
103   struct type *type;
104
105   /* The value of this expression or subexpression.  This may be NULL. 
106      Invariant: if varobj_value_is_changeable_p (this) is non-zero, 
107      the value is either NULL, or not lazy.  */
108   struct value *value;
109
110   /* Did an error occur evaluating the expression or getting its value? */
111   int error;
112
113   /* The number of (immediate) children this variable has */
114   int num_children;
115
116   /* If this object is a child, this points to its immediate parent. */
117   struct varobj *parent;
118
119   /* A list of this object's children */
120   struct varobj_child *children;
121
122   /* Description of the root variable. Points to root variable for children. */
123   struct varobj_root *root;
124
125   /* The format of the output for this object */
126   enum varobj_display_formats format;
127
128   /* Was this variable updated via a varobj_set_value operation */
129   int updated;
130 };
131
132 /* Every variable keeps a linked list of its children, described
133    by the following structure. */
134 /* FIXME: Deprecated.  All should use vlist instead */
135
136 struct varobj_child
137 {
138
139   /* Pointer to the child's data */
140   struct varobj *child;
141
142   /* Pointer to the next child */
143   struct varobj_child *next;
144 };
145
146 /* A stack of varobjs */
147 /* FIXME: Deprecated.  All should use vlist instead */
148
149 struct vstack
150 {
151   struct varobj *var;
152   struct vstack *next;
153 };
154
155 struct cpstack
156 {
157   char *name;
158   struct cpstack *next;
159 };
160
161 /* A list of varobjs */
162
163 struct vlist
164 {
165   struct varobj *var;
166   struct vlist *next;
167 };
168
169 /* Private function prototypes */
170
171 /* Helper functions for the above subcommands. */
172
173 static int delete_variable (struct cpstack **, struct varobj *, int);
174
175 static void delete_variable_1 (struct cpstack **, int *,
176                                struct varobj *, int, int);
177
178 static int install_variable (struct varobj *);
179
180 static void uninstall_variable (struct varobj *);
181
182 static struct varobj *child_exists (struct varobj *, char *);
183
184 static struct varobj *create_child (struct varobj *, int, char *);
185
186 static void save_child_in_parent (struct varobj *, struct varobj *);
187
188 static void remove_child_from_parent (struct varobj *, struct varobj *);
189
190 /* Utility routines */
191
192 static struct varobj *new_variable (void);
193
194 static struct varobj *new_root_variable (void);
195
196 static void free_variable (struct varobj *var);
197
198 static struct cleanup *make_cleanup_free_variable (struct varobj *var);
199
200 static struct type *get_type (struct varobj *var);
201
202 static struct type *get_type_deref (struct varobj *var);
203
204 static struct type *get_target_type (struct type *);
205
206 static enum varobj_display_formats variable_default_display (struct varobj *);
207
208 static void vpush (struct vstack **pstack, struct varobj *var);
209
210 static struct varobj *vpop (struct vstack **pstack);
211
212 static void cppush (struct cpstack **pstack, char *name);
213
214 static char *cppop (struct cpstack **pstack);
215
216 static int install_new_value (struct varobj *var, struct value *value, 
217                               int initial);
218
219 /* Language-specific routines. */
220
221 static enum varobj_languages variable_language (struct varobj *var);
222
223 static int number_of_children (struct varobj *);
224
225 static char *name_of_variable (struct varobj *);
226
227 static char *name_of_child (struct varobj *, int);
228
229 static struct value *value_of_root (struct varobj **var_handle, int *);
230
231 static struct value *value_of_child (struct varobj *parent, int index);
232
233 static int variable_editable (struct varobj *var);
234
235 static char *my_value_of_variable (struct varobj *var);
236
237 static int varobj_value_is_changeable_p (struct varobj *var);
238
239 static int is_root_p (struct varobj *var);
240
241 /* C implementation */
242
243 static int c_number_of_children (struct varobj *var);
244
245 static char *c_name_of_variable (struct varobj *parent);
246
247 static char *c_name_of_child (struct varobj *parent, int index);
248
249 static struct value *c_value_of_root (struct varobj **var_handle);
250
251 static struct value *c_value_of_child (struct varobj *parent, int index);
252
253 static struct type *c_type_of_child (struct varobj *parent, int index);
254
255 static int c_variable_editable (struct varobj *var);
256
257 static char *c_value_of_variable (struct varobj *var);
258
259 /* C++ implementation */
260
261 static int cplus_number_of_children (struct varobj *var);
262
263 static void cplus_class_num_children (struct type *type, int children[3]);
264
265 static char *cplus_name_of_variable (struct varobj *parent);
266
267 static char *cplus_name_of_child (struct varobj *parent, int index);
268
269 static struct value *cplus_value_of_root (struct varobj **var_handle);
270
271 static struct value *cplus_value_of_child (struct varobj *parent, int index);
272
273 static struct type *cplus_type_of_child (struct varobj *parent, int index);
274
275 static int cplus_variable_editable (struct varobj *var);
276
277 static char *cplus_value_of_variable (struct varobj *var);
278
279 /* Java implementation */
280
281 static int java_number_of_children (struct varobj *var);
282
283 static char *java_name_of_variable (struct varobj *parent);
284
285 static char *java_name_of_child (struct varobj *parent, int index);
286
287 static struct value *java_value_of_root (struct varobj **var_handle);
288
289 static struct value *java_value_of_child (struct varobj *parent, int index);
290
291 static struct type *java_type_of_child (struct varobj *parent, int index);
292
293 static int java_variable_editable (struct varobj *var);
294
295 static char *java_value_of_variable (struct varobj *var);
296
297 /* The language specific vector */
298
299 struct language_specific
300 {
301
302   /* The language of this variable */
303   enum varobj_languages language;
304
305   /* The number of children of PARENT. */
306   int (*number_of_children) (struct varobj * parent);
307
308   /* The name (expression) of a root varobj. */
309   char *(*name_of_variable) (struct varobj * parent);
310
311   /* The name of the INDEX'th child of PARENT. */
312   char *(*name_of_child) (struct varobj * parent, int index);
313
314   /* The ``struct value *'' of the root variable ROOT. */
315   struct value *(*value_of_root) (struct varobj ** root_handle);
316
317   /* The ``struct value *'' of the INDEX'th child of PARENT. */
318   struct value *(*value_of_child) (struct varobj * parent, int index);
319
320   /* The type of the INDEX'th child of PARENT. */
321   struct type *(*type_of_child) (struct varobj * parent, int index);
322
323   /* Is VAR editable? */
324   int (*variable_editable) (struct varobj * var);
325
326   /* The current value of VAR. */
327   char *(*value_of_variable) (struct varobj * var);
328 };
329
330 /* Array of known source language routines. */
331 static struct language_specific languages[vlang_end] = {
332   /* Unknown (try treating as C */
333   {
334    vlang_unknown,
335    c_number_of_children,
336    c_name_of_variable,
337    c_name_of_child,
338    c_value_of_root,
339    c_value_of_child,
340    c_type_of_child,
341    c_variable_editable,
342    c_value_of_variable}
343   ,
344   /* C */
345   {
346    vlang_c,
347    c_number_of_children,
348    c_name_of_variable,
349    c_name_of_child,
350    c_value_of_root,
351    c_value_of_child,
352    c_type_of_child,
353    c_variable_editable,
354    c_value_of_variable}
355   ,
356   /* C++ */
357   {
358    vlang_cplus,
359    cplus_number_of_children,
360    cplus_name_of_variable,
361    cplus_name_of_child,
362    cplus_value_of_root,
363    cplus_value_of_child,
364    cplus_type_of_child,
365    cplus_variable_editable,
366    cplus_value_of_variable}
367   ,
368   /* Java */
369   {
370    vlang_java,
371    java_number_of_children,
372    java_name_of_variable,
373    java_name_of_child,
374    java_value_of_root,
375    java_value_of_child,
376    java_type_of_child,
377    java_variable_editable,
378    java_value_of_variable}
379 };
380
381 /* A little convenience enum for dealing with C++/Java */
382 enum vsections
383 {
384   v_public = 0, v_private, v_protected
385 };
386
387 /* Private data */
388
389 /* Mappings of varobj_display_formats enums to gdb's format codes */
390 static int format_code[] = { 0, 't', 'd', 'x', 'o' };
391
392 /* Header of the list of root variable objects */
393 static struct varobj_root *rootlist;
394 static int rootcount = 0;       /* number of root varobjs in the list */
395
396 /* Prime number indicating the number of buckets in the hash table */
397 /* A prime large enough to avoid too many colisions */
398 #define VAROBJ_TABLE_SIZE 227
399
400 /* Pointer to the varobj hash table (built at run time) */
401 static struct vlist **varobj_table;
402
403 /* Is the variable X one of our "fake" children? */
404 #define CPLUS_FAKE_CHILD(x) \
405 ((x) != NULL && (x)->type == NULL && (x)->value == NULL)
406 \f
407
408 /* API Implementation */
409 static int
410 is_root_p (struct varobj *var)
411 {
412   return (var->root->rootvar == var);
413 }
414
415 /* Creates a varobj (not its children) */
416
417 /* Return the full FRAME which corresponds to the given CORE_ADDR
418    or NULL if no FRAME on the chain corresponds to CORE_ADDR.  */
419
420 static struct frame_info *
421 find_frame_addr_in_frame_chain (CORE_ADDR frame_addr)
422 {
423   struct frame_info *frame = NULL;
424
425   if (frame_addr == (CORE_ADDR) 0)
426     return NULL;
427
428   while (1)
429     {
430       frame = get_prev_frame (frame);
431       if (frame == NULL)
432         return NULL;
433       if (get_frame_base_address (frame) == frame_addr)
434         return frame;
435     }
436 }
437
438 struct varobj *
439 varobj_create (char *objname,
440                char *expression, CORE_ADDR frame, enum varobj_type type)
441 {
442   struct varobj *var;
443   struct frame_info *fi;
444   struct frame_info *old_fi = NULL;
445   struct block *block;
446   struct cleanup *old_chain;
447
448   /* Fill out a varobj structure for the (root) variable being constructed. */
449   var = new_root_variable ();
450   old_chain = make_cleanup_free_variable (var);
451
452   if (expression != NULL)
453     {
454       char *p;
455       enum varobj_languages lang;
456       struct value *value;
457
458       /* Parse and evaluate the expression, filling in as much
459          of the variable's data as possible */
460
461       /* Allow creator to specify context of variable */
462       if ((type == USE_CURRENT_FRAME) || (type == USE_SELECTED_FRAME))
463         fi = deprecated_selected_frame;
464       else
465         /* FIXME: cagney/2002-11-23: This code should be doing a
466            lookup using the frame ID and not just the frame's
467            ``address''.  This, of course, means an interface change.
468            However, with out that interface change ISAs, such as the
469            ia64 with its two stacks, won't work.  Similar goes for the
470            case where there is a frameless function.  */
471         fi = find_frame_addr_in_frame_chain (frame);
472
473       /* frame = -2 means always use selected frame */
474       if (type == USE_SELECTED_FRAME)
475         var->root->use_selected_frame = 1;
476
477       block = NULL;
478       if (fi != NULL)
479         block = get_frame_block (fi, 0);
480
481       p = expression;
482       innermost_block = NULL;
483       /* Wrap the call to parse expression, so we can 
484          return a sensible error. */
485       if (!gdb_parse_exp_1 (&p, block, 0, &var->root->exp))
486         {
487           return NULL;
488         }
489
490       /* Don't allow variables to be created for types. */
491       if (var->root->exp->elts[0].opcode == OP_TYPE)
492         {
493           do_cleanups (old_chain);
494           fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
495                               " as an expression.\n");
496           return NULL;
497         }
498
499       var->format = variable_default_display (var);
500       var->root->valid_block = innermost_block;
501       var->name = savestring (expression, strlen (expression));
502
503       /* When the frame is different from the current frame, 
504          we must select the appropriate frame before parsing
505          the expression, otherwise the value will not be current.
506          Since select_frame is so benign, just call it for all cases. */
507       if (fi != NULL)
508         {
509           var->root->frame = get_frame_id (fi);
510           old_fi = deprecated_selected_frame;
511           select_frame (fi);
512         }
513
514       /* We definitively need to catch errors here.
515          If evaluate_expression succeeds we got the value we wanted.
516          But if it fails, we still go on with a call to evaluate_type()  */
517       if (!gdb_evaluate_expression (var->root->exp, &value))
518         /* Error getting the value.  Try to at least get the
519            right type.  */
520         value = evaluate_type (var->root->exp);
521
522       var->type = value_type (value);
523       install_new_value (var, value, 1 /* Initial assignment */);
524
525       /* Set language info */
526       lang = variable_language (var);
527       var->root->lang = &languages[lang];
528
529       /* Set ourselves as our root */
530       var->root->rootvar = var;
531
532       /* Reset the selected frame */
533       if (fi != NULL)
534         select_frame (old_fi);
535     }
536
537   /* If the variable object name is null, that means this
538      is a temporary variable, so don't install it. */
539
540   if ((var != NULL) && (objname != NULL))
541     {
542       var->obj_name = savestring (objname, strlen (objname));
543
544       /* If a varobj name is duplicated, the install will fail so
545          we must clenup */
546       if (!install_variable (var))
547         {
548           do_cleanups (old_chain);
549           return NULL;
550         }
551     }
552
553   discard_cleanups (old_chain);
554   return var;
555 }
556
557 /* Generates an unique name that can be used for a varobj */
558
559 char *
560 varobj_gen_name (void)
561 {
562   static int id = 0;
563   char *obj_name;
564
565   /* generate a name for this object */
566   id++;
567   obj_name = xstrprintf ("var%d", id);
568
569   return obj_name;
570 }
571
572 /* Given an "objname", returns the pointer to the corresponding varobj
573    or NULL if not found */
574
575 struct varobj *
576 varobj_get_handle (char *objname)
577 {
578   struct vlist *cv;
579   const char *chp;
580   unsigned int index = 0;
581   unsigned int i = 1;
582
583   for (chp = objname; *chp; chp++)
584     {
585       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
586     }
587
588   cv = *(varobj_table + index);
589   while ((cv != NULL) && (strcmp (cv->var->obj_name, objname) != 0))
590     cv = cv->next;
591
592   if (cv == NULL)
593     error (_("Variable object not found"));
594
595   return cv->var;
596 }
597
598 /* Given the handle, return the name of the object */
599
600 char *
601 varobj_get_objname (struct varobj *var)
602 {
603   return var->obj_name;
604 }
605
606 /* Given the handle, return the expression represented by the object */
607
608 char *
609 varobj_get_expression (struct varobj *var)
610 {
611   return name_of_variable (var);
612 }
613
614 /* Deletes a varobj and all its children if only_children == 0,
615    otherwise deletes only the children; returns a malloc'ed list of all the 
616    (malloc'ed) names of the variables that have been deleted (NULL terminated) */
617
618 int
619 varobj_delete (struct varobj *var, char ***dellist, int only_children)
620 {
621   int delcount;
622   int mycount;
623   struct cpstack *result = NULL;
624   char **cp;
625
626   /* Initialize a stack for temporary results */
627   cppush (&result, NULL);
628
629   if (only_children)
630     /* Delete only the variable children */
631     delcount = delete_variable (&result, var, 1 /* only the children */ );
632   else
633     /* Delete the variable and all its children */
634     delcount = delete_variable (&result, var, 0 /* parent+children */ );
635
636   /* We may have been asked to return a list of what has been deleted */
637   if (dellist != NULL)
638     {
639       *dellist = xmalloc ((delcount + 1) * sizeof (char *));
640
641       cp = *dellist;
642       mycount = delcount;
643       *cp = cppop (&result);
644       while ((*cp != NULL) && (mycount > 0))
645         {
646           mycount--;
647           cp++;
648           *cp = cppop (&result);
649         }
650
651       if (mycount || (*cp != NULL))
652         warning (_("varobj_delete: assertion failed - mycount(=%d) <> 0"),
653                  mycount);
654     }
655
656   return delcount;
657 }
658
659 /* Set/Get variable object display format */
660
661 enum varobj_display_formats
662 varobj_set_display_format (struct varobj *var,
663                            enum varobj_display_formats format)
664 {
665   switch (format)
666     {
667     case FORMAT_NATURAL:
668     case FORMAT_BINARY:
669     case FORMAT_DECIMAL:
670     case FORMAT_HEXADECIMAL:
671     case FORMAT_OCTAL:
672       var->format = format;
673       break;
674
675     default:
676       var->format = variable_default_display (var);
677     }
678
679   return var->format;
680 }
681
682 enum varobj_display_formats
683 varobj_get_display_format (struct varobj *var)
684 {
685   return var->format;
686 }
687
688 int
689 varobj_get_num_children (struct varobj *var)
690 {
691   if (var->num_children == -1)
692     var->num_children = number_of_children (var);
693
694   return var->num_children;
695 }
696
697 /* Creates a list of the immediate children of a variable object;
698    the return code is the number of such children or -1 on error */
699
700 int
701 varobj_list_children (struct varobj *var, struct varobj ***childlist)
702 {
703   struct varobj *child;
704   char *name;
705   int i;
706
707   /* sanity check: have we been passed a pointer? */
708   if (childlist == NULL)
709     return -1;
710
711   *childlist = NULL;
712
713   if (var->num_children == -1)
714     var->num_children = number_of_children (var);
715
716   /* List of children */
717   *childlist = xmalloc ((var->num_children + 1) * sizeof (struct varobj *));
718
719   for (i = 0; i < var->num_children; i++)
720     {
721       /* Mark as the end in case we bail out */
722       *((*childlist) + i) = NULL;
723
724       /* check if child exists, if not create */
725       name = name_of_child (var, i);
726       child = child_exists (var, name);
727       if (child == NULL)
728         child = create_child (var, i, name);
729
730       *((*childlist) + i) = child;
731     }
732
733   /* End of list is marked by a NULL pointer */
734   *((*childlist) + i) = NULL;
735
736   return var->num_children;
737 }
738
739 /* Obtain the type of an object Variable as a string similar to the one gdb
740    prints on the console */
741
742 char *
743 varobj_get_type (struct varobj *var)
744 {
745   struct value *val;
746   struct cleanup *old_chain;
747   struct ui_file *stb;
748   char *thetype;
749   long length;
750
751   /* For the "fake" variables, do not return a type. (It's type is
752      NULL, too.) */
753   if (CPLUS_FAKE_CHILD (var))
754     return NULL;
755
756   stb = mem_fileopen ();
757   old_chain = make_cleanup_ui_file_delete (stb);
758
759   /* To print the type, we simply create a zero ``struct value *'' and
760      cast it to our type. We then typeprint this variable. */
761   val = value_zero (var->type, not_lval);
762   type_print (value_type (val), "", stb, -1);
763
764   thetype = ui_file_xstrdup (stb, &length);
765   do_cleanups (old_chain);
766   return thetype;
767 }
768
769 /* Obtain the type of an object variable.  */
770
771 struct type *
772 varobj_get_gdb_type (struct varobj *var)
773 {
774   return var->type;
775 }
776
777 enum varobj_languages
778 varobj_get_language (struct varobj *var)
779 {
780   return variable_language (var);
781 }
782
783 int
784 varobj_get_attributes (struct varobj *var)
785 {
786   int attributes = 0;
787
788   if (variable_editable (var))
789     /* FIXME: define masks for attributes */
790     attributes |= 0x00000001;   /* Editable */
791
792   return attributes;
793 }
794
795 char *
796 varobj_get_value (struct varobj *var)
797 {
798   return my_value_of_variable (var);
799 }
800
801 /* Set the value of an object variable (if it is editable) to the
802    value of the given expression */
803 /* Note: Invokes functions that can call error() */
804
805 int
806 varobj_set_value (struct varobj *var, char *expression)
807 {
808   struct value *val;
809   int offset = 0;
810   int error = 0;
811
812   /* The argument "expression" contains the variable's new value.
813      We need to first construct a legal expression for this -- ugh! */
814   /* Does this cover all the bases? */
815   struct expression *exp;
816   struct value *value;
817   int saved_input_radix = input_radix;
818
819   if (var->value != NULL && variable_editable (var) && !var->error)
820     {
821       char *s = expression;
822       int i;
823
824       input_radix = 10;         /* ALWAYS reset to decimal temporarily */
825       exp = parse_exp_1 (&s, 0, 0);
826       if (!gdb_evaluate_expression (exp, &value))
827         {
828           /* We cannot proceed without a valid expression. */
829           xfree (exp);
830           return 0;
831         }
832
833       /* All types that are editable must also be changeable.  */
834       gdb_assert (varobj_value_is_changeable_p (var));
835
836       /* The value of a changeable variable object must not be lazy.  */
837       gdb_assert (!value_lazy (var->value));
838
839       /* Need to coerce the input.  We want to check if the
840          value of the variable object will be different
841          after assignment, and the first thing value_assign
842          does is coerce the input.
843          For example, if we are assigning an array to a pointer variable we
844          should compare the pointer with the the array's address, not with the
845          array's content.  */
846       value = coerce_array (value);
847
848       /* The new value may be lazy.  gdb_value_assign, or 
849          rather value_contents, will take care of this.
850          If fetching of the new value will fail, gdb_value_assign
851          with catch the exception.  */
852       if (!gdb_value_assign (var->value, value, &val))
853         return 0;
854      
855       /* If the value has changed, record it, so that next -var-update can
856          report this change.  If a variable had a value of '1', we've set it
857          to '333' and then set again to '1', when -var-update will report this
858          variable as changed -- because the first assignment has set the
859          'updated' flag.  There's no need to optimize that, because return value
860          of -var-update should be considered an approximation.  */
861       var->updated = install_new_value (var, val, 0 /* Compare values. */);
862       input_radix = saved_input_radix;
863       return 1;
864     }
865
866   return 0;
867 }
868
869 /* Returns a malloc'ed list with all root variable objects */
870 int
871 varobj_list (struct varobj ***varlist)
872 {
873   struct varobj **cv;
874   struct varobj_root *croot;
875   int mycount = rootcount;
876
877   /* Alloc (rootcount + 1) entries for the result */
878   *varlist = xmalloc ((rootcount + 1) * sizeof (struct varobj *));
879
880   cv = *varlist;
881   croot = rootlist;
882   while ((croot != NULL) && (mycount > 0))
883     {
884       *cv = croot->rootvar;
885       mycount--;
886       cv++;
887       croot = croot->next;
888     }
889   /* Mark the end of the list */
890   *cv = NULL;
891
892   if (mycount || (croot != NULL))
893     warning
894       ("varobj_list: assertion failed - wrong tally of root vars (%d:%d)",
895        rootcount, mycount);
896
897   return rootcount;
898 }
899
900 /* Assign a new value to a variable object.  If INITIAL is non-zero,
901    this is the first assignement after the variable object was just
902    created, or changed type.  In that case, just assign the value 
903    and return 0.
904    Otherwise, assign the value and if type_changeable returns non-zero,
905    find if the new value is different from the current value.
906    Return 1 if so, and 0 if the values are equal.  
907
908    The VALUE parameter should not be released -- the function will
909    take care of releasing it when needed.  */
910 static int
911 install_new_value (struct varobj *var, struct value *value, int initial)
912
913   int changeable;
914   int need_to_fetch;
915   int changed = 0;
916
917   var->error = 0;
918   /* We need to know the varobj's type to decide if the value should
919      be fetched or not.  C++ fake children (public/protected/private) don't have
920      a type. */
921   gdb_assert (var->type || CPLUS_FAKE_CHILD (var));
922   changeable = varobj_value_is_changeable_p (var);
923   need_to_fetch = changeable;
924
925   /* We are not interested in the address of references, and given
926      that in C++ a reference is not rebindable, it cannot
927      meaningfully change.  So, get hold of the real value.  */
928   if (value)
929     {
930       value = coerce_ref (value);
931       release_value (value);
932     }
933
934   if (var->type && TYPE_CODE (var->type) == TYPE_CODE_UNION)
935     /* For unions, we need to fetch the value implicitly because
936        of implementation of union member fetch.  When gdb
937        creates a value for a field and the value of the enclosing
938        structure is not lazy,  it immediately copies the necessary
939        bytes from the enclosing values.  If the enclosing value is
940        lazy, the call to value_fetch_lazy on the field will read
941        the data from memory.  For unions, that means we'll read the
942        same memory more than once, which is not desirable.  So
943        fetch now.  */
944     need_to_fetch = 1;
945
946   /* The new value might be lazy.  If the type is changeable,
947      that is we'll be comparing values of this type, fetch the
948      value now.  Otherwise, on the next update the old value
949      will be lazy, which means we've lost that old value.  */
950   if (need_to_fetch && value && value_lazy (value))
951     {
952       if (!gdb_value_fetch_lazy (value))
953         {
954           var->error = 1;
955           /* Set the value to NULL, so that for the next -var-update,
956              we don't try to compare the new value with this value,
957              that we couldn't even read.  */
958           value = NULL;
959         }
960       else
961         var->error = 0;
962     }
963
964   /* If the type is changeable, compare the old and the new values.
965      If this is the initial assignment, we don't have any old value
966      to compare with.  */
967   if (!initial && changeable)
968     {
969       /* If the value of the varobj was changed by -var-set-value, then the 
970          value in the varobj and in the target is the same.  However, that value
971          is different from the value that the varobj had after the previous
972          -var-update. So need to the varobj as changed.  */      
973       if (var->updated)
974         changed = 1;
975       else 
976         {
977           /* Try to compare the values.  That requires that both
978              values are non-lazy.  */
979           
980           /* Quick comparison of NULL values.  */
981           if (var->value == NULL && value == NULL)
982             /* Equal. */
983             ;
984           else if (var->value == NULL || value == NULL)
985             changed = 1;
986           else
987             {
988               gdb_assert (!value_lazy (var->value));
989               gdb_assert (!value_lazy (value));
990               
991               if (!value_contents_equal (var->value, value))
992                 changed = 1;
993             }
994         }
995     }
996     
997   /* We must always keep the new value, since children depend on it.  */
998   if (var->value != NULL)
999     value_free (var->value);
1000   var->value = value;
1001   var->updated = 0;
1002   
1003   gdb_assert (!var->value || value_type (var->value));
1004
1005   return changed;
1006 }
1007   
1008
1009 /* Update the values for a variable and its children.  This is a
1010    two-pronged attack.  First, re-parse the value for the root's
1011    expression to see if it's changed.  Then go all the way
1012    through its children, reconstructing them and noting if they've
1013    changed.
1014    Return value:
1015     -1 if there was an error updating the varobj
1016     -2 if the type changed
1017     Otherwise it is the number of children + parent changed
1018
1019    Only root variables can be updated... 
1020
1021    NOTE: This function may delete the caller's varobj. If it
1022    returns -2, then it has done this and VARP will be modified
1023    to point to the new varobj. */
1024
1025 int
1026 varobj_update (struct varobj **varp, struct varobj ***changelist)
1027 {
1028   int changed = 0;
1029   int error = 0;
1030   int type_changed;
1031   int i;
1032   int vleft;
1033   struct varobj *v;
1034   struct varobj **cv;
1035   struct varobj **templist = NULL;
1036   struct value *new;
1037   struct vstack *stack = NULL;
1038   struct vstack *result = NULL;
1039   struct frame_id old_fid;
1040   struct frame_info *fi;
1041
1042   /* sanity check: have we been passed a pointer? */
1043   if (changelist == NULL)
1044     return -1;
1045
1046   /*  Only root variables can be updated... */
1047   if (!is_root_p (*varp))
1048     /* Not a root var */
1049     return -1;
1050
1051   /* Save the selected stack frame, since we will need to change it
1052      in order to evaluate expressions. */
1053   old_fid = get_frame_id (deprecated_selected_frame);
1054
1055   /* Update the root variable. value_of_root can return NULL
1056      if the variable is no longer around, i.e. we stepped out of
1057      the frame in which a local existed. We are letting the 
1058      value_of_root variable dispose of the varobj if the type
1059      has changed. */
1060   type_changed = 1;
1061   new = value_of_root (varp, &type_changed);
1062
1063   /* Restore selected frame */
1064   fi = frame_find_by_id (old_fid);
1065   if (fi)
1066     select_frame (fi);
1067
1068   if (new == NULL)
1069     {
1070       (*varp)->error = 1;
1071       return -1;
1072     }
1073
1074   /* Initialize a stack for temporary results */
1075   vpush (&result, NULL);
1076
1077   /* If this is a "use_selected_frame" varobj, and its type has changed,
1078      them note that it's changed. */
1079   if (type_changed)
1080     {
1081       vpush (&result, *varp);
1082       changed++;
1083     }
1084
1085   if (install_new_value ((*varp), new, type_changed))
1086     {
1087       /* If type_changed is 1, install_new_value will never return
1088          non-zero, so we'll never report the same variable twice.  */
1089       gdb_assert (!type_changed);
1090       vpush (&result, (*varp));
1091       changed++;
1092     }
1093
1094   /* Initialize a stack */
1095   vpush (&stack, NULL);
1096
1097   /* Push the root's children */
1098   if ((*varp)->children != NULL)
1099     {
1100       struct varobj_child *c;
1101       for (c = (*varp)->children; c != NULL; c = c->next)
1102         vpush (&stack, c->child);
1103     }
1104
1105   /* Walk through the children, reconstructing them all. */
1106   v = vpop (&stack);
1107   while (v != NULL)
1108     {
1109       /* Push any children */
1110       if (v->children != NULL)
1111         {
1112           struct varobj_child *c;
1113           for (c = v->children; c != NULL; c = c->next)
1114             vpush (&stack, c->child);
1115         }
1116
1117       /* Update this variable */
1118       new = value_of_child (v->parent, v->index);
1119       if (install_new_value (v, new, 0 /* type not changed */))
1120         {
1121           /* Note that it's changed */
1122           vpush (&result, v);
1123           v->updated = 0;
1124           changed++;
1125         }
1126
1127       /* Get next child */
1128       v = vpop (&stack);
1129     }
1130
1131   /* Alloc (changed + 1) list entries */
1132   /* FIXME: add a cleanup for the allocated list(s)
1133      because one day the select_frame called below can longjump */
1134   *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1135   if (changed > 1)
1136     {
1137       templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1138       cv = templist;
1139     }
1140   else
1141     cv = *changelist;
1142
1143   /* Copy from result stack to list */
1144   vleft = changed;
1145   *cv = vpop (&result);
1146   while ((*cv != NULL) && (vleft > 0))
1147     {
1148       vleft--;
1149       cv++;
1150       *cv = vpop (&result);
1151     }
1152   if (vleft)
1153     warning (_("varobj_update: assertion failed - vleft <> 0"));
1154
1155   if (changed > 1)
1156     {
1157       /* Now we revert the order. */
1158       for (i = 0; i < changed; i++)
1159         *(*changelist + i) = *(templist + changed - 1 - i);
1160       *(*changelist + changed) = NULL;
1161     }
1162
1163   if (type_changed)
1164     return -2;
1165   else
1166     return changed;
1167 }
1168 \f
1169
1170 /* Helper functions */
1171
1172 /*
1173  * Variable object construction/destruction
1174  */
1175
1176 static int
1177 delete_variable (struct cpstack **resultp, struct varobj *var,
1178                  int only_children_p)
1179 {
1180   int delcount = 0;
1181
1182   delete_variable_1 (resultp, &delcount, var,
1183                      only_children_p, 1 /* remove_from_parent_p */ );
1184
1185   return delcount;
1186 }
1187
1188 /* Delete the variable object VAR and its children */
1189 /* IMPORTANT NOTE: If we delete a variable which is a child
1190    and the parent is not removed we dump core.  It must be always
1191    initially called with remove_from_parent_p set */
1192 static void
1193 delete_variable_1 (struct cpstack **resultp, int *delcountp,
1194                    struct varobj *var, int only_children_p,
1195                    int remove_from_parent_p)
1196 {
1197   struct varobj_child *vc;
1198   struct varobj_child *next;
1199
1200   /* Delete any children of this variable, too. */
1201   for (vc = var->children; vc != NULL; vc = next)
1202     {
1203       if (!remove_from_parent_p)
1204         vc->child->parent = NULL;
1205       delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
1206       next = vc->next;
1207       xfree (vc);
1208     }
1209
1210   /* if we were called to delete only the children we are done here */
1211   if (only_children_p)
1212     return;
1213
1214   /* Otherwise, add it to the list of deleted ones and proceed to do so */
1215   /* If the name is null, this is a temporary variable, that has not
1216      yet been installed, don't report it, it belongs to the caller... */
1217   if (var->obj_name != NULL)
1218     {
1219       cppush (resultp, xstrdup (var->obj_name));
1220       *delcountp = *delcountp + 1;
1221     }
1222
1223   /* If this variable has a parent, remove it from its parent's list */
1224   /* OPTIMIZATION: if the parent of this variable is also being deleted, 
1225      (as indicated by remove_from_parent_p) we don't bother doing an
1226      expensive list search to find the element to remove when we are
1227      discarding the list afterwards */
1228   if ((remove_from_parent_p) && (var->parent != NULL))
1229     {
1230       remove_child_from_parent (var->parent, var);
1231     }
1232
1233   if (var->obj_name != NULL)
1234     uninstall_variable (var);
1235
1236   /* Free memory associated with this variable */
1237   free_variable (var);
1238 }
1239
1240 /* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1241 static int
1242 install_variable (struct varobj *var)
1243 {
1244   struct vlist *cv;
1245   struct vlist *newvl;
1246   const char *chp;
1247   unsigned int index = 0;
1248   unsigned int i = 1;
1249
1250   for (chp = var->obj_name; *chp; chp++)
1251     {
1252       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1253     }
1254
1255   cv = *(varobj_table + index);
1256   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1257     cv = cv->next;
1258
1259   if (cv != NULL)
1260     error (_("Duplicate variable object name"));
1261
1262   /* Add varobj to hash table */
1263   newvl = xmalloc (sizeof (struct vlist));
1264   newvl->next = *(varobj_table + index);
1265   newvl->var = var;
1266   *(varobj_table + index) = newvl;
1267
1268   /* If root, add varobj to root list */
1269   if (is_root_p (var))
1270     {
1271       /* Add to list of root variables */
1272       if (rootlist == NULL)
1273         var->root->next = NULL;
1274       else
1275         var->root->next = rootlist;
1276       rootlist = var->root;
1277       rootcount++;
1278     }
1279
1280   return 1;                     /* OK */
1281 }
1282
1283 /* Unistall the object VAR. */
1284 static void
1285 uninstall_variable (struct varobj *var)
1286 {
1287   struct vlist *cv;
1288   struct vlist *prev;
1289   struct varobj_root *cr;
1290   struct varobj_root *prer;
1291   const char *chp;
1292   unsigned int index = 0;
1293   unsigned int i = 1;
1294
1295   /* Remove varobj from hash table */
1296   for (chp = var->obj_name; *chp; chp++)
1297     {
1298       index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1299     }
1300
1301   cv = *(varobj_table + index);
1302   prev = NULL;
1303   while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1304     {
1305       prev = cv;
1306       cv = cv->next;
1307     }
1308
1309   if (varobjdebug)
1310     fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1311
1312   if (cv == NULL)
1313     {
1314       warning
1315         ("Assertion failed: Could not find variable object \"%s\" to delete",
1316          var->obj_name);
1317       return;
1318     }
1319
1320   if (prev == NULL)
1321     *(varobj_table + index) = cv->next;
1322   else
1323     prev->next = cv->next;
1324
1325   xfree (cv);
1326
1327   /* If root, remove varobj from root list */
1328   if (is_root_p (var))
1329     {
1330       /* Remove from list of root variables */
1331       if (rootlist == var->root)
1332         rootlist = var->root->next;
1333       else
1334         {
1335           prer = NULL;
1336           cr = rootlist;
1337           while ((cr != NULL) && (cr->rootvar != var))
1338             {
1339               prer = cr;
1340               cr = cr->next;
1341             }
1342           if (cr == NULL)
1343             {
1344               warning
1345                 ("Assertion failed: Could not find varobj \"%s\" in root list",
1346                  var->obj_name);
1347               return;
1348             }
1349           if (prer == NULL)
1350             rootlist = NULL;
1351           else
1352             prer->next = cr->next;
1353         }
1354       rootcount--;
1355     }
1356
1357 }
1358
1359 /* Does a child with the name NAME exist in VAR? If so, return its data.
1360    If not, return NULL. */
1361 static struct varobj *
1362 child_exists (struct varobj *var, char *name)
1363 {
1364   struct varobj_child *vc;
1365
1366   for (vc = var->children; vc != NULL; vc = vc->next)
1367     {
1368       if (strcmp (vc->child->name, name) == 0)
1369         return vc->child;
1370     }
1371
1372   return NULL;
1373 }
1374
1375 /* Create and install a child of the parent of the given name */
1376 static struct varobj *
1377 create_child (struct varobj *parent, int index, char *name)
1378 {
1379   struct varobj *child;
1380   char *childs_name;
1381   struct value *value;
1382
1383   child = new_variable ();
1384
1385   /* name is allocated by name_of_child */
1386   child->name = name;
1387   child->index = index;
1388   value = value_of_child (parent, index);
1389   child->parent = parent;
1390   child->root = parent->root;
1391   childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
1392   child->obj_name = childs_name;
1393   install_variable (child);
1394
1395   /* Save a pointer to this child in the parent */
1396   save_child_in_parent (parent, child);
1397
1398   /* Compute the type of the child.  Must do this before
1399      calling install_new_value.  */
1400   if (value != NULL)
1401     /* If the child had no evaluation errors, var->value
1402        will be non-NULL and contain a valid type. */
1403     child->type = value_type (value);
1404   else
1405     /* Otherwise, we must compute the type. */
1406     child->type = (*child->root->lang->type_of_child) (child->parent, 
1407                                                        child->index);
1408   install_new_value (child, value, 1);
1409
1410   if ((!CPLUS_FAKE_CHILD (child) && child->value == NULL) || parent->error)
1411     child->error = 1;
1412
1413   return child;
1414 }
1415
1416 /* FIXME: This should be a generic add to list */
1417 /* Save CHILD in the PARENT's data. */
1418 static void
1419 save_child_in_parent (struct varobj *parent, struct varobj *child)
1420 {
1421   struct varobj_child *vc;
1422
1423   /* Insert the child at the top */
1424   vc = parent->children;
1425   parent->children =
1426     (struct varobj_child *) xmalloc (sizeof (struct varobj_child));
1427
1428   parent->children->next = vc;
1429   parent->children->child = child;
1430 }
1431
1432 /* FIXME: This should be a generic remove from list */
1433 /* Remove the CHILD from the PARENT's list of children. */
1434 static void
1435 remove_child_from_parent (struct varobj *parent, struct varobj *child)
1436 {
1437   struct varobj_child *vc, *prev;
1438
1439   /* Find the child in the parent's list */
1440   prev = NULL;
1441   for (vc = parent->children; vc != NULL;)
1442     {
1443       if (vc->child == child)
1444         break;
1445       prev = vc;
1446       vc = vc->next;
1447     }
1448
1449   if (prev == NULL)
1450     parent->children = vc->next;
1451   else
1452     prev->next = vc->next;
1453
1454 }
1455 \f
1456
1457 /*
1458  * Miscellaneous utility functions.
1459  */
1460
1461 /* Allocate memory and initialize a new variable */
1462 static struct varobj *
1463 new_variable (void)
1464 {
1465   struct varobj *var;
1466
1467   var = (struct varobj *) xmalloc (sizeof (struct varobj));
1468   var->name = NULL;
1469   var->obj_name = NULL;
1470   var->index = -1;
1471   var->type = NULL;
1472   var->value = NULL;
1473   var->error = 0;
1474   var->num_children = -1;
1475   var->parent = NULL;
1476   var->children = NULL;
1477   var->format = 0;
1478   var->root = NULL;
1479   var->updated = 0;
1480
1481   return var;
1482 }
1483
1484 /* Allocate memory and initialize a new root variable */
1485 static struct varobj *
1486 new_root_variable (void)
1487 {
1488   struct varobj *var = new_variable ();
1489   var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1490   var->root->lang = NULL;
1491   var->root->exp = NULL;
1492   var->root->valid_block = NULL;
1493   var->root->frame = null_frame_id;
1494   var->root->use_selected_frame = 0;
1495   var->root->rootvar = NULL;
1496
1497   return var;
1498 }
1499
1500 /* Free any allocated memory associated with VAR. */
1501 static void
1502 free_variable (struct varobj *var)
1503 {
1504   /* Free the expression if this is a root variable. */
1505   if (is_root_p (var))
1506     {
1507       free_current_contents (&var->root->exp);
1508       xfree (var->root);
1509     }
1510
1511   xfree (var->name);
1512   xfree (var->obj_name);
1513   xfree (var);
1514 }
1515
1516 static void
1517 do_free_variable_cleanup (void *var)
1518 {
1519   free_variable (var);
1520 }
1521
1522 static struct cleanup *
1523 make_cleanup_free_variable (struct varobj *var)
1524 {
1525   return make_cleanup (do_free_variable_cleanup, var);
1526 }
1527
1528 /* This returns the type of the variable. It also skips past typedefs
1529    to return the real type of the variable.
1530
1531    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1532    except within get_target_type and get_type. */
1533 static struct type *
1534 get_type (struct varobj *var)
1535 {
1536   struct type *type;
1537   type = var->type;
1538
1539   if (type != NULL)
1540     type = check_typedef (type);
1541
1542   return type;
1543 }
1544
1545 /* This returns the type of the variable, dereferencing pointers, too. */
1546 static struct type *
1547 get_type_deref (struct varobj *var)
1548 {
1549   struct type *type;
1550
1551   type = get_type (var);
1552
1553   if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR
1554                        || TYPE_CODE (type) == TYPE_CODE_REF))
1555     type = get_target_type (type);
1556
1557   return type;
1558 }
1559
1560 /* This returns the target type (or NULL) of TYPE, also skipping
1561    past typedefs, just like get_type ().
1562
1563    NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1564    except within get_target_type and get_type. */
1565 static struct type *
1566 get_target_type (struct type *type)
1567 {
1568   if (type != NULL)
1569     {
1570       type = TYPE_TARGET_TYPE (type);
1571       if (type != NULL)
1572         type = check_typedef (type);
1573     }
1574
1575   return type;
1576 }
1577
1578 /* What is the default display for this variable? We assume that
1579    everything is "natural". Any exceptions? */
1580 static enum varobj_display_formats
1581 variable_default_display (struct varobj *var)
1582 {
1583   return FORMAT_NATURAL;
1584 }
1585
1586 /* FIXME: The following should be generic for any pointer */
1587 static void
1588 vpush (struct vstack **pstack, struct varobj *var)
1589 {
1590   struct vstack *s;
1591
1592   s = (struct vstack *) xmalloc (sizeof (struct vstack));
1593   s->var = var;
1594   s->next = *pstack;
1595   *pstack = s;
1596 }
1597
1598 /* FIXME: The following should be generic for any pointer */
1599 static struct varobj *
1600 vpop (struct vstack **pstack)
1601 {
1602   struct vstack *s;
1603   struct varobj *v;
1604
1605   if ((*pstack)->var == NULL && (*pstack)->next == NULL)
1606     return NULL;
1607
1608   s = *pstack;
1609   v = s->var;
1610   *pstack = (*pstack)->next;
1611   xfree (s);
1612
1613   return v;
1614 }
1615
1616 /* FIXME: The following should be generic for any pointer */
1617 static void
1618 cppush (struct cpstack **pstack, char *name)
1619 {
1620   struct cpstack *s;
1621
1622   s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1623   s->name = name;
1624   s->next = *pstack;
1625   *pstack = s;
1626 }
1627
1628 /* FIXME: The following should be generic for any pointer */
1629 static char *
1630 cppop (struct cpstack **pstack)
1631 {
1632   struct cpstack *s;
1633   char *v;
1634
1635   if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1636     return NULL;
1637
1638   s = *pstack;
1639   v = s->name;
1640   *pstack = (*pstack)->next;
1641   xfree (s);
1642
1643   return v;
1644 }
1645 \f
1646 /*
1647  * Language-dependencies
1648  */
1649
1650 /* Common entry points */
1651
1652 /* Get the language of variable VAR. */
1653 static enum varobj_languages
1654 variable_language (struct varobj *var)
1655 {
1656   enum varobj_languages lang;
1657
1658   switch (var->root->exp->language_defn->la_language)
1659     {
1660     default:
1661     case language_c:
1662       lang = vlang_c;
1663       break;
1664     case language_cplus:
1665       lang = vlang_cplus;
1666       break;
1667     case language_java:
1668       lang = vlang_java;
1669       break;
1670     }
1671
1672   return lang;
1673 }
1674
1675 /* Return the number of children for a given variable.
1676    The result of this function is defined by the language
1677    implementation. The number of children returned by this function
1678    is the number of children that the user will see in the variable
1679    display. */
1680 static int
1681 number_of_children (struct varobj *var)
1682 {
1683   return (*var->root->lang->number_of_children) (var);;
1684 }
1685
1686 /* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1687 static char *
1688 name_of_variable (struct varobj *var)
1689 {
1690   return (*var->root->lang->name_of_variable) (var);
1691 }
1692
1693 /* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1694 static char *
1695 name_of_child (struct varobj *var, int index)
1696 {
1697   return (*var->root->lang->name_of_child) (var, index);
1698 }
1699
1700 /* What is the ``struct value *'' of the root variable VAR? 
1701    TYPE_CHANGED controls what to do if the type of a
1702    use_selected_frame = 1 variable changes.  On input,
1703    TYPE_CHANGED = 1 means discard the old varobj, and replace
1704    it with this one.  TYPE_CHANGED = 0 means leave it around.
1705    NB: In both cases, var_handle will point to the new varobj,
1706    so if you use TYPE_CHANGED = 0, you will have to stash the
1707    old varobj pointer away somewhere before calling this.
1708    On return, TYPE_CHANGED will be 1 if the type has changed, and 
1709    0 otherwise. */
1710 static struct value *
1711 value_of_root (struct varobj **var_handle, int *type_changed)
1712 {
1713   struct varobj *var;
1714
1715   if (var_handle == NULL)
1716     return NULL;
1717
1718   var = *var_handle;
1719
1720   /* This should really be an exception, since this should
1721      only get called with a root variable. */
1722
1723   if (!is_root_p (var))
1724     return NULL;
1725
1726   if (var->root->use_selected_frame)
1727     {
1728       struct varobj *tmp_var;
1729       char *old_type, *new_type;
1730       old_type = varobj_get_type (var);
1731       tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1732                                USE_SELECTED_FRAME);
1733       if (tmp_var == NULL)
1734         {
1735           return NULL;
1736         }
1737       new_type = varobj_get_type (tmp_var);
1738       if (strcmp (old_type, new_type) == 0)
1739         {
1740           varobj_delete (tmp_var, NULL, 0);
1741           *type_changed = 0;
1742         }
1743       else
1744         {
1745           if (*type_changed)
1746             {
1747               tmp_var->obj_name =
1748                 savestring (var->obj_name, strlen (var->obj_name));
1749               varobj_delete (var, NULL, 0);
1750             }
1751           else
1752             {
1753               tmp_var->obj_name = varobj_gen_name ();
1754             }
1755           install_variable (tmp_var);
1756           *var_handle = tmp_var;
1757           var = *var_handle;
1758           *type_changed = 1;
1759         }
1760     }
1761   else
1762     {
1763       *type_changed = 0;
1764     }
1765
1766   return (*var->root->lang->value_of_root) (var_handle);
1767 }
1768
1769 /* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1770 static struct value *
1771 value_of_child (struct varobj *parent, int index)
1772 {
1773   struct value *value;
1774
1775   value = (*parent->root->lang->value_of_child) (parent, index);
1776
1777   return value;
1778 }
1779
1780 /* Is this variable editable? Use the variable's type to make
1781    this determination. */
1782 static int
1783 variable_editable (struct varobj *var)
1784 {
1785   return (*var->root->lang->variable_editable) (var);
1786 }
1787
1788 /* GDB already has a command called "value_of_variable". Sigh. */
1789 static char *
1790 my_value_of_variable (struct varobj *var)
1791 {
1792   return (*var->root->lang->value_of_variable) (var);
1793 }
1794
1795 /* Return non-zero if changes in value of VAR
1796    must be detected and reported by -var-update.
1797    Return zero is -var-update should never report
1798    changes of such values.  This makes sense for structures
1799    (since the changes in children values will be reported separately),
1800    or for artifical objects (like 'public' pseudo-field in C++).
1801
1802    Return value of 0 means that gdb need not call value_fetch_lazy
1803    for the value of this variable object.  */
1804 static int
1805 varobj_value_is_changeable_p (struct varobj *var)
1806 {
1807   int r;
1808   struct type *type;
1809
1810   if (CPLUS_FAKE_CHILD (var))
1811     return 0;
1812
1813   type = get_type (var);
1814
1815   switch (TYPE_CODE (type))
1816     {
1817     case TYPE_CODE_STRUCT:
1818     case TYPE_CODE_UNION:
1819     case TYPE_CODE_ARRAY:
1820       r = 0;
1821       break;
1822
1823     default:
1824       r = 1;
1825     }
1826
1827   return r;
1828 }
1829
1830 /* C */
1831 static int
1832 c_number_of_children (struct varobj *var)
1833 {
1834   struct type *type;
1835   struct type *target;
1836   int children;
1837
1838   type = get_type (var);
1839   target = get_target_type (type);
1840   children = 0;
1841
1842   switch (TYPE_CODE (type))
1843     {
1844     case TYPE_CODE_ARRAY:
1845       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
1846           && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
1847         children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1848       else
1849         children = -1;
1850       break;
1851
1852     case TYPE_CODE_STRUCT:
1853     case TYPE_CODE_UNION:
1854       children = TYPE_NFIELDS (type);
1855       break;
1856
1857     case TYPE_CODE_PTR:
1858       /* This is where things get compilcated. All pointers have one child.
1859          Except, of course, for struct and union ptr, which we automagically
1860          dereference for the user and function ptrs, which have no children.
1861          We also don't dereference void* as we don't know what to show.
1862          We can show char* so we allow it to be dereferenced.  If you decide
1863          to test for it, please mind that a little magic is necessary to
1864          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and 
1865          TYPE_NAME == "char" */
1866
1867       switch (TYPE_CODE (target))
1868         {
1869         case TYPE_CODE_STRUCT:
1870         case TYPE_CODE_UNION:
1871           children = TYPE_NFIELDS (target);
1872           break;
1873
1874         case TYPE_CODE_FUNC:
1875         case TYPE_CODE_VOID:
1876           children = 0;
1877           break;
1878
1879         default:
1880           children = 1;
1881         }
1882       break;
1883
1884     default:
1885       /* Other types have no children */
1886       break;
1887     }
1888
1889   return children;
1890 }
1891
1892 static char *
1893 c_name_of_variable (struct varobj *parent)
1894 {
1895   return savestring (parent->name, strlen (parent->name));
1896 }
1897
1898 static char *
1899 c_name_of_child (struct varobj *parent, int index)
1900 {
1901   struct type *type;
1902   struct type *target;
1903   char *name;
1904   char *string;
1905
1906   type = get_type (parent);
1907   target = get_target_type (type);
1908
1909   switch (TYPE_CODE (type))
1910     {
1911     case TYPE_CODE_ARRAY:
1912       name = xstrprintf ("%d", index
1913                          + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
1914       break;
1915
1916     case TYPE_CODE_STRUCT:
1917     case TYPE_CODE_UNION:
1918       string = TYPE_FIELD_NAME (type, index);
1919       name = savestring (string, strlen (string));
1920       break;
1921
1922     case TYPE_CODE_PTR:
1923       switch (TYPE_CODE (target))
1924         {
1925         case TYPE_CODE_STRUCT:
1926         case TYPE_CODE_UNION:
1927           string = TYPE_FIELD_NAME (target, index);
1928           name = savestring (string, strlen (string));
1929           break;
1930
1931         default:
1932           name = xstrprintf ("*%s", parent->name);
1933           break;
1934         }
1935       break;
1936
1937     default:
1938       /* This should not happen */
1939       name = xstrdup ("???");
1940     }
1941
1942   return name;
1943 }
1944
1945 static struct value *
1946 c_value_of_root (struct varobj **var_handle)
1947 {
1948   struct value *new_val;
1949   struct varobj *var = *var_handle;
1950   struct frame_info *fi;
1951   int within_scope;
1952
1953   /*  Only root variables can be updated... */
1954   if (!is_root_p (var))
1955     /* Not a root var */
1956     return NULL;
1957
1958
1959   /* Determine whether the variable is still around. */
1960   if (var->root->valid_block == NULL)
1961     within_scope = 1;
1962   else
1963     {
1964       reinit_frame_cache ();
1965       fi = frame_find_by_id (var->root->frame);
1966       within_scope = fi != NULL;
1967       /* FIXME: select_frame could fail */
1968       if (fi)
1969         {
1970           CORE_ADDR pc = get_frame_pc (fi);
1971           if (pc <  BLOCK_START (var->root->valid_block) ||
1972               pc >= BLOCK_END (var->root->valid_block))
1973             within_scope = 0;
1974           select_frame (fi);
1975         }         
1976     }
1977
1978   if (within_scope)
1979     {
1980       /* We need to catch errors here, because if evaluate
1981          expression fails we just want to make val->error = 1 and
1982          go on */
1983       if (gdb_evaluate_expression (var->root->exp, &new_val))
1984         {
1985           var->error = 0;
1986           release_value (new_val);
1987         }
1988       else
1989         var->error = 1;
1990
1991       return new_val;
1992     }
1993
1994   return NULL;
1995 }
1996
1997 static struct value *
1998 c_value_of_child (struct varobj *parent, int index)
1999 {
2000   struct value *value;
2001   struct value *temp;
2002   struct value *indval;
2003   struct type *type, *target;
2004   char *name;
2005   int real_index;
2006
2007   type = get_type (parent);
2008   target = get_target_type (type);
2009   name = name_of_child (parent, index);
2010   temp = parent->value;
2011   value = NULL;
2012
2013   if (temp != NULL)
2014     {
2015       switch (TYPE_CODE (type))
2016         {
2017         case TYPE_CODE_ARRAY:
2018           real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
2019 #if 0
2020           /* This breaks if the array lives in a (vector) register. */
2021           value = value_slice (temp, real_index, 1);
2022           temp = value_coerce_array (value);
2023           gdb_value_ind (temp, &value);
2024 #else
2025           indval = value_from_longest (builtin_type_int, (LONGEST) real_index);
2026           gdb_value_subscript (temp, indval, &value);
2027 #endif
2028           break;
2029
2030         case TYPE_CODE_STRUCT:
2031         case TYPE_CODE_UNION:
2032           gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2033                                 "vstructure");
2034           break;
2035
2036         case TYPE_CODE_PTR:
2037           switch (TYPE_CODE (target))
2038             {
2039             case TYPE_CODE_STRUCT:
2040             case TYPE_CODE_UNION:
2041               gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2042                                     "vstructure");
2043               break;
2044
2045             default:
2046               gdb_value_ind (temp, &value);
2047               break;
2048             }
2049           break;
2050
2051         default:
2052           break;
2053         }
2054     }
2055
2056   if (value != NULL)
2057     release_value (value);
2058
2059   xfree (name);
2060   return value;
2061 }
2062
2063 static struct type *
2064 c_type_of_child (struct varobj *parent, int index)
2065 {
2066   struct type *type;
2067   char *name = name_of_child (parent, index);
2068
2069   switch (TYPE_CODE (parent->type))
2070     {
2071     case TYPE_CODE_ARRAY:
2072       type = get_target_type (parent->type);
2073       break;
2074
2075     case TYPE_CODE_STRUCT:
2076     case TYPE_CODE_UNION:
2077       type = lookup_struct_elt_type (parent->type, name, 0);
2078       break;
2079
2080     case TYPE_CODE_PTR:
2081       switch (TYPE_CODE (get_target_type (parent->type)))
2082         {
2083         case TYPE_CODE_STRUCT:
2084         case TYPE_CODE_UNION:
2085           type = lookup_struct_elt_type (parent->type, name, 0);
2086           break;
2087
2088         default:
2089           type = get_target_type (parent->type);
2090           break;
2091         }
2092       break;
2093
2094     default:
2095       /* This should not happen as only the above types have children */
2096       warning (_("Child of parent whose type does not allow children"));
2097       /* FIXME: Can we still go on? */
2098       type = NULL;
2099       break;
2100     }
2101
2102   xfree (name);
2103   return type;
2104 }
2105
2106 static int
2107 c_variable_editable (struct varobj *var)
2108 {
2109   switch (TYPE_CODE (get_type (var)))
2110     {
2111     case TYPE_CODE_STRUCT:
2112     case TYPE_CODE_UNION:
2113     case TYPE_CODE_ARRAY:
2114     case TYPE_CODE_FUNC:
2115     case TYPE_CODE_METHOD:
2116       return 0;
2117       break;
2118
2119     default:
2120       return 1;
2121       break;
2122     }
2123 }
2124
2125 static char *
2126 c_value_of_variable (struct varobj *var)
2127 {
2128   /* BOGUS: if val_print sees a struct/class, or a reference to one,
2129      it will print out its children instead of "{...}".  So we need to
2130      catch that case explicitly.  */
2131   struct type *type = get_type (var);
2132
2133   /* Strip top-level references. */
2134   while (TYPE_CODE (type) == TYPE_CODE_REF)
2135     type = check_typedef (TYPE_TARGET_TYPE (type));
2136
2137   switch (TYPE_CODE (type))
2138     {
2139     case TYPE_CODE_STRUCT:
2140     case TYPE_CODE_UNION:
2141       return xstrdup ("{...}");
2142       /* break; */
2143
2144     case TYPE_CODE_ARRAY:
2145       {
2146         char *number;
2147         number = xstrprintf ("[%d]", var->num_children);
2148         return (number);
2149       }
2150       /* break; */
2151
2152     default:
2153       {
2154         if (var->value == NULL)
2155           {
2156             /* This can happen if we attempt to get the value of a struct
2157                member when the parent is an invalid pointer. This is an
2158                error condition, so we should tell the caller. */
2159             return NULL;
2160           }
2161         else
2162           {
2163             long dummy;
2164             struct ui_file *stb = mem_fileopen ();
2165             struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
2166             char *thevalue;
2167
2168             gdb_assert (varobj_value_is_changeable_p (var));
2169             gdb_assert (!value_lazy (var->value));
2170             common_val_print (var->value, stb,
2171                               format_code[(int) var->format], 1, 0, 0);
2172             thevalue = ui_file_xstrdup (stb, &dummy);
2173             do_cleanups (old_chain);
2174         return thevalue;
2175       }
2176       }
2177     }
2178 }
2179 \f
2180
2181 /* C++ */
2182
2183 static int
2184 cplus_number_of_children (struct varobj *var)
2185 {
2186   struct type *type;
2187   int children, dont_know;
2188
2189   dont_know = 1;
2190   children = 0;
2191
2192   if (!CPLUS_FAKE_CHILD (var))
2193     {
2194       type = get_type_deref (var);
2195
2196       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2197           ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2198         {
2199           int kids[3];
2200
2201           cplus_class_num_children (type, kids);
2202           if (kids[v_public] != 0)
2203             children++;
2204           if (kids[v_private] != 0)
2205             children++;
2206           if (kids[v_protected] != 0)
2207             children++;
2208
2209           /* Add any baseclasses */
2210           children += TYPE_N_BASECLASSES (type);
2211           dont_know = 0;
2212
2213           /* FIXME: save children in var */
2214         }
2215     }
2216   else
2217     {
2218       int kids[3];
2219
2220       type = get_type_deref (var->parent);
2221
2222       cplus_class_num_children (type, kids);
2223       if (strcmp (var->name, "public") == 0)
2224         children = kids[v_public];
2225       else if (strcmp (var->name, "private") == 0)
2226         children = kids[v_private];
2227       else
2228         children = kids[v_protected];
2229       dont_know = 0;
2230     }
2231
2232   if (dont_know)
2233     children = c_number_of_children (var);
2234
2235   return children;
2236 }
2237
2238 /* Compute # of public, private, and protected variables in this class.
2239    That means we need to descend into all baseclasses and find out
2240    how many are there, too. */
2241 static void
2242 cplus_class_num_children (struct type *type, int children[3])
2243 {
2244   int i;
2245
2246   children[v_public] = 0;
2247   children[v_private] = 0;
2248   children[v_protected] = 0;
2249
2250   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2251     {
2252       /* If we have a virtual table pointer, omit it. */
2253       if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
2254         continue;
2255
2256       if (TYPE_FIELD_PROTECTED (type, i))
2257         children[v_protected]++;
2258       else if (TYPE_FIELD_PRIVATE (type, i))
2259         children[v_private]++;
2260       else
2261         children[v_public]++;
2262     }
2263 }
2264
2265 static char *
2266 cplus_name_of_variable (struct varobj *parent)
2267 {
2268   return c_name_of_variable (parent);
2269 }
2270
2271 static char *
2272 cplus_name_of_child (struct varobj *parent, int index)
2273 {
2274   char *name;
2275   struct type *type;
2276
2277   if (CPLUS_FAKE_CHILD (parent))
2278     {
2279       /* Looking for children of public, private, or protected. */
2280       type = get_type_deref (parent->parent);
2281     }
2282   else
2283     type = get_type_deref (parent);
2284
2285   name = NULL;
2286   switch (TYPE_CODE (type))
2287     {
2288     case TYPE_CODE_STRUCT:
2289     case TYPE_CODE_UNION:
2290       if (CPLUS_FAKE_CHILD (parent))
2291         {
2292           /* The fields of the class type are ordered as they
2293              appear in the class.  We are given an index for a
2294              particular access control type ("public","protected",
2295              or "private").  We must skip over fields that don't
2296              have the access control we are looking for to properly
2297              find the indexed field. */
2298           int type_index = TYPE_N_BASECLASSES (type);
2299           if (strcmp (parent->name, "private") == 0)
2300             {
2301               while (index >= 0)
2302                 {
2303                   if (TYPE_VPTR_BASETYPE (type) == type
2304                       && type_index == TYPE_VPTR_FIELDNO (type))
2305                     ; /* ignore vptr */
2306                   else if (TYPE_FIELD_PRIVATE (type, type_index))
2307                     --index;
2308                   ++type_index;
2309                 }
2310               --type_index;
2311             }
2312           else if (strcmp (parent->name, "protected") == 0)
2313             {
2314               while (index >= 0)
2315                 {
2316                   if (TYPE_VPTR_BASETYPE (type) == type
2317                       && type_index == TYPE_VPTR_FIELDNO (type))
2318                     ; /* ignore vptr */
2319                   else if (TYPE_FIELD_PROTECTED (type, type_index))
2320                     --index;
2321                   ++type_index;
2322                 }
2323               --type_index;
2324             }
2325           else
2326             {
2327               while (index >= 0)
2328                 {
2329                   if (TYPE_VPTR_BASETYPE (type) == type
2330                       && type_index == TYPE_VPTR_FIELDNO (type))
2331                     ; /* ignore vptr */
2332                   else if (!TYPE_FIELD_PRIVATE (type, type_index) &&
2333                       !TYPE_FIELD_PROTECTED (type, type_index))
2334                     --index;
2335                   ++type_index;
2336                 }
2337               --type_index;
2338             }
2339
2340           name = TYPE_FIELD_NAME (type, type_index);
2341         }
2342       else if (index < TYPE_N_BASECLASSES (type))
2343         /* We are looking up the name of a base class */
2344         name = TYPE_FIELD_NAME (type, index);
2345       else
2346         {
2347           int children[3];
2348           cplus_class_num_children(type, children);
2349
2350           /* Everything beyond the baseclasses can
2351              only be "public", "private", or "protected"
2352
2353              The special "fake" children are always output by varobj in
2354              this order. So if INDEX == 2, it MUST be "protected". */
2355           index -= TYPE_N_BASECLASSES (type);
2356           switch (index)
2357             {
2358             case 0:
2359               if (children[v_public] > 0)
2360                 name = "public";
2361               else if (children[v_private] > 0)
2362                 name = "private";
2363               else 
2364                 name = "protected";
2365               break;
2366             case 1:
2367               if (children[v_public] > 0)
2368                 {
2369                   if (children[v_private] > 0)
2370                     name = "private";
2371                   else
2372                     name = "protected";
2373                 }
2374               else if (children[v_private] > 0)
2375                 name = "protected";
2376               break;
2377             case 2:
2378               /* Must be protected */
2379               name = "protected";
2380               break;
2381             default:
2382               /* error! */
2383               break;
2384             }
2385         }
2386       break;
2387
2388     default:
2389       break;
2390     }
2391
2392   if (name == NULL)
2393     return c_name_of_child (parent, index);
2394   else
2395     {
2396       if (name != NULL)
2397         name = savestring (name, strlen (name));
2398     }
2399
2400   return name;
2401 }
2402
2403 static struct value *
2404 cplus_value_of_root (struct varobj **var_handle)
2405 {
2406   return c_value_of_root (var_handle);
2407 }
2408
2409 static struct value *
2410 cplus_value_of_child (struct varobj *parent, int index)
2411 {
2412   struct type *type;
2413   struct value *value;
2414
2415   if (CPLUS_FAKE_CHILD (parent))
2416     type = get_type_deref (parent->parent);
2417   else
2418     type = get_type_deref (parent);
2419
2420   value = NULL;
2421
2422   if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2423       ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2424     {
2425       if (CPLUS_FAKE_CHILD (parent))
2426         {
2427           char *name;
2428           struct value *temp = parent->parent->value;
2429
2430           if (temp == NULL)
2431             return NULL;
2432
2433           name = name_of_child (parent, index);
2434           gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2435                                 "cplus_structure");
2436           if (value != NULL)
2437             release_value (value);
2438
2439           xfree (name);
2440         }
2441       else if (index >= TYPE_N_BASECLASSES (type))
2442         {
2443           /* public, private, or protected */
2444           return NULL;
2445         }
2446       else
2447         {
2448           /* Baseclass */
2449           if (parent->value != NULL)
2450             {
2451               struct value *temp = NULL;
2452
2453               /* No special processing for references is needed --
2454                  value_cast below handles references.  */
2455               if (TYPE_CODE (value_type (parent->value)) == TYPE_CODE_PTR)
2456                 {
2457                   if (!gdb_value_ind (parent->value, &temp))
2458                     return NULL;
2459                 }
2460               else
2461                 temp = parent->value;
2462
2463               if (temp != NULL)
2464                 {
2465                   value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
2466                   release_value (value);
2467                 }
2468               else
2469                 {
2470                   /* We failed to evaluate the parent's value, so don't even
2471                      bother trying to evaluate this child. */
2472                   return NULL;
2473                 }
2474             }
2475         }
2476     }
2477
2478   if (value == NULL)
2479     return c_value_of_child (parent, index);
2480
2481   return value;
2482 }
2483
2484 static struct type *
2485 cplus_type_of_child (struct varobj *parent, int index)
2486 {
2487   struct type *type, *t;
2488
2489   if (CPLUS_FAKE_CHILD (parent))
2490     {
2491       /* Looking for the type of a child of public, private, or protected. */
2492       t = get_type_deref (parent->parent);
2493     }
2494   else
2495     t = get_type_deref (parent);
2496
2497   type = NULL;
2498   switch (TYPE_CODE (t))
2499     {
2500     case TYPE_CODE_STRUCT:
2501     case TYPE_CODE_UNION:
2502       if (CPLUS_FAKE_CHILD (parent))
2503         {
2504           char *name = cplus_name_of_child (parent, index);
2505           type = lookup_struct_elt_type (t, name, 0);
2506           xfree (name);
2507         }
2508       else if (index < TYPE_N_BASECLASSES (t))
2509         type = TYPE_FIELD_TYPE (t, index);
2510       else
2511         {
2512           /* special */
2513           return NULL;
2514         }
2515       break;
2516
2517     default:
2518       break;
2519     }
2520
2521   if (type == NULL)
2522     return c_type_of_child (parent, index);
2523
2524   return type;
2525 }
2526
2527 static int
2528 cplus_variable_editable (struct varobj *var)
2529 {
2530   if (CPLUS_FAKE_CHILD (var))
2531     return 0;
2532
2533   return c_variable_editable (var);
2534 }
2535
2536 static char *
2537 cplus_value_of_variable (struct varobj *var)
2538 {
2539
2540   /* If we have one of our special types, don't print out
2541      any value. */
2542   if (CPLUS_FAKE_CHILD (var))
2543     return xstrdup ("");
2544
2545   return c_value_of_variable (var);
2546 }
2547 \f
2548 /* Java */
2549
2550 static int
2551 java_number_of_children (struct varobj *var)
2552 {
2553   return cplus_number_of_children (var);
2554 }
2555
2556 static char *
2557 java_name_of_variable (struct varobj *parent)
2558 {
2559   char *p, *name;
2560
2561   name = cplus_name_of_variable (parent);
2562   /* If  the name has "-" in it, it is because we
2563      needed to escape periods in the name... */
2564   p = name;
2565
2566   while (*p != '\000')
2567     {
2568       if (*p == '-')
2569         *p = '.';
2570       p++;
2571     }
2572
2573   return name;
2574 }
2575
2576 static char *
2577 java_name_of_child (struct varobj *parent, int index)
2578 {
2579   char *name, *p;
2580
2581   name = cplus_name_of_child (parent, index);
2582   /* Escape any periods in the name... */
2583   p = name;
2584
2585   while (*p != '\000')
2586     {
2587       if (*p == '.')
2588         *p = '-';
2589       p++;
2590     }
2591
2592   return name;
2593 }
2594
2595 static struct value *
2596 java_value_of_root (struct varobj **var_handle)
2597 {
2598   return cplus_value_of_root (var_handle);
2599 }
2600
2601 static struct value *
2602 java_value_of_child (struct varobj *parent, int index)
2603 {
2604   return cplus_value_of_child (parent, index);
2605 }
2606
2607 static struct type *
2608 java_type_of_child (struct varobj *parent, int index)
2609 {
2610   return cplus_type_of_child (parent, index);
2611 }
2612
2613 static int
2614 java_variable_editable (struct varobj *var)
2615 {
2616   return cplus_variable_editable (var);
2617 }
2618
2619 static char *
2620 java_value_of_variable (struct varobj *var)
2621 {
2622   return cplus_value_of_variable (var);
2623 }
2624 \f
2625 extern void _initialize_varobj (void);
2626 void
2627 _initialize_varobj (void)
2628 {
2629   int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2630
2631   varobj_table = xmalloc (sizeof_table);
2632   memset (varobj_table, 0, sizeof_table);
2633
2634   add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2635                             &varobjdebug, _("\
2636 Set varobj debugging."), _("\
2637 Show varobj debugging."), _("\
2638 When non-zero, varobj debugging is enabled."),
2639                             NULL,
2640                             show_varobjdebug,
2641                             &setlist, &showlist);
2642 }