Skip to content

Commit b467c6b

Browse files
committed
[NFC] [Serialization] Turn type alias GlobalDeclID into a class
Succsessor of b8e3b2a. This patch also converts the type alias GlobalDeclID to a class to improve the readability and type safety.
1 parent be1c72d commit b467c6b

File tree

7 files changed

+364
-252
lines changed

7 files changed

+364
-252
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,71 @@ class LocalDeclID {
7979
DeclID ID;
8080
};
8181

82-
// FIXME: Turn GlobalDeclID into class so we can have some type safety when
83-
// we go from local ID to global and vice-versa.
84-
using GlobalDeclID = DeclID;
82+
/// Wrapper class for DeclID. This is helpful to not mix the use of LocalDeclID
83+
/// and GlobalDeclID to improve the type safety.
84+
class GlobalDeclID {
85+
public:
86+
GlobalDeclID() : ID(0) {}
87+
explicit GlobalDeclID(DeclID ID) : ID(ID) {}
88+
89+
DeclID get() const { return ID; }
90+
91+
explicit operator DeclID() const { return ID; }
92+
93+
friend bool operator==(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
94+
return LHS.ID == RHS.ID;
95+
}
96+
friend bool operator!=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
97+
return LHS.ID != RHS.ID;
98+
}
99+
// We may sort the global decl ID.
100+
friend bool operator<(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
101+
return LHS.ID < RHS.ID;
102+
}
103+
friend bool operator>(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
104+
return LHS.ID > RHS.ID;
105+
}
106+
friend bool operator<=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
107+
return LHS.ID <= RHS.ID;
108+
}
109+
friend bool operator>=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) {
110+
return LHS.ID >= RHS.ID;
111+
}
112+
113+
private:
114+
DeclID ID;
115+
};
116+
117+
/// A helper iterator adaptor to convert the iterators to `SmallVector<DeclID>`
118+
/// to the iterators to `SmallVector<GlobalDeclID>`.
119+
class GlobalDeclIDIterator
120+
: public llvm::iterator_adaptor_base<GlobalDeclIDIterator, const DeclID *,
121+
std::forward_iterator_tag,
122+
GlobalDeclID> {
123+
public:
124+
GlobalDeclIDIterator() : iterator_adaptor_base(nullptr) {}
125+
126+
GlobalDeclIDIterator(const DeclID *ID) : iterator_adaptor_base(ID) {}
127+
128+
value_type operator*() const { return GlobalDeclID(*I); }
129+
130+
bool operator==(const GlobalDeclIDIterator &RHS) const { return I == RHS.I; }
131+
};
132+
133+
/// A helper iterator adaptor to convert the iterators to
134+
/// `SmallVector<GlobalDeclID>` to the iterators to `SmallVector<DeclID>`.
135+
class DeclIDIterator
136+
: public llvm::iterator_adaptor_base<DeclIDIterator, const GlobalDeclID *,
137+
std::forward_iterator_tag, DeclID> {
138+
public:
139+
DeclIDIterator() : iterator_adaptor_base(nullptr) {}
140+
141+
DeclIDIterator(const GlobalDeclID *ID) : iterator_adaptor_base(ID) {}
142+
143+
value_type operator*() const { return DeclID(*I); }
144+
145+
bool operator==(const DeclIDIterator &RHS) const { return I == RHS.I; }
146+
};
85147

86148
/// An ID number that refers to a type in an AST file.
87149
///
@@ -2169,6 +2231,27 @@ template <> struct DenseMapInfo<clang::serialization::DeclarationNameKey> {
21692231
}
21702232
};
21712233

2234+
template <> struct DenseMapInfo<clang::serialization::GlobalDeclID> {
2235+
using DeclID = clang::serialization::DeclID;
2236+
using GlobalDeclID = clang::serialization::GlobalDeclID;
2237+
2238+
static GlobalDeclID getEmptyKey() {
2239+
return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey());
2240+
}
2241+
2242+
static GlobalDeclID getTombstoneKey() {
2243+
return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey());
2244+
}
2245+
2246+
static unsigned getHashValue(const GlobalDeclID &Key) {
2247+
return DenseMapInfo<DeclID>::getHashValue(Key.get());
2248+
}
2249+
2250+
static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) {
2251+
return L == R;
2252+
}
2253+
};
2254+
21722255
} // namespace llvm
21732256

21742257
#endif // LLVM_CLANG_SERIALIZATION_ASTBITCODES_H

clang/include/clang/Serialization/ASTReader.h

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class ASTReader
504504
static_assert(std::is_same_v<serialization::DeclID, Decl::DeclID>);
505505

