Skip to content

Commit 6bd0421

Browse files
committed
[Serialization] Factor out common loop in writing AST block entities
No intended functionality change (other than a small reorder).
1 parent 9e1b206 commit 6bd0421

File tree

2 files changed

+46
-55
lines changed

2 files changed

+46
-55
lines changed

lib/Serialization/Serialization.cpp

Lines changed: 33 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ void Serializer::writeGenericRequirements(ArrayRef<Requirement> requirements,
11771177
}
11781178
}
11791179

1180-
void Serializer::writeGenericSignature(const GenericSignature *sig) {
1180+
void Serializer::writeASTBlockEntity(const GenericSignature *sig) {
11811181
using namespace decls_block;
11821182

11831183
assert(sig != nullptr);
@@ -1257,7 +1257,7 @@ void Serializer::writeGenericEnvironment(const GenericEnvironment *env) {
12571257
DeclTypeAbbrCodes);
12581258
}
12591259

1260-
void Serializer::writeSubstitutionMap(const SubstitutionMap substitutions) {
1260+
void Serializer::writeASTBlockEntity(const SubstitutionMap substitutions) {
12611261
using namespace decls_block;
12621262
assert(substitutions);
12631263
assert(SubstitutionMapsToSerialize.hasRef(substitutions));
@@ -1277,7 +1277,7 @@ void Serializer::writeSubstitutionMap(const SubstitutionMap substitutions) {
12771277
writeConformances(substitutions.getConformances(), DeclTypeAbbrCodes);
12781278
}
12791279

1280-
void Serializer::writeSILLayout(const SILLayout *layout) {
1280+
void Serializer::writeASTBlockEntity(const SILLayout *layout) {
12811281
using namespace decls_block;
12821282
assert(SILLayoutsToSerialize.hasRef(layout));
12831283

@@ -1301,7 +1301,7 @@ void Serializer::writeSILLayout(const SILLayout *layout) {
13011301
data);
13021302
}
13031303

1304-
void Serializer::writeNormalConformance(
1304+
void Serializer::writeASTBlockEntity(
13051305
const NormalProtocolConformance *conformance) {
13061306
using namespace decls_block;
13071307

@@ -1925,11 +1925,11 @@ void Serializer::writeAbstractClosureExpr(const DeclContext *parentContext,
19251925
parentID.getOpaqueValue());
19261926
}
19271927

1928-
void Serializer::writeLocalDeclContext(const DeclContext *DC) {
1928+
void Serializer::writeASTBlockEntity(const DeclContext *DC) {
19291929
using namespace decls_block;
19301930

19311931
assert(shouldSerializeAsLocalContext(DC) &&
1932-
"Can't serialize as local context");
1932+
"should be serialized as a Decl instead");
19331933
assert(LocalDeclContextsToSerialize.hasRef(DC));
19341934

19351935
switch (DC->getContextKind()) {
@@ -3577,7 +3577,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
35773577
}
35783578
};
35793579

3580-
void Serializer::writeDecl(const Decl *D) {
3580+
void Serializer::writeASTBlockEntity(const Decl *D) {
35813581
using namespace decls_block;
35823582

35833583
PrettyStackTraceDecl trace("serializing", D);
@@ -4103,7 +4103,7 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
41034103
}
41044104
};
41054105

4106-
void Serializer::writeType(Type ty) {
4106+
void Serializer::writeASTBlockEntity(Type ty) {
41074107
using namespace decls_block;
41084108
PrettyStackTraceType traceRAII(ty->getASTContext(), "serializing", ty);
41094109
assert(TypesToSerialize.hasRef(ty));
@@ -4120,6 +4120,16 @@ void Serializer::writeType(Type ty) {
41204120
TypeSerializer(*this).visit(ty);
41214121
}
41224122

4123+
template <typename SpecificASTBlockRecordKeeper>
4124+
bool Serializer::writeASTBlockEntitiesIfNeeded(
4125+
SpecificASTBlockRecordKeeper &entities) {
4126+
if (!entities.hasMoreToSerialize())
4127+
return false;
4128+
while (auto next = entities.popNext(Out.GetCurrentBitNo()))
4129+
writeASTBlockEntity(next.getValue());
4130+
return true;
4131+
}
4132+
41234133
void Serializer::writeAllDeclsAndTypes() {
41244134
BCBlockRAII restoreBlock(Out, DECLS_AND_TYPES_BLOCK_ID, 8);
41254135
using namespace decls_block;
@@ -4234,50 +4244,25 @@ void Serializer::writeAllDeclsAndTypes() {
42344244
// until /all/ of the pending lists are empty.
42354245
wroteSomething = false;
42364246

4237-
while (auto next = DeclsToSerialize.popNext(Out.GetCurrentBitNo())) {
4238-
writeDecl(next.getValue());
4239-
wroteSomething = true;
4240-
}
4241-
4242-
while (auto next = TypesToSerialize.popNext(Out.GetCurrentBitNo())) {
4243-
writeType(next.getValue());
4244-
wroteSomething = true;
4245-
}
4246-
4247-
while (auto next =
4248-
LocalDeclContextsToSerialize.popNext(Out.GetCurrentBitNo())) {
4249-
writeLocalDeclContext(next.getValue());
4250-
wroteSomething = true;
4251-
}
4252-
4253-
while (auto next =
4254-
GenericSignaturesToSerialize.popNext(Out.GetCurrentBitNo())) {
4255-
writeGenericSignature(next.getValue());
4256-
wroteSomething = true;
4257-
}
4258-
4247+
wroteSomething |= writeASTBlockEntitiesIfNeeded(DeclsToSerialize);
4248+
wroteSomething |= writeASTBlockEntitiesIfNeeded(TypesToSerialize);
4249+
wroteSomething |=
4250+
writeASTBlockEntitiesIfNeeded(LocalDeclContextsToSerialize);
4251+
wroteSomething |=
4252+
writeASTBlockEntitiesIfNeeded(GenericSignaturesToSerialize);
4253+
wroteSomething |=
4254+
writeASTBlockEntitiesIfNeeded(SubstitutionMapsToSerialize);
4255+
wroteSomething |=
4256+
writeASTBlockEntitiesIfNeeded(NormalConformancesToSerialize);
4257+
wroteSomething |= writeASTBlockEntitiesIfNeeded(SILLayoutsToSerialize);
4258+
4259+
// Generic environments are recorded in a funny way; see
4260+
// writeNextGenericEnvironment() for why they can't just use the same logic
4261+
// as everything else.
42594262
while (GenericEnvironmentsToSerialize.hasMoreToSerialize()) {
42604263
writeNextGenericEnvironment();
42614264
wroteSomething = true;
42624265
}
4263-
4264-
while (auto next =
4265-
SubstitutionMapsToSerialize.popNext(Out.GetCurrentBitNo())) {
4266-
writeSubstitutionMap(next.getValue());
4267-
wroteSomething = true;
4268-
}
4269-
4270-
while (auto next =
4271-
NormalConformancesToSerialize.popNext(Out.GetCurrentBitNo())) {
4272-
writeNormalConformance(next.getValue());
4273-
wroteSomething = true;
4274-
}
4275-
4276-
while (auto next = SILLayoutsToSerialize.popNext(Out.GetCurrentBitNo())) {
4277-
writeSILLayout(next.getValue());
4278-
wroteSomething = true;
4279-
}
4280-
42814266
} while (wroteSomething);
42824267
}
42834268

lib/Serialization/Serialization.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ class Serializer : public SerializerBase {
299299
void writeCrossReference(const Decl *D);
300300

301301
/// Writes the given decl.
302-
void writeDecl(const Decl *D);
302+
void writeASTBlockEntity(const Decl *D);
303303

304304
/// Write a DeclContext as a local DeclContext at the current offset.
305-
void writeLocalDeclContext(const DeclContext *DC);
305+
void writeASTBlockEntity(const DeclContext *DC);
306306

307307
/// Write the components of a PatternBindingInitializer as a local context.
308308
void writePatternBindingInitializer(PatternBindingDecl *binding,
@@ -315,10 +315,10 @@ class Serializer : public SerializerBase {
315315
void writeAbstractClosureExpr(const DeclContext *parentContext, Type Ty, bool isImplicit, unsigned discriminator);
316316

317317
/// Writes the given type.
318-
void writeType(Type ty);
318+
void writeASTBlockEntity(Type ty);
319319

320320
/// Writes a generic signature.
321-
void writeGenericSignature(const GenericSignature *sig);
321+
void writeASTBlockEntity(const GenericSignature *sig);
322322

323323
/// Writes the next generic environment in #GenericEnvironmentsToSerialize.
324324
///
@@ -330,7 +330,7 @@ class Serializer : public SerializerBase {
330330
void writeGenericEnvironment(const GenericEnvironment *env);
331331

332332
/// Writes a substitution map.
333-
void writeSubstitutionMap(const SubstitutionMap substitutions);
333+
void writeASTBlockEntity(const SubstitutionMap substitutions);
334334

335335
/// Registers the abbreviation for the given decl or type layout.
336336
template <typename Layout>
@@ -341,6 +341,12 @@ class Serializer : public SerializerBase {
341341
DeclTypeAbbrCodes[Layout::Code] = Layout::emitAbbrev(Out);
342342
}
343343

344+
/// Writes all queued \p entities until there are no more.
345+
///
346+
/// \returns true if any entities were written
347+
template <typename SpecificASTBlockRecordKeeper>
348+
bool writeASTBlockEntitiesIfNeeded(SpecificASTBlockRecordKeeper &entities);
349+
344350
/// Writes all decls and types in the DeclsToWrite queue.
345351
///
346352
/// This will continue until the queue is empty, even if the items currently
@@ -470,10 +476,10 @@ class Serializer : public SerializerBase {
470476
IdentifierID addContainingModuleRef(const DeclContext *DC);
471477

472478
/// Write a normal protocol conformance.
473-
void writeNormalConformance(const NormalProtocolConformance *conformance);
479+
void writeASTBlockEntity(const NormalProtocolConformance *conformance);
474480

475481
/// Write a SILLayout.
476-
void writeSILLayout(const SILLayout *layout);
482+
void writeASTBlockEntity(const SILLayout *layout);
477483

478484
/// Writes a protocol conformance.
479485
///

0 commit comments

Comments
 (0)