IR: Add MDCompositeTypeBase::replace*()

Add `replaceElements()`, `replaceVTableHolder()`, and
`replaceTemplateParams()` to `MDCompositeTypeBase`.  Included an
assertion in `replaceElements()` to match the one in
`DICompositeType::replaceArrays()`.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@229744 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2015-02-18 20:47:52 +00:00
parent 70ee038129
commit 668aca9b4a
2 changed files with 64 additions and 0 deletions

View File

@ -576,6 +576,29 @@ public:
MDString *getRawIdentifier() const { return getOperandAs<MDString>(7); }
/// \brief Replace operands.
///
/// If this \a isUniqued() and not \a isResolved(), on a uniquing collision
/// this will be RAUW'ed and deleted. Use a \a TrackingMDRef to keep track
/// of its movement if necessary.
/// @{
void replaceElements(MDTuple *Elements) {
#ifndef NDEBUG
if (auto *Old = cast_or_null<MDTuple>(getElements()))
for (const auto &Op : Old->operands())
assert(std::find(Elements->op_begin(), Elements->op_end(), Op) &&
"Lost a member during member list replacement");
#endif
replaceOperandWith(4, Elements);
}
void replaceVTableHolder(Metadata *VTableHolder) {
replaceOperandWith(5, VTableHolder);
}
void replaceTemplateParams(MDTuple *TemplateParams) {
replaceOperandWith(6, TemplateParams);
}
/// @}
static bool classof(const Metadata *MD) {
return MD->getMetadataID() == MDCompositeTypeKind ||
MD->getMetadataID() == MDSubroutineTypeKind;

View File

@ -897,6 +897,47 @@ TEST_F(MDCompositeTypeTest, get) {
EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
}
TEST_F(MDCompositeTypeTest, replaceOperands) {
unsigned Tag = dwarf::DW_TAG_structure_type;
StringRef Name = "some name";
Metadata *File = MDTuple::getDistinct(Context, None);
unsigned Line = 1;
Metadata *Scope = MDTuple::getDistinct(Context, None);
Metadata *BaseType = MDTuple::getDistinct(Context, None);
unsigned SizeInBits = 2;
unsigned AlignInBits = 3;
unsigned OffsetInBits = 4;
unsigned Flags = 5;
unsigned RuntimeLang = 6;
StringRef Identifier = "some id";
auto *N = MDCompositeType::get(Context, Tag, Name, File, Line, Scope,
BaseType, SizeInBits, AlignInBits,
OffsetInBits, Flags, nullptr, RuntimeLang,
nullptr, nullptr, Identifier);
auto *Elements = MDTuple::getDistinct(Context, None);
EXPECT_EQ(nullptr, N->getElements());
N->replaceElements(Elements);
EXPECT_EQ(Elements, N->getElements());
N->replaceElements(nullptr);
EXPECT_EQ(nullptr, N->getElements());
auto *VTableHolder = MDTuple::getDistinct(Context, None);
EXPECT_EQ(nullptr, N->getVTableHolder());
N->replaceVTableHolder(VTableHolder);
EXPECT_EQ(VTableHolder, N->getVTableHolder());
N->replaceVTableHolder(nullptr);
EXPECT_EQ(nullptr, N->getVTableHolder());
auto *TemplateParams = MDTuple::getDistinct(Context, None);
EXPECT_EQ(nullptr, N->getTemplateParams());
N->replaceTemplateParams(TemplateParams);
EXPECT_EQ(TemplateParams, N->getTemplateParams());
N->replaceTemplateParams(nullptr);
EXPECT_EQ(nullptr, N->getTemplateParams());
}
typedef MetadataTest MDSubroutineTypeTest;
TEST_F(MDSubroutineTypeTest, get) {