From 87bcee88d9b49de8214aa23d07c96f7bec3198e0 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Sat, 19 Oct 2013 16:55:03 +0000 Subject: [PATCH] Simplify some implementations of get*Decl. * NamedDecl and CXXMethodDecl were missing getMostRecentDecl. * The const version can just forward to the non const. * getMostRecentDecl can use cast instead of cast_or_null. This then removes some casts from the callers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193039 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Decl.h | 33 +++++++++++++++++++------------- include/clang/AST/DeclCXX.h | 28 +++++++++++++++++---------- include/clang/AST/DeclTemplate.h | 5 ++--- lib/AST/Decl.cpp | 2 +- lib/Analysis/ThreadSafety.cpp | 6 ++---- lib/Sema/SemaDecl.cpp | 2 +- lib/Serialization/ASTReader.cpp | 2 +- 7 files changed, 45 insertions(+), 33 deletions(-) diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index a7e41ea2d8..e390a66c9c 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -273,6 +273,13 @@ public: return const_cast(this)->getUnderlyingDecl(); } + NamedDecl *getMostRecentDecl() { + return cast(Decl::getMostRecentDecl()); + } + const NamedDecl *getMostRecentDecl() const { + return const_cast(this)->getMostRecentDecl(); + } + static bool classof(const Decl *D) { return classofKind(D->getKind()); } static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; } }; @@ -2810,22 +2817,22 @@ public: return cast(TagDecl::getCanonicalDecl()); } const EnumDecl *getCanonicalDecl() const { - return cast(TagDecl::getCanonicalDecl()); + return const_cast(this)->getCanonicalDecl(); } - const EnumDecl *getPreviousDecl() const { - return cast_or_null(TagDecl::getPreviousDecl()); - } EnumDecl *getPreviousDecl() { return cast_or_null(TagDecl::getPreviousDecl()); } - - const EnumDecl *getMostRecentDecl() const { - return cast(TagDecl::getMostRecentDecl()); + const EnumDecl *getPreviousDecl() const { + return const_cast(this)->getPreviousDecl(); } + EnumDecl *getMostRecentDecl() { return cast(TagDecl::getMostRecentDecl()); } + const EnumDecl *getMostRecentDecl() const { + return const_cast(this)->getMostRecentDecl(); + } EnumDecl *getDefinition() const { return cast_or_null(TagDecl::getDefinition()); @@ -3020,19 +3027,19 @@ public: IdentifierInfo *Id, RecordDecl* PrevDecl = 0); static RecordDecl *CreateDeserialized(const ASTContext &C, unsigned ID); - const RecordDecl *getPreviousDecl() const { - return cast_or_null(TagDecl::getPreviousDecl()); - } RecordDecl *getPreviousDecl() { return cast_or_null(TagDecl::getPreviousDecl()); } - - const RecordDecl *getMostRecentDecl() const { - return cast(TagDecl::getMostRecentDecl()); + const RecordDecl *getPreviousDecl() const { + return const_cast(this)->getPreviousDecl(); } + RecordDecl *getMostRecentDecl() { return cast(TagDecl::getMostRecentDecl()); } + const RecordDecl *getMostRecentDecl() const { + return const_cast(this)->getMostRecentDecl(); + } bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h index 7b5e0cbb4a..7681a16b0e 100644 --- a/include/clang/AST/DeclCXX.h +++ b/include/clang/AST/DeclCXX.h @@ -641,18 +641,19 @@ public: return cast(RecordDecl::getCanonicalDecl()); } - const CXXRecordDecl *getPreviousDecl() const { - return cast_or_null(RecordDecl::getPreviousDecl()); - } CXXRecordDecl *getPreviousDecl() { return cast_or_null(RecordDecl::getPreviousDecl()); } - - const CXXRecordDecl *getMostRecentDecl() const { - return cast_or_null(RecordDecl::getMostRecentDecl()); + const CXXRecordDecl *getPreviousDecl() const { + return const_cast(this)->getPreviousDecl(); } + CXXRecordDecl *getMostRecentDecl() { - return cast_or_null(RecordDecl::getMostRecentDecl()); + return cast(RecordDecl::getMostRecentDecl()); + } + + const CXXRecordDecl *getMostRecentDecl() const { + return const_cast(this)->getMostRecentDecl(); } CXXRecordDecl *getDefinition() const { @@ -1716,12 +1717,19 @@ public: /// \brief Determine whether this is a move assignment operator. bool isMoveAssignmentOperator() const; - const CXXMethodDecl *getCanonicalDecl() const { - return cast(FunctionDecl::getCanonicalDecl()); - } CXXMethodDecl *getCanonicalDecl() { return cast(FunctionDecl::getCanonicalDecl()); } + const CXXMethodDecl *getCanonicalDecl() const { + return const_cast(this)->getCanonicalDecl(); + } + + CXXMethodDecl *getMostRecentDecl() { + return cast(FunctionDecl::getMostRecentDecl()); + } + const CXXMethodDecl *getMostRecentDecl() const { + return const_cast(this)->getMostRecentDecl(); + } /// True if this method is user-declared and was not /// deleted or defaulted on its first declaration. diff --git a/include/clang/AST/DeclTemplate.h b/include/clang/AST/DeclTemplate.h index 38b0799965..26f6891b5b 100644 --- a/include/clang/AST/DeclTemplate.h +++ b/include/clang/AST/DeclTemplate.h @@ -1449,8 +1449,7 @@ public: bool Qualified) const; ClassTemplateSpecializationDecl *getMostRecentDecl() { - CXXRecordDecl *Recent - = cast(CXXRecordDecl::getMostRecentDecl()); + CXXRecordDecl *Recent = CXXRecordDecl::getMostRecentDecl(); while (!isa(Recent)) { // FIXME: Does injected class name need to be in the redeclarations chain? assert(Recent->isInjectedClassName() && Recent->getPreviousDecl()); @@ -2310,7 +2309,7 @@ public: bool Qualified) const; VarTemplateSpecializationDecl *getMostRecentDecl() { - VarDecl *Recent = cast(VarDecl::getMostRecentDecl()); + VarDecl *Recent = VarDecl::getMostRecentDecl(); return cast(Recent); } diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 484248f09f..89eeb5e8a1 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -972,7 +972,7 @@ NamedDecl::getExplicitVisibility(ExplicitVisibilityKind kind) const { kind); // Use the most recent declaration. - const NamedDecl *MostRecent = cast(this->getMostRecentDecl()); + const NamedDecl *MostRecent = getMostRecentDecl(); if (MostRecent != this) return MostRecent->getExplicitVisibility(kind); diff --git a/lib/Analysis/ThreadSafety.cpp b/lib/Analysis/ThreadSafety.cpp index d0655878c7..e5e5f18b51 100644 --- a/lib/Analysis/ThreadSafety.cpp +++ b/lib/Analysis/ThreadSafety.cpp @@ -323,8 +323,7 @@ private: } else if (const CXXMemberCallExpr *CMCE = dyn_cast(Exp)) { // When calling a function with a lock_returned attribute, replace // the function call with the expression in lock_returned. - const CXXMethodDecl* MD = - cast(CMCE->getMethodDecl()->getMostRecentDecl()); + const CXXMethodDecl *MD = CMCE->getMethodDecl()->getMostRecentDecl(); if (LockReturnedAttr* At = MD->getAttr()) { CallingContext LRCallCtx(CMCE->getMethodDecl()); LRCallCtx.SelfArg = CMCE->getImplicitObjectArgument(); @@ -353,8 +352,7 @@ private: NodeVec[Root].setSize(Sz + 1); return Sz + 1; } else if (const CallExpr *CE = dyn_cast(Exp)) { - const FunctionDecl* FD = - cast(CE->getDirectCallee()->getMostRecentDecl()); + const FunctionDecl *FD = CE->getDirectCallee()->getMostRecentDecl(); if (LockReturnedAttr* At = FD->getAttr()) { CallingContext LRCallCtx(CE->getDirectCallee()); LRCallCtx.NumArgs = CE->getNumArgs(); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 2b48de421c..d7c4f7834f 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -4487,7 +4487,7 @@ NamedDecl *Sema::findLocallyScopedExternCDecl(DeclarationName Name) { } NamedDecl *D = LocallyScopedExternCDecls.lookup(Name); - return D ? cast(D->getMostRecentDecl()) : 0; + return D ? D->getMostRecentDecl() : 0; } /// \brief Diagnose function specifiers on a declaration of an identifier that diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index 6fe03c7d55..7e68656cdb 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -7580,7 +7580,7 @@ void ASTReader::FinishedDeserializing() { } void ASTReader::pushExternalDeclIntoScope(NamedDecl *D, DeclarationName Name) { - D = cast(D->getMostRecentDecl()); + D = D->getMostRecentDecl(); if (SemaObj->IdResolver.tryAddTopLevelDecl(D, Name) && SemaObj->TUScope) { SemaObj->TUScope->AddDecl(D); -- 2.40.0