diff options
Diffstat (limited to 'contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp')
-rw-r--r-- | contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp | 103 |
1 files changed, 62 insertions, 41 deletions
diff --git a/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp b/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp index 16a1c74b9d91..07ddafce3291 100644 --- a/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp +++ b/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp @@ -33,6 +33,7 @@ class ClangASTNodesEmitter { typedef std::multimap<ASTNode, ASTNode> ChildMap; typedef ChildMap::const_iterator ChildIterator; + std::set<ASTNode> PrioritizedClasses; RecordKeeper &Records; ASTNode Root; const std::string &NodeClassName; @@ -70,8 +71,16 @@ class ClangASTNodesEmitter { std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base); public: explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N, - const std::string &S) - : Records(R), NodeClassName(N), BaseSuffix(S) {} + const std::string &S, + std::string_view PriorizeIfSubclassOf) + : Records(R), NodeClassName(N), BaseSuffix(S) { + auto vecPrioritized = + PriorizeIfSubclassOf.empty() + ? std::vector<Record *>{} + : R.getAllDerivedDefinitions(PriorizeIfSubclassOf); + PrioritizedClasses = + std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end()); + } // run - Output the .inc file contents void run(raw_ostream &OS); @@ -95,8 +104,23 @@ std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS, if (!Base.isAbstract()) First = Last = Base; + auto comp = [this](ASTNode LHS, ASTNode RHS) { + auto LHSPrioritized = PrioritizedClasses.count(LHS) > 0; + auto RHSPrioritized = PrioritizedClasses.count(RHS) > 0; + if (LHSPrioritized && !RHSPrioritized) + return true; + if (!LHSPrioritized && RHSPrioritized) + return false; + + return LHS.getName() > RHS.getName(); + }; + auto SortedChildren = std::set<ASTNode, decltype(comp)>(comp); + for (; i != e; ++i) { - ASTNode Child = i->second; + SortedChildren.insert(i->second); + } + + for (const auto &Child : SortedChildren) { bool Abstract = Child.isAbstract(); std::string NodeName = macroName(std::string(Child.getName())); @@ -148,9 +172,7 @@ void ClangASTNodesEmitter::deriveChildTree() { const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions(NodeClassName); - for (unsigned i = 0, e = Stmts.size(); i != e; ++i) { - Record *R = Stmts[i]; - + for (auto *R : Stmts) { if (auto B = R->getValueAsOptionalDef(BaseFieldName)) Tree.insert(std::make_pair(B, R)); else if (Root) @@ -182,9 +204,9 @@ void ClangASTNodesEmitter::run(raw_ostream &OS) { OS << "#endif\n\n"; OS << "#ifndef LAST_" << macroHierarchyName() << "_RANGE\n"; - OS << "# define LAST_" - << macroHierarchyName() << "_RANGE(Base, First, Last) " - << macroHierarchyName() << "_RANGE(Base, First, Last)\n"; + OS << "# define LAST_" << macroHierarchyName() + << "_RANGE(Base, First, Last) " << macroHierarchyName() + << "_RANGE(Base, First, Last)\n"; OS << "#endif\n\n"; EmitNode(OS, Root); @@ -196,8 +218,20 @@ void ClangASTNodesEmitter::run(raw_ostream &OS) { } void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS, - const std::string &N, const std::string &S) { - ClangASTNodesEmitter(RK, N, S).run(OS); + const std::string &N, const std::string &S, + std::string_view PriorizeIfSubclassOf) { + ClangASTNodesEmitter(RK, N, S, PriorizeIfSubclassOf).run(OS); +} + +void printDeclContext(const std::multimap<Record *, Record *> &Tree, + Record *DeclContext, raw_ostream &OS) { + if (!DeclContext->getValueAsBit(AbstractFieldName)) + OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n"; + auto i = Tree.lower_bound(DeclContext); + auto end = Tree.upper_bound(DeclContext); + for (; i != end; ++i) { + printDeclContext(Tree, i->second, OS); + } } // Emits and addendum to a .inc file to enumerate the clang declaration @@ -210,38 +244,25 @@ void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) { OS << "#ifndef DECL_CONTEXT\n"; OS << "# define DECL_CONTEXT(DECL)\n"; OS << "#endif\n"; - - OS << "#ifndef DECL_CONTEXT_BASE\n"; - OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n"; - OS << "#endif\n"; - - typedef std::set<Record*> RecordSet; - typedef std::vector<Record*> RecordVector; - - RecordVector DeclContextsVector - = Records.getAllDerivedDefinitions(DeclContextNodeClassName); - RecordVector Decls = Records.getAllDerivedDefinitions(DeclNodeClassName); - RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end()); - - for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) { - Record *R = *i; - - if (Record *B = R->getValueAsOptionalDef(BaseFieldName)) { - if (DeclContexts.find(B) != DeclContexts.end()) { - OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n"; - DeclContexts.erase(B); - } - } + + std::vector<Record *> DeclContextsVector = + Records.getAllDerivedDefinitions(DeclContextNodeClassName); + std::vector<Record *> Decls = + Records.getAllDerivedDefinitions(DeclNodeClassName); + + std::multimap<Record *, Record *> Tree; + + const std::vector<Record *> Stmts = + Records.getAllDerivedDefinitions(DeclNodeClassName); + + for (auto *R : Stmts) { + if (auto *B = R->getValueAsOptionalDef(BaseFieldName)) + Tree.insert(std::make_pair(B, R)); } - // To keep identical order, RecordVector may be used - // instead of RecordSet. - for (RecordVector::iterator - i = DeclContextsVector.begin(), e = DeclContextsVector.end(); - i != e; ++i) - if (DeclContexts.find(*i) != DeclContexts.end()) - OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n"; + for (auto *DeclContext : DeclContextsVector) { + printDeclContext(Tree, DeclContext, OS); + } OS << "#undef DECL_CONTEXT\n"; - OS << "#undef DECL_CONTEXT_BASE\n"; } |