TableGen: Fix type deduction for !foreach

Summary:
In the case of !foreach(id, input-list, transform) where the type of
input-list is list<A> and the type of transform is B, we now correctly
deduce list<B> as the type of the !foreach.

Change-Id: Ia19dd65eecc5991dd648280ba6a15f6a20fd61de

Reviewers: arsenm, craig.topper, tra, MartinO

Subscribers: wdng, llvm-commits

Differential Revision: https://reviews.llvm.org/D43555

llvm-svn: 325797
diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp
index 12ae237..17c2c2e 100644
--- a/llvm/lib/TableGen/TGParser.cpp
+++ b/llvm/lib/TableGen/TGParser.cpp
@@ -1075,6 +1075,14 @@
         return nullptr;
       }
       Type = MHSt->getType();
+      if (isa<ListRecTy>(Type)) {
+        TypedInit *RHSt = dyn_cast<TypedInit>(RHS);
+        if (!RHSt) {
+          TokError("could not get type of !foreach list elements");
+          return nullptr;
+        }
+        Type = RHSt->getType()->getListTy();
+      }
       break;
     }
     case tgtok::XSubst: {