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