clang 22.0.0git
DeclID.h
Go to the documentation of this file.
1//===--- DeclID.h - ID number for deserialized declarations ----*- 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 DeclID class family to describe the deserialized
10// declarations. The DeclID is widely used in AST via LazyDeclPtr, or calls to
11// `ExternalASTSource::getExternalDecl`. It will be helpful for type safety to
12// require the use of `DeclID` to explicit.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef LLVM_CLANG_AST_DECLID_H
17#define LLVM_CLANG_AST_DECLID_H
18
19#include "llvm/ADT/DenseMapInfo.h"
20#include "llvm/ADT/Hashing.h"
21#include "llvm/ADT/iterator.h"
22
23namespace clang {
24
25/// Predefined declaration IDs.
26///
27/// These declaration IDs correspond to predefined declarations in the AST
28/// context, such as the NULL declaration ID. Such declarations are never
29/// actually serialized, since they will be built by the AST context when
30/// it is created.
32 /// The NULL declaration.
34
35 /// The translation unit.
37
38 /// The Objective-C 'id' type.
40
41 /// The Objective-C 'SEL' type.
43
44 /// The Objective-C 'Class' type.
46
47 /// The Objective-C 'Protocol' type.
49
50 /// The signed 128-bit integer type.
52
53 /// The unsigned 128-bit integer type.
55
56 /// The internal 'instancetype' typedef.
58
59 /// The internal '__builtin_va_list' typedef.
61
62 /// The internal '__va_list_tag' struct, if any.
64
65 /// The internal '__builtin_ms_va_list' typedef.
67
68 /// The predeclared '_GUID' struct.
70
71 /// The extern "C" context.
73
74 /// The internal '__NSConstantString' typedef.
76
77 /// The internal '__NSConstantString' tag type.
79
80 /// The predeclared 'type_info' struct.
82
83#define BuiltinTemplate(BTName) PREDEF_DECL##BTName##_ID,
84#include "clang/Basic/BuiltinTemplates.inc"
85
86 /// The number of declaration IDs that are predefined.
88};
89
90/// GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means
91/// DeclID specific to a certain ModuleFile. Specially, in ASTWriter, the
92/// LocalDeclID to the ModuleFile been writting is equal to the GlobalDeclID.
93/// Outside the serializer, all the DeclID been used should be GlobalDeclID.
94/// We can translate a LocalDeclID to the GlobalDeclID by
95/// `ASTReader::getGlobalDeclID()`.
96
98public:
99 /// An ID number that refers to a declaration in an AST file.
100 ///
101 /// The ID numbers of declarations are consecutive (in order of
102 /// discovery), with values below NUM_PREDEF_DECL_IDS being reserved.
103 /// At the start of a chain of precompiled headers, declaration ID 1 is
104 /// used for the translation unit declaration.
105 ///
106 /// DeclID should only be used directly in serialization. All other users
107 /// should use LocalDeclID or GlobalDeclID.
108 using DeclID = uint64_t;
109
110protected:
112 explicit DeclIDBase(DeclID ID) : ID(ID) {}
113
114public:
115 DeclID getRawValue() const { return ID; }
116
117 explicit operator DeclID() const { return ID; }
118
119 explicit operator PredefinedDeclIDs() const { return (PredefinedDeclIDs)ID; }
120
121 bool isValid() const { return ID != PREDEF_DECL_NULL_ID; }
122
123 bool isInvalid() const { return ID == PREDEF_DECL_NULL_ID; }
124
125 unsigned getModuleFileIndex() const { return ID >> 32; }
126
127 unsigned getLocalDeclIndex() const;
128
129 // The DeclID may be compared with predefined decl ID.
130 friend bool operator==(const DeclIDBase &LHS, const DeclID &RHS) {
131 return LHS.ID == RHS;
132 }
133 friend bool operator!=(const DeclIDBase &LHS, const DeclID &RHS) {
134 return !operator==(LHS, RHS);
135 }
136 friend bool operator<(const DeclIDBase &LHS, const DeclID &RHS) {
137 return LHS.ID < RHS;
138 }
139 friend bool operator<=(const DeclIDBase &LHS, const DeclID &RHS) {
140 return LHS.ID <= RHS;
141 }
142 friend bool operator>(const DeclIDBase &LHS, const DeclID &RHS) {
143 return LHS.ID > RHS;
144 }
145 friend bool operator>=(const DeclIDBase &LHS, const DeclID &RHS) {
146 return LHS.ID >= RHS;
147 }
148
149 friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
150 return LHS.ID == RHS.ID;
151 }
152 friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
153 return LHS.ID != RHS.ID;
154 }
155
156 // We may sort the decl ID.
157 friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS) {
158 return LHS.ID < RHS.ID;
159 }
160 friend bool operator>(const DeclIDBase &LHS, const DeclIDBase &RHS) {
161 return LHS.ID > RHS.ID;
162 }
163 friend bool operator<=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
164 return LHS.ID <= RHS.ID;
165 }
166 friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
167 return LHS.ID >= RHS.ID;
168 }
169
170protected:
172};
173
174class ASTWriter;
175class ASTReader;
176namespace serialization {
177class ModuleFile;
178} // namespace serialization
179
180class LocalDeclID : public DeclIDBase {
181 using Base = DeclIDBase;
182
184 explicit LocalDeclID(DeclID ID) : Base(ID) {}
185
186 // Every Decl ID is a local decl ID to the module being writing in ASTWriter.
187 friend class ASTWriter;
188 friend class GlobalDeclID;
189 friend struct llvm::DenseMapInfo<clang::LocalDeclID>;
190
191public:
193
195 DeclID ID);
197 unsigned ModuleFileIndex, unsigned LocalDeclID);
198
200 ++ID;
201 return *this;
202 }
203
205 LocalDeclID Ret = *this;
206 ++(*this);
207 return Ret;
208 }
209};
210
211class GlobalDeclID : public DeclIDBase {
212 using Base = DeclIDBase;
213
214public:
216 explicit GlobalDeclID(DeclID ID) : Base(ID) {}
217
218 explicit GlobalDeclID(unsigned ModuleFileIndex, unsigned LocalID)
219 : Base((DeclID)ModuleFileIndex << 32 | (DeclID)LocalID) {}
220
221 // For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
222 // to a LocalDeclID.
223 explicit operator LocalDeclID() const { return LocalDeclID(this->ID); }
224};
225
226/// A helper iterator adaptor to convert the iterators to
227/// `SmallVector<SomeDeclID>` to the iterators to `SmallVector<OtherDeclID>`.
228template <class FromTy, class ToTy>
230 : public llvm::iterator_adaptor_base<DeclIDIterator<FromTy, ToTy>,
231 const FromTy *,
232 std::forward_iterator_tag, ToTy> {
233public:
234 DeclIDIterator() : DeclIDIterator::iterator_adaptor_base(nullptr) {}
235
236 DeclIDIterator(const FromTy *ID)
237 : DeclIDIterator::iterator_adaptor_base(ID) {}
238
239 ToTy operator*() const { return ToTy(*this->I); }
240
241 bool operator==(const DeclIDIterator &RHS) const { return this->I == RHS.I; }
242};
243
244} // namespace clang
245
246namespace llvm {
247template <> struct DenseMapInfo<clang::GlobalDeclID> {
250
252 return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
253 }
254
256 return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
257 }
258
259 static unsigned getHashValue(const GlobalDeclID &Key) {
260 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
261 }
262
263 static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {
264 return L == R;
265 }
266};
267
268template <> struct DenseMapInfo<clang::LocalDeclID> {
271
273 return LocalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
274 }
275
277 return LocalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
278 }
279
280 static unsigned getHashValue(const LocalDeclID &Key) {
281 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
282 }
283
284 static bool isEqual(const LocalDeclID &L, const LocalDeclID &R) {
285 return L == R;
286 }
287};
288
289} // namespace llvm
290
291#endif
static char ID
Definition: Arena.cpp:183
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:429
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:97
GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means DeclID specific to a certai...
Definition: DeclID.h:97
friend bool operator!=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:133
bool isValid() const
Definition: DeclID.h:121
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:149
friend bool operator>(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:160
unsigned getModuleFileIndex() const
Definition: DeclID.h:125
friend bool operator<(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:136
DeclIDBase(DeclID ID)
Definition: DeclID.h:112
DeclID getRawValue() const
Definition: DeclID.h:115
friend bool operator==(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:130
unsigned getLocalDeclIndex() const
Definition: DeclBase.cpp:2251
friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:152
uint64_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: DeclID.h:108
friend bool operator<=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:163
bool isInvalid() const
Definition: DeclID.h:123
friend bool operator<=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:139
friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:166
friend bool operator>(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:142
friend bool operator>=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:145
friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:157
A helper iterator adaptor to convert the iterators to SmallVector<SomeDeclID> to the iterators to Sma...
Definition: DeclID.h:232
DeclIDIterator(const FromTy *ID)
Definition: DeclID.h:236
ToTy operator*() const
Definition: DeclID.h:239
bool operator==(const DeclIDIterator &RHS) const
Definition: DeclID.h:241
GlobalDeclID(unsigned ModuleFileIndex, unsigned LocalID)
Definition: DeclID.h:218
GlobalDeclID(DeclID ID)
Definition: DeclID.h:216
LocalDeclID & operator++()
Definition: DeclID.h:199
friend class GlobalDeclID
Definition: DeclID.h:188
static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF, DeclID ID)
Definition: ASTReader.cpp:1019
LocalDeclID operator++(int)
Definition: DeclID.h:204
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:130
The JSON file list parser is used to communicate input to InstallAPI.
PredefinedDeclIDs
Predefined declaration IDs.
Definition: DeclID.h:31
@ PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID
The internal '__NSConstantString' tag type.
Definition: DeclID.h:78
@ PREDEF_DECL_TRANSLATION_UNIT_ID
The translation unit.
Definition: DeclID.h:36
@ PREDEF_DECL_OBJC_CLASS_ID
The Objective-C 'Class' type.
Definition: DeclID.h:45
@ PREDEF_DECL_BUILTIN_MS_GUID_ID
The predeclared '_GUID' struct.
Definition: DeclID.h:69
@ PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID
The predeclared 'type_info' struct.
Definition: DeclID.h:81
@ PREDEF_DECL_OBJC_INSTANCETYPE_ID
The internal 'instancetype' typedef.
Definition: DeclID.h:57
@ PREDEF_DECL_OBJC_PROTOCOL_ID
The Objective-C 'Protocol' type.
Definition: DeclID.h:48
@ PREDEF_DECL_UNSIGNED_INT_128_ID
The unsigned 128-bit integer type.
Definition: DeclID.h:54
@ PREDEF_DECL_OBJC_SEL_ID
The Objective-C 'SEL' type.
Definition: DeclID.h:42
@ NUM_PREDEF_DECL_IDS
The number of declaration IDs that are predefined.
Definition: DeclID.h:87
@ PREDEF_DECL_INT_128_ID
The signed 128-bit integer type.
Definition: DeclID.h:51
@ PREDEF_DECL_VA_LIST_TAG
The internal '__va_list_tag' struct, if any.
Definition: DeclID.h:63
@ PREDEF_DECL_BUILTIN_MS_VA_LIST_ID
The internal '__builtin_ms_va_list' typedef.
Definition: DeclID.h:66
@ PREDEF_DECL_CF_CONSTANT_STRING_ID
The internal '__NSConstantString' typedef.
Definition: DeclID.h:75
@ PREDEF_DECL_NULL_ID
The NULL declaration.
Definition: DeclID.h:33
@ PREDEF_DECL_BUILTIN_VA_LIST_ID
The internal '__builtin_va_list' typedef.
Definition: DeclID.h:60
@ PREDEF_DECL_EXTERN_C_CONTEXT_ID
The extern "C" context.
Definition: DeclID.h:72
@ PREDEF_DECL_OBJC_ID_ID
The Objective-C 'id' type.
Definition: DeclID.h:39
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
static GlobalDeclID getTombstoneKey()
Definition: DeclID.h:255
static unsigned getHashValue(const GlobalDeclID &Key)
Definition: DeclID.h:259
static GlobalDeclID getEmptyKey()
Definition: DeclID.h:251
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R)
Definition: DeclID.h:263
static unsigned getHashValue(const LocalDeclID &Key)
Definition: DeclID.h:280
static LocalDeclID getTombstoneKey()
Definition: DeclID.h:276
static bool isEqual(const LocalDeclID &L, const LocalDeclID &R)
Definition: DeclID.h:284
static LocalDeclID getEmptyKey()
Definition: DeclID.h:272