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