Skip to content

Commit 57a3fa3

Browse files
committed
Standardize serialization of DeclNameRefs
There are four attributes which serialize out a DeclNameRef, sometimes by dropping some of its components. Standardize them with a representation that can handle module selectors.
1 parent 24a4c4a commit 57a3fa3

File tree

6 files changed

+139
-100
lines changed

6 files changed

+139
-100
lines changed

lib/Serialization/DeclTypeRecordNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ OTHER(DERIVATIVE_FUNCTION_CONFIGURATION, 154)
213213

214214
OTHER(ERROR_FLAG, 155)
215215
OTHER(ABI_ONLY_COUNTERPART, 156)
216+
OTHER(DECL_NAME_REF, 157)
216217

217218
TRAILING_INFO(CONDITIONAL_SUBSTITUTION)
218219
TRAILING_INFO(CONDITIONAL_SUBSTITUTION_COND)
219220

220221
OTHER(LIFETIME_DEPENDENCE, 160)
221-
222222
TRAILING_INFO(INHERITED_PROTOCOLS)
223223

224224
#ifndef DECL_ATTR

lib/Serialization/Deserialization.cpp

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3498,6 +3498,58 @@ class DeclDeserializer {
34983498
/// offsets in \c customAttrOffsets.
34993499
llvm::Error deserializeCustomAttrs();
35003500

3501+
DeclNameRef deserializeDeclNameRefIfPresent() {
3502+
using namespace decls_block;
3503+
3504+
SmallVector<uint64_t, 64> scratch;
3505+
StringRef blobData;
3506+
3507+
BCOffsetRAII restoreOffset(MF.DeclTypeCursor);
3508+
llvm::BitstreamEntry entry =
3509+
MF.fatalIfUnexpected(MF.DeclTypeCursor.advance());
3510+
3511+
unsigned recordID = MF.fatalIfUnexpected(
3512+
MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData));
3513+
3514+
if (recordID != DECL_NAME_REF)
3515+
// This is normal--it just means there isn't a DeclNameRef here.
3516+
return { DeclNameRef() };
3517+
3518+
bool isCompoundName;
3519+
bool hasModuleSelector;
3520+
ArrayRef<uint64_t> rawPieceIDs;
3521+
3522+
DeclNameRefLayout::readRecord(scratch, isCompoundName, hasModuleSelector,
3523+
rawPieceIDs);
3524+
restoreOffset.cancel();
3525+
3526+
Identifier moduleSelector;
3527+
DeclBaseName baseName;
3528+
3529+
unsigned restIndex = 0;
3530+
3531+
ASSERT(rawPieceIDs.size() > 0);
3532+
if (hasModuleSelector) {
3533+
moduleSelector = MF.getIdentifier(rawPieceIDs[restIndex]);
3534+
restIndex++;
3535+
}
3536+
3537+
ASSERT(rawPieceIDs.size() > restIndex);
3538+
baseName = MF.getDeclBaseName(rawPieceIDs[restIndex]);
3539+
restIndex++;
3540+
3541+
if (isCompoundName) {
3542+
SmallVector<Identifier, 8> argLabels;
3543+
for (auto rawArgLabel : rawPieceIDs.drop_front(restIndex))
3544+
argLabels.push_back(MF.getIdentifier(rawArgLabel));
3545+
3546+
return DeclNameRef(ctx, moduleSelector, baseName, argLabels);
3547+
}
3548+
3549+
ASSERT(rawPieceIDs.size() == restIndex);
3550+
return DeclNameRef(ctx, moduleSelector, baseName);
3551+
}
3552+
35013553
Expected<Decl *> getDeclCheckedImpl(
35023554
llvm::function_ref<bool(DeclAttributes)> matchAttributes = nullptr);
35033555

@@ -6141,56 +6193,32 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
61416193
unsigned specializationKindVal;
61426194
GenericSignatureID specializedSigID;
61436195

6144-
ArrayRef<uint64_t> rawPieceIDs;
6145-
uint64_t numArgs;
6196+
ArrayRef<uint64_t> rawTrailingIDs;
61466197
uint64_t numSPIGroups;
61476198
uint64_t numAvailabilityAttrs;
6148-
uint64_t numTypeErasedParams;
61496199
DeclID targetFunID;
61506200

