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