PageRenderTime 66ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/SheepShaver/src/kpx_cpu/src/cpu/jit/cxxdemangle.cpp

https://github.com/RedOrion/macemu
C++ | 4495 lines | 3251 code | 475 blank | 769 comment | 938 complexity | 1d936889ed9087db9f479b5db6bf9a35 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * cxxdemangle.cpp - C++ demangler
  3. *
  4. * Kheperix (C) 2003-2005 Gwenole Beauchesne
  5. *
  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 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include <string.h>
  23. #include "cxxdemangle.h"
  24. #if defined(__GNUC__) && (__GXX_ABI_VERSION > 0)
  25. #include <cxxabi.h>
  26. char *
  27. cxx_demangle(const char *mangled_name, char *buf, size_t *n, int *status)
  28. {
  29. return abi::__cxa_demangle(mangled_name, buf, n, status);
  30. }
  31. #else
  32. /* Use demangler from libiberty */
  33. char *cplus_demangle (const char *mangled, int options);
  34. /* Options passed to cplus_demangle (in 2nd parameter). */
  35. #define DMGL_NO_OPTS 0 /* For readability... */
  36. #define DMGL_PARAMS (1 << 0) /* Include function args */
  37. #define DMGL_ANSI (1 << 1) /* Include const, volatile, etc */
  38. #define DMGL_AUTO (1 << 8)
  39. #define DMGL_GNU (1 << 9)
  40. #define DMGL_LUCID (1 << 10)
  41. #define DMGL_ARM (1 << 11)
  42. #define DMGL_HP (1 << 12)
  43. #define DMGL_EDG (1 << 13)
  44. #ifdef __GNUC__
  45. #define DMGL_COMPILER DMGL_GNU
  46. #endif
  47. char *
  48. cxx_demangle(const char *mangled_name, char *buf, size_t *n, int *status)
  49. {
  50. if (mangled_name == NULL || (buf != NULL && n == NULL)) {
  51. if (status)
  52. *status = -3;
  53. return NULL;
  54. }
  55. char *demangled = cplus_demangle(mangled_name, DMGL_COMPILER | DMGL_PARAMS | DMGL_ANSI);
  56. if (demangled == NULL) {
  57. if (status)
  58. *status = -2;
  59. return NULL;
  60. }
  61. else {
  62. int len = strlen(demangled) + 1;
  63. if (buf && n) {
  64. if (len <= *n)
  65. strcpy(buf, demangled);
  66. else {
  67. if (status)
  68. *status = -1;
  69. return NULL;
  70. }
  71. }
  72. }
  73. if (status)
  74. *status = 0;
  75. return demangled;
  76. }
  77. #include <stdlib.h>
  78. #define xmalloc(size) malloc(size)
  79. #define xrealloc(ptr, size) realloc(ptr, size)
  80. #include <ctype.h>
  81. #define ISDIGIT(c) isdigit(c)
  82. #define ISLOWER(c) islower(c)
  83. /* ANSI and traditional C compatability macros
  84. Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
  85. Free Software Foundation, Inc.
  86. This file is part of the GNU C Library. */
  87. #define PARAMS(ARGS) ARGS
  88. /* Define macros for some gcc attributes. This permits us to use the
  89. macros freely, and know that they will come into play for the
  90. version of gcc in which they are supported. */
  91. #ifndef ATTRIBUTE_UNUSED
  92. #define ATTRIBUTE_UNUSED
  93. #endif /* ATTRIBUTE_UNUSED */
  94. #ifndef ATTRIBUTE_NORETURN
  95. #define ATTRIBUTE_NORETURN
  96. #endif /* ATTRIBUTE_NORETURN */
  97. /* Demangler for GNU C++
  98. Copyright 1989, 1991, 1994, 1995, 1996, 1997, 1998, 1999,
  99. 2000, 2001 Free Software Foundation, Inc.
  100. Written by James Clark (jjc@jclark.uucp)
  101. Rewritten by Fred Fish (fnf@cygnus.com) for ARM and Lucid demangling
  102. Modified by Satish Pai (pai@apollo.hp.com) for HP demangling
  103. This file is part of the libiberty library. */
  104. /* This file exports two functions; cplus_mangle_opname and cplus_demangle.
  105. This file imports xmalloc and xrealloc, which are like malloc and
  106. realloc except that they generate a fatal error if there is no
  107. available memory. */
  108. /* This file lives in both GCC and libiberty. When making changes, please
  109. try not to break either. */
  110. #undef CURRENT_DEMANGLING_STYLE
  111. #define CURRENT_DEMANGLING_STYLE work->options
  112. #define AUTO_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_AUTO)
  113. #define GNU_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU)
  114. #define LUCID_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_LUCID)
  115. #define ARM_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_ARM)
  116. #define HP_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_HP)
  117. #define EDG_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_EDG)
  118. #define GNU_V3_DEMANGLING (((int) CURRENT_DEMANGLING_STYLE) & DMGL_GNU_V3)
  119. #define min(X,Y) (((X) < (Y)) ? (X) : (Y))
  120. /* A value at least one greater than the maximum number of characters
  121. that will be output when using the `%d' format with `printf'. */
  122. #define INTBUF_SIZE 32
  123. #ifndef ARRAY_SIZE
  124. #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
  125. #endif
  126. extern void fancy_abort PARAMS ((void)) ATTRIBUTE_NORETURN;
  127. /* In order to allow a single demangler executable to demangle strings
  128. using various common values of CPLUS_MARKER, as well as any specific
  129. one set at compile time, we maintain a string containing all the
  130. commonly used ones, and check to see if the marker we are looking for
  131. is in that string. CPLUS_MARKER is usually '$' on systems where the
  132. assembler can deal with that. Where the assembler can't, it's usually
  133. '.' (but on many systems '.' is used for other things). We put the
  134. current defined CPLUS_MARKER first (which defaults to '$'), followed
  135. by the next most common value, followed by an explicit '$' in case
  136. the value of CPLUS_MARKER is not '$'.
  137. We could avoid this if we could just get g++ to tell us what the actual
  138. cplus marker character is as part of the debug information, perhaps by
  139. ensuring that it is the character that terminates the gcc<n>_compiled
  140. marker symbol (FIXME). */
  141. #if !defined (CPLUS_MARKER)
  142. #define CPLUS_MARKER '$'
  143. #endif
  144. static char cplus_markers[] = { CPLUS_MARKER, '.', '$', '\0' };
  145. static char char_str[2] = { '\000', '\000' };
  146. typedef struct string /* Beware: these aren't required to be */
  147. { /* '\0' terminated. */
  148. char *b; /* pointer to start of string */
  149. char *p; /* pointer after last character */
  150. char *e; /* pointer after end of allocated space */
  151. } string;
  152. /* Stuff that is shared between sub-routines.
  153. Using a shared structure allows cplus_demangle to be reentrant. */
  154. struct work_stuff
  155. {
  156. int options;
  157. char **typevec;
  158. char **ktypevec;
  159. char **btypevec;
  160. int numk;
  161. int numb;
  162. int ksize;
  163. int bsize;
  164. int ntypes;
  165. int typevec_size;
  166. int constructor;
  167. int destructor;
  168. int static_type; /* A static member function */
  169. int temp_start; /* index in demangled to start of template args */
  170. int type_quals; /* The type qualifiers. */
  171. int dllimported; /* Symbol imported from a PE DLL */
  172. char **tmpl_argvec; /* Template function arguments. */
  173. int ntmpl_args; /* The number of template function arguments. */
  174. int forgetting_types; /* Nonzero if we are not remembering the types
  175. we see. */
  176. string* previous_argument; /* The last function argument demangled. */
  177. int nrepeats; /* The number of times to repeat the previous
  178. argument. */
  179. };
  180. #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
  181. #define PRINT_ARG_TYPES (work -> options & DMGL_PARAMS)
  182. static const struct optable
  183. {
  184. const char *const in;
  185. const char *const out;
  186. const int flags;
  187. } optable[] = {
  188. {"nw", " new", DMGL_ANSI}, /* new (1.92, ansi) */
  189. {"dl", " delete", DMGL_ANSI}, /* new (1.92, ansi) */
  190. {"new", " new", 0}, /* old (1.91, and 1.x) */
  191. {"delete", " delete", 0}, /* old (1.91, and 1.x) */
  192. {"vn", " new []", DMGL_ANSI}, /* GNU, pending ansi */
  193. {"vd", " delete []", DMGL_ANSI}, /* GNU, pending ansi */
  194. {"as", "=", DMGL_ANSI}, /* ansi */
  195. {"ne", "!=", DMGL_ANSI}, /* old, ansi */
  196. {"eq", "==", DMGL_ANSI}, /* old, ansi */
  197. {"ge", ">=", DMGL_ANSI}, /* old, ansi */
  198. {"gt", ">", DMGL_ANSI}, /* old, ansi */
  199. {"le", "<=", DMGL_ANSI}, /* old, ansi */
  200. {"lt", "<", DMGL_ANSI}, /* old, ansi */
  201. {"plus", "+", 0}, /* old */
  202. {"pl", "+", DMGL_ANSI}, /* ansi */
  203. {"apl", "+=", DMGL_ANSI}, /* ansi */
  204. {"minus", "-", 0}, /* old */
  205. {"mi", "-", DMGL_ANSI}, /* ansi */
  206. {"ami", "-=", DMGL_ANSI}, /* ansi */
  207. {"mult", "*", 0}, /* old */
  208. {"ml", "*", DMGL_ANSI}, /* ansi */
  209. {"amu", "*=", DMGL_ANSI}, /* ansi (ARM/Lucid) */
  210. {"aml", "*=", DMGL_ANSI}, /* ansi (GNU/g++) */
  211. {"convert", "+", 0}, /* old (unary +) */
  212. {"negate", "-", 0}, /* old (unary -) */
  213. {"trunc_mod", "%", 0}, /* old */
  214. {"md", "%", DMGL_ANSI}, /* ansi */
  215. {"amd", "%=", DMGL_ANSI}, /* ansi */
  216. {"trunc_div", "/", 0}, /* old */
  217. {"dv", "/", DMGL_ANSI}, /* ansi */
  218. {"adv", "/=", DMGL_ANSI}, /* ansi */
  219. {"truth_andif", "&&", 0}, /* old */
  220. {"aa", "&&", DMGL_ANSI}, /* ansi */
  221. {"truth_orif", "||", 0}, /* old */
  222. {"oo", "||", DMGL_ANSI}, /* ansi */
  223. {"truth_not", "!", 0}, /* old */
  224. {"nt", "!", DMGL_ANSI}, /* ansi */
  225. {"postincrement","++", 0}, /* old */
  226. {"pp", "++", DMGL_ANSI}, /* ansi */
  227. {"postdecrement","--", 0}, /* old */
  228. {"mm", "--", DMGL_ANSI}, /* ansi */
  229. {"bit_ior", "|", 0}, /* old */
  230. {"or", "|", DMGL_ANSI}, /* ansi */
  231. {"aor", "|=", DMGL_ANSI}, /* ansi */
  232. {"bit_xor", "^", 0}, /* old */
  233. {"er", "^", DMGL_ANSI}, /* ansi */
  234. {"aer", "^=", DMGL_ANSI}, /* ansi */
  235. {"bit_and", "&", 0}, /* old */
  236. {"ad", "&", DMGL_ANSI}, /* ansi */
  237. {"aad", "&=", DMGL_ANSI}, /* ansi */
  238. {"bit_not", "~", 0}, /* old */
  239. {"co", "~", DMGL_ANSI}, /* ansi */
  240. {"call", "()", 0}, /* old */
  241. {"cl", "()", DMGL_ANSI}, /* ansi */
  242. {"alshift", "<<", 0}, /* old */
  243. {"ls", "<<", DMGL_ANSI}, /* ansi */
  244. {"als", "<<=", DMGL_ANSI}, /* ansi */
  245. {"arshift", ">>", 0}, /* old */
  246. {"rs", ">>", DMGL_ANSI}, /* ansi */
  247. {"ars", ">>=", DMGL_ANSI}, /* ansi */
  248. {"component", "->", 0}, /* old */
  249. {"pt", "->", DMGL_ANSI}, /* ansi; Lucid C++ form */
  250. {"rf", "->", DMGL_ANSI}, /* ansi; ARM/GNU form */
  251. {"indirect", "*", 0}, /* old */
  252. {"method_call", "->()", 0}, /* old */
  253. {"addr", "&", 0}, /* old (unary &) */
  254. {"array", "[]", 0}, /* old */
  255. {"vc", "[]", DMGL_ANSI}, /* ansi */
  256. {"compound", ", ", 0}, /* old */
  257. {"cm", ", ", DMGL_ANSI}, /* ansi */
  258. {"cond", "?:", 0}, /* old */
  259. {"cn", "?:", DMGL_ANSI}, /* pseudo-ansi */
  260. {"max", ">?", 0}, /* old */
  261. {"mx", ">?", DMGL_ANSI}, /* pseudo-ansi */
  262. {"min", "<?", 0}, /* old */
  263. {"mn", "<?", DMGL_ANSI}, /* pseudo-ansi */
  264. {"nop", "", 0}, /* old (for operator=) */
  265. {"rm", "->*", DMGL_ANSI}, /* ansi */
  266. {"sz", "sizeof ", DMGL_ANSI} /* pseudo-ansi */
  267. };
  268. /* These values are used to indicate the various type varieties.
  269. They are all non-zero so that they can be used as `success'
  270. values. */
  271. typedef enum type_kind_t
  272. {
  273. tk_none,
  274. tk_pointer,
  275. tk_reference,
  276. tk_integral,
  277. tk_bool,
  278. tk_char,
  279. tk_real
  280. } type_kind_t;
  281. #define STRING_EMPTY(str) ((str) -> b == (str) -> p)
  282. #define PREPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
  283. string_prepend(str, " ");}
  284. #define APPEND_BLANK(str) {if (!STRING_EMPTY(str)) \
  285. string_append(str, " ");}
  286. #define LEN_STRING(str) ( (STRING_EMPTY(str))?0:((str)->p - (str)->b))
  287. /* The scope separator appropriate for the language being demangled. */
  288. #define SCOPE_STRING(work) "::"
  289. #define ARM_VTABLE_STRING "__vtbl__" /* Lucid/ARM virtual table prefix */
  290. #define ARM_VTABLE_STRLEN 8 /* strlen (ARM_VTABLE_STRING) */
  291. /* Prototypes for local functions */
  292. static void
  293. delete_work_stuff PARAMS ((work_stuff *));
  294. static void
  295. delete_non_B_K_work_stuff PARAMS ((work_stuff *));
  296. static char *
  297. mop_up PARAMS ((work_stuff *, string *, int));
  298. static void
  299. squangle_mop_up PARAMS ((work_stuff *));
  300. static void
  301. work_stuff_copy_to_from PARAMS ((work_stuff *, work_stuff *));
  302. static char *
  303. internal_cplus_demangle PARAMS ((work_stuff *, const char *));
  304. static int
  305. demangle_template_template_parm PARAMS ((work_stuff *work,
  306. const char **, string *));
  307. static int
  308. demangle_template PARAMS ((work_stuff *work, const char **, string *,
  309. string *, int, int));
  310. static int
  311. arm_pt PARAMS ((work_stuff *, const char *, int, const char **,
  312. const char **));
  313. static int
  314. demangle_class_name PARAMS ((work_stuff *, const char **, string *));
  315. static int
  316. demangle_qualified PARAMS ((work_stuff *, const char **, string *,
  317. int, int));
  318. static int
  319. demangle_class PARAMS ((work_stuff *, const char **, string *));
  320. static int
  321. demangle_fund_type PARAMS ((work_stuff *, const char **, string *));
  322. static int
  323. demangle_signature PARAMS ((work_stuff *, const char **, string *));
  324. static int
  325. demangle_prefix PARAMS ((work_stuff *, const char **, string *));
  326. static int
  327. gnu_special PARAMS ((work_stuff *, const char **, string *));
  328. static int
  329. arm_special PARAMS ((const char **, string *));
  330. static void
  331. string_need PARAMS ((string *, int));
  332. static void
  333. string_delete PARAMS ((string *));
  334. static void
  335. string_init PARAMS ((string *));
  336. static void
  337. string_clear PARAMS ((string *));
  338. static void
  339. string_append PARAMS ((string *, const char *));
  340. static void
  341. string_appends PARAMS ((string *, string *));
  342. static void
  343. string_appendn PARAMS ((string *, const char *, int));
  344. static void
  345. string_prepend PARAMS ((string *, const char *));
  346. static void
  347. string_prependn PARAMS ((string *, const char *, int));
  348. static void
  349. string_append_template_idx PARAMS ((string *, int));
  350. static int
  351. get_count PARAMS ((const char **, int *));
  352. static int
  353. consume_count PARAMS ((const char **));
  354. static int
  355. consume_count_with_underscores PARAMS ((const char**));
  356. static int
  357. demangle_args PARAMS ((work_stuff *, const char **, string *));
  358. static int
  359. demangle_nested_args PARAMS ((work_stuff*, const char**, string*));
  360. static int
  361. do_type PARAMS ((work_stuff *, const char **, string *));
  362. static int
  363. do_arg PARAMS ((work_stuff *, const char **, string *));
  364. static void
  365. demangle_function_name PARAMS ((work_stuff *, const char **, string *,
  366. const char *));
  367. static int
  368. iterate_demangle_function PARAMS ((work_stuff *,
  369. const char **, string *, const char *));
  370. static void
  371. remember_type PARAMS ((work_stuff *, const char *, int));
  372. static void
  373. remember_Btype PARAMS ((work_stuff *, const char *, int, int));
  374. static int
  375. register_Btype PARAMS ((work_stuff *));
  376. static void
  377. remember_Ktype PARAMS ((work_stuff *, const char *, int));
  378. static void
  379. forget_types PARAMS ((work_stuff *));
  380. static void
  381. forget_B_and_K_types PARAMS ((work_stuff *));
  382. static void
  383. string_prepends PARAMS ((string *, string *));
  384. static int
  385. demangle_template_value_parm PARAMS ((work_stuff*, const char**,
  386. string*, type_kind_t));
  387. static int
  388. do_hpacc_template_const_value PARAMS ((work_stuff *, const char **, string *));
  389. static int
  390. do_hpacc_template_literal PARAMS ((work_stuff *, const char **, string *));
  391. static int
  392. snarf_numeric_literal PARAMS ((const char **, string *));
  393. /* There is a TYPE_QUAL value for each type qualifier. They can be
  394. combined by bitwise-or to form the complete set of qualifiers for a
  395. type. */
  396. #define TYPE_UNQUALIFIED 0x0
  397. #define TYPE_QUAL_CONST 0x1
  398. #define TYPE_QUAL_VOLATILE 0x2
  399. #define TYPE_QUAL_RESTRICT 0x4
  400. static int
  401. code_for_qualifier PARAMS ((int));
  402. static const char*
  403. qualifier_string PARAMS ((int));
  404. static const char*
  405. demangle_qualifier PARAMS ((int));
  406. static int
  407. demangle_expression PARAMS ((work_stuff *, const char **, string *,
  408. type_kind_t));
  409. static int
  410. demangle_integral_value PARAMS ((work_stuff *, const char **,
  411. string *));
  412. static int
  413. demangle_real_value PARAMS ((work_stuff *, const char **, string *));
  414. static void
  415. demangle_arm_hp_template PARAMS ((work_stuff *, const char **, int,
  416. string *));
  417. static void
  418. recursively_demangle PARAMS ((work_stuff *, const char **, string *,
  419. int));
  420. static void
  421. grow_vect PARAMS ((void **, size_t *, size_t, int));
  422. /* Translate count to integer, consuming tokens in the process.
  423. Conversion terminates on the first non-digit character.
  424. Trying to consume something that isn't a count results in no
  425. consumption of input and a return of -1.
  426. Overflow consumes the rest of the digits, and returns -1. */
  427. static int
  428. consume_count (const char **type)
  429. {
  430. int count = 0;
  431. if (! ISDIGIT ((unsigned char)**type))
  432. return -1;
  433. while (ISDIGIT ((unsigned char)**type))
  434. {
  435. count *= 10;
  436. /* Check for overflow.
  437. We assume that count is represented using two's-complement;
  438. no power of two is divisible by ten, so if an overflow occurs
  439. when multiplying by ten, the result will not be a multiple of
  440. ten. */
  441. if ((count % 10) != 0)
  442. {
  443. while (ISDIGIT ((unsigned char) **type))
  444. (*type)++;
  445. return -1;
  446. }
  447. count += **type - '0';
  448. (*type)++;
  449. }
  450. if (count < 0)
  451. count = -1;
  452. return (count);
  453. }
  454. /* Like consume_count, but for counts that are preceded and followed
  455. by '_' if they are greater than 10. Also, -1 is returned for
  456. failure, since 0 can be a valid value. */
  457. static int
  458. consume_count_with_underscores (const char **mangled)
  459. {
  460. int idx;
  461. if (**mangled == '_')
  462. {
  463. (*mangled)++;
  464. if (!ISDIGIT ((unsigned char)**mangled))
  465. return -1;
  466. idx = consume_count (mangled);
  467. if (**mangled != '_')
  468. /* The trailing underscore was missing. */
  469. return -1;
  470. (*mangled)++;
  471. }
  472. else
  473. {
  474. if (**mangled < '0' || **mangled > '9')
  475. return -1;
  476. idx = **mangled - '0';
  477. (*mangled)++;
  478. }
  479. return idx;
  480. }
  481. /* C is the code for a type-qualifier. Return the TYPE_QUAL
  482. corresponding to this qualifier. */
  483. static int
  484. code_for_qualifier (int c)
  485. {
  486. switch (c)
  487. {
  488. case 'C':
  489. return TYPE_QUAL_CONST;
  490. case 'V':
  491. return TYPE_QUAL_VOLATILE;
  492. case 'u':
  493. return TYPE_QUAL_RESTRICT;
  494. default:
  495. break;
  496. }
  497. /* C was an invalid qualifier. */
  498. abort ();
  499. }
  500. /* Return the string corresponding to the qualifiers given by
  501. TYPE_QUALS. */
  502. static const char*
  503. qualifier_string (int type_quals)
  504. {
  505. switch (type_quals)
  506. {
  507. case TYPE_UNQUALIFIED:
  508. return "";
  509. case TYPE_QUAL_CONST:
  510. return "const";
  511. case TYPE_QUAL_VOLATILE:
  512. return "volatile";
  513. case TYPE_QUAL_RESTRICT:
  514. return "__restrict";
  515. case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE:
  516. return "const volatile";
  517. case TYPE_QUAL_CONST | TYPE_QUAL_RESTRICT:
  518. return "const __restrict";
  519. case TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
  520. return "volatile __restrict";
  521. case TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE | TYPE_QUAL_RESTRICT:
  522. return "const volatile __restrict";
  523. default:
  524. break;
  525. }
  526. /* TYPE_QUALS was an invalid qualifier set. */
  527. abort ();
  528. }
  529. /* C is the code for a type-qualifier. Return the string
  530. corresponding to this qualifier. This function should only be
  531. called with a valid qualifier code. */
  532. static const char*
  533. demangle_qualifier (int c)
  534. {
  535. return qualifier_string (code_for_qualifier (c));
  536. }
  537. /* char *cplus_demangle (const char *mangled, int options)
  538. If MANGLED is a mangled function name produced by GNU C++, then
  539. a pointer to a @code{malloc}ed string giving a C++ representation
  540. of the name will be returned; otherwise NULL will be returned.
  541. It is the caller's responsibility to free the string which
  542. is returned.
  543. The OPTIONS arg may contain one or more of the following bits:
  544. DMGL_ANSI ANSI qualifiers such as `const' and `void' are
  545. included.
  546. DMGL_PARAMS Function parameters are included.
  547. For example,
  548. cplus_demangle ("foo__1Ai", DMGL_PARAMS) => "A::foo(int)"
  549. cplus_demangle ("foo__1Ai", DMGL_PARAMS | DMGL_ANSI) => "A::foo(int)"
  550. cplus_demangle ("foo__1Ai", 0) => "A::foo"
  551. cplus_demangle ("foo__1Afe", DMGL_PARAMS) => "A::foo(float,...)"
  552. cplus_demangle ("foo__1Afe", DMGL_PARAMS | DMGL_ANSI)=> "A::foo(float,...)"
  553. cplus_demangle ("foo__1Afe", 0) => "A::foo"
  554. Note that any leading underscores, or other such characters prepended by
  555. the compilation system, are presumed to have already been stripped from
  556. MANGLED. */
  557. char *
  558. cplus_demangle (const char *mangled, int options)
  559. {
  560. char *ret;
  561. work_stuff work[1];
  562. memset ((char *) work, 0, sizeof (work));
  563. work->options = options;
  564. ret = internal_cplus_demangle (work, mangled);
  565. squangle_mop_up (work);
  566. return (ret);
  567. }
  568. /* Assuming *OLD_VECT points to an array of *SIZE objects of size
  569. ELEMENT_SIZE, grow it to contain at least MIN_SIZE objects,
  570. updating *OLD_VECT and *SIZE as necessary. */
  571. static void
  572. grow_vect (void **old_vect, size_t *size, size_t min_size, int element_size)
  573. {
  574. if (*size < min_size)
  575. {
  576. *size *= 2;
  577. if (*size < min_size)
  578. *size = min_size;
  579. *old_vect = (char *) xrealloc (*old_vect, *size * element_size);
  580. }
  581. }
  582. /* This function performs most of what cplus_demangle use to do, but
  583. to be able to demangle a name with a B, K or n code, we need to
  584. have a longer term memory of what types have been seen. The original
  585. now initializes and cleans up the squangle code info, while internal
  586. calls go directly to this routine to avoid resetting that info. */
  587. static char *
  588. internal_cplus_demangle (work_stuff *work, const char *mangled)
  589. {
  590. string decl;
  591. int success = 0;
  592. char *demangled = NULL;
  593. int s1, s2, s3, s4;
  594. s1 = work->constructor;
  595. s2 = work->destructor;
  596. s3 = work->static_type;
  597. s4 = work->type_quals;
  598. work->constructor = work->destructor = 0;
  599. work->type_quals = TYPE_UNQUALIFIED;
  600. work->dllimported = 0;
  601. if ((mangled != NULL) && (*mangled != '\0'))
  602. {
  603. string_init (&decl);
  604. /* First check to see if gnu style demangling is active and if the
  605. string to be demangled contains a CPLUS_MARKER. If so, attempt to
  606. recognize one of the gnu special forms rather than looking for a
  607. standard prefix. In particular, don't worry about whether there
  608. is a "__" string in the mangled string. Consider "_$_5__foo" for
  609. example. */
  610. if ((AUTO_DEMANGLING || GNU_DEMANGLING))
  611. {
  612. success = gnu_special (work, &mangled, &decl);
  613. }
  614. if (!success)
  615. {
  616. success = demangle_prefix (work, &mangled, &decl);
  617. }
  618. if (success && (*mangled != '\0'))
  619. {
  620. success = demangle_signature (work, &mangled, &decl);
  621. }
  622. if (work->constructor == 2)
  623. {
  624. string_prepend (&decl, "global constructors keyed to ");
  625. work->constructor = 0;
  626. }
  627. else if (work->destructor == 2)
  628. {
  629. string_prepend (&decl, "global destructors keyed to ");
  630. work->destructor = 0;
  631. }
  632. else if (work->dllimported == 1)
  633. {
  634. string_prepend (&decl, "import stub for ");
  635. work->dllimported = 0;
  636. }
  637. demangled = mop_up (work, &decl, success);
  638. }
  639. work->constructor = s1;
  640. work->destructor = s2;
  641. work->static_type = s3;
  642. work->type_quals = s4;
  643. return demangled;
  644. }
  645. /* Clear out and squangling related storage */
  646. static void
  647. squangle_mop_up (work_stuff *work)
  648. {
  649. /* clean up the B and K type mangling types. */
  650. forget_B_and_K_types (work);
  651. if (work -> btypevec != NULL)
  652. {
  653. free ((char *) work -> btypevec);
  654. }
  655. if (work -> ktypevec != NULL)
  656. {
  657. free ((char *) work -> ktypevec);
  658. }
  659. }
  660. /* Copy the work state and storage. */
  661. static void
  662. work_stuff_copy_to_from (work_stuff *to, work_stuff *from)
  663. {
  664. int i;
  665. delete_work_stuff (to);
  666. /* Shallow-copy scalars. */
  667. memcpy (to, from, sizeof (*to));
  668. /* Deep-copy dynamic storage. */
  669. if (from->typevec_size)
  670. to->typevec
  671. = (char **) xmalloc (from->typevec_size * sizeof (to->typevec[0]));
  672. for (i = 0; i < from->ntypes; i++)
  673. {
  674. int len = strlen (from->typevec[i]) + 1;
  675. to->typevec[i] = (char *) xmalloc (len);
  676. memcpy (to->typevec[i], from->typevec[i], len);
  677. }
  678. if (from->ksize)
  679. to->ktypevec
  680. = (char **) xmalloc (from->ksize * sizeof (to->ktypevec[0]));
  681. for (i = 0; i < from->numk; i++)
  682. {
  683. int len = strlen (from->ktypevec[i]) + 1;
  684. to->ktypevec[i] = (char *) xmalloc (len);
  685. memcpy (to->ktypevec[i], from->ktypevec[i], len);
  686. }
  687. if (from->bsize)
  688. to->btypevec
  689. = (char **) xmalloc (from->bsize * sizeof (to->btypevec[0]));
  690. for (i = 0; i < from->numb; i++)
  691. {
  692. int len = strlen (from->btypevec[i]) + 1;
  693. to->btypevec[i] = (char *) xmalloc (len);
  694. memcpy (to->btypevec[i], from->btypevec[i], len);
  695. }
  696. if (from->ntmpl_args)
  697. to->tmpl_argvec
  698. = (char **) xmalloc (from->ntmpl_args * sizeof (to->tmpl_argvec[0]));
  699. for (i = 0; i < from->ntmpl_args; i++)
  700. {
  701. int len = strlen (from->tmpl_argvec[i]) + 1;
  702. to->tmpl_argvec[i] = (char *) xmalloc (len);
  703. memcpy (to->tmpl_argvec[i], from->tmpl_argvec[i], len);
  704. }
  705. if (from->previous_argument)
  706. {
  707. to->previous_argument = (string*) xmalloc (sizeof (string));
  708. string_init (to->previous_argument);
  709. string_appends (to->previous_argument, from->previous_argument);
  710. }
  711. }
  712. /* Delete dynamic stuff in work_stuff that is not to be re-used. */
  713. static void
  714. delete_non_B_K_work_stuff (work_stuff *work)
  715. {
  716. /* Discard the remembered types, if any. */
  717. forget_types (work);
  718. if (work -> typevec != NULL)
  719. {
  720. free ((char *) work -> typevec);
  721. work -> typevec = NULL;
  722. work -> typevec_size = 0;
  723. }
  724. if (work->tmpl_argvec)
  725. {
  726. int i;
  727. for (i = 0; i < work->ntmpl_args; i++)
  728. if (work->tmpl_argvec[i])
  729. free ((char*) work->tmpl_argvec[i]);
  730. free ((char*) work->tmpl_argvec);
  731. work->tmpl_argvec = NULL;
  732. }
  733. if (work->previous_argument)
  734. {
  735. string_delete (work->previous_argument);
  736. free ((char*) work->previous_argument);
  737. work->previous_argument = NULL;
  738. }
  739. }
  740. /* Delete all dynamic storage in work_stuff. */
  741. static void
  742. delete_work_stuff (work_stuff *work)
  743. {
  744. delete_non_B_K_work_stuff (work);
  745. squangle_mop_up (work);
  746. }
  747. /* Clear out any mangled storage */
  748. static char *
  749. mop_up (work_stuff *work, string *declp, int success)
  750. {
  751. char *demangled = NULL;
  752. delete_non_B_K_work_stuff (work);
  753. /* If demangling was successful, ensure that the demangled string is null
  754. terminated and return it. Otherwise, free the demangling decl. */
  755. if (!success)
  756. {
  757. string_delete (declp);
  758. }
  759. else
  760. {
  761. string_appendn (declp, "", 1);
  762. demangled = declp->b;
  763. }
  764. return (demangled);
  765. }
  766. /*
  767. LOCAL FUNCTION
  768. demangle_signature -- demangle the signature part of a mangled name
  769. SYNOPSIS
  770. static int
  771. demangle_signature (work_stuff *work, const char **mangled,
  772. string *declp);
  773. DESCRIPTION
  774. Consume and demangle the signature portion of the mangled name.
  775. DECLP is the string where demangled output is being built. At
  776. entry it contains the demangled root name from the mangled name
  777. prefix. I.E. either a demangled operator name or the root function
  778. name. In some special cases, it may contain nothing.
  779. *MANGLED points to the current unconsumed location in the mangled
  780. name. As tokens are consumed and demangling is performed, the
  781. pointer is updated to continuously point at the next token to
  782. be consumed.
  783. Demangling GNU style mangled names is nasty because there is no
  784. explicit token that marks the start of the outermost function
  785. argument list. */
  786. static int
  787. demangle_signature (work_stuff *work, const char **mangled, string *declp)
  788. {
  789. int success = 1;
  790. int func_done = 0;
  791. int expect_func = 0;
  792. int expect_return_type = 0;
  793. const char *oldmangled = NULL;
  794. string trawname;
  795. string tname;
  796. while (success && (**mangled != '\0'))
  797. {
  798. switch (**mangled)
  799. {
  800. case 'Q':
  801. oldmangled = *mangled;
  802. success = demangle_qualified (work, mangled, declp, 1, 0);
  803. if (success)
  804. remember_type (work, oldmangled, *mangled - oldmangled);
  805. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  806. expect_func = 1;
  807. oldmangled = NULL;
  808. break;
  809. case 'K':
  810. oldmangled = *mangled;
  811. success = demangle_qualified (work, mangled, declp, 1, 0);
  812. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  813. {
  814. expect_func = 1;
  815. }
  816. oldmangled = NULL;
  817. break;
  818. case 'S':
  819. /* Static member function */
  820. if (oldmangled == NULL)
  821. {
  822. oldmangled = *mangled;
  823. }
  824. (*mangled)++;
  825. work -> static_type = 1;
  826. break;
  827. case 'C':
  828. case 'V':
  829. case 'u':
  830. work->type_quals |= code_for_qualifier (**mangled);
  831. /* a qualified member function */
  832. if (oldmangled == NULL)
  833. oldmangled = *mangled;
  834. (*mangled)++;
  835. break;
  836. case 'L':
  837. /* Local class name follows after "Lnnn_" */
  838. if (HP_DEMANGLING)
  839. {
  840. while (**mangled && (**mangled != '_'))
  841. (*mangled)++;
  842. if (!**mangled)
  843. success = 0;
  844. else
  845. (*mangled)++;
  846. }
  847. else
  848. success = 0;
  849. break;
  850. case '0': case '1': case '2': case '3': case '4':
  851. case '5': case '6': case '7': case '8': case '9':
  852. if (oldmangled == NULL)
  853. {
  854. oldmangled = *mangled;
  855. }
  856. work->temp_start = -1; /* uppermost call to demangle_class */
  857. success = demangle_class (work, mangled, declp);
  858. if (success)
  859. {
  860. remember_type (work, oldmangled, *mangled - oldmangled);
  861. }
  862. if (AUTO_DEMANGLING || GNU_DEMANGLING || EDG_DEMANGLING)
  863. {
  864. /* EDG and others will have the "F", so we let the loop cycle
  865. if we are looking at one. */
  866. if (**mangled != 'F')
  867. expect_func = 1;
  868. }
  869. oldmangled = NULL;
  870. break;
  871. case 'B':
  872. {
  873. string s;
  874. success = do_type (work, mangled, &s);
  875. if (success)
  876. {
  877. string_append (&s, SCOPE_STRING (work));
  878. string_prepends (declp, &s);
  879. }
  880. oldmangled = NULL;
  881. expect_func = 1;
  882. }
  883. break;
  884. case 'F':
  885. /* Function */
  886. /* ARM/HP style demangling includes a specific 'F' character after
  887. the class name. For GNU style, it is just implied. So we can
  888. safely just consume any 'F' at this point and be compatible
  889. with either style. */
  890. oldmangled = NULL;
  891. func_done = 1;
  892. (*mangled)++;
  893. /* For lucid/ARM/HP style we have to forget any types we might
  894. have remembered up to this point, since they were not argument
  895. types. GNU style considers all types seen as available for
  896. back references. See comment in demangle_args() */
  897. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  898. {
  899. forget_types (work);
  900. }
  901. success = demangle_args (work, mangled, declp);
  902. /* After picking off the function args, we expect to either
  903. find the function return type (preceded by an '_') or the
  904. end of the string. */
  905. if (success && (AUTO_DEMANGLING || EDG_DEMANGLING) && **mangled == '_')
  906. {
  907. ++(*mangled);
  908. /* At this level, we do not care about the return type. */
  909. success = do_type (work, mangled, &tname);
  910. string_delete (&tname);
  911. }
  912. break;
  913. case 't':
  914. /* G++ Template */
  915. string_init(&trawname);
  916. string_init(&tname);
  917. if (oldmangled == NULL)
  918. {
  919. oldmangled = *mangled;
  920. }
  921. success = demangle_template (work, mangled, &tname,
  922. &trawname, 1, 1);
  923. if (success)
  924. {
  925. remember_type (work, oldmangled, *mangled - oldmangled);
  926. }
  927. string_append (&tname, SCOPE_STRING (work));
  928. string_prepends(declp, &tname);
  929. if (work -> destructor & 1)
  930. {
  931. string_prepend (&trawname, "~");
  932. string_appends (declp, &trawname);
  933. work->destructor -= 1;
  934. }
  935. if ((work->constructor & 1) || (work->destructor & 1))
  936. {
  937. string_appends (declp, &trawname);
  938. work->constructor -= 1;
  939. }
  940. string_delete(&trawname);
  941. string_delete(&tname);
  942. oldmangled = NULL;
  943. expect_func = 1;
  944. break;
  945. case '_':
  946. if ((AUTO_DEMANGLING || GNU_DEMANGLING) && expect_return_type)
  947. {
  948. /* Read the return type. */
  949. string return_type;
  950. string_init (&return_type);
  951. (*mangled)++;
  952. success = do_type (work, mangled, &return_type);
  953. APPEND_BLANK (&return_type);
  954. string_prepends (declp, &return_type);
  955. string_delete (&return_type);
  956. break;
  957. }
  958. else
  959. /* At the outermost level, we cannot have a return type specified,
  960. so if we run into another '_' at this point we are dealing with
  961. a mangled name that is either bogus, or has been mangled by
  962. some algorithm we don't know how to deal with. So just
  963. reject the entire demangling. */
  964. /* However, "_nnn" is an expected suffix for alternate entry point
  965. numbered nnn for a function, with HP aCC, so skip over that
  966. without reporting failure. pai/1997-09-04 */
  967. if (HP_DEMANGLING)
  968. {
  969. (*mangled)++;
  970. while (**mangled && ISDIGIT ((unsigned char)**mangled))
  971. (*mangled)++;
  972. }
  973. else
  974. success = 0;
  975. break;
  976. case 'H':
  977. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  978. {
  979. /* A G++ template function. Read the template arguments. */
  980. success = demangle_template (work, mangled, declp, 0, 0,
  981. 0);
  982. if (!(work->constructor & 1))
  983. expect_return_type = 1;
  984. (*mangled)++;
  985. break;
  986. }
  987. else
  988. /* fall through */
  989. {;}
  990. default:
  991. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  992. {
  993. /* Assume we have stumbled onto the first outermost function
  994. argument token, and start processing args. */
  995. func_done = 1;
  996. success = demangle_args (work, mangled, declp);
  997. }
  998. else
  999. {
  1000. /* Non-GNU demanglers use a specific token to mark the start
  1001. of the outermost function argument tokens. Typically 'F',
  1002. for ARM/HP-demangling, for example. So if we find something
  1003. we are not prepared for, it must be an error. */
  1004. success = 0;
  1005. }
  1006. break;
  1007. }
  1008. /*
  1009. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1010. */
  1011. {
  1012. if (success && expect_func)
  1013. {
  1014. func_done = 1;
  1015. if (LUCID_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING)
  1016. {
  1017. forget_types (work);
  1018. }
  1019. success = demangle_args (work, mangled, declp);
  1020. /* Since template include the mangling of their return types,
  1021. we must set expect_func to 0 so that we don't try do
  1022. demangle more arguments the next time we get here. */
  1023. expect_func = 0;
  1024. }
  1025. }
  1026. }
  1027. if (success && !func_done)
  1028. {
  1029. if (AUTO_DEMANGLING || GNU_DEMANGLING)
  1030. {
  1031. /* With GNU style demangling, bar__3foo is 'foo::bar(void)', and
  1032. bar__3fooi is 'foo::bar(int)'. We get here when we find the
  1033. first case, and need to ensure that the '(void)' gets added to
  1034. the current declp. Note that with ARM/HP, the first case
  1035. represents the name of a static data member 'foo::bar',
  1036. which is in the current declp, so we leave it alone. */
  1037. success = demangle_args (work, mangled, declp);
  1038. }
  1039. }
  1040. if (success && PRINT_ARG_TYPES)
  1041. {
  1042. if (work->static_type)
  1043. string_append (declp, " static");
  1044. if (work->type_quals != TYPE_UNQUALIFIED)
  1045. {
  1046. APPEND_BLANK (declp);
  1047. string_append (declp, qualifier_string (work->type_quals));
  1048. }
  1049. }
  1050. return (success);
  1051. }
  1052. static int
  1053. demangle_template_template_parm (work_stuff *work,
  1054. const char **mangled,
  1055. string *tname)
  1056. {
  1057. int i;
  1058. int r;
  1059. int need_comma = 0;
  1060. int success = 1;
  1061. string temp;
  1062. string_append (tname, "template <");
  1063. /* get size of template parameter list */
  1064. if (get_count (mangled, &r))
  1065. {
  1066. for (i = 0; i < r; i++)
  1067. {
  1068. if (need_comma)
  1069. {
  1070. string_append (tname, ", ");
  1071. }
  1072. /* Z for type parameters */
  1073. if (**mangled == 'Z')
  1074. {
  1075. (*mangled)++;
  1076. string_append (tname, "class");
  1077. }
  1078. /* z for template parameters */
  1079. else if (**mangled == 'z')
  1080. {
  1081. (*mangled)++;
  1082. success =
  1083. demangle_template_template_parm (work, mangled, tname);
  1084. if (!success)
  1085. {
  1086. break;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. /* temp is initialized in do_type */
  1092. success = do_type (work, mangled, &temp);
  1093. if (success)
  1094. {
  1095. string_appends (tname, &temp);
  1096. }
  1097. string_delete(&temp);
  1098. if (!success)
  1099. {
  1100. break;
  1101. }
  1102. }
  1103. need_comma = 1;
  1104. }
  1105. }
  1106. if (tname->p[-1] == '>')
  1107. string_append (tname, " ");
  1108. string_append (tname, "> class");
  1109. return (success);
  1110. }
  1111. static int
  1112. demangle_expression (
  1113. work_stuff *work,
  1114. const char** mangled,
  1115. string* s,
  1116. type_kind_t tk)
  1117. {
  1118. int need_operator = 0;
  1119. int success;
  1120. success = 1;
  1121. string_appendn (s, "(", 1);
  1122. (*mangled)++;
  1123. while (success && **mangled != 'W' && **mangled != '\0')
  1124. {
  1125. if (need_operator)
  1126. {
  1127. size_t i;
  1128. size_t len;
  1129. success = 0;
  1130. len = strlen (*mangled);
  1131. for (i = 0; i < (size_t)ARRAY_SIZE (optable); ++i)
  1132. {
  1133. size_t l = strlen (optable[i].in);
  1134. if (l <= len
  1135. && memcmp (optable[i].in, *mangled, l) == 0)
  1136. {
  1137. string_appendn (s, " ", 1);
  1138. string_append (s, optable[i].out);
  1139. string_appendn (s, " ", 1);
  1140. success = 1;
  1141. (*mangled) += l;
  1142. break;
  1143. }
  1144. }
  1145. if (!success)
  1146. break;
  1147. }
  1148. else
  1149. need_operator = 1;
  1150. success = demangle_template_value_parm (work, mangled, s, tk);
  1151. }
  1152. if (**mangled != 'W')
  1153. success = 0;
  1154. else
  1155. {
  1156. string_appendn (s, ")", 1);
  1157. (*mangled)++;
  1158. }
  1159. return success;
  1160. }
  1161. static int
  1162. demangle_integral_value (
  1163. work_stuff *work,
  1164. const char** mangled,
  1165. string* s)
  1166. {
  1167. int success;
  1168. if (**mangled == 'E')
  1169. success = demangle_expression (work, mangled, s, tk_integral);
  1170. else if (**mangled == 'Q' || **mangled == 'K')
  1171. success = demangle_qualified (work, mangled, s, 0, 1);
  1172. else
  1173. {
  1174. int value;
  1175. /* By default, we let the number decide whether we shall consume an
  1176. underscore. */
  1177. int consume_following_underscore = 0;
  1178. int leave_following_underscore = 0;
  1179. success = 0;
  1180. /* Negative numbers are indicated with a leading `m'. */
  1181. if (**mangled == 'm')
  1182. {
  1183. string_appendn (s, "-", 1);
  1184. (*mangled)++;
  1185. }
  1186. else if (mangled[0][0] == '_' && mangled[0][1] == 'm')
  1187. {
  1188. /* Since consume_count_with_underscores does not handle the
  1189. `m'-prefix we must do it here, using consume_count and
  1190. adjusting underscores: we have to consume the underscore
  1191. matching the prepended one. */
  1192. consume_following_underscore = 1;
  1193. string_appendn (s, "-", 1);
  1194. (*mangled) += 2;
  1195. }
  1196. else if (**mangled == '_')
  1197. {
  1198. /* Do not consume a following underscore;
  1199. consume_following_underscore will consume what should be
  1200. consumed. */
  1201. leave_following_underscore = 1;
  1202. }
  1203. /* We must call consume_count if we expect to remove a trailing
  1204. underscore, since consume_count_with_underscores expects
  1205. the leading underscore (that we consumed) if it is to handle
  1206. multi-digit numbers. */
  1207. if (consume_following_underscore)
  1208. value = consume_count (mangled);
  1209. else
  1210. value = consume_count_with_underscores (mangled);
  1211. if (value != -1)
  1212. {
  1213. char buf[INTBUF_SIZE];
  1214. sprintf (buf, "%d", value);
  1215. string_append (s, buf);
  1216. /* Numbers not otherwise delimited, might have an underscore
  1217. appended as a delimeter, which we should skip.
  1218. ??? This used to always remove a following underscore, which
  1219. is wrong. If other (arbitrary) cases are followed by an
  1220. underscore, we need to do something more radical. */
  1221. if ((value > 9 || consume_following_underscore)
  1222. && ! leave_following_underscore
  1223. && **mangled == '_')
  1224. (*mangled)++;
  1225. /* All is well. */
  1226. success = 1;
  1227. }
  1228. }
  1229. return success;
  1230. }
  1231. /* Demangle the real value in MANGLED. */
  1232. static int
  1233. demangle_real_value (
  1234. work_stuff *work,
  1235. const char **mangled,
  1236. string* s)
  1237. {
  1238. if (**mangled == 'E')
  1239. return demangle_expression (work, mangled, s, tk_real);
  1240. if (**mangled == 'm')
  1241. {
  1242. string_appendn (s, "-", 1);
  1243. (*mangled)++;
  1244. }
  1245. while (ISDIGIT ((unsigned char)**mangled))
  1246. {
  1247. string_appendn (s, *mangled, 1);
  1248. (*mangled)++;
  1249. }
  1250. if (**mangled == '.') /* fraction */
  1251. {
  1252. string_appendn (s, ".", 1);
  1253. (*mangled)++;
  1254. while (ISDIGIT ((unsigned char)**mangled))
  1255. {
  1256. string_appendn (s, *mangled, 1);
  1257. (*mangled)++;
  1258. }
  1259. }
  1260. if (**mangled == 'e') /* exponent */
  1261. {
  1262. string_appendn (s, "e", 1);
  1263. (*mangled)++;
  1264. while (ISDIGIT ((unsigned char)**mangled))
  1265. {
  1266. string_appendn (s, *mangled, 1);
  1267. (*mangled)++;
  1268. }
  1269. }
  1270. return 1;
  1271. }
  1272. static int
  1273. demangle_template_value_parm (
  1274. work_stuff *work,
  1275. const char **mangled,
  1276. string* s,
  1277. type_kind_t tk)
  1278. {
  1279. int success = 1;
  1280. if (**mangled == 'Y')
  1281. {
  1282. /* The next argument is a template parameter. */
  1283. int idx;
  1284. (*mangled)++;
  1285. idx = consume_count_with_underscores (mangled);
  1286. if (idx == -1
  1287. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  1288. || consume_count_with_underscores (mangled) == -1)
  1289. return -1;
  1290. if (work->tmpl_argvec)
  1291. string_append (s, work->tmpl_argvec[idx]);
  1292. else
  1293. string_append_template_idx (s, idx);
  1294. }
  1295. else if (tk == tk_integral)
  1296. success = demangle_integral_value (work, mangled, s);
  1297. else if (tk == tk_char)
  1298. {
  1299. char tmp[2];
  1300. int val;
  1301. if (**mangled == 'm')
  1302. {
  1303. string_appendn (s, "-", 1);
  1304. (*mangled)++;
  1305. }
  1306. string_appendn (s, "'", 1);
  1307. val = consume_count(mangled);
  1308. if (val <= 0)
  1309. success = 0;
  1310. else
  1311. {
  1312. tmp[0] = (char)val;
  1313. tmp[1] = '\0';
  1314. string_appendn (s, &tmp[0], 1);
  1315. string_appendn (s, "'", 1);
  1316. }
  1317. }
  1318. else if (tk == tk_bool)
  1319. {
  1320. int val = consume_count (mangled);
  1321. if (val == 0)
  1322. string_appendn (s, "false", 5);
  1323. else if (val == 1)
  1324. string_appendn (s, "true", 4);
  1325. else
  1326. success = 0;
  1327. }
  1328. else if (tk == tk_real)
  1329. success = demangle_real_value (work, mangled, s);
  1330. else if (tk == tk_pointer || tk == tk_reference)
  1331. {
  1332. if (**mangled == 'Q')
  1333. success = demangle_qualified (work, mangled, s,
  1334. /*isfuncname=*/0,
  1335. /*append=*/1);
  1336. else
  1337. {
  1338. int symbol_len = consume_count (mangled);
  1339. if (symbol_len == -1)
  1340. return -1;
  1341. if (symbol_len == 0)
  1342. string_appendn (s, "0", 1);
  1343. else
  1344. {
  1345. char *p = (char *) xmalloc (symbol_len + 1), *q;
  1346. strncpy (p, *mangled, symbol_len);
  1347. p [symbol_len] = '\0';
  1348. /* We use cplus_demangle here, rather than
  1349. internal_cplus_demangle, because the name of the entity
  1350. mangled here does not make use of any of the squangling
  1351. or type-code information we have built up thus far; it is
  1352. mangled independently. */
  1353. q = cplus_demangle (p, work->options);
  1354. if (tk == tk_pointer)
  1355. string_appendn (s, "&", 1);
  1356. /* FIXME: Pointer-to-member constants should get a
  1357. qualifying class name here. */
  1358. if (q)
  1359. {
  1360. string_append (s, q);
  1361. free (q);
  1362. }
  1363. else
  1364. string_append (s, p);
  1365. free (p);
  1366. }
  1367. *mangled += symbol_len;
  1368. }
  1369. }
  1370. return success;
  1371. }
  1372. /* Demangle the template name in MANGLED. The full name of the
  1373. template (e.g., S<int>) is placed in TNAME. The name without the
  1374. template parameters (e.g. S) is placed in TRAWNAME if TRAWNAME is
  1375. non-NULL. If IS_TYPE is nonzero, this template is a type template,
  1376. not a function template. If both IS_TYPE and REMEMBER are nonzero,
  1377. the template is remembered in the list of back-referenceable
  1378. types. */
  1379. static int
  1380. demangle_template (
  1381. work_stuff *work,
  1382. const char **mangled,
  1383. string *tname,
  1384. string *trawname,
  1385. int is_type,
  1386. int remember)
  1387. {
  1388. int i;
  1389. int r;
  1390. int need_comma = 0;
  1391. int success = 0;
  1392. const char *start;
  1393. int is_java_array = 0;
  1394. string temp;
  1395. int bindex = 0;
  1396. (*mangled)++;
  1397. if (is_type)
  1398. {
  1399. if (remember)
  1400. bindex = register_Btype (work);
  1401. start = *mangled;
  1402. /* get template name */
  1403. if (**mangled == 'z')
  1404. {
  1405. int idx;
  1406. (*mangled)++;
  1407. (*mangled)++;
  1408. idx = consume_count_with_underscores (mangled);
  1409. if (idx == -1
  1410. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  1411. || consume_count_with_underscores (mangled) == -1)
  1412. return (0);
  1413. if (work->tmpl_argvec)
  1414. {
  1415. string_append (tname, work->tmpl_argvec[idx]);
  1416. if (trawname)
  1417. string_append (trawname, work->tmpl_argvec[idx]);
  1418. }
  1419. else
  1420. {
  1421. string_append_template_idx (tname, idx);
  1422. if (trawname)
  1423. string_append_template_idx (trawname, idx);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. if ((r = consume_count (mangled)) <= 0
  1429. || (int) strlen (*mangled) < r)
  1430. {
  1431. return (0);
  1432. }
  1433. string_appendn (tname, *mangled, r);
  1434. if (trawname)
  1435. string_appendn (trawname, *mangled, r);
  1436. *mangled += r;
  1437. }
  1438. }
  1439. if (!is_java_array)
  1440. string_append (tname, "<");
  1441. /* get size of template parameter list */
  1442. if (!get_count (mangled, &r))
  1443. {
  1444. return (0);
  1445. }
  1446. if (!is_type)
  1447. {
  1448. /* Create an array for saving the template argument values. */
  1449. work->tmpl_argvec = (char**) xmalloc (r * sizeof (char *));
  1450. work->ntmpl_args = r;
  1451. for (i = 0; i < r; i++)
  1452. work->tmpl_argvec[i] = 0;
  1453. }
  1454. for (i = 0; i < r; i++)
  1455. {
  1456. if (need_comma)
  1457. {
  1458. string_append (tname, ", ");
  1459. }
  1460. /* Z for type parameters */
  1461. if (**mangled == 'Z')
  1462. {
  1463. (*mangled)++;
  1464. /* temp is initialized in do_type */
  1465. success = do_type (work, mangled, &temp);
  1466. if (success)
  1467. {
  1468. string_appends (tname, &temp);
  1469. if (!is_type)
  1470. {
  1471. /* Save the template argument. */
  1472. int len = temp.p - temp.b;
  1473. work->tmpl_argvec[i] = (char *) xmalloc (len + 1);
  1474. memcpy (work->tmpl_argvec[i], temp.b, len);
  1475. work->tmpl_argvec[i][len] = '\0';
  1476. }
  1477. }
  1478. string_delete(&temp);
  1479. if (!success)
  1480. {
  1481. break;
  1482. }
  1483. }
  1484. /* z for template parameters */
  1485. else if (**mangled == 'z')
  1486. {
  1487. int r2;
  1488. (*mangled)++;
  1489. success = demangle_template_template_parm (work, mangled, tname);
  1490. if (success
  1491. && (r2 = consume_count (mangled)) > 0
  1492. && (int) strlen (*mangled) >= r2)
  1493. {
  1494. string_append (tname, " ");
  1495. string_appendn (tname, *mangled, r2);
  1496. if (!is_type)
  1497. {
  1498. /* Save the template argument. */
  1499. int len = r2;
  1500. work->tmpl_argvec[i] = (char *) xmalloc (len + 1);
  1501. memcpy (work->tmpl_argvec[i], *mangled, len);
  1502. work->tmpl_argvec[i][len] = '\0';
  1503. }
  1504. *mangled += r2;
  1505. }
  1506. if (!success)
  1507. {
  1508. break;
  1509. }
  1510. }
  1511. else
  1512. {
  1513. string param;
  1514. string* s;
  1515. /* otherwise, value parameter */
  1516. /* temp is initialized in do_type */
  1517. success = do_type (work, mangled, &temp);
  1518. string_delete(&temp);
  1519. if (!success)
  1520. break;
  1521. if (!is_type)
  1522. {
  1523. s = &param;
  1524. string_init (s);
  1525. }
  1526. else
  1527. s = tname;
  1528. success = demangle_template_value_parm (work, mangled, s,
  1529. (type_kind_t) success);
  1530. if (!success)
  1531. {
  1532. if (!is_type)
  1533. string_delete (s);
  1534. success = 0;
  1535. break;
  1536. }
  1537. if (!is_type)
  1538. {
  1539. int len = s->p - s->b;
  1540. work->tmpl_argvec[i] = (char *) xmalloc (len + 1);
  1541. memcpy (work->tmpl_argvec[i], s->b, len);
  1542. work->tmpl_argvec[i][len] = '\0';
  1543. string_appends (tname, s);
  1544. string_delete (s);
  1545. }
  1546. }
  1547. need_comma = 1;
  1548. }
  1549. if (is_java_array)
  1550. {
  1551. string_append (tname, "[]");
  1552. }
  1553. else
  1554. {
  1555. if (tname->p[-1] == '>')
  1556. string_append (tname, " ");
  1557. string_append (tname, ">");
  1558. }
  1559. if (is_type && remember)
  1560. remember_Btype (work, tname->b, LEN_STRING (tname), bindex);
  1561. /*
  1562. if (work -> static_type)
  1563. {
  1564. string_append (declp, *mangled + 1);
  1565. *mangled += strlen (*mangled);
  1566. success = 1;
  1567. }
  1568. else
  1569. {
  1570. success = demangle_args (work, mangled, declp);
  1571. }
  1572. }
  1573. */
  1574. return (success);
  1575. }
  1576. static int
  1577. arm_pt (
  1578. work_stuff *work,
  1579. const char *mangled,
  1580. int n,
  1581. const char **anchor,
  1582. const char **args)
  1583. {
  1584. /* Check if ARM template with "__pt__" in it ("parameterized type") */
  1585. /* Allow HP also here, because HP's cfront compiler follows ARM to some extent */
  1586. if ((ARM_DEMANGLING || HP_DEMANGLING) && (*anchor = strstr (mangled, "__pt__")))
  1587. {
  1588. int len;
  1589. *args = *anchor + 6;
  1590. len = consume_count (args);
  1591. if (len == -1)
  1592. return 0;
  1593. if (*args + len == mangled + n && **args == '_')
  1594. {
  1595. ++*args;
  1596. return 1;
  1597. }
  1598. }
  1599. if (AUTO_DEMANGLING || EDG_DEMANGLING)
  1600. {
  1601. if ((*anchor = strstr (mangled, "__tm__"))
  1602. || (*anchor = strstr (mangled, "__ps__"))
  1603. || (*anchor = strstr (mangled, "__pt__")))
  1604. {
  1605. int len;
  1606. *args = *anchor + 6;
  1607. len = consume_count (args);
  1608. if (len == -1)
  1609. return 0;
  1610. if (*args + len == mangled + n && **args == '_')
  1611. {
  1612. ++*args;
  1613. return 1;
  1614. }
  1615. }
  1616. else if ((*anchor = strstr (mangled, "__S")))
  1617. {
  1618. int len;
  1619. *args = *anchor + 3;
  1620. len = consume_count (args);
  1621. if (len == -1)
  1622. return 0;
  1623. if (*args + len == mangled + n && **args == '_')
  1624. {
  1625. ++*args;
  1626. return 1;
  1627. }
  1628. }
  1629. }
  1630. return 0;
  1631. }
  1632. static void
  1633. demangle_arm_hp_template (
  1634. work_stuff *work,
  1635. const char **mangled,
  1636. int n,
  1637. string *declp)
  1638. {
  1639. const char *p;
  1640. const char *args;
  1641. const char *e = *mangled + n;
  1642. string arg;
  1643. /* Check for HP aCC template spec: classXt1t2 where t1, t2 are
  1644. template args */
  1645. if (HP_DEMANGLING && ((*mangled)[n] == 'X'))
  1646. {
  1647. char *start_spec_args = NULL;
  1648. /* First check for and omit template specialization pseudo-arguments,
  1649. such as in "Spec<#1,#1.*>" */
  1650. start_spec_args = strchr (*mangled, '<');
  1651. if (start_spec_args && (start_spec_args - *mangled < n))
  1652. string_appendn (declp, *mangled, start_spec_args - *mangled);
  1653. else
  1654. string_appendn (declp, *mangled, n);
  1655. (*mangled) += n + 1;
  1656. string_init (&arg);
  1657. if (work->temp_start == -1) /* non-recursive call */
  1658. work->temp_start = declp->p - declp->b;
  1659. string_append (declp, "<");
  1660. while (1)
  1661. {
  1662. string_clear (&arg);
  1663. switch (**mangled)
  1664. {
  1665. case 'T':
  1666. /* 'T' signals a type parameter */
  1667. (*mangled)++;
  1668. if (!do_type (work, mangled, &arg))
  1669. goto hpacc_template_args_done;
  1670. break;
  1671. case 'U':
  1672. case 'S':
  1673. /* 'U' or 'S' signals an integral value */
  1674. if (!do_hpacc_template_const_value (work, mangled, &arg))
  1675. goto hpacc_template_args_done;
  1676. break;
  1677. case 'A':
  1678. /* 'A' signals a named constant expression (literal) */
  1679. if (!do_hpacc_template_literal (work, mangled, &arg))
  1680. goto hpacc_template_args_done;
  1681. break;
  1682. default:
  1683. /* Today, 1997-09-03, we have only the above types
  1684. of template parameters */
  1685. /* FIXME: maybe this should fail and return null */
  1686. goto hpacc_template_args_done;
  1687. }
  1688. string_appends (declp, &arg);
  1689. /* Check if we're at the end of template args.
  1690. 0 if at end of static member of template class,
  1691. _ if done with template args for a function */
  1692. if ((**mangled == '\000') || (**mangled == '_'))
  1693. break;
  1694. else
  1695. string_append (declp, ",");
  1696. }
  1697. hpacc_template_args_done:
  1698. string_append (declp, ">");
  1699. string_delete (&arg);
  1700. if (**mangled == '_')
  1701. (*mangled)++;
  1702. return;
  1703. }
  1704. /* ARM template? (Also handles HP cfront extensions) */
  1705. else if (arm_pt (work, *mangled, n, &p, &args))
  1706. {
  1707. string type_str;
  1708. string_init (&arg);
  1709. string_appendn (declp, *mangled, p - *mangled);
  1710. if (work->temp_start == -1) /* non-recursive call */
  1711. work->temp_start = declp->p - declp->b;
  1712. string_append (declp, "<");
  1713. /* should do error checking here */
  1714. while (args < e) {
  1715. string_clear (&arg);
  1716. /* Check for type or literal here */
  1717. switch (*args)
  1718. {
  1719. /* HP cfront extensions to ARM for template args */
  1720. /* spec: Xt1Lv1 where t1 is a type, v1 is a literal value */
  1721. /* FIXME: We handle only numeric literals for HP cfront */
  1722. case 'X':
  1723. /* A typed constant value follows */
  1724. args++;
  1725. if (!do_type (work, &args, &type_str))
  1726. goto cfront_template_args_done;
  1727. string_append (&arg, "(");
  1728. string_appends (&arg, &type_str);
  1729. string_append (&arg, ")");
  1730. if (*args != 'L')
  1731. goto cfront_template_args_done;
  1732. args++;
  1733. /* Now snarf a literal value following 'L' */
  1734. if (!snarf_numeric_literal (&args, &arg))
  1735. goto cfront_template_args_done;
  1736. break;
  1737. case 'L':
  1738. /* Snarf a literal following 'L' */
  1739. args++;
  1740. if (!snarf_numeric_literal (&args, &arg))
  1741. goto cfront_template_args_done;
  1742. break;
  1743. default:
  1744. /* Not handling other HP cfront stuff */
  1745. if (!do_type (work, &args, &arg))
  1746. goto cfront_template_args_done;
  1747. }
  1748. string_appends (declp, &arg);
  1749. string_append (declp, ",");
  1750. }
  1751. cfront_template_args_done:
  1752. string_delete (&arg);
  1753. if (args >= e)
  1754. --declp->p; /* remove extra comma */
  1755. string_append (declp, ">");
  1756. }
  1757. else if (n>10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
  1758. && (*mangled)[9] == 'N'
  1759. && (*mangled)[8] == (*mangled)[10]
  1760. && strchr (cplus_markers, (*mangled)[8]))
  1761. {
  1762. /* A member of the anonymous namespace. */
  1763. string_append (declp, "{anonymous}");
  1764. }
  1765. else
  1766. {
  1767. if (work->temp_start == -1) /* non-recursive call only */
  1768. work->temp_start = 0; /* disable in recursive calls */
  1769. string_appendn (declp, *mangled, n);
  1770. }
  1771. *mangled += n;
  1772. }
  1773. /* Extract a class name, possibly a template with arguments, from the
  1774. mangled string; qualifiers, local class indicators, etc. have
  1775. already been dealt with */
  1776. static int
  1777. demangle_class_name (
  1778. work_stuff *work,
  1779. const char **mangled,
  1780. string *declp)
  1781. {
  1782. int n;
  1783. int success = 0;
  1784. n = consume_count (mangled);
  1785. if (n == -1)
  1786. return 0;
  1787. if ((int) strlen (*mangled) >= n)
  1788. {
  1789. demangle_arm_hp_template (work, mangled, n, declp);
  1790. success = 1;
  1791. }
  1792. return (success);
  1793. }
  1794. /*
  1795. LOCAL FUNCTION
  1796. demangle_class -- demangle a mangled class sequence
  1797. SYNOPSIS
  1798. static int
  1799. demangle_class (work_stuff *work, const char **mangled,
  1800. strint *declp)
  1801. DESCRIPTION
  1802. DECLP points to the buffer into which demangling is being done.
  1803. *MANGLED points to the current token to be demangled. On input,
  1804. it points to a mangled class (I.E. "3foo", "13verylongclass", etc.)
  1805. On exit, it points to the next token after the mangled class on
  1806. success, or the first unconsumed token on failure.
  1807. If the CONSTRUCTOR or DESTRUCTOR flags are set in WORK, then
  1808. we are demangling a constructor or destructor. In this case
  1809. we prepend "class::class" or "class::~class" to DECLP.
  1810. Otherwise, we prepend "class::" to the current DECLP.
  1811. Reset the constructor/destructor flags once they have been
  1812. "consumed". This allows demangle_class to be called later during
  1813. the same demangling, to do normal class demangling.
  1814. Returns 1 if demangling is successful, 0 otherwise.
  1815. */
  1816. static int
  1817. demangle_class (
  1818. work_stuff *work,
  1819. const char **mangled,
  1820. string *declp)
  1821. {
  1822. int success = 0;
  1823. int btype;
  1824. string class_name;
  1825. char *save_class_name_end = 0;
  1826. string_init (&class_name);
  1827. btype = register_Btype (work);
  1828. if (demangle_class_name (work, mangled, &class_name))
  1829. {
  1830. save_class_name_end = class_name.p;
  1831. if ((work->constructor & 1) || (work->destructor & 1))
  1832. {
  1833. /* adjust so we don't include template args */
  1834. if (work->temp_start && (work->temp_start != -1))
  1835. {
  1836. class_name.p = class_name.b + work->temp_start;
  1837. }
  1838. string_prepends (declp, &class_name);
  1839. if (work -> destructor & 1)
  1840. {
  1841. string_prepend (declp, "~");
  1842. work -> destructor -= 1;
  1843. }
  1844. else
  1845. {
  1846. work -> constructor -= 1;
  1847. }
  1848. }
  1849. class_name.p = save_class_name_end;
  1850. remember_Ktype (work, class_name.b, LEN_STRING(&class_name));
  1851. remember_Btype (work, class_name.b, LEN_STRING(&class_name), btype);
  1852. string_prepend (declp, SCOPE_STRING (work));
  1853. string_prepends (declp, &class_name);
  1854. success = 1;
  1855. }
  1856. string_delete (&class_name);
  1857. return (success);
  1858. }
  1859. /* Called when there's a "__" in the mangled name, with `scan' pointing to
  1860. the rightmost guess.
  1861. Find the correct "__"-sequence where the function name ends and the
  1862. signature starts, which is ambiguous with GNU mangling.
  1863. Call demangle_signature here, so we can make sure we found the right
  1864. one; *mangled will be consumed so caller will not make further calls to
  1865. demangle_signature. */
  1866. static int
  1867. iterate_demangle_function (
  1868. work_stuff *work,
  1869. const char **mangled,
  1870. string *declp,
  1871. const char *scan)
  1872. {
  1873. const char *mangle_init = *mangled;
  1874. int success = 0;
  1875. string decl_init;
  1876. work_stuff work_init;
  1877. if (*(scan + 2) == '\0')
  1878. return 0;
  1879. /* Do not iterate for some demangling modes, or if there's only one
  1880. "__"-sequence. This is the normal case. */
  1881. if (ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING
  1882. || strstr (scan + 2, "__") == NULL)
  1883. {
  1884. demangle_function_name (work, mangled, declp, scan);
  1885. return 1;
  1886. }
  1887. /* Save state so we can restart if the guess at the correct "__" was
  1888. wrong. */
  1889. string_init (&decl_init);
  1890. string_appends (&decl_init, declp);
  1891. memset (&work_init, 0, sizeof work_init);
  1892. work_stuff_copy_to_from (&work_init, work);
  1893. /* Iterate over occurrences of __, allowing names and types to have a
  1894. "__" sequence in them. We must start with the first (not the last)
  1895. occurrence, since "__" most often occur between independent mangled
  1896. parts, hence starting at the last occurrence inside a signature
  1897. might get us a "successful" demangling of the signature. */
  1898. while (scan[2])
  1899. {
  1900. demangle_function_name (work, mangled, declp, scan);
  1901. success = demangle_signature (work, mangled, declp);
  1902. if (success)
  1903. break;
  1904. /* Reset demangle state for the next round. */
  1905. *mangled = mangle_init;
  1906. string_clear (declp);
  1907. string_appends (declp, &decl_init);
  1908. work_stuff_copy_to_from (work, &work_init);
  1909. /* Leave this underscore-sequence. */
  1910. scan += 2;
  1911. /* Scan for the next "__" sequence. */
  1912. while (*scan && (scan[0] != '_' || scan[1] != '_'))
  1913. scan++;
  1914. /* Move to last "__" in this sequence. */
  1915. while (*scan && *scan == '_')
  1916. scan++;
  1917. scan -= 2;
  1918. }
  1919. /* Delete saved state. */
  1920. delete_work_stuff (&work_init);
  1921. string_delete (&decl_init);
  1922. return success;
  1923. }
  1924. /*
  1925. LOCAL FUNCTION
  1926. demangle_prefix -- consume the mangled name prefix and find signature
  1927. SYNOPSIS
  1928. static int
  1929. demangle_prefix (work_stuff *work, const char **mangled,
  1930. string *declp);
  1931. DESCRIPTION
  1932. Consume and demangle the prefix of the mangled name.
  1933. While processing the function name root, arrange to call
  1934. demangle_signature if the root is ambiguous.
  1935. DECLP points to the string buffer into which demangled output is
  1936. placed. On entry, the buffer is empty. On exit it contains
  1937. the root function name, the demangled operator name, or in some
  1938. special cases either nothing or the completely demangled result.
  1939. MANGLED points to the current pointer into the mangled name. As each
  1940. token of the mangled name is consumed, it is updated. Upon entry
  1941. the current mangled name pointer points to the first character of
  1942. the mangled name. Upon exit, it should point to the first character
  1943. of the signature if demangling was successful, or to the first
  1944. unconsumed character if demangling of the prefix was unsuccessful.
  1945. Returns 1 on success, 0 otherwise.
  1946. */
  1947. static int
  1948. demangle_prefix (
  1949. work_stuff *work,
  1950. const char **mangled,
  1951. string *declp)
  1952. {
  1953. int success = 1;
  1954. const char *scan;
  1955. int i;
  1956. if (strlen(*mangled) > 6
  1957. && (strncmp(*mangled, "_imp__", 6) == 0
  1958. || strncmp(*mangled, "__imp_", 6) == 0))
  1959. {
  1960. /* it's a symbol imported from a PE dynamic library. Check for both
  1961. new style prefix _imp__ and legacy __imp_ used by older versions
  1962. of dlltool. */
  1963. (*mangled) += 6;
  1964. work->dllimported = 1;
  1965. }
  1966. else if (strlen(*mangled) >= 11 && strncmp(*mangled, "_GLOBAL_", 8) == 0)
  1967. {
  1968. char *marker = strchr (cplus_markers, (*mangled)[8]);
  1969. if (marker != NULL && *marker == (*mangled)[10])
  1970. {
  1971. if ((*mangled)[9] == 'D')
  1972. {
  1973. /* it's a GNU global destructor to be executed at program exit */
  1974. (*mangled) += 11;
  1975. work->destructor = 2;
  1976. if (gnu_special (work, mangled, declp))
  1977. return success;
  1978. }
  1979. else if ((*mangled)[9] == 'I')
  1980. {
  1981. /* it's a GNU global constructor to be executed at program init */
  1982. (*mangled) += 11;
  1983. work->constructor = 2;
  1984. if (gnu_special (work, mangled, declp))
  1985. return success;
  1986. }
  1987. }
  1988. }
  1989. else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__std__", 7) == 0)
  1990. {
  1991. /* it's a ARM global destructor to be executed at program exit */
  1992. (*mangled) += 7;
  1993. work->destructor = 2;
  1994. }
  1995. else if ((ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING) && strncmp(*mangled, "__sti__", 7) == 0)
  1996. {
  1997. /* it's a ARM global constructor to be executed at program initial */
  1998. (*mangled) += 7;
  1999. work->constructor = 2;
  2000. }
  2001. /* This block of code is a reduction in strength time optimization
  2002. of:
  2003. scan = strstr (*mangled, "__"); */
  2004. {
  2005. scan = *mangled;
  2006. do {
  2007. scan = strchr (scan, '_');
  2008. } while (scan != NULL && *++scan != '_');
  2009. if (scan != NULL) --scan;
  2010. }
  2011. if (scan != NULL)
  2012. {
  2013. /* We found a sequence of two or more '_', ensure that we start at
  2014. the last pair in the sequence. */
  2015. /* i = strspn (scan, "_"); */
  2016. i = 0;
  2017. while (scan[i] == '_') i++;
  2018. if (i > 2)
  2019. {
  2020. scan += (i - 2);
  2021. }
  2022. }
  2023. if (scan == NULL)
  2024. {
  2025. success = 0;
  2026. }
  2027. else if (work -> static_type)
  2028. {
  2029. if (!ISDIGIT ((unsigned char)scan[0]) && (scan[0] != 't'))
  2030. {
  2031. success = 0;
  2032. }
  2033. }
  2034. else if ((scan == *mangled)
  2035. && (ISDIGIT ((unsigned char)scan[2]) || (scan[2] == 'Q')
  2036. || (scan[2] == 't') || (scan[2] == 'K') || (scan[2] == 'H')))
  2037. {
  2038. /* The ARM says nothing about the mangling of local variables.
  2039. But cfront mangles local variables by prepending __<nesting_level>
  2040. to them. As an extension to ARM demangling we handle this case. */
  2041. if ((LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING)
  2042. && ISDIGIT ((unsigned char)scan[2]))
  2043. {
  2044. *mangled = scan + 2;
  2045. consume_count (mangled);
  2046. string_append (declp, *mangled);
  2047. *mangled += strlen (*mangled);
  2048. success = 1;
  2049. }
  2050. else
  2051. {
  2052. /* A GNU style constructor starts with __[0-9Qt]. But cfront uses
  2053. names like __Q2_3foo3bar for nested type names. So don't accept
  2054. this style of constructor for cfront demangling. A GNU
  2055. style member-template constructor starts with 'H'. */
  2056. if (!(LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING))
  2057. work -> constructor += 1;
  2058. *mangled = scan + 2;
  2059. }
  2060. }
  2061. else if (ARM_DEMANGLING && scan[2] == 'p' && scan[3] == 't')
  2062. {
  2063. /* Cfront-style parameterized type. Handled later as a signature. */
  2064. success = 1;
  2065. /* ARM template? */
  2066. demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
  2067. }
  2068. else if (EDG_DEMANGLING && ((scan[2] == 't' && scan[3] == 'm')
  2069. || (scan[2] == 'p' && scan[3] == 's')
  2070. || (scan[2] == 'p' && scan[3] == 't')))
  2071. {
  2072. /* EDG-style parameterized type. Handled later as a signature. */
  2073. success = 1;
  2074. /* EDG template? */
  2075. demangle_arm_hp_template (work, mangled, strlen (*mangled), declp);
  2076. }
  2077. else if ((scan == *mangled) && !ISDIGIT ((unsigned char)scan[2])
  2078. && (scan[2] != 't'))
  2079. {
  2080. /* Mangled name starts with "__". Skip over any leading '_' characters,
  2081. then find the next "__" that separates the prefix from the signature.
  2082. */
  2083. if (!(ARM_DEMANGLING || LUCID_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  2084. || (arm_special (mangled, declp) == 0))
  2085. {
  2086. while (*scan == '_')
  2087. {
  2088. scan++;
  2089. }
  2090. if ((scan = strstr (scan, "__")) == NULL || (*(scan + 2) == '\0'))
  2091. {
  2092. /* No separator (I.E. "__not_mangled"), or empty signature
  2093. (I.E. "__not_mangled_either__") */
  2094. success = 0;
  2095. }
  2096. else
  2097. return iterate_demangle_function (work, mangled, declp, scan);
  2098. }
  2099. }
  2100. else if (*(scan + 2) != '\0')
  2101. {
  2102. /* Mangled name does not start with "__" but does have one somewhere
  2103. in there with non empty stuff after it. Looks like a global
  2104. function name. Iterate over all "__":s until the right
  2105. one is found. */
  2106. return iterate_demangle_function (work, mangled, declp, scan);
  2107. }
  2108. else
  2109. {
  2110. /* Doesn't look like a mangled name */
  2111. success = 0;
  2112. }
  2113. if (!success && (work->constructor == 2 || work->destructor == 2))
  2114. {
  2115. string_append (declp, *mangled);
  2116. *mangled += strlen (*mangled);
  2117. success = 1;
  2118. }
  2119. return (success);
  2120. }
  2121. /*
  2122. LOCAL FUNCTION
  2123. gnu_special -- special handling of gnu mangled strings
  2124. SYNOPSIS
  2125. static int
  2126. gnu_special (work_stuff *work, const char **mangled,
  2127. string *declp);
  2128. DESCRIPTION
  2129. Process some special GNU style mangling forms that don't fit
  2130. the normal pattern. For example:
  2131. _$_3foo (destructor for class foo)
  2132. _vt$foo (foo virtual table)
  2133. _vt$foo$bar (foo::bar virtual table)
  2134. __vt_foo (foo virtual table, new style with thunks)
  2135. _3foo$varname (static data member)
  2136. _Q22rs2tu$vw (static data member)
  2137. __t6vector1Zii (constructor with template)
  2138. __thunk_4__$_7ostream (virtual function thunk)
  2139. */
  2140. static int
  2141. gnu_special (
  2142. work_stuff *work,
  2143. const char **mangled,
  2144. string *declp)
  2145. {
  2146. int n;
  2147. int success = 1;
  2148. const char *p;
  2149. if ((*mangled)[0] == '_'
  2150. && strchr (cplus_markers, (*mangled)[1]) != NULL
  2151. && (*mangled)[2] == '_')
  2152. {
  2153. /* Found a GNU style destructor, get past "_<CPLUS_MARKER>_" */
  2154. (*mangled) += 3;
  2155. work -> destructor += 1;
  2156. }
  2157. else if ((*mangled)[0] == '_'
  2158. && (((*mangled)[1] == '_'
  2159. && (*mangled)[2] == 'v'
  2160. && (*mangled)[3] == 't'
  2161. && (*mangled)[4] == '_')
  2162. || ((*mangled)[1] == 'v'
  2163. && (*mangled)[2] == 't'
  2164. && strchr (cplus_markers, (*mangled)[3]) != NULL)))
  2165. {
  2166. /* Found a GNU style virtual table, get past "_vt<CPLUS_MARKER>"
  2167. and create the decl. Note that we consume the entire mangled
  2168. input string, which means that demangle_signature has no work
  2169. to do. */
  2170. if ((*mangled)[2] == 'v')
  2171. (*mangled) += 5; /* New style, with thunks: "__vt_" */
  2172. else
  2173. (*mangled) += 4; /* Old style, no thunks: "_vt<CPLUS_MARKER>" */
  2174. while (**mangled != '\0')
  2175. {
  2176. switch (**mangled)
  2177. {
  2178. case 'Q':
  2179. case 'K':
  2180. success = demangle_qualified (work, mangled, declp, 0, 1);
  2181. break;
  2182. case 't':
  2183. success = demangle_template (work, mangled, declp, 0, 1,
  2184. 1);
  2185. break;
  2186. default:
  2187. if (ISDIGIT((unsigned char)*mangled[0]))
  2188. {
  2189. n = consume_count(mangled);
  2190. /* We may be seeing a too-large size, or else a
  2191. ".<digits>" indicating a static local symbol. In
  2192. any case, declare victory and move on; *don't* try
  2193. to use n to allocate. */
  2194. if (n > (int) strlen (*mangled))
  2195. {
  2196. success = 1;
  2197. break;
  2198. }
  2199. }
  2200. else
  2201. {
  2202. /*n = strcspn (*mangled, cplus_markers);*/
  2203. const char *check = *mangled;
  2204. n = 0;
  2205. while (*check)
  2206. if (strchr (cplus_markers, *check++) == NULL)
  2207. n++;
  2208. else
  2209. break;
  2210. }
  2211. string_appendn (declp, *mangled, n);
  2212. (*mangled) += n;
  2213. }
  2214. p = strpbrk (*mangled, cplus_markers);
  2215. if (success && ((p == NULL) || (p == *mangled)))
  2216. {
  2217. if (p != NULL)
  2218. {
  2219. string_append (declp, SCOPE_STRING (work));
  2220. (*mangled)++;
  2221. }
  2222. }
  2223. else
  2224. {
  2225. success = 0;
  2226. break;
  2227. }
  2228. }
  2229. if (success)
  2230. string_append (declp, " virtual table");
  2231. }
  2232. else if ((*mangled)[0] == '_'
  2233. && (strchr("0123456789Qt", (*mangled)[1]) != NULL)
  2234. && (p = strpbrk (*mangled, cplus_markers)) != NULL)
  2235. {
  2236. /* static data member, "_3foo$varname" for example */
  2237. (*mangled)++;
  2238. switch (**mangled)
  2239. {
  2240. case 'Q':
  2241. case 'K':
  2242. success = demangle_qualified (work, mangled, declp, 0, 1);
  2243. break;
  2244. case 't':
  2245. success = demangle_template (work, mangled, declp, 0, 1, 1);
  2246. break;
  2247. default:
  2248. n = consume_count (mangled);
  2249. if (n < 0 || n > (long) strlen (*mangled))
  2250. {
  2251. success = 0;
  2252. break;
  2253. }
  2254. if (n > 10 && strncmp (*mangled, "_GLOBAL_", 8) == 0
  2255. && (*mangled)[9] == 'N'
  2256. && (*mangled)[8] == (*mangled)[10]
  2257. && strchr (cplus_markers, (*mangled)[8]))
  2258. {
  2259. /* A member of the anonymous namespace. There's information
  2260. about what identifier or filename it was keyed to, but
  2261. it's just there to make the mangled name unique; we just
  2262. step over it. */
  2263. string_append (declp, "{anonymous}");
  2264. (*mangled) += n;
  2265. /* Now p points to the marker before the N, so we need to
  2266. update it to the first marker after what we consumed. */
  2267. p = strpbrk (*mangled, cplus_markers);
  2268. break;
  2269. }
  2270. string_appendn (declp, *mangled, n);
  2271. (*mangled) += n;
  2272. }
  2273. if (success && (p == *mangled))
  2274. {
  2275. /* Consumed everything up to the cplus_marker, append the
  2276. variable name. */
  2277. (*mangled)++;
  2278. string_append (declp, SCOPE_STRING (work));
  2279. n = strlen (*mangled);
  2280. string_appendn (declp, *mangled, n);
  2281. (*mangled) += n;
  2282. }
  2283. else
  2284. {
  2285. success = 0;
  2286. }
  2287. }
  2288. else if (strncmp (*mangled, "__thunk_", 8) == 0)
  2289. {
  2290. int delta;
  2291. (*mangled) += 8;
  2292. delta = consume_count (mangled);
  2293. if (delta == -1)
  2294. success = 0;
  2295. else
  2296. {
  2297. char *method = internal_cplus_demangle (work, ++*mangled);
  2298. if (method)
  2299. {
  2300. char buf[50];
  2301. sprintf (buf, "virtual function thunk (delta:%d) for ", -delta);
  2302. string_append (declp, buf);
  2303. string_append (declp, method);
  2304. free (method);
  2305. n = strlen (*mangled);
  2306. (*mangled) += n;
  2307. }
  2308. else
  2309. {
  2310. success = 0;
  2311. }
  2312. }
  2313. }
  2314. else if (strncmp (*mangled, "__t", 3) == 0
  2315. && ((*mangled)[3] == 'i' || (*mangled)[3] == 'f'))
  2316. {
  2317. p = (*mangled)[3] == 'i' ? " type_info node" : " type_info function";
  2318. (*mangled) += 4;
  2319. switch (**mangled)
  2320. {
  2321. case 'Q':
  2322. case 'K':
  2323. success = demangle_qualified (work, mangled, declp, 0, 1);
  2324. break;
  2325. case 't':
  2326. success = demangle_template (work, mangled, declp, 0, 1, 1);
  2327. break;
  2328. default:
  2329. success = do_type (work, mangled, declp);
  2330. break;
  2331. }
  2332. if (success && **mangled != '\0')
  2333. success = 0;
  2334. if (success)
  2335. string_append (declp, p);
  2336. }
  2337. else
  2338. {
  2339. success = 0;
  2340. }
  2341. return (success);
  2342. }
  2343. static void
  2344. recursively_demangle (
  2345. work_stuff *work,
  2346. const char **mangled,
  2347. string *result,
  2348. int namelength)
  2349. {
  2350. char * recurse = (char *)NULL;
  2351. char * recurse_dem = (char *)NULL;
  2352. recurse = (char *) xmalloc (namelength + 1);
  2353. memcpy (recurse, *mangled, namelength);
  2354. recurse[namelength] = '\000';
  2355. recurse_dem = cplus_demangle (recurse, work->options);
  2356. if (recurse_dem)
  2357. {
  2358. string_append (result, recurse_dem);
  2359. free (recurse_dem);
  2360. }
  2361. else
  2362. {
  2363. string_appendn (result, *mangled, namelength);
  2364. }
  2365. free (recurse);
  2366. *mangled += namelength;
  2367. }
  2368. /*
  2369. LOCAL FUNCTION
  2370. arm_special -- special handling of ARM/lucid mangled strings
  2371. SYNOPSIS
  2372. static int
  2373. arm_special (const char **mangled,
  2374. string *declp);
  2375. DESCRIPTION
  2376. Process some special ARM style mangling forms that don't fit
  2377. the normal pattern. For example:
  2378. __vtbl__3foo (foo virtual table)
  2379. __vtbl__3foo__3bar (bar::foo virtual table)
  2380. */
  2381. static int
  2382. arm_special (const char **mangled, string *declp)
  2383. {
  2384. int n;
  2385. int success = 1;
  2386. const char *scan;
  2387. if (strncmp (*mangled, ARM_VTABLE_STRING, ARM_VTABLE_STRLEN) == 0)
  2388. {
  2389. /* Found a ARM style virtual table, get past ARM_VTABLE_STRING
  2390. and create the decl. Note that we consume the entire mangled
  2391. input string, which means that demangle_signature has no work
  2392. to do. */
  2393. scan = *mangled + ARM_VTABLE_STRLEN;
  2394. while (*scan != '\0') /* first check it can be demangled */
  2395. {
  2396. n = consume_count (&scan);
  2397. if (n == -1)
  2398. {
  2399. return (0); /* no good */
  2400. }
  2401. scan += n;
  2402. if (scan[0] == '_' && scan[1] == '_')
  2403. {
  2404. scan += 2;
  2405. }
  2406. }
  2407. (*mangled) += ARM_VTABLE_STRLEN;
  2408. while (**mangled != '\0')
  2409. {
  2410. n = consume_count (mangled);
  2411. if (n == -1
  2412. || n > (long) strlen (*mangled))
  2413. return 0;
  2414. string_prependn (declp, *mangled, n);
  2415. (*mangled) += n;
  2416. if ((*mangled)[0] == '_' && (*mangled)[1] == '_')
  2417. {
  2418. string_prepend (declp, "::");
  2419. (*mangled) += 2;
  2420. }
  2421. }
  2422. string_append (declp, " virtual table");
  2423. }
  2424. else
  2425. {
  2426. success = 0;
  2427. }
  2428. return (success);
  2429. }
  2430. /*
  2431. LOCAL FUNCTION
  2432. demangle_qualified -- demangle 'Q' qualified name strings
  2433. SYNOPSIS
  2434. static int
  2435. demangle_qualified (work_stuff *, const char *mangled,
  2436. string *result, int isfuncname, int append);
  2437. DESCRIPTION
  2438. Demangle a qualified name, such as "Q25Outer5Inner" which is
  2439. the mangled form of "Outer::Inner". The demangled output is
  2440. prepended or appended to the result string according to the
  2441. state of the append flag.
  2442. If isfuncname is nonzero, then the qualified name we are building
  2443. is going to be used as a member function name, so if it is a
  2444. constructor or destructor function, append an appropriate
  2445. constructor or destructor name. I.E. for the above example,
  2446. the result for use as a constructor is "Outer::Inner::Inner"
  2447. and the result for use as a destructor is "Outer::Inner::~Inner".
  2448. BUGS
  2449. Numeric conversion is ASCII dependent (FIXME).
  2450. */
  2451. static int
  2452. demangle_qualified (
  2453. work_stuff *work,
  2454. const char **mangled,
  2455. string *result,
  2456. int isfuncname,
  2457. int append)
  2458. {
  2459. int qualifiers = 0;
  2460. int success = 1;
  2461. string temp;
  2462. string last_name;
  2463. int bindex = register_Btype (work);
  2464. /* We only make use of ISFUNCNAME if the entity is a constructor or
  2465. destructor. */
  2466. isfuncname = (isfuncname
  2467. && ((work->constructor & 1) || (work->destructor & 1)));
  2468. string_init (&temp);
  2469. string_init (&last_name);
  2470. if ((*mangled)[0] == 'K')
  2471. {
  2472. /* Squangling qualified name reuse */
  2473. int idx;
  2474. (*mangled)++;
  2475. idx = consume_count_with_underscores (mangled);
  2476. if (idx == -1 || idx >= work -> numk)
  2477. success = 0;
  2478. else
  2479. string_append (&temp, work -> ktypevec[idx]);
  2480. }
  2481. else
  2482. switch ((*mangled)[1])
  2483. {
  2484. case '_':
  2485. /* GNU mangled name with more than 9 classes. The count is preceded
  2486. by an underscore (to distinguish it from the <= 9 case) and followed
  2487. by an underscore. */
  2488. (*mangled)++;
  2489. qualifiers = consume_count_with_underscores (mangled);
  2490. if (qualifiers == -1)
  2491. success = 0;
  2492. break;
  2493. case '1':
  2494. case '2':
  2495. case '3':
  2496. case '4':
  2497. case '5':
  2498. case '6':
  2499. case '7':
  2500. case '8':
  2501. case '9':
  2502. /* The count is in a single digit. */
  2503. qualifiers = (*mangled)[1] - '0';
  2504. /* If there is an underscore after the digit, skip it. This is
  2505. said to be for ARM-qualified names, but the ARM makes no
  2506. mention of such an underscore. Perhaps cfront uses one. */
  2507. if ((*mangled)[2] == '_')
  2508. {
  2509. (*mangled)++;
  2510. }
  2511. (*mangled) += 2;
  2512. break;
  2513. case '0':
  2514. default:
  2515. success = 0;
  2516. }
  2517. if (!success)
  2518. {
  2519. string_delete (&last_name);
  2520. string_delete (&temp);
  2521. return success;
  2522. }
  2523. /* Pick off the names and collect them in the temp buffer in the order
  2524. in which they are found, separated by '::'. */
  2525. while (qualifiers-- > 0)
  2526. {
  2527. int remember_K = 1;
  2528. string_clear (&last_name);
  2529. if (*mangled[0] == '_')
  2530. (*mangled)++;
  2531. if (*mangled[0] == 't')
  2532. {
  2533. /* Here we always append to TEMP since we will want to use
  2534. the template name without the template parameters as a
  2535. constructor or destructor name. The appropriate
  2536. (parameter-less) value is returned by demangle_template
  2537. in LAST_NAME. We do not remember the template type here,
  2538. in order to match the G++ mangling algorithm. */
  2539. success = demangle_template(work, mangled, &temp,
  2540. &last_name, 1, 0);
  2541. if (!success)
  2542. break;
  2543. }
  2544. else if (*mangled[0] == 'K')
  2545. {
  2546. int idx;
  2547. (*mangled)++;
  2548. idx = consume_count_with_underscores (mangled);
  2549. if (idx == -1 || idx >= work->numk)
  2550. success = 0;
  2551. else
  2552. string_append (&temp, work->ktypevec[idx]);
  2553. remember_K = 0;
  2554. if (!success) break;
  2555. }
  2556. else
  2557. {
  2558. if (EDG_DEMANGLING)
  2559. {
  2560. int namelength;
  2561. /* Now recursively demangle the qualifier
  2562. * This is necessary to deal with templates in
  2563. * mangling styles like EDG */
  2564. namelength = consume_count (mangled);
  2565. if (namelength == -1)
  2566. {
  2567. success = 0;
  2568. break;
  2569. }
  2570. recursively_demangle(work, mangled, &temp, namelength);
  2571. }
  2572. else
  2573. {
  2574. string temp_last_name;
  2575. string_init (&temp_last_name);
  2576. success = do_type (work, mangled, &temp_last_name);
  2577. if (!success)
  2578. {
  2579. string_delete (&temp_last_name);
  2580. break;
  2581. }
  2582. string_appends (&temp, &temp_last_name);
  2583. string_appends (&last_name, &temp_last_name);
  2584. string_delete (&temp_last_name);
  2585. }
  2586. }
  2587. if (remember_K)
  2588. remember_Ktype (work, temp.b, LEN_STRING (&temp));
  2589. if (qualifiers > 0)
  2590. string_append (&temp, SCOPE_STRING (work));
  2591. }
  2592. remember_Btype (work, temp.b, LEN_STRING (&temp), bindex);
  2593. /* If we are using the result as a function name, we need to append
  2594. the appropriate '::' separated constructor or destructor name.
  2595. We do this here because this is the most convenient place, where
  2596. we already have a pointer to the name and the length of the name. */
  2597. if (isfuncname)
  2598. {
  2599. string_append (&temp, SCOPE_STRING (work));
  2600. if (work -> destructor & 1)
  2601. string_append (&temp, "~");
  2602. string_appends (&temp, &last_name);
  2603. }
  2604. /* Now either prepend the temp buffer to the result, or append it,
  2605. depending upon the state of the append flag. */
  2606. if (append)
  2607. string_appends (result, &temp);
  2608. else
  2609. {
  2610. if (!STRING_EMPTY (result))
  2611. string_append (&temp, SCOPE_STRING (work));
  2612. string_prepends (result, &temp);
  2613. }
  2614. string_delete (&last_name);
  2615. string_delete (&temp);
  2616. return (success);
  2617. }
  2618. /*
  2619. LOCAL FUNCTION
  2620. get_count -- convert an ascii count to integer, consuming tokens
  2621. SYNOPSIS
  2622. static int
  2623. get_count (const char **type, int *count)
  2624. DESCRIPTION
  2625. Assume that *type points at a count in a mangled name; set
  2626. *count to its value, and set *type to the next character after
  2627. the count. There are some weird rules in effect here.
  2628. If *type does not point at a string of digits, return zero.
  2629. If *type points at a string of digits followed by an
  2630. underscore, set *count to their value as an integer, advance
  2631. *type to point *after the underscore, and return 1.
  2632. If *type points at a string of digits not followed by an
  2633. underscore, consume only the first digit. Set *count to its
  2634. value as an integer, leave *type pointing after that digit,
  2635. and return 1.
  2636. The excuse for this odd behavior: in the ARM and HP demangling
  2637. styles, a type can be followed by a repeat count of the form
  2638. `Nxy', where:
  2639. `x' is a single digit specifying how many additional copies
  2640. of the type to append to the argument list, and
  2641. `y' is one or more digits, specifying the zero-based index of
  2642. the first repeated argument in the list. Yes, as you're
  2643. unmangling the name you can figure this out yourself, but
  2644. it's there anyway.
  2645. So, for example, in `bar__3fooFPiN51', the first argument is a
  2646. pointer to an integer (`Pi'), and then the next five arguments
  2647. are the same (`N5'), and the first repeat is the function's
  2648. second argument (`1').
  2649. */
  2650. static int
  2651. get_count (const char **type, int *count)
  2652. {
  2653. const char *p;
  2654. int n;
  2655. if (!ISDIGIT ((unsigned char)**type))
  2656. return (0);
  2657. else
  2658. {
  2659. *count = **type - '0';
  2660. (*type)++;
  2661. if (ISDIGIT ((unsigned char)**type))
  2662. {
  2663. p = *type;
  2664. n = *count;
  2665. do
  2666. {
  2667. n *= 10;
  2668. n += *p - '0';
  2669. p++;
  2670. }
  2671. while (ISDIGIT ((unsigned char)*p));
  2672. if (*p == '_')
  2673. {
  2674. *type = p + 1;
  2675. *count = n;
  2676. }
  2677. }
  2678. }
  2679. return (1);
  2680. }
  2681. /* RESULT will be initialised here; it will be freed on failure. The
  2682. value returned is really a type_kind_t. */
  2683. static int
  2684. do_type (work_stuff *work, const char **mangled, string *result)
  2685. {
  2686. int n;
  2687. int done;
  2688. int success;
  2689. string decl;
  2690. const char *remembered_type;
  2691. int type_quals;
  2692. string btype;
  2693. type_kind_t tk = tk_none;
  2694. string_init (&btype);
  2695. string_init (&decl);
  2696. string_init (result);
  2697. done = 0;
  2698. success = 1;
  2699. while (success && !done)
  2700. {
  2701. int member;
  2702. switch (**mangled)
  2703. {
  2704. /* A pointer type */
  2705. case 'P':
  2706. case 'p':
  2707. (*mangled)++;
  2708. string_prepend (&decl, "*");
  2709. if (tk == tk_none)
  2710. tk = tk_pointer;
  2711. break;
  2712. /* A reference type */
  2713. case 'R':
  2714. (*mangled)++;
  2715. string_prepend (&decl, "&");
  2716. if (tk == tk_none)
  2717. tk = tk_reference;
  2718. break;
  2719. /* An array */
  2720. case 'A':
  2721. {
  2722. ++(*mangled);
  2723. if (!STRING_EMPTY (&decl)
  2724. && (decl.b[0] == '*' || decl.b[0] == '&'))
  2725. {
  2726. string_prepend (&decl, "(");
  2727. string_append (&decl, ")");
  2728. }
  2729. string_append (&decl, "[");
  2730. if (**mangled != '_')
  2731. success = demangle_template_value_parm (work, mangled, &decl,
  2732. tk_integral);
  2733. if (**mangled == '_')
  2734. ++(*mangled);
  2735. string_append (&decl, "]");
  2736. break;
  2737. }
  2738. /* A back reference to a previously seen type */
  2739. case 'T':
  2740. (*mangled)++;
  2741. if (!get_count (mangled, &n) || n >= work -> ntypes)
  2742. {
  2743. success = 0;
  2744. }
  2745. else
  2746. {
  2747. remembered_type = work -> typevec[n];
  2748. mangled = &remembered_type;
  2749. }
  2750. break;
  2751. /* A function */
  2752. case 'F':
  2753. (*mangled)++;
  2754. if (!STRING_EMPTY (&decl)
  2755. && (decl.b[0] == '*' || decl.b[0] == '&'))
  2756. {
  2757. string_prepend (&decl, "(");
  2758. string_append (&decl, ")");
  2759. }
  2760. /* After picking off the function args, we expect to either find the
  2761. function return type (preceded by an '_') or the end of the
  2762. string. */
  2763. if (!demangle_nested_args (work, mangled, &decl)
  2764. || (**mangled != '_' && **mangled != '\0'))
  2765. {
  2766. success = 0;
  2767. break;
  2768. }
  2769. if (success && (**mangled == '_'))
  2770. (*mangled)++;
  2771. break;
  2772. case 'M':
  2773. case 'O':
  2774. {
  2775. type_quals = TYPE_UNQUALIFIED;
  2776. member = **mangled == 'M';
  2777. (*mangled)++;
  2778. string_append (&decl, ")");
  2779. /* We don't need to prepend `::' for a qualified name;
  2780. demangle_qualified will do that for us. */
  2781. if (**mangled != 'Q')
  2782. string_prepend (&decl, SCOPE_STRING (work));
  2783. if (ISDIGIT ((unsigned char)**mangled))
  2784. {
  2785. n = consume_count (mangled);
  2786. if (n == -1
  2787. || (int) strlen (*mangled) < n)
  2788. {
  2789. success = 0;
  2790. break;
  2791. }
  2792. string_prependn (&decl, *mangled, n);
  2793. *mangled += n;
  2794. }
  2795. else if (**mangled == 'X' || **mangled == 'Y')
  2796. {
  2797. string temp;
  2798. do_type (work, mangled, &temp);
  2799. string_prepends (&decl, &temp);
  2800. }
  2801. else if (**mangled == 't')
  2802. {
  2803. string temp;
  2804. string_init (&temp);
  2805. success = demangle_template (work, mangled, &temp,
  2806. NULL, 1, 1);
  2807. if (success)
  2808. {
  2809. string_prependn (&decl, temp.b, temp.p - temp.b);
  2810. string_clear (&temp);
  2811. }
  2812. else
  2813. break;
  2814. }
  2815. else if (**mangled == 'Q')
  2816. {
  2817. success = demangle_qualified (work, mangled, &decl,
  2818. /*isfuncnam=*/0,
  2819. /*append=*/0);
  2820. if (!success)
  2821. break;
  2822. }
  2823. else
  2824. {
  2825. success = 0;
  2826. break;
  2827. }
  2828. string_prepend (&decl, "(");
  2829. if (member)
  2830. {
  2831. switch (**mangled)
  2832. {
  2833. case 'C':
  2834. case 'V':
  2835. case 'u':
  2836. type_quals |= code_for_qualifier (**mangled);
  2837. (*mangled)++;
  2838. break;
  2839. default:
  2840. break;
  2841. }
  2842. if (*(*mangled)++ != 'F')
  2843. {
  2844. success = 0;
  2845. break;
  2846. }
  2847. }
  2848. if ((member && !demangle_nested_args (work, mangled, &decl))
  2849. || **mangled != '_')
  2850. {
  2851. success = 0;
  2852. break;
  2853. }
  2854. (*mangled)++;
  2855. if (! PRINT_ANSI_QUALIFIERS)
  2856. {
  2857. break;
  2858. }
  2859. if (type_quals != TYPE_UNQUALIFIED)
  2860. {
  2861. APPEND_BLANK (&decl);
  2862. string_append (&decl, qualifier_string (type_quals));
  2863. }
  2864. break;
  2865. }
  2866. case 'G':
  2867. (*mangled)++;
  2868. break;
  2869. case 'C':
  2870. case 'V':
  2871. case 'u':
  2872. if (PRINT_ANSI_QUALIFIERS)
  2873. {
  2874. if (!STRING_EMPTY (&decl))
  2875. string_prepend (&decl, " ");
  2876. string_prepend (&decl, demangle_qualifier (**mangled));
  2877. }
  2878. (*mangled)++;
  2879. break;
  2880. /*
  2881. }
  2882. */
  2883. /* fall through */
  2884. default:
  2885. done = 1;
  2886. break;
  2887. }
  2888. }
  2889. if (success) switch (**mangled)
  2890. {
  2891. /* A qualified name, such as "Outer::Inner". */
  2892. case 'Q':
  2893. case 'K':
  2894. {
  2895. success = demangle_qualified (work, mangled, result, 0, 1);
  2896. break;
  2897. }
  2898. /* A back reference to a previously seen squangled type */
  2899. case 'B':
  2900. (*mangled)++;
  2901. if (!get_count (mangled, &n) || n >= work -> numb)
  2902. success = 0;
  2903. else
  2904. string_append (result, work->btypevec[n]);
  2905. break;
  2906. case 'X':
  2907. case 'Y':
  2908. /* A template parm. We substitute the corresponding argument. */
  2909. {
  2910. int idx;
  2911. (*mangled)++;
  2912. idx = consume_count_with_underscores (mangled);
  2913. if (idx == -1
  2914. || (work->tmpl_argvec && idx >= work->ntmpl_args)
  2915. || consume_count_with_underscores (mangled) == -1)
  2916. {
  2917. success = 0;
  2918. break;
  2919. }
  2920. if (work->tmpl_argvec)
  2921. string_append (result, work->tmpl_argvec[idx]);
  2922. else
  2923. string_append_template_idx (result, idx);
  2924. success = 1;
  2925. }
  2926. break;
  2927. default:
  2928. success = demangle_fund_type (work, mangled, result);
  2929. if (tk == tk_none)
  2930. tk = (type_kind_t) success;
  2931. break;
  2932. }
  2933. if (success)
  2934. {
  2935. if (!STRING_EMPTY (&decl))
  2936. {
  2937. string_append (result, " ");
  2938. string_appends (result, &decl);
  2939. }
  2940. }
  2941. else
  2942. string_delete (result);
  2943. string_delete (&decl);
  2944. if (success)
  2945. /* Assume an integral type, if we're not sure. */
  2946. return (int) ((tk == tk_none) ? tk_integral : tk);
  2947. else
  2948. return 0;
  2949. }
  2950. /* Given a pointer to a type string that represents a fundamental type
  2951. argument (int, long, unsigned int, etc) in TYPE, a pointer to the
  2952. string in which the demangled output is being built in RESULT, and
  2953. the WORK structure, decode the types and add them to the result.
  2954. For example:
  2955. "Ci" => "const int"
  2956. "Sl" => "signed long"
  2957. "CUs" => "const unsigned short"
  2958. The value returned is really a type_kind_t. */
  2959. static int
  2960. demangle_fund_type (work_stuff *work, const char **mangled, string *result)
  2961. {
  2962. int done = 0;
  2963. int success = 1;
  2964. char buf[10];
  2965. unsigned int dec = 0;
  2966. string btype;
  2967. type_kind_t tk = tk_integral;
  2968. string_init (&btype);
  2969. /* First pick off any type qualifiers. There can be more than one. */
  2970. while (!done)
  2971. {
  2972. switch (**mangled)
  2973. {
  2974. case 'C':
  2975. case 'V':
  2976. case 'u':
  2977. if (PRINT_ANSI_QUALIFIERS)
  2978. {
  2979. if (!STRING_EMPTY (result))
  2980. string_prepend (result, " ");
  2981. string_prepend (result, demangle_qualifier (**mangled));
  2982. }
  2983. (*mangled)++;
  2984. break;
  2985. case 'U':
  2986. (*mangled)++;
  2987. APPEND_BLANK (result);
  2988. string_append (result, "unsigned");
  2989. break;
  2990. case 'S': /* signed char only */
  2991. (*mangled)++;
  2992. APPEND_BLANK (result);
  2993. string_append (result, "signed");
  2994. break;
  2995. case 'J':
  2996. (*mangled)++;
  2997. APPEND_BLANK (result);
  2998. string_append (result, "__complex");
  2999. break;
  3000. default:
  3001. done = 1;
  3002. break;
  3003. }
  3004. }
  3005. /* Now pick off the fundamental type. There can be only one. */
  3006. switch (**mangled)
  3007. {
  3008. case '\0':
  3009. case '_':
  3010. break;
  3011. case 'v':
  3012. (*mangled)++;
  3013. APPEND_BLANK (result);
  3014. string_append (result, "void");
  3015. break;
  3016. case 'x':
  3017. (*mangled)++;
  3018. APPEND_BLANK (result);
  3019. string_append (result, "long long");
  3020. break;
  3021. case 'l':
  3022. (*mangled)++;
  3023. APPEND_BLANK (result);
  3024. string_append (result, "long");
  3025. break;
  3026. case 'i':
  3027. (*mangled)++;
  3028. APPEND_BLANK (result);
  3029. string_append (result, "int");
  3030. break;
  3031. case 's':
  3032. (*mangled)++;
  3033. APPEND_BLANK (result);
  3034. string_append (result, "short");
  3035. break;
  3036. case 'b':
  3037. (*mangled)++;
  3038. APPEND_BLANK (result);
  3039. string_append (result, "bool");
  3040. tk = tk_bool;
  3041. break;
  3042. case 'c':
  3043. (*mangled)++;
  3044. APPEND_BLANK (result);
  3045. string_append (result, "char");
  3046. tk = tk_char;
  3047. break;
  3048. case 'w':
  3049. (*mangled)++;
  3050. APPEND_BLANK (result);
  3051. string_append (result, "wchar_t");
  3052. tk = tk_char;
  3053. break;
  3054. case 'r':
  3055. (*mangled)++;
  3056. APPEND_BLANK (result);
  3057. string_append (result, "long double");
  3058. tk = tk_real;
  3059. break;
  3060. case 'd':
  3061. (*mangled)++;
  3062. APPEND_BLANK (result);
  3063. string_append (result, "double");
  3064. tk = tk_real;
  3065. break;
  3066. case 'f':
  3067. (*mangled)++;
  3068. APPEND_BLANK (result);
  3069. string_append (result, "float");
  3070. tk = tk_real;
  3071. break;
  3072. case 'G':
  3073. (*mangled)++;
  3074. if (!ISDIGIT ((unsigned char)**mangled))
  3075. {
  3076. success = 0;
  3077. break;
  3078. }
  3079. case 'I':
  3080. (*mangled)++;
  3081. if (**mangled == '_')
  3082. {
  3083. int i;
  3084. (*mangled)++;
  3085. for (i = 0;
  3086. i < (long) sizeof (buf) - 1 && **mangled && **mangled != '_';
  3087. (*mangled)++, i++)
  3088. buf[i] = **mangled;
  3089. if (**mangled != '_')
  3090. {
  3091. success = 0;
  3092. break;
  3093. }
  3094. buf[i] = '\0';
  3095. (*mangled)++;
  3096. }
  3097. else
  3098. {
  3099. strncpy (buf, *mangled, 2);
  3100. buf[2] = '\0';
  3101. *mangled += min (strlen (*mangled), 2);
  3102. }
  3103. /*sscanf (buf, "%x", &dec);
  3104. sprintf (buf, "int%u_t", dec);*/
  3105. sprintf (buf, "i_xx_t");
  3106. APPEND_BLANK (result);
  3107. string_append (result, buf);
  3108. break;
  3109. /* fall through */
  3110. /* An explicit type, such as "6mytype" or "7integer" */
  3111. case '0':
  3112. case '1':
  3113. case '2':
  3114. case '3':
  3115. case '4':
  3116. case '5':
  3117. case '6':
  3118. case '7':
  3119. case '8':
  3120. case '9':
  3121. {
  3122. int bindex = register_Btype (work);
  3123. string loc_btype;
  3124. string_init (&loc_btype);
  3125. if (demangle_class_name (work, mangled, &loc_btype)) {
  3126. remember_Btype (work, loc_btype.b, LEN_STRING (&loc_btype), bindex);
  3127. APPEND_BLANK (result);
  3128. string_appends (result, &loc_btype);
  3129. }
  3130. else
  3131. success = 0;
  3132. string_delete (&loc_btype);
  3133. break;
  3134. }
  3135. case 't':
  3136. {
  3137. success = demangle_template (work, mangled, &btype, 0, 1, 1);
  3138. string_appends (result, &btype);
  3139. break;
  3140. }
  3141. default:
  3142. success = 0;
  3143. break;
  3144. }
  3145. string_delete (&btype);
  3146. return success ? ((int) tk) : 0;
  3147. }
  3148. /* Handle a template's value parameter for HP aCC (extension from ARM)
  3149. **mangled points to 'S' or 'U' */
  3150. static int
  3151. do_hpacc_template_const_value (
  3152. work_stuff *work ATTRIBUTE_UNUSED,
  3153. const char **mangled,
  3154. string *result)
  3155. {
  3156. int unsigned_const;
  3157. if (**mangled != 'U' && **mangled != 'S')
  3158. return 0;
  3159. unsigned_const = (**mangled == 'U');
  3160. (*mangled)++;
  3161. switch (**mangled)
  3162. {
  3163. case 'N':
  3164. string_append (result, "-");
  3165. /* fall through */
  3166. case 'P':
  3167. (*mangled)++;
  3168. break;
  3169. case 'M':
  3170. /* special case for -2^31 */
  3171. string_append (result, "-2147483648");
  3172. (*mangled)++;
  3173. return 1;
  3174. default:
  3175. return 0;
  3176. }
  3177. /* We have to be looking at an integer now */
  3178. if (!(ISDIGIT ((unsigned char)**mangled)))
  3179. return 0;
  3180. /* We only deal with integral values for template
  3181. parameters -- so it's OK to look only for digits */
  3182. while (ISDIGIT ((unsigned char)**mangled))
  3183. {
  3184. char_str[0] = **mangled;
  3185. string_append (result, char_str);
  3186. (*mangled)++;
  3187. }
  3188. if (unsigned_const)
  3189. string_append (result, "U");
  3190. /* FIXME? Some day we may have 64-bit (or larger :-) ) constants
  3191. with L or LL suffixes. pai/1997-09-03 */
  3192. return 1; /* success */
  3193. }
  3194. /* Handle a template's literal parameter for HP aCC (extension from ARM)
  3195. **mangled is pointing to the 'A' */
  3196. static int
  3197. do_hpacc_template_literal (
  3198. work_stuff *work,
  3199. const char **mangled,
  3200. string *result)
  3201. {
  3202. int literal_len = 0;
  3203. char * recurse;
  3204. char * recurse_dem;
  3205. if (**mangled != 'A')
  3206. return 0;
  3207. (*mangled)++;
  3208. literal_len = consume_count (mangled);
  3209. if (literal_len <= 0)
  3210. return 0;
  3211. /* Literal parameters are names of arrays, functions, etc. and the
  3212. canonical representation uses the address operator */
  3213. string_append (result, "&");
  3214. /* Now recursively demangle the literal name */
  3215. recurse = (char *) xmalloc (literal_len + 1);
  3216. memcpy (recurse, *mangled, literal_len);
  3217. recurse[literal_len] = '\000';
  3218. recurse_dem = cplus_demangle (recurse, work->options);
  3219. if (recurse_dem)
  3220. {
  3221. string_append (result, recurse_dem);
  3222. free (recurse_dem);
  3223. }
  3224. else
  3225. {
  3226. string_appendn (result, *mangled, literal_len);
  3227. }
  3228. (*mangled) += literal_len;
  3229. free (recurse);
  3230. return 1;
  3231. }
  3232. static int
  3233. snarf_numeric_literal (const char **args, string *arg)
  3234. {
  3235. if (**args == '-')
  3236. {
  3237. char_str[0] = '-';
  3238. string_append (arg, char_str);
  3239. (*args)++;
  3240. }
  3241. else if (**args == '+')
  3242. (*args)++;
  3243. if (!ISDIGIT ((unsigned char)**args))
  3244. return 0;
  3245. while (ISDIGIT ((unsigned char)**args))
  3246. {
  3247. char_str[0] = **args;
  3248. string_append (arg, char_str);
  3249. (*args)++;
  3250. }
  3251. return 1;
  3252. }
  3253. /* Demangle the next argument, given by MANGLED into RESULT, which
  3254. *should be an uninitialized* string. It will be initialized here,
  3255. and free'd should anything go wrong. */
  3256. static int
  3257. do_arg (
  3258. work_stuff *work,
  3259. const char **mangled,
  3260. string *result)
  3261. {
  3262. /* Remember where we started so that we can record the type, for
  3263. non-squangling type remembering. */
  3264. const char *start = *mangled;
  3265. string temp_result;
  3266. string_init (result);
  3267. string_init (&temp_result);
  3268. if (work->nrepeats > 0)
  3269. {
  3270. --work->nrepeats;
  3271. if (work->previous_argument == 0)
  3272. return 0;
  3273. /* We want to reissue the previous type in this argument list. */
  3274. string_appends (result, work->previous_argument);
  3275. return 1;
  3276. }
  3277. if (**mangled == 'n')
  3278. {
  3279. /* A squangling-style repeat. */
  3280. (*mangled)++;
  3281. work->nrepeats = consume_count(mangled);
  3282. if (work->nrepeats <= 0)
  3283. /* This was not a repeat count after all. */
  3284. return 0;
  3285. if (work->nrepeats > 9)
  3286. {
  3287. if (**mangled != '_')
  3288. /* The repeat count should be followed by an '_' in this
  3289. case. */
  3290. return 0;
  3291. else
  3292. (*mangled)++;
  3293. }
  3294. /* Now, the repeat is all set up. */
  3295. return do_arg (work, mangled, result);
  3296. }
  3297. /* Save the result in WORK->previous_argument so that we can find it
  3298. if it's repeated. Note that saving START is not good enough: we
  3299. do not want to add additional types to the back-referenceable
  3300. type vector when processing a repeated type. */
  3301. if (work->previous_argument)
  3302. string_clear (work->previous_argument);
  3303. else
  3304. {
  3305. work->previous_argument = (string*) xmalloc (sizeof (string));
  3306. string_init (work->previous_argument);
  3307. }
  3308. if (!do_type (work, mangled, &temp_result))
  3309. {
  3310. string_delete (&temp_result);
  3311. return 0;
  3312. }
  3313. string_appends (work->previous_argument, &temp_result);
  3314. string_delete (&temp_result);
  3315. string_appends (result, work->previous_argument);
  3316. remember_type (work, start, *mangled - start);
  3317. return 1;
  3318. }
  3319. static void
  3320. remember_type (
  3321. work_stuff *work,
  3322. const char *start,
  3323. int len)
  3324. {
  3325. char *tem;
  3326. if (work->forgetting_types)
  3327. return;
  3328. if (work -> ntypes >= work -> typevec_size)
  3329. {
  3330. if (work -> typevec_size == 0)
  3331. {
  3332. work -> typevec_size = 3;
  3333. work -> typevec
  3334. = (char **) xmalloc (sizeof (char *) * work -> typevec_size);
  3335. }
  3336. else
  3337. {
  3338. work -> typevec_size *= 2;
  3339. work -> typevec
  3340. = (char **) xrealloc ((char *)work -> typevec,
  3341. sizeof (char *) * work -> typevec_size);
  3342. }
  3343. }
  3344. tem = (char *) xmalloc (len + 1);
  3345. memcpy (tem, start, len);
  3346. tem[len] = '\0';
  3347. work -> typevec[work -> ntypes++] = tem;
  3348. }
  3349. /* Remember a K type class qualifier. */
  3350. static void
  3351. remember_Ktype (
  3352. work_stuff *work,
  3353. const char *start,
  3354. int len)
  3355. {
  3356. char *tem;
  3357. if (work -> numk >= work -> ksize)
  3358. {
  3359. if (work -> ksize == 0)
  3360. {
  3361. work -> ksize = 5;
  3362. work -> ktypevec
  3363. = (char **) xmalloc (sizeof (char *) * work -> ksize);
  3364. }
  3365. else
  3366. {
  3367. work -> ksize *= 2;
  3368. work -> ktypevec
  3369. = (char **) xrealloc ((char *)work -> ktypevec,
  3370. sizeof (char *) * work -> ksize);
  3371. }
  3372. }
  3373. tem = (char *) xmalloc (len + 1);
  3374. memcpy (tem, start, len);
  3375. tem[len] = '\0';
  3376. work -> ktypevec[work -> numk++] = tem;
  3377. }
  3378. /* Register a B code, and get an index for it. B codes are registered
  3379. as they are seen, rather than as they are completed, so map<temp<char> >
  3380. registers map<temp<char> > as B0, and temp<char> as B1 */
  3381. static int
  3382. register_Btype (work_stuff *work)
  3383. {
  3384. int ret;
  3385. if (work -> numb >= work -> bsize)
  3386. {
  3387. if (work -> bsize == 0)
  3388. {
  3389. work -> bsize = 5;
  3390. work -> btypevec
  3391. = (char **) xmalloc (sizeof (char *) * work -> bsize);
  3392. }
  3393. else
  3394. {
  3395. work -> bsize *= 2;
  3396. work -> btypevec
  3397. = (char **) xrealloc ((char *)work -> btypevec,
  3398. sizeof (char *) * work -> bsize);
  3399. }
  3400. }
  3401. ret = work -> numb++;
  3402. work -> btypevec[ret] = NULL;
  3403. return(ret);
  3404. }
  3405. /* Store a value into a previously registered B code type. */
  3406. static void
  3407. remember_Btype (
  3408. work_stuff *work,
  3409. const char *start,
  3410. int len, int ind)
  3411. {
  3412. char *tem;
  3413. tem = (char *) xmalloc (len + 1);
  3414. memcpy (tem, start, len);
  3415. tem[len] = '\0';
  3416. work -> btypevec[ind] = tem;
  3417. }
  3418. /* Lose all the info related to B and K type codes. */
  3419. static void
  3420. forget_B_and_K_types (work_stuff *work)
  3421. {
  3422. int i;
  3423. while (work -> numk > 0)
  3424. {
  3425. i = --(work -> numk);
  3426. if (work -> ktypevec[i] != NULL)
  3427. {
  3428. free (work -> ktypevec[i]);
  3429. work -> ktypevec[i] = NULL;
  3430. }
  3431. }
  3432. while (work -> numb > 0)
  3433. {
  3434. i = --(work -> numb);
  3435. if (work -> btypevec[i] != NULL)
  3436. {
  3437. free (work -> btypevec[i]);
  3438. work -> btypevec[i] = NULL;
  3439. }
  3440. }
  3441. }
  3442. /* Forget the remembered types, but not the type vector itself. */
  3443. static void
  3444. forget_types (work_stuff *work)
  3445. {
  3446. int i;
  3447. while (work -> ntypes > 0)
  3448. {
  3449. i = --(work -> ntypes);
  3450. if (work -> typevec[i] != NULL)
  3451. {
  3452. free (work -> typevec[i]);
  3453. work -> typevec[i] = NULL;
  3454. }
  3455. }
  3456. }
  3457. /* Process the argument list part of the signature, after any class spec
  3458. has been consumed, as well as the first 'F' character (if any). For
  3459. example:
  3460. "__als__3fooRT0" => process "RT0"
  3461. "complexfunc5__FPFPc_PFl_i" => process "PFPc_PFl_i"
  3462. DECLP must be already initialised, usually non-empty. It won't be freed
  3463. on failure.
  3464. Note that g++ differs significantly from ARM and lucid style mangling
  3465. with regards to references to previously seen types. For example, given
  3466. the source fragment:
  3467. class foo {
  3468. public:
  3469. foo::foo (int, foo &ia, int, foo &ib, int, foo &ic);
  3470. };
  3471. foo::foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  3472. void foo (int, foo &ia, int, foo &ib, int, foo &ic) { ia = ib = ic; }
  3473. g++ produces the names:
  3474. __3fooiRT0iT2iT2
  3475. foo__FiR3fooiT1iT1
  3476. while lcc (and presumably other ARM style compilers as well) produces:
  3477. foo__FiR3fooT1T2T1T2
  3478. __ct__3fooFiR3fooT1T2T1T2
  3479. Note that g++ bases its type numbers starting at zero and counts all
  3480. previously seen types, while lucid/ARM bases its type numbers starting
  3481. at one and only considers types after it has seen the 'F' character
  3482. indicating the start of the function args. For lucid/ARM style, we
  3483. account for this difference by discarding any previously seen types when
  3484. we see the 'F' character, and subtracting one from the type number
  3485. reference.
  3486. */
  3487. static int
  3488. demangle_args (
  3489. work_stuff *work,
  3490. const char **mangled,
  3491. string *declp)
  3492. {
  3493. string arg;
  3494. int need_comma = 0;
  3495. int r;
  3496. int t;
  3497. const char *tem;
  3498. char temptype;
  3499. if (PRINT_ARG_TYPES)
  3500. {
  3501. string_append (declp, "(");
  3502. if (**mangled == '\0')
  3503. {
  3504. string_append (declp, "void");
  3505. }
  3506. }
  3507. while ((**mangled != '_' && **mangled != '\0' && **mangled != 'e')
  3508. || work->nrepeats > 0)
  3509. {
  3510. if ((**mangled == 'N') || (**mangled == 'T'))
  3511. {
  3512. temptype = *(*mangled)++;
  3513. if (temptype == 'N')
  3514. {
  3515. if (!get_count (mangled, &r))
  3516. {
  3517. return (0);
  3518. }
  3519. }
  3520. else
  3521. {
  3522. r = 1;
  3523. }
  3524. if ((HP_DEMANGLING || ARM_DEMANGLING || EDG_DEMANGLING) && work -> ntypes >= 10)
  3525. {
  3526. /* If we have 10 or more types we might have more than a 1 digit
  3527. index so we'll have to consume the whole count here. This
  3528. will lose if the next thing is a type name preceded by a
  3529. count but it's impossible to demangle that case properly
  3530. anyway. Eg if we already have 12 types is T12Pc "(..., type1,
  3531. Pc, ...)" or "(..., type12, char *, ...)" */
  3532. if ((t = consume_count(mangled)) <= 0)
  3533. {
  3534. return (0);
  3535. }
  3536. }
  3537. else
  3538. {
  3539. if (!get_count (mangled, &t))
  3540. {
  3541. return (0);
  3542. }
  3543. }
  3544. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  3545. {
  3546. t--;
  3547. }
  3548. /* Validate the type index. Protect against illegal indices from
  3549. malformed type strings. */
  3550. if ((t < 0) || (t >= work -> ntypes))
  3551. {
  3552. return (0);
  3553. }
  3554. while (work->nrepeats > 0 || --r >= 0)
  3555. {
  3556. tem = work -> typevec[t];
  3557. if (need_comma && PRINT_ARG_TYPES)
  3558. {
  3559. string_append (declp, ", ");
  3560. }
  3561. if (!do_arg (work, &tem, &arg))
  3562. {
  3563. return (0);
  3564. }
  3565. if (PRINT_ARG_TYPES)
  3566. {
  3567. string_appends (declp, &arg);
  3568. }
  3569. string_delete (&arg);
  3570. need_comma = 1;
  3571. }
  3572. }
  3573. else
  3574. {
  3575. if (need_comma && PRINT_ARG_TYPES)
  3576. string_append (declp, ", ");
  3577. if (!do_arg (work, mangled, &arg))
  3578. {
  3579. string_delete (&arg);
  3580. return (0);
  3581. }
  3582. if (PRINT_ARG_TYPES)
  3583. string_appends (declp, &arg);
  3584. string_delete (&arg);
  3585. need_comma = 1;
  3586. }
  3587. }
  3588. if (**mangled == 'e')
  3589. {
  3590. (*mangled)++;
  3591. if (PRINT_ARG_TYPES)
  3592. {
  3593. if (need_comma)
  3594. {
  3595. string_append (declp, ",");
  3596. }
  3597. string_append (declp, "...");
  3598. }
  3599. }
  3600. if (PRINT_ARG_TYPES)
  3601. {
  3602. string_append (declp, ")");
  3603. }
  3604. return (1);
  3605. }
  3606. /* Like demangle_args, but for demangling the argument lists of function
  3607. and method pointers or references, not top-level declarations. */
  3608. static int
  3609. demangle_nested_args (
  3610. work_stuff *work,
  3611. const char **mangled,
  3612. string *declp)
  3613. {
  3614. string* saved_previous_argument;
  3615. int result;
  3616. int saved_nrepeats;
  3617. /* The G++ name-mangling algorithm does not remember types on nested
  3618. argument lists, unless -fsquangling is used, and in that case the
  3619. type vector updated by remember_type is not used. So, we turn
  3620. off remembering of types here. */
  3621. ++work->forgetting_types;
  3622. /* For the repeat codes used with -fsquangling, we must keep track of
  3623. the last argument. */
  3624. saved_previous_argument = work->previous_argument;
  3625. saved_nrepeats = work->nrepeats;
  3626. work->previous_argument = 0;
  3627. work->nrepeats = 0;
  3628. /* Actually demangle the arguments. */
  3629. result = demangle_args (work, mangled, declp);
  3630. /* Restore the previous_argument field. */
  3631. if (work->previous_argument)
  3632. {
  3633. string_delete (work->previous_argument);
  3634. free ((char*) work->previous_argument);
  3635. }
  3636. work->previous_argument = saved_previous_argument;
  3637. --work->forgetting_types;
  3638. work->nrepeats = saved_nrepeats;
  3639. return result;
  3640. }
  3641. static void
  3642. demangle_function_name (
  3643. work_stuff *work,
  3644. const char **mangled,
  3645. string *declp,
  3646. const char *scan)
  3647. {
  3648. size_t i;
  3649. string type;
  3650. const char *tem;
  3651. string_appendn (declp, (*mangled), scan - (*mangled));
  3652. string_need (declp, 1);
  3653. *(declp -> p) = '\0';
  3654. /* Consume the function name, including the "__" separating the name
  3655. from the signature. We are guaranteed that SCAN points to the
  3656. separator. */
  3657. (*mangled) = scan + 2;
  3658. /* We may be looking at an instantiation of a template function:
  3659. foo__Xt1t2_Ft3t4, where t1, t2, ... are template arguments and a
  3660. following _F marks the start of the function arguments. Handle
  3661. the template arguments first. */
  3662. if (HP_DEMANGLING && (**mangled == 'X'))
  3663. {
  3664. demangle_arm_hp_template (work, mangled, 0, declp);
  3665. /* This leaves MANGLED pointing to the 'F' marking func args */
  3666. }
  3667. if (LUCID_DEMANGLING || ARM_DEMANGLING || HP_DEMANGLING || EDG_DEMANGLING)
  3668. {
  3669. /* See if we have an ARM style constructor or destructor operator.
  3670. If so, then just record it, clear the decl, and return.
  3671. We can't build the actual constructor/destructor decl until later,
  3672. when we recover the class name from the signature. */
  3673. if (strcmp (declp -> b, "__ct") == 0)
  3674. {
  3675. work -> constructor += 1;
  3676. string_clear (declp);
  3677. return;
  3678. }
  3679. else if (strcmp (declp -> b, "__dt") == 0)
  3680. {
  3681. work -> destructor += 1;
  3682. string_clear (declp);
  3683. return;
  3684. }
  3685. }
  3686. if (declp->p - declp->b >= 3
  3687. && declp->b[0] == 'o'
  3688. && declp->b[1] == 'p'
  3689. && strchr (cplus_markers, declp->b[2]) != NULL)
  3690. {
  3691. /* see if it's an assignment expression */
  3692. if (declp->p - declp->b >= 10 /* op$assign_ */
  3693. && memcmp (declp->b + 3, "assign_", 7) == 0)
  3694. {
  3695. for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
  3696. {
  3697. int len = declp->p - declp->b - 10;
  3698. if ((int) strlen (optable[i].in) == len
  3699. && memcmp (optable[i].in, declp->b + 10, len) == 0)
  3700. {
  3701. string_clear (declp);
  3702. string_append (declp, "operator");
  3703. string_append (declp, optable[i].out);
  3704. string_append (declp, "=");
  3705. break;
  3706. }
  3707. }
  3708. }
  3709. else
  3710. {
  3711. for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
  3712. {
  3713. int len = declp->p - declp->b - 3;
  3714. if ((int) strlen (optable[i].in) == len
  3715. && memcmp (optable[i].in, declp->b + 3, len) == 0)
  3716. {
  3717. string_clear (declp);
  3718. string_append (declp, "operator");
  3719. string_append (declp, optable[i].out);
  3720. break;
  3721. }
  3722. }
  3723. }
  3724. }
  3725. else if (declp->p - declp->b >= 5 && memcmp (declp->b, "type", 4) == 0
  3726. && strchr (cplus_markers, declp->b[4]) != NULL)
  3727. {
  3728. /* type conversion operator */
  3729. tem = declp->b + 5;
  3730. if (do_type (work, &tem, &type))
  3731. {
  3732. string_clear (declp);
  3733. string_append (declp, "operator ");
  3734. string_appends (declp, &type);
  3735. string_delete (&type);
  3736. }
  3737. }
  3738. else if (declp->b[0] == '_' && declp->b[1] == '_'
  3739. && declp->b[2] == 'o' && declp->b[3] == 'p')
  3740. {
  3741. /* ANSI. */
  3742. /* type conversion operator. */
  3743. tem = declp->b + 4;
  3744. if (do_type (work, &tem, &type))
  3745. {
  3746. string_clear (declp);
  3747. string_append (declp, "operator ");
  3748. string_appends (declp, &type);
  3749. string_delete (&type);
  3750. }
  3751. }
  3752. else if (declp->b[0] == '_' && declp->b[1] == '_'
  3753. && ISLOWER((unsigned char)declp->b[2])
  3754. && ISLOWER((unsigned char)declp->b[3]))
  3755. {
  3756. if (declp->b[4] == '\0')
  3757. {
  3758. /* Operator. */
  3759. for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
  3760. {
  3761. if (strlen (optable[i].in) == 2
  3762. && memcmp (optable[i].in, declp->b + 2, 2) == 0)
  3763. {
  3764. string_clear (declp);
  3765. string_append (declp, "operator");
  3766. string_append (declp, optable[i].out);
  3767. break;
  3768. }
  3769. }
  3770. }
  3771. else
  3772. {
  3773. if (declp->b[2] == 'a' && declp->b[5] == '\0')
  3774. {
  3775. /* Assignment. */
  3776. for (i = 0; i < (size_t)ARRAY_SIZE (optable); i++)
  3777. {
  3778. if (strlen (optable[i].in) == 3
  3779. && memcmp (optable[i].in, declp->b + 2, 3) == 0)
  3780. {
  3781. string_clear (declp);
  3782. string_append (declp, "operator");
  3783. string_append (declp, optable[i].out);
  3784. break;
  3785. }
  3786. }
  3787. }
  3788. }
  3789. }
  3790. }
  3791. /* a mini string-handling package */
  3792. static void
  3793. string_need (string *s, int n)
  3794. {
  3795. int tem;
  3796. if (s->b == NULL)
  3797. {
  3798. if (n < 32)
  3799. {
  3800. n = 32;
  3801. }
  3802. s->p = s->b = (char *) xmalloc (n);
  3803. s->e = s->b + n;
  3804. }
  3805. else if (s->e - s->p < n)
  3806. {
  3807. tem = s->p - s->b;
  3808. n += tem;
  3809. n *= 2;
  3810. s->b = (char *) xrealloc (s->b, n);
  3811. s->p = s->b + tem;
  3812. s->e = s->b + n;
  3813. }
  3814. }
  3815. static void
  3816. string_delete (string *s)
  3817. {
  3818. if (s->b != NULL)
  3819. {
  3820. free (s->b);
  3821. s->b = s->e = s->p = NULL;
  3822. }
  3823. }
  3824. static void
  3825. string_init (string *s)
  3826. {
  3827. s->b = s->p = s->e = NULL;
  3828. }
  3829. static void
  3830. string_clear (string *s)
  3831. {
  3832. s->p = s->b;
  3833. }
  3834. static void
  3835. string_append (string *p, const char *s)
  3836. {
  3837. int n;
  3838. if (s == NULL || *s == '\0')
  3839. return;
  3840. n = strlen (s);
  3841. string_need (p, n);
  3842. memcpy (p->p, s, n);
  3843. p->p += n;
  3844. }
  3845. static void
  3846. string_appends (string *p, string *s)
  3847. {
  3848. int n;
  3849. if (s->b != s->p)
  3850. {
  3851. n = s->p - s->b;
  3852. string_need (p, n);
  3853. memcpy (p->p, s->b, n);
  3854. p->p += n;
  3855. }
  3856. }
  3857. static void
  3858. string_appendn (string *p, const char *s, int n)
  3859. {
  3860. if (n != 0)
  3861. {
  3862. string_need (p, n);
  3863. memcpy (p->p, s, n);
  3864. p->p += n;
  3865. }
  3866. }
  3867. static void
  3868. string_prepend (string *p, const char *s)
  3869. {
  3870. if (s != NULL && *s != '\0')
  3871. {
  3872. string_prependn (p, s, strlen (s));
  3873. }
  3874. }
  3875. static void
  3876. string_prepends (string *p, string *s)
  3877. {
  3878. if (s->b != s->p)
  3879. {
  3880. string_prependn (p, s->b, s->p - s->b);
  3881. }
  3882. }
  3883. static void
  3884. string_prependn (string *p, const char *s, int n)
  3885. {
  3886. char *q;
  3887. if (n != 0)
  3888. {
  3889. string_need (p, n);
  3890. for (q = p->p - 1; q >= p->b; q--)
  3891. {
  3892. q[n] = q[0];
  3893. }
  3894. memcpy (p->b, s, n);
  3895. p->p += n;
  3896. }
  3897. }
  3898. static void
  3899. string_append_template_idx (string *s, int idx)
  3900. {
  3901. char buf[INTBUF_SIZE + 1 /* 'T' */];
  3902. sprintf(buf, "T%d", idx);
  3903. string_append (s, buf);
  3904. }
  3905. #endif
  3906. #ifdef STANDALONE_DEMANGLER
  3907. #include <stdio.h>
  3908. #include <stdlib.h>
  3909. int main(int argc, char *argv[])
  3910. {
  3911. for (int i = 1; i < argc; i++) {
  3912. const char *mangled_name = argv[i];
  3913. char *demangled_name = cxx_demangle(mangled_name, NULL, NULL, NULL);
  3914. if (demangled_name == NULL)
  3915. printf("Could not demangle string '%s'\n", mangled_name);
  3916. else {
  3917. printf("'%s'\n -> '%s'\n\n", mangled_name, demangled_name);
  3918. free(demangled_name);
  3919. }
  3920. }
  3921. }
  3922. #endif