Simplify usage of mi_continue_to.
[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"
d2353924 29#include "block.h"
a6c442d8
MK
30
31#include "gdb_assert.h"
b66d6d2e 32#include "gdb_string.h"
8b93c638
JM
33
34#include "varobj.h"
35
36/* Non-zero if we want to see trace of varobj level stuff. */
37
38int varobjdebug = 0;
920d2a44
AC
39static void
40show_varobjdebug (struct ui_file *file, int from_tty,
41 struct cmd_list_element *c, const char *value)
42{
43 fprintf_filtered (file, _("Varobj debugging is %s.\n"), value);
44}
8b93c638
JM
45
46/* String representations of gdb's format codes */
47char *varobj_format_string[] =
72330bd6 48 { "natural", "binary", "decimal", "hexadecimal", "octal" };
8b93c638
JM
49
50/* String representations of gdb's known languages */
72330bd6 51char *varobj_language_string[] = { "unknown", "C", "C++", "Java" };
8b93c638
JM
52
53/* Data structures */
54
55/* Every root variable has one of these structures saved in its
56 varobj. Members which must be free'd are noted. */
57struct varobj_root
72330bd6 58{
8b93c638 59
72330bd6
AC
60 /* Alloc'd expression for this parent. */
61 struct expression *exp;
8b93c638 62
72330bd6
AC
63 /* Block for which this expression is valid */
64 struct block *valid_block;
8b93c638 65
72330bd6 66 /* The frame for this expression */
e64d9b3d 67 struct frame_id frame;
8b93c638 68
72330bd6
AC
69 /* If 1, "update" always recomputes the frame & valid block
70 using the currently selected frame. */
71 int use_selected_frame;
73a93a32 72
72330bd6
AC
73 /* Language info for this variable and its children */
74 struct language_specific *lang;
8b93c638 75
72330bd6
AC
76 /* The varobj for this root node. */
77 struct varobj *rootvar;
8b93c638 78
72330bd6
AC
79 /* Next root variable */
80 struct varobj_root *next;
81};
8b93c638
JM
82
83/* Every variable in the system has a structure of this type defined
84 for it. This structure holds all information necessary to manipulate
85 a particular object variable. Members which must be freed are noted. */
86struct varobj
72330bd6 87{
8b93c638 88
72330bd6
AC
89 /* Alloc'd name of the variable for this object.. If this variable is a
90 child, then this name will be the child's source name.
91 (bar, not foo.bar) */
92 /* NOTE: This is the "expression" */
93 char *name;
8b93c638 94
72330bd6
AC
95 /* The alloc'd name for this variable's object. This is here for
96 convenience when constructing this object's children. */
97 char *obj_name;
8b93c638 98
72330bd6
AC
99 /* Index of this variable in its parent or -1 */
100 int index;
8b93c638 101
72330bd6
AC
102 /* The type of this variable. This may NEVER be NULL. */
103 struct type *type;
8b93c638 104
acd65feb
VP
105 /* The value of this expression or subexpression. This may be NULL.
106 Invariant: if type_changeable (this) is non-zero, the value is either
107 NULL, or not lazy. */
30b28db1 108 struct value *value;
8b93c638 109
72330bd6
AC
110 /* Did an error occur evaluating the expression or getting its value? */
111 int error;
8b93c638 112
72330bd6
AC
113 /* The number of (immediate) children this variable has */
114 int num_children;
8b93c638 115
72330bd6
AC
116 /* If this object is a child, this points to its immediate parent. */
117 struct varobj *parent;
8b93c638 118
72330bd6
AC
119 /* A list of this object's children */
120 struct varobj_child *children;
8b93c638 121
72330bd6
AC
122 /* Description of the root variable. Points to root variable for children. */
123 struct varobj_root *root;
8b93c638 124
72330bd6
AC
125 /* The format of the output for this object */
126 enum varobj_display_formats format;
fb9b6b35
JJ
127
128 /* Was this variable updated via a varobj_set_value operation */
129 int updated;
72330bd6 130};
8b93c638
JM
131
132/* Every variable keeps a linked list of its children, described
133 by the following structure. */
134/* FIXME: Deprecated. All should use vlist instead */
135
136struct varobj_child
72330bd6 137{
8b93c638 138
72330bd6
AC
139 /* Pointer to the child's data */
140 struct varobj *child;
8b93c638 141
72330bd6
AC
142 /* Pointer to the next child */
143 struct varobj_child *next;
144};
8b93c638
JM
145
146/* A stack of varobjs */
147/* FIXME: Deprecated. All should use vlist instead */
148
149struct vstack
72330bd6
AC
150{
151 struct varobj *var;
152 struct vstack *next;
153};
8b93c638
JM
154
155struct cpstack
72330bd6
AC
156{
157 char *name;
158 struct cpstack *next;
159};
8b93c638
JM
160
161/* A list of varobjs */
162
163struct vlist
72330bd6
AC
164{
165 struct varobj *var;
166 struct vlist *next;
167};
8b93c638
JM
168
169/* Private function prototypes */
170
171/* Helper functions for the above subcommands. */
172
a14ed312 173static int delete_variable (struct cpstack **, struct varobj *, int);
8b93c638 174
a14ed312
KB
175static void delete_variable_1 (struct cpstack **, int *,
176 struct varobj *, int, int);
8b93c638 177
a14ed312 178static int install_variable (struct varobj *);
8b93c638 179
a14ed312 180static void uninstall_variable (struct varobj *);
8b93c638 181
a14ed312 182static struct varobj *child_exists (struct varobj *, char *);
8b93c638 183
a14ed312 184static struct varobj *create_child (struct varobj *, int, char *);
8b93c638 185
a14ed312 186static void save_child_in_parent (struct varobj *, struct varobj *);
8b93c638 187
a14ed312 188static void remove_child_from_parent (struct varobj *, struct varobj *);
8b93c638
JM
189
190/* Utility routines */
191
a14ed312 192static struct varobj *new_variable (void);
8b93c638 193
a14ed312 194static struct varobj *new_root_variable (void);
8b93c638 195
a14ed312 196static void free_variable (struct varobj *var);
8b93c638 197
74b7792f
AC
198static struct cleanup *make_cleanup_free_variable (struct varobj *var);
199
a14ed312 200static struct type *get_type (struct varobj *var);
8b93c638 201
a14ed312 202static struct type *get_type_deref (struct varobj *var);
8b93c638 203
a14ed312 204static struct type *get_target_type (struct type *);
8b93c638 205
a14ed312 206static enum varobj_display_formats variable_default_display (struct varobj *);
8b93c638 207
a14ed312 208static void vpush (struct vstack **pstack, struct varobj *var);
8b93c638 209
a14ed312 210static struct varobj *vpop (struct vstack **pstack);
8b93c638 211
a14ed312 212static void cppush (struct cpstack **pstack, char *name);
8b93c638 213
a14ed312 214static char *cppop (struct cpstack **pstack);
8b93c638 215
acd65feb
VP
216static int install_new_value (struct varobj *var, struct value *value,
217 int initial);
218
8b93c638
JM
219/* Language-specific routines. */
220
a14ed312 221static enum varobj_languages variable_language (struct varobj *var);
8b93c638 222
a14ed312 223static int number_of_children (struct varobj *);
8b93c638 224
a14ed312 225static char *name_of_variable (struct varobj *);
8b93c638 226
a14ed312 227static char *name_of_child (struct varobj *, int);
8b93c638 228
30b28db1 229static struct value *value_of_root (struct varobj **var_handle, int *);
8b93c638 230
30b28db1 231static struct value *value_of_child (struct varobj *parent, int index);
8b93c638 232
a14ed312 233static int variable_editable (struct varobj *var);
8b93c638 234
a14ed312 235static char *my_value_of_variable (struct varobj *var);
8b93c638 236
a14ed312 237static int type_changeable (struct varobj *var);
8b93c638
JM
238
239/* C implementation */
240
a14ed312 241static int c_number_of_children (struct varobj *var);
8b93c638 242
a14ed312 243static char *c_name_of_variable (struct varobj *parent);
8b93c638 244
a14ed312 245static char *c_name_of_child (struct varobj *parent, int index);
8b93c638 246
30b28db1 247static struct value *c_value_of_root (struct varobj **var_handle);
8b93c638 248
30b28db1 249static struct value *c_value_of_child (struct varobj *parent, int index);
8b93c638 250
a14ed312 251static struct type *c_type_of_child (struct varobj *parent, int index);
8b93c638 252
a14ed312 253static int c_variable_editable (struct varobj *var);
8b93c638 254
a14ed312 255static char *c_value_of_variable (struct varobj *var);
8b93c638
JM
256
257/* C++ implementation */
258
a14ed312 259static int cplus_number_of_children (struct varobj *var);
8b93c638 260
a14ed312 261static void cplus_class_num_children (struct type *type, int children[3]);
8b93c638 262
a14ed312 263static char *cplus_name_of_variable (struct varobj *parent);
8b93c638 264
a14ed312 265static char *cplus_name_of_child (struct varobj *parent, int index);
8b93c638 266
30b28db1 267static struct value *cplus_value_of_root (struct varobj **var_handle);
8b93c638 268
30b28db1 269static struct value *cplus_value_of_child (struct varobj *parent, int index);
8b93c638 270
a14ed312 271static struct type *cplus_type_of_child (struct varobj *parent, int index);
8b93c638 272
a14ed312 273static int cplus_variable_editable (struct varobj *var);
8b93c638 274
a14ed312 275static char *cplus_value_of_variable (struct varobj *var);
8b93c638
JM
276
277/* Java implementation */
278
a14ed312 279static int java_number_of_children (struct varobj *var);
8b93c638 280
a14ed312 281static char *java_name_of_variable (struct varobj *parent);
8b93c638 282
a14ed312 283static char *java_name_of_child (struct varobj *parent, int index);
8b93c638 284
30b28db1 285static struct value *java_value_of_root (struct varobj **var_handle);
8b93c638 286
30b28db1 287static struct value *java_value_of_child (struct varobj *parent, int index);
8b93c638 288
a14ed312 289static struct type *java_type_of_child (struct varobj *parent, int index);
8b93c638 290
a14ed312 291static int java_variable_editable (struct varobj *var);
8b93c638 292
a14ed312 293static char *java_value_of_variable (struct varobj *var);
8b93c638
JM
294
295/* The language specific vector */
296
297struct language_specific
72330bd6 298{
8b93c638 299
72330bd6
AC
300 /* The language of this variable */
301 enum varobj_languages language;
8b93c638 302
72330bd6
AC
303 /* The number of children of PARENT. */
304 int (*number_of_children) (struct varobj * parent);
8b93c638 305
72330bd6
AC
306 /* The name (expression) of a root varobj. */
307 char *(*name_of_variable) (struct varobj * parent);
8b93c638 308
72330bd6
AC
309 /* The name of the INDEX'th child of PARENT. */
310 char *(*name_of_child) (struct varobj * parent, int index);
8b93c638 311
30b28db1
AC
312 /* The ``struct value *'' of the root variable ROOT. */
313 struct value *(*value_of_root) (struct varobj ** root_handle);
8b93c638 314
30b28db1
AC
315 /* The ``struct value *'' of the INDEX'th child of PARENT. */
316 struct value *(*value_of_child) (struct varobj * parent, int index);
8b93c638 317
72330bd6
AC
318 /* The type of the INDEX'th child of PARENT. */
319 struct type *(*type_of_child) (struct varobj * parent, int index);
8b93c638 320
72330bd6
AC
321 /* Is VAR editable? */
322 int (*variable_editable) (struct varobj * var);
8b93c638 323
72330bd6
AC
324 /* The current value of VAR. */
325 char *(*value_of_variable) (struct varobj * var);
326};
8b93c638
JM
327
328/* Array of known source language routines. */
d5d6fca5 329static struct language_specific languages[vlang_end] = {
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);
d5d6fca5 520 var->root->lang = &languages[lang];
8b93c638
JM
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);
0d2bd018
NR
1055
1056 /* Restore selected frame */
1057 fi = frame_find_by_id (old_fid);
1058 if (fi)
1059 select_frame (fi);
1060
8b93c638 1061 if (new == NULL)
73a93a32 1062 {
705da579 1063 (*varp)->error = 1;
73a93a32
JI
1064 return -1;
1065 }
8b93c638
JM
1066
1067 /* Initialize a stack for temporary results */
1068 vpush (&result, NULL);
1069
ae093f96
FN
1070 /* If this is a "use_selected_frame" varobj, and its type has changed,
1071 them note that it's changed. */
1072 if (type_changed)
8b93c638 1073 {
705da579 1074 vpush (&result, *varp);
ae093f96
FN
1075 changed++;
1076 }
acd65feb
VP
1077
1078 if (install_new_value ((*varp), new, type_changed))
ae093f96 1079 {
acd65feb
VP
1080 /* If type_changed is 1, install_new_value will never return
1081 non-zero, so we'll never report the same variable twice. */
1082 gdb_assert (!type_changed);
1083 vpush (&result, (*varp));
ae093f96 1084 changed++;
8b93c638 1085 }
8b93c638 1086
8b93c638
JM
1087 /* Initialize a stack */
1088 vpush (&stack, NULL);
1089
1090 /* Push the root's children */
705da579 1091 if ((*varp)->children != NULL)
8b93c638
JM
1092 {
1093 struct varobj_child *c;
705da579 1094 for (c = (*varp)->children; c != NULL; c = c->next)
8b93c638
JM
1095 vpush (&stack, c->child);
1096 }
1097
1098 /* Walk through the children, reconstructing them all. */
1099 v = vpop (&stack);
1100 while (v != NULL)
1101 {
1102 /* Push any children */
1103 if (v->children != NULL)
1104 {
1105 struct varobj_child *c;
1106 for (c = v->children; c != NULL; c = c->next)
1107 vpush (&stack, c->child);
1108 }
1109
1110 /* Update this variable */
1111 new = value_of_child (v->parent, v->index);
acd65feb
VP
1112 if (install_new_value (v, new, 0 /* type not changed */))
1113 {
8b93c638
JM
1114 /* Note that it's changed */
1115 vpush (&result, v);
fb9b6b35 1116 v->updated = 0;
8b93c638
JM
1117 changed++;
1118 }
8b93c638
JM
1119
1120 /* Get next child */
1121 v = vpop (&stack);
1122 }
1123
1124 /* Alloc (changed + 1) list entries */
1125 /* FIXME: add a cleanup for the allocated list(s)
1126 because one day the select_frame called below can longjump */
1127 *changelist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1128 if (changed > 1)
1129 {
1130 templist = xmalloc ((changed + 1) * sizeof (struct varobj *));
1131 cv = templist;
1132 }
1133 else
1134 cv = *changelist;
1135
1136 /* Copy from result stack to list */
1137 vleft = changed;
1138 *cv = vpop (&result);
1139 while ((*cv != NULL) && (vleft > 0))
1140 {
1141 vleft--;
1142 cv++;
1143 *cv = vpop (&result);
1144 }
1145 if (vleft)
8a3fe4f8 1146 warning (_("varobj_update: assertion failed - vleft <> 0"));
8b93c638
JM
1147
1148 if (changed > 1)
1149 {
1150 /* Now we revert the order. */
72330bd6
AC
1151 for (i = 0; i < changed; i++)
1152 *(*changelist + i) = *(templist + changed - 1 - i);
8b93c638
JM
1153 *(*changelist + changed) = NULL;
1154 }
1155
73a93a32
JI
1156 if (type_changed)
1157 return -2;
1158 else
1159 return changed;
8b93c638
JM
1160}
1161\f
1162
1163/* Helper functions */
1164
1165/*
1166 * Variable object construction/destruction
1167 */
1168
1169static int
fba45db2
KB
1170delete_variable (struct cpstack **resultp, struct varobj *var,
1171 int only_children_p)
8b93c638
JM
1172{
1173 int delcount = 0;
1174
1175 delete_variable_1 (resultp, &delcount, var,
1176 only_children_p, 1 /* remove_from_parent_p */ );
1177
1178 return delcount;
1179}
1180
1181/* Delete the variable object VAR and its children */
1182/* IMPORTANT NOTE: If we delete a variable which is a child
1183 and the parent is not removed we dump core. It must be always
1184 initially called with remove_from_parent_p set */
1185static void
72330bd6
AC
1186delete_variable_1 (struct cpstack **resultp, int *delcountp,
1187 struct varobj *var, int only_children_p,
1188 int remove_from_parent_p)
8b93c638
JM
1189{
1190 struct varobj_child *vc;
1191 struct varobj_child *next;
1192
1193 /* Delete any children of this variable, too. */
1194 for (vc = var->children; vc != NULL; vc = next)
1195 {
1196 if (!remove_from_parent_p)
1197 vc->child->parent = NULL;
1198 delete_variable_1 (resultp, delcountp, vc->child, 0, only_children_p);
1199 next = vc->next;
b8c9b27d 1200 xfree (vc);
8b93c638
JM
1201 }
1202
1203 /* if we were called to delete only the children we are done here */
1204 if (only_children_p)
1205 return;
1206
1207 /* Otherwise, add it to the list of deleted ones and proceed to do so */
73a93a32
JI
1208 /* If the name is null, this is a temporary variable, that has not
1209 yet been installed, don't report it, it belongs to the caller... */
1210 if (var->obj_name != NULL)
8b93c638 1211 {
5b616ba1 1212 cppush (resultp, xstrdup (var->obj_name));
8b93c638
JM
1213 *delcountp = *delcountp + 1;
1214 }
1215
1216 /* If this variable has a parent, remove it from its parent's list */
1217 /* OPTIMIZATION: if the parent of this variable is also being deleted,
1218 (as indicated by remove_from_parent_p) we don't bother doing an
1219 expensive list search to find the element to remove when we are
1220 discarding the list afterwards */
72330bd6 1221 if ((remove_from_parent_p) && (var->parent != NULL))
8b93c638
JM
1222 {
1223 remove_child_from_parent (var->parent, var);
1224 }
72330bd6 1225
73a93a32
JI
1226 if (var->obj_name != NULL)
1227 uninstall_variable (var);
8b93c638
JM
1228
1229 /* Free memory associated with this variable */
1230 free_variable (var);
1231}
1232
1233/* Install the given variable VAR with the object name VAR->OBJ_NAME. */
1234static int
fba45db2 1235install_variable (struct varobj *var)
8b93c638
JM
1236{
1237 struct vlist *cv;
1238 struct vlist *newvl;
1239 const char *chp;
1240 unsigned int index = 0;
1241 unsigned int i = 1;
1242
1243 for (chp = var->obj_name; *chp; chp++)
1244 {
1245 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1246 }
1247
1248 cv = *(varobj_table + index);
1249 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1250 cv = cv->next;
1251
1252 if (cv != NULL)
8a3fe4f8 1253 error (_("Duplicate variable object name"));
8b93c638
JM
1254
1255 /* Add varobj to hash table */
1256 newvl = xmalloc (sizeof (struct vlist));
1257 newvl->next = *(varobj_table + index);
1258 newvl->var = var;
1259 *(varobj_table + index) = newvl;
1260
1261 /* If root, add varobj to root list */
1262 if (var->root->rootvar == var)
1263 {
1264 /* Add to list of root variables */
1265 if (rootlist == NULL)
1266 var->root->next = NULL;
1267 else
1268 var->root->next = rootlist;
1269 rootlist = var->root;
1270 rootcount++;
1271 }
1272
1273 return 1; /* OK */
1274}
1275
1276/* Unistall the object VAR. */
1277static void
fba45db2 1278uninstall_variable (struct varobj *var)
8b93c638
JM
1279{
1280 struct vlist *cv;
1281 struct vlist *prev;
1282 struct varobj_root *cr;
1283 struct varobj_root *prer;
1284 const char *chp;
1285 unsigned int index = 0;
1286 unsigned int i = 1;
1287
1288 /* Remove varobj from hash table */
1289 for (chp = var->obj_name; *chp; chp++)
1290 {
1291 index = (index + (i++ * (unsigned int) *chp)) % VAROBJ_TABLE_SIZE;
1292 }
1293
1294 cv = *(varobj_table + index);
1295 prev = NULL;
1296 while ((cv != NULL) && (strcmp (cv->var->obj_name, var->obj_name) != 0))
1297 {
1298 prev = cv;
1299 cv = cv->next;
1300 }
1301
1302 if (varobjdebug)
1303 fprintf_unfiltered (gdb_stdlog, "Deleting %s\n", var->obj_name);
1304
1305 if (cv == NULL)
1306 {
72330bd6
AC
1307 warning
1308 ("Assertion failed: Could not find variable object \"%s\" to delete",
1309 var->obj_name);
8b93c638
JM
1310 return;
1311 }
1312
1313 if (prev == NULL)
1314 *(varobj_table + index) = cv->next;
1315 else
1316 prev->next = cv->next;
1317
b8c9b27d 1318 xfree (cv);
8b93c638
JM
1319
1320 /* If root, remove varobj from root list */
1321 if (var->root->rootvar == var)
1322 {
1323 /* Remove from list of root variables */
1324 if (rootlist == var->root)
1325 rootlist = var->root->next;
1326 else
1327 {
1328 prer = NULL;
1329 cr = rootlist;
1330 while ((cr != NULL) && (cr->rootvar != var))
1331 {
1332 prer = cr;
1333 cr = cr->next;
1334 }
1335 if (cr == NULL)
1336 {
72330bd6
AC
1337 warning
1338 ("Assertion failed: Could not find varobj \"%s\" in root list",
1339 var->obj_name);
8b93c638
JM
1340 return;
1341 }
1342 if (prer == NULL)
1343 rootlist = NULL;
1344 else
1345 prer->next = cr->next;
1346 }
1347 rootcount--;
1348 }
1349
1350}
1351
1352/* Does a child with the name NAME exist in VAR? If so, return its data.
1353 If not, return NULL. */
1354static struct varobj *
1669605f 1355child_exists (struct varobj *var, char *name)
8b93c638
JM
1356{
1357 struct varobj_child *vc;
1358
1359 for (vc = var->children; vc != NULL; vc = vc->next)
1360 {
6e382aa3 1361 if (strcmp (vc->child->name, name) == 0)
8b93c638
JM
1362 return vc->child;
1363 }
1364
1365 return NULL;
1366}
1367
1368/* Create and install a child of the parent of the given name */
1369static struct varobj *
fba45db2 1370create_child (struct varobj *parent, int index, char *name)
8b93c638
JM
1371{
1372 struct varobj *child;
1373 char *childs_name;
acd65feb 1374 struct value *value;
8b93c638
JM
1375
1376 child = new_variable ();
1377
1378 /* name is allocated by name_of_child */
1379 child->name = name;
1380 child->index = index;
acd65feb 1381 value = value_of_child (parent, index);
8b93c638
JM
1382 child->parent = parent;
1383 child->root = parent->root;
b435e160 1384 childs_name = xstrprintf ("%s.%s", parent->obj_name, name);
8b93c638
JM
1385 child->obj_name = childs_name;
1386 install_variable (child);
1387
1388 /* Save a pointer to this child in the parent */
1389 save_child_in_parent (parent, child);
1390
acd65feb
VP
1391 /* Compute the type of the child. Must do this before
1392 calling install_new_value. */
1393 if (value != NULL)
1394 /* If the child had no evaluation errors, var->value
1395 will be non-NULL and contain a valid type. */
1396 child->type = value_type (value);
1397 else
1398 /* Otherwise, we must compute the type. */
1399 child->type = (*child->root->lang->type_of_child) (child->parent,
1400 child->index);
1401 install_new_value (child, value, 1);
1402
1403 if ((!CPLUS_FAKE_CHILD (child) && child->value == NULL) || parent->error)
1404 child->error = 1;
8b93c638
JM
1405
1406 return child;
1407}
1408
1409/* FIXME: This should be a generic add to list */
1410/* Save CHILD in the PARENT's data. */
1411static void
fba45db2 1412save_child_in_parent (struct varobj *parent, struct varobj *child)
8b93c638
JM
1413{
1414 struct varobj_child *vc;
1415
1416 /* Insert the child at the top */
1417 vc = parent->children;
1418 parent->children =
1419 (struct varobj_child *) xmalloc (sizeof (struct varobj_child));
1420
1421 parent->children->next = vc;
1422 parent->children->child = child;
1423}
1424
1425/* FIXME: This should be a generic remove from list */
1426/* Remove the CHILD from the PARENT's list of children. */
1427static void
fba45db2 1428remove_child_from_parent (struct varobj *parent, struct varobj *child)
8b93c638
JM
1429{
1430 struct varobj_child *vc, *prev;
1431
1432 /* Find the child in the parent's list */
1433 prev = NULL;
1434 for (vc = parent->children; vc != NULL;)
1435 {
1436 if (vc->child == child)
1437 break;
1438 prev = vc;
1439 vc = vc->next;
1440 }
1441
1442 if (prev == NULL)
1443 parent->children = vc->next;
1444 else
1445 prev->next = vc->next;
1446
1447}
1448\f
1449
1450/*
1451 * Miscellaneous utility functions.
1452 */
1453
1454/* Allocate memory and initialize a new variable */
1455static struct varobj *
1456new_variable (void)
1457{
1458 struct varobj *var;
1459
1460 var = (struct varobj *) xmalloc (sizeof (struct varobj));
1461 var->name = NULL;
1462 var->obj_name = NULL;
1463 var->index = -1;
1464 var->type = NULL;
1465 var->value = NULL;
1466 var->error = 0;
1467 var->num_children = -1;
1468 var->parent = NULL;
1469 var->children = NULL;
1470 var->format = 0;
1471 var->root = NULL;
fb9b6b35 1472 var->updated = 0;
8b93c638
JM
1473
1474 return var;
1475}
1476
1477/* Allocate memory and initialize a new root variable */
1478static struct varobj *
1479new_root_variable (void)
1480{
1481 struct varobj *var = new_variable ();
1482 var->root = (struct varobj_root *) xmalloc (sizeof (struct varobj_root));;
1483 var->root->lang = NULL;
1484 var->root->exp = NULL;
1485 var->root->valid_block = NULL;
7a424e99 1486 var->root->frame = null_frame_id;
73a93a32 1487 var->root->use_selected_frame = 0;
8b93c638
JM
1488 var->root->rootvar = NULL;
1489
1490 return var;
1491}
1492
1493/* Free any allocated memory associated with VAR. */
1494static void
fba45db2 1495free_variable (struct varobj *var)
8b93c638
JM
1496{
1497 /* Free the expression if this is a root variable. */
1498 if (var->root->rootvar == var)
1499 {
96c1eda2 1500 free_current_contents (&var->root->exp);
8038e1e2 1501 xfree (var->root);
8b93c638
JM
1502 }
1503
8038e1e2
AC
1504 xfree (var->name);
1505 xfree (var->obj_name);
1506 xfree (var);
8b93c638
JM
1507}
1508
74b7792f
AC
1509static void
1510do_free_variable_cleanup (void *var)
1511{
1512 free_variable (var);
1513}
1514
1515static struct cleanup *
1516make_cleanup_free_variable (struct varobj *var)
1517{
1518 return make_cleanup (do_free_variable_cleanup, var);
1519}
1520
6766a268
DJ
1521/* This returns the type of the variable. It also skips past typedefs
1522 to return the real type of the variable.
94b66fa7
KS
1523
1524 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1525 except within get_target_type and get_type. */
8b93c638 1526static struct type *
fba45db2 1527get_type (struct varobj *var)
8b93c638
JM
1528{
1529 struct type *type;
1530 type = var->type;
1531
6766a268
DJ
1532 if (type != NULL)
1533 type = check_typedef (type);
8b93c638
JM
1534
1535 return type;
1536}
1537
1538/* This returns the type of the variable, dereferencing pointers, too. */
1539static struct type *
fba45db2 1540get_type_deref (struct varobj *var)
8b93c638
JM
1541{
1542 struct type *type;
1543
1544 type = get_type (var);
1545
1546 if (type != NULL && (TYPE_CODE (type) == TYPE_CODE_PTR
1547 || TYPE_CODE (type) == TYPE_CODE_REF))
1548 type = get_target_type (type);
1549
1550 return type;
1551}
1552
1553/* This returns the target type (or NULL) of TYPE, also skipping
94b66fa7
KS
1554 past typedefs, just like get_type ().
1555
1556 NOTE: TYPE_TARGET_TYPE should NOT be used anywhere in this file
1557 except within get_target_type and get_type. */
8b93c638 1558static struct type *
fba45db2 1559get_target_type (struct type *type)
8b93c638
JM
1560{
1561 if (type != NULL)
1562 {
1563 type = TYPE_TARGET_TYPE (type);
6766a268
DJ
1564 if (type != NULL)
1565 type = check_typedef (type);
8b93c638
JM
1566 }
1567
1568 return type;
1569}
1570
1571/* What is the default display for this variable? We assume that
1572 everything is "natural". Any exceptions? */
1573static enum varobj_display_formats
fba45db2 1574variable_default_display (struct varobj *var)
8b93c638
JM
1575{
1576 return FORMAT_NATURAL;
1577}
1578
8b93c638
JM
1579/* FIXME: The following should be generic for any pointer */
1580static void
fba45db2 1581vpush (struct vstack **pstack, struct varobj *var)
8b93c638
JM
1582{
1583 struct vstack *s;
1584
1585 s = (struct vstack *) xmalloc (sizeof (struct vstack));
1586 s->var = var;
1587 s->next = *pstack;
1588 *pstack = s;
1589}
1590
1591/* FIXME: The following should be generic for any pointer */
1592static struct varobj *
fba45db2 1593vpop (struct vstack **pstack)
8b93c638
JM
1594{
1595 struct vstack *s;
1596 struct varobj *v;
1597
1598 if ((*pstack)->var == NULL && (*pstack)->next == NULL)
1599 return NULL;
1600
1601 s = *pstack;
1602 v = s->var;
1603 *pstack = (*pstack)->next;
b8c9b27d 1604 xfree (s);
8b93c638
JM
1605
1606 return v;
1607}
1608
1609/* FIXME: The following should be generic for any pointer */
1610static void
fba45db2 1611cppush (struct cpstack **pstack, char *name)
8b93c638
JM
1612{
1613 struct cpstack *s;
1614
1615 s = (struct cpstack *) xmalloc (sizeof (struct cpstack));
1616 s->name = name;
1617 s->next = *pstack;
1618 *pstack = s;
1619}
1620
1621/* FIXME: The following should be generic for any pointer */
1622static char *
fba45db2 1623cppop (struct cpstack **pstack)
8b93c638
JM
1624{
1625 struct cpstack *s;
1626 char *v;
1627
1628 if ((*pstack)->name == NULL && (*pstack)->next == NULL)
1629 return NULL;
1630
1631 s = *pstack;
1632 v = s->name;
1633 *pstack = (*pstack)->next;
b8c9b27d 1634 xfree (s);
8b93c638
JM
1635
1636 return v;
1637}
1638\f
1639/*
1640 * Language-dependencies
1641 */
1642
1643/* Common entry points */
1644
1645/* Get the language of variable VAR. */
1646static enum varobj_languages
fba45db2 1647variable_language (struct varobj *var)
8b93c638
JM
1648{
1649 enum varobj_languages lang;
1650
1651 switch (var->root->exp->language_defn->la_language)
1652 {
1653 default:
1654 case language_c:
1655 lang = vlang_c;
1656 break;
1657 case language_cplus:
1658 lang = vlang_cplus;
1659 break;
1660 case language_java:
1661 lang = vlang_java;
1662 break;
1663 }
1664
1665 return lang;
1666}
1667
1668/* Return the number of children for a given variable.
1669 The result of this function is defined by the language
1670 implementation. The number of children returned by this function
1671 is the number of children that the user will see in the variable
1672 display. */
1673static int
fba45db2 1674number_of_children (struct varobj *var)
8b93c638
JM
1675{
1676 return (*var->root->lang->number_of_children) (var);;
1677}
1678
1679/* What is the expression for the root varobj VAR? Returns a malloc'd string. */
1680static char *
fba45db2 1681name_of_variable (struct varobj *var)
8b93c638
JM
1682{
1683 return (*var->root->lang->name_of_variable) (var);
1684}
1685
1686/* What is the name of the INDEX'th child of VAR? Returns a malloc'd string. */
1687static char *
fba45db2 1688name_of_child (struct varobj *var, int index)
8b93c638
JM
1689{
1690 return (*var->root->lang->name_of_child) (var, index);
1691}
1692
30b28db1 1693/* What is the ``struct value *'' of the root variable VAR?
73a93a32
JI
1694 TYPE_CHANGED controls what to do if the type of a
1695 use_selected_frame = 1 variable changes. On input,
1696 TYPE_CHANGED = 1 means discard the old varobj, and replace
1697 it with this one. TYPE_CHANGED = 0 means leave it around.
1698 NB: In both cases, var_handle will point to the new varobj,
1699 so if you use TYPE_CHANGED = 0, you will have to stash the
1700 old varobj pointer away somewhere before calling this.
1701 On return, TYPE_CHANGED will be 1 if the type has changed, and
1702 0 otherwise. */
30b28db1 1703static struct value *
fba45db2 1704value_of_root (struct varobj **var_handle, int *type_changed)
8b93c638 1705{
73a93a32
JI
1706 struct varobj *var;
1707
1708 if (var_handle == NULL)
1709 return NULL;
1710
1711 var = *var_handle;
1712
1713 /* This should really be an exception, since this should
1714 only get called with a root variable. */
1715
1716 if (var->root->rootvar != var)
1717 return NULL;
1718
1719 if (var->root->use_selected_frame)
1720 {
1721 struct varobj *tmp_var;
1722 char *old_type, *new_type;
1723 old_type = varobj_get_type (var);
1724 tmp_var = varobj_create (NULL, var->name, (CORE_ADDR) 0,
1725 USE_SELECTED_FRAME);
1726 if (tmp_var == NULL)
1727 {
1728 return NULL;
1729 }
1730 new_type = varobj_get_type (tmp_var);
72330bd6 1731 if (strcmp (old_type, new_type) == 0)
73a93a32
JI
1732 {
1733 varobj_delete (tmp_var, NULL, 0);
1734 *type_changed = 0;
1735 }
1736 else
1737 {
1738 if (*type_changed)
1739 {
72330bd6 1740 tmp_var->obj_name =
73a93a32 1741 savestring (var->obj_name, strlen (var->obj_name));
f7635dd9 1742 varobj_delete (var, NULL, 0);
73a93a32
JI
1743 }
1744 else
1745 {
72330bd6 1746 tmp_var->obj_name = varobj_gen_name ();
73a93a32
JI
1747 }
1748 install_variable (tmp_var);
1749 *var_handle = tmp_var;
705da579 1750 var = *var_handle;
73a93a32
JI
1751 *type_changed = 1;
1752 }
1753 }
1754 else
1755 {
1756 *type_changed = 0;
1757 }
1758
1759 return (*var->root->lang->value_of_root) (var_handle);
8b93c638
JM
1760}
1761
30b28db1
AC
1762/* What is the ``struct value *'' for the INDEX'th child of PARENT? */
1763static struct value *
fba45db2 1764value_of_child (struct varobj *parent, int index)
8b93c638 1765{
30b28db1 1766 struct value *value;
8b93c638
JM
1767
1768 value = (*parent->root->lang->value_of_child) (parent, index);
1769
8b93c638
JM
1770 return value;
1771}
1772
8b93c638
JM
1773/* Is this variable editable? Use the variable's type to make
1774 this determination. */
1775static int
fba45db2 1776variable_editable (struct varobj *var)
8b93c638
JM
1777{
1778 return (*var->root->lang->variable_editable) (var);
1779}
1780
1781/* GDB already has a command called "value_of_variable". Sigh. */
1782static char *
fba45db2 1783my_value_of_variable (struct varobj *var)
8b93c638
JM
1784{
1785 return (*var->root->lang->value_of_variable) (var);
1786}
1787
acd65feb
VP
1788/* Return non-zero if changes in value of VAR
1789 must be detected and reported by -var-update.
1790 Return zero is -var-update should never report
1791 changes of such values. This makes sense for structures
1792 (since the changes in children values will be reported separately),
1793 or for artifical objects (like 'public' pseudo-field in C++).
1794
1795 Return value of 0 means that gdb need not call value_fetch_lazy
1796 for the value of this variable object. */
8b93c638 1797static int
fba45db2 1798type_changeable (struct varobj *var)
8b93c638
JM
1799{
1800 int r;
1801 struct type *type;
1802
1803 if (CPLUS_FAKE_CHILD (var))
1804 return 0;
1805
1806 type = get_type (var);
1807
1808 switch (TYPE_CODE (type))
1809 {
72330bd6
AC
1810 case TYPE_CODE_STRUCT:
1811 case TYPE_CODE_UNION:
1812 case TYPE_CODE_ARRAY:
1813 r = 0;
1814 break;
8b93c638 1815
72330bd6
AC
1816 default:
1817 r = 1;
8b93c638
JM
1818 }
1819
1820 return r;
1821}
1822
1823/* C */
1824static int
fba45db2 1825c_number_of_children (struct varobj *var)
8b93c638
JM
1826{
1827 struct type *type;
1828 struct type *target;
1829 int children;
1830
1831 type = get_type (var);
1832 target = get_target_type (type);
1833 children = 0;
1834
1835 switch (TYPE_CODE (type))
1836 {
1837 case TYPE_CODE_ARRAY:
1838 if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
72330bd6 1839 && TYPE_ARRAY_UPPER_BOUND_TYPE (type) != BOUND_CANNOT_BE_DETERMINED)
8b93c638
JM
1840 children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
1841 else
1842 children = -1;
1843 break;
1844
1845 case TYPE_CODE_STRUCT:
1846 case TYPE_CODE_UNION:
1847 children = TYPE_NFIELDS (type);
1848 break;
1849
1850 case TYPE_CODE_PTR:
1851 /* This is where things get compilcated. All pointers have one child.
1852 Except, of course, for struct and union ptr, which we automagically
0755e6c1
FN
1853 dereference for the user and function ptrs, which have no children.
1854 We also don't dereference void* as we don't know what to show.
1855 We can show char* so we allow it to be dereferenced. If you decide
1856 to test for it, please mind that a little magic is necessary to
1857 properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and
1858 TYPE_NAME == "char" */
1859
8b93c638
JM
1860 switch (TYPE_CODE (target))
1861 {
1862 case TYPE_CODE_STRUCT:
1863 case TYPE_CODE_UNION:
1864 children = TYPE_NFIELDS (target);
1865 break;
1866
1867 case TYPE_CODE_FUNC:
0755e6c1 1868 case TYPE_CODE_VOID:
8b93c638
JM
1869 children = 0;
1870 break;
1871
1872 default:
0755e6c1 1873 children = 1;
8b93c638
JM
1874 }
1875 break;
1876
1877 default:
1878 /* Other types have no children */
1879 break;
1880 }
1881
1882 return children;
1883}
1884
1885static char *
fba45db2 1886c_name_of_variable (struct varobj *parent)
8b93c638
JM
1887{
1888 return savestring (parent->name, strlen (parent->name));
1889}
1890
1891static char *
fba45db2 1892c_name_of_child (struct varobj *parent, int index)
8b93c638
JM
1893{
1894 struct type *type;
1895 struct type *target;
1896 char *name;
1897 char *string;
1898
1899 type = get_type (parent);
1900 target = get_target_type (type);
1901
1902 switch (TYPE_CODE (type))
1903 {
1904 case TYPE_CODE_ARRAY:
7a24eb7c
NR
1905 name = xstrprintf ("%d", index
1906 + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)));
8b93c638
JM
1907 break;
1908
1909 case TYPE_CODE_STRUCT:
1910 case TYPE_CODE_UNION:
1911 string = TYPE_FIELD_NAME (type, index);
1912 name = savestring (string, strlen (string));
1913 break;
1914
1915 case TYPE_CODE_PTR:
1916 switch (TYPE_CODE (target))
1917 {
1918 case TYPE_CODE_STRUCT:
1919 case TYPE_CODE_UNION:
1920 string = TYPE_FIELD_NAME (target, index);
1921 name = savestring (string, strlen (string));
1922 break;
1923
1924 default:
b435e160 1925 name = xstrprintf ("*%s", parent->name);
8b93c638
JM
1926 break;
1927 }
1928 break;
1929
1930 default:
1931 /* This should not happen */
1932 name = xstrdup ("???");
1933 }
1934
1935 return name;
1936}
1937
30b28db1 1938static struct value *
fba45db2 1939c_value_of_root (struct varobj **var_handle)
8b93c638 1940{
30b28db1 1941 struct value *new_val;
73a93a32 1942 struct varobj *var = *var_handle;
8b93c638
JM
1943 struct frame_info *fi;
1944 int within_scope;
1945
73a93a32
JI
1946 /* Only root variables can be updated... */
1947 if (var->root->rootvar != var)
1948 /* Not a root var */
1949 return NULL;
1950
72330bd6 1951
8b93c638
JM
1952 /* Determine whether the variable is still around. */
1953 if (var->root->valid_block == NULL)
1954 within_scope = 1;
1955 else
1956 {
1957 reinit_frame_cache ();
e64d9b3d 1958 fi = frame_find_by_id (var->root->frame);
8b93c638
JM
1959 within_scope = fi != NULL;
1960 /* FIXME: select_frame could fail */
d2353924
NR
1961 if (fi)
1962 {
1963 CORE_ADDR pc = get_frame_pc (fi);
1964 if (pc < BLOCK_START (var->root->valid_block) ||
1965 pc >= BLOCK_END (var->root->valid_block))
1966 within_scope = 0;
1967 select_frame (fi);
1968 }
8b93c638 1969 }
72330bd6 1970
8b93c638
JM
1971 if (within_scope)
1972 {
73a93a32 1973 /* We need to catch errors here, because if evaluate
72330bd6
AC
1974 expression fails we just want to make val->error = 1 and
1975 go on */
8b93c638
JM
1976 if (gdb_evaluate_expression (var->root->exp, &new_val))
1977 {
acd65feb
VP
1978 var->error = 0;
1979 release_value (new_val);
8b93c638
JM
1980 }
1981 else
1982 var->error = 1;
72330bd6 1983
8b93c638
JM
1984 return new_val;
1985 }
1986
1987 return NULL;
1988}
1989
30b28db1 1990static struct value *
fba45db2 1991c_value_of_child (struct varobj *parent, int index)
8b93c638 1992{
30b28db1
AC
1993 struct value *value;
1994 struct value *temp;
1995 struct value *indval;
8b93c638
JM
1996 struct type *type, *target;
1997 char *name;
7a24eb7c 1998 int real_index;
8b93c638
JM
1999
2000 type = get_type (parent);
2001 target = get_target_type (type);
2002 name = name_of_child (parent, index);
2003 temp = parent->value;
2004 value = NULL;
2005
2006 if (temp != NULL)
2007 {
2008 switch (TYPE_CODE (type))
2009 {
2010 case TYPE_CODE_ARRAY:
7a24eb7c 2011 real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
8310b29b 2012#if 0
72330bd6 2013 /* This breaks if the array lives in a (vector) register. */
7a24eb7c 2014 value = value_slice (temp, real_index, 1);
8b93c638
JM
2015 temp = value_coerce_array (value);
2016 gdb_value_ind (temp, &value);
8310b29b 2017#else
7a24eb7c 2018 indval = value_from_longest (builtin_type_int, (LONGEST) real_index);
8310b29b
FN
2019 gdb_value_subscript (temp, indval, &value);
2020#endif
8b93c638
JM
2021 break;
2022
2023 case TYPE_CODE_STRUCT:
2024 case TYPE_CODE_UNION:
7f19b9a2
AC
2025 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2026 "vstructure");
8b93c638
JM
2027 break;
2028
2029 case TYPE_CODE_PTR:
2030 switch (TYPE_CODE (target))
2031 {
2032 case TYPE_CODE_STRUCT:
2033 case TYPE_CODE_UNION:
7f19b9a2
AC
2034 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2035 "vstructure");
8b93c638
JM
2036 break;
2037
2038 default:
2039 gdb_value_ind (temp, &value);
2040 break;
2041 }
2042 break;
2043
2044 default:
2045 break;
2046 }
2047 }
2048
2049 if (value != NULL)
2050 release_value (value);
2051
5bbc1a8e 2052 xfree (name);
8b93c638
JM
2053 return value;
2054}
2055
2056static struct type *
fba45db2 2057c_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2058{
2059 struct type *type;
2060 char *name = name_of_child (parent, index);
2061
2062 switch (TYPE_CODE (parent->type))
2063 {
2064 case TYPE_CODE_ARRAY:
94b66fa7 2065 type = get_target_type (parent->type);
8b93c638
JM
2066 break;
2067
2068 case TYPE_CODE_STRUCT:
2069 case TYPE_CODE_UNION:
2070 type = lookup_struct_elt_type (parent->type, name, 0);
2071 break;
2072
2073 case TYPE_CODE_PTR:
94b66fa7 2074 switch (TYPE_CODE (get_target_type (parent->type)))
8b93c638
JM
2075 {
2076 case TYPE_CODE_STRUCT:
2077 case TYPE_CODE_UNION:
2078 type = lookup_struct_elt_type (parent->type, name, 0);
2079 break;
2080
2081 default:
94b66fa7 2082 type = get_target_type (parent->type);
8b93c638
JM
2083 break;
2084 }
2085 break;
2086
2087 default:
2088 /* This should not happen as only the above types have children */
8a3fe4f8 2089 warning (_("Child of parent whose type does not allow children"));
8b93c638
JM
2090 /* FIXME: Can we still go on? */
2091 type = NULL;
2092 break;
2093 }
2094
5bbc1a8e 2095 xfree (name);
8b93c638
JM
2096 return type;
2097}
2098
2099static int
fba45db2 2100c_variable_editable (struct varobj *var)
8b93c638
JM
2101{
2102 switch (TYPE_CODE (get_type (var)))
2103 {
2104 case TYPE_CODE_STRUCT:
2105 case TYPE_CODE_UNION:
2106 case TYPE_CODE_ARRAY:
2107 case TYPE_CODE_FUNC:
8b93c638
JM
2108 case TYPE_CODE_METHOD:
2109 return 0;
2110 break;
2111
2112 default:
2113 return 1;
2114 break;
2115 }
2116}
2117
2118static char *
fba45db2 2119c_value_of_variable (struct varobj *var)
8b93c638 2120{
14b3d9c9
JB
2121 /* BOGUS: if val_print sees a struct/class, or a reference to one,
2122 it will print out its children instead of "{...}". So we need to
2123 catch that case explicitly. */
2124 struct type *type = get_type (var);
e64d9b3d 2125
14b3d9c9
JB
2126 /* Strip top-level references. */
2127 while (TYPE_CODE (type) == TYPE_CODE_REF)
2128 type = check_typedef (TYPE_TARGET_TYPE (type));
2129
2130 switch (TYPE_CODE (type))
8b93c638
JM
2131 {
2132 case TYPE_CODE_STRUCT:
2133 case TYPE_CODE_UNION:
2134 return xstrdup ("{...}");
2135 /* break; */
2136
2137 case TYPE_CODE_ARRAY:
2138 {
e64d9b3d 2139 char *number;
b435e160 2140 number = xstrprintf ("[%d]", var->num_children);
e64d9b3d 2141 return (number);
8b93c638
JM
2142 }
2143 /* break; */
2144
2145 default:
2146 {
575bbeb6
KS
2147 if (var->value == NULL)
2148 {
2149 /* This can happen if we attempt to get the value of a struct
2150 member when the parent is an invalid pointer. This is an
2151 error condition, so we should tell the caller. */
2152 return NULL;
2153 }
2154 else
2155 {
e64d9b3d
MH
2156 long dummy;
2157 struct ui_file *stb = mem_fileopen ();
2158 struct cleanup *old_chain = make_cleanup_ui_file_delete (stb);
2159 char *thevalue;
2160
acd65feb
VP
2161 gdb_assert (type_changeable (var));
2162 gdb_assert (!value_lazy (var->value));
806048c6
DJ
2163 common_val_print (var->value, stb,
2164 format_code[(int) var->format], 1, 0, 0);
575bbeb6
KS
2165 thevalue = ui_file_xstrdup (stb, &dummy);
2166 do_cleanups (old_chain);
8b93c638
JM
2167 return thevalue;
2168 }
e64d9b3d 2169 }
8b93c638
JM
2170 }
2171}
2172\f
2173
2174/* C++ */
2175
2176static int
fba45db2 2177cplus_number_of_children (struct varobj *var)
8b93c638
JM
2178{
2179 struct type *type;
2180 int children, dont_know;
2181
2182 dont_know = 1;
2183 children = 0;
2184
2185 if (!CPLUS_FAKE_CHILD (var))
2186 {
2187 type = get_type_deref (var);
2188
2189 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
72330bd6 2190 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
8b93c638
JM
2191 {
2192 int kids[3];
2193
2194 cplus_class_num_children (type, kids);
2195 if (kids[v_public] != 0)
2196 children++;
2197 if (kids[v_private] != 0)
2198 children++;
2199 if (kids[v_protected] != 0)
2200 children++;
2201
2202 /* Add any baseclasses */
2203 children += TYPE_N_BASECLASSES (type);
2204 dont_know = 0;
2205
2206 /* FIXME: save children in var */
2207 }
2208 }
2209 else
2210 {
2211 int kids[3];
2212
2213 type = get_type_deref (var->parent);
2214
2215 cplus_class_num_children (type, kids);
6e382aa3 2216 if (strcmp (var->name, "public") == 0)
8b93c638 2217 children = kids[v_public];
6e382aa3 2218 else if (strcmp (var->name, "private") == 0)
8b93c638
JM
2219 children = kids[v_private];
2220 else
2221 children = kids[v_protected];
2222 dont_know = 0;
2223 }
2224
2225 if (dont_know)
2226 children = c_number_of_children (var);
2227
2228 return children;
2229}
2230
2231/* Compute # of public, private, and protected variables in this class.
2232 That means we need to descend into all baseclasses and find out
2233 how many are there, too. */
2234static void
1669605f 2235cplus_class_num_children (struct type *type, int children[3])
8b93c638
JM
2236{
2237 int i;
2238
2239 children[v_public] = 0;
2240 children[v_private] = 0;
2241 children[v_protected] = 0;
2242
2243 for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
2244 {
2245 /* If we have a virtual table pointer, omit it. */
72330bd6 2246 if (TYPE_VPTR_BASETYPE (type) == type && TYPE_VPTR_FIELDNO (type) == i)
8b93c638
JM
2247 continue;
2248
2249 if (TYPE_FIELD_PROTECTED (type, i))
2250 children[v_protected]++;
2251 else if (TYPE_FIELD_PRIVATE (type, i))
2252 children[v_private]++;
2253 else
2254 children[v_public]++;
2255 }
2256}
2257
2258static char *
fba45db2 2259cplus_name_of_variable (struct varobj *parent)
8b93c638
JM
2260{
2261 return c_name_of_variable (parent);
2262}
2263
2264static char *
fba45db2 2265cplus_name_of_child (struct varobj *parent, int index)
8b93c638
JM
2266{
2267 char *name;
2268 struct type *type;
8b93c638
JM
2269
2270 if (CPLUS_FAKE_CHILD (parent))
2271 {
2272 /* Looking for children of public, private, or protected. */
2273 type = get_type_deref (parent->parent);
2274 }
2275 else
2276 type = get_type_deref (parent);
2277
2278 name = NULL;
2279 switch (TYPE_CODE (type))
2280 {
2281 case TYPE_CODE_STRUCT:
2282 case TYPE_CODE_UNION:
8b93c638
JM
2283 if (CPLUS_FAKE_CHILD (parent))
2284 {
6e382aa3
JJ
2285 /* The fields of the class type are ordered as they
2286 appear in the class. We are given an index for a
2287 particular access control type ("public","protected",
2288 or "private"). We must skip over fields that don't
2289 have the access control we are looking for to properly
2290 find the indexed field. */
2291 int type_index = TYPE_N_BASECLASSES (type);
2292 if (strcmp (parent->name, "private") == 0)
2293 {
2294 while (index >= 0)
2295 {
2296 if (TYPE_VPTR_BASETYPE (type) == type
2297 && type_index == TYPE_VPTR_FIELDNO (type))
2298 ; /* ignore vptr */
2299 else if (TYPE_FIELD_PRIVATE (type, type_index))
2300 --index;
2301 ++type_index;
2302 }
2303 --type_index;
2304 }
2305 else if (strcmp (parent->name, "protected") == 0)
2306 {
2307 while (index >= 0)
2308 {
2309 if (TYPE_VPTR_BASETYPE (type) == type
2310 && type_index == TYPE_VPTR_FIELDNO (type))
2311 ; /* ignore vptr */
2312 else if (TYPE_FIELD_PROTECTED (type, type_index))
2313 --index;
2314 ++type_index;
2315 }
2316 --type_index;
2317 }
2318 else
2319 {
2320 while (index >= 0)
2321 {
2322 if (TYPE_VPTR_BASETYPE (type) == type
2323 && type_index == TYPE_VPTR_FIELDNO (type))
2324 ; /* ignore vptr */
2325 else if (!TYPE_FIELD_PRIVATE (type, type_index) &&
2326 !TYPE_FIELD_PROTECTED (type, type_index))
2327 --index;
2328 ++type_index;
2329 }
2330 --type_index;
2331 }
2332
2333 name = TYPE_FIELD_NAME (type, type_index);
8b93c638
JM
2334 }
2335 else if (index < TYPE_N_BASECLASSES (type))
6e382aa3 2336 /* We are looking up the name of a base class */
8b93c638
JM
2337 name = TYPE_FIELD_NAME (type, index);
2338 else
2339 {
6e382aa3
JJ
2340 int children[3];
2341 cplus_class_num_children(type, children);
2342
8b93c638 2343 /* Everything beyond the baseclasses can
6e382aa3
JJ
2344 only be "public", "private", or "protected"
2345
2346 The special "fake" children are always output by varobj in
2347 this order. So if INDEX == 2, it MUST be "protected". */
8b93c638
JM
2348 index -= TYPE_N_BASECLASSES (type);
2349 switch (index)
2350 {
2351 case 0:
6e382aa3
JJ
2352 if (children[v_public] > 0)
2353 name = "public";
2354 else if (children[v_private] > 0)
2355 name = "private";
2356 else
2357 name = "protected";
2358 break;
8b93c638 2359 case 1:
6e382aa3 2360 if (children[v_public] > 0)
8b93c638 2361 {
6e382aa3
JJ
2362 if (children[v_private] > 0)
2363 name = "private";
2364 else
2365 name = "protected";
8b93c638 2366 }
6e382aa3
JJ
2367 else if (children[v_private] > 0)
2368 name = "protected";
2369 break;
8b93c638 2370 case 2:
6e382aa3
JJ
2371 /* Must be protected */
2372 name = "protected";
2373 break;
8b93c638
JM
2374 default:
2375 /* error! */
2376 break;
2377 }
2378 }
2379 break;
2380
2381 default:
2382 break;
2383 }
2384
2385 if (name == NULL)
2386 return c_name_of_child (parent, index);
2387 else
2388 {
2389 if (name != NULL)
2390 name = savestring (name, strlen (name));
2391 }
2392
2393 return name;
2394}
2395
30b28db1 2396static struct value *
fba45db2 2397cplus_value_of_root (struct varobj **var_handle)
8b93c638 2398{
73a93a32 2399 return c_value_of_root (var_handle);
8b93c638
JM
2400}
2401
30b28db1 2402static struct value *
fba45db2 2403cplus_value_of_child (struct varobj *parent, int index)
8b93c638
JM
2404{
2405 struct type *type;
30b28db1 2406 struct value *value;
8b93c638
JM
2407
2408 if (CPLUS_FAKE_CHILD (parent))
2409 type = get_type_deref (parent->parent);
2410 else
2411 type = get_type_deref (parent);
2412
2413 value = NULL;
8b93c638
JM
2414
2415 if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT) ||
2416 ((TYPE_CODE (type)) == TYPE_CODE_UNION))
2417 {
2418 if (CPLUS_FAKE_CHILD (parent))
2419 {
5bbc1a8e 2420 char *name;
30b28db1 2421 struct value *temp = parent->parent->value;
30c6b1fb 2422
575bbeb6
KS
2423 if (temp == NULL)
2424 return NULL;
2425
5bbc1a8e 2426 name = name_of_child (parent, index);
30c6b1fb
KS
2427 gdb_value_struct_elt (NULL, &value, &temp, NULL, name, NULL,
2428 "cplus_structure");
2429 if (value != NULL)
2430 release_value (value);
5bbc1a8e
KS
2431
2432 xfree (name);
8b93c638
JM
2433 }
2434 else if (index >= TYPE_N_BASECLASSES (type))
2435 {
2436 /* public, private, or protected */
2437 return NULL;
2438 }
2439 else
2440 {
2441 /* Baseclass */
2442 if (parent->value != NULL)
2443 {
575bbeb6 2444 struct value *temp = NULL;
8b93c638 2445
4ae4f4fb
VP
2446 /* No special processing for references is needed --
2447 value_cast below handles references. */
2448 if (TYPE_CODE (value_type (parent->value)) == TYPE_CODE_PTR)
4abb499e
KS
2449 {
2450 if (!gdb_value_ind (parent->value, &temp))
2451 return NULL;
2452 }
8b93c638
JM
2453 else
2454 temp = parent->value;
2455
575bbeb6
KS
2456 if (temp != NULL)
2457 {
2458 value = value_cast (TYPE_FIELD_TYPE (type, index), temp);
2459 release_value (value);
2460 }
2461 else
2462 {
2463 /* We failed to evaluate the parent's value, so don't even
2464 bother trying to evaluate this child. */
2465 return NULL;
2466 }
8b93c638
JM
2467 }
2468 }
2469 }
2470
2471 if (value == NULL)
2472 return c_value_of_child (parent, index);
2473
2474 return value;
2475}
2476
2477static struct type *
fba45db2 2478cplus_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2479{
2480 struct type *type, *t;
2481
575bbeb6
KS
2482 if (CPLUS_FAKE_CHILD (parent))
2483 {
2484 /* Looking for the type of a child of public, private, or protected. */
2485 t = get_type_deref (parent->parent);
2486 }
2487 else
2488 t = get_type_deref (parent);
2489
8b93c638
JM
2490 type = NULL;
2491 switch (TYPE_CODE (t))
2492 {
2493 case TYPE_CODE_STRUCT:
2494 case TYPE_CODE_UNION:
575bbeb6 2495 if (CPLUS_FAKE_CHILD (parent))
8b93c638 2496 {
575bbeb6
KS
2497 char *name = cplus_name_of_child (parent, index);
2498 type = lookup_struct_elt_type (t, name, 0);
2499 xfree (name);
8b93c638 2500 }
575bbeb6
KS
2501 else if (index < TYPE_N_BASECLASSES (t))
2502 type = TYPE_FIELD_TYPE (t, index);
8b93c638
JM
2503 else
2504 {
575bbeb6
KS
2505 /* special */
2506 return NULL;
8b93c638
JM
2507 }
2508 break;
2509
2510 default:
2511 break;
2512 }
2513
2514 if (type == NULL)
2515 return c_type_of_child (parent, index);
2516
2517 return type;
2518}
2519
2520static int
fba45db2 2521cplus_variable_editable (struct varobj *var)
8b93c638
JM
2522{
2523 if (CPLUS_FAKE_CHILD (var))
2524 return 0;
2525
2526 return c_variable_editable (var);
2527}
2528
2529static char *
fba45db2 2530cplus_value_of_variable (struct varobj *var)
8b93c638
JM
2531{
2532
2533 /* If we have one of our special types, don't print out
2534 any value. */
2535 if (CPLUS_FAKE_CHILD (var))
2536 return xstrdup ("");
2537
2538 return c_value_of_variable (var);
2539}
2540\f
2541/* Java */
2542
2543static int
fba45db2 2544java_number_of_children (struct varobj *var)
8b93c638
JM
2545{
2546 return cplus_number_of_children (var);
2547}
2548
2549static char *
fba45db2 2550java_name_of_variable (struct varobj *parent)
8b93c638
JM
2551{
2552 char *p, *name;
2553
2554 name = cplus_name_of_variable (parent);
2555 /* If the name has "-" in it, it is because we
2556 needed to escape periods in the name... */
2557 p = name;
2558
2559 while (*p != '\000')
2560 {
2561 if (*p == '-')
2562 *p = '.';
2563 p++;
2564 }
2565
2566 return name;
2567}
2568
2569static char *
fba45db2 2570java_name_of_child (struct varobj *parent, int index)
8b93c638
JM
2571{
2572 char *name, *p;
2573
2574 name = cplus_name_of_child (parent, index);
2575 /* Escape any periods in the name... */
2576 p = name;
2577
2578 while (*p != '\000')
2579 {
2580 if (*p == '.')
2581 *p = '-';
2582 p++;
2583 }
2584
2585 return name;
2586}
2587
30b28db1 2588static struct value *
fba45db2 2589java_value_of_root (struct varobj **var_handle)
8b93c638 2590{
73a93a32 2591 return cplus_value_of_root (var_handle);
8b93c638
JM
2592}
2593
30b28db1 2594static struct value *
fba45db2 2595java_value_of_child (struct varobj *parent, int index)
8b93c638
JM
2596{
2597 return cplus_value_of_child (parent, index);
2598}
2599
2600static struct type *
fba45db2 2601java_type_of_child (struct varobj *parent, int index)
8b93c638
JM
2602{
2603 return cplus_type_of_child (parent, index);
2604}
2605
2606static int
fba45db2 2607java_variable_editable (struct varobj *var)
8b93c638
JM
2608{
2609 return cplus_variable_editable (var);
2610}
2611
2612static char *
fba45db2 2613java_value_of_variable (struct varobj *var)
8b93c638
JM
2614{
2615 return cplus_value_of_variable (var);
2616}
2617\f
2618extern void _initialize_varobj (void);
2619void
2620_initialize_varobj (void)
2621{
2622 int sizeof_table = sizeof (struct vlist *) * VAROBJ_TABLE_SIZE;
2623
2624 varobj_table = xmalloc (sizeof_table);
2625 memset (varobj_table, 0, sizeof_table);
2626
85c07804
AC
2627 add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
2628 &varobjdebug, _("\
2629Set varobj debugging."), _("\
2630Show varobj debugging."), _("\
2631When non-zero, varobj debugging is enabled."),
2632 NULL,
920d2a44 2633 show_varobjdebug,
85c07804 2634 &setlist, &showlist);
8b93c638 2635}
This page took 0.738342 seconds and 4 git commands to generate.