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;

clang/lib/AST/DeclTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
337337
CommonBase *CommonBasePtr = getMostRecentDecl()->getCommonPtr();
338338
if (CommonBasePtr->LazySpecializations) {
339339
ASTContext &Context = getASTContext();
340-
uint32_t *Specs = CommonBasePtr->LazySpecializations;
340+
Decl::DeclID *Specs = CommonBasePtr->LazySpecializations;
341341
CommonBasePtr->LazySpecializations = nullptr;
342342
for (uint32_t I = 0, N = *Specs++; I != N; ++I)
343343
(void)Context.getExternalSource()->GetExternalDecl(Specs[I]);

clang/lib/AST/ExternalASTSource.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,7 @@ bool ExternalASTSource::layoutRecordType(
6868
return false;
6969
}
7070

71-
Decl *ExternalASTSource::GetExternalDecl(uint32_t ID) {
72-
return nullptr;
73-
}
71+
Decl *ExternalASTSource::GetExternalDecl(Decl::DeclID ID) { return nullptr; }
7472

7573
Selector ExternalASTSource::GetExternalSelector(uint32_t ID) {
7674
return Selector();

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void MultiplexExternalSemaSource::AddSource(ExternalSemaSource *Source) {
4646
// ExternalASTSource.
4747
//===----------------------------------------------------------------------===//
4848

49-
Decl *MultiplexExternalSemaSource::GetExternalDecl(uint32_t ID) {
49+
Decl *MultiplexExternalSemaSource::GetExternalDecl(Decl::DeclID ID) {
5050
for(size_t i = 0; i < Sources.size(); ++i)
5151
if (Decl *Result = Sources[i]->GetExternalDecl(ID))
5252
return Result;

clang/lib/Serialization/ASTReader.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -954,14 +954,14 @@ ASTSelectorLookupTrait::ReadData(Selector, const unsigned char* d,
954954
// Load instance methods
955955
for (unsigned I = 0; I != NumInstanceMethods; ++I) {
956956
if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
957-
F, endian::readNext<uint32_t, llvm::endianness::little>(d)))
957+
F, endian::readNext<DeclID, llvm::endianness::little>(d)))
958958
Result.Instance.push_back(Method);
959959
}
960960

961961
// Load factory methods
962962
for (unsigned I = 0; I != NumFactoryMethods; ++I) {
963963
if (ObjCMethodDecl *Method = Reader.GetLocalDeclAs<ObjCMethodDecl>(
964-
F, endian::readNext<uint32_t, llvm::endianness::little>(d)))
964+
F, endian::readNext<DeclID, llvm::endianness::little>(d)))
965965
Result.Factory.push_back(Method);
966966
}
967967

@@ -1088,10 +1088,10 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
10881088
// Read all of the declarations visible at global scope with this
10891089
// name.
10901090
if (DataLen > 0) {
1091-
SmallVector<uint32_t, 4> DeclIDs;
1092-
for (; DataLen > 0; DataLen -= 4)
1091+
SmallVector<DeclID, 4> DeclIDs;
1092+
for (; DataLen > 0; DataLen -= sizeof(DeclID))
10931093
DeclIDs.push_back(Reader.getGlobalDeclID(
1094-
F, endian::readNext<uint32_t, llvm::endianness::little>(d)));
1094+
F, endian::readNext<DeclID, llvm::endianness::little>(d)));
10951095
Reader.SetGloballyVisibleDecls(II, DeclIDs);
10961096
}
10971097

