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