diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h index e8f4860e13f1f8..5f7c362c6701af 100644 --- a/clang/include/clang/AST/DeclID.h +++ b/clang/include/clang/AST/DeclID.h @@ -197,6 +197,8 @@ class LocalDeclID : public DeclIDBase { static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF, DeclID ID); + static LocalDeclID get(ASTReader &Reader, serialization::ModuleFile &MF, + unsigned ModuleFileIndex, unsigned LocalDeclID); LocalDeclID &operator++() { ++ID; diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h index 9f0f900a029149..8d789683b31641 100644 --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -261,6 +261,9 @@ using unaligned_decl_id_t = serialization::DeclID, llvm::endianness::native, llvm::support::unaligned>; +/// The number of slots needed to record a DeclID in bitstreams. +const unsigned int DeclIDRefSize = 2; + /// The number of predefined preprocessed entity IDs. const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1; diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index 480f852e3bf07f..980c43c7db2cbc 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -600,7 +600,7 @@ class ASTReader /// An array of lexical contents of a declaration context, as a sequence of /// Decl::Kind, DeclID pairs. - using LexicalContents = ArrayRef; + using LexicalContents = ArrayRef; /// Map from a DeclContext to its lexical contents. llvm::DenseMap> @@ -961,8 +961,7 @@ class ASTReader SmallVector DelayedDeleteExprs; // A list of late parsed template function data with their module files. - SmallVector>, 4> - LateParsedTemplates; + SmallVector, 4> LateParsedTemplates; /// The IDs of all decls to be checked for deferred diags. /// diff --git a/clang/include/clang/Serialization/ASTRecordReader.h b/clang/include/clang/Serialization/ASTRecordReader.h index d00fb182f05f4e..003bb592d188b5 100644 --- a/clang/include/clang/Serialization/ASTRecordReader.h +++ b/clang/include/clang/Serialization/ASTRecordReader.h @@ -187,12 +187,26 @@ class ASTRecordReader /// Reads a declaration from the given position in a record in the /// given module, advancing Idx. Decl *readDecl() { +#ifndef NDEBUG + unsigned OldIdx = Idx; + Decl *D = Reader->ReadDecl(*F, Record, Idx); + assert(Idx - OldIdx == serialization::DeclIDRefSize); + return D; +#endif return Reader->ReadDecl(*F, Record, Idx); } Decl *readDeclRef() { return readDecl(); } + template void readDeclArray(Func &&ConsumeFunc) { + unsigned LengthOfArray = readInt(); + unsigned End = Idx + LengthOfArray; + + while (Idx < End) + ConsumeFunc(readDeclAs()); + } + /// Reads a declaration from the given position in the record, /// advancing Idx. /// diff --git a/clang/include/clang/Serialization/ASTRecordWriter.h b/clang/include/clang/Serialization/ASTRecordWriter.h index 0c8ac75fc40f40..ed96fbc310096b 100644 --- a/clang/include/clang/Serialization/ASTRecordWriter.h +++ b/clang/include/clang/Serialization/ASTRecordWriter.h @@ -234,12 +234,39 @@ class ASTRecordWriter /// Emit a reference to a declaration. void AddDeclRef(const Decl *D) { +#ifndef NDEBUG + unsigned OldSize = size(); + Writer->AddDeclRef(D, *Record); + assert(size() - OldSize == serialization::DeclIDRefSize); + return; +#endif return Writer->AddDeclRef(D, *Record); } void writeDeclRef(const Decl *D) { AddDeclRef(D); } + void writeNullDeclRef() { +#ifndef NDEBUG + unsigned OldSize = size(); +#endif + + push_back(0); + push_back(0); + +#ifndef NDEBUG + assert(size() - OldSize == serialization::DeclIDRefSize); +#endif + } + + template void writeDeclArray(ArrayRef Array) { + unsigned ElementNum = Array.size(); + push_back(ElementNum * serialization::DeclIDRefSize); + + for (DeclKind *D : Array) + AddDeclRef(D); + } + /// Emit a declaration name. void AddDeclarationName(DeclarationName Name) { writeDeclarationName(Name); diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index 973475cf56b8cd..853e87a3a13eae 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -935,9 +935,8 @@ LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, DeclID Value) { return ID; } -static LocalDeclID getLocalDeclID(ASTReader &Reader, ModuleFile &MF, - unsigned ModuleFileIndex, - unsigned LocalDeclID) { +LocalDeclID LocalDeclID::get(ASTReader &Reader, ModuleFile &MF, + unsigned ModuleFileIndex, unsigned LocalDeclID) { DeclID Value = (DeclID)ModuleFileIndex << 32 | (DeclID)LocalDeclID; return LocalDeclID::get(Reader, MF, Value); } @@ -1303,9 +1302,8 @@ bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, auto &Lex = LexicalDecls[DC]; if (!Lex.first) { Lex = std::make_pair( - &M, llvm::ArrayRef( - reinterpret_cast(Blob.data()), - Blob.size() / sizeof(DeclID))); + &M, llvm::ArrayRef(reinterpret_cast(Blob.data()), + Blob.size() / sizeof(uint32_t))); } DC->setHasExternalLexicalStorage(true); return false; @@ -3426,8 +3424,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, case TU_UPDATE_LEXICAL: { DeclContext *TU = ContextObj->getTranslationUnitDecl(); LexicalContents Contents( - reinterpret_cast(Blob.data()), - static_cast(Blob.size() / sizeof(DeclID))); + reinterpret_cast(Blob.data()), + static_cast(Blob.size() / sizeof(uint32_t))); TULexicalDecls.push_back(std::make_pair(&F, Contents)); TU->setHasExternalLexicalStorage(true); break; @@ -3713,16 +3711,16 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case VTABLE_USES: - if (Record.size() % 3 != 0) - return llvm::createStringError(std::errc::illegal_byte_sequence, - "Invalid VTABLE_USES record"); - // Later tables overwrite earlier ones. // FIXME: Modules will have some trouble with this. This is clearly not // the right way to do this. VTableUses.clear(); for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { + if (Idx > N) + return llvm::createStringError(std::errc::illegal_byte_sequence, + "Invalid VTABLE_USES record"); + VTableUses.push_back( {ReadDeclID(F, Record, Idx), ReadSourceLocation(F, Record, Idx).getRawEncoding(), @@ -3731,13 +3729,12 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case PENDING_IMPLICIT_INSTANTIATIONS: - - if (Record.size() % 2 != 0) - return llvm::createStringError( - std::errc::illegal_byte_sequence, - "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); - for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { + if (I > N) + return llvm::createStringError( + std::errc::illegal_byte_sequence, + "Invalid PENDING_IMPLICIT_INSTANTIATIONS block"); + PendingInstantiations.push_back( {ReadDeclID(F, Record, I), ReadSourceLocation(F, Record, I).getRawEncoding()}); @@ -3745,7 +3742,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case SEMA_DECL_REFS: - if (Record.size() != 3) + if (Record.size() != 3 * serialization::DeclIDRefSize) return llvm::createStringError(std::errc::illegal_byte_sequence, "Invalid SEMA_DECL_REFS block"); for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) @@ -3803,11 +3800,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, } case DECL_UPDATE_OFFSETS: - if (Record.size() % 2 != 0) - return llvm::createStringError( - std::errc::illegal_byte_sequence, - "invalid DECL_UPDATE_OFFSETS block in AST file"); for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) { + if (I > N) + return llvm::createStringError( + std::errc::illegal_byte_sequence, + "invalid DECL_UPDATE_OFFSETS block in AST file"); GlobalDeclID ID = ReadDeclID(F, Record, I); DeclUpdateOffsets[ID].push_back(std::make_pair(&F, Record[I++])); @@ -3820,12 +3817,12 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: { - if (Record.size() % 3 != 0) - return llvm::createStringError( - std::errc::illegal_byte_sequence, - "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST " - "file"); for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/) { + if (I > N) + return llvm::createStringError( + std::errc::illegal_byte_sequence, + "invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST " + "file"); GlobalDeclID ID = ReadDeclID(F, Record, I); uint64_t BaseOffset = F.DeclsBlockStartOffset; @@ -3915,10 +3912,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case UNDEFINED_BUT_USED: - if (Record.size() % 2 != 0) - return llvm::createStringError(std::errc::illegal_byte_sequence, - "invalid undefined-but-used record"); for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { + if (I > N) + return llvm::createStringError(std::errc::illegal_byte_sequence, + "invalid undefined-but-used record"); + UndefinedButUsed.push_back( {ReadDeclID(F, Record, I), ReadSourceLocation(F, Record, I).getRawEncoding()}); @@ -7880,7 +7878,7 @@ LocalDeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, if (!OrignalModuleFileIndex) return LocalDeclID(); - return getLocalDeclID(*this, M, OrignalModuleFileIndex, ID); + return LocalDeclID::get(*this, M, OrignalModuleFileIndex, ID); } GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record, @@ -7890,7 +7888,10 @@ GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record, return GlobalDeclID(0); } - return getGlobalDeclID(F, LocalDeclID::get(*this, F, Record[Idx++])); + uint32_t ModuleFileIndex = Record[Idx++]; + uint32_t LocalDeclIndex = Record[Idx++]; + return getGlobalDeclID( + F, LocalDeclID::get(*this, F, ModuleFileIndex, LocalDeclIndex)); } /// Resolve the offset of a statement into a statement. @@ -7919,25 +7920,26 @@ void ASTReader::FindExternalLexicalDecls( SmallVectorImpl &Decls) { bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {}; - auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) { - assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries"); - for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) { + auto Visit = [&](ModuleFile *M, LexicalContents LexicalDecls) { + assert(LexicalDecls.size() % 3 == 0 && "incorrect number of entries"); + for (int I = 0, N = LexicalDecls.size(); I != N; I += 3) { auto K = (Decl::Kind)+LexicalDecls[I]; if (!IsKindWeWant(K)) continue; - auto ID = (DeclID) + LexicalDecls[I + 1]; + LocalDeclID ID = + LocalDeclID::get(*this, *M, LexicalDecls[I + 1], LexicalDecls[I + 2]); // Don't add predefined declarations to the lexical context more // than once. if (ID < NUM_PREDEF_DECL_IDS) { - if (PredefsVisited[ID]) + if (PredefsVisited[ID.getRawValue()]) continue; - PredefsVisited[ID] = true; + PredefsVisited[ID.getRawValue()] = true; } - if (Decl *D = GetLocalDecl(*M, LocalDeclID::get(*this, *M, ID))) { + if (Decl *D = GetLocalDecl(*M, ID)) { assert(D->getKind() == K && "wrong kind for lexical decl"); if (!DC->isDeclInLexicalTraversal(D)) Decls.push_back(D); @@ -8836,7 +8838,7 @@ void ASTReader::ReadLateParsedTemplates( &LPTMap) { for (auto &LPT : LateParsedTemplates) { ModuleFile *FMod = LPT.first; - RecordDataImpl &LateParsed = LPT.second; + const RecordData &LateParsed = LPT.second; for (unsigned Idx = 0, N = LateParsed.size(); Idx < N; /* In loop */) { FunctionDecl *FD = ReadDeclAs(*FMod, LateParsed, Idx); diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 4b8b515c02c70d..c1f58d92948bf7 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -1020,9 +1020,8 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) { case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { // Templates. UnresolvedSet<8> Candidates; - unsigned NumCandidates = Record.readInt(); - while (NumCandidates--) - Candidates.addDecl(readDeclAs()); + Record.readDeclArray( + [&Candidates](NamedDecl *ND) { Candidates.addDecl(ND); }); // Templates args. TemplateArgumentListInfo TemplArgsWritten; @@ -1152,11 +1151,9 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) { FD->setIsPureVirtual(Pure); // Read in the parameters. - unsigned NumParams = Record.readInt(); SmallVector Params; - Params.reserve(NumParams); - for (unsigned I = 0; I != NumParams; ++I) - Params.push_back(readDeclAs()); + Record.readDeclArray( + [&Params](ParmVarDecl *ParmD) { Params.push_back(ParmD); }); FD->setParams(Reader.getContext(), Params); } @@ -2309,7 +2306,7 @@ void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) { } else { // We don't care about which declarations this used to override; we get // the relevant information from the canonical declaration. - Record.skipInts(NumOverridenMethods); + Record.skipInts(NumOverridenMethods * serialization::DeclIDRefSize); } } @@ -4354,8 +4351,9 @@ void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) { // FIXME: We have several different dispatches on decl kind here; maybe // we should instead generate one loop per kind and dispatch up-front? Decl *MostRecent = FirstLocal; - for (unsigned I = 0, N = Record.size(); I != N; ++I) { - unsigned Idx = N - I - 1; + for (unsigned I = 0, N = Record.size(); I != N; + I += serialization::DeclIDRefSize) { + unsigned Idx = N - I - serialization::DeclIDRefSize; auto *D = ReadDecl(*M, Record, Idx); ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl); MostRecent = D; diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index 67ef170251914e..7ec74a39ad55fc 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -351,12 +351,14 @@ void ASTStmtReader::VisitDeclStmt(DeclStmt *S) { S->setStartLoc(readSourceLocation()); S->setEndLoc(readSourceLocation()); - if (Record.size() - Record.getIdx() == 1) { + unsigned NumDecls = + (Record.size() - Record.getIdx()) / serialization::DeclIDRefSize; + if (NumDecls == 1) { // Single declaration S->setDeclGroup(DeclGroupRef(readDecl())); } else { SmallVector Decls; - int N = Record.size() - Record.getIdx(); + int N = NumDecls; Decls.reserve(N); for (int I = 0; I < N; ++I) Decls.push_back(readDecl()); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 50fa44d34f524c..ca416acf5daee1 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -3320,7 +3320,7 @@ uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, return 0; uint64_t Offset = Stream.GetCurrentBitNo(); - SmallVector KindDeclPairs; + SmallVector KindDeclPairs; for (const auto *D : DC->decls()) { if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D)) continue; @@ -3335,7 +3335,9 @@ uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context, continue; KindDeclPairs.push_back(D->getKind()); - KindDeclPairs.push_back(GetDeclRef(D).getRawValue()); + LocalDeclID ID = GetDeclRef(D); + KindDeclPairs.push_back(ID.getModuleFileIndex()); + KindDeclPairs.push_back(ID.getLocalDeclIndex()); } ++NumLexicalDeclContexts; @@ -4443,8 +4445,9 @@ void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) { DC = cast(Chain->getKeyDeclaration(cast(DC))); // Write the lookup table - RecordData::value_type Record[] = {UPDATE_VISIBLE, - getDeclID(cast(DC)).getRawValue()}; + LocalDeclID ID = getDeclID(cast(DC)); + RecordData::value_type Record[] = {UPDATE_VISIBLE, ID.getModuleFileIndex(), + ID.getLocalDeclIndex()}; Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable); } @@ -5227,9 +5230,10 @@ void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) { RecordData SemaDeclRefs; if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) { auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) { - if (!D || !wasDeclEmitted(D)) + if (!D || !wasDeclEmitted(D)) { SemaDeclRefs.push_back(0); - else + SemaDeclRefs.push_back(0); + } else AddDeclRef(D, SemaDeclRefs); }; @@ -5647,7 +5651,7 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) { const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); // Create a lexical update block containing all of the declarations in the // translation unit that do not come from other AST files. - SmallVector NewGlobalKindDeclPairs; + SmallVector NewGlobalKindDeclPairs; for (const auto *D : TU->noload_decls()) { if (D->isFromASTFile()) continue; @@ -5657,7 +5661,9 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) { continue; NewGlobalKindDeclPairs.push_back(D->getKind()); - NewGlobalKindDeclPairs.push_back(GetDeclRef(D).getRawValue()); + LocalDeclID ID = GetDeclRef(D); + NewGlobalKindDeclPairs.push_back(ID.getModuleFileIndex()); + NewGlobalKindDeclPairs.push_back(ID.getLocalDeclIndex()); } auto Abv = std::make_shared(); @@ -5672,6 +5678,7 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) { Abv = std::make_shared(); Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE)); Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); + Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6)); Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob)); UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv)); @@ -6163,7 +6170,10 @@ void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) { } void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) { - Record.push_back(GetDeclRef(D).getRawValue()); + LocalDeclID ID = GetDeclRef(D); + + Record.push_back(ID.getModuleFileIndex()); + Record.push_back(ID.getLocalDeclIndex()); } LocalDeclID ASTWriter::GetDeclRef(const Decl *D) { diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp index 59d94c3d79824b..90cb01e5381197 100644 --- a/clang/lib/Serialization/ASTWriterDecl.cpp +++ b/clang/lib/Serialization/ASTWriterDecl.cpp @@ -243,12 +243,14 @@ namespace clang { assert(D->isCanonicalDecl() && "non-canonical decl in set"); AddFirstDeclFromEachModule(D, /*IncludeLocal*/true); } - Record.append( - DeclIDIterator(LazySpecializations.begin()), - DeclIDIterator(LazySpecializations.end())); + + for (GlobalDeclID LazyID : LazySpecializations) { + Record.push_back(LazyID.getModuleFileIndex()); + Record.push_back(LazyID.getLocalDeclIndex()); + } // Update the size entry we added earlier. - Record[I] = Record.size() - I - 1; + Record[I] = (Record.size() - I - 1) / serialization::DeclIDRefSize; } /// Ensure that this template specialization is associated with the specified @@ -695,9 +697,7 @@ void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) { DFTSInfo = D->getDependentSpecializationInfo(); // Candidates. - Record.push_back(DFTSInfo->getCandidates().size()); - for (FunctionTemplateDecl *FTD : DFTSInfo->getCandidates()) - Record.AddDeclRef(FTD); + Record.writeDeclArray(DFTSInfo->getCandidates()); // Templates args. Record.push_back(DFTSInfo->TemplateArgumentsAsWritten != nullptr); @@ -769,9 +769,7 @@ void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) { } } - Record.push_back(D->param_size()); - for (auto *P : D->parameters()) - Record.AddDeclRef(P); + Record.writeDeclArray(D->parameters()); Code = serialization::DECL_FUNCTION; } @@ -1527,7 +1525,7 @@ void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) { Record.AddDeclRef(Context); Record.push_back(D->getLambdaIndexInContext()); } else { - Record.push_back(0); + Record.writeNullDeclRef(); } } else { Record.push_back(CXXRecNotTemplate); @@ -1567,7 +1565,7 @@ void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) { !D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() && D->getDeclName().getNameKind() == DeclarationName::Identifier && !shouldSkipCheckingODR(D) && !D->hasExtInfo() && - !D->isExplicitlyDefaulted()) { + !D->isExplicitlyDefaulted() && !D->size_overridden_methods()) { if (D->getTemplatedKind() == FunctionDecl::TK_NonTemplate || D->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate || D->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization || @@ -2060,7 +2058,7 @@ void ASTDeclWriter::VisitRedeclarable(Redeclarable *D) { if (Writer.Chain) AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false); // This is the number of imported first declarations + 1. - Record[I] = Record.size() - I; + Record[I] = ((Record.size() - I - 1) / serialization::DeclIDRefSize) + 1; // Collect the set of local redeclarations of this declaration, from // newest to oldest. @@ -2091,8 +2089,8 @@ void ASTDeclWriter::VisitRedeclarable(Redeclarable *D) { (void)Writer.GetDeclRef(D->getPreviousDecl()); (void)Writer.GetDeclRef(MostRecent); } else { - // We use the sentinel value 0 to indicate an only declaration. - Record.push_back(0); + // Use the null decl to indicate an only declaration. + Record.writeNullDeclRef(); } } @@ -2168,37 +2166,52 @@ getFunctionDeclAbbrev(serialization::DeclCode Code) { auto Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(Code)); - // RedeclarableDecl - Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl + // CanonicalDecl of RedeclarableDecl + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); + // Kind Abv->Add(BitCodeAbbrevOp(Kind)); if constexpr (Kind == FunctionDecl::TK_NonTemplate) { } else if constexpr (Kind == FunctionDecl::TK_FunctionTemplate) { // DescribedFunctionTemplate Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); } else if constexpr (Kind == FunctionDecl::TK_DependentNonTemplate) { // Instantiated From Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); } else if constexpr (Kind == FunctionDecl::TK_MemberSpecialization) { - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedFrom - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, - 3)); // TemplateSpecializationKind - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Specialized Location + // InstantiatedFrom + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + // TemplateSpecializationKind + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); + // Specialized Location + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); } else if constexpr (Kind == FunctionDecl::TK_FunctionTemplateSpecialization) { - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, - 3)); // TemplateSpecializationKind - Abv->Add(BitCodeAbbrevOp(1)); // Template Argument Size - Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); // Template Argument Kind - Abv->Add( - BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template Argument Type - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is Defaulted - Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation + // Template + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + // TemplateSpecializationKind + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); + // Template Argument Size + Abv->Add(BitCodeAbbrevOp(1)); + // Template Argument Kind + Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); + // Template Argument Type + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + // Is Defaulted + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); + // TemplateArgumentsAsWritten + Abv->Add(BitCodeAbbrevOp(0)); + // SourceLocation + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(0)); - Abv->Add( - BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Canonical Decl of template + // Canonical Decl of template + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); } else if constexpr (Kind == FunctionDecl:: TK_DependentFunctionTemplateSpecialization) { // Candidates of specialization @@ -2217,7 +2230,9 @@ getFunctionDeclAbbrev(serialization::DeclCode Code) { // HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, // isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind @@ -2251,6 +2266,7 @@ getFunctionDeclAbbrev(serialization::DeclCode Code) { // // Add an AbbrevOp for 'size then elements' and use it here. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array)); + // NumOverriddenMethods Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); return Abv; } @@ -2278,7 +2294,9 @@ void ASTWriter::WriteDeclAbbrevs() { // isImplicit, HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, // isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2307,7 +2325,9 @@ void ASTWriter::WriteDeclAbbrevs() { // isInvalidDecl, HasAttrs, isImplicit, isUsed, // isReferenced, TopLevelDeclInObjCContainer, // AccessSpecifier, ModuleOwnershipKind - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2333,8 +2353,9 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_ENUM Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No Redeclarable + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // Packed DeclBits: ModuleOwnershipKind, @@ -2344,7 +2365,9 @@ void ASTWriter::WriteDeclAbbrevs() { // isImplicit, HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, // isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2367,8 +2390,10 @@ void ASTWriter::WriteDeclAbbrevs() { Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IntegerType Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getPromotionType Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 20)); // Enum Decl Bits - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedMembEnum + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash + // InstantiatedMembEnum + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DC Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset @@ -2377,8 +2402,9 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_RECORD Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No redeclaration + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // Packed DeclBits: ModuleOwnershipKind, @@ -2388,7 +2414,9 @@ void ASTWriter::WriteDeclAbbrevs() { // isImplicit, HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, // isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2428,8 +2456,9 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_PARM_VAR Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No redeclaration + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 8)); // Packed DeclBits: ModuleOwnershipKind, isUsed, @@ -2437,7 +2466,9 @@ void ASTWriter::WriteDeclAbbrevs() { // HasStandaloneLexicalDC, HasAttrs, isImplicit, // TopLevelDeclInObjCContainer, // isInvalidDecl, - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2470,8 +2501,9 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_TYPEDEF Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No redeclaration + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7)); // Packed DeclBits: ModuleOwnershipKind, @@ -2479,7 +2511,9 @@ void ASTWriter::WriteDeclAbbrevs() { // higher bits should be 0: isImplicit, // HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2496,15 +2530,18 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_VAR Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No redeclaration + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 12)); // Packed DeclBits: HasStandaloneLexicalDC, // isInvalidDecl, HasAttrs, isImplicit, isUsed, // isReferenced, TopLevelDeclInObjCContainer, // AccessSpecifier, ModuleOwnershipKind - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2558,7 +2595,9 @@ void ASTWriter::WriteDeclAbbrevs() { // higher bits should be 0: isImplicit, // HasStandaloneLexicalDC, HasAttrs, // TopLevelDeclInObjCContainer, isInvalidDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier @@ -2576,26 +2615,34 @@ void ASTWriter::WriteDeclAbbrevs() { // Abbreviation for DECL_USING_SHADOW Abv = std::make_shared(); Abv->Add(BitCodeAbbrevOp(serialization::DECL_USING_SHADOW)); - // Redeclarable - Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration + // No redeclaration + Abv->Add(BitCodeAbbrevOp(0)); + Abv->Add(BitCodeAbbrevOp(0)); // Decl Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 12)); // Packed DeclBits: HasStandaloneLexicalDC, // isInvalidDecl, HasAttrs, isImplicit, isUsed, // isReferenced, TopLevelDeclInObjCContainer, // AccessSpecifier, ModuleOwnershipKind - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext + // DeclContext + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID // NamedDecl Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name Abv->Add(BitCodeAbbrevOp(0)); // UsingShadowDecl - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TargetDecl + // TargetDecl + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // UsingOrNextShadow - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, - 6)); // InstantiatedFromUsingShadowDecl + // UsingOrNextShadow + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + // InstantiatedFromUsingShadowDecl + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); DeclUsingShadowAbbrev = Stream.EmitAbbrev(std::move(Abv)); // Abbreviation for EXPR_DECL_REF @@ -2611,7 +2658,9 @@ void ASTWriter::WriteDeclAbbrevs() { // IsImmediateEscalating, NonOdrUseReason. // GetDeclFound, HasQualifier and ExplicitTemplateArgs should be 0. Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5)); - Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef + // DeclRef + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); + Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));