clang  6.0.0svn
DeclContextInternals.h
Go to the documentation of this file.
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 
15 #ifndef LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
16 #define LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
17 
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/PointerIntPair.h"
24 #include "llvm/ADT/PointerUnion.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include <algorithm>
27 #include <cassert>
28 
29 namespace clang {
30 
31 class DependentDiagnostic;
32 
33 /// \brief An array of decls optimized for the common case of only containing
34 /// one entry.
36  /// \brief When in vector form, this is what the Data pointer points to.
38 
39  /// \brief A collection of declarations, with a flag to indicate if we have
40  /// further external declarations.
41  using DeclsAndHasExternalTy = llvm::PointerIntPair<DeclsTy *, 1, bool>;
42 
43  /// \brief The stored data, which will be either a pointer to a NamedDecl,
44  /// or a pointer to a vector with a flag to indicate if there are further
45  /// external declarations.
46  llvm::PointerUnion<NamedDecl *, DeclsAndHasExternalTy> Data;
47 
48 public:
49  StoredDeclsList() = default;
50 
51  StoredDeclsList(StoredDeclsList &&RHS) : Data(RHS.Data) {
52  RHS.Data = (NamedDecl *)nullptr;
53  }
54 
56  // If this is a vector-form, free the vector.
57  if (DeclsTy *Vector = getAsVector())
58  delete Vector;
59  }
60 
62  if (DeclsTy *Vector = getAsVector())
63  delete Vector;
64  Data = RHS.Data;
65  RHS.Data = (NamedDecl *)nullptr;
66  return *this;
67  }
68 
69  bool isNull() const { return Data.isNull(); }
70 
71  NamedDecl *getAsDecl() const {
72  return Data.dyn_cast<NamedDecl *>();
73  }
74 
76  return Data.dyn_cast<DeclsAndHasExternalTy>();
77  }
78 
79  DeclsTy *getAsVector() const {
80  return getAsVectorAndHasExternal().getPointer();
81  }
82 
83  bool hasExternalDecls() const {
84  return getAsVectorAndHasExternal().getInt();
85  }
86 
88  if (DeclsTy *Vec = getAsVector())
89  Data = DeclsAndHasExternalTy(Vec, true);
90  else {
91  DeclsTy *VT = new DeclsTy();
92  if (NamedDecl *OldD = getAsDecl())
93  VT->push_back(OldD);
94  Data = DeclsAndHasExternalTy(VT, true);
95  }
96  }
97 
98  void setOnlyValue(NamedDecl *ND) {
99  assert(!getAsVector() && "Not inline");
100  Data = ND;
101  // Make sure that Data is a plain NamedDecl* so we can use its address
102  // at getLookupResult.
103  assert(*(NamedDecl **)&Data == ND &&
104  "PointerUnion mangles the NamedDecl pointer!");
105  }
106 
107  void remove(NamedDecl *D) {
108  assert(!isNull() && "removing from empty list");
109  if (NamedDecl *Singleton = getAsDecl()) {
110  assert(Singleton == D && "list is different singleton");
111  (void)Singleton;
112  Data = (NamedDecl *)nullptr;
113  return;
114  }
115 
116  DeclsTy &Vec = *getAsVector();
117  DeclsTy::iterator I = std::find(Vec.begin(), Vec.end(), D);
118  assert(I != Vec.end() && "list does not contain decl");
119  Vec.erase(I);
120 
121  assert(std::find(Vec.begin(), Vec.end(), D)
122  == Vec.end() && "list still contains decl");
123  }
124 
125  /// \brief Remove any declarations which were imported from an external
126  /// AST source.
128  if (isNull()) {
129  // Nothing to do.
130  } else if (NamedDecl *Singleton = getAsDecl()) {
131  if (Singleton->isFromASTFile())
132  *this = StoredDeclsList();
133  } else {
134  DeclsTy &Vec = *getAsVector();
135  Vec.erase(std::remove_if(Vec.begin(), Vec.end(),
136  [](Decl *D) { return D->isFromASTFile(); }),
137  Vec.end());
138  // Don't have any external decls any more.
139  Data = DeclsAndHasExternalTy(&Vec, false);
140  }
141  }
142 
143  /// getLookupResult - Return an array of all the decls that this list
144  /// represents.
146  if (isNull())
148 
149  // If we have a single NamedDecl, return it.
150  if (NamedDecl *ND = getAsDecl()) {
151  assert(!isNull() && "Empty list isn't allowed");
152 
153  // Data is a raw pointer to a NamedDecl*, return it.
154  return DeclContext::lookup_result(ND);
155  }
156 
157  assert(getAsVector() && "Must have a vector at this point");
158  DeclsTy &Vector = *getAsVector();
159 
160  // Otherwise, we have a range result.
161  return DeclContext::lookup_result(Vector);
162  }
163 
164  /// HandleRedeclaration - If this is a redeclaration of an existing decl,
165  /// replace the old one with D and return true. Otherwise return false.
166  bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer) {
167  // Most decls only have one entry in their list, special case it.
168  if (NamedDecl *OldD = getAsDecl()) {
169  if (!D->declarationReplaces(OldD, IsKnownNewer))
170  return false;
171  setOnlyValue(D);
172  return true;
173  }
174 
175  // Determine if this declaration is actually a redeclaration.
176  DeclsTy &Vec = *getAsVector();
177  for (DeclsTy::iterator OD = Vec.begin(), ODEnd = Vec.end();
178  OD != ODEnd; ++OD) {
179  NamedDecl *OldD = *OD;
180  if (D->declarationReplaces(OldD, IsKnownNewer)) {
181  *OD = D;
182  return true;
183  }
184  }
185 
186  return false;
187  }
188 
189  /// AddSubsequentDecl - This is called on the second and later decl when it is
190  /// not a redeclaration to merge it into the appropriate place in our list.
192  assert(!isNull() && "don't AddSubsequentDecl when we have no decls");
193 
194  // If this is the second decl added to the list, convert this to vector
195  // form.
196  if (NamedDecl *OldD = getAsDecl()) {
197  DeclsTy *VT = new DeclsTy();
198  VT->push_back(OldD);
199  Data = DeclsAndHasExternalTy(VT, false);
200  }
201 
202  DeclsTy &Vec = *getAsVector();
203 
204  // Using directives end up in a special entry which contains only
205  // other using directives, so all this logic is wasted for them.
206  // But avoiding the logic wastes time in the far-more-common case
207  // that we're *not* adding a new using directive.
208 
209  // Tag declarations always go at the end of the list so that an
210  // iterator which points at the first tag will start a span of
211  // decls that only contains tags.
212  if (D->hasTagIdentifierNamespace())
213  Vec.push_back(D);
214 
215  // Resolved using declarations go at the front of the list so that
216  // they won't show up in other lookup results. Unresolved using
217  // declarations (which are always in IDNS_Using | IDNS_Ordinary)
218  // follow that so that the using declarations will be contiguous.
219  else if (D->getIdentifierNamespace() & Decl::IDNS_Using) {
220  DeclsTy::iterator I = Vec.begin();
222  while (I != Vec.end() &&
223  (*I)->getIdentifierNamespace() == Decl::IDNS_Using)
224  ++I;
225  }
226  Vec.insert(I, D);
227 
228  // All other declarations go at the end of the list, but before any
229  // tag declarations. But we can be clever about tag declarations
230  // because there can only ever be one in a scope.
231  } else if (!Vec.empty() && Vec.back()->hasTagIdentifierNamespace()) {
232  NamedDecl *TagD = Vec.back();
233  Vec.back() = D;
234  Vec.push_back(TagD);
235  } else
236  Vec.push_back(D);
237  }
238 };
239 
241  : public llvm::SmallDenseMap<DeclarationName, StoredDeclsList, 4> {
242 public:
243  static void DestroyAll(StoredDeclsMap *Map, bool Dependent);
244 
245 private:
246  friend class ASTContext; // walks the chain deleting these
247  friend class DeclContext;
248 
249  llvm::PointerIntPair<StoredDeclsMap*, 1> Previous;
250 };
251 
253 public:
254  DependentStoredDeclsMap() = default;
255 
256 private:
257  friend class DeclContext; // iterates over diagnostics
258  friend class DependentDiagnostic;
259 
260  DependentDiagnostic *FirstDiagnostic = nullptr;
261 };
262 
263 } // namespace clang
264 
265 #endif // LLVM_CLANG_AST_DECLCONTEXTINTERNALS_H
void AddSubsequentDecl(NamedDecl *D)
AddSubsequentDecl - This is called on the second and later decl when it is not a redeclaration to mer...
llvm::PointerUnion< NamedDecl *, DeclsAndHasExternalTy > Data
The stored data, which will be either a pointer to a NamedDecl, or a pointer to a vector with a flag ...
llvm::PointerIntPair< DeclsTy *, 1, bool > DeclsAndHasExternalTy
A collection of declarations, with a flag to indicate if we have further external declarations...
SmallVector< NamedDecl *, 4 > DeclsTy
When in vector form, this is what the Data pointer points to.
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
unsigned getIdentifierNamespace() const
Definition: DeclBase.h:791
bool HandleRedeclaration(NamedDecl *D, bool IsKnownNewer)
HandleRedeclaration - If this is a redeclaration of an existing decl, replace the old one with D and ...
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:149
The results of name lookup within a DeclContext.
Definition: DeclBase.h:1178
bool hasTagIdentifierNamespace() const
Definition: DeclBase.h:801
DeclsAndHasExternalTy getAsVectorAndHasExternal() const
StoredDeclsList & operator=(StoredDeclsList &&RHS)
DeclsTy * getAsVector() const
void removeExternalDecls()
Remove any declarations which were imported from an external AST source.
StateNode * Previous
DeclContext::lookup_result getLookupResult()
getLookupResult - Return an array of all the decls that this list represents.
bool declarationReplaces(NamedDecl *OldD, bool IsKnownNewer=true) const
Determine whether this declaration, if known to be well-formed within its context, will replace the declaration OldD if introduced into scope.
Definition: Decl.cpp:1591
NamedDecl * getAsDecl() const
An array of decls optimized for the common case of only containing one entry.
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1252
A dependently-generated diagnostic.
void setOnlyValue(NamedDecl *ND)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate.h) and friends (in DeclFriend.h).
StoredDeclsList(StoredDeclsList &&RHS)
DeclContextLookupResult lookup_result
Definition: DeclBase.h:1776
NamedDecl - This represents a decl with a name.
Definition: Decl.h:245
This declaration is a using declaration.
Definition: DeclBase.h:163