Start fleshing out ASTMutationListener; notify when a tag definition is completed.
In that case a chained PCH will record the updates to the DefinitionData pointer of forward references.
If a forward reference mutated into a definition re-write it into the chained PCH, this is too big of a change.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@117239 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp
index fc5b57f..42e762d 100644
--- a/lib/AST/Decl.cpp
+++ b/lib/AST/Decl.cpp
@@ -21,6 +21,7 @@
#include "clang/AST/Expr.h"
#include "clang/AST/ExprCXX.h"
#include "clang/AST/PrettyPrinter.h"
+#include "clang/AST/ASTMutationListener.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/IdentifierTable.h"
#include "clang/Basic/Specifiers.h"
@@ -1713,6 +1714,9 @@
IsDefinition = true;
IsBeingDefined = false;
+
+ if (ASTMutationListener *L = getASTMutationListener())
+ L->CompletedTagDefinition(this);
}
TagDecl* TagDecl::getDefinition() const {
diff --git a/lib/Serialization/ASTCommon.h b/lib/Serialization/ASTCommon.h
index a0e2ecd..36958c7 100644
--- a/lib/Serialization/ASTCommon.h
+++ b/lib/Serialization/ASTCommon.h
@@ -20,6 +20,10 @@
namespace serialization {
+enum DeclUpdateKind {
+ UPD_CXX_SET_DEFINITIONDATA
+};
+
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);
template <typename IdxForTypeTy>
diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp
index 45d729a..f4568a7 100644
--- a/lib/Serialization/ASTReaderDecl.cpp
+++ b/lib/Serialization/ASTReaderDecl.cpp
@@ -1588,5 +1588,17 @@
}
void ASTDeclReader::UpdateDecl(Decl *D, const RecordData &Record) {
- // No update is tracked yet.
+ unsigned Idx = 0;
+ while (Idx < Record.size()) {
+ switch ((DeclUpdateKind)Record[Idx++]) {
+ case UPD_CXX_SET_DEFINITIONDATA: {
+ CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
+ CXXRecordDecl *
+ DefinitionDecl = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]));
+ assert(!RD->DefinitionData && "DefinitionData is already set!");
+ InitializeCXXDefinitionData(RD, DefinitionDecl, Record, Idx);
+ break;
+ }
+ }
+ }
}
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp
index c6df2dd..436525f 100644
--- a/lib/Serialization/ASTWriter.cpp
+++ b/lib/Serialization/ASTWriter.cpp
@@ -3297,3 +3297,32 @@
MacroDefinition *MD) {
MacroDefinitions[MD] = ID;
}
+
+void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
+ assert(D->isDefinition());
+ if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(D)) {
+ // We are interested when a PCH decl is modified.
+ if (RD->getPCHLevel() > 0) {
+ // A forward reference was mutated into a definition. Rewrite it.
+ // FIXME: This happens during template instantiation, should we
+ // have created a new definition decl instead ?
+ DeclsToRewrite.insert(RD);
+ }
+
+ for (CXXRecordDecl::redecl_iterator
+ I = RD->redecls_begin(), E = RD->redecls_end(); I != E; ++I) {
+ CXXRecordDecl *Redecl = cast<CXXRecordDecl>(*I);
+ if (Redecl == RD)
+ continue;
+
+ // We are interested when a PCH decl is modified.
+ if (Redecl->getPCHLevel() > 0) {
+ UpdateRecord &Record = DeclUpdates[Redecl];
+ Record.push_back(UPD_CXX_SET_DEFINITIONDATA);
+ assert(Redecl->DefinitionData);
+ assert(Redecl->DefinitionData->Definition == D);
+ AddDeclRef(D, Record); // the DefinitionDecl
+ }
+ }
+ }
+}