ada-typeprint.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  1. /* Support for printing Ada types for GDB, the GNU debugger.
  2. Copyright (C) 1986-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 "bfd.h" /* Binary File Description */
  16. #include "gdbtypes.h"
  17. #include "value.h"
  18. #include "c-lang.h"
  19. #include "cli/cli-style.h"
  20. #include "typeprint.h"
  21. #include "target-float.h"
  22. #include "ada-lang.h"
  23. #include <ctype.h>
  24. static int print_selected_record_field_types (struct type *, struct type *,
  25. int, int,
  26. struct ui_file *, int, int,
  27. const struct type_print_options *);
  28. static int print_record_field_types (struct type *, struct type *,
  29. struct ui_file *, int, int,
  30. const struct type_print_options *);
  31. static char *name_buffer;
  32. static int name_buffer_len;
  33. /* The (decoded) Ada name of TYPE. This value persists until the
  34. next call. */
  35. static char *
  36. decoded_type_name (struct type *type)
  37. {
  38. if (ada_type_name (type) == NULL)
  39. return NULL;
  40. else
  41. {
  42. const char *raw_name = ada_type_name (type);
  43. char *s, *q;
  44. if (name_buffer == NULL || name_buffer_len <= strlen (raw_name))
  45. {
  46. name_buffer_len = 16 + 2 * strlen (raw_name);
  47. name_buffer = (char *) xrealloc (name_buffer, name_buffer_len);
  48. }
  49. strcpy (name_buffer, raw_name);
  50. s = (char *) strstr (name_buffer, "___");
  51. if (s != NULL)
  52. *s = '\0';
  53. s = name_buffer + strlen (name_buffer) - 1;
  54. while (s > name_buffer && (s[0] != '_' || s[-1] != '_'))
  55. s -= 1;
  56. if (s == name_buffer)
  57. return name_buffer;
  58. if (!islower (s[1]))
  59. return NULL;
  60. for (s = q = name_buffer; *s != '\0'; q += 1)
  61. {
  62. if (s[0] == '_' && s[1] == '_')
  63. {
  64. *q = '.';
  65. s += 2;
  66. }
  67. else
  68. {
  69. *q = *s;
  70. s += 1;
  71. }
  72. }
  73. *q = '\0';
  74. return name_buffer;
  75. }
  76. }
  77. /* Return nonzero if TYPE is a subrange type, and its bounds
  78. are identical to the bounds of its subtype. */
  79. static int
  80. type_is_full_subrange_of_target_type (struct type *type)
  81. {
  82. struct type *subtype;
  83. if (type->code () != TYPE_CODE_RANGE)
  84. return 0;
  85. subtype = TYPE_TARGET_TYPE (type);
  86. if (subtype == NULL)
  87. return 0;
  88. if (is_dynamic_type (type))
  89. return 0;
  90. if (ada_discrete_type_low_bound (type)
  91. != ada_discrete_type_low_bound (subtype))
  92. return 0;
  93. if (ada_discrete_type_high_bound (type)
  94. != ada_discrete_type_high_bound (subtype))
  95. return 0;
  96. return 1;
  97. }
  98. /* Print TYPE on STREAM, preferably as a range if BOUNDS_PREFERED_P
  99. is nonzero. */
  100. static void
  101. print_range (struct type *type, struct ui_file *stream,
  102. int bounds_prefered_p)
  103. {
  104. if (!bounds_prefered_p)
  105. {
  106. /* Try stripping all TYPE_CODE_RANGE layers whose bounds
  107. are identical to the bounds of their subtype. When
  108. the bounds of both types match, it can allow us to
  109. print a range using the name of its base type, which
  110. is easier to read. For instance, we would print...
  111. array (character) of ...
  112. ... instead of...
  113. array ('["00"]' .. '["ff"]') of ... */
  114. while (type_is_full_subrange_of_target_type (type))
  115. type = TYPE_TARGET_TYPE (type);
  116. }
  117. switch (type->code ())
  118. {
  119. case TYPE_CODE_RANGE:
  120. case TYPE_CODE_ENUM:
  121. {
  122. LONGEST lo = 0, hi = 0; /* init for gcc -Wall */
  123. int got_error = 0;
  124. try
  125. {
  126. lo = ada_discrete_type_low_bound (type);
  127. hi = ada_discrete_type_high_bound (type);
  128. }
  129. catch (const gdb_exception_error &e)
  130. {
  131. /* This can happen when the range is dynamic. Sometimes,
  132. resolving dynamic property values requires us to have
  133. access to an actual object, which is not available
  134. when the user is using the "ptype" command on a type.
  135. Print the range as an unbounded range. */
  136. gdb_printf (stream, "<>");
  137. got_error = 1;
  138. }
  139. if (!got_error)
  140. {
  141. ada_print_scalar (type, lo, stream);
  142. gdb_printf (stream, " .. ");
  143. ada_print_scalar (type, hi, stream);
  144. }
  145. }
  146. break;
  147. default:
  148. gdb_printf (stream, "%.*s",
  149. ada_name_prefix_len (type->name ()),
  150. type->name ());
  151. break;
  152. }
  153. }
  154. /* Print the number or discriminant bound at BOUNDS+*N on STREAM, and
  155. set *N past the bound and its delimiter, if any. */
  156. static void
  157. print_range_bound (struct type *type, const char *bounds, int *n,
  158. struct ui_file *stream)
  159. {
  160. LONGEST B;
  161. if (ada_scan_number (bounds, *n, &B, n))
  162. {
  163. /* STABS decodes all range types which bounds are 0 .. -1 as
  164. unsigned integers (ie. the type code is TYPE_CODE_INT, not
  165. TYPE_CODE_RANGE). Unfortunately, ada_print_scalar() relies
  166. on the unsigned flag to determine whether the bound should
  167. be printed as a signed or an unsigned value. This causes
  168. the upper bound of the 0 .. -1 range types to be printed as
  169. a very large unsigned number instead of -1.
  170. To workaround this stabs deficiency, we replace the TYPE by NULL
  171. to indicate default output when we detect that the bound is negative,
  172. and the type is a TYPE_CODE_INT. The bound is negative when
  173. 'm' is the last character of the number scanned in BOUNDS. */
  174. if (bounds[*n - 1] == 'm' && type->code () == TYPE_CODE_INT)
  175. type = NULL;
  176. ada_print_scalar (type, B, stream);
  177. if (bounds[*n] == '_')
  178. *n += 2;
  179. }
  180. else
  181. {
  182. int bound_len;
  183. const char *bound = bounds + *n;
  184. const char *pend;
  185. pend = strstr (bound, "__");
  186. if (pend == NULL)
  187. *n += bound_len = strlen (bound);
  188. else
  189. {
  190. bound_len = pend - bound;
  191. *n += bound_len + 2;
  192. }
  193. gdb_printf (stream, "%.*s", bound_len, bound);
  194. }
  195. }
  196. /* Assuming NAME[0 .. NAME_LEN-1] is the name of a range type, print
  197. the value (if found) of the bound indicated by SUFFIX ("___L" or
  198. "___U") according to the ___XD conventions. */
  199. static void
  200. print_dynamic_range_bound (struct type *type, const char *name, int name_len,
  201. const char *suffix, struct ui_file *stream)
  202. {
  203. LONGEST B;
  204. std::string name_buf (name, name_len);
  205. name_buf += suffix;
  206. if (get_int_var_value (name_buf.c_str (), B))
  207. ada_print_scalar (type, B, stream);
  208. else
  209. gdb_printf (stream, "?");
  210. }
  211. /* Print RAW_TYPE as a range type, using any bound information
  212. following the GNAT encoding (if available).
  213. If BOUNDS_PREFERED_P is nonzero, force the printing of the range
  214. using its bounds. Otherwise, try printing the range without
  215. printing the value of the bounds, if possible (this is only
  216. considered a hint, not a guaranty). */
  217. static void
  218. print_range_type (struct type *raw_type, struct ui_file *stream,
  219. int bounds_prefered_p)
  220. {
  221. const char *name;
  222. struct type *base_type;
  223. const char *subtype_info;
  224. gdb_assert (raw_type != NULL);
  225. name = raw_type->name ();
  226. gdb_assert (name != NULL);
  227. if (raw_type->code () == TYPE_CODE_RANGE)
  228. base_type = TYPE_TARGET_TYPE (raw_type);
  229. else
  230. base_type = raw_type;
  231. subtype_info = strstr (name, "___XD");
  232. if (subtype_info == NULL)
  233. print_range (raw_type, stream, bounds_prefered_p);
  234. else
  235. {
  236. int prefix_len = subtype_info - name;
  237. const char *bounds_str;
  238. int n;
  239. subtype_info += 5;
  240. bounds_str = strchr (subtype_info, '_');
  241. n = 1;
  242. if (*subtype_info == 'L')
  243. {
  244. print_range_bound (base_type, bounds_str, &n, stream);
  245. subtype_info += 1;
  246. }
  247. else
  248. print_dynamic_range_bound (base_type, name, prefix_len, "___L",
  249. stream);
  250. gdb_printf (stream, " .. ");
  251. if (*subtype_info == 'U')
  252. print_range_bound (base_type, bounds_str, &n, stream);
  253. else
  254. print_dynamic_range_bound (base_type, name, prefix_len, "___U",
  255. stream);
  256. }
  257. }
  258. /* Print enumerated type TYPE on STREAM. */
  259. static void
  260. print_enum_type (struct type *type, struct ui_file *stream)
  261. {
  262. int len = type->num_fields ();
  263. int i;
  264. LONGEST lastval;
  265. gdb_printf (stream, "(");
  266. stream->wrap_here (1);
  267. lastval = 0;
  268. for (i = 0; i < len; i++)
  269. {
  270. QUIT;
  271. if (i)
  272. gdb_printf (stream, ", ");
  273. stream->wrap_here (4);
  274. fputs_styled (ada_enum_name (type->field (i).name ()),
  275. variable_name_style.style (), stream);
  276. if (lastval != type->field (i).loc_enumval ())
  277. {
  278. gdb_printf (stream, " => %s",
  279. plongest (type->field (i).loc_enumval ()));
  280. lastval = type->field (i).loc_enumval ();
  281. }
  282. lastval += 1;
  283. }
  284. gdb_printf (stream, ")");
  285. }
  286. /* Print simple (constrained) array type TYPE on STREAM. LEVEL is the
  287. recursion (indentation) level, in case the element type itself has
  288. nested structure, and SHOW is the number of levels of internal
  289. structure to show (see ada_print_type). */
  290. static void
  291. print_array_type (struct type *type, struct ui_file *stream, int show,
  292. int level, const struct type_print_options *flags)
  293. {
  294. int bitsize;
  295. int n_indices;
  296. struct type *elt_type = NULL;
  297. if (ada_is_constrained_packed_array_type (type))
  298. type = ada_coerce_to_simple_array_type (type);
  299. bitsize = 0;
  300. gdb_printf (stream, "array (");
  301. if (type == NULL)
  302. {
  303. fprintf_styled (stream, metadata_style.style (),
  304. _("<undecipherable array type>"));
  305. return;
  306. }
  307. n_indices = -1;
  308. if (ada_is_simple_array_type (type))
  309. {
  310. struct type *range_desc_type;
  311. struct type *arr_type;
  312. range_desc_type = ada_find_parallel_type (type, "___XA");
  313. ada_fixup_array_indexes_type (range_desc_type);
  314. bitsize = 0;
  315. if (range_desc_type == NULL)
  316. {
  317. for (arr_type = type; arr_type->code () == TYPE_CODE_ARRAY;
  318. arr_type = TYPE_TARGET_TYPE (arr_type))
  319. {
  320. if (arr_type != type)
  321. gdb_printf (stream, ", ");
  322. print_range (arr_type->index_type (), stream,
  323. 0 /* bounds_prefered_p */);
  324. if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
  325. bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
  326. }
  327. }
  328. else
  329. {
  330. int k;
  331. n_indices = range_desc_type->num_fields ();
  332. for (k = 0, arr_type = type;
  333. k < n_indices;
  334. k += 1, arr_type = TYPE_TARGET_TYPE (arr_type))
  335. {
  336. if (k > 0)
  337. gdb_printf (stream, ", ");
  338. print_range_type (range_desc_type->field (k).type (),
  339. stream, 0 /* bounds_prefered_p */);
  340. if (TYPE_FIELD_BITSIZE (arr_type, 0) > 0)
  341. bitsize = TYPE_FIELD_BITSIZE (arr_type, 0);
  342. }
  343. }
  344. }
  345. else
  346. {
  347. int i, i0;
  348. for (i = i0 = ada_array_arity (type); i > 0; i -= 1)
  349. gdb_printf (stream, "%s<>", i == i0 ? "" : ", ");
  350. }
  351. elt_type = ada_array_element_type (type, n_indices);
  352. gdb_printf (stream, ") of ");
  353. stream->wrap_here (0);
  354. ada_print_type (elt_type, "", stream, show == 0 ? 0 : show - 1, level + 1,
  355. flags);
  356. /* Arrays with variable-length elements are never bit-packed in practice but
  357. compilers have to describe their stride so that we can properly fetch
  358. individual elements. Do not say the array is packed in this case. */
  359. if (bitsize > 0 && !is_dynamic_type (elt_type))
  360. gdb_printf (stream, " <packed: %d-bit elements>", bitsize);
  361. }
  362. /* Print the choices encoded by field FIELD_NUM of variant-part TYPE on
  363. STREAM, assuming that VAL_TYPE (if non-NULL) is the type of the
  364. values. Return non-zero if the field is an encoding of
  365. discriminant values, as in a standard variant record, and 0 if the
  366. field is not so encoded (as happens with single-component variants
  367. in types annotated with pragma Unchecked_Union). */
  368. static int
  369. print_choices (struct type *type, int field_num, struct ui_file *stream,
  370. struct type *val_type)
  371. {
  372. int have_output;
  373. int p;
  374. const char *name = type->field (field_num).name ();
  375. have_output = 0;
  376. /* Skip over leading 'V': NOTE soon to be obsolete. */
  377. if (name[0] == 'V')
  378. {
  379. if (!ada_scan_number (name, 1, NULL, &p))
  380. goto Huh;
  381. }
  382. else
  383. p = 0;
  384. while (1)
  385. {
  386. switch (name[p])
  387. {
  388. default:
  389. goto Huh;
  390. case '_':
  391. case '\0':
  392. gdb_printf (stream, " =>");
  393. return 1;
  394. case 'S':
  395. case 'R':
  396. case 'O':
  397. if (have_output)
  398. gdb_printf (stream, " | ");
  399. have_output = 1;
  400. break;
  401. }
  402. switch (name[p])
  403. {
  404. case 'S':
  405. {
  406. LONGEST W;
  407. if (!ada_scan_number (name, p + 1, &W, &p))
  408. goto Huh;
  409. ada_print_scalar (val_type, W, stream);
  410. break;
  411. }
  412. case 'R':
  413. {
  414. LONGEST L, U;
  415. if (!ada_scan_number (name, p + 1, &L, &p)
  416. || name[p] != 'T' || !ada_scan_number (name, p + 1, &U, &p))
  417. goto Huh;
  418. ada_print_scalar (val_type, L, stream);
  419. gdb_printf (stream, " .. ");
  420. ada_print_scalar (val_type, U, stream);
  421. break;
  422. }
  423. case 'O':
  424. gdb_printf (stream, "others");
  425. p += 1;
  426. break;
  427. }
  428. }
  429. Huh:
  430. gdb_printf (stream, "? =>");
  431. return 0;
  432. }
  433. /* A helper for print_variant_clauses that prints the members of
  434. VAR_TYPE. DISCR_TYPE is the type of the discriminant (or nullptr
  435. if not available). The discriminant is contained in OUTER_TYPE.
  436. STREAM, LEVEL, SHOW, and FLAGS are the same as for
  437. ada_print_type. */
  438. static void
  439. print_variant_clauses (struct type *var_type, struct type *discr_type,
  440. struct type *outer_type, struct ui_file *stream,
  441. int show, int level,
  442. const struct type_print_options *flags)
  443. {
  444. for (int i = 0; i < var_type->num_fields (); i += 1)
  445. {
  446. gdb_printf (stream, "\n%*swhen ", level, "");
  447. if (print_choices (var_type, i, stream, discr_type))
  448. {
  449. if (print_record_field_types (var_type->field (i).type (),
  450. outer_type, stream, show, level,
  451. flags)
  452. <= 0)
  453. gdb_printf (stream, " null;");
  454. }
  455. else
  456. print_selected_record_field_types (var_type, outer_type, i, i,
  457. stream, show, level, flags);
  458. }
  459. }
  460. /* Assuming that field FIELD_NUM of TYPE represents variants whose
  461. discriminant is contained in OUTER_TYPE, print its components on STREAM.
  462. LEVEL is the recursion (indentation) level, in case any of the fields
  463. themselves have nested structure, and SHOW is the number of levels of
  464. internal structure to show (see ada_print_type). For this purpose,
  465. fields nested in a variant part are taken to be at the same level as
  466. the fields immediately outside the variant part. */
  467. static void
  468. print_variant_clauses (struct type *type, int field_num,
  469. struct type *outer_type, struct ui_file *stream,
  470. int show, int level,
  471. const struct type_print_options *flags)
  472. {
  473. struct type *var_type, *par_type;
  474. struct type *discr_type;
  475. var_type = type->field (field_num).type ();
  476. discr_type = ada_variant_discrim_type (var_type, outer_type);
  477. if (var_type->code () == TYPE_CODE_PTR)
  478. {
  479. var_type = TYPE_TARGET_TYPE (var_type);
  480. if (var_type == NULL || var_type->code () != TYPE_CODE_UNION)
  481. return;
  482. }
  483. par_type = ada_find_parallel_type (var_type, "___XVU");
  484. if (par_type != NULL)
  485. var_type = par_type;
  486. print_variant_clauses (var_type, discr_type, outer_type, stream, show,
  487. level + 4, flags);
  488. }
  489. /* Assuming that field FIELD_NUM of TYPE is a variant part whose
  490. discriminants are contained in OUTER_TYPE, print a description of it
  491. on STREAM. LEVEL is the recursion (indentation) level, in case any of
  492. the fields themselves have nested structure, and SHOW is the number of
  493. levels of internal structure to show (see ada_print_type). For this
  494. purpose, fields nested in a variant part are taken to be at the same
  495. level as the fields immediately outside the variant part. */
  496. static void
  497. print_variant_part (struct type *type, int field_num, struct type *outer_type,
  498. struct ui_file *stream, int show, int level,
  499. const struct type_print_options *flags)
  500. {
  501. const char *variant
  502. = ada_variant_discrim_name (type->field (field_num).type ());
  503. if (*variant == '\0')
  504. variant = "?";
  505. gdb_printf (stream, "\n%*scase %s is", level + 4, "", variant);
  506. print_variant_clauses (type, field_num, outer_type, stream, show,
  507. level + 4, flags);
  508. gdb_printf (stream, "\n%*send case;", level + 4, "");
  509. }
  510. /* Print a description on STREAM of the fields FLD0 through FLD1 in
  511. record or union type TYPE, whose discriminants are in OUTER_TYPE.
  512. LEVEL is the recursion (indentation) level, in case any of the
  513. fields themselves have nested structure, and SHOW is the number of
  514. levels of internal structure to show (see ada_print_type). Does
  515. not print parent type information of TYPE. Returns 0 if no fields
  516. printed, -1 for an incomplete type, else > 0. Prints each field
  517. beginning on a new line, but does not put a new line at end. */
  518. static int
  519. print_selected_record_field_types (struct type *type, struct type *outer_type,
  520. int fld0, int fld1,
  521. struct ui_file *stream, int show, int level,
  522. const struct type_print_options *flags)
  523. {
  524. int i, flds;
  525. flds = 0;
  526. if (fld0 > fld1 && type->is_stub ())
  527. return -1;
  528. for (i = fld0; i <= fld1; i += 1)
  529. {
  530. QUIT;
  531. if (ada_is_parent_field (type, i) || ada_is_ignored_field (type, i))
  532. ;
  533. else if (ada_is_wrapper_field (type, i))
  534. flds += print_record_field_types (type->field (i).type (), type,
  535. stream, show, level, flags);
  536. else if (ada_is_variant_part (type, i))
  537. {
  538. print_variant_part (type, i, outer_type, stream, show, level, flags);
  539. flds = 1;
  540. }
  541. else
  542. {
  543. flds += 1;
  544. gdb_printf (stream, "\n%*s", level + 4, "");
  545. ada_print_type (type->field (i).type (),
  546. type->field (i).name (),
  547. stream, show - 1, level + 4, flags);
  548. gdb_printf (stream, ";");
  549. }
  550. }
  551. return flds;
  552. }
  553. static void print_record_field_types_dynamic
  554. (const gdb::array_view<variant_part> &parts,
  555. int from, int to, struct type *type, struct ui_file *stream,
  556. int show, int level, const struct type_print_options *flags);
  557. /* Print the choices encoded by VARIANT on STREAM. LEVEL is the
  558. indentation level. The type of the discriminant for VARIANT is
  559. given by DISR_TYPE. */
  560. static void
  561. print_choices (struct type *discr_type, const variant &variant,
  562. struct ui_file *stream, int level)
  563. {
  564. gdb_printf (stream, "\n%*swhen ", level, "");
  565. if (variant.is_default ())
  566. gdb_printf (stream, "others");
  567. else
  568. {
  569. bool first = true;
  570. for (const discriminant_range &range : variant.discriminants)
  571. {
  572. if (!first)
  573. gdb_printf (stream, " | ");
  574. first = false;
  575. ada_print_scalar (discr_type, range.low, stream);
  576. if (range.low != range.high)
  577. ada_print_scalar (discr_type, range.high, stream);
  578. }
  579. }
  580. gdb_printf (stream, " =>");
  581. }
  582. /* Print a single variant part, PART, on STREAM. TYPE is the
  583. enclosing type. SHOW, LEVEL, and FLAGS are the usual type-printing
  584. settings. This prints information about PART and the fields it
  585. controls. It returns the index of the next field that should be
  586. shown -- that is, one after the last field printed by this
  587. call. */
  588. static int
  589. print_variant_part (const variant_part &part,
  590. struct type *type, struct ui_file *stream,
  591. int show, int level,
  592. const struct type_print_options *flags)
  593. {
  594. struct type *discr_type = nullptr;
  595. const char *name;
  596. if (part.discriminant_index == -1)
  597. name = "?";
  598. else
  599. {
  600. name = type->field (part.discriminant_index).name ();;
  601. discr_type = type->field (part.discriminant_index).type ();
  602. }
  603. gdb_printf (stream, "\n%*scase %s is", level + 4, "", name);
  604. int last_field = -1;
  605. for (const variant &variant : part.variants)
  606. {
  607. print_choices (discr_type, variant, stream, level + 8);
  608. if (variant.first_field == variant.last_field)
  609. gdb_printf (stream, " null;");
  610. else
  611. {
  612. print_record_field_types_dynamic (variant.parts,
  613. variant.first_field,
  614. variant.last_field, type, stream,
  615. show, level + 8, flags);
  616. last_field = variant.last_field;
  617. }
  618. }
  619. gdb_printf (stream, "\n%*send case;", level + 4, "");
  620. return last_field;
  621. }
  622. /* Print some fields of TYPE to STREAM. SHOW, LEVEL, and FLAGS are
  623. the usual type-printing settings. PARTS is the array of variant
  624. parts that correspond to the range of fields to be printed. FROM
  625. and TO are the range of fields to print. */
  626. static void
  627. print_record_field_types_dynamic (const gdb::array_view<variant_part> &parts,
  628. int from, int to,
  629. struct type *type, struct ui_file *stream,
  630. int show, int level,
  631. const struct type_print_options *flags)
  632. {
  633. int field = from;
  634. for (const variant_part &part : parts)
  635. {
  636. if (part.variants.empty ())
  637. continue;
  638. /* Print any non-varying fields. */
  639. int first_varying = part.variants[0].first_field;
  640. print_selected_record_field_types (type, type, field,
  641. first_varying - 1, stream,
  642. show, level, flags);
  643. field = print_variant_part (part, type, stream, show, level, flags);
  644. }
  645. /* Print any trailing fields that we were asked to print. */
  646. print_selected_record_field_types (type, type, field, to - 1, stream, show,
  647. level, flags);
  648. }
  649. /* Print a description on STREAM of all fields of record or union type
  650. TYPE, as for print_selected_record_field_types, above. */
  651. static int
  652. print_record_field_types (struct type *type, struct type *outer_type,
  653. struct ui_file *stream, int show, int level,
  654. const struct type_print_options *flags)
  655. {
  656. struct dynamic_prop *prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
  657. if (prop != nullptr)
  658. {
  659. if (prop->kind () == PROP_TYPE)
  660. {
  661. type = prop->original_type ();
  662. prop = type->dyn_prop (DYN_PROP_VARIANT_PARTS);
  663. }
  664. gdb_assert (prop->kind () == PROP_VARIANT_PARTS);
  665. print_record_field_types_dynamic (*prop->variant_parts (),
  666. 0, type->num_fields (),
  667. type, stream, show, level, flags);
  668. return type->num_fields ();
  669. }
  670. return print_selected_record_field_types (type, outer_type,
  671. 0, type->num_fields () - 1,
  672. stream, show, level, flags);
  673. }
  674. /* Print record type TYPE on STREAM. LEVEL is the recursion (indentation)
  675. level, in case the element type itself has nested structure, and SHOW is
  676. the number of levels of internal structure to show (see ada_print_type). */
  677. static void
  678. print_record_type (struct type *type0, struct ui_file *stream, int show,
  679. int level, const struct type_print_options *flags)
  680. {
  681. struct type *parent_type;
  682. struct type *type;
  683. type = ada_find_parallel_type (type0, "___XVE");
  684. if (type == NULL)
  685. type = type0;
  686. parent_type = ada_parent_type (type);
  687. if (ada_type_name (parent_type) != NULL)
  688. {
  689. const char *parent_name = decoded_type_name (parent_type);
  690. /* If we fail to decode the parent type name, then use the parent
  691. type name as is. Not pretty, but should never happen except
  692. when the debugging info is incomplete or incorrect. This
  693. prevents a crash trying to print a NULL pointer. */
  694. if (parent_name == NULL)
  695. parent_name = ada_type_name (parent_type);
  696. gdb_printf (stream, "new %s with record", parent_name);
  697. }
  698. else if (parent_type == NULL && ada_is_tagged_type (type, 0))
  699. gdb_printf (stream, "tagged record");
  700. else
  701. gdb_printf (stream, "record");
  702. if (show < 0)
  703. gdb_printf (stream, " ... end record");
  704. else
  705. {
  706. int flds;
  707. flds = 0;
  708. if (parent_type != NULL && ada_type_name (parent_type) == NULL)
  709. flds += print_record_field_types (parent_type, parent_type,
  710. stream, show, level, flags);
  711. flds += print_record_field_types (type, type, stream, show, level,
  712. flags);
  713. if (flds > 0)
  714. gdb_printf (stream, "\n%*send record", level, "");
  715. else if (flds < 0)
  716. gdb_printf (stream, _(" <incomplete type> end record"));
  717. else
  718. gdb_printf (stream, " null; end record");
  719. }
  720. }
  721. /* Print the unchecked union type TYPE in something resembling Ada
  722. format on STREAM. LEVEL is the recursion (indentation) level
  723. in case the element type itself has nested structure, and SHOW is the
  724. number of levels of internal structure to show (see ada_print_type). */
  725. static void
  726. print_unchecked_union_type (struct type *type, struct ui_file *stream,
  727. int show, int level,
  728. const struct type_print_options *flags)
  729. {
  730. if (show < 0)
  731. gdb_printf (stream, "record (?) is ... end record");
  732. else if (type->num_fields () == 0)
  733. gdb_printf (stream, "record (?) is null; end record");
  734. else
  735. {
  736. gdb_printf (stream, "record (?) is\n%*scase ? is", level + 4, "");
  737. print_variant_clauses (type, nullptr, type, stream, show, level + 8, flags);
  738. gdb_printf (stream, "\n%*send case;\n%*send record",
  739. level + 4, "", level, "");
  740. }
  741. }
  742. /* Print function or procedure type TYPE on STREAM. Make it a header
  743. for function or procedure NAME if NAME is not null. */
  744. static void
  745. print_func_type (struct type *type, struct ui_file *stream, const char *name,
  746. const struct type_print_options *flags)
  747. {
  748. int i, len = type->num_fields ();
  749. if (TYPE_TARGET_TYPE (type) != NULL
  750. && TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_VOID)
  751. gdb_printf (stream, "procedure");
  752. else
  753. gdb_printf (stream, "function");
  754. if (name != NULL && name[0] != '\0')
  755. {
  756. gdb_puts (" ", stream);
  757. fputs_styled (name, function_name_style.style (), stream);
  758. }
  759. if (len > 0)
  760. {
  761. gdb_printf (stream, " (");
  762. for (i = 0; i < len; i += 1)
  763. {
  764. if (i > 0)
  765. {
  766. gdb_puts ("; ", stream);
  767. stream->wrap_here (4);
  768. }
  769. gdb_printf (stream, "a%d: ", i + 1);
  770. ada_print_type (type->field (i).type (), "", stream, -1, 0,
  771. flags);
  772. }
  773. gdb_printf (stream, ")");
  774. }
  775. if (TYPE_TARGET_TYPE (type) == NULL)
  776. gdb_printf (stream, " return <unknown return type>");
  777. else if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
  778. {
  779. gdb_printf (stream, " return ");
  780. ada_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
  781. }
  782. }
  783. /* Print a description of a type TYPE0.
  784. Output goes to STREAM (via stdio).
  785. If VARSTRING is a non-empty string, print as an Ada variable/field
  786. declaration.
  787. SHOW+1 is the maximum number of levels of internal type structure
  788. to show (this applies to record types, enumerated types, and
  789. array types).
  790. SHOW is the number of levels of internal type structure to show
  791. when there is a type name for the SHOWth deepest level (0th is
  792. outer level).
  793. When SHOW<0, no inner structure is shown.
  794. LEVEL indicates level of recursion (for nested definitions). */
  795. void
  796. ada_print_type (struct type *type0, const char *varstring,
  797. struct ui_file *stream, int show, int level,
  798. const struct type_print_options *flags)
  799. {
  800. struct type *type = ada_check_typedef (ada_get_base_type (type0));
  801. /* If we can decode the original type name, use it. However, there
  802. are cases where the original type is an internally-generated type
  803. with a name that can't be decoded (and whose encoded name might
  804. not actually bear any relation to the type actually declared in
  805. the sources). In that case, try using the name of the base type
  806. in its place.
  807. Note that we looked at the possibility of always using the name
  808. of the base type. This does not always work, unfortunately, as
  809. there are situations where it's the base type which has an
  810. internally-generated name. */
  811. const char *type_name = decoded_type_name (type0);
  812. if (type_name == nullptr)
  813. type_name = decoded_type_name (type);
  814. int is_var_decl = (varstring != NULL && varstring[0] != '\0');
  815. if (type == NULL)
  816. {
  817. if (is_var_decl)
  818. gdb_printf (stream, "%.*s: ",
  819. ada_name_prefix_len (varstring), varstring);
  820. fprintf_styled (stream, metadata_style.style (), "<null type?>");
  821. return;
  822. }
  823. if (is_var_decl && type->code () != TYPE_CODE_FUNC)
  824. gdb_printf (stream, "%.*s: ",
  825. ada_name_prefix_len (varstring), varstring);
  826. if (type_name != NULL && show <= 0 && !ada_is_aligner_type (type))
  827. {
  828. gdb_printf (stream, "%.*s",
  829. ada_name_prefix_len (type_name), type_name);
  830. return;
  831. }
  832. if (ada_is_aligner_type (type))
  833. ada_print_type (ada_aligned_type (type), "", stream, show, level, flags);
  834. else if (ada_is_constrained_packed_array_type (type)
  835. && type->code () != TYPE_CODE_PTR)
  836. print_array_type (type, stream, show, level, flags);
  837. else
  838. switch (type->code ())
  839. {
  840. default:
  841. gdb_printf (stream, "<");
  842. c_print_type (type, "", stream, show, level, flags);
  843. gdb_printf (stream, ">");
  844. break;
  845. case TYPE_CODE_PTR:
  846. case TYPE_CODE_TYPEDEF:
  847. /* An __XVL field is not truly a pointer, so don't print
  848. "access" in this case. */
  849. if (type->code () != TYPE_CODE_PTR
  850. || strstr (varstring, "___XVL") == nullptr)
  851. gdb_printf (stream, "access ");
  852. ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
  853. flags);
  854. break;
  855. case TYPE_CODE_REF:
  856. gdb_printf (stream, "<ref> ");
  857. ada_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
  858. flags);
  859. break;
  860. case TYPE_CODE_ARRAY:
  861. print_array_type (type, stream, show, level, flags);
  862. break;
  863. case TYPE_CODE_BOOL:
  864. gdb_printf (stream, "(false, true)");
  865. break;
  866. case TYPE_CODE_INT:
  867. {
  868. const char *name = ada_type_name (type);
  869. if (!ada_is_range_type_name (name))
  870. fprintf_styled (stream, metadata_style.style (),
  871. _("<%s-byte integer>"),
  872. pulongest (TYPE_LENGTH (type)));
  873. else
  874. {
  875. gdb_printf (stream, "range ");
  876. print_range_type (type, stream, 1 /* bounds_prefered_p */);
  877. }
  878. }
  879. break;
  880. case TYPE_CODE_RANGE:
  881. if (is_fixed_point_type (type))
  882. {
  883. gdb_printf (stream, "<");
  884. print_type_fixed_point (type, stream);
  885. gdb_printf (stream, ">");
  886. }
  887. else if (ada_is_modular_type (type))
  888. gdb_printf (stream, "mod %s",
  889. int_string (ada_modulus (type), 10, 0, 0, 1));
  890. else
  891. {
  892. gdb_printf (stream, "range ");
  893. print_range (type, stream, 1 /* bounds_prefered_p */);
  894. }
  895. break;
  896. case TYPE_CODE_FLT:
  897. fprintf_styled (stream, metadata_style.style (),
  898. _("<%s-byte float>"),
  899. pulongest (TYPE_LENGTH (type)));
  900. break;
  901. case TYPE_CODE_ENUM:
  902. if (show < 0)
  903. gdb_printf (stream, "(...)");
  904. else
  905. print_enum_type (type, stream);
  906. break;
  907. case TYPE_CODE_STRUCT:
  908. if (ada_is_array_descriptor_type (type))
  909. print_array_type (type, stream, show, level, flags);
  910. else if (ada_is_bogus_array_descriptor (type))
  911. gdb_printf (stream,
  912. _("array (?) of ? (<mal-formed descriptor>)"));
  913. else
  914. print_record_type (type, stream, show, level, flags);
  915. break;
  916. case TYPE_CODE_UNION:
  917. print_unchecked_union_type (type, stream, show, level, flags);
  918. break;
  919. case TYPE_CODE_FUNC:
  920. print_func_type (type, stream, varstring, flags);
  921. break;
  922. }
  923. }
  924. /* Implement the la_print_typedef language method for Ada. */
  925. void
  926. ada_print_typedef (struct type *type, struct symbol *new_symbol,
  927. struct ui_file *stream)
  928. {
  929. type = ada_check_typedef (type);
  930. ada_print_type (type, "", stream, 0, 0, &type_print_raw_options);
  931. }