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