diff options
| author | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-04-19 03:48:30 +0000 |
|---|---|---|
| committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2014-04-19 03:48:30 +0000 |
| commit | f4ff7ca63fe65f490a62ac7a0a9d92bca33bb4de (patch) | |
| tree | a8df418dd7d7cfc2299a3424ca6c35e6ef04d7b0 /lib/Serialization/ASTWriter.cpp | |
| parent | 0d60af37712b2a5f81565b43545c19dea1dab93f (diff) | |
| download | clang-f4ff7ca63fe65f490a62ac7a0a9d92bca33bb4de.tar.gz clang-f4ff7ca63fe65f490a62ac7a0a9d92bca33bb4de.tar.bz2 clang-f4ff7ca63fe65f490a62ac7a0a9d92bca33bb4de.tar.xz | |
When a module completes the definition of a class template specialization imported from another module, emit an update record, rather than using the broken decl rewriting mechanism. If multiple modules do this, merge the definitions together, much as we would if they were separate declarations.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206680 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Serialization/ASTWriter.cpp')
| -rw-r--r-- | lib/Serialization/ASTWriter.cpp | 145 |
1 files changed, 107 insertions, 38 deletions
diff --git a/lib/Serialization/ASTWriter.cpp b/lib/Serialization/ASTWriter.cpp index d770f8df26..7402961ae8 100644 --- a/lib/Serialization/ASTWriter.cpp +++ b/lib/Serialization/ASTWriter.cpp @@ -3471,11 +3471,54 @@ public: }; } // end anonymous namespace +template<typename Visitor> +static void visitLocalLookupResults(const DeclContext *ConstDC, + bool NeedToReconcileExternalVisibleStorage, + Visitor AddLookupResult) { + // FIXME: We need to build the lookups table, which is logically const. + DeclContext *DC = const_cast<DeclContext*>(ConstDC); + assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table"); + + SmallVector<DeclarationName, 16> ExternalNames; + for (auto &Lookup : *DC->buildLookup()) { + if (Lookup.second.hasExternalDecls() || + NeedToReconcileExternalVisibleStorage) { + // We don't know for sure what declarations are found by this name, + // because the external source might have a different set from the set + // that are in the lookup map, and we can't update it now without + // risking invalidating our lookup iterator. So add it to a queue to + // deal with later. + ExternalNames.push_back(Lookup.first); + continue; + } + + AddLookupResult(Lookup.first, Lookup.second.getLookupResult()); + } + + // Add the names we needed to defer. Note, this shouldn't add any new decls + // to the list we need to serialize: any new declarations we find here should + // be imported from an external source. + // FIXME: What if the external source isn't an ASTReader? + for (const auto &Name : ExternalNames) + AddLookupResult(Name, DC->lookup(Name)); +} + +void ASTWriter::AddUpdatedDeclContext(const DeclContext *DC) { + if (UpdatedDeclContexts.insert(DC) && WritingAST) { + // Ensure we emit all the visible declarations. + visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage, + [&](DeclarationName Name, + DeclContext::lookup_const_result Result) { + for (auto *Decl : Result) + GetDeclRef(Decl); + }); + } +} + uint32_t ASTWriter::GenerateNameLookupTable(const DeclContext *DC, llvm::SmallVectorImpl<char> &LookupTable) { assert(!DC->LookupPtr.getInt() && "must call buildLookups first"); - assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table"); llvm::OnDiskChainedHashTableGenerator<ASTDeclContextNameLookupTrait> Generator; @@ -3487,8 +3530,9 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *DC, SmallVector<NamedDecl *, 8> ConstructorDecls; SmallVector<NamedDecl *, 4> ConversionDecls; - auto AddLookupResult = [&](DeclarationName Name, - DeclContext::lookup_result Result) { + visitLocalLookupResults(DC, DC->NeedToReconcileExternalVisibleStorage, + [&](DeclarationName Name, + DeclContext::lookup_result Result) { if (Result.empty()) return; @@ -3504,41 +3548,19 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *DC, ConstructorName = Name; ConstructorDecls.append(Result.begin(), Result.end()); return; + case DeclarationName::CXXConversionFunctionName: if (!ConversionName) ConversionName = Name; ConversionDecls.append(Result.begin(), Result.end()); return; + default: break; } Generator.insert(Name, Result, Trait); - }; - - SmallVector<DeclarationName, 16> ExternalNames; - for (auto &Lookup : *DC->getLookupPtr()) { - if (Lookup.second.hasExternalDecls() || - DC->NeedToReconcileExternalVisibleStorage) { - // We don't know for sure what declarations are found by this name, - // because the external source might have a different set from the set - // that are in the lookup map, and we can't update it now without - // risking invalidating our lookup iterator. So add it to a queue to - // deal with later. - ExternalNames.push_back(Lookup.first); - continue; - } - - AddLookupResult(Lookup.first, Lookup.second.getLookupResult()); - } - - // Add the names we needed to defer. Note, this shouldn't add any new decls - // to the list we need to serialize: any new declarations we find here should - // be imported from an external source. - // FIXME: What if the external source isn't an ASTReader? - for (const auto &Name : ExternalNames) - // FIXME: const_cast since OnDiskHashTable wants a non-const lookup result. - AddLookupResult(Name, const_cast<DeclContext*>(DC)->lookup(Name)); + }); // Add the constructors. if (!ConstructorDecls.empty()) { @@ -3547,6 +3569,7 @@ ASTWriter::GenerateNameLookupTable(const DeclContext *DC, ConstructorDecls.end()), Trait); } + // Add the conversion functions. if (!ConversionDecls.empty()) { Generator.insert(ConversionName, @@ -3792,7 +3815,7 @@ void ASTWriter::WriteMergedDecls() { IEnd = Chain->MergedDecls.end(); I != IEnd; ++I) { DeclID CanonID = I->first->isFromASTFile()? I->first->getGlobalID() - : getDeclID(I->first); + : GetDeclRef(I->first); assert(CanonID && "Merged declaration not known?"); Record.push_back(CanonID); @@ -4029,6 +4052,15 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, } } + // If we saw any DeclContext updates before we started writing the AST file, + // make sure all visible decls in those DeclContexts are written out. + if (!UpdatedDeclContexts.empty()) { + auto OldUpdatedDeclContexts = std::move(UpdatedDeclContexts); + UpdatedDeclContexts.clear(); + for (auto *DC : OldUpdatedDeclContexts) + AddUpdatedDeclContext(DC); + } + // Build a record containing all of the tentative definitions in this file, in // TentativeDefinitions order. Generally, this record will be empty for // headers. @@ -4388,11 +4420,8 @@ void ASTWriter::WriteASTCore(Sema &SemaRef, Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed); // Write the visible updates to DeclContexts. - for (llvm::SmallPtrSet<const DeclContext *, 16>::iterator - I = UpdatedDeclContexts.begin(), - E = UpdatedDeclContexts.end(); - I != E; ++I) - WriteDeclContextVisibleUpdate(*I); + for (auto *DC : UpdatedDeclContexts) + WriteDeclContextVisibleUpdate(DC); if (!WritingModule) { // Write the submodules that were imported, if any. @@ -4459,9 +4488,6 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { if (isRewritten(D)) continue; // The decl will be written completely,no need to store updates. - OffsetsRecord.push_back(GetDeclRef(D)); - OffsetsRecord.push_back(Stream.GetCurrentBitNo()); - bool HasUpdatedBody = false; RecordData Record; for (auto &Update : DeclUpdate.second) { @@ -4472,6 +4498,7 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { case UPD_CXX_ADDED_IMPLICIT_MEMBER: case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION: case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: + assert(Update.getDecl() && "no decl to add?"); Record.push_back(GetDeclRef(Update.getDecl())); break; @@ -4486,6 +4513,39 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { HasUpdatedBody = true; break; + case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: { + auto *RD = cast<CXXRecordDecl>(D); + AddUpdatedDeclContext(RD->getPrimaryContext()); + AddCXXDefinitionData(RD, Record); + Record.push_back(WriteDeclContextLexicalBlock( + *Context, const_cast<CXXRecordDecl *>(RD))); + + // This state is sometimes updated by template instantiation, when we + // switch from the specialization referring to the template declaration + // to it referring to the template definition. + if (auto *MSInfo = RD->getMemberSpecializationInfo()) { + Record.push_back(MSInfo->getTemplateSpecializationKind()); + AddSourceLocation(MSInfo->getPointOfInstantiation(), Record); + } else { + auto *Spec = cast<ClassTemplateSpecializationDecl>(RD); + Record.push_back(Spec->getTemplateSpecializationKind()); + AddSourceLocation(Spec->getPointOfInstantiation(), Record); + } + Record.push_back(RD->getTagKind()); + AddSourceLocation(RD->getLocation(), Record); + AddSourceLocation(RD->getLocStart(), Record); + AddSourceLocation(RD->getRBraceLoc(), Record); + + // Instantiation may change attributes; write them all out afresh. + Record.push_back(D->hasAttrs()); + if (Record.back()) + WriteAttributes(ArrayRef<const Attr*>(D->getAttrs().begin(), + D->getAttrs().size()), Record); + + // FIXME: Ensure we don't get here for explicit instantiations. + break; + } + case UPD_CXX_RESOLVED_EXCEPTION_SPEC: addExceptionSpec( *this, @@ -4515,10 +4575,16 @@ void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) { AddFunctionDefinition(Def, Record); } + OffsetsRecord.push_back(GetDeclRef(D)); + OffsetsRecord.push_back(Stream.GetCurrentBitNo()); + Stream.EmitRecord(DECL_UPDATES, Record); // Flush any statements that were written as part of this update record. FlushStmts(); + + // Flush C++ base specifiers, if there are any. + FlushCXXBaseSpecifiers(); } } @@ -5405,7 +5471,10 @@ void ASTWriter::CompletedTagDefinition(const TagDecl *D) { // 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 ? - RewriteDecl(RD); + assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) && + "completed a tag from another module but not by instantiation?"); + DeclUpdates[RD].push_back( + DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION)); } } } |