506506
using GlobalDeclMapType =
507-
ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>;
507+
ContinuousRangeMap<serialization::GlobalDeclID, ModuleFile *, 4>;
508508

509509
/// Mapping from global declaration IDs to the module in which the
510510
/// declaration resides.
@@ -513,14 +513,14 @@ class ASTReader
513513
using FileOffset = std::pair<ModuleFile *, uint64_t>;
514514
using FileOffsetsTy = SmallVector<FileOffset, 2>;
515515
using DeclUpdateOffsetsMap =
516-
llvm::DenseMap<serialization::DeclID, FileOffsetsTy>;
516+
llvm::DenseMap<serialization::GlobalDeclID, FileOffsetsTy>;
517517

518518
/// Declarations that have modifications residing in a later file
519519
/// in the chain.
520520
DeclUpdateOffsetsMap DeclUpdateOffsets;
521521

522522
using DelayedNamespaceOffsetMapTy = llvm::DenseMap<
523-
serialization::DeclID,
523+
serialization::GlobalDeclID,
524524
std::pair</*LexicalOffset*/ uint64_t, /*VisibleOffset*/ uint64_t>>;
525525

526526
/// Mapping from global declaration IDs to the lexical and visible block
@@ -635,7 +635,7 @@ class ASTReader
635635

636636
/// Updates to the visible declarations of declaration contexts that
637637
/// haven't been loaded yet.
638-
llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates>
638+
llvm::DenseMap<serialization::GlobalDeclID, DeclContextVisibleUpdates>
639639
PendingVisibleUpdates;
640640

641641
/// The set of C++ or Objective-C classes that have forward
@@ -662,7 +662,8 @@ class ASTReader
662662
/// Read the record that describes the visible contents of a DC.
663663
bool ReadVisibleDeclContextStorage(ModuleFile &M,
664664
llvm::BitstreamCursor &Cursor,
665-
uint64_t Offset, serialization::DeclID ID);
665+
uint64_t Offset,
666+
serialization::GlobalDeclID ID);
666667

667668
/// A vector containing identifiers that have already been
668669
/// loaded.
@@ -815,29 +816,38 @@ class ASTReader
815816
/// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks
816817
/// in the chain. The referenced declarations are deserialized and passed to
817818
/// the consumer eagerly.
818-
SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls;
819+
SmallVector<serialization::GlobalDeclID, 16> EagerlyDeserializedDecls;
819820

820821
/// The IDs of all tentative definitions stored in the chain.
821822
///
822823
/// Sema keeps track of all tentative definitions in a TU because it has to
823824
/// complete them and pass them on to CodeGen. Thus, tentative definitions in
824825
/// the PCH chain must be eagerly deserialized.
825-
SmallVector<serialization::DeclID, 16> TentativeDefinitions;
826+
SmallVector<serialization::GlobalDeclID, 16> TentativeDefinitions;
826827

827828
/// The IDs of all CXXRecordDecls stored in the chain whose VTables are
828829
/// used.
829830
///
830831
/// CodeGen has to emit VTables for these records, so they have to be eagerly
831832
/// deserialized.
832-
SmallVector<serialization::DeclID, 64> VTableUses;
833+
struct VTableUse {
834+
serialization::GlobalDeclID ID;
835+
SourceLocation::UIntTy RawLoc;
836+
bool Used;
837+
};
838+
SmallVector<VTableUse> VTableUses;
833839

834840
/// A snapshot of the pending instantiations in the chain.
835841
///
836842
/// This record tracks the instantiations that Sema has to perform at the
837843
/// end of the TU. It consists of a pair of values for every pending
838844
/// instantiation where the first value is the ID of the decl and the second
839845
/// is the instantiation location.
840-
SmallVector<serialization::DeclID, 64> PendingInstantiations;
846+
struct PendingInstantiation {
847+
serialization::GlobalDeclID ID;
848+
SourceLocation::UIntTy RawLoc;
849+
};
850+
SmallVector<PendingInstantiation, 64> PendingInstantiations;
841851

842852
//@}
843853

@@ -847,11 +857,11 @@ class ASTReader
847857

848858
/// A snapshot of Sema's unused file-scoped variable tracking, for
849859
/// generating warnings.
850-
SmallVector<serialization::DeclID, 16> UnusedFileScopedDecls;
860+
SmallVector<serialization::GlobalDeclID, 16> UnusedFileScopedDecls;
851861

852862
/// A list of all the delegating constructors we've seen, to diagnose
853863
/// cycles.
854-
SmallVector<serialization::DeclID, 4> DelegatingCtorDecls;
864+
SmallVector<serialization::GlobalDeclID, 4> DelegatingCtorDecls;
855865

856866
/// Method selectors used in a @selector expression. Used for
857867
/// implementation of -Wselector.
@@ -864,7 +874,7 @@ class ASTReader
864874
/// The IDs of type aliases for ext_vectors that exist in the chain.
865875
///
866876
/// Used by Sema for finding sugared names for ext_vectors in diagnostics.
867-
SmallVector<serialization::DeclID, 4> ExtVectorDecls;
877+
SmallVector<serialization::GlobalDeclID, 4> ExtVectorDecls;
868878

869879
//@}
870880

@@ -875,7 +885,7 @@ class ASTReader
875885
/// The IDs of all potentially unused typedef names in the chain.
876886
///
877887
/// Sema tracks these to emit warnings.
878-
SmallVector<serialization::DeclID, 16> UnusedLocalTypedefNameCandidates;
888+
SmallVector<serialization::GlobalDeclID, 16> UnusedLocalTypedefNameCandidates;
879889

880890
/// Our current depth in #pragma cuda force_host_device begin/end
881891
/// macros.
@@ -884,7 +894,7 @@ class ASTReader
884894
/// The IDs of the declarations Sema stores directly.
885895
///
886896
/// Sema tracks a few important decls, such as namespace std, directly.
887-
SmallVector<serialization::DeclID, 4> SemaDeclRefs;
897+
SmallVector<serialization::GlobalDeclID, 4> SemaDeclRefs;
888898

889899
/// The IDs of the types ASTContext stores directly.
890900
///
@@ -895,7 +905,7 @@ class ASTReader
895905
///
896906
/// The AST context tracks a few important decls, currently cudaConfigureCall,
897907
/// directly.
898-
SmallVector<serialization::DeclID, 2> CUDASpecialDeclRefs;
908+
SmallVector<serialization::GlobalDeclID, 2> CUDASpecialDeclRefs;
899909

900910
/// The floating point pragma option settings.
901911
SmallVector<uint64_t, 1> FPPragmaOptions;
@@ -944,11 +954,15 @@ class ASTReader
944954
llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap;
945955

946956
/// A list of the namespaces we've seen.
947-
SmallVector<serialization::DeclID, 4> KnownNamespaces;
957+
SmallVector<serialization::GlobalDeclID, 4> KnownNamespaces;
948958

949959
/// A list of undefined decls with internal linkage followed by the
950960
/// SourceLocation of a matching ODR-use.
951-
SmallVector<serialization::DeclID, 8> UndefinedButUsed;
961+
struct UndefinedButUsedDecl {
962+
serialization::GlobalDeclID ID;
963+
SourceLocation::UIntTy RawLoc;
964+
};
965+
SmallVector<UndefinedButUsedDecl, 8> UndefinedButUsed;
952966

953967
/// Delete expressions to analyze at the end of translation unit.
954968
SmallVector<uint64_t, 8> DelayedDeleteExprs;
@@ -960,7 +974,8 @@ class ASTReader
960974
/// The IDs of all decls to be checked for deferred diags.
961975
///
962976
/// Sema tracks these to emit deferred diags.
963-
llvm::SmallSetVector<serialization::DeclID, 4> DeclsToCheckForDeferredDiags;
977+
llvm::SmallSetVector<serialization::GlobalDeclID, 4>
978+
DeclsToCheckForDeferredDiags;
964979

965980
private:
966981
struct ImportedSubmodule {
@@ -1097,7 +1112,7 @@ class ASTReader
10971112
///
10981113
/// The declarations on the identifier chain for these identifiers will be
10991114
/// loaded once the recursive loading has completed.
1100-
llvm::MapVector<IdentifierInfo *, SmallVector<serialization::DeclID, 4>>
1115+
llvm::MapVector<IdentifierInfo *, SmallVector<serialization::GlobalDeclID, 4>>
11011116
PendingIdentifierInfos;
11021117

11031118
/// The set of lookup results that we have faked in order to support
@@ -1225,7 +1240,7 @@ class ASTReader
12251240
SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded;
12261241

12271242
using KeyDeclsMap =
1228-
llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2>>;
1243+
llvm::DenseMap<Decl *, SmallVector<serialization::GlobalDeclID, 2>>;
12291244

12301245
/// A mapping from canonical declarations to the set of global
12311246
/// declaration IDs for key declaration that have been merged with that
@@ -1434,15 +1449,15 @@ class ASTReader
14341449
QualType readTypeRecord(unsigned Index);
14351450
RecordLocation TypeCursorForIndex(unsigned Index);
14361451
void LoadedDecl(unsigned Index, Decl *D);
1437-
Decl *ReadDeclRecord(serialization::DeclID ID);
1452+
Decl *ReadDeclRecord(serialization::GlobalDeclID ID);
14381453
void markIncompleteDeclChain(Decl *D);
14391454

14401455
/// Returns the most recent declaration of a declaration (which must be
14411456
/// of a redeclarable kind) that is either local or has already been loaded
14421457
/// merged into its redecl chain.
14431458
Decl *getMostRecentExistingDecl(Decl *D);
14441459

1445-
RecordLocation DeclCursorForID(serialization::DeclID ID,
1460+
RecordLocation DeclCursorForID(serialization::GlobalDeclID ID,
14461461
SourceLocation &Location);
14471462
void loadDeclUpdateRecords(PendingUpdateRecord &Record);
14481463
void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
@@ -1901,8 +1916,8 @@ class ASTReader
19011916

19021917
/// Map from a local declaration ID within a given module to a
19031918
/// global declaration ID.
1904-
serialization::DeclID getGlobalDeclID(ModuleFile &F,
1905-
serialization::LocalDeclID LocalID) const;
1919+
serialization::GlobalDeclID
1920+
getGlobalDeclID(ModuleFile &F, serialization::LocalDeclID LocalID) const;
19061921

19071922
/// Returns true if global DeclID \p ID originated from module \p M.
19081923
bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const;
@@ -1916,12 +1931,12 @@ class ASTReader
19161931

19171932
/// Resolve a declaration ID into a declaration, potentially
19181933
/// building a new declaration.
1919-
Decl *GetDecl(serialization::DeclID ID);
1920-
Decl *GetExternalDecl(serialization::DeclID ID) override;
1934+
Decl *GetDecl(serialization::GlobalDeclID ID);
1935+
Decl *GetExternalDecl(Decl::DeclID ID) override;
19211936

19221937
/// Resolve a declaration ID into a declaration. Return 0 if it's not
19231938
/// been loaded yet.
1924-
Decl *GetExistingDecl(serialization::DeclID ID);
1939+
Decl *GetExistingDecl(serialization::GlobalDeclID ID);
19251940

19261941
/// Reads a declaration with the given local ID in the given module.
19271942
Decl *GetLocalDecl(ModuleFile &F, serialization::LocalDeclID LocalID) {
@@ -1943,14 +1958,14 @@ class ASTReader
19431958
/// module file.
19441959
serialization::DeclID
19451960
mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
1946-
serialization::DeclID GlobalID);
1961+
serialization::GlobalDeclID GlobalID);
19471962

19481963
/// Reads a declaration ID from the given position in a record in the
19491964
/// given module.
19501965
///
19511966
/// \returns The declaration ID read from the record, adjusted to a global ID.
1952-
serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record,
1953-
unsigned &Idx);
1967+
serialization::GlobalDeclID
1968+
ReadDeclID(ModuleFile &F, const RecordData &Record, unsigned &Idx);
19541969

19551970
/// Reads a declaration from the given position in a record in the
19561971
/// given module.
@@ -2124,10 +2139,10 @@ class ASTReader
21242139
void LoadSelector(Selector Sel);
21252140

21262141
void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
2127-
void
2128-
SetGloballyVisibleDecls(IdentifierInfo *II,
2129-
const SmallVectorImpl<serialization::DeclID> &DeclIDs,
2130-
SmallVectorImpl<Decl *> *Decls = nullptr);
2142+
void SetGloballyVisibleDecls(
2143+
IdentifierInfo *II,
2144+
const SmallVectorImpl<serialization::GlobalDeclID> &DeclIDs,
2145+
SmallVectorImpl<Decl *> *Decls = nullptr);
21312146

21322147
/// Report a diagnostic.
21332148
DiagnosticBuilder Diag(unsigned DiagID) const;
@@ -2368,7 +2383,7 @@ class ASTReader
23682383

23692384
// Contains the IDs for declarations that were requested before we have
23702385
// access to a Sema object.
2371-
SmallVector<uint64_t, 16> PreloadedDeclIDs;
2386+
SmallVector<serialization::GlobalDeclID, 16> PreloadedDeclIDs;
23722387

23732388
/// Retrieve the semantic analysis object used to analyze the
23742389
/// translation unit in which the precompiled header is being

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class ASTRecordReader
182182
/// Reads a declaration ID from the given position in this record.
183183
///
184184
/// \returns The declaration ID read from the record, adjusted to a global ID.
185-
serialization::DeclID readDeclID() {
185+
serialization::GlobalDeclID readDeclID() {
186186
return Reader->ReadDeclID(*F, Record, Idx);
187187
}
188188

0 commit comments

Comments
 (0)