Skip to content

Commit ae9d862

Browse files
committed
[Serialization] Split doc serialization out of the Serializer class
Extract the common utilities into a SerializerBase class, and make a new DocSerializer.
1 parent 6bcfc78 commit ae9d862

File tree

3 files changed

+86
-65
lines changed

3 files changed

+86
-65
lines changed

lib/Serialization/Serialization.cpp

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -771,38 +771,35 @@ NormalConformanceID Serializer::addConformanceRef(
771771
}
772772

773773
/// Record the name of a block.
774-
static void emitBlockID(llvm::BitstreamWriter &out, unsigned ID,
775-
StringRef name,
776-
SmallVectorImpl<unsigned char> &nameBuffer) {
774+
void SerializerBase::emitBlockID(unsigned ID, StringRef name,
775+
SmallVectorImpl<unsigned char> &nameBuffer) {
777776
SmallVector<unsigned, 1> idBuffer;
778777
idBuffer.push_back(ID);
779-
out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, idBuffer);
778+
Out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, idBuffer);
780779

781780
// Emit the block name if present.
782781
if (name.empty())
783782
return;
784783
nameBuffer.resize(name.size());
785784
memcpy(nameBuffer.data(), name.data(), name.size());
786-
out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, nameBuffer);
785+
Out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, nameBuffer);
787786
}
788787

789-
/// Record the name of a record within a block.
790-
static void emitRecordID(llvm::BitstreamWriter &out, unsigned ID,
791-
StringRef name,
792-
SmallVectorImpl<unsigned char> &nameBuffer) {
788+
void SerializerBase::emitRecordID(unsigned ID, StringRef name,
789+
SmallVectorImpl<unsigned char> &nameBuffer) {
793790
assert(ID < 256 && "can't fit record ID in next to name");
794791
nameBuffer.resize(name.size()+1);
795792
nameBuffer[0] = ID;
796793
memcpy(nameBuffer.data()+1, name.data(), name.size());
797-
out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, nameBuffer);
794+
Out.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, nameBuffer);
798795
}
799796

800797
void Serializer::writeBlockInfoBlock() {
801798
BCBlockRAII restoreBlock(Out, llvm::bitc::BLOCKINFO_BLOCK_ID, 2);
802799

803800
SmallVector<unsigned char, 64> nameBuffer;
804-
#define BLOCK(X) emitBlockID(Out, X ## _ID, #X, nameBuffer)
805-
#define BLOCK_RECORD(K, X) emitRecordID(Out, K::X, #X, nameBuffer)
801+
#define BLOCK(X) emitBlockID(X ## _ID, #X, nameBuffer)
802+
#define BLOCK_RECORD(K, X) emitRecordID(K::X, #X, nameBuffer)
806803

807804
BLOCK(MODULE_BLOCK);
808805

@@ -891,7 +888,7 @@ void Serializer::writeBlockInfoBlock() {
891888
BLOCK_RECORD(sil_block, SIL_TWO_OPERANDS_EXTRA_ATTR);
892889

893890
// These layouts can exist in both decl blocks and sil blocks.
894-
#define BLOCK_RECORD_WITH_NAMESPACE(K, X) emitRecordID(Out, X, #X, nameBuffer)
891+
#define BLOCK_RECORD_WITH_NAMESPACE(K, X) emitRecordID(X, #X, nameBuffer)
895892
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
896893
decls_block::INVALID_PROTOCOL_CONFORMANCE);
897894
BLOCK_RECORD_WITH_NAMESPACE(sil_block,
@@ -932,28 +929,6 @@ void Serializer::writeBlockInfoBlock() {
932929
#undef BLOCK_RECORD
933930
}
934931

935-
void Serializer::writeDocBlockInfoBlock() {
936-
BCBlockRAII restoreBlock(Out, llvm::bitc::BLOCKINFO_BLOCK_ID, 2);
937-
938-
SmallVector<unsigned char, 64> nameBuffer;
939-
#define BLOCK(X) emitBlockID(Out, X ## _ID, #X, nameBuffer)
940-
#define BLOCK_RECORD(K, X) emitRecordID(Out, K::X, #X, nameBuffer)
941-
942-
BLOCK(MODULE_DOC_BLOCK);
943-
944-
BLOCK(CONTROL_BLOCK);
945-
BLOCK_RECORD(control_block, METADATA);
946-
BLOCK_RECORD(control_block, MODULE_NAME);
947-
BLOCK_RECORD(control_block, TARGET);
948-
949-
BLOCK(COMMENT_BLOCK);
950-
BLOCK_RECORD(comment_block, DECL_COMMENTS);
951-
BLOCK_RECORD(comment_block, GROUP_NAMES);
952-
953-
#undef BLOCK
954-
#undef BLOCK_RECORD
955-
}
956-
957932
void Serializer::writeHeader(const SerializationOptions &options) {
958933
{
959934
BCBlockRAII restoreBlock(Out, CONTROL_BLOCK_ID, 3);
@@ -4686,22 +4661,20 @@ void Serializer::writeAST(ModuleOrSourceFile DC,
46864661
}
46874662
}
46884663

4689-
void Serializer::writeToStream(raw_ostream &os) {
4664+
void SerializerBase::writeToStream(raw_ostream &os) {
46904665
os.write(Buffer.data(), Buffer.size());
46914666
os.flush();
46924667
}
46934668

4694-
Serializer::Serializer(ArrayRef<unsigned char> signature,
4695-
ModuleOrSourceFile DC) {
4669+
SerializerBase::SerializerBase(ArrayRef<unsigned char> signature,
4670+
ModuleOrSourceFile DC) {
46964671
for (unsigned char byte : signature)
46974672
Out.Emit(byte, 8);
46984673

46994674
this->M = getModule(DC);
47004675
this->SF = DC.dyn_cast<SourceFile *>();
47014676
}
47024677

4703-
Serializer::~Serializer() = default;
4704-
47054678
void Serializer::writeToStream(raw_ostream &os, ModuleOrSourceFile DC,
47064679
const SILModule *SILMod,
47074680
const SerializationOptions &options) {
@@ -4748,8 +4721,7 @@ void swift::serialize(ModuleOrSourceFile DC,
47484721
options.DocOutputPath,
47494722
[&](raw_ostream &out) {
47504723
SharedTimer timer("Serialization, swiftdoc");
4751-
Serializer::writeDocToStream(out, DC, options.GroupInfoPath,
4752-
getContext(DC));
4724+
writeDocToStream(out, DC, options.GroupInfoPath);
47534725
return false;
47544726
});
47554727
}

lib/Serialization/Serialization.h

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ namespace serialization {
3434

3535
using FilenamesTy = ArrayRef<std::string>;
3636

37-
class Serializer {
37+
class SerializerBase {
38+
protected:
3839
SmallVector<char, 0> Buffer;
3940
llvm::BitstreamWriter Out{Buffer};
4041

@@ -50,6 +51,21 @@ class Serializer {
5051
/// serialized. Any other decls will be cross-referenced instead.
5152
const SourceFile *SF = nullptr;
5253

54+
/// Record the name of a block.
55+
void emitBlockID(unsigned ID, StringRef name,
56+
SmallVectorImpl<unsigned char> &nameBuffer);
57+
58+
/// Record the name of a record within a block.
59+
void emitRecordID(unsigned ID, StringRef name,
60+
SmallVectorImpl<unsigned char> &nameBuffer);
61+
62+
void writeToStream(raw_ostream &os);
63+
64+
public:
65+
SerializerBase(ArrayRef<unsigned char> signature, ModuleOrSourceFile DC);
66+
};
67+
68+
class Serializer : public SerializerBase {
5369
public:
5470
/// Stores a declaration or a type to be written to the AST file.
5571
///
@@ -313,16 +329,10 @@ class Serializer {
313329
/// Writes the BLOCKINFO block for the serialized module file.
314330
void writeBlockInfoBlock();
315331

316-
/// Writes the BLOCKINFO block for the module documentation file.
317-
void writeDocBlockInfoBlock();
318-
319332
/// Writes the Swift module file header and name, plus metadata determining
320333
/// if the module can be loaded.
321334
void writeHeader(const SerializationOptions &options = {});
322335

323-
/// Writes the Swift doc module file header and name.
324-
void writeDocHeader();
325-
326336
/// Writes the dependencies used to build this module: its imported
327337
/// modules and its source files.
328338
void writeInputBlock(const SerializationOptions &options);
@@ -439,21 +449,15 @@ class Serializer {
439449
void writeAST(ModuleOrSourceFile DC,
440450
bool enableNestedTypeLookupTable);
441451

442-
void writeToStream(raw_ostream &os);
443-
444-
Serializer(ArrayRef<unsigned char> signature, ModuleOrSourceFile DC);
445-
~Serializer();
452+
using SerializerBase::SerializerBase;
453+
using SerializerBase::writeToStream;
446454

447455
public:
448456
/// Serialize a module to the given stream.
449457
static void writeToStream(raw_ostream &os, ModuleOrSourceFile DC,
450458
const SILModule *M,
451459
const SerializationOptions &options);
452460

453-
/// Serialize module documentation to the given stream.
454-
static void writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
455-
StringRef GroupInfoPath, ASTContext &Ctx);
456-
457461
/// Records the use of the given Type.
458462
///
459463
/// The Type will be scheduled for serialization if necessary.
@@ -562,6 +566,10 @@ class Serializer {
562566
void writeGenericRequirements(ArrayRef<Requirement> requirements,
563567
const std::array<unsigned, 256> &abbrCodes);
564568
};
569+
570+
/// Serialize module documentation to the given stream.
571+
void writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
572+
StringRef GroupInfoPath);
565573
} // end namespace serialization
566574
} // end namespace swift
567575
#endif

lib/Serialization/SerializeDoc.cpp

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ using pFileNameToGroupNameMap = std::unique_ptr<FileNameToGroupNameMap>;
3838
namespace {
3939
class YamlGroupInputParser {
4040
StringRef RecordPath;
41-
std::string Separator = "/";
41+
static constexpr const char * const Separator = "/";
42+
43+
// FIXME: This isn't thread-safe.
4244
static llvm::StringMap<pFileNameToGroupNameMap> AllMaps;
4345

4446
bool parseRoot(FileNameToGroupNameMap &Map, llvm::yaml::Node *Root,
@@ -136,7 +138,7 @@ llvm::StringMap<pFileNameToGroupNameMap> YamlGroupInputParser::AllMaps;
136138

137139
class DeclGroupNameContext {
138140
struct GroupNameCollector {
139-
const std::string NullGroupName = "";
141+
static const StringLiteral NullGroupName;
140142
const bool Enable;
141143
GroupNameCollector(bool Enable) : Enable(Enable) {}
142144
virtual ~GroupNameCollector() = default;
@@ -163,7 +165,7 @@ class DeclGroupNameContext {
163165
if (!PathOp.hasValue())
164166
return NullGroupName;
165167
StringRef FullPath =
166-
VD->getASTContext().SourceMgr.getIdentifierForBuffer(PathOp.getValue());
168+
Ctx.SourceMgr.getIdentifierForBuffer(PathOp.getValue());
167169
if (!pMap) {
168170
YamlGroupInputParser Parser(RecordPath);
169171
if (!Parser.parse()) {
@@ -209,6 +211,9 @@ class DeclGroupNameContext {
209211
}
210212
};
211213

214+
const StringLiteral
215+
DeclGroupNameContext::GroupNameCollector::NullGroupName = "";
216+
212217
struct DeclCommentTableData {
213218
StringRef Brief;
214219
RawComment Raw;
@@ -276,6 +281,42 @@ class DeclCommentTableInfo {
276281
}
277282
};
278283

284+
class DocSerializer : public SerializerBase {
285+
public:
286+
using SerializerBase::SerializerBase;
287+
using SerializerBase::writeToStream;
288+
289+
using SerializerBase::Out;
290+
using SerializerBase::M;
291+
using SerializerBase::SF;
292+
293+
/// Writes the BLOCKINFO block for the module documentation file.
294+
void writeDocBlockInfoBlock() {
295+
BCBlockRAII restoreBlock(Out, llvm::bitc::BLOCKINFO_BLOCK_ID, 2);
296+
297+
SmallVector<unsigned char, 64> nameBuffer;
298+
#define BLOCK(X) emitBlockID(X ## _ID, #X, nameBuffer)
299+
#define BLOCK_RECORD(K, X) emitRecordID(K::X, #X, nameBuffer)
300+
301+
BLOCK(MODULE_DOC_BLOCK);
302+
303+
BLOCK(CONTROL_BLOCK);
304+
BLOCK_RECORD(control_block, METADATA);
305+
BLOCK_RECORD(control_block, MODULE_NAME);
306+
BLOCK_RECORD(control_block, TARGET);
307+
308+
BLOCK(COMMENT_BLOCK);
309+
BLOCK_RECORD(comment_block, DECL_COMMENTS);
310+
BLOCK_RECORD(comment_block, GROUP_NAMES);
311+
312+
#undef BLOCK
313+
#undef BLOCK_RECORD
314+
}
315+
316+
/// Writes the Swift doc module file header and name.
317+
void writeDocHeader();
318+
};
319+
279320
} // end anonymous namespace
280321

281322
static void writeGroupNames(const comment_block::GroupNamesLayout &GroupNames,
@@ -422,7 +463,7 @@ static void writeDeclCommentTable(
422463
DeclCommentList.emit(scratch, tableOffset, hashTableBlob);
423464
}
424465

425-
void Serializer::writeDocHeader() {
466+
void DocSerializer::writeDocHeader() {
426467
{
427468
BCBlockRAII restoreBlock(Out, CONTROL_BLOCK_ID, 3);
428469
control_block::ModuleNameLayout ModuleName(Out);
@@ -440,9 +481,9 @@ void Serializer::writeDocHeader() {
440481
}
441482
}
442483

443-
void Serializer::writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
444-
StringRef GroupInfoPath, ASTContext &Ctx) {
445-
Serializer S{MODULE_DOC_SIGNATURE, DC};
484+
void serialization::writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
485+
StringRef GroupInfoPath) {
486+
DocSerializer S{MODULE_DOC_SIGNATURE, DC};
446487
// FIXME: This is only really needed for debugging. We don't actually use it.
447488
S.writeDocBlockInfoBlock();
448489

@@ -451,7 +492,7 @@ void Serializer::writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
451492
S.writeDocHeader();
452493
{
453494
BCBlockRAII restoreBlock(S.Out, COMMENT_BLOCK_ID, 4);
454-
DeclGroupNameContext GroupContext(GroupInfoPath, Ctx);
495+
DeclGroupNameContext GroupContext(GroupInfoPath, S.M->getASTContext());
455496
comment_block::DeclCommentListLayout DeclCommentList(S.Out);
456497
writeDeclCommentTable(DeclCommentList, S.SF, S.M, GroupContext);
457498
comment_block::GroupNamesLayout GroupNames(S.Out);

0 commit comments

Comments
 (0)