/// \brief The first ID number we can use for our own declarations.
pch::DeclID FirstDeclID;
+ /// \brief The decl ID that will be assigned to the next new decl.
+ pch::DeclID NextDeclID;
+
/// \brief Map that provides the ID numbers of each declaration within
/// the output stream, as well as those deserialized from a chained PCH.
///
/// \brief The first ID number we can use for our own types.
pch::TypeID FirstTypeID;
+ /// \brief The type ID that will be assigned to the next new type.
+ pch::TypeID NextTypeID;
+
/// \brief Map that provides the ID numbers of each type within the
/// output stream, plus those deserialized from a chained PCH.
///
/// the type's ID.
std::vector<uint32_t> TypeOffsets;
- /// \brief The type ID that will be assigned to the next new type.
- pch::TypeID NextTypeID;
+ /// \brief The first ID number we can use for our own identifiers.
+ pch::IdentID FirstIdentID;
+
+ /// \brief The identifier ID that will be assigned to the next new identifier.
+ pch::IdentID NextIdentID;
/// \brief Map that provides the ID numbers of each identifier in
/// the output stream.
unsigned getParmVarDeclAbbrev() const { return ParmVarDeclAbbrev; }
+ bool hasChain() const { return Chain; }
+
// PCHDeserializationListener implementation
+ void IdentifierRead(pch::IdentID ID, IdentifierInfo *II);
void TypeRead(pch::TypeID ID, QualType T);
void DeclRead(pch::DeclID ID, const Decl *D);
};
// "stat"), but IdentifierResolver::AddDeclToIdentifierChain()
// adds declarations to the end of the list (so we need to see the
// struct "status" before the function "status").
+ // Only emit declarations that aren't from a chained PCH, though.
llvm::SmallVector<Decl *, 16> Decls(IdentifierResolver::begin(II),
IdentifierResolver::end());
for (llvm::SmallVector<Decl *, 16>::reverse_iterator D = Decls.rbegin(),
DEnd = Decls.rend();
D != DEnd; ++D)
- clang::io::Emit32(Out, Writer.getDeclID(*D));
+ if (!Writer.hasChain() || (*D)->getPCHLevel() == 0)
+ clang::io::Emit32(Out, Writer.getDeclID(*D));
}
};
} // end anonymous namespace
ID != IDEnd; ++ID)
getIdentifierRef(ID->second);
- // Create the on-disk hash table representation.
- IdentifierOffsets.resize(IdentifierIDs.size());
+ // Create the on-disk hash table representation. We only store offsets
+ // for identifiers that appear here for the first time.
+ IdentifierOffsets.resize(NextIdentID - FirstIdentID);
for (llvm::DenseMap<const IdentifierInfo *, pch::IdentID>::iterator
ID = IdentifierIDs.begin(), IDEnd = IdentifierIDs.end();
ID != IDEnd; ++ID) {
assert(ID->first && "NULL identifier in identifier table");
- Generator.insert(ID->first, ID->second);
+ // FIXME: Right now, we only write identifiers that are new to this file.
+ // We need to write older identifiers that changed too, though.
+ if (ID->second >= FirstIdentID)
+ Generator.insert(ID->first, ID->second);
}
// Create the on-disk hash table in a buffer.
/// \brief Note that the identifier II occurs at the given offset
/// within the identifier table.
void PCHWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
- IdentifierOffsets[IdentifierIDs[II] - 1] = Offset;
+ pch::IdentID ID = IdentifierIDs[II];
+ // Only store offsets new to this PCH file. Other identifier names are looked
+ // up earlier in the chain and thus don't need an offset.
+ if (ID >= FirstIdentID)
+ IdentifierOffsets[ID - FirstIdentID] = Offset;
}
/// \brief Note that the selector Sel occurs at the given offset
PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain)
: Stream(Stream), Chain(Chain), FirstDeclID(1),
- FirstTypeID(pch::NUM_PREDEF_TYPE_IDS),
+ FirstTypeID(pch::NUM_PREDEF_TYPE_IDS), FirstIdentID(1),
CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) {
if (Chain) {
Chain->setDeserializationListener(this);
FirstDeclID += Chain->getTotalNumDecls();
FirstTypeID += Chain->getTotalNumTypes();
+ FirstIdentID += Chain->getTotalNumIdentifiers();
}
+ NextDeclID = FirstDeclID;
NextTypeID = FirstTypeID;
+ NextIdentID = FirstIdentID;
}
void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls,
// The translation unit is the first declaration we'll emit.
DeclIDs[Context.getTranslationUnitDecl()] = 1;
+ ++NextDeclID;
DeclTypesToEmit.push(Context.getTranslationUnitDecl());
// Make sure that we emit IdentifierInfos (and any attached
// We don't start with the translation unit, but with its decls that
// don't come from the other PCH.
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
+ // The TU was loaded before we managed to register ourselves as a listener.
+ // Thus we need to add it manually.
+ DeclIDs[TU] = 1;
// FIXME: We don't want to iterate over everything here, because it needlessly
// deserializes the entire original PCH. Instead we only want to iterate over
// the stuff that's already there.
// FIXME: Preprocessor
// FIXME: Method pool
- // FIXME: Identifier table
+ WriteIdentifierTable(PP);
WriteTypeDeclOffsets();
// FIXME: External unnamed definitions
// FIXME: Tentative definitions
pch::IdentID &ID = IdentifierIDs[II];
if (ID == 0)
- ID = IdentifierIDs.size();
+ ID = NextIdentID++;
return ID;
}
if (ID == 0) {
// We haven't seen this declaration before. Give it a new ID and
// enqueue it in the list of declarations to emit.
- ID = DeclIDs.size();
+ ID = NextDeclID++;
DeclTypesToEmit.push(const_cast<Decl *>(D));
}
AddSourceRange(Base.getSourceRange(), Record);
}
+void PCHWriter::IdentifierRead(pch::IdentID ID, IdentifierInfo *II) {
+ IdentifierIDs[II] = ID;
+}
+
void PCHWriter::TypeRead(pch::TypeID ID, QualType T) {
TypeIDs[T] = ID;
}