Fix the way field bit references are resolved, also allow resolution of field referen...
[oota-llvm.git] / support / tools / TableGen / Record.cpp
index d48d987a85a5ccf65e79eda45b027c011f6cacc8..fc035aceb3886248c3895d5abef0789243a59bf4 100644 (file)
@@ -19,7 +19,6 @@ Init *BitRecTy::convertValue(BitsInit *BI) {
 Init *BitRecTy::convertValue(IntInit *II) {
   int Val = II->getValue();
   if (Val != 0 && Val != 1) return 0;  // Only accept 0 or 1 for a bit!
-  delete II;
   
   return new BitInit(Val != 0); 
 }
@@ -50,7 +49,6 @@ Init *BitsRecTy::convertValue(BitInit *UI) {
 //
 Init *BitsRecTy::convertValue(IntInit *II) {
   int Value = II->getValue();
-  delete II;
 
   BitsInit *Ret = new BitsInit(Size);
   for (unsigned i = 0; i != Size; ++i)
@@ -118,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();
 }
@@ -129,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
 //===----------------------------------------------------------------------===//
@@ -155,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 << " }";
 }
@@ -205,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());
@@ -295,11 +316,22 @@ Init *VarInit::getFieldInit(Record &R, const std::string &FieldName) const {
       if (Init *I = RV->getValue()->getFieldInit(R, FieldName))
         return I;
       else
-        return (Init*)this;
+        return 0;
   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());
@@ -341,14 +373,22 @@ Init *FieldInit::convertInitializerBitRange(const std::vector<unsigned> &Bits) {
 
 Init *FieldInit::resolveBitReference(Record &R, unsigned Bit) {
   Init *BitsVal = Rec->getFieldInit(R, FieldName);
-  assert(BitsVal && "No initializer found!");
-
-  if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
-    assert(Bit < BI->getNumBits() && "Bit reference out of range!");
-    Init *B = BI->getBit(Bit);
-    
-    if (dynamic_cast<BitInit*>(B))  // If the bit is set...
-      return B;                     // Replace the VarBitInit with it.
+  if (BitsVal)
+    if (BitsInit *BI = dynamic_cast<BitsInit*>(BitsVal)) {
+      assert(Bit < BI->getNumBits() && "Bit reference out of range!");
+      Init *B = BI->getBit(Bit);
+      
+      if (dynamic_cast<BitInit*>(B))  // If the bit is set...
+        return B;                     // Replace the VarBitInit with it.
+    }
+  return this;
+}
+
+Init *FieldInit::resolveReferences(Record &R) {
+  Init *BitsVal = Rec->getFieldInit(R, FieldName);
+  if (BitsVal) {
+    Init *BVR = BitsVal->resolveReferences(R);
+    return BVR->isComplete() ? BVR : this;
   }
   return this;
 }
@@ -420,6 +460,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) {
@@ -436,3 +510,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;
+}