@@ -2231,82 +2231,6 @@ class swift::ASTDeserializer {
2231
2231
using Serialized = ModuleFile::Serialized<T>;
2232
2232
using TypeID = serialization::TypeID;
2233
2233
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
-
2310
2234
class PrivateDiscriminatorRAII {
2311
2235
ModuleFile &moduleFile;
2312
2236
Serialized<Decl *> &declOrOffset;
@@ -2364,30 +2288,71 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2364
2288
}
2365
2289
};
2366
2290
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) {}
2371
2294
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);
2382
2304
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;
2383
2345
while (true ) {
2346
+ BCOffsetRAII restoreOffset (MF.DeclTypeCursor );
2347
+ auto entry = MF.DeclTypeCursor .advance ();
2384
2348
if (entry.Kind != llvm::BitstreamEntry::Record) {
2385
2349
// We don't know how to serialize decls represented by sub-blocks.
2386
2350
MF.error ();
2387
- return nullptr ;
2351
+ return llvm::Error::success () ;
2388
2352
}
2389
2353
2390
- recordID = MF.DeclTypeCursor .readRecord (entry.ID , scratch, &blobData);
2354
+ unsigned recordID = MF.DeclTypeCursor .readRecord (entry.ID , scratch,
2355
+ &blobData);
2391
2356
2392
2357
if (isDeclAttrRecord (recordID)) {
2393
2358
DeclAttribute *Attr = nullptr ;
@@ -2417,13 +2382,13 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2417
2382
isImplicit);
2418
2383
break ;
2419
2384
}
2420
-
2385
+
2421
2386
case decls_block::SwiftNativeObjCRuntimeBase_DECL_ATTR: {
2422
2387
bool isImplicit;
2423
2388
IdentifierID nameID;
2424
2389
serialization::decls_block::SwiftNativeObjCRuntimeBaseDeclAttrLayout
2425
2390
::readRecord (scratch, isImplicit, nameID);
2426
-
2391
+
2427
2392
auto name = MF.getIdentifier (nameID);
2428
2393
Attr = new (ctx) SwiftNativeObjCRuntimeBaseAttr (name, SourceLoc (),
2429
2394
SourceRange (),
@@ -2612,11 +2577,11 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2612
2577
default :
2613
2578
// We don't know how to deserialize this kind of attribute.
2614
2579
MF.error ();
2615
- return nullptr ;
2580
+ return llvm::Error::success () ;
2616
2581
}
2617
2582
2618
2583
if (!Attr)
2619
- return nullptr ;
2584
+ return llvm::Error::success () ;
2620
2585
2621
2586
AddAttribute (Attr);
2622
2587
@@ -2635,15 +2600,81 @@ ASTDeserializer::getDeclCheckedImpl(DeclID DID) {
2635
2600
decls_block::FilenameForPrivateLayout::readRecord (scratch, filenameID);
2636
2601
filenameForPrivate.filename = MF.getIdentifier (filenameID);
2637
2602
} else {
2638
- break ;
2603
+ return llvm::Error::success () ;
2639
2604
}
2640
2605
2641
- // Advance bitstream cursor to the next record.
2642
- entry = MF.DeclTypeCursor .advance ();
2643
-
2644
2606
// Prepare to read the next record.
2607
+ restoreOffset.cancel ();
2645
2608
scratch.clear ();
2646
2609
}
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);
2647
2678
2648
2679
PrettyDeclDeserialization stackTraceEntry (
2649
2680
&MF, declOrOffset, DID, static_cast <decls_block::RecordKind>(recordID));
0 commit comments