From a6c34887f70d43d733157650d4c16a426574e868 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Thu, 16 Aug 2018 21:40:57 +0000 Subject: Factor Node creation out of the demangler. No functionality change intended. llvm-svn: 339944 --- llvm/lib/Demangle/ItaniumDemangle.cpp | 176 +++++++++++++++++++--------------- 1 file changed, 99 insertions(+), 77 deletions(-) (limited to 'llvm/lib') diff --git a/llvm/lib/Demangle/ItaniumDemangle.cpp b/llvm/lib/Demangle/ItaniumDemangle.cpp index 887e9be9b49..f8b70a09788 100644 --- a/llvm/lib/Demangle/ItaniumDemangle.cpp +++ b/llvm/lib/Demangle/ItaniumDemangle.cpp @@ -1942,6 +1942,23 @@ public: } }; +class DefaultAllocator { + BumpPointerAllocator Alloc; + +public: + void reset() { Alloc.reset(); } + + template T *makeNode(Args &&...args) { + return new (Alloc.allocate(sizeof(T))) + T(std::forward(args)...); + } + + void *allocateNodeArray(size_t sz) { + return Alloc.allocate(sizeof(Node *) * sz); + } +}; + +template struct Db { const char *First; const char *Last; @@ -1972,7 +1989,7 @@ struct Db { bool PermitForwardTemplateReferences = false; bool ParsingLambdaParams = false; - BumpPointerAllocator ASTAllocator; + Alloc ASTAllocator; Db(const char *First_, const char *Last_) : First(First_), Last(Last_) {} @@ -1989,13 +2006,12 @@ struct Db { } template T *make(Args &&... args) { - return new (ASTAllocator.allocate(sizeof(T))) - T(std::forward(args)...); + return ASTAllocator.template makeNode(std::forward(args)...); } template NodeArray makeNodeArray(It begin, It end) { size_t sz = static_cast(end - begin); - void *mem = ASTAllocator.allocate(sizeof(Node *) * sz); + void *mem = ASTAllocator.allocateNodeArray(sz); Node **data = new (mem) Node *[sz]; std::copy(begin, end, data); return NodeArray(data, sz); @@ -2132,7 +2148,7 @@ const char* parse_discriminator(const char* first, const char* last); // // ::= // ::= -Node *Db::parseName(NameState *State) { +template Node *Db::parseName(NameState *State) { consumeIf('L'); // extension if (look() == 'N') @@ -2173,7 +2189,7 @@ Node *Db::parseName(NameState *State) { // := Z E [] // := Z E s [] // := Z Ed [ ] _ -Node *Db::parseLocalName(NameState *State) { +template Node *Db::parseLocalName(NameState *State) { if (!consumeIf('Z')) return nullptr; Node *Encoding = parseEncoding(); @@ -2205,7 +2221,7 @@ Node *Db::parseLocalName(NameState *State) { // ::= // ::= St # ::std:: // extension ::= StL -Node *Db::parseUnscopedName(NameState *State) { +template Node *Db::parseUnscopedName(NameState *State) { if (consumeIf("StL") || consumeIf("St")) { Node *R = parseUnqualifiedName(State); if (R == nullptr) @@ -2220,27 +2236,28 @@ Node *Db::parseUnscopedName(NameState *State) { // ::= // ::= // ::= DC + E # structured binding declaration -Node *Db::parseUnqualifiedName(NameState *State) { - // s are special-cased in parseNestedName(). - Node *Result; - if (look() == 'U') - Result = parseUnnamedTypeName(State); - else if (look() >= '1' && look() <= '9') - Result = parseSourceName(State); - else if (consumeIf("DC")) { - size_t BindingsBegin = Names.size(); - do { - Node *Binding = parseSourceName(State); - if (Binding == nullptr) - return nullptr; - Names.push_back(Binding); - } while (!consumeIf('E')); - Result = make(popTrailingNodeArray(BindingsBegin)); - } else - Result = parseOperatorName(State); - if (Result != nullptr) - Result = parseAbiTags(Result); - return Result; +template +Node *Db::parseUnqualifiedName(NameState *State) { + // s are special-cased in parseNestedName(). + Node *Result; + if (look() == 'U') + Result = parseUnnamedTypeName(State); + else if (look() >= '1' && look() <= '9') + Result = parseSourceName(State); + else if (consumeIf("DC")) { + size_t BindingsBegin = Names.size(); + do { + Node *Binding = parseSourceName(State); + if (Binding == nullptr) + return nullptr; + Names.push_back(Binding); + } while (!consumeIf('E')); + Result = make(popTrailingNodeArray(BindingsBegin)); + } else + Result = parseOperatorName(State); + if (Result != nullptr) + Result = parseAbiTags(Result); + return Result; } // ::= Ut [] _ @@ -2249,7 +2266,7 @@ Node *Db::parseUnqualifiedName(NameState *State) { // ::= Ul E [ ] _ // // ::= + # Parameter types or "v" if the lambda has no parameters -Node *Db::parseUnnamedTypeName(NameState *) { +template Node *Db::parseUnnamedTypeName(NameState *) { if (consumeIf("Ut")) { StringView Count = parseNumber(); if (!consumeIf('_')) @@ -2278,7 +2295,7 @@ Node *Db::parseUnnamedTypeName(NameState *) { } // ::= -Node *Db::parseSourceName(NameState *) { +template Node *Db::parseSourceName(NameState *) { size_t Length = 0; if (parsePositiveInteger(&Length)) return nullptr; @@ -2342,7 +2359,7 @@ Node *Db::parseSourceName(NameState *) { // ::= rS # >>= // ::= ss # <=> C++2a // ::= v # vendor extended operator -Node *Db::parseOperatorName(NameState *State) { +template Node *Db::parseOperatorName(NameState *State) { switch (look()) { case 'a': switch (look(1)) { @@ -2585,7 +2602,8 @@ Node *Db::parseOperatorName(NameState *State) { // ::= D1 # complete object destructor // ::= D2 # base object destructor // extension ::= D5 # ? -Node *Db::parseCtorDtorName(Node *&SoFar, NameState *State) { +template +Node *Db::parseCtorDtorName(Node *&SoFar, NameState *State) { if (SoFar->K == Node::KSpecialSubstitution) { auto SSK = static_cast(SoFar)->SSK; switch (SSK) { @@ -2639,7 +2657,7 @@ Node *Db::parseCtorDtorName(Node *&SoFar, NameState *State) { // ::=