Implement !cast<string>.
authorDavid Greene <greened@obbligato.org>
Mon, 29 Jun 2009 20:05:29 +0000 (20:05 +0000)
committerDavid Greene <greened@obbligato.org>
Mon, 29 Jun 2009 20:05:29 +0000 (20:05 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74444 91177308-0d34-0410-b5e6-96231b3b80d8

docs/TableGenFundamentals.html
utils/TableGen/Record.cpp
utils/TableGen/Record.h

index 05b2b233e6a1ca709aa055eedf70bab90ebb0cfd..ec87f2967cb4c8e135389c01f5e85abff8d63e5e 100644 (file)
@@ -411,7 +411,8 @@ which case the user must specify it explicitly.</dd>
 <dt><tt>!cast<type>(a)</tt></dt>
   <dd>A symbol of type <em>type</em> obtained by looking up the string 'a' in
 the symbol table.  If the type of 'a' does not match <em>type</em>, TableGen
-aborts with an error. </dd>
+aborts with an error. !cast<string> is a special case in that the argument must
+be an object defined by a 'def' construct.</dd>
 <dt><tt>!nameconcat&lt;type&gt;(a, b)</tt></dt>
   <dd>Shorthand for !cast<type>(!strconcat(a, b))</dd>
 <dt><tt>!subst(a, b, c)</tt></dt>
index c62e21b3aa182e96efa53d4065ec1ac664e228e9..92ba62879ee0c8a47892d302bd6447b319b07e0b 100644 (file)
@@ -537,52 +537,80 @@ Init *UnOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
   switch (getOpcode()) {
   default: assert(0 && "Unknown unop");
   case CAST: {
-    StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
-    if (LHSs) {
-      std::string Name = LHSs->getValue();
+    if (getType()->getAsString() == "string") {
+      StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+      if (LHSs) {
+        return LHSs;
+      }
 
-      // From TGParser::ParseIDValue
-      if (CurRec) {
-        if (const RecordVal *RV = CurRec->getValue(Name)) {
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
-          }
-          return new VarInit(Name, RV->getType());
-        }
-        
-        std::string TemplateArgName = CurRec->getName()+":"+Name;
-        if (CurRec->isTemplateArg(TemplateArgName)) {
-          const RecordVal *RV = CurRec->getValue(TemplateArgName);
-          assert(RV && "Template arg doesn't exist??");
+      DefInit *LHSd = dynamic_cast<DefInit*>(LHS);
+      if (LHSd) {
+        return new StringInit(LHSd->getDef()->getName());
+      }
 
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
+//       VarInit *LHSv = dynamic_cast<VarInit*>(LHS);
+//       if (LHSv) {
+//         // If this is not a template arg, cast it
+//         if (!CurRec->isTemplateArg(LHSv->getName())
+//             && !CurMultiClass) {
+//           return new StringInit(LHSv->getName());
+//         }
+//         break;
+//       }
+
+//       OpInit *LHSo = dynamic_cast<OpInit*>(LHS);
+//       if (!LHSo) {
+//         return new StringInit(LHS->getAsString());
+//       }
+    }
+    else {
+      StringInit *LHSs = dynamic_cast<StringInit*>(LHS);
+      if (LHSs) {
+        std::string Name = LHSs->getValue();
+
+        // From TGParser::ParseIDValue
+        if (CurRec) {
+          if (const RecordVal *RV = CurRec->getValue(Name)) {
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
+            return new VarInit(Name, RV->getType());
           }
 
-          return new VarInit(TemplateArgName, RV->getType());
-        }
-      }
+          std::string TemplateArgName = CurRec->getName()+":"+Name;
+          if (CurRec->isTemplateArg(TemplateArgName)) {
+            const RecordVal *RV = CurRec->getValue(TemplateArgName);
+            assert(RV && "Template arg doesn't exist??");
 
-      if (CurMultiClass) {
-        std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
-        if (CurMultiClass->Rec.isTemplateArg(MCName)) {
-          const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
-          assert(RV && "Template arg doesn't exist??");
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
 
-          if (RV->getType() != getType()) {
-            throw "type mismatch in nameconcat";
+            return new VarInit(TemplateArgName, RV->getType());
           }
-          
-          return new VarInit(MCName, RV->getType());
         }
-      }
 
-      if (Record *D = Records.getDef(Name))
-        return new DefInit(D);
+        if (CurMultiClass) {
+          std::string MCName = CurMultiClass->Rec.getName()+"::"+Name;
+          if (CurMultiClass->Rec.isTemplateArg(MCName)) {
+            const RecordVal *RV = CurMultiClass->Rec.getValue(MCName);
+            assert(RV && "Template arg doesn't exist??");
+            
+            if (RV->getType() != getType()) {
+              throw "type mismatch in nameconcat";
+            }
+            
+            return new VarInit(MCName, RV->getType());
+          }
+        }
+        
+        if (Record *D = Records.getDef(Name))
+          return new DefInit(D);
 
-      cerr << "Variable not defined: '" + Name + "'\n";
-      assert(0 && "Variable not found");
-      return 0;
+        cerr << "Variable not defined: '" + Name + "'\n";
+        assert(0 && "Variable not found");
+        return 0;
+      }
     }
     break;
   }
@@ -654,6 +682,23 @@ std::string UnOpInit::getAsString() const {
   return Result + "(" + LHS->getAsString() + ")";
 }
 
+RecTy *UnOpInit::getFieldType(const std::string &FieldName) const {
+  switch (getOpcode()) {
+  default: assert(0 && "Unknown unop");
+  case CAST: {
+    RecordRecTy *RecordType = dynamic_cast<RecordRecTy *>(getType());
+    if (RecordType) {
+      RecordVal *Field = RecordType->getRecord()->getValue(FieldName);
+      if (Field) {
+        return Field->getType();
+      }
+    }
+    break;
+  }
+  }
+  return 0;
+}
+
 Init *BinOpInit::Fold(Record *CurRec, MultiClass *CurMultiClass) {
   switch (getOpcode()) {
   default: assert(0 && "Unknown binop");
index 5f45ea09cafa55f407aee3d7e479953a3d029d2f..2254dd84e08984a8ec267a9557ecc5883d02681c 100644 (file)
@@ -834,6 +834,12 @@ public:
 
   virtual Init *resolveReferences(Record &R, const RecordVal *RV);
   
+  /// getFieldType - This method is used to implement the FieldInit class.
+  /// Implementors of this method should return the type of the named field if
+  /// they are of record type.
+  ///
+  virtual RecTy *getFieldType(const std::string &FieldName) const;
+
   virtual std::string getAsString() const;
 };