Skip to content

Commit 728e97c

Browse files
committed
Serialization: Switch serialization to the new function type representation
Instead of serializing the input type of a function type, which is a TupleType or ParenType, serialize the individual parameters instead. This means that we no longer need to serialize TupleTypes or ParenTypes with custom flags, nor do we ever serialize standalone InOutTypes, so all of that can be removed.
1 parent f3b207e commit 728e97c

File tree

4 files changed

+133
-153
lines changed

4 files changed

+133
-153
lines changed

include/swift/Serialization/DeclTypeRecordNodes.def

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ TYPE(PAREN)
8686
TYPE(TUPLE)
8787
TRAILING_INFO(TUPLE_TYPE_ELT)
8888
TYPE(FUNCTION)
89+
TRAILING_INFO(FUNCTION_PARAM)
8990
TYPE(METATYPE)
90-
TYPE(INOUT)
9191
TYPE(ARCHETYPE)
9292
TYPE(PROTOCOL_COMPOSITION)
9393
TYPE(BOUND_GENERIC)
@@ -99,7 +99,6 @@ TYPE(REFERENCE_STORAGE)
9999
TYPE(UNBOUND_GENERIC)
100100
TYPE(OPTIONAL)
101101
TYPE(SIL_FUNCTION)
102-
TYPE(UNCHECKED_OPTIONAL)
103102
TYPE(DYNAMIC_SELF)
104103
TYPE(OPENED_EXISTENTIAL)
105104
TYPE(EXISTENTIAL_METATYPE)

include/swift/Serialization/ModuleFormat.h

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 434; // Last change: don't serialize derivable interface types
58+
const uint16_t VERSION_MINOR = 435; // Last change: serialize new-style function parameters
5959

6060
using DeclIDField = BCFixed<31>;
6161

@@ -706,11 +706,7 @@ namespace decls_block {
706706

707707
using ParenTypeLayout = BCRecordLayout<
708708
PAREN_TYPE,
709-
TypeIDField, // inner type
710-
BCFixed<1>, // vararg?
711-
BCFixed<1>, // autoclosure?
712-
BCFixed<1>, // escaping?
713-
ValueOwnershipField // inout, shared or owned?
709+
TypeIDField // inner type
714710
>;
715711

716712
using TupleTypeLayout = BCRecordLayout<
@@ -720,21 +716,28 @@ namespace decls_block {
720716
using TupleTypeEltLayout = BCRecordLayout<
721717
TUPLE_TYPE_ELT,
722718
IdentifierIDField, // name
723-
TypeIDField, // type
724-
BCFixed<1>, // vararg?
725-
BCFixed<1>, // autoclosure?
726-
BCFixed<1>, // escaping?
727-
ValueOwnershipField // inout, shared or owned?
719+
TypeIDField // type
728720
>;
729721

730722
using FunctionTypeLayout = BCRecordLayout<
731723
FUNCTION_TYPE,
732-
TypeIDField, // input
733724
TypeIDField, // output
734725
FunctionTypeRepresentationField, // representation
735726
BCFixed<1>, // auto-closure?
736727
BCFixed<1>, // noescape?
737728
BCFixed<1> // throws?
729+
730+
// trailed by parameters
731+
>;
732+
733+
using FunctionParamLayout = BCRecordLayout<
734+
FUNCTION_PARAM,
735+
IdentifierIDField, // name
736+
TypeIDField, // type
737+
BCFixed<1>, // vararg?
738+
BCFixed<1>, // autoclosure?
739+
BCFixed<1>, // escaping?
740+
ValueOwnershipField // inout, shared or owned?
738741
>;
739742

740743
using MetatypeTypeLayout = BCRecordLayout<
@@ -749,11 +752,6 @@ namespace decls_block {
749752
MetatypeRepresentationField // representation
750753
>;
751754

752-
using InOutTypeLayout = BCRecordLayout<
753-
INOUT_TYPE,
754-
TypeIDField // object type
755-
>;
756-
757755
using ArchetypeTypeLayout = BCRecordLayout<
758756
ARCHETYPE_TYPE,
759757
GenericEnvironmentIDField, // generic environment
@@ -785,11 +783,12 @@ namespace decls_block {
785783

786784
using GenericFunctionTypeLayout = BCRecordLayout<
787785
GENERIC_FUNCTION_TYPE,
788-
TypeIDField, // input
789786
TypeIDField, // output
790787
FunctionTypeRepresentationField, // representation
791788
BCFixed<1>, // throws?
792789
GenericSignatureIDField // generic signture
790+
791+
// trailed by parameters
793792
>;
794793

795794
using SILFunctionTypeLayout = BCRecordLayout<

lib/Serialization/Deserialization.cpp

Lines changed: 71 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,7 +2999,7 @@ ModuleFile::getDeclCheckedImpl(DeclID DID) {
29992999
return nullptr;
30003000
}
30013001

3002-
param->setInterfaceType(paramTy->getInOutObjectType());
3002+
param->setInterfaceType(paramTy);
30033003
param->setVariadic(isVariadic);
30043004

30053005
// Decode the default argument kind.
@@ -4263,25 +4263,13 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
42634263

42644264
case decls_block::PAREN_TYPE: {
42654265
TypeID underlyingID;
4266-
bool isVariadic, isAutoClosure, isEscaping;
4267-
unsigned rawOwnership;
4268-
decls_block::ParenTypeLayout::readRecord(scratch, underlyingID, isVariadic,
4269-
isAutoClosure, isEscaping,
4270-
rawOwnership);
4271-
auto ownership =
4272-
getActualValueOwnership((serialization::ValueOwnership)rawOwnership);
4273-
if (!ownership) {
4274-
error();
4275-
return nullptr;
4276-
}
4266+
decls_block::ParenTypeLayout::readRecord(scratch, underlyingID);
42774267

42784268
auto underlyingTy = getTypeChecked(underlyingID);
42794269
if (!underlyingTy)
42804270
return underlyingTy.takeError();
42814271

4282-
typeOrOffset = ParenType::get(
4283-
ctx, underlyingTy.get()->getInOutObjectType(),
4284-
ParameterTypeFlags(isVariadic, isAutoClosure, isEscaping, *ownership));
4272+
typeOrOffset = ParenType::get(ctx, underlyingTy.get());
42854273
break;
42864274
}
42874275

@@ -4301,44 +4289,42 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
43014289

43024290
IdentifierID nameID;
43034291
TypeID typeID;
4304-
bool isVariadic, isAutoClosure, isEscaping;
4305-
unsigned rawOwnership;
4306-
decls_block::TupleTypeEltLayout::readRecord(scratch, nameID, typeID,
4307-
isVariadic, isAutoClosure,
4308-
isEscaping, rawOwnership);
4309-
4310-
auto ownership =
4311-
getActualValueOwnership((serialization::ValueOwnership)rawOwnership);
4312-
if (!ownership) {
4313-
error();
4314-
return nullptr;
4315-
}
4292+
decls_block::TupleTypeEltLayout::readRecord(scratch, nameID, typeID);
43164293

43174294
auto elementTy = getTypeChecked(typeID);
43184295
if (!elementTy)
43194296
return elementTy.takeError();
43204297

4321-
elements.emplace_back(elementTy.get()->getInOutObjectType(),
4322-
getIdentifier(nameID),
4323-
ParameterTypeFlags(isVariadic, isAutoClosure,
4324-
isEscaping, *ownership));
4298+
elements.emplace_back(elementTy.get(), getIdentifier(nameID));
43254299
}
43264300

43274301
typeOrOffset = TupleType::get(elements, ctx);
43284302
break;
43294303
}
43304304

4331-
case decls_block::FUNCTION_TYPE: {
4332-
TypeID inputID;
4305+
case decls_block::FUNCTION_TYPE:
4306+
case decls_block::GENERIC_FUNCTION_TYPE: {
43334307
TypeID resultID;
43344308
uint8_t rawRepresentation;
4335-
bool autoClosure, noescape, throws;
4309+
bool autoClosure = false, noescape = false, throws;
4310+
GenericSignature *genericSig = nullptr;
4311+
4312+
if (recordID == decls_block::FUNCTION_TYPE) {
4313+
decls_block::FunctionTypeLayout::readRecord(scratch, resultID,
4314+
rawRepresentation,
4315+
autoClosure,
4316+
noescape,
4317+
throws);
4318+
} else {
4319+
GenericSignatureID rawGenericSig;
4320+
decls_block::GenericFunctionTypeLayout::readRecord(scratch,
4321+
resultID,
4322+
rawRepresentation,
4323+
throws,
4324+
rawGenericSig);
4325+
genericSig = getGenericSignature(rawGenericSig);
4326+
}
43364327

4337-
decls_block::FunctionTypeLayout::readRecord(scratch, inputID, resultID,
4338-
rawRepresentation,
4339-
autoClosure,
4340-
noescape,
4341-
throws);
43424328
auto representation = getActualFunctionTypeRepresentation(rawRepresentation);
43434329
if (!representation.hasValue()) {
43444330
error();
@@ -4348,14 +4334,56 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
43484334
auto info = FunctionType::ExtInfo(*representation, autoClosure, noescape,
43494335
throws);
43504336

4351-
auto inputTy = getTypeChecked(inputID);
4352-
if (!inputTy)
4353-
return inputTy.takeError();
43544337
auto resultTy = getTypeChecked(resultID);
43554338
if (!resultTy)
43564339
return resultTy.takeError();
43574340

4358-
typeOrOffset = FunctionType::get(inputTy.get(), resultTy.get(), info);
4341+
SmallVector<AnyFunctionType::Param, 8> params;
4342+
while (true) {
4343+
auto entry = DeclTypeCursor.advance(AF_DontPopBlockAtEnd);
4344+
if (entry.Kind != llvm::BitstreamEntry::Record)
4345+
break;
4346+
4347+
scratch.clear();
4348+
unsigned recordID = DeclTypeCursor.readRecord(entry.ID, scratch,
4349+
&blobData);
4350+
if (recordID != decls_block::FUNCTION_PARAM)
4351+
break;
4352+
4353+
IdentifierID labelID;
4354+
TypeID typeID;
4355+
bool isVariadic, isAutoClosure, isEscaping;
4356+
unsigned rawOwnership;
4357+
decls_block::FunctionParamLayout::readRecord(scratch, labelID, typeID,
4358+
isVariadic, isAutoClosure,
4359+
isEscaping, rawOwnership);
4360+
4361+
auto ownership =
4362+
getActualValueOwnership((serialization::ValueOwnership)rawOwnership);
4363+
if (!ownership) {
4364+
error();
4365+
return nullptr;
4366+
}
4367+
4368+
auto paramTy = getTypeChecked(typeID);
4369+
if (!paramTy)
4370+
return paramTy.takeError();
4371+
4372+
params.emplace_back(paramTy.get(),
4373+
getIdentifier(labelID),
4374+
ParameterTypeFlags(isVariadic, isAutoClosure,
4375+
isEscaping, *ownership));
4376+
}
4377+
4378+
if (recordID == decls_block::FUNCTION_TYPE) {
4379+
assert(genericSig == nullptr);
4380+
typeOrOffset = FunctionType::get(params, resultTy.get(), info);
4381+
} else {
4382+
assert(genericSig != nullptr);
4383+
typeOrOffset = GenericFunctionType::get(genericSig,
4384+
params, resultTy.get(), info);
4385+
}
4386+
43594387
break;
43604388
}
43614389

@@ -4437,18 +4465,6 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
44374465
break;
44384466
}
44394467

4440-
case decls_block::INOUT_TYPE: {
4441-
TypeID objectTypeID;
4442-
decls_block::InOutTypeLayout::readRecord(scratch, objectTypeID);
4443-
4444-
auto objectTy = getTypeChecked(objectTypeID);
4445-
if (!objectTy)
4446-
return objectTy.takeError();
4447-
4448-
typeOrOffset = InOutType::get(objectTy.get());
4449-
break;
4450-
}
4451-
44524468
case decls_block::REFERENCE_STORAGE_TYPE: {
44534469
uint8_t rawOwnership;
44544470
TypeID objectTypeID;
@@ -4596,40 +4612,6 @@ Expected<Type> ModuleFile::getTypeChecked(TypeID TID) {
45964612
break;
45974613
}
45984614

4599-
case decls_block::GENERIC_FUNCTION_TYPE: {
4600-
TypeID inputID;
4601-
TypeID resultID;
4602-
uint8_t rawRep;
4603-
bool throws = false;
4604-
GenericSignatureID rawGenericSig;
4605-
4606-
decls_block::GenericFunctionTypeLayout::readRecord(scratch,
4607-
inputID,
4608-
resultID,
4609-
rawRep,
4610-
throws,
4611-
rawGenericSig);
4612-
auto rep = getActualFunctionTypeRepresentation(rawRep);
4613-
if (!rep.hasValue()) {
4614-
error();
4615-
return nullptr;
4616-
}
4617-
4618-
auto sig = getGenericSignature(rawGenericSig);
4619-
auto info = GenericFunctionType::ExtInfo(*rep, throws);
4620-
4621-
auto inputTy = getTypeChecked(inputID);
4622-
if (!inputTy)
4623-
return inputTy.takeError();
4624-
auto resultTy = getTypeChecked(resultID);
4625-
if (!resultTy)
4626-
return resultTy.takeError();
4627-
4628-
typeOrOffset = GenericFunctionType::get(sig, inputTy.get(), resultTy.get(),
4629-
info);
4630-
break;
4631-
}
4632-
46334615
case decls_block::SIL_BLOCK_STORAGE_TYPE: {
46344616
TypeID captureID;
46354617

0 commit comments

Comments
 (0)