Skip to content

Commit b5a86cf

Browse files
committed
[Serialization] Move decl attribute deserialization into new helper
No functionality change.
1 parent 3273f06 commit b5a86cf

File tree

1 file changed

+131
-100
lines changed

1 file changed

+131
-100
lines changed

lib/Serialization/Deserialization.cpp

Lines changed: 131 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,82 +2231,6 @@ class swift::ASTDeserializer {
22312231
using Serialized = ModuleFile::Serialized<T>;
22322232
using TypeID = serialization::TypeID;
22332233

2234-
ModuleFile &MF;
2235-
public:
2236-
ASTDeserializer(ModuleFile &MF) : MF(MF) {}
2237-
2238-
Expected<Decl *> getDeclCheckedImpl(DeclID DID);
2239-
};
2240-
2241-
Expected<Decl *>
2242-
ModuleFile::getDeclChecked(DeclID DID) {
2243-
// Tag every deserialized ValueDecl coming out of getDeclChecked with its ID.
2244-
Expected<Decl *> deserialized =
2245-
ASTDeserializer(*this).getDeclCheckedImpl(DID);
2246-
if (deserialized && deserialized.get()) {
2247-
if (auto *IDC = dyn_cast<IterableDeclContext>(deserialized.get())) {
2248-
// Only set the DeclID on the returned Decl if it's one that was loaded
2249-
// and _wasn't_ one that had its DeclID set elsewhere (a followed XREF).
2250-
if (IDC->wasDeserialized() &&
2251-
static_cast<uint32_t>(IDC->getDeclID()) == 0) {
2252-
IDC->setDeclID(DID);
2253-
}
2254-
}
2255-
}
2256-
return deserialized;
2257-
}
2258-
2259-
template <typename DERIVED>
2260-
static bool attributeChainContains(DeclAttribute *attr) {
2261-
DeclAttributes tempAttrs;
2262-
tempAttrs.setRawAttributeChain(attr);
2263-
static_assert(std::is_trivially_destructible<DeclAttributes>::value,
2264-
"must not try to destroy the attribute chain");
2265-
return tempAttrs.hasAttribute<DERIVED>();
2266-
}
2267-
2268-
Expected<Decl *>
2269-
ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2270-
if (DID == 0)
2271-
return nullptr;
2272-
2273-
assert(DID <= MF.Decls.size() && "invalid decl ID");
2274-
auto &declOrOffset = MF.Decls[DID-1];
2275-
2276-
if (declOrOffset.isComplete())
2277-
return declOrOffset;
2278-
2279-
++NumDeclsLoaded;
2280-
BCOffsetRAII restoreOffset(MF.DeclTypeCursor);
2281-
MF.DeclTypeCursor.JumpToBit(declOrOffset);
2282-
auto entry = MF.DeclTypeCursor.advance();
2283-
2284-
if (entry.Kind != llvm::BitstreamEntry::Record) {
2285-
// We don't know how to serialize decls represented by sub-blocks.
2286-
MF.error();
2287-
return nullptr;
2288-
}
2289-
2290-
ASTContext &ctx = MF.getContext();
2291-
SmallVector<uint64_t, 64> scratch;
2292-
StringRef blobData;
2293-
2294-
if (auto s = ctx.Stats)
2295-
s->getFrontendCounters().NumDeclsDeserialized++;
2296-
2297-
// Read the attributes (if any).
2298-
// This isn't just using DeclAttributes because that would result in the
2299-
// attributes getting reversed.
2300-
// FIXME: If we reverse them at serialization time we could get rid of this.
2301-
DeclAttribute *DAttrs = nullptr;
2302-
DeclAttribute **AttrsNext = &DAttrs;
2303-
auto AddAttribute = [&](DeclAttribute *Attr) {
2304-
// Advance the linked list.
2305-
*AttrsNext = Attr;
2306-
AttrsNext = Attr->getMutableNext();
2307-
};
2308-
unsigned recordID;
2309-
23102234
class PrivateDiscriminatorRAII {
23112235
ModuleFile &moduleFile;
23122236
Serialized<Decl *> &declOrOffset;
@@ -2364,30 +2288,71 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
23642288
}
23652289
};
23662290

2367-
PrivateDiscriminatorRAII privateDiscriminatorRAII{MF, declOrOffset};
2368-
LocalDiscriminatorRAII localDiscriminatorRAII(declOrOffset);
2369-
ModuleFile::DeserializingEntityRAII deserializingEntity(MF);
2370-
FilenameForPrivateRAII filenameForPrivate(declOrOffset);
2291+
ModuleFile &MF;
2292+
public:
2293+
ASTDeserializer(ModuleFile &MF) : MF(MF) {}
23712294

2372-
// Local function that handles the "inherited" list for a type.
2373-
auto handleInherited
2374-
= [&](TypeDecl *nominal, ArrayRef<uint64_t> rawInheritedIDs) {
2375-
auto inheritedTypes = ctx.Allocate<TypeLoc>(rawInheritedIDs.size());
2376-
for_each(inheritedTypes, rawInheritedIDs,
2377-
[this](TypeLoc &tl, uint64_t rawID) {
2378-
tl = TypeLoc::withoutLoc(MF.getType(rawID));
2379-
});
2380-
nominal->setInherited(inheritedTypes);
2381-
};
2295+
/// Deserializes decl attribute and attribute-like records from
2296+
/// \c MF.DeclTypesCursor until a non-attribute record is found,
2297+
/// passing each one to \p AddAttribute.
2298+
llvm::Error deserializeDeclAttributes(
2299+
ASTContext &ctx,
2300+
llvm::function_ref<void (DeclAttribute *)> AddAttribute,
2301+
FilenameForPrivateRAII &filenameForPrivate,
2302+
LocalDiscriminatorRAII &localDiscriminatorRAII,
2303+
PrivateDiscriminatorRAII &privateDiscriminatorRAII);
23822304

2305+
Expected<Decl *> getDeclCheckedImpl(DeclID DID);
2306+
};
2307+
2308+
Expected<Decl *>
2309+
ModuleFile::getDeclChecked(DeclID DID) {
2310+
// Tag every deserialized ValueDecl coming out of getDeclChecked with its ID.
2311+
Expected<Decl *> deserialized =
2312+
ASTDeserializer(*this).getDeclCheckedImpl(DID);
2313+
if (deserialized && deserialized.get()) {
2314+
if (auto *IDC = dyn_cast<IterableDeclContext>(deserialized.get())) {
2315+
// Only set the DeclID on the returned Decl if it's one that was loaded
2316+
// and _wasn't_ one that had its DeclID set elsewhere (a followed XREF).
2317+
if (IDC->wasDeserialized() &&
2318+
static_cast<uint32_t>(IDC->getDeclID()) == 0) {
2319+
IDC->setDeclID(DID);
2320+
}
2321+
}
2322+
}
2323+
return deserialized;
2324+
}
2325+
2326+
template <typename DERIVED>
2327+
static bool attributeChainContains(DeclAttribute *attr) {
2328+
DeclAttributes tempAttrs;
2329+
tempAttrs.setRawAttributeChain(attr);
2330+
static_assert(std::is_trivially_destructible<DeclAttributes>::value,
2331+
"must not try to destroy the attribute chain");
2332+
return tempAttrs.hasAttribute<DERIVED>();
2333+
}
2334+
2335+
llvm::Error ASTDeserializer::deserializeDeclAttributes(
2336+
ASTContext &ctx,
2337+
llvm::function_ref<void (DeclAttribute *)> AddAttribute,
2338+
FilenameForPrivateRAII &filenameForPrivate,
2339+
LocalDiscriminatorRAII &localDiscriminatorRAII,
2340+
PrivateDiscriminatorRAII &privateDiscriminatorRAII) {
2341+
using namespace decls_block;
2342+
2343+
SmallVector<uint64_t, 64> scratch;
2344+
StringRef blobData;
23832345
while (true) {
2346+
BCOffsetRAII restoreOffset(MF.DeclTypeCursor);
2347+
auto entry = MF.DeclTypeCursor.advance();
23842348
if (entry.Kind != llvm::BitstreamEntry::Record) {
23852349
// We don't know how to serialize decls represented by sub-blocks.
23862350
MF.error();
2387-
return nullptr;
2351+
return llvm::Error::success();
23882352
}
23892353

2390-
recordID = MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData);
2354+
unsigned recordID = MF.DeclTypeCursor.readRecord(entry.ID, scratch,
2355+
&blobData);
23912356

23922357
if (isDeclAttrRecord(recordID)) {
23932358
DeclAttribute *Attr = nullptr;
@@ -2417,13 +2382,13 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
24172382
isImplicit);
24182383
break;
24192384
}
2420-
2385+
24212386
case decls_block::SwiftNativeObjCRuntimeBase_DECL_ATTR: {
24222387
bool isImplicit;
24232388
IdentifierID nameID;
24242389
serialization::decls_block::SwiftNativeObjCRuntimeBaseDeclAttrLayout
24252390
::readRecord(scratch, isImplicit, nameID);
2426-
2391+
24272392
auto name = MF.getIdentifier(nameID);
24282393
Attr = new (ctx) SwiftNativeObjCRuntimeBaseAttr(name, SourceLoc(),
24292394
SourceRange(),
@@ -2612,11 +2577,11 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
26122577
default:
26132578
// We don't know how to deserialize this kind of attribute.
26142579
MF.error();
2615-
return nullptr;
2580+
return llvm::Error::success();
26162581
}
26172582

26182583
if (!Attr)
2619-
return nullptr;
2584+
return llvm::Error::success();
26202585

26212586
AddAttribute(Attr);
26222587

@@ -2635,15 +2600,81 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
26352600
decls_block::FilenameForPrivateLayout::readRecord(scratch, filenameID);
26362601
filenameForPrivate.filename = MF.getIdentifier(filenameID);
26372602
} else {
2638-
break;
2603+
return llvm::Error::success();
26392604
}
26402605

