Add a new Record::getValueAsCode method to mirror the other getValueAs*
authorChris Lattner <sabre@nondot.org>
Tue, 13 Sep 2005 21:44:28 +0000 (21:44 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 13 Sep 2005 21:44:28 +0000 (21:44 +0000)
methods.  Use it to simplify some code.

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

utils/TableGen/CodeGenTarget.cpp
utils/TableGen/Record.cpp
utils/TableGen/Record.h

index 61e237f1bd9a2c6c99cd741dc2503783172e0171..7c153ece928678987b4d4314b1a9b922c7b59d1a 100644 (file)
@@ -154,17 +154,8 @@ CodeGenRegisterClass::CodeGenRegisterClass(Record *R) : TheDef(R) {
   SpillAlignment = R->getValueAsInt("Alignment");
   VT = getValueType(R->getValueAsDef("RegType"));
 
-  if (CodeInit *CI = dynamic_cast<CodeInit*>(R->getValueInit("MethodBodies")))
-    MethodBodies = CI->getValue();
-  else
-    throw "Expected 'code' fragment for 'MethodBodies' value in register "
-          "class '" + getName() + "'!";
-
-  if (CodeInit *CI = dynamic_cast<CodeInit*>(R->getValueInit("MethodProtos")))
-    MethodProtos = CI->getValue();
-  else
-    throw "Expected 'code' fragment for 'MethodProtos' value in register "
-      "class '" + getName() + "'!";
+  MethodBodies = R->getValueAsCode("MethodBodies");
+  MethodProtos = R->getValueAsCode("MethodProtos");
   
   ListInit *RegList = R->getValueAsListInit("MemberList");
   for (unsigned i = 0, e = RegList->getSize(); i != e; ++i) {
index 077476d07bccb366ec44d4b1d6660f69cb08e191..c5f0565749979bdf4637e1ac9b527c4a397d621d 100644 (file)
@@ -773,6 +773,18 @@ DagInit *Record::getValueAsDag(const std::string &FieldName) const {
         "' does not have a dag initializer!";
 }
 
+std::string Record::getValueAsCode(const std::string &FieldName) const {
+  const RecordVal *R = getValue(FieldName);
+  if (R == 0 || R->getValue() == 0)
+    throw "Record `" + getName() + "' does not have a field named `" +
+      FieldName + "'!\n";
+  
+  if (const CodeInit *CI = dynamic_cast<const CodeInit*>(R->getValue()))
+    return CI->getValue();
+  throw "Record `" + getName() + "', field `" + FieldName +
+    "' does not have a code initializer!";
+}
+
 
 void RecordKeeper::dump() const { std::cerr << *this; }
 
index 4ca08902cfe756cb04778a2fc388cc09d94987a5..edd875afcec51c6e3b7d1db6b5b2bfc3ab410f68 100644 (file)
@@ -1023,6 +1023,12 @@ public:
   /// the value is not the right type.
   ///
   DagInit *getValueAsDag(const std::string &FieldName) const;
+  
+  /// getValueAsCode - This method looks up the specified field and returns
+  /// its value as the string data in a CodeInit, throwing an exception if the
+  /// field does not exist or if the value is not a code object.
+  ///
+  std::string getValueAsCode(const std::string &FieldName) const;
 };
 
 std::ostream &operator<<(std::ostream &OS, const Record &R);