From 2a6dca1f553de927e0872e010f6935b6e8a10fa1 Mon Sep 17 00:00:00 2001 From: Argiris Kirtzidis Date: Mon, 29 Jun 2009 17:38:40 +0000 Subject: [PATCH] -Keep a reference to the ASTContext inside the TranslationUnitDecl. -Introduce Decl::getASTContext() which returns the reference from the TranslationUnitDecl that it is contained in. The general idea is that Decls can point to their own ASTContext so that it is no longer required to "manually" keep track and make sure that you pass the correct ASTContext to Decls' methods, e.g. methods like Decl::getAttrs should eventually not require a ASTContext parameter. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@74434 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Decl.h | 9 +++++++-- include/clang/AST/DeclBase.h | 7 +++++++ lib/AST/Decl.cpp | 2 +- lib/AST/DeclBase.cpp | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/include/clang/AST/Decl.h b/include/clang/AST/Decl.h index e976d833a..c201409f5 100644 --- a/include/clang/AST/Decl.h +++ b/include/clang/AST/Decl.h @@ -30,10 +30,15 @@ class FunctionTemplateSpecializationInfo; /// TranslationUnitDecl - The top declaration context. class TranslationUnitDecl : public Decl, public DeclContext { - TranslationUnitDecl() + ASTContext &Ctx; + + explicit TranslationUnitDecl(ASTContext &ctx) : Decl(TranslationUnit, 0, SourceLocation()), - DeclContext(TranslationUnit) {} + DeclContext(TranslationUnit), + Ctx(ctx) {} public: + ASTContext &getASTContext() const { return Ctx; } + static TranslationUnitDecl *Create(ASTContext &C); // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; } diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index c959b0547..3bf999c74 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -213,6 +213,13 @@ public: const DeclContext *getDeclContext() const { return const_cast(this)->getDeclContext(); } + + TranslationUnitDecl *getTranslationUnitDecl(); + const TranslationUnitDecl *getTranslationUnitDecl() const { + return const_cast(this)->getTranslationUnitDecl(); + } + + ASTContext &getASTContext() const; void setAccess(AccessSpecifier AS) { Access = AS; diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 94a02f418..725b06676 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -41,7 +41,7 @@ void Attr::Destroy(ASTContext &C) { TranslationUnitDecl *TranslationUnitDecl::Create(ASTContext &C) { - return new (C) TranslationUnitDecl(); + return new (C) TranslationUnitDecl(C); } NamespaceDecl *NamespaceDecl::Create(ASTContext &C, DeclContext *DC, diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp index 5815d820a..0ccd6b436 100644 --- a/lib/AST/DeclBase.cpp +++ b/lib/AST/DeclBase.cpp @@ -157,6 +157,22 @@ void Decl::setLexicalDeclContext(DeclContext *DC) { } } +TranslationUnitDecl *Decl::getTranslationUnitDecl() { + DeclContext *DC = getDeclContext(); + assert(DC && "This decl is not contained in a translation unit!"); + + while (!DC->isTranslationUnit()) { + DC = DC->getParent(); + assert(DC && "This decl is not contained in a translation unit!"); + } + + return cast(DC); +} + +ASTContext &Decl::getASTContext() const { + return getTranslationUnitDecl()->getASTContext(); +} + unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) { switch (DeclKind) { default: -- 2.11.4.GIT