@@ -1211,8 +1211,8 @@ void ASTDeclContextNameLookupTrait::ReadDataInto(internal_key_type,
12111211
data_type_builder &Val) {
12121212
using namespace llvm::support;
12131213

1214-
for (unsigned NumDecls = DataLen / 4; NumDecls; --NumDecls) {
1215-
uint32_t LocalID = endian::readNext<uint32_t, llvm::endianness::little>(d);
1214+
for (unsigned NumDecls = DataLen / sizeof(DeclID); NumDecls; --NumDecls) {
1215+
DeclID LocalID = endian::readNext<DeclID, llvm::endianness::little>(d);
12161216
Val.insert(Reader.getGlobalDeclID(F, LocalID));
12171217
}
12181218
}
@@ -1259,9 +1259,8 @@ bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
12591259
if (!Lex.first) {
12601260
Lex = std::make_pair(
12611261
&M, llvm::ArrayRef(
1262-
reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
1263-
Blob.data()),
1264-
Blob.size() / 4));
1262+
reinterpret_cast<const unalighed_decl_id_t *>(Blob.data()),
1263+
Blob.size() / sizeof(DeclID)));
12651264
}
12661265
DC->setHasExternalLexicalStorage(true);
12671266
return false;
@@ -3389,9 +3388,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
33893388
case TU_UPDATE_LEXICAL: {
33903389
DeclContext *TU = ContextObj->getTranslationUnitDecl();
33913390
LexicalContents Contents(
3392-
reinterpret_cast<const llvm::support::unaligned_uint32_t *>(
3393-
Blob.data()),
3394-
static_cast<unsigned int>(Blob.size() / 4));
3391+
reinterpret_cast<const unalighed_decl_id_t *>(Blob.data()),
3392+
static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
33953393
TULexicalDecls.push_back(std::make_pair(&F, Contents));
33963394
TU->setHasExternalLexicalStorage(true);
33973395
break;
@@ -6010,7 +6008,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
60106008
case SUBMODULE_INITIALIZERS: {
60116009
if (!ContextObj)
60126010
break;
6013-
SmallVector<uint32_t, 16> Inits;
6011+
SmallVector<DeclID, 16> Inits;
60146012
for (auto &ID : Record)
60156013
Inits.push_back(getGlobalDeclID(F, ID));
60166014
ContextObj->addLazyModuleInitializers(CurrentModule, Inits);
@@ -7517,9 +7515,7 @@ ASTRecordReader::readASTTemplateArgumentListInfo() {
75177515
return ASTTemplateArgumentListInfo::Create(getContext(), Result);
75187516
}
75197517

7520-
Decl *ASTReader::GetExternalDecl(uint32_t ID) {
7521-
return GetDecl(ID);
7522-
}
7518+
Decl *ASTReader::GetExternalDecl(DeclID ID) { return GetDecl(ID); }
75237519

75247520
void ASTReader::CompleteRedeclChain(const Decl *D) {
75257521
if (NumCurrentElementsDeserializing) {
@@ -7660,8 +7656,8 @@ ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const {
76607656
if (!F.ModuleOffsetMap.empty())
76617657
ReadModuleOffsetMap(F);
76627658

7663-
ContinuousRangeMap<uint32_t, int, 2>::iterator I
7664-
= F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
7659+
ContinuousRangeMap<DeclID, int, 2>::iterator I =
7660+
F.DeclRemap.find(LocalID - NUM_PREDEF_DECL_IDS);
76657661
assert(I != F.DeclRemap.end() && "Invalid index into decl index remap");
76667662

76677663
return LocalID + I->second;
@@ -8833,10 +8829,9 @@ void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
88338829
/// \param Decls if non-null, this vector will be populated with the set of
88348830
/// deserialized declarations. These declarations will not be pushed into
88358831
/// scope.
8836-
void
8837-
ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8838-
const SmallVectorImpl<uint32_t> &DeclIDs,
8839-
SmallVectorImpl<Decl *> *Decls) {
8832+
void ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II,
8833+
const SmallVectorImpl<DeclID> &DeclIDs,
8834+
SmallVectorImpl<Decl *> *Decls) {
88408835
if (NumCurrentElementsDeserializing && !Decls) {
88418836
PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end());
88428837
return;
@@ -9560,7 +9555,7 @@ void ASTReader::finishPendingActions() {
95609555

95619556
while (!PendingIdentifierInfos.empty()) {
95629557
IdentifierInfo *II = PendingIdentifierInfos.back().first;
9563-
SmallVector<uint32_t, 4> DeclIDs =
9558+
SmallVector<DeclID, 4> DeclIDs =
95649559
std::move(PendingIdentifierInfos.back().second);
95659560
PendingIdentifierInfos.pop_back();
95669561

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3140,7 +3140,7 @@ class AttrReader {
31403140

31413141
OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
31423142

3143-
template <typename T> T *GetLocalDeclAs(uint32_t LocalID) {
3143+
template <typename T> T *GetLocalDeclAs(DeclID LocalID) {
31443144
return Reader.GetLocalDeclAs<T>(LocalID);
31453145
}
31463146
};

0 commit comments

Comments
 (0)