c-typeprint.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723
  1. /* Support for printing C and C++ 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 "target.h"
  23. #include "language.h"
  24. #include "demangle.h"
  25. #include "c-lang.h"
  26. #include "cli/cli-style.h"
  27. #include "typeprint.h"
  28. #include "cp-abi.h"
  29. #include "cp-support.h"
  30. /* A list of access specifiers used for printing. */
  31. enum access_specifier
  32. {
  33. s_none,
  34. s_public,
  35. s_private,
  36. s_protected
  37. };
  38. static void c_type_print_varspec_suffix (struct type *, struct ui_file *, int,
  39. int, int,
  40. enum language,
  41. const struct type_print_options *);
  42. static void c_type_print_varspec_prefix (struct type *,
  43. struct ui_file *,
  44. int, int, int,
  45. enum language,
  46. const struct type_print_options *,
  47. struct print_offset_data *);
  48. /* Print "const", "volatile", or address space modifiers. */
  49. static void c_type_print_modifier (struct type *,
  50. struct ui_file *,
  51. int, int, enum language);
  52. static void c_type_print_base_1 (struct type *type, struct ui_file *stream,
  53. int show, int level, enum language language,
  54. const struct type_print_options *flags,
  55. struct print_offset_data *podata);
  56. /* A callback function for cp_canonicalize_string_full that uses
  57. typedef_hash_table::find_typedef. */
  58. static const char *
  59. find_typedef_for_canonicalize (struct type *t, void *data)
  60. {
  61. return typedef_hash_table::find_typedef
  62. ((const struct type_print_options *) data, t);
  63. }
  64. /* Print NAME on STREAM. If the 'raw' field of FLAGS is not set,
  65. canonicalize NAME using the local typedefs first. */
  66. static void
  67. print_name_maybe_canonical (const char *name,
  68. const struct type_print_options *flags,
  69. struct ui_file *stream)
  70. {
  71. gdb::unique_xmalloc_ptr<char> s;
  72. if (!flags->raw)
  73. s = cp_canonicalize_string_full (name,
  74. find_typedef_for_canonicalize,
  75. (void *) flags);
  76. gdb_puts (s != nullptr ? s.get () : name, stream);
  77. }
  78. /* Helper function for c_print_type. */
  79. static void
  80. c_print_type_1 (struct type *type,
  81. const char *varstring,
  82. struct ui_file *stream,
  83. int show, int level,
  84. enum language language,
  85. const struct type_print_options *flags,
  86. struct print_offset_data *podata)
  87. {
  88. enum type_code code;
  89. int demangled_args;
  90. int need_post_space;
  91. const char *local_name;
  92. if (show > 0)
  93. type = check_typedef (type);
  94. local_name = typedef_hash_table::find_typedef (flags, type);
  95. code = type->code ();
  96. if (local_name != NULL)
  97. {
  98. c_type_print_modifier (type, stream, 0, 1, language);
  99. gdb_puts (local_name, stream);
  100. if (varstring != NULL && *varstring != '\0')
  101. gdb_puts (" ", stream);
  102. }
  103. else
  104. {
  105. c_type_print_base_1 (type, stream, show, level, language, flags, podata);
  106. if ((varstring != NULL && *varstring != '\0')
  107. /* Need a space if going to print stars or brackets;
  108. but not if we will print just a type name. */
  109. || ((show > 0 || type->name () == 0)
  110. && (code == TYPE_CODE_PTR || code == TYPE_CODE_FUNC
  111. || code == TYPE_CODE_METHOD
  112. || (code == TYPE_CODE_ARRAY
  113. && !type->is_vector ())
  114. || code == TYPE_CODE_MEMBERPTR
  115. || code == TYPE_CODE_METHODPTR
  116. || TYPE_IS_REFERENCE (type))))
  117. gdb_puts (" ", stream);
  118. need_post_space = (varstring != NULL && strcmp (varstring, "") != 0);
  119. c_type_print_varspec_prefix (type, stream, show, 0, need_post_space,
  120. language, flags, podata);
  121. }
  122. if (varstring != NULL)
  123. {
  124. if (code == TYPE_CODE_FUNC || code == TYPE_CODE_METHOD)
  125. fputs_styled (varstring, function_name_style.style (), stream);
  126. else
  127. fputs_styled (varstring, variable_name_style.style (), stream);
  128. /* For demangled function names, we have the arglist as part of
  129. the name, so don't print an additional pair of ()'s. */
  130. if (local_name == NULL)
  131. {
  132. demangled_args = strchr (varstring, '(') != NULL;
  133. c_type_print_varspec_suffix (type, stream, show,
  134. 0, demangled_args,
  135. language, flags);
  136. }
  137. }
  138. }
  139. /* LEVEL is the depth to indent lines by. */
  140. void
  141. c_print_type (struct type *type,
  142. const char *varstring,
  143. struct ui_file *stream,
  144. int show, int level,
  145. const struct type_print_options *flags)
  146. {
  147. struct print_offset_data podata (flags);
  148. c_print_type_1 (type, varstring, stream, show, level,
  149. current_language->la_language, flags, &podata);
  150. }
  151. /* See c-lang.h. */
  152. void
  153. c_print_type (struct type *type,
  154. const char *varstring,
  155. struct ui_file *stream,
  156. int show, int level,
  157. enum language language,
  158. const struct type_print_options *flags)
  159. {
  160. struct print_offset_data podata (flags);
  161. c_print_type_1 (type, varstring, stream, show, level, language, flags,
  162. &podata);
  163. }
  164. /* Print a typedef using C syntax. TYPE is the underlying type.
  165. NEW_SYMBOL is the symbol naming the type. STREAM is the stream on
  166. which to print. */
  167. void
  168. c_print_typedef (struct type *type,
  169. struct symbol *new_symbol,
  170. struct ui_file *stream)
  171. {
  172. type = check_typedef (type);
  173. gdb_printf (stream, "typedef ");
  174. type_print (type, "", stream, -1);
  175. if ((new_symbol->type ())->name () == 0
  176. || strcmp ((new_symbol->type ())->name (),
  177. new_symbol->linkage_name ()) != 0
  178. || new_symbol->type ()->code () == TYPE_CODE_TYPEDEF)
  179. gdb_printf (stream, " %s", new_symbol->print_name ());
  180. gdb_printf (stream, ";");
  181. }
  182. /* If TYPE is a derived type, then print out derivation information.
  183. Print only the actual base classes of this type, not the base
  184. classes of the base classes. I.e. for the derivation hierarchy:
  185. class A { int a; };
  186. class B : public A {int b; };
  187. class C : public B {int c; };
  188. Print the type of class C as:
  189. class C : public B {
  190. int c;
  191. }
  192. Not as the following (like gdb used to), which is not legal C++
  193. syntax for derived types and may be confused with the multiple
  194. inheritance form:
  195. class C : public B : public A {
  196. int c;
  197. }
  198. In general, gdb should try to print the types as closely as
  199. possible to the form that they appear in the source code. */
  200. static void
  201. cp_type_print_derivation_info (struct ui_file *stream,
  202. struct type *type,
  203. const struct type_print_options *flags)
  204. {
  205. const char *name;
  206. int i;
  207. for (i = 0; i < TYPE_N_BASECLASSES (type); i++)
  208. {
  209. stream->wrap_here (8);
  210. gdb_puts (i == 0 ? ": " : ", ", stream);
  211. gdb_printf (stream, "%s%s ",
  212. BASETYPE_VIA_PUBLIC (type, i)
  213. ? "public" : (TYPE_FIELD_PROTECTED (type, i)
  214. ? "protected" : "private"),
  215. BASETYPE_VIA_VIRTUAL (type, i) ? " virtual" : "");
  216. name = TYPE_BASECLASS (type, i)->name ();
  217. if (name)
  218. print_name_maybe_canonical (name, flags, stream);
  219. else
  220. gdb_printf (stream, "(null)");
  221. }
  222. if (i > 0)
  223. {
  224. gdb_puts (" ", stream);
  225. }
  226. }
  227. /* Print the C++ method arguments ARGS to the file STREAM. */
  228. static void
  229. cp_type_print_method_args (struct type *mtype, const char *prefix,
  230. const char *varstring, int staticp,
  231. struct ui_file *stream,
  232. enum language language,
  233. const struct type_print_options *flags)
  234. {
  235. struct field *args = mtype->fields ();
  236. int nargs = mtype->num_fields ();
  237. int varargs = mtype->has_varargs ();
  238. int i;
  239. fprintf_symbol (stream, prefix,
  240. language_cplus, DMGL_ANSI);
  241. fprintf_symbol (stream, varstring,
  242. language_cplus, DMGL_ANSI);
  243. gdb_puts ("(", stream);
  244. /* Skip the class variable. We keep this here to accommodate older
  245. compilers and debug formats which may not support artificial
  246. parameters. */
  247. i = staticp ? 0 : 1;
  248. if (nargs > i)
  249. {
  250. while (i < nargs)
  251. {
  252. struct field arg = args[i++];
  253. /* Skip any artificial arguments. */
  254. if (FIELD_ARTIFICIAL (arg))
  255. continue;
  256. c_print_type (arg.type (), "", stream, 0, 0, flags);
  257. if (i == nargs && varargs)
  258. gdb_printf (stream, ", ...");
  259. else if (i < nargs)
  260. {
  261. gdb_printf (stream, ", ");
  262. stream->wrap_here (8);
  263. }
  264. }
  265. }
  266. else if (varargs)
  267. gdb_printf (stream, "...");
  268. else if (language == language_cplus)
  269. gdb_printf (stream, "void");
  270. gdb_printf (stream, ")");
  271. /* For non-static methods, read qualifiers from the type of
  272. THIS. */
  273. if (!staticp)
  274. {
  275. struct type *domain;
  276. gdb_assert (nargs > 0);
  277. gdb_assert (args[0].type ()->code () == TYPE_CODE_PTR);
  278. domain = TYPE_TARGET_TYPE (args[0].type ());
  279. if (TYPE_CONST (domain))
  280. gdb_printf (stream, " const");
  281. if (TYPE_VOLATILE (domain))
  282. gdb_printf (stream, " volatile");
  283. if (TYPE_RESTRICT (domain))
  284. gdb_printf (stream, (language == language_cplus
  285. ? " __restrict__"
  286. : " restrict"));
  287. if (TYPE_ATOMIC (domain))
  288. gdb_printf (stream, " _Atomic");
  289. }
  290. }
  291. /* Print any asterisks or open-parentheses needed before the
  292. variable name (to describe its type).
  293. On outermost call, pass 0 for PASSED_A_PTR.
  294. On outermost call, SHOW > 0 means should ignore
  295. any typename for TYPE and show its details.
  296. SHOW is always zero on recursive calls.
  297. NEED_POST_SPACE is non-zero when a space will be be needed
  298. between a trailing qualifier and a field, variable, or function
  299. name. */
  300. static void
  301. c_type_print_varspec_prefix (struct type *type,
  302. struct ui_file *stream,
  303. int show, int passed_a_ptr,
  304. int need_post_space,
  305. enum language language,
  306. const struct type_print_options *flags,
  307. struct print_offset_data *podata)
  308. {
  309. const char *name;
  310. if (type == 0)
  311. return;
  312. if (type->name () && show <= 0)
  313. return;
  314. QUIT;
  315. switch (type->code ())
  316. {
  317. case TYPE_CODE_PTR:
  318. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  319. stream, show, 1, 1, language, flags,
  320. podata);
  321. gdb_printf (stream, "*");
  322. c_type_print_modifier (type, stream, 1, need_post_space, language);
  323. break;
  324. case TYPE_CODE_MEMBERPTR:
  325. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  326. stream, show, 0, 0, language, flags, podata);
  327. name = TYPE_SELF_TYPE (type)->name ();
  328. if (name)
  329. print_name_maybe_canonical (name, flags, stream);
  330. else
  331. c_type_print_base_1 (TYPE_SELF_TYPE (type),
  332. stream, -1, passed_a_ptr, language, flags,
  333. podata);
  334. gdb_printf (stream, "::*");
  335. break;
  336. case TYPE_CODE_METHODPTR:
  337. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  338. stream, show, 0, 0, language, flags,
  339. podata);
  340. gdb_printf (stream, "(");
  341. name = TYPE_SELF_TYPE (type)->name ();
  342. if (name)
  343. print_name_maybe_canonical (name, flags, stream);
  344. else
  345. c_type_print_base_1 (TYPE_SELF_TYPE (type),
  346. stream, -1, passed_a_ptr, language, flags,
  347. podata);
  348. gdb_printf (stream, "::*");
  349. break;
  350. case TYPE_CODE_REF:
  351. case TYPE_CODE_RVALUE_REF:
  352. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  353. stream, show, 1, 0, language, flags,
  354. podata);
  355. gdb_printf (stream, type->code () == TYPE_CODE_REF ? "&" : "&&");
  356. c_type_print_modifier (type, stream, 1, need_post_space, language);
  357. break;
  358. case TYPE_CODE_METHOD:
  359. case TYPE_CODE_FUNC:
  360. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  361. stream, show, 0, 0, language, flags,
  362. podata);
  363. if (passed_a_ptr)
  364. gdb_printf (stream, "(");
  365. break;
  366. case TYPE_CODE_ARRAY:
  367. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  368. stream, show, 0, need_post_space,
  369. language, flags, podata);
  370. if (passed_a_ptr)
  371. gdb_printf (stream, "(");
  372. break;
  373. case TYPE_CODE_TYPEDEF:
  374. c_type_print_varspec_prefix (TYPE_TARGET_TYPE (type),
  375. stream, show, passed_a_ptr, 0,
  376. language, flags, podata);
  377. break;
  378. case TYPE_CODE_UNDEF:
  379. case TYPE_CODE_STRUCT:
  380. case TYPE_CODE_UNION:
  381. case TYPE_CODE_ENUM:
  382. case TYPE_CODE_FLAGS:
  383. case TYPE_CODE_INT:
  384. case TYPE_CODE_FLT:
  385. case TYPE_CODE_VOID:
  386. case TYPE_CODE_ERROR:
  387. case TYPE_CODE_CHAR:
  388. case TYPE_CODE_BOOL:
  389. case TYPE_CODE_SET:
  390. case TYPE_CODE_RANGE:
  391. case TYPE_CODE_STRING:
  392. case TYPE_CODE_COMPLEX:
  393. case TYPE_CODE_NAMESPACE:
  394. case TYPE_CODE_DECFLOAT:
  395. case TYPE_CODE_FIXED_POINT:
  396. /* These types need no prefix. They are listed here so that
  397. gcc -Wall will reveal any types that haven't been handled. */
  398. break;
  399. default:
  400. error (_("type not handled in c_type_print_varspec_prefix()"));
  401. break;
  402. }
  403. }
  404. /* Print out "const" and "volatile" attributes,
  405. and address space id if present.
  406. TYPE is a pointer to the type being printed out.
  407. STREAM is the output destination.
  408. NEED_PRE_SPACE = 1 indicates an initial white space is needed.
  409. NEED_POST_SPACE = 1 indicates a final white space is needed. */
  410. static void
  411. c_type_print_modifier (struct type *type, struct ui_file *stream,
  412. int need_pre_space, int need_post_space,
  413. enum language language)
  414. {
  415. int did_print_modifier = 0;
  416. const char *address_space_id;
  417. /* We don't print `const' qualifiers for references --- since all
  418. operators affect the thing referenced, not the reference itself,
  419. every reference is `const'. */
  420. if (TYPE_CONST (type) && !TYPE_IS_REFERENCE (type))
  421. {
  422. if (need_pre_space)
  423. gdb_printf (stream, " ");
  424. gdb_printf (stream, "const");
  425. did_print_modifier = 1;
  426. }
  427. if (TYPE_VOLATILE (type))
  428. {
  429. if (did_print_modifier || need_pre_space)
  430. gdb_printf (stream, " ");
  431. gdb_printf (stream, "volatile");
  432. did_print_modifier = 1;
  433. }
  434. if (TYPE_RESTRICT (type))
  435. {
  436. if (did_print_modifier || need_pre_space)
  437. gdb_printf (stream, " ");
  438. gdb_printf (stream, (language == language_cplus
  439. ? "__restrict__"
  440. : "restrict"));
  441. did_print_modifier = 1;
  442. }
  443. if (TYPE_ATOMIC (type))
  444. {
  445. if (did_print_modifier || need_pre_space)
  446. gdb_printf (stream, " ");
  447. gdb_printf (stream, "_Atomic");
  448. did_print_modifier = 1;
  449. }
  450. address_space_id
  451. = address_space_type_instance_flags_to_name (type->arch (),
  452. type->instance_flags ());
  453. if (address_space_id)
  454. {
  455. if (did_print_modifier || need_pre_space)
  456. gdb_printf (stream, " ");
  457. gdb_printf (stream, "@%s", address_space_id);
  458. did_print_modifier = 1;
  459. }
  460. if (did_print_modifier && need_post_space)
  461. gdb_printf (stream, " ");
  462. }
  463. /* Print out the arguments of TYPE, which should have TYPE_CODE_METHOD
  464. or TYPE_CODE_FUNC, to STREAM. Artificial arguments, such as "this"
  465. in non-static methods, are displayed if LINKAGE_NAME is zero. If
  466. LINKAGE_NAME is non-zero and LANGUAGE is language_cplus the topmost
  467. parameter types get removed their possible const and volatile qualifiers to
  468. match demangled linkage name parameters part of such function type.
  469. LANGUAGE is the language in which TYPE was defined. This is a necessary
  470. evil since this code is used by the C and C++. */
  471. void
  472. c_type_print_args (struct type *type, struct ui_file *stream,
  473. int linkage_name, enum language language,
  474. const struct type_print_options *flags)
  475. {
  476. int i;
  477. int printed_any = 0;
  478. gdb_printf (stream, "(");
  479. for (i = 0; i < type->num_fields (); i++)
  480. {
  481. struct type *param_type;
  482. if (TYPE_FIELD_ARTIFICIAL (type, i) && linkage_name)
  483. continue;
  484. if (printed_any)
  485. {
  486. gdb_printf (stream, ", ");
  487. stream->wrap_here (4);
  488. }
  489. param_type = type->field (i).type ();
  490. if (language == language_cplus && linkage_name)
  491. {
  492. /* C++ standard, 13.1 Overloadable declarations, point 3, item:
  493. - Parameter declarations that differ only in the presence or
  494. absence of const and/or volatile are equivalent.
  495. And the const/volatile qualifiers are not present in the mangled
  496. names as produced by GCC. */
  497. param_type = make_cv_type (0, 0, param_type, NULL);
  498. }
  499. c_print_type (param_type, "", stream, -1, 0, language, flags);
  500. printed_any = 1;
  501. }
  502. if (printed_any && type->has_varargs ())
  503. {
  504. /* Print out a trailing ellipsis for varargs functions. Ignore
  505. TYPE_VARARGS if the function has no named arguments; that
  506. represents unprototyped (K&R style) C functions. */
  507. if (printed_any && type->has_varargs ())
  508. {
  509. gdb_printf (stream, ", ");
  510. stream->wrap_here (4);
  511. gdb_printf (stream, "...");
  512. }
  513. }
  514. else if (!printed_any
  515. && (type->is_prototyped () || language == language_cplus))
  516. gdb_printf (stream, "void");
  517. gdb_printf (stream, ")");
  518. }
  519. /* Return true iff the j'th overloading of the i'th method of TYPE
  520. is a type conversion operator, like `operator int () { ... }'.
  521. When listing a class's methods, we don't print the return type of
  522. such operators. */
  523. static int
  524. is_type_conversion_operator (struct type *type, int i, int j)
  525. {
  526. /* I think the whole idea of recognizing type conversion operators
  527. by their name is pretty terrible. But I don't think our present
  528. data structure gives us any other way to tell. If you know of
  529. some other way, feel free to rewrite this function. */
  530. const char *name = TYPE_FN_FIELDLIST_NAME (type, i);
  531. if (!startswith (name, CP_OPERATOR_STR))
  532. return 0;
  533. name += 8;
  534. if (! strchr (" \t\f\n\r", *name))
  535. return 0;
  536. while (strchr (" \t\f\n\r", *name))
  537. name++;
  538. if (!('a' <= *name && *name <= 'z')
  539. && !('A' <= *name && *name <= 'Z')
  540. && *name != '_')
  541. /* If this doesn't look like the start of an identifier, then it
  542. isn't a type conversion operator. */
  543. return 0;
  544. else if (startswith (name, "new"))
  545. name += 3;
  546. else if (startswith (name, "delete"))
  547. name += 6;
  548. else
  549. /* If it doesn't look like new or delete, it's a type conversion
  550. operator. */
  551. return 1;
  552. /* Is that really the end of the name? */
  553. if (('a' <= *name && *name <= 'z')
  554. || ('A' <= *name && *name <= 'Z')
  555. || ('0' <= *name && *name <= '9')
  556. || *name == '_')
  557. /* No, so the identifier following "operator" must be a type name,
  558. and this is a type conversion operator. */
  559. return 1;
  560. /* That was indeed the end of the name, so it was `operator new' or
  561. `operator delete', neither of which are type conversion
  562. operators. */
  563. return 0;
  564. }
  565. /* Given a C++ qualified identifier QID, strip off the qualifiers,
  566. yielding the unqualified name. The return value is a pointer into
  567. the original string.
  568. It's a pity we don't have this information in some more structured
  569. form. Even the author of this function feels that writing little
  570. parsers like this everywhere is stupid. */
  571. static const char *
  572. remove_qualifiers (const char *qid)
  573. {
  574. int quoted = 0; /* Zero if we're not in quotes;
  575. '"' if we're in a double-quoted string;
  576. '\'' if we're in a single-quoted string. */
  577. int depth = 0; /* Number of unclosed parens we've seen. */
  578. char *parenstack = (char *) alloca (strlen (qid));
  579. const char *scan;
  580. const char *last = 0; /* The character after the rightmost
  581. `::' token we've seen so far. */
  582. for (scan = qid; *scan; scan++)
  583. {
  584. if (quoted)
  585. {
  586. if (*scan == quoted)
  587. quoted = 0;
  588. else if (*scan == '\\' && *(scan + 1))
  589. scan++;
  590. }
  591. else if (scan[0] == ':' && scan[1] == ':')
  592. {
  593. /* If we're inside parenthesis (i.e., an argument list) or
  594. angle brackets (i.e., a list of template arguments), then
  595. we don't record the position of this :: token, since it's
  596. not relevant to the top-level structure we're trying to
  597. operate on. */
  598. if (depth == 0)
  599. {
  600. last = scan + 2;
  601. scan++;
  602. }
  603. }
  604. else if (*scan == '"' || *scan == '\'')
  605. quoted = *scan;
  606. else if (*scan == '(')
  607. parenstack[depth++] = ')';
  608. else if (*scan == '[')
  609. parenstack[depth++] = ']';
  610. /* We're going to treat <> as a pair of matching characters,
  611. since we're more likely to see those in template id's than
  612. real less-than characters. What a crock. */
  613. else if (*scan == '<')
  614. parenstack[depth++] = '>';
  615. else if (*scan == ')' || *scan == ']' || *scan == '>')
  616. {
  617. if (depth > 0 && parenstack[depth - 1] == *scan)
  618. depth--;
  619. else
  620. {
  621. /* We're going to do a little error recovery here. If
  622. we don't find a match for *scan on the paren stack,
  623. but there is something lower on the stack that does
  624. match, we pop the stack to that point. */
  625. int i;
  626. for (i = depth - 1; i >= 0; i--)
  627. if (parenstack[i] == *scan)
  628. {
  629. depth = i;
  630. break;
  631. }
  632. }
  633. }
  634. }
  635. if (last)
  636. return last;
  637. else
  638. /* We didn't find any :: tokens at the top level, so declare the
  639. whole thing an unqualified identifier. */
  640. return qid;
  641. }
  642. /* Print any array sizes, function arguments or close parentheses
  643. needed after the variable name (to describe its type).
  644. Args work like c_type_print_varspec_prefix. */
  645. static void
  646. c_type_print_varspec_suffix (struct type *type,
  647. struct ui_file *stream,
  648. int show, int passed_a_ptr,
  649. int demangled_args,
  650. enum language language,
  651. const struct type_print_options *flags)
  652. {
  653. if (type == 0)
  654. return;
  655. if (type->name () && show <= 0)
  656. return;
  657. QUIT;
  658. switch (type->code ())
  659. {
  660. case TYPE_CODE_ARRAY:
  661. {
  662. LONGEST low_bound, high_bound;
  663. int is_vector = type->is_vector ();
  664. if (passed_a_ptr)
  665. gdb_printf (stream, ")");
  666. gdb_printf (stream, (is_vector ?
  667. " __attribute__ ((vector_size(" : "["));
  668. /* Bounds are not yet resolved, print a bounds placeholder instead. */
  669. if (type->bounds ()->high.kind () == PROP_LOCEXPR
  670. || type->bounds ()->high.kind () == PROP_LOCLIST)
  671. gdb_printf (stream, "variable length");
  672. else if (get_array_bounds (type, &low_bound, &high_bound))
  673. gdb_printf (stream, "%s",
  674. plongest (high_bound - low_bound + 1));
  675. gdb_printf (stream, (is_vector ? ")))" : "]"));
  676. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  677. show, 0, 0, language, flags);
  678. }
  679. break;
  680. case TYPE_CODE_MEMBERPTR:
  681. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  682. show, 0, 0, language, flags);
  683. break;
  684. case TYPE_CODE_METHODPTR:
  685. gdb_printf (stream, ")");
  686. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  687. show, 0, 0, language, flags);
  688. break;
  689. case TYPE_CODE_PTR:
  690. case TYPE_CODE_REF:
  691. case TYPE_CODE_RVALUE_REF:
  692. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  693. show, 1, 0, language, flags);
  694. break;
  695. case TYPE_CODE_METHOD:
  696. case TYPE_CODE_FUNC:
  697. if (passed_a_ptr)
  698. gdb_printf (stream, ")");
  699. if (!demangled_args)
  700. c_type_print_args (type, stream, 0, language, flags);
  701. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  702. show, passed_a_ptr, 0, language, flags);
  703. break;
  704. case TYPE_CODE_TYPEDEF:
  705. c_type_print_varspec_suffix (TYPE_TARGET_TYPE (type), stream,
  706. show, passed_a_ptr, 0, language, flags);
  707. break;
  708. case TYPE_CODE_UNDEF:
  709. case TYPE_CODE_STRUCT:
  710. case TYPE_CODE_UNION:
  711. case TYPE_CODE_FLAGS:
  712. case TYPE_CODE_ENUM:
  713. case TYPE_CODE_INT:
  714. case TYPE_CODE_FLT:
  715. case TYPE_CODE_VOID:
  716. case TYPE_CODE_ERROR:
  717. case TYPE_CODE_CHAR:
  718. case TYPE_CODE_BOOL:
  719. case TYPE_CODE_SET:
  720. case TYPE_CODE_RANGE:
  721. case TYPE_CODE_STRING:
  722. case TYPE_CODE_COMPLEX:
  723. case TYPE_CODE_NAMESPACE:
  724. case TYPE_CODE_DECFLOAT:
  725. case TYPE_CODE_FIXED_POINT:
  726. /* These types do not need a suffix. They are listed so that
  727. gcc -Wall will report types that may not have been
  728. considered. */
  729. break;
  730. default:
  731. error (_("type not handled in c_type_print_varspec_suffix()"));
  732. break;
  733. }
  734. }
  735. /* A helper for c_type_print_base that displays template
  736. parameters and their bindings, if needed.
  737. TABLE is the local bindings table to use. If NULL, no printing is
  738. done. Note that, at this point, TABLE won't have any useful
  739. information in it -- but it is also used as a flag to
  740. print_name_maybe_canonical to activate searching the global typedef
  741. table.
  742. TYPE is the type whose template arguments are being displayed.
  743. STREAM is the stream on which to print. */
  744. static void
  745. c_type_print_template_args (const struct type_print_options *flags,
  746. struct type *type, struct ui_file *stream)
  747. {
  748. int first = 1, i;
  749. if (flags->raw)
  750. return;
  751. for (i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
  752. {
  753. struct symbol *sym = TYPE_TEMPLATE_ARGUMENT (type, i);
  754. if (sym->aclass () != LOC_TYPEDEF)
  755. continue;
  756. if (first)
  757. {
  758. stream->wrap_here (4);
  759. gdb_printf (stream, _("[with %s = "), sym->linkage_name ());
  760. first = 0;
  761. }
  762. else
  763. {
  764. gdb_puts (", ", stream);
  765. stream->wrap_here (9);
  766. gdb_printf (stream, "%s = ", sym->linkage_name ());
  767. }
  768. c_print_type (sym->type (), "", stream, -1, 0, flags);
  769. }
  770. if (!first)
  771. gdb_puts (_("] "), stream);
  772. }
  773. /* Use 'print_spaces', but take into consideration the
  774. type_print_options FLAGS in order to determine how many whitespaces
  775. will be printed. */
  776. static void
  777. print_spaces_filtered_with_print_options
  778. (int level, struct ui_file *stream, const struct type_print_options *flags)
  779. {
  780. if (!flags->print_offsets)
  781. print_spaces (level, stream);
  782. else
  783. print_spaces (level + print_offset_data::indentation, stream);
  784. }
  785. /* Output an access specifier to STREAM, if needed. LAST_ACCESS is the
  786. last access specifier output (typically returned by this function). */
  787. static enum access_specifier
  788. output_access_specifier (struct ui_file *stream,
  789. enum access_specifier last_access,
  790. int level, bool is_protected, bool is_private,
  791. const struct type_print_options *flags)
  792. {
  793. if (is_protected)
  794. {
  795. if (last_access != s_protected)
  796. {
  797. last_access = s_protected;
  798. print_spaces_filtered_with_print_options (level + 2, stream, flags);
  799. gdb_printf (stream, "protected:\n");
  800. }
  801. }
  802. else if (is_private)
  803. {
  804. if (last_access != s_private)
  805. {
  806. last_access = s_private;
  807. print_spaces_filtered_with_print_options (level + 2, stream, flags);
  808. gdb_printf (stream, "private:\n");
  809. }
  810. }
  811. else
  812. {
  813. if (last_access != s_public)
  814. {
  815. last_access = s_public;
  816. print_spaces_filtered_with_print_options (level + 2, stream, flags);
  817. gdb_printf (stream, "public:\n");
  818. }
  819. }
  820. return last_access;
  821. }
  822. /* Return true if an access label (i.e., "public:", "private:",
  823. "protected:") needs to be printed for TYPE. */
  824. static bool
  825. need_access_label_p (struct type *type)
  826. {
  827. if (type->is_declared_class ())
  828. {
  829. QUIT;
  830. for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
  831. if (!TYPE_FIELD_PRIVATE (type, i))
  832. return true;
  833. QUIT;
  834. for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
  835. for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
  836. if (!TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
  837. j), i))
  838. return true;
  839. QUIT;
  840. for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
  841. if (!TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
  842. return true;
  843. }
  844. else
  845. {
  846. QUIT;
  847. for (int i = TYPE_N_BASECLASSES (type); i < type->num_fields (); i++)
  848. if (TYPE_FIELD_PRIVATE (type, i) || TYPE_FIELD_PROTECTED (type, i))
  849. return true;
  850. QUIT;
  851. for (int j = 0; j < TYPE_NFN_FIELDS (type); j++)
  852. {
  853. QUIT;
  854. for (int i = 0; i < TYPE_FN_FIELDLIST_LENGTH (type, j); i++)
  855. if (TYPE_FN_FIELD_PROTECTED (TYPE_FN_FIELDLIST1 (type,
  856. j), i)
  857. || TYPE_FN_FIELD_PRIVATE (TYPE_FN_FIELDLIST1 (type,
  858. j),
  859. i))
  860. return true;
  861. }
  862. QUIT;
  863. for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); ++i)
  864. if (TYPE_TYPEDEF_FIELD_PROTECTED (type, i)
  865. || TYPE_TYPEDEF_FIELD_PRIVATE (type, i))
  866. return true;
  867. }
  868. return false;
  869. }
  870. /* Helper function that temporarily disables FLAGS->PRINT_OFFSETS,
  871. calls 'c_print_type_1', and then reenables FLAGS->PRINT_OFFSETS if
  872. applicable. */
  873. static void
  874. c_print_type_no_offsets (struct type *type,
  875. const char *varstring,
  876. struct ui_file *stream,
  877. int show, int level,
  878. enum language language,
  879. struct type_print_options *flags,
  880. struct print_offset_data *podata)
  881. {
  882. unsigned int old_po = flags->print_offsets;
  883. /* Temporarily disable print_offsets, because it would mess with
  884. indentation. */
  885. flags->print_offsets = 0;
  886. c_print_type_1 (type, varstring, stream, show, level, language, flags,
  887. podata);
  888. flags->print_offsets = old_po;
  889. }
  890. /* Helper for 'c_type_print_base' that handles structs and unions.
  891. For a description of the arguments, see 'c_type_print_base'. */
  892. static void
  893. c_type_print_base_struct_union (struct type *type, struct ui_file *stream,
  894. int show, int level,
  895. enum language language,
  896. const struct type_print_options *flags,
  897. struct print_offset_data *podata)
  898. {
  899. struct type_print_options local_flags = *flags;
  900. local_flags.local_typedefs = NULL;
  901. std::unique_ptr<typedef_hash_table> hash_holder;
  902. if (!flags->raw)
  903. {
  904. if (flags->local_typedefs)
  905. local_flags.local_typedefs
  906. = new typedef_hash_table (*flags->local_typedefs);
  907. else
  908. local_flags.local_typedefs = new typedef_hash_table ();
  909. hash_holder.reset (local_flags.local_typedefs);
  910. }
  911. c_type_print_modifier (type, stream, 0, 1, language);
  912. if (type->code () == TYPE_CODE_UNION)
  913. gdb_printf (stream, "union ");
  914. else if (type->is_declared_class ())
  915. gdb_printf (stream, "class ");
  916. else
  917. gdb_printf (stream, "struct ");
  918. /* Print the tag if it exists. The HP aCC compiler emits a
  919. spurious "{unnamed struct}"/"{unnamed union}"/"{unnamed
  920. enum}" tag for unnamed struct/union/enum's, which we don't
  921. want to print. */
  922. if (type->name () != NULL
  923. && !startswith (type->name (), "{unnamed"))
  924. {
  925. /* When printing the tag name, we are still effectively
  926. printing in the outer context, hence the use of FLAGS
  927. here. */
  928. print_name_maybe_canonical (type->name (), flags, stream);
  929. if (show > 0)
  930. gdb_puts (" ", stream);
  931. }
  932. if (show < 0)
  933. {
  934. /* If we just printed a tag name, no need to print anything
  935. else. */
  936. if (type->name () == NULL)
  937. gdb_printf (stream, "{...}");
  938. }
  939. else if (show > 0 || type->name () == NULL)
  940. {
  941. struct type *basetype;
  942. int vptr_fieldno;
  943. c_type_print_template_args (&local_flags, type, stream);
  944. /* Add in template parameters when printing derivation info. */
  945. if (local_flags.local_typedefs != NULL)
  946. local_flags.local_typedefs->add_template_parameters (type);
  947. cp_type_print_derivation_info (stream, type, &local_flags);
  948. /* This holds just the global typedefs and the template
  949. parameters. */
  950. struct type_print_options semi_local_flags = *flags;
  951. semi_local_flags.local_typedefs = NULL;
  952. std::unique_ptr<typedef_hash_table> semi_holder;
  953. if (local_flags.local_typedefs != nullptr)
  954. {
  955. semi_local_flags.local_typedefs
  956. = new typedef_hash_table (*local_flags.local_typedefs);
  957. semi_holder.reset (semi_local_flags.local_typedefs);
  958. /* Now add in the local typedefs. */
  959. local_flags.local_typedefs->recursively_update (type);
  960. }
  961. gdb_printf (stream, "{\n");
  962. if (type->num_fields () == 0 && TYPE_NFN_FIELDS (type) == 0
  963. && TYPE_TYPEDEF_FIELD_COUNT (type) == 0)
  964. {
  965. print_spaces_filtered_with_print_options (level + 4, stream, flags);
  966. if (type->is_stub ())
  967. gdb_printf (stream, _("%p[<incomplete type>%p]\n"),
  968. metadata_style.style ().ptr (), nullptr);
  969. else
  970. gdb_printf (stream, _("%p[<no data fields>%p]\n"),
  971. metadata_style.style ().ptr (), nullptr);
  972. }
  973. /* Start off with no specific section type, so we can print
  974. one for the first field we find, and use that section type
  975. thereafter until we find another type. */
  976. enum access_specifier section_type = s_none;
  977. /* For a class, if all members are private, there's no need
  978. for a "private:" label; similarly, for a struct or union
  979. masquerading as a class, if all members are public, there's
  980. no need for a "public:" label. */
  981. bool need_access_label = need_access_label_p (type);
  982. /* If there is a base class for this type,
  983. do not print the field that it occupies. */
  984. int len = type->num_fields ();
  985. vptr_fieldno = get_vptr_fieldno (type, &basetype);
  986. struct print_offset_data local_podata (flags);
  987. for (int i = TYPE_N_BASECLASSES (type); i < len; i++)
  988. {
  989. QUIT;
  990. /* If we have a virtual table pointer, omit it. Even if
  991. virtual table pointers are not specifically marked in
  992. the debug info, they should be artificial. */
  993. if ((i == vptr_fieldno && type == basetype)
  994. || TYPE_FIELD_ARTIFICIAL (type, i))
  995. continue;
  996. if (need_access_label)
  997. {
  998. section_type = output_access_specifier
  999. (stream, section_type, level,
  1000. TYPE_FIELD_PROTECTED (type, i),
  1001. TYPE_FIELD_PRIVATE (type, i), flags);
  1002. }
  1003. bool is_static = field_is_static (&type->field (i));
  1004. if (flags->print_offsets)
  1005. podata->update (type, i, stream);
  1006. print_spaces (level + 4, stream);
  1007. if (is_static)
  1008. gdb_printf (stream, "static ");
  1009. int newshow = show - 1;
  1010. if (!is_static && flags->print_offsets
  1011. && (type->field (i).type ()->code () == TYPE_CODE_STRUCT
  1012. || type->field (i).type ()->code () == TYPE_CODE_UNION))
  1013. {
  1014. /* If we're printing offsets and this field's type is
  1015. either a struct or an union, then we're interested in
  1016. expanding it. */
  1017. ++newshow;
  1018. /* Make sure we carry our offset when we expand the
  1019. struct/union. */
  1020. local_podata.offset_bitpos
  1021. = podata->offset_bitpos + type->field (i).loc_bitpos ();
  1022. /* We're entering a struct/union. Right now,
  1023. PODATA->END_BITPOS points right *after* the
  1024. struct/union. However, when printing the first field
  1025. of this inner struct/union, the end_bitpos we're
  1026. expecting is exactly at the beginning of the
  1027. struct/union. Therefore, we subtract the length of
  1028. the whole struct/union. */
  1029. local_podata.end_bitpos
  1030. = podata->end_bitpos
  1031. - TYPE_LENGTH (type->field (i).type ()) * TARGET_CHAR_BIT;
  1032. }
  1033. c_print_type_1 (type->field (i).type (),
  1034. type->field (i).name (),
  1035. stream, newshow, level + 4,
  1036. language, &local_flags, &local_podata);
  1037. if (!is_static && TYPE_FIELD_PACKED (type, i))
  1038. {
  1039. /* It is a bitfield. This code does not attempt
  1040. to look at the bitpos and reconstruct filler,
  1041. unnamed fields. This would lead to misleading
  1042. results if the compiler does not put out fields
  1043. for such things (I don't know what it does). */
  1044. gdb_printf (stream, " : %d",
  1045. TYPE_FIELD_BITSIZE (type, i));
  1046. }
  1047. gdb_printf (stream, ";\n");
  1048. }
  1049. /* If there are both fields and methods, put a blank line
  1050. between them. Make sure to count only method that we
  1051. will display; artificial methods will be hidden. */
  1052. len = TYPE_NFN_FIELDS (type);
  1053. if (!flags->print_methods)
  1054. len = 0;
  1055. int real_len = 0;
  1056. for (int i = 0; i < len; i++)
  1057. {
  1058. struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  1059. int len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
  1060. int j;
  1061. for (j = 0; j < len2; j++)
  1062. if (!TYPE_FN_FIELD_ARTIFICIAL (f, j))
  1063. real_len++;
  1064. }
  1065. if (real_len > 0 && section_type != s_none)
  1066. gdb_printf (stream, "\n");
  1067. /* C++: print out the methods. */
  1068. for (int i = 0; i < len; i++)
  1069. {
  1070. struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  1071. int j, len2 = TYPE_FN_FIELDLIST_LENGTH (type, i);
  1072. const char *method_name = TYPE_FN_FIELDLIST_NAME (type, i);
  1073. const char *name = type->name ();
  1074. int is_constructor = name && strcmp (method_name,
  1075. name) == 0;
  1076. for (j = 0; j < len2; j++)
  1077. {
  1078. const char *mangled_name;
  1079. gdb::unique_xmalloc_ptr<char> mangled_name_holder;
  1080. const char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
  1081. int is_full_physname_constructor =
  1082. TYPE_FN_FIELD_CONSTRUCTOR (f, j)
  1083. || is_constructor_name (physname)
  1084. || is_destructor_name (physname)
  1085. || method_name[0] == '~';
  1086. /* Do not print out artificial methods. */
  1087. if (TYPE_FN_FIELD_ARTIFICIAL (f, j))
  1088. continue;
  1089. QUIT;
  1090. section_type = output_access_specifier
  1091. (stream, section_type, level,
  1092. TYPE_FN_FIELD_PROTECTED (f, j),
  1093. TYPE_FN_FIELD_PRIVATE (f, j), flags);
  1094. print_spaces_filtered_with_print_options (level + 4, stream,
  1095. flags);
  1096. if (TYPE_FN_FIELD_VIRTUAL_P (f, j))
  1097. gdb_printf (stream, "virtual ");
  1098. else if (TYPE_FN_FIELD_STATIC_P (f, j))
  1099. gdb_printf (stream, "static ");
  1100. if (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)) == 0)
  1101. {
  1102. /* Keep GDB from crashing here. */
  1103. gdb_printf (stream,
  1104. _("%p[<undefined type>%p] %s;\n"),
  1105. metadata_style.style ().ptr (), nullptr,
  1106. TYPE_FN_FIELD_PHYSNAME (f, j));
  1107. break;
  1108. }
  1109. else if (!is_constructor /* Constructors don't
  1110. have declared
  1111. types. */
  1112. && !is_full_physname_constructor /* " " */
  1113. && !is_type_conversion_operator (type, i, j))
  1114. {
  1115. c_print_type_no_offsets
  1116. (TYPE_TARGET_TYPE (TYPE_FN_FIELD_TYPE (f, j)),
  1117. "", stream, -1, 0, language, &local_flags, podata);
  1118. gdb_puts (" ", stream);
  1119. }
  1120. if (TYPE_FN_FIELD_STUB (f, j))
  1121. {
  1122. /* Build something we can demangle. */
  1123. mangled_name_holder.reset (gdb_mangle_name (type, i, j));
  1124. mangled_name = mangled_name_holder.get ();
  1125. }
  1126. else
  1127. mangled_name = TYPE_FN_FIELD_PHYSNAME (f, j);
  1128. gdb::unique_xmalloc_ptr<char> demangled_name
  1129. = gdb_demangle (mangled_name,
  1130. DMGL_ANSI | DMGL_PARAMS);
  1131. if (demangled_name == NULL)
  1132. {
  1133. /* In some cases (for instance with the HP
  1134. demangling), if a function has more than 10
  1135. arguments, the demangling will fail.
  1136. Let's try to reconstruct the function
  1137. signature from the symbol information. */
  1138. if (!TYPE_FN_FIELD_STUB (f, j))
  1139. {
  1140. int staticp = TYPE_FN_FIELD_STATIC_P (f, j);
  1141. struct type *mtype = TYPE_FN_FIELD_TYPE (f, j);
  1142. cp_type_print_method_args (mtype,
  1143. "",
  1144. method_name,
  1145. staticp,
  1146. stream, language,
  1147. &local_flags);
  1148. }
  1149. else
  1150. fprintf_styled (stream, metadata_style.style (),
  1151. _("<badly mangled name '%s'>"),
  1152. mangled_name);
  1153. }
  1154. else
  1155. {
  1156. const char *p;
  1157. const char *demangled_no_class
  1158. = remove_qualifiers (demangled_name.get ());
  1159. /* Get rid of the `static' appended by the
  1160. demangler. */
  1161. p = strstr (demangled_no_class, " static");
  1162. if (p != NULL)
  1163. {
  1164. int length = p - demangled_no_class;
  1165. std::string demangled_no_static (demangled_no_class,
  1166. length);
  1167. gdb_puts (demangled_no_static.c_str (), stream);
  1168. }
  1169. else
  1170. gdb_puts (demangled_no_class, stream);
  1171. }
  1172. gdb_printf (stream, ";\n");
  1173. }
  1174. }
  1175. /* Print out nested types. */
  1176. if (TYPE_NESTED_TYPES_COUNT (type) != 0
  1177. && semi_local_flags.print_nested_type_limit != 0)
  1178. {
  1179. if (semi_local_flags.print_nested_type_limit > 0)
  1180. --semi_local_flags.print_nested_type_limit;
  1181. if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0)
  1182. gdb_printf (stream, "\n");
  1183. for (int i = 0; i < TYPE_NESTED_TYPES_COUNT (type); ++i)
  1184. {
  1185. print_spaces_filtered_with_print_options (level + 4, stream,
  1186. flags);
  1187. c_print_type_no_offsets (TYPE_NESTED_TYPES_FIELD_TYPE (type, i),
  1188. "", stream, show, level + 4,
  1189. language, &semi_local_flags, podata);
  1190. gdb_printf (stream, ";\n");
  1191. }
  1192. }
  1193. /* Print typedefs defined in this class. */
  1194. if (TYPE_TYPEDEF_FIELD_COUNT (type) != 0 && flags->print_typedefs)
  1195. {
  1196. if (type->num_fields () != 0 || TYPE_NFN_FIELDS (type) != 0
  1197. || TYPE_NESTED_TYPES_COUNT (type) != 0)
  1198. gdb_printf (stream, "\n");
  1199. for (int i = 0; i < TYPE_TYPEDEF_FIELD_COUNT (type); i++)
  1200. {
  1201. struct type *target = TYPE_TYPEDEF_FIELD_TYPE (type, i);
  1202. /* Dereference the typedef declaration itself. */
  1203. gdb_assert (target->code () == TYPE_CODE_TYPEDEF);
  1204. target = TYPE_TARGET_TYPE (target);
  1205. if (need_access_label)
  1206. {
  1207. section_type = output_access_specifier
  1208. (stream, section_type, level,
  1209. TYPE_TYPEDEF_FIELD_PROTECTED (type, i),
  1210. TYPE_TYPEDEF_FIELD_PRIVATE (type, i), flags);
  1211. }
  1212. print_spaces_filtered_with_print_options (level + 4, stream,
  1213. flags);
  1214. gdb_printf (stream, "typedef ");
  1215. /* We want to print typedefs with substitutions
  1216. from the template parameters or globally-known
  1217. typedefs but not local typedefs. */
  1218. c_print_type_no_offsets (target,
  1219. TYPE_TYPEDEF_FIELD_NAME (type, i),
  1220. stream, show - 1, level + 4,
  1221. language, &semi_local_flags, podata);
  1222. gdb_printf (stream, ";\n");
  1223. }
  1224. }
  1225. if (flags->print_offsets)
  1226. {
  1227. if (show > 0)
  1228. podata->finish (type, level, stream);
  1229. print_spaces (print_offset_data::indentation, stream);
  1230. if (level == 0)
  1231. print_spaces (2, stream);
  1232. }
  1233. gdb_printf (stream, "%*s}", level, "");
  1234. }
  1235. }
  1236. /* Print the name of the type (or the ultimate pointer target,
  1237. function value or array element), or the description of a structure
  1238. or union.
  1239. SHOW positive means print details about the type (e.g. enum
  1240. values), and print structure elements passing SHOW - 1 for show.
  1241. SHOW negative means just print the type name or struct tag if there
  1242. is one. If there is no name, print something sensible but concise
  1243. like "struct {...}".
  1244. SHOW zero means just print the type name or struct tag if there is
  1245. one. If there is no name, print something sensible but not as
  1246. concise like "struct {int x; int y;}".
  1247. LEVEL is the number of spaces to indent by.
  1248. We increase it for some recursive calls. */
  1249. static void
  1250. c_type_print_base_1 (struct type *type, struct ui_file *stream,
  1251. int show, int level,
  1252. enum language language,
  1253. const struct type_print_options *flags,
  1254. struct print_offset_data *podata)
  1255. {
  1256. int i;
  1257. int len;
  1258. QUIT;
  1259. if (type == NULL)
  1260. {
  1261. fputs_styled (_("<type unknown>"), metadata_style.style (), stream);
  1262. return;
  1263. }
  1264. /* When SHOW is zero or less, and there is a valid type name, then
  1265. always just print the type name directly from the type. */
  1266. if (show <= 0
  1267. && type->name () != NULL)
  1268. {
  1269. c_type_print_modifier (type, stream, 0, 1, language);
  1270. /* If we have "typedef struct foo {. . .} bar;" do we want to
  1271. print it as "struct foo" or as "bar"? Pick the latter for
  1272. C++, because C++ folk tend to expect things like "class5
  1273. *foo" rather than "struct class5 *foo". We rather
  1274. arbitrarily choose to make language_minimal work in a C-like
  1275. way. */
  1276. if (language == language_c || language == language_minimal)
  1277. {
  1278. if (type->code () == TYPE_CODE_UNION)
  1279. gdb_printf (stream, "union ");
  1280. else if (type->code () == TYPE_CODE_STRUCT)
  1281. {
  1282. if (type->is_declared_class ())
  1283. gdb_printf (stream, "class ");
  1284. else
  1285. gdb_printf (stream, "struct ");
  1286. }
  1287. else if (type->code () == TYPE_CODE_ENUM)
  1288. gdb_printf (stream, "enum ");
  1289. }
  1290. print_name_maybe_canonical (type->name (), flags, stream);
  1291. return;
  1292. }
  1293. type = check_typedef (type);
  1294. switch (type->code ())
  1295. {
  1296. case TYPE_CODE_TYPEDEF:
  1297. /* If we get here, the typedef doesn't have a name, and we
  1298. couldn't resolve TYPE_TARGET_TYPE. Not much we can do. */
  1299. gdb_assert (type->name () == NULL);
  1300. gdb_assert (TYPE_TARGET_TYPE (type) == NULL);
  1301. fprintf_styled (stream, metadata_style.style (),
  1302. _("<unnamed typedef>"));
  1303. break;
  1304. case TYPE_CODE_FUNC:
  1305. case TYPE_CODE_METHOD:
  1306. if (TYPE_TARGET_TYPE (type) == NULL)
  1307. type_print_unknown_return_type (stream);
  1308. else
  1309. c_type_print_base_1 (TYPE_TARGET_TYPE (type),
  1310. stream, show, level, language, flags, podata);
  1311. break;
  1312. case TYPE_CODE_ARRAY:
  1313. case TYPE_CODE_PTR:
  1314. case TYPE_CODE_MEMBERPTR:
  1315. case TYPE_CODE_REF:
  1316. case TYPE_CODE_RVALUE_REF:
  1317. case TYPE_CODE_METHODPTR:
  1318. c_type_print_base_1 (TYPE_TARGET_TYPE (type),
  1319. stream, show, level, language, flags, podata);
  1320. break;
  1321. case TYPE_CODE_STRUCT:
  1322. case TYPE_CODE_UNION:
  1323. c_type_print_base_struct_union (type, stream, show, level,
  1324. language, flags, podata);
  1325. break;
  1326. case TYPE_CODE_ENUM:
  1327. c_type_print_modifier (type, stream, 0, 1, language);
  1328. gdb_printf (stream, "enum ");
  1329. if (type->is_declared_class ())
  1330. gdb_printf (stream, "class ");
  1331. /* Print the tag name if it exists.
  1332. The aCC compiler emits a spurious
  1333. "{unnamed struct}"/"{unnamed union}"/"{unnamed enum}"
  1334. tag for unnamed struct/union/enum's, which we don't
  1335. want to print. */
  1336. if (type->name () != NULL
  1337. && !startswith (type->name (), "{unnamed"))
  1338. {
  1339. print_name_maybe_canonical (type->name (), flags, stream);
  1340. if (show > 0)
  1341. gdb_puts (" ", stream);
  1342. }
  1343. stream->wrap_here (4);
  1344. if (show < 0)
  1345. {
  1346. /* If we just printed a tag name, no need to print anything
  1347. else. */
  1348. if (type->name () == NULL)
  1349. gdb_printf (stream, "{...}");
  1350. }
  1351. else if (show > 0 || type->name () == NULL)
  1352. {
  1353. LONGEST lastval = 0;
  1354. /* We can't handle this case perfectly, as DWARF does not
  1355. tell us whether or not the underlying type was specified
  1356. in the source (and other debug formats don't provide this
  1357. at all). We choose to print the underlying type, if it
  1358. has a name, when in C++ on the theory that it's better to
  1359. print too much than too little; but conversely not to
  1360. print something egregiously outside the current
  1361. language's syntax. */
  1362. if (language == language_cplus && TYPE_TARGET_TYPE (type) != NULL)
  1363. {
  1364. struct type *underlying = check_typedef (TYPE_TARGET_TYPE (type));
  1365. if (underlying->name () != NULL)
  1366. gdb_printf (stream, ": %s ", underlying->name ());
  1367. }
  1368. gdb_printf (stream, "{");
  1369. len = type->num_fields ();
  1370. for (i = 0; i < len; i++)
  1371. {
  1372. QUIT;
  1373. if (i)
  1374. gdb_printf (stream, ", ");
  1375. stream->wrap_here (4);
  1376. fputs_styled (type->field (i).name (),
  1377. variable_name_style.style (), stream);
  1378. if (lastval != type->field (i).loc_enumval ())
  1379. {
  1380. gdb_printf (stream, " = %s",
  1381. plongest (type->field (i).loc_enumval ()));
  1382. lastval = type->field (i).loc_enumval ();
  1383. }
  1384. lastval++;
  1385. }
  1386. gdb_printf (stream, "}");
  1387. }
  1388. break;
  1389. case TYPE_CODE_FLAGS:
  1390. {
  1391. struct type_print_options local_flags = *flags;
  1392. local_flags.local_typedefs = NULL;
  1393. c_type_print_modifier (type, stream, 0, 1, language);
  1394. gdb_printf (stream, "flag ");
  1395. print_name_maybe_canonical (type->name (), flags, stream);
  1396. if (show > 0)
  1397. {
  1398. gdb_puts (" ", stream);
  1399. gdb_printf (stream, "{\n");
  1400. if (type->num_fields () == 0)
  1401. {
  1402. if (type->is_stub ())
  1403. gdb_printf (stream,
  1404. _("%*s%p[<incomplete type>%p]\n"),
  1405. level + 4, "",
  1406. metadata_style.style ().ptr (), nullptr);
  1407. else
  1408. gdb_printf (stream,
  1409. _("%*s%p[<no data fields>%p]\n"),
  1410. level + 4, "",
  1411. metadata_style.style ().ptr (), nullptr);
  1412. }
  1413. len = type->num_fields ();
  1414. for (i = 0; i < len; i++)
  1415. {
  1416. QUIT;
  1417. print_spaces (level + 4, stream);
  1418. /* We pass "show" here and not "show - 1" to get enum types
  1419. printed. There's no other way to see them. */
  1420. c_print_type_1 (type->field (i).type (),
  1421. type->field (i).name (),
  1422. stream, show, level + 4,
  1423. language, &local_flags, podata);
  1424. gdb_printf (stream, " @%s",
  1425. plongest (type->field (i).loc_bitpos ()));
  1426. if (TYPE_FIELD_BITSIZE (type, i) > 1)
  1427. {
  1428. gdb_printf (stream, "-%s",
  1429. plongest (type->field (i).loc_bitpos ()
  1430. + TYPE_FIELD_BITSIZE (type, i)
  1431. - 1));
  1432. }
  1433. gdb_printf (stream, ";\n");
  1434. }
  1435. gdb_printf (stream, "%*s}", level, "");
  1436. }
  1437. }
  1438. break;
  1439. case TYPE_CODE_VOID:
  1440. gdb_printf (stream, "void");
  1441. break;
  1442. case TYPE_CODE_UNDEF:
  1443. gdb_printf (stream, _("struct <unknown>"));
  1444. break;
  1445. case TYPE_CODE_ERROR:
  1446. gdb_printf (stream, "%s", TYPE_ERROR_NAME (type));
  1447. break;
  1448. case TYPE_CODE_RANGE:
  1449. /* This should not occur. */
  1450. fprintf_styled (stream, metadata_style.style (), _("<range type>"));
  1451. break;
  1452. case TYPE_CODE_FIXED_POINT:
  1453. print_type_fixed_point (type, stream);
  1454. break;
  1455. case TYPE_CODE_NAMESPACE:
  1456. gdb_puts ("namespace ", stream);
  1457. gdb_puts (type->name (), stream);
  1458. break;
  1459. default:
  1460. /* Handle types not explicitly handled by the other cases, such
  1461. as fundamental types. For these, just print whatever the
  1462. type name is, as recorded in the type itself. If there is no
  1463. type name, then complain. */
  1464. if (type->name () != NULL)
  1465. {
  1466. c_type_print_modifier (type, stream, 0, 1, language);
  1467. print_name_maybe_canonical (type->name (), flags, stream);
  1468. }
  1469. else
  1470. {
  1471. /* At least for dump_symtab, it is important that this not
  1472. be an error (). */
  1473. fprintf_styled (stream, metadata_style.style (),
  1474. _("<invalid type code %d>"), type->code ());
  1475. }
  1476. break;
  1477. }
  1478. }
  1479. /* See c_type_print_base_1. */
  1480. void
  1481. c_type_print_base (struct type *type, struct ui_file *stream,
  1482. int show, int level,
  1483. const struct type_print_options *flags)
  1484. {
  1485. struct print_offset_data podata (flags);
  1486. c_type_print_base_1 (type, stream, show, level,
  1487. current_language->la_language, flags, &podata);
  1488. }