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 ASTDeclReader;
190 friend class ASTDeclWriter;
191 friend class IncrementalParser;
192
195 First(static_cast<decl_type *>(this)) {}
196
197 /// Return the previous declaration of this declaration or NULL if this
198 /// is the first declaration.
199 decl_type *getPreviousDecl() {
200 if (!RedeclLink.isFirst())
201 return getNextRedeclaration();
202 return nullptr;
203 }
204 const decl_type *getPreviousDecl() const {
205 return const_cast<decl_type *>(
206 static_cast<const decl_type*>(this))->getPreviousDecl();
207 }
208
209 /// Return the first declaration of this declaration or itself if this
210 /// is the only declaration.
211 decl_type *getFirstDecl() { return First; }
212
213 /// Return the first declaration of this declaration or itself if this
214 /// is the only declaration.
215 const decl_type *getFirstDecl() const { return First; }
216
217 /// True if this is the first declaration in its redeclaration chain.
218 bool isFirstDecl() const { return RedeclLink.isFirst(); }
219
220 /// Returns the most recent (re)declaration of this declaration.
221 decl_type *getMostRecentDecl() {
223 }
224
225 /// Returns the most recent (re)declaration of this declaration.
226 const decl_type *getMostRecentDecl() const {
228 }
229
230 /// Set the previous declaration. If PrevDecl is NULL, set this as the
231 /// first and only declaration.
232 void setPreviousDecl(decl_type *PrevDecl);
233
234 /// Iterates through all the redeclarations of the same decl.
236 /// Current - The current declaration.
237 decl_type *Current = nullptr;
238 decl_type *Starter = nullptr;
239 bool PassedFirst = false;
240
241 public:
242 using value_type = decl_type *;
243 using reference = decl_type *;
244 using pointer = decl_type *;
245 using iterator_category = std::forward_iterator_tag;
246 using difference_type = std::ptrdiff_t;
247
248 redecl_iterator() = default;
249 explicit redecl_iterator(decl_type *C) : Current(C), Starter(C) {}
250
251 reference operator*() const { return Current; }
252 pointer operator->() const { return Current; }
253
255 assert(Current && "Advancing while iterator has reached end");
256 // Make sure we don't infinitely loop on an invalid redecl chain. This
257 // should never happen.
258 if (Current->isFirstDecl()) {
259 if (PassedFirst) {
260 assert(0 && "Passed first decl twice, invalid redecl chain!");
261 Current = nullptr;
262 return *this;
263 }
264 PassedFirst = true;
265 }
266
267 // Get either previous decl or latest decl.
268 decl_type *Next = Current->getNextRedeclaration();
269 Current = (Next != Starter) ? Next : nullptr;
270 return *this;
271 }
272
274 redecl_iterator tmp(*this);
275 ++(*this);
276 return tmp;
277 }
278
279 friend bool operator==(const redecl_iterator &x, const redecl_iterator &y) {
280 return x.Current == y.Current;
281 }
282 friend bool operator!=(const redecl_iterator &x, const redecl_iterator &y) {
283 return x.Current != y.Current;
284 }
285 };
286
287 using redecl_range = llvm::iterator_range<redecl_iterator>;
288
289 /// Returns an iterator range for all the redeclarations of the same
290 /// decl. It will iterate at least once (when this decl is the only one).
292 return redecl_range(redecl_iterator(const_cast<decl_type *>(
293 static_cast<const decl_type *>(this))),
294 redecl_iterator());
295 }
296
297 redecl_iterator redecls_begin() const { return redecls().begin(); }
298 redecl_iterator redecls_end() const { return redecls().end(); }
299};
300
301/// Get the primary declaration for a declaration from an AST file. That
302/// will be the first-loaded declaration.
303Decl *getPrimaryMergedDecl(Decl *D);
304
305/// Provides common interface for the Decls that cannot be redeclared,
306/// but can be merged if the same declaration is brought in from multiple
307/// modules.
308template<typename decl_type>
310public:
311 Mergeable() = default;
312
313 /// Return the first declaration of this declaration or itself if this
314 /// is the only declaration.
315 decl_type *getFirstDecl() {
316 auto *D = static_cast<decl_type *>(this);
317 if (!D->isFromASTFile())
318 return D;
319 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
320 }
321
322 /// Return the first declaration of this declaration or itself if this
323 /// is the only declaration.
324 const decl_type *getFirstDecl() const {
325 const auto *D = static_cast<const decl_type *>(this);
326 if (!D->isFromASTFile())
327 return D;
328 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
329 }
330
331 /// Returns true if this is the first declaration.
332 bool isFirstDecl() const { return getFirstDecl() == this; }
333};
334
335/// A wrapper class around a pointer that always points to its canonical
336/// declaration.
337///
338/// CanonicalDeclPtr<decl_type> behaves just like decl_type*, except we call
339/// decl_type::getCanonicalDecl() on construction.
340///
341/// This is useful for hashtables that you want to be keyed on a declaration's
342/// canonical decl -- if you use CanonicalDeclPtr as the key, you don't need to
343/// remember to call getCanonicalDecl() everywhere.
344template <typename decl_type> class CanonicalDeclPtr {
345public:
346 CanonicalDeclPtr() = default;
347 CanonicalDeclPtr(decl_type *Ptr)
348 : Ptr(Ptr ? Ptr->getCanonicalDecl() : nullptr) {}
351
352 operator decl_type *() { return Ptr; }
353 operator const decl_type *() const { return Ptr; }
354
355 decl_type *operator->() { return Ptr; }
356 const decl_type *operator->() const { return Ptr; }
357
358 decl_type &operator*() { return *Ptr; }
359 const decl_type &operator*() const { return *Ptr; }
360
362 return LHS.Ptr == RHS.Ptr;
363 }
365 return LHS.Ptr != RHS.Ptr;
366 }
367
368private:
369 friend struct llvm::DenseMapInfo<CanonicalDeclPtr<decl_type>>;
370 friend struct llvm::PointerLikeTypeTraits<CanonicalDeclPtr<decl_type>>;
371
372 decl_type *Ptr = nullptr;
373};
374
375} // namespace clang
376
377namespace llvm {
378
379template <typename decl_type>
380struct DenseMapInfo<clang::CanonicalDeclPtr<decl_type>> {
382 using BaseInfo = DenseMapInfo<decl_type *>;
383
384 static unsigned getHashValue(const CanonicalDeclPtr &P) {
385 return BaseInfo::getHashValue(P);
386 }
387
388 static bool isEqual(const CanonicalDeclPtr &LHS,
389 const CanonicalDeclPtr &RHS) {
390 return BaseInfo::isEqual(LHS, RHS);
391 }
392};
393
394template <typename decl_type>
407
408} // namespace llvm
409
410#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:5465
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)