clang 19.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
119 explicit DeclIDBase(unsigned LocalID, unsigned ModuleFileIndex) {
120 ID = (DeclID)LocalID | ((DeclID)ModuleFileIndex << 32);
121 }
122
123public:
124 DeclID get() const { return ID; }
125
126 explicit operator DeclID() const { return ID; }
127
128 explicit operator PredefinedDeclIDs() const { return (PredefinedDeclIDs)ID; }
129
130 bool isValid() const { return ID != PREDEF_DECL_NULL_ID; }
131
132 bool isInvalid() const { return ID == PREDEF_DECL_NULL_ID; }
133
134 unsigned getModuleFileIndex() const { return ID >> 32; }
135
136 unsigned getLocalDeclIndex() const;
137
138 friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
139 return LHS.ID == RHS.ID;
140 }
141 friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
142 return LHS.ID != RHS.ID;
143 }
144 // We may sort the decl ID.
145 friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS) {
146 return LHS.ID < RHS.ID;
147 }
148 friend bool operator>(const DeclIDBase &LHS, const DeclIDBase &RHS) {
149 return LHS.ID > RHS.ID;
150 }
151 friend bool operator<=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
152 return LHS.ID <= RHS.ID;
153 }
154 friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
155 return LHS.ID >= RHS.ID;
156 }
157
158protected:
160};
161
162class LocalDeclID : public DeclIDBase {
163 using Base = DeclIDBase;
164
165public:
168 explicit LocalDeclID(DeclID ID) : Base(ID) {}
169
170 explicit LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
171 : Base(LocalID, ModuleFileIndex) {}
172
174 ++ID;
175 return *this;
176 }
177
179 LocalDeclID Ret = *this;
180 ++(*this);
181 return Ret;
182 }
183};
184
185class GlobalDeclID : public DeclIDBase {
186 using Base = DeclIDBase;
187
188public:
190 explicit GlobalDeclID(DeclID ID) : Base(ID) {}
191
192 explicit GlobalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
193 : Base(LocalID, ModuleFileIndex) {}
194
195 // For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
196 // to a LocalDeclID.
197 explicit operator LocalDeclID() const { return LocalDeclID(this->ID); }
198};
199
200/// A helper iterator adaptor to convert the iterators to
201/// `SmallVector<SomeDeclID>` to the iterators to `SmallVector<OtherDeclID>`.
202template <class FromTy, class ToTy>
204 : public llvm::iterator_adaptor_base<DeclIDIterator<FromTy, ToTy>,
205 const FromTy *,
206 std::forward_iterator_tag, ToTy> {
207public:
208 DeclIDIterator() : DeclIDIterator::iterator_adaptor_base(nullptr) {}
209
210 DeclIDIterator(const FromTy *ID)
211 : DeclIDIterator::iterator_adaptor_base(ID) {}
212
213 ToTy operator*() const { return ToTy(*this->I); }
214
215 bool operator==(const DeclIDIterator &RHS) const { return this->I == RHS.I; }
216};
217
218} // namespace clang
219
220namespace llvm {
221template <> struct DenseMapInfo<clang::GlobalDeclID> {
224
226 return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
227 }
228
230 return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
231 }
232
233 static unsigned getHashValue(const GlobalDeclID &Key) {
234 // Our default hash algorithm for 64 bits integer may not be very good.
235 // In GlobalDeclID's case, it is pretty common that the lower 32 bits can
236 // be same.
237 // FIXME: Remove this when we fix the underlying issue.
238 return llvm::hash_value(Key.get());
239 }
240
241 static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {
242 return L == R;
243 }
244};
245
246} // namespace llvm
247
248#endif
static char ID
Definition: Arena.cpp:183
GlobalDeclID means DeclID in the current ASTContext and LocalDeclID means DeclID specific to a certai...
Definition: DeclID.h:102
bool isValid() const
Definition: DeclID.h:130
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:138
friend bool operator>(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:148
unsigned getModuleFileIndex() const
Definition: DeclID.h:134
DeclIDBase(DeclID ID)
Definition: DeclID.h:117
unsigned getLocalDeclIndex() const
Definition: DeclBase.cpp:2198
friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:141
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:151
bool isInvalid() const
Definition: DeclID.h:132
friend bool operator>=(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:154
DeclID get() const
Definition: DeclID.h:124
friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS)
Definition: DeclID.h:145
DeclIDBase(unsigned LocalID, unsigned ModuleFileIndex)
Definition: DeclID.h:119
A helper iterator adaptor to convert the iterators to SmallVector<SomeDeclID> to the iterators to Sma...
Definition: DeclID.h:206
DeclIDIterator(const FromTy *ID)
Definition: DeclID.h:210
ToTy operator*() const
Definition: DeclID.h:213
bool operator==(const DeclIDIterator &RHS) const
Definition: DeclID.h:215
GlobalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
Definition: DeclID.h:192
GlobalDeclID(DeclID ID)
Definition: DeclID.h:190
LocalDeclID & operator++()
Definition: DeclID.h:173
LocalDeclID(DeclID ID)
Definition: DeclID.h:168
LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
Definition: DeclID.h:170
LocalDeclID(PredefinedDeclIDs ID)
Definition: DeclID.h:167
LocalDeclID operator++(int)
Definition: DeclID.h:178
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
hash_code hash_value(const clang::tooling::dependencies::ModuleID &ID)
static GlobalDeclID getTombstoneKey()
Definition: DeclID.h:229
static unsigned getHashValue(const GlobalDeclID &Key)
Definition: DeclID.h:233
static GlobalDeclID getEmptyKey()
Definition: DeclID.h:225
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R)
Definition: DeclID.h:241