DebugInfo: Use MDTypeRef throughout the hierarchy

Use `MDTypeRef` (etc.) in the new debug info hierarchy rather than raw
`Metadata *` pointers.

I rolled in a change to `DIBuilder` that looks unrelated: take `DIType`
instead of `DITypeRef` as type arguments when creating variables.
However, this was the simplest way to use `MDTypeRef` within the
functions, and didn't require any cleanups from callers in clang (since
they were all passing in `DIType`s anyway, relying on their implicit
conversions to `DITypeRef`).

llvm-svn: 234197
diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp
index ac8a256..27699f8 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -85,25 +85,29 @@
                                      nullptr, false, false, 0, nullptr, 0, 0, 0,
                                      0);
   }
+  MDScopeRef getSubprogramRef() { return getSubprogram()->getRef(); }
   MDFile *getFile() {
     return MDFile::getDistinct(Context, "file.c", "/path/to/dir");
   }
-  MDBasicType *getBasicType(StringRef Name) {
-    return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
+  MDTypeRef getBasicType(StringRef Name) {
+    return MDBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name)
+        ->getRef();
   }
-  MDDerivedType *getDerivedType() {
+  MDTypeRef getDerivedType() {
     return MDDerivedType::getDistinct(Context, dwarf::DW_TAG_pointer_type, "",
                                       nullptr, 0, nullptr,
-                                      getBasicType("basictype"), 1, 2, 0, 0);
+                                      getBasicType("basictype"), 1, 2, 0, 0)
+        ->getRef();
   }
   ConstantAsMetadata *getConstantAsMetadata() {
     return ConstantAsMetadata::get(
         ConstantInt::get(Type::getInt32Ty(Context), Counter++));
   }
