24
24
#include " swift/AST/GenericSignature.h"
25
25
#include " swift/AST/ModuleLoader.h"
26
26
#include " swift/AST/TypeCheckRequests.h"
27
- #include " swift/AST/TypeVisitor .h"
27
+ #include " swift/AST/CanTypeVisitor .h"
28
28
#include " swift/SIL/TypeLowering.h"
29
29
#include " clang/AST/ASTContext.h"
30
30
#include " clang/AST/Attr.h"
@@ -690,6 +690,28 @@ size_t AbstractionPattern::getNumPackExpandedComponents() const {
690
690
return substShape->getNumElements ();
691
691
}
692
692
693
+ AbstractionPattern AbstractionPattern::getMetatypeInstanceType () const {
694
+ assert (getKind () == Kind::Type);
695
+ return AbstractionPattern (getGenericSubstitutions (),
696
+ getGenericSignature (),
697
+ cast<AnyMetatypeType>(getType ()).getInstanceType ());
698
+ }
699
+
700
+ AbstractionPattern AbstractionPattern::getDynamicSelfSelfType () const {
701
+ assert (getKind () == Kind::Type);
702
+ return AbstractionPattern (getGenericSubstitutions (),
703
+ getGenericSignature (),
704
+ cast<DynamicSelfType>(getType ()).getSelfType ());
705
+ }
706
+
707
+ AbstractionPattern
708
+ AbstractionPattern::getParameterizedProtocolArgType (unsigned argIndex) const {
709
+ assert (getKind () == Kind::Type);
710
+ return AbstractionPattern (getGenericSubstitutions (),
711
+ getGenericSignature (),
712
+ cast<ParameterizedProtocolType>(getType ()).getArgs ()[argIndex]);
713
+ }
714
+
693
715
AbstractionPattern AbstractionPattern::removingMoveOnlyWrapper () const {
694
716
switch (getKind ()) {
695
717
case Kind::Invalid:
@@ -1798,8 +1820,8 @@ AbstractionPattern::operator==(const AbstractionPattern &other) const {
1798
1820
1799
1821
namespace {
1800
1822
class SubstFunctionTypePatternVisitor
1801
- : public TypeVisitor <SubstFunctionTypePatternVisitor, CanType,
1802
- AbstractionPattern>
1823
+ : public CanTypeVisitor <SubstFunctionTypePatternVisitor, CanType,
1824
+ AbstractionPattern>
1803
1825
{
1804
1826
public:
1805
1827
TypeConverter &TC;
@@ -1815,7 +1837,7 @@ class SubstFunctionTypePatternVisitor
1815
1837
// signature if `pattern` is a type parameter or opaque archetype. Returns
1816
1838
// null otherwise.
1817
1839
CanType handleTypeParameterInAbstractionPattern (AbstractionPattern pattern,
1818
- Type substTy) {
1840
+ CanType substTy) {
1819
1841
if (!pattern.isTypeParameterOrOpaqueArchetype ())
1820
1842
return CanType ();
1821
1843
@@ -1891,52 +1913,47 @@ class SubstFunctionTypePatternVisitor
1891
1913
return CanType (gp);
1892
1914
}
1893
1915
1894
- CanType visitType (TypeBase * t, AbstractionPattern pattern) {
1916
+ CanType visit (CanType t, AbstractionPattern pattern) {
1895
1917
if (auto gp = handleTypeParameterInAbstractionPattern (pattern, t))
1896
1918
return gp;
1897
-
1919
+
1920
+ return CanTypeVisitor::visit (t, pattern);
1921
+ }
1922
+
1923
+ CanType visitType (CanType t, AbstractionPattern pattern) {
1898
1924
assert (pattern.getType ()->isExistentialType () ||
1899
1925
(!pattern.getType ()->hasTypeParameter ()
1900
1926
&& !pattern.getType ()->hasArchetype ()
1901
1927
&& !pattern.getType ()->hasOpaqueArchetype ()));
1902
1928
return pattern.getType ();
1903
1929
}
1904
1930
1905
- CanType visitDynamicSelfType (DynamicSelfType * dst,
1931
+ CanType visitDynamicSelfType (CanDynamicSelfType dst,
1906
1932
AbstractionPattern pattern) {
1907
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, dst))
1908
- return gp;
1909
-
1910
1933
// A "dynamic self" type can be bound to another dynamic self type, or the
1911
1934
// non-dynamic base class type.
1912
- if (auto origDynSelf = dyn_cast<DynamicSelfType>(pattern.getType ())) {
1913
- auto origSelf = AbstractionPattern (pattern.getGenericSignatureOrNull (),
1914
- origDynSelf.getSelfType ());
1915
-
1916
- auto newBase = visit (dst->getSelfType (), origSelf);
1935
+ if (isa<DynamicSelfType>(pattern.getType ())) {
1936
+ auto origSelf = pattern.getDynamicSelfSelfType ();
1937
+ auto newBase = visit (dst.getSelfType (), origSelf);
1917
1938
return DynamicSelfType::get (newBase, TC.Context )
1918
1939
->getCanonicalType ();
1919
1940
}
1920
1941
1921
- return visit (dst-> getSelfType (), pattern);
1942
+ return visit (dst. getSelfType (), pattern);
1922
1943
}
1923
1944
1924
- CanType visitAnyMetatypeType (AnyMetatypeType * mt, AbstractionPattern pattern){
1925
- if ( auto gp = handleTypeParameterInAbstractionPattern (pattern, mt))
1926
- return gp ;
1945
+ CanType visitAnyMetatypeType (CanAnyMetatypeType mt, AbstractionPattern pattern){
1946
+ auto substInstance = visit (mt. getInstanceType (),
1947
+ pattern. getMetatypeInstanceType ()) ;
1927
1948
1928
- auto origMeta = cast<AnyMetatypeType>(pattern.getType ());
1929
-
1930
- auto substInstance = visit (mt->getInstanceType (),
1931
- AbstractionPattern (pattern.getGenericSignatureOrNull (),
1932
- origMeta.getInstanceType ()));
1933
-
1934
- return isa<ExistentialMetatypeType>(origMeta)
1935
- ? CanType (CanExistentialMetatypeType::get (substInstance))
1949
+ // The CanType cast is required for this to type-check because
1950
+ // C++'s ?: operator doesn't look for common superclasses.
1951
+ return isa<ExistentialMetatypeType>(mt)
1952
+ ? CanExistentialMetatypeType::get (substInstance)
1936
1953
: CanType (CanMetatypeType::get (substInstance));
1937
1954
}
1938
1955
1939
- CanType handleGenericNominalType (CanType orig, Type subst,
1956
+ CanType handleGenericNominalType (CanType orig, CanType subst,
1940
1957
CanGenericSignature origSig) {
1941
1958
// If there are no loose type parameters in the pattern here, we don't need
1942
1959
// to do a recursive visit at all.
@@ -1945,7 +1962,7 @@ class SubstFunctionTypePatternVisitor
1945
1962
&& !orig->hasOpaqueArchetype ()) {
1946
1963
return CanType (subst);
1947
1964
}
1948
-
1965
+
1949
1966
// If the substituted type is a subclass of the abstraction pattern
1950
1967
// type, use the substituted type for the abstraction pattern. This only
1951
1968
// comes up when lowering override types for vtable entries.
@@ -1980,7 +1997,7 @@ class SubstFunctionTypePatternVisitor
1980
1997
}
1981
1998
1982
1999
if (differentOrigClass) {
1983
- orig = CanType ( subst) ;
2000
+ orig = subst;
1984
2001
origSig = TC.getCurGenericSignature ();
1985
2002
assert ((!subst->hasTypeParameter () || origSig) &&
1986
2003
" lowering mismatched interface types in a context without "
@@ -2017,10 +2034,7 @@ class SubstFunctionTypePatternVisitor
2017
2034
return decl->getDeclaredInterfaceType ().subst (newSubMap)->getCanonicalType ();
2018
2035
}
2019
2036
2020
- CanType visitNominalType (NominalType *nom, AbstractionPattern pattern) {
2021
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, nom))
2022
- return gp;
2023
-
2037
+ CanType visitNominalType (CanNominalType nom, AbstractionPattern pattern) {
2024
2038
auto nomDecl = nom->getDecl ();
2025
2039
2026
2040
// If the type is generic (because it's a nested type in a generic context),
@@ -2031,43 +2045,37 @@ class SubstFunctionTypePatternVisitor
2031
2045
}
2032
2046
2033
2047
// Otherwise, there are no structural type parameters to visit.
2034
- return CanType ( nom) ;
2048
+ return nom;
2035
2049
}
2036
2050
2037
- CanType visitBoundGenericType (BoundGenericType * bgt,
2051
+ CanType visitBoundGenericType (CanBoundGenericType bgt,
2038
2052
AbstractionPattern pattern) {
2039
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, bgt))
2040
- return gp;
2041
-
2042
2053
return handleGenericNominalType (pattern.getType (), bgt,
2043
2054
pattern.getGenericSignatureOrNull ());
2044
2055
}
2045
2056
2046
- CanType visitPackType (PackType *pack, AbstractionPattern pattern) {
2047
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, pack))
2048
- return gp;
2049
-
2057
+ CanType visitPackType (CanPackType pack, AbstractionPattern pattern) {
2050
2058
// Break down the pack.
2051
- SmallVector<Type , 4 > packElts;
2052
- for (unsigned i = 0 ; i < pack->getNumElements (); ++i ) {
2053
- packElts.push_back (visit (pack-> getElementType (i),
2059
+ SmallVector<CanType , 4 > packElts;
2060
+ for (auto i : range ( pack->getNumElements ()) ) {
2061
+ packElts.push_back (visit (pack. getElementType (i),
2054
2062
pattern.getPackElementType (i)));
2055
2063
}
2056
2064
2057
- return CanType ( PackType ::get (TC.Context , packElts) );
2065
+ return CanPackType ::get (TC.Context , packElts);
2058
2066
}
2059
2067
2060
- CanType visitPackExpansionType (PackExpansionType * pack,
2068
+ CanType visitPackExpansionType (CanPackExpansionType pack,
2061
2069
AbstractionPattern pattern) {
2062
2070
// Avoid walking into the pattern and count type if we can help it.
2063
2071
if (!pack->hasTypeParameter () && !pack->hasArchetype () &&
2064
2072
!pack->hasOpaqueArchetype ()) {
2065
2073
return CanType (pack);
2066
2074
}
2067
2075
2068
- auto substPatternType = visit (pack-> getPatternType (),
2076
+ auto substPatternType = visit (pack. getPatternType (),
2069
2077
pattern.getPackExpansionPatternType ());
2070
- auto substCountType = visit (pack-> getCountType (),
2078
+ auto substCountType = visit (pack. getCountType (),
2071
2079
AbstractionPattern::getOpaque ());
2072
2080
2073
2081
SmallVector<Type> rootParameterPacks;
@@ -2078,40 +2086,32 @@ class SubstFunctionTypePatternVisitor
2078
2086
parameterPack, substCountType);
2079
2087
}
2080
2088
2081
- return CanType (PackExpansionType::get (
2082
- substPatternType, substCountType));
2089
+ return CanPackExpansionType::get (substPatternType, substCountType);
2083
2090
}
2084
2091
2085
- CanType visitExistentialType (ExistentialType * exist,
2092
+ CanType visitExistentialType (CanExistentialType exist,
2086
2093
AbstractionPattern pattern) {
2087
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, exist))
2088
- return gp;
2089
-
2090
2094
// Avoid walking into the constraint type if we can help it.
2091
2095
if (!exist->hasTypeParameter () && !exist->hasArchetype () &&
2092
2096
!exist->hasOpaqueArchetype ()) {
2093
2097
return CanType (exist);
2094
2098
}
2095
2099
2096
2100
return CanExistentialType::get (visit (
2097
- exist-> getConstraintType (), pattern.getExistentialConstraintType ()));
2101
+ exist. getConstraintType (), pattern.getExistentialConstraintType ()));
2098
2102
}
2099
2103
2100
- CanType visitParameterizedProtocolType (ParameterizedProtocolType * ppt,
2104
+ CanType visitParameterizedProtocolType (CanParameterizedProtocolType ppt,
2101
2105
AbstractionPattern pattern) {
2102
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, ppt))
2103
- return gp;
2104
-
2105
2106
// Recurse into the arguments of the parameterized protocol.
2106
2107
SmallVector<Type, 4 > substArgs;
2107
2108
auto origPPT = pattern.getAs <ParameterizedProtocolType>();
2108
2109
if (!origPPT)
2109
- return CanType ( ppt) ;
2110
+ return ppt;
2110
2111
2111
2112
for (unsigned i = 0 ; i < ppt->getArgs ().size (); ++i) {
2112
- auto argTy = ppt->getArgs ()[i];
2113
- auto origArgTy = AbstractionPattern (pattern.getGenericSignatureOrNull (),
2114
- origPPT.getArgs ()[i]);
2113
+ auto argTy = ppt.getArgs ()[i];
2114
+ auto origArgTy = pattern.getParameterizedProtocolArgType (i);
2115
2115
auto substEltTy = visit (argTy, origArgTy);
2116
2116
substArgs.push_back (substEltTy);
2117
2117
}
@@ -2120,22 +2120,20 @@ class SubstFunctionTypePatternVisitor
2120
2120
TC.Context , ppt->getBaseType (), substArgs));
2121
2121
}
2122
2122
2123
- CanType visitTupleType (TupleType *tuple, AbstractionPattern pattern) {
2124
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, tuple))
2125
- return gp;
2126
-
2123
+ CanType visitTupleType (CanTupleType tuple, AbstractionPattern pattern) {
2127
2124
// Break down the tuple.
2128
2125
SmallVector<TupleTypeElt, 4 > tupleElts;
2129
2126
for (unsigned i = 0 ; i < tuple->getNumElements (); ++i) {
2130
2127
auto elt = tuple->getElement (i);
2131
- auto substEltTy = visit (elt.getType (), pattern.getTupleElementType (i));
2128
+ auto substEltTy = visit (tuple.getElementType (i),
2129
+ pattern.getTupleElementType (i));
2132
2130
tupleElts.emplace_back (substEltTy, elt.getName ());
2133
2131
}
2134
2132
2135
2133
return CanType (TupleType::get (tupleElts, TC.Context ));
2136
2134
}
2137
2135
2138
- CanType handleUnabstractedFunctionType (AnyFunctionType * func,
2136
+ CanType handleUnabstractedFunctionType (CanAnyFunctionType func,
2139
2137
AbstractionPattern pattern,
2140
2138
CanType yieldType,
2141
2139
AbstractionPattern yieldPattern) {
@@ -2144,7 +2142,7 @@ class SubstFunctionTypePatternVisitor
2144
2142
for (unsigned i = 0 ; i < func->getParams ().size (); ++i) {
2145
2143
auto param = func->getParams ()[i];
2146
2144
// Lower the formal type of the argument binding, eliminating variadicity.
2147
- auto newParamTy = visit (param.getParameterType (true )-> getCanonicalType ( ),
2145
+ auto newParamTy = visit (CanType ( param.getParameterType (true )),
2148
2146
pattern.getFunctionParamType (i));
2149
2147
auto newParam = FunctionType::Param (newParamTy,
2150
2148
param.getLabel (),
@@ -2158,23 +2156,19 @@ class SubstFunctionTypePatternVisitor
2158
2156
substYieldType = visit (yieldType, yieldPattern);
2159
2157
}
2160
2158
2161
- auto newResultTy = visit (func-> getResult (),
2159
+ auto newResultTy = visit (func. getResult (),
2162
2160
pattern.getFunctionResultType ());
2163
2161
2164
2162
Optional<FunctionType::ExtInfo> extInfo;
2165
2163
if (func->hasExtInfo ())
2166
2164
extInfo = func->getExtInfo ();
2167
2165
2168
2166
return CanFunctionType::get (FunctionType::CanParamArrayRef (newParams),
2169
- CanType (newResultTy),
2170
- extInfo);
2167
+ newResultTy, extInfo);
2171
2168
}
2172
2169
2173
- CanType visitFunctionType (FunctionType * func,
2170
+ CanType visitFunctionType (CanFunctionType func,
2174
2171
AbstractionPattern pattern) {
2175
- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, func))
2176
- return gp;
2177
-
2178
2172
return handleUnabstractedFunctionType (func, pattern,
2179
2173
CanType (),
2180
2174
AbstractionPattern::getInvalid ());
0 commit comments