Home | History | Annotate | Download | only in AST
      1 //===-- DeclContextInternals.h - DeclContext Representation -----*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 //  This file defines the data structures used in the implementation
     11 //  of DeclContext.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 #ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
     15 #define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
     16 
     17 #include "clang/AST/Decl.h"
     18 #include "clang/AST/DeclarationName.h"
     19 #include "clang/AST/DeclCXX.h"
     20 #include "llvm/ADT/DenseMap.h"
     21 #include "llvm/ADT/PointerUnion.h"
     22 #include "llvm/ADT/SmallVector.h"
     23 #include <algorithm>
     24 
     25 namespace clang {
     26 
     27 class DependentDiagnostic;
     28 
     29 /// StoredDeclsList - This is an array of decls optimized a common case of only
     30 /// containing one entry.
     31 struct StoredDeclsList {
     32 
     33   /// DeclsTy - When in vector form, this is what the Data pointer points to.
     34   typedef SmallVector<NamedDecl *, 4> DeclsTy;
     35 
     36   /// \brief The stored data, which will be either a pointer to a NamedDecl,
     37   /// or a pointer to a vector.
     38   llvm::PointerUnion<NamedDecl *, DeclsTy *> Data;
     39 
     40 public:
     41   StoredDeclsList() {}
     42 
     43   StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) {
     44     if (DeclsTy *RHSVec = RHS.getAsVector())
     45       Data = new DeclsTy(*RHSVec);
     46   }
     47 
     48   ~StoredDeclsList() {
     49     // If this is a vector-form, free the vector.
     50     if (DeclsTy *Vector = getAsVector())
     51       delete Vector;
     52   }
     53 
     54   StoredDeclsList &operator=(const StoredDeclsList &RHS) {
     55     if (DeclsTy *Vector = getAsVector())
     56       delete Vector;
     57     Data = RHS.Data;
     58     if (DeclsTy *RHSVec = RHS.getAsVector())
     59       Data = new DeclsTy(*RHSVec);
     60     return *this;
     61   }
     62 
     63   bool isNull() const { return Data.isNull(); }
     64 
     65   NamedDecl *getAsDecl() const {
     66     return Data.dyn_cast<NamedDecl *>();
     67   }
     68 
     69   DeclsTy *getAsVector() const {
     70     return Data.dyn_cast<DeclsTy *>();
     71   }
     72 
     73   void setOnlyValue(NamedDecl *ND) {
     74     assert(!getAsVector() && "Not inline");
     75     Data = ND;
     76     // Make sure that Data is a plain NamedDecl* so we can use its address
     77     // at getLookupResult.
     78     assert(*(NamedDecl **)&Data == ND &&
     79            "PointerUnion mangles the NamedDecl pointer!");
     80   }
     81 
     82   void remove(NamedDecl *D) {
     83     assert(!isNull() && "removing from empty list");
     84     if (NamedDecl *Singleton = getAsDecl()) {
     85       assert(Singleton == D && "list is different singleton");
     86       (void)Singleton;
     87       Data = (NamedDecl *)0;
     88       return;
     89     }
     90 
     91     DeclsTy &Vec = *getAsVector();
     92     DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
     93     assert(I != Vec.end() && "list does not contain decl");
     94     Vec.erase(I);
     95 
     96     assert(std::find(Vec.begin(), Vec.end(), D)
     97              == Vec.end() && "list still contains decl");
     98   }
     99 
    100   /// getLookupResult - Return an array of all the decls that this list
    101   /// represents.
    102   DeclContext::lookup_result getLookupResult() {
    103     if (isNull())
    104       return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
    105                                         DeclContext::lookup_iterator(0));
    106 
    107     // If we have a single NamedDecl, return it.
    108     if (getAsDecl()) {
    109       assert(!isNull() && "Empty list isn't allowed");
    110 
    111       // Data is a raw pointer to a NamedDecl*, return it.
    112       void *Ptr = &Data;
    113       return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1);
    114     }
    115 
    116     assert(getAsVector() && "Must have a vector at this point");
    117     DeclsTy &Vector = *getAsVector();
    118 
    119     // Otherwise, we have a range result.
    120     return DeclContext::lookup_result(&Vector[0], &Vector[0]+Vector.size());
    121   }
    122 
    123   /// HandleRedeclaration - If this is a redeclaration of an existing decl,
    124   /// replace the old one with D and return true.  Otherwise return false.
    125   bool HandleRedeclaration(NamedDecl *D) {
    126     // Most decls only have one entry in their list, special case it.
    127     if (NamedDecl *OldD = getAsDecl()) {
    128       if (!D->declarationReplaces(OldD))
    129         return false;
    130       setOnlyValue(D);
    131       return true;
    132     }
    133 
    134     // Determine if this declaration is actually a redeclaration.
    135     DeclsTy &Vec = *getAsVector();
    136     for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
    137          OD != ODEnd; ++OD) {
    138       NamedDecl *OldD = *OD;
    139       if (D->declarationReplaces(OldD)) {
    140         *OD = D;
    141         return true;
    142       }
    143     }
    144 
    145     return false;
    146   }
    147 
    148   /// AddSubsequentDecl - This is called on the second and later decl when it is
    149   /// not a redeclaration to merge it into the appropriate place in our list.
    150   ///
    151   void AddSubsequentDecl(NamedDecl *D) {
    152     // If this is the second decl added to the list, convert this to vector
    153     // form.
    154     if (NamedDecl *OldD = getAsDecl()) {
    155       DeclsTy *VT = new DeclsTy();
    156       VT->push_back(OldD);
    157       Data = VT;
    158     }
    159 
    160     DeclsTy &Vec = *getAsVector();
    161 
    162     // Using directives end up in a special entry which contains only
    163     // other using directives, so all this logic is wasted for them.
    164     // But avoiding the logic wastes time in the far-more-common case
    165     // that we're *not* adding a new using directive.
    166 
    167     // Tag declarations always go at the end of the list so that an
    168     // iterator which points at the first tag will start a span of
    169     // decls that only contains tags.
    170     if (D->hasTagIdentifierNamespace())
    171       Vec.push_back(D);
    172 
    173     // Resolved using declarations go at the front of the list so that
    174     // they won't show up in other lookup results.  Unresolved using
    175     // declarations (which are always in IDNS_Using | IDNS_Ordinary)
    176     // follow that so that the using declarations will be contiguous.
    177     else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
    178       DeclsTy::iterator I = Vec.begin();
    179       if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
    180         while (I != Vec.end() &&
    181                (*I)->getIdentifierNamespace() == Decl::IDNS_Using)
    182           ++I;
    183       }
    184       Vec.insert(I, D);
    185 
    186     // All other declarations go at the end of the list, but before any
    187     // tag declarations.  But we can be clever about tag declarations
    188     // because there can only ever be one in a scope.
    189     } else if (Vec.back()->hasTagIdentifierNamespace()) {
    190       NamedDecl *TagD = Vec.back();
    191       Vec.back() = D;
    192       Vec.push_back(TagD);
    193     } else
    194       Vec.push_back(D);
    195   }
    196 };
    197 
    198 class StoredDeclsMap
    199   : public llvm::DenseMap<DeclarationName, StoredDeclsList> {
    200 
    201 public:
    202   static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
    203 
    204 private:
    205   friend class ASTContext; // walks the chain deleting these
    206   friend class DeclContext;
    207   llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
    208 };
    209 
    210 class DependentStoredDeclsMap : public StoredDeclsMap {
    211 public:
    212   DependentStoredDeclsMap() : FirstDiagnostic(0) {}
    213 
    214 private:
    215   friend class DependentDiagnostic;
    216   friend class DeclContext; // iterates over diagnostics
    217 
    218   DependentDiagnostic *FirstDiagnostic;
    219 };
    220 
    221 } // end namespace clang
    222 
    223 #endif
    224