PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/breakpad/src/third_party/glog/src/demangle.cc

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