@@ -1728,31 +1728,27 @@ getTypeLoweringForExpansion(TypeKey key,
1728
1728
}
1729
1729
1730
1730
static GenericSignature *
1731
- getEffectiveGenericSignature (DeclContext *dc) {
1732
- if (auto sig = dc->getGenericSignatureOfContext ()) {
1733
- if (sig->areAllParamsConcrete ())
1734
- return nullptr ;
1735
- return sig;
1736
- }
1731
+ getEffectiveGenericSignature (DeclContext *dc,
1732
+ CaptureInfo captureInfo) {
1733
+ if (dc->getParent ()->isLocalContext () &&
1734
+ !captureInfo.hasGenericParamCaptures ())
1735
+ return nullptr ;
1737
1736
1738
- return nullptr ;
1737
+ return dc-> getGenericSignatureOfContext () ;
1739
1738
}
1740
1739
1741
1740
static GenericSignature *
1742
1741
getEffectiveGenericSignature (AnyFunctionRef fn,
1743
1742
CaptureInfo captureInfo) {
1744
- auto dc = fn.getAsDeclContext ();
1745
-
1746
- if (dc->getParent ()->isLocalContext () &&
1747
- !captureInfo.hasGenericParamCaptures ())
1748
- return nullptr ;
1749
-
1750
- return getEffectiveGenericSignature (dc);
1743
+ return getEffectiveGenericSignature (fn.getAsDeclContext (), captureInfo);
1751
1744
}
1752
1745
1753
1746
static CanGenericSignature
1754
1747
getCanonicalSignatureOrNull (GenericSignature *sig) {
1755
- return sig ? sig->getCanonicalSignature () : nullptr ;
1748
+ if (!sig || sig->areAllParamsConcrete ())
1749
+ return nullptr ;
1750
+
1751
+ return sig->getCanonicalSignature ();
1756
1752
}
1757
1753
1758
1754
// / Get the type of a global variable accessor function, () -> RawPointer.
@@ -1763,7 +1759,6 @@ static CanAnyFunctionType getGlobalAccessorType(CanType varType) {
1763
1759
1764
1760
// / Get the type of a default argument generator, () -> T.
1765
1761
static CanAnyFunctionType getDefaultArgGeneratorInterfaceType (
1766
- TypeConverter &TC,
1767
1762
SILDeclRef c) {
1768
1763
auto *vd = c.getDecl ();
1769
1764
auto resultTy = getParameterAt (vd,
@@ -1785,7 +1780,15 @@ static CanAnyFunctionType getDefaultArgGeneratorInterfaceType(
1785
1780
}
1786
1781
1787
1782
// Get the generic signature from the surrounding context.
1788
- auto sig = TC.getConstantGenericSignature (c);
1783
+ auto *sig = vd->getInnermostDeclContext ()->getGenericSignatureOfContext ();
1784
+ if (auto *afd = dyn_cast<AbstractFunctionDecl>(vd)) {
1785
+ auto *param = getParameterAt (afd, c.defaultArgIndex );
1786
+ if (param->getDefaultValue ()) {
1787
+ auto &captureInfo = param->getDefaultArgumentCaptureInfo ();
1788
+ sig = getEffectiveGenericSignature (afd, captureInfo);
1789
+ }
1790
+ }
1791
+
1789
1792
return CanAnyFunctionType::get (getCanonicalSignatureOrNull (sig),
1790
1793
{}, canResultTy);
1791
1794
}
@@ -1806,7 +1809,7 @@ static CanAnyFunctionType getStoredPropertyInitializerInterfaceType(
1806
1809
resultTy = originalProperty->getValueInterfaceType ()->getCanonicalType ();
1807
1810
}
1808
1811
1809
- auto sig = getEffectiveGenericSignature (DC );
1812
+ auto sig = DC-> getGenericSignatureOfContext ( );
1810
1813
1811
1814
return CanAnyFunctionType::get (getCanonicalSignatureOrNull (sig),
1812
1815
{}, resultTy);
@@ -1837,7 +1840,7 @@ static CanAnyFunctionType getDestructorInterfaceType(DestructorDecl *dd,
1837
1840
: C.TheNativeObjectType );
1838
1841
CanType methodTy = CanFunctionType::get ({}, resultTy);
1839
1842
1840
- auto sig = getEffectiveGenericSignature (dd );
1843
+ auto sig = dd-> getGenericSignatureOfContext ( );
1841
1844
FunctionType::Param args[] = {FunctionType::Param (classType)};
1842
1845
return CanAnyFunctionType::get (getCanonicalSignatureOrNull (sig),
1843
1846
llvm::makeArrayRef (args),
@@ -1862,22 +1865,24 @@ static CanAnyFunctionType getIVarInitDestroyerInterfaceType(ClassDecl *cd,
1862
1865
: SILFunctionTypeRepresentation::Method);
1863
1866
1864
1867
resultType = CanFunctionType::get ({}, resultType, extInfo);
1865
- auto sig = getEffectiveGenericSignature (cd );
1868
+ auto sig = cd-> getGenericSignature ( );
1866
1869
FunctionType::Param args[] = {FunctionType::Param (classType)};
1867
1870
return CanAnyFunctionType::get (getCanonicalSignatureOrNull (sig),
1868
1871
llvm::makeArrayRef (args),
1869
1872
resultType, extInfo);
1870
1873
}
1871
1874
1872
- CanAnyFunctionType
1873
- TypeConverter::getFunctionInterfaceTypeWithCaptures (CanAnyFunctionType funcType,
1874
- AnyFunctionRef theClosure) {
1875
+ static CanAnyFunctionType
1876
+ getFunctionInterfaceTypeWithCaptures (TypeConverter &TC,
1877
+ CanAnyFunctionType funcType,
1878
+ SILDeclRef constant) {
1875
1879
// Get transitive closure of value captured by this function, and any
1876
1880
// captured functions.
1877
- auto captureInfo = getLoweredLocalCaptures (theClosure );
1881
+ auto captureInfo = TC. getLoweredLocalCaptures (constant );
1878
1882
1879
1883
// Capture generic parameters from the enclosing context if necessary.
1880
- auto *genericSig = getEffectiveGenericSignature (theClosure, captureInfo);
1884
+ auto closure = *constant.getAnyFunctionRef ();
1885
+ auto *genericSig = getEffectiveGenericSignature (closure, captureInfo);
1881
1886
1882
1887
auto innerExtInfo = AnyFunctionType::ExtInfo (FunctionType::Representation::Thin,
1883
1888
funcType->throws ());
@@ -1897,25 +1902,22 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
1897
1902
1898
1903
switch (c.kind ) {
1899
1904
case SILDeclRef::Kind::Func: {
1905
+ CanAnyFunctionType funcTy;
1900
1906
if (auto *ACE = c.loc .dyn_cast <AbstractClosureExpr *>()) {
1901
1907
// FIXME: Closures could have an interface type computed by Sema.
1902
- auto funcTy = cast<AnyFunctionType>(ACE->getType ()->getCanonicalType ());
1903
1908
funcTy = cast<AnyFunctionType>(
1904
- funcTy->mapTypeOutOfContext ()
1905
- ->getCanonicalType ());
1906
- return getFunctionInterfaceTypeWithCaptures (funcTy, ACE);
1909
+ ACE->getType ()->mapTypeOutOfContext ()->getCanonicalType ());
1910
+ } else {
1911
+ funcTy = cast<AnyFunctionType>(
1912
+ vd->getInterfaceType ()->getCanonicalType ());
1907
1913
}
1908
-
1909
- FuncDecl *func = cast<FuncDecl>(vd);
1910
- auto funcTy = cast<AnyFunctionType>(
1911
- func->getInterfaceType ()->getCanonicalType ());
1912
- return getFunctionInterfaceTypeWithCaptures (funcTy, func);
1914
+ return getFunctionInterfaceTypeWithCaptures (*this , funcTy, c);
1913
1915
}
1914
1916
1915
1917
case SILDeclRef::Kind::EnumElement: {
1916
1918
auto funcTy = cast<AnyFunctionType>(
1917
- vd->getInterfaceType ()->getCanonicalType ());
1918
- auto sig = getEffectiveGenericSignature ( vd->getDeclContext ());
1919
+ vd->getInterfaceType ()->getCanonicalType ());
1920
+ auto sig = vd->getDeclContext ()-> getGenericSignatureOfContext ( );
1919
1921
return CanAnyFunctionType::get (getCanonicalSignatureOrNull (sig),
1920
1922
funcTy->getParams (),
1921
1923
funcTy.getResult (),
@@ -1926,14 +1928,14 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
1926
1928
auto *cd = cast<ConstructorDecl>(vd);
1927
1929
auto funcTy = cast<AnyFunctionType>(
1928
1930
cd->getInterfaceType ()->getCanonicalType ());
1929
- return getFunctionInterfaceTypeWithCaptures (funcTy, cd );
1931
+ return getFunctionInterfaceTypeWithCaptures (* this , funcTy, c );
1930
1932
}
1931
1933
1932
1934
case SILDeclRef::Kind::Initializer: {
1933
1935
auto *cd = cast<ConstructorDecl>(vd);
1934
1936
auto funcTy = cast<AnyFunctionType>(
1935
1937
cd->getInitializerInterfaceType ()->getCanonicalType ());
1936
- return getFunctionInterfaceTypeWithCaptures (funcTy, cd );
1938
+ return getFunctionInterfaceTypeWithCaptures (* this , funcTy, c );
1937
1939
}
1938
1940
1939
1941
case SILDeclRef::Kind::Destroyer:
@@ -1949,7 +1951,7 @@ CanAnyFunctionType TypeConverter::makeConstantInterfaceType(SILDeclRef c) {
1949
1951
return getGlobalAccessorType (var->getInterfaceType ()->getCanonicalType ());
1950
1952
}
1951
1953
case SILDeclRef::Kind::DefaultArgGenerator:
1952
- return getDefaultArgGeneratorInterfaceType (* this , c);
1954
+ return getDefaultArgGeneratorInterfaceType (c);
1953
1955
case SILDeclRef::Kind::StoredPropertyInitializer:
1954
1956
return getStoredPropertyInitializerInterfaceType (cast<VarDecl>(vd));
1955
1957
case SILDeclRef::Kind::IVarInitializer:
@@ -1969,43 +1971,28 @@ TypeConverter::getConstantGenericSignature(SILDeclRef c) {
1969
1971
1970
1972
// / Get the function generic params, including outer params.
1971
1973
switch (c.kind ) {
1972
- case SILDeclRef::Kind::Func: {
1973
- if (auto *ACE = c.getAbstractClosureExpr ()) {
1974
- auto captureInfo = getLoweredLocalCaptures (ACE);
1975
- return getEffectiveGenericSignature (ACE, captureInfo);
1976
- }
1977
- FuncDecl *func = cast<FuncDecl>(vd);
1978
- auto captureInfo = getLoweredLocalCaptures (func);
1979
- return getEffectiveGenericSignature (func, captureInfo);
1980
- }
1981
- case SILDeclRef::Kind::EnumElement: {
1982
- auto eltDecl = cast<EnumElementDecl>(vd);
1983
- return getEffectiveGenericSignature (eltDecl->getDeclContext ());
1984
- }
1974
+ case SILDeclRef::Kind::Func:
1985
1975
case SILDeclRef::Kind::Allocator:
1986
1976
case SILDeclRef::Kind::Initializer:
1987
1977
case SILDeclRef::Kind::Destroyer:
1988
1978
case SILDeclRef::Kind::Deallocator: {
1989
- auto *afd = cast<AbstractFunctionDecl>(vd );
1990
- auto captureInfo = getLoweredLocalCaptures (afd);
1991
- return getEffectiveGenericSignature (afd , captureInfo);
1979
+ auto captureInfo = getLoweredLocalCaptures (c );
1980
+ return getEffectiveGenericSignature (
1981
+ *c. getAnyFunctionRef () , captureInfo);
1992
1982
}
1993
- case SILDeclRef::Kind::GlobalAccessor:
1994
- return getEffectiveGenericSignature (vd->getDeclContext ());
1995
1983
case SILDeclRef::Kind::IVarInitializer:
1996
1984
case SILDeclRef::Kind::IVarDestroyer:
1997
- return getEffectiveGenericSignature ( cast<ClassDecl>(vd));
1998
- case SILDeclRef::Kind::DefaultArgGenerator:
1985
+ return cast<ClassDecl>(vd)-> getGenericSignature ( );
1986
+ case SILDeclRef::Kind::DefaultArgGenerator: {
1999
1987
// Use the generic environment of the original function.
2000
- if (auto *afd = dyn_cast<AbstractFunctionDecl>(c.getDecl ())) {
2001
- auto captureInfo = getLoweredLocalCaptures (afd);
2002
- return getEffectiveGenericSignature (afd, captureInfo);
2003
- }
1988
+ auto captureInfo = getLoweredLocalCaptures (c);
2004
1989
return getEffectiveGenericSignature (
2005
- c.getDecl ()->getInnermostDeclContext ());
1990
+ vd->getInnermostDeclContext (), captureInfo);
1991
+ }
1992
+ case SILDeclRef::Kind::EnumElement:
1993
+ case SILDeclRef::Kind::GlobalAccessor:
2006
1994
case SILDeclRef::Kind::StoredPropertyInitializer:
2007
- // Use the generic environment of the containing type.
2008
- return getEffectiveGenericSignature (c.getDecl ()->getDeclContext ());
1995
+ return vd->getDeclContext ()->getGenericSignatureOfContext ();
2009
1996
}
2010
1997
2011
1998
llvm_unreachable (" Unhandled SILDeclRefKind in switch." );
@@ -2107,12 +2094,16 @@ getAnyFunctionRefFromCapture(CapturedValue capture) {
2107
2094
}
2108
2095
2109
2096
bool
2110
- TypeConverter::hasLoweredLocalCaptures (AnyFunctionRef fn) {
2097
+ TypeConverter::hasLoweredLocalCaptures (SILDeclRef fn) {
2111
2098
return !getLoweredLocalCaptures (fn).getCaptures ().empty ();
2112
2099
}
2113
2100
2114
2101
CaptureInfo
2115
- TypeConverter::getLoweredLocalCaptures (AnyFunctionRef fn) {
2102
+ TypeConverter::getLoweredLocalCaptures (SILDeclRef fn) {
2103
+ fn.isForeign = 0 ;
2104
+ fn.isCurried = 0 ;
2105
+ fn.isDirectReference = 0 ;
2106
+
2116
2107
// See if we've cached the lowered capture list for this function.
2117
2108
auto found = LoweredCaptures.find (fn);
2118
2109
if (found != LoweredCaptures.end ())
@@ -2132,6 +2123,7 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2132
2123
2133
2124
std::function<void (const CaptureInfo &captureInfo)> collectCaptures;
2134
2125
std::function<void (AnyFunctionRef)> collectFunctionCaptures;
2126
+ std::function<void (SILDeclRef)> collectConstantCaptures;
2135
2127
2136
2128
collectCaptures = [&](const CaptureInfo &captureInfo) {
2137
2129
if (captureInfo.hasGenericParamCaptures ())
@@ -2262,7 +2254,10 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2262
2254
2263
2255
collectCaptures (curFn.getCaptureInfo ());
2264
2256
2265
- // Also visit default argument captures.
2257
+ // A function's captures also include its default arguments, because
2258
+ // when we reference a function we don't track which default arguments
2259
+ // are referenced too.
2260
+ //
2266
2261
// FIXME: This should be more fine-grained -- we should only need the
2267
2262
// captures for default arguments that are actually referenced.
2268
2263
if (auto *AFD = curFn.getAbstractFunctionDecl ()) {
@@ -2272,7 +2267,27 @@ TypeConverter::getLoweredLocalCaptures(AnyFunctionRef fn) {
2272
2267
}
2273
2268
}
2274
2269
};
2275
- collectFunctionCaptures (fn);
2270
+
2271
+ collectConstantCaptures = [&](SILDeclRef curFn) {
2272
+ if (curFn.isDefaultArgGenerator ()) {
2273
+ if (auto *afd = dyn_cast<AbstractFunctionDecl>(curFn.getDecl ())) {
2274
+ auto *param = getParameterAt (afd, curFn.defaultArgIndex );
2275
+ if (param->getDefaultValue ())
2276
+ collectCaptures (param->getDefaultArgumentCaptureInfo ());
2277
+ return ;
2278
+ }
2279
+
2280
+ if (curFn.getDecl ()->getInnermostDeclContext ()
2281
+ ->getGenericSignatureOfContext ())
2282
+ capturesGenericParams = true ;
2283
+
2284
+ return ;
2285
+ }
2286
+
2287
+ collectFunctionCaptures (*curFn.getAnyFunctionRef ());
2288
+ };
2289
+
2290
+ collectConstantCaptures (fn);
2276
2291
2277
2292
SmallVector<CapturedValue, 4 > resultingCaptures;
2278
2293
for (auto capturePair : captures) {
@@ -2518,7 +2533,7 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
2518
2533
bool isMutable) {
2519
2534
auto &C = M.getASTContext ();
2520
2535
auto signature = getCanonicalSignatureOrNull (
2521
- getEffectiveGenericSignature ( captured->getDeclContext ()));
2536
+ captured->getDeclContext ()-> getGenericSignatureOfContext ( ));
2522
2537
2523
2538
// If the type is not dependent at all, we can form a concrete box layout.
2524
2539
// We don't need to capture the generic environment.
@@ -2606,7 +2621,7 @@ CanSILBoxType TypeConverter::getBoxTypeForEnumElement(SILType enumType,
2606
2621
Context.getLazyResolver ()->resolveDeclSignature (elt);
2607
2622
2608
2623
auto boxSignature = getCanonicalSignatureOrNull (
2609
- getEffectiveGenericSignature ( enumDecl));
2624
+ enumDecl-> getGenericSignature ( ));
2610
2625
2611
2626
if (boxSignature == CanGenericSignature ()) {
2612
2627
auto eltIntfTy = elt->getArgumentInterfaceType ();
0 commit comments