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