llvm.org GIT mirror llvm / 87bdde8
[demangler] NFC: Some refactoring to support partial demangling. I'm committing this to libcxxabi too so that the two demanglers remain as simular as possible. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@329950 91177308-0d34-0410-b5e6-96231b3b80d8 Erik Pilkington 1 year, 7 months ago
1 changed file(s) with 99 addition(s) and 44 deletion(s). Raw diff Collapse all Expand all
9595 public:
9696 OutputStream(char *StartBuf, size_t Size)
9797 : Buffer(StartBuf), CurrentPosition(0), BufferCapacity(Size) {}
98 OutputStream() = default;
99 void reset(char *Buffer_, size_t BufferCapacity_) {
100 CurrentPosition = 0;
101 Buffer = Buffer_;
102 BufferCapacity = BufferCapacity_;
103 }
98104
99105 /// If a ParameterPackExpansion (or similar type) is encountered, the offset
100106 /// into the pack that we're currently printing.
175181 KSpecialName,
176182 KCtorVtableSpecialName,
177183 KQualifiedName,
178 KEmptyName,
184 KNestedName,
185 KLocalName,
179186 KVectorType,
180187 KParameterPack,
181188 KTemplateArgumentPack,
461468 }
462469 };
463470
464 class AbiTagAttr final : public Node {
465 const Node* Base;
471 struct AbiTagAttr : Node {
472 Node *Base;
466473 StringView Tag;
467 public:
468 AbiTagAttr(const Node* Base_, StringView Tag_)
474
475 AbiTagAttr(Node* Base_, StringView Tag_)
469476 : Node(KAbiTagAttr, Base_->RHSComponentCache,
470477 Base_->ArrayCache, Base_->FunctionCache),
471478 Base(Base_), Tag(Tag_) {}
795802 };
796803
797804 class FunctionEncoding final : public Node {
798 const Node *Ret;
799 const Node *Name;
805 Node *Ret;
806 Node *Name;
800807 NodeArray Params;
801808 Node *Attrs;
802809 Qualifiers CVQuals;
811818 Ret(Ret_), Name(Name_), Params(Params_), Attrs(Attrs_),
812819 CVQuals(CVQuals_), RefQual(RefQual_) {}
813820
821 Qualifiers getCVQuals() const { return CVQuals; }
822 FunctionRefQual getRefQual() const { return RefQual; }
823 NodeArray getParams() const { return Params; }
824 Node *getReturnType() const { return Ret; }
825
814826 bool hasRHSComponentSlow(OutputStream &) const override { return true; }
815827 bool hasFunctionSlow(OutputStream &) const override { return true; }
816828
892904 }
893905 };
894906
907 struct NestedName : Node {
908 Node *Qual;
909 Node *Name;
910
911 NestedName(Node *Qual_, Node *Name_)
912 : Node(KNestedName), Qual(Qual_), Name(Name_) {}
913
914 StringView getBaseName() const override { return Name->getBaseName(); }
915
916 void printLeft(OutputStream &S) const override {
917 Qual->print(S);
918 S += "::";
919 Name->print(S);
920 }
921 };
922
923 struct LocalName : Node {
924 Node *Encoding;
925 Node *Entity;
926
927 LocalName(Node *Encoding_, Node *Entity_)
928 : Node(KLocalName), Encoding(Encoding_), Entity(Entity_) {}
929
930 void printLeft(OutputStream &S) const override {
931 Encoding->print(S);
932 S += "::";
933 Entity->print(S);
934 }
935 };
936
895937 class QualifiedName final : public Node {
896938 // qualifier::name
897939 const Node *Qualifier;
908950 S += "::";
909951 Name->print(S);
910952 }
911 };
912
913 class EmptyName : public Node {
914 public:
915 EmptyName() : Node(KEmptyName) {}
916 void printLeft(OutputStream &) const override {}
917953 };
918954
919955 class VectorType final : public Node {
11391175 }
11401176 };
11411177
1142 class NameWithTemplateArgs final : public Node {
1178 struct NameWithTemplateArgs : Node {
11431179 // name
11441180 Node *Name;
11451181 Node *TemplateArgs;
11461182
1147 public:
11481183 NameWithTemplateArgs(Node *Name_, Node *TemplateArgs_)
11491184 : Node(KNameWithTemplateArgs), Name(Name_), TemplateArgs(TemplateArgs_) {}
11501185
11711206 }
11721207 };
11731208
1174 class StdQualifiedName final : public Node {
1209 struct StdQualifiedName : Node {
11751210 Node *Child;
11761211
1177 public:
11781212 StdQualifiedName(Node *Child_) : Node(KStdQualifiedName), Child(Child_) {}
11791213
11801214 StringView getBaseName() const override { return Child->getBaseName(); }
18781912 BlockList->Current - N);
18791913 }
18801914
1881 ~BumpPointerAllocator() {
1915 void reset() {
18821916 while (BlockList) {
18831917 BlockMeta* Tmp = BlockList;
18841918 BlockList = BlockList->Next;
18851919 if (reinterpret_cast(Tmp) != InitialBuffer)
18861920 delete[] reinterpret_cast(Tmp);
18871921 }
1888 }
1922 BlockList = new (InitialBuffer) BlockMeta{nullptr, 0};
1923 }
1924
1925 ~BumpPointerAllocator() { reset(); }
18891926 };
18901927
18911928 template
20322069 BumpPointerAllocator ASTAllocator;
20332070
20342071 Db(const char *First_, const char *Last_) : First(First_), Last(Last_) {}
2072
2073 void reset(const char *First_, const char *Last_) {
2074 First = First_;
2075 Last = Last_;
2076 Names.clear();
2077 Subs.clear();
2078 TemplateParams.clear();
2079 ParsingLambdaParams = false;
2080 TryToParseTemplateArgs = true;
2081 PermitForwardTemplateReferences = false;
2082 ASTAllocator.reset();
2083 }
20352084
20362085 template T *make(Args &&... args) {
20372086 return new (ASTAllocator.allocate(sizeof(T)))
22272276
22282277 if (consumeIf('s')) {
22292278 First = parse_discriminator(First, Last);
2230 return make<QualifiedName>(Encoding, make("string literal"));
2279 return make<LocalName>(Encoding, make("string literal"));
22312280 }
22322281
22332282 if (consumeIf('d')) {
22372286 Node *N = parseName(State);
22382287 if (N == nullptr)
22392288 return nullptr;
2240 return make<QualifiedName>(Encoding, N);
2289 return make<LocalName>(Encoding, N);
22412290 }
22422291
22432292 Node *Entity = parseName(State);
22442293 if (Entity == nullptr)
22452294 return nullptr;
22462295 First = parse_discriminator(First, Last);
2247 return make<QualifiedName>(Encoding, Entity);
2296 return make<LocalName>(Encoding, Entity);
22482297 }
22492298
22502299 // ::=
27002749
27012750 Node *SoFar = nullptr;
27022751 auto PushComponent = [&](Node *Comp) {
2703 if (SoFar) SoFar = make<QualifiedName>(SoFar, Comp);
2752 if (SoFar) SoFar = make<NestedName>(SoFar, Comp);
27042753 else SoFar = Comp;
27052754 if (State) State->EndsWithTemplateArgs = false;
27062755 };
49825031 return nullptr;
49835032 return Ty;
49845033 }
5034
5035 bool initializeOutputStream(char *Buf, size_t *N, OutputStream &S,
5036 size_t InitSize) {
5037 size_t BufferSize;
5038 if (Buf == nullptr) {
5039 Buf = static_cast(std::malloc(InitSize));
5040 if (Buf == nullptr)
5041 return true;
5042 BufferSize = InitSize;
5043 } else
5044 BufferSize = *N;
5045
5046 S.reset(Buf, BufferSize);
5047 return false;
5048 }
5049
49855050 } // unnamed namespace
49865051
49875052 enum {
50005065 return nullptr;
50015066 }
50025067
5003 size_t BufSize = Buf != nullptr ? *N : 0;
50045068 int InternalStatus = success;
5005 size_t MangledNameLength = std::strlen(MangledName);
5006
5007 Db Parser(MangledName, MangledName + MangledNameLength);
5069 Db Parser(MangledName, MangledName + std::strlen(MangledName));
5070 OutputStream S;
5071
50085072 Node *AST = Parser.parse();
50095073
50105074 if (AST == nullptr)
50115075 InternalStatus = invalid_mangled_name;
5012
5013 if (InternalStatus == success) {
5076 else if (initializeOutputStream(Buf, N, S, 1024))
5077 InternalStatus = memory_alloc_failure;
5078 else {
50145079 assert(Parser.ForwardTemplateRefs.empty());
5015
5016 if (Buf == nullptr) {
5017 BufSize = 1024;
5018 Buf = static_cast(std::malloc(BufSize));
5019 }
5020
5021 if (Buf) {
5022 OutputStream Stream(Buf, BufSize);
5023 AST->print(Stream);
5024 Stream += '\0';
5025 if (N != nullptr)
5026 *N = Stream.getCurrentPosition();
5027 Buf = Stream.getBuffer();
5028 } else
5029 InternalStatus = memory_alloc_failure;
5080 AST->print(S);
5081 S += '\0';
5082 if (N != nullptr)
5083 *N = S.getCurrentPosition();
5084 Buf = S.getBuffer();
50305085 }
50315086
50325087 if (Status)