Skip to content

Commit c2a98fd

Browse files
authored
[NFC] Move DeclID from serialization/ASTBitCodes.h to AST/DeclID.h (#89873)
Previously, the DeclID is defined in serialization/ASTBitCodes.h under clang::serialization namespace. However, actually the DeclID is not purely used in serialization part. The DeclID is already widely used in AST and all around the clang project via classes like `LazyPtrDecl` or calling `ExternalASTSource::getExernalDecl()`. All such uses are via the raw underlying type of `DeclID` as `uint32_t`. This is not pretty good. This patch moves the DeclID class family to a new header `AST/DeclID.h` so that the whole project can use the wrapped class `DeclID`, `GlobalDeclID` and `LocalDeclID` instead of the raw underlying type. This can improve the readability and the type safety.
1 parent fd5f06e commit c2a98fd

31 files changed

+384
-398
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
455455
/// initialization of another module).
456456
struct PerModuleInitializers {
457457
llvm::SmallVector<Decl*, 4> Initializers;
458-
llvm::SmallVector<Decl::DeclID, 4> LazyInitializers;
458+
llvm::SmallVector<DeclID, 4> LazyInitializers;
459459

460460
void resolve(ASTContext &Ctx);
461461
};
@@ -1059,7 +1059,7 @@ class ASTContext : public RefCountedBase<ASTContext> {
10591059
/// or an ImportDecl nominating another module that has initializers.
10601060
void addModuleInitializer(Module *M, Decl *Init);
10611061

1062-
void addLazyModuleInitializers(Module *M, ArrayRef<Decl::DeclID> IDs);
1062+
void addLazyModuleInitializers(Module *M, ArrayRef<DeclID> IDs);
10631063

10641064
/// Get the initializations to perform when importing a module, if any.
10651065
ArrayRef<Decl*> getModuleInitializers(Module *M);

clang/include/clang/AST/DeclBase.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "clang/AST/ASTDumperUtils.h"
1717
#include "clang/AST/AttrIterator.h"
18+
#include "clang/AST/DeclID.h"
1819
#include "clang/AST/DeclarationName.h"
1920
#include "clang/AST/SelectorLocationsKind.h"
2021
#include "clang/Basic/IdentifierTable.h"
@@ -239,9 +240,6 @@ class alignas(8) Decl {
239240
ModulePrivate
240241
};
241242

242-
/// An ID number that refers to a declaration in an AST file.
243-
using DeclID = uint32_t;
244-
245243
protected:
246244
/// The next declaration within the same lexical
247245
/// DeclContext. These pointers form the linked list that is

clang/include/clang/AST/DeclID.h

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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/iterator.h"
20+
21+
namespace clang {
22+
23+
/// Predefined declaration IDs.
24+
///
25+
/// These declaration IDs correspond to predefined declarations in the AST
26+
/// context, such as the NULL declaration ID. Such declarations are never
27+
/// actually serialized, since they will be built by the AST context when
28+
/// it is created.
29+
enum PredefinedDeclIDs {
30+
/// The NULL declaration.
31+
PREDEF_DECL_NULL_ID = 0,
32+
33+
/// The translation unit.
34+
PREDEF_DECL_TRANSLATION_UNIT_ID = 1,
35+
36+
/// The Objective-C 'id' type.
37+
PREDEF_DECL_OBJC_ID_ID = 2,
38+
39+
/// The Objective-C 'SEL' type.
40+
PREDEF_DECL_OBJC_SEL_ID = 3,
41+
42+
/// The Objective-C 'Class' type.
43+
PREDEF_DECL_OBJC_CLASS_ID = 4,
44+
45+
/// The Objective-C 'Protocol' type.
46+
PREDEF_DECL_OBJC_PROTOCOL_ID = 5,
47+
48+
/// The signed 128-bit integer type.
49+
PREDEF_DECL_INT_128_ID = 6,
50+
51+
/// The unsigned 128-bit integer type.
52+
PREDEF_DECL_UNSIGNED_INT_128_ID = 7,
53+
54+
/// The internal 'instancetype' typedef.
55+
PREDEF_DECL_OBJC_INSTANCETYPE_ID = 8,
56+
57+
/// The internal '__builtin_va_list' typedef.
58+
PREDEF_DECL_BUILTIN_VA_LIST_ID = 9,
59+
60+
/// The internal '__va_list_tag' struct, if any.
61+
PREDEF_DECL_VA_LIST_TAG = 10,
62+
63+
/// The internal '__builtin_ms_va_list' typedef.
64+
PREDEF_DECL_BUILTIN_MS_VA_LIST_ID = 11,
65+
66+
/// The predeclared '_GUID' struct.
67+
PREDEF_DECL_BUILTIN_MS_GUID_ID = 12,
68+
69+
/// The extern "C" context.
70+
PREDEF_DECL_EXTERN_C_CONTEXT_ID = 13,
71+
72+
/// The internal '__make_integer_seq' template.
73+
PREDEF_DECL_MAKE_INTEGER_SEQ_ID = 14,
74+
75+
/// The internal '__NSConstantString' typedef.
76+
PREDEF_DECL_CF_CONSTANT_STRING_ID = 15,
77+
78+
/// The internal '__NSConstantString' tag type.
79+
PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID = 16,
80+
81+
/// The internal '__type_pack_element' template.
82+
PREDEF_DECL_TYPE_PACK_ELEMENT_ID = 17,
83+
};
84+
85+
/// The number of declaration IDs that are predefined.
86+
///
87+
/// For more information about predefined declarations, see the
88+
/// \c PredefinedDeclIDs type and the PREDEF_DECL_*_ID constants.
89+
const unsigned int NUM_PREDEF_DECL_IDS = 18;
90+
91+
/// An ID number that refers to a declaration in an AST file.
92+
///
93+
/// The ID numbers of declarations are consecutive (in order of
94+
/// discovery), with values below NUM_PREDEF_DECL_IDS being reserved.
95+
/// At the start of a chain of precompiled headers, declaration ID 1 is
96+
/// used for the translation unit declaration.
97+
using DeclID = uint32_t;
98+
99+
class LocalDeclID {
100+
public:
101+
explicit LocalDeclID(DeclID ID) : ID(ID) {}
102+
103+
DeclID get() const { return ID; }
104+
105+
private:
106+
DeclID ID;
107+
};
108+
109+
/// Wrapper class for DeclID. This is helpful to not mix the use of LocalDeclID
110+
/// and GlobalDeclID to improve the type safety.
111+
class GlobalDeclID {
112+
public:
113+
GlobalDeclID() : ID(PREDEF_DECL_NULL_ID) {}
114+
explicit GlobalDeclID(DeclID ID) : ID(ID) {}
115+
116+
DeclID get() const { return ID; }
117+
118+
explicit operator DeclID() const { return ID; }
119+
120+
friend bool operator==(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
121+
return LHS.ID == RHS.ID;
122+
}
123+
friend bool operator!=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
124+
return LHS.ID != RHS.ID;
125+
}
126+
// We may sort the global decl ID.
127+
friend bool operator<(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
128+
return LHS.ID < RHS.ID;
129+
}
130+
friend bool operator>(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
131+
return LHS.ID > RHS.ID;
132+
}
133+
friend bool operator<=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
134+
return LHS.ID <= RHS.ID;
135+
}
136+
friend bool operator>=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
137+
return LHS.ID >= RHS.ID;
138+
}
139+
140+
private:
141+
DeclID ID;
142+
};
143+
144+
/// A helper iterator adaptor to convert the iterators to `SmallVector<DeclID>`
145+
/// to the iterators to `SmallVector<GlobalDeclID>`.
146+
class GlobalDeclIDIterator
147+
: public llvm::iterator_adaptor_base<GlobalDeclIDIterator, const DeclID *,
148+
std::forward_iterator_tag,
149+
GlobalDeclID> {
150+
public:
151+
GlobalDeclIDIterator() : iterator_adaptor_base(nullptr) {}
152+
153+
GlobalDeclIDIterator(const DeclID *ID) : iterator_adaptor_base(ID) {}
154+
155+
value_type operator*() const { return GlobalDeclID(*I); }
156+
157+
bool operator==(const GlobalDeclIDIterator &RHS) const { return I == RHS.I; }
158+
};
159+
160+
/// A helper iterator adaptor to convert the iterators to
161+
/// `SmallVector<GlobalDeclID>` to the iterators to `SmallVector<DeclID>`.
162+
class DeclIDIterator
163+
: public llvm::iterator_adaptor_base<DeclIDIterator, const GlobalDeclID *,
164+
std::forward_iterator_tag, DeclID> {
165+
public:
166+
DeclIDIterator() : iterator_adaptor_base(nullptr) {}
167+
168+
DeclIDIterator(const GlobalDeclID *ID) : iterator_adaptor_base(ID) {}
169+
170+
value_type operator*() const { return DeclID(*I); }
171+
172+
bool operator==(const DeclIDIterator &RHS) const { return I == RHS.I; }
173+
};
174+
175+
} // namespace clang
176+
177+
#endif

clang/include/clang/AST/DeclTemplate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ class RedeclarableTemplateDecl : public TemplateDecl,
797797
///
798798
/// The first value in the array is the number of specializations/partial
799799
/// specializations that follow.
800-
Decl::DeclID *LazySpecializations = nullptr;
800+
DeclID *LazySpecializations = nullptr;
801801

802802
/// The set of "injected" template arguments used within this
803803
/// template.

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
9999
/// passes back decl sets as VisibleDeclaration objects.
100100
///
101101
/// The default implementation of this method is a no-op.
102-
virtual Decl *GetExternalDecl(Decl::DeclID ID);
102+
virtual Decl *GetExternalDecl(DeclID ID);
103103

104104
/// Resolve a selector ID into a selector.
105105
///
@@ -579,7 +579,7 @@ using LazyDeclStmtPtr =
579579

580580
/// A lazy pointer to a declaration.
581581
using LazyDeclPtr =
582-
LazyOffsetPtr<Decl, Decl::DeclID, &ExternalASTSource::GetExternalDecl>;
582+
LazyOffsetPtr<Decl, DeclID, &ExternalASTSource::GetExternalDecl>;
583583

584584
/// A lazy pointer to a set of CXXCtorInitializers.
585585
using LazyCXXCtorInitializersPtr =

