From c81307af0f07121f9e9f142078c04aeaae8c5eee Mon Sep 17 00:00:00 2001 From: "Duncan P. N. Exon Smith" Date: Fri, 14 Nov 2014 23:55:03 +0000 Subject: [PATCH] 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. llvm-svn: 222064 --- llvm/include/llvm/IR/DebugInfo.h | 11 ++++++----- llvm/lib/IR/DebugInfo.cpp | 38 ++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h index 2a248e8..4bc7464 100644 --- a/llvm/include/llvm/IR/DebugInfo.h +++ b/llvm/include/llvm/IR/DebugInfo.h @@ -39,6 +39,7 @@ class Value; class DbgDeclareInst; class DbgValueInst; class Instruction; +class Metadata; class MDNode; class MDString; class NamedMDNode; @@ -333,13 +334,13 @@ template class DIRef { /// \brief Val can be either a MDNode or a MDString. /// /// In the latter, MDString specifies the type identifier. - const Value *Val; - explicit DIRef(const Value *V); + const Metadata *Val; + explicit DIRef(const Metadata *V); public: T resolve(const DITypeIdentifierMap &Map) const; StringRef getName() const; - operator Value *() const { return const_cast(Val); } + operator Metadata *() const { return const_cast(Val); } }; template @@ -373,12 +374,12 @@ template StringRef DIRef::getName() const { /// \brief Handle fields that are references to DIScopes. template <> DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const; /// \brief Specialize DIRef constructor for DIScopeRef. -template <> DIRef::DIRef(const Value *V); +template <> DIRef::DIRef(const Metadata *V); /// \brief Handle fields that are references to DITypes. template <> DITypeRef DIDescriptor::getFieldAs(unsigned Elt) const; /// \brief Specialize DIRef constructor for DITypeRef. -template <> DIRef::DIRef(const Value *V); +template <> DIRef::DIRef(const Metadata *V); /// \briefThis is a wrapper for a type. /// diff --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp index 60904c2..de4f213 100644 --- a/llvm/lib/IR/DebugInfo.cpp +++ b/llvm/lib/IR/DebugInfo.cpp @@ -411,31 +411,33 @@ static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) { } /// \brief Check if a value can be a reference to a type. -static bool isTypeRef(const Value *Val) { - return !Val || - (isa(Val) && !cast(Val)->getString().empty()) || - (isa(Val) && DIType(cast(Val)).isType()); +static bool isTypeRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast(MD)) + return !S->getString().empty(); + if (auto *N = dyn_cast(MD)) + return DIType(N).isType(); + return false; } /// \brief Check if referenced field might be a type. static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); - return isTypeRef(Fld); + return isTypeRef(dyn_cast_or_null(getField(DbgNode, Elt))); } /// \brief Check if a value can be a ScopeRef. -static bool isScopeRef(const Value *Val) { - return !Val || - (isa(Val) && !cast(Val)->getString().empty()) || - // Not checking for Val->isScope() here, because it would work - // only for lexical scopes and not all subclasses of DIScope. - isa(Val); +static bool isScopeRef(const Metadata *MD) { + if (!MD) + return true; + if (auto *S = dyn_cast(MD)) + return !S->getString().empty(); + return isa(MD); } /// \brief Check if a field at position Elt of a MDNode can be a ScopeRef. static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) { - Value *Fld = getField(DbgNode, Elt); - return isScopeRef(Fld); + return isScopeRef(dyn_cast_or_null(getField(DbgNode, Elt))); } bool DIType::Verify() const { @@ -1465,19 +1467,19 @@ void DIVariable::printExtendedName(raw_ostream &OS) const { } } -template <> DIRef::DIRef(const Value *V) : Val(V) { +template <> DIRef::DIRef(const Metadata *V) : Val(V) { assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); } -template <> DIRef::DIRef(const Value *V) : Val(V) { +template <> DIRef::DIRef(const Metadata *V) : Val(V) { assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); } template <> DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const { - return DIScopeRef(getField(DbgNode, Elt)); + return DIScopeRef(cast_or_null(getField(DbgNode, Elt))); } template <> DITypeRef DIDescriptor::getFieldAs(unsigned Elt) const { - return DITypeRef(getField(DbgNode, Elt)); + return DITypeRef(cast_or_null(getField(DbgNode, Elt))); } bool llvm::StripDebugInfo(Module &M) { -- 2.7.4