// Copyright 2018 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // For reference check out: // https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling #include "absl/debugging/internal/demangle.h" #include <cstddef> #include <cstdint> #include <cstdio> #include <cstdlib> #include <cstring> #include <limits> #include <string> #include "absl/base/config.h" #include "absl/debugging/internal/demangle_rust.h" #if ABSL_INTERNAL_HAS_CXA_DEMANGLE #include <cxxabi.h> #endif namespace absl { ABSL_NAMESPACE_BEGIN namespace debugging_internal { AbbrevPair; // List of operators from Itanium C++ ABI. static const AbbrevPair kOperatorList[] = …; // List of builtin types from Itanium C++ ABI. // // Invariant: only one- or two-character type abbreviations here. static const AbbrevPair kBuiltinTypeList[] = …; // List of substitutions Itanium C++ ABI. static const AbbrevPair kSubstitutionList[] = …; // State needed for demangling. This struct is copied in almost every stack // frame, so every byte counts. ParseState; static_assert …; // One-off state for demangling that's not subject to backtracking -- either // constant data, data that's intentionally immune to backtracking (steps), or // data that would never be changed by backtracking anyway (recursion_depth). // // Only one copy of this exists for each call to Demangle, so the size of this // struct is nearly inconsequential. State; namespace { #ifdef ABSL_INTERNAL_DEMANGLE_RECORDS_HIGH_WATER_MARK void UpdateHighWaterMark(State *state) { if (state->high_water_mark < state->parse_state.mangled_idx) { state->high_water_mark = state->parse_state.mangled_idx; } } void ReportHighWaterMark(State *state) { // Write out the mangled name with the trouble point marked, provided that the // output buffer is large enough and the mangled name did not hit a complexity // limit (in which case the high water mark wouldn't point out an unparsable // construct, only the point where a budget ran out). const size_t input_length = std::strlen(state->mangled_begin); if (input_length + 6 > static_cast<size_t>(state->out_end_idx) || state->too_complex) { if (state->out_end_idx > 0) state->out[0] = '\0'; return; } const size_t high_water_mark = static_cast<size_t>(state->high_water_mark); std::memcpy(state->out, state->mangled_begin, high_water_mark); std::memcpy(state->out + high_water_mark, "--!--", 5); std::memcpy(state->out + high_water_mark + 5, state->mangled_begin + high_water_mark, input_length - high_water_mark); state->out[input_length + 5] = '\0'; } #else void UpdateHighWaterMark(State *) { … } void ReportHighWaterMark(State *) { … } #endif // Prevent deep recursion / stack exhaustion. // Also prevent unbounded handling of complex inputs. class ComplexityGuard { … }; } // namespace // We don't use strlen() in libc since it's not guaranteed to be async // signal safe. static size_t StrLen(const char *str) { … } // Returns true if "str" has at least "n" characters remaining. static bool AtLeastNumCharsRemaining(const char *str, size_t n) { … } // Returns true if "str" has "prefix" as a prefix. static bool StrPrefix(const char *str, const char *prefix) { … } static void InitState(State* state, const char* mangled, char* out, size_t out_size) { … } static inline const char *RemainingInput(State *state) { … } // Returns true and advances "mangled_idx" if we find "one_char_token" // at "mangled_idx" position. It is assumed that "one_char_token" does // not contain '\0'. static bool ParseOneCharToken(State *state, const char one_char_token) { … } // Returns true and advances "mangled_idx" if we find "two_char_token" // at "mangled_idx" position. It is assumed that "two_char_token" does // not contain '\0'. static bool ParseTwoCharToken(State *state, const char *two_char_token) { … } // Returns true and advances "mangled_idx" if we find "three_char_token" // at "mangled_idx" position. It is assumed that "three_char_token" does // not contain '\0'. static bool ParseThreeCharToken(State *state, const char *three_char_token) { … } // Returns true and advances "mangled_idx" if we find a copy of the // NUL-terminated string "long_token" at "mangled_idx" position. static bool ParseLongToken(State *state, const char *long_token) { … } // Returns true and advances "mangled_cur" if we find any character in // "char_class" at "mangled_cur" position. static bool ParseCharClass(State *state, const char *char_class) { … } static bool ParseDigit(State *state, int *digit) { … } // This function is used for handling an optional non-terminal. static bool Optional(bool /*status*/) { … } // This function is used for handling <non-terminal>+ syntax. ParseFunc; static bool OneOrMore(ParseFunc parse_func, State *state) { … } // This function is used for handling <non-terminal>* syntax. The function // always returns true and must be followed by a termination token or a // terminating sequence not handled by parse_func (e.g. // ParseOneCharToken(state, 'E')). static bool ZeroOrMore(ParseFunc parse_func, State *state) { … } // Append "str" at "out_cur_idx". If there is an overflow, out_cur_idx is // set to out_end_idx+1. The output string is ensured to // always terminate with '\0' as long as there is no overflow. static void Append(State *state, const char *const str, const size_t length) { … } // We don't use equivalents in libc to avoid locale issues. static bool IsLower(char c) { … } static bool IsAlpha(char c) { … } static bool IsDigit(char c) { … } // Returns true if "str" is a function clone suffix. These suffixes are used // by GCC 4.5.x and later versions (and our locally-modified version of GCC // 4.4.x) to indicate functions which have been cloned during optimization. // We treat any sequence (.<alpha>+.<digit>+)+ as a function clone suffix. // Additionally, '_' is allowed along with the alphanumeric sequence. static bool IsFunctionCloneSuffix(const char *str) { … } static bool EndsWith(State *state, const char chr) { … } // Append "str" with some tweaks, iff "append" state is true. static void MaybeAppendWithLength(State *state, const char *const str, const size_t length) { … } // Appends a positive decimal number to the output if appending is enabled. static bool MaybeAppendDecimal(State *state, int val) { … } // A convenient wrapper around MaybeAppendWithLength(). // Returns true so that it can be placed in "if" conditions. static bool MaybeAppend(State *state, const char *const str) { … } // This function is used for handling nested names. static bool EnterNestedName(State *state) { … } // This function is used for handling nested names. static bool LeaveNestedName(State *state, int16_t prev_value) { … } // Disable the append mode not to print function parameters, etc. static bool DisableAppend(State *state) { … } // Restore the append mode to the previous state. static bool RestoreAppend(State *state, bool prev_value) { … } // Increase the nest level for nested names. static void MaybeIncreaseNestLevel(State *state) { … } // Appends :: for nested names if necessary. static void MaybeAppendSeparator(State *state) { … } // Cancel the last separator if necessary. static void MaybeCancelLastSeparator(State *state) { … } // Returns true if the identifier of the given length pointed to by // "mangled_cur" is anonymous namespace. static bool IdentifierIsAnonymousNamespace(State *state, size_t length) { … } // Forward declarations of our parsing functions. static bool ParseMangledName(State *state); static bool ParseEncoding(State *state); static bool ParseName(State *state); static bool ParseUnscopedName(State *state); static bool ParseNestedName(State *state); static bool ParsePrefix(State *state); static bool ParseUnqualifiedName(State *state); static bool ParseSourceName(State *state); static bool ParseLocalSourceName(State *state); static bool ParseUnnamedTypeName(State *state); static bool ParseNumber(State *state, int *number_out); static bool ParseFloatNumber(State *state); static bool ParseSeqId(State *state); static bool ParseIdentifier(State *state, size_t length); static bool ParseOperatorName(State *state, int *arity); static bool ParseConversionOperatorType(State *state); static bool ParseSpecialName(State *state); static bool ParseCallOffset(State *state); static bool ParseNVOffset(State *state); static bool ParseVOffset(State *state); static bool ParseAbiTags(State *state); static bool ParseCtorDtorName(State *state); static bool ParseDecltype(State *state); static bool ParseType(State *state); static bool ParseCVQualifiers(State *state); static bool ParseExtendedQualifier(State *state); static bool ParseBuiltinType(State *state); static bool ParseVendorExtendedType(State *state); static bool ParseFunctionType(State *state); static bool ParseBareFunctionType(State *state); static bool ParseOverloadAttribute(State *state); static bool ParseClassEnumType(State *state); static bool ParseArrayType(State *state); static bool ParsePointerToMemberType(State *state); static bool ParseTemplateParam(State *state); static bool ParseTemplateParamDecl(State *state); static bool ParseTemplateTemplateParam(State *state); static bool ParseTemplateArgs(State *state); static bool ParseTemplateArg(State *state); static bool ParseBaseUnresolvedName(State *state); static bool ParseUnresolvedName(State *state); static bool ParseUnresolvedQualifierLevel(State *state); static bool ParseUnionSelector(State* state); static bool ParseFunctionParam(State* state); static bool ParseBracedExpression(State *state); static bool ParseExpression(State *state); static bool ParseInitializer(State *state); static bool ParseExprPrimary(State *state); static bool ParseExprCastValueAndTrailingE(State *state); static bool ParseQRequiresClauseExpr(State *state); static bool ParseRequirement(State *state); static bool ParseTypeConstraint(State *state); static bool ParseLocalName(State *state); static bool ParseLocalNameSuffix(State *state); static bool ParseDiscriminator(State *state); static bool ParseSubstitution(State *state, bool accept_std); // Implementation note: the following code is a straightforward // translation of the Itanium C++ ABI defined in BNF with a couple of // exceptions. // // - Support GNU extensions not defined in the Itanium C++ ABI // - <prefix> and <template-prefix> are combined to avoid infinite loop // - Reorder patterns to shorten the code // - Reorder patterns to give greedier functions precedence // We'll mark "Less greedy than" for these cases in the code // // Each parsing function changes the parse state and returns true on // success, or returns false and doesn't change the parse state (note: // the parse-steps counter increases regardless of success or failure). // To ensure that the parse state isn't changed in the latter case, we // save the original state before we call multiple parsing functions // consecutively with &&, and restore it if unsuccessful. See // ParseEncoding() as an example of this convention. We follow the // convention throughout the code. // // Originally we tried to do demangling without following the full ABI // syntax but it turned out we needed to follow the full syntax to // parse complicated cases like nested template arguments. Note that // implementing a full-fledged demangler isn't trivial (libiberty's // cp-demangle.c has +4300 lines). // // Note that (foo) in <(foo) ...> is a modifier to be ignored. // // Reference: // - Itanium C++ ABI // <https://itanium-cxx-abi.github.io/cxx-abi/abi.html#mangling> // <mangled-name> ::= _Z <encoding> static bool ParseMangledName(State *state) { … } // <encoding> ::= <(function) name> <bare-function-type> // [`Q` <requires-clause expr>] // ::= <(data) name> // ::= <special-name> // // NOTE: Based on http://shortn/_Hoq9qG83rx static bool ParseEncoding(State *state) { … } // <name> ::= <nested-name> // ::= <unscoped-template-name> <template-args> // ::= <unscoped-name> // ::= <local-name> static bool ParseName(State *state) { … } // <unscoped-name> ::= <unqualified-name> // ::= St <unqualified-name> static bool ParseUnscopedName(State *state) { … } // <ref-qualifer> ::= R // lvalue method reference qualifier // ::= O // rvalue method reference qualifier static inline bool ParseRefQualifier(State *state) { … } // <nested-name> ::= N [<CV-qualifiers>] [<ref-qualifier>] <prefix> // <unqualified-name> E // ::= N [<CV-qualifiers>] [<ref-qualifier>] <template-prefix> // <template-args> E static bool ParseNestedName(State *state) { … } // This part is tricky. If we literally translate them to code, we'll // end up infinite loop. Hence we merge them to avoid the case. // // <prefix> ::= <prefix> <unqualified-name> // ::= <template-prefix> <template-args> // ::= <template-param> // ::= <decltype> // ::= <substitution> // ::= # empty // <template-prefix> ::= <prefix> <(template) unqualified-name> // ::= <template-param> // ::= <substitution> // ::= <vendor-extended-type> static bool ParsePrefix(State *state) { … } // <unqualified-name> ::= <operator-name> [<abi-tags>] // ::= <ctor-dtor-name> [<abi-tags>] // ::= <source-name> [<abi-tags>] // ::= <local-source-name> [<abi-tags>] // ::= <unnamed-type-name> [<abi-tags>] // ::= DC <source-name>+ E # C++17 structured binding // ::= F <source-name> # C++20 constrained friend // ::= F <operator-name> # C++20 constrained friend // // <local-source-name> is a GCC extension; see below. // // For the F notation for constrained friends, see // https://github.com/itanium-cxx-abi/cxx-abi/issues/24#issuecomment-1491130332. static bool ParseUnqualifiedName(State *state) { … } // <abi-tags> ::= <abi-tag> [<abi-tags>] // <abi-tag> ::= B <source-name> static bool ParseAbiTags(State *state) { … } // <source-name> ::= <positive length number> <identifier> static bool ParseSourceName(State *state) { … } // <local-source-name> ::= L <source-name> [<discriminator>] // // References: // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=31775 // https://gcc.gnu.org/viewcvs?view=rev&revision=124467 static bool ParseLocalSourceName(State *state) { … } // <unnamed-type-name> ::= Ut [<(nonnegative) number>] _ // ::= <closure-type-name> // <closure-type-name> ::= Ul <lambda-sig> E [<(nonnegative) number>] _ // <lambda-sig> ::= <template-param-decl>* <(parameter) type>+ // // For <template-param-decl>* in <lambda-sig> see: // // https://github.com/itanium-cxx-abi/cxx-abi/issues/31 static bool ParseUnnamedTypeName(State *state) { … } // <number> ::= [n] <non-negative decimal integer> // If "number_out" is non-null, then *number_out is set to the value of the // parsed number on success. static bool ParseNumber(State *state, int *number_out) { … } // Floating-point literals are encoded using a fixed-length lowercase // hexadecimal string. static bool ParseFloatNumber(State *state) { … } // The <seq-id> is a sequence number in base 36, // using digits and upper case letters static bool ParseSeqId(State *state) { … } // <identifier> ::= <unqualified source code identifier> (of given length) static bool ParseIdentifier(State *state, size_t length) { … } // <operator-name> ::= nw, and other two letters cases // ::= cv <type> # (cast) // ::= li <source-name> # C++11 user-defined literal // ::= v <digit> <source-name> # vendor extended operator static bool ParseOperatorName(State *state, int *arity) { … } // <operator-name> ::= cv <type> # (cast) // // The name of a conversion operator is the one place where cv-qualifiers, *, &, // and other simple type combinators are expected to appear in our stripped-down // demangling (elsewhere they appear in function signatures or template // arguments, which we omit from the output). We make reasonable efforts to // render simple cases accurately. static bool ParseConversionOperatorType(State *state) { … } // <special-name> ::= TV <type> // ::= TT <type> // ::= TI <type> // ::= TS <type> // ::= TW <name> # thread-local wrapper // ::= TH <name> # thread-local initialization // ::= Tc <call-offset> <call-offset> <(base) encoding> // ::= GV <(object) name> // ::= GR <(object) name> [<seq-id>] _ // ::= T <call-offset> <(base) encoding> // ::= GTt <encoding> # transaction-safe entry point // ::= TA <template-arg> # nontype template parameter object // G++ extensions: // ::= TC <type> <(offset) number> _ <(base) type> // ::= TF <type> // ::= TJ <type> // ::= GR <name> # without final _, perhaps an earlier form? // ::= GA <encoding> // ::= Th <call-offset> <(base) encoding> // ::= Tv <call-offset> <(base) encoding> // // Note: Most of these are special data, not functions that occur in stack // traces. Exceptions are TW and TH, which denote functions supporting the // thread_local feature. For these see: // // https://maskray.me/blog/2021-02-14-all-about-thread-local-storage // // For TA see https://github.com/itanium-cxx-abi/cxx-abi/issues/63. static bool ParseSpecialName(State *state) { … } // <call-offset> ::= h <nv-offset> _ // ::= v <v-offset> _ static bool ParseCallOffset(State *state) { … } // <nv-offset> ::= <(offset) number> static bool ParseNVOffset(State *state) { … } // <v-offset> ::= <(offset) number> _ <(virtual offset) number> static bool ParseVOffset(State *state) { … } // <ctor-dtor-name> ::= C1 | C2 | C3 | CI1 <base-class-type> | CI2 // <base-class-type> // ::= D0 | D1 | D2 // # GCC extensions: "unified" constructor/destructor. See // # // https://github.com/gcc-mirror/gcc/blob/7ad17b583c3643bd4557f29b8391ca7ef08391f5/gcc/cp/mangle.c#L1847 // ::= C4 | D4 static bool ParseCtorDtorName(State *state) { … } // <decltype> ::= Dt <expression> E # decltype of an id-expression or class // # member access (C++0x) // ::= DT <expression> E # decltype of an expression (C++0x) static bool ParseDecltype(State *state) { … } // <type> ::= <CV-qualifiers> <type> // ::= P <type> # pointer-to // ::= R <type> # reference-to // ::= O <type> # rvalue reference-to (C++0x) // ::= C <type> # complex pair (C 2000) // ::= G <type> # imaginary (C 2000) // ::= <builtin-type> // ::= <function-type> // ::= <class-enum-type> # note: just an alias for <name> // ::= <array-type> // ::= <pointer-to-member-type> // ::= <template-template-param> <template-args> // ::= <template-param> // ::= <decltype> // ::= <substitution> // ::= Dp <type> # pack expansion of (C++0x) // ::= Dv <(elements) number> _ <type> # GNU vector extension // ::= Dv <(bytes) expression> _ <type> // ::= Dk <type-constraint> # constrained auto // static bool ParseType(State *state) { … } // <qualifiers> ::= <extended-qualifier>* <CV-qualifiers> // <CV-qualifiers> ::= [r] [V] [K] // // We don't allow empty <CV-qualifiers> to avoid infinite loop in // ParseType(). static bool ParseCVQualifiers(State *state) { … } // <extended-qualifier> ::= U <source-name> [<template-args>] static bool ParseExtendedQualifier(State *state) { … } // <builtin-type> ::= v, etc. # single-character builtin types // ::= <vendor-extended-type> // ::= Dd, etc. # two-character builtin types // ::= DB (<number> | <expression>) _ # _BitInt(N) // ::= DU (<number> | <expression>) _ # unsigned _BitInt(N) // ::= DF <number> _ # _FloatN (N bits) // ::= DF <number> x # _FloatNx // ::= DF16b # std::bfloat16_t // // Not supported: // ::= [DS] DA <fixed-point-size> // ::= [DS] DR <fixed-point-size> // because real implementations of N1169 fixed-point are scant. static bool ParseBuiltinType(State *state) { … } // <vendor-extended-type> ::= u <source-name> [<template-args>] static bool ParseVendorExtendedType(State *state) { … } // <exception-spec> ::= Do # non-throwing // exception-specification (e.g., // noexcept, throw()) // ::= DO <expression> E # computed (instantiation-dependent) // noexcept // ::= Dw <type>+ E # dynamic exception specification // with instantiation-dependent types static bool ParseExceptionSpec(State *state) { … } // <function-type> ::= // [exception-spec] [Dx] F [Y] <bare-function-type> [<ref-qualifier>] E // // <ref-qualifier> ::= R | O static bool ParseFunctionType(State *state) { … } // <bare-function-type> ::= <overload-attribute>* <(signature) type>+ // // The <overload-attribute>* prefix is nonstandard; see the comment on // ParseOverloadAttribute. static bool ParseBareFunctionType(State *state) { … } // <overload-attribute> ::= Ua <name> // // The nonstandard <overload-attribute> production is sufficient to accept the // current implementation of __attribute__((enable_if(condition, "message"))) // and future attributes of a similar shape. See // https://clang.llvm.org/docs/AttributeReference.html#enable-if and the // definition of CXXNameMangler::mangleFunctionEncodingBareType in Clang's // source code. static bool ParseOverloadAttribute(State *state) { … } // <class-enum-type> ::= <name> // ::= Ts <name> # struct Name or class Name // ::= Tu <name> # union Name // ::= Te <name> # enum Name // // See http://shortn/_W3YrltiEd0. static bool ParseClassEnumType(State *state) { … } // <array-type> ::= A <(positive dimension) number> _ <(element) type> // ::= A [<(dimension) expression>] _ <(element) type> static bool ParseArrayType(State *state) { … } // <pointer-to-member-type> ::= M <(class) type> <(member) type> static bool ParsePointerToMemberType(State *state) { … } // <template-param> ::= T_ // ::= T <parameter-2 non-negative number> _ // ::= TL <level-1> __ // ::= TL <level-1> _ <parameter-2 non-negative number> _ static bool ParseTemplateParam(State *state) { … } // <template-param-decl> // ::= Ty # template type parameter // ::= Tk <concept name> [<template-args>] # constrained type parameter // ::= Tn <type> # template non-type parameter // ::= Tt <template-param-decl>* E # template template parameter // ::= Tp <template-param-decl> # template parameter pack // // NOTE: <concept name> is just a <name>: http://shortn/_MqJVyr0fc1 // TODO(b/324066279): Implement optional suffix for `Tt`: // [Q <requires-clause expr>] static bool ParseTemplateParamDecl(State *state) { … } // <template-template-param> ::= <template-param> // ::= <substitution> static bool ParseTemplateTemplateParam(State *state) { … } // <template-args> ::= I <template-arg>+ [Q <requires-clause expr>] E static bool ParseTemplateArgs(State *state) { … } // <template-arg> ::= <template-param-decl> <template-arg> // ::= <type> // ::= <expr-primary> // ::= J <template-arg>* E # argument pack // ::= X <expression> E static bool ParseTemplateArg(State *state) { … } // <unresolved-type> ::= <template-param> [<template-args>] // ::= <decltype> // ::= <substitution> static inline bool ParseUnresolvedType(State *state) { … } // <simple-id> ::= <source-name> [<template-args>] static inline bool ParseSimpleId(State *state) { … } // <base-unresolved-name> ::= <source-name> [<template-args>] // ::= on <operator-name> [<template-args>] // ::= dn <destructor-name> static bool ParseBaseUnresolvedName(State *state) { … } // <unresolved-name> ::= [gs] <base-unresolved-name> // ::= sr <unresolved-type> <base-unresolved-name> // ::= srN <unresolved-type> <unresolved-qualifier-level>+ E // <base-unresolved-name> // ::= [gs] sr <unresolved-qualifier-level>+ E // <base-unresolved-name> // ::= sr St <simple-id> <simple-id> # nonstandard // // The last case is not part of the official grammar but has been observed in // real-world examples that the GNU demangler (but not the LLVM demangler) is // able to decode; see demangle_test.cc for one such symbol name. The shape // sr St <simple-id> <simple-id> was inferred by closed-box testing of the GNU // demangler. static bool ParseUnresolvedName(State *state) { … } // <unresolved-qualifier-level> ::= <simple-id> // ::= <substitution> <template-args> // // The production <substitution> <template-args> is nonstandard but is observed // in practice. An upstream discussion on the best shape of <unresolved-name> // has not converged: // // https://github.com/itanium-cxx-abi/cxx-abi/issues/38 static bool ParseUnresolvedQualifierLevel(State *state) { … } // <union-selector> ::= _ [<number>] // // https://github.com/itanium-cxx-abi/cxx-abi/issues/47 static bool ParseUnionSelector(State *state) { … } // <function-param> ::= fp <(top-level) CV-qualifiers> _ // ::= fp <(top-level) CV-qualifiers> <number> _ // ::= fL <number> p <(top-level) CV-qualifiers> _ // ::= fL <number> p <(top-level) CV-qualifiers> <number> _ // ::= fpT # this static bool ParseFunctionParam(State *state) { … } // <braced-expression> ::= <expression> // ::= di <field source-name> <braced-expression> // ::= dx <index expression> <braced-expression> // ::= dX <expression> <expression> <braced-expression> static bool ParseBracedExpression(State *state) { … } // <expression> ::= <1-ary operator-name> <expression> // ::= <2-ary operator-name> <expression> <expression> // ::= <3-ary operator-name> <expression> <expression> <expression> // ::= pp_ <expression> # ++e; pp <expression> is e++ // ::= mm_ <expression> # --e; mm <expression> is e-- // ::= cl <expression>+ E // ::= cp <simple-id> <expression>* E # Clang-specific. // ::= so <type> <expression> [<number>] <union-selector>* [p] E // ::= cv <type> <expression> # type (expression) // ::= cv <type> _ <expression>* E # type (expr-list) // ::= tl <type> <braced-expression>* E // ::= il <braced-expression>* E // ::= [gs] nw <expression>* _ <type> E // ::= [gs] nw <expression>* _ <type> <initializer> // ::= [gs] na <expression>* _ <type> E // ::= [gs] na <expression>* _ <type> <initializer> // ::= [gs] dl <expression> // ::= [gs] da <expression> // ::= dc <type> <expression> // ::= sc <type> <expression> // ::= cc <type> <expression> // ::= rc <type> <expression> // ::= ti <type> // ::= te <expression> // ::= st <type> // ::= at <type> // ::= az <expression> // ::= nx <expression> // ::= <template-param> // ::= <function-param> // ::= sZ <template-param> // ::= sZ <function-param> // ::= sP <template-arg>* E // ::= <expr-primary> // ::= dt <expression> <unresolved-name> # expr.name // ::= pt <expression> <unresolved-name> # expr->name // ::= sp <expression> # argument pack expansion // ::= fl <binary operator-name> <expression> // ::= fr <binary operator-name> <expression> // ::= fL <binary operator-name> <expression> <expression> // ::= fR <binary operator-name> <expression> <expression> // ::= tw <expression> // ::= tr // ::= sr <type> <unqualified-name> <template-args> // ::= sr <type> <unqualified-name> // ::= u <source-name> <template-arg>* E # vendor extension // ::= rq <requirement>+ E // ::= rQ <bare-function-type> _ <requirement>+ E static bool ParseExpression(State *state) { … } // <initializer> ::= pi <expression>* E // ::= il <braced-expression>* E // // The il ... E form is not in the ABI spec but is seen in practice for // braced-init-lists in new-expressions, which are standard syntax from C++11 // on. static bool ParseInitializer(State *state) { … } // <expr-primary> ::= L <type> <(value) number> E // ::= L <type> <(value) float> E // ::= L <mangled-name> E // // A bug in g++'s C++ ABI version 2 (-fabi-version=2). // ::= LZ <encoding> E // // Warning, subtle: the "bug" LZ production above is ambiguous with the first // production where <type> starts with <local-name>, which can lead to // exponential backtracking in two scenarios: // // - When whatever follows the E in the <local-name> in the first production is // not a name, we backtrack the whole <encoding> and re-parse the whole thing. // // - When whatever follows the <local-name> in the first production is not a // number and this <expr-primary> may be followed by a name, we backtrack the // <name> and re-parse it. // // Moreover this ambiguity isn't always resolved -- for example, the following // has two different parses: // // _ZaaILZ4aoeuE1x1EvE // => operator&&<aoeu, x, E, void> // => operator&&<(aoeu::x)(1), void> // // To resolve this, we just do what GCC's demangler does, and refuse to parse // casts to <local-name> types. static bool ParseExprPrimary(State *state) { … } // <number> or <float>, followed by 'E', as described above ParseExprPrimary. static bool ParseExprCastValueAndTrailingE(State *state) { … } // Parses `Q <requires-clause expr>`. // If parsing fails, applies backtracking to `state`. // // This function covers two symbols instead of one for convenience, // because in LLVM's Itanium ABI mangling grammar, <requires-clause expr> // always appears after Q. // // Does not emit the parsed `requires` clause to simplify the implementation. // In other words, these two functions' mangled names will demangle identically: // // template <typename T> // int foo(T) requires IsIntegral<T>; // // vs. // // template <typename T> // int foo(T); static bool ParseQRequiresClauseExpr(State *state) { … } // <requirement> ::= X <expression> [N] [R <type-constraint>] // <requirement> ::= T <type> // <requirement> ::= Q <constraint-expression> // // <constraint-expression> ::= <expression> // // https://github.com/itanium-cxx-abi/cxx-abi/issues/24 static bool ParseRequirement(State *state) { … } // <type-constraint> ::= <name> static bool ParseTypeConstraint(State *state) { … } // <local-name> ::= Z <(function) encoding> E <(entity) name> [<discriminator>] // ::= Z <(function) encoding> E s [<discriminator>] // ::= Z <(function) encoding> E d [<(parameter) number>] _ <name> // // Parsing a common prefix of these two productions together avoids an // exponential blowup of backtracking. Parse like: // <local-name> := Z <encoding> E <local-name-suffix> // <local-name-suffix> ::= s [<discriminator>] // ::= d [<(parameter) number>] _ <name> // ::= <name> [<discriminator>] static bool ParseLocalNameSuffix(State *state) { … } static bool ParseLocalName(State *state) { … } // <discriminator> := _ <digit> // := __ <number (>= 10)> _ static bool ParseDiscriminator(State *state) { … } // <substitution> ::= S_ // ::= S <seq-id> _ // ::= St, etc. // // "St" is special in that it's not valid as a standalone name, and it *is* // allowed to precede a name without being wrapped in "N...E". This means that // if we accept it on its own, we can accept "St1a" and try to parse // template-args, then fail and backtrack, accept "St" on its own, then "1a" as // an unqualified name and re-parse the same template-args. To block this // exponential backtracking, we disable it with 'accept_std=false' in // problematic contexts. static bool ParseSubstitution(State *state, bool accept_std) { … } // Parse <mangled-name>, optionally followed by either a function-clone suffix // or version suffix. Returns true only if all of "mangled_cur" was consumed. static bool ParseTopLevelMangledName(State *state) { … } static bool Overflowed(const State *state) { … } // The demangler entry point. bool Demangle(const char* mangled, char* out, size_t out_size) { … } std::string DemangleString(const char* mangled) { … } } // namespace debugging_internal ABSL_NAMESPACE_END } // namespace absl