[llvm] r234183 - Metadata: Add typed array-like wrapper for MDTuple
Duncan P. N. Exon Smith
dexonsmith at apple.com
Mon Apr 6 10:45:08 PDT 2015
Author: dexonsmith
Date: Mon Apr 6 12:45:07 2015
New Revision: 234183
URL: http://llvm.org/viewvc/llvm-project?rev=234183&view=rev
Log:
Metadata: Add typed array-like wrapper for MDTuple
Add `MDTupleTypedArrayWrapper`, a wrapper around `MDTuple` that adapts
it to look like an array and cast its operands to the given type. This
is designed to be a replacement for `DITypedArray<>`, which is in the
`DIDescriptor` hierarchy.
Modified:
llvm/trunk/include/llvm/IR/DebugInfoMetadata.h
llvm/trunk/include/llvm/IR/Metadata.h
llvm/trunk/lib/IR/Verifier.cpp
Modified: llvm/trunk/include/llvm/IR/DebugInfoMetadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/DebugInfoMetadata.h?rev=234183&r1=234182&r2=234183&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/DebugInfoMetadata.h (original)
+++ llvm/trunk/include/llvm/IR/DebugInfoMetadata.h Mon Apr 6 12:45:07 2015
@@ -607,7 +607,7 @@ public:
return cast_or_null<MDTuple>(getRawElements());
}
Metadata *getVTableHolder() const { return getRawVTableHolder(); }
- MDTuple *getTemplateParams() const {
+ MDTemplateParameterArray getTemplateParams() const {
return cast_or_null<MDTuple>(getRawTemplateParams());
}
StringRef getIdentifier() const { return getStringOperand(7); }
@@ -636,7 +636,7 @@ public:
void replaceVTableHolder(Metadata *VTableHolder) {
replaceOperandWith(5, VTableHolder);
}
- void replaceTemplateParams(MDTuple *TemplateParams) {
+ void replaceTemplateParams(MDTemplateParameterArray TemplateParams) {
replaceOperandWith(6, TemplateParams);
}
/// @}
@@ -844,19 +844,19 @@ public:
StringRef getProducer() const { return getStringOperand(1); }
StringRef getFlags() const { return getStringOperand(2); }
StringRef getSplitDebugFilename() const { return getStringOperand(3); }
- MDTuple *getEnumTypes() const {
+ MDCompositeTypeArray getEnumTypes() const {
return cast_or_null<MDTuple>(getRawEnumTypes());
}
- MDTuple *getRetainedTypes() const {
+ MDTypeArray getRetainedTypes() const {
return cast_or_null<MDTuple>(getRawRetainedTypes());
}
- MDTuple *getSubprograms() const {
+ MDSubprogramArray getSubprograms() const {
return cast_or_null<MDTuple>(getRawSubprograms());
}
- MDTuple *getGlobalVariables() const {
+ MDGlobalVariableArray getGlobalVariables() const {
return cast_or_null<MDTuple>(getRawGlobalVariables());
}
- MDTuple *getImportedEntities() const {
+ MDImportedEntityArray getImportedEntities() const {
return cast_or_null<MDTuple>(getRawImportedEntities());
}
@@ -1103,13 +1103,13 @@ public:
ConstantAsMetadata *getFunction() const {
return cast_or_null<ConstantAsMetadata>(getRawFunction());
}
- MDTuple *getTemplateParams() const {
+ MDTemplateParameterArray getTemplateParams() const {
return cast_or_null<MDTuple>(getRawTemplateParams());
}
MDSubprogram *getDeclaration() const {
return cast_or_null<MDSubprogram>(getRawDeclaration());
}
- MDTuple *getVariables() const {
+ MDLocalVariableArray getVariables() const {
return cast_or_null<MDTuple>(getRawVariables());
}
Modified: llvm/trunk/include/llvm/IR/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Metadata.h?rev=234183&r1=234182&r2=234183&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Metadata.h (original)
+++ llvm/trunk/include/llvm/IR/Metadata.h Mon Apr 6 12:45:07 2015
@@ -1033,6 +1033,56 @@ void TempMDNodeDeleter::operator()(MDNod
MDNode::deleteTemporary(Node);
}
+/// \brief Typed iterator through MDNode operands.
+///
+/// An iterator that transforms an \a MDNode::iterator into an iterator over a
+/// particular Metadata subclass.
+template <class T>
+class TypedMDOperandIterator
+ : std::iterator<std::input_iterator_tag, T *, std::ptrdiff_t, void, T *> {
+ MDNode::op_iterator I;
+
+public:
+ explicit TypedMDOperandIterator(MDNode::op_iterator I) : I(I) {}
+ T *operator*() const { return cast_or_null<T>(*I); }
+ TypedMDOperandIterator &operator++() {
+ ++I;
+ return *this;
+ }
+ TypedMDOperandIterator operator++(int) {
+ TypedMDOperandIterator Temp(*this);
+ ++I;
+ return Temp;
+ }
+ bool operator==(const TypedMDOperandIterator &X) const { return I == X.I; }
+ bool operator!=(const TypedMDOperandIterator &X) const { return I != X.I; }
+};
+
+/// \brief Typed, array-like tuple of metadata.
+///
+/// This is a wrapper for \a MDTuple that makes it act like an array holding a
+/// particular type of metadata.
+template <class T> class MDTupleTypedArrayWrapper {
+ const MDTuple *N = nullptr;
+
+public:
+ MDTupleTypedArrayWrapper(const MDTuple *N) : N(N) {}
+ operator MDTuple *() const { return const_cast<MDTuple *>(N); }
+ MDTuple *operator->() const { return const_cast<MDTuple *>(N); }
+ MDTuple &operator*() const { return *const_cast<MDTuple *>(N); }
+
+ unsigned size() const { return N->getNumOperands(); }
+ T *operator[](unsigned I) const { return cast_or_null<T>(N->getOperand(I)); }
+
+ typedef TypedMDOperandIterator<T> iterator;
+ iterator begin() const { return iterator(N->op_begin()); }
+ iterator end() const { return iterator(N->op_end()); }
+};
+
+#define HANDLE_METADATA(CLASS) \
+ typedef MDTupleTypedArrayWrapper<CLASS> CLASS##Array;
+#include "llvm/IR/Metadata.def"
+
//===----------------------------------------------------------------------===//
/// \brief A tuple of MDNodes.
///
Modified: llvm/trunk/lib/IR/Verifier.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Verifier.cpp?rev=234183&r1=234182&r2=234183&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Verifier.cpp (original)
+++ llvm/trunk/lib/IR/Verifier.cpp Mon Apr 6 12:45:07 2015
@@ -3401,8 +3401,8 @@ void Verifier::verifyTypeRefs() {
// Visit all the compile units again to check the type references.
for (auto *CU : CUs->operands())
- if (auto *Ts = cast<MDCompileUnit>(CU)->getRetainedTypes())
- for (auto &Op : Ts->operands())
+ if (auto Ts = cast<MDCompileUnit>(CU)->getRetainedTypes())
+ for (MDType *Op : Ts)
if (auto *T = dyn_cast<MDCompositeType>(Op))
TypeRefs.erase(T->getRawIdentifier());
if (TypeRefs.empty())
More information about the llvm-commits
mailing list