2641-
// Advance bitstream cursor to the next record.
2642-
entry = MF.DeclTypeCursor.advance();
2643-
26442606
// Prepare to read the next record.
2607+
restoreOffset.cancel();
26452608
scratch.clear();
26462609
}
2610+
}
2611+
2612+
Expected<Decl *>
2613+
ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2614+
if (DID == 0)
2615+
return nullptr;
2616+
2617+
assert(DID <= MF.Decls.size() && "invalid decl ID");
2618+
auto &declOrOffset = MF.Decls[DID-1];
2619+
2620+
if (declOrOffset.isComplete())
2621+
return declOrOffset;
2622+
2623+
++NumDeclsLoaded;
2624+
BCOffsetRAII restoreOffset(MF.DeclTypeCursor);
2625+
MF.DeclTypeCursor.JumpToBit(declOrOffset);
2626+
2627+
ASTContext &ctx = MF.getContext();
2628+
2629+
if (auto s = ctx.Stats)
2630+
s->getFrontendCounters().NumDeclsDeserialized++;
2631+
2632+
// Read the attributes (if any).
2633+
// This isn't just using DeclAttributes because that would result in the
2634+
// attributes getting reversed.
2635+
// FIXME: If we reverse them at serialization time we could get rid of this.
2636+
DeclAttribute *DAttrs = nullptr;
2637+
DeclAttribute **AttrsNext = &DAttrs;
2638+
auto AddAttribute = [&](DeclAttribute *Attr) {
2639+
// Advance the linked list.
2640+
*AttrsNext = Attr;
2641+
AttrsNext = Attr->getMutableNext();
2642+
};
2643+
2644+
PrivateDiscriminatorRAII privateDiscriminatorRAII{MF, declOrOffset};
2645+
LocalDiscriminatorRAII localDiscriminatorRAII(declOrOffset);
2646+
ModuleFile::DeserializingEntityRAII deserializingEntity(MF);
2647+
FilenameForPrivateRAII filenameForPrivate(declOrOffset);
2648+
2649+
// Local function that handles the "inherited" list for a type.
2650+
auto handleInherited
2651+
= [&](TypeDecl *nominal, ArrayRef<uint64_t> rawInheritedIDs) {
2652+
auto inheritedTypes = ctx.Allocate<TypeLoc>(rawInheritedIDs.size());
2653+
for_each(inheritedTypes, rawInheritedIDs,
2654+
[this](TypeLoc &tl, uint64_t rawID) {
2655+
tl = TypeLoc::withoutLoc(MF.getType(rawID));
2656+
});
2657+
nominal->setInherited(inheritedTypes);
2658+
};
2659+
2660+
auto attrError = deserializeDeclAttributes(ctx, AddAttribute,
2661+
filenameForPrivate,
2662+
localDiscriminatorRAII,
2663+
privateDiscriminatorRAII);
2664+
if (attrError)
2665+
return std::move(attrError);
2666+
2667+
auto entry = MF.DeclTypeCursor.advance();
2668+
if (entry.Kind != llvm::BitstreamEntry::Record) {
2669+
// We don't know how to serialize decls represented by sub-blocks.
2670+
MF.error();
2671+
return nullptr;
2672+
}
2673+
2674+
SmallVector<uint64_t, 64> scratch;
2675+
StringRef blobData;
2676+
unsigned recordID = MF.DeclTypeCursor.readRecord(entry.ID, scratch,
2677+
&blobData);
26472678

26482679
PrettyDeclDeserialization stackTraceEntry(
26492680
&MF, declOrOffset, DID, static_cast<decls_block::RecordKind>(recordID));

0 commit comments

Comments
 (0)