Switch over to an exception handling model for "high-level" requests.
authorChris Lattner <sabre@nondot.org>
Fri, 1 Aug 2003 04:37:57 +0000 (04:37 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 1 Aug 2003 04:37:57 +0000 (04:37 +0000)
Add new getValueAsString method.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@7463 91177308-0d34-0410-b5e6-96231b3b80d8

support/tools/TableGen/Record.cpp
support/tools/TableGen/Record.h
utils/TableGen/Record.cpp
utils/TableGen/Record.h

index 6dc409b1c2afc399b95b8e414d1ac43828848f48..f282e4b0d6055f1c468d2ea9214c9f67c9d2b47b 100644 (file)
@@ -452,6 +452,23 @@ std::ostream &operator<<(std::ostream &OS, const Record &R) {
   return OS << "}\n";
 }
 
+/// getValueAsString - This method looks up the specified field and returns its
+/// value as a string, throwing an exception if the field does not exist or if
+/// the value is not a string.
+///
+std::string Record::getValueAsString(const std::string &FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (R == 0 || R->getValue() == 0)
+    throw "Record '" + R->getName() + "' does not have a field named '" +
+          FieldName + "!\n";
+
+  if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
+    return SI->getValue();
+  throw "Record '" + R->getName() + "', field '" + FieldName +
+        "' does not have a string initializer!";
+}
+
+
 void RecordKeeper::dump() const { std::cerr << *this; }
 
 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
@@ -473,18 +490,17 @@ std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
 /// 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<Record*> &Defs) const {
+std::vector<Record*>
+RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
   Record *Class = Records.getClass(ClassName);
-  if (!Class) {
-    std::cerr << "ERROR: Couldn't find the '" << ClassName << "' class!\n";
-    return true;
-  }
+  if (!Class)
+    throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
 
+  std::vector<Record*> Defs;
   for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
          E = getDefs().end(); I != E; ++I)
     if (I->second->isSubClassOf(Class))
       Defs.push_back(I->second);
 
-  return false;
+  return Defs;
 }
index 7f703aaa9e665a469f81985b79d20ab0373a3330..467f12a6ab368ba3a86735da11294427831ef21e 100644 (file)
@@ -319,6 +319,8 @@ class StringInit : public Init {
 public:
   StringInit(const std::string &V) : Value(V) {}
 
+  const std::string &getValue() const { return Value; }
+
   virtual Init *convertInitializerTo(RecTy *Ty) {
     return Ty->convertValue(this);
   }
@@ -592,6 +594,17 @@ public:
   void resolveReferences();
 
   void dump() const;
+
+  //===--------------------------------------------------------------------===//
+  // High-level methods useful to tablegen back-ends
+  //
+
+  /// getValueAsString - This method looks up the specified field and returns
+  /// its value as a string, throwing an exception if the field does not exist
+  /// or if the value is not a string.
+  ///
+  std::string getValueAsString(const std::string &FieldName) const;
+
 };
 
 std::ostream &operator<<(std::ostream &OS, const Record &R);
@@ -633,9 +646,9 @@ public:
 
   /// 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<Record*> &ReturnDefs) const;
+  /// name does not exist, an exception is thrown.
+  std::vector<Record*>
+  getAllDerivedDefinitions(const std::string &ClassName) const;
 
 
   void dump() const;
index 6dc409b1c2afc399b95b8e414d1ac43828848f48..f282e4b0d6055f1c468d2ea9214c9f67c9d2b47b 100644 (file)
@@ -452,6 +452,23 @@ std::ostream &operator<<(std::ostream &OS, const Record &R) {
   return OS << "}\n";
 }
 
+/// getValueAsString - This method looks up the specified field and returns its
+/// value as a string, throwing an exception if the field does not exist or if
+/// the value is not a string.
+///
+std::string Record::getValueAsString(const std::string &FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (R == 0 || R->getValue() == 0)
+    throw "Record '" + R->getName() + "' does not have a field named '" +
+          FieldName + "!\n";
+
+  if (const StringInit *SI = dynamic_cast<const StringInit*>(R->getValue()))
+    return SI->getValue();
+  throw "Record '" + R->getName() + "', field '" + FieldName +
+        "' does not have a string initializer!";
+}
+
+
 void RecordKeeper::dump() const { std::cerr << *this; }
 
 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
@@ -473,18 +490,17 @@ std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
 /// 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<Record*> &Defs) const {
+std::vector<Record*>
+RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
   Record *Class = Records.getClass(ClassName);
-  if (!Class) {
-    std::cerr << "ERROR: Couldn't find the '" << ClassName << "' class!\n";
-    return true;
-  }
+  if (!Class)
+    throw "ERROR: Couldn't find the '" + ClassName + "' class!\n";
 
+  std::vector<Record*> Defs;
   for (std::map<std::string, Record*>::const_iterator I = getDefs().begin(),
          E = getDefs().end(); I != E; ++I)
     if (I->second->isSubClassOf(Class))
       Defs.push_back(I->second);
 
-  return false;
+  return Defs;
 }
index 7f703aaa9e665a469f81985b79d20ab0373a3330..467f12a6ab368ba3a86735da11294427831ef21e 100644 (file)
@@ -319,6 +319,8 @@ class StringInit : public Init {
 public:
   StringInit(const std::string &V) : Value(V) {}
 
+  const std::string &getValue() const { return Value; }
+
   virtual Init *convertInitializerTo(RecTy *Ty) {
     return Ty->convertValue(this);
   }
@@ -592,6 +594,17 @@ public:
   void resolveReferences();
 
   void dump() const;
+
+  //===--------------------------------------------------------------------===//
+  // High-level methods useful to tablegen back-ends
+  //
+
+  /// getValueAsString - This method looks up the specified field and returns
+  /// its value as a string, throwing an exception if the field does not exist
+  /// or if the value is not a string.
+  ///
+  std::string getValueAsString(const std::string &FieldName) const;
+
 };
 
 std::ostream &operator<<(std::ostream &OS, const Record &R);
@@ -633,9 +646,9 @@ public:
 
   /// 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<Record*> &ReturnDefs) const;
+  /// name does not exist, an exception is thrown.
+  std::vector<Record*>
+  getAllDerivedDefinitions(const std::string &ClassName) const;
 
 
   void dump() const;