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