Skip to content

Commit 8c06223

Browse files
committed
[Serialization] Make ASTDeserializer stateful, and use that state
...for decl attributes. Also, rename to DeclDeserializer.
1 parent e540874 commit 8c06223

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

include/swift/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ModuleFile
4848
: public LazyMemberLoader,
4949
public LazyConformanceLoader {
5050
friend class SerializedASTFile;
51-
friend class ASTDeserializer;
51+
friend class DeclDeserializer;
5252
friend class SILDeserializer;
5353
using Status = serialization::Status;
5454
using TypeID = serialization::TypeID;

lib/Serialization/Deserialization.cpp

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,27 @@ StringRef swift::getNameOfModule(const ModuleFile *MF) {
5555
}
5656

5757
namespace {
58-
struct IDAndKind {
58+
struct OffsetAndKind {
5959
const Decl *D;
60-
DeclID ID;
60+
uint64_t offset;
6161
};
6262

63-
static raw_ostream &operator<<(raw_ostream &os, IDAndKind &&pair) {
63+
static raw_ostream &operator<<(raw_ostream &os, OffsetAndKind &&pair) {
6464
return os << Decl::getKindName(pair.D->getKind())
65-
<< "Decl #" << pair.ID;
65+
<< "Decl @ " << pair.offset;
6666
}
6767

6868
class PrettyDeclDeserialization : public llvm::PrettyStackTraceEntry {
6969
const ModuleFile *MF;
7070
const ModuleFile::Serialized<Decl*> &DeclOrOffset;
71-
DeclID ID;
71+
uint64_t offset;
7272
decls_block::RecordKind Kind;
7373
public:
7474
PrettyDeclDeserialization(ModuleFile *module,
7575
const ModuleFile::Serialized<Decl*> &declOrOffset,
76-
DeclID DID, decls_block::RecordKind kind)
77-
: MF(module), DeclOrOffset(declOrOffset), ID(DID), Kind(kind) {
76+
decls_block::RecordKind kind)
77+
: MF(module), DeclOrOffset(declOrOffset), offset(declOrOffset),
78+
Kind(kind) {
7879
}
7980

8081
static const char *getRecordKindString(decls_block::RecordKind Kind) {
@@ -88,18 +89,19 @@ namespace {
8889

8990
void print(raw_ostream &os) const override {
9091
if (!DeclOrOffset.isComplete()) {
91-
os << "While deserializing decl #" << ID << " ("
92+
os << "While deserializing decl @ " << offset << " ("
9293
<< getRecordKindString(Kind) << ")";
9394
} else {
9495
os << "While deserializing ";
9596

9697
if (auto VD = dyn_cast<ValueDecl>(DeclOrOffset.get())) {
97-
os << "'" << VD->getBaseName() << "' (" << IDAndKind{VD, ID} << ")";
98+
os << "'" << VD->getBaseName() << "' (" << OffsetAndKind{VD, offset}
99+
<< ")";
98100
} else if (auto ED = dyn_cast<ExtensionDecl>(DeclOrOffset.get())) {
99101
os << "extension of '" << ED->getExtendedType() << "' ("
100-
<< IDAndKind{ED, ID} << ")";
102+
<< OffsetAndKind{ED, offset} << ")";
101103
} else {
102-
os << IDAndKind{DeclOrOffset.get(), ID};
104+
os << OffsetAndKind{DeclOrOffset.get(), offset};
103105
}
104106
}
105107
os << " in '" << getNameOfModule(MF) << "'\n";
@@ -2226,7 +2228,7 @@ Decl *ModuleFile::getDecl(DeclID DID) {
22262228
}
22272229

22282230
/// Used to split up methods that would otherwise live in ModuleFile.
2229-
class swift::ASTDeserializer {
2231+
class swift::DeclDeserializer {
22302232
template <typename T>
22312233
using Serialized = ModuleFile::Serialized<T>;
22322234
using TypeID = serialization::TypeID;
@@ -2289,20 +2291,35 @@ class swift::ASTDeserializer {
22892291
};
22902292

22912293
ModuleFile &MF;
2294+
Serialized<Decl *> &declOrOffset;
2295+
2296+
DeclAttribute *DAttrs = nullptr;
2297+
DeclAttribute **AttrsNext = &DAttrs;
2298+
2299+
void AddAttribute(DeclAttribute *Attr) {
2300+
// Advance the linked list.
2301+
// This isn't just using DeclAttributes because that would result in the
2302+
// attributes getting reversed.
2303+
// FIXME: If we reverse them at serialization time we could get rid of this.
2304+
*AttrsNext = Attr;
2305+
AttrsNext = Attr->getMutableNext();
2306+
};
2307+
22922308
public:
2293-
ASTDeserializer(ModuleFile &MF) : MF(MF) {}
2309+
DeclDeserializer(ModuleFile &MF, Serialized<Decl *> &declOrOffset)
2310+
: MF(MF), declOrOffset(declOrOffset) {}
22942311

22952312
/// Deserializes decl attribute and attribute-like records from
22962313
/// \c MF.DeclTypesCursor until a non-attribute record is found,
22972314
/// passing each one to \p AddAttribute.
22982315
llvm::Error deserializeDeclAttributes(
22992316
ASTContext &ctx,
2300-
llvm::function_ref<void (DeclAttribute *)> AddAttribute,
23012317
FilenameForPrivateRAII &filenameForPrivate,
23022318
LocalDiscriminatorRAII &localDiscriminatorRAII,
23032319
PrivateDiscriminatorRAII &privateDiscriminatorRAII);
23042320

2305-
Expected<Decl *> getDeclCheckedImpl(DeclID DID);
2321+
static Expected<Decl *> getDeclCheckedImpl(ModuleFile &MF, DeclID DID);
2322+
Expected<Decl *> getDeclCheckedImpl();
23062323

23072324
Expected<Decl *> deserializeTypeAlias(Serialized<Decl *> &declOrOffset,
23082325
ArrayRef<uint64_t> scratch,
@@ -2362,7 +2379,7 @@ Expected<Decl *>
23622379
ModuleFile::getDeclChecked(DeclID DID) {
23632380
// Tag every deserialized ValueDecl coming out of getDeclChecked with its ID.
23642381
Expected<Decl *> deserialized =
2365-
ASTDeserializer(*this).getDeclCheckedImpl(DID);
2382+
DeclDeserializer::getDeclCheckedImpl(*this, DID);
23662383
if (deserialized && deserialized.get()) {
23672384
if (auto *IDC = dyn_cast<IterableDeclContext>(deserialized.get())) {
23682385
// Only set the DeclID on the returned Decl if it's one that was loaded
@@ -2385,9 +2402,8 @@ static bool attributeChainContains(DeclAttribute *attr) {
23852402
return tempAttrs.hasAttribute<DERIVED>();
23862403
}
23872404

2388-
llvm::Error ASTDeserializer::deserializeDeclAttributes(
2405+
llvm::Error DeclDeserializer::deserializeDeclAttributes(
23892406
ASTContext &ctx,
2390-
llvm::function_ref<void (DeclAttribute *)> AddAttribute,
23912407
FilenameForPrivateRAII &filenameForPrivate,
23922408
LocalDiscriminatorRAII &localDiscriminatorRAII,
23932409
PrivateDiscriminatorRAII &privateDiscriminatorRAII) {
@@ -2663,7 +2679,7 @@ llvm::Error ASTDeserializer::deserializeDeclAttributes(
26632679
}
26642680

26652681
Expected<Decl *>
2666-
ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2682+
DeclDeserializer::getDeclCheckedImpl(ModuleFile &MF, DeclID DID) {
26672683
if (DID == 0)
26682684
return nullptr;
26692685

@@ -2677,23 +2693,16 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
26772693
BCOffsetRAII restoreOffset(MF.DeclTypeCursor);
26782694
MF.DeclTypeCursor.JumpToBit(declOrOffset);
26792695

2696+
return DeclDeserializer(MF, declOrOffset).getDeclCheckedImpl();
2697+
}
2698+
2699+
Expected<Decl *>
2700+
DeclDeserializer::getDeclCheckedImpl() {
26802701
ASTContext &ctx = MF.getContext();
26812702

26822703
if (auto s = ctx.Stats)
26832704
s->getFrontendCounters().NumDeclsDeserialized++;
26842705

2685-
// Read the attributes (if any).
2686-
// This isn't just using DeclAttributes because that would result in the
2687-
// attributes getting reversed.
2688-
// FIXME: If we reverse them at serialization time we could get rid of this.
2689-
DeclAttribute *DAttrs = nullptr;
2690-
DeclAttribute **AttrsNext = &DAttrs;
2691-
auto AddAttribute = [&](DeclAttribute *Attr) {
2692-
// Advance the linked list.
2693-
*AttrsNext = Attr;
2694-
AttrsNext = Attr->getMutableNext();
2695-
};
2696-
26972706
PrivateDiscriminatorRAII privateDiscriminatorRAII{MF, declOrOffset};
26982707
LocalDiscriminatorRAII localDiscriminatorRAII(declOrOffset);
26992708
ModuleFile::DeserializingEntityRAII deserializingEntity(MF);
@@ -2710,7 +2719,7 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
27102719
nominal->setInherited(inheritedTypes);
27112720
};
27122721

2713-
auto attrError = deserializeDeclAttributes(ctx, AddAttribute,
2722+
auto attrError = deserializeDeclAttributes(ctx,
27142723
filenameForPrivate,
27152724
localDiscriminatorRAII,
27162725
privateDiscriminatorRAII);
@@ -2740,7 +2749,7 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
27402749
&blobData);
27412750

27422751
PrettyDeclDeserialization stackTraceEntry(
2743-
&MF, declOrOffset, DID, static_cast<decls_block::RecordKind>(recordID));
2752+
&MF, declOrOffset, static_cast<decls_block::RecordKind>(recordID));
27442753

27452754
switch (recordID) {
27462755
case decls_block::TypeAliasLayout::Code:

0 commit comments

Comments
 (0)