Add tests for PR ld/16452 and PR ld/16457
[platform/upstream/binutils.git] / gdb / c-varobj.c
1 /* varobj support for C and C++.
2
3    Copyright (C) 1999-2014 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include "defs.h"
19 #include "value.h"
20 #include "varobj.h"
21 #include "gdbthread.h"
22 #include "valprint.h"
23
24 static void cplus_class_num_children (struct type *type, int children[3]);
25
26 /* The names of varobjs representing anonymous structs or unions.  */
27 #define ANONYMOUS_STRUCT_NAME _("<anonymous struct>")
28 #define ANONYMOUS_UNION_NAME _("<anonymous union>")
29
30 /* Does CHILD represent a child with no name?  This happens when
31    the child is an anonmous struct or union and it has no field name
32    in its parent variable.
33
34    This has already been determined by *_describe_child. The easiest
35    thing to do is to compare the child's name with ANONYMOUS_*_NAME.  */
36
37 int
38 varobj_is_anonymous_child (struct varobj *child)
39 {
40   return (strcmp (child->name, ANONYMOUS_STRUCT_NAME) == 0
41           || strcmp (child->name, ANONYMOUS_UNION_NAME) == 0);
42 }
43
44 /* Given the value and the type of a variable object,
45    adjust the value and type to those necessary
46    for getting children of the variable object.
47    This includes dereferencing top-level references
48    to all types and dereferencing pointers to
49    structures.
50
51    If LOOKUP_ACTUAL_TYPE is set the enclosing type of the
52    value will be fetched and if it differs from static type
53    the value will be casted to it.
54
55    Both TYPE and *TYPE should be non-null.  VALUE
56    can be null if we want to only translate type.
57    *VALUE can be null as well -- if the parent
58    value is not known.
59
60    If WAS_PTR is not NULL, set *WAS_PTR to 0 or 1
61    depending on whether pointer was dereferenced
62    in this function.  */
63
64 static void
65 adjust_value_for_child_access (struct value **value,
66                                   struct type **type,
67                                   int *was_ptr,
68                                   int lookup_actual_type)
69 {
70   gdb_assert (type && *type);
71
72   if (was_ptr)
73     *was_ptr = 0;
74
75   *type = check_typedef (*type);
76   
77   /* The type of value stored in varobj, that is passed
78      to us, is already supposed to be
79      reference-stripped.  */
80
81   gdb_assert (TYPE_CODE (*type) != TYPE_CODE_REF);
82
83   /* Pointers to structures are treated just like
84      structures when accessing children.  Don't
85      dererences pointers to other types.  */
86   if (TYPE_CODE (*type) == TYPE_CODE_PTR)
87     {
88       struct type *target_type = get_target_type (*type);
89       if (TYPE_CODE (target_type) == TYPE_CODE_STRUCT
90           || TYPE_CODE (target_type) == TYPE_CODE_UNION)
91         {
92           if (value && *value)
93             {
94               volatile struct gdb_exception except;
95
96               TRY_CATCH (except, RETURN_MASK_ERROR)
97                 {
98                   *value = value_ind (*value);
99                 }
100
101               if (except.reason < 0)
102                 *value = NULL;
103             }
104           *type = target_type;
105           if (was_ptr)
106             *was_ptr = 1;
107         }
108     }
109
110   /* The 'get_target_type' function calls check_typedef on
111      result, so we can immediately check type code.  No
112      need to call check_typedef here.  */
113
114   /* Access a real type of the value (if necessary and possible).  */
115   if (value && *value && lookup_actual_type)
116     {
117       struct type *enclosing_type;
118       int real_type_found = 0;
119
120       enclosing_type = value_actual_type (*value, 1, &real_type_found);
121       if (real_type_found)
122         {
123           *type = enclosing_type;
124           *value = value_cast (enclosing_type, *value);
125         }
126     }
127 }
128
129 /* Is VAR a path expression parent, i.e., can it be used to construct
130    a valid path expression?  */
131
132 static int
133 c_is_path_expr_parent (struct varobj *var)
134 {
135   struct type *type;
136
137   /* "Fake" children are not path_expr parents.  */
138   if (CPLUS_FAKE_CHILD (var))
139     return 0;
140
141   type = varobj_get_gdb_type (var);
142
143   /* Anonymous unions and structs are also not path_expr parents.  */
144   if ((TYPE_CODE (type) == TYPE_CODE_STRUCT
145        || TYPE_CODE (type) == TYPE_CODE_UNION)
146       && TYPE_NAME (type) == NULL
147       && TYPE_TAG_NAME (type) == NULL)
148     {
149       struct varobj *parent = var->parent;
150
151       while (parent != NULL && CPLUS_FAKE_CHILD (parent))
152         parent = parent->parent;
153
154       if (parent != NULL)
155         {
156           struct type *parent_type;
157           int was_ptr;
158
159           parent_type = varobj_get_value_type (parent);
160           adjust_value_for_child_access (NULL, &parent_type, &was_ptr, 0);
161
162           if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT
163               || TYPE_CODE (parent_type) == TYPE_CODE_UNION)
164             {
165               const char *field_name;
166
167               gdb_assert (var->index < TYPE_NFIELDS (parent_type));
168               field_name = TYPE_FIELD_NAME (parent_type, var->index);
169               return !(field_name == NULL || *field_name == '\0');
170             }
171         }
172
173       return 0;
174     }
175
176   return 1;
177 }
178
179 /* C */
180
181 static int
182 c_number_of_children (struct varobj *var)
183 {
184   struct type *type = varobj_get_value_type (var);
185   int children = 0;
186   struct type *target;
187
188   adjust_value_for_child_access (NULL, &type, NULL, 0);
189   target = get_target_type (type);
190
191   switch (TYPE_CODE (type))
192     {
193     case TYPE_CODE_ARRAY:
194       if (TYPE_LENGTH (type) > 0 && TYPE_LENGTH (target) > 0
195           && !TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED (type))
196         children = TYPE_LENGTH (type) / TYPE_LENGTH (target);
197       else
198         /* If we don't know how many elements there are, don't display
199            any.  */
200         children = 0;
201       break;
202
203     case TYPE_CODE_STRUCT:
204     case TYPE_CODE_UNION:
205       children = TYPE_NFIELDS (type);
206       break;
207
208     case TYPE_CODE_PTR:
209       /* The type here is a pointer to non-struct.  Typically, pointers
210          have one child, except for function ptrs, which have no children,
211          and except for void*, as we don't know what to show.
212
213          We can show char* so we allow it to be dereferenced.  If you decide
214          to test for it, please mind that a little magic is necessary to
215          properly identify it: char* has TYPE_CODE == TYPE_CODE_INT and 
216          TYPE_NAME == "char".  */
217       if (TYPE_CODE (target) == TYPE_CODE_FUNC
218           || TYPE_CODE (target) == TYPE_CODE_VOID)
219         children = 0;
220       else
221         children = 1;
222       break;
223
224     default:
225       /* Other types have no children.  */
226       break;
227     }
228
229   return children;
230 }
231
232 static char *
233 c_name_of_variable (struct varobj *parent)
234 {
235   return xstrdup (parent->name);
236 }
237
238 /* Return the value of element TYPE_INDEX of a structure
239    value VALUE.  VALUE's type should be a structure,
240    or union, or a typedef to struct/union.
241
242    Returns NULL if getting the value fails.  Never throws.  */
243
244 static struct value *
245 value_struct_element_index (struct value *value, int type_index)
246 {
247   struct value *result = NULL;
248   volatile struct gdb_exception e;
249   struct type *type = value_type (value);
250
251   type = check_typedef (type);
252
253   gdb_assert (TYPE_CODE (type) == TYPE_CODE_STRUCT
254               || TYPE_CODE (type) == TYPE_CODE_UNION);
255
256   TRY_CATCH (e, RETURN_MASK_ERROR)
257     {
258       if (field_is_static (&TYPE_FIELD (type, type_index)))
259         result = value_static_field (type, type_index);
260       else
261         result = value_primitive_field (value, 0, type_index, type);
262     }
263   if (e.reason < 0)
264     {
265       return NULL;
266     }
267   else
268     {
269       return result;
270     }
271 }
272
273 /* Obtain the information about child INDEX of the variable
274    object PARENT.
275    If CNAME is not null, sets *CNAME to the name of the child relative
276    to the parent.
277    If CVALUE is not null, sets *CVALUE to the value of the child.
278    If CTYPE is not null, sets *CTYPE to the type of the child.
279
280    If any of CNAME, CVALUE, or CTYPE is not null, but the corresponding
281    information cannot be determined, set *CNAME, *CVALUE, or *CTYPE
282    to NULL.  */
283
284 static void 
285 c_describe_child (struct varobj *parent, int index,
286                   char **cname, struct value **cvalue, struct type **ctype,
287                   char **cfull_expression)
288 {
289   struct value *value = parent->value;
290   struct type *type = varobj_get_value_type (parent);
291   char *parent_expression = NULL;
292   int was_ptr;
293   volatile struct gdb_exception except;
294
295   if (cname)
296     *cname = NULL;
297   if (cvalue)
298     *cvalue = NULL;
299   if (ctype)
300     *ctype = NULL;
301   if (cfull_expression)
302     {
303       *cfull_expression = NULL;
304       parent_expression
305         = varobj_get_path_expr (varobj_get_path_expr_parent (parent));
306     }
307   adjust_value_for_child_access (&value, &type, &was_ptr, 0);
308       
309   switch (TYPE_CODE (type))
310     {
311     case TYPE_CODE_ARRAY:
312       if (cname)
313         *cname
314           = xstrdup (int_string (index 
315                                  + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
316                                  10, 1, 0, 0));
317
318       if (cvalue && value)
319         {
320           int real_index = index + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type));
321
322           TRY_CATCH (except, RETURN_MASK_ERROR)
323             {
324               *cvalue = value_subscript (value, real_index);
325             }
326         }
327
328       if (ctype)
329         *ctype = get_target_type (type);
330
331       if (cfull_expression)
332         *cfull_expression = 
333           xstrprintf ("(%s)[%s]", parent_expression, 
334                       int_string (index
335                                   + TYPE_LOW_BOUND (TYPE_INDEX_TYPE (type)),
336                                   10, 1, 0, 0));
337
338
339       break;
340
341     case TYPE_CODE_STRUCT:
342     case TYPE_CODE_UNION:
343       {
344         const char *field_name;
345
346         /* If the type is anonymous and the field has no name,
347            set an appropriate name.  */
348         field_name = TYPE_FIELD_NAME (type, index);
349         if (field_name == NULL || *field_name == '\0')
350           {
351             if (cname)
352               {
353                 if (TYPE_CODE (TYPE_FIELD_TYPE (type, index))
354                     == TYPE_CODE_STRUCT)
355                   *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
356                 else
357                   *cname = xstrdup (ANONYMOUS_UNION_NAME);
358               }
359
360             if (cfull_expression)
361               *cfull_expression = xstrdup ("");
362           }
363         else
364           {
365             if (cname)
366               *cname = xstrdup (field_name);
367
368             if (cfull_expression)
369               {
370                 char *join = was_ptr ? "->" : ".";
371
372                 *cfull_expression = xstrprintf ("(%s)%s%s", parent_expression,
373                                                 join, field_name);
374               }
375           }
376
377         if (cvalue && value)
378           {
379             /* For C, varobj index is the same as type index.  */
380             *cvalue = value_struct_element_index (value, index);
381           }
382
383         if (ctype)
384           *ctype = TYPE_FIELD_TYPE (type, index);
385       }
386       break;
387
388     case TYPE_CODE_PTR:
389       if (cname)
390         *cname = xstrprintf ("*%s", parent->name);
391
392       if (cvalue && value)
393         {
394           TRY_CATCH (except, RETURN_MASK_ERROR)
395             {
396               *cvalue = value_ind (value);
397             }
398
399           if (except.reason < 0)
400             *cvalue = NULL;
401         }
402
403       /* Don't use get_target_type because it calls
404          check_typedef and here, we want to show the true
405          declared type of the variable.  */
406       if (ctype)
407         *ctype = TYPE_TARGET_TYPE (type);
408
409       if (cfull_expression)
410         *cfull_expression = xstrprintf ("*(%s)", parent_expression);
411       
412       break;
413
414     default:
415       /* This should not happen.  */
416       if (cname)
417         *cname = xstrdup ("???");
418       if (cfull_expression)
419         *cfull_expression = xstrdup ("???");
420       /* Don't set value and type, we don't know then.  */
421     }
422 }
423
424 static char *
425 c_name_of_child (struct varobj *parent, int index)
426 {
427   char *name;
428
429   c_describe_child (parent, index, &name, NULL, NULL, NULL);
430   return name;
431 }
432
433 static char *
434 c_path_expr_of_child (struct varobj *child)
435 {
436   c_describe_child (child->parent, child->index, NULL, NULL, NULL, 
437                     &child->path_expr);
438   return child->path_expr;
439 }
440
441 static struct value *
442 c_value_of_child (struct varobj *parent, int index)
443 {
444   struct value *value = NULL;
445
446   c_describe_child (parent, index, NULL, &value, NULL, NULL);
447   return value;
448 }
449
450 static struct type *
451 c_type_of_child (struct varobj *parent, int index)
452 {
453   struct type *type = NULL;
454
455   c_describe_child (parent, index, NULL, NULL, &type, NULL);
456   return type;
457 }
458
459 /* This returns the type of the variable.  It also skips past typedefs
460    to return the real type of the variable.  */
461
462 static struct type *
463 get_type (struct varobj *var)
464 {
465   struct type *type;
466
467   type = var->type;
468   if (type != NULL)
469     type = check_typedef (type);
470
471   return type;
472 }
473
474 static char *
475 c_value_of_variable (struct varobj *var, enum varobj_display_formats format)
476 {
477   /* BOGUS: if val_print sees a struct/class, or a reference to one,
478      it will print out its children instead of "{...}".  So we need to
479      catch that case explicitly.  */
480   struct type *type = get_type (var);
481
482   /* Strip top-level references.  */
483   while (TYPE_CODE (type) == TYPE_CODE_REF)
484     type = check_typedef (TYPE_TARGET_TYPE (type));
485
486   switch (TYPE_CODE (type))
487     {
488     case TYPE_CODE_STRUCT:
489     case TYPE_CODE_UNION:
490       return xstrdup ("{...}");
491       /* break; */
492
493     case TYPE_CODE_ARRAY:
494       {
495         char *number;
496
497         number = xstrprintf ("[%d]", var->num_children);
498         return (number);
499       }
500       /* break; */
501
502     default:
503       {
504         if (var->value == NULL)
505           {
506             /* This can happen if we attempt to get the value of a struct
507                member when the parent is an invalid pointer.  This is an
508                error condition, so we should tell the caller.  */
509             return NULL;
510           }
511         else
512           {
513             if (var->not_fetched && value_lazy (var->value))
514               /* Frozen variable and no value yet.  We don't
515                  implicitly fetch the value.  MI response will
516                  use empty string for the value, which is OK.  */
517               return NULL;
518
519             gdb_assert (varobj_value_is_changeable_p (var));
520             gdb_assert (!value_lazy (var->value));
521             
522             /* If the specified format is the current one,
523                we can reuse print_value.  */
524             if (format == var->format)
525               return xstrdup (var->print_value);
526             else
527               return varobj_value_get_print_value (var->value, format, var);
528           }
529       }
530     }
531 }
532 \f
533
534 /* varobj operations for c.  */
535
536 const struct lang_varobj_ops c_varobj_ops =
537 {
538    c_number_of_children,
539    c_name_of_variable,
540    c_name_of_child,
541    c_path_expr_of_child,
542    c_value_of_child,
543    c_type_of_child,
544    c_value_of_variable,
545    varobj_default_value_is_changeable_p,
546    NULL, /* value_has_mutated */
547    c_is_path_expr_parent  /* is_path_expr_parent */
548 };
549
550 /* A little convenience enum for dealing with C++/Java.  */
551 enum vsections
552 {
553   v_public = 0, v_private, v_protected
554 };
555
556 /* C++ */
557
558 static int
559 cplus_number_of_children (struct varobj *var)
560 {
561   struct value *value = NULL;
562   struct type *type;
563   int children, dont_know;
564   int lookup_actual_type = 0;
565   struct value_print_options opts;
566
567   dont_know = 1;
568   children = 0;
569
570   get_user_print_options (&opts);
571
572   if (!CPLUS_FAKE_CHILD (var))
573     {
574       type = varobj_get_value_type (var);
575
576       /* It is necessary to access a real type (via RTTI).  */
577       if (opts.objectprint)
578         {
579           value = var->value;
580           lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
581                                 || TYPE_CODE (var->type) == TYPE_CODE_PTR);
582         }
583       adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
584
585       if (((TYPE_CODE (type)) == TYPE_CODE_STRUCT)
586           || ((TYPE_CODE (type)) == TYPE_CODE_UNION))
587         {
588           int kids[3];
589
590           cplus_class_num_children (type, kids);
591           if (kids[v_public] != 0)
592             children++;
593           if (kids[v_private] != 0)
594             children++;
595           if (kids[v_protected] != 0)
596             children++;
597
598           /* Add any baseclasses.  */
599           children += TYPE_N_BASECLASSES (type);
600           dont_know = 0;
601
602           /* FIXME: save children in var.  */
603         }
604     }
605   else
606     {
607       int kids[3];
608
609       type = varobj_get_value_type (var->parent);
610
611       /* It is necessary to access a real type (via RTTI).  */
612       if (opts.objectprint)
613         {
614           struct varobj *parent = var->parent;
615
616           value = parent->value;
617           lookup_actual_type = (TYPE_CODE (parent->type) == TYPE_CODE_REF
618                                 || TYPE_CODE (parent->type) == TYPE_CODE_PTR);
619         }
620       adjust_value_for_child_access (&value, &type, NULL, lookup_actual_type);
621
622       cplus_class_num_children (type, kids);
623       if (strcmp (var->name, "public") == 0)
624         children = kids[v_public];
625       else if (strcmp (var->name, "private") == 0)
626         children = kids[v_private];
627       else
628         children = kids[v_protected];
629       dont_know = 0;
630     }
631
632   if (dont_know)
633     children = c_number_of_children (var);
634
635   return children;
636 }
637
638 /* Compute # of public, private, and protected variables in this class.
639    That means we need to descend into all baseclasses and find out
640    how many are there, too.  */
641
642 static void
643 cplus_class_num_children (struct type *type, int children[3])
644 {
645   int i, vptr_fieldno;
646   struct type *basetype = NULL;
647
648   children[v_public] = 0;
649   children[v_private] = 0;
650   children[v_protected] = 0;
651
652   vptr_fieldno = get_vptr_fieldno (type, &basetype);
653   for (i = TYPE_N_BASECLASSES (type); i < TYPE_NFIELDS (type); i++)
654     {
655       /* If we have a virtual table pointer, omit it.  Even if virtual
656          table pointers are not specifically marked in the debug info,
657          they should be artificial.  */
658       if ((type == basetype && i == vptr_fieldno)
659           || TYPE_FIELD_ARTIFICIAL (type, i))
660         continue;
661
662       if (TYPE_FIELD_PROTECTED (type, i))
663         children[v_protected]++;
664       else if (TYPE_FIELD_PRIVATE (type, i))
665         children[v_private]++;
666       else
667         children[v_public]++;
668     }
669 }
670
671 static char *
672 cplus_name_of_variable (struct varobj *parent)
673 {
674   return c_name_of_variable (parent);
675 }
676
677 enum accessibility { private_field, protected_field, public_field };
678
679 /* Check if field INDEX of TYPE has the specified accessibility.
680    Return 0 if so and 1 otherwise.  */
681
682 static int 
683 match_accessibility (struct type *type, int index, enum accessibility acc)
684 {
685   if (acc == private_field && TYPE_FIELD_PRIVATE (type, index))
686     return 1;
687   else if (acc == protected_field && TYPE_FIELD_PROTECTED (type, index))
688     return 1;
689   else if (acc == public_field && !TYPE_FIELD_PRIVATE (type, index)
690            && !TYPE_FIELD_PROTECTED (type, index))
691     return 1;
692   else
693     return 0;
694 }
695
696 static void
697 cplus_describe_child (struct varobj *parent, int index,
698                       char **cname, struct value **cvalue, struct type **ctype,
699                       char **cfull_expression)
700 {
701   struct value *value;
702   struct type *type;
703   int was_ptr;
704   int lookup_actual_type = 0;
705   char *parent_expression = NULL;
706   struct varobj *var;
707   struct value_print_options opts;
708
709   if (cname)
710     *cname = NULL;
711   if (cvalue)
712     *cvalue = NULL;
713   if (ctype)
714     *ctype = NULL;
715   if (cfull_expression)
716     *cfull_expression = NULL;
717
718   get_user_print_options (&opts);
719
720   var = (CPLUS_FAKE_CHILD (parent)) ? parent->parent : parent;
721   if (opts.objectprint)
722     lookup_actual_type = (TYPE_CODE (var->type) == TYPE_CODE_REF
723                           || TYPE_CODE (var->type) == TYPE_CODE_PTR);
724   value = var->value;
725   type = varobj_get_value_type (var);
726   if (cfull_expression)
727     parent_expression
728       = varobj_get_path_expr (varobj_get_path_expr_parent (var));
729
730   adjust_value_for_child_access (&value, &type, &was_ptr, lookup_actual_type);
731
732   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
733       || TYPE_CODE (type) == TYPE_CODE_UNION)
734     {
735       char *join = was_ptr ? "->" : ".";
736
737       if (CPLUS_FAKE_CHILD (parent))
738         {
739           /* The fields of the class type are ordered as they
740              appear in the class.  We are given an index for a
741              particular access control type ("public","protected",
742              or "private").  We must skip over fields that don't
743              have the access control we are looking for to properly
744              find the indexed field.  */
745           int type_index = TYPE_N_BASECLASSES (type);
746           enum accessibility acc = public_field;
747           int vptr_fieldno;
748           struct type *basetype = NULL;
749           const char *field_name;
750
751           vptr_fieldno = get_vptr_fieldno (type, &basetype);
752           if (strcmp (parent->name, "private") == 0)
753             acc = private_field;
754           else if (strcmp (parent->name, "protected") == 0)
755             acc = protected_field;
756
757           while (index >= 0)
758             {
759               if ((type == basetype && type_index == vptr_fieldno)
760                   || TYPE_FIELD_ARTIFICIAL (type, type_index))
761                 ; /* ignore vptr */
762               else if (match_accessibility (type, type_index, acc))
763                     --index;
764                   ++type_index;
765             }
766           --type_index;
767
768           /* If the type is anonymous and the field has no name,
769              set an appopriate name.  */
770           field_name = TYPE_FIELD_NAME (type, type_index);
771           if (field_name == NULL || *field_name == '\0')
772             {
773               if (cname)
774                 {
775                   if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
776                       == TYPE_CODE_STRUCT)
777                     *cname = xstrdup (ANONYMOUS_STRUCT_NAME);
778                   else if (TYPE_CODE (TYPE_FIELD_TYPE (type, type_index))
779                            == TYPE_CODE_UNION)
780                     *cname = xstrdup (ANONYMOUS_UNION_NAME);
781                 }
782
783               if (cfull_expression)
784                 *cfull_expression = xstrdup ("");
785             }
786           else
787             {
788               if (cname)
789                 *cname = xstrdup (TYPE_FIELD_NAME (type, type_index));
790
791               if (cfull_expression)
792                 *cfull_expression
793                   = xstrprintf ("((%s)%s%s)", parent_expression, join,
794                                 field_name);
795             }
796
797           if (cvalue && value)
798             *cvalue = value_struct_element_index (value, type_index);
799
800           if (ctype)
801             *ctype = TYPE_FIELD_TYPE (type, type_index);
802         }
803       else if (index < TYPE_N_BASECLASSES (type))
804         {
805           /* This is a baseclass.  */
806           if (cname)
807             *cname = xstrdup (TYPE_FIELD_NAME (type, index));
808
809           if (cvalue && value)
810             *cvalue = value_cast (TYPE_FIELD_TYPE (type, index), value);
811
812           if (ctype)
813             {
814               *ctype = TYPE_FIELD_TYPE (type, index);
815             }
816
817           if (cfull_expression)
818             {
819               char *ptr = was_ptr ? "*" : "";
820
821               /* Cast the parent to the base' type.  Note that in gdb,
822                  expression like 
823                          (Base1)d
824                  will create an lvalue, for all appearences, so we don't
825                  need to use more fancy:
826                          *(Base1*)(&d)
827                  construct.
828
829                  When we are in the scope of the base class or of one
830                  of its children, the type field name will be interpreted
831                  as a constructor, if it exists.  Therefore, we must
832                  indicate that the name is a class name by using the
833                  'class' keyword.  See PR mi/11912  */
834               *cfull_expression = xstrprintf ("(%s(class %s%s) %s)", 
835                                               ptr, 
836                                               TYPE_FIELD_NAME (type, index),
837                                               ptr,
838                                               parent_expression);
839             }
840         }
841       else
842         {
843           char *access = NULL;
844           int children[3];
845
846           cplus_class_num_children (type, children);
847
848           /* Everything beyond the baseclasses can
849              only be "public", "private", or "protected"
850
851              The special "fake" children are always output by varobj in
852              this order.  So if INDEX == 2, it MUST be "protected".  */
853           index -= TYPE_N_BASECLASSES (type);
854           switch (index)
855             {
856             case 0:
857               if (children[v_public] > 0)
858                 access = "public";
859               else if (children[v_private] > 0)
860                 access = "private";
861               else 
862                 access = "protected";
863               break;
864             case 1:
865               if (children[v_public] > 0)
866                 {
867                   if (children[v_private] > 0)
868                     access = "private";
869                   else
870                     access = "protected";
871                 }
872               else if (children[v_private] > 0)
873                 access = "protected";
874               break;
875             case 2:
876               /* Must be protected.  */
877               access = "protected";
878               break;
879             default:
880               /* error!  */
881               break;
882             }
883
884           gdb_assert (access);
885           if (cname)
886             *cname = xstrdup (access);
887
888           /* Value and type and full expression are null here.  */
889         }
890     }
891   else
892     {
893       c_describe_child (parent, index, cname, cvalue, ctype, cfull_expression);
894     }  
895 }
896
897 static char *
898 cplus_name_of_child (struct varobj *parent, int index)
899 {
900   char *name = NULL;
901
902   cplus_describe_child (parent, index, &name, NULL, NULL, NULL);
903   return name;
904 }
905
906 static char *
907 cplus_path_expr_of_child (struct varobj *child)
908 {
909   cplus_describe_child (child->parent, child->index, NULL, NULL, NULL, 
910                         &child->path_expr);
911   return child->path_expr;
912 }
913
914 static struct value *
915 cplus_value_of_child (struct varobj *parent, int index)
916 {
917   struct value *value = NULL;
918
919   cplus_describe_child (parent, index, NULL, &value, NULL, NULL);
920   return value;
921 }
922
923 static struct type *
924 cplus_type_of_child (struct varobj *parent, int index)
925 {
926   struct type *type = NULL;
927
928   cplus_describe_child (parent, index, NULL, NULL, &type, NULL);
929   return type;
930 }
931
932 static char *
933 cplus_value_of_variable (struct varobj *var, 
934                          enum varobj_display_formats format)
935 {
936
937   /* If we have one of our special types, don't print out
938      any value.  */
939   if (CPLUS_FAKE_CHILD (var))
940     return xstrdup ("");
941
942   return c_value_of_variable (var, format);
943 }
944 \f
945
946 /* varobj operations for c++.  */
947
948 const struct lang_varobj_ops cplus_varobj_ops =
949 {
950    cplus_number_of_children,
951    cplus_name_of_variable,
952    cplus_name_of_child,
953    cplus_path_expr_of_child,
954    cplus_value_of_child,
955    cplus_type_of_child,
956    cplus_value_of_variable,
957    varobj_default_value_is_changeable_p,
958    NULL, /* value_has_mutated */
959    c_is_path_expr_parent  /* is_path_expr_parent */
960 };
961
962 \f