Skip to content

Commit 3c8e76d

Browse files
committed
[NFC] Move DeclID from serialization/ASTBitCodes.h to AST/DeclID.h
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 da1e3e8 commit 3c8e76d

31 files changed

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