From f62c489e353b3a0aceffc6744e4c409e74596d56 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 1 Aug 2003 04:09:58 +0000 Subject: [PATCH] Factor code out into a new getAllDerivedDefinitions method, which is generally useful llvm-svn: 7461 --- .../support/tools/TableGen/CodeEmitterGen.cpp | 7 +------ llvm/support/tools/TableGen/Record.cpp | 20 +++++++++++++++++++ llvm/support/tools/TableGen/Record.h | 10 ++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/llvm/support/tools/TableGen/CodeEmitterGen.cpp b/llvm/support/tools/TableGen/CodeEmitterGen.cpp index 87f3b87dbdf9..75303c4f60d2 100644 --- a/llvm/support/tools/TableGen/CodeEmitterGen.cpp +++ b/llvm/support/tools/TableGen/CodeEmitterGen.cpp @@ -12,13 +12,8 @@ bool CodeEmitterGen::run(std::ostream &o) { std::vector Insts; const std::map &Defs = Records.getDefs(); - Record *Inst = Records.getClass("Instruction"); - assert(Inst && "Couldn't find Instruction class!"); - for (std::map::const_iterator I = Defs.begin(), - E = Defs.end(); I != E; ++I) - if (I->second->isSubClassOf(Inst)) - Insts.push_back(I->second); + Records.getAllDerivedDefinitions("Instruction", Insts); std::string Namespace = "V9::"; std::string ClassName = "SparcV9CodeEmitter::"; diff --git a/llvm/support/tools/TableGen/Record.cpp b/llvm/support/tools/TableGen/Record.cpp index a54f8e238b13..6dc409b1c2af 100644 --- a/llvm/support/tools/TableGen/Record.cpp +++ b/llvm/support/tools/TableGen/Record.cpp @@ -468,3 +468,23 @@ std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) { OS << "def " << *I->second; return OS; } + + +/// getAllDerivedDefinitions - This method returns all concrete definitions +/// that derive from the specified class name. If a class with the specified +/// name does not exist, an error is printed and true is returned. +bool RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName, + std::vector &Defs) const { + Record *Class = Records.getClass(ClassName); + if (!Class) { + std::cerr << "ERROR: Couldn't find the '" << ClassName << "' class!\n"; + return true; + } + + for (std::map::const_iterator I = getDefs().begin(), + E = getDefs().end(); I != E; ++I) + if (I->second->isSubClassOf(Class)) + Defs.push_back(I->second); + + return false; +} diff --git a/llvm/support/tools/TableGen/Record.h b/llvm/support/tools/TableGen/Record.h index cbc9cadd6659..7f703aaa9e66 100644 --- a/llvm/support/tools/TableGen/Record.h +++ b/llvm/support/tools/TableGen/Record.h @@ -628,6 +628,16 @@ public: Defs.insert(std::make_pair(R->getName(), R)); } + //===--------------------------------------------------------------------===// + // High-level helper methods, useful for tablegen backends... + + /// getAllDerivedDefinitions - This method returns all concrete definitions + /// that derive from the specified class name. If a class with the specified + /// name does not exist, an error is printed and true is returned. + bool getAllDerivedDefinitions(const std::string &ClassName, + std::vector &ReturnDefs) const; + + void dump() const; };