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