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