Use the ASTMutationListener to track added template specializations in a chained PCH.

llvm-svn: 117533
This commit is contained in:
Argyrios Kyrtzidis 2010-10-28 07:38:42 +00:00
parent 2efaf11695
commit 402dbbbd66
11 changed files with 46 additions and 86 deletions

View File

@ -14,9 +14,11 @@
#define LLVM_CLANG_AST_ASTMUTATIONLISTENER_H #define LLVM_CLANG_AST_ASTMUTATIONLISTENER_H
namespace clang { namespace clang {
class Decl;
class TagDecl; class TagDecl;
class CXXRecordDecl; class CXXRecordDecl;
class CXXMethodDecl; class ClassTemplateDecl;
class ClassTemplateSpecializationDecl;
/// \brief An abstract interface that should be implemented by listeners /// \brief An abstract interface that should be implemented by listeners
/// that want to be notified when an AST entity gets modified after its /// that want to be notified when an AST entity gets modified after its
@ -30,6 +32,11 @@ public:
/// \brief An implicit member was added after the definition was completed. /// \brief An implicit member was added after the definition was completed.
virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {} virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {}
/// \brief A template specialization (or partial one) was added to the
/// template declaration.
virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
const ClassTemplateSpecializationDecl *D) {}
}; };
} // end namespace clang } // end namespace clang

View File

@ -1697,9 +1697,7 @@ public:
/// \brief Insert the specified specialization knowing that it is not already /// \brief Insert the specified specialization knowing that it is not already
/// in. InsertPos must be obtained from findSpecialization. /// in. InsertPos must be obtained from findSpecialization.
void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos) { void AddSpecialization(ClassTemplateSpecializationDecl *D, void *InsertPos);
getSpecializations().InsertNode(D, InsertPos);
}
ClassTemplateDecl *getCanonicalDecl() { ClassTemplateDecl *getCanonicalDecl() {
return redeclarable_base::getCanonicalDecl(); return redeclarable_base::getCanonicalDecl();
@ -1733,9 +1731,7 @@ public:
/// \brief Insert the specified partial specialization knowing that it is not /// \brief Insert the specified partial specialization knowing that it is not
/// already in. InsertPos must be obtained from findPartialSpecialization. /// already in. InsertPos must be obtained from findPartialSpecialization.
void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D, void AddPartialSpecialization(ClassTemplatePartialSpecializationDecl *D,
void *InsertPos) { void *InsertPos);
getPartialSpecializations().InsertNode(D, InsertPos);
}
/// \brief Return the next partial specialization sequence number. /// \brief Return the next partial specialization sequence number.
unsigned getNextPartialSpecSequenceNumber() { unsigned getNextPartialSpecSequenceNumber() {

View File

@ -327,17 +327,13 @@ namespace clang {
/// In practice, this should only be used for the TU and namespaces. /// In practice, this should only be used for the TU and namespaces.
UPDATE_VISIBLE = 34, UPDATE_VISIBLE = 34,
/// \brief Record code for template specializations introduced after
/// serializations of the original template decl.
ADDITIONAL_TEMPLATE_SPECIALIZATIONS = 35,
/// \brief Record for offsets of DECL_UPDATES records for declarations /// \brief Record for offsets of DECL_UPDATES records for declarations
/// that were modified after being deserialized and need updates. /// that were modified after being deserialized and need updates.
DECL_UPDATE_OFFSETS = 36, DECL_UPDATE_OFFSETS = 35,
/// \brief Record of updates for a declaration that was modified after /// \brief Record of updates for a declaration that was modified after
/// being deserialized. /// being deserialized.
DECL_UPDATES = 37 DECL_UPDATES = 36
}; };
/// \brief Record types used within a source manager block. /// \brief Record types used within a source manager block.

View File

@ -458,16 +458,6 @@ private:
/// most recent declarations in another AST file. /// most recent declarations in another AST file.
FirstLatestDeclIDMap FirstLatestDeclIDs; FirstLatestDeclIDMap FirstLatestDeclIDs;
typedef llvm::SmallVector<serialization::DeclID, 4>
AdditionalTemplateSpecializations;
typedef llvm::DenseMap<serialization::DeclID,
AdditionalTemplateSpecializations>
AdditionalTemplateSpecializationsMap;
/// \brief Additional specializations (including partial) of templates that
/// were introduced after the template was serialized.
AdditionalTemplateSpecializationsMap AdditionalTemplateSpecializationsPending;
/// \brief Read the records that describe the contents of declcontexts. /// \brief Read the records that describe the contents of declcontexts.
bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor, bool ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
const std::pair<uint64_t, uint64_t> &Offsets, const std::pair<uint64_t, uint64_t> &Offsets,

View File

@ -236,16 +236,6 @@ private:
llvm::SmallVector<std::pair<serialization::DeclID, uint64_t>, 16> llvm::SmallVector<std::pair<serialization::DeclID, uint64_t>, 16>
ReplacedDecls; ReplacedDecls;
typedef llvm::SmallVector<serialization::DeclID, 4>
AdditionalTemplateSpecializationsList;
typedef llvm::DenseMap<serialization::DeclID,
AdditionalTemplateSpecializationsList>
AdditionalTemplateSpecializationsMap;
/// \brief Additional specializations (including partial) of templates that
/// were introduced after the template was serialized.
AdditionalTemplateSpecializationsMap AdditionalTemplateSpecializations;
/// \brief Statements that we've encountered while serializing a /// \brief Statements that we've encountered while serializing a
/// declaration or type. /// declaration or type.
llvm::SmallVector<Stmt *, 16> StmtsToEmit; llvm::SmallVector<Stmt *, 16> StmtsToEmit;
@ -296,7 +286,6 @@ private:
void WriteDeclUpdatesBlocks(); void WriteDeclUpdatesBlocks();
void WriteDeclReplacementsBlock(); void WriteDeclReplacementsBlock();
void WriteDeclContextVisibleUpdate(const DeclContext *DC); void WriteDeclContextVisibleUpdate(const DeclContext *DC);
void WriteAdditionalTemplateSpecializations();
unsigned ParmVarDeclAbbrev; unsigned ParmVarDeclAbbrev;
unsigned DeclContextLexicalAbbrev; unsigned DeclContextLexicalAbbrev;
@ -459,13 +448,6 @@ public:
UpdatedNamespaces.insert(NS); UpdatedNamespaces.insert(NS);
} }
/// \brief Record a template specialization or partial specialization of
/// a template from a previous PCH file.
void AddAdditionalTemplateSpecialization(serialization::DeclID Templ,
serialization::DeclID Spec) {
AdditionalTemplateSpecializations[Templ].push_back(Spec);
}
/// \brief Note that the identifier II occurs at the given offset /// \brief Note that the identifier II occurs at the given offset
/// within the identifier table. /// within the identifier table.
void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset); void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
@ -515,6 +497,8 @@ public:
// ASTMutationListener implementation. // ASTMutationListener implementation.
virtual void CompletedTagDefinition(const TagDecl *D); virtual void CompletedTagDefinition(const TagDecl *D);
virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D); virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
const ClassTemplateSpecializationDecl *D);
}; };
/// \brief AST and semantic-analysis consumer that generates a /// \brief AST and semantic-analysis consumer that generates a

View File

@ -16,6 +16,7 @@
#include "clang/AST/Expr.h" #include "clang/AST/Expr.h"
#include "clang/AST/ASTContext.h" #include "clang/AST/ASTContext.h"
#include "clang/AST/TypeLoc.h" #include "clang/AST/TypeLoc.h"
#include "clang/AST/ASTMutationListener.h"
#include "clang/Basic/IdentifierTable.h" #include "clang/Basic/IdentifierTable.h"
#include "llvm/ADT/STLExtras.h" #include "llvm/ADT/STLExtras.h"
using namespace clang; using namespace clang;
@ -225,6 +226,13 @@ ClassTemplateDecl::findSpecialization(const TemplateArgument *Args,
return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos); return findSpecializationImpl(getSpecializations(), Args, NumArgs, InsertPos);
} }
void ClassTemplateDecl::AddSpecialization(ClassTemplateSpecializationDecl *D,
void *InsertPos) {
getSpecializations().InsertNode(D, InsertPos);
if (ASTMutationListener *L = getASTMutationListener())
L->AddedCXXTemplateSpecialization(this, D);
}
ClassTemplatePartialSpecializationDecl * ClassTemplatePartialSpecializationDecl *
ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args, ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
unsigned NumArgs, unsigned NumArgs,
@ -233,6 +241,14 @@ ClassTemplateDecl::findPartialSpecialization(const TemplateArgument *Args,
InsertPos); InsertPos);
} }
void ClassTemplateDecl::AddPartialSpecialization(
ClassTemplatePartialSpecializationDecl *D,
void *InsertPos) {
getPartialSpecializations().InsertNode(D, InsertPos);
if (ASTMutationListener *L = getASTMutationListener())
L->AddedCXXTemplateSpecialization(this, D);
}
void ClassTemplateDecl::getPartialSpecializations( void ClassTemplateDecl::getPartialSpecializations(
llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) { llvm::SmallVectorImpl<ClassTemplatePartialSpecializationDecl *> &PS) {
llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> &PartialSpecs

View File

@ -22,7 +22,8 @@ namespace serialization {
enum DeclUpdateKind { enum DeclUpdateKind {
UPD_CXX_SET_DEFINITIONDATA, UPD_CXX_SET_DEFINITIONDATA,
UPD_CXX_ADDED_IMPLICIT_MEMBER UPD_CXX_ADDED_IMPLICIT_MEMBER,
UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION
}; };
TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT); TypeIdx TypeIdxFromBuiltin(const BuiltinType *BT);

View File

@ -2069,13 +2069,6 @@ ASTReader::ReadASTBlock(PerFileData &F) {
std::make_pair(&F, Record[I+1]); std::make_pair(&F, Record[I+1]);
break; break;
} }
case ADDITIONAL_TEMPLATE_SPECIALIZATIONS: {
AdditionalTemplateSpecializations &ATS =
AdditionalTemplateSpecializationsPending[Record[0]];
ATS.insert(ATS.end(), Record.begin()+1, Record.end());
break;
}
} }
First = false; First = false;
} }

