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