2424#include " swift/AST/GenericSignature.h"
2525#include " swift/AST/ModuleLoader.h"
2626#include " swift/AST/TypeCheckRequests.h"
27- #include " swift/AST/TypeVisitor .h"
27+ #include " swift/AST/CanTypeVisitor .h"
2828#include " swift/SIL/TypeLowering.h"
2929#include " clang/AST/ASTContext.h"
3030#include " clang/AST/Attr.h"
@@ -690,6 +690,28 @@ size_t AbstractionPattern::getNumPackExpandedComponents() const {
690690 return substShape->getNumElements ();
691691}
692692
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+
693715AbstractionPattern AbstractionPattern::removingMoveOnlyWrapper () const {
694716 switch (getKind ()) {
695717 case Kind::Invalid:
@@ -1798,8 +1820,8 @@ AbstractionPattern::operator==(const AbstractionPattern &other) const {
17981820
17991821namespace {
18001822class SubstFunctionTypePatternVisitor
1801- : public TypeVisitor <SubstFunctionTypePatternVisitor, CanType,
1802- AbstractionPattern>
1823+ : public CanTypeVisitor <SubstFunctionTypePatternVisitor, CanType,
1824+ AbstractionPattern>
18031825{
18041826public:
18051827 TypeConverter &TC;
@@ -1815,7 +1837,7 @@ class SubstFunctionTypePatternVisitor
18151837 // signature if `pattern` is a type parameter or opaque archetype. Returns
18161838 // null otherwise.
18171839 CanType handleTypeParameterInAbstractionPattern (AbstractionPattern pattern,
1818- Type substTy) {
1840+ CanType substTy) {
18191841 if (!pattern.isTypeParameterOrOpaqueArchetype ())
18201842 return CanType ();
18211843
@@ -1891,52 +1913,47 @@ class SubstFunctionTypePatternVisitor
18911913 return CanType (gp);
18921914 }
18931915
1894- CanType visitType (TypeBase * t, AbstractionPattern pattern) {
1916+ CanType visit (CanType t, AbstractionPattern pattern) {
18951917 if (auto gp = handleTypeParameterInAbstractionPattern (pattern, t))
18961918 return gp;
1897-
1919+
1920+ return CanTypeVisitor::visit (t, pattern);
1921+ }
1922+
1923+ CanType visitType (CanType t, AbstractionPattern pattern) {
18981924 assert (pattern.getType ()->isExistentialType () ||
18991925 (!pattern.getType ()->hasTypeParameter ()
19001926 && !pattern.getType ()->hasArchetype ()
19011927 && !pattern.getType ()->hasOpaqueArchetype ()));
19021928 return pattern.getType ();
19031929 }
19041930
1905- CanType visitDynamicSelfType (DynamicSelfType * dst,
1931+ CanType visitDynamicSelfType (CanDynamicSelfType dst,
19061932 AbstractionPattern pattern) {
1907- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, dst))
1908- return gp;
1909-
19101933 // A "dynamic self" type can be bound to another dynamic self type, or the
19111934 // 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);
19171938 return DynamicSelfType::get (newBase, TC.Context )
19181939 ->getCanonicalType ();
19191940 }
19201941
1921- return visit (dst-> getSelfType (), pattern);
1942+ return visit (dst. getSelfType (), pattern);
19221943 }
19231944
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 ()) ;
19271948
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)
19361953 : CanType (CanMetatypeType::get (substInstance));
19371954 }
19381955
1939- CanType handleGenericNominalType (CanType orig, Type subst,
1956+ CanType handleGenericNominalType (CanType orig, CanType subst,
19401957 CanGenericSignature origSig) {
19411958 // If there are no loose type parameters in the pattern here, we don't need
19421959 // to do a recursive visit at all.
@@ -1945,7 +1962,7 @@ class SubstFunctionTypePatternVisitor
19451962 && !orig->hasOpaqueArchetype ()) {
19461963 return CanType (subst);
19471964 }
1948-
1965+
19491966 // If the substituted type is a subclass of the abstraction pattern
19501967 // type, use the substituted type for the abstraction pattern. This only
19511968 // comes up when lowering override types for vtable entries.
@@ -1980,7 +1997,7 @@ class SubstFunctionTypePatternVisitor
19801997 }
19811998
19821999 if (differentOrigClass) {
1983- orig = CanType ( subst) ;
2000+ orig = subst;
19842001 origSig = TC.getCurGenericSignature ();
19852002 assert ((!subst->hasTypeParameter () || origSig) &&
19862003 " lowering mismatched interface types in a context without "
@@ -2017,10 +2034,7 @@ class SubstFunctionTypePatternVisitor
20172034 return decl->getDeclaredInterfaceType ().subst (newSubMap)->getCanonicalType ();
20182035 }
20192036
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) {
20242038 auto nomDecl = nom->getDecl ();
20252039
20262040 // If the type is generic (because it's a nested type in a generic context),
@@ -2031,43 +2045,37 @@ class SubstFunctionTypePatternVisitor
20312045 }
20322046
20332047 // Otherwise, there are no structural type parameters to visit.
2034- return CanType ( nom) ;
2048+ return nom;
20352049 }
20362050
2037- CanType visitBoundGenericType (BoundGenericType * bgt,
2051+ CanType visitBoundGenericType (CanBoundGenericType bgt,
20382052 AbstractionPattern pattern) {
2039- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, bgt))
2040- return gp;
2041-
20422053 return handleGenericNominalType (pattern.getType (), bgt,
20432054 pattern.getGenericSignatureOrNull ());
20442055 }
20452056
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) {
20502058 // 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),
20542062 pattern.getPackElementType (i)));
20552063 }
20562064
2057- return CanType ( PackType ::get (TC.Context , packElts) );
2065+ return CanPackType ::get (TC.Context , packElts);
20582066 }
20592067
2060- CanType visitPackExpansionType (PackExpansionType * pack,
2068+ CanType visitPackExpansionType (CanPackExpansionType pack,
20612069 AbstractionPattern pattern) {
20622070 // Avoid walking into the pattern and count type if we can help it.
20632071 if (!pack->hasTypeParameter () && !pack->hasArchetype () &&
20642072 !pack->hasOpaqueArchetype ()) {
20652073 return CanType (pack);
20662074 }
20672075
2068- auto substPatternType = visit (pack-> getPatternType (),
2076+ auto substPatternType = visit (pack. getPatternType (),
20692077 pattern.getPackExpansionPatternType ());
2070- auto substCountType = visit (pack-> getCountType (),
2078+ auto substCountType = visit (pack. getCountType (),
20712079 AbstractionPattern::getOpaque ());
20722080
20732081 SmallVector<Type> rootParameterPacks;
@@ -2078,40 +2086,32 @@ class SubstFunctionTypePatternVisitor
20782086 parameterPack, substCountType);
20792087 }
20802088
2081- return CanType (PackExpansionType::get (
2082- substPatternType, substCountType));
2089+ return CanPackExpansionType::get (substPatternType, substCountType);
20832090 }
20842091
2085- CanType visitExistentialType (ExistentialType * exist,
2092+ CanType visitExistentialType (CanExistentialType exist,
20862093 AbstractionPattern pattern) {
2087- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, exist))
2088- return gp;
2089-
20902094 // Avoid walking into the constraint type if we can help it.
20912095 if (!exist->hasTypeParameter () && !exist->hasArchetype () &&
20922096 !exist->hasOpaqueArchetype ()) {
20932097 return CanType (exist);
20942098 }
20952099
20962100 return CanExistentialType::get (visit (
2097- exist-> getConstraintType (), pattern.getExistentialConstraintType ()));
2101+ exist. getConstraintType (), pattern.getExistentialConstraintType ()));
20982102 }
20992103
2100- CanType visitParameterizedProtocolType (ParameterizedProtocolType * ppt,
2104+ CanType visitParameterizedProtocolType (CanParameterizedProtocolType ppt,
21012105 AbstractionPattern pattern) {
2102- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, ppt))
2103- return gp;
2104-
21052106 // Recurse into the arguments of the parameterized protocol.
21062107 SmallVector<Type, 4 > substArgs;
21072108 auto origPPT = pattern.getAs <ParameterizedProtocolType>();
21082109 if (!origPPT)
2109- return CanType ( ppt) ;
2110+ return ppt;
21102111
21112112 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);
21152115 auto substEltTy = visit (argTy, origArgTy);
21162116 substArgs.push_back (substEltTy);
21172117 }
@@ -2120,22 +2120,20 @@ class SubstFunctionTypePatternVisitor
21202120 TC.Context , ppt->getBaseType (), substArgs));
21212121 }
21222122
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) {
21272124 // Break down the tuple.
21282125 SmallVector<TupleTypeElt, 4 > tupleElts;
21292126 for (unsigned i = 0 ; i < tuple->getNumElements (); ++i) {
21302127 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));
21322130 tupleElts.emplace_back (substEltTy, elt.getName ());
21332131 }
21342132
21352133 return CanType (TupleType::get (tupleElts, TC.Context ));
21362134 }
21372135
2138- CanType handleUnabstractedFunctionType (AnyFunctionType * func,
2136+ CanType handleUnabstractedFunctionType (CanAnyFunctionType func,
21392137 AbstractionPattern pattern,
21402138 CanType yieldType,
21412139 AbstractionPattern yieldPattern) {
@@ -2144,7 +2142,7 @@ class SubstFunctionTypePatternVisitor
21442142 for (unsigned i = 0 ; i < func->getParams ().size (); ++i) {
21452143 auto param = func->getParams ()[i];
21462144 // 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 )),
21482146 pattern.getFunctionParamType (i));
21492147 auto newParam = FunctionType::Param (newParamTy,
21502148 param.getLabel (),
@@ -2158,23 +2156,19 @@ class SubstFunctionTypePatternVisitor
21582156 substYieldType = visit (yieldType, yieldPattern);
21592157 }
21602158
2161- auto newResultTy = visit (func-> getResult (),
2159+ auto newResultTy = visit (func. getResult (),
21622160 pattern.getFunctionResultType ());
21632161
21642162 Optional<FunctionType::ExtInfo> extInfo;
21652163 if (func->hasExtInfo ())
21662164 extInfo = func->getExtInfo ();
21672165
21682166 return CanFunctionType::get (FunctionType::CanParamArrayRef (newParams),
2169- CanType (newResultTy),
2170- extInfo);
2167+ newResultTy, extInfo);
21712168 }
21722169
2173- CanType visitFunctionType (FunctionType * func,
2170+ CanType visitFunctionType (CanFunctionType func,
21742171 AbstractionPattern pattern) {
2175- if (auto gp = handleTypeParameterInAbstractionPattern (pattern, func))
2176- return gp;
2177-
21782172 return handleUnabstractedFunctionType (func, pattern,
21792173 CanType (),
21802174 AbstractionPattern::getInvalid ());
0 commit comments