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