@@ -10810,13 +10810,16 @@ bool AbstractFunctionDecl::isResilient(ModuleDecl *M,
1081010810OpaqueTypeDecl::OpaqueTypeDecl (ValueDecl *NamingDecl,
1081110811 GenericParamList *GenericParams, DeclContext *DC,
1081210812 GenericSignature OpaqueInterfaceGenericSignature,
10813- ArrayRef<TypeRepr *>
10814- OpaqueReturnTypeReprs )
10813+ ArrayRef<TypeRepr *> OpaqueReturnTypeReprs,
10814+ bool hasLazyUnderlyingSubstitutions )
1081510815 : GenericTypeDecl(DeclKind::OpaqueType, DC, Identifier(), SourceLoc(), {},
1081610816 GenericParams),
1081710817 NamingDeclAndHasOpaqueReturnTypeRepr (
1081810818 NamingDecl, !OpaqueReturnTypeReprs.empty()),
1081910819 OpaqueInterfaceGenericSignature(OpaqueInterfaceGenericSignature) {
10820+ Bits.OpaqueTypeDecl .HasLazyUnderlyingSubstitutions
10821+ = hasLazyUnderlyingSubstitutions;
10822+
1082010823 // Always implicit.
1082110824 setImplicit ();
1082210825
@@ -10829,7 +10832,7 @@ OpaqueTypeDecl::OpaqueTypeDecl(ValueDecl *NamingDecl,
1082910832 OpaqueReturnTypeReprs.end (), getTrailingObjects ());
1083010833}
1083110834
10832- OpaqueTypeDecl *OpaqueTypeDecl::get (
10835+ OpaqueTypeDecl *OpaqueTypeDecl::create (
1083310836 ValueDecl *NamingDecl, GenericParamList *GenericParams,
1083410837 DeclContext *DC,
1083510838 GenericSignature OpaqueInterfaceGenericSignature,
@@ -10840,7 +10843,33 @@ OpaqueTypeDecl *OpaqueTypeDecl::get(
1084010843 auto mem = ctx.Allocate (size, alignof (OpaqueTypeDecl));
1084110844 return new (mem) OpaqueTypeDecl (
1084210845 NamingDecl, GenericParams, DC, OpaqueInterfaceGenericSignature,
10843- OpaqueReturnTypeReprs);
10846+ OpaqueReturnTypeReprs, /* hasLazyUnderlyingSubstitutions=*/ false );
10847+ }
10848+
10849+ OpaqueTypeDecl *OpaqueTypeDecl::createDeserialized (
10850+ GenericParamList *GenericParams, DeclContext *DC,
10851+ GenericSignature OpaqueInterfaceGenericSignature,
10852+ LazyMemberLoader *lazyLoader, uint64_t underlyingSubsData) {
10853+ bool hasLazyUnderlyingSubstitutions = (underlyingSubsData != 0 );
10854+
10855+ ASTContext &ctx = DC->getASTContext ();
10856+ auto size = totalSizeToAlloc<TypeRepr *>(0 );
10857+ auto mem = ctx.Allocate (size, alignof (OpaqueTypeDecl));
10858+
10859+ // NamingDecl is set later by deserialization
10860+ auto *decl = new (mem) OpaqueTypeDecl (
10861+ /* namingDecl=*/ nullptr , GenericParams, DC,
10862+ OpaqueInterfaceGenericSignature, { },
10863+ hasLazyUnderlyingSubstitutions);
10864+
10865+ if (hasLazyUnderlyingSubstitutions) {
10866+ auto &ctx = DC->getASTContext ();
10867+ auto *data = static_cast <LazyOpaqueTypeData *>(
10868+ ctx.getOrCreateLazyContextData (decl, lazyLoader));
10869+ data->underlyingSubsData = underlyingSubsData;
10870+ }
10871+
10872+ return decl;
1084410873}
1084510874
1084610875bool OpaqueTypeDecl::isOpaqueReturnTypeOf (const Decl *ownerDecl) const {
@@ -10896,16 +10925,41 @@ bool OpaqueTypeDecl::exportUnderlyingType() const {
1089610925 llvm_unreachable (" The naming decl is expected to be either an AFD or ASD" );
1089710926}
1089810927
10928+ void OpaqueTypeDecl::loadLazyUnderlyingSubstitutions () {
10929+ if (!Bits.OpaqueTypeDecl .HasLazyUnderlyingSubstitutions )
10930+ return ;
10931+
10932+ Bits.OpaqueTypeDecl .HasLazyUnderlyingSubstitutions = 0 ;
10933+
10934+ auto &ctx = getASTContext ();
10935+ auto *data = static_cast <LazyOpaqueTypeData *>(
10936+ ctx.getLazyContextData (this ));
10937+ ASSERT (data != nullptr );
10938+
10939+ data->loader ->finishOpaqueTypeDecl (
10940+ this , data->underlyingSubsData );
10941+ }
10942+
1089910943std::optional<SubstitutionMap>
1090010944OpaqueTypeDecl::getUniqueUnderlyingTypeSubstitutions (
1090110945 bool typeCheckFunctionBodies) const {
10946+ const_cast <OpaqueTypeDecl *>(this )->loadLazyUnderlyingSubstitutions ();
10947+
1090210948 if (!typeCheckFunctionBodies)
1090310949 return UniqueUnderlyingType;
1090410950
1090510951 return evaluateOrDefault (getASTContext ().evaluator ,
1090610952 UniqueUnderlyingTypeSubstitutionsRequest{this }, {});
1090710953}
1090810954
10955+ ArrayRef<OpaqueTypeDecl::ConditionallyAvailableSubstitutions *>
10956+ OpaqueTypeDecl::getConditionallyAvailableSubstitutions () const {
10957+ const_cast <OpaqueTypeDecl *>(this )->loadLazyUnderlyingSubstitutions ();
10958+
10959+ assert (ConditionallyAvailableTypes);
10960+ return ConditionallyAvailableTypes.value ();
10961+ }
10962+
1090910963std::optional<unsigned >
1091010964OpaqueTypeDecl::getAnonymousOpaqueParamOrdinal (TypeRepr *repr) const {
1091110965 assert (NamingDeclAndHasOpaqueReturnTypeRepr.getInt () &&
@@ -10935,7 +10989,8 @@ Identifier OpaqueTypeDecl::getOpaqueReturnTypeIdentifier() const {
1093510989
1093610990void OpaqueTypeDecl::setConditionallyAvailableSubstitutions (
1093710991 ArrayRef<ConditionallyAvailableSubstitutions *> substitutions) {
10938- assert (!ConditionallyAvailableTypes &&
10992+ ASSERT (!Bits.OpaqueTypeDecl .HasLazyUnderlyingSubstitutions );
10993+ ASSERT (!ConditionallyAvailableTypes &&
1093910994 " resetting conditionally available substitutions?!" );
1094010995 ConditionallyAvailableTypes = getASTContext ().AllocateCopy (substitutions);
1094110996}
0 commit comments