]> granicus.if.org Git - clang/commitdiff
-Keep a reference to the ASTContext inside the TranslationUnitDecl.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 29 Jun 2009 17:38:40 +0000 (17:38 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 29 Jun 2009 17:38:40 +0000 (17:38 +0000)
-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
include/clang/AST/DeclBase.h
lib/AST/Decl.cpp
lib/AST/DeclBase.cpp

index e976d833acae773e8f5e148367b4796f06e2488b..c201409f5171c09efbcd83d6972ec757a290aa02 100644 (file)
@@ -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; }
index c959b05473fee51fe883c412e8e0f72d0a4aa18f..3bf999c74b7f37adffabcfa9c09ea011f4e31a92 100644 (file)
@@ -213,6 +213,13 @@ public:
   const DeclContext *getDeclContext() const {
     return const_cast<Decl*>(this)->getDeclContext();
   }
+
+  TranslationUnitDecl *getTranslationUnitDecl();
+  const TranslationUnitDecl *getTranslationUnitDecl() const {
+    return const_cast<Decl*>(this)->getTranslationUnitDecl();
+  }
+
+  ASTContext &getASTContext() const;
   
   void setAccess(AccessSpecifier AS) {
     Access = AS; 
index 94a02f418ef9ebe3e6f1d7144553ccf523349fb0..725b06676e69594774406be559b03ef54d88e50c 100644 (file)
@@ -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,
index 5815d820aef609ccbda77470ed40231a28968098..0ccd6b436b8a81ea8a94fd1270671e68c564ab42 100644 (file)
@@ -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<TranslationUnitDecl>(DC);
+}
+
+ASTContext &Decl::getASTContext() const {
+  return getTranslationUnitDecl()->getASTContext();  
+}
+
 unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
   switch (DeclKind) {
     default: