@@ -3357,20 +3357,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
33573357 " duplicate TYPE_OFFSET record in AST file" );
33583358 F.TypeOffsets = reinterpret_cast <const UnalignedUInt64 *>(Blob.data ());
33593359 F.LocalNumTypes = Record[0 ];
3360- unsigned LocalBaseTypeIndex = Record[1 ];
33613360 F.BaseTypeIndex = getTotalNumTypes ();
33623361
3363- if (F.LocalNumTypes > 0 ) {
3364- // Introduce the global -> local mapping for types within this module.
3365- GlobalTypeMap.insert (std::make_pair (getTotalNumTypes (), &F));
3366-
3367- // Introduce the local -> global mapping for types within this module.
3368- F.TypeRemap .insertOrReplace (
3369- std::make_pair (LocalBaseTypeIndex,
3370- F.BaseTypeIndex - LocalBaseTypeIndex));
3371-
3362+ if (F.LocalNumTypes > 0 )
33723363 TypesLoaded.resize (TypesLoaded.size () + F.LocalNumTypes );
3373- }
3364+
33743365 break ;
33753366 }
33763367
@@ -4033,7 +4024,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40334024 RemapBuilder PreprocessedEntityRemap (F.PreprocessedEntityRemap );
40344025 RemapBuilder SubmoduleRemap (F.SubmoduleRemap );
40354026 RemapBuilder SelectorRemap (F.SelectorRemap );
4036- RemapBuilder TypeRemap (F.TypeRemap );
40374027
40384028 auto &ImportedModuleVector = F.DependentModules ;
40394029 assert (ImportedModuleVector.empty ());
@@ -4069,8 +4059,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40694059 endian::readNext<uint32_t , llvm::endianness::little>(Data);
40704060 uint32_t SelectorIDOffset =
40714061 endian::readNext<uint32_t , llvm::endianness::little>(Data);
4072- uint32_t TypeIndexOffset =
4073- endian::readNext<uint32_t , llvm::endianness::little>(Data);
40744062
40754063 auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
40764064 RemapBuilder &Remap) {
@@ -4085,7 +4073,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40854073 PreprocessedEntityRemap);
40864074 mapOffset (SubmoduleIDOffset, OM->BaseSubmoduleID , SubmoduleRemap);
40874075 mapOffset (SelectorIDOffset, OM->BaseSelectorID , SelectorRemap);
4088- mapOffset (TypeIndexOffset, OM->BaseTypeIndex , TypeRemap);
40894076 }
40904077}
40914078
@@ -5064,12 +5051,12 @@ void ASTReader::InitializeContext() {
50645051
50655052 // Load the special types.
50665053 if (SpecialTypes.size () >= NumSpecialTypeIDs) {
5067- if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5054+ if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
50685055 if (!Context.CFConstantStringTypeDecl )
50695056 Context.setCFConstantStringType (GetType (String));
50705057 }
50715058
5072- if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5059+ if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
50735060 QualType FileType = GetType (File);
50745061 if (FileType.isNull ()) {
50755062 Error (" FILE type is NULL" );
@@ -5090,7 +5077,7 @@ void ASTReader::InitializeContext() {
50905077 }
50915078 }
50925079
5093- if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5080+ if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
50945081 QualType Jmp_bufType = GetType (Jmp_buf);
50955082 if (Jmp_bufType.isNull ()) {
50965083 Error (" jmp_buf type is NULL" );
@@ -5111,7 +5098,7 @@ void ASTReader::InitializeContext() {
51115098 }
51125099 }
51135100
5114- if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5101+ if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
51155102 QualType Sigjmp_bufType = GetType (Sigjmp_buf);
51165103 if (Sigjmp_bufType.isNull ()) {
51175104 Error (" sigjmp_buf type is NULL" );
@@ -5129,25 +5116,24 @@ void ASTReader::InitializeContext() {
51295116 }
51305117 }
51315118
5132- if (unsigned ObjCIdRedef
5133- = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5119+ if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
51345120 if (Context.ObjCIdRedefinitionType .isNull ())
51355121 Context.ObjCIdRedefinitionType = GetType (ObjCIdRedef);
51365122 }
51375123
5138- if (unsigned ObjCClassRedef
5139- = SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5124+ if (TypeID ObjCClassRedef =
5125+ SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
51405126 if (Context.ObjCClassRedefinitionType .isNull ())
51415127 Context.ObjCClassRedefinitionType = GetType (ObjCClassRedef);
51425128 }
51435129
5144- if (unsigned ObjCSelRedef
5145- = SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5130+ if (TypeID ObjCSelRedef =
5131+ SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
51465132 if (Context.ObjCSelRedefinitionType .isNull ())
51475133 Context.ObjCSelRedefinitionType = GetType (ObjCSelRedef);
51485134 }
51495135
5150- if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5136+ if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
51515137 QualType Ucontext_tType = GetType (Ucontext_t);
51525138 if (Ucontext_tType.isNull ()) {
51535139 Error (" ucontext_t type is NULL" );
@@ -6632,10 +6618,8 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
66326618}
66336619
66346620// / Get the correct cursor and offset for loading a type.
6635- ASTReader::RecordLocation ASTReader::TypeCursorForIndex (unsigned Index) {
6636- GlobalTypeMapType::iterator I = GlobalTypeMap.find (Index);
6637- assert (I != GlobalTypeMap.end () && " Corrupted global type map" );
6638- ModuleFile *M = I->second ;
6621+ ASTReader::RecordLocation ASTReader::TypeCursorForIndex (TypeID ID) {
6622+ auto [M, Index] = translateTypeIDToIndex (ID);
66396623 return RecordLocation (M, M->TypeOffsets [Index - M->BaseTypeIndex ].get () +
66406624 M->DeclsBlockStartOffset );
66416625}
@@ -6656,10 +6640,10 @@ static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
66566640// / routine actually reads the record corresponding to the type at the given
66576641// / location. It is a helper routine for GetType, which deals with reading type
66586642// / IDs.
6659- QualType ASTReader::readTypeRecord (unsigned Index ) {
6643+ QualType ASTReader::readTypeRecord (TypeID ID ) {
66606644 assert (ContextObj && " reading type with no AST context" );
66616645 ASTContext &Context = *ContextObj;
6662- RecordLocation Loc = TypeCursorForIndex (Index );
6646+ RecordLocation Loc = TypeCursorForIndex (ID );
66636647 BitstreamCursor &DeclsCursor = Loc.F ->DeclsCursor ;
66646648
66656649 // Keep track of where we are in the stream, then jump back there
@@ -7100,14 +7084,25 @@ TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
71007084 return TInfo;
71017085}
71027086
7087+ std::pair<ModuleFile *, unsigned >
7088+ ASTReader::translateTypeIDToIndex (serialization::TypeID ID) const {
7089+ unsigned Index =
7090+ (ID & llvm::maskTrailingOnes<TypeID>(32 )) >> Qualifiers::FastWidth;
7091+
7092+ ModuleFile *OwningModuleFile = getOwningModuleFile (ID);
7093+ assert (OwningModuleFile &&
7094+ " untranslated type ID or local type ID shouldn't be in TypesLoaded" );
7095+ return {OwningModuleFile, OwningModuleFile->BaseTypeIndex + Index};
7096+ }
7097+
71037098QualType ASTReader::GetType (TypeID ID) {
71047099 assert (ContextObj && " reading type with no AST context" );
71057100 ASTContext &Context = *ContextObj;
71067101
71077102 unsigned FastQuals = ID & Qualifiers::FastMask;
7108- unsigned Index = ID >> Qualifiers::FastWidth;
71097103
7110- if (Index < NUM_PREDEF_TYPE_IDS) {
7104+ if (uint64_t Index = ID >> Qualifiers::FastWidth;
7105+ Index < NUM_PREDEF_TYPE_IDS) {
71117106 QualType T;
71127107 switch ((PredefinedTypeIDs)Index) {
71137108 case PREDEF_TYPE_LAST_ID:
@@ -7376,10 +7371,11 @@ QualType ASTReader::GetType(TypeID ID) {
73767371 return T.withFastQualifiers (FastQuals);
73777372 }
73787373
7379- Index -= NUM_PREDEF_TYPE_IDS;
7374+ unsigned Index = translateTypeIDToIndex (ID).second ;
7375+
73807376 assert (Index < TypesLoaded.size () && " Type index out-of-range" );
73817377 if (TypesLoaded[Index].isNull ()) {
7382- TypesLoaded[Index] = readTypeRecord (Index );
7378+ TypesLoaded[Index] = readTypeRecord (ID );
73837379 if (TypesLoaded[Index].isNull ())
73847380 return QualType ();
73857381
@@ -7392,27 +7388,28 @@ QualType ASTReader::GetType(TypeID ID) {
73927388 return TypesLoaded[Index].withFastQualifiers (FastQuals);
73937389}
73947390
7395- QualType ASTReader::getLocalType (ModuleFile &F, unsigned LocalID) {
7391+ QualType ASTReader::getLocalType (ModuleFile &F, TypeID LocalID) {
73967392 return GetType (getGlobalTypeID (F, LocalID));
73977393}
73987394
7399- serialization::TypeID
7400- ASTReader::getGlobalTypeID (ModuleFile &F, unsigned LocalID) const {
7401- unsigned FastQuals = LocalID & Qualifiers::FastMask;
7402- unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7403-
7404- if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7395+ serialization::TypeID ASTReader::getGlobalTypeID (ModuleFile &F,
7396+ TypeID LocalID) const {
7397+ if ((LocalID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS)
74057398 return LocalID;
74067399
74077400 if (!F.ModuleOffsetMap .empty ())
74087401 ReadModuleOffsetMap (F);
74097402
7410- ContinuousRangeMap<uint32_t , int , 2 >::iterator I
7411- = F.TypeRemap .find (LocalIndex - NUM_PREDEF_TYPE_IDS);
7412- assert (I != F.TypeRemap .end () && " Invalid index into type index remap" );
7403+ unsigned ModuleFileIndex = LocalID >> 32 ;
7404+ LocalID &= llvm::maskTrailingOnes<TypeID>(32 );
74137405
7414- unsigned GlobalIndex = LocalIndex + I->second ;
7415- return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7406+ if (ModuleFileIndex == 0 )
7407+ LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
7408+
7409+ ModuleFile &MF =
7410+ ModuleFileIndex ? *F.DependentModules [ModuleFileIndex - 1 ] : F;
7411+ ModuleFileIndex = MF.Index + 1 ;
7412+ return ((uint64_t )ModuleFileIndex << 32 ) | LocalID;
74167413}
74177414
74187415TemplateArgumentLocInfo
@@ -7650,6 +7647,16 @@ ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
76507647 return &getModuleManager ()[ModuleFileIndex - 1 ];
76517648}
76527649
7650+ ModuleFile *ASTReader::getOwningModuleFile (TypeID ID) const {
7651+ if (ID < NUM_PREDEF_TYPE_IDS)
7652+ return nullptr ;
7653+
7654+ uint64_t ModuleFileIndex = ID >> 32 ;
7655+ assert (ModuleFileIndex && " Untranslated Local Decl?" );
7656+
7657+ return &getModuleManager ()[ModuleFileIndex - 1 ];
7658+ }
7659+
76537660ModuleFile *ASTReader::getOwningModuleFile (const Decl *D) const {
76547661 if (!D->isFromASTFile ())
76557662 return nullptr ;
@@ -8167,7 +8174,6 @@ LLVM_DUMP_METHOD void ASTReader::dump() {
81678174 llvm::errs () << " *** PCH/ModuleFile Remappings:\n " ;
81688175 dumpModuleIDMap (" Global bit offset map" , GlobalBitOffsetsMap);
81698176 dumpModuleIDMap (" Global source location entry map" , GlobalSLocEntryMap);
8170- dumpModuleIDMap (" Global type map" , GlobalTypeMap);
81718177 dumpModuleIDMap (" Global macro map" , GlobalMacroMap);
81728178 dumpModuleIDMap (" Global submodule map" , GlobalSubmoduleMap);
81738179 dumpModuleIDMap (" Global selector map" , GlobalSelectorMap);
0 commit comments