1 //===- DeclContextInternals.h - DeclContext Representation ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the data structures used in the implementation
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
15 #define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/DeclCXX.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/PointerIntPair.h"
23 #include "llvm/ADT/PointerUnion.h"
24 #include "llvm/ADT/SmallVector.h"
30 class DependentDiagnostic;
32 /// An array of decls optimized for the common case of only containing
34 struct StoredDeclsList {
35 /// When in vector form, this is what the Data pointer points to.
36 using DeclsTy = SmallVector<NamedDecl *, 4>;
38 /// A collection of declarations, with a flag to indicate if we have
39 /// further external declarations.
40 using DeclsAndHasExternalTy = llvm::PointerIntPair<DeclsTy *, 1, bool>;
42 /// The stored data, which will be either a pointer to a NamedDecl,
43 /// or a pointer to a vector with a flag to indicate if there are further
44 /// external declarations.
45 llvm::PointerUnion<NamedDecl *, DeclsAndHasExternalTy> Data;
48 StoredDeclsList() = default;
50 StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
51 RHS.Data = (NamedDecl *)nullptr;
55 // If this is a vector-form, free the vector.
56 if (DeclsTy *Vector = getAsVector())
60 StoredDeclsList &operator=(StoredDeclsList &&RHS) {
61 if (DeclsTy *Vector = getAsVector())
64 RHS.Data = (NamedDecl *)nullptr;
68 bool isNull() const { return Data.isNull(); }
70 NamedDecl *getAsDecl() const {
71 return Data.dyn_cast<NamedDecl *>();
74 DeclsAndHasExternalTy getAsVectorAndHasExternal() const {
75 return Data.dyn_cast<DeclsAndHasExternalTy>();
78 DeclsTy *getAsVector() const {
79 return getAsVectorAndHasExternal().getPointer();
82 bool hasExternalDecls() const {
83 return getAsVectorAndHasExternal().getInt();
86 void setHasExternalDecls() {
87 if (DeclsTy *Vec = getAsVector())
88 Data = DeclsAndHasExternalTy(Vec, true);
90 DeclsTy *VT = new DeclsTy();
91 if (NamedDecl *OldD = getAsDecl())
93 Data = DeclsAndHasExternalTy(VT, true);
97 void setOnlyValue(NamedDecl *ND) {
98 assert(!getAsVector() && "Not inline");
100 // Make sure that Data is a plain NamedDecl* so we can use its address
101 // at getLookupResult.
102 assert(*(NamedDecl **)&Data == ND &&
103 "PointerUnion mangles the NamedDecl pointer!");
106 void remove(NamedDecl *D) {
107 assert(!isNull() && "removing from empty list");
108 if (NamedDecl *Singleton = getAsDecl()) {
109 assert(Singleton == D && "list is different singleton");
111 Data = (NamedDecl *)nullptr;
115 DeclsTy &Vec = *getAsVector();
116 DeclsTy::iterator I = llvm::find(Vec, D);
117 assert(I != Vec.end() && "list does not contain decl");
120 assert(llvm::find(Vec, D) == Vec.end() && "list still contains decl");
123 /// Remove any declarations which were imported from an external
125 void removeExternalDecls() {
128 } else if (NamedDecl *Singleton = getAsDecl()) {
129 if (Singleton->isFromASTFile())
130 *this = StoredDeclsList();
132 DeclsTy &Vec = *getAsVector();
133 Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
134 [](Decl *D) { return D->isFromASTFile(); }),
136 // Don't have any external decls any more.
137 Data = DeclsAndHasExternalTy(&Vec, false);
141 /// getLookupResult - Return an array of all the decls that this list
143 DeclContext::lookup_result getLookupResult() {
145 return DeclContext::lookup_result();
147 // If we have a single NamedDecl, return it.
148 if (NamedDecl *ND = getAsDecl()) {
149 assert(!isNull() && "Empty list isn't allowed");
151 // Data is a raw pointer to a NamedDecl*, return it.
152 return DeclContext::lookup_result(ND);
155 assert(getAsVector() && "Must have a vector at this point");
156 DeclsTy &Vector = *getAsVector();
158 // Otherwise, we have a range result.
159 return DeclContext::lookup_result(Vector);
162 /// HandleRedeclaration - If this is a redeclaration of an existing decl,
163 /// replace the old one with D and return true. Otherwise return false.
164 bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
165 // Most decls only have one entry in their list, special case it.
166 if (NamedDecl *OldD = getAsDecl()) {
167 if (!D->declarationReplaces(OldD, IsKnownNewer))
173 // Determine if this declaration is actually a redeclaration.
174 DeclsTy &Vec = *getAsVector();
175 for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
177 NamedDecl *OldD = *OD;
178 if (D->declarationReplaces(OldD, IsKnownNewer)) {
187 /// AddSubsequentDecl - This is called on the second and later decl when it is
188 /// not a redeclaration to merge it into the appropriate place in our list.
189 void AddSubsequentDecl(NamedDecl *D) {
190 assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
192 // If this is the second decl added to the list, convert this to vector
194 if (NamedDecl *OldD = getAsDecl()) {
195 DeclsTy *VT = new DeclsTy();
197 Data = DeclsAndHasExternalTy(VT, false);
200 DeclsTy &Vec = *getAsVector();
202 // Using directives end up in a special entry which contains only
203 // other using directives, so all this logic is wasted for them.
204 // But avoiding the logic wastes time in the far-more-common case
205 // that we're *not* adding a new using directive.
207 // Tag declarations always go at the end of the list so that an
208 // iterator which points at the first tag will start a span of
209 // decls that only contains tags.
210 if (D->hasTagIdentifierNamespace())
213 // Resolved using declarations go at the front of the list so that
214 // they won't show up in other lookup results. Unresolved using
215 // declarations (which are always in IDNS_Using | IDNS_Ordinary)
216 // follow that so that the using declarations will be contiguous.
217 else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
218 DeclsTy::iterator I = Vec.begin();
219 if (D->getIdentifierNamespace() != Decl::IDNS_Using) {
220 while (I != Vec.end() &&
221 (*I)->getIdentifierNamespace() == Decl::IDNS_Using)
226 // All other declarations go at the end of the list, but before any
227 // tag declarations. But we can be clever about tag declarations
228 // because there can only ever be one in a scope.
229 } else if (!Vec.empty() && Vec.back()->hasTagIdentifierNamespace()) {
230 NamedDecl *TagD = Vec.back();
239 : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
241 static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
244 friend class ASTContext; // walks the chain deleting these
245 friend class DeclContext;
247 llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
250 class DependentStoredDeclsMap : public StoredDeclsMap {
252 DependentStoredDeclsMap() = default;
255 friend class DeclContext; // iterates over diagnostics
256 friend class DependentDiagnostic;
258 DependentDiagnostic *FirstDiagnostic = nullptr;
263 #endif // LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H