-  MDCompositeType *getCompositeType() {
+  MDTypeRef getCompositeType() {
     return MDCompositeType::getDistinct(
-        Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
-        32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "");
+               Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr,
+               nullptr, 32, 32, 0, 0, nullptr, 0, nullptr, nullptr, "")
+        ->getRef();
   }
   ConstantAsMetadata *getFunctionAsMetadata(StringRef Name) {
     return ConstantAsMetadata::get(M.getOrInsertFunction(
@@ -958,8 +962,8 @@
 
 TEST_F(MDDerivedTypeTest, get) {
   MDFile *File = getFile();
-  MDScope *Scope = getSubprogram();
-  MDType *BaseType = getBasicType("basic");
+  MDScopeRef Scope = getSubprogramRef();
+  MDTypeRef BaseType = getBasicType("basic");
   MDTuple *ExtraData = getTuple();
 
   auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
@@ -992,7 +996,7 @@
                                   "something", File, 2, Scope, BaseType, 2, 3,
                                   4, 5, ExtraData));
   EXPECT_NE(N, MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
-                                  "something", File, 1, getSubprogram(),
+                                  "something", File, 1, getSubprogramRef(),
                                   BaseType, 2, 3, 4, 5, ExtraData));
   EXPECT_NE(N, MDDerivedType::get(
                    Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
@@ -1019,8 +1023,8 @@
 
 TEST_F(MDDerivedTypeTest, getWithLargeValues) {
   MDFile *File = getFile();
-  MDScope *Scope = getSubprogram();
-  MDType *BaseType = getBasicType("basic");
+  MDScopeRef Scope = getSubprogramRef();
+  MDTypeRef BaseType = getBasicType("basic");
   MDTuple *ExtraData = getTuple();
 
   auto *N = MDDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something",
@@ -1038,15 +1042,15 @@
   StringRef Name = "some name";
   MDFile *File = getFile();
   unsigned Line = 1;
-  MDScope *Scope = getSubprogram();
-  MDType *BaseType = getCompositeType();
+  MDScopeRef Scope = getSubprogramRef();
+  MDTypeRef BaseType = getCompositeType();
   uint64_t SizeInBits = 2;
   uint64_t AlignInBits = 3;
   uint64_t OffsetInBits = 4;
   unsigned Flags = 5;
   MDTuple *Elements = getTuple();
   unsigned RuntimeLang = 6;
-  MDType *VTableHolder = getCompositeType();
+  MDTypeRef VTableHolder = getCompositeType();
   MDTuple *TemplateParams = getTuple();
   StringRef Identifier = "some id";
 
@@ -1092,13 +1096,13 @@
                                     OffsetInBits, Flags, Elements, RuntimeLang,
                                     VTableHolder, TemplateParams, Identifier));
   EXPECT_NE(N, MDCompositeType::get(
-                   Context, Tag, Name, File, Line, getSubprogram(), BaseType,
+                   Context, Tag, Name, File, Line, getSubprogramRef(), BaseType,
                    SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
                    RuntimeLang, VTableHolder, TemplateParams, Identifier));
-  EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope, File,
-                                    SizeInBits, AlignInBits, OffsetInBits,
-                                    Flags, Elements, RuntimeLang, VTableHolder,
-                                    TemplateParams, Identifier));
+  EXPECT_NE(N, MDCompositeType::get(
+                   Context, Tag, Name, File, Line, Scope, getBasicType("other"),
+                   SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
+                   RuntimeLang, VTableHolder, TemplateParams, Identifier));
   EXPECT_NE(N, MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
                                     BaseType, SizeInBits + 1, AlignInBits,
                                     OffsetInBits, Flags, Elements, RuntimeLang,
@@ -1155,15 +1159,15 @@
   StringRef Name = "some name";
   MDFile *File = getFile();
   unsigned Line = 1;
-  MDScope *Scope = getSubprogram();
-  MDType *BaseType = getCompositeType();
+  MDScopeRef Scope = getSubprogramRef();
+  MDTypeRef BaseType = getCompositeType();
   uint64_t SizeInBits = UINT64_MAX;
   uint64_t AlignInBits = UINT64_MAX - 1;
   uint64_t OffsetInBits = UINT64_MAX - 2;
   unsigned Flags = 5;
   MDTuple *Elements = getTuple();
   unsigned RuntimeLang = 6;
-  MDType *VTableHolder = getCompositeType();
+  MDTypeRef VTableHolder = getCompositeType();
   MDTuple *TemplateParams = getTuple();
   StringRef Identifier = "some id";
 
@@ -1181,8 +1185,8 @@
   StringRef Name = "some name";
   MDFile *File = getFile();
   unsigned Line = 1;
-  MDScope *Scope = getSubprogram();
-  MDType *BaseType = getCompositeType();
+  MDScopeRef Scope = getSubprogramRef();
+  MDTypeRef BaseType = getCompositeType();
   uint64_t SizeInBits = 2;
   uint64_t AlignInBits = 3;
   uint64_t OffsetInBits = 4;
@@ -1202,7 +1206,7 @@
   N->replaceElements(nullptr);
   EXPECT_EQ(nullptr, N->getElements());
 
-  auto *VTableHolder = MDTuple::getDistinct(Context, None);
+  MDTypeRef VTableHolder = getCompositeType();
   EXPECT_EQ(nullptr, N->getVTableHolder());
   N->replaceVTableHolder(VTableHolder);
   EXPECT_EQ(VTableHolder, N->getVTableHolder());
@@ -1413,7 +1417,7 @@
 typedef MetadataTest MDSubprogramTest;
 
 TEST_F(MDSubprogramTest, get) {
-  MDScope *Scope = getCompositeType();
+  MDScopeRef Scope = getCompositeType();
   StringRef Name = "name";
   StringRef LinkageName = "linkage";
   MDFile *File = getFile();
@@ -1422,7 +1426,7 @@
   bool IsLocalToUnit = false;
   bool IsDefinition = true;
   unsigned ScopeLine = 3;
-  MDType *ContainingType = getCompositeType();
+  MDTypeRef ContainingType = getCompositeType();
   unsigned Virtuality = 4;
   unsigned VirtualIndex = 5;
   unsigned Flags = 6;
@@ -1559,7 +1563,7 @@
 }
 
 TEST_F(MDSubprogramTest, replaceFunction) {
-  MDScope *Scope = getCompositeType();
+  MDScopeRef Scope = getCompositeType();
   StringRef Name = "name";
   StringRef LinkageName = "linkage";
   MDFile *File = getFile();
@@ -1568,7 +1572,7 @@
   bool IsLocalToUnit = false;
   bool IsDefinition = true;
   unsigned ScopeLine = 3;
-  MDCompositeType *ContainingType = getCompositeType();
+  MDTypeRef ContainingType = getCompositeType();
   unsigned Virtuality = 4;
   unsigned VirtualIndex = 5;
   unsigned Flags = 6;
@@ -1677,7 +1681,7 @@
 
 TEST_F(MDTemplateTypeParameterTest, get) {
   StringRef Name = "template";
-  MDType *Type = getBasicType("basic");
+  MDTypeRef Type = getBasicType("basic");
 
   auto *N = MDTemplateTypeParameter::get(Context, Name, Type);
 
@@ -1699,7 +1703,7 @@
 TEST_F(MDTemplateValueParameterTest, get) {
   unsigned Tag = dwarf::DW_TAG_template_value_parameter;
   StringRef Name = "template";
-  MDType *Type = getBasicType("basic");
+  MDTypeRef Type = getBasicType("basic");
   Metadata *Value = getConstantAsMetadata();
 
   auto *N = MDTemplateValueParameter::get(Context, Tag, Name, Type, Value);
@@ -1731,11 +1735,12 @@
   StringRef LinkageName = "linkage";
   MDFile *File = getFile();
   unsigned Line = 5;
-  Metadata *Type = MDTuple::getDistinct(Context, None);
+  MDTypeRef Type = getDerivedType();
   bool IsLocalToUnit = false;
   bool IsDefinition = true;
   ConstantAsMetadata *Variable = getConstantAsMetadata();
-  MDDerivedType *StaticDataMemberDeclaration = getDerivedType();
+  MDDerivedType *StaticDataMemberDeclaration =
+      cast<MDDerivedType>(getDerivedType());
 
   auto *N = MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
                                   Type, IsLocalToUnit, IsDefinition, Variable,
@@ -1773,9 +1778,10 @@
             MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
                                   Line + 1, Type, IsLocalToUnit, IsDefinition,
                                   Variable, StaticDataMemberDeclaration));
-  EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
-                                     Line, Scope, IsLocalToUnit, IsDefinition,
-                                     Variable, StaticDataMemberDeclaration));
+  EXPECT_NE(N,
+            MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
+                                  getDerivedType(), IsLocalToUnit, IsDefinition,
+                                  Variable, StaticDataMemberDeclaration));
   EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
                                      Line, Type, !IsLocalToUnit, IsDefinition,
                                      Variable, StaticDataMemberDeclaration));
