diff options
author | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2009-07-18 00:34:07 +0000 |
---|---|---|
committer | Argyrios Kyrtzidis <akyrtzi@gmail.com> | 2009-07-18 00:34:07 +0000 |
commit | b57a4fe73b8227c0dba651818b8495dfca61e530 (patch) | |
tree | 56854fb0f97e67a7dc7ef19ba5fedb56155a29a9 /lib | |
parent | 0df134715d75c62422502af0f5610885a5a4f472 (diff) |
Move the functionality of ASTContext::getCanonicalDecl(), into a virtual method Decl::getCanonicalDecl().
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@76273 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AST/ASTContext.cpp | 33 | ||||
-rw-r--r-- | lib/AST/Decl.cpp | 22 | ||||
-rw-r--r-- | lib/AST/DeclTemplate.cpp | 14 | ||||
-rw-r--r-- | lib/Index/DeclReferenceMap.cpp | 10 |
4 files changed, 38 insertions, 41 deletions
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index b43aadb62e..f680606dcf 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -2085,38 +2085,7 @@ QualType ASTContext::getCanonicalType(QualType T) { Decl *ASTContext::getCanonicalDecl(Decl *D) { if (!D) return 0; - - if (TagDecl *Tag = dyn_cast<TagDecl>(D)) { - QualType T = getTagDeclType(Tag); - return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType) - ->getDecl()); - } - - if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) { - while (Template->getPreviousDeclaration()) - Template = Template->getPreviousDeclaration(); - return Template; - } - - if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { - while (Function->getPreviousDeclaration()) - Function = Function->getPreviousDeclaration(); - return const_cast<FunctionDecl *>(Function); - } - - if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { - while (FunTmpl->getPreviousDeclaration()) - FunTmpl = FunTmpl->getPreviousDeclaration(); - return FunTmpl; - } - - if (const VarDecl *Var = dyn_cast<VarDecl>(D)) { - while (Var->getPreviousDeclaration()) - Var = Var->getPreviousDeclaration(); - return const_cast<VarDecl *>(Var); - } - - return D; + return D->getCanonicalDecl(); } TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) { diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 728724f1b3..0a6f28d3fb 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -387,8 +387,11 @@ VarDecl *VarDecl::getFirstDeclaration() { return First; } -Decl *VarDecl::getPrimaryDecl() const { - return const_cast<VarDecl *>(getFirstDeclaration()); +VarDecl *VarDecl::getCanonicalDecl() { + VarDecl *Var = this; + while (Var->getPreviousDeclaration()) + Var = Var->getPreviousDeclaration(); + return Var; } //===----------------------------------------------------------------------===// @@ -621,8 +624,11 @@ FunctionDecl *FunctionDecl::getFirstDeclaration() { return First; } -Decl *FunctionDecl::getPrimaryDecl() const { - return const_cast<FunctionDecl *>(getFirstDeclaration()); +FunctionDecl *FunctionDecl::getCanonicalDecl() { + FunctionDecl *FD = this; + while (FD->getPreviousDeclaration()) + FD = FD->getPreviousDeclaration(); + return FD; } /// getOverloadedOperator - Which C++ overloaded operator this @@ -703,6 +709,14 @@ SourceRange TagDecl::getSourceRange() const { return SourceRange(getLocation(), E); } +TagDecl* TagDecl::getCanonicalDecl() { + Type *T = getTypeForDecl(); + if (T == 0) + T = getASTContext().getTagDeclType(this).getTypePtr(); + + return cast<TagDecl>(cast<TagType>(T->getCanonicalTypeInternal())->getDecl()); +} + void TagDecl::startDefinition() { TagType *TagT = const_cast<TagType *>(TypeForDecl->getAsTagType()); TagT->decl.setPointer(this); diff --git a/lib/AST/DeclTemplate.cpp b/lib/AST/DeclTemplate.cpp index f1bd1b67d2..e096e41aed 100644 --- a/lib/AST/DeclTemplate.cpp +++ b/lib/AST/DeclTemplate.cpp @@ -98,6 +98,13 @@ void FunctionTemplateDecl::Destroy(ASTContext &C) { Decl::Destroy(C); } +FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() { + FunctionTemplateDecl *FunTmpl = this; + while (FunTmpl->getPreviousDeclaration()) + FunTmpl = FunTmpl->getPreviousDeclaration(); + return FunTmpl; +} + FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { // Find the first declaration of this function template. FunctionTemplateDecl *First = this; @@ -115,6 +122,13 @@ FunctionTemplateDecl::Common *FunctionTemplateDecl::getCommonPtr() { // ClassTemplateDecl Implementation //===----------------------------------------------------------------------===// +ClassTemplateDecl *ClassTemplateDecl::getCanonicalDecl() { + ClassTemplateDecl *Template = this; + while (Template->getPreviousDeclaration()) + Template = Template->getPreviousDeclaration(); + return Template; +} + ClassTemplateDecl *ClassTemplateDecl::Create(ASTContext &C, DeclContext *DC, SourceLocation L, diff --git a/lib/Index/DeclReferenceMap.cpp b/lib/Index/DeclReferenceMap.cpp index 3ec8794b37..2e5e7c389a 100644 --- a/lib/Index/DeclReferenceMap.cpp +++ b/lib/Index/DeclReferenceMap.cpp @@ -66,12 +66,12 @@ void StmtMapper::VisitDeclStmt(DeclStmt *Node) { } void StmtMapper::VisitDeclRefExpr(DeclRefExpr *Node) { - NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getPrimaryDecl()); + NamedDecl *PrimD = cast<NamedDecl>(Node->getDecl()->getCanonicalDecl()); Map.insert(std::make_pair(PrimD, ASTLocation(Parent, Node))); } void StmtMapper::VisitMemberExpr(MemberExpr *Node) { - NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getPrimaryDecl()); + NamedDecl *PrimD = cast<NamedDecl>(Node->getMemberDecl()->getCanonicalDecl()); Map.insert(std::make_pair(PrimD, ASTLocation(Parent, Node))); } @@ -122,17 +122,17 @@ DeclReferenceMap::DeclReferenceMap(ASTContext &Ctx) { DeclReferenceMap::astlocation_iterator DeclReferenceMap::refs_begin(NamedDecl *D) const { - NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); return astlocation_iterator(Map.lower_bound(Prim)); } DeclReferenceMap::astlocation_iterator DeclReferenceMap::refs_end(NamedDecl *D) const { - NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); return astlocation_iterator(Map.upper_bound(Prim)); } bool DeclReferenceMap::refs_empty(NamedDecl *D) const { - NamedDecl *Prim = cast<NamedDecl>(D->getPrimaryDecl()); + NamedDecl *Prim = cast<NamedDecl>(D->getCanonicalDecl()); return refs_begin(Prim) == refs_end(Prim); } |