OSDN Git Service

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