Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 1 | //===------------------------- ItaniumDemangle.cpp ------------------------===// |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 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 | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 10 | // FIXME: (possibly) incomplete list of features that clang mangles that this |
| 11 | // file does not yet support: |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 12 | // - C++ modules TS |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 13 | |
Reid Kleckner | b2881f1 | 2016-09-06 19:39:56 +0000 | [diff] [blame] | 14 | #include "llvm/Demangle/Demangle.h" |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 15 | #include "llvm/Demangle/ItaniumDemangle.h" |
Serge Pavlov | 1a09552 | 2018-05-29 07:05:41 +0000 | [diff] [blame] | 16 | |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 17 | #include <cassert> |
David Blaikie | 99e1725 | 2018-03-21 17:31:49 +0000 | [diff] [blame] | 18 | #include <cctype> |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 19 | #include <cstdio> |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 20 | #include <cstdlib> |
| 21 | #include <cstring> |
David Blaikie | a25e2069 | 2018-08-20 20:02:29 +0000 | [diff] [blame] | 22 | #include <functional> |
David Blaikie | 99e1725 | 2018-03-21 17:31:49 +0000 | [diff] [blame] | 23 | #include <numeric> |
Erik Pilkington | 3a6fed4 | 2018-07-27 17:27:40 +0000 | [diff] [blame] | 24 | #include <utility> |
David Blaikie | 99e1725 | 2018-03-21 17:31:49 +0000 | [diff] [blame] | 25 | #include <vector> |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 26 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | using namespace llvm::itanium_demangle; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 29 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 30 | constexpr const char *itanium_demangle::FloatData<float>::spec; |
| 31 | constexpr const char *itanium_demangle::FloatData<double>::spec; |
| 32 | constexpr const char *itanium_demangle::FloatData<long double>::spec; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 33 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 34 | // <discriminator> := _ <non-negative number> # when number < 10 |
| 35 | // := __ <non-negative number> _ # when number >= 10 |
| 36 | // extension := decimal-digit+ # at the end of string |
| 37 | const char *itanium_demangle::parse_discriminator(const char *first, |
| 38 | const char *last) { |
| 39 | // parse but ignore discriminator |
| 40 | if (first != last) { |
| 41 | if (*first == '_') { |
| 42 | const char *t1 = first + 1; |
| 43 | if (t1 != last) { |
| 44 | if (std::isdigit(*t1)) |
| 45 | first = t1 + 1; |
| 46 | else if (*t1 == '_') { |
| 47 | for (++t1; t1 != last && std::isdigit(*t1); ++t1) |
| 48 | ; |
| 49 | if (t1 != last && *t1 == '_') |
| 50 | first = t1 + 1; |
| 51 | } |
Erik Pilkington | 8c7013d | 2018-03-25 22:49:57 +0000 | [diff] [blame] | 52 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 53 | } else if (std::isdigit(*first)) { |
| 54 | const char *t1 = first + 1; |
| 55 | for (; t1 != last && std::isdigit(*t1); ++t1) |
| 56 | ; |
| 57 | if (t1 == last) |
| 58 | first = last; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 59 | } |
| 60 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 61 | return first; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 64 | #ifndef NDEBUG |
| 65 | namespace { |
| 66 | struct DumpVisitor { |
| 67 | unsigned Depth = 0; |
| 68 | bool PendingNewline = false; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 69 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 70 | template<typename NodeT> static constexpr bool wantsNewline(const NodeT *) { |
| 71 | return true; |
| 72 | } |
| 73 | static bool wantsNewline(NodeArray A) { return !A.empty(); } |
| 74 | static constexpr bool wantsNewline(...) { return false; } |
| 75 | |
| 76 | template<typename ...Ts> static bool anyWantNewline(Ts ...Vs) { |
| 77 | for (bool B : {wantsNewline(Vs)...}) |
| 78 | if (B) |
| 79 | return true; |
| 80 | return false; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 83 | void printStr(const char *S) { fprintf(stderr, "%s", S); } |
| 84 | void print(StringView SV) { |
| 85 | fprintf(stderr, "\"%.*s\"", (int)SV.size(), SV.begin()); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 86 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 87 | void print(const Node *N) { |
| 88 | if (N) |
| 89 | N->visit(std::ref(*this)); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 90 | else |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 91 | printStr("<null>"); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 92 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 93 | void print(NodeOrString NS) { |
| 94 | if (NS.isNode()) |
| 95 | print(NS.asNode()); |
| 96 | else if (NS.isString()) |
| 97 | print(NS.asString()); |
| 98 | else |
| 99 | printStr("NodeOrString()"); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 100 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 101 | void print(NodeArray A) { |
| 102 | ++Depth; |
| 103 | printStr("{"); |
| 104 | bool First = true; |
| 105 | for (const Node *N : A) { |
| 106 | if (First) |
| 107 | print(N); |
| 108 | else |
| 109 | printWithComma(N); |
| 110 | First = false; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 111 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 112 | printStr("}"); |
| 113 | --Depth; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 114 | } |
Erik Pilkington | fbca8d5 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 115 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 116 | // Overload used when T is exactly 'bool', not merely convertible to 'bool'. |
Erik Pilkington | fbca8d5 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 117 | void print(bool B) { printStr(B ? "true" : "false"); } |
| 118 | |
| 119 | template <class T> |
| 120 | typename std::enable_if<std::is_unsigned<T>::value>::type print(T N) { |
| 121 | fprintf(stderr, "%llu", (unsigned long long)N); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 122 | } |
Erik Pilkington | fbca8d5 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 123 | |
| 124 | template <class T> |
| 125 | typename std::enable_if<std::is_signed<T>::value>::type print(T N) { |
| 126 | fprintf(stderr, "%lld", (long long)N); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 127 | } |
Erik Pilkington | fbca8d5 | 2018-10-15 22:03:53 +0000 | [diff] [blame] | 128 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 129 | void print(ReferenceKind RK) { |
| 130 | switch (RK) { |
| 131 | case ReferenceKind::LValue: |
| 132 | return printStr("ReferenceKind::LValue"); |
| 133 | case ReferenceKind::RValue: |
| 134 | return printStr("ReferenceKind::RValue"); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 137 | void print(FunctionRefQual RQ) { |
| 138 | switch (RQ) { |
| 139 | case FunctionRefQual::FrefQualNone: |
| 140 | return printStr("FunctionRefQual::FrefQualNone"); |
| 141 | case FunctionRefQual::FrefQualLValue: |
| 142 | return printStr("FunctionRefQual::FrefQualLValue"); |
| 143 | case FunctionRefQual::FrefQualRValue: |
| 144 | return printStr("FunctionRefQual::FrefQualRValue"); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 147 | void print(Qualifiers Qs) { |
| 148 | if (!Qs) return printStr("QualNone"); |
| 149 | struct QualName { Qualifiers Q; const char *Name; } Names[] = { |
| 150 | {QualConst, "QualConst"}, |
| 151 | {QualVolatile, "QualVolatile"}, |
| 152 | {QualRestrict, "QualRestrict"}, |
Erik Pilkington | d43931d | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 153 | }; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 154 | for (QualName Name : Names) { |
| 155 | if (Qs & Name.Q) { |
| 156 | printStr(Name.Name); |
| 157 | Qs = Qualifiers(Qs & ~Name.Q); |
| 158 | if (Qs) printStr(" | "); |
Erik Pilkington | d43931d | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
Erik Pilkington | d43931d | 2018-04-09 18:33:01 +0000 | [diff] [blame] | 161 | } |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 162 | void print(SpecialSubKind SSK) { |
| 163 | switch (SSK) { |
| 164 | case SpecialSubKind::allocator: |
| 165 | return printStr("SpecialSubKind::allocator"); |
| 166 | case SpecialSubKind::basic_string: |
| 167 | return printStr("SpecialSubKind::basic_string"); |
| 168 | case SpecialSubKind::string: |
| 169 | return printStr("SpecialSubKind::string"); |
| 170 | case SpecialSubKind::istream: |
| 171 | return printStr("SpecialSubKind::istream"); |
| 172 | case SpecialSubKind::ostream: |
| 173 | return printStr("SpecialSubKind::ostream"); |
| 174 | case SpecialSubKind::iostream: |
| 175 | return printStr("SpecialSubKind::iostream"); |
| 176 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 177 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 178 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 179 | void newLine() { |
| 180 | printStr("\n"); |
| 181 | for (unsigned I = 0; I != Depth; ++I) |
| 182 | printStr(" "); |
| 183 | PendingNewline = false; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 184 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 185 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 186 | template<typename T> void printWithPendingNewline(T V) { |
| 187 | print(V); |
| 188 | if (wantsNewline(V)) |
| 189 | PendingNewline = true; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 190 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 191 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 192 | template<typename T> void printWithComma(T V) { |
| 193 | if (PendingNewline || wantsNewline(V)) { |
| 194 | printStr(","); |
| 195 | newLine(); |
| 196 | } else { |
| 197 | printStr(", "); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 200 | printWithPendingNewline(V); |
| 201 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 202 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 203 | struct CtorArgPrinter { |
| 204 | DumpVisitor &Visitor; |
| 205 | |
| 206 | template<typename T, typename ...Rest> void operator()(T V, Rest ...Vs) { |
| 207 | if (Visitor.anyWantNewline(V, Vs...)) |
| 208 | Visitor.newLine(); |
| 209 | Visitor.printWithPendingNewline(V); |
| 210 | int PrintInOrder[] = { (Visitor.printWithComma(Vs), 0)..., 0 }; |
| 211 | (void)PrintInOrder; |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | template<typename NodeT> void operator()(const NodeT *Node) { |
| 216 | Depth += 2; |
| 217 | fprintf(stderr, "%s(", itanium_demangle::NodeKind<NodeT>::name()); |
| 218 | Node->match(CtorArgPrinter{*this}); |
| 219 | fprintf(stderr, ")"); |
| 220 | Depth -= 2; |
| 221 | } |
| 222 | |
| 223 | void operator()(const ForwardTemplateReference *Node) { |
| 224 | Depth += 2; |
| 225 | fprintf(stderr, "ForwardTemplateReference("); |
| 226 | if (Node->Ref && !Node->Printing) { |
| 227 | Node->Printing = true; |
| 228 | CtorArgPrinter{*this}(Node->Ref); |
| 229 | Node->Printing = false; |
| 230 | } else { |
| 231 | CtorArgPrinter{*this}(Node->Index); |
| 232 | } |
| 233 | fprintf(stderr, ")"); |
| 234 | Depth -= 2; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 235 | } |
| 236 | }; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 237 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 238 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 239 | void itanium_demangle::Node::dump() const { |
| 240 | DumpVisitor V; |
| 241 | visit(std::ref(V)); |
| 242 | V.newLine(); |
| 243 | } |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 244 | #endif |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 245 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 246 | namespace { |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 247 | class BumpPointerAllocator { |
| 248 | struct BlockMeta { |
| 249 | BlockMeta* Next; |
| 250 | size_t Current; |
| 251 | }; |
| 252 | |
| 253 | static constexpr size_t AllocSize = 4096; |
| 254 | static constexpr size_t UsableAllocSize = AllocSize - sizeof(BlockMeta); |
| 255 | |
Serge Pavlov | 892bd81 | 2018-07-05 06:22:39 +0000 | [diff] [blame] | 256 | alignas(long double) char InitialBuffer[AllocSize]; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 257 | BlockMeta* BlockList = nullptr; |
| 258 | |
| 259 | void grow() { |
Erik Pilkington | 28e08a0 | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 260 | char* NewMeta = static_cast<char *>(std::malloc(AllocSize)); |
| 261 | if (NewMeta == nullptr) |
| 262 | std::terminate(); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 263 | BlockList = new (NewMeta) BlockMeta{BlockList, 0}; |
| 264 | } |
| 265 | |
| 266 | void* allocateMassive(size_t NBytes) { |
| 267 | NBytes += sizeof(BlockMeta); |
Erik Pilkington | 28e08a0 | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 268 | BlockMeta* NewMeta = reinterpret_cast<BlockMeta*>(std::malloc(NBytes)); |
| 269 | if (NewMeta == nullptr) |
| 270 | std::terminate(); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 271 | BlockList->Next = new (NewMeta) BlockMeta{BlockList->Next, 0}; |
| 272 | return static_cast<void*>(NewMeta + 1); |
| 273 | } |
| 274 | |
| 275 | public: |
| 276 | BumpPointerAllocator() |
| 277 | : BlockList(new (InitialBuffer) BlockMeta{nullptr, 0}) {} |
| 278 | |
| 279 | void* allocate(size_t N) { |
| 280 | N = (N + 15u) & ~15u; |
| 281 | if (N + BlockList->Current >= UsableAllocSize) { |
| 282 | if (N > UsableAllocSize) |
| 283 | return allocateMassive(N); |
| 284 | grow(); |
| 285 | } |
| 286 | BlockList->Current += N; |
| 287 | return static_cast<void*>(reinterpret_cast<char*>(BlockList + 1) + |
| 288 | BlockList->Current - N); |
| 289 | } |
| 290 | |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 291 | void reset() { |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 292 | while (BlockList) { |
| 293 | BlockMeta* Tmp = BlockList; |
| 294 | BlockList = BlockList->Next; |
| 295 | if (reinterpret_cast<char*>(Tmp) != InitialBuffer) |
Erik Pilkington | 28e08a0 | 2018-07-23 22:23:04 +0000 | [diff] [blame] | 296 | std::free(Tmp); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 297 | } |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 298 | BlockList = new (InitialBuffer) BlockMeta{nullptr, 0}; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 299 | } |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 300 | |
| 301 | ~BumpPointerAllocator() { reset(); } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Richard Smith | a6c3488 | 2018-08-16 21:40:57 +0000 | [diff] [blame] | 304 | class DefaultAllocator { |
| 305 | BumpPointerAllocator Alloc; |
| 306 | |
| 307 | public: |
| 308 | void reset() { Alloc.reset(); } |
| 309 | |
| 310 | template<typename T, typename ...Args> T *makeNode(Args &&...args) { |
| 311 | return new (Alloc.allocate(sizeof(T))) |
| 312 | T(std::forward<Args>(args)...); |
| 313 | } |
| 314 | |
| 315 | void *allocateNodeArray(size_t sz) { |
| 316 | return Alloc.allocate(sizeof(Node *) * sz); |
| 317 | } |
| 318 | }; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 319 | } // unnamed namespace |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 320 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 321 | //===----------------------------------------------------------------------===// |
| 322 | // Code beyond this point should not be synchronized with libc++abi. |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | |
Pavel Labath | f4c1582 | 2018-10-17 18:50:25 +0000 | [diff] [blame] | 325 | using Demangler = itanium_demangle::ManglingParser<DefaultAllocator>; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 326 | |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 327 | char *llvm::itaniumDemangle(const char *MangledName, char *Buf, |
| 328 | size_t *N, int *Status) { |
| 329 | if (MangledName == nullptr || (Buf != nullptr && N == nullptr)) { |
| 330 | if (Status) |
Zachary Turner | 8a0efd0 | 2018-07-17 19:42:29 +0000 | [diff] [blame] | 331 | *Status = demangle_invalid_args; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 332 | return nullptr; |
| 333 | } |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 334 | |
Zachary Turner | 8a0efd0 | 2018-07-17 19:42:29 +0000 | [diff] [blame] | 335 | int InternalStatus = demangle_success; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 336 | Demangler Parser(MangledName, MangledName + std::strlen(MangledName)); |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 337 | OutputStream S; |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 338 | |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 339 | Node *AST = Parser.parse(); |
| 340 | |
| 341 | if (AST == nullptr) |
Zachary Turner | 8a0efd0 | 2018-07-17 19:42:29 +0000 | [diff] [blame] | 342 | InternalStatus = demangle_invalid_mangled_name; |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 343 | else if (initializeOutputStream(Buf, N, S, 1024)) |
Zachary Turner | 8a0efd0 | 2018-07-17 19:42:29 +0000 | [diff] [blame] | 344 | InternalStatus = demangle_memory_alloc_failure; |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 345 | else { |
Erik Pilkington | 8a1cb33 | 2018-03-25 22:50:33 +0000 | [diff] [blame] | 346 | assert(Parser.ForwardTemplateRefs.empty()); |
Erik Pilkington | f2a9b0f | 2018-04-12 20:41:06 +0000 | [diff] [blame] | 347 | AST->print(S); |
| 348 | S += '\0'; |
| 349 | if (N != nullptr) |
| 350 | *N = S.getCurrentPosition(); |
| 351 | Buf = S.getBuffer(); |
Erik Pilkington | bb7feae | 2018-03-19 15:18:23 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | if (Status) |
| 355 | *Status = InternalStatus; |
Zachary Turner | 8a0efd0 | 2018-07-17 19:42:29 +0000 | [diff] [blame] | 356 | return InternalStatus == demangle_success ? Buf : nullptr; |
Rafael Espindola | b940b66 | 2016-09-06 19:16:48 +0000 | [diff] [blame] | 357 | } |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 358 | |
Erik Pilkington | ac6a801c | 2018-08-13 16:37:47 +0000 | [diff] [blame] | 359 | bool llvm::itaniumFindTypesInMangledName(const char *MangledName, void *Ctx, |
| 360 | void (*Callback)(void *, |
| 361 | const char *)) { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 362 | Demangler Parser(MangledName, MangledName + std::strlen(MangledName)); |
Erik Pilkington | ac6a801c | 2018-08-13 16:37:47 +0000 | [diff] [blame] | 363 | Parser.TypeCallback = Callback; |
| 364 | Parser.TypeCallbackContext = Ctx; |
| 365 | return Parser.parse() == nullptr; |
| 366 | } |
| 367 | |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 368 | ItaniumPartialDemangler::ItaniumPartialDemangler() |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 369 | : RootNode(nullptr), Context(new Demangler{nullptr, nullptr}) {} |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 370 | |
| 371 | ItaniumPartialDemangler::~ItaniumPartialDemangler() { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 372 | delete static_cast<Demangler *>(Context); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | ItaniumPartialDemangler::ItaniumPartialDemangler( |
| 376 | ItaniumPartialDemangler &&Other) |
| 377 | : RootNode(Other.RootNode), Context(Other.Context) { |
| 378 | Other.Context = Other.RootNode = nullptr; |
| 379 | } |
| 380 | |
| 381 | ItaniumPartialDemangler &ItaniumPartialDemangler:: |
| 382 | operator=(ItaniumPartialDemangler &&Other) { |
| 383 | std::swap(RootNode, Other.RootNode); |
| 384 | std::swap(Context, Other.Context); |
| 385 | return *this; |
| 386 | } |
| 387 | |
| 388 | // Demangle MangledName into an AST, storing it into this->RootNode. |
| 389 | bool ItaniumPartialDemangler::partialDemangle(const char *MangledName) { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 390 | Demangler *Parser = static_cast<Demangler *>(Context); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 391 | size_t Len = std::strlen(MangledName); |
| 392 | Parser->reset(MangledName, MangledName + Len); |
| 393 | RootNode = Parser->parse(); |
| 394 | return RootNode == nullptr; |
| 395 | } |
| 396 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 397 | static char *printNode(const Node *RootNode, char *Buf, size_t *N) { |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 398 | OutputStream S; |
| 399 | if (initializeOutputStream(Buf, N, S, 128)) |
| 400 | return nullptr; |
| 401 | RootNode->print(S); |
| 402 | S += '\0'; |
| 403 | if (N != nullptr) |
| 404 | *N = S.getCurrentPosition(); |
| 405 | return S.getBuffer(); |
| 406 | } |
| 407 | |
| 408 | char *ItaniumPartialDemangler::getFunctionBaseName(char *Buf, size_t *N) const { |
| 409 | if (!isFunction()) |
| 410 | return nullptr; |
| 411 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 412 | const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName(); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 413 | |
| 414 | while (true) { |
| 415 | switch (Name->getKind()) { |
| 416 | case Node::KAbiTagAttr: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 417 | Name = static_cast<const AbiTagAttr *>(Name)->Base; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 418 | continue; |
| 419 | case Node::KStdQualifiedName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 420 | Name = static_cast<const StdQualifiedName *>(Name)->Child; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 421 | continue; |
| 422 | case Node::KNestedName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 423 | Name = static_cast<const NestedName *>(Name)->Name; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 424 | continue; |
| 425 | case Node::KLocalName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 426 | Name = static_cast<const LocalName *>(Name)->Entity; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 427 | continue; |
| 428 | case Node::KNameWithTemplateArgs: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 429 | Name = static_cast<const NameWithTemplateArgs *>(Name)->Name; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 430 | continue; |
| 431 | default: |
| 432 | return printNode(Name, Buf, N); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | char *ItaniumPartialDemangler::getFunctionDeclContextName(char *Buf, |
| 438 | size_t *N) const { |
| 439 | if (!isFunction()) |
| 440 | return nullptr; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 441 | const Node *Name = static_cast<const FunctionEncoding *>(RootNode)->getName(); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 442 | |
| 443 | OutputStream S; |
| 444 | if (initializeOutputStream(Buf, N, S, 128)) |
| 445 | return nullptr; |
| 446 | |
| 447 | KeepGoingLocalFunction: |
| 448 | while (true) { |
| 449 | if (Name->getKind() == Node::KAbiTagAttr) { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 450 | Name = static_cast<const AbiTagAttr *>(Name)->Base; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 451 | continue; |
| 452 | } |
| 453 | if (Name->getKind() == Node::KNameWithTemplateArgs) { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 454 | Name = static_cast<const NameWithTemplateArgs *>(Name)->Name; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 455 | continue; |
| 456 | } |
| 457 | break; |
| 458 | } |
| 459 | |
| 460 | switch (Name->getKind()) { |
| 461 | case Node::KStdQualifiedName: |
| 462 | S += "std"; |
| 463 | break; |
| 464 | case Node::KNestedName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 465 | static_cast<const NestedName *>(Name)->Qual->print(S); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 466 | break; |
| 467 | case Node::KLocalName: { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 468 | auto *LN = static_cast<const LocalName *>(Name); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 469 | LN->Encoding->print(S); |
| 470 | S += "::"; |
| 471 | Name = LN->Entity; |
| 472 | goto KeepGoingLocalFunction; |
| 473 | } |
| 474 | default: |
| 475 | break; |
| 476 | } |
| 477 | S += '\0'; |
| 478 | if (N != nullptr) |
| 479 | *N = S.getCurrentPosition(); |
| 480 | return S.getBuffer(); |
| 481 | } |
| 482 | |
| 483 | char *ItaniumPartialDemangler::getFunctionName(char *Buf, size_t *N) const { |
| 484 | if (!isFunction()) |
| 485 | return nullptr; |
| 486 | auto *Name = static_cast<FunctionEncoding *>(RootNode)->getName(); |
| 487 | return printNode(Name, Buf, N); |
| 488 | } |
| 489 | |
| 490 | char *ItaniumPartialDemangler::getFunctionParameters(char *Buf, |
| 491 | size_t *N) const { |
| 492 | if (!isFunction()) |
| 493 | return nullptr; |
| 494 | NodeArray Params = static_cast<FunctionEncoding *>(RootNode)->getParams(); |
| 495 | |
| 496 | OutputStream S; |
| 497 | if (initializeOutputStream(Buf, N, S, 128)) |
| 498 | return nullptr; |
| 499 | |
| 500 | S += '('; |
| 501 | Params.printWithComma(S); |
| 502 | S += ')'; |
| 503 | S += '\0'; |
| 504 | if (N != nullptr) |
| 505 | *N = S.getCurrentPosition(); |
| 506 | return S.getBuffer(); |
| 507 | } |
| 508 | |
| 509 | char *ItaniumPartialDemangler::getFunctionReturnType( |
| 510 | char *Buf, size_t *N) const { |
| 511 | if (!isFunction()) |
| 512 | return nullptr; |
| 513 | |
| 514 | OutputStream S; |
| 515 | if (initializeOutputStream(Buf, N, S, 128)) |
| 516 | return nullptr; |
| 517 | |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 518 | if (const Node *Ret = |
| 519 | static_cast<const FunctionEncoding *>(RootNode)->getReturnType()) |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 520 | Ret->print(S); |
| 521 | |
| 522 | S += '\0'; |
| 523 | if (N != nullptr) |
| 524 | *N = S.getCurrentPosition(); |
| 525 | return S.getBuffer(); |
| 526 | } |
| 527 | |
| 528 | char *ItaniumPartialDemangler::finishDemangle(char *Buf, size_t *N) const { |
| 529 | assert(RootNode != nullptr && "must call partialDemangle()"); |
| 530 | return printNode(static_cast<Node *>(RootNode), Buf, N); |
| 531 | } |
| 532 | |
| 533 | bool ItaniumPartialDemangler::hasFunctionQualifiers() const { |
| 534 | assert(RootNode != nullptr && "must call partialDemangle()"); |
| 535 | if (!isFunction()) |
| 536 | return false; |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 537 | auto *E = static_cast<const FunctionEncoding *>(RootNode); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 538 | return E->getCVQuals() != QualNone || E->getRefQual() != FrefQualNone; |
| 539 | } |
| 540 | |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 541 | bool ItaniumPartialDemangler::isCtorOrDtor() const { |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 542 | const Node *N = static_cast<const Node *>(RootNode); |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 543 | while (N) { |
| 544 | switch (N->getKind()) { |
| 545 | default: |
| 546 | return false; |
| 547 | case Node::KCtorDtorName: |
| 548 | return true; |
| 549 | |
| 550 | case Node::KAbiTagAttr: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 551 | N = static_cast<const AbiTagAttr *>(N)->Base; |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 552 | break; |
| 553 | case Node::KFunctionEncoding: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 554 | N = static_cast<const FunctionEncoding *>(N)->getName(); |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 555 | break; |
| 556 | case Node::KLocalName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 557 | N = static_cast<const LocalName *>(N)->Entity; |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 558 | break; |
| 559 | case Node::KNameWithTemplateArgs: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 560 | N = static_cast<const NameWithTemplateArgs *>(N)->Name; |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 561 | break; |
| 562 | case Node::KNestedName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 563 | N = static_cast<const NestedName *>(N)->Name; |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 564 | break; |
| 565 | case Node::KStdQualifiedName: |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 566 | N = static_cast<const StdQualifiedName *>(N)->Child; |
Fangrui Song | 79420ac | 2018-05-24 06:57:57 +0000 | [diff] [blame] | 567 | break; |
| 568 | } |
| 569 | } |
| 570 | return false; |
| 571 | } |
| 572 | |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 573 | bool ItaniumPartialDemangler::isFunction() const { |
| 574 | assert(RootNode != nullptr && "must call partialDemangle()"); |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 575 | return static_cast<const Node *>(RootNode)->getKind() == |
| 576 | Node::KFunctionEncoding; |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | bool ItaniumPartialDemangler::isSpecialName() const { |
| 580 | assert(RootNode != nullptr && "must call partialDemangle()"); |
Richard Smith | 8a57f2e | 2018-08-20 19:44:01 +0000 | [diff] [blame] | 581 | auto K = static_cast<const Node *>(RootNode)->getKind(); |
Erik Pilkington | 67d82d6 | 2018-04-12 20:41:38 +0000 | [diff] [blame] | 582 | return K == Node::KSpecialName || K == Node::KCtorVtableSpecialName; |
| 583 | } |
| 584 | |
| 585 | bool ItaniumPartialDemangler::isData() const { |
| 586 | return !isFunction() && !isSpecialName(); |
| 587 | } |