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