d-demangle.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595
  1. /* Demangler for the D programming language
  2. Copyright 2014, 2015 Free Software Foundation, Inc.
  3. Written by Iain Buclaw (ibuclaw@gdcproject.org)
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. In addition to the permissions in the GNU Library General Public
  10. License, the Free Software Foundation gives you unlimited permission
  11. to link the compiled version of this file into combinations with other
  12. programs, and to distribute those combinations without any restriction
  13. coming from the use of this file. (The Library Public License
  14. restrictions do apply in other respects; for example, they cover
  15. modification of the file, and distribution when not linked into a
  16. combined executable.)
  17. Libiberty is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. Library General Public License for more details.
  21. You should have received a copy of the GNU Library General Public
  22. License along with libiberty; see the file COPYING.LIB.
  23. If not, see <http://www.gnu.org/licenses/>. */
  24. /* This file exports one function; dlang_demangle.
  25. This file imports strtol for decoding mangled literals. */
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include "safe-ctype.h"
  30. #include <sys/types.h>
  31. #include <string.h>
  32. #include <stdio.h>
  33. #ifdef HAVE_STDLIB_H
  34. #include <stdlib.h>
  35. #else
  36. extern long strtol (const char *nptr, char **endptr, int base);
  37. #endif
  38. #include <demangle.h>
  39. #include "libiberty.h"
  40. /* A mini string-handling package */
  41. typedef struct string /* Beware: these aren't required to be */
  42. { /* '\0' terminated. */
  43. char *b; /* pointer to start of string */
  44. char *p; /* pointer after last character */
  45. char *e; /* pointer after end of allocated space */
  46. } string;
  47. static void
  48. string_need (string *s, int n)
  49. {
  50. int tem;
  51. if (s->b == NULL)
  52. {
  53. if (n < 32)
  54. {
  55. n = 32;
  56. }
  57. s->p = s->b = XNEWVEC (char, n);
  58. s->e = s->b + n;
  59. }
  60. else if (s->e - s->p < n)
  61. {
  62. tem = s->p - s->b;
  63. n += tem;
  64. n *= 2;
  65. s->b = XRESIZEVEC (char, s->b, n);
  66. s->p = s->b + tem;
  67. s->e = s->b + n;
  68. }
  69. }
  70. static void
  71. string_delete (string *s)
  72. {
  73. if (s->b != NULL)
  74. {
  75. XDELETEVEC (s->b);
  76. s->b = s->e = s->p = NULL;
  77. }
  78. }
  79. static void
  80. string_init (string *s)
  81. {
  82. s->b = s->p = s->e = NULL;
  83. }
  84. static int
  85. string_length (string *s)
  86. {
  87. if (s->p == s->b)
  88. {
  89. return 0;
  90. }
  91. return s->p - s->b;
  92. }
  93. static void
  94. string_setlength (string *s, int n)
  95. {
  96. if (n - string_length (s) < 0)
  97. {
  98. s->p = s->b + n;
  99. }
  100. }
  101. static void
  102. string_append (string *p, const char *s)
  103. {
  104. int n = strlen (s);
  105. string_need (p, n);
  106. memcpy (p->p, s, n);
  107. p->p += n;
  108. }
  109. static void
  110. string_appendn (string *p, const char *s, int n)
  111. {
  112. if (n != 0)
  113. {
  114. string_need (p, n);
  115. memcpy (p->p, s, n);
  116. p->p += n;
  117. }
  118. }
  119. static void
  120. string_prependn (string *p, const char *s, int n)
  121. {
  122. char *q;
  123. if (n != 0)
  124. {
  125. string_need (p, n);
  126. for (q = p->p - 1; q >= p->b; q--)
  127. {
  128. q[n] = q[0];
  129. }
  130. memcpy (p->b, s, n);
  131. p->p += n;
  132. }
  133. }
  134. static void
  135. string_prepend (string *p, const char *s)
  136. {
  137. if (s != NULL && *s != '\0')
  138. {
  139. string_prependn (p, s, strlen (s));
  140. }
  141. }
  142. /* What kinds of symbol we could be parsing. */
  143. enum dlang_symbol_kinds
  144. {
  145. /* Top-level symbol, needs it's type checked. */
  146. dlang_top_level,
  147. /* Function symbol, needs it's type checked. */
  148. dlang_function,
  149. /* Strongly typed name, such as for classes, structs and enums. */
  150. dlang_type_name,
  151. /* Template identifier. */
  152. dlang_template_ident,
  153. /* Template symbol parameter. */
  154. dlang_template_param
  155. };
  156. /* Prototypes for forward referenced functions */
  157. static const char *dlang_function_args (string *, const char *);
  158. static const char *dlang_type (string *, const char *);
  159. static const char *dlang_value (string *, const char *, const char *, char);
  160. static const char *dlang_parse_symbol (string *, const char *,
  161. enum dlang_symbol_kinds);
  162. static const char *dlang_parse_tuple (string *, const char *);
  163. static const char *dlang_parse_template (string *, const char *, long);
  164. /* Demangle the calling convention from MANGLED and append it to DECL.
  165. Return the remaining string on success or NULL on failure. */
  166. static const char *
  167. dlang_call_convention (string *decl, const char *mangled)
  168. {
  169. if (mangled == NULL || *mangled == '\0')
  170. return NULL;
  171. switch (*mangled)
  172. {
  173. case 'F': /* (D) */
  174. mangled++;
  175. break;
  176. case 'U': /* (C) */
  177. mangled++;
  178. string_append (decl, "extern(C) ");
  179. break;
  180. case 'W': /* (Windows) */
  181. mangled++;
  182. string_append (decl, "extern(Windows) ");
  183. break;
  184. case 'V': /* (Pascal) */
  185. mangled++;
  186. string_append (decl, "extern(Pascal) ");
  187. break;
  188. case 'R': /* (C++) */
  189. mangled++;
  190. string_append (decl, "extern(C++) ");
  191. break;
  192. default:
  193. return NULL;
  194. }
  195. return mangled;
  196. }
  197. /* Extract the type modifiers from MANGLED and append them to DECL.
  198. Returns the remaining signature on success or NULL on failure. */
  199. static const char *
  200. dlang_type_modifiers (string *decl, const char *mangled)
  201. {
  202. if (mangled == NULL || *mangled == '\0')
  203. return NULL;
  204. switch (*mangled)
  205. {
  206. case 'x': /* const */
  207. mangled++;
  208. string_append (decl, " const");
  209. return mangled;
  210. case 'y': /* immutable */
  211. mangled++;
  212. string_append (decl, " immutable");
  213. return mangled;
  214. case 'O': /* shared */
  215. mangled++;
  216. string_append (decl, " shared");
  217. return dlang_type_modifiers (decl, mangled);
  218. case 'N':
  219. mangled++;
  220. if (*mangled == 'g') /* wild */
  221. {
  222. mangled++;
  223. string_append (decl, " inout");
  224. return dlang_type_modifiers (decl, mangled);
  225. }
  226. else
  227. return NULL;
  228. default:
  229. return mangled;
  230. }
  231. }
  232. /* Demangle the D function attributes from MANGLED and append it to DECL.
  233. Return the remaining string on success or NULL on failure. */
  234. static const char *
  235. dlang_attributes (string *decl, const char *mangled)
  236. {
  237. if (mangled == NULL || *mangled == '\0')
  238. return NULL;
  239. while (*mangled == 'N')
  240. {
  241. mangled++;
  242. switch (*mangled)
  243. {
  244. case 'a': /* pure */
  245. mangled++;
  246. string_append (decl, "pure ");
  247. continue;
  248. case 'b': /* nothrow */
  249. mangled++;
  250. string_append (decl, "nothrow ");
  251. continue;
  252. case 'c': /* ref */
  253. mangled++;
  254. string_append (decl, "ref ");
  255. continue;
  256. case 'd': /* @property */
  257. mangled++;
  258. string_append (decl, "@property ");
  259. continue;
  260. case 'e': /* @trusted */
  261. mangled++;
  262. string_append (decl, "@trusted ");
  263. continue;
  264. case 'f': /* @safe */
  265. mangled++;
  266. string_append (decl, "@safe ");
  267. continue;
  268. case 'g':
  269. case 'h':
  270. case 'k':
  271. /* inout parameter is represented as 'Ng'.
  272. vector parameter is represented as 'Nh'.
  273. return paramenter is represented as 'Nk'.
  274. If we see this, then we know we're really in the
  275. parameter list. Rewind and break. */
  276. mangled--;
  277. break;
  278. case 'i': /* @nogc */
  279. mangled++;
  280. string_append (decl, "@nogc ");
  281. continue;
  282. case 'j': /* return */
  283. mangled++;
  284. string_append (decl, "return ");
  285. continue;
  286. default: /* unknown attribute */
  287. return NULL;
  288. }
  289. break;
  290. }
  291. return mangled;
  292. }
  293. /* Demangle the function type from MANGLED and append it to DECL.
  294. Return the remaining string on success or NULL on failure. */
  295. static const char *
  296. dlang_function_type (string *decl, const char *mangled)
  297. {
  298. string attr, args, type;
  299. size_t szattr, szargs, sztype;
  300. if (mangled == NULL || *mangled == '\0')
  301. return NULL;
  302. /* The order of the mangled string is:
  303. CallConvention FuncAttrs Arguments ArgClose Type
  304. The demangled string is re-ordered as:
  305. CallConvention Type Arguments FuncAttrs
  306. */
  307. string_init (&attr);
  308. string_init (&args);
  309. string_init (&type);
  310. /* Function call convention. */
  311. mangled = dlang_call_convention (decl, mangled);
  312. /* Function attributes. */
  313. mangled = dlang_attributes (&attr, mangled);
  314. szattr = string_length (&attr);
  315. /* Function arguments. */
  316. mangled = dlang_function_args (&args, mangled);
  317. szargs = string_length (&args);
  318. /* Function return type. */
  319. mangled = dlang_type (&type, mangled);
  320. sztype = string_length (&type);
  321. /* Append to decl in order. */
  322. string_appendn (decl, type.b, sztype);
  323. string_append (decl, "(");
  324. string_appendn (decl, args.b, szargs);
  325. string_append (decl, ") ");
  326. string_appendn (decl, attr.b, szattr);
  327. string_delete (&attr);
  328. string_delete (&args);
  329. string_delete (&type);
  330. return mangled;
  331. }
  332. /* Demangle the argument list from MANGLED and append it to DECL.
  333. Return the remaining string on success or NULL on failure. */
  334. static const char *
  335. dlang_function_args (string *decl, const char *mangled)
  336. {
  337. size_t n = 0;
  338. while (mangled && *mangled != '\0')
  339. {
  340. switch (*mangled)
  341. {
  342. case 'X': /* (variadic T t...) style. */
  343. mangled++;
  344. string_append (decl, "...");
  345. return mangled;
  346. case 'Y': /* (variadic T t, ...) style. */
  347. mangled++;
  348. string_append (decl, ", ...");
  349. return mangled;
  350. case 'Z': /* Normal function. */
  351. mangled++;
  352. return mangled;
  353. }
  354. if (n++)
  355. string_append (decl, ", ");
  356. if (*mangled == 'M') /* scope(T) */
  357. {
  358. mangled++;
  359. string_append (decl, "scope ");
  360. }
  361. if (mangled[0] == 'N' && mangled[1] == 'k') /* return(T) */
  362. {
  363. mangled += 2;
  364. string_append (decl, "return ");
  365. }
  366. switch (*mangled)
  367. {
  368. case 'J': /* out(T) */
  369. mangled++;
  370. string_append (decl, "out ");
  371. break;
  372. case 'K': /* ref(T) */
  373. mangled++;
  374. string_append (decl, "ref ");
  375. break;
  376. case 'L': /* lazy(T) */
  377. mangled++;
  378. string_append (decl, "lazy ");
  379. break;
  380. }
  381. mangled = dlang_type (decl, mangled);
  382. }
  383. return mangled;
  384. }
  385. /* Demangle the type from MANGLED and append it to DECL.
  386. Return the remaining string on success or NULL on failure. */
  387. static const char *
  388. dlang_type (string *decl, const char *mangled)
  389. {
  390. if (mangled == NULL || *mangled == '\0')
  391. return NULL;
  392. switch (*mangled)
  393. {
  394. case 'O': /* shared(T) */
  395. mangled++;
  396. string_append (decl, "shared(");
  397. mangled = dlang_type (decl, mangled);
  398. string_append (decl, ")");
  399. return mangled;
  400. case 'x': /* const(T) */
  401. mangled++;
  402. string_append (decl, "const(");
  403. mangled = dlang_type (decl, mangled);
  404. string_append (decl, ")");
  405. return mangled;
  406. case 'y': /* immutable(T) */
  407. mangled++;
  408. string_append (decl, "immutable(");
  409. mangled = dlang_type (decl, mangled);
  410. string_append (decl, ")");
  411. return mangled;
  412. case 'N':
  413. mangled++;
  414. if (*mangled == 'g') /* wild(T) */
  415. {
  416. mangled++;
  417. string_append (decl, "inout(");
  418. mangled = dlang_type (decl, mangled);
  419. string_append (decl, ")");
  420. return mangled;
  421. }
  422. else if (*mangled == 'h') /* vector(T) */
  423. {
  424. mangled++;
  425. string_append (decl, "__vector(");
  426. mangled = dlang_type (decl, mangled);
  427. string_append (decl, ")");
  428. return mangled;
  429. }
  430. else
  431. return NULL;
  432. case 'A': /* dynamic array (T[]) */
  433. mangled++;
  434. mangled = dlang_type (decl, mangled);
  435. string_append (decl, "[]");
  436. return mangled;
  437. case 'G': /* static array (T[N]) */
  438. {
  439. const char *numptr;
  440. size_t num = 0;
  441. mangled++;
  442. numptr = mangled;
  443. while (ISDIGIT (*mangled))
  444. {
  445. num++;
  446. mangled++;
  447. }
  448. mangled = dlang_type (decl, mangled);
  449. string_append (decl, "[");
  450. string_appendn (decl, numptr, num);
  451. string_append (decl, "]");
  452. return mangled;
  453. }
  454. case 'H': /* associative array (T[T]) */
  455. {
  456. string type;
  457. size_t sztype;
  458. mangled++;
  459. string_init (&type);
  460. mangled = dlang_type (&type, mangled);
  461. sztype = string_length (&type);
  462. mangled = dlang_type (decl, mangled);
  463. string_append (decl, "[");
  464. string_appendn (decl, type.b, sztype);
  465. string_append (decl, "]");
  466. string_delete (&type);
  467. return mangled;
  468. }
  469. case 'P': /* pointer (T*) */
  470. mangled++;
  471. mangled = dlang_type (decl, mangled);
  472. string_append (decl, "*");
  473. return mangled;
  474. case 'I': /* ident T */
  475. case 'C': /* class T */
  476. case 'S': /* struct T */
  477. case 'E': /* enum T */
  478. case 'T': /* typedef T */
  479. mangled++;
  480. return dlang_parse_symbol (decl, mangled, dlang_type_name);
  481. case 'D': /* delegate T */
  482. {
  483. string mods;
  484. size_t szmods;
  485. mangled++;
  486. string_init (&mods);
  487. mangled = dlang_type_modifiers (&mods, mangled);
  488. szmods = string_length (&mods);
  489. mangled = dlang_function_type (decl, mangled);
  490. string_append (decl, "delegate");
  491. string_appendn (decl, mods.b, szmods);
  492. string_delete (&mods);
  493. return mangled;
  494. }
  495. case 'B': /* tuple T */
  496. mangled++;
  497. return dlang_parse_tuple (decl, mangled);
  498. /* Function types */
  499. case 'F': case 'U': case 'W':
  500. case 'V': case 'R':
  501. mangled = dlang_function_type (decl, mangled);
  502. string_append (decl, "function");
  503. return mangled;
  504. /* Basic types */
  505. case 'n':
  506. mangled++;
  507. string_append (decl, "none");
  508. return mangled;
  509. case 'v':
  510. mangled++;
  511. string_append (decl, "void");
  512. return mangled;
  513. case 'g':
  514. mangled++;
  515. string_append (decl, "byte");
  516. return mangled;
  517. case 'h':
  518. mangled++;
  519. string_append (decl, "ubyte");
  520. return mangled;
  521. case 's':
  522. mangled++;
  523. string_append (decl, "short");
  524. return mangled;
  525. case 't':
  526. mangled++;
  527. string_append (decl, "ushort");
  528. return mangled;
  529. case 'i':
  530. mangled++;
  531. string_append (decl, "int");
  532. return mangled;
  533. case 'k':
  534. mangled++;
  535. string_append (decl, "uint");
  536. return mangled;
  537. case 'l':
  538. mangled++;
  539. string_append (decl, "long");
  540. return mangled;
  541. case 'm':
  542. mangled++;
  543. string_append (decl, "ulong");
  544. return mangled;
  545. case 'f':
  546. mangled++;
  547. string_append (decl, "float");
  548. return mangled;
  549. case 'd':
  550. mangled++;
  551. string_append (decl, "double");
  552. return mangled;
  553. case 'e':
  554. mangled++;
  555. string_append (decl, "real");
  556. return mangled;
  557. /* Imaginary and Complex types */
  558. case 'o':
  559. mangled++;
  560. string_append (decl, "ifloat");
  561. return mangled;
  562. case 'p':
  563. mangled++;
  564. string_append (decl, "idouble");
  565. return mangled;
  566. case 'j':
  567. mangled++;
  568. string_append (decl, "ireal");
  569. return mangled;
  570. case 'q':
  571. mangled++;
  572. string_append (decl, "cfloat");
  573. return mangled;
  574. case 'r':
  575. mangled++;
  576. string_append (decl, "cdouble");
  577. return mangled;
  578. case 'c':
  579. mangled++;
  580. string_append (decl, "creal");
  581. return mangled;
  582. /* Other types */
  583. case 'b':
  584. mangled++;
  585. string_append (decl, "bool");
  586. return mangled;
  587. case 'a':
  588. mangled++;
  589. string_append (decl, "char");
  590. return mangled;
  591. case 'u':
  592. mangled++;
  593. string_append (decl, "wchar");
  594. return mangled;
  595. case 'w':
  596. mangled++;
  597. string_append (decl, "dchar");
  598. return mangled;
  599. case 'z':
  600. mangled++;
  601. switch (*mangled)
  602. {
  603. case 'i':
  604. mangled++;
  605. string_append (decl, "cent");
  606. return mangled;
  607. case 'k':
  608. mangled++;
  609. string_append (decl, "ucent");
  610. return mangled;
  611. }
  612. return NULL;
  613. default: /* unhandled */
  614. return NULL;
  615. }
  616. }
  617. /* Extract the identifier from MANGLED and append it to DECL.
  618. Return the remaining string on success or NULL on failure. */
  619. static const char *
  620. dlang_identifier (string *decl, const char *mangled,
  621. enum dlang_symbol_kinds kind)
  622. {
  623. char *endptr;
  624. long len;
  625. if (mangled == NULL || *mangled == '\0')
  626. return NULL;
  627. len = strtol (mangled, &endptr, 10);
  628. if (endptr == NULL || len <= 0)
  629. return NULL;
  630. /* In template parameter symbols, the first character of the mangled
  631. name can be a digit. This causes ambiguity issues because the
  632. digits of the two numbers are adjacent. */
  633. if (kind == dlang_template_param)
  634. {
  635. long psize = len;
  636. char *pend;
  637. int saved = string_length (decl);
  638. /* Work backwards until a match is found. */
  639. for (pend = endptr; endptr != NULL; pend--)
  640. {
  641. mangled = pend;
  642. /* Reached the beginning of the pointer to the name length,
  643. try parsing the entire symbol. */
  644. if (psize == 0)
  645. {
  646. psize = len;
  647. pend = endptr;
  648. endptr = NULL;
  649. }
  650. /* Check whether template parameter is a function with a valid
  651. return type or an untyped identifier. */
  652. if (ISDIGIT (*mangled))
  653. mangled = dlang_parse_symbol (decl, mangled, dlang_template_ident);
  654. else if (strncmp (mangled, "_D", 2) == 0)
  655. {
  656. mangled += 2;
  657. mangled = dlang_parse_symbol (decl, mangled, dlang_function);
  658. }
  659. /* Check for name length mismatch. */
  660. if (mangled && (mangled - pend) == psize)
  661. return mangled;
  662. psize /= 10;
  663. string_setlength (decl, saved);
  664. }
  665. /* No match on any combinations. */
  666. return NULL;
  667. }
  668. else
  669. {
  670. if (strlen (endptr) < (size_t) len)
  671. return NULL;
  672. mangled = endptr;
  673. /* May be a template instance. */
  674. if (len >= 5 && strncmp (mangled, "__T", 3) == 0)
  675. {
  676. /* Template symbol. */
  677. if (ISDIGIT (mangled[3]) && mangled[3] != '0')
  678. return dlang_parse_template (decl, mangled, len);
  679. return NULL;
  680. }
  681. switch (len)
  682. {
  683. case 6:
  684. if (strncmp (mangled, "__ctor", len) == 0)
  685. {
  686. /* Constructor symbol for a class/struct. */
  687. string_append (decl, "this");
  688. mangled += len;
  689. return mangled;
  690. }
  691. else if (strncmp (mangled, "__dtor", len) == 0)
  692. {
  693. /* Destructor symbol for a class/struct. */
  694. string_append (decl, "~this");
  695. mangled += len;
  696. return mangled;
  697. }
  698. else if (strncmp (mangled, "__initZ", len+1) == 0)
  699. {
  700. /* The static initialiser for a given symbol. */
  701. string_append (decl, "init$");
  702. mangled += len;
  703. return mangled;
  704. }
  705. else if (strncmp (mangled, "__vtblZ", len+1) == 0)
  706. {
  707. /* The vtable symbol for a given class. */
  708. string_prepend (decl, "vtable for ");
  709. string_setlength (decl, string_length (decl) - 1);
  710. mangled += len;
  711. return mangled;
  712. }
  713. break;
  714. case 7:
  715. if (strncmp (mangled, "__ClassZ", len+1) == 0)
  716. {
  717. /* The classinfo symbol for a given class. */
  718. string_prepend (decl, "ClassInfo for ");
  719. string_setlength (decl, string_length (decl) - 1);
  720. mangled += len;
  721. return mangled;
  722. }
  723. break;
  724. case 10:
  725. if (strncmp (mangled, "__postblitMFZ", len+3) == 0)
  726. {
  727. /* Postblit symbol for a struct. */
  728. string_append (decl, "this(this)");
  729. mangled += len + 3;
  730. return mangled;
  731. }
  732. break;
  733. case 11:
  734. if (strncmp (mangled, "__InterfaceZ", len+1) == 0)
  735. {
  736. /* The interface symbol for a given class. */
  737. string_prepend (decl, "Interface for ");
  738. string_setlength (decl, string_length (decl) - 1);
  739. mangled += len;
  740. return mangled;
  741. }
  742. break;
  743. case 12:
  744. if (strncmp (mangled, "__ModuleInfoZ", len+1) == 0)
  745. {
  746. /* The ModuleInfo symbol for a given module. */
  747. string_prepend (decl, "ModuleInfo for ");
  748. string_setlength (decl, string_length (decl) - 1);
  749. mangled += len;
  750. return mangled;
  751. }
  752. break;
  753. }
  754. string_appendn (decl, mangled, len);
  755. mangled += len;
  756. }
  757. return mangled;
  758. }
  759. /* Extract the integer value from MANGLED and append it to DECL,
  760. where TYPE is the type it should be represented as.
  761. Return the remaining string on success or NULL on failure. */
  762. static const char *
  763. dlang_parse_integer (string *decl, const char *mangled, char type)
  764. {
  765. if (type == 'a' || type == 'u' || type == 'w')
  766. {
  767. /* Parse character value. */
  768. char value[10];
  769. int pos = 10;
  770. int width = 0;
  771. char *endptr;
  772. long val = strtol (mangled, &endptr, 10);
  773. if (endptr == NULL || val < 0)
  774. return NULL;
  775. string_append (decl, "'");
  776. if (type == 'a' && val >= 0x20 && val < 0x7F)
  777. {
  778. /* Represent as a character literal. */
  779. char c = (char) val;
  780. string_appendn (decl, &c, 1);
  781. }
  782. else
  783. {
  784. /* Represent as a hexadecimal value. */
  785. switch (type)
  786. {
  787. case 'a': /* char */
  788. string_append (decl, "\\x");
  789. width = 2;
  790. break;
  791. case 'u': /* wchar */
  792. string_append (decl, "\\u");
  793. width = 4;
  794. break;
  795. case 'w': /* dchar */
  796. string_append (decl, "\\U");
  797. width = 8;
  798. break;
  799. }
  800. while (val > 0)
  801. {
  802. int digit = val % 16;
  803. if (digit < 10)
  804. value[--pos] = (char)(digit + '0');
  805. else
  806. value[--pos] = (char)((digit - 10) + 'a');
  807. val /= 16;
  808. width--;
  809. }
  810. for (; width > 0; width--)
  811. value[--pos] = '0';
  812. string_appendn (decl, &(value[pos]), 10 - pos);
  813. }
  814. string_append (decl, "'");
  815. mangled = endptr;
  816. }
  817. else if (type == 'b')
  818. {
  819. /* Parse boolean value. */
  820. char *endptr;
  821. long val = strtol (mangled, &endptr, 10);
  822. if (endptr == NULL || val < 0)
  823. return NULL;
  824. string_append (decl, val ? "true" : "false");
  825. mangled = endptr;
  826. }
  827. else
  828. {
  829. /* Parse integer value. */
  830. const char *numptr = mangled;
  831. size_t num = 0;
  832. while (ISDIGIT (*mangled))
  833. {
  834. num++;
  835. mangled++;
  836. }
  837. string_appendn (decl, numptr, num);
  838. /* Append suffix. */
  839. switch (type)
  840. {
  841. case 'h': /* ubyte */
  842. case 't': /* ushort */
  843. case 'k': /* uint */
  844. string_append (decl, "u");
  845. break;
  846. case 'l': /* long */
  847. string_append (decl, "L");
  848. break;
  849. case 'm': /* ulong */
  850. string_append (decl, "uL");
  851. break;
  852. }
  853. }
  854. return mangled;
  855. }
  856. /* Extract the floating-point value from MANGLED and append it to DECL.
  857. Return the remaining string on success or NULL on failure. */
  858. static const char *
  859. dlang_parse_real (string *decl, const char *mangled)
  860. {
  861. char buffer[64];
  862. int len = 0;
  863. /* Handle NAN and +-INF. */
  864. if (strncmp (mangled, "NAN", 3) == 0)
  865. {
  866. string_append (decl, "NaN");
  867. mangled += 3;
  868. return mangled;
  869. }
  870. else if (strncmp (mangled, "INF", 3) == 0)
  871. {
  872. string_append (decl, "Inf");
  873. mangled += 3;
  874. return mangled;
  875. }
  876. else if (strncmp (mangled, "NINF", 4) == 0)
  877. {
  878. string_append (decl, "-Inf");
  879. mangled += 4;
  880. return mangled;
  881. }
  882. /* Hexadecimal prefix and leading bit. */
  883. if (*mangled == 'N')
  884. {
  885. buffer[len++] = '-';
  886. mangled++;
  887. }
  888. if (!ISXDIGIT (*mangled))
  889. return NULL;
  890. buffer[len++] = '0';
  891. buffer[len++] = 'x';
  892. buffer[len++] = *mangled;
  893. buffer[len++] = '.';
  894. mangled++;
  895. /* Significand. */
  896. while (ISXDIGIT (*mangled))
  897. {
  898. buffer[len++] = *mangled;
  899. mangled++;
  900. }
  901. /* Exponent. */
  902. if (*mangled != 'P')
  903. return NULL;
  904. buffer[len++] = 'p';
  905. mangled++;
  906. if (*mangled == 'N')
  907. {
  908. buffer[len++] = '-';
  909. mangled++;
  910. }
  911. while (ISDIGIT (*mangled))
  912. {
  913. buffer[len++] = *mangled;
  914. mangled++;
  915. }
  916. /* Write out the demangled hexadecimal, rather than trying to
  917. convert the buffer into a floating-point value. */
  918. buffer[len] = '\0';
  919. len = strlen (buffer);
  920. string_appendn (decl, buffer, len);
  921. return mangled;
  922. }
  923. /* Convert VAL from an ascii hexdigit to value. */
  924. static char
  925. ascii2hex (char val)
  926. {
  927. if (val >= 'a' && val <= 'f')
  928. return (val - 'a' + 10);
  929. if (val >= 'A' && val <= 'F')
  930. return (val - 'A' + 10);
  931. if (val >= '0' && val <= '9')
  932. return (val - '0');
  933. return 0;
  934. }
  935. /* Extract the string value from MANGLED and append it to DECL.
  936. Return the remaining string on success or NULL on failure. */
  937. static const char *
  938. dlang_parse_string (string *decl, const char *mangled)
  939. {
  940. char type = *mangled;
  941. char *endptr;
  942. long len;
  943. mangled++;
  944. len = strtol (mangled, &endptr, 10);
  945. if (endptr == NULL || len < 0)
  946. return NULL;
  947. mangled = endptr;
  948. if (*mangled != '_')
  949. return NULL;
  950. mangled++;
  951. string_append (decl, "\"");
  952. while (len--)
  953. {
  954. if (ISXDIGIT (mangled[0]) && ISXDIGIT (mangled[1]))
  955. {
  956. char a = ascii2hex (mangled[0]);
  957. char b = ascii2hex (mangled[1]);
  958. char val = (a << 4) | b;
  959. /* Sanitize white and non-printable characters. */
  960. switch (val)
  961. {
  962. case ' ':
  963. string_append (decl, " ");
  964. break;
  965. case '\t':
  966. string_append (decl, "\\t");
  967. break;
  968. case '\n':
  969. string_append (decl, "\\n");
  970. break;
  971. case '\r':
  972. string_append (decl, "\\r");
  973. break;
  974. case '\f':
  975. string_append (decl, "\\f");
  976. break;
  977. case '\v':
  978. string_append (decl, "\\v");
  979. break;
  980. default:
  981. if (ISPRINT (val))
  982. string_appendn (decl, &val, 1);
  983. else
  984. {
  985. string_append (decl, "\\x");
  986. string_appendn (decl, mangled, 2);
  987. }
  988. }
  989. }
  990. else
  991. return NULL;
  992. mangled += 2;
  993. }
  994. string_append (decl, "\"");
  995. if (type != 'a')
  996. string_appendn (decl, &type, 1);
  997. return mangled;
  998. }
  999. /* Extract the static array value from MANGLED and append it to DECL.
  1000. Return the remaining string on success or NULL on failure. */
  1001. static const char *
  1002. dlang_parse_arrayliteral (string *decl, const char *mangled)
  1003. {
  1004. char *endptr;
  1005. long elements = strtol (mangled, &endptr, 10);
  1006. if (endptr == NULL || elements < 0)
  1007. return NULL;
  1008. mangled = endptr;
  1009. string_append (decl, "[");
  1010. while (elements--)
  1011. {
  1012. mangled = dlang_value (decl, mangled, NULL, '\0');
  1013. if (elements != 0)
  1014. string_append (decl, ", ");
  1015. }
  1016. string_append (decl, "]");
  1017. return mangled;
  1018. }
  1019. /* Extract the associative array value from MANGLED and append it to DECL.
  1020. Return the remaining string on success or NULL on failure. */
  1021. static const char *
  1022. dlang_parse_assocarray (string *decl, const char *mangled)
  1023. {
  1024. char *endptr;
  1025. long elements = strtol (mangled, &endptr, 10);
  1026. if (endptr == NULL || elements < 0)
  1027. return NULL;
  1028. mangled = endptr;
  1029. string_append (decl, "[");
  1030. while (elements--)
  1031. {
  1032. mangled = dlang_value (decl, mangled, NULL, '\0');
  1033. string_append (decl, ":");
  1034. mangled = dlang_value (decl, mangled, NULL, '\0');
  1035. if (elements != 0)
  1036. string_append (decl, ", ");
  1037. }
  1038. string_append (decl, "]");
  1039. return mangled;
  1040. }
  1041. /* Extract the struct literal value for NAME from MANGLED and append it to DECL.
  1042. Return the remaining string on success or NULL on failure. */
  1043. static const char *
  1044. dlang_parse_structlit (string *decl, const char *mangled, const char *name)
  1045. {
  1046. char *endptr;
  1047. long args = strtol (mangled, &endptr, 10);
  1048. if (endptr == NULL || args < 0)
  1049. return NULL;
  1050. mangled = endptr;
  1051. if (name != NULL)
  1052. string_append (decl, name);
  1053. string_append (decl, "(");
  1054. while (args--)
  1055. {
  1056. mangled = dlang_value (decl, mangled, NULL, '\0');
  1057. if (args != 0)
  1058. string_append (decl, ", ");
  1059. }
  1060. string_append (decl, ")");
  1061. return mangled;
  1062. }
  1063. /* Extract the value from MANGLED and append it to DECL.
  1064. Return the remaining string on success or NULL on failure. */
  1065. static const char *
  1066. dlang_value (string *decl, const char *mangled, const char *name, char type)
  1067. {
  1068. if (mangled == NULL || *mangled == '\0')
  1069. return NULL;
  1070. switch (*mangled)
  1071. {
  1072. /* Null value. */
  1073. case 'n':
  1074. mangled++;
  1075. string_append (decl, "null");
  1076. break;
  1077. /* Integral values. */
  1078. case 'N':
  1079. mangled++;
  1080. string_append (decl, "-");
  1081. mangled = dlang_parse_integer (decl, mangled, type);
  1082. break;
  1083. case 'i':
  1084. mangled++;
  1085. if (*mangled < '0' || *mangled > '9')
  1086. return NULL;
  1087. /* Fall through */
  1088. case '0': case '1': case '2': case '3': case '4':
  1089. case '5': case '6': case '7': case '8': case '9':
  1090. mangled = dlang_parse_integer (decl, mangled, type);
  1091. break;
  1092. /* Real value. */
  1093. case 'e':
  1094. mangled++;
  1095. mangled = dlang_parse_real (decl, mangled);
  1096. break;
  1097. /* Complex value. */
  1098. case 'c':
  1099. mangled++;
  1100. mangled = dlang_parse_real (decl, mangled);
  1101. string_append (decl, "+");
  1102. if (mangled == NULL || *mangled != 'c')
  1103. return NULL;
  1104. mangled++;
  1105. mangled = dlang_parse_real (decl, mangled);
  1106. string_append (decl, "i");
  1107. break;
  1108. /* String values. */
  1109. case 'a': /* UTF8 */
  1110. case 'w': /* UTF16 */
  1111. case 'd': /* UTF32 */
  1112. mangled = dlang_parse_string (decl, mangled);
  1113. break;
  1114. /* Array values. */
  1115. case 'A':
  1116. mangled++;
  1117. if (type == 'H')
  1118. mangled = dlang_parse_assocarray (decl, mangled);
  1119. else
  1120. mangled = dlang_parse_arrayliteral (decl, mangled);
  1121. break;
  1122. /* Struct values. */
  1123. case 'S':
  1124. mangled++;
  1125. mangled = dlang_parse_structlit (decl, mangled, name);
  1126. break;
  1127. default:
  1128. return NULL;
  1129. }
  1130. return mangled;
  1131. }
  1132. /* Extract the type modifiers from MANGLED and return the string
  1133. length that it consumes in MANGLED on success or 0 on failure. */
  1134. static int
  1135. dlang_type_modifier_p (const char *mangled)
  1136. {
  1137. int i;
  1138. switch (*mangled)
  1139. {
  1140. case 'x': case 'y':
  1141. return 1;
  1142. case 'O':
  1143. mangled++;
  1144. i = dlang_type_modifier_p (mangled);
  1145. return i + 1;
  1146. case 'N':
  1147. mangled++;
  1148. if (*mangled == 'g')
  1149. {
  1150. mangled++;
  1151. i = dlang_type_modifier_p (mangled);
  1152. return i + 2;
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. /* Extract the function calling convention from MANGLED and
  1158. return 1 on success or 0 on failure. */
  1159. static int
  1160. dlang_call_convention_p (const char *mangled)
  1161. {
  1162. /* Prefix for functions needing 'this' */
  1163. if (*mangled == 'M')
  1164. {
  1165. mangled++;
  1166. /* Also skip over any type modifiers. */
  1167. mangled += dlang_type_modifier_p (mangled);
  1168. }
  1169. switch (*mangled)
  1170. {
  1171. case 'F': case 'U': case 'V':
  1172. case 'W': case 'R':
  1173. return 1;
  1174. default:
  1175. return 0;
  1176. }
  1177. }
  1178. /* Extract and demangle the symbol in MANGLED and append it to DECL.
  1179. Returns the remaining signature on success or NULL on failure. */
  1180. static const char *
  1181. dlang_parse_symbol (string *decl, const char *mangled,
  1182. enum dlang_symbol_kinds kind)
  1183. {
  1184. int saved;
  1185. size_t n = 0;
  1186. do
  1187. {
  1188. if (n++)
  1189. string_append (decl, ".");
  1190. mangled = dlang_identifier (decl, mangled, kind);
  1191. if (mangled && dlang_call_convention_p (mangled))
  1192. {
  1193. string mods;
  1194. const char *start = NULL;
  1195. int checkpoint = 0;
  1196. /* Skip over 'this' parameter. */
  1197. if (*mangled == 'M')
  1198. mangled++;
  1199. /* We have reached here because we expect an extern(Pascal) function.
  1200. However this is so rare, that it is more likely a template value
  1201. parameter. Since this can't be assumed, first attempt parsing
  1202. the symbol as a function, and then back out on failure. */
  1203. if (*mangled == 'V')
  1204. {
  1205. start = mangled;
  1206. checkpoint = string_length (decl);
  1207. }
  1208. /* Save the type modifiers for appending at the end. */
  1209. string_init (&mods);
  1210. mangled = dlang_type_modifiers (&mods, mangled);
  1211. /* Skip over calling convention and attributes in qualified name. */
  1212. saved = string_length (decl);
  1213. mangled = dlang_call_convention (decl, mangled);
  1214. mangled = dlang_attributes (decl, mangled);
  1215. string_setlength (decl, saved);
  1216. string_append (decl, "(");
  1217. mangled = dlang_function_args (decl, mangled);
  1218. string_append (decl, ")");
  1219. /* Add any const/immutable/shared modifier. */
  1220. string_appendn (decl, mods.b, string_length (&mods));
  1221. string_delete (&mods);
  1222. if (mangled == NULL && checkpoint != 0)
  1223. {
  1224. mangled = start;
  1225. string_setlength (decl, checkpoint);
  1226. }
  1227. }
  1228. }
  1229. while (mangled && ISDIGIT (*mangled));
  1230. /* Only top-level symbols or function template parameters have
  1231. a type that needs checking. */
  1232. if (kind == dlang_top_level || kind == dlang_function)
  1233. {
  1234. /* Artificial symbols end with 'Z' and have no type. */
  1235. if (mangled && *mangled == 'Z')
  1236. mangled++;
  1237. else
  1238. {
  1239. saved = string_length (decl);
  1240. mangled = dlang_type (decl, mangled);
  1241. string_setlength (decl, saved);
  1242. }
  1243. /* Check that the entire symbol was successfully demangled. */
  1244. if (kind == dlang_top_level)
  1245. {
  1246. if (mangled == NULL || *mangled != '\0')
  1247. return NULL;
  1248. }
  1249. }
  1250. return mangled;
  1251. }
  1252. /* Demangle the tuple from MANGLED and append it to DECL.
  1253. Return the remaining string on success or NULL on failure. */
  1254. static const char *
  1255. dlang_parse_tuple (string *decl, const char *mangled)
  1256. {
  1257. char *endptr;
  1258. long elements = strtol (mangled, &endptr, 10);
  1259. if (endptr == NULL || elements < 0)
  1260. return NULL;
  1261. mangled = endptr;
  1262. string_append (decl, "Tuple!(");
  1263. while (elements--)
  1264. {
  1265. mangled = dlang_type (decl, mangled);
  1266. if (elements != 0)
  1267. string_append (decl, ", ");
  1268. }
  1269. string_append (decl, ")");
  1270. return mangled;
  1271. }
  1272. /* Demangle the argument list from MANGLED and append it to DECL.
  1273. Return the remaining string on success or NULL on failure. */
  1274. static const char *
  1275. dlang_template_args (string *decl, const char *mangled)
  1276. {
  1277. size_t n = 0;
  1278. while (mangled && *mangled != '\0')
  1279. {
  1280. switch (*mangled)
  1281. {
  1282. case 'Z': /* End of parameter list. */
  1283. mangled++;
  1284. return mangled;
  1285. }
  1286. if (n++)
  1287. string_append (decl, ", ");
  1288. /* Skip over specialised template prefix. */
  1289. if (*mangled == 'H')
  1290. mangled++;
  1291. switch (*mangled)
  1292. {
  1293. case 'S': /* Symbol parameter. */
  1294. mangled++;
  1295. mangled = dlang_parse_symbol (decl, mangled, dlang_template_param);
  1296. break;
  1297. case 'T': /* Type parameter. */
  1298. mangled++;
  1299. mangled = dlang_type (decl, mangled);
  1300. break;
  1301. case 'V': /* Value parameter. */
  1302. {
  1303. string name;
  1304. char type;
  1305. /* Peek at the type. */
  1306. mangled++;
  1307. type = *mangled;
  1308. /* In the few instances where the type is actually desired in
  1309. the output, it should precede the value from dlang_value. */
  1310. string_init (&name);
  1311. mangled = dlang_type (&name, mangled);
  1312. string_need (&name, 1);
  1313. *(name.p) = '\0';
  1314. mangled = dlang_value (decl, mangled, name.b, type);
  1315. string_delete (&name);
  1316. break;
  1317. }
  1318. default:
  1319. return NULL;
  1320. }
  1321. }
  1322. return mangled;
  1323. }
  1324. /* Extract and demangle the template symbol in MANGLED, expected to
  1325. be made up of LEN characters, and append it to DECL.
  1326. Returns the remaining signature on success or NULL on failure. */
  1327. static const char *
  1328. dlang_parse_template (string *decl, const char *mangled, long len)
  1329. {
  1330. const char *start = mangled;
  1331. /* Template instance names have the types and values of its parameters
  1332. encoded into it.
  1333. TemplateInstanceName:
  1334. Number __T LName TemplateArgs Z
  1335. ^
  1336. The start pointer should be at the above location, and LEN should be
  1337. the value of the decoded number.
  1338. */
  1339. if (strncmp (mangled, "__T", 3) != 0)
  1340. return NULL;
  1341. mangled += 3;
  1342. /* Template identifier. */
  1343. mangled = dlang_identifier (decl, mangled, dlang_template_ident);
  1344. /* Template arguments. */
  1345. string_append (decl, "!(");
  1346. mangled = dlang_template_args (decl, mangled);
  1347. string_append (decl, ")");
  1348. /* Check for template name length mismatch. */
  1349. if (mangled && (mangled - start) != len)
  1350. return NULL;
  1351. return mangled;
  1352. }
  1353. /* Extract and demangle the symbol in MANGLED. Returns the demangled
  1354. signature on success or NULL on failure. */
  1355. char *
  1356. dlang_demangle (const char *mangled, int option ATTRIBUTE_UNUSED)
  1357. {
  1358. string decl;
  1359. char *demangled = NULL;
  1360. if (mangled == NULL || *mangled == '\0')
  1361. return NULL;
  1362. if (strncmp (mangled, "_D", 2) != 0)
  1363. return NULL;
  1364. string_init (&decl);
  1365. if (strcmp (mangled, "_Dmain") == 0)
  1366. {
  1367. string_append (&decl, "D main");
  1368. }
  1369. else
  1370. {
  1371. mangled += 2;
  1372. if (dlang_parse_symbol (&decl, mangled, dlang_top_level) == NULL)
  1373. string_delete (&decl);
  1374. }
  1375. if (string_length (&decl) > 0)
  1376. {
  1377. string_need (&decl, 1);
  1378. *(decl.p) = '\0';
  1379. demangled = decl.b;
  1380. }
  1381. return demangled;
  1382. }