demangle.cc 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // Author: Satoru Takabayashi
  31. //
  32. // For reference check out:
  33. // http://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling
  34. //
  35. // Note that we only have partial C++0x support yet.
  36. #include <stdio.h> // for NULL
  37. #include "utilities.h"
  38. #include "demangle.h"
  39. #if defined(OS_WINDOWS)
  40. #include <dbghelp.h>
  41. #pragma comment(lib, "dbghelp")
  42. #endif
  43. _START_GOOGLE_NAMESPACE_
  44. #if !defined(OS_WINDOWS)
  45. typedef struct {
  46. const char *abbrev;
  47. const char *real_name;
  48. } AbbrevPair;
  49. // List of operators from Itanium C++ ABI.
  50. static const AbbrevPair kOperatorList[] = {
  51. { "nw", "new" },
  52. { "na", "new[]" },
  53. { "dl", "delete" },
  54. { "da", "delete[]" },
  55. { "ps", "+" },
  56. { "ng", "-" },
  57. { "ad", "&" },
  58. { "de", "*" },
  59. { "co", "~" },
  60. { "pl", "+" },
  61. { "mi", "-" },
  62. { "ml", "*" },
  63. { "dv", "/" },
  64. { "rm", "%" },
  65. { "an", "&" },
  66. { "or", "|" },
  67. { "eo", "^" },
  68. { "aS", "=" },
  69. { "pL", "+=" },
  70. { "mI", "-=" },
  71. { "mL", "*=" },
  72. { "dV", "/=" },
  73. { "rM", "%=" },
  74. { "aN", "&=" },
  75. { "oR", "|=" },
  76. { "eO", "^=" },
  77. { "ls", "<<" },
  78. { "rs", ">>" },
  79. { "lS", "<<=" },
  80. { "rS", ">>=" },
  81. { "eq", "==" },
  82. { "ne", "!=" },
  83. { "lt", "<" },
  84. { "gt", ">" },
  85. { "le", "<=" },
  86. { "ge", ">=" },
  87. { "nt", "!" },
  88. { "aa", "&&" },
  89. { "oo", "||" },
  90. { "pp", "++" },
  91. { "mm", "--" },
  92. { "cm", "," },
  93. { "pm", "->*" },
  94. { "pt", "->" },
  95. { "cl", "()" },
  96. { "ix", "[]" },
  97. { "qu", "?" },
  98. { "st", "sizeof" },
  99. { "sz", "sizeof" },
  100. { NULL, NULL },
  101. };
  102. // List of builtin types from Itanium C++ ABI.
  103. static const AbbrevPair kBuiltinTypeList[] = {
  104. { "v", "void" },
  105. { "w", "wchar_t" },
  106. { "b", "bool" },
  107. { "c", "char" },
  108. { "a", "signed char" },
  109. { "h", "unsigned char" },
  110. { "s", "short" },
  111. { "t", "unsigned short" },
  112. { "i", "int" },
  113. { "j", "unsigned int" },
  114. { "l", "long" },
  115. { "m", "unsigned long" },
  116. { "x", "long long" },
  117. { "y", "unsigned long long" },
  118. { "n", "__int128" },
  119. { "o", "unsigned __int128" },
  120. { "f", "float" },
  121. { "d", "double" },
  122. { "e", "long double" },
  123. { "g", "__float128" },
  124. { "z", "ellipsis" },
  125. { NULL, NULL }
  126. };
  127. // List of substitutions Itanium C++ ABI.
  128. static const AbbrevPair kSubstitutionList[] = {
  129. { "St", "" },
  130. { "Sa", "allocator" },
  131. { "Sb", "basic_string" },
  132. // std::basic_string<char, std::char_traits<char>,std::allocator<char> >
  133. { "Ss", "string"},
  134. // std::basic_istream<char, std::char_traits<char> >
  135. { "Si", "istream" },
  136. // std::basic_ostream<char, std::char_traits<char> >
  137. { "So", "ostream" },
  138. // std::basic_iostream<char, std::char_traits<char> >
  139. { "Sd", "iostream" },
  140. { NULL, NULL }
  141. };
  142. // State needed for demangling.
  143. typedef struct {
  144. const char *mangled_cur; // Cursor of mangled name.
  145. char *out_cur; // Cursor of output string.
  146. const char *out_begin; // Beginning of output string.
  147. const char *out_end; // End of output string.
  148. const char *prev_name; // For constructors/destructors.
  149. int prev_name_length; // For constructors/destructors.
  150. short nest_level; // For nested names.
  151. bool append; // Append flag.
  152. bool overflowed; // True if output gets overflowed.
  153. } State;
  154. // We don't use strlen() in libc since it's not guaranteed to be async
  155. // signal safe.
  156. static size_t StrLen(const char *str) {
  157. size_t len = 0;
  158. while (*str != '\0') {
  159. ++str;
  160. ++len;
  161. }
  162. return len;
  163. }
  164. // Returns true if "str" has at least "n" characters remaining.
  165. static bool AtLeastNumCharsRemaining(const char *str, int n) {
  166. for (int i = 0; i < n; ++i) {
  167. if (str[i] == '\0') {
  168. return false;
  169. }
  170. }
  171. return true;
  172. }
  173. // Returns true if "str" has "prefix" as a prefix.
  174. static bool StrPrefix(const char *str, const char *prefix) {
  175. size_t i = 0;
  176. while (str[i] != '\0' && prefix[i] != '\0' &&
  177. str[i] == prefix[i]) {
  178. ++i;
  179. }
  180. return prefix[i] == '\0'; // Consumed everything in "prefix".
  181. }
  182. static void InitState(State *state, const char *mangled,
  183. char *out, int out_size) {
  184. state->mangled_cur = mangled;
  185. state->out_cur = out;
  186. state->out_begin = out;
  187. state->out_end = out + out_size;
  188. state->prev_name = NULL;
  189. state->prev_name_length = -1;
  190. state->nest_level = -1;
  191. state->append = true;
  192. state->overflowed = false;
  193. }
  194. // Returns true and advances "mangled_cur" if we find "one_char_token"
  195. // at "mangled_cur" position. It is assumed that "one_char_token" does
  196. // not contain '\0'.
  197. static bool ParseOneCharToken(State *state, const char one_char_token) {
  198. if (state->mangled_cur[0] == one_char_token) {
  199. ++state->mangled_cur;
  200. return true;
  201. }
  202. return false;
  203. }
  204. // Returns true and advances "mangled_cur" if we find "two_char_token"
  205. // at "mangled_cur" position. It is assumed that "two_char_token" does
  206. // not contain '\0'.
  207. static bool ParseTwoCharToken(State *state, const char *two_char_token) {
  208. if (state->mangled_cur[0] == two_char_token[0] &&
  209. state->mangled_cur[1] == two_char_token[1]) {
  210. state->mangled_cur += 2;
  211. return true;
  212. }
  213. return false;
  214. }
  215. // Returns true and advances "mangled_cur" if we find any character in
  216. // "char_class" at "mangled_cur" position.
  217. static bool ParseCharClass(State *state, const char *char_class) {
  218. const char *p = char_class;
  219. for (; *p != '\0'; ++p) {
  220. if (state->mangled_cur[0] == *p) {
  221. ++state->mangled_cur;
  222. return true;
  223. }
  224. }
  225. return false;
  226. }
  227. // This function is used for handling an optional non-terminal.
  228. static bool Optional(bool) {
  229. return true;
  230. }
  231. // This function is used for handling <non-terminal>+ syntax.
  232. typedef bool (*ParseFunc)(State *);
  233. static bool OneOrMore(ParseFunc parse_func, State *state) {
  234. if (parse_func(state)) {
  235. while (parse_func(state)) {
  236. }
  237. return true;
  238. }
  239. return false;
  240. }
  241. // This function is used for handling <non-terminal>* syntax. The function
  242. // always returns true and must be followed by a termination token or a
  243. // terminating sequence not handled by parse_func (e.g.
  244. // ParseOneCharToken(state, 'E')).
  245. static bool ZeroOrMore(ParseFunc parse_func, State *state) {
  246. while (parse_func(state)) {
  247. }
  248. return true;
  249. }
  250. // Append "str" at "out_cur". If there is an overflow, "overflowed"
  251. // is set to true for later use. The output string is ensured to
  252. // always terminate with '\0' as long as there is no overflow.
  253. static void Append(State *state, const char * const str, const int length) {
  254. int i;
  255. for (i = 0; i < length; ++i) {
  256. if (state->out_cur + 1 < state->out_end) { // +1 for '\0'
  257. *state->out_cur = str[i];
  258. ++state->out_cur;
  259. } else {
  260. state->overflowed = true;
  261. break;
  262. }
  263. }
  264. if (!state->overflowed) {
  265. *state->out_cur = '\0'; // Terminate it with '\0'
  266. }
  267. }
  268. // We don't use equivalents in libc to avoid locale issues.
  269. static bool IsLower(char c) {
  270. return c >= 'a' && c <= 'z';
  271. }
  272. static bool IsAlpha(char c) {
  273. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  274. }
  275. static bool IsDigit(char c) {
  276. return c >= '0' && c <= '9';
  277. }
  278. // Returns true if "str" is a function clone suffix. These suffixes are used
  279. // by GCC 4.5.x and later versions to indicate functions which have been
  280. // cloned during optimization. We treat any sequence (.<alpha>+.<digit>+)+ as
  281. // a function clone suffix.
  282. static bool IsFunctionCloneSuffix(const char *str) {
  283. size_t i = 0;
  284. while (str[i] != '\0') {
  285. // Consume a single .<alpha>+.<digit>+ sequence.
  286. if (str[i] != '.' || !IsAlpha(str[i + 1])) {
  287. return false;
  288. }
  289. i += 2;
  290. while (IsAlpha(str[i])) {
  291. ++i;
  292. }
  293. if (str[i] != '.' || !IsDigit(str[i + 1])) {
  294. return false;
  295. }
  296. i += 2;
  297. while (IsDigit(str[i])) {
  298. ++i;
  299. }
  300. }
  301. return true; // Consumed everything in "str".
  302. }
  303. // Append "str" with some tweaks, iff "append" state is true.
  304. // Returns true so that it can be placed in "if" conditions.
  305. static void MaybeAppendWithLength(State *state, const char * const str,
  306. const int length) {
  307. if (state->append && length > 0) {
  308. // Append a space if the output buffer ends with '<' and "str"
  309. // starts with '<' to avoid <<<.
  310. if (str[0] == '<' && state->out_begin < state->out_cur &&
  311. state->out_cur[-1] == '<') {
  312. Append(state, " ", 1);
  313. }
  314. // Remember the last identifier name for ctors/dtors.
  315. if (IsAlpha(str[0]) || str[0] == '_') {
  316. state->prev_name = state->out_cur;
  317. state->prev_name_length = length;
  318. }
  319. Append(state, str, length);
  320. }
  321. }
  322. // A convenient wrapper arount MaybeAppendWithLength().
  323. static bool MaybeAppend(State *state, const char * const str) {
  324. if (state->append) {
  325. int length = StrLen(str);
  326. MaybeAppendWithLength(state, str, length);
  327. }
  328. return true;
  329. }
  330. // This function is used for handling nested names.
  331. static bool EnterNestedName(State *state) {
  332. state->nest_level = 0;
  333. return true;
  334. }
  335. // This function is used for handling nested names.
  336. static bool LeaveNestedName(State *state, short prev_value) {
  337. state->nest_level = prev_value;
  338. return true;
  339. }
  340. // Disable the append mode not to print function parameters, etc.
  341. static bool DisableAppend(State *state) {
  342. state->append = false;
  343. return true;
  344. }
  345. // Restore the append mode to the previous state.
  346. static bool RestoreAppend(State *state, bool prev_value) {
  347. state->append = prev_value;
  348. return true;
  349. }
  350. // Increase the nest level for nested names.
  351. static void MaybeIncreaseNestLevel(State *state) {
  352. if (state->nest_level > -1) {
  353. ++state->nest_level;
  354. }
  355. }
  356. // Appends :: for nested names if necessary.
  357. static void MaybeAppendSeparator(State *state) {
  358. if (state->nest_level >= 1) {
  359. MaybeAppend(state, "::");
  360. }
  361. }
  362. // Cancel the last separator if necessary.
  363. static void MaybeCancelLastSeparator(State *state) {
  364. if (state->nest_level >= 1 && state->append &&
  365. state->out_begin <= state->out_cur - 2) {
  366. state->out_cur -= 2;
  367. *state->out_cur = '\0';
  368. }
  369. }
  370. // Returns true if the identifier of the given length pointed to by
  371. // "mangled_cur" is anonymous namespace.
  372. static bool IdentifierIsAnonymousNamespace(State *state, int length) {
  373. static const char anon_prefix[] = "_GLOBAL__N_";
  374. return (length > (int)sizeof(anon_prefix) - 1 && // Should be longer.
  375. StrPrefix(state->mangled_cur, anon_prefix));
  376. }
  377. // Forward declarations of our parsing functions.
  378. static bool ParseMangledName(State *state);
  379. static bool ParseEncoding(State *state);
  380. static bool ParseName(State *state);
  381. static bool ParseUnscopedName(State *state);
  382. static bool ParseUnscopedTemplateName(State *state);
  383. static bool ParseNestedName(State *state);
  384. static bool ParsePrefix(State *state);
  385. static bool ParseUnqualifiedName(State *state);
  386. static bool ParseSourceName(State *state);
  387. static bool ParseLocalSourceName(State *state);
  388. static bool ParseNumber(State *state, int *number_out);
  389. static bool ParseFloatNumber(State *state);
  390. static bool ParseSeqId(State *state);
  391. static bool ParseIdentifier(State *state, int length);
  392. static bool ParseAbiTags(State *state);
  393. static bool ParseAbiTag(State *state);
  394. static bool ParseOperatorName(State *state);
  395. static bool ParseSpecialName(State *state);
  396. static bool ParseCallOffset(State *state);
  397. static bool ParseNVOffset(State *state);
  398. static bool ParseVOffset(State *state);
  399. static bool ParseCtorDtorName(State *state);
  400. static bool ParseType(State *state);
  401. static bool ParseCVQualifiers(State *state);
  402. static bool ParseBuiltinType(State *state);
  403. static bool ParseFunctionType(State *state);
  404. static bool ParseBareFunctionType(State *state);
  405. static bool ParseClassEnumType(State *state);
  406. static bool ParseArrayType(State *state);
  407. static bool ParsePointerToMemberType(State *state);
  408. static bool ParseTemplateParam(State *state);
  409. static bool ParseTemplateTemplateParam(State *state);
  410. static bool ParseTemplateArgs(State *state);
  411. static bool ParseTemplateArg(State *state);
  412. static bool ParseExpression(State *state);
  413. static bool ParseExprPrimary(State *state);
  414. static bool ParseLocalName(State *state);
  415. static bool ParseDiscriminator(State *state);
  416. static bool ParseSubstitution(State *state);
  417. // Implementation note: the following code is a straightforward
  418. // translation of the Itanium C++ ABI defined in BNF with a couple of
  419. // exceptions.
  420. //
  421. // - Support GNU extensions not defined in the Itanium C++ ABI
  422. // - <prefix> and <template-prefix> are combined to avoid infinite loop
  423. // - Reorder patterns to shorten the code
  424. // - Reorder patterns to give greedier functions precedence
  425. // We'll mark "Less greedy than" for these cases in the code
  426. //
  427. // Each parsing function changes the state and returns true on
  428. // success. Otherwise, don't change the state and returns false. To
  429. // ensure that the state isn't changed in the latter case, we save the
  430. // original state before we call more than one parsing functions
  431. // consecutively with &&, and restore the state if unsuccessful. See
  432. // ParseEncoding() as an example of this convention. We follow the
  433. // convention throughout the code.
  434. //
  435. // Originally we tried to do demangling without following the full ABI
  436. // syntax but it turned out we needed to follow the full syntax to
  437. // parse complicated cases like nested template arguments. Note that
  438. // implementing a full-fledged demangler isn't trivial (libiberty's
  439. // cp-demangle.c has +4300 lines).
  440. //
  441. // Note that (foo) in <(foo) ...> is a modifier to be ignored.
  442. //
  443. // Reference:
  444. // - Itanium C++ ABI
  445. // <http://www.codesourcery.com/cxx-abi/abi.html#mangling>
  446. // <mangled-name> ::= _Z <encoding>
  447. static bool ParseMangledName(State *state) {
  448. return ParseTwoCharToken(state, "_Z") && ParseEncoding(state);
  449. }
  450. // <encoding> ::= <(function) name> <bare-function-type>
  451. // ::= <(data) name>
  452. // ::= <special-name>
  453. static bool ParseEncoding(State *state) {
  454. State copy = *state;
  455. if (ParseName(state) && ParseBareFunctionType(state)) {
  456. return true;
  457. }
  458. *state = copy;
  459. if (ParseName(state) || ParseSpecialName(state)) {
  460. return true;
  461. }
  462. return false;
  463. }
  464. // <name> ::= <nested-name>
  465. // ::= <unscoped-template-name> <template-args>
  466. // ::= <unscoped-name>
  467. // ::= <local-name>
  468. static bool ParseName(State *state) {
  469. if (ParseNestedName(state) || ParseLocalName(state)) {
  470. return true;
  471. }
  472. State copy = *state;
  473. if (ParseUnscopedTemplateName(state) &&
  474. ParseTemplateArgs(state)) {
  475. return true;
  476. }
  477. *state = copy;
  478. // Less greedy than <unscoped-template-name> <template-args>.
  479. if (ParseUnscopedName(state)) {
  480. return true;
  481. }
  482. return false;
  483. }
  484. // <unscoped-name> ::= <unqualified-name>
  485. // ::= St <unqualified-name>
  486. static bool ParseUnscopedName(State *state) {
  487. if (ParseUnqualifiedName(state)) {
  488. return true;
  489. }
  490. State copy = *state;
  491. if (ParseTwoCharToken(state, "St") &&
  492. MaybeAppend(state, "std::") &&
  493. ParseUnqualifiedName(state)) {
  494. return true;
  495. }
  496. *state = copy;
  497. return false;
  498. }
  499. // <unscoped-template-name> ::= <unscoped-name>
  500. // ::= <substitution>
  501. static bool ParseUnscopedTemplateName(State *state) {
  502. return ParseUnscopedName(state) || ParseSubstitution(state);
  503. }
  504. // <nested-name> ::= N [<CV-qualifiers>] <prefix> <unqualified-name> E
  505. // ::= N [<CV-qualifiers>] <template-prefix> <template-args> E
  506. static bool ParseNestedName(State *state) {
  507. State copy = *state;
  508. if (ParseOneCharToken(state, 'N') &&
  509. EnterNestedName(state) &&
  510. Optional(ParseCVQualifiers(state)) &&
  511. ParsePrefix(state) &&
  512. LeaveNestedName(state, copy.nest_level) &&
  513. ParseOneCharToken(state, 'E')) {
  514. return true;
  515. }
  516. *state = copy;
  517. return false;
  518. }
  519. // This part is tricky. If we literally translate them to code, we'll
  520. // end up infinite loop. Hence we merge them to avoid the case.
  521. //
  522. // <prefix> ::= <prefix> <unqualified-name>
  523. // ::= <template-prefix> <template-args>
  524. // ::= <template-param>
  525. // ::= <substitution>
  526. // ::= # empty
  527. // <template-prefix> ::= <prefix> <(template) unqualified-name>
  528. // ::= <template-param>
  529. // ::= <substitution>
  530. static bool ParsePrefix(State *state) {
  531. bool has_something = false;
  532. while (true) {
  533. MaybeAppendSeparator(state);
  534. if (ParseTemplateParam(state) ||
  535. ParseSubstitution(state) ||
  536. ParseUnscopedName(state)) {
  537. has_something = true;
  538. MaybeIncreaseNestLevel(state);
  539. continue;
  540. }
  541. MaybeCancelLastSeparator(state);
  542. if (has_something && ParseTemplateArgs(state)) {
  543. return ParsePrefix(state);
  544. } else {
  545. break;
  546. }
  547. }
  548. return true;
  549. }
  550. // <unqualified-name> ::= <operator-name>
  551. // ::= <ctor-dtor-name>
  552. // ::= <source-name> [<abi-tags>]
  553. // ::= <local-source-name> [<abi-tags>]
  554. static bool ParseUnqualifiedName(State *state) {
  555. return (ParseOperatorName(state) ||
  556. ParseCtorDtorName(state) ||
  557. (ParseSourceName(state) && Optional(ParseAbiTags(state))) ||
  558. (ParseLocalSourceName(state) && Optional(ParseAbiTags(state))));
  559. }
  560. // <source-name> ::= <positive length number> <identifier>
  561. static bool ParseSourceName(State *state) {
  562. State copy = *state;
  563. int length = -1;
  564. if (ParseNumber(state, &length) && ParseIdentifier(state, length)) {
  565. return true;
  566. }
  567. *state = copy;
  568. return false;
  569. }
  570. // <local-source-name> ::= L <source-name> [<discriminator>]
  571. //
  572. // References:
  573. // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775
  574. // http://gcc.gnu.org/viewcvs?view=rev&revision=124467
  575. static bool ParseLocalSourceName(State *state) {
  576. State copy = *state;
  577. if (ParseOneCharToken(state, 'L') && ParseSourceName(state) &&
  578. Optional(ParseDiscriminator(state))) {
  579. return true;
  580. }
  581. *state = copy;
  582. return false;
  583. }
  584. // <number> ::= [n] <non-negative decimal integer>
  585. // If "number_out" is non-null, then *number_out is set to the value of the
  586. // parsed number on success.
  587. static bool ParseNumber(State *state, int *number_out) {
  588. int sign = 1;
  589. if (ParseOneCharToken(state, 'n')) {
  590. sign = -1;
  591. }
  592. const char *p = state->mangled_cur;
  593. int number = 0;
  594. for (;*p != '\0'; ++p) {
  595. if (IsDigit(*p)) {
  596. number = number * 10 + (*p - '0');
  597. } else {
  598. break;
  599. }
  600. }
  601. if (p != state->mangled_cur) { // Conversion succeeded.
  602. state->mangled_cur = p;
  603. if (number_out != NULL) {
  604. *number_out = number * sign;
  605. }
  606. return true;
  607. }
  608. return false;
  609. }
  610. // Floating-point literals are encoded using a fixed-length lowercase
  611. // hexadecimal string.
  612. static bool ParseFloatNumber(State *state) {
  613. const char *p = state->mangled_cur;
  614. for (;*p != '\0'; ++p) {
  615. if (!IsDigit(*p) && !(*p >= 'a' && *p <= 'f')) {
  616. break;
  617. }
  618. }
  619. if (p != state->mangled_cur) { // Conversion succeeded.
  620. state->mangled_cur = p;
  621. return true;
  622. }
  623. return false;
  624. }
  625. // The <seq-id> is a sequence number in base 36,
  626. // using digits and upper case letters
  627. static bool ParseSeqId(State *state) {
  628. const char *p = state->mangled_cur;
  629. for (;*p != '\0'; ++p) {
  630. if (!IsDigit(*p) && !(*p >= 'A' && *p <= 'Z')) {
  631. break;
  632. }
  633. }
  634. if (p != state->mangled_cur) { // Conversion succeeded.
  635. state->mangled_cur = p;
  636. return true;
  637. }
  638. return false;
  639. }
  640. // <identifier> ::= <unqualified source code identifier> (of given length)
  641. static bool ParseIdentifier(State *state, int length) {
  642. if (length == -1 ||
  643. !AtLeastNumCharsRemaining(state->mangled_cur, length)) {
  644. return false;
  645. }
  646. if (IdentifierIsAnonymousNamespace(state, length)) {
  647. MaybeAppend(state, "(anonymous namespace)");
  648. } else {
  649. MaybeAppendWithLength(state, state->mangled_cur, length);
  650. }
  651. state->mangled_cur += length;
  652. return true;
  653. }
  654. // <abi-tags> ::= <abi-tag> [<abi-tags>]
  655. static bool ParseAbiTags(State *state) {
  656. State copy = *state;
  657. DisableAppend(state);
  658. if (OneOrMore(ParseAbiTag, state)) {
  659. RestoreAppend(state, copy.append);
  660. return true;
  661. }
  662. *state = copy;
  663. return false;
  664. }
  665. // <abi-tag> ::= B <source-name>
  666. static bool ParseAbiTag(State *state) {
  667. return ParseOneCharToken(state, 'B') && ParseSourceName(state);
  668. }
  669. // <operator-name> ::= nw, and other two letters cases
  670. // ::= cv <type> # (cast)
  671. // ::= v <digit> <source-name> # vendor extended operator
  672. static bool ParseOperatorName(State *state) {
  673. if (!AtLeastNumCharsRemaining(state->mangled_cur, 2)) {
  674. return false;
  675. }
  676. // First check with "cv" (cast) case.
  677. State copy = *state;
  678. if (ParseTwoCharToken(state, "cv") &&
  679. MaybeAppend(state, "operator ") &&
  680. EnterNestedName(state) &&
  681. ParseType(state) &&
  682. LeaveNestedName(state, copy.nest_level)) {
  683. return true;
  684. }
  685. *state = copy;
  686. // Then vendor extended operators.
  687. if (ParseOneCharToken(state, 'v') && ParseCharClass(state, "0123456789") &&
  688. ParseSourceName(state)) {
  689. return true;
  690. }
  691. *state = copy;
  692. // Other operator names should start with a lower alphabet followed
  693. // by a lower/upper alphabet.
  694. if (!(IsLower(state->mangled_cur[0]) &&
  695. IsAlpha(state->mangled_cur[1]))) {
  696. return false;
  697. }
  698. // We may want to perform a binary search if we really need speed.
  699. const AbbrevPair *p;
  700. for (p = kOperatorList; p->abbrev != NULL; ++p) {
  701. if (state->mangled_cur[0] == p->abbrev[0] &&
  702. state->mangled_cur[1] == p->abbrev[1]) {
  703. MaybeAppend(state, "operator");
  704. if (IsLower(*p->real_name)) { // new, delete, etc.
  705. MaybeAppend(state, " ");
  706. }
  707. MaybeAppend(state, p->real_name);
  708. state->mangled_cur += 2;
  709. return true;
  710. }
  711. }
  712. return false;
  713. }
  714. // <special-name> ::= TV <type>
  715. // ::= TT <type>
  716. // ::= TI <type>
  717. // ::= TS <type>
  718. // ::= Tc <call-offset> <call-offset> <(base) encoding>
  719. // ::= GV <(object) name>
  720. // ::= T <call-offset> <(base) encoding>
  721. // G++ extensions:
  722. // ::= TC <type> <(offset) number> _ <(base) type>
  723. // ::= TF <type>
  724. // ::= TJ <type>
  725. // ::= GR <name>
  726. // ::= GA <encoding>
  727. // ::= Th <call-offset> <(base) encoding>
  728. // ::= Tv <call-offset> <(base) encoding>
  729. //
  730. // Note: we don't care much about them since they don't appear in
  731. // stack traces. The are special data.
  732. static bool ParseSpecialName(State *state) {
  733. State copy = *state;
  734. if (ParseOneCharToken(state, 'T') &&
  735. ParseCharClass(state, "VTIS") &&
  736. ParseType(state)) {
  737. return true;
  738. }
  739. *state = copy;
  740. if (ParseTwoCharToken(state, "Tc") && ParseCallOffset(state) &&
  741. ParseCallOffset(state) && ParseEncoding(state)) {
  742. return true;
  743. }
  744. *state = copy;
  745. if (ParseTwoCharToken(state, "GV") &&
  746. ParseName(state)) {
  747. return true;
  748. }
  749. *state = copy;
  750. if (ParseOneCharToken(state, 'T') && ParseCallOffset(state) &&
  751. ParseEncoding(state)) {
  752. return true;
  753. }
  754. *state = copy;
  755. // G++ extensions
  756. if (ParseTwoCharToken(state, "TC") && ParseType(state) &&
  757. ParseNumber(state, NULL) && ParseOneCharToken(state, '_') &&
  758. DisableAppend(state) &&
  759. ParseType(state)) {
  760. RestoreAppend(state, copy.append);
  761. return true;
  762. }
  763. *state = copy;
  764. if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "FJ") &&
  765. ParseType(state)) {
  766. return true;
  767. }
  768. *state = copy;
  769. if (ParseTwoCharToken(state, "GR") && ParseName(state)) {
  770. return true;
  771. }
  772. *state = copy;
  773. if (ParseTwoCharToken(state, "GA") && ParseEncoding(state)) {
  774. return true;
  775. }
  776. *state = copy;
  777. if (ParseOneCharToken(state, 'T') && ParseCharClass(state, "hv") &&
  778. ParseCallOffset(state) && ParseEncoding(state)) {
  779. return true;
  780. }
  781. *state = copy;
  782. return false;
  783. }
  784. // <call-offset> ::= h <nv-offset> _
  785. // ::= v <v-offset> _
  786. static bool ParseCallOffset(State *state) {
  787. State copy = *state;
  788. if (ParseOneCharToken(state, 'h') &&
  789. ParseNVOffset(state) && ParseOneCharToken(state, '_')) {
  790. return true;
  791. }
  792. *state = copy;
  793. if (ParseOneCharToken(state, 'v') &&
  794. ParseVOffset(state) && ParseOneCharToken(state, '_')) {
  795. return true;
  796. }
  797. *state = copy;
  798. return false;
  799. }
  800. // <nv-offset> ::= <(offset) number>
  801. static bool ParseNVOffset(State *state) {
  802. return ParseNumber(state, NULL);
  803. }
  804. // <v-offset> ::= <(offset) number> _ <(virtual offset) number>
  805. static bool ParseVOffset(State *state) {
  806. State copy = *state;
  807. if (ParseNumber(state, NULL) && ParseOneCharToken(state, '_') &&
  808. ParseNumber(state, NULL)) {
  809. return true;
  810. }
  811. *state = copy;
  812. return false;
  813. }
  814. // <ctor-dtor-name> ::= C1 | C2 | C3
  815. // ::= D0 | D1 | D2
  816. static bool ParseCtorDtorName(State *state) {
  817. State copy = *state;
  818. if (ParseOneCharToken(state, 'C') &&
  819. ParseCharClass(state, "123")) {
  820. const char * const prev_name = state->prev_name;
  821. const int prev_name_length = state->prev_name_length;
  822. MaybeAppendWithLength(state, prev_name, prev_name_length);
  823. return true;
  824. }
  825. *state = copy;
  826. if (ParseOneCharToken(state, 'D') &&
  827. ParseCharClass(state, "012")) {
  828. const char * const prev_name = state->prev_name;
  829. const int prev_name_length = state->prev_name_length;
  830. MaybeAppend(state, "~");
  831. MaybeAppendWithLength(state, prev_name, prev_name_length);
  832. return true;
  833. }
  834. *state = copy;
  835. return false;
  836. }
  837. // <type> ::= <CV-qualifiers> <type>
  838. // ::= P <type> # pointer-to
  839. // ::= R <type> # reference-to
  840. // ::= O <type> # rvalue reference-to (C++0x)
  841. // ::= C <type> # complex pair (C 2000)
  842. // ::= G <type> # imaginary (C 2000)
  843. // ::= U <source-name> <type> # vendor extended type qualifier
  844. // ::= <builtin-type>
  845. // ::= <function-type>
  846. // ::= <class-enum-type>
  847. // ::= <array-type>
  848. // ::= <pointer-to-member-type>
  849. // ::= <template-template-param> <template-args>
  850. // ::= <template-param>
  851. // ::= <substitution>
  852. // ::= Dp <type> # pack expansion of (C++0x)
  853. // ::= Dt <expression> E # decltype of an id-expression or class
  854. // # member access (C++0x)
  855. // ::= DT <expression> E # decltype of an expression (C++0x)
  856. //
  857. static bool ParseType(State *state) {
  858. // We should check CV-qualifers, and PRGC things first.
  859. State copy = *state;
  860. if (ParseCVQualifiers(state) && ParseType(state)) {
  861. return true;
  862. }
  863. *state = copy;
  864. if (ParseCharClass(state, "OPRCG") && ParseType(state)) {
  865. return true;
  866. }
  867. *state = copy;
  868. if (ParseTwoCharToken(state, "Dp") && ParseType(state)) {
  869. return true;
  870. }
  871. *state = copy;
  872. if (ParseOneCharToken(state, 'D') && ParseCharClass(state, "tT") &&
  873. ParseExpression(state) && ParseOneCharToken(state, 'E')) {
  874. return true;
  875. }
  876. *state = copy;
  877. if (ParseOneCharToken(state, 'U') && ParseSourceName(state) &&
  878. ParseType(state)) {
  879. return true;
  880. }
  881. *state = copy;
  882. if (ParseBuiltinType(state) ||
  883. ParseFunctionType(state) ||
  884. ParseClassEnumType(state) ||
  885. ParseArrayType(state) ||
  886. ParsePointerToMemberType(state) ||
  887. ParseSubstitution(state)) {
  888. return true;
  889. }
  890. if (ParseTemplateTemplateParam(state) &&
  891. ParseTemplateArgs(state)) {
  892. return true;
  893. }
  894. *state = copy;
  895. // Less greedy than <template-template-param> <template-args>.
  896. if (ParseTemplateParam(state)) {
  897. return true;
  898. }
  899. return false;
  900. }
  901. // <CV-qualifiers> ::= [r] [V] [K]
  902. // We don't allow empty <CV-qualifiers> to avoid infinite loop in
  903. // ParseType().
  904. static bool ParseCVQualifiers(State *state) {
  905. int num_cv_qualifiers = 0;
  906. num_cv_qualifiers += ParseOneCharToken(state, 'r');
  907. num_cv_qualifiers += ParseOneCharToken(state, 'V');
  908. num_cv_qualifiers += ParseOneCharToken(state, 'K');
  909. return num_cv_qualifiers > 0;
  910. }
  911. // <builtin-type> ::= v, etc.
  912. // ::= u <source-name>
  913. static bool ParseBuiltinType(State *state) {
  914. const AbbrevPair *p;
  915. for (p = kBuiltinTypeList; p->abbrev != NULL; ++p) {
  916. if (state->mangled_cur[0] == p->abbrev[0]) {
  917. MaybeAppend(state, p->real_name);
  918. ++state->mangled_cur;
  919. return true;
  920. }
  921. }
  922. State copy = *state;
  923. if (ParseOneCharToken(state, 'u') && ParseSourceName(state)) {
  924. return true;
  925. }
  926. *state = copy;
  927. return false;
  928. }
  929. // <function-type> ::= F [Y] <bare-function-type> E
  930. static bool ParseFunctionType(State *state) {
  931. State copy = *state;
  932. if (ParseOneCharToken(state, 'F') &&
  933. Optional(ParseOneCharToken(state, 'Y')) &&
  934. ParseBareFunctionType(state) && ParseOneCharToken(state, 'E')) {
  935. return true;
  936. }
  937. *state = copy;
  938. return false;
  939. }
  940. // <bare-function-type> ::= <(signature) type>+
  941. static bool ParseBareFunctionType(State *state) {
  942. State copy = *state;
  943. DisableAppend(state);
  944. if (OneOrMore(ParseType, state)) {
  945. RestoreAppend(state, copy.append);
  946. MaybeAppend(state, "()");
  947. return true;
  948. }
  949. *state = copy;
  950. return false;
  951. }
  952. // <class-enum-type> ::= <name>
  953. static bool ParseClassEnumType(State *state) {
  954. return ParseName(state);
  955. }
  956. // <array-type> ::= A <(positive dimension) number> _ <(element) type>
  957. // ::= A [<(dimension) expression>] _ <(element) type>
  958. static bool ParseArrayType(State *state) {
  959. State copy = *state;
  960. if (ParseOneCharToken(state, 'A') && ParseNumber(state, NULL) &&
  961. ParseOneCharToken(state, '_') && ParseType(state)) {
  962. return true;
  963. }
  964. *state = copy;
  965. if (ParseOneCharToken(state, 'A') && Optional(ParseExpression(state)) &&
  966. ParseOneCharToken(state, '_') && ParseType(state)) {
  967. return true;
  968. }
  969. *state = copy;
  970. return false;
  971. }
  972. // <pointer-to-member-type> ::= M <(class) type> <(member) type>
  973. static bool ParsePointerToMemberType(State *state) {
  974. State copy = *state;
  975. if (ParseOneCharToken(state, 'M') && ParseType(state) &&
  976. ParseType(state)) {
  977. return true;
  978. }
  979. *state = copy;
  980. return false;
  981. }
  982. // <template-param> ::= T_
  983. // ::= T <parameter-2 non-negative number> _
  984. static bool ParseTemplateParam(State *state) {
  985. if (ParseTwoCharToken(state, "T_")) {
  986. MaybeAppend(state, "?"); // We don't support template substitutions.
  987. return true;
  988. }
  989. State copy = *state;
  990. if (ParseOneCharToken(state, 'T') && ParseNumber(state, NULL) &&
  991. ParseOneCharToken(state, '_')) {
  992. MaybeAppend(state, "?"); // We don't support template substitutions.
  993. return true;
  994. }
  995. *state = copy;
  996. return false;
  997. }
  998. // <template-template-param> ::= <template-param>
  999. // ::= <substitution>
  1000. static bool ParseTemplateTemplateParam(State *state) {
  1001. return (ParseTemplateParam(state) ||
  1002. ParseSubstitution(state));
  1003. }
  1004. // <template-args> ::= I <template-arg>+ E
  1005. static bool ParseTemplateArgs(State *state) {
  1006. State copy = *state;
  1007. DisableAppend(state);
  1008. if (ParseOneCharToken(state, 'I') &&
  1009. OneOrMore(ParseTemplateArg, state) &&
  1010. ParseOneCharToken(state, 'E')) {
  1011. RestoreAppend(state, copy.append);
  1012. MaybeAppend(state, "<>");
  1013. return true;
  1014. }
  1015. *state = copy;
  1016. return false;
  1017. }
  1018. // <template-arg> ::= <type>
  1019. // ::= <expr-primary>
  1020. // ::= I <template-arg>* E # argument pack
  1021. // ::= J <template-arg>* E # argument pack
  1022. // ::= X <expression> E
  1023. static bool ParseTemplateArg(State *state) {
  1024. State copy = *state;
  1025. if ((ParseOneCharToken(state, 'I') || ParseOneCharToken(state, 'J')) &&
  1026. ZeroOrMore(ParseTemplateArg, state) &&
  1027. ParseOneCharToken(state, 'E')) {
  1028. return true;
  1029. }
  1030. *state = copy;
  1031. if (ParseType(state) ||
  1032. ParseExprPrimary(state)) {
  1033. return true;
  1034. }
  1035. *state = copy;
  1036. if (ParseOneCharToken(state, 'X') && ParseExpression(state) &&
  1037. ParseOneCharToken(state, 'E')) {
  1038. return true;
  1039. }
  1040. *state = copy;
  1041. return false;
  1042. }
  1043. // <expression> ::= <template-param>
  1044. // ::= <expr-primary>
  1045. // ::= <unary operator-name> <expression>
  1046. // ::= <binary operator-name> <expression> <expression>
  1047. // ::= <trinary operator-name> <expression> <expression>
  1048. // <expression>
  1049. // ::= st <type>
  1050. // ::= sr <type> <unqualified-name> <template-args>
  1051. // ::= sr <type> <unqualified-name>
  1052. static bool ParseExpression(State *state) {
  1053. if (ParseTemplateParam(state) || ParseExprPrimary(state)) {
  1054. return true;
  1055. }
  1056. State copy = *state;
  1057. if (ParseOperatorName(state) &&
  1058. ParseExpression(state) &&
  1059. ParseExpression(state) &&
  1060. ParseExpression(state)) {
  1061. return true;
  1062. }
  1063. *state = copy;
  1064. if (ParseOperatorName(state) &&
  1065. ParseExpression(state) &&
  1066. ParseExpression(state)) {
  1067. return true;
  1068. }
  1069. *state = copy;
  1070. if (ParseOperatorName(state) &&
  1071. ParseExpression(state)) {
  1072. return true;
  1073. }
  1074. *state = copy;
  1075. if (ParseTwoCharToken(state, "st") && ParseType(state)) {
  1076. return true;
  1077. }
  1078. *state = copy;
  1079. if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
  1080. ParseUnqualifiedName(state) &&
  1081. ParseTemplateArgs(state)) {
  1082. return true;
  1083. }
  1084. *state = copy;
  1085. if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
  1086. ParseUnqualifiedName(state)) {
  1087. return true;
  1088. }
  1089. *state = copy;
  1090. return false;
  1091. }
  1092. // <expr-primary> ::= L <type> <(value) number> E
  1093. // ::= L <type> <(value) float> E
  1094. // ::= L <mangled-name> E
  1095. // // A bug in g++'s C++ ABI version 2 (-fabi-version=2).
  1096. // ::= LZ <encoding> E
  1097. static bool ParseExprPrimary(State *state) {
  1098. State copy = *state;
  1099. if (ParseOneCharToken(state, 'L') && ParseType(state) &&
  1100. ParseNumber(state, NULL) &&
  1101. ParseOneCharToken(state, 'E')) {
  1102. return true;
  1103. }
  1104. *state = copy;
  1105. if (ParseOneCharToken(state, 'L') && ParseType(state) &&
  1106. ParseFloatNumber(state) &&
  1107. ParseOneCharToken(state, 'E')) {
  1108. return true;
  1109. }
  1110. *state = copy;
  1111. if (ParseOneCharToken(state, 'L') && ParseMangledName(state) &&
  1112. ParseOneCharToken(state, 'E')) {
  1113. return true;
  1114. }
  1115. *state = copy;
  1116. if (ParseTwoCharToken(state, "LZ") && ParseEncoding(state) &&
  1117. ParseOneCharToken(state, 'E')) {
  1118. return true;
  1119. }
  1120. *state = copy;
  1121. return false;
  1122. }
  1123. // <local-name> := Z <(function) encoding> E <(entity) name>
  1124. // [<discriminator>]
  1125. // := Z <(function) encoding> E s [<discriminator>]
  1126. static bool ParseLocalName(State *state) {
  1127. State copy = *state;
  1128. if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
  1129. ParseOneCharToken(state, 'E') && MaybeAppend(state, "::") &&
  1130. ParseName(state) && Optional(ParseDiscriminator(state))) {
  1131. return true;
  1132. }
  1133. *state = copy;
  1134. if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
  1135. ParseTwoCharToken(state, "Es") && Optional(ParseDiscriminator(state))) {
  1136. return true;
  1137. }
  1138. *state = copy;
  1139. return false;
  1140. }
  1141. // <discriminator> := _ <(non-negative) number>
  1142. static bool ParseDiscriminator(State *state) {
  1143. State copy = *state;
  1144. if (ParseOneCharToken(state, '_') && ParseNumber(state, NULL)) {
  1145. return true;
  1146. }
  1147. *state = copy;
  1148. return false;
  1149. }
  1150. // <substitution> ::= S_
  1151. // ::= S <seq-id> _
  1152. // ::= St, etc.
  1153. static bool ParseSubstitution(State *state) {
  1154. if (ParseTwoCharToken(state, "S_")) {
  1155. MaybeAppend(state, "?"); // We don't support substitutions.
  1156. return true;
  1157. }
  1158. State copy = *state;
  1159. if (ParseOneCharToken(state, 'S') && ParseSeqId(state) &&
  1160. ParseOneCharToken(state, '_')) {
  1161. MaybeAppend(state, "?"); // We don't support substitutions.
  1162. return true;
  1163. }
  1164. *state = copy;
  1165. // Expand abbreviations like "St" => "std".
  1166. if (ParseOneCharToken(state, 'S')) {
  1167. const AbbrevPair *p;
  1168. for (p = kSubstitutionList; p->abbrev != NULL; ++p) {
  1169. if (state->mangled_cur[0] == p->abbrev[1]) {
  1170. MaybeAppend(state, "std");
  1171. if (p->real_name[0] != '\0') {
  1172. MaybeAppend(state, "::");
  1173. MaybeAppend(state, p->real_name);
  1174. }
  1175. ++state->mangled_cur;
  1176. return true;
  1177. }
  1178. }
  1179. }
  1180. *state = copy;
  1181. return false;
  1182. }
  1183. // Parse <mangled-name>, optionally followed by either a function-clone suffix
  1184. // or version suffix. Returns true only if all of "mangled_cur" was consumed.
  1185. static bool ParseTopLevelMangledName(State *state) {
  1186. if (ParseMangledName(state)) {
  1187. if (state->mangled_cur[0] != '\0') {
  1188. // Drop trailing function clone suffix, if any.
  1189. if (IsFunctionCloneSuffix(state->mangled_cur)) {
  1190. return true;
  1191. }
  1192. // Append trailing version suffix if any.
  1193. // ex. _Z3foo@@GLIBCXX_3.4
  1194. if (state->mangled_cur[0] == '@') {
  1195. MaybeAppend(state, state->mangled_cur);
  1196. return true;
  1197. }
  1198. return false; // Unconsumed suffix.
  1199. }
  1200. return true;
  1201. }
  1202. return false;
  1203. }
  1204. #endif
  1205. // The demangler entry point.
  1206. bool Demangle(const char *mangled, char *out, int out_size) {
  1207. #if defined(OS_WINDOWS)
  1208. // When built with incremental linking, the Windows debugger
  1209. // library provides a more complicated `Symbol->Name` with the
  1210. // Incremental Linking Table offset, which looks like
  1211. // `@ILT+1105(?func@Foo@@SAXH@Z)`. However, the demangler expects
  1212. // only the mangled symbol, `?func@Foo@@SAXH@Z`. Fortunately, the
  1213. // mangled symbol is guaranteed not to have parentheses,
  1214. // so we search for `(` and extract up to `)`.
  1215. //
  1216. // Since we may be in a signal handler here, we cannot use `std::string`.
  1217. char buffer[1024]; // Big enough for a sane symbol.
  1218. const char *lparen = strchr(mangled, '(');
  1219. if (lparen) {
  1220. // Extract the string `(?...)`
  1221. const char *rparen = strchr(lparen, ')');
  1222. size_t length = rparen - lparen - 1;
  1223. strncpy(buffer, lparen + 1, length);
  1224. buffer[length] = '\0';
  1225. mangled = buffer;
  1226. } // Else the symbol wasn't inside a set of parentheses
  1227. // We use the ANSI version to ensure the string type is always `char *`.
  1228. return UnDecorateSymbolName(mangled, out, out_size, UNDNAME_COMPLETE);
  1229. #else
  1230. State state;
  1231. InitState(&state, mangled, out, out_size);
  1232. return ParseTopLevelMangledName(&state) && !state.overflowed;
  1233. #endif
  1234. }
  1235. _END_GOOGLE_NAMESPACE_