m2-typeprint.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* Support for printing Modula 2 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 "gdbsupport/gdb_obstack.h"
  16. #include "bfd.h" /* Binary File Description */
  17. #include "symtab.h"
  18. #include "gdbtypes.h"
  19. #include "expression.h"
  20. #include "value.h"
  21. #include "gdbcore.h"
  22. #include "m2-lang.h"
  23. #include "target.h"
  24. #include "language.h"
  25. #include "demangle.h"
  26. #include "c-lang.h"
  27. #include "typeprint.h"
  28. #include "cp-abi.h"
  29. #include "cli/cli-style.h"
  30. static void m2_print_bounds (struct type *type,
  31. struct ui_file *stream, int show, int level,
  32. int print_high);
  33. static void m2_typedef (struct type *, struct ui_file *, int, int,
  34. const struct type_print_options *);
  35. static void m2_array (struct type *, struct ui_file *, int, int,
  36. const struct type_print_options *);
  37. static void m2_pointer (struct type *, struct ui_file *, int, int,
  38. const struct type_print_options *);
  39. static void m2_ref (struct type *, struct ui_file *, int, int,
  40. const struct type_print_options *);
  41. static void m2_procedure (struct type *, struct ui_file *, int, int,
  42. const struct type_print_options *);
  43. static void m2_union (struct type *, struct ui_file *);
  44. static void m2_enum (struct type *, struct ui_file *, int, int);
  45. static void m2_range (struct type *, struct ui_file *, int, int,
  46. const struct type_print_options *);
  47. static void m2_type_name (struct type *type, struct ui_file *stream);
  48. static void m2_short_set (struct type *type, struct ui_file *stream,
  49. int show, int level);
  50. static int m2_long_set (struct type *type, struct ui_file *stream,
  51. int show, int level, const struct type_print_options *flags);
  52. static int m2_unbounded_array (struct type *type, struct ui_file *stream,
  53. int show, int level,
  54. const struct type_print_options *flags);
  55. static void m2_record_fields (struct type *type, struct ui_file *stream,
  56. int show, int level, const struct type_print_options *flags);
  57. static void m2_unknown (const char *s, struct type *type,
  58. struct ui_file *stream, int show, int level);
  59. int m2_is_long_set (struct type *type);
  60. int m2_is_long_set_of_type (struct type *type, struct type **of_type);
  61. int m2_is_unbounded_array (struct type *type);
  62. void
  63. m2_print_type (struct type *type, const char *varstring,
  64. struct ui_file *stream,
  65. int show, int level,
  66. const struct type_print_options *flags)
  67. {
  68. type = check_typedef (type);
  69. QUIT;
  70. stream->wrap_here (4);
  71. if (type == NULL)
  72. {
  73. fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
  74. return;
  75. }
  76. switch (type->code ())
  77. {
  78. case TYPE_CODE_SET:
  79. m2_short_set(type, stream, show, level);
  80. break;
  81. case TYPE_CODE_STRUCT:
  82. if (m2_long_set (type, stream, show, level, flags)
  83. || m2_unbounded_array (type, stream, show, level, flags))
  84. break;
  85. m2_record_fields (type, stream, show, level, flags);
  86. break;
  87. case TYPE_CODE_TYPEDEF:
  88. m2_typedef (type, stream, show, level, flags);
  89. break;
  90. case TYPE_CODE_ARRAY:
  91. m2_array (type, stream, show, level, flags);
  92. break;
  93. case TYPE_CODE_PTR:
  94. m2_pointer (type, stream, show, level, flags);
  95. break;
  96. case TYPE_CODE_REF:
  97. m2_ref (type, stream, show, level, flags);
  98. break;
  99. case TYPE_CODE_METHOD:
  100. m2_unknown (_("method"), type, stream, show, level);
  101. break;
  102. case TYPE_CODE_FUNC:
  103. m2_procedure (type, stream, show, level, flags);
  104. break;
  105. case TYPE_CODE_UNION:
  106. m2_union (type, stream);
  107. break;
  108. case TYPE_CODE_ENUM:
  109. m2_enum (type, stream, show, level);
  110. break;
  111. case TYPE_CODE_VOID:
  112. break;
  113. case TYPE_CODE_UNDEF:
  114. /* i18n: Do not translate the "struct" part! */
  115. m2_unknown (_("undef"), type, stream, show, level);
  116. break;
  117. case TYPE_CODE_ERROR:
  118. m2_unknown (_("error"), type, stream, show, level);
  119. break;
  120. case TYPE_CODE_RANGE:
  121. m2_range (type, stream, show, level, flags);
  122. break;
  123. default:
  124. m2_type_name (type, stream);
  125. break;
  126. }
  127. }
  128. /* Print a typedef using M2 syntax. TYPE is the underlying type.
  129. NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
  130. which to print. */
  131. void
  132. m2_language::print_typedef (struct type *type, struct symbol *new_symbol,
  133. struct ui_file *stream) const
  134. {
  135. type = check_typedef (type);
  136. gdb_printf (stream, "TYPE ");
  137. if (!new_symbol->type ()->name ()
  138. || strcmp ((new_symbol->type ())->name (),
  139. new_symbol->linkage_name ()) != 0)
  140. gdb_printf (stream, "%s = ", new_symbol->print_name ());
  141. else
  142. gdb_printf (stream, "<builtin> = ");
  143. type_print (type, "", stream, 0);
  144. gdb_printf (stream, ";");
  145. }
  146. /* m2_type_name - if a, type, has a name then print it. */
  147. void
  148. m2_type_name (struct type *type, struct ui_file *stream)
  149. {
  150. if (type->name () != NULL)
  151. gdb_puts (type->name (), stream);
  152. }
  153. /* m2_range - displays a Modula-2 subrange type. */
  154. void
  155. m2_range (struct type *type, struct ui_file *stream, int show,
  156. int level, const struct type_print_options *flags)
  157. {
  158. if (type->bounds ()->high.const_val () == type->bounds ()->low.const_val ())
  159. {
  160. /* FIXME: TYPE_TARGET_TYPE used to be TYPE_DOMAIN_TYPE but that was
  161. wrong. Not sure if TYPE_TARGET_TYPE is correct though. */
  162. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level,
  163. flags);
  164. }
  165. else
  166. {
  167. struct type *target = TYPE_TARGET_TYPE (type);
  168. gdb_printf (stream, "[");
  169. print_type_scalar (target, type->bounds ()->low.const_val (), stream);
  170. gdb_printf (stream, "..");
  171. print_type_scalar (target, type->bounds ()->high.const_val (), stream);
  172. gdb_printf (stream, "]");
  173. }
  174. }
  175. static void
  176. m2_typedef (struct type *type, struct ui_file *stream, int show,
  177. int level, const struct type_print_options *flags)
  178. {
  179. if (type->name () != NULL)
  180. {
  181. gdb_puts (type->name (), stream);
  182. gdb_puts (" = ", stream);
  183. }
  184. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  185. }
  186. /* m2_array - prints out a Modula-2 ARRAY ... OF type. */
  187. static void m2_array (struct type *type, struct ui_file *stream,
  188. int show, int level, const struct type_print_options *flags)
  189. {
  190. gdb_printf (stream, "ARRAY [");
  191. if (TYPE_LENGTH (TYPE_TARGET_TYPE (type)) > 0
  192. && type->bounds ()->high.kind () != PROP_UNDEFINED)
  193. {
  194. if (type->index_type () != 0)
  195. {
  196. m2_print_bounds (type->index_type (), stream, show, -1, 0);
  197. gdb_printf (stream, "..");
  198. m2_print_bounds (type->index_type (), stream, show, -1, 1);
  199. }
  200. else
  201. gdb_puts (pulongest ((TYPE_LENGTH (type)
  202. / TYPE_LENGTH (TYPE_TARGET_TYPE (type)))),
  203. stream);
  204. }
  205. gdb_printf (stream, "] OF ");
  206. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  207. }
  208. static void
  209. m2_pointer (struct type *type, struct ui_file *stream, int show,
  210. int level, const struct type_print_options *flags)
  211. {
  212. if (TYPE_CONST (type))
  213. gdb_printf (stream, "[...] : ");
  214. else
  215. gdb_printf (stream, "POINTER TO ");
  216. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  217. }
  218. static void
  219. m2_ref (struct type *type, struct ui_file *stream, int show,
  220. int level, const struct type_print_options *flags)
  221. {
  222. gdb_printf (stream, "VAR");
  223. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, show, level, flags);
  224. }
  225. static void
  226. m2_unknown (const char *s, struct type *type, struct ui_file *stream,
  227. int show, int level)
  228. {
  229. gdb_printf (stream, "%s %s", s, _("is unknown"));
  230. }
  231. static void m2_union (struct type *type, struct ui_file *stream)
  232. {
  233. gdb_printf (stream, "union");
  234. }
  235. static void
  236. m2_procedure (struct type *type, struct ui_file *stream,
  237. int show, int level, const struct type_print_options *flags)
  238. {
  239. gdb_printf (stream, "PROCEDURE ");
  240. m2_type_name (type, stream);
  241. if (TYPE_TARGET_TYPE (type) == NULL
  242. || TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_VOID)
  243. {
  244. int i, len = type->num_fields ();
  245. gdb_printf (stream, " (");
  246. for (i = 0; i < len; i++)
  247. {
  248. if (i > 0)
  249. {
  250. gdb_puts (", ", stream);
  251. stream->wrap_here (4);
  252. }
  253. m2_print_type (type->field (i).type (), "", stream, -1, 0, flags);
  254. }
  255. gdb_printf (stream, ") : ");
  256. if (TYPE_TARGET_TYPE (type) != NULL)
  257. m2_print_type (TYPE_TARGET_TYPE (type), "", stream, 0, 0, flags);
  258. else
  259. type_print_unknown_return_type (stream);
  260. }
  261. }
  262. static void
  263. m2_print_bounds (struct type *type,
  264. struct ui_file *stream, int show, int level,
  265. int print_high)
  266. {
  267. struct type *target = TYPE_TARGET_TYPE (type);
  268. if (type->num_fields () == 0)
  269. return;
  270. if (print_high)
  271. print_type_scalar (target, type->bounds ()->high.const_val (), stream);
  272. else
  273. print_type_scalar (target, type->bounds ()->low.const_val (), stream);
  274. }
  275. static void
  276. m2_short_set (struct type *type, struct ui_file *stream, int show, int level)
  277. {
  278. gdb_printf(stream, "SET [");
  279. m2_print_bounds (type->index_type (), stream,
  280. show - 1, level, 0);
  281. gdb_printf(stream, "..");
  282. m2_print_bounds (type->index_type (), stream,
  283. show - 1, level, 1);
  284. gdb_printf(stream, "]");
  285. }
  286. int
  287. m2_is_long_set (struct type *type)
  288. {
  289. LONGEST previous_high = 0; /* Unnecessary initialization
  290. keeps gcc -Wall happy. */
  291. int len, i;
  292. struct type *range;
  293. if (type->code () == TYPE_CODE_STRUCT)
  294. {
  295. /* check if all fields of the RECORD are consecutive sets. */
  296. len = type->num_fields ();
  297. for (i = TYPE_N_BASECLASSES (type); i < len; i++)
  298. {
  299. if (type->field (i).type () == NULL)
  300. return 0;
  301. if (type->field (i).type ()->code () != TYPE_CODE_SET)
  302. return 0;
  303. if (type->field (i).name () != NULL
  304. && (strcmp (type->field (i).name (), "") != 0))
  305. return 0;
  306. range = type->field (i).type ()->index_type ();
  307. if ((i > TYPE_N_BASECLASSES (type))
  308. && previous_high + 1 != range->bounds ()->low.const_val ())
  309. return 0;
  310. previous_high = range->bounds ()->high.const_val ();
  311. }
  312. return len>0;
  313. }
  314. return 0;
  315. }
  316. /* m2_get_discrete_bounds - a wrapper for get_discrete_bounds which
  317. understands that CHARs might be signed.
  318. This should be integrated into gdbtypes.c
  319. inside get_discrete_bounds. */
  320. static bool
  321. m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
  322. {
  323. type = check_typedef (type);
  324. switch (type->code ())
  325. {
  326. case TYPE_CODE_CHAR:
  327. if (TYPE_LENGTH (type) < sizeof (LONGEST))
  328. {
  329. if (!type->is_unsigned ())
  330. {
  331. *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
  332. *highp = -*lowp - 1;
  333. return 0;
  334. }
  335. }
  336. /* fall through */
  337. default:
  338. return get_discrete_bounds (type, lowp, highp);
  339. }
  340. }
  341. /* m2_is_long_set_of_type - returns TRUE if the long set was declared as
  342. SET OF <oftype> of_type is assigned to the
  343. subtype. */
  344. int
  345. m2_is_long_set_of_type (struct type *type, struct type **of_type)
  346. {
  347. int len, i;
  348. struct type *range;
  349. struct type *target;
  350. LONGEST l1, l2;
  351. LONGEST h1, h2;
  352. if (type->code () == TYPE_CODE_STRUCT)
  353. {
  354. len = type->num_fields ();
  355. i = TYPE_N_BASECLASSES (type);
  356. if (len == 0)
  357. return 0;
  358. range = type->field (i).type ()->index_type ();
  359. target = TYPE_TARGET_TYPE (range);
  360. l1 = type->field (i).type ()->bounds ()->low.const_val ();
  361. h1 = type->field (len - 1).type ()->bounds ()->high.const_val ();
  362. *of_type = target;
  363. if (m2_get_discrete_bounds (target, &l2, &h2))
  364. return (l1 == l2 && h1 == h2);
  365. error (_("long_set failed to find discrete bounds for its subtype"));
  366. return 0;
  367. }
  368. error (_("expecting long_set"));
  369. return 0;
  370. }
  371. static int
  372. m2_long_set (struct type *type, struct ui_file *stream, int show, int level,
  373. const struct type_print_options *flags)
  374. {
  375. struct type *of_type;
  376. int i;
  377. int len = type->num_fields ();
  378. LONGEST low;
  379. LONGEST high;
  380. if (m2_is_long_set (type))
  381. {
  382. if (type->name () != NULL)
  383. {
  384. gdb_puts (type->name (), stream);
  385. if (show == 0)
  386. return 1;
  387. gdb_puts (" = ", stream);
  388. }
  389. if (get_long_set_bounds (type, &low, &high))
  390. {
  391. gdb_printf(stream, "SET OF ");
  392. i = TYPE_N_BASECLASSES (type);
  393. if (m2_is_long_set_of_type (type, &of_type))
  394. m2_print_type (of_type, "", stream, show - 1, level, flags);
  395. else
  396. {
  397. gdb_printf(stream, "[");
  398. m2_print_bounds (type->field (i).type ()->index_type (),
  399. stream, show - 1, level, 0);
  400. gdb_printf(stream, "..");
  401. m2_print_bounds (type->field (len - 1).type ()->index_type (),
  402. stream, show - 1, level, 1);
  403. gdb_printf(stream, "]");
  404. }
  405. }
  406. else
  407. /* i18n: Do not translate the "SET OF" part! */
  408. gdb_printf(stream, _("SET OF <unknown>"));
  409. return 1;
  410. }
  411. return 0;
  412. }
  413. /* m2_is_unbounded_array - returns TRUE if, type, should be regarded
  414. as a Modula-2 unbounded ARRAY type. */
  415. int
  416. m2_is_unbounded_array (struct type *type)
  417. {
  418. if (type->code () == TYPE_CODE_STRUCT)
  419. {
  420. /*
  421. * check if we have a structure with exactly two fields named
  422. * _m2_contents and _m2_high. It also checks to see if the
  423. * type of _m2_contents is a pointer. The TYPE_TARGET_TYPE
  424. * of the pointer determines the unbounded ARRAY OF type.
  425. */
  426. if (type->num_fields () != 2)
  427. return 0;
  428. if (strcmp (type->field (0).name (), "_m2_contents") != 0)
  429. return 0;
  430. if (strcmp (type->field (1).name (), "_m2_high") != 0)
  431. return 0;
  432. if (type->field (0).type ()->code () != TYPE_CODE_PTR)
  433. return 0;
  434. return 1;
  435. }
  436. return 0;
  437. }
  438. /* m2_unbounded_array - if the struct type matches a Modula-2 unbounded
  439. parameter type then display the type as an
  440. ARRAY OF type. Returns TRUE if an unbounded
  441. array type was detected. */
  442. static int
  443. m2_unbounded_array (struct type *type, struct ui_file *stream, int show,
  444. int level, const struct type_print_options *flags)
  445. {
  446. if (m2_is_unbounded_array (type))
  447. {
  448. if (show > 0)
  449. {
  450. gdb_puts ("ARRAY OF ", stream);
  451. m2_print_type (TYPE_TARGET_TYPE (type->field (0).type ()),
  452. "", stream, 0, level, flags);
  453. }
  454. return 1;
  455. }
  456. return 0;
  457. }
  458. void
  459. m2_record_fields (struct type *type, struct ui_file *stream, int show,
  460. int level, const struct type_print_options *flags)
  461. {
  462. /* Print the tag if it exists. */
  463. if (type->name () != NULL)
  464. {
  465. if (!startswith (type->name (), "$$"))
  466. {
  467. gdb_puts (type->name (), stream);
  468. if (show > 0)
  469. gdb_printf (stream, " = ");
  470. }
  471. }
  472. stream->wrap_here (4);
  473. if (show < 0)
  474. {
  475. if (type->code () == TYPE_CODE_STRUCT)
  476. gdb_printf (stream, "RECORD ... END ");
  477. else if (type->code () == TYPE_CODE_UNION)
  478. gdb_printf (stream, "CASE ... END ");
  479. }
  480. else if (show > 0)
  481. {
  482. int i;
  483. int len = type->num_fields ();
  484. if (type->code () == TYPE_CODE_STRUCT)
  485. gdb_printf (stream, "RECORD\n");
  486. else if (type->code () == TYPE_CODE_UNION)
  487. /* i18n: Do not translate "CASE" and "OF". */
  488. gdb_printf (stream, _("CASE <variant> OF\n"));
  489. for (i = TYPE_N_BASECLASSES (type); i < len; i++)
  490. {
  491. QUIT;
  492. print_spaces (level + 4, stream);
  493. fputs_styled (type->field (i).name (),
  494. variable_name_style.style (), stream);
  495. gdb_puts (" : ", stream);
  496. m2_print_type (type->field (i).type (),
  497. "",
  498. stream, 0, level + 4, flags);
  499. if (TYPE_FIELD_PACKED (type, i))
  500. {
  501. /* It is a bitfield. This code does not attempt
  502. to look at the bitpos and reconstruct filler,
  503. unnamed fields. This would lead to misleading
  504. results if the compiler does not put out fields
  505. for such things (I don't know what it does). */
  506. gdb_printf (stream, " : %d",
  507. TYPE_FIELD_BITSIZE (type, i));
  508. }
  509. gdb_printf (stream, ";\n");
  510. }
  511. gdb_printf (stream, "%*sEND ", level, "");
  512. }
  513. }
  514. void
  515. m2_enum (struct type *type, struct ui_file *stream, int show, int level)
  516. {
  517. LONGEST lastval;
  518. int i, len;
  519. if (show < 0)
  520. {
  521. /* If we just printed a tag name, no need to print anything else. */
  522. if (type->name () == NULL)
  523. gdb_printf (stream, "(...)");
  524. }
  525. else if (show > 0 || type->name () == NULL)
  526. {
  527. gdb_printf (stream, "(");
  528. len = type->num_fields ();
  529. lastval = 0;
  530. for (i = 0; i < len; i++)
  531. {
  532. QUIT;
  533. if (i > 0)
  534. gdb_printf (stream, ", ");
  535. stream->wrap_here (4);
  536. fputs_styled (type->field (i).name (),
  537. variable_name_style.style (), stream);
  538. if (lastval != type->field (i).loc_enumval ())
  539. {
  540. gdb_printf (stream, " = %s",
  541. plongest (type->field (i).loc_enumval ()));
  542. lastval = type->field (i).loc_enumval ();
  543. }
  544. lastval++;
  545. }
  546. gdb_printf (stream, ")");
  547. }
  548. }