Skip to content

Commit 07b1177

Browse files
committed
[NFC] [Serialization] Use semantical type DeclID instead of raw type 'uint32_t'
This patch tries to use DeclID in the code bases to avoid use the raw type 'uint32_t'. It is problematic to use the raw type 'uint32_t' if we want to change the type of DeclID some day.
1 parent 25a391c commit 07b1177

16 files changed

+67
-65
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<uint32_t, 4> LazyInitializers;
458+
llvm::SmallVector<Decl::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<uint32_t> IDs);
1062+
void addLazyModuleInitializers(Module *M, ArrayRef<Decl::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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ class alignas(8) Decl {
239239
ModulePrivate
240240
};
241241

242+
/// An ID number that refers to a declaration in an AST file.
243+
using DeclID = uint32_t;
244+
242245
protected:
243246
/// The next declaration within the same lexical
244247
/// DeclContext. These pointers form the linked list that is
@@ -349,8 +352,6 @@ class alignas(8) Decl {
349352
LLVM_PREFERRED_TYPE(Linkage)
350353
mutable unsigned CacheValidAndLinkage : 3;
351354

352-
using DeclID = uint32_t;
353-
354355
/// Allocate memory for a deserialized declaration.
355356
///
356357
/// This routine must be used to allocate memory for any declaration that is
@@ -778,9 +779,9 @@ class alignas(8) Decl {
778779

779780
/// Retrieve the global declaration ID associated with this
780781
/// declaration, which specifies where this Decl was loaded from.
781-
unsigned getGlobalID() const {
782+
DeclID getGlobalID() const {
782783
if (isFromASTFile())
783-
return *((const unsigned*)this - 1);
784+
return *((const DeclID *)this - 1);
784785
return 0;
785786
}
786787

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-
uint32_t *LazySpecializations = nullptr;
800+
Decl::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(uint32_t ID);
102+
virtual Decl *GetExternalDecl(Decl::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, uint32_t, &ExternalASTSource::GetExternalDecl>;
582+
LazyOffsetPtr<Decl, Decl::DeclID, &ExternalASTSource::GetExternalDecl>;
583583

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

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(uint32_t ID) override;
68+
Decl *GetExternalDecl(Decl::DeclID ID) override;
6969

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

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ using IdentifierID = uint32_t;
6565
/// discovery), with values below NUM_PREDEF_DECL_IDS being reserved.
6666
/// At the start of a chain of precompiled headers, declaration ID 1 is
6767
/// used for the translation unit declaration.
68+
///
69+
/// FIXME: Merge with Decl::DeclID
6870
using DeclID = uint32_t;
6971

7072
// FIXME: Turn these into classes so we can have some type safety when

clang/include/clang/Serialization/ASTReader.h

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,11 @@ class ASTReader
606606

607607
/// An array of lexical contents of a declaration context, as a sequence of
608608
/// Decl::Kind, DeclID pairs.
609-
using LexicalContents = ArrayRef<llvm::support::unaligned_uint32_t>;
609+
using unalighed_decl_id_t =
610+
llvm::support::detail::packed_endian_specific_integral<
611+
serialization::DeclID, llvm::endianness::native,
612+
llvm::support::unaligned>;
613+
using LexicalContents = ArrayRef<unalighed_decl_id_t>;
610614

611615
/// Map from a DeclContext to its lexical contents.
612616
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
@@ -1093,8 +1097,8 @@ class ASTReader
10931097
///
10941098
/// The declarations on the identifier chain for these identifiers will be
10951099
/// loaded once the recursive loading has completed.
1096-
llvm::MapVector<IdentifierInfo *, SmallVector<uint32_t, 4>>
1097-
PendingIdentifierInfos;
1100+
llvm::MapVector<IdentifierInfo *, SmallVector<serialization::DeclID, 4>>
1101+
PendingIdentifierInfos;
10981102

10991103
/// The set of lookup results that we have faked in order to support
11001104
/// merging of partially deserialized decls but that we have not yet removed.
@@ -1913,22 +1917,22 @@ class ASTReader
19131917
/// Resolve a declaration ID into a declaration, potentially
19141918
/// building a new declaration.
19151919
Decl *GetDecl(serialization::DeclID ID);
1916-
Decl *GetExternalDecl(uint32_t ID) override;
1920+
Decl *GetExternalDecl(serialization::DeclID ID) override;
19171921

19181922
/// Resolve a declaration ID into a declaration. Return 0 if it's not
19191923
/// been loaded yet.
19201924
Decl *GetExistingDecl(serialization::DeclID ID);
19211925

19221926
/// Reads a declaration with the given local ID in the given module.
1923-
Decl *GetLocalDecl(ModuleFile &F, uint32_t LocalID) {
1927+
Decl *GetLocalDecl(ModuleFile &F, serialization::DeclID LocalID) {
19241928
return GetDecl(getGlobalDeclID(F, LocalID));
19251929
}
19261930

19271931
/// Reads a declaration with the given local ID in the given module.
19281932
///
19291933
/// \returns The requested declaration, casted to the given return type.
1930-
template<typename T>
1931-
T *GetLocalDeclAs(ModuleFile &F, uint32_t LocalID) {
1934+
template <typename T>
1935+
T *GetLocalDeclAs(ModuleFile &F, serialization::DeclID LocalID) {
19321936
return cast_or_null<T>(GetLocalDecl(F, LocalID));
19331937
}
19341938

@@ -2120,9 +2124,10 @@ class ASTReader
21202124
void LoadSelector(Selector Sel);
21212125

21222126
void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
2123-
void SetGloballyVisibleDecls(IdentifierInfo *II,
2124-
const SmallVectorImpl<uint32_t> &DeclIDs,
2125-
SmallVectorImpl<Decl *> *Decls = nullptr);
2127+
void
2128+
SetGloballyVisibleDecls(IdentifierInfo *II,
2129+
const SmallVectorImpl<serialization::DeclID> &DeclIDs,
2130+
SmallVectorImpl<Decl *> *Decls = nullptr);
21262131

21272132
/// Report a diagnostic.
21282133
DiagnosticBuilder Diag(unsigned DiagID) const;

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ class ASTRecordReader
143143
/// Reads a declaration with the given local ID in the given module.
144144
///
145145
/// \returns The requested declaration, casted to the given return type.
146-
template<typename T>
147-
T *GetLocalDeclAs(uint32_t LocalID) {
146+
template <typename T> T *GetLocalDeclAs(serialization::LocalDeclID LocalID) {
148147
return cast_or_null<T>(Reader->GetLocalDecl(*F, LocalID));
149148
}
150149

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ class ModuleFile {
462462
serialization::DeclID BaseDeclID = 0;
463463

464464
/// Remapping table for declaration IDs in this module.
465-
ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
465+
ContinuousRangeMap<serialization::DeclID, int, 2> DeclRemap;
466466

467467
/// Mapping from the module files that this module file depends on
468468
/// to the base declaration ID for that module as it is understood within this

clang/lib/AST/ASTContext.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,8 @@ void ASTContext::addModuleInitializer(Module *M, Decl *D) {
10831083
Inits->Initializers.push_back(D);
10841084
}
10851085

1086-
void ASTContext::addLazyModuleInitializers(Module *M, ArrayRef<uint32_t> IDs) {
1086+
void ASTContext::addLazyModuleInitializers(Module *M,
1087+
ArrayRef<Decl::DeclID> IDs) {
10871088
auto *&Inits = ModuleInitializers[M];
10881089
if (!Inits)
10891090
Inits = new (*this) PerModuleInitializers;

0 commit comments

Comments
 (0)