f-typeprint.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* Support for printing Fortran types for GDB, the GNU debugger.
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. Contributed by Motorola. Adapted from the C version by Farooq Butt
  4. (fmbutt@engage.sps.mot.com).
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "gdbsupport/gdb_obstack.h"
  18. #include "bfd.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "expression.h"
  22. #include "value.h"
  23. #include "gdbcore.h"
  24. #include "target.h"
  25. #include "f-lang.h"
  26. #include "typeprint.h"
  27. #include "cli/cli-style.h"
  28. /* See f-lang.h. */
  29. void
  30. f_language::print_typedef (struct type *type, struct symbol *new_symbol,
  31. struct ui_file *stream) const
  32. {
  33. type = check_typedef (type);
  34. print_type (type, "", stream, 0, 0, &type_print_raw_options);
  35. }
  36. /* See f-lang.h. */
  37. void
  38. f_language::print_type (struct type *type, const char *varstring,
  39. struct ui_file *stream, int show, int level,
  40. const struct type_print_options *flags) const
  41. {
  42. enum type_code code;
  43. f_type_print_base (type, stream, show, level);
  44. code = type->code ();
  45. if ((varstring != NULL && *varstring != '\0')
  46. /* Need a space if going to print stars or brackets; but not if we
  47. will print just a type name. */
  48. || ((show > 0
  49. || type->name () == 0)
  50. && (code == TYPE_CODE_FUNC
  51. || code == TYPE_CODE_METHOD
  52. || code == TYPE_CODE_ARRAY
  53. || ((code == TYPE_CODE_PTR
  54. || code == TYPE_CODE_REF)
  55. && (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_FUNC
  56. || (TYPE_TARGET_TYPE (type)->code ()
  57. == TYPE_CODE_METHOD)
  58. || (TYPE_TARGET_TYPE (type)->code ()
  59. == TYPE_CODE_ARRAY))))))
  60. gdb_puts (" ", stream);
  61. f_type_print_varspec_prefix (type, stream, show, 0);
  62. if (varstring != NULL)
  63. {
  64. int demangled_args;
  65. gdb_puts (varstring, stream);
  66. /* For demangled function names, we have the arglist as part of the name,
  67. so don't print an additional pair of ()'s. */
  68. demangled_args = (*varstring != '\0'
  69. && varstring[strlen (varstring) - 1] == ')');
  70. f_type_print_varspec_suffix (type, stream, show, 0, demangled_args, 0, false);
  71. }
  72. }
  73. /* See f-lang.h. */
  74. void
  75. f_language::f_type_print_varspec_prefix (struct type *type,
  76. struct ui_file *stream,
  77. int show, int passed_a_ptr) const
  78. {
  79. if (type == 0)
  80. return;
  81. if (type->name () && show <= 0)
  82. return;
  83. QUIT;
  84. switch (type->code ())
  85. {
  86. case TYPE_CODE_PTR:
  87. f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 1);
  88. break;
  89. case TYPE_CODE_FUNC:
  90. f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
  91. if (passed_a_ptr)
  92. gdb_printf (stream, "(");
  93. break;
  94. case TYPE_CODE_ARRAY:
  95. f_type_print_varspec_prefix (TYPE_TARGET_TYPE (type), stream, 0, 0);
  96. break;
  97. case TYPE_CODE_UNDEF:
  98. case TYPE_CODE_STRUCT:
  99. case TYPE_CODE_UNION:
  100. case TYPE_CODE_NAMELIST:
  101. case TYPE_CODE_ENUM:
  102. case TYPE_CODE_INT:
  103. case TYPE_CODE_FLT:
  104. case TYPE_CODE_VOID:
  105. case TYPE_CODE_ERROR:
  106. case TYPE_CODE_CHAR:
  107. case TYPE_CODE_BOOL:
  108. case TYPE_CODE_SET:
  109. case TYPE_CODE_RANGE:
  110. case TYPE_CODE_STRING:
  111. case TYPE_CODE_METHOD:
  112. case TYPE_CODE_REF:
  113. case TYPE_CODE_COMPLEX:
  114. case TYPE_CODE_TYPEDEF:
  115. /* These types need no prefix. They are listed here so that
  116. gcc -Wall will reveal any types that haven't been handled. */
  117. break;
  118. }
  119. }
  120. /* See f-lang.h. */
  121. void
  122. f_language::f_type_print_varspec_suffix (struct type *type,
  123. struct ui_file *stream,
  124. int show, int passed_a_ptr,
  125. int demangled_args,
  126. int arrayprint_recurse_level,
  127. bool print_rank_only) const
  128. {
  129. /* No static variables are permitted as an error call may occur during
  130. execution of this function. */
  131. if (type == 0)
  132. return;
  133. if (type->name () && show <= 0)
  134. return;
  135. QUIT;
  136. switch (type->code ())
  137. {
  138. case TYPE_CODE_ARRAY:
  139. arrayprint_recurse_level++;
  140. if (arrayprint_recurse_level == 1)
  141. gdb_printf (stream, "(");
  142. if (type_not_associated (type))
  143. print_rank_only = true;
  144. else if (type_not_allocated (type))
  145. print_rank_only = true;
  146. else if ((TYPE_ASSOCIATED_PROP (type)
  147. && PROP_CONST != TYPE_ASSOCIATED_PROP (type)->kind ())
  148. || (TYPE_ALLOCATED_PROP (type)
  149. && PROP_CONST != TYPE_ALLOCATED_PROP (type)->kind ())
  150. || (TYPE_DATA_LOCATION (type)
  151. && PROP_CONST != TYPE_DATA_LOCATION (type)->kind ()))
  152. {
  153. /* This case exist when we ptype a typename which has the dynamic
  154. properties but cannot be resolved as there is no object. */
  155. print_rank_only = true;
  156. }
  157. if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ARRAY)
  158. f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
  159. 0, 0, arrayprint_recurse_level,
  160. print_rank_only);
  161. if (print_rank_only)
  162. gdb_printf (stream, ":");
  163. else
  164. {
  165. LONGEST lower_bound = f77_get_lowerbound (type);
  166. if (lower_bound != 1) /* Not the default. */
  167. gdb_printf (stream, "%s:", plongest (lower_bound));
  168. /* Make sure that, if we have an assumed size array, we
  169. print out a warning and print the upperbound as '*'. */
  170. if (type->bounds ()->high.kind () == PROP_UNDEFINED)
  171. gdb_printf (stream, "*");
  172. else
  173. {
  174. LONGEST upper_bound = f77_get_upperbound (type);
  175. gdb_puts (plongest (upper_bound), stream);
  176. }
  177. }
  178. if (TYPE_TARGET_TYPE (type)->code () != TYPE_CODE_ARRAY)
  179. f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
  180. 0, 0, arrayprint_recurse_level,
  181. print_rank_only);
  182. if (arrayprint_recurse_level == 1)
  183. gdb_printf (stream, ")");
  184. else
  185. gdb_printf (stream, ",");
  186. arrayprint_recurse_level--;
  187. break;
  188. case TYPE_CODE_PTR:
  189. case TYPE_CODE_REF:
  190. f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0, 1, 0,
  191. arrayprint_recurse_level, false);
  192. gdb_printf (stream, " )");
  193. break;
  194. case TYPE_CODE_FUNC:
  195. {
  196. int i, nfields = type->num_fields ();
  197. f_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream, 0,
  198. passed_a_ptr, 0,
  199. arrayprint_recurse_level, false);
  200. if (passed_a_ptr)
  201. gdb_printf (stream, ") ");
  202. gdb_printf (stream, "(");
  203. if (nfields == 0 && type->is_prototyped ())
  204. print_type (builtin_f_type (type->arch ())->builtin_void,
  205. "", stream, -1, 0, 0);
  206. else
  207. for (i = 0; i < nfields; i++)
  208. {
  209. if (i > 0)
  210. {
  211. gdb_puts (", ", stream);
  212. stream->wrap_here (4);
  213. }
  214. print_type (type->field (i).type (), "", stream, -1, 0, 0);
  215. }
  216. gdb_printf (stream, ")");
  217. }
  218. break;
  219. case TYPE_CODE_UNDEF:
  220. case TYPE_CODE_STRUCT:
  221. case TYPE_CODE_UNION:
  222. case TYPE_CODE_NAMELIST:
  223. case TYPE_CODE_ENUM:
  224. case TYPE_CODE_INT:
  225. case TYPE_CODE_FLT:
  226. case TYPE_CODE_VOID:
  227. case TYPE_CODE_ERROR:
  228. case TYPE_CODE_CHAR:
  229. case TYPE_CODE_BOOL:
  230. case TYPE_CODE_SET:
  231. case TYPE_CODE_RANGE:
  232. case TYPE_CODE_STRING:
  233. case TYPE_CODE_METHOD:
  234. case TYPE_CODE_COMPLEX:
  235. case TYPE_CODE_TYPEDEF:
  236. /* These types do not need a suffix. They are listed so that
  237. gcc -Wall will report types that may not have been considered. */
  238. break;
  239. }
  240. }
  241. /* See f-lang.h. */
  242. void
  243. f_language::f_type_print_derivation_info (struct type *type,
  244. struct ui_file *stream) const
  245. {
  246. /* Fortran doesn't support multiple inheritance. */
  247. const int i = 0;
  248. if (TYPE_N_BASECLASSES (type) > 0)
  249. gdb_printf (stream, ", extends(%s) ::", TYPE_BASECLASS (type, i)->name ());
  250. }
  251. /* See f-lang.h. */
  252. void
  253. f_language::f_type_print_base (struct type *type, struct ui_file *stream,
  254. int show, int level) const
  255. {
  256. int index;
  257. QUIT;
  258. stream->wrap_here (4);
  259. if (type == NULL)
  260. {
  261. fputs_styled ("<type unknown>", metadata_style.style (), stream);
  262. return;
  263. }
  264. /* When SHOW is zero or less, and there is a valid type name, then always
  265. just print the type name directly from the type. */
  266. if ((show <= 0) && (type->name () != NULL))
  267. {
  268. const char *prefix = "";
  269. if (type->code () == TYPE_CODE_UNION)
  270. prefix = "Type, C_Union :: ";
  271. else if (type->code () == TYPE_CODE_STRUCT
  272. || type->code () == TYPE_CODE_NAMELIST)
  273. prefix = "Type ";
  274. gdb_printf (stream, "%*s%s%s", level, "", prefix, type->name ());
  275. return;
  276. }
  277. if (type->code () != TYPE_CODE_TYPEDEF)
  278. type = check_typedef (type);
  279. switch (type->code ())
  280. {
  281. case TYPE_CODE_TYPEDEF:
  282. f_type_print_base (TYPE_TARGET_TYPE (type), stream, 0, level);
  283. break;
  284. case TYPE_CODE_ARRAY:
  285. f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
  286. break;
  287. case TYPE_CODE_FUNC:
  288. if (TYPE_TARGET_TYPE (type) == NULL)
  289. type_print_unknown_return_type (stream);
  290. else
  291. f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, level);
  292. break;
  293. case TYPE_CODE_PTR:
  294. gdb_printf (stream, "%*sPTR TO -> ( ", level, "");
  295. f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
  296. break;
  297. case TYPE_CODE_REF:
  298. gdb_printf (stream, "%*sREF TO -> ( ", level, "");
  299. f_type_print_base (TYPE_TARGET_TYPE (type), stream, show, 0);
  300. break;
  301. case TYPE_CODE_VOID:
  302. {
  303. struct type *void_type = builtin_f_type (type->arch ())->builtin_void;
  304. gdb_printf (stream, "%*s%s", level, "", void_type->name ());
  305. }
  306. break;
  307. case TYPE_CODE_UNDEF:
  308. gdb_printf (stream, "%*sstruct <unknown>", level, "");
  309. break;
  310. case TYPE_CODE_ERROR:
  311. gdb_printf (stream, "%*s%s", level, "", TYPE_ERROR_NAME (type));
  312. break;
  313. case TYPE_CODE_RANGE:
  314. /* This should not occur. */
  315. gdb_printf (stream, "%*s<range type>", level, "");
  316. break;
  317. case TYPE_CODE_CHAR:
  318. case TYPE_CODE_INT:
  319. /* There may be some character types that attempt to come
  320. through as TYPE_CODE_INT since dbxstclass.h is so
  321. C-oriented, we must change these to "character" from "char". */
  322. if (strcmp (type->name (), "char") == 0)
  323. gdb_printf (stream, "%*scharacter", level, "");
  324. else
  325. goto default_case;
  326. break;
  327. case TYPE_CODE_STRING:
  328. /* Strings may have dynamic upperbounds (lengths) like arrays. We
  329. check specifically for the PROP_CONST case to indicate that the
  330. dynamic type has been resolved. If we arrive here having been
  331. asked to print the type of a value with a dynamic type then the
  332. bounds will not have been resolved. */
  333. if (type->bounds ()->high.kind () == PROP_CONST)
  334. {
  335. LONGEST upper_bound = f77_get_upperbound (type);
  336. gdb_printf (stream, "character*%s", pulongest (upper_bound));
  337. }
  338. else
  339. gdb_printf (stream, "%*scharacter*(*)", level, "");
  340. break;
  341. case TYPE_CODE_STRUCT:
  342. case TYPE_CODE_UNION:
  343. case TYPE_CODE_NAMELIST:
  344. if (type->code () == TYPE_CODE_UNION)
  345. gdb_printf (stream, "%*sType, C_Union ::", level, "");
  346. else
  347. gdb_printf (stream, "%*sType", level, "");
  348. if (show > 0)
  349. f_type_print_derivation_info (type, stream);
  350. gdb_puts (" ", stream);
  351. gdb_puts (type->name (), stream);
  352. /* According to the definition,
  353. we only print structure elements in case show > 0. */
  354. if (show > 0)
  355. {
  356. gdb_puts ("\n", stream);
  357. for (index = 0; index < type->num_fields (); index++)
  358. {
  359. f_type_print_base (type->field (index).type (), stream,
  360. show - 1, level + 4);
  361. gdb_puts (" :: ", stream);
  362. fputs_styled (type->field (index).name (),
  363. variable_name_style.style (), stream);
  364. f_type_print_varspec_suffix (type->field (index).type (),
  365. stream, show - 1, 0, 0, 0, false);
  366. gdb_puts ("\n", stream);
  367. }
  368. gdb_printf (stream, "%*sEnd Type ", level, "");
  369. gdb_puts (type->name (), stream);
  370. }
  371. break;
  372. case TYPE_CODE_MODULE:
  373. gdb_printf (stream, "%*smodule %s", level, "", type->name ());
  374. break;
  375. default_case:
  376. default:
  377. /* Handle types not explicitly handled by the other cases,
  378. such as fundamental types. For these, just print whatever
  379. the type name is, as recorded in the type itself. If there
  380. is no type name, then complain. */
  381. if (type->name () != NULL)
  382. gdb_printf (stream, "%*s%s", level, "", type->name ());
  383. else
  384. error (_("Invalid type code (%d) in symbol table."), type->code ());
  385. break;
  386. }
  387. if (TYPE_IS_ALLOCATABLE (type))
  388. gdb_printf (stream, ", allocatable");
  389. }