Commit | Line | Data |
---|---|---|
181875a4 JB |
1 | /* varobj support for Ada. |
2 | ||
42a4f53d | 3 | Copyright (C) 2012-2019 Free Software Foundation, Inc. |
181875a4 JB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "defs.h" | |
d55e5aa6 TT |
21 | |
22 | /* Local non-gdb includes. */ | |
181875a4 JB |
23 | #include "ada-lang.h" |
24 | #include "language.h" | |
25 | #include "valprint.h" | |
d55e5aa6 | 26 | #include "varobj.h" |
181875a4 JB |
27 | |
28 | /* Implementation principle used in this unit: | |
29 | ||
30 | For our purposes, the meat of the varobj object is made of two | |
31 | elements: The varobj's (struct) value, and the varobj's (struct) | |
32 | type. In most situations, the varobj has a non-NULL value, and | |
33 | the type becomes redundant, as it can be directly derived from | |
34 | the value. In the initial implementation of this unit, most | |
35 | routines would only take a value, and return a value. | |
36 | ||
37 | But there are many situations where it is possible for a varobj | |
38 | to have a NULL value. For instance, if the varobj becomes out of | |
39 | scope. Or better yet, when the varobj is the child of another | |
40 | NULL pointer varobj. In that situation, we must rely on the type | |
41 | instead of the value to create the child varobj. | |
42 | ||
43 | That's why most functions below work with a (value, type) pair. | |
44 | The value may or may not be NULL. But the type is always expected | |
45 | to be set. When the value is NULL, then we work with the type | |
46 | alone, and keep the value NULL. But when the value is not NULL, | |
47 | then we work using the value, because it provides more information. | |
48 | But we still always set the type as well, even if that type could | |
49 | easily be derived from the value. The reason behind this is that | |
50 | it allows the code to use the type without having to worry about | |
51 | it being set or not. It makes the code clearer. */ | |
52 | ||
c4124bf1 YQ |
53 | static int ada_varobj_get_number_of_children (struct value *parent_value, |
54 | struct type *parent_type); | |
55 | ||
181875a4 JB |
56 | /* A convenience function that decodes the VALUE_PTR/TYPE_PTR couple: |
57 | If there is a value (*VALUE_PTR not NULL), then perform the decoding | |
58 | using it, and compute the associated type from the resulting value. | |
59 | Otherwise, compute a static approximation of *TYPE_PTR, leaving | |
60 | *VALUE_PTR unchanged. | |
61 | ||
62 | The results are written in place. */ | |
63 | ||
64 | static void | |
65 | ada_varobj_decode_var (struct value **value_ptr, struct type **type_ptr) | |
66 | { | |
67 | if (*value_ptr) | |
68 | { | |
69 | *value_ptr = ada_get_decoded_value (*value_ptr); | |
70 | *type_ptr = ada_check_typedef (value_type (*value_ptr)); | |
71 | } | |
72 | else | |
73 | *type_ptr = ada_get_decoded_type (*type_ptr); | |
74 | } | |
75 | ||
76 | /* Return a string containing an image of the given scalar value. | |
77 | VAL is the numeric value, while TYPE is the value's type. | |
78 | This is useful for plain integers, of course, but even more | |
2f408ecb | 79 | so for enumerated types. */ |
181875a4 | 80 | |
2f408ecb | 81 | static std::string |
181875a4 JB |
82 | ada_varobj_scalar_image (struct type *type, LONGEST val) |
83 | { | |
d7e74731 | 84 | string_file buf; |
181875a4 | 85 | |
d7e74731 PA |
86 | ada_print_scalar (type, val, &buf); |
87 | return std::move (buf.string ()); | |
181875a4 JB |
88 | } |
89 | ||
90 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates | |
91 | a struct or union, compute the (CHILD_VALUE, CHILD_TYPE) couple | |
92 | corresponding to the field number FIELDNO. */ | |
93 | ||
94 | static void | |
95 | ada_varobj_struct_elt (struct value *parent_value, | |
96 | struct type *parent_type, | |
97 | int fieldno, | |
98 | struct value **child_value, | |
99 | struct type **child_type) | |
100 | { | |
101 | struct value *value = NULL; | |
102 | struct type *type = NULL; | |
103 | ||
104 | if (parent_value) | |
105 | { | |
106 | value = value_field (parent_value, fieldno); | |
107 | type = value_type (value); | |
108 | } | |
109 | else | |
110 | type = TYPE_FIELD_TYPE (parent_type, fieldno); | |
111 | ||
112 | if (child_value) | |
113 | *child_value = value; | |
114 | if (child_type) | |
115 | *child_type = type; | |
116 | } | |
117 | ||
118 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a pointer or | |
119 | reference, return a (CHILD_VALUE, CHILD_TYPE) couple corresponding | |
120 | to the dereferenced value. */ | |
121 | ||
122 | static void | |
123 | ada_varobj_ind (struct value *parent_value, | |
124 | struct type *parent_type, | |
125 | struct value **child_value, | |
126 | struct type **child_type) | |
127 | { | |
128 | struct value *value = NULL; | |
129 | struct type *type = NULL; | |
130 | ||
131 | if (ada_is_array_descriptor_type (parent_type)) | |
132 | { | |
133 | /* This can only happen when PARENT_VALUE is NULL. Otherwise, | |
134 | ada_get_decoded_value would have transformed our parent_type | |
135 | into a simple array pointer type. */ | |
136 | gdb_assert (parent_value == NULL); | |
137 | gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF); | |
138 | ||
139 | /* Decode parent_type by the equivalent pointer to (decoded) | |
140 | array. */ | |
141 | while (TYPE_CODE (parent_type) == TYPE_CODE_TYPEDEF) | |
142 | parent_type = TYPE_TARGET_TYPE (parent_type); | |
143 | parent_type = ada_coerce_to_simple_array_type (parent_type); | |
144 | parent_type = lookup_pointer_type (parent_type); | |
145 | } | |
146 | ||
147 | /* If parent_value is a null pointer, then only perform static | |
148 | dereferencing. We cannot dereference null pointers. */ | |
149 | if (parent_value && value_as_address (parent_value) == 0) | |
150 | parent_value = NULL; | |
151 | ||
152 | if (parent_value) | |
153 | { | |
154 | value = ada_value_ind (parent_value); | |
155 | type = value_type (value); | |
156 | } | |
157 | else | |
158 | type = TYPE_TARGET_TYPE (parent_type); | |
159 | ||
160 | if (child_value) | |
161 | *child_value = value; | |
162 | if (child_type) | |
163 | *child_type = type; | |
164 | } | |
165 | ||
166 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a simple | |
167 | array (TYPE_CODE_ARRAY), return the (CHILD_VALUE, CHILD_TYPE) | |
168 | pair corresponding to the element at ELT_INDEX. */ | |
169 | ||
170 | static void | |
171 | ada_varobj_simple_array_elt (struct value *parent_value, | |
172 | struct type *parent_type, | |
173 | int elt_index, | |
174 | struct value **child_value, | |
175 | struct type **child_type) | |
176 | { | |
177 | struct value *value = NULL; | |
178 | struct type *type = NULL; | |
179 | ||
180 | if (parent_value) | |
181 | { | |
182 | struct value *index_value = | |
183 | value_from_longest (TYPE_INDEX_TYPE (parent_type), elt_index); | |
184 | ||
185 | value = ada_value_subscript (parent_value, 1, &index_value); | |
186 | type = value_type (value); | |
187 | } | |
188 | else | |
189 | type = TYPE_TARGET_TYPE (parent_type); | |
190 | ||
191 | if (child_value) | |
192 | *child_value = value; | |
193 | if (child_type) | |
194 | *child_type = type; | |
195 | } | |
196 | ||
197 | /* Given the decoded value and decoded type of a variable object, | |
198 | adjust the value and type to those necessary for getting children | |
199 | of the variable object. | |
200 | ||
201 | The replacement is performed in place. */ | |
202 | ||
203 | static void | |
204 | ada_varobj_adjust_for_child_access (struct value **value, | |
205 | struct type **type) | |
206 | { | |
207 | /* Pointers to struct/union types are special: Instead of having | |
208 | one child (the struct), their children are the components of | |
209 | the struct/union type. We handle this situation by dereferencing | |
210 | the (value, type) couple. */ | |
211 | if (TYPE_CODE (*type) == TYPE_CODE_PTR | |
212 | && (TYPE_CODE (TYPE_TARGET_TYPE (*type)) == TYPE_CODE_STRUCT | |
213 | || TYPE_CODE (TYPE_TARGET_TYPE (*type)) == TYPE_CODE_UNION) | |
214 | && !ada_is_array_descriptor_type (TYPE_TARGET_TYPE (*type)) | |
215 | && !ada_is_constrained_packed_array_type (TYPE_TARGET_TYPE (*type))) | |
216 | ada_varobj_ind (*value, *type, value, type); | |
f30b8b38 JB |
217 | |
218 | /* If this is a tagged type, we need to transform it a bit in order | |
219 | to be able to fetch its full view. As always with tagged types, | |
220 | we can only do that if we have a value. */ | |
221 | if (*value != NULL && ada_is_tagged_type (*type, 1)) | |
222 | { | |
223 | *value = ada_tag_value_at_base_address (*value); | |
224 | *type = value_type (*value); | |
225 | } | |
181875a4 JB |
226 | } |
227 | ||
228 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is an array | |
229 | (any type of array, "simple" or not), return the number of children | |
230 | that this array contains. */ | |
231 | ||
232 | static int | |
233 | ada_varobj_get_array_number_of_children (struct value *parent_value, | |
234 | struct type *parent_type) | |
235 | { | |
236 | LONGEST lo, hi; | |
181875a4 | 237 | |
4a0ca9ec JB |
238 | if (parent_value == NULL |
239 | && is_dynamic_type (TYPE_INDEX_TYPE (parent_type))) | |
240 | { | |
241 | /* This happens when listing the children of an object | |
242 | which does not exist in memory (Eg: when requesting | |
243 | the children of a null pointer, which is allowed by | |
244 | varobj). The array index type being dynamic, we cannot | |
245 | determine how many elements this array has. Just assume | |
246 | it has none. */ | |
247 | return 0; | |
248 | } | |
249 | ||
181875a4 JB |
250 | if (!get_array_bounds (parent_type, &lo, &hi)) |
251 | { | |
252 | /* Could not get the array bounds. Pretend this is an empty array. */ | |
253 | warning (_("unable to get bounds of array, assuming null array")); | |
254 | return 0; | |
255 | } | |
256 | ||
257 | /* Ada allows the upper bound to be less than the lower bound, | |
258 | in order to specify empty arrays... */ | |
259 | if (hi < lo) | |
260 | return 0; | |
261 | ||
262 | return hi - lo + 1; | |
263 | } | |
264 | ||
265 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair is a struct or | |
266 | union, return the number of children this struct contains. */ | |
267 | ||
268 | static int | |
269 | ada_varobj_get_struct_number_of_children (struct value *parent_value, | |
270 | struct type *parent_type) | |
271 | { | |
272 | int n_children = 0; | |
273 | int i; | |
274 | ||
275 | gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT | |
276 | || TYPE_CODE (parent_type) == TYPE_CODE_UNION); | |
277 | ||
278 | for (i = 0; i < TYPE_NFIELDS (parent_type); i++) | |
279 | { | |
280 | if (ada_is_ignored_field (parent_type, i)) | |
281 | continue; | |
282 | ||
283 | if (ada_is_wrapper_field (parent_type, i)) | |
284 | { | |
285 | struct value *elt_value; | |
286 | struct type *elt_type; | |
287 | ||
288 | ada_varobj_struct_elt (parent_value, parent_type, i, | |
289 | &elt_value, &elt_type); | |
290 | if (ada_is_tagged_type (elt_type, 0)) | |
291 | { | |
292 | /* We must not use ada_varobj_get_number_of_children | |
293 | to determine is element's number of children, because | |
294 | this function first calls ada_varobj_decode_var, | |
295 | which "fixes" the element. For tagged types, this | |
296 | includes reading the object's tag to determine its | |
297 | real type, which happens to be the parent_type, and | |
298 | leads to an infinite loop (because the element gets | |
299 | fixed back into the parent). */ | |
300 | n_children += ada_varobj_get_struct_number_of_children | |
301 | (elt_value, elt_type); | |
302 | } | |
303 | else | |
304 | n_children += ada_varobj_get_number_of_children (elt_value, elt_type); | |
305 | } | |
306 | else if (ada_is_variant_part (parent_type, i)) | |
307 | { | |
308 | /* In normal situations, the variant part of the record should | |
309 | have been "fixed". Or, in other words, it should have been | |
310 | replaced by the branch of the variant part that is relevant | |
311 | for our value. But there are still situations where this | |
312 | can happen, however (Eg. when our parent is a NULL pointer). | |
313 | We do not support showing this part of the record for now, | |
314 | so just pretend this field does not exist. */ | |
315 | } | |
316 | else | |
317 | n_children++; | |
318 | } | |
319 | ||
320 | return n_children; | |
321 | } | |
322 | ||
323 | /* Assuming that the (PARENT_VALUE, PARENT_TYPE) pair designates | |
324 | a pointer, return the number of children this pointer has. */ | |
325 | ||
326 | static int | |
327 | ada_varobj_get_ptr_number_of_children (struct value *parent_value, | |
328 | struct type *parent_type) | |
329 | { | |
330 | struct type *child_type = TYPE_TARGET_TYPE (parent_type); | |
331 | ||
332 | /* Pointer to functions and to void do not have a child, since | |
333 | you cannot print what they point to. */ | |
334 | if (TYPE_CODE (child_type) == TYPE_CODE_FUNC | |
335 | || TYPE_CODE (child_type) == TYPE_CODE_VOID) | |
336 | return 0; | |
337 | ||
338 | /* All other types have 1 child. */ | |
339 | return 1; | |
340 | } | |
341 | ||
342 | /* Return the number of children for the (PARENT_VALUE, PARENT_TYPE) | |
343 | pair. */ | |
344 | ||
c4124bf1 | 345 | static int |
181875a4 JB |
346 | ada_varobj_get_number_of_children (struct value *parent_value, |
347 | struct type *parent_type) | |
348 | { | |
349 | ada_varobj_decode_var (&parent_value, &parent_type); | |
350 | ada_varobj_adjust_for_child_access (&parent_value, &parent_type); | |
351 | ||
352 | /* A typedef to an array descriptor in fact represents a pointer | |
353 | to an unconstrained array. These types always have one child | |
354 | (the unconstrained array). */ | |
d91e9ea8 | 355 | if (ada_is_access_to_unconstrained_array (parent_type)) |
181875a4 JB |
356 | return 1; |
357 | ||
358 | if (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY) | |
359 | return ada_varobj_get_array_number_of_children (parent_value, | |
360 | parent_type); | |
361 | ||
362 | if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT | |
363 | || TYPE_CODE (parent_type) == TYPE_CODE_UNION) | |
364 | return ada_varobj_get_struct_number_of_children (parent_value, | |
365 | parent_type); | |
366 | ||
367 | if (TYPE_CODE (parent_type) == TYPE_CODE_PTR) | |
368 | return ada_varobj_get_ptr_number_of_children (parent_value, | |
369 | parent_type); | |
370 | ||
371 | /* All other types have no child. */ | |
372 | return 0; | |
373 | } | |
374 | ||
375 | /* Describe the child of the (PARENT_VALUE, PARENT_TYPE) pair | |
376 | whose index is CHILD_INDEX: | |
377 | ||
378 | - If CHILD_NAME is not NULL, then a copy of the child's name | |
379 | is saved in *CHILD_NAME. This copy must be deallocated | |
380 | with xfree after use. | |
381 | ||
382 | - If CHILD_VALUE is not NULL, then save the child's value | |
383 | in *CHILD_VALUE. Same thing for the child's type with | |
384 | CHILD_TYPE if not NULL. | |
385 | ||
386 | - If CHILD_PATH_EXPR is not NULL, then compute the child's | |
387 | path expression. The resulting string must be deallocated | |
388 | after use with xfree. | |
389 | ||
390 | Computing the child's path expression requires the PARENT_PATH_EXPR | |
391 | to be non-NULL. Otherwise, PARENT_PATH_EXPR may be null if | |
392 | CHILD_PATH_EXPR is NULL. | |
393 | ||
394 | PARENT_NAME is the name of the parent, and should never be NULL. */ | |
395 | ||
396 | static void ada_varobj_describe_child (struct value *parent_value, | |
397 | struct type *parent_type, | |
398 | const char *parent_name, | |
399 | const char *parent_path_expr, | |
400 | int child_index, | |
2f408ecb | 401 | std::string *child_name, |
181875a4 JB |
402 | struct value **child_value, |
403 | struct type **child_type, | |
2f408ecb | 404 | std::string *child_path_expr); |
181875a4 JB |
405 | |
406 | /* Same as ada_varobj_describe_child, but limited to struct/union | |
407 | objects. */ | |
408 | ||
409 | static void | |
410 | ada_varobj_describe_struct_child (struct value *parent_value, | |
411 | struct type *parent_type, | |
412 | const char *parent_name, | |
413 | const char *parent_path_expr, | |
414 | int child_index, | |
2f408ecb | 415 | std::string *child_name, |
181875a4 JB |
416 | struct value **child_value, |
417 | struct type **child_type, | |
2f408ecb | 418 | std::string *child_path_expr) |
181875a4 JB |
419 | { |
420 | int fieldno; | |
421 | int childno = 0; | |
422 | ||
2963898f XR |
423 | gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT |
424 | || TYPE_CODE (parent_type) == TYPE_CODE_UNION); | |
181875a4 JB |
425 | |
426 | for (fieldno = 0; fieldno < TYPE_NFIELDS (parent_type); fieldno++) | |
427 | { | |
428 | if (ada_is_ignored_field (parent_type, fieldno)) | |
429 | continue; | |
430 | ||
431 | if (ada_is_wrapper_field (parent_type, fieldno)) | |
432 | { | |
433 | struct value *elt_value; | |
434 | struct type *elt_type; | |
435 | int elt_n_children; | |
436 | ||
437 | ada_varobj_struct_elt (parent_value, parent_type, fieldno, | |
438 | &elt_value, &elt_type); | |
439 | if (ada_is_tagged_type (elt_type, 0)) | |
440 | { | |
441 | /* Same as in ada_varobj_get_struct_number_of_children: | |
442 | For tagged types, we must be careful to not call | |
443 | ada_varobj_get_number_of_children, to prevent our | |
444 | element from being fixed back into the parent. */ | |
445 | elt_n_children = ada_varobj_get_struct_number_of_children | |
446 | (elt_value, elt_type); | |
447 | } | |
448 | else | |
449 | elt_n_children = | |
450 | ada_varobj_get_number_of_children (elt_value, elt_type); | |
451 | ||
452 | /* Is the child we're looking for one of the children | |
453 | of this wrapper field? */ | |
454 | if (child_index - childno < elt_n_children) | |
455 | { | |
456 | if (ada_is_tagged_type (elt_type, 0)) | |
457 | { | |
458 | /* Same as in ada_varobj_get_struct_number_of_children: | |
459 | For tagged types, we must be careful to not call | |
460 | ada_varobj_describe_child, to prevent our element | |
461 | from being fixed back into the parent. */ | |
462 | ada_varobj_describe_struct_child | |
463 | (elt_value, elt_type, parent_name, parent_path_expr, | |
464 | child_index - childno, child_name, child_value, | |
465 | child_type, child_path_expr); | |
466 | } | |
467 | else | |
468 | ada_varobj_describe_child (elt_value, elt_type, | |
469 | parent_name, parent_path_expr, | |
470 | child_index - childno, | |
471 | child_name, child_value, | |
472 | child_type, child_path_expr); | |
473 | return; | |
474 | } | |
475 | ||
476 | /* The child we're looking for is beyond this wrapper | |
477 | field, so skip all its children. */ | |
478 | childno += elt_n_children; | |
479 | continue; | |
480 | } | |
481 | else if (ada_is_variant_part (parent_type, fieldno)) | |
482 | { | |
483 | /* In normal situations, the variant part of the record should | |
484 | have been "fixed". Or, in other words, it should have been | |
485 | replaced by the branch of the variant part that is relevant | |
486 | for our value. But there are still situations where this | |
487 | can happen, however (Eg. when our parent is a NULL pointer). | |
488 | We do not support showing this part of the record for now, | |
489 | so just pretend this field does not exist. */ | |
490 | continue; | |
491 | } | |
492 | ||
493 | if (childno == child_index) | |
494 | { | |
495 | if (child_name) | |
496 | { | |
497 | /* The name of the child is none other than the field's | |
498 | name, except that we need to strip suffixes from it. | |
499 | For instance, fields with alignment constraints will | |
500 | have an __XVA suffix added to them. */ | |
501 | const char *field_name = TYPE_FIELD_NAME (parent_type, fieldno); | |
502 | int child_name_len = ada_name_prefix_len (field_name); | |
503 | ||
2f408ecb | 504 | *child_name = string_printf ("%.*s", child_name_len, field_name); |
181875a4 JB |
505 | } |
506 | ||
507 | if (child_value && parent_value) | |
508 | ada_varobj_struct_elt (parent_value, parent_type, fieldno, | |
509 | child_value, NULL); | |
510 | ||
511 | if (child_type) | |
512 | ada_varobj_struct_elt (parent_value, parent_type, fieldno, | |
513 | NULL, child_type); | |
514 | ||
515 | if (child_path_expr) | |
516 | { | |
517 | /* The name of the child is none other than the field's | |
518 | name, except that we need to strip suffixes from it. | |
519 | For instance, fields with alignment constraints will | |
520 | have an __XVA suffix added to them. */ | |
521 | const char *field_name = TYPE_FIELD_NAME (parent_type, fieldno); | |
522 | int child_name_len = ada_name_prefix_len (field_name); | |
523 | ||
524 | *child_path_expr = | |
2f408ecb PA |
525 | string_printf ("(%s).%.*s", parent_path_expr, |
526 | child_name_len, field_name); | |
181875a4 JB |
527 | } |
528 | ||
529 | return; | |
530 | } | |
531 | ||
532 | childno++; | |
533 | } | |
534 | ||
535 | /* Something went wrong. Either we miscounted the number of | |
536 | children, or CHILD_INDEX was too high. But we should never | |
537 | reach here. We don't have enough information to recover | |
538 | nicely, so just raise an assertion failure. */ | |
539 | gdb_assert_not_reached ("unexpected code path"); | |
540 | } | |
541 | ||
542 | /* Same as ada_varobj_describe_child, but limited to pointer objects. | |
543 | ||
544 | Note that CHILD_INDEX is unused in this situation, but still provided | |
545 | for consistency of interface with other routines describing an object's | |
546 | child. */ | |
547 | ||
548 | static void | |
549 | ada_varobj_describe_ptr_child (struct value *parent_value, | |
550 | struct type *parent_type, | |
551 | const char *parent_name, | |
552 | const char *parent_path_expr, | |
553 | int child_index, | |
2f408ecb | 554 | std::string *child_name, |
181875a4 JB |
555 | struct value **child_value, |
556 | struct type **child_type, | |
2f408ecb | 557 | std::string *child_path_expr) |
181875a4 JB |
558 | { |
559 | if (child_name) | |
2f408ecb | 560 | *child_name = string_printf ("%s.all", parent_name); |
181875a4 JB |
561 | |
562 | if (child_value && parent_value) | |
563 | ada_varobj_ind (parent_value, parent_type, child_value, NULL); | |
564 | ||
565 | if (child_type) | |
566 | ada_varobj_ind (parent_value, parent_type, NULL, child_type); | |
567 | ||
568 | if (child_path_expr) | |
2f408ecb | 569 | *child_path_expr = string_printf ("(%s).all", parent_path_expr); |
181875a4 JB |
570 | } |
571 | ||
572 | /* Same as ada_varobj_describe_child, limited to simple array objects | |
573 | (TYPE_CODE_ARRAY only). | |
574 | ||
575 | Assumes that the (PARENT_VALUE, PARENT_TYPE) pair is properly decoded. | |
576 | This is done by ada_varobj_describe_child before calling us. */ | |
577 | ||
578 | static void | |
579 | ada_varobj_describe_simple_array_child (struct value *parent_value, | |
580 | struct type *parent_type, | |
581 | const char *parent_name, | |
582 | const char *parent_path_expr, | |
583 | int child_index, | |
2f408ecb | 584 | std::string *child_name, |
181875a4 JB |
585 | struct value **child_value, |
586 | struct type **child_type, | |
2f408ecb | 587 | std::string *child_path_expr) |
181875a4 | 588 | { |
181875a4 JB |
589 | struct type *index_type; |
590 | int real_index; | |
591 | ||
592 | gdb_assert (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY); | |
593 | ||
4d072ce4 | 594 | index_type = TYPE_INDEX_TYPE (parent_type); |
181875a4 JB |
595 | real_index = child_index + ada_discrete_type_low_bound (index_type); |
596 | ||
597 | if (child_name) | |
598 | *child_name = ada_varobj_scalar_image (index_type, real_index); | |
599 | ||
600 | if (child_value && parent_value) | |
601 | ada_varobj_simple_array_elt (parent_value, parent_type, real_index, | |
602 | child_value, NULL); | |
603 | ||
604 | if (child_type) | |
605 | ada_varobj_simple_array_elt (parent_value, parent_type, real_index, | |
606 | NULL, child_type); | |
607 | ||
608 | if (child_path_expr) | |
609 | { | |
2f408ecb | 610 | std::string index_img = ada_varobj_scalar_image (index_type, real_index); |
181875a4 JB |
611 | |
612 | /* Enumeration litterals by themselves are potentially ambiguous. | |
613 | For instance, consider the following package spec: | |
614 | ||
615 | package Pck is | |
616 | type Color is (Red, Green, Blue, White); | |
617 | type Blood_Cells is (White, Red); | |
618 | end Pck; | |
619 | ||
620 | In this case, the litteral "red" for instance, or even | |
621 | the fully-qualified litteral "pck.red" cannot be resolved | |
622 | by itself. Type qualification is needed to determine which | |
623 | enumeration litterals should be used. | |
624 | ||
625 | The following variable will be used to contain the name | |
626 | of the array index type when such type qualification is | |
627 | needed. */ | |
628 | const char *index_type_name = NULL; | |
629 | ||
630 | /* If the index type is a range type, find the base type. */ | |
631 | while (TYPE_CODE (index_type) == TYPE_CODE_RANGE) | |
632 | index_type = TYPE_TARGET_TYPE (index_type); | |
633 | ||
634 | if (TYPE_CODE (index_type) == TYPE_CODE_ENUM | |
635 | || TYPE_CODE (index_type) == TYPE_CODE_BOOL) | |
636 | { | |
637 | index_type_name = ada_type_name (index_type); | |
638 | if (index_type_name) | |
639 | index_type_name = ada_decode (index_type_name); | |
640 | } | |
641 | ||
642 | if (index_type_name != NULL) | |
643 | *child_path_expr = | |
2f408ecb PA |
644 | string_printf ("(%s)(%.*s'(%s))", parent_path_expr, |
645 | ada_name_prefix_len (index_type_name), | |
646 | index_type_name, index_img.c_str ()); | |
181875a4 JB |
647 | else |
648 | *child_path_expr = | |
2f408ecb | 649 | string_printf ("(%s)(%s)", parent_path_expr, index_img.c_str ()); |
181875a4 JB |
650 | } |
651 | } | |
652 | ||
653 | /* See description at declaration above. */ | |
654 | ||
655 | static void | |
656 | ada_varobj_describe_child (struct value *parent_value, | |
657 | struct type *parent_type, | |
658 | const char *parent_name, | |
659 | const char *parent_path_expr, | |
660 | int child_index, | |
2f408ecb | 661 | std::string *child_name, |
181875a4 JB |
662 | struct value **child_value, |
663 | struct type **child_type, | |
2f408ecb | 664 | std::string *child_path_expr) |
181875a4 JB |
665 | { |
666 | /* We cannot compute the child's path expression without | |
667 | the parent's path expression. This is a pre-condition | |
668 | for calling this function. */ | |
669 | if (child_path_expr) | |
670 | gdb_assert (parent_path_expr != NULL); | |
671 | ||
672 | ada_varobj_decode_var (&parent_value, &parent_type); | |
673 | ada_varobj_adjust_for_child_access (&parent_value, &parent_type); | |
674 | ||
675 | if (child_name) | |
2f408ecb | 676 | *child_name = std::string (); |
181875a4 JB |
677 | if (child_value) |
678 | *child_value = NULL; | |
679 | if (child_type) | |
680 | *child_type = NULL; | |
681 | if (child_path_expr) | |
2f408ecb | 682 | *child_path_expr = std::string (); |
181875a4 | 683 | |
d91e9ea8 | 684 | if (ada_is_access_to_unconstrained_array (parent_type)) |
181875a4 JB |
685 | { |
686 | ada_varobj_describe_ptr_child (parent_value, parent_type, | |
687 | parent_name, parent_path_expr, | |
688 | child_index, child_name, | |
689 | child_value, child_type, | |
690 | child_path_expr); | |
691 | return; | |
692 | } | |
693 | ||
694 | if (TYPE_CODE (parent_type) == TYPE_CODE_ARRAY) | |
695 | { | |
696 | ada_varobj_describe_simple_array_child | |
697 | (parent_value, parent_type, parent_name, parent_path_expr, | |
698 | child_index, child_name, child_value, child_type, | |
699 | child_path_expr); | |
700 | return; | |
701 | } | |
702 | ||
2963898f XR |
703 | if (TYPE_CODE (parent_type) == TYPE_CODE_STRUCT |
704 | || TYPE_CODE (parent_type) == TYPE_CODE_UNION) | |
181875a4 JB |
705 | { |
706 | ada_varobj_describe_struct_child (parent_value, parent_type, | |
707 | parent_name, parent_path_expr, | |
708 | child_index, child_name, | |
709 | child_value, child_type, | |
710 | child_path_expr); | |
711 | return; | |
712 | } | |
713 | ||
714 | if (TYPE_CODE (parent_type) == TYPE_CODE_PTR) | |
715 | { | |
716 | ada_varobj_describe_ptr_child (parent_value, parent_type, | |
717 | parent_name, parent_path_expr, | |
718 | child_index, child_name, | |
719 | child_value, child_type, | |
720 | child_path_expr); | |
721 | return; | |
722 | } | |
723 | ||
724 | /* It should never happen. But rather than crash, report dummy names | |
725 | and return a NULL child_value. */ | |
726 | if (child_name) | |
2f408ecb | 727 | *child_name = "???"; |
181875a4 JB |
728 | } |
729 | ||
730 | /* Return the name of the child number CHILD_INDEX of the (PARENT_VALUE, | |
2f408ecb | 731 | PARENT_TYPE) pair. PARENT_NAME is the name of the PARENT. */ |
181875a4 | 732 | |
2f408ecb | 733 | static std::string |
181875a4 JB |
734 | ada_varobj_get_name_of_child (struct value *parent_value, |
735 | struct type *parent_type, | |
736 | const char *parent_name, int child_index) | |
737 | { | |
2f408ecb | 738 | std::string child_name; |
181875a4 JB |
739 | |
740 | ada_varobj_describe_child (parent_value, parent_type, parent_name, | |
741 | NULL, child_index, &child_name, NULL, | |
742 | NULL, NULL); | |
743 | return child_name; | |
744 | } | |
745 | ||
746 | /* Return the path expression of the child number CHILD_INDEX of | |
747 | the (PARENT_VALUE, PARENT_TYPE) pair. PARENT_NAME is the name | |
748 | of the parent, and PARENT_PATH_EXPR is the parent's path expression. | |
2f408ecb | 749 | Both must be non-NULL. */ |
181875a4 | 750 | |
2f408ecb | 751 | static std::string |
181875a4 JB |
752 | ada_varobj_get_path_expr_of_child (struct value *parent_value, |
753 | struct type *parent_type, | |
754 | const char *parent_name, | |
755 | const char *parent_path_expr, | |
756 | int child_index) | |
757 | { | |
2f408ecb | 758 | std::string child_path_expr; |
181875a4 JB |
759 | |
760 | ada_varobj_describe_child (parent_value, parent_type, parent_name, | |
761 | parent_path_expr, child_index, NULL, | |
762 | NULL, NULL, &child_path_expr); | |
763 | ||
764 | return child_path_expr; | |
765 | } | |
766 | ||
767 | /* Return the value of child number CHILD_INDEX of the (PARENT_VALUE, | |
768 | PARENT_TYPE) pair. PARENT_NAME is the name of the parent. */ | |
769 | ||
c4124bf1 | 770 | static struct value * |
181875a4 JB |
771 | ada_varobj_get_value_of_child (struct value *parent_value, |
772 | struct type *parent_type, | |
773 | const char *parent_name, int child_index) | |
774 | { | |
775 | struct value *child_value; | |
776 | ||
777 | ada_varobj_describe_child (parent_value, parent_type, parent_name, | |
778 | NULL, child_index, NULL, &child_value, | |
779 | NULL, NULL); | |
780 | ||
781 | return child_value; | |
782 | } | |
783 | ||
784 | /* Return the type of child number CHILD_INDEX of the (PARENT_VALUE, | |
785 | PARENT_TYPE) pair. */ | |
786 | ||
c4124bf1 | 787 | static struct type * |
181875a4 JB |
788 | ada_varobj_get_type_of_child (struct value *parent_value, |
789 | struct type *parent_type, | |
790 | int child_index) | |
791 | { | |
792 | struct type *child_type; | |
793 | ||
794 | ada_varobj_describe_child (parent_value, parent_type, NULL, NULL, | |
795 | child_index, NULL, NULL, &child_type, NULL); | |
796 | ||
797 | return child_type; | |
798 | } | |
799 | ||
800 | /* Return a string that contains the image of the given VALUE, using | |
801 | the print options OPTS as the options for formatting the result. | |
802 | ||
803 | The resulting string must be deallocated after use with xfree. */ | |
804 | ||
2f408ecb | 805 | static std::string |
181875a4 JB |
806 | ada_varobj_get_value_image (struct value *value, |
807 | struct value_print_options *opts) | |
808 | { | |
d7e74731 | 809 | string_file buffer; |
181875a4 | 810 | |
d7e74731 PA |
811 | common_val_print (value, &buffer, 0, opts, current_language); |
812 | return std::move (buffer.string ()); | |
181875a4 JB |
813 | } |
814 | ||
815 | /* Assuming that the (VALUE, TYPE) pair designates an array varobj, | |
816 | return a string that is suitable for use in the "value" field of | |
817 | the varobj output. Most of the time, this is the number of elements | |
818 | in the array inside square brackets, but there are situations where | |
819 | it's useful to add more info. | |
820 | ||
821 | OPTS are the print options used when formatting the result. | |
822 | ||
823 | The result should be deallocated after use using xfree. */ | |
824 | ||
2f408ecb | 825 | static std::string |
181875a4 JB |
826 | ada_varobj_get_value_of_array_variable (struct value *value, |
827 | struct type *type, | |
828 | struct value_print_options *opts) | |
829 | { | |
181875a4 JB |
830 | const int numchild = ada_varobj_get_array_number_of_children (value, type); |
831 | ||
832 | /* If we have a string, provide its contents in the "value" field. | |
833 | Otherwise, the only other way to inspect the contents of the string | |
834 | is by looking at the value of each element, as in any other array, | |
835 | which is not very convenient... */ | |
836 | if (value | |
837 | && ada_is_string_type (type) | |
838 | && (opts->format == 0 || opts->format == 's')) | |
839 | { | |
2f408ecb PA |
840 | std::string str = ada_varobj_get_value_image (value, opts); |
841 | return string_printf ("[%d] %s", numchild, str.c_str ()); | |
181875a4 JB |
842 | } |
843 | else | |
2f408ecb | 844 | return string_printf ("[%d]", numchild); |
181875a4 JB |
845 | } |
846 | ||
847 | /* Return a string representation of the (VALUE, TYPE) pair, using | |
848 | the given print options OPTS as our formatting options. */ | |
849 | ||
2f408ecb | 850 | static std::string |
181875a4 JB |
851 | ada_varobj_get_value_of_variable (struct value *value, |
852 | struct type *type, | |
853 | struct value_print_options *opts) | |
854 | { | |
181875a4 JB |
855 | ada_varobj_decode_var (&value, &type); |
856 | ||
857 | switch (TYPE_CODE (type)) | |
858 | { | |
859 | case TYPE_CODE_STRUCT: | |
860 | case TYPE_CODE_UNION: | |
2f408ecb | 861 | return "{...}"; |
181875a4 | 862 | case TYPE_CODE_ARRAY: |
2f408ecb | 863 | return ada_varobj_get_value_of_array_variable (value, type, opts); |
181875a4 JB |
864 | default: |
865 | if (!value) | |
2f408ecb | 866 | return ""; |
181875a4 | 867 | else |
2f408ecb | 868 | return ada_varobj_get_value_image (value, opts); |
181875a4 | 869 | } |
181875a4 JB |
870 | } |
871 | ||
99ad9427 | 872 | /* Ada specific callbacks for VAROBJs. */ |
181875a4 | 873 | |
99ad9427 | 874 | static int |
b09e2c59 | 875 | ada_number_of_children (const struct varobj *var) |
99ad9427 | 876 | { |
b4d61099 | 877 | return ada_varobj_get_number_of_children (var->value.get (), var->type); |
99ad9427 YQ |
878 | } |
879 | ||
2f408ecb | 880 | static std::string |
b09e2c59 | 881 | ada_name_of_variable (const struct varobj *parent) |
99ad9427 YQ |
882 | { |
883 | return c_varobj_ops.name_of_variable (parent); | |
884 | } | |
885 | ||
2f408ecb | 886 | static std::string |
c1cc6152 | 887 | ada_name_of_child (const struct varobj *parent, int index) |
99ad9427 | 888 | { |
b4d61099 | 889 | return ada_varobj_get_name_of_child (parent->value.get (), parent->type, |
2f408ecb | 890 | parent->name.c_str (), index); |
99ad9427 YQ |
891 | } |
892 | ||
2f408ecb | 893 | static std::string |
b09e2c59 | 894 | ada_path_expr_of_child (const struct varobj *child) |
99ad9427 | 895 | { |
c1cc6152 | 896 | const struct varobj *parent = child->parent; |
99ad9427 YQ |
897 | const char *parent_path_expr = varobj_get_path_expr (parent); |
898 | ||
b4d61099 | 899 | return ada_varobj_get_path_expr_of_child (parent->value.get (), |
99ad9427 | 900 | parent->type, |
2f408ecb | 901 | parent->name.c_str (), |
99ad9427 YQ |
902 | parent_path_expr, |
903 | child->index); | |
904 | } | |
905 | ||
906 | static struct value * | |
c1cc6152 | 907 | ada_value_of_child (const struct varobj *parent, int index) |
99ad9427 | 908 | { |
b4d61099 | 909 | return ada_varobj_get_value_of_child (parent->value.get (), parent->type, |
2f408ecb | 910 | parent->name.c_str (), index); |
99ad9427 YQ |
911 | } |
912 | ||
913 | static struct type * | |
c1cc6152 | 914 | ada_type_of_child (const struct varobj *parent, int index) |
99ad9427 | 915 | { |
b4d61099 | 916 | return ada_varobj_get_type_of_child (parent->value.get (), parent->type, |
99ad9427 YQ |
917 | index); |
918 | } | |
919 | ||
2f408ecb | 920 | static std::string |
b09e2c59 SM |
921 | ada_value_of_variable (const struct varobj *var, |
922 | enum varobj_display_formats format) | |
99ad9427 YQ |
923 | { |
924 | struct value_print_options opts; | |
925 | ||
926 | varobj_formatted_print_options (&opts, format); | |
927 | ||
b4d61099 TT |
928 | return ada_varobj_get_value_of_variable (var->value.get (), var->type, |
929 | &opts); | |
99ad9427 YQ |
930 | } |
931 | ||
932 | /* Implement the "value_is_changeable_p" routine for Ada. */ | |
933 | ||
4c37490d | 934 | static bool |
b09e2c59 | 935 | ada_value_is_changeable_p (const struct varobj *var) |
99ad9427 | 936 | { |
b4d61099 TT |
937 | struct type *type = (var->value != nullptr |
938 | ? value_type (var->value.get ()) : var->type); | |
99ad9427 | 939 | |
aff29d1c JB |
940 | if (TYPE_CODE (type) == TYPE_CODE_REF) |
941 | type = TYPE_TARGET_TYPE (type); | |
942 | ||
d91e9ea8 | 943 | if (ada_is_access_to_unconstrained_array (type)) |
99ad9427 YQ |
944 | { |
945 | /* This is in reality a pointer to an unconstrained array. | |
946 | its value is changeable. */ | |
4c37490d | 947 | return true; |
99ad9427 YQ |
948 | } |
949 | ||
950 | if (ada_is_string_type (type)) | |
951 | { | |
952 | /* We display the contents of the string in the array's | |
953 | "value" field. The contents can change, so consider | |
954 | that the array is changeable. */ | |
4c37490d | 955 | return true; |
99ad9427 YQ |
956 | } |
957 | ||
958 | return varobj_default_value_is_changeable_p (var); | |
959 | } | |
960 | ||
961 | /* Implement the "value_has_mutated" routine for Ada. */ | |
962 | ||
4c37490d | 963 | static bool |
b09e2c59 | 964 | ada_value_has_mutated (const struct varobj *var, struct value *new_val, |
99ad9427 YQ |
965 | struct type *new_type) |
966 | { | |
99ad9427 YQ |
967 | int from = -1; |
968 | int to = -1; | |
969 | ||
970 | /* If the number of fields have changed, then for sure the type | |
971 | has mutated. */ | |
972 | if (ada_varobj_get_number_of_children (new_val, new_type) | |
973 | != var->num_children) | |
4c37490d | 974 | return true; |
99ad9427 YQ |
975 | |
976 | /* If the number of fields have remained the same, then we need | |
977 | to check the name of each field. If they remain the same, | |
978 | then chances are the type hasn't mutated. This is technically | |
979 | an incomplete test, as the child's type might have changed | |
980 | despite the fact that the name remains the same. But we'll | |
981 | handle this situation by saying that the child has mutated, | |
982 | not this value. | |
983 | ||
984 | If only part (or none!) of the children have been fetched, | |
985 | then only check the ones we fetched. It does not matter | |
986 | to the frontend whether a child that it has not fetched yet | |
987 | has mutated or not. So just assume it hasn't. */ | |
988 | ||
989 | varobj_restrict_range (var->children, &from, &to); | |
ddf0ea08 | 990 | for (int i = from; i < to; i++) |
2f408ecb PA |
991 | if (ada_varobj_get_name_of_child (new_val, new_type, |
992 | var->name.c_str (), i) | |
ddf0ea08 | 993 | != var->children[i]->name) |
4c37490d | 994 | return true; |
99ad9427 | 995 | |
4c37490d | 996 | return false; |
99ad9427 YQ |
997 | } |
998 | ||
999 | /* varobj operations for ada. */ | |
1000 | ||
1001 | const struct lang_varobj_ops ada_varobj_ops = | |
1002 | { | |
1003 | ada_number_of_children, | |
1004 | ada_name_of_variable, | |
1005 | ada_name_of_child, | |
1006 | ada_path_expr_of_child, | |
1007 | ada_value_of_child, | |
1008 | ada_type_of_child, | |
1009 | ada_value_of_variable, | |
1010 | ada_value_is_changeable_p, | |
9a9a7608 AB |
1011 | ada_value_has_mutated, |
1012 | varobj_default_is_path_expr_parent | |
99ad9427 | 1013 | }; |