]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/llvm/tools/clang/include/clang/AST/DeclContextInternals.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / llvm / tools / clang / include / clang / AST / DeclContextInternals.h
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/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"
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   /// \brief Remove any declarations which were imported from an external
101   /// AST source.
102   void removeExternalDecls() {
103     if (isNull()) {
104       // Nothing to do.
105     } else if (NamedDecl *Singleton = getAsDecl()) {
106       if (Singleton->isFromASTFile())
107         *this = StoredDeclsList();
108     } else {
109       DeclsTy &Vec = *getAsVector();
110       Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
111                                std::mem_fun(&Decl::isFromASTFile)),
112                 Vec.end());
113     }
114   }
115
116   /// getLookupResult - Return an array of all the decls that this list
117   /// represents.
118   DeclContext::lookup_result getLookupResult() {
119     if (isNull())
120       return DeclContext::lookup_result(DeclContext::lookup_iterator(0),
121                                         DeclContext::lookup_iterator(0));
122
123     // If we have a single NamedDecl, return it.
124     if (getAsDecl()) {
125       assert(!isNull() && "Empty list isn't allowed");
126
127       // Data is a raw pointer to a NamedDecl*, return it.
128       void *Ptr = &Data;
129       return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1);
130     }
131
132     assert(getAsVector() && "Must have a vector at this point");
133     DeclsTy &Vector = *getAsVector();
134
135     // Otherwise, we have a range result.
136     return DeclContext::lookup_result(Vector.begin(), Vector.end());
137   }
138
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))
145         return false;
146       setOnlyValue(D);
147       return true;
148     }
149
150     // Determine if this declaration is actually a redeclaration.
151     DeclsTy &Vec = *getAsVector();
152     for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
153          OD != ODEnd; ++OD) {
154       NamedDecl *OldD = *OD;
155       if (D->declarationReplaces(OldD)) {
156         *OD = D;
157         return true;
158       }
159     }
160
161     return false;
162   }
163
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.
166   ///
167   void AddSubsequentDecl(NamedDecl *D) {
168     // If this is the second decl added to the list, convert this to vector
169     // form.
170     if (NamedDecl *OldD = getAsDecl()) {
171       DeclsTy *VT = new DeclsTy();
172       VT->push_back(OldD);
173       Data = VT;
174     }
175
176     DeclsTy &Vec = *getAsVector();
177
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.
182
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())
187       Vec.push_back(D);
188
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)
198           ++I;
199       }
200       Vec.insert(I, D);
201
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();
207       Vec.back() = D;
208       Vec.push_back(TagD);
209     } else
210       Vec.push_back(D);
211   }
212 };
213
214 class StoredDeclsMap
215   : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
216
217 public:
218   static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
219
220 private:
221   friend class ASTContext; // walks the chain deleting these
222   friend class DeclContext;
223   llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
224 };
225
226 class DependentStoredDeclsMap : public StoredDeclsMap {
227 public:
228   DependentStoredDeclsMap() : FirstDiagnostic(0) {}
229
230 private:
231   friend class DependentDiagnostic;
232   friend class DeclContext; // iterates over diagnostics
233
234   DependentDiagnostic *FirstDiagnostic;
235 };
236
237 } // end namespace clang
238
239 #endif