Skip to content

Commit 6b99c81

Browse files
committed
[Serialization] ProtocolLayout: Serialize inherited protocols instead of types
1 parent 1df8ef7 commit 6b99c81

File tree

5 files changed

+74
-21
lines changed

5 files changed

+74
-21
lines changed

lib/Serialization/DeclTypeRecordNodes.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ TRAILING_INFO(CONDITIONAL_SUBSTITUTION_COND)
214214

215215
OTHER(LIFETIME_DEPENDENCE, 158)
216216

217+
TRAILING_INFO(INHERITED_PROTOCOLS)
218+
217219
#ifndef DECL_ATTR
218220
#define DECL_ATTR(NAME, CLASS, OPTIONS, CODE) RECORD_VAL(CLASS##_DECL_ATTR, 180+CODE)
219221
#endif

lib/Serialization/Deserialization.cpp

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,6 +1717,35 @@ ModuleFile::getSubstitutionMapChecked(serialization::SubstitutionMapID id) {
17171717
return substitutions;
17181718
}
17191719

1720+
bool ModuleFile::readInheritedProtocols(
1721+
SmallVectorImpl<ProtocolDecl *> &inherited) {
1722+
using namespace decls_block;
1723+
1724+
BCOffsetRAII lastRecordOffset(DeclTypeCursor);
1725+
1726+
llvm::BitstreamEntry entry =
1727+
fatalIfUnexpected(DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
1728+
if (entry.Kind != llvm::BitstreamEntry::Record)
1729+
return false;
1730+
1731+
SmallVector<uint64_t, 8> scratch;
1732+
unsigned recordID =
1733+
fatalIfUnexpected(DeclTypeCursor.readRecord(entry.ID, scratch));
1734+
if (recordID != INHERITED_PROTOCOLS)
1735+
return false;
1736+
1737+
lastRecordOffset.reset();
1738+
1739+
ArrayRef<uint64_t> inheritedIDs;
1740+
InheritedProtocolsLayout::readRecord(scratch, inheritedIDs);
1741+
1742+
llvm::transform(inheritedIDs, std::back_inserter(inherited),
1743+
[&](uint64_t protocolID) {
1744+
return cast<ProtocolDecl>(getDecl(protocolID));
1745+
});
1746+
return true;
1747+
}
1748+
17201749
bool ModuleFile::readDefaultWitnessTable(ProtocolDecl *proto) {
17211750
using namespace decls_block;
17221751

@@ -4425,21 +4454,19 @@ class DeclDeserializer {
44254454
bool isImplicit, isClassBounded, isObjC, hasSelfOrAssocTypeRequirements;
44264455
TypeID superclassID;
44274456
uint8_t rawAccessLevel;
4428-
unsigned numInheritedTypes;
4429-
ArrayRef<uint64_t> rawInheritedAndDependencyIDs;
4457+
ArrayRef<uint64_t> dependencyIDs;
44304458

44314459
decls_block::ProtocolLayout::readRecord(scratch, nameID, contextID,
44324460
isImplicit, isClassBounded, isObjC,
44334461
hasSelfOrAssocTypeRequirements,
44344462
superclassID,
4435-
rawAccessLevel, numInheritedTypes,
4436-
rawInheritedAndDependencyIDs);
4463+
rawAccessLevel,
4464+
dependencyIDs);
44374465

44384466
Identifier name = MF.getIdentifier(nameID);
44394467
PrettySupplementalDeclNameTrace trace(name);
44404468

4441-
for (TypeID dependencyID :
4442-
rawInheritedAndDependencyIDs.slice(numInheritedTypes)) {
4469+
for (TypeID dependencyID : dependencyIDs) {
44434470
auto dependency = MF.getTypeChecked(dependencyID);
44444471
if (!dependency) {
44454472
return llvm::make_error<TypeError>(
@@ -4469,14 +4496,17 @@ class DeclDeserializer {
44694496
else
44704497
return MF.diagnoseFatal();
44714498

4499+
SmallVector<ProtocolDecl *, 2> inherited;
4500+
if (!MF.readInheritedProtocols(inherited))
4501+
return MF.diagnoseFatal();
4502+
ctx.evaluator.cacheOutput(InheritedProtocolsRequest{proto},
4503+
ctx.AllocateCopy(inherited));
4504+
44724505
auto genericParams = MF.maybeReadGenericParams(DC);
44734506
assert(genericParams && "protocol with no generic parameters?");
44744507
ctx.evaluator.cacheOutput(GenericParamListRequest{proto},
44754508
std::move(genericParams));
44764509

4477-
handleInherited(proto,
4478-
rawInheritedAndDependencyIDs.slice(0, numInheritedTypes));
4479-
44804510
if (isImplicit)
44814511
proto->setImplicit();
44824512
proto->setIsObjC(isObjC);

lib/Serialization/ModuleFile.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ class ModuleFile
538538
SmallVectorImpl<AssociatedTypeDecl *> &assocTypes,
539539
llvm::BitstreamCursor &Cursor);
540540

541+
/// Read a list of the protocol declarations inherited by another protocol.
542+
bool readInheritedProtocols(SmallVectorImpl<ProtocolDecl *> &inherited);
543+
541544
/// Populates the protocol's default witness table.
542545
///
543546
/// Returns true if there is an error.

lib/Serialization/ModuleFormat.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 857; // Add superclass to protocol layout
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 858; // Replace inherited types with protocols in protocol layout
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1529,10 +1529,16 @@ namespace decls_block {
15291529
BCFixed<1>, // existential-type-supported?
15301530
TypeIDField, // superclass
15311531
AccessLevelField, // access level
1532-
BCVBR<4>, // number of inherited types
1533-
BCArray<TypeIDField> // inherited types, followed by dependency types
1534-
// Trailed by the generic parameters (if any), the members record, and
1535-
// the default witness table record
1532+
BCArray<TypeIDField> // dependency types
1533+
// Trailed by the inherited protocols, the generic parameters (if any),
1534+
// the generic signature, the members record, and the default witness table record
1535+
>;
1536+
1537+
/// A default witness table for a protocol.
1538+
using InheritedProtocolsLayout = BCRecordLayout<
1539+
INHERITED_PROTOCOLS,
1540+
BCArray<DeclIDField>
1541+
// An array of inherited protocol declarations
15361542
>;
15371543

15381544
/// A default witness table for a protocol.

lib/Serialization/Serialization.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3712,6 +3712,18 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
37123712
}
37133713
}
37143714

3715+
void writeInheritedProtocols(ArrayRef<ProtocolDecl *> inherited) {
3716+
using namespace decls_block;
3717+
3718+
SmallVector<DeclID, 4> inheritedIDs;
3719+
llvm::transform(inherited, std::back_inserter(inheritedIDs),
3720+
[&](const ProtocolDecl *P) { return S.addDeclRef(P); });
3721+
3722+
unsigned abbrCode = S.DeclTypeAbbrCodes[InheritedProtocolsLayout::Code];
3723+
InheritedProtocolsLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
3724+
inheritedIDs);
3725+
}
3726+
37153727
void writeDefaultWitnessTable(const ProtocolDecl *proto) {
37163728
using namespace decls_block;
37173729

@@ -4280,12 +4292,8 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
42804292

42814293
auto contextID = S.addDeclContextRef(proto->getDeclContext());
42824294

4283-
SmallVector<TypeID, 4> inheritedAndDependencyTypes;
42844295
swift::SmallSetVector<Type, 4> dependencyTypes;
42854296

4286-
unsigned numInherited = addInherited(
4287-
proto->getInherited(), inheritedAndDependencyTypes);
4288-
42894297
// Separately collect inherited protocol types as dependencies.
42904298
for (auto element : proto->getInherited().getEntries()) {
42914299
auto elementType = element.getType();
@@ -4311,8 +4319,9 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
43114319
/*excluding*/S.M);
43124320
}
43134321

4322+
SmallVector<TypeID, 4> dependencyTypeIDs;
43144323
for (Type ty : dependencyTypes)
4315-
inheritedAndDependencyTypes.push_back(S.addTypeRef(ty));
4324+
dependencyTypeIDs.push_back(S.addTypeRef(ty));
43164325

43174326
uint8_t rawAccessLevel = getRawStableAccessLevel(proto->getFormalAccess());
43184327

@@ -4326,9 +4335,10 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
43264335
proto->isObjC(),
43274336
proto->hasSelfOrAssociatedTypeRequirements(),
43284337
S.addTypeRef(proto->getSuperclass()),
4329-
rawAccessLevel, numInherited,
4330-
inheritedAndDependencyTypes);
4338+
rawAccessLevel,
4339+
dependencyTypeIDs);
43314340

4341+
writeInheritedProtocols(proto->getInheritedProtocols());
43324342
writeGenericParams(proto->getGenericParams());
43334343
S.writeRequirementSignature(proto->getRequirementSignature());
43344344
S.writeAssociatedTypes(proto->getAssociatedTypeMembers());
@@ -6032,6 +6042,8 @@ void Serializer::writeAllDeclsAndTypes() {
60326042
registerDeclTypeAbbr<ConditionalSubstitutionLayout>();
60336043
registerDeclTypeAbbr<ConditionalSubstitutionConditionLayout>();
60346044

6045+
registerDeclTypeAbbr<InheritedProtocolsLayout>();
6046+
60356047
#define DECL_ATTR(X, NAME, ...) \
60366048
registerDeclTypeAbbr<NAME##DeclAttrLayout>();
60376049
#include "swift/AST/DeclAttr.def"

0 commit comments

Comments
 (0)