DI: Use Metadata for DITypeRef and DIScopeRef

Now that `MDString` and `MDNode` have a common base class, use it.  Note
that it's not useful to assume subclasses of `Metadata` must be one or
the other since we'll be adding more subclasses soon enough.

Part of PR21532.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222064 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith 2014-11-14 23:55:03 +00:00
parent 0737b4ee14
commit f4d2109d2d
2 changed files with 26 additions and 23 deletions

View File

@ -39,6 +39,7 @@ class Value;
class DbgDeclareInst; class DbgDeclareInst;
class DbgValueInst; class DbgValueInst;
class Instruction; class Instruction;
class Metadata;
class MDNode; class MDNode;
class MDString; class MDString;
class NamedMDNode; class NamedMDNode;
@ -333,13 +334,13 @@ template <typename T> class DIRef {
/// \brief Val can be either a MDNode or a MDString. /// \brief Val can be either a MDNode or a MDString.
/// ///
/// In the latter, MDString specifies the type identifier. /// In the latter, MDString specifies the type identifier.
const Value *Val; const Metadata *Val;
explicit DIRef(const Value *V); explicit DIRef(const Metadata *V);
public: public:
T resolve(const DITypeIdentifierMap &Map) const; T resolve(const DITypeIdentifierMap &Map) const;
StringRef getName() const; StringRef getName() const;
operator Value *() const { return const_cast<Value *>(Val); } operator Metadata *() const { return const_cast<Metadata *>(Val); }
}; };
template <typename T> template <typename T>
@ -373,12 +374,12 @@ template <typename T> StringRef DIRef<T>::getName() const {
/// \brief Handle fields that are references to DIScopes. /// \brief Handle fields that are references to DIScopes.
template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const; template <> DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const;
/// \brief Specialize DIRef constructor for DIScopeRef. /// \brief Specialize DIRef constructor for DIScopeRef.
template <> DIRef<DIScope>::DIRef(const Value *V); template <> DIRef<DIScope>::DIRef(const Metadata *V);
/// \brief Handle fields that are references to DITypes. /// \brief Handle fields that are references to DITypes.
template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const; template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const;
/// \brief Specialize DIRef constructor for DITypeRef. /// \brief Specialize DIRef constructor for DITypeRef.
template <> DIRef<DIType>::DIRef(const Value *V); template <> DIRef<DIType>::DIRef(const Metadata *V);
/// \briefThis is a wrapper for a type. /// \briefThis is a wrapper for a type.
/// ///

View File

@ -411,31 +411,33 @@ static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
} }
/// \brief Check if a value can be a reference to a type. /// \brief Check if a value can be a reference to a type.
static bool isTypeRef(const Value *Val) { static bool isTypeRef(const Metadata *MD) {
return !Val || if (!MD)
(isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || return true;
(isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType()); if (auto *S = dyn_cast<MDString>(MD))
return !S->getString().empty();
if (auto *N = dyn_cast<MDNode>(MD))
return DIType(N).isType();
return false;
} }
/// \brief Check if referenced field might be a type. /// \brief Check if referenced field might be a type.
static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
Value *Fld = getField(DbgNode, Elt); return isTypeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
return isTypeRef(Fld);
} }
/// \brief Check if a value can be a ScopeRef. /// \brief Check if a value can be a ScopeRef.
static bool isScopeRef(const Value *Val) { static bool isScopeRef(const Metadata *MD) {
return !Val || if (!MD)
(isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) || return true;
// Not checking for Val->isScope() here, because it would work if (auto *S = dyn_cast<MDString>(MD))
// only for lexical scopes and not all subclasses of DIScope. return !S->getString().empty();
isa<MDNode>(Val); return isa<MDNode>(MD);
} }
/// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef.
static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
Value *Fld = getField(DbgNode, Elt); return isScopeRef(dyn_cast_or_null<Metadata>(getField(DbgNode, Elt)));
return isScopeRef(Fld);
} }
bool DIType::Verify() const { bool DIType::Verify() const {
@ -1465,19 +1467,19 @@ void DIVariable::printExtendedName(raw_ostream &OS) const {
} }
} }
template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) { template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
} }
template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) { template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
} }
template <> template <>
DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const { DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
return DIScopeRef(getField(DbgNode, Elt)); return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
} }
template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const { template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
return DITypeRef(getField(DbgNode, Elt)); return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
} }
bool llvm::StripDebugInfo(Module &M) { bool llvm::StripDebugInfo(Module &M) {