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