Author: Chuanqi Xu Date: 2024-04-23T17:52:58+08:00 New Revision: b467c6b53660dcaa458c2b5d7fbf5f93ee2af910
URL: https://github.com/llvm/llvm-project/commit/b467c6b53660dcaa458c2b5d7fbf5f93ee2af910 DIFF: https://github.com/llvm/llvm-project/commit/b467c6b53660dcaa458c2b5d7fbf5f93ee2af910.diff LOG: [NFC] [Serialization] Turn type alias GlobalDeclID into a class Succsessor of b8e3b2ad66cf78ad2b. This patch also converts the type alias GlobalDeclID to a class to improve the readability and type safety. Added: Modified: clang/include/clang/Serialization/ASTBitCodes.h clang/include/clang/Serialization/ASTReader.h clang/include/clang/Serialization/ASTRecordReader.h clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTReaderDecl.cpp clang/lib/Serialization/ASTReaderInternals.h clang/lib/Serialization/ASTWriter.cpp Removed: ################################################################################ diff --git a/clang/include/clang/Serialization/ASTBitCodes.h b/clang/include/clang/Serialization/ASTBitCodes.h index ca51a2dff3d57b..dcfa4ac0c19677 100644 --- a/clang/include/clang/Serialization/ASTBitCodes.h +++ b/clang/include/clang/Serialization/ASTBitCodes.h @@ -79,9 +79,71 @@ class LocalDeclID { DeclID ID; }; -// FIXME: Turn GlobalDeclID into class so we can have some type safety when -// we go from local ID to global and vice-versa. -using GlobalDeclID = DeclID; +/// Wrapper class for DeclID. This is helpful to not mix the use of LocalDeclID +/// and GlobalDeclID to improve the type safety. +class GlobalDeclID { +public: + GlobalDeclID() : ID(0) {} + explicit GlobalDeclID(DeclID ID) : ID(ID) {} + + DeclID get() const { return ID; } + + explicit operator DeclID() const { return ID; } + + friend bool operator==(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID == RHS.ID; + } + friend bool operator!=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID != RHS.ID; + } + // We may sort the global decl ID. + friend bool operator<(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID < RHS.ID; + } + friend bool operator>(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID > RHS.ID; + } + friend bool operator<=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID <= RHS.ID; + } + friend bool operator>=(const GlobalDeclID &LHS, const GlobalDeclID &RHS) { + return LHS.ID >= RHS.ID; + } + +private: + DeclID ID; +}; + +/// A helper iterator adaptor to convert the iterators to `SmallVector<DeclID>` +/// to the iterators to `SmallVector<GlobalDeclID>`. +class GlobalDeclIDIterator + : public llvm::iterator_adaptor_base<GlobalDeclIDIterator, const DeclID *, + std::forward_iterator_tag, + GlobalDeclID> { +public: + GlobalDeclIDIterator() : iterator_adaptor_base(nullptr) {} + + GlobalDeclIDIterator(const DeclID *ID) : iterator_adaptor_base(ID) {} + + value_type operator*() const { return GlobalDeclID(*I); } + + bool operator==(const GlobalDeclIDIterator &RHS) const { return I == RHS.I; } +}; + +/// A helper iterator adaptor to convert the iterators to +/// `SmallVector<GlobalDeclID>` to the iterators to `SmallVector<DeclID>`. +class DeclIDIterator + : public llvm::iterator_adaptor_base<DeclIDIterator, const GlobalDeclID *, + std::forward_iterator_tag, DeclID> { +public: + DeclIDIterator() : iterator_adaptor_base(nullptr) {} + + DeclIDIterator(const GlobalDeclID *ID) : iterator_adaptor_base(ID) {} + + value_type operator*() const { return DeclID(*I); } + + bool operator==(const DeclIDIterator &RHS) const { return I == RHS.I; } +}; /// An ID number that refers to a type in an AST file. /// @@ -2169,6 +2231,27 @@ template <> struct DenseMapInfo<clang::serialization::DeclarationNameKey> { } }; +template <> struct DenseMapInfo<clang::serialization::GlobalDeclID> { + using DeclID = clang::serialization::DeclID; + using GlobalDeclID = clang::serialization::GlobalDeclID; + + static GlobalDeclID getEmptyKey() { + return GlobalDeclID(DenseMapInfo<DeclID>::getEmptyKey()); + } + + static GlobalDeclID getTombstoneKey() { + return GlobalDeclID(DenseMapInfo<DeclID>::getTombstoneKey()); + } + + static unsigned getHashValue(const GlobalDeclID &Key) { + return DenseMapInfo<DeclID>::getHashValue(Key.get()); + } + + static bool isEqual(const GlobalDeclID &L, const GlobalDeclID &R) { + return L == R; + } +}; + } // namespace llvm #endif // LLVM_CLANG_SERIALIZATION_ASTBITCODES_H diff --git a/clang/include/clang/Serialization/ASTReader.h b/clang/include/clang/Serialization/ASTReader.h index 42aecf059907e8..ed917aa1642293 100644 --- a/clang/include/clang/Serialization/ASTReader.h +++ b/clang/include/clang/Serialization/ASTReader.h @@ -504,7 +504,7 @@ class ASTReader static_assert(std::is_same_v<serialization::DeclID, Decl::DeclID>); using GlobalDeclMapType = - ContinuousRangeMap<serialization::DeclID, ModuleFile *, 4>; + ContinuousRangeMap<serialization::GlobalDeclID, ModuleFile *, 4>; /// Mapping from global declaration IDs to the module in which the /// declaration resides. @@ -513,14 +513,14 @@ class ASTReader using FileOffset = std::pair<ModuleFile *, uint64_t>; using FileOffsetsTy = SmallVector<FileOffset, 2>; using DeclUpdateOffsetsMap = - llvm::DenseMap<serialization::DeclID, FileOffsetsTy>; + llvm::DenseMap<serialization::GlobalDeclID, FileOffsetsTy>; /// Declarations that have modifications residing in a later file /// in the chain. DeclUpdateOffsetsMap DeclUpdateOffsets; using DelayedNamespaceOffsetMapTy = llvm::DenseMap< - serialization::DeclID, + serialization::GlobalDeclID, std::pair</*LexicalOffset*/ uint64_t, /*VisibleOffset*/ uint64_t>>; /// Mapping from global declaration IDs to the lexical and visible block @@ -635,7 +635,7 @@ class ASTReader /// Updates to the visible declarations of declaration contexts that /// haven't been loaded yet. - llvm::DenseMap<serialization::DeclID, DeclContextVisibleUpdates> + llvm::DenseMap<serialization::GlobalDeclID, DeclContextVisibleUpdates> PendingVisibleUpdates; /// The set of C++ or Objective-C classes that have forward @@ -662,7 +662,8 @@ class ASTReader /// Read the record that describes the visible contents of a DC. bool ReadVisibleDeclContextStorage(ModuleFile &M, llvm::BitstreamCursor &Cursor, - uint64_t Offset, serialization::DeclID ID); + uint64_t Offset, + serialization::GlobalDeclID ID); /// A vector containing identifiers that have already been /// loaded. @@ -815,21 +816,26 @@ class ASTReader /// This contains the data loaded from all EAGERLY_DESERIALIZED_DECLS blocks /// in the chain. The referenced declarations are deserialized and passed to /// the consumer eagerly. - SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls; + SmallVector<serialization::GlobalDeclID, 16> EagerlyDeserializedDecls; /// The IDs of all tentative definitions stored in the chain. /// /// Sema keeps track of all tentative definitions in a TU because it has to /// complete them and pass them on to CodeGen. Thus, tentative definitions in /// the PCH chain must be eagerly deserialized. - SmallVector<serialization::DeclID, 16> TentativeDefinitions; + SmallVector<serialization::GlobalDeclID, 16> TentativeDefinitions; /// The IDs of all CXXRecordDecls stored in the chain whose VTables are /// used. /// /// CodeGen has to emit VTables for these records, so they have to be eagerly /// deserialized. - SmallVector<serialization::DeclID, 64> VTableUses; + struct VTableUse { + serialization::GlobalDeclID ID; + SourceLocation::UIntTy RawLoc; + bool Used; + }; + SmallVector<VTableUse> VTableUses; /// A snapshot of the pending instantiations in the chain. /// @@ -837,7 +843,11 @@ class ASTReader /// end of the TU. It consists of a pair of values for every pending /// instantiation where the first value is the ID of the decl and the second /// is the instantiation location. - SmallVector<serialization::DeclID, 64> PendingInstantiations; + struct PendingInstantiation { + serialization::GlobalDeclID ID; + SourceLocation::UIntTy RawLoc; + }; + SmallVector<PendingInstantiation, 64> PendingInstantiations; //@} @@ -847,11 +857,11 @@ class ASTReader /// A snapshot of Sema's unused file-scoped variable tracking, for /// generating warnings. - SmallVector<serialization::DeclID, 16> UnusedFileScopedDecls; + SmallVector<serialization::GlobalDeclID, 16> UnusedFileScopedDecls; /// A list of all the delegating constructors we've seen, to diagnose /// cycles. - SmallVector<serialization::DeclID, 4> DelegatingCtorDecls; + SmallVector<serialization::GlobalDeclID, 4> DelegatingCtorDecls; /// Method selectors used in a @selector expression. Used for /// implementation of -Wselector. @@ -864,7 +874,7 @@ class ASTReader /// The IDs of type aliases for ext_vectors that exist in the chain. /// /// Used by Sema for finding sugared names for ext_vectors in diagnostics. - SmallVector<serialization::DeclID, 4> ExtVectorDecls; + SmallVector<serialization::GlobalDeclID, 4> ExtVectorDecls; //@} @@ -875,7 +885,7 @@ class ASTReader /// The IDs of all potentially unused typedef names in the chain. /// /// Sema tracks these to emit warnings. - SmallVector<serialization::DeclID, 16> UnusedLocalTypedefNameCandidates; + SmallVector<serialization::GlobalDeclID, 16> UnusedLocalTypedefNameCandidates; /// Our current depth in #pragma cuda force_host_device begin/end /// macros. @@ -884,7 +894,7 @@ class ASTReader /// The IDs of the declarations Sema stores directly. /// /// Sema tracks a few important decls, such as namespace std, directly. - SmallVector<serialization::DeclID, 4> SemaDeclRefs; + SmallVector<serialization::GlobalDeclID, 4> SemaDeclRefs; /// The IDs of the types ASTContext stores directly. /// @@ -895,7 +905,7 @@ class ASTReader /// /// The AST context tracks a few important decls, currently cudaConfigureCall, /// directly. - SmallVector<serialization::DeclID, 2> CUDASpecialDeclRefs; + SmallVector<serialization::GlobalDeclID, 2> CUDASpecialDeclRefs; /// The floating point pragma option settings. SmallVector<uint64_t, 1> FPPragmaOptions; @@ -944,11 +954,15 @@ class ASTReader llvm::DenseMap<const Decl *, std::set<std::string>> OpenCLDeclExtMap; /// A list of the namespaces we've seen. - SmallVector<serialization::DeclID, 4> KnownNamespaces; + SmallVector<serialization::GlobalDeclID, 4> KnownNamespaces; /// A list of undefined decls with internal linkage followed by the /// SourceLocation of a matching ODR-use. - SmallVector<serialization::DeclID, 8> UndefinedButUsed; + struct UndefinedButUsedDecl { + serialization::GlobalDeclID ID; + SourceLocation::UIntTy RawLoc; + }; + SmallVector<UndefinedButUsedDecl, 8> UndefinedButUsed; /// Delete expressions to analyze at the end of translation unit. SmallVector<uint64_t, 8> DelayedDeleteExprs; @@ -960,7 +974,8 @@ class ASTReader /// The IDs of all decls to be checked for deferred diags. /// /// Sema tracks these to emit deferred diags. - llvm::SmallSetVector<serialization::DeclID, 4> DeclsToCheckForDeferredDiags; + llvm::SmallSetVector<serialization::GlobalDeclID, 4> + DeclsToCheckForDeferredDiags; private: struct ImportedSubmodule { @@ -1097,7 +1112,7 @@ class ASTReader /// /// The declarations on the identifier chain for these identifiers will be /// loaded once the recursive loading has completed. - llvm::MapVector<IdentifierInfo *, SmallVector<serialization::DeclID, 4>> + llvm::MapVector<IdentifierInfo *, SmallVector<serialization::GlobalDeclID, 4>> PendingIdentifierInfos; /// The set of lookup results that we have faked in order to support @@ -1225,7 +1240,7 @@ class ASTReader SmallVector<ObjCInterfaceDecl *, 16> ObjCClassesLoaded; using KeyDeclsMap = - llvm::DenseMap<Decl *, SmallVector<serialization::DeclID, 2>>; + llvm::DenseMap<Decl *, SmallVector<serialization::GlobalDeclID, 2>>; /// A mapping from canonical declarations to the set of global /// declaration IDs for key declaration that have been merged with that @@ -1434,7 +1449,7 @@ class ASTReader QualType readTypeRecord(unsigned Index); RecordLocation TypeCursorForIndex(unsigned Index); void LoadedDecl(unsigned Index, Decl *D); - Decl *ReadDeclRecord(serialization::DeclID ID); + Decl *ReadDeclRecord(serialization::GlobalDeclID ID); void markIncompleteDeclChain(Decl *D); /// Returns the most recent declaration of a declaration (which must be @@ -1442,7 +1457,7 @@ class ASTReader /// merged into its redecl chain. Decl *getMostRecentExistingDecl(Decl *D); - RecordLocation DeclCursorForID(serialization::DeclID ID, + RecordLocation DeclCursorForID(serialization::GlobalDeclID ID, SourceLocation &Location); void loadDeclUpdateRecords(PendingUpdateRecord &Record); void loadPendingDeclChain(Decl *D, uint64_t LocalOffset); @@ -1901,8 +1916,8 @@ class ASTReader /// Map from a local declaration ID within a given module to a /// global declaration ID. - serialization::DeclID getGlobalDeclID(ModuleFile &F, - serialization::LocalDeclID LocalID) const; + serialization::GlobalDeclID + getGlobalDeclID(ModuleFile &F, serialization::LocalDeclID LocalID) const; /// Returns true if global DeclID \p ID originated from module \p M. bool isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const; @@ -1916,12 +1931,12 @@ class ASTReader /// Resolve a declaration ID into a declaration, potentially /// building a new declaration. - Decl *GetDecl(serialization::DeclID ID); - Decl *GetExternalDecl(serialization::DeclID ID) override; + Decl *GetDecl(serialization::GlobalDeclID ID); + Decl *GetExternalDecl(Decl::DeclID ID) override; /// Resolve a declaration ID into a declaration. Return 0 if it's not /// been loaded yet. - Decl *GetExistingDecl(serialization::DeclID ID); + Decl *GetExistingDecl(serialization::GlobalDeclID ID); /// Reads a declaration with the given local ID in the given module. Decl *GetLocalDecl(ModuleFile &F, serialization::LocalDeclID LocalID) { @@ -1943,14 +1958,14 @@ class ASTReader /// module file. serialization::DeclID mapGlobalIDToModuleFileGlobalID(ModuleFile &M, - serialization::DeclID GlobalID); + serialization::GlobalDeclID GlobalID); /// Reads a declaration ID from the given position in a record in the /// given module. /// /// \returns The declaration ID read from the record, adjusted to a global ID. - serialization::DeclID ReadDeclID(ModuleFile &F, const RecordData &Record, - unsigned &Idx); + serialization::GlobalDeclID + ReadDeclID(ModuleFile &F, const RecordData &Record, unsigned &Idx); /// Reads a declaration from the given position in a record in the /// given module. @@ -2124,10 +2139,10 @@ class ASTReader void LoadSelector(Selector Sel); void SetIdentifierInfo(unsigned ID, IdentifierInfo *II); - void - SetGloballyVisibleDecls(IdentifierInfo *II, - const SmallVectorImpl<serialization::DeclID> &DeclIDs, - SmallVectorImpl<Decl *> *Decls = nullptr); + void SetGloballyVisibleDecls( + IdentifierInfo *II, + const SmallVectorImpl<serialization::GlobalDeclID> &DeclIDs, + SmallVectorImpl<Decl *> *Decls = nullptr); /// Report a diagnostic. DiagnosticBuilder Diag(unsigned DiagID) const; @@ -2368,7 +2383,7 @@ class ASTReader // Contains the IDs for declarations that were requested before we have // access to a Sema object. - SmallVector<uint64_t, 16> PreloadedDeclIDs; + SmallVector<serialization::GlobalDeclID, 16> PreloadedDeclIDs; /// Retrieve the semantic analysis object used to analyze the /// translation unit in which the precompiled header is being diff --git a/clang/include/clang/Serialization/ASTRecordReader.h b/clang/include/clang/Serialization/ASTRecordReader.h index da03ffcb2974bb..9eaf50a76d52f4 100644 --- a/clang/include/clang/Serialization/ASTRecordReader.h +++ b/clang/include/clang/Serialization/ASTRecordReader.h @@ -182,7 +182,7 @@ class ASTRecordReader /// Reads a declaration ID from the given position in this record. /// /// \returns The declaration ID read from the record, adjusted to a global ID. - serialization::DeclID readDeclID() { + serialization::GlobalDeclID readDeclID() { return Reader->ReadDeclID(*F, Record, Idx); } diff --git a/clang/lib/Serialization/ASTReader.cpp b/clang/lib/Serialization/ASTReader.cpp index cfb6ab42c36bd7..43b69045bb0543 100644 --- a/clang/lib/Serialization/ASTReader.cpp +++ b/clang/lib/Serialization/ASTReader.cpp @@ -1090,7 +1090,7 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k, // Read all of the declarations visible at global scope with this // name. if (DataLen > 0) { - SmallVector<DeclID, 4> DeclIDs; + SmallVector<GlobalDeclID, 4> DeclIDs; for (; DataLen > 0; DataLen -= sizeof(DeclID)) DeclIDs.push_back(Reader.getGlobalDeclID( F, @@ -1272,7 +1272,7 @@ bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M, bool ASTReader::ReadVisibleDeclContextStorage(ModuleFile &M, BitstreamCursor &Cursor, uint64_t Offset, - DeclID ID) { + GlobalDeclID ID) { assert(Offset != 0); SavedStreamPosition SavedPosition(Cursor); @@ -3371,8 +3371,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, if (F.LocalNumDecls > 0) { // Introduce the global -> local mapping for declarations within this // module. - GlobalDeclMap.insert( - std::make_pair(getTotalNumDecls() + NUM_PREDEF_DECL_IDS, &F)); + GlobalDeclMap.insert(std::make_pair( + GlobalDeclID(getTotalNumDecls() + NUM_PREDEF_DECL_IDS), &F)); // Introduce the local -> global mapping for declarations within this // module. @@ -3400,7 +3400,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, case UPDATE_VISIBLE: { unsigned Idx = 0; - serialization::DeclID ID = ReadDeclID(F, Record, Idx); + GlobalDeclID ID = ReadDeclID(F, Record, Idx); auto *Data = (const unsigned char*)Blob.data(); PendingVisibleUpdates[ID].push_back(PendingVisibleUpdate{&F, Data}); // If we've already loaded the decl, perform the updates when we finish @@ -3688,18 +3688,14 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, VTableUses.clear(); for (unsigned Idx = 0, N = Record.size(); Idx != N; /* In loop */) { - VTableUses.push_back(getGlobalDeclID(F, LocalDeclID(Record[Idx++]))); VTableUses.push_back( - ReadSourceLocation(F, Record, Idx).getRawEncoding()); - VTableUses.push_back(Record[Idx++]); + {getGlobalDeclID(F, LocalDeclID(Record[Idx++])), + ReadSourceLocation(F, Record, Idx).getRawEncoding(), + (bool)Record[Idx++]}); } break; case PENDING_IMPLICIT_INSTANTIATIONS: - if (PendingInstantiations.size() % 2 != 0) - return llvm::createStringError( - std::errc::illegal_byte_sequence, - "Invalid existing PendingInstantiations"); if (Record.size() % 2 != 0) return llvm::createStringError( @@ -3708,9 +3704,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) { PendingInstantiations.push_back( - getGlobalDeclID(F, LocalDeclID(Record[I++]))); - PendingInstantiations.push_back( - ReadSourceLocation(F, Record, I).getRawEncoding()); + {getGlobalDeclID(F, LocalDeclID(Record[I++])), + ReadSourceLocation(F, Record, I).getRawEncoding()}); } break; @@ -3883,25 +3878,20 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, break; case UNDEFINED_BUT_USED: - if (UndefinedButUsed.size() % 2 != 0) - return llvm::createStringError(std::errc::illegal_byte_sequence, - "Invalid existing UndefinedButUsed"); - 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 */) { UndefinedButUsed.push_back( - getGlobalDeclID(F, LocalDeclID(Record[I++]))); - UndefinedButUsed.push_back( - ReadSourceLocation(F, Record, I).getRawEncoding()); + {getGlobalDeclID(F, LocalDeclID(Record[I++])), + ReadSourceLocation(F, Record, I).getRawEncoding()}); } break; case DELETE_EXPRS_TO_ANALYZE: for (unsigned I = 0, N = Record.size(); I != N;) { DelayedDeleteExprs.push_back( - getGlobalDeclID(F, LocalDeclID(Record[I++]))); + getGlobalDeclID(F, LocalDeclID(Record[I++])).get()); const uint64_t Count = Record[I++]; DelayedDeleteExprs.push_back(Count); for (uint64_t C = 0; C < Count; ++C) { @@ -4666,9 +4656,8 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type, // that we load any additional categories. if (ContextObj) { for (unsigned I = 0, N = ObjCClassesLoaded.size(); I != N; ++I) { - loadObjCCategories(ObjCClassesLoaded[I]->getGlobalID(), - ObjCClassesLoaded[I], - PreviousGeneration); + loadObjCCategories(GlobalDeclID(ObjCClassesLoaded[I]->getGlobalID()), + ObjCClassesLoaded[I], PreviousGeneration); } } @@ -6023,7 +6012,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F, break; SmallVector<DeclID, 16> Inits; for (auto &ID : Record) - Inits.push_back(getGlobalDeclID(F, LocalDeclID(ID))); + Inits.push_back(getGlobalDeclID(F, LocalDeclID(ID)).get()); ContextObj->addLazyModuleInitializers(CurrentModule, Inits); break; } @@ -7528,7 +7517,9 @@ ASTRecordReader::readASTTemplateArgumentListInfo() { return ASTTemplateArgumentListInfo::Create(getContext(), Result); } -Decl *ASTReader::GetExternalDecl(DeclID ID) { return GetDecl(ID); } +Decl *ASTReader::GetExternalDecl(DeclID ID) { + return GetDecl(GlobalDeclID(ID)); +} void ASTReader::CompleteRedeclChain(const Decl *D) { if (NumCurrentElementsDeserializing) { @@ -7661,11 +7652,11 @@ CXXBaseSpecifier *ASTReader::GetExternalCXXBaseSpecifiers(uint64_t Offset) { return Bases; } -serialization::DeclID -ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { +GlobalDeclID ASTReader::getGlobalDeclID(ModuleFile &F, + LocalDeclID LocalID) const { DeclID ID = LocalID.get(); if (ID < NUM_PREDEF_DECL_IDS) - return ID; + return GlobalDeclID(ID); if (!F.ModuleOffsetMap.empty()) ReadModuleOffsetMap(F); @@ -7674,32 +7665,33 @@ ASTReader::getGlobalDeclID(ModuleFile &F, LocalDeclID LocalID) const { F.DeclRemap.find(ID - NUM_PREDEF_DECL_IDS); assert(I != F.DeclRemap.end() && "Invalid index into decl index remap"); - return ID + I->second; + return GlobalDeclID(ID + I->second); } bool ASTReader::isDeclIDFromModule(serialization::GlobalDeclID ID, ModuleFile &M) const { // Predefined decls aren't from any module. - if (ID < NUM_PREDEF_DECL_IDS) + if (ID.get() < NUM_PREDEF_DECL_IDS) return false; - return ID - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && - ID - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; + return ID.get() - NUM_PREDEF_DECL_IDS >= M.BaseDeclID && + ID.get() - NUM_PREDEF_DECL_IDS < M.BaseDeclID + M.LocalNumDecls; } ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) { if (!D->isFromASTFile()) return nullptr; - GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(D->getGlobalID()); + GlobalDeclMapType::const_iterator I = + GlobalDeclMap.find(GlobalDeclID(D->getGlobalID())); assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); return I->second; } SourceLocation ASTReader::getSourceLocationForDeclID(GlobalDeclID ID) { - if (ID < NUM_PREDEF_DECL_IDS) + if (ID.get() < NUM_PREDEF_DECL_IDS) return SourceLocation(); - unsigned Index = ID - NUM_PREDEF_DECL_IDS; + unsigned Index = ID.get() - NUM_PREDEF_DECL_IDS; if (Index > DeclsLoaded.size()) { Error("declaration ID out-of-range for AST file"); @@ -7773,10 +7765,10 @@ static Decl *getPredefinedDecl(ASTContext &Context, PredefinedDeclIDs ID) { llvm_unreachable("PredefinedDeclIDs unknown enum value"); } -Decl *ASTReader::GetExistingDecl(DeclID ID) { +Decl *ASTReader::GetExistingDecl(GlobalDeclID ID) { assert(ContextObj && "reading decl with no AST context"); - if (ID < NUM_PREDEF_DECL_IDS) { - Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID); + if (ID.get() < NUM_PREDEF_DECL_IDS) { + Decl *D = getPredefinedDecl(*ContextObj, (PredefinedDeclIDs)ID.get()); if (D) { // Track that we have merged the declaration with ID \p ID into the // pre-existing predefined declaration \p D. @@ -7787,7 +7779,7 @@ Decl *ASTReader::GetExistingDecl(DeclID ID) { return D; } - unsigned Index = ID - NUM_PREDEF_DECL_IDS; + unsigned Index = ID.get() - NUM_PREDEF_DECL_IDS; if (Index >= DeclsLoaded.size()) { assert(0 && "declaration ID out-of-range for AST file"); @@ -7798,11 +7790,11 @@ Decl *ASTReader::GetExistingDecl(DeclID ID) { return DeclsLoaded[Index]; } -Decl *ASTReader::GetDecl(DeclID ID) { - if (ID < NUM_PREDEF_DECL_IDS) +Decl *ASTReader::GetDecl(GlobalDeclID ID) { + if (ID.get() < NUM_PREDEF_DECL_IDS) return GetExistingDecl(ID); - unsigned Index = ID - NUM_PREDEF_DECL_IDS; + unsigned Index = ID.get() - NUM_PREDEF_DECL_IDS; if (Index >= DeclsLoaded.size()) { assert(0 && "declaration ID out-of-range for AST file"); @@ -7813,16 +7805,17 @@ Decl *ASTReader::GetDecl(DeclID ID) { if (!DeclsLoaded[Index]) { ReadDeclRecord(ID); if (DeserializationListener) - DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); + DeserializationListener->DeclRead(ID.get(), DeclsLoaded[Index]); } return DeclsLoaded[Index]; } DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, - DeclID GlobalID) { - if (GlobalID < NUM_PREDEF_DECL_IDS) - return GlobalID; + GlobalDeclID GlobalID) { + DeclID ID = GlobalID.get(); + if (ID < NUM_PREDEF_DECL_IDS) + return ID; GlobalDeclMapType::const_iterator I = GlobalDeclMap.find(GlobalID); assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); @@ -7833,15 +7826,14 @@ DeclID ASTReader::mapGlobalIDToModuleFileGlobalID(ModuleFile &M, if (Pos == M.GlobalToLocalDeclIDs.end()) return 0; - return GlobalID - Owner->BaseDeclID + Pos->second; + return ID - Owner->BaseDeclID + Pos->second; } -serialization::DeclID ASTReader::ReadDeclID(ModuleFile &F, - const RecordData &Record, - unsigned &Idx) { +GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordData &Record, + unsigned &Idx) { if (Idx >= Record.size()) { Error("Corrupted AST file"); - return 0; + return GlobalDeclID(0); } return getGlobalDeclID(F, LocalDeclID(Record[Idx++])); @@ -8002,7 +7994,7 @@ ASTReader::FindExternalVisibleDeclsByName(const DeclContext *DC, // Load the list of declarations. SmallVector<NamedDecl *, 64> Decls; llvm::SmallPtrSet<NamedDecl *, 8> Found; - for (DeclID ID : It->second.Table.find(Name)) { + for (GlobalDeclID ID : It->second.Table.find(Name)) { NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); if (ND->getDeclName() == Name && Found.insert(ND).second) Decls.push_back(ND); @@ -8023,7 +8015,7 @@ void ASTReader::completeVisibleDeclsMap(const DeclContext *DC) { DeclsMap Decls; - for (DeclID ID : It->second.Table.findAll()) { + for (GlobalDeclID ID : It->second.Table.findAll()) { NamedDecl *ND = cast<NamedDecl>(GetDecl(ID)); Decls[ND->getDeclName()].push_back(ND); } @@ -8174,8 +8166,12 @@ dumpModuleIDMap(StringRef Name, llvm::errs() << Name << ":\n"; for (typename MapType::const_iterator I = Map.begin(), IEnd = Map.end(); I != IEnd; ++I) { - llvm::errs() << " " << I->first << " -> " << I->second->FileName - << "\n"; + uint64_t ID = 0; + if constexpr (std::is_integral_v<Key>) + ID = I->first; + else /*GlobalDeclID*/ + ID = I->first.get(); + llvm::errs() << " " << ID << " -> " << I->second->FileName << "\n"; } } @@ -8221,7 +8217,7 @@ void ASTReader::InitializeSema(Sema &S) { // Makes sure any declarations that were deserialized "too early" // still get added to the identifier's declaration chains. - for (uint64_t ID : PreloadedDeclIDs) { + for (GlobalDeclID ID : PreloadedDeclIDs) { NamedDecl *D = cast<NamedDecl>(GetDecl(ID)); pushExternalDeclIntoScope(D, D->getDeclName()); } @@ -8250,11 +8246,11 @@ void ASTReader::UpdateSema() { assert(SemaDeclRefs.size() % 3 == 0); for (unsigned I = 0; I != SemaDeclRefs.size(); I += 3) { if (!SemaObj->StdNamespace) - SemaObj->StdNamespace = SemaDeclRefs[I]; + SemaObj->StdNamespace = SemaDeclRefs[I].get(); if (!SemaObj->StdBadAlloc) - SemaObj->StdBadAlloc = SemaDeclRefs[I+1]; + SemaObj->StdBadAlloc = SemaDeclRefs[I + 1].get(); if (!SemaObj->StdAlignValT) - SemaObj->StdAlignValT = SemaDeclRefs[I+2]; + SemaObj->StdAlignValT = SemaDeclRefs[I + 2].get(); } SemaDeclRefs.clear(); } @@ -8627,18 +8623,20 @@ void ASTReader::ReadKnownNamespaces( void ASTReader::ReadUndefinedButUsed( llvm::MapVector<NamedDecl *, SourceLocation> &Undefined) { for (unsigned Idx = 0, N = UndefinedButUsed.size(); Idx != N;) { - NamedDecl *D = cast<NamedDecl>(GetDecl(UndefinedButUsed[Idx++])); - SourceLocation Loc = - SourceLocation::getFromRawEncoding(UndefinedButUsed[Idx++]); + UndefinedButUsedDecl &U = UndefinedButUsed[Idx++]; + NamedDecl *D = cast<NamedDecl>(GetDecl(U.ID)); + SourceLocation Loc = SourceLocation::getFromRawEncoding(U.RawLoc); Undefined.insert(std::make_pair(D, Loc)); } + UndefinedButUsed.clear(); } void ASTReader::ReadMismatchingDeleteExpressions(llvm::MapVector< FieldDecl *, llvm::SmallVector<std::pair<SourceLocation, bool>, 4>> & Exprs) { for (unsigned Idx = 0, N = DelayedDeleteExprs.size(); Idx != N;) { - FieldDecl *FD = cast<FieldDecl>(GetDecl(DelayedDeleteExprs[Idx++])); + FieldDecl *FD = + cast<FieldDecl>(GetDecl(GlobalDeclID(DelayedDeleteExprs[Idx++]))); uint64_t Count = DelayedDeleteExprs[Idx++]; for (uint64_t C = 0; C < Count; ++C) { SourceLocation DeleteLoc = @@ -8752,9 +8750,10 @@ void ASTReader::ReadWeakUndeclaredIdentifiers( void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { for (unsigned Idx = 0, N = VTableUses.size(); Idx < N; /* In loop */) { ExternalVTableUse VT; - VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); - VT.Location = SourceLocation::getFromRawEncoding(VTableUses[Idx++]); - VT.DefinitionRequired = VTableUses[Idx++]; + VTableUse &TableInfo = VTableUses[Idx++]; + VT.Record = dyn_cast_or_null<CXXRecordDecl>(GetDecl(TableInfo.ID)); + VT.Location = SourceLocation::getFromRawEncoding(TableInfo.RawLoc); + VT.DefinitionRequired = TableInfo.Used; VTables.push_back(VT); } @@ -8764,9 +8763,9 @@ void ASTReader::ReadUsedVTables(SmallVectorImpl<ExternalVTableUse> &VTables) { void ASTReader::ReadPendingInstantiations( SmallVectorImpl<std::pair<ValueDecl *, SourceLocation>> &Pending) { for (unsigned Idx = 0, N = PendingInstantiations.size(); Idx < N;) { - ValueDecl *D = cast<ValueDecl>(GetDecl(PendingInstantiations[Idx++])); - SourceLocation Loc - = SourceLocation::getFromRawEncoding(PendingInstantiations[Idx++]); + PendingInstantiation &Inst = PendingInstantiations[Idx++]; + ValueDecl *D = cast<ValueDecl>(GetDecl(Inst.ID)); + SourceLocation Loc = SourceLocation::getFromRawEncoding(Inst.RawLoc); Pending.push_back(std::make_pair(D, Loc)); } @@ -8843,9 +8842,9 @@ void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { /// \param Decls if non-null, this vector will be populated with the set of /// deserialized declarations. These declarations will not be pushed into /// scope. -void ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, - const SmallVectorImpl<DeclID> &DeclIDs, - SmallVectorImpl<Decl *> *Decls) { +void ASTReader::SetGloballyVisibleDecls( + IdentifierInfo *II, const SmallVectorImpl<GlobalDeclID> &DeclIDs, + SmallVectorImpl<Decl *> *Decls) { if (NumCurrentElementsDeserializing && !Decls) { PendingIdentifierInfos[II].append(DeclIDs.begin(), DeclIDs.end()); return; @@ -9193,9 +9192,9 @@ void ASTRecordReader::readUnresolvedSet(LazyASTUnresolvedSet &Set) { unsigned NumDecls = readInt(); Set.reserve(getContext(), NumDecls); while (NumDecls--) { - DeclID ID = readDeclID(); + GlobalDeclID ID = readDeclID(); AccessSpecifier AS = (AccessSpecifier) readInt(); - Set.addLazyDecl(getContext(), ID, AS); + Set.addLazyDecl(getContext(), ID.get(), AS); } } @@ -9569,7 +9568,7 @@ void ASTReader::finishPendingActions() { while (!PendingIdentifierInfos.empty()) { IdentifierInfo *II = PendingIdentifierInfos.back().first; - SmallVector<DeclID, 4> DeclIDs = + SmallVector<GlobalDeclID, 4> DeclIDs = std::move(PendingIdentifierInfos.back().second); PendingIdentifierInfos.pop_back(); diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 7e1b1592c9d0b1..bb82173dfe0b3a 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -84,14 +84,14 @@ namespace clang { ASTReader &Reader; ASTRecordReader &Record; ASTReader::RecordLocation Loc; - const DeclID ThisDeclID; + const GlobalDeclID ThisDeclID; const SourceLocation ThisDeclLoc; using RecordData = ASTReader::RecordData; TypeID DeferredTypeID = 0; unsigned AnonymousDeclNumber = 0; - GlobalDeclID NamedDeclForTagDecl = 0; + GlobalDeclID NamedDeclForTagDecl = GlobalDeclID(); IdentifierInfo *TypedefNameForLinkage = nullptr; ///A flag to carry the information for a decl from the entity is @@ -124,15 +124,13 @@ namespace clang { return Record.readTypeSourceInfo(); } - serialization::DeclID readDeclID() { - return Record.readDeclID(); - } + GlobalDeclID readDeclID() { return Record.readDeclID(); } std::string readString() { return Record.readString(); } - void readDeclIDList(SmallVectorImpl<DeclID> &IDs) { + void readDeclIDList(SmallVectorImpl<GlobalDeclID> &IDs) { for (unsigned I = 0, Size = Record.readInt(); I != Size; ++I) IDs.push_back(readDeclID()); } @@ -258,14 +256,14 @@ namespace clang { public: ASTDeclReader(ASTReader &Reader, ASTRecordReader &Record, - ASTReader::RecordLocation Loc, - DeclID thisDeclID, SourceLocation ThisDeclLoc) + ASTReader::RecordLocation Loc, GlobalDeclID thisDeclID, + SourceLocation ThisDeclLoc) : Reader(Reader), Record(Record), Loc(Loc), ThisDeclID(thisDeclID), ThisDeclLoc(ThisDeclLoc) {} - template <typename T> static - void AddLazySpecializations(T *D, - SmallVectorImpl<serialization::DeclID>& IDs) { + template <typename T> + static void AddLazySpecializations(T *D, + SmallVectorImpl<GlobalDeclID> &IDs) { if (IDs.empty()) return; @@ -275,14 +273,17 @@ namespace clang { auto *&LazySpecializations = D->getCommonPtr()->LazySpecializations; if (auto &Old = LazySpecializations) { - IDs.insert(IDs.end(), Old + 1, Old + 1 + Old[0]); + IDs.insert(IDs.end(), GlobalDeclIDIterator(Old + 1), + GlobalDeclIDIterator(Old + 1 + Old[0])); llvm::sort(IDs); IDs.erase(std::unique(IDs.begin(), IDs.end()), IDs.end()); } auto *Result = new (C) serialization::DeclID[1 + IDs.size()]; *Result = IDs.size(); - std::copy(IDs.begin(), IDs.end(), Result + 1); + + std::copy(DeclIDIterator(IDs.begin()), DeclIDIterator(IDs.end()), + Result + 1); LazySpecializations = Result; } @@ -315,7 +316,7 @@ namespace clang { void ReadFunctionDefinition(FunctionDecl *FD); void Visit(Decl *D); - void UpdateDecl(Decl *D, SmallVectorImpl<serialization::DeclID> &); + void UpdateDecl(Decl *D, SmallVectorImpl<GlobalDeclID> &); static void setNextObjCCategory(ObjCCategoryDecl *Cat, ObjCCategoryDecl *Next) { @@ -557,7 +558,7 @@ void ASTDeclReader::Visit(Decl *D) { // If this is a tag declaration with a typedef name for linkage, it's safe // to load that typedef now. - if (NamedDeclForTagDecl) + if (NamedDeclForTagDecl != GlobalDeclID()) cast<TagDecl>(D)->TypedefNameDeclOrQualifier = cast<TypedefNameDecl>(Reader.GetDecl(NamedDeclForTagDecl)); } else if (auto *ID = dyn_cast<ObjCInterfaceDecl>(D)) { @@ -601,8 +602,8 @@ void ASTDeclReader::VisitDecl(Decl *D) { // placeholder. GlobalDeclID SemaDCIDForTemplateParmDecl = readDeclID(); GlobalDeclID LexicalDCIDForTemplateParmDecl = - HasStandaloneLexicalDC ? readDeclID() : 0; - if (!LexicalDCIDForTemplateParmDecl) + HasStandaloneLexicalDC ? readDeclID() : GlobalDeclID(); + if (LexicalDCIDForTemplateParmDecl == GlobalDeclID()) LexicalDCIDForTemplateParmDecl = SemaDCIDForTemplateParmDecl; Reader.addPendingDeclContextInfo(D, SemaDCIDForTemplateParmDecl, @@ -1848,7 +1849,7 @@ void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) { // this namespace; loading it might load a later declaration of the // same namespace, and we have an invariant that older declarations // get merged before newer ones try to merge. - GlobalDeclID AnonNamespace = 0; + GlobalDeclID AnonNamespace; if (Redecl.getFirstID() == ThisDeclID) { AnonNamespace = readDeclID(); } else { @@ -1859,7 +1860,7 @@ void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) { mergeRedeclarable(D, Redecl); - if (AnonNamespace) { + if (AnonNamespace != GlobalDeclID()) { // Each module has its own anonymous namespace, which is disjoint from // any other module's anonymous namespaces, so don't attach the anonymous // namespace at all. @@ -2019,7 +2020,7 @@ void ASTDeclReader::ReadCXXDefinitionData( if (Data.NumVBases) Data.VBases = ReadGlobalOffset(); - Data.FirstFriend = readDeclID(); + Data.FirstFriend = readDeclID().get(); } else { using Capture = LambdaCapture; @@ -2278,12 +2279,12 @@ ASTDeclReader::VisitCXXRecordDeclImpl(CXXRecordDecl *D) { // Lazily load the key function to avoid deserializing every method so we can // compute it. if (WasDefinition) { - DeclID KeyFn = readDeclID(); - if (KeyFn && D->isCompleteDefinition()) + GlobalDeclID KeyFn = readDeclID(); + if (KeyFn.get() && D->isCompleteDefinition()) // FIXME: This is wrong for the ARM ABI, where some other module may have // made this function no longer be a key function. We need an update // record or similar for that case. - C.KeyFunctions[D] = KeyFn; + C.KeyFunctions[D] = KeyFn.get(); } return Redecl; @@ -2372,7 +2373,7 @@ void ASTDeclReader::VisitFriendDecl(FriendDecl *D) { for (unsigned i = 0; i != D->NumTPLists; ++i) D->getTrailingObjects<TemplateParameterList *>()[i] = Record.readTemplateParameterList(); - D->NextFriend = readDeclID(); + D->NextFriend = readDeclID().get(); D->UnsupportedFriend = (Record.readInt() != 0); D->FriendLoc = readSourceLocation(); } @@ -2457,7 +2458,7 @@ void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of // the specializations. - SmallVector<serialization::DeclID, 32> SpecIDs; + SmallVector<GlobalDeclID, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -2485,7 +2486,7 @@ void ASTDeclReader::VisitVarTemplateDecl(VarTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This VarTemplateDecl owns a CommonPtr; read it to keep track of all of // the specializations. - SmallVector<serialization::DeclID, 32> SpecIDs; + SmallVector<GlobalDeclID, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -2587,7 +2588,7 @@ void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { if (ThisDeclID == Redecl.getFirstID()) { // This FunctionTemplateDecl owns a CommonPtr; read it. - SmallVector<serialization::DeclID, 32> SpecIDs; + SmallVector<GlobalDeclID, 32> SpecIDs; readDeclIDList(SpecIDs); ASTDeclReader::AddLazySpecializations(D, SpecIDs); } @@ -2783,7 +2784,7 @@ ASTDeclReader::VisitDeclContext(DeclContext *DC) { template <typename T> ASTDeclReader::RedeclarableResult ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) { - DeclID FirstDeclID = readDeclID(); + GlobalDeclID FirstDeclID = readDeclID(); Decl *MergeWith = nullptr; bool IsKeyDecl = ThisDeclID == FirstDeclID; @@ -2793,7 +2794,7 @@ ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) { // 0 indicates that this declaration was the only declaration of its entity, // and is used for space optimization. - if (FirstDeclID == 0) { + if (FirstDeclID == GlobalDeclID()) { FirstDeclID = ThisDeclID; IsKeyDecl = true; IsFirstLocalDecl = true; @@ -2922,9 +2923,9 @@ void ASTDeclReader::mergeTemplatePattern(RedeclarableTemplateDecl *D, bool IsKeyDecl) { auto *DPattern = D->getTemplatedDecl(); auto *ExistingPattern = Existing->getTemplatedDecl(); - RedeclarableResult Result(/*MergeWith*/ ExistingPattern, - DPattern->getCanonicalDecl()->getGlobalID(), - IsKeyDecl); + RedeclarableResult Result( + /*MergeWith*/ ExistingPattern, + GlobalDeclID(DPattern->getCanonicalDecl()->getGlobalID()), IsKeyDecl); if (auto *DClass = dyn_cast<CXXRecordDecl>(DPattern)) { // Merge with any existing definition. @@ -3079,14 +3080,14 @@ void ASTDeclReader::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) { Expr *Init = Record.readExpr(); auto IK = static_cast<OMPDeclareReductionInitKind>(Record.readInt()); D->setInitializer(Init, IK); - D->PrevDeclInScope = readDeclID(); + D->PrevDeclInScope = readDeclID().get(); } void ASTDeclReader::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) { Record.readOMPChildren(D->Data); VisitValueDecl(D); D->VarName = Record.readDeclarationName(); - D->PrevDeclInScope = readDeclID(); + D->PrevDeclInScope = readDeclID().get(); } void ASTDeclReader::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) { @@ -3243,13 +3244,13 @@ bool ASTReader::isConsumerInterestedIn(Decl *D) { } /// Get the correct cursor and offset for loading a declaration. -ASTReader::RecordLocation -ASTReader::DeclCursorForID(DeclID ID, SourceLocation &Loc) { +ASTReader::RecordLocation ASTReader::DeclCursorForID(GlobalDeclID ID, + SourceLocation &Loc) { GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID); assert(I != GlobalDeclMap.end() && "Corrupted global declaration map"); ModuleFile *M = I->second; const DeclOffset &DOffs = - M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; + M->DeclOffsets[ID.get() - M->BaseDeclID - NUM_PREDEF_DECL_IDS]; Loc = TranslateSourceLocation(*M, DOffs.getLocation()); return RecordLocation(M, DOffs.getBitOffset(M->DeclsBlockStartOffset)); } @@ -3792,8 +3793,8 @@ void ASTReader::markIncompleteDeclChain(Decl *D) { } /// Read the declaration at the given offset from the AST file. -Decl *ASTReader::ReadDeclRecord(DeclID ID) { - unsigned Index = ID - NUM_PREDEF_DECL_IDS; +Decl *ASTReader::ReadDeclRecord(GlobalDeclID ID) { + unsigned Index = ID.get() - NUM_PREDEF_DECL_IDS; SourceLocation DeclLoc; RecordLocation Loc = DeclCursorForID(ID, DeclLoc); llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor; @@ -3827,233 +3828,241 @@ Decl *ASTReader::ReadDeclRecord(DeclID ID) { llvm::report_fatal_error( Twine("ASTReader::readDeclRecord failed reading decl code: ") + toString(MaybeDeclCode.takeError())); + + DeclID RawGlobalID = ID.get(); switch ((DeclCode)MaybeDeclCode.get()) { case DECL_CONTEXT_LEXICAL: case DECL_CONTEXT_VISIBLE: llvm_unreachable("Record cannot be de-serialized with readDeclRecord"); case DECL_TYPEDEF: - D = TypedefDecl::CreateDeserialized(Context, ID); + D = TypedefDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_TYPEALIAS: - D = TypeAliasDecl::CreateDeserialized(Context, ID); + D = TypeAliasDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_ENUM: - D = EnumDecl::CreateDeserialized(Context, ID); + D = EnumDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_RECORD: - D = RecordDecl::CreateDeserialized(Context, ID); + D = RecordDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_ENUM_CONSTANT: - D = EnumConstantDecl::CreateDeserialized(Context, ID); + D = EnumConstantDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_FUNCTION: - D = FunctionDecl::CreateDeserialized(Context, ID); + D = FunctionDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_LINKAGE_SPEC: - D = LinkageSpecDecl::CreateDeserialized(Context, ID); + D = LinkageSpecDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_EXPORT: - D = ExportDecl::CreateDeserialized(Context, ID); + D = ExportDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_LABEL: - D = LabelDecl::CreateDeserialized(Context, ID); + D = LabelDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_NAMESPACE: - D = NamespaceDecl::CreateDeserialized(Context, ID); + D = NamespaceDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_NAMESPACE_ALIAS: - D = NamespaceAliasDecl::CreateDeserialized(Context, ID); + D = NamespaceAliasDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_USING: - D = UsingDecl::CreateDeserialized(Context, ID); + D = UsingDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_USING_PACK: - D = UsingPackDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = UsingPackDecl::CreateDeserialized(Context, RawGlobalID, + Record.readInt()); break; case DECL_USING_SHADOW: - D = UsingShadowDecl::CreateDeserialized(Context, ID); + D = UsingShadowDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_USING_ENUM: - D = UsingEnumDecl::CreateDeserialized(Context, ID); + D = UsingEnumDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CONSTRUCTOR_USING_SHADOW: - D = ConstructorUsingShadowDecl::CreateDeserialized(Context, ID); + D = ConstructorUsingShadowDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_USING_DIRECTIVE: - D = UsingDirectiveDecl::CreateDeserialized(Context, ID); + D = UsingDirectiveDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_UNRESOLVED_USING_VALUE: - D = UnresolvedUsingValueDecl::CreateDeserialized(Context, ID); + D = UnresolvedUsingValueDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_UNRESOLVED_USING_TYPENAME: - D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, ID); + D = UnresolvedUsingTypenameDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_UNRESOLVED_USING_IF_EXISTS: - D = UnresolvedUsingIfExistsDecl::CreateDeserialized(Context, ID); + D = UnresolvedUsingIfExistsDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CXX_RECORD: - D = CXXRecordDecl::CreateDeserialized(Context, ID); + D = CXXRecordDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CXX_DEDUCTION_GUIDE: - D = CXXDeductionGuideDecl::CreateDeserialized(Context, ID); + D = CXXDeductionGuideDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CXX_METHOD: - D = CXXMethodDecl::CreateDeserialized(Context, ID); + D = CXXMethodDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CXX_CONSTRUCTOR: - D = CXXConstructorDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = CXXConstructorDecl::CreateDeserialized(Context, RawGlobalID, + Record.readInt()); break; case DECL_CXX_DESTRUCTOR: - D = CXXDestructorDecl::CreateDeserialized(Context, ID); + D = CXXDestructorDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CXX_CONVERSION: - D = CXXConversionDecl::CreateDeserialized(Context, ID); + D = CXXConversionDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_ACCESS_SPEC: - D = AccessSpecDecl::CreateDeserialized(Context, ID); + D = AccessSpecDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_FRIEND: - D = FriendDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = FriendDecl::CreateDeserialized(Context, RawGlobalID, Record.readInt()); break; case DECL_FRIEND_TEMPLATE: - D = FriendTemplateDecl::CreateDeserialized(Context, ID); + D = FriendTemplateDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CLASS_TEMPLATE: - D = ClassTemplateDecl::CreateDeserialized(Context, ID); + D = ClassTemplateDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CLASS_TEMPLATE_SPECIALIZATION: - D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, ID); + D = ClassTemplateSpecializationDecl::CreateDeserialized(Context, + RawGlobalID); break; case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION: - D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); + D = ClassTemplatePartialSpecializationDecl::CreateDeserialized(Context, + RawGlobalID); break; case DECL_VAR_TEMPLATE: - D = VarTemplateDecl::CreateDeserialized(Context, ID); + D = VarTemplateDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_VAR_TEMPLATE_SPECIALIZATION: - D = VarTemplateSpecializationDecl::CreateDeserialized(Context, ID); + D = VarTemplateSpecializationDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION: - D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, ID); + D = VarTemplatePartialSpecializationDecl::CreateDeserialized(Context, + RawGlobalID); break; case DECL_FUNCTION_TEMPLATE: - D = FunctionTemplateDecl::CreateDeserialized(Context, ID); + D = FunctionTemplateDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_TEMPLATE_TYPE_PARM: { bool HasTypeConstraint = Record.readInt(); - D = TemplateTypeParmDecl::CreateDeserialized(Context, ID, + D = TemplateTypeParmDecl::CreateDeserialized(Context, RawGlobalID, HasTypeConstraint); break; } case DECL_NON_TYPE_TEMPLATE_PARM: { bool HasTypeConstraint = Record.readInt(); - D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, + D = NonTypeTemplateParmDecl::CreateDeserialized(Context, RawGlobalID, HasTypeConstraint); break; } case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK: { bool HasTypeConstraint = Record.readInt(); - D = NonTypeTemplateParmDecl::CreateDeserialized(Context, ID, - Record.readInt(), - HasTypeConstraint); + D = NonTypeTemplateParmDecl::CreateDeserialized( + Context, RawGlobalID, Record.readInt(), HasTypeConstraint); break; } case DECL_TEMPLATE_TEMPLATE_PARM: - D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID); + D = TemplateTemplateParmDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK: - D = TemplateTemplateParmDecl::CreateDeserialized(Context, ID, + D = TemplateTemplateParmDecl::CreateDeserialized(Context, RawGlobalID, Record.readInt()); break; case DECL_TYPE_ALIAS_TEMPLATE: - D = TypeAliasTemplateDecl::CreateDeserialized(Context, ID); + D = TypeAliasTemplateDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CONCEPT: - D = ConceptDecl::CreateDeserialized(Context, ID); + D = ConceptDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_REQUIRES_EXPR_BODY: - D = RequiresExprBodyDecl::CreateDeserialized(Context, ID); + D = RequiresExprBodyDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_STATIC_ASSERT: - D = StaticAssertDecl::CreateDeserialized(Context, ID); + D = StaticAssertDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_METHOD: - D = ObjCMethodDecl::CreateDeserialized(Context, ID); + D = ObjCMethodDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_INTERFACE: - D = ObjCInterfaceDecl::CreateDeserialized(Context, ID); + D = ObjCInterfaceDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_IVAR: - D = ObjCIvarDecl::CreateDeserialized(Context, ID); + D = ObjCIvarDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_PROTOCOL: - D = ObjCProtocolDecl::CreateDeserialized(Context, ID); + D = ObjCProtocolDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_AT_DEFS_FIELD: - D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, ID); + D = ObjCAtDefsFieldDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_CATEGORY: - D = ObjCCategoryDecl::CreateDeserialized(Context, ID); + D = ObjCCategoryDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_CATEGORY_IMPL: - D = ObjCCategoryImplDecl::CreateDeserialized(Context, ID); + D = ObjCCategoryImplDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_IMPLEMENTATION: - D = ObjCImplementationDecl::CreateDeserialized(Context, ID); + D = ObjCImplementationDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_COMPATIBLE_ALIAS: - D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, ID); + D = ObjCCompatibleAliasDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_PROPERTY: - D = ObjCPropertyDecl::CreateDeserialized(Context, ID); + D = ObjCPropertyDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_PROPERTY_IMPL: - D = ObjCPropertyImplDecl::CreateDeserialized(Context, ID); + D = ObjCPropertyImplDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_FIELD: - D = FieldDecl::CreateDeserialized(Context, ID); + D = FieldDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_INDIRECTFIELD: - D = IndirectFieldDecl::CreateDeserialized(Context, ID); + D = IndirectFieldDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_VAR: - D = VarDecl::CreateDeserialized(Context, ID); + D = VarDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_IMPLICIT_PARAM: - D = ImplicitParamDecl::CreateDeserialized(Context, ID); + D = ImplicitParamDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_PARM_VAR: - D = ParmVarDecl::CreateDeserialized(Context, ID); + D = ParmVarDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_DECOMPOSITION: - D = DecompositionDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = DecompositionDecl::CreateDeserialized(Context, RawGlobalID, + Record.readInt()); break; case DECL_BINDING: - D = BindingDecl::CreateDeserialized(Context, ID); + D = BindingDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_FILE_SCOPE_ASM: - D = FileScopeAsmDecl::CreateDeserialized(Context, ID); + D = FileScopeAsmDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_TOP_LEVEL_STMT_DECL: - D = TopLevelStmtDecl::CreateDeserialized(Context, ID); + D = TopLevelStmtDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_BLOCK: - D = BlockDecl::CreateDeserialized(Context, ID); + D = BlockDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_MS_PROPERTY: - D = MSPropertyDecl::CreateDeserialized(Context, ID); + D = MSPropertyDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_MS_GUID: - D = MSGuidDecl::CreateDeserialized(Context, ID); + D = MSGuidDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_UNNAMED_GLOBAL_CONSTANT: - D = UnnamedGlobalConstantDecl::CreateDeserialized(Context, ID); + D = UnnamedGlobalConstantDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_TEMPLATE_PARAM_OBJECT: - D = TemplateParamObjectDecl::CreateDeserialized(Context, ID); + D = TemplateParamObjectDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_CAPTURED: - D = CapturedDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = CapturedDecl::CreateDeserialized(Context, RawGlobalID, + Record.readInt()); break; case DECL_CXX_BASE_SPECIFIERS: Error("attempt to read a C++ base-specifier record as a declaration"); @@ -4064,62 +4073,66 @@ Decl *ASTReader::ReadDeclRecord(DeclID ID) { case DECL_IMPORT: // Note: last entry of the ImportDecl record is the number of stored source // locations. - D = ImportDecl::CreateDeserialized(Context, ID, Record.back()); + D = ImportDecl::CreateDeserialized(Context, RawGlobalID, Record.back()); break; case DECL_OMP_THREADPRIVATE: { Record.skipInts(1); unsigned NumChildren = Record.readInt(); Record.skipInts(1); - D = OMPThreadPrivateDecl::CreateDeserialized(Context, ID, NumChildren); + D = OMPThreadPrivateDecl::CreateDeserialized(Context, RawGlobalID, + NumChildren); break; } case DECL_OMP_ALLOCATE: { unsigned NumClauses = Record.readInt(); unsigned NumVars = Record.readInt(); Record.skipInts(1); - D = OMPAllocateDecl::CreateDeserialized(Context, ID, NumVars, NumClauses); + D = OMPAllocateDecl::CreateDeserialized(Context, RawGlobalID, NumVars, + NumClauses); break; } case DECL_OMP_REQUIRES: { unsigned NumClauses = Record.readInt(); Record.skipInts(2); - D = OMPRequiresDecl::CreateDeserialized(Context, ID, NumClauses); + D = OMPRequiresDecl::CreateDeserialized(Context, RawGlobalID, NumClauses); break; } case DECL_OMP_DECLARE_REDUCTION: - D = OMPDeclareReductionDecl::CreateDeserialized(Context, ID); + D = OMPDeclareReductionDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OMP_DECLARE_MAPPER: { unsigned NumClauses = Record.readInt(); Record.skipInts(2); - D = OMPDeclareMapperDecl::CreateDeserialized(Context, ID, NumClauses); + D = OMPDeclareMapperDecl::CreateDeserialized(Context, RawGlobalID, + NumClauses); break; } case DECL_OMP_CAPTUREDEXPR: - D = OMPCapturedExprDecl::CreateDeserialized(Context, ID); + D = OMPCapturedExprDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_PRAGMA_COMMENT: - D = PragmaCommentDecl::CreateDeserialized(Context, ID, Record.readInt()); + D = PragmaCommentDecl::CreateDeserialized(Context, RawGlobalID, + Record.readInt()); break; case DECL_PRAGMA_DETECT_MISMATCH: - D = PragmaDetectMismatchDecl::CreateDeserialized(Context, ID, + D = PragmaDetectMismatchDecl::CreateDeserialized(Context, RawGlobalID, Record.readInt()); break; case DECL_EMPTY: - D = EmptyDecl::CreateDeserialized(Context, ID); + D = EmptyDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_LIFETIME_EXTENDED_TEMPORARY: - D = LifetimeExtendedTemporaryDecl::CreateDeserialized(Context, ID); + D = LifetimeExtendedTemporaryDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_OBJC_TYPE_PARAM: - D = ObjCTypeParamDecl::CreateDeserialized(Context, ID); + D = ObjCTypeParamDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_HLSL_BUFFER: - D = HLSLBufferDecl::CreateDeserialized(Context, ID); + D = HLSLBufferDecl::CreateDeserialized(Context, RawGlobalID); break; case DECL_IMPLICIT_CONCEPT_SPECIALIZATION: - D = ImplicitConceptSpecializationDecl::CreateDeserialized(Context, ID, - Record.readInt()); + D = ImplicitConceptSpecializationDecl::CreateDeserialized( + Context, RawGlobalID, Record.readInt()); break; } @@ -4207,7 +4220,7 @@ void ASTReader::loadDeclUpdateRecords(PendingUpdateRecord &Record) { ProcessingUpdatesRAIIObj ProcessingUpdates(*this); DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID); - SmallVector<serialization::DeclID, 8> PendingLazySpecializationIDs; + SmallVector<GlobalDeclID, 8> PendingLazySpecializationIDs; if (UpdI != DeclUpdateOffsets.end()) { auto UpdateOffsets = std::move(UpdI->second); @@ -4473,8 +4486,9 @@ static void forAllLaterRedecls(DeclT *D, Fn F) { } } -void ASTDeclReader::UpdateDecl(Decl *D, - llvm::SmallVectorImpl<serialization::DeclID> &PendingLazySpecializationIDs) { +void ASTDeclReader::UpdateDecl( + Decl *D, + llvm::SmallVectorImpl<GlobalDeclID> &PendingLazySpecializationIDs) { while (Record.getIdx() < Record.size()) { switch ((DeclUpdateKind)Record.readInt()) { case UPD_CXX_ADDED_IMPLICIT_MEMBER: { diff --git a/clang/lib/Serialization/ASTReaderInternals.h b/clang/lib/Serialization/ASTReaderInternals.h index 25a46ddabcb707..49268ad5251dff 100644 --- a/clang/lib/Serialization/ASTReaderInternals.h +++ b/clang/lib/Serialization/ASTReaderInternals.h @@ -49,15 +49,15 @@ class ASTDeclContextNameLookupTrait { static const int MaxTables = 4; /// The lookup result is a list of global declaration IDs. - using data_type = SmallVector<DeclID, 4>; + using data_type = SmallVector<GlobalDeclID, 4>; struct data_type_builder { data_type &Data; - llvm::DenseSet<DeclID> Found; + llvm::DenseSet<GlobalDeclID> Found; data_type_builder(data_type &D) : Data(D) {} - void insert(DeclID ID) { + void insert(GlobalDeclID ID) { // Just use a linear scan unless we have more than a few IDs. if (Found.empty() && !Data.empty()) { if (Data.size() <= 4) { @@ -108,7 +108,7 @@ class ASTDeclContextNameLookupTrait { static void MergeDataInto(const data_type &From, data_type_builder &To) { To.Data.reserve(To.Data.size() + From.size()); - for (DeclID ID : From) + for (GlobalDeclID ID : From) To.insert(ID); } diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index 9c649b076bc67e..a1b340b252fb08 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -3884,7 +3884,8 @@ class ASTDeclContextNameLookupTrait { data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) { unsigned Start = DeclIDs.size(); - llvm::append_range(DeclIDs, FromReader); + DeclIDs.insert(DeclIDs.end(), DeclIDIterator(FromReader.begin()), + DeclIDIterator(FromReader.end())); return std::make_pair(Start, DeclIDs.size()); } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits