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