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