OSDN Git Service

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