ada-varobj.c 32 KB

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