Add new getValueAsBitsInit 'high-level' method
[oota-llvm.git] / support / tools / TableGen / Record.cpp
index b33e5e774b7e9a113d85c07b0aa89446b3d73536..acb61ec18525386dcefd79a9f3d7e1245cd56d4e 100644 (file)
@@ -116,6 +116,14 @@ Init *ListRecTy::convertValue(ListInit *LI) {
   return LI;
 }
 
+Init *ListRecTy::convertValue(TypedInit *TI) {
+  // Ensure that TI is compatible with our class.
+  if (ListRecTy *LRT = dynamic_cast<ListRecTy*>(TI->getType()))
+    if (LRT->getElementClass() == getElementClass())
+      return TI;
+  return 0;
+}
+
 void RecordRecTy::print(std::ostream &OS) const {
   OS << Rec->getName();
 }
@@ -127,6 +135,15 @@ Init *RecordRecTy::convertValue(DefInit *DI) {
   return DI;
 }
 
+Init *RecordRecTy::convertValue(TypedInit *TI) {
+  // Ensure that TI is compatible with Rec.
+  if (RecordRecTy *RRT = dynamic_cast<RecordRecTy*>(TI->getType()))
+    if (RRT->getRecord()->isSubClassOf(getRecord()) ||
+        RRT->getRecord() == getRecord())
+      return TI;
+  return 0;
+}
+
 //===----------------------------------------------------------------------===//
 //    Initializer implementations
 //===----------------------------------------------------------------------===//
@@ -153,7 +170,10 @@ void BitsInit::print(std::ostream &OS) const {
   OS << "{ ";
   for (unsigned i = 0, e = getNumBits(); i != e; ++i) {
     if (i) OS << ", ";
-    getBit(e-i-1)->print(OS);
+    if (Init *Bit = getBit(e-i-1))
+      Bit->print(OS);
+    else
+      OS << "*";
   }
   OS << " }";
 }
@@ -203,6 +223,9 @@ bool BitsInit::printAsUnset(std::ostream &OS) const {
   return false;
 }
 
+// resolveReferences - If there are any field references that refer to fields
+// that have been filled in, we can propagate the values now.
+//
 Init *BitsInit::resolveReferences(Record &R) {
   bool Changed = false;
   BitsInit *New = new BitsInit(getNumBits());
@@ -297,7 +320,18 @@ Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
   return 0;
 }
 
-
+/// resolveReferences - This method is used by classes that refer to other
+/// variables which may not be defined at the time they expression is formed.
+/// If a value is set for the variable later, this method will be called on
+/// users of the value to allow the value to propagate out.
+///
+Init *VarInit::resolveReferences(Record &R) {
+  if (RecordVal *Val = R.getValue(VarName))
+    if (!dynamic_cast<UnsetInit*>(Val->getValue()))
+      return Val->getValue();
+  return this;
+}
+  
 
 Init *VarBitInit::resolveReferences(Record &R) {
   Init *I = getVariable()->resolveBitReference(R, getBitNum());
@@ -418,6 +452,40 @@ 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!";
+}
+
+/// getValueAsBitsInit - This method looks up the specified field and returns
+/// its value as a BitsInit, throwing an exception if the field does not exist
+/// or if the value is not the right type.
+///
+BitsInit *Record::getValueAsBitsInit(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 (BitsInit *BI = dynamic_cast<BitsInit*>(R->getValue()))
+    return BI;
+  throw "Record '" + R->getName() + "', field '" + FieldName +
+        "' does not have a BitsInit initializer!";
+}
+
+
+
 void RecordKeeper::dump() const { std::cerr << *this; }
 
 std::ostream &operator<<(std::ostream &OS, const RecordKeeper &RK) {
@@ -434,3 +502,22 @@ 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.
+std::vector<Record*>
+RecordKeeper::getAllDerivedDefinitions(const std::string &ClassName) const {
+  Record *Class = Records.getClass(ClassName);
+  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 Defs;
+}