clang/include/clang/Frontend/ASTUnit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class ASTUnit {
241241

242242
/// A list of the serialization ID numbers for each of the top-level
243243
/// declarations parsed within the precompiled preamble.
244-
std::vector<serialization::DeclID> TopLevelDeclsInPreamble;
244+
std::vector<DeclID> TopLevelDeclsInPreamble;
245245

246246
/// Whether we should be caching code-completion results.
247247
bool ShouldCacheCodeCompletionResults : 1;

clang/include/clang/Frontend/MultiplexConsumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class MultiplexASTDeserializationListener : public ASTDeserializationListener {
3535
void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
3636
void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
3737
void TypeRead(serialization::TypeIdx Idx, QualType T) override;
38-
void DeclRead(serialization::DeclID ID, const Decl *D) override;
38+
void DeclRead(DeclID ID, const Decl *D) override;
3939
void SelectorRead(serialization::SelectorID iD, Selector Sel) override;
4040
void MacroDefinitionRead(serialization::PreprocessedEntityID,
4141
MacroDefinitionRecord *MD) override;

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
6565

6666
/// Resolve a declaration ID into a declaration, potentially
6767
/// building a new declaration.
68-
Decl *GetExternalDecl(Decl::DeclID ID) override;
68+
Decl *GetExternalDecl(DeclID ID) override;
6969

7070
/// Complete the redeclaration chain if it's been extended since the
7171
/// previous generation of the AST source.

0 commit comments

Comments
 (0)