[cfe-commits] r76273 - in /cfe/trunk: include/clang/AST/Decl.h include/clang/AST/DeclBase.h include/clang/AST/DeclTemplate.h lib/AST/ASTContext.cpp lib/AST/Decl.cpp lib/AST/DeclTemplate.cpp lib/Index/DeclReferenceMap.cpp
Argiris Kirtzidis
akyrtzi at gmail.com
Fri Jul 17 17:34:08 PDT 2009
Author: akirtzidis
Date: Fri Jul 17 19:34:07 2009
New Revision: 76273
URL: http://llvm.org/viewvc/llvm-project?rev=76273&view=rev
Log:
Move the functionality of ASTContext::getCanonicalDecl(), into a virtual method Decl::getCanonicalDecl().
Modified:
cfe/trunk/include/clang/AST/Decl.h
cfe/trunk/include/clang/AST/DeclBase.h
cfe/trunk/include/clang/AST/DeclTemplate.h
cfe/trunk/lib/AST/ASTContext.cpp
cfe/trunk/lib/AST/Decl.cpp
cfe/trunk/lib/AST/DeclTemplate.cpp
cfe/trunk/lib/Index/DeclReferenceMap.cpp
Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Fri Jul 17 19:34:07 2009
@@ -437,7 +437,7 @@
return First->PreviousDeclaration.getPointer();
}
- virtual Decl *getPrimaryDecl() const;
+ virtual VarDecl *getCanonicalDecl();
/// \brief Iterates through all the redeclarations of the same var decl.
class redecl_iterator {
@@ -908,7 +908,7 @@
void setPreviousDeclaration(FunctionDecl * PrevDecl);
- virtual Decl *getPrimaryDecl() const;
+ virtual FunctionDecl *getCanonicalDecl();
/// \brief Iterates through all the redeclarations of the same function decl.
class redecl_iterator {
@@ -1316,6 +1316,8 @@
void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
virtual SourceRange getSourceRange() const;
+
+ virtual TagDecl* getCanonicalDecl();
/// isDefinition - Return true if this decl has its body specified.
bool isDefinition() const {
Modified: cfe/trunk/include/clang/AST/DeclBase.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclBase.h?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclBase.h (original)
+++ cfe/trunk/include/clang/AST/DeclBase.h Fri Jul 17 19:34:07 2009
@@ -312,12 +312,14 @@
// be defined inside or outside a function etc).
bool isDefinedOutsideFunctionOrMethod() const;
- /// \brief When there are multiple re-declarations (e.g. for functions),
- /// this will return the primary one which all of them point to.
- virtual Decl *getPrimaryDecl() const { return const_cast<Decl*>(this); }
+ /// \brief Retrieves the "canonical" declaration of the given declaration.
+ virtual Decl *getCanonicalDecl() { return this; }
+ const Decl *getCanonicalDecl() const {
+ return const_cast<Decl*>(this)->getCanonicalDecl();
+ }
/// \brief Whether this particular Decl is a primary one.
- bool isPrimaryDecl() const { return getPrimaryDecl() == this; }
+ bool isCanonicalDecl() const { return getCanonicalDecl() == this; }
/// getBody - If this Decl represents a declaration for a body of code,
/// such as a function or method definition, this method returns the
Modified: cfe/trunk/include/clang/AST/DeclTemplate.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/DeclTemplate.h?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/include/clang/AST/DeclTemplate.h (original)
+++ cfe/trunk/include/clang/AST/DeclTemplate.h Fri Jul 17 19:34:07 2009
@@ -604,6 +604,8 @@
CommonOrPrev = Prev;
}
+ virtual FunctionTemplateDecl *getCanonicalDecl();
+
/// Create a template function node.
static FunctionTemplateDecl *Create(ASTContext &C, DeclContext *DC,
SourceLocation L,
@@ -1027,6 +1029,8 @@
ClassTemplateDecl *getPreviousDeclaration() const {
return PreviousDeclaration;
}
+
+ virtual ClassTemplateDecl *getCanonicalDecl();
/// Create a class template node.
static ClassTemplateDecl *Create(ASTContext &C, DeclContext *DC,
Modified: cfe/trunk/lib/AST/ASTContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTContext.cpp?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTContext.cpp (original)
+++ cfe/trunk/lib/AST/ASTContext.cpp Fri Jul 17 19:34:07 2009
@@ -2085,38 +2085,7 @@
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) {
Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Fri Jul 17 19:34:07 2009
@@ -387,8 +387,11 @@
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 @@
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 @@
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);
Modified: cfe/trunk/lib/AST/DeclTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/DeclTemplate.cpp?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/lib/AST/DeclTemplate.cpp (original)
+++ cfe/trunk/lib/AST/DeclTemplate.cpp Fri Jul 17 19:34:07 2009
@@ -98,6 +98,13 @@
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 @@
// 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,
Modified: cfe/trunk/lib/Index/DeclReferenceMap.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/DeclReferenceMap.cpp?rev=76273&r1=76272&r2=76273&view=diff
==============================================================================
--- cfe/trunk/lib/Index/DeclReferenceMap.cpp (original)
+++ cfe/trunk/lib/Index/DeclReferenceMap.cpp Fri Jul 17 19:34:07 2009
@@ -66,12 +66,12 @@
}
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::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);
}
More information about the cfe-commits
mailing list