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