@@ -1786,9 +1792,10 @@
                                      Line, Type, IsLocalToUnit, IsDefinition,
                                      getConstantAsMetadata(),
                                      StaticDataMemberDeclaration));
-  EXPECT_NE(N, MDGlobalVariable::get(Context, Scope, Name, LinkageName, File,
-                                     Line, Type, IsLocalToUnit, IsDefinition,
-                                     Variable, getDerivedType()));
+  EXPECT_NE(N,
+            MDGlobalVariable::get(Context, Scope, Name, LinkageName, File, Line,
+                                  Type, IsLocalToUnit, IsDefinition, Variable,
+                                  cast<MDDerivedType>(getDerivedType())));
 
   TempMDGlobalVariable Temp = N->clone();
   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
@@ -1802,7 +1809,7 @@
   StringRef Name = "name";
   MDFile *File = getFile();
   unsigned Line = 5;
-  Metadata *Type = MDTuple::getDistinct(Context, None);
+  MDTypeRef Type = getDerivedType();
   unsigned Arg = 6;
   unsigned Flags = 7;
   MDLocation *InlinedAt =
@@ -1834,7 +1841,7 @@
   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line + 1,
                                     Type, Arg, Flags, InlinedAt));
   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line,
-                                    Scope, Arg, Flags, InlinedAt));
+                                    getDerivedType(), Arg, Flags, InlinedAt));
   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
                                     Arg + 1, Flags, InlinedAt));
   EXPECT_NE(N, MDLocalVariable::get(Context, Tag, Scope, Name, File, Line, Type,
@@ -1924,7 +1931,7 @@
   StringRef GetterName = "getter";
   StringRef SetterName = "setter";
   unsigned Attributes = 7;
-  MDType *Type = getBasicType("basic");
+  MDType *Type = cast<MDBasicType>(getBasicType("basic"));
 
   auto *N = MDObjCProperty::get(Context, Name, File, Line, GetterName,
                                 SetterName, Attributes, Type);
@@ -1952,9 +1959,9 @@
                                    "other", Attributes, Type));
   EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
                                    SetterName, Attributes + 1, Type));
-  EXPECT_NE(N,
-            MDObjCProperty::get(Context, Name, File, Line, GetterName,
-                                SetterName, Attributes, getBasicType("other")));
+  EXPECT_NE(N, MDObjCProperty::get(Context, Name, File, Line, GetterName,
+                                   SetterName, Attributes,
+                                   cast<MDBasicType>(getBasicType("other"))));
 
   TempMDObjCProperty Temp = N->clone();
   EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
@@ -1965,7 +1972,7 @@
 TEST_F(MDImportedEntityTest, get) {
   unsigned Tag = dwarf::DW_TAG_imported_module;
   MDScope *Scope = getSubprogram();
-  DebugNode *Entity = getCompositeType();
+  DebugNodeRef Entity = getCompositeType();
   unsigned Line = 5;
   StringRef Name = "name";