From 2c46deb1d07f4588ee70059cdd4c7145f81bc8e8 Mon Sep 17 00:00:00 2001 From: Manman Ren Date: Tue, 10 Sep 2013 18:30:07 +0000 Subject: [PATCH] Debug Info: define a DIRef template. Specialize the constructors for DIRef and DIRef to make sure the Value is indeed a scope ref and a type ref. Use DIScopeRef for DIScope::getContext and DIType::getContext and use DITypeRef for getContainingType and getClassType. DIScope::generateRef now returns a DIScopeRef instead of a "Value *" for readability and type safety. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190418 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/DebugInfo.h | 97 ++++++++++++++++++--------- lib/CodeGen/AsmPrinter/DwarfDebug.cpp | 5 -- lib/CodeGen/AsmPrinter/DwarfDebug.h | 5 +- lib/IR/DebugInfo.cpp | 41 ++++++----- 4 files changed, 88 insertions(+), 60 deletions(-) diff --git a/include/llvm/DebugInfo.h b/include/llvm/DebugInfo.h index e9a78314bcc..deebcfd4395 100644 --- a/include/llvm/DebugInfo.h +++ b/include/llvm/DebugInfo.h @@ -17,6 +17,7 @@ #ifndef LLVM_DEBUGINFO_H #define LLVM_DEBUGINFO_H +#include "llvm/Support/Casting.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" @@ -46,7 +47,7 @@ namespace llvm { class DILexicalBlockFile; class DIVariable; class DIType; - class DIScopeRef; + class DIScope; class DIObjCProperty; /// Maps from type identifier to the actual MDNode. @@ -56,9 +57,10 @@ namespace llvm { /// This should not be stored in a container, because the underlying MDNode /// may change in certain situations. class DIDescriptor { - // Befriends DIScopeRef so DIScopeRef can befriend the protected member - // function: getFieldAs. - friend class DIScopeRef; + // Befriends DIRef so DIRef can befriend the protected member + // function: getFieldAs. + template + friend class DIRef; public: enum { FlagPrivate = 1 << 0, @@ -151,10 +153,6 @@ namespace llvm { void dump() const; }; - /// npecialize getFieldAs to handle fields that are references to DIScopes. - template <> - DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const; - /// DISubrange - This is used to represent ranges, for array bounds. class DISubrange : public DIDescriptor { friend class DIDescriptor; @@ -192,6 +190,10 @@ namespace llvm { bool Verify() const; }; + template + class DIRef; + typedef DIRef DIScopeRef; + /// DIScope - A base class for various scopes. class DIScope : public DIDescriptor { protected: @@ -208,23 +210,7 @@ namespace llvm { /// Generate a reference to this DIScope. Uses the type identifier instead /// of the actual MDNode if possible, to help type uniquing. - Value *generateRef(); - }; - - /// Represents reference to a DIScope, abstracts over direct and - /// identifier-based metadata scope references. - class DIScopeRef { - template - friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const; - friend DIScopeRef DIScope::getContext() const; - - /// Val can be either a MDNode or a MDString, in the latter, - /// MDString specifies the type identifier. - const Value *Val; - explicit DIScopeRef(const Value *V); - public: - DIScope resolve(const DITypeIdentifierMap &Map) const; - operator Value *() const { return const_cast(Val); } + DIScopeRef generateRef(); }; /// DIType - This is a wrapper for a type. @@ -241,7 +227,7 @@ namespace llvm { /// Verify - Verify that a type descriptor is well formed. bool Verify() const; - DIScopeRef getContext() const { return getFieldAs(2); } + DIScopeRef getContext() const; StringRef getName() const { return getStringField(3); } unsigned getLineNumber() const { return getUnsignedField(4); } uint64_t getSizeInBits() const { return getUInt64Field(5); } @@ -297,6 +283,53 @@ namespace llvm { void replaceAllUsesWith(MDNode *D); }; + /// Represents reference to a DIDescriptor, abstracts over direct and + /// identifier-based metadata references. + template + class DIRef { + template + friend DescTy DIDescriptor::getFieldAs(unsigned Elt) const; + friend DIScopeRef DIScope::getContext() const; + friend DIScopeRef DIScope::generateRef(); + + /// 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); + public: + T resolve(const DITypeIdentifierMap &Map) const { + if (!Val) + return T(); + + if (const MDNode *MD = dyn_cast(Val)) + return T(MD); + + const MDString *MS = cast(Val); + // Find the corresponding MDNode. + DITypeIdentifierMap::const_iterator Iter = Map.find(MS); + assert(Iter != Map.end() && "Identifier not in the type map?"); + assert(DIType(Iter->second).isType() && + "MDNode in DITypeIdentifierMap should be a DIType."); + return T(Iter->second); + } + operator Value *() const { return const_cast(Val); } + }; + + /// Specialize getFieldAs to handle fields that are references to DIScopes. + template <> + DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const; + /// Specialize DIRef constructor for DIScopeRef. + template <> + DIRef::DIRef(const Value *V); + + typedef DIRef DITypeRef; + /// Specialize getFieldAs to handle fields that are references to DITypes. + template <> + DITypeRef DIDescriptor::getFieldAs(unsigned Elt) const; + /// Specialize DIRef constructor for DITypeRef. + template <> + DIRef::DIRef(const Value *V); + /// DIBasicType - A basic type, like 'int' or 'float'. class DIBasicType : public DIType { public: @@ -328,9 +361,9 @@ namespace llvm { /// associated with one. MDNode *getObjCProperty() const; - DIScopeRef getClassType() const { + DITypeRef getClassType() const { assert(getTag() == dwarf::DW_TAG_ptr_to_member_type); - return getFieldAs(10); + return getFieldAs(10); } Constant *getConstant() const { @@ -358,8 +391,8 @@ namespace llvm { void setTypeArray(DIArray Elements, DIArray TParams = DIArray()); void addMember(DIDescriptor D); unsigned getRunTimeLang() const { return getUnsignedField(11); } - DIScopeRef getContainingType() const { - return getFieldAs(12); + DITypeRef getContainingType() const { + return getFieldAs(12); } void setContainingType(DICompositeType ContainingType); DIArray getTemplateParams() const { return getFieldAs(13); } @@ -426,8 +459,8 @@ namespace llvm { unsigned getVirtuality() const { return getUnsignedField(10); } unsigned getVirtualIndex() const { return getUnsignedField(11); } - DIScopeRef getContainingType() const { - return getFieldAs(12); + DITypeRef getContainingType() const { + return getFieldAs(12); } unsigned getFlags() const { diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp index 7db6df0e19a..608ce6a5688 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -2644,8 +2644,3 @@ void DwarfDebug::emitDebugStrDWO() { InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(), OffSec, StrSym); } - -/// Find the MDNode for the given scope reference. -DIScope DwarfDebug::resolve(DIScopeRef SRef) const { - return SRef.resolve(TypeIdentifierMap); -} diff --git a/lib/CodeGen/AsmPrinter/DwarfDebug.h b/lib/CodeGen/AsmPrinter/DwarfDebug.h index e026c668135..6c13b9ec6f9 100644 --- a/lib/CodeGen/AsmPrinter/DwarfDebug.h +++ b/lib/CodeGen/AsmPrinter/DwarfDebug.h @@ -684,7 +684,10 @@ public: unsigned getDwarfVersion() const { return DwarfVersion; } /// Find the MDNode for the given scope reference. - DIScope resolve(DIScopeRef SRef) const; + template + T resolve(DIRef Ref) const { + return Ref.resolve(TypeIdentifierMap); + } /// isSubprogramContext - Return true if Context is either a subprogram /// or another context nested inside a subprogram. diff --git a/lib/IR/DebugInfo.cpp b/lib/IR/DebugInfo.cpp index 51c9e58b2d7..87984a09759 100644 --- a/lib/IR/DebugInfo.cpp +++ b/lib/IR/DebugInfo.cpp @@ -725,13 +725,13 @@ void DICompositeType::addMember(DIDescriptor D) { /// Generate a reference to this DIType. Uses the type identifier instead /// of the actual MDNode if possible, to help type uniquing. -Value *DIScope::generateRef() { +DIScopeRef DIScope::generateRef() { if (!isCompositeType()) - return *this; + return DIScopeRef(*this); DICompositeType DTy(DbgNode); if (!DTy.getIdentifier()) - return *this; - return DTy.getIdentifier(); + return DIScopeRef(*this); + return DIScopeRef(DTy.getIdentifier()); } /// \brief Set the containing type. @@ -1432,26 +1432,14 @@ void DIVariable::printExtendedName(raw_ostream &OS) const { } } -DIScopeRef::DIScopeRef(const Value *V) : Val(V) { +/// Specialize constructor to make sure it has the correct type. +template <> +DIRef::DIRef(const Value *V) : Val(V) { assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode"); } - -/// Given a DITypeIdentifierMap, tries to find the corresponding -/// DIScope for a DIScopeRef. -DIScope DIScopeRef::resolve(const DITypeIdentifierMap &Map) const { - if (!Val) - return DIScope(); - - if (const MDNode *MD = dyn_cast(Val)) - return DIScope(MD); - - const MDString *MS = cast(Val); - // Find the corresponding MDNode. - DITypeIdentifierMap::const_iterator Iter = Map.find(MS); - assert(Iter != Map.end() && "Identifier not in the type map?"); - assert(DIType(Iter->second).isType() && - "MDNode in DITypeIdentifierMap should be a DIType."); - return DIScope(Iter->second); +template <> +DIRef::DIRef(const Value *V) : Val(V) { + assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode"); } /// Specialize getFieldAs to handle fields that are references to DIScopes. @@ -1459,3 +1447,12 @@ template <> DIScopeRef DIDescriptor::getFieldAs(unsigned Elt) const { return DIScopeRef(getField(DbgNode, Elt)); } +/// Specialize getFieldAs to handle fields that are references to DITypes. +template <> +DITypeRef DIDescriptor::getFieldAs(unsigned Elt) const { + return DITypeRef(getField(DbgNode, Elt)); +} + +DIScopeRef DIType::getContext() const { + return getFieldAs(2); +}