clang 20.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 '__make_integer_seq' template.
76
77 /// The internal '__NSConstantString' typedef.
79
80 /// The internal '__NSConstantString' tag type.
82
83 /// The internal '__type_pack_element' template.
85
86 /// The internal '__builtin_common_type' template.
88
89 /// The number of declaration IDs that are predefined.
91};
92
93/// GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means
94/// DeclID specific to a certain ModuleFile. Specially, in ASTWriter, the
95/// LocalDeclID to the ModuleFile been writting is equal to the GlobalDeclID.
96/// Outside the serializer, all the DeclID been used should be GlobalDeclID.
97/// We can translate a LocalDeclID to the GlobalDeclID by
98/// `ASTReader::getGlobalDeclID()`.
99
101public:
102 /// An ID number that refers to a declaration in an AST file.
103 ///
104 /// The ID numbers of declarations are consecutive (in order of
105 /// discovery), with values below NUM_PREDEF_DECL_IDS being reserved.
106 /// At the start of a chain of precompiled headers, declaration ID 1 is
107 /// used for the translation unit declaration.
108 ///
109 /// DeclID should only be used directly in serialization. All other users
110 /// should use LocalDeclID or GlobalDeclID.
111 using DeclID = uint64_t;
112
113protected:
115 explicit DeclIDBase(DeclID ID) : ID(ID) {}
116
117public:
118 DeclID getRawValue() const { return ID; }
119
120 explicit operator DeclID() const { return ID; }
121
122 explicit operator PredefinedDeclIDs() const { return (PredefinedDeclIDs)ID; }
123
124 bool isValid() const { return ID != PREDEF_DECL_NULL_ID; }
125
126 bool isInvalid() const { return ID == PREDEF_DECL_NULL_ID; }
127
128 unsigned getModuleFileIndex() const { return ID >> 32; }
129
130 unsigned getLocalDeclIndex() const;
131
132 // The DeclID may be compared with predefined decl ID.
133 friend bool operator==(const DeclIDBase &LHS, const DeclID &RHS) {
134 return LHS.ID == RHS;
135 }
136 friend bool operator!=(const DeclIDBase &LHS, const DeclID &RHS) {
137 return !operator==(LHS, 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 friend bool operator>=(const DeclIDBase &LHS, const DeclID &RHS) {
149 return LHS.ID >= RHS;
150 }
151
152 friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
153 return LHS.ID == RHS.ID;
154 }
155 friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
156 return LHS.ID != RHS.ID;
157 }
158
159 // We may sort the decl ID.
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 friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
170 return LHS.ID >= RHS.ID;
171 }
172
173protected:
175};
176
177class ASTWriter;
178class ASTReader;
179namespace serialization {
180class ModuleFile;
181} // namespace serialization
182
183class LocalDeclID : public DeclIDBase {
184 using Base = DeclIDBase;
185
187 explicit LocalDeclID(DeclID ID) : Base(ID) {}
188
189 // Every Decl ID is a local decl ID to the module being writing in ASTWriter.
190 friend class ASTWriter;
191 friend class GlobalDeclID;
192 friend struct llvm::DenseMapInfo<clang::LocalDeclID>;
193
194public:
196
198 DeclID ID);
200 unsigned ModuleFileIndex, unsigned LocalDeclID);
201
203 ++ID;
204 return *this;
205 }
206
208 LocalDeclID Ret = *this;
209 ++(*this);
210 return Ret;
211 }
212};
213
214class GlobalDeclID : public DeclIDBase {
215 using Base = DeclIDBase;
216
217public:
219 explicit GlobalDeclID(DeclID ID) : Base(ID) {}
220
221 explicit GlobalDeclID(unsigned ModuleFileIndex, unsigned LocalID)
222 : Base((DeclID)ModuleFileIndex << 32 | (DeclID)LocalID) {}
223
224 // For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
225 // to a LocalDeclID.
226 explicit operator LocalDeclID() const { return LocalDeclID(this->ID); }
227};
228
229/// A helper iterator adaptor to convert the iterators to
230/// `SmallVector<SomeDeclID>` to the iterators to `SmallVector<OtherDeclID>`.
231template <class FromTy, class ToTy>
233 : public llvm::iterator_adaptor_base<DeclIDIterator<FromTy, ToTy>,
234 const FromTy *,
235 std::forward_iterator_tag, ToTy> {
236public:
237 DeclIDIterator() : DeclIDIterator::iterator_adaptor_base(nullptr) {}
238
239 DeclIDIterator(const FromTy *ID)
240 : DeclIDIterator::iterator_adaptor_base(ID) {}
241
242 ToTy operator*() const { return ToTy(*this->I); }
243
244 bool operator==(const DeclIDIterator &RHS) const { return this->I == RHS.I; }
245};
246
247} // namespace clang
248
249namespace llvm {
250template <> struct DenseMapInfo<clang::GlobalDeclID> {
253
255 return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
256 }
257
259 return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
260 }
261
262 static unsigned getHashValue(const GlobalDeclID &Key) {
263 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
264 }
265
266 static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {
267 return L == R;
268 }
269};
270
271template <> struct DenseMapInfo<clang::LocalDeclID> {
274
276 return LocalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
277 }
278
280 return LocalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
281 }
282
283 static unsigned getHashValue(const LocalDeclID &Key) {
284 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
285 }
286
287 static bool isEqual(const LocalDeclID &L, const LocalDeclID &R) {
288 return L == R;
289 }
290};
291
292} // namespace llvm
293
294#endif
static char ID
Definition: Arena.cpp:183
Reads an AST files chain containing the contents of a translation unit.
Definition: ASTReader.h:383
Writes an AST file containing the contents of a translation unit.
Definition: ASTWriter.h:89
GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means DeclID specific to a certai...
Definition: DeclID.h:100
friend bool operator!=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:136
bool isValid() const
Definition: DeclID.h:124
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:152
friend bool operator>(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:163
unsigned getModuleFileIndex() const
Definition: DeclID.h:128
friend bool operator<(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:139
DeclIDBase(DeclID ID)
Definition: DeclID.h:115
DeclID getRawValue() const
Definition: DeclID.h:118
friend bool operator==(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:133
unsigned getLocalDeclIndex() const
Definition: DeclBase.cpp:2219
friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:155
uint64_t DeclID
An ID number that refers to a declaration in an AST file.
Definition: DeclID.h:111
friend bool operator<=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:166
bool isInvalid() const
Definition: DeclID.h:126
friend bool operator<=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:142
friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:169
friend bool operator>(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:145
friend bool operator>=(const DeclIDBase &LHS, const DeclID &RHS)
Definition: DeclID.h:148
friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:160
A helper iterator adaptor to convert the iterators to SmallVector<SomeDeclID> to the iterators to Sma...
Definition: DeclID.h:235
DeclIDIterator(const FromTy *ID)
Definition: DeclID.h:239
ToTy operator*() const
Definition: DeclID.h:242
bool operator==(const DeclIDIterator &RHS) const
Definition: DeclID.h:244
GlobalDeclID(unsigned ModuleFileIndex, unsigned LocalID)
Definition: DeclID.h:221
GlobalDeclID(DeclID ID)
Definition: DeclID.h:219
LocalDeclID & operator++()
Definition: DeclID.h:202
friend class GlobalDeclID
Definition: DeclID.h:191
static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF, DeclID ID)
Definition: ASTReader.cpp:941
LocalDeclID operator++(int)
Definition: DeclID.h:207
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:81
@ PREDEF_DECL_COMMON_TYPE_ID
The internal '__builtin_common_type' template.
Definition: DeclID.h:87
@ PREDEF_DECL_TRANSLATION_UNIT_ID
The translation unit.
Definition: DeclID.h:36
@ PREDEF_DECL_TYPE_PACK_ELEMENT_ID
The internal '__type_pack_element' template.
Definition: DeclID.h:84
@ 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_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:90
@ 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:78
@ 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
@ PREDEF_DECL_MAKE_INTEGER_SEQ_ID
The internal '__make_integer_seq' template.
Definition: DeclID.h:75
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
static GlobalDeclID getTombstoneKey()
Definition: DeclID.h:258
static unsigned getHashValue(const GlobalDeclID &Key)
Definition: DeclID.h:262
static GlobalDeclID getEmptyKey()
Definition: DeclID.h:254
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R)
Definition: DeclID.h:266
static unsigned getHashValue(const LocalDeclID &Key)
Definition: DeclID.h:283
static LocalDeclID getTombstoneKey()
Definition: DeclID.h:279
static bool isEqual(const LocalDeclID &L, const LocalDeclID &R)
Definition: DeclID.h:287
static LocalDeclID getEmptyKey()
Definition: DeclID.h:275