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