-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSerialization.h
599 lines (482 loc) · 21.9 KB
/
Serialization.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//===--- Serialization.h - Read and write Swift modules ---------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the Serializer interface.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_SERIALIZATION_SERIALIZATION_H
#define SWIFT_SERIALIZATION_SERIALIZATION_H
#include "ModuleFormat.h"
#include "swift/Basic/LLVMExtras.h"
#include "swift/Serialization/SerializationOptions.h"
#include "swift/Subsystems.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/RequirementSignature.h"
#include "swift/Basic/LLVM.h"
#include "llvm/ADT/MapVector.h"
#include <array>
#include <queue>
#include <tuple>
namespace clang {
class Type;
}
namespace swift {
class SILModule;
namespace fine_grained_dependencies {
class SourceFileDepGraph;
}
namespace serialization {
using FilenamesTy = ArrayRef<std::string>;
class SerializerBase {
protected:
SmallVector<char, 0> Buffer;
llvm::BitstreamWriter Out{Buffer};
/// A reusable buffer for emitting records.
SmallVector<uint64_t, 64> ScratchRecord;
/// The module currently being serialized.
const ModuleDecl *M = nullptr;
/// The SourceFile currently being serialized, if any.
///
/// If this is non-null, only decls actually from this SourceFile will be
/// serialized. Any other decls will be cross-referenced instead.
const SourceFile *SF = nullptr;
/// Record the name of a block.
void emitBlockID(unsigned ID, StringRef name,
SmallVectorImpl<unsigned char> &nameBuffer);
/// Record the name of a record within a block.
void emitRecordID(unsigned ID, StringRef name,
SmallVectorImpl<unsigned char> &nameBuffer,
SmallVectorImpl<unsigned> *wideNameBuffer = nullptr);
void writeToStream(raw_ostream &os);
public:
SerializerBase(ArrayRef<unsigned char> signature, ModuleOrSourceFile DC);
ASTContext &getASTContext() const;
};
class Serializer : public SerializerBase {
class DeclSerializer;
friend class DeclSerializer;
class TypeSerializer;
friend class TypeSerializer;
const SerializationOptions &Options;
/// A map from non-identifier uniqued strings to their serialized IDs.
///
/// Since we never remove items from this map, we can use a BumpPtrAllocator
/// to back the entries.
llvm::StringMap<IdentifierID, llvm::BumpPtrAllocator> UniquedStringIDs;
/// A map from Identifiers to their serialized IDs.
///
/// This is stored separately from \p UniquedStringIDs because it's faster
/// to do lookups in, even though that may lead to some duplication between
/// identifier and non-identifier strings.
llvm::DenseMap<Identifier, IdentifierID> IdentifierIDs;
/// All uniqued strings that need to be serialized (identifiers and
/// non-identifiers).
std::vector<StringRef> StringsToWrite;
/// The last assigned IdentifierID for uniqued strings from this module.
///
/// Note that special module IDs and IDs of special names must not be valid
/// IdentifierIDs, except that 0 will always represent the empty identifier.
uint32_t /*IdentifierID*/ LastUniquedStringID =
serialization::NUM_SPECIAL_IDS - 1;
SmallVector<DeclID, 16> exportedPrespecializationDecls;
/// Will be set to true if any serialization step failed, for example due to
/// an error in the AST.
bool hadError = false;
/// Helper for serializing entities in the AST block object graph.
///
/// Keeps track of assigning IDs to newly-seen entities, and collecting
/// offsets when those entities are ready to be serialized.
///
/// \tparam T The AST type that's being serialized
/// \tparam ID The ID type for encoding into the bitstream
/// \tparam RecordCode_ The code for the offsets record in the Index block for
/// referring to these entities.
template <typename T, typename ID, unsigned RecordCode_>
class ASTBlockRecordKeeper {
/// Map of entities to assigned ID numbers.
llvm::DenseMap<T, ID> IDs;
/// All entities that still need to be written.
///
/// This is a queue and not simply a vector because serializing one
/// entity might result in another one getting queued up.
std::queue<T> EntitiesToWrite;
/// The offsets of entities that have already been written.
///
/// `Offsets[entityID - 1]` gives the offset for a particular entity. (0 is
/// reserved for "empty" entities.)
std::vector<BitOffset> Offsets;
public:
/// The code for the offsets record in the Index block for referring to
/// these entities.
static const unsigned RecordCode = RecordCode_;
/// Assigns \p entity an ID and schedules it for writing.
///
/// If \p entity has been seen before, returns the existing ID.
///
/// 0 is used for "empty" entities (those that coerce to \c false ).
ID addRef(T entity) {
assert(ID() == 0 && "bad default constructor for ID");
if (!entity)
return 0;
auto &entityID = IDs[entity];
if (entityID == ID()) {
EntitiesToWrite.push(entity);
entityID = IDs.size();
}
return entityID;
}
/// Returns true if \p entity has been seen before (i.e. ever passed to
/// #addRef).
bool hasRef(T entity) const {
return IDs.find(entity) != IDs.end();
}
bool hasMoreToSerialize() const {
return !EntitiesToWrite.empty();
}
/// Returns the next entity to be written.
///
/// If there is nothing left to serialize, returns None.
std::optional<T> peekNext() const {
if (!hasMoreToSerialize())
return std::nullopt;
return EntitiesToWrite.front();
}
/// Records that the next entity will be written at \p offset, and returns
/// it so it can be written.
///
/// If there is nothing left to serialize, returns None.
std::optional<T> popNext(BitOffset offset) {
if (!hasMoreToSerialize())
return std::nullopt;
T result = EntitiesToWrite.front();
EntitiesToWrite.pop();
Offsets.push_back(offset);
assert(IDs.lookup(result) == Offsets.size());
return result;
}
/// Returns the offsets for all entities that have been written.
///
/// Should only be called after all entities \e have been written.
ArrayRef<BitOffset> getOffsets() const {
assert(!hasMoreToSerialize() && "not all entities were serialized");
return Offsets;
}
};
ASTBlockRecordKeeper<const Decl *, DeclID,
index_block::DECL_OFFSETS>
DeclsToSerialize;
ASTBlockRecordKeeper<Type, TypeID,
index_block::TYPE_OFFSETS>
TypesToSerialize;
ASTBlockRecordKeeper<const clang::Type *, ClangTypeID,
index_block::CLANG_TYPE_OFFSETS>
ClangTypesToSerialize;
ASTBlockRecordKeeper<const DeclContext *, LocalDeclContextID,
index_block::LOCAL_DECL_CONTEXT_OFFSETS>
LocalDeclContextsToSerialize;
ASTBlockRecordKeeper<GenericSignature, GenericSignatureID,
index_block::GENERIC_SIGNATURE_OFFSETS>
GenericSignaturesToSerialize;
ASTBlockRecordKeeper<const GenericEnvironment *, GenericEnvironmentID,
index_block::GENERIC_ENVIRONMENT_OFFSETS>
GenericEnvironmentsToSerialize;
ASTBlockRecordKeeper<SubstitutionMap, SubstitutionMapID,
index_block::SUBSTITUTION_MAP_OFFSETS>
SubstitutionMapsToSerialize;
ASTBlockRecordKeeper<ProtocolConformance *, ProtocolConformanceID,
index_block::PROTOCOL_CONFORMANCE_OFFSETS>
ConformancesToSerialize;
ASTBlockRecordKeeper<PackConformance *, ProtocolConformanceID,
index_block::PACK_CONFORMANCE_OFFSETS>
PackConformancesToSerialize;
ASTBlockRecordKeeper<const SILLayout *, SILLayoutID,
index_block::SIL_LAYOUT_OFFSETS>
SILLayoutsToSerialize;
public:
using DeclTableData = SmallVector<std::pair<uint8_t, DeclID>, 4>;
/// The in-memory representation of what will eventually be an on-disk hash
/// table.
using DeclTable = llvm::MapVector<DeclBaseName, DeclTableData>;
using ObjCMethodTableData =
SmallVector<std::tuple<std::string, bool, DeclID>, 4>;
// In-memory representation of what will eventually be an on-disk
// hash table of all defined Objective-C methods.
using ObjCMethodTable = llvm::MapVector<ObjCSelector, ObjCMethodTableData>;
using NestedTypeDeclsData = SmallVector<std::pair<DeclID, DeclID>, 4>;
// In-memory representation of what will eventually be an on-disk
// hash table of all defined Objective-C methods.
using NestedTypeDeclsTable = llvm::MapVector<Identifier, NestedTypeDeclsData>;
using DeclMembersData = SmallVector<DeclID, 2>;
// In-memory representation of what will eventually be an on-disk
// hash table of all ValueDecl-members of a particular DeclBaseName.
using DeclMembersTable = llvm::MapVector<uint32_t, DeclMembersData>;
using DeclMemberNamesData = std::pair<serialization::BitOffset,
std::unique_ptr<DeclMembersTable>>;
// In-memory representation of what will eventually be an on-disk
// hash table mapping DeclBaseNames to DeclMembersData tables.
using DeclMemberNamesTable = llvm::MapVector<DeclBaseName, DeclMemberNamesData>;
using ExtensionTableData =
SmallVector<std::pair<const NominalTypeDecl *, DeclID>, 4>;
using ExtensionTable = llvm::MapVector<Identifier, ExtensionTableData>;
using DerivativeFunctionConfigTableData =
llvm::SmallVector<std::pair<std::string, GenericSignatureID>, 4>;
// In-memory representation of what will eventually be an on-disk hash table
// mapping original declaration USRs to derivative function configurations.
using DerivativeFunctionConfigTable =
llvm::MapVector<Identifier, DerivativeFunctionConfigTableData>;
// Uniqued mapping from original declarations USRs to derivative function
// configurations.
// Note: this exists because `GenericSignature` can be used as a `DenseMap`
// key, while `GenericSignatureID` cannot
// (`DenseMapInfo<GenericSignatureID>::getEmptyKey()` crashes). To work
// around this, a `UniquedDerivativeFunctionConfigTable` is first
// constructed, and then converted to a `DerivativeFunctionConfigTableData`.
using UniquedDerivativeFunctionConfigTable = llvm::MapVector<
Identifier,
swift::SmallSetVector<std::pair<Identifier, GenericSignature>, 4>>;
// In-memory representation of what will eventually be an on-disk
// hash table of the fingerprint associated with a serialized
// iterable decl context. It is keyed by that context's decl ID.
using DeclFingerprintsTable = llvm::MapVector<uint32_t, Fingerprint>;
private:
/// A map from identifiers to methods and properties with the given name.
///
/// This is used for id-style lookup.
DeclTable ClassMembersForDynamicLookup;
/// A map from DeclBaseNames of members to Decl->members sub-tables.
///
/// This is for Named Lazy Member Loading.
DeclMemberNamesTable DeclMemberNames;
enum {NumDeclTypeAbbrCodes = 512};
/// The abbreviation code for each record in the "decls-and-types" block.
///
/// These are registered up front when entering the block, so they can be
/// reused.
std::array<unsigned, NumDeclTypeAbbrCodes> DeclTypeAbbrCodes;
/// The decls that adopt compiler-known protocols.
SmallVector<DeclID, 2> KnownProtocolAdopters[NumKnownProtocols];
/// Writes the BLOCKINFO block for the serialized module file.
void writeBlockInfoBlock();
/// Writes the Swift module file header and name, plus metadata determining
/// if the module can be loaded.
void writeHeader();
/// Writes the dependencies used to build this module: its imported
/// modules and its source files.
void writeInputBlock();
/// Check if a decl is cross-referenced.
bool isDeclXRef(const Decl *D) const;
/// Check if a decl should be skipped during serialization.
bool shouldSkipDecl(const Decl *D) const;
/// Writes a reference to a decl in another module.
void writeCrossReference(const DeclContext *DC, uint32_t pathLen = 1);
/// Writes a reference to a decl in another module.
void writeCrossReference(const Decl *D);
/// Writes the given decl.
void writeASTBlockEntity(const Decl *D);
/// Write a DeclContext as a local DeclContext at the current offset.
void writeASTBlockEntity(const DeclContext *DC);
/// Write the components of a PatternBindingInitializer as a local context.
void writePatternBindingInitializer(PatternBindingDecl *binding,
unsigned bindingIndex);
/// Write the components of a DefaultArgumentInitializer as a local context.
void writeDefaultArgumentInitializer(const DeclContext *parentContext, unsigned index);
/// Write the components of an AbstractClosureExpr as a local context.
void writeAbstractClosureExpr(const DeclContext *parentContext, Type Ty, bool isImplicit, unsigned discriminator);
/// Writes the given type.
void writeASTBlockEntity(Type ty);
/// Writes the given Clang type.
void writeASTBlockEntity(const clang::Type *ty);
/// Writes a generic signature.
void writeASTBlockEntity(GenericSignature sig);
/// Writes a generic environment.
void writeASTBlockEntity(const GenericEnvironment *env);
/// Writes a substitution map.
void writeASTBlockEntity(const SubstitutionMap substitutions);
/// Writes a protocol conformance.
void writeASTBlockEntity(ProtocolConformance *conformance);
void writeLocalNormalProtocolConformance(NormalProtocolConformance *);
/// Writes a pack conformance.
void writeASTBlockEntity(PackConformance *conformance);
/// Writes lifetime dependencies
void writeLifetimeDependencies(
ArrayRef<LifetimeDependenceInfo> lifetimeDependenceInfo);
/// Registers the abbreviation for the given decl or type layout.
template <typename Layout>
void registerDeclTypeAbbr() {
using AbbrArrayTy = decltype(DeclTypeAbbrCodes);
static_assert(Layout::Code <= std::tuple_size<AbbrArrayTy>::value,
"layout has invalid record code");
DeclTypeAbbrCodes[Layout::Code] = Layout::emitAbbrev(Out);
}
/// Writes all queued \p entities until there are no more.
///
/// \returns true if any entities were written
template <typename SpecificASTBlockRecordKeeper>
bool writeASTBlockEntitiesIfNeeded(SpecificASTBlockRecordKeeper &entities);
/// Writes all decls and types in the DeclsToWrite queue.
///
/// This will continue until the queue is empty, even if the items currently
/// in the queue trigger the serialization of additional decls and/or types.
void writeAllDeclsAndTypes();
/// Writes all identifiers in the IdentifiersToWrite queue.
///
/// This must be called after writeAllDeclsAndTypes(), since that may add
/// additional identifiers to the pool.
std::vector<CharOffset> writeAllIdentifiers();
/// Writes the offsets for a serialized entity kind.
///
/// \see ASTBlockRecordKeeper
template <typename SpecificASTBlockRecordKeeper>
void writeOffsets(const index_block::OffsetsLayout &Offsets,
const SpecificASTBlockRecordKeeper &entities);
/// Serializes all transparent SIL functions in the SILModule.
void writeSIL(const SILModule *M, bool serializeAllSIL,
bool serializeDebugInfo);
/// Top-level entry point for serializing a module.
void writeAST(ModuleOrSourceFile DC);
/// Serializes the given dependency graph into the incremental information
/// section of this swift module.
void writeIncrementalInfo(
const fine_grained_dependencies::SourceFileDepGraph *DepGraph);
using SerializerBase::SerializerBase;
using SerializerBase::writeToStream;
public:
Serializer(ArrayRef<unsigned char> signature, ModuleOrSourceFile DC,
const SerializationOptions &options)
: SerializerBase(signature, DC), Options(options) {}
/// Serialize a module to the given stream.
static void
writeToStream(raw_ostream &os, ModuleOrSourceFile DC,
const SILModule *M,
const SerializationOptions &options,
const fine_grained_dependencies::SourceFileDepGraph *DepGraph);
/// Records the use of the given Type.
///
/// The Type will be scheduled for serialization if necessary.
///
/// \returns The ID for the given Type in this module.
TypeID addTypeRef(Type ty);
/// Records the use of the given C type.
///
/// The type will be scheduled for serialization if necessary.,
///
/// \returns The ID for the given type in this module.
ClangTypeID addClangTypeRef(const clang::Type *ty);
/// Records the use of the given DeclBaseName.
///
/// The Identifier will be scheduled for serialization if necessary.
///
/// \returns The ID for the given DeclBaseName in this module.
IdentifierID addDeclBaseNameRef(DeclBaseName ident);
/// Records the use of the given string, which will only be stored once in
/// the resulting module file.
///
/// \returns A pair containing the copy of the string now owned by the
/// Serializer and the ID for the string in this module.
/// \sa addUniquedStringRef
std::pair<StringRef, IdentifierID> addUniquedString(StringRef str);
/// Records the use of the given string, which will only be stored once in
/// the resulting module file.
///
/// \returns The ID for the given string in this module.
/// \sa addUniquedString
IdentifierID addUniquedStringRef(StringRef str) {
return addUniquedString(str).second;
}
/// Records the use of the given file name.
///
/// The Identifier will be scheduled for serialization if necessary.
///
/// \returns The ID for the given file name in this module.
IdentifierID addFilename(StringRef filename);
/// Records the use of the given Decl.
///
/// The Decl will be scheduled for serialization if necessary.
///
/// \returns The ID for the given Decl in this module.
DeclID addDeclRef(const Decl *D, bool allowTypeAliasXRef = false);
/// Records the use of the given DeclContext.
///
/// The DeclContext will be scheduled for serialization if necessary.
DeclContextID addDeclContextRef(const DeclContext *DC);
/// Records the use of the given local DeclContext.
///
/// The DeclContext will be scheduled for serialization if necessary.
LocalDeclContextID addLocalDeclContextRef(const DeclContext *DC);
/// Records the use of the given generic signature.
///
/// The GenericSignature will be scheduled for serialization if necessary.
GenericSignatureID addGenericSignatureRef(GenericSignature sig);
/// Records the use of the given opened generic environment.
GenericEnvironmentID addGenericEnvironmentRef(GenericEnvironment *env);
/// Records the use of the given substitution map.
///
/// The SubstitutionMap will be scheduled for serialization if necessary.
SubstitutionMapID addSubstitutionMapRef(SubstitutionMap substitutions);
/// Records the use of the given protocol conformance.
///
/// The protocol conformance will be scheduled for serialization
/// if necessary.
///
/// \returns The ID for the given conformance in this module.
ProtocolConformanceID addConformanceRef(ProtocolConformance *conformance);
ProtocolConformanceID addConformanceRef(PackConformance *conformance);
ProtocolConformanceID addConformanceRef(ProtocolConformanceRef conformance);
SmallVector<ProtocolConformanceID, 4>
addConformanceRefs(ArrayRef<ProtocolConformanceRef> conformances);
SmallVector<ProtocolConformanceID, 4>
addConformanceRefs(ArrayRef<ProtocolConformance *> conformances);
/// Records the use of the given SILLayout.
SILLayoutID addSILLayoutRef(const SILLayout *layout);
/// Records the module containing \p DC.
///
/// The module's name will be scheduled for serialization if necessary. This
/// may not be exactly the same as the name of the module containing DC;
/// instead, it will match the containing file's "exported module name".
///
/// \param ignoreExport When true, register the real module name,
/// ignoring exported_as definitions.
/// \returns The ID for the identifier for the module's name, or one of the
/// special module codes defined above.
/// \see FileUnit::getExportedModuleName
IdentifierID addContainingModuleRef(const DeclContext *DC,
bool ignoreExport);
/// Records the module \m.
IdentifierID addModuleRef(const ModuleDecl *m);
/// Write a SILLayout.
void writeASTBlockEntity(const SILLayout *layout);
/// Adds an encoding of the given list of generic requirements to
/// the given list of values.
void serializeGenericRequirements(ArrayRef<Requirement> requirements,
SmallVectorImpl<uint64_t> &scratch);
/// Writes a protocol's requirement signature, consisting of a list of
/// generic requirements and a list of protocol typealias records.
void writeRequirementSignature(const RequirementSignature &requirementSig);
/// Writes a protocol's associated type table.
void writeAssociatedTypes(ArrayRef<AssociatedTypeDecl *> assocTypes);
/// Writes a protocol's primary associated type table.
void writePrimaryAssociatedTypes(ArrayRef<AssociatedTypeDecl *> assocTypes);
bool allowCompilerErrors() const;
private:
/// If the declaration is invalid, records that an error occurred and returns
/// true if the decl should be skipped.
bool skipDeclIfInvalid(const Decl *decl);
/// If the type is invalid, records that an error occurred and returns
/// true if the type should be skipped.
bool skipTypeIfInvalid(Type ty, TypeRepr *tyRepr);
/// If the type is invalid, records that an error occurred and returns
/// true if the type should be skipped.
bool skipTypeIfInvalid(Type ty, SourceLoc loc);
};
} // end namespace serialization
} // end namespace swift
#endif