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