clang 23.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 /// The internal '__builtin_zos_va_list' typedef.
85
86#define BuiltinTemplate(BTName) PREDEF_DECL##BTName##_ID,
87#include "clang/Basic/BuiltinTemplates.inc"
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:
195 LocalDeclID() : Base() {}
196
198 DeclID ID);
200 unsigned ModuleFileIndex, unsigned LocalDeclID);
201
202 LocalDeclID &operator++() {
203 ++ID;
204 return *this;
205 }
206
207 LocalDeclID operator++(int) {
208 LocalDeclID Ret = *this;
209 ++(*this);
210 return Ret;
211 }
212};
213
214class GlobalDeclID : public DeclIDBase {
215 using Base = DeclIDBase;
216
217public:
218 GlobalDeclID() : Base() {}
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
254 static unsigned getHashValue(const GlobalDeclID &Key) {
255 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
256 }
257
258 static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {
259 return L == R;
260 }
261};
262
263template <> struct DenseMapInfo<clang::LocalDeclID> {
266
267 static unsigned getHashValue(const LocalDeclID &Key) {
268 return DenseMapInfo<DeclID>::getHashValue(Key.getRawValue());
269 }
270
271 static bool isEqual(const LocalDeclID &L, const LocalDeclID &R) {
272 return L == R;
273 }
274};
275
276} // namespace llvm
277
278#endif
Reads an AST files chain containing the contents of a translation unit.
Definition ASTReader.h:427
Writes an AST file containing the contents of a translation unit.
Definition ASTWriter.h:97
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
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
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 ASTWriter
Definition DeclID.h:190
friend class GlobalDeclID
Definition DeclID.h:191
static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF, DeclID ID)
LocalDeclID operator++(int)
Definition DeclID.h:207
Information about a module that has been loaded by the ASTReader.
Definition ModuleFile.h:158
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
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: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: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
@ PREDEF_DECL_BUILTIN_ZOS_VA_LIST_ID
The internal '__builtin_zos_va_list' typedef.
Definition DeclID.h:84
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
static unsigned getHashValue(const GlobalDeclID &Key)
Definition DeclID.h:254
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R)
Definition DeclID.h:258
static unsigned getHashValue(const LocalDeclID &Key)
Definition DeclID.h:267
static bool isEqual(const LocalDeclID &L, const LocalDeclID &R)
Definition DeclID.h:271