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