Add NAME Member

Add a Value named "NAME" to each Record.  This will be set to the def or defm
name when instantiating multiclasses.  This will replace the #NAME# processing
hack once paste functionality is in place.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142518 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/TableGen/Record.h b/include/llvm/TableGen/Record.h
index f7a48c4..0cf1725 100644
--- a/include/llvm/TableGen/Record.h
+++ b/include/llvm/TableGen/Record.h
@@ -1473,6 +1473,13 @@
   void addValue(const RecordVal &RV) {
     assert(getValue(RV.getName()) == 0 && "Value already added!");
     Values.push_back(RV);
+    if (Values.size() > 1)
+      // Keep NAME at the end of the list.  It makes record dumps a
+      // bit prettier and allows TableGen tests to be written more
+      // naturally.  Tests can use CHECK-NEXT to look for Record
+      // fields they expect to see after a def.  They can't do that if
+      // NAME is the first Record field.
+      std::swap(Values[Values.size() - 2], Values[Values.size() - 1]);
   }
 
   void removeValue(Init *Name) {
diff --git a/lib/TableGen/Record.cpp b/lib/TableGen/Record.cpp
index a22c80c..92559d1 100644
--- a/lib/TableGen/Record.cpp
+++ b/lib/TableGen/Record.cpp
@@ -1686,6 +1686,11 @@
 
 void Record::init() {
   checkName();
+
+  // Every record potentially has a def at the top.  This value is
+  // replaced with the top-level def name at instantiation time.
+  RecordVal DN("NAME", StringRecTy::get(), 0);
+  addValue(DN);
 }
 
 void Record::checkName() {
diff --git a/lib/TableGen/TGParser.cpp b/lib/TableGen/TGParser.cpp
index 45625f9..085136d 100644
--- a/lib/TableGen/TGParser.cpp
+++ b/lib/TableGen/TGParser.cpp
@@ -1744,7 +1744,7 @@
   Record *CurRec = Records.getClass(Lex.getCurStrVal());
   if (CurRec) {
     // If the body was previously defined, this is an error.
-    if (!CurRec->getValues().empty() ||
+    if (CurRec->getValues().size() > 1 ||  // Account for NAME.
         !CurRec->getSuperClasses().empty() ||
         !CurRec->getTemplateArgs().empty())
       return TokError("Class '" + CurRec->getNameInitAsString()
diff --git a/test/TableGen/Dag.td b/test/TableGen/Dag.td
index 9ed2301..7ceb4e7 100644
--- a/test/TableGen/Dag.td
+++ b/test/TableGen/Dag.td
@@ -60,6 +60,7 @@
 // CHECK-NEXT:  dag Dag1 = (somedef1 1);
 // CHECK-NEXT:  dag Dag2 = (somedef1 2);
 // CHECK-NEXT:  dag Dag3 = (somedef1 2);
+// CHECK-NEXT:  NAME = ?
 // CHECK-NEXT: }
 
 
@@ -68,4 +69,5 @@
 // CHECK-NEXT:  dag Dag1 = (somedef1 1);
 // CHECK-NEXT:  dag Dag2 = (somedef2 2);
 // CHECK-NEXT:  dag Dag3 = (somedef2 2);
+// CHECK-NEXT:  NAME = ?
 // CHECK-NEXT: }
diff --git a/test/TableGen/foreach.td b/test/TableGen/foreach.td
index cbcade9..814ae6e 100644
--- a/test/TableGen/foreach.td
+++ b/test/TableGen/foreach.td
@@ -1,6 +1,6 @@
 // RUN: llvm-tblgen %s | grep {Jr} | count 2
 // RUN: llvm-tblgen %s | grep {Sr} | count 2
-// RUN: llvm-tblgen %s | grep {NAME} | count 1
+// RUN: llvm-tblgen %s | grep {"NAME"} | count 1
 // XFAIL: vg_leak
 
 // Variables for foreach