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