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