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