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