Use the ASTMutationListener to track added template specializations in a chained...
[clang.git] / include / clang / AST / DeclContextInternals.h
blob97da6ca9a1ef9f59d65909db204bfdaccaa9033e
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.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
15 #define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
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>
25 namespace clang {
27 class DependentDiagnostic;
29 /// StoredDeclsList - This is an array of decls optimized a common case of only
30 /// containing one entry.
31 struct StoredDeclsList {
33 /// DeclsTy - When in vector form, this is what the Data pointer points to.
34 typedef llvm::SmallVector<NamedDecl *, 4> DeclsTy;
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;
40 public:
41 StoredDeclsList() {}
43 StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) {
44 if (DeclsTy *RHSVec = RHS.getAsVector())
45 Data = new DeclsTy(*RHSVec);
48 ~StoredDeclsList() {
49 // If this is a vector-form, free the vector.
50 if (DeclsTy *Vector = getAsVector())
51 delete Vector;
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;
63 bool isNull() const { return Data.isNull(); }
65 NamedDecl *getAsDecl() const {
66 return Data.dyn_cast<NamedDecl *>();
69 DeclsTy *getAsVector() const {
70 return Data.dyn_cast<DeclsTy *>();
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!");
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;
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);
96 assert(std::find(Vec.begin(), Vec.end(), D)
97 == Vec.end() && "list still contains decl");
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));
107 // If we have a single NamedDecl, return it.
108 if (getAsDecl()) {
109 assert(!isNull() && "Empty list isn't allowed");
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);
116 assert(getAsVector() && "Must have a vector at this point");
117 DeclsTy &Vector = *getAsVector();
119 // Otherwise, we have a range result.
120 return DeclContext::lookup_result(&Vector[0], &Vector[0]+Vector.size());
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;
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;
145 return false;
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.
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;
160 DeclsTy &Vec = *getAsVector();
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.
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);
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;
184 Vec.insert(I, D);
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);
198 class StoredDeclsMap
199 : public llvm::DenseMap<DeclarationName, StoredDeclsList> {
201 public:
202 static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
204 private:
205 friend class ASTContext; // walks the chain deleting these
206 friend class DeclContext;
207 llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
210 class DependentStoredDeclsMap : public StoredDeclsMap {
211 public:
212 DependentStoredDeclsMap() : FirstDiagnostic(0) {}
214 private:
215 friend class DependentDiagnostic;
216 friend class DeclContext; // iterates over diagnostics
218 DependentDiagnostic *FirstDiagnostic;
221 } // end namespace clang
223 #endif