View File

@ -1553,20 +1553,6 @@ Decl *ASTReader::ReadDeclRecord(unsigned Index, DeclID ID) {
} }
} }
} }
// If this is a template, read additional specializations that may be in a
// different part of the chain.
if (isa<RedeclarableTemplateDecl>(D)) {
AdditionalTemplateSpecializationsMap::iterator F =
AdditionalTemplateSpecializationsPending.find(ID);
if (F != AdditionalTemplateSpecializationsPending.end()) {
for (AdditionalTemplateSpecializations::iterator I = F->second.begin(),
E = F->second.end();
I != E; ++I)
GetDecl(*I);
AdditionalTemplateSpecializationsPending.erase(F);
}
}
assert(Idx == Record.size()); assert(Idx == Record.size());
// The declaration may have been modified by files later in the chain. // The declaration may have been modified by files later in the chain.
@ -1617,6 +1603,10 @@ void ASTDeclReader::UpdateDecl(Decl *D, const RecordData &Record) {
case UPD_CXX_ADDED_IMPLICIT_MEMBER: case UPD_CXX_ADDED_IMPLICIT_MEMBER:
cast<CXXRecordDecl>(D)->addedMember(Reader.GetDecl(Record[Idx++])); cast<CXXRecordDecl>(D)->addedMember(Reader.GetDecl(Record[Idx++]));
break; break;
case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
// It will be added to the template's specializations set when loaded.
Reader.GetDecl(Record[Idx++]);
} }
} }
} }

View File

@ -2181,21 +2181,6 @@ void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str()); Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable.str());
} }
/// \brief Write ADDITIONAL_TEMPLATE_SPECIALIZATIONS blocks for all templates
/// that have new specializations in the current AST file.
void ASTWriter::WriteAdditionalTemplateSpecializations() {
RecordData Record;
for (AdditionalTemplateSpecializationsMap::iterator
I = AdditionalTemplateSpecializations.begin(),
E = AdditionalTemplateSpecializations.end();
I != E; ++I) {
Record.clear();
Record.push_back(I->first);
Record.insert(Record.end(), I->second.begin(), I->second.end());
Stream.EmitRecord(ADDITIONAL_TEMPLATE_SPECIALIZATIONS, Record);
}
}
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// General Serialization Routines // General Serialization Routines
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -2697,10 +2682,6 @@ void ASTWriter::WriteASTChain(Sema &SemaRef, MemorizeStatCalls *StatCalls,
I != E; ++I) I != E; ++I)
WriteDeclContextVisibleUpdate(*I); WriteDeclContextVisibleUpdate(*I);
// Write the updates to C++ template specialization lists.
if (!AdditionalTemplateSpecializations.empty())
WriteAdditionalTemplateSpecializations();
WriteDeclUpdatesBlocks(); WriteDeclUpdatesBlocks();
Record.clear(); Record.clear();
@ -3340,3 +3321,13 @@ void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER); Record.push_back(UPD_CXX_ADDED_IMPLICIT_MEMBER);
AddDeclRef(D, Record); AddDeclRef(D, Record);
} }
void ASTWriter::AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
const ClassTemplateSpecializationDecl *D) {
if (!(D->getPCHLevel() == 0 && TD->getPCHLevel() > 0))
return; // Not a source specialization added to a template from PCH.
UpdateRecord &Record = DeclUpdates[TD];
Record.push_back(UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION);
AddDeclRef(D, Record);
}

View File

@ -905,10 +905,6 @@ void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
InstFromD = cast<ClassTemplatePartialSpecializationDecl>(InstFromD)-> InstFromD = cast<ClassTemplatePartialSpecializationDecl>(InstFromD)->
getSpecializedTemplate(); getSpecializedTemplate();
} }
// Is this a specialization of an already-serialized template?
if (InstFromD->getCanonicalDecl()->getPCHLevel() != 0)
Writer.AddAdditionalTemplateSpecialization(Writer.getDeclID(InstFromD),
Writer.getDeclID(D));
// Explicit info. // Explicit info.
Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record); Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);