[serialization] Introduce ModuleID, with codes for special modules.

ModuleID is compatible with IdentifierID, but uses 0 to mean “the builtin module”
and 1 to mean “the current module”. Anything else is a top-level module name,
as an identifier. As an implementation detail, 1 is now never a valid IdentifierID.
(0 remains “the empty string”.)

Using this, simplify the encoding of the owner of a conformance.

Swift SVN r9944
This commit is contained in:
Jordan Rose
2013-11-04 23:50:46 +00:00
parent e001667ac2
commit 63731584b6
5 changed files with 70 additions and 40 deletions

View File

@@ -176,10 +176,12 @@ IdentifierID Serializer::addIdentifierRef(Identifier ident) {
}
IdentifierID Serializer::addModuleRef(const Module *M) {
assert(M != TU && "cannot form cross-reference to module being serialized");
if (M == TU->Ctx.TheBuiltinModule)
return 0;
return BUILTIN_MODULE_ID;
if (M == TU)
return CURRENT_MODULE_ID;
assert(!M->Name.empty());
return addIdentifierRef(M->Name);
}
@@ -626,20 +628,19 @@ Serializer::encodeUnderlyingConformance(const ProtocolConformance *conformance,
IdentifierID &moduleID) {
bool append = !isa<NormalProtocolConformance>(conformance);
if (append) {
// Encode the type in typeID. Set moduleID to 0 to indicate that the
// underlying conformance will follow.
// Encode the type in typeID. Set moduleID to BUILTIN_MODULE_ID to indicate
// that the underlying conformance will follow. This is safe because there
// should never be any conformances in the Builtin module.
typeID = addTypeRef(conformance->getType());
moduleID = 0;
moduleID = serialization::BUILTIN_MODULE_ID;
} else {
typeID = addDeclRef(conformance->getType()->getAnyNominal());
assert(typeID && "Missing nominal type for specialized conformance");
// '0' is a sentinel for a trailing underlying conformance record.
// Use '1' to mean 'this module', and add 2 to any other module reference.
if (conformance->getContainingModule() == TU)
moduleID = 1;
else
moduleID = addModuleRef(conformance->getContainingModule()) + 2;
// BUILTIN_MODULE_ID is a sentinel for a trailing underlying conformance
// record.
moduleID = addModuleRef(conformance->getContainingModule());
assert(moduleID != serialization::BUILTIN_MODULE_ID);
}
return append;
@@ -2016,8 +2017,6 @@ void Serializer::writeTranslationUnit(const TranslationUnit *TU,
writeAllDeclsAndTypes();
writeAllIdentifiers();
//
{
BCBlockRAII restoreBlock(Out, INDEX_BLOCK_ID, 3);