[clang] [NFC] [Serialization] Extract the functionality of merging decls from ASTReaderDecl (PR #103022)
via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 13 01:29:14 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-modules
Author: Chuanqi Xu (ChuanqiXu9)
<details>
<summary>Changes</summary>
Currently we're merging the decls in ASTReaderDecl. But it is not so convinient if we want to merge things in ASTReader.
This patch extract the funcitonality of merging decls from ASTReaderDecl to a new class, ASTReaderMerger. Then it will be easier to merge decls in ASTReader.
This may help the readability slightly.
---
Patch is 47.21 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/103022.diff
6 Files Affected:
- (modified) clang/include/clang/AST/DeclBase.h (+1)
- (modified) clang/include/clang/AST/DeclCXX.h (+1)
- (modified) clang/include/clang/AST/DeclObjC.h (+2)
- (modified) clang/include/clang/AST/Redeclarable.h (+1)
- (modified) clang/include/clang/Serialization/ASTReader.h (+1)
- (modified) clang/lib/Serialization/ASTReaderDecl.cpp (+393-391)
``````````diff
diff --git a/clang/include/clang/AST/DeclBase.h b/clang/include/clang/AST/DeclBase.h
index 04dbd1db6cba81..ee662ed73d7e0e 100644
--- a/clang/include/clang/AST/DeclBase.h
+++ b/clang/include/clang/AST/DeclBase.h
@@ -324,6 +324,7 @@ class alignas(8) Decl {
static bool StatisticsEnabled;
protected:
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTDeclWriter;
friend class ASTNodeImporter;
diff --git a/clang/include/clang/AST/DeclCXX.h b/clang/include/clang/AST/DeclCXX.h
index bf6a5ce92d438d..0d72cc6a08dcb4 100644
--- a/clang/include/clang/AST/DeclCXX.h
+++ b/clang/include/clang/AST/DeclCXX.h
@@ -256,6 +256,7 @@ class CXXBaseSpecifier {
/// Represents a C++ struct/union/class.
class CXXRecordDecl : public RecordDecl {
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTDeclWriter;
friend class ASTNodeImporter;
diff --git a/clang/include/clang/AST/DeclObjC.h b/clang/include/clang/AST/DeclObjC.h
index d2cc61ca19f8a5..1cda70530d7d83 100644
--- a/clang/include/clang/AST/DeclObjC.h
+++ b/clang/include/clang/AST/DeclObjC.h
@@ -1747,6 +1747,7 @@ class ObjCInterfaceDecl : public ObjCContainerDecl
static bool isKnownExtension(ObjCCategoryDecl *Cat);
public:
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTDeclWriter;
friend class ASTReader;
@@ -2134,6 +2135,7 @@ class ObjCProtocolDecl : public ObjCContainerDecl,
void setHasODRHash(bool HasHash);
public:
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTDeclWriter;
friend class ASTReader;
diff --git a/clang/include/clang/AST/Redeclarable.h b/clang/include/clang/AST/Redeclarable.h
index 74ccd74ed60d6b..8d320a9ced2792 100644
--- a/clang/include/clang/AST/Redeclarable.h
+++ b/clang/include/clang/AST/Redeclarable.h
@@ -191,6 +191,7 @@ class Redeclarable {
}
public:
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTDeclWriter;
friend class IncrementalParser;
diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h
index a0e90e62bd60ec..4593213c5f43ce 100644
--- a/clang/include/clang/Serialization/ASTReader.h
+++ b/clang/include/clang/Serialization/ASTReader.h
@@ -378,6 +378,7 @@ class ASTReader
{
public:
/// Types of AST files.
+ friend class ASTDeclMerger;
friend class ASTDeclReader;
friend class ASTIdentifierIterator;
friend class ASTRecordReader;
diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp
index c118f3818467d9..a9199f7e50f5dc 100644
--- a/clang/lib/Serialization/ASTReaderDecl.cpp
+++ b/clang/lib/Serialization/ASTReaderDecl.cpp
@@ -76,402 +76,407 @@ using namespace clang;
using namespace serialization;
//===----------------------------------------------------------------------===//
-// Declaration deserialization
+// Declaration Merging
//===----------------------------------------------------------------------===//
-namespace clang {
-
- class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
- ASTReader &Reader;
- ASTRecordReader &Record;
- ASTReader::RecordLocation Loc;
- const GlobalDeclID ThisDeclID;
- const SourceLocation ThisDeclLoc;
-
- using RecordData = ASTReader::RecordData;
+namespace {
+/// Results from loading a RedeclarableDecl.
+class RedeclarableResult {
+ Decl *MergeWith;
+ GlobalDeclID FirstID;
+ bool IsKeyDecl;
- TypeID DeferredTypeID = 0;
- unsigned AnonymousDeclNumber = 0;
- GlobalDeclID NamedDeclForTagDecl = GlobalDeclID();
- IdentifierInfo *TypedefNameForLinkage = nullptr;
+public:
+ RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
+ : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
- ///A flag to carry the information for a decl from the entity is
- /// used. We use it to delay the marking of the canonical decl as used until
- /// the entire declaration is deserialized and merged.
- bool IsDeclMarkedUsed = false;
+ /// Retrieve the first ID.
+ GlobalDeclID getFirstID() const { return FirstID; }
- uint64_t GetCurrentCursorOffset();
+ /// Is this declaration a key declaration?
+ bool isKeyDecl() const { return IsKeyDecl; }
- uint64_t ReadLocalOffset() {
- uint64_t LocalOffset = Record.readInt();
- assert(LocalOffset < Loc.Offset && "offset point after current record");
- return LocalOffset ? Loc.Offset - LocalOffset : 0;
- }
+ /// Get a known declaration that this should be merged with, if
+ /// any.
+ Decl *getKnownMergeTarget() const { return MergeWith; }
+};
+} // namespace
- uint64_t ReadGlobalOffset() {
- uint64_t Local = ReadLocalOffset();
- return Local ? Record.getGlobalBitOffset(Local) : 0;
- }
+namespace clang {
+class ASTDeclMerger {
+ ASTReader &Reader;
- SourceLocation readSourceLocation() {
- return Record.readSourceLocation();
- }
+public:
+ ASTDeclMerger(ASTReader &Reader) : Reader(Reader) {}
+
+ void mergeLambda(CXXRecordDecl *D, RedeclarableResult &Redecl, Decl &Context,
+ unsigned Number);
+
+ /// \param KeyDeclID the decl ID of the key declaration \param D.
+ /// GlobalDeclID() if \param is not a key declaration.
+ /// See the comments of ASTReader::KeyDecls for the explanation
+ /// of key declaration.
+ template <typename T>
+ void mergeRedeclarableImpl(Redeclarable<T> *D, T *Existing,
+ GlobalDeclID KeyDeclID);
+
+ template <typename T>
+ void mergeRedeclarable(Redeclarable<T> *D, T *Existing,
+ RedeclarableResult &Redecl) {
+ mergeRedeclarableImpl(
+ D, Existing, Redecl.isKeyDecl() ? Redecl.getFirstID() : GlobalDeclID());
+ }
- SourceRange readSourceRange() {
- return Record.readSourceRange();
- }
+ void mergeTemplatePattern(RedeclarableTemplateDecl *D,
+ RedeclarableTemplateDecl *Existing, bool IsKeyDecl);
- TypeSourceInfo *readTypeSourceInfo() {
- return Record.readTypeSourceInfo();
- }
+ void MergeDefinitionData(CXXRecordDecl *D,
+ struct CXXRecordDecl::DefinitionData &&NewDD);
+ void MergeDefinitionData(ObjCInterfaceDecl *D,
+ struct ObjCInterfaceDecl::DefinitionData &&NewDD);
+ void MergeDefinitionData(ObjCProtocolDecl *D,
+ struct ObjCProtocolDecl::DefinitionData &&NewDD);
+};
+} // namespace clang
- GlobalDeclID readDeclID() { return Record.readDeclID(); }
+//===----------------------------------------------------------------------===//
+// Declaration deserialization
+//===----------------------------------------------------------------------===//
- std::string readString() {
- return Record.readString();
- }
+namespace clang {
+class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
+ ASTReader &Reader;
+ ASTDeclMerger MergeImpl;
+ ASTRecordReader &Record;
+ ASTReader::RecordLocation Loc;
+ const GlobalDeclID ThisDeclID;
+ const SourceLocation ThisDeclLoc;
+
+ using RecordData = ASTReader::RecordData;
+
+ TypeID DeferredTypeID = 0;
+ unsigned AnonymousDeclNumber = 0;
+ GlobalDeclID NamedDeclForTagDecl = GlobalDeclID();
+ IdentifierInfo *TypedefNameForLinkage = nullptr;
+
+ /// A flag to carry the information for a decl from the entity is
+ /// used. We use it to delay the marking of the canonical decl as used until
+ /// the entire declaration is deserialized and merged.
+ bool IsDeclMarkedUsed = false;
+
+ uint64_t GetCurrentCursorOffset();
+
+ uint64_t ReadLocalOffset() {
+ uint64_t LocalOffset = Record.readInt();
+ assert(LocalOffset < Loc.Offset && "offset point after current record");
+ return LocalOffset ? Loc.Offset - LocalOffset : 0;
+ }
- void readDeclIDList(SmallVectorImpl<GlobalDeclID> &IDs) {
- for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
- IDs.push_back(readDeclID());
- }
+ uint64_t ReadGlobalOffset() {
+ uint64_t Local = ReadLocalOffset();
+ return Local ? Record.getGlobalBitOffset(Local) : 0;
+ }
- Decl *readDecl() {
- return Record.readDecl();
- }
+ SourceLocation readSourceLocation() { return Record.readSourceLocation(); }
- template<typename T>
- T *readDeclAs() {
- return Record.readDeclAs<T>();
- }
+ SourceRange readSourceRange() { return Record.readSourceRange(); }
- serialization::SubmoduleID readSubmoduleID() {
- if (Record.getIdx() == Record.size())
- return 0;
+ TypeSourceInfo *readTypeSourceInfo() { return Record.readTypeSourceInfo(); }
- return Record.getGlobalSubmoduleID(Record.readInt());
- }
+ GlobalDeclID readDeclID() { return Record.readDeclID(); }
- Module *readModule() {
- return Record.getSubmodule(readSubmoduleID());
- }
+ std::string readString() { return Record.readString(); }
- void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update,
- Decl *LambdaContext = nullptr,
- unsigned IndexInLambdaContext = 0);
- void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
- const CXXRecordDecl *D, Decl *LambdaContext,
- unsigned IndexInLambdaContext);
- void MergeDefinitionData(CXXRecordDecl *D,
- struct CXXRecordDecl::DefinitionData &&NewDD);
- void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
- void MergeDefinitionData(ObjCInterfaceDecl *D,
- struct ObjCInterfaceDecl::DefinitionData &&NewDD);
- void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
- void MergeDefinitionData(ObjCProtocolDecl *D,
- struct ObjCProtocolDecl::DefinitionData &&NewDD);
-
- static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
-
- static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
- DeclContext *DC,
- unsigned Index);
- static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
- unsigned Index, NamedDecl *D);
-
- /// Commit to a primary definition of the class RD, which is known to be
- /// a definition of the class. We might not have read the definition data
- /// for it yet. If we haven't then allocate placeholder definition data
- /// now too.
- static CXXRecordDecl *getOrFakePrimaryClassDefinition(ASTReader &Reader,
- CXXRecordDecl *RD);
-
- /// Results from loading a RedeclarableDecl.
- class RedeclarableResult {
- Decl *MergeWith;
- GlobalDeclID FirstID;
- bool IsKeyDecl;
+ void readDeclIDList(SmallVectorImpl<GlobalDeclID> &IDs) {
+ for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I)
+ IDs.push_back(readDeclID());
+ }
- public:
- RedeclarableResult(Decl *MergeWith, GlobalDeclID FirstID, bool IsKeyDecl)
- : MergeWith(MergeWith), FirstID(FirstID), IsKeyDecl(IsKeyDecl) {}
+ Decl *readDecl() { return Record.readDecl(); }
- /// Retrieve the first ID.
- GlobalDeclID getFirstID() const { return FirstID; }
+ template <typename T> T *readDeclAs() { return Record.readDeclAs<T>(); }
- /// Is this declaration a key declaration?
- bool isKeyDecl() const { return IsKeyDecl; }
+ serialization::SubmoduleID readSubmoduleID() {
+ if (Record.getIdx() == Record.size())
+ return 0;
- /// Get a known declaration that this should be merged with, if
- /// any.
- Decl *getKnownMergeTarget() const { return MergeWith; }
- };
+ return Record.getGlobalSubmoduleID(Record.readInt());
+ }
- /// Class used to capture the result of searching for an existing
- /// declaration of a specific kind and name, along with the ability
- /// to update the place where this result was found (the declaration
- /// chain hanging off an identifier or the DeclContext we searched in)
- /// if requested.
- class FindExistingResult {
- ASTReader &Reader;
- NamedDecl *New = nullptr;
- NamedDecl *Existing = nullptr;
- bool AddResult = false;
- unsigned AnonymousDeclNumber = 0;
- IdentifierInfo *TypedefNameForLinkage = nullptr;
+ Module *readModule() { return Record.getSubmodule(readSubmoduleID()); }
+
+ void ReadCXXRecordDefinition(CXXRecordDecl *D, bool Update,
+ Decl *LambdaContext = nullptr,
+ unsigned IndexInLambdaContext = 0);
+ void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
+ const CXXRecordDecl *D, Decl *LambdaContext,
+ unsigned IndexInLambdaContext);
+ void ReadObjCDefinitionData(struct ObjCInterfaceDecl::DefinitionData &Data);
+ void ReadObjCDefinitionData(struct ObjCProtocolDecl::DefinitionData &Data);
+
+ static DeclContext *getPrimaryDCForAnonymousDecl(DeclContext *LexicalDC);
+
+ static NamedDecl *getAnonymousDeclForMerging(ASTReader &Reader,
+ DeclContext *DC, unsigned Index);
+ static void setAnonymousDeclForMerging(ASTReader &Reader, DeclContext *DC,
+ unsigned Index, NamedDecl *D);
+
+ /// Commit to a primary definition of the class RD, which is known to be
+ /// a definition of the class. We might not have read the definition data
+ /// for it yet. If we haven't then allocate placeholder definition data
+ /// now too.
+ static CXXRecordDecl *getOrFakePrimaryClassDefinition(ASTReader &Reader,
+ CXXRecordDecl *RD);
+
+ /// Class used to capture the result of searching for an existing
+ /// declaration of a specific kind and name, along with the ability
+ /// to update the place where this result was found (the declaration
+ /// chain hanging off an identifier or the DeclContext we searched in)
+ /// if requested.
+ class FindExistingResult {
+ ASTReader &Reader;
+ NamedDecl *New = nullptr;
+ NamedDecl *Existing = nullptr;
+ bool AddResult = false;
+ unsigned AnonymousDeclNumber = 0;
+ IdentifierInfo *TypedefNameForLinkage = nullptr;
- public:
- FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
-
- FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
- unsigned AnonymousDeclNumber,
- IdentifierInfo *TypedefNameForLinkage)
- : Reader(Reader), New(New), Existing(Existing), AddResult(true),
- AnonymousDeclNumber(AnonymousDeclNumber),
- TypedefNameForLinkage(TypedefNameForLinkage) {}
-
- FindExistingResult(FindExistingResult &&Other)
- : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
- AddResult(Other.AddResult),
- AnonymousDeclNumber(Other.AnonymousDeclNumber),
- TypedefNameForLinkage(Other.TypedefNameForLinkage) {
- Other.AddResult = false;
- }
+ public:
+ FindExistingResult(ASTReader &Reader) : Reader(Reader) {}
+
+ FindExistingResult(ASTReader &Reader, NamedDecl *New, NamedDecl *Existing,
+ unsigned AnonymousDeclNumber,
+ IdentifierInfo *TypedefNameForLinkage)
+ : Reader(Reader), New(New), Existing(Existing), AddResult(true),
+ AnonymousDeclNumber(AnonymousDeclNumber),
+ TypedefNameForLinkage(TypedefNameForLinkage) {}
+
+ FindExistingResult(FindExistingResult &&Other)
+ : Reader(Other.Reader), New(Other.New), Existing(Other.Existing),
+ AddResult(Other.AddResult),
+ AnonymousDeclNumber(Other.AnonymousDeclNumber),
+ TypedefNameForLinkage(Other.TypedefNameForLinkage) {
+ Other.AddResult = false;
+ }
- FindExistingResult &operator=(FindExistingResult &&) = delete;
- ~FindExistingResult();
+ FindExistingResult &operator=(FindExistingResult &&) = delete;
+ ~FindExistingResult();
- /// Suppress the addition of this result into the known set of
- /// names.
- void suppress() { AddResult = false; }
+ /// Suppress the addition of this result into the known set of
+ /// names.
+ void suppress() { AddResult = false; }
- operator NamedDecl*() const { return Existing; }
+ operator NamedDecl *() const { return Existing; }
- template<typename T>
- operator T*() const { return dyn_cast_or_null<T>(Existing); }
- };
+ template <typename T> operator T *() const {
+ return dyn_cast_or_null<T>(Existing);
+ }
+ };
- static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
- DeclContext *DC);
- FindExistingResult findExisting(NamedDecl *D);
+ static DeclContext *getPrimaryContextForMerging(ASTReader &Reader,
+ DeclContext *DC);
+ FindExistingResult findExisting(NamedDecl *D);
- public:
- ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
- ASTReader::RecordLocation Loc, GlobalDeclID thisDeclID,
- SourceLocation ThisDeclLoc)
- : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID),
- ThisDeclLoc(ThisDeclLoc) {}
-
- template <typename T>
- static void AddLazySpecializations(T *D,
- SmallVectorImpl<GlobalDeclID> &IDs) {
- if (IDs.empty())
- return;
+public:
+ ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record,
+ ASTReader::RecordLocation Loc, GlobalDeclID thisDeclID,
+ SourceLocation ThisDeclLoc)
+ : Reader(Reader), MergeImpl(Reader), Record(Record), Loc(Loc),
+ ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc) {}
+
+ template <typename T>
+ static void AddLazySpecializations(T *D, SmallVectorImpl<GlobalDeclID> &IDs) {
+ if (IDs.empty())
+ return;
- // FIXME: We should avoid this pattern of getting the ASTContext.
- ASTContext &C = D->getASTContext();
+ // FIXME: We should avoid this pattern of getting the ASTContext.
+ ASTContext &C = D->getASTContext();
- auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
+ auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations;
- if (auto &Old = LazySpecializations) {
- IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0].getRawValue());
- llvm::sort(IDs);
- IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
- }
+ if (auto &Old = LazySpecializations) {
+ IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0].getRawValue());
+ llvm::sort(IDs);
+ IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end());
+ }
- auto *Result = new (C) GlobalDeclID[1 + IDs.size()];
- *Result = GlobalDeclID(IDs.size());
+ auto *Result = new (C) GlobalDeclID[1 + IDs.size()];
+ *Result = GlobalDeclID(IDs.size());
- std::copy(IDs.begin(), IDs.end(), Result + 1);
+ std::copy(IDs.begin(), IDs.end(), Result + 1);
- LazySpecializations = Result;
- }
-
- template <typename DeclT>
- static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
- static Decl *getMostRecentDeclImpl(...);
- static Decl *getMostRecentDecl(Decl *D);
+ LazySpecializations = Result;
+ }
- static void mergeInheritableAttributes(ASTReader &Reader, Decl *D,
- Decl *Previous);
+ template <typename DeclT>
+ static Decl *getMostRecentDeclImpl(Redeclarable<DeclT> *D);
+ static Decl *getMostRecentDeclImpl(...);
+ static Decl *getMostRecentDecl(Decl *D);
- template <typename DeclT>
- static void attachPreviousDeclImpl(ASTReader &Reader,
- Redeclarable<DeclT> *D, Decl *Previous,
- Decl *Canon);
- static void attachPreviousDeclImpl(ASTReader &Reader, ...);
- static void attachPreviousDecl(ASTReader &Reader, Decl *D, Decl *Previous,
- Decl *Canon);
+ template <typename DeclT>
+ static void attachPreviousDeclImpl(ASTReader &Reader, Redeclar...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/103022
More information about the cfe-commits
mailing list