1 //===-- DeclContextInternals.h - DeclContext Representation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the data structures used in the implementation
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/DeclCXX.h"
19 #include "clang/AST/DeclarationName.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/SmallVector.h"
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 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;
43 StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) {
44 if (DeclsTy *RHSVec = RHS.getAsVector())
45 Data = new DeclsTy(*RHSVec);
49 // If this is a vector-form, free the vector.
50 if (DeclsTy *Vector = getAsVector())
54 StoredDeclsList &operator=(const StoredDeclsList &RHS) {
55 if (DeclsTy *Vector = getAsVector())
58 if (DeclsTy *RHSVec = RHS.getAsVector())
59 Data = new DeclsTy(*RHSVec);
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");
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");
87 Data = (NamedDecl *)0;
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");
96 assert(std::find(Vec.begin(), Vec.end(), D)
97 == Vec.end() && "list still contains decl");
100 /// \brief Remove any declarations which were imported from an external
102 void removeExternalDecls() {
105 } else if (NamedDecl *Singleton = getAsDecl()) {
106 if (Singleton->isFromASTFile())
107 *this = StoredDeclsList();
109 DeclsTy &Vec = *getAsVector();
110 Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
111 std::mem_fun(&Decl::isFromASTFile)),
116 /// getLookupResult - Return an array of all the decls that this list
118 DeclContext::lookup_result getLookupResult() {
120 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
121 DeclContext::lookup_iterator(0));
123 // If we have a single NamedDecl, return it.
125 assert(!isNull() && "Empty list isn't allowed");
127 // Data is a raw pointer to a NamedDecl*, return it.
129 return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1);
132 assert(getAsVector() && "Must have a vector at this point");
133 DeclsTy &Vector = *getAsVector();
135 // Otherwise, we have a range result.
136 return DeclContext::lookup_result(Vector.begin(), Vector.end());
139 /// HandleRedeclaration - If this is a redeclaration of an existing decl,
140 /// replace the old one with D and return true. Otherwise return false.
141 bool HandleRedeclaration(NamedDecl *D) {
142 // Most decls only have one entry in their list, special case it.
143 if (NamedDecl *OldD = getAsDecl()) {
144 if (!D->declarationReplaces(OldD))
150 // Determine if this declaration is actually a redeclaration.
151 DeclsTy &Vec = *getAsVector();
152 for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
154 NamedDecl *OldD = *OD;
155 if (D->declarationReplaces(OldD)) {
164 /// AddSubsequentDecl - This is called on the second and later decl when it is
165 /// not a redeclaration to merge it into the appropriate place in our list.
167 void AddSubsequentDecl(NamedDecl *D) {
168 // If this is the second decl added to the list, convert this to vector
170 if (NamedDecl *OldD = getAsDecl()) {
171 DeclsTy *VT = new DeclsTy();
176 DeclsTy &Vec = *getAsVector();
178 // Using directives end up in a special entry which contains only
179 // other using directives, so all this logic is wasted for them.
180 // But avoiding the logic wastes time in the far-more-common case
181 // that we're *not* adding a new using directive.
183 // Tag declarations always go at the end of the list so that an
184 // iterator which points at the first tag will start a span of
185 // decls that only contains tags.
186 if (D->hasTagIdentifierNamespace())
189 // Resolved using declarations go at the front of the list so that
190 // they won't show up in other lookup results. Unresolved using
191 // declarations (which are always in IDNS_Using | IDNS_Ordinary)
192 // follow that so that the using declarations will be contiguous.
193 else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
194 DeclsTy::iterator I = Vec.begin();
195 if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
196 while (I != Vec.end() &&
197 (*I)->getIdentifierNamespace() == Decl::IDNS_Using)
202 // All other declarations go at the end of the list, but before any
203 // tag declarations. But we can be clever about tag declarations
204 // because there can only ever be one in a scope.
205 } else if (!Vec.empty() && Vec.back()->hasTagIdentifierNamespace()) {
206 NamedDecl *TagD = Vec.back();
215 : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
218 static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
221 friend class ASTContext; // walks the chain deleting these
222 friend class DeclContext;
223 llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
226 class DependentStoredDeclsMap : public StoredDeclsMap {
228 DependentStoredDeclsMap() : FirstDiagnostic(0) {}
231 friend class DependentDiagnostic;
232 friend class DeclContext; // iterates over diagnostics
234 DependentDiagnostic *FirstDiagnostic;
237 } // end namespace clang