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