clang 24.0.0git
Redeclarable.h
Go to the documentation of this file.
1//===- Redeclarable.h - Base for Decls that can be redeclared --*- C++ -*-====//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Redeclarable interface.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_REDECLARABLE_H
14#define LLVM_CLANG_AST_REDECLARABLE_H
15
17#include "llvm/ADT/DenseMapInfo.h"
18#include "llvm/ADT/PointerUnion.h"
19#include "llvm/ADT/iterator_range.h"
20#include "llvm/Support/Casting.h"
21#include <cassert>
22#include <cstddef>
23#include <iterator>
24
25namespace clang {
26
27class ASTContext;
28class Decl;
29
30// Some notes on redeclarables:
31//
32// - Every redeclarable is on a circular linked list.
33//
34// - Every decl has a pointer to the first element of the chain _and_ a
35// DeclLink that may point to one of 3 possible states:
36// - the "previous" (temporal) element in the chain
37// - the "latest" (temporal) element in the chain
38// - the "uninitialized-latest" value (when newly-constructed)
39//
40// - The first element is also often called the canonical element. Every
41// element has a pointer to it so that "getCanonical" can be fast.
42//
43// - Most links in the chain point to previous, except the link out of
44// the first; it points to latest.
45//
46// - Elements are called "first", "previous", "latest" or
47// "most-recent" when referring to temporal order: order of addition
48// to the chain.
49//
50// - It's easiest to just ignore the implementation of DeclLink when making
51// sense of the redeclaration chain.
52//
53// - There's also a "definition" link for several types of
54// redeclarable, where only one definition should exist at any given
55// time (and the defn pointer is stored in the decl's "data" which
56// is copied to every element on the chain when it's changed).
57//
58// Here is some ASCII art:
59//
60// "first" "latest"
61// "canonical" "most recent"
62// +------------+ first +--------------+
63// | | <--------------------------- | |
64// | | | |
65// | | | |
66// | | +--------------+ | |
67// | | first | | | |
68// | | <---- | | | |
69// | | | | | |
70// | @class A | link | @interface A | link | @class A |
71// | seen first | <---- | seen second | <---- | seen third |
72// | | | | | |
73// +------------+ +--------------+ +--------------+
74// | data | defn | data | defn | data |
75// | | ----> | | <---- | |
76// +------------+ +--------------+ +--------------+
77// | | ^ ^
78// | |defn | |
79// | link +-----+ |
80// +-->-------------------------------------------+
81
82/// Provides common interface for the Decls that can be redeclared.
83template<typename decl_type>
85protected:
86 class DeclLink {
87 /// A pointer to a known latest declaration, either statically known or
88 /// generationally updated as decls are added by an external source.
89 using KnownLatest = LazyGenerationalDeclPtr;
90
91 /// We store a pointer to the ASTContext in the UninitializedLatest
92 /// pointer, but to avoid circular type dependencies when we steal the low
93 /// bits of this pointer, we use a raw void* here.
94 using UninitializedLatest = const void *;
95
96 using Previous = Decl *;
97
98 /// A pointer to either an uninitialized latest declaration (where either
99 /// we've not yet set the previous decl or there isn't one), or to a known
100 /// previous declaration.
101 using NotKnownLatest = llvm::PointerUnion<Previous, UninitializedLatest>;
102
103 mutable llvm::PointerUnion<NotKnownLatest, KnownLatest> Link;
104
105 public:
108
110 : Link(NotKnownLatest(reinterpret_cast<UninitializedLatest>(&Ctx))) {}
111 DeclLink(PreviousTag, decl_type *D) : Link(NotKnownLatest(Previous(D))) {}
112
113 bool isFirst() const {
114 return isa<KnownLatest>(Link) ||
116 }
117
118 decl_type *getPrevious(const decl_type *D) const {
119 if (NotKnownLatest NKL = dyn_cast<NotKnownLatest>(Link)) {
120 if (auto *Prev = dyn_cast<Previous>(NKL))
121 return static_cast<decl_type *>(Prev);
122
123 // Allocate the generational 'most recent' cache now, if needed.
124 Link = KnownLatest(*reinterpret_cast<const ASTContext *>(
126 const_cast<decl_type *>(D));
127 }
128
129 return static_cast<decl_type *>(cast<KnownLatest>(Link).get(D));
130 }
131
132 void setPrevious(decl_type *D) {
133 assert(!isFirst() && "decl became non-canonical unexpectedly");
134 Link = Previous(D);
135 }
136
137 void setLatest(decl_type *D) {
138 assert(isFirst() && "decl became canonical unexpectedly");
139 if (NotKnownLatest NKL = dyn_cast<NotKnownLatest>(Link)) {
140 Link = KnownLatest(*reinterpret_cast<const ASTContext *>(
142 D);
143 } else {
144 auto Latest = cast<KnownLatest>(Link);
145 Latest.set(D);
146 Link = Latest;
147 }
148 }
149
150 void markIncomplete() { cast<KnownLatest>(Link).markIncomplete(); }
151
153 assert(isFirst() && "expected a canonical decl");
154 if (isa<NotKnownLatest>(Link))
155 return nullptr;
156 return cast<KnownLatest>(Link).getNotUpdated();
157 }
158 };
159
160 static DeclLink PreviousDeclLink(decl_type *D) {
161 return DeclLink(DeclLink::PreviousLink, D);
162 }
163
164 static DeclLink LatestDeclLink(const ASTContext &Ctx) {
165 return DeclLink(DeclLink::LatestLink, Ctx);
166 }
167
168 /// Points to the next redeclaration in the chain.
169 ///
170 /// If isFirst() is false, this is a link to the previous declaration
171 /// of this same Decl. If isFirst() is true, this is the first
172 /// declaration and Link points to the latest declaration. For example:
173 ///
174 /// #1 int f(int x, int y = 1); // <pointer to #3, true>
175 /// #2 int f(int x = 0, int y); // <pointer to #1, false>
176 /// #3 int f(int x, int y) { return x + y; } // <pointer to #2, false>
177 ///
178 /// If there is only one declaration, it is <pointer to self, true>
179 DeclLink RedeclLink;
180
181 decl_type *First;
182
183 decl_type *getNextRedeclaration() const {
184 return RedeclLink.getPrevious(static_cast<const decl_type *>(this));
185 }
186
187public:
188 friend class ASTDeclMerger;
189 friend class ASTDeclUnmerger;
190 friend class ASTDeclReader;
191 friend class ASTDeclWriter;
192 friend class IncrementalParser;
193
196 First(static_cast<decl_type *>(this)) {}
197
198 /// Return the previous declaration of this declaration or NULL if this
199 /// is the first declaration.
200 decl_type *getPreviousDecl() {
201 if (!RedeclLink.isFirst())
202 return getNextRedeclaration();
203 return nullptr;
204 }
205 const decl_type *getPreviousDecl() const {
206 return const_cast<decl_type *>(
207 static_cast<const decl_type*>(this))->getPreviousDecl();
208 }
209
210 /// Return the first declaration of this declaration or itself if this
211 /// is the only declaration.
212 decl_type *getFirstDecl() { return First; }
213
214 /// Return the first declaration of this declaration or itself if this
215 /// is the only declaration.
216 const decl_type *getFirstDecl() const { return First; }
217
218 /// True if this is the first declaration in its redeclaration chain.
219 bool isFirstDecl() const { return RedeclLink.isFirst(); }
220
221 /// Returns the most recent (re)declaration of this declaration.
222 decl_type *getMostRecentDecl() {
224 }
225
226 /// Returns the most recent (re)declaration of this declaration.
227 const decl_type *getMostRecentDecl() const {
229 }
230
231 /// Set the previous declaration. If PrevDecl is NULL, set this as the
232 /// first and only declaration.
233 void setPreviousDecl(decl_type *PrevDecl);
234
235 /// Iterates through all the redeclarations of the same decl.
237 /// Current - The current declaration.
238 decl_type *Current = nullptr;
239 decl_type *Starter = nullptr;
240 bool PassedFirst = false;
241
242 public:
243 using value_type = decl_type *;
244 using reference = decl_type *;
245 using pointer = decl_type *;
246 using iterator_category = std::forward_iterator_tag;
247 using difference_type = std::ptrdiff_t;
248
249 redecl_iterator() = default;
250 explicit redecl_iterator(decl_type *C) : Current(C), Starter(C) {}
251
252 reference operator*() const { return Current; }
253 pointer operator->() const { return Current; }
254
256 assert(Current && "Advancing while iterator has reached end");
257 // Make sure we don't infinitely loop on an invalid redecl chain. This
258 // should never happen.
259 if (Current->isFirstDecl()) {
260 if (PassedFirst) {
261 assert(0 && "Passed first decl twice, invalid redecl chain!");
262 Current = nullptr;
263 return *this;
264 }
265 PassedFirst = true;
266 }
267
268 // Get either previous decl or latest decl.
269 decl_type *Next = Current->getNextRedeclaration();
270 Current = (Next != Starter) ? Next : nullptr;
271 return *this;
272 }
273
275 redecl_iterator tmp(*this);
276 ++(*this);
277 return tmp;
278 }
279
280 friend bool operator==(const redecl_iterator &x, const redecl_iterator &y) {
281 return x.Current == y.Current;
282 }
283 friend bool operator!=(const redecl_iterator &x, const redecl_iterator &y) {
284 return x.Current != y.Current;
285 }
286 };
287
288 using redecl_range = llvm::iterator_range<redecl_iterator>;
289
290 /// Returns an iterator range for all the redeclarations of the same
291 /// decl. It will iterate at least once (when this decl is the only one).
293 return redecl_range(redecl_iterator(const_cast<decl_type *>(
294 static_cast<const decl_type *>(this))),
295 redecl_iterator());
296 }
297
298 redecl_iterator redecls_begin() const { return redecls().begin(); }
299 redecl_iterator redecls_end() const { return redecls().end(); }
300};
301
302/// Get the primary declaration for a declaration from an AST file. That
303/// will be the first-loaded declaration.
304Decl *getPrimaryMergedDecl(Decl *D);
305
306/// Provides common interface for the Decls that cannot be redeclared,
307/// but can be merged if the same declaration is brought in from multiple
308/// modules.
309template<typename decl_type>
311public:
312 Mergeable() = default;
313
314 /// Return the first declaration of this declaration or itself if this
315 /// is the only declaration.
316 decl_type *getFirstDecl() {
317 auto *D = static_cast<decl_type *>(this);
318 if (!D->isFromASTFile())
319 return D;
320 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
321 }
322
323 /// Return the first declaration of this declaration or itself if this
324 /// is the only declaration.
325 const decl_type *getFirstDecl() const {
326 const auto *D = static_cast<const decl_type *>(this);
327 if (!D->isFromASTFile())
328 return D;
329 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
330 }
331
332 /// Returns true if this is the first declaration.
333 bool isFirstDecl() const { return getFirstDecl() == this; }
334};
335
336/// A wrapper class around a pointer that always points to its canonical
337/// declaration.
338///
339/// CanonicalDeclPtr<decl_type> behaves just like decl_type*, except we call
340/// decl_type::getCanonicalDecl() on construction.
341///
342/// This is useful for hashtables that you want to be keyed on a declaration's
343/// canonical decl -- if you use CanonicalDeclPtr as the key, you don't need to
344/// remember to call getCanonicalDecl() everywhere.
345template <typename decl_type> class CanonicalDeclPtr {
346public:
347 CanonicalDeclPtr() = default;
348 CanonicalDeclPtr(decl_type *Ptr)
349 : Ptr(Ptr ? Ptr->getCanonicalDecl() : nullptr) {}
352
353 operator decl_type *() { return Ptr; }
354 operator const decl_type *() const { return Ptr; }
355
356 decl_type *operator->() { return Ptr; }
357 const decl_type *operator->() const { return Ptr; }
358
359 decl_type &operator*() { return *Ptr; }
360 const decl_type &operator*() const { return *Ptr; }
361
363 return LHS.Ptr == RHS.Ptr;
364 }
366 return LHS.Ptr != RHS.Ptr;
367 }
368
369private:
370 friend struct llvm::DenseMapInfo<CanonicalDeclPtr<decl_type>>;
371 friend struct llvm::PointerLikeTypeTraits<CanonicalDeclPtr<decl_type>>;
372
373 decl_type *Ptr = nullptr;
374};
375
376} // namespace clang
377
378namespace llvm {
379
380template <typename decl_type>
381struct DenseMapInfo<clang::CanonicalDeclPtr<decl_type>> {
383 using BaseInfo = DenseMapInfo<decl_type *>;
384
385 static unsigned getHashValue(const CanonicalDeclPtr &P) {
386 return BaseInfo::getHashValue(P);
387 }
388
389 static bool isEqual(const CanonicalDeclPtr &LHS,
390 const CanonicalDeclPtr &RHS) {
391 return BaseInfo::isEqual(LHS, RHS);
392 }
393};
394
395template <typename decl_type>
408
409} // namespace llvm
410
411#endif // LLVM_CLANG_AST_REDECLARABLE_H
FormatToken * Previous
The previous token in the unwrapped line.
FormatToken * Next
The next token in the unwrapped line.
static const Decl * getCanonicalDecl(const Decl *D)
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:239
A wrapper class around a pointer that always points to its canonical declaration.
CanonicalDeclPtr(const CanonicalDeclPtr &)=default
const decl_type * operator->() const
CanonicalDeclPtr & operator=(const CanonicalDeclPtr &)=default
const decl_type & operator*() const
CanonicalDeclPtr(decl_type *Ptr)
friend bool operator==(CanonicalDeclPtr LHS, CanonicalDeclPtr RHS)
friend bool operator!=(CanonicalDeclPtr LHS, CanonicalDeclPtr RHS)
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isFirstDecl() const
Returns true if this is the first declaration.
const decl_type * getFirstDecl() const
Return the first declaration of this declaration or itself if this is the only declaration.
Mergeable()=default
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
friend bool operator!=(const redecl_iterator &x, const redecl_iterator &y)
friend bool operator==(const redecl_iterator &x, const redecl_iterator &y)
std::forward_iterator_tag iterator_category
decl_type * getFirstDecl()
Return the first declaration of this declaration or itself if this is the only declaration.
const decl_type * getMostRecentDecl() const
Returns the most recent (re)declaration of this declaration.
const decl_type * getFirstDecl() const
Return the first declaration of this declaration or itself if this is the only declaration.
decl_type * getNextRedeclaration() const
Redeclarable(const ASTContext &Ctx)
decl_type * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
redecl_iterator redecls_end() const
const decl_type * getPreviousDecl() const
llvm::iterator_range< redecl_iterator > redecl_range
decl_type * getMostRecentDecl()
Returns the most recent (re)declaration of this declaration.
void setPreviousDecl(decl_type *PrevDecl)
Set the previous declaration.
Definition Decl.h:5468
static DeclLink LatestDeclLink(const ASTContext &Ctx)
bool isFirstDecl() const
True if this is the first declaration in its redeclaration chain.
redecl_iterator redecls_begin() const
redecl_range redecls() const
Returns an iterator range for all the redeclarations of the same decl.
static DeclLink PreviousDeclLink(decl_type *D)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
Decl * getPrimaryMergedDecl(Decl *D)
Get the primary declaration for a declaration from an AST file.
Definition Decl.cpp:77
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
U cast(CodeGen::Address addr)
Definition Address.h:327
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
A lazy Decl value where the value might change in later generations of the external AST source.
static unsigned getHashValue(const CanonicalDeclPtr &P)
static bool isEqual(const CanonicalDeclPtr &LHS, const CanonicalDeclPtr &RHS)
clang::CanonicalDeclPtr< decl_type > CanonicalDeclPtr
static void * getAsVoidPointer(clang::CanonicalDeclPtr< decl_type > P)
static clang::CanonicalDeclPtr< decl_type > getFromVoidPointer(void *P)