61516201
serialization::decls_block::SpecializeDeclAttrLayout::readRecord(
61526202
scratch, exported, specializationKindVal, specializedSigID,
6153-
targetFunID, numArgs, numSPIGroups, numAvailabilityAttrs,
6154-
numTypeErasedParams, rawPieceIDs);
6203+
targetFunID, numSPIGroups, numAvailabilityAttrs, rawTrailingIDs);
61556204

6156-
assert(rawPieceIDs.size() == numArgs + numSPIGroups + numTypeErasedParams ||
6157-
rawPieceIDs.size() == (numArgs - 1 + numSPIGroups + numTypeErasedParams));
61586205
specializationKind = specializationKindVal
61596206
? SpecializeAttr::SpecializationKind::Partial
61606207
: SpecializeAttr::SpecializationKind::Full;
6161-
// The 'target' parameter.
6162-
DeclNameRef replacedFunctionName;
6163-
if (numArgs) {
6164-
bool numArgumentLabels = (numArgs == 1) ? 0 : numArgs - 2;
6165-
auto baseName = MF.getDeclBaseName(rawPieceIDs[0]);
6166-
SmallVector<Identifier, 4> pieces;
6167-
if (numArgumentLabels) {
6168-
for (auto pieceID : rawPieceIDs.slice(1, numArgumentLabels))
6169-
pieces.push_back(MF.getIdentifier(pieceID));
6170-
}
6171-
replacedFunctionName = (numArgs == 1)
6172-
? DeclNameRef({baseName}) // simple name
6173-
: DeclNameRef({ctx, baseName, pieces});
6174-
}
61756208

6209+
auto specializedSig = MF.getGenericSignature(specializedSigID);
6210+
6211+
// Take `numSPIGroups` trailing identifiers for the SPI groups.
61766212
SmallVector<Identifier, 4> spis;
6177-
if (numSPIGroups) {
6178-
auto numTargetFunctionPiecesToSkip =
6179-
(rawPieceIDs.size() == numArgs + numSPIGroups + numTypeErasedParams) ? numArgs
6180-
: numArgs - 1;
6181-
for (auto id : rawPieceIDs.slice(numTargetFunctionPiecesToSkip))
6213+
for (auto id : rawTrailingIDs.take_front(numSPIGroups))
61826214
spis.push_back(MF.getIdentifier(id));
6183-
}
61846215

6216+
// Take the rest for type-erased parameters.
61856217
SmallVector<Type, 4> typeErasedParams;
6186-
if (numTypeErasedParams) {
6187-
auto numTargetFunctionPiecesToSkip =
6188-
(rawPieceIDs.size() == numArgs + numSPIGroups + numTypeErasedParams) ? numArgs + numSPIGroups
6189-
: numArgs - 1 + numSPIGroups;
6190-
for (auto id : rawPieceIDs.slice(numTargetFunctionPiecesToSkip))
6191-
typeErasedParams.push_back(MF.getType(id));
6192-
}
6218+
for (auto id : rawTrailingIDs.drop_front(numSPIGroups))
6219+
typeErasedParams.push_back(MF.getType(id));
61936220

6221+
// Read availability attrs.
61946222
SmallVector<AvailableAttr *, 4> availabilityAttrs;
61956223
while (numAvailabilityAttrs) {
61966224
// Prepare to read the next record.
@@ -6220,10 +6248,12 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
62206248
--numAvailabilityAttrs;
62216249
}
62226250

6223-
auto specializedSig = MF.getGenericSignature(specializedSigID);
6251+
// Read target function DeclNameRef, if present.
6252+
DeclNameRef targetFunName = deserializeDeclNameRefIfPresent();
6253+
62246254
Attr = SpecializeAttr::create(ctx, exported != 0, specializationKind,
62256255
spis, availabilityAttrs, typeErasedParams,
6226-
specializedSig, replacedFunctionName, &MF,
6256+
specializedSig, targetFunName, &MF,
62276257
targetFunID);
62286258
break;
62296259
}
@@ -6254,21 +6284,15 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
62546284

62556285
case decls_block::DynamicReplacement_DECL_ATTR: {
62566286
bool isImplicit;
6257-
uint64_t numArgs;
6258-
ArrayRef<uint64_t> rawPieceIDs;
62596287
DeclID replacedFunID;
62606288
serialization::decls_block::DynamicReplacementDeclAttrLayout::
6261-
readRecord(scratch, isImplicit, replacedFunID, numArgs, rawPieceIDs);
6289+
readRecord(scratch, isImplicit, replacedFunID);
62626290

6263-
auto baseName = MF.getDeclBaseName(rawPieceIDs[0]);
6264-
SmallVector<Identifier, 4> pieces;
6265-
for (auto pieceID : rawPieceIDs.slice(1))
6266-
pieces.push_back(MF.getIdentifier(pieceID));
6291+
DeclNameRef replacedFunName = deserializeDeclNameRefIfPresent();
62676292

6268-
assert(numArgs != 0);
62696293
assert(!isImplicit && "Need to update for implicit");
6270-
Attr = DynamicReplacementAttr::create(
6271-
ctx, DeclNameRef({ ctx, baseName, pieces }), &MF, replacedFunID);
6294+
Attr = DynamicReplacementAttr::create(ctx, replacedFunName, &MF,
6295+
replacedFunID);
62726296
break;
62736297
}
62746298

@@ -6339,15 +6363,14 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
63396363

63406364
case decls_block::Derivative_DECL_ATTR: {
63416365
bool isImplicit;
6342-
uint64_t origNameId;
63436366
bool hasAccessorKind;
63446367
uint64_t rawAccessorKind;
63456368
DeclID origDeclId;
63466369
uint64_t rawDerivativeKind;
63476370
ArrayRef<uint64_t> parameters;
63486371

63496372
serialization::decls_block::DerivativeDeclAttrLayout::readRecord(
6350-
scratch, isImplicit, origNameId, hasAccessorKind, rawAccessorKind,
6373+
scratch, isImplicit, hasAccessorKind, rawAccessorKind,
63516374
origDeclId, rawDerivativeKind, parameters);
63526375

63536376
std::optional<AccessorKind> accessorKind = std::nullopt;
@@ -6358,8 +6381,6 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
63586381
accessorKind = *maybeAccessorKind;
63596382
}
63606383

6361-
DeclNameRefWithLoc origName{DeclNameRef(MF.getDeclBaseName(origNameId)),
6362-
DeclNameLoc(), accessorKind};
63636384
auto derivativeKind =
63646385
getActualAutoDiffDerivativeFunctionKind(rawDerivativeKind);
63656386
if (!derivativeKind)
@@ -6369,9 +6390,14 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
63696390
parametersBitVector[i] = parameters[i];
63706391
auto *indices = IndexSubset::get(ctx, parametersBitVector);
63716392

6393+
auto origName = deserializeDeclNameRefIfPresent();
6394+
DeclNameRefWithLoc origNameWithLoc{origName, DeclNameLoc(),
6395+
accessorKind};
6396+
63726397
auto *derivativeAttr =
63736398
DerivativeAttr::create(ctx, isImplicit, SourceLoc(), SourceRange(),
6374-
/*baseType*/ nullptr, origName, indices);
6399+
/*baseType*/ nullptr, origNameWithLoc,
6400+
indices);
63756401
derivativeAttr->setOriginalFunctionResolver(&MF, origDeclId);
63766402
derivativeAttr->setDerivativeKind(*derivativeKind);
63776403
Attr = derivativeAttr;
@@ -6380,20 +6406,21 @@ llvm::Error DeclDeserializer::deserializeDeclCommon() {
63806406

63816407
case decls_block::Transpose_DECL_ATTR: {
63826408
bool isImplicit;
6383-
uint64_t origNameId;
63846409
DeclID origDeclId;
63856410
ArrayRef<uint64_t> parameters;
63866411

63876412
serialization::decls_block::TransposeDeclAttrLayout::readRecord(
6388-
scratch, isImplicit, origNameId, origDeclId, parameters);
6413+
scratch, isImplicit, origDeclId, parameters);
63896414

6390-
DeclNameRefWithLoc origName{DeclNameRef(MF.getDeclBaseName(origNameId)),
6391-
DeclNameLoc(), std::nullopt};
63926415
auto *origDecl = cast<AbstractFunctionDecl>(MF.getDecl(origDeclId));
63936416
llvm::SmallBitVector parametersBitVector(parameters.size());
63946417
for (unsigned i : indices(parameters))
63956418
parametersBitVector[i] = parameters[i];
63966419
auto *indices = IndexSubset::get(ctx, parametersBitVector);
6420+
6421+
auto origNameRef = deserializeDeclNameRefIfPresent();
6422+
DeclNameRefWithLoc origName{origNameRef, DeclNameLoc(), std::nullopt};
6423+
63976424
auto *transposeAttr =
63986425
TransposeAttr::create(ctx, isImplicit, SourceLoc(), SourceRange(),
63996426
/*baseTypeRepr*/ nullptr, origName, indices);

lib/Serialization/ModuleFormat.h

Lines changed: 12 additions & 9 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 = 948; // remove SwiftSettings
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 949; // DeclNameRef module selectors
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1258,6 +1258,15 @@ namespace decls_block {
12581258
DeclIDField // API decl
12591259
>;
12601260

1261+
/// A field containing the pieces of a \c DeclNameRef and the information
1262+
/// needed to reconstruct it.
1263+
using DeclNameRefLayout = BCRecordLayout<
1264+
DECL_NAME_REF,
1265+
BCFixed<1>, // isCompoundName
1266+
BCFixed<1>, // hasModuleSelector
1267+
BCArray<IdentifierIDField> // module selector, base name, arg labels
1268+
>;
1269+
12611270
/// A placeholder for invalid types
12621271
TYPE_LAYOUT(ErrorTypeLayout,
12631272
ERROR_TYPE,
@@ -2444,11 +2453,9 @@ namespace decls_block {
24442453
BCFixed<1>, // specialization kind
24452454
GenericSignatureIDField, // specialized signature
24462455
DeclIDField, // target function
2447-
BCVBR<4>, // # of arguments (+1) or 1 if simple decl name, 0 if no target
24482456
BCVBR<4>, // # of SPI groups
24492457
BCVBR<4>, // # of availability attributes
2450-
BCVBR<4>, // # of type erased parameters
2451-
BCArray<IdentifierIDField> // target function pieces, spi groups, type erased params
2458+
BCArray<IdentifierIDField> // spi groups, type erased params
24522459
>;
24532460

24542461
using StorageRestrictionsDeclAttrLayout = BCRecordLayout<
@@ -2468,7 +2475,6 @@ namespace decls_block {
24682475
using DerivativeDeclAttrLayout = BCRecordLayout<
24692476
Derivative_DECL_ATTR,
24702477
BCFixed<1>, // Implicit flag.
2471-
IdentifierIDField, // Original name.
24722478
BCFixed<1>, // Has original accessor kind?
24732479
AccessorKindField, // Original accessor kind.
24742480
DeclIDField, // Original function declaration.
@@ -2479,7 +2485,6 @@ namespace decls_block {
24792485
using TransposeDeclAttrLayout = BCRecordLayout<
24802486
Transpose_DECL_ATTR,
24812487
BCFixed<1>, // Implicit flag.
2482-
IdentifierIDField, // Original name.
24832488
DeclIDField, // Original function declaration.
24842489
BCArray<BCFixed<1>> // Transposed parameter indices' bitvector.
24852490
>;
@@ -2494,9 +2499,7 @@ namespace decls_block {
24942499
using DynamicReplacementDeclAttrLayout = BCRecordLayout<
24952500
DynamicReplacement_DECL_ATTR,
24962501
BCFixed<1>, // implicit flag
2497-
DeclIDField, // replaced function
2498-
BCVBR<4>, // # of arguments (+1) or zero if no name
2499-
BCArray<IdentifierIDField>
2502+
DeclIDField // replaced function
25002503
>;
25012504

25022505
using TypeEraserDeclAttrLayout = BCRecordLayout<

0 commit comments

Comments
 (0)