From 95eab176f51beed44a9bc14c0dcdd37844b23740 Mon Sep 17 00:00:00 2001 From: Douglas Gregor Date: Thu, 28 Jul 2011 20:55:49 +0000 Subject: [PATCH] Teach the ASTReader to perform local and global mapping of identifier IDs properly, although the mapping itself is still trivial. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@136391 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Serialization/ASTBitCodes.h | 6 +++ include/clang/Serialization/ASTReader.h | 17 ++++--- lib/Serialization/ASTReader.cpp | 62 ++++++++++++++--------- lib/Serialization/ASTReaderDecl.cpp | 2 +- lib/Serialization/ASTReaderStmt.cpp | 13 +++-- 5 files changed, 62 insertions(+), 38 deletions(-) diff --git a/include/clang/Serialization/ASTBitCodes.h b/include/clang/Serialization/ASTBitCodes.h index a01bf6235a..4e7b0faebf 100644 --- a/include/clang/Serialization/ASTBitCodes.h +++ b/include/clang/Serialization/ASTBitCodes.h @@ -47,6 +47,12 @@ namespace clang { /// should be increased. const unsigned VERSION_MINOR = 0; + /// \brief An ID number that refers to an identifier in an AST file. + /// + /// The ID numbers of identifiers are consecutive (in order of discovery) + /// and start at 1. 0 is reserved for NULL. + typedef uint32_t IdentifierID; + /// \brief An ID number that refers to a declaration in an AST file. /// /// The ID numbers of declarations are consecutive (in order of diff --git a/include/clang/Serialization/ASTReader.h b/include/clang/Serialization/ASTReader.h index bd2115039f..319c231cfa 100644 --- a/include/clang/Serialization/ASTReader.h +++ b/include/clang/Serialization/ASTReader.h @@ -1424,20 +1424,21 @@ public: /// \brief Report a diagnostic. DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID); - IdentifierInfo *DecodeIdentifierInfo(unsigned Idx); + IdentifierInfo *DecodeIdentifierInfo(serialization::IdentifierID ID); - IdentifierInfo *GetIdentifierInfo(const RecordData &Record, unsigned &Idx) { - return DecodeIdentifierInfo(Record[Idx++]); + IdentifierInfo *GetIdentifierInfo(Module &M, const RecordData &Record, + unsigned &Idx) { + return DecodeIdentifierInfo(getGlobalIdentifierID(M, Record[Idx++])); } - virtual IdentifierInfo *GetIdentifier(unsigned ID) { + virtual IdentifierInfo *GetIdentifier(serialization::IdentifierID ID) { return DecodeIdentifierInfo(ID); } - unsigned getGlobalIdentifierID(Module &M, unsigned LocalID) { - // FIXME: Remap local -> global identifier IDs - return LocalID; - } + IdentifierInfo *getLocalIdentifier(Module &M, unsigned LocalID); + + serialization::IdentifierID getGlobalIdentifierID(Module &M, + unsigned LocalID); /// \brief Read the source location entry with index ID. virtual bool ReadSLocEntry(int ID); diff --git a/lib/Serialization/ASTReader.cpp b/lib/Serialization/ASTReader.cpp index acd31c9a64..1c9c106f1d 100644 --- a/lib/Serialization/ASTReader.cpp +++ b/lib/Serialization/ASTReader.cpp @@ -523,7 +523,7 @@ public: SelectorTable &SelTable = Reader.getContext()->Selectors; unsigned N = ReadUnalignedLE16(d); IdentifierInfo *FirstII - = Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); + = Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); if (N == 0) return SelTable.getNullarySelector(FirstII); else if (N == 1) @@ -532,7 +532,7 @@ public: SmallVector Args; Args.push_back(FirstII); for (unsigned I = 1; I != N; ++I) - Args.push_back(Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d))); + Args.push_back(Reader.getLocalIdentifier(F, ReadUnalignedLE32(d))); return SelTable.getSelector(N, Args.data()); } @@ -877,7 +877,7 @@ public: Key.Kind = (DeclarationName::NameKind)*d++; switch (Key.Kind) { case DeclarationName::Identifier: - Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); + Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); break; case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: @@ -894,7 +894,7 @@ public: Key.Data = *d++; // OverloadedOperatorKind break; case DeclarationName::CXXLiteralOperatorName: - Key.Data = (uint64_t)Reader.DecodeIdentifierInfo(ReadUnalignedLE32(d)); + Key.Data = (uint64_t)Reader.getLocalIdentifier(F, ReadUnalignedLE32(d)); break; case DeclarationName::CXXUsingDirective: break; @@ -1462,7 +1462,7 @@ PreprocessedEntity *ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) { if (Macro) return 0; - IdentifierInfo *II = DecodeIdentifierInfo(Record[0]); + IdentifierInfo *II = getLocalIdentifier(F, Record[0]); if (II == 0) { Error("macro must have a name in AST file"); return 0; @@ -1483,7 +1483,7 @@ PreprocessedEntity *ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) { unsigned NumArgs = Record[5]; NextIndex = 6 + NumArgs; for (unsigned i = 0; i != NumArgs; ++i) - MacroArgs.push_back(DecodeIdentifierInfo(Record[6+i])); + MacroArgs.push_back(getLocalIdentifier(F, Record[6+i])); // Install function-like macro info. MI->setIsFunctionLike(); @@ -1519,7 +1519,7 @@ PreprocessedEntity *ASTReader::ReadMacroRecord(Module &F, uint64_t Offset) { Tok.startToken(); Tok.setLocation(ReadSourceLocation(F, Record[0])); Tok.setLength(Record[1]); - if (IdentifierInfo *II = DecodeIdentifierInfo(Record[2])) + if (IdentifierInfo *II = getLocalIdentifier(F, Record[2])) Tok.setIdentifierInfo(II); Tok.setKind((tok::TokenKind)Record[3]); Tok.setFlag((Token::TokenFlags)Record[4]); @@ -1570,7 +1570,7 @@ PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) { return PE; MacroExpansion *ME = - new (PPRec) MacroExpansion(DecodeIdentifierInfo(Record[3]), + new (PPRec) MacroExpansion(getLocalIdentifier(F, Record[3]), SourceRange(ReadSourceLocation(F, Record[1]), ReadSourceLocation(F, Record[2])), getMacroDefinition(Record[4])); @@ -1589,7 +1589,7 @@ PreprocessedEntity *ASTReader::LoadPreprocessedEntity(Module &F) { // Decode the identifier info and then check again; if the macro is // still defined and associated with the identifier, - IdentifierInfo *II = DecodeIdentifierInfo(Record[4]); + IdentifierInfo *II = getLocalIdentifier(F, Record[4]); if (!MacroDefinitionsLoaded[Record[1] - 1]) { MacroDefinition *MD = new (PPRec) MacroDefinition(II, @@ -1647,6 +1647,8 @@ namespace { /// inode numbers, so that the search can cope with non-normalized path names /// and symlinks. class HeaderFileInfoTrait { + ASTReader &Reader; + Module &M; HeaderSearch *HS; const char *FrameworkStrings; const char *SearchPath; @@ -1671,10 +1673,11 @@ namespace { typedef HeaderFileInfo data_type; - HeaderFileInfoTrait(HeaderSearch *HS, + HeaderFileInfoTrait(ASTReader &Reader, Module &M, HeaderSearch *HS, const char *FrameworkStrings, const char *SearchPath = 0) - : HS(HS), FrameworkStrings(FrameworkStrings), SearchPath(SearchPath) { } + : Reader(Reader), M(M), HS(HS), FrameworkStrings(FrameworkStrings), + SearchPath(SearchPath) { } static unsigned ComputeHash(const char *path) { return llvm::HashString(llvm::sys::path::filename(path)); @@ -1721,7 +1724,7 @@ namespace { HFI.Resolved = (Flags >> 1) & 0x01; HFI.IndexHeaderMapHeader = Flags & 0x01; HFI.NumIncludes = ReadUnalignedLE16(d); - HFI.ControllingMacroID = ReadUnalignedLE32(d); + HFI.ControllingMacroID = Reader.getGlobalDeclID(M, ReadUnalignedLE32(d)); if (unsigned FrameworkOffset = ReadUnalignedLE32(d)) { // The framework offset is 1 greater than the actual offset, // since 0 is used as an indicator for "no framework name". @@ -1795,7 +1798,7 @@ void ASTReader::ReadDefinedMacros() { case PP_MACRO_OBJECT_LIKE: case PP_MACRO_FUNCTION_LIKE: - DecodeIdentifierInfo(Record[0]); + getLocalIdentifier(**I, Record[0]); break; case PP_TOKEN: @@ -2492,7 +2495,8 @@ ASTReader::ReadASTBlock(Module &F) { = HeaderFileInfoLookupTable::Create( (const unsigned char *)F.HeaderFileInfoTableData + Record[0], (const unsigned char *)F.HeaderFileInfoTableData, - HeaderFileInfoTrait(PP? &PP->getHeaderSearchInfo() : 0, + HeaderFileInfoTrait(*this, F, + PP? &PP->getHeaderSearchInfo() : 0, BlobStart + Record[2])); if (PP) PP->getHeaderSearchInfo().SetExternalSource(this); @@ -3142,7 +3146,7 @@ HeaderFileInfo ASTReader::GetHeaderFileInfo(const FileEntry *FE) { for (ModuleIterator I = ModuleMgr.begin(), E = ModuleMgr.end(); I != E; ++I) { Module &F = *(*I); - HeaderFileInfoTrait Trait(&PP->getHeaderSearchInfo(), + HeaderFileInfoTrait Trait(*this, F, &PP->getHeaderSearchInfo(), F.HeaderFileFrameworkStrings, FE->getName()); @@ -3565,7 +3569,7 @@ QualType ASTReader::readTypeRecord(unsigned Index) { unsigned Idx = 0; ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); - const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); + const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); QualType Canon = readType(*Loc.F, Record, Idx); if (!Canon.isNull()) Canon = Context->getCanonicalType(Canon); @@ -3576,7 +3580,7 @@ QualType ASTReader::readTypeRecord(unsigned Index) { unsigned Idx = 0; ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; NestedNameSpecifier *NNS = ReadNestedNameSpecifier(*Loc.F, Record, Idx); - const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); + const IdentifierInfo *Name = this->GetIdentifierInfo(*Loc.F, Record, Idx); unsigned NumArgs = Record[Idx++]; SmallVector Args; Args.reserve(NumArgs); @@ -4667,7 +4671,7 @@ void ASTReader::LoadSelector(Selector Sel) { ReadMethodPool(Sel); } -void ASTReader::SetIdentifierInfo(unsigned ID, IdentifierInfo *II) { +void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) { assert(ID && "Non-zero identifier ID required"); assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range"); IdentifiersLoaded[ID - 1] = II; @@ -4722,7 +4726,7 @@ ASTReader::SetGloballyVisibleDecls(IdentifierInfo *II, } } -IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) { +IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) { if (ID == 0) return 0; @@ -4757,6 +4761,15 @@ IdentifierInfo *ASTReader::DecodeIdentifierInfo(unsigned ID) { return IdentifiersLoaded[ID]; } +IdentifierInfo *ASTReader::getLocalIdentifier(Module &M, unsigned LocalID) { + return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID)); +} + +IdentifierID ASTReader::getGlobalIdentifierID(Module &M, unsigned LocalID) { + // FIXME: Perform local-to-global remapping + return LocalID; +} + bool ASTReader::ReadSLocEntry(int ID) { return ReadSLocEntryRecord(ID) != Success; } @@ -4807,7 +4820,7 @@ ASTReader::ReadDeclarationName(Module &F, DeclarationName::NameKind Kind = (DeclarationName::NameKind)Record[Idx++]; switch (Kind) { case DeclarationName::Identifier: - return DeclarationName(GetIdentifierInfo(Record, Idx)); + return DeclarationName(GetIdentifierInfo(F, Record, Idx)); case DeclarationName::ObjCZeroArgSelector: case DeclarationName::ObjCOneArgSelector: @@ -4832,7 +4845,7 @@ ASTReader::ReadDeclarationName(Module &F, case DeclarationName::CXXLiteralOperatorName: return Context->DeclarationNames.getCXXLiteralOperatorName( - GetIdentifierInfo(Record, Idx)); + GetIdentifierInfo(F, Record, Idx)); case DeclarationName::CXXUsingDirective: return DeclarationName::getUsingDirectiveName(); @@ -4924,7 +4937,8 @@ ASTReader::ReadTemplateName(Module &F, const RecordData &Record, NestedNameSpecifier *NNS = ReadNestedNameSpecifier(F, Record, Idx); if (Record[Idx++]) // isIdentifier return Context->getDependentTemplateName(NNS, - GetIdentifierInfo(Record, Idx)); + GetIdentifierInfo(F, Record, + Idx)); return Context->getDependentTemplateName(NNS, (OverloadedOperatorKind)Record[Idx++]); } @@ -5146,7 +5160,7 @@ ASTReader::ReadNestedNameSpecifier(Module &F, = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; switch (Kind) { case NestedNameSpecifier::Identifier: { - IdentifierInfo *II = GetIdentifierInfo(Record, Idx); + IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); NNS = NestedNameSpecifier::Create(*Context, Prev, II); break; } @@ -5195,7 +5209,7 @@ ASTReader::ReadNestedNameSpecifierLoc(Module &F, const RecordData &Record, = (NestedNameSpecifier::SpecifierKind)Record[Idx++]; switch (Kind) { case NestedNameSpecifier::Identifier: { - IdentifierInfo *II = GetIdentifierInfo(Record, Idx); + IdentifierInfo *II = GetIdentifierInfo(F, Record, Idx); SourceRange Range = ReadSourceRange(F, Record, Idx); Builder.Extend(*Context, II, Range.getBegin(), Range.getEnd()); break; diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index 289b6c835e..5b36743992 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -650,7 +650,7 @@ void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) { void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) { VisitObjCImplDecl(D); - D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx)); + D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx)); } void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) { diff --git a/lib/Serialization/ASTReaderStmt.cpp b/lib/Serialization/ASTReaderStmt.cpp index 7a3c589c21..5e8262b947 100644 --- a/lib/Serialization/ASTReaderStmt.cpp +++ b/lib/Serialization/ASTReaderStmt.cpp @@ -287,7 +287,7 @@ void ASTStmtReader::VisitAsmStmt(AsmStmt *S) { SmallVector Constraints; SmallVector Exprs; for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) { - Names.push_back(Reader.GetIdentifierInfo(Record, Idx)); + Names.push_back(Reader.GetIdentifierInfo(F, Record, Idx)); Constraints.push_back(cast_or_null(Reader.ReadSubStmt())); Exprs.push_back(Reader.ReadSubStmt()); } @@ -440,7 +440,10 @@ void ASTStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) { break; case Node::Identifier: - E->setComponent(I, Node(Start, Reader.GetIdentifier(Record[Idx++]), End)); + E->setComponent(I, + Node(Start, + Reader.GetIdentifierInfo(F, Record, Idx), + End)); break; case Node::Base: { @@ -593,7 +596,7 @@ void ASTStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { void ASTStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) { VisitExpr(E); E->setBase(Reader.ReadSubExpr()); - E->setAccessor(Reader.GetIdentifierInfo(Record, Idx)); + E->setAccessor(Reader.GetIdentifierInfo(F, Record, Idx)); E->setAccessorLoc(ReadSourceLocation(Record, Idx)); } @@ -650,7 +653,7 @@ void ASTStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) { } case DESIG_FIELD_NAME: { - const IdentifierInfo *Name = Reader.GetIdentifierInfo(Record, Idx); + const IdentifierInfo *Name = Reader.GetIdentifierInfo(F, Record, Idx); SourceLocation DotLoc = ReadSourceLocation(Record, Idx); SourceLocation FieldLoc @@ -1128,7 +1131,7 @@ void ASTStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { E->ColonColonLoc = ReadSourceLocation(Record, Idx); E->TildeLoc = ReadSourceLocation(Record, Idx); - IdentifierInfo *II = Reader.GetIdentifierInfo(Record, Idx); + IdentifierInfo *II = Reader.GetIdentifierInfo(F, Record, Idx); if (II) E->setDestroyedType(II, ReadSourceLocation(Record, Idx)); else