@@ -1695,10 +1695,56 @@ tryCastUnwrappingExistentialSource(
1695
1695
return tryCast (destLocation, destType,
1696
1696
srcInnerValue, srcInnerType,
1697
1697
destFailureType, srcFailureType,
1698
- takeOnSuccess & (srcInnerValue == srcValue),
1698
+ takeOnSuccess && (srcInnerValue == srcValue),
1699
1699
mayDeferChecks);
1700
1700
}
1701
1701
1702
+ static DynamicCastResult tryCastUnwrappingExtendedExistentialSource (
1703
+ OpaqueValue *destLocation, const Metadata *destType, OpaqueValue *srcValue,
1704
+ const Metadata *srcType, const Metadata *&destFailureType,
1705
+ const Metadata *&srcFailureType, bool takeOnSuccess, bool mayDeferChecks) {
1706
+ assert (srcType != destType);
1707
+ assert (srcType->getKind () == MetadataKind::ExtendedExistential);
1708
+
1709
+ auto srcExistentialType = cast<ExtendedExistentialTypeMetadata>(srcType);
1710
+
1711
+ // Unpack the existential content
1712
+ const Metadata *srcInnerType = nullptr ;
1713
+ OpaqueValue *srcInnerValue = nullptr ;
1714
+ switch (srcExistentialType->Shape ->Flags .getSpecialKind ()) {
1715
+ case ExtendedExistentialTypeShape::SpecialKind::None: {
1716
+ auto opaqueContainer =
1717
+ reinterpret_cast <OpaqueExistentialContainer *>(srcValue);
1718
+ srcInnerType = opaqueContainer->Type ;
1719
+ srcInnerValue = const_cast <OpaqueValue *>(opaqueContainer->projectValue ());
1720
+ break ;
1721
+ }
1722
+ case ExtendedExistentialTypeShape::SpecialKind::Class: {
1723
+ auto classContainer =
1724
+ reinterpret_cast <ClassExistentialContainer *>(srcValue);
1725
+ srcInnerType = swift_getObjectType ((HeapObject *)classContainer->Value );
1726
+ srcInnerValue = reinterpret_cast <OpaqueValue *>(&classContainer->Value );
1727
+ break ;
1728
+ }
1729
+ case ExtendedExistentialTypeShape::SpecialKind::Metatype: {
1730
+ auto srcExistentialContainer =
1731
+ reinterpret_cast <ExistentialMetatypeContainer *>(srcValue);
1732
+ srcInnerType = swift_getMetatypeMetadata (srcExistentialContainer->Value );
1733
+ srcInnerValue = reinterpret_cast <OpaqueValue *>(&srcExistentialContainer->Value );
1734
+ break ;
1735
+ }
1736
+ case ExtendedExistentialTypeShape::SpecialKind::ExplicitLayout: {
1737
+ swift_unreachable (" Explicit layout not yet implemented" );
1738
+ break ;
1739
+ }
1740
+ }
1741
+
1742
+ srcFailureType = srcInnerType;
1743
+ return tryCast (destLocation, destType, srcInnerValue, srcInnerType,
1744
+ destFailureType, srcFailureType,
1745
+ takeOnSuccess & (srcInnerValue == srcValue), mayDeferChecks);
1746
+ }
1747
+
1702
1748
static DynamicCastResult
1703
1749
tryCastUnwrappingExistentialMetatypeSource (
1704
1750
OpaqueValue *destLocation, const Metadata *destType,
@@ -1722,6 +1768,103 @@ tryCastUnwrappingExistentialMetatypeSource(
1722
1768
mayDeferChecks);
1723
1769
}
1724
1770
1771
+
1772
+ static DynamicCastResult tryCastToExtendedExistential (
1773
+ OpaqueValue *destLocation, const Metadata *destType, OpaqueValue *srcValue,
1774
+ const Metadata *srcType, const Metadata *&destFailureType,
1775
+ const Metadata *&srcFailureType, bool takeOnSuccess, bool mayDeferChecks) {
1776
+ assert (srcType != destType);
1777
+ assert (destType->getKind () == MetadataKind::ExtendedExistential);
1778
+
1779
+ auto destExistentialType = cast<ExtendedExistentialTypeMetadata>(destType);
1780
+ auto *destExistentialShape = destExistentialType->Shape ;
1781
+ const unsigned shapeArgumentCount =
1782
+ destExistentialShape->getGenSigArgumentLayoutSizeInWords ();
1783
+
1784
+ llvm::SmallVector<const void *, 8 > allGenericArgsVec;
1785
+ unsigned witnessesMark = 0 ;
1786
+ {
1787
+ // Line up the arguments to the requirement signature.
1788
+ auto genArgs = destExistentialType->getGeneralizationArguments ();
1789
+ allGenericArgsVec.append (genArgs, genArgs + shapeArgumentCount);
1790
+ // Tack on the `Self` argument.
1791
+ allGenericArgsVec.push_back ((const void *)srcType);
1792
+ // Mark the point where the generic arguments end.
1793
+ // _checkGenericRequirements is going to fill in a set of witness tables
1794
+ // after that.
1795
+ witnessesMark = allGenericArgsVec.size ();
1796
+
1797
+ SubstGenericParametersFromMetadata substitutions (destExistentialShape,
1798
+ allGenericArgsVec.data ());
1799
+ // Verify the requirements in the requirement signature against the
1800
+ // arguments from the source value.
1801
+ auto error = swift::_checkGenericRequirements (
1802
+ destExistentialShape->getRequirementSignature ().getRequirements (),
1803
+ allGenericArgsVec,
1804
+ [&substitutions](unsigned depth, unsigned index) {
1805
+ return substitutions.getMetadata (depth, index);
1806
+ },
1807
+ [](const Metadata *type, unsigned index) -> const WitnessTable * {
1808
+ swift_unreachable (" Resolution of witness tables is not supported" );
1809
+ });
1810
+ if (error)
1811
+ return DynamicCastResult::Failure;
1812
+ }
1813
+
1814
+ OpaqueValue *destBox = nullptr ;
1815
+ const WitnessTable **destWitnesses = nullptr ;
1816
+ switch (destExistentialShape->Flags .getSpecialKind ()) {
1817
+ case ExtendedExistentialTypeShape::SpecialKind::None: {
1818
+ auto destExistential =
1819
+ reinterpret_cast <OpaqueExistentialContainer *>(destLocation);
1820
+
1821
+ // Allocate a box and fill in the type information.
1822
+ destExistential->Type = srcType;
1823
+ destBox = srcType->allocateBoxForExistentialIn (&destExistential->Buffer );
1824
+ destWitnesses = destExistential->getWitnessTables ();
1825
+ break ;
1826
+ }
1827
+ case ExtendedExistentialTypeShape::SpecialKind::Class: {
1828
+ auto destExistential =
1829
+ reinterpret_cast <ClassExistentialContainer *>(destLocation);
1830
+ destBox = reinterpret_cast <OpaqueValue *>(&destExistential->Value );
1831
+ destWitnesses = destExistential->getWitnessTables ();
1832
+ break ;
1833
+ }
1834
+ case ExtendedExistentialTypeShape::SpecialKind::Metatype: {
1835
+ auto destExistential =
1836
+ reinterpret_cast <ExistentialMetatypeContainer *>(destLocation);
1837
+ destBox = reinterpret_cast <OpaqueValue *>(&destExistential->Value );
1838
+ destWitnesses = destExistential->getWitnessTables ();
1839
+ break ;
1840
+ }
1841
+ case ExtendedExistentialTypeShape::SpecialKind::ExplicitLayout:
1842
+ swift_unreachable (" Witnesses for explicit layout not yet implemented" );
1843
+ }
1844
+
1845
+ // Fill in the trailing set of witness tables.
1846
+ const unsigned numWitnessTables = allGenericArgsVec.size () - witnessesMark;
1847
+ assert (numWitnessTables ==
1848
+ llvm::count_if (destExistentialShape->getRequirementSignature ().getRequirements (),
1849
+ [](const auto &req) -> bool {
1850
+ return req.getKind () ==
1851
+ GenericRequirementKind::Protocol;
1852
+ }));
1853
+ for (unsigned i = 0 ; i < numWitnessTables; ++i) {
1854
+ const auto witness = i + witnessesMark;
1855
+ destWitnesses[i] =
1856
+ reinterpret_cast <const WitnessTable *>(allGenericArgsVec[witness]);
1857
+ }
1858
+
1859
+ if (takeOnSuccess) {
1860
+ srcType->vw_initializeWithTake (destBox, srcValue);
1861
+ return DynamicCastResult::SuccessViaTake;
1862
+ } else {
1863
+ srcType->vw_initializeWithCopy (destBox, srcValue);
1864
+ return DynamicCastResult::SuccessViaCopy;
1865
+ }
1866
+ }
1867
+
1725
1868
/* *****************************************************************************/
1726
1869
/* *************************** Opaque Destination ******************************/
1727
1870
/* *****************************************************************************/
@@ -2006,12 +2149,14 @@ static tryCastFunctionType *selectCasterForDest(const Metadata *destType) {
2006
2149
swift_unreachable (
2007
2150
" Unknown existential type representation in dynamic cast dispatch" );
2008
2151
}
2152
+ case MetadataKind::ExtendedExistential:
2153
+ return tryCastToExtendedExistential;
2009
2154
case MetadataKind::Metatype:
2010
- return tryCastToMetatype;
2011
- case MetadataKind::ObjCClassWrapper:
2155
+ return tryCastToMetatype;
2156
+ case MetadataKind::ObjCClassWrapper:
2012
2157
return tryCastToObjectiveCClass;
2013
2158
case MetadataKind::ExistentialMetatype:
2014
- return tryCastToExistentialMetatype;
2159
+ return tryCastToExistentialMetatype;
2015
2160
case MetadataKind::HeapLocalVariable:
2016
2161
case MetadataKind::HeapGenericLocalVariable:
2017
2162
case MetadataKind::ErrorObject:
@@ -2169,6 +2314,15 @@ tryCast(
2169
2314
break ;
2170
2315
}
2171
2316
2317
+ case MetadataKind::ExtendedExistential: {
2318
+ auto subcastResult = tryCastUnwrappingExtendedExistentialSource (
2319
+ destLocation, destType, srcValue, srcType, destFailureType,
2320
+ srcFailureType, takeOnSuccess, mayDeferChecks);
2321
+ if (isSuccess (subcastResult)) {
2322
+ return subcastResult;
2323
+ }
2324
+ break ;
2325
+ }
2172
2326
default :
2173
2327
break ;
2174
2328
}
0 commit comments