Generally types expect an initialized TypeDecl; its safer and less complicated to delay PCH reading the type of a TypeDecl.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@107469 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/PCHReaderDecl.cpp b/lib/Frontend/PCHReaderDecl.cpp
index 2bdf166..443842d 100644
--- a/lib/Frontend/PCHReaderDecl.cpp
+++ b/lib/Frontend/PCHReaderDecl.cpp
@@ -32,14 +32,17 @@
PCHReader &Reader;
const PCHReader::RecordData &Record;
unsigned &Idx;
+ pch::TypeID TypeIDForTypeDecl;
public:
PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
unsigned &Idx)
- : Reader(Reader), Record(Record), Idx(Idx) { }
+ : Reader(Reader), Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { }
CXXBaseSpecifier *ReadCXXBaseSpecifier();
+ void Visit(Decl *D);
+
void VisitDecl(Decl *D);
void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
void VisitNamedDecl(NamedDecl *ND);
@@ -107,6 +110,14 @@
};
}
+void PCHDeclReader::Visit(Decl *D) {
+ DeclVisitor<PCHDeclReader, void>::Visit(D);
+
+ // if we have a fully initialized TypeDecl, we can safely read its type now.
+ if (TypeDecl *TD = dyn_cast<TypeDecl>(D))
+ TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr());
+}
+
void PCHDeclReader::VisitDecl(Decl *D) {
D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
D->setLexicalDeclContext(
@@ -134,17 +145,13 @@
void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
VisitNamedDecl(TD);
- TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
+ // Delay type reading until after we have fully initialized the decl.
+ TypeIDForTypeDecl = Record[Idx++];
}
void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
- // Note that we cannot use VisitTypeDecl here, because we need to
- // set the underlying type of the typedef *before* we try to read
- // the type associated with the TypedefDecl.
- VisitNamedDecl(TD);
- uint64_t TypeData = Record[Idx++];
+ VisitTypeDecl(TD);
TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx));
- TD->setTypeForDecl(Reader.GetType(TypeData).getTypePtr());
}
void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
diff --git a/lib/Frontend/PCHWriterDecl.cpp b/lib/Frontend/PCHWriterDecl.cpp
index 29bbae2..000c2d8 100644
--- a/lib/Frontend/PCHWriterDecl.cpp
+++ b/lib/Frontend/PCHWriterDecl.cpp
@@ -137,14 +137,7 @@
void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
VisitNamedDecl(D);
- if (isa<CXXRecordDecl>(D)) {
- // FIXME: Hack. To read a templated CXXRecordDecl from PCH, we need an
- // initialized CXXRecordDecl before creating an InjectedClassNameType.
- // Delay emitting/reading CXXRecordDecl's TypeForDecl until when we handle
- // CXXRecordDecl emitting/initialization.
- Writer.AddTypeRef(QualType(), Record);
- } else
- Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
+ Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
}
void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {