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.
31enum PredefinedDeclIDs {
32 /// The NULL declaration.
33 PREDEF_DECL_NULL_ID,
34
35 /// The translation unit.
36 PREDEF_DECL_TRANSLATION_UNIT_ID,
37
38 /// The Objective-C 'id' type.
39 PREDEF_DECL_OBJC_ID_ID,
40
41 /// The Objective-C 'SEL' type.
42 PREDEF_DECL_OBJC_SEL_ID,
43
44 /// The Objective-C 'Class' type.
45 PREDEF_DECL_OBJC_CLASS_ID,
46
47 /// The Objective-C 'Protocol' type.
48 PREDEF_DECL_OBJC_PROTOCOL_ID,
49
50 /// The signed 128-bit integer type.
51 PREDEF_DECL_INT_128_ID,
52
53 /// The unsigned 128-bit integer type.
54 PREDEF_DECL_UNSIGNED_INT_128_ID,
55
56 /// The internal 'instancetype' typedef.
57 PREDEF_DECL_OBJC_INSTANCETYPE_ID,
58
59 /// The internal '__builtin_va_list' typedef.
60 PREDEF_DECL_BUILTIN_VA_LIST_ID,
61
62 /// The internal '__va_list_tag' struct, if any.
63 PREDEF_DECL_VA_LIST_TAG,
64
65 /// The internal '__builtin_ms_va_list' typedef.
66 PREDEF_DECL_BUILTIN_MS_VA_LIST_ID,
67
68 /// The predeclared '_GUID' struct.
69 PREDEF_DECL_BUILTIN_MS_GUID_ID,
70
71 /// The extern "C" context.
72 PREDEF_DECL_EXTERN_C_CONTEXT_ID,
73
74 /// The internal '__NSConstantString' typedef.
75 PREDEF_DECL_CF_CONSTANT_STRING_ID,
76
77 /// The internal '__NSConstantString' tag type.
78 PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
79
80 /// The predeclared 'type_info' struct.
81 PREDEF_DECL_BUILTIN_MS_TYPE_INFO_TAG_ID,
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.
87 NUM_PREDEF_DECL_IDS
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
97class DeclIDBase {
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:
111 DeclIDBase() : ID(PREDEF_DECL_NULL_ID) {}
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:
171 DeclID ID;
172};
173
174class ASTWriter;
175class ASTReader;
176namespace serialization {
177class ModuleFile;
178} // namespace serialization
179
180class LocalDeclID : public DeclIDBase {
181 using Base = DeclIDBase;
182
183 LocalDeclID(PredefinedDeclIDs ID) : Base(ID) {}
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:
192 LocalDeclID() : Base() {}
193
194 static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF,
195 DeclID ID);
196 static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF,
197 unsigned ModuleFileIndex, unsigned LocalDeclID);
198
199 LocalDeclID &operator++() {
200 ++ID;
201 return *this;
202 }
203
204 LocalDeclID operator++(int) {
205 LocalDeclID Ret = *this;
206 ++(*this);
207 return Ret;
208 }
209};
210
211class GlobalDeclID : public DeclIDBase {
212 using Base = DeclIDBase;
213
214public:
215 GlobalDeclID() : Base() {}
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>
229class DeclIDIterator
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> {
248 using GlobalDeclID = clang::GlobalDeclID;
249 using DeclID = GlobalDeclID::DeclID;
250
251 static GlobalDeclID getEmptyKey() {
252 return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
253 }
254
255 static GlobalDeclID getTombstoneKey() {
256 return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
257 }
258
259 static unsigned getHashValue(const GlobalDeclID &Key) {
260 return DenseMapInfo<DeclID>::getHashValue(Val: 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> {
269 using LocalDeclID = clang::LocalDeclID;
270 using DeclID = LocalDeclID::DeclID;
271
272 static LocalDeclID getEmptyKey() {
273 return LocalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
274 }
275
276 static LocalDeclID getTombstoneKey() {
277 return LocalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
278 }
279
280 static unsigned getHashValue(const LocalDeclID &Key) {
281 return DenseMapInfo<DeclID>::getHashValue(Val: 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
292