Howard Hinnant | d213ffd | 2011-05-05 15:27:28 +0000 | [diff] [blame] | 1 | //===-------------------------- cxa_demangle.cpp --------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Erik Pilkington | 1c300b9 | 2017-11-21 15:04:08 +0000 | [diff] [blame] | 10 | // FIXME: (possibly) incomplete list of features that clang mangles that this |
| 11 | // file does not yet support: |
Erik Pilkington | 1c300b9 | 2017-11-21 15:04:08 +0000 | [diff] [blame] | 12 | // - C++ modules TS |
| 13 | |
Howard Hinnant | 6c33e76 | 2013-06-17 18:10:34 +0000 | [diff] [blame] | 14 | #define _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | d213ffd | 2011-05-05 15:27:28 +0000 | [diff] [blame] | 15 | |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 16 | #include "__cxxabi_config.h" |
| 17 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 18 | #include "demangle/ItaniumDemangle.h" |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 19 | |
Erik Pilkington | 761e6b0 | 2018-01-31 20:17:06 +0000 | [diff] [blame] | 20 | #include <cassert> |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 21 | #include <cctype> |
Erik Pilkington | 7710154 | 2017-07-28 00:53:30 +0000 | [diff] [blame] | 22 | #include <cstdio> |
Howard Hinnant | 6c33e76 | 2013-06-17 18:10:34 +0000 | [diff] [blame] | 23 | #include <cstdlib> |
| 24 | #include <cstring> |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 25 | #include <functional> |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 26 | #include <numeric> |
Erik Pilkington | 4a3bc0a | 2018-07-27 17:27:40 +0000 | [diff] [blame] | 27 | #include <utility> |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 28 | #include <vector> |
Howard Hinnant | d213ffd | 2011-05-05 15:27:28 +0000 | [diff] [blame] | 29 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 30 | using namespace itanium_demangle; |
Erik Pilkington | 761e6b0 | 2018-01-31 20:17:06 +0000 | [diff] [blame] | 31 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 32 | constexpr const char *itanium_demangle::FloatData<float>::spec; |
| 33 | constexpr const char *itanium_demangle::FloatData<double>::spec; |
| 34 | constexpr const char *itanium_demangle::FloatData<long double>::spec; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 35 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 36 | // <discriminator> := _ <non-negative number> # when number < 10 |
| 37 | // := __ <non-negative number> _ # when number >= 10 |
| 38 | // extension := decimal-digit+ # at the end of string |
| 39 | const char *itanium_demangle::parse_discriminator(const char *first, |
| 40 | const char *last) { |
| 41 | // parse but ignore discriminator |
| 42 | if (first != last) { |
| 43 | if (*first == '_') { |
| 44 | const char *t1 = first + 1; |
| 45 | if (t1 != last) { |
| 46 | if (std::isdigit(*t1)) |
| 47 | first = t1 + 1; |
| 48 | else if (*t1 == '_') { |
| 49 | for (++t1; t1 != last && std::isdigit(*t1); ++t1) |
| 50 | ; |
| 51 | if (t1 != last && *t1 == '_') |
| 52 | first = t1 + 1; |
| 53 | } |
Erik Pilkington | 85619f5 | 2018-03-25 22:49:57 +0000 | [diff] [blame] | 54 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 55 | } else if (std::isdigit(*first)) { |
| 56 | const char *t1 = first + 1; |
| 57 | for (; t1 != last && std::isdigit(*t1); ++t1) |
| 58 | ; |
| 59 | if (t1 == last) |
| 60 | first = last; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 63 | return first; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 66 | #ifndef NDEBUG |
| 67 | namespace { |
| 68 | struct DumpVisitor { |
| 69 | unsigned Depth = 0; |
| 70 | bool PendingNewline = false; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 71 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 72 | template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) { |
| 73 | return true; |
| 74 | } |
| 75 | static bool wantsNewline(NodeArray A) { return !A.empty(); } |
| 76 | static constexpr bool wantsNewline(...) { return false; } |
| 77 | |
| 78 | template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) { |
| 79 | for (bool B : {wantsNewline(Vs)...}) |
| 80 | if (B) |
| 81 | return true; |
| 82 | return false; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 85 | void printStr(const char *S) { fprintf(stderr, "%s", S); } |
| 86 | void print(StringView SV) { |
| 87 | fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin()); |
Erik Pilkington | 761e6b0 | 2018-01-31 20:17:06 +0000 | [diff] [blame] | 88 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 89 | void print(const Node *N) { |
| 90 | if (N) |
| 91 | N->visit(std::ref(*this)); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 92 | else |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 93 | printStr("<null>"); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 94 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 95 | void print(NodeOrString NS) { |
| 96 | if (NS.isNode()) |
| 97 | print(NS.asNode()); |
| 98 | else if (NS.isString()) |
| 99 | print(NS.asString()); |
| 100 | else |
| 101 | printStr("NodeOrString()"); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 102 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 103 | void print(NodeArray A) { |
| 104 | ++Depth; |
| 105 | printStr("{"); |
| 106 | bool First = true; |
| 107 | for (const Node *N : A) { |
| 108 | if (First) |
| 109 | print(N); |
| 110 | else |
| 111 | printWithComma(N); |
| 112 | First = false; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 113 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 114 | printStr("}"); |
| 115 | --Depth; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 116 | } |
Erik Pilkington | cd48625 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 117 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 118 | // Overload used when T is exactly 'bool', not merely convertible to 'bool'. |
Erik Pilkington | cd48625 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 119 | void print(bool B) { printStr(B ? "true" : "false"); } |
| 120 | |
| 121 | template <class T> |
| 122 | typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) { |
| 123 | fprintf(stderr, "%llu", (unsigned long long)N); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 124 | } |
Erik Pilkington | cd48625 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 125 | |
| 126 | template <class T> |
| 127 | typename std::enable_if<std::is_signed<T>::value>::type print(T N) { |
| 128 | fprintf(stderr, "%lld", (long long)N); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 129 | } |
Erik Pilkington | cd48625 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 130 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 131 | void print(ReferenceKind RK) { |
| 132 | switch (RK) { |
| 133 | case ReferenceKind::LValue: |
| 134 | return printStr("ReferenceKind::LValue"); |
| 135 | case ReferenceKind::RValue: |
| 136 | return printStr("ReferenceKind::RValue"); |
Erik Pilkington | 7e027ac | 2018-02-14 01:08:20 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 139 | void print(FunctionRefQual RQ) { |
| 140 | switch (RQ) { |
| 141 | case FunctionRefQual::FrefQualNone: |
| 142 | return printStr("FunctionRefQual::FrefQualNone"); |
| 143 | case FunctionRefQual::FrefQualLValue: |
| 144 | return printStr("FunctionRefQual::FrefQualLValue"); |
| 145 | case FunctionRefQual::FrefQualRValue: |
| 146 | return printStr("FunctionRefQual::FrefQualRValue"); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 149 | void print(Qualifiers Qs) { |
| 150 | if (!Qs) return printStr("QualNone"); |
| 151 | struct QualName { Qualifiers Q; const char *Name; } Names[] = { |
| 152 | {QualConst, "QualConst"}, |
| 153 | {QualVolatile, "QualVolatile"}, |
| 154 | {QualRestrict, "QualRestrict"}, |
Erik Pilkington | 967b00e | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 155 | }; |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 156 | for (QualName Name : Names) { |
| 157 | if (Qs & Name.Q) { |
| 158 | printStr(Name.Name); |
| 159 | Qs = Qualifiers(Qs & ~Name.Q); |
| 160 | if (Qs) printStr(" | "); |
Erik Pilkington | 967b00e | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Erik Pilkington | 967b00e | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 163 | } |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 164 | void print(SpecialSubKind SSK) { |
| 165 | switch (SSK) { |
| 166 | case SpecialSubKind::allocator: |
| 167 | return printStr("SpecialSubKind::allocator"); |
| 168 | case SpecialSubKind::basic_string: |
| 169 | return printStr("SpecialSubKind::basic_string"); |
| 170 | case SpecialSubKind::string: |
| 171 | return printStr("SpecialSubKind::string"); |
| 172 | case SpecialSubKind::istream: |
| 173 | return printStr("SpecialSubKind::istream"); |
| 174 | case SpecialSubKind::ostream: |
| 175 | return printStr("SpecialSubKind::ostream"); |
| 176 | case SpecialSubKind::iostream: |
| 177 | return printStr("SpecialSubKind::iostream"); |
| 178 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 179 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 180 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 181 | void newLine() { |
| 182 | printStr("\n"); |
| 183 | for (unsigned I = 0; I != Depth; ++I) |
| 184 | printStr(" "); |
| 185 | PendingNewline = false; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 186 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 187 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 188 | template<typename T> void printWithPendingNewline(T V) { |
| 189 | print(V); |
| 190 | if (wantsNewline(V)) |
| 191 | PendingNewline = true; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 192 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 193 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 194 | template<typename T> void printWithComma(T V) { |
| 195 | if (PendingNewline || wantsNewline(V)) { |
| 196 | printStr(","); |
| 197 | newLine(); |
| 198 | } else { |
| 199 | printStr(", "); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 202 | printWithPendingNewline(V); |
| 203 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 204 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 205 | struct CtorArgPrinter { |
| 206 | DumpVisitor &Visitor; |
| 207 | |
| 208 | template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) { |
| 209 | if (Visitor.anyWantNewline(V, Vs...)) |
| 210 | Visitor.newLine(); |
| 211 | Visitor.printWithPendingNewline(V); |
| 212 | int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 }; |
| 213 | (void)PrintInOrder; |
| 214 | } |
| 215 | }; |
| 216 | |
| 217 | template<typename NodeT> void operator()(const NodeT *Node) { |
| 218 | Depth += 2; |
| 219 | fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name()); |
| 220 | Node->match(CtorArgPrinter{*this}); |
| 221 | fprintf(stderr, ")"); |
| 222 | Depth -= 2; |
| 223 | } |
| 224 | |
| 225 | void operator()(const ForwardTemplateReference *Node) { |
| 226 | Depth += 2; |
| 227 | fprintf(stderr, "ForwardTemplateReference("); |
| 228 | if (Node->Ref && !Node->Printing) { |
| 229 | Node->Printing = true; |
| 230 | CtorArgPrinter{*this}(Node->Ref); |
| 231 | Node->Printing = false; |
| 232 | } else { |
| 233 | CtorArgPrinter{*this}(Node->Index); |
| 234 | } |
| 235 | fprintf(stderr, ")"); |
| 236 | Depth -= 2; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 237 | } |
| 238 | }; |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 239 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 240 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 241 | void itanium_demangle::Node::dump() const { |
| 242 | DumpVisitor V; |
| 243 | visit(std::ref(V)); |
| 244 | V.newLine(); |
| 245 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 246 | #endif |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 247 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 248 | namespace { |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 249 | class BumpPointerAllocator { |
| 250 | struct BlockMeta { |
| 251 | BlockMeta* Next; |
| 252 | size_t Current; |
| 253 | }; |
Erik Pilkington | 9dd63d2 | 2017-07-08 18:54:07 +0000 | [diff] [blame] | 254 | |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 255 | static constexpr size_t AllocSize = 4096; |
| 256 | static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta); |
Erik Pilkington | 9dd63d2 | 2017-07-08 18:54:07 +0000 | [diff] [blame] | 257 | |
Serge Pavlov | 047fb73 | 2018-07-05 06:24:29 +0000 | [diff] [blame] | 258 | alignas(long double) char InitialBuffer[AllocSize]; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 259 | BlockMeta* BlockList = nullptr; |
| 260 | |
| 261 | void grow() { |
Erik Pilkington | 64f6c6a | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 262 | char* NewMeta = static_cast<char *>(std::malloc(AllocSize)); |
| 263 | if (NewMeta == nullptr) |
| 264 | std::terminate(); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 265 | BlockList = new (NewMeta) BlockMeta{BlockList, 0}; |
| 266 | } |
| 267 | |
| 268 | void* allocateMassive(size_t NBytes) { |
| 269 | NBytes += sizeof(BlockMeta); |
Erik Pilkington | 64f6c6a | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 270 | BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes)); |
| 271 | if (NewMeta == nullptr) |
| 272 | std::terminate(); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 273 | BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0}; |
| 274 | return static_cast<void*>(NewMeta + 1); |
| 275 | } |
| 276 | |
| 277 | public: |
| 278 | BumpPointerAllocator() |
| 279 | : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {} |
| 280 | |
| 281 | void* allocate(size_t N) { |
| 282 | N = (N + 15u) & ~15u; |
| 283 | if (N + BlockList->Current >= UsableAllocSize) { |
| 284 | if (N > UsableAllocSize) |
| 285 | return allocateMassive(N); |
| 286 | grow(); |
| 287 | } |
| 288 | BlockList->Current += N; |
| 289 | return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) + |
| 290 | BlockList->Current - N); |
| 291 | } |
| 292 | |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 293 | void reset() { |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 294 | while (BlockList) { |
| 295 | BlockMeta* Tmp = BlockList; |
| 296 | BlockList = BlockList->Next; |
| 297 | if (reinterpret_cast<char*>(Tmp) != InitialBuffer) |
Erik Pilkington | 64f6c6a | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 298 | std::free(Tmp); |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 299 | } |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 300 | BlockList = new (InitialBuffer) BlockMeta{nullptr, 0}; |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 301 | } |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 302 | |
| 303 | ~BumpPointerAllocator() { reset(); } |
Erik Pilkington | 9dd63d2 | 2017-07-08 18:54:07 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
Richard Smith | 70f40ff | 2018-08-16 22:04:36 +0000 | [diff] [blame] | 306 | class DefaultAllocator { |
| 307 | BumpPointerAllocator Alloc; |
| 308 | |
| 309 | public: |
| 310 | void reset() { Alloc.reset(); } |
| 311 | |
| 312 | template<typename T, typename ...Args> T *makeNode(Args &&...args) { |
| 313 | return new (Alloc.allocate(sizeof(T))) |
| 314 | T(std::forward<Args>(args)...); |
| 315 | } |
| 316 | |
| 317 | void *allocateNodeArray(size_t sz) { |
| 318 | return Alloc.allocate(sizeof(Node *) * sz); |
| 319 | } |
| 320 | }; |
Howard Hinnant | 6c33e76 | 2013-06-17 18:10:34 +0000 | [diff] [blame] | 321 | } // unnamed namespace |
| 322 | |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 323 | //===----------------------------------------------------------------------===// |
| 324 | // Code beyond this point should not be synchronized with LLVM. |
| 325 | //===----------------------------------------------------------------------===// |
| 326 | |
Pavel Labath | 794d4c0 | 2018-10-16 14:29:14 +0000 | [diff] [blame] | 327 | using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>; |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 328 | |
| 329 | namespace { |
| 330 | enum : int { |
| 331 | demangle_invalid_args = -3, |
| 332 | demangle_invalid_mangled_name = -2, |
| 333 | demangle_memory_alloc_failure = -1, |
| 334 | demangle_success = 0, |
| 335 | }; |
| 336 | } |
| 337 | |
Erik Pilkington | 761e6b0 | 2018-01-31 20:17:06 +0000 | [diff] [blame] | 338 | namespace __cxxabiv1 { |
Saleem Abdulrasool | b4ec579 | 2015-12-04 02:14:58 +0000 | [diff] [blame] | 339 | extern "C" _LIBCXXABI_FUNC_VIS char * |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 340 | __cxa_demangle(const char *MangledName, char *Buf, size_t *N, int *Status) { |
| 341 | if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) { |
| 342 | if (Status) |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 343 | *Status = demangle_invalid_args; |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 344 | return nullptr; |
| 345 | } |
| 346 | |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 347 | int InternalStatus = demangle_success; |
Richard Smith | 29ffb8f | 2018-08-20 20:14:49 +0000 | [diff] [blame] | 348 | Demangler Parser(MangledName, MangledName + std::strlen(MangledName)); |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 349 | OutputStream S; |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 350 | |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 351 | Node *AST = Parser.parse(); |
| 352 | |
| 353 | if (AST == nullptr) |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 354 | InternalStatus = demangle_invalid_mangled_name; |
Nico Weber | 3591960 | 2018-11-11 10:09:06 +0000 | [diff] [blame] | 355 | else if (!initializeOutputStream(Buf, N, S, 1024)) |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 356 | InternalStatus = demangle_memory_alloc_failure; |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 357 | else { |
Erik Pilkington | 04f3985 | 2018-03-25 22:50:33 +0000 | [diff] [blame] | 358 | assert(Parser.ForwardTemplateRefs.empty()); |
Erik Pilkington | 0df654b | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 359 | AST->print(S); |
| 360 | S += '\0'; |
| 361 | if (N != nullptr) |
| 362 | *N = S.getCurrentPosition(); |
| 363 | Buf = S.getBuffer(); |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 364 | } |
Erik Pilkington | 0024acd | 2017-07-28 00:43:49 +0000 | [diff] [blame] | 365 | |
Erik Pilkington | f26c8d1 | 2018-03-06 14:21:10 +0000 | [diff] [blame] | 366 | if (Status) |
| 367 | *Status = InternalStatus; |
Zachary Turner | bcadcdd | 2018-07-20 17:16:49 +0000 | [diff] [blame] | 368 | return InternalStatus == demangle_success ? Buf : nullptr; |
Howard Hinnant | d213ffd | 2011-05-05 15:27:28 +0000 | [diff] [blame] | 369 | } |
Howard Hinnant | 6c33e76 | 2013-06-17 18:10:34 +0000 | [diff] [blame] | 370 | } // __cxxabiv1 |