Skip to content

Commit faf1bc5

Browse files
ChuanqiXu9AlexisPerry
authored andcommitted
[NFC] [Serialization] Unify how LocalDeclID can be created
Now we can create a LocalDeclID directly with an integer without verifying. It may be hard to refactor if we want to change the way we serialize DeclIDs (See llvm#95897). Also it is hard for us to debug if someday someone construct a LocalDeclID with an incorrect value. So in this patch, I tried to unify the way we can construct a LocalDeclID in ASTReader, where we will construct the LocalDeclID from the serialized data. Also, now we can verify the constructed LocalDeclID sooner in the new interface.
1 parent 10b6a10 commit faf1bc5

File tree

13 files changed

+193
-137
lines changed

13 files changed

+193
-137
lines changed

clang/include/clang/AST/ASTUnresolvedSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ASTUnresolvedSet {
5858
}
5959

6060
void addLazyDecl(ASTContext &C, GlobalDeclID ID, AccessSpecifier AS) {
61-
Decls.push_back(DeclAccessPair::makeLazy(ID.get(), AS), C);
61+
Decls.push_back(DeclAccessPair::makeLazy(ID.getRawValue(), AS), C);
6262
}
6363

6464
/// Replaces the given declaration with the new one, once.

clang/include/clang/AST/DeclID.h

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,8 @@ class DeclIDBase {
116116
DeclIDBase() : ID(PREDEF_DECL_NULL_ID) {}
117117
explicit DeclIDBase(DeclID ID) : ID(ID) {}
118118

119-
explicit DeclIDBase(unsigned LocalID, unsigned ModuleFileIndex) {
120-
ID = (DeclID)LocalID | ((DeclID)ModuleFileIndex << 32);
121-
}
122-
123119
public:
124-
DeclID get() const { return ID; }
120+
DeclID getRawValue() const { return ID; }
125121

126122
explicit operator DeclID() const { return ID; }
127123

@@ -135,12 +131,33 @@ class DeclIDBase {
135131

136132
unsigned getLocalDeclIndex() const;
137133

134+
// The DeclID may be compared with predefined decl ID.
135+
friend bool operator==(const DeclIDBase &LHS, const DeclID &RHS) {
136+
return LHS.ID == RHS;
137+
}
138+
friend bool operator!=(const DeclIDBase &LHS, const DeclID &RHS) {
139+
return !operator==(LHS, RHS);
140+
}
141+
friend bool operator<(const DeclIDBase &LHS, const DeclID &RHS) {
142+
return LHS.ID < RHS;
143+
}
144+
friend bool operator<=(const DeclIDBase &LHS, const DeclID &RHS) {
145+
return LHS.ID <= RHS;
146+
}
147+
friend bool operator>(const DeclIDBase &LHS, const DeclID &RHS) {
148+
return LHS.ID > RHS;
149+
}
150+
friend bool operator>=(const DeclIDBase &LHS, const DeclID &RHS) {
151+
return LHS.ID >= RHS;
152+
}
153+
138154
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
139155
return LHS.ID == RHS.ID;
140156
}
141157
friend bool operator!=(const DeclIDBase &LHS, const DeclIDBase &RHS) {
142158
return LHS.ID != RHS.ID;
143159
}
160+
144161
// We may sort the decl ID.
145162
friend bool operator<(const DeclIDBase &LHS, const DeclIDBase &RHS) {
146163
return LHS.ID < RHS.ID;
@@ -159,16 +176,27 @@ class DeclIDBase {
159176
DeclID ID;
160177
};
161178

179+
class ASTWriter;
180+
class ASTReader;
181+
namespace serialization {
182+
class ModuleFile;
183+
} // namespace serialization
184+
162185
class LocalDeclID : public DeclIDBase {
163186
using Base = DeclIDBase;
164187

165-
public:
166-
LocalDeclID() : Base() {}
167188
LocalDeclID(PredefinedDeclIDs ID) : Base(ID) {}
168189
explicit LocalDeclID(DeclID ID) : Base(ID) {}
169190

170-
explicit LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
171-
: Base(LocalID, ModuleFileIndex) {}
191+
// Every Decl ID is a local decl ID to the module being writing in ASTWriter.
192+
friend class ASTWriter;
193+
friend class GlobalDeclID;
194+
195+
public:
196+
LocalDeclID() : Base() {}
197+
198+
static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF,
199+
DeclID ID);
172200

173201
LocalDeclID &operator++() {
174202
++ID;
@@ -189,8 +217,8 @@ class GlobalDeclID : public DeclIDBase {
189217
GlobalDeclID() : Base() {}
190218
explicit GlobalDeclID(DeclID ID) : Base(ID) {}
191219

192-
explicit GlobalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
193-
: Base(LocalID, ModuleFileIndex) {}
220+
explicit GlobalDeclID(unsigned ModuleFileIndex, unsigned LocalID)
221+
: Base((DeclID)ModuleFileIndex << 32 | (DeclID)LocalID) {}
194222

195223
// For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
196224
// to a LocalDeclID.
@@ -235,7 +263,7 @@ template <> struct DenseMapInfo<clang::GlobalDeclID> {
235263
// In GlobalDeclID's case, it is pretty common that the lower 32 bits can
236264
// be same.
237265
// FIXME: Remove this when we fix the underlying issue.
238-
return llvm::hash_value(Key.get());
266+
return llvm::hash_value(Key.getRawValue());
239267
}
240268

241269
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,9 +2004,9 @@ struct ObjCCategoriesInfo {
20042004

20052005
ObjCCategoriesInfo() = default;
20062006
ObjCCategoriesInfo(LocalDeclID ID, unsigned Offset)
2007-
: DefinitionID(ID.get()), Offset(Offset) {}
2007+
: DefinitionID(ID.getRawValue()), Offset(Offset) {}
20082008

2009-
LocalDeclID getDefinitionID() const { return LocalDeclID(DefinitionID); }
2009+
DeclID getDefinitionID() const { return DefinitionID; }
20102010

20112011
friend bool operator<(const ObjCCategoriesInfo &X,
20122012
const ObjCCategoriesInfo &Y) {

clang/include/clang/Serialization/ASTReader.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ class ASTReader
375375
friend class serialization::reader::ASTIdentifierLookupTrait;
376376
friend class serialization::ReadMethodPoolVisitor;
377377
friend class TypeLocReader;
378+
friend class LocalDeclID;
378379

379380
using RecordData = SmallVector<uint64_t, 64>;
380381
using RecordDataImpl = SmallVectorImpl<uint64_t>;
@@ -1501,7 +1502,8 @@ class ASTReader
15011502
: iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
15021503

15031504
value_type operator*() const {
1504-
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, (LocalDeclID)*I));
1505+
LocalDeclID ID = LocalDeclID::get(*Reader, *Mod, *I);
1506+
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, ID));
15051507
}
15061508

15071509
value_type operator->() const { return **this; }
@@ -1955,12 +1957,12 @@ class ASTReader
19551957
/// given module.
19561958
///
19571959
/// \returns The declaration ID read from the record, adjusted to a global ID.
1958-
GlobalDeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1960+
GlobalDeclID ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
19591961
unsigned &Idx);
19601962

19611963
/// Reads a declaration from the given position in a record in the
19621964
/// given module.
1963-
Decl *ReadDecl(ModuleFile &F, const RecordData &R, unsigned &I) {
1965+
Decl *ReadDecl(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
19641966
return GetDecl(ReadDeclID(F, R, I));
19651967
}
19661968

@@ -1969,8 +1971,8 @@ class ASTReader
19691971
///
19701972
/// \returns The declaration read from this location, casted to the given
19711973
/// result type.
1972-
template<typename T>
1973-
T *ReadDeclAs(ModuleFile &F, const RecordData &R, unsigned &I) {
1974+
template <typename T>
1975+
T *ReadDeclAs(ModuleFile &F, const RecordDataImpl &R, unsigned &I) {
19741976
return cast_or_null<T>(GetDecl(ReadDeclID(F, R, I)));
19751977
}
19761978

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,7 @@ class ASTWriter : public ASTDeserializationListener,
729729
if (D->isFromASTFile())
730730
return false;
731731
auto I = DeclIDs.find(D);
732-
return (I == DeclIDs.end() ||
733-
I->second.get() >= clang::NUM_PREDEF_DECL_IDS);
732+
return (I == DeclIDs.end() || I->second >= clang::NUM_PREDEF_DECL_IDS);
734733
};
735734

736735
/// Emit a reference to a declaration.

clang/lib/AST/DeclBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void *Decl::operator new(std::size_t Size, const ASTContext &Context,
8080

8181
uint64_t *PrefixPtr = (uint64_t *)Result - 1;
8282

83-
*PrefixPtr = ID.get();
83+
*PrefixPtr = ID.getRawValue();
8484

8585
// We leave the upper 16 bits to store the module IDs. 48 bits should be
8686
// sufficient to store a declaration ID.

clang/lib/AST/DeclTemplate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ void RedeclarableTemplateDecl::loadLazySpecializationsImpl() const {
339339
ASTContext &Context = getASTContext();
340340
GlobalDeclID *Specs = CommonBasePtr->LazySpecializations;
341341
CommonBasePtr->LazySpecializations = nullptr;
342-
unsigned SpecSize = (*Specs++).get();
342+
unsigned SpecSize = (*Specs++).getRawValue();
343343
for (unsigned I = 0; I != SpecSize; ++I)
344344
(void)Context.getExternalSource()->GetExternalDecl(Specs[I]);
345345
}

clang/lib/Frontend/ASTUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1473,7 +1473,7 @@ void ASTUnit::RealizeTopLevelDeclsFromPreamble() {
14731473
for (const auto TopLevelDecl : TopLevelDeclsInPreamble) {
14741474
// Resolve the declaration ID to an actual declaration, possibly
14751475
// deserializing the declaration in the process.
1476-
if (Decl *D = Reader->GetDecl(Reader->getGlobalDeclID(MF, TopLevelDecl)))
1476+
if (Decl *D = Reader->GetLocalDecl(MF, TopLevelDecl))
14771477
Resolved.push_back(D);
14781478
}
14791479
TopLevelDeclsInPreamble.clear();

0 commit comments

Comments
 (0)