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/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"
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;
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 /// getLookupResult - Return an array of all the decls that this list
102 DeclContext::lookup_result getLookupResult() {
104 return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
105 DeclContext::lookup_iterator(0));
107 // If we have a single NamedDecl, return it.
109 assert(!isNull() && "Empty list isn't allowed");
111 // Data is a raw pointer to a NamedDecl*, return it.
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))
134 // Determine if this declaration is actually a redeclaration.
135 DeclsTy &Vec = *getAsVector();
136 for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
138 NamedDecl *OldD = *OD;
139 if (D->declarationReplaces(OldD)) {
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
154 if (NamedDecl *OldD = getAsDecl()) {
155 DeclsTy *VT = new DeclsTy();
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())
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)
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();
199 : public llvm::DenseMap<DeclarationName, StoredDeclsList> {
202 static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
205 friend class ASTContext; // walks the chain deleting these
206 friend class DeclContext;
207 llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
210 class DependentStoredDeclsMap : public StoredDeclsMap {
212 DependentStoredDeclsMap() : FirstDiagnostic(0) {}
215 friend class DependentDiagnostic;
216 friend class DeclContext; // iterates over diagnostics
218 DependentDiagnostic *FirstDiagnostic;
221 } // end namespace clang