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