Skip to content

Commit 4539dbf

Browse files
authored
[Serialization] Simplify the representation of GenericParamLists (#20446)
Now that we don't store requirements in the GenericParamList, there's no reason to use trailing records to list out the GenericTypeParamDecls. No functionality change.
1 parent faa535f commit 4539dbf

File tree

6 files changed

+23
-56
lines changed

6 files changed

+23
-56
lines changed

include/swift/Serialization/DeclTypeRecordNodes.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ OTHER(DEFAULT_ARGUMENT_INITIALIZER_CONTEXT, 222)
159159
OTHER(TOP_LEVEL_CODE_DECL_CONTEXT, 223)
160160

161161
OTHER(GENERIC_PARAM_LIST, 230)
162-
TRAILING_INFO(GENERIC_PARAM)
162+
OTHER(GENERIC_SIGNATURE, 231)
163163
TRAILING_INFO(GENERIC_REQUIREMENT)
164164
TRAILING_INFO(LAYOUT_REQUIREMENT)
165-
OTHER(GENERIC_SIGNATURE, 234)
165+
// 234 is unused
166166
OTHER(SIL_GENERIC_ENVIRONMENT, 235)
167167
OTHER(SUBSTITUTION_MAP, 236)
168168

include/swift/Serialization/ModuleFormat.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 463; // Last change: enable-private-imports
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 464; // Last change: simplify GenericParamLists
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -1259,13 +1259,8 @@ namespace decls_block {
12591259
>;
12601260

12611261
using GenericParamListLayout = BCRecordLayout<
1262-
GENERIC_PARAM_LIST
1263-
// The actual parameters and requirements trail the record.
1264-
>;
1265-
1266-
using GenericParamLayout = BCRecordLayout<
1267-
GENERIC_PARAM,
1268-
DeclIDField // Typealias
1262+
GENERIC_PARAM_LIST,
1263+
BCArray<DeclIDField> // the GenericTypeParamDecls
12691264
>;
12701265

12711266
using GenericSignatureLayout = BCRecordLayout<

lib/Serialization/Deserialization.cpp

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -652,39 +652,19 @@ GenericParamList *ModuleFile::maybeReadGenericParams(DeclContext *DC,
652652
unsigned kind = DeclTypeCursor.readRecord(next.ID, scratch, &blobData);
653653
if (kind != GENERIC_PARAM_LIST)
654654
return nullptr;
655+
lastRecordOffset.reset();
655656

656657
SmallVector<GenericTypeParamDecl *, 8> params;
657658

658-
while (true) {
659-
lastRecordOffset.reset();
660-
bool shouldContinue = true;
661-
662-
auto entry = DeclTypeCursor.advance(AF_DontPopBlockAtEnd);
663-
if (entry.Kind != llvm::BitstreamEntry::Record)
664-
break;
665-
666-
scratch.clear();
667-
unsigned recordID = DeclTypeCursor.readRecord(entry.ID, scratch,
668-
&blobData);
669-
switch (recordID) {
670-
case GENERIC_PARAM: {
671-
DeclID paramDeclID;
672-
GenericParamLayout::readRecord(scratch, paramDeclID);
673-
auto genericParam = cast<GenericTypeParamDecl>(getDecl(paramDeclID));
674-
params.push_back(genericParam);
675-
break;
676-
}
677-
default:
678-
// This record is not part of the GenericParamList.
679-
shouldContinue = false;
680-
break;
681-
}
682-
683-
if (!shouldContinue)
684-
break;
659+
ArrayRef<uint64_t> paramIDs;
660+
GenericParamListLayout::readRecord(scratch, paramIDs);
661+
for (DeclID nextParamID : paramIDs) {
662+
auto genericParam = cast<GenericTypeParamDecl>(getDecl(nextParamID));
663+
params.push_back(genericParam);
685664
}
686665

687-
// Don't create empty generic parameter lists.
666+
// Don't create empty generic parameter lists. (This should never happen in
667+
// practice, but it doesn't hurt to be defensive.)
688668
if (params.empty())
689669
return nullptr;
690670

lib/Serialization/SILFormat.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ namespace sil_block {
181181
INHERITED_PROTOCOL_CONFORMANCE
182182
= decls_block::INHERITED_PROTOCOL_CONFORMANCE,
183183
INVALID_PROTOCOL_CONFORMANCE = decls_block::INVALID_PROTOCOL_CONFORMANCE,
184-
GENERIC_PARAM = decls_block::GENERIC_PARAM,
185184
GENERIC_REQUIREMENT = decls_block::GENERIC_REQUIREMENT,
186185
LAYOUT_REQUIREMENT = decls_block::LAYOUT_REQUIREMENT,
187186
};

lib/Serialization/Serialization.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,6 @@ void Serializer::writeBlockInfoBlock() {
915915
decls_block::PROTOCOL_CONFORMANCE_XREF);
916916
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
917917
decls_block::GENERIC_PARAM_LIST);
918-
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
919-
decls_block::GENERIC_PARAM);
920918
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
921919
decls_block::GENERIC_REQUIREMENT);
922920
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
@@ -1372,23 +1370,19 @@ void Serializer::writeInlinableBodyTextIfNeeded(
13721370
InlinableBodyTextLayout::emitRecord(Out, ScratchRecord, abbrCode, body);
13731371
}
13741372

1375-
bool Serializer::writeGenericParams(const GenericParamList *genericParams) {
1373+
void Serializer::writeGenericParams(const GenericParamList *genericParams) {
13761374
using namespace decls_block;
13771375

13781376
// Don't write anything if there are no generic params.
13791377
if (!genericParams)
1380-
return true;
1381-
1382-
unsigned abbrCode = DeclTypeAbbrCodes[GenericParamListLayout::Code];
1383-
GenericParamListLayout::emitRecord(Out, ScratchRecord, abbrCode);
1378+
return;
13841379

1385-
abbrCode = DeclTypeAbbrCodes[GenericParamLayout::Code];
1386-
for (auto next : genericParams->getParams()) {
1387-
GenericParamLayout::emitRecord(Out, ScratchRecord, abbrCode,
1388-
addDeclRef(next));
1389-
}
1380+
SmallVector<DeclID, 4> paramIDs;
1381+
for (auto next : genericParams->getParams())
1382+
paramIDs.push_back(addDeclRef(next));
13901383

1391-
return true;
1384+
unsigned abbrCode = DeclTypeAbbrCodes[GenericParamListLayout::Code];
1385+
GenericParamListLayout::emitRecord(Out, ScratchRecord, abbrCode, paramIDs);
13921386
}
13931387

13941388
void Serializer::writeGenericSignature(const GenericSignature *sig) {
@@ -4131,10 +4125,9 @@ void Serializer::writeAllDeclsAndTypes() {
41314125
registerDeclTypeAbbr<TypedPatternLayout>();
41324126
registerDeclTypeAbbr<InlinableBodyTextLayout>();
41334127
registerDeclTypeAbbr<GenericParamListLayout>();
4134-
registerDeclTypeAbbr<GenericParamLayout>();
4128+
registerDeclTypeAbbr<GenericSignatureLayout>();
41354129
registerDeclTypeAbbr<GenericRequirementLayout>();
41364130
registerDeclTypeAbbr<LayoutRequirementLayout>();
4137-
registerDeclTypeAbbr<GenericSignatureLayout>();
41384131
registerDeclTypeAbbr<SILGenericEnvironmentLayout>();
41394132
registerDeclTypeAbbr<SubstitutionMapLayout>();
41404133

lib/Serialization/Serialization.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,8 @@ class Serializer : public SerializerBase {
342342
/// Writes the given pattern, recursively.
343343
void writePattern(const Pattern *pattern, DeclContext *owningDC);
344344

345-
/// Writes a generic parameter list.
346-
bool writeGenericParams(const GenericParamList *genericParams);
345+
/// Writes a generic parameter list, if non-null.
346+
void writeGenericParams(const GenericParamList *genericParams);
347347

348348
/// Writes the body text of the provided funciton, if the function is
349349
/// inlinable and has body text.

0 commit comments

Comments
 (0)