Skip to content

Commit 118f68b

Browse files
committed
Make getProtocol and getLiteralProtocol static utilities
1 parent 929332e commit 118f68b

10 files changed

+101
-95
lines changed

lib/Sema/CSApply.cpp

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,12 +1837,12 @@ namespace {
18371837
return expr;
18381838

18391839
auto &tc = cs.getTypeChecker();
1840-
ProtocolDecl *protocol
1841-
= tc.getProtocol(expr->getLoc(),
1842-
KnownProtocolKind::ExpressibleByIntegerLiteral);
1843-
ProtocolDecl *builtinProtocol
1844-
= tc.getProtocol(expr->getLoc(),
1845-
KnownProtocolKind::ExpressibleByBuiltinIntegerLiteral);
1840+
ProtocolDecl *protocol = TypeChecker::getProtocol(
1841+
cs.getASTContext(), expr->getLoc(),
1842+
KnownProtocolKind::ExpressibleByIntegerLiteral);
1843+
ProtocolDecl *builtinProtocol = TypeChecker::getProtocol(
1844+
cs.getASTContext(), expr->getLoc(),
1845+
KnownProtocolKind::ExpressibleByBuiltinIntegerLiteral);
18461846

18471847
// For type-sugar reasons, prefer the spelling of the default literal
18481848
// type.
@@ -1851,9 +1851,9 @@ namespace {
18511851
if (defaultType->isEqual(type))
18521852
type = defaultType;
18531853
}
1854-
if (auto floatProtocol
1855-
= tc.getProtocol(expr->getLoc(),
1856-
KnownProtocolKind::ExpressibleByFloatLiteral)) {
1854+
if (auto floatProtocol = TypeChecker::getProtocol(
1855+
cs.getASTContext(), expr->getLoc(),
1856+
KnownProtocolKind::ExpressibleByFloatLiteral)) {
18571857
if (auto defaultFloatType = tc.getDefaultType(floatProtocol, dc)) {
18581858
if (defaultFloatType->isEqual(type))
18591859
type = defaultFloatType;
@@ -1888,8 +1888,9 @@ namespace {
18881888
}
18891889

18901890
auto &tc = cs.getTypeChecker();
1891-
auto *protocol = tc.getProtocol(expr->getLoc(),
1892-
KnownProtocolKind::ExpressibleByNilLiteral);
1891+
auto *protocol =
1892+
TypeChecker::getProtocol(cs.getASTContext(), expr->getLoc(),
1893+
KnownProtocolKind::ExpressibleByNilLiteral);
18931894

18941895
// For type-sugar reasons, prefer the spelling of the default literal
18951896
// type.
@@ -1920,12 +1921,12 @@ namespace {
19201921
return expr;
19211922

19221923
auto &tc = cs.getTypeChecker();
1923-
ProtocolDecl *protocol
1924-
= tc.getProtocol(expr->getLoc(),
1925-
KnownProtocolKind::ExpressibleByFloatLiteral);
1926-
ProtocolDecl *builtinProtocol
1927-
= tc.getProtocol(expr->getLoc(),
1928-
KnownProtocolKind::ExpressibleByBuiltinFloatLiteral);
1924+
ProtocolDecl *protocol = TypeChecker::getProtocol(
1925+
cs.getASTContext(), expr->getLoc(),
1926+
KnownProtocolKind::ExpressibleByFloatLiteral);
1927+
ProtocolDecl *builtinProtocol = TypeChecker::getProtocol(
1928+
cs.getASTContext(), expr->getLoc(),
1929+
KnownProtocolKind::ExpressibleByBuiltinFloatLiteral);
19291930

19301931
// For type-sugar reasons, prefer the spelling of the default literal
19311932
// type.
@@ -1964,12 +1965,12 @@ namespace {
19641965
return expr;
19651966

19661967
auto &tc = cs.getTypeChecker();
1967-
ProtocolDecl *protocol
1968-
= tc.getProtocol(expr->getLoc(),
1969-
KnownProtocolKind::ExpressibleByBooleanLiteral);
1970-
ProtocolDecl *builtinProtocol
1971-
= tc.getProtocol(expr->getLoc(),
1972-
KnownProtocolKind::ExpressibleByBuiltinBooleanLiteral);
1968+
ProtocolDecl *protocol = TypeChecker::getProtocol(
1969+
cs.getASTContext(), expr->getLoc(),
1970+
KnownProtocolKind::ExpressibleByBooleanLiteral);
1971+
ProtocolDecl *builtinProtocol = TypeChecker::getProtocol(
1972+
cs.getASTContext(), expr->getLoc(),
1973+
KnownProtocolKind::ExpressibleByBuiltinBooleanLiteral);
19731974
if (!protocol || !builtinProtocol)
19741975
return nullptr;
19751976

@@ -2001,24 +2002,25 @@ namespace {
20012002

20022003
bool isStringLiteral = true;
20032004
bool isGraphemeClusterLiteral = false;
2004-
ProtocolDecl *protocol = tc.getProtocol(
2005-
expr->getLoc(), KnownProtocolKind::ExpressibleByStringLiteral);
2005+
ProtocolDecl *protocol = TypeChecker::getProtocol(
2006+
cs.getASTContext(), expr->getLoc(),
2007+
KnownProtocolKind::ExpressibleByStringLiteral);
20062008

20072009
if (!TypeChecker::conformsToProtocol(
20082010
type, protocol, cs.DC, ConformanceCheckFlags::InExpression)) {
20092011
// If the type does not conform to ExpressibleByStringLiteral, it should
20102012
// be ExpressibleByExtendedGraphemeClusterLiteral.
2011-
protocol = tc.getProtocol(
2012-
expr->getLoc(),
2013+
protocol = TypeChecker::getProtocol(
2014+
cs.getASTContext(), expr->getLoc(),
20132015
KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral);
20142016
isStringLiteral = false;
20152017
isGraphemeClusterLiteral = true;
20162018
}
20172019
if (!TypeChecker::conformsToProtocol(
20182020
type, protocol, cs.DC, ConformanceCheckFlags::InExpression)) {
20192021
// ... or it should be ExpressibleByUnicodeScalarLiteral.
2020-
protocol = tc.getProtocol(
2021-
expr->getLoc(),
2022+
protocol = TypeChecker::getProtocol(
2023+
cs.getASTContext(), expr->getLoc(),
20222024
KnownProtocolKind::ExpressibleByUnicodeScalarLiteral);
20232025
isStringLiteral = false;
20242026
isGraphemeClusterLiteral = false;
@@ -2044,8 +2046,8 @@ namespace {
20442046
literalFuncName = DeclName(tc.Context, DeclBaseName::createConstructor(),
20452047
{ tc.Context.Id_stringLiteral });
20462048

2047-
builtinProtocol = tc.getProtocol(
2048-
expr->getLoc(),
2049+
builtinProtocol = TypeChecker::getProtocol(
2050+
cs.getASTContext(), expr->getLoc(),
20492051
KnownProtocolKind::ExpressibleByBuiltinStringLiteral);
20502052
builtinLiteralFuncName
20512053
= DeclName(tc.Context, DeclBaseName::createConstructor(),
@@ -2070,9 +2072,10 @@ namespace {
20702072
tc.Context.getIdentifier("utf8CodeUnitCount"),
20712073
tc.Context.getIdentifier("isASCII") });
20722074

2073-
builtinProtocol = tc.getProtocol(
2074-
expr->getLoc(),
2075-
KnownProtocolKind::ExpressibleByBuiltinExtendedGraphemeClusterLiteral);
2075+
builtinProtocol = TypeChecker::getProtocol(
2076+
cs.getASTContext(), expr->getLoc(),
2077+
KnownProtocolKind::
2078+
ExpressibleByBuiltinExtendedGraphemeClusterLiteral);
20762079
brokenProtocolDiag =
20772080
diag::extended_grapheme_cluster_literal_broken_proto;
20782081
brokenBuiltinProtocolDiag =
@@ -2088,8 +2091,8 @@ namespace {
20882091
= DeclName(tc.Context, DeclBaseName::createConstructor(),
20892092
{tc.Context.Id_builtinUnicodeScalarLiteral});
20902093

2091-
builtinProtocol = tc.getProtocol(
2092-
expr->getLoc(),
2094+
builtinProtocol = TypeChecker::getProtocol(
2095+
cs.getASTContext(), expr->getLoc(),
20932096
KnownProtocolKind::ExpressibleByBuiltinUnicodeScalarLiteral);
20942097

20952098
brokenProtocolDiag = diag::unicode_scalar_literal_broken_proto;
@@ -2125,10 +2128,10 @@ namespace {
21252128
auto loc = expr->getStartLoc();
21262129

21272130
auto fetchProtocolInitWitness =
2128-
[&](KnownProtocolKind protocolKind, Type type,
2129-
ArrayRef<Identifier> argLabels) -> ConcreteDeclRef {
2130-
2131-
auto proto = tc.getProtocol(loc, protocolKind);
2131+
[&](KnownProtocolKind protocolKind, Type type,
2132+
ArrayRef<Identifier> argLabels) -> ConcreteDeclRef {
2133+
auto proto =
2134+
TypeChecker::getProtocol(cs.getASTContext(), loc, protocolKind);
21322135
assert(proto && "Missing string interpolation protocol?");
21332136

21342137
auto conformance =
@@ -2145,9 +2148,9 @@ namespace {
21452148
return witness;
21462149
};
21472150

2148-
auto *interpolationProto =
2149-
tc.getProtocol(expr->getLoc(),
2150-
KnownProtocolKind::ExpressibleByStringInterpolation);
2151+
auto *interpolationProto = TypeChecker::getProtocol(
2152+
cs.getASTContext(), expr->getLoc(),
2153+
KnownProtocolKind::ExpressibleByStringInterpolation);
21512154
auto associatedTypeDecl = interpolationProto->getAssociatedType(
21522155
tc.Context.Id_StringInterpolation);
21532156
if (associatedTypeDecl == nullptr) {
@@ -2235,7 +2238,7 @@ namespace {
22352238
}
22362239

22372240
// Find the appropriate object literal protocol.
2238-
auto proto = tc.getLiteralProtocol(expr);
2241+
auto proto = TypeChecker::getLiteralProtocol(cs.getASTContext(), expr);
22392242
assert(proto && "Missing object literal protocol?");
22402243
auto conformance =
22412244
TypeChecker::conformsToProtocol(conformingType, proto, cs.DC,
@@ -2891,9 +2894,9 @@ namespace {
28912894
Type arrayTy = cs.getType(expr);
28922895
auto &tc = cs.getTypeChecker();
28932896

2894-
ProtocolDecl *arrayProto
2895-
= tc.getProtocol(expr->getLoc(),
2896-
KnownProtocolKind::ExpressibleByArrayLiteral);
2897+
ProtocolDecl *arrayProto = TypeChecker::getProtocol(
2898+
cs.getASTContext(), expr->getLoc(),
2899+
KnownProtocolKind::ExpressibleByArrayLiteral);
28972900
assert(arrayProto && "type-checked array literal w/o protocol?!");
28982901

28992902
auto conformance =
@@ -2937,9 +2940,9 @@ namespace {
29372940
Type dictionaryTy = cs.getType(expr);
29382941

29392942
auto &tc = cs.getTypeChecker();
2940-
ProtocolDecl *dictionaryProto
2941-
= tc.getProtocol(expr->getLoc(),
2942-
KnownProtocolKind::ExpressibleByDictionaryLiteral);
2943+
ProtocolDecl *dictionaryProto = TypeChecker::getProtocol(
2944+
cs.getASTContext(), expr->getLoc(),
2945+
KnownProtocolKind::ExpressibleByDictionaryLiteral);
29432946

29442947
auto conformance =
29452948
TypeChecker::conformsToProtocol(dictionaryTy, dictionaryProto, cs.DC,
@@ -6772,8 +6775,8 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
67726775
auto dictLitProto =
67736776
ctx.getProtocol(KnownProtocolKind::ExpressibleByDictionaryLiteral);
67746777
auto conformance =
6775-
cs.TC.conformsToProtocol(argumentType, dictLitProto, cs.DC,
6776-
ConformanceCheckFlags::InExpression);
6778+
TypeChecker::conformsToProtocol(argumentType, dictLitProto, cs.DC,
6779+
ConformanceCheckFlags::InExpression);
67776780
auto keyType = conformance.getTypeWitnessByName(argumentType, ctx.Id_Key);
67786781
auto valueType =
67796782
conformance.getTypeWitnessByName(argumentType, ctx.Id_Value);

lib/Sema/CSDiag.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3663,7 +3663,8 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
36633663

36643664
// Validate that the contextual type conforms to ExpressibleByArrayLiteral and
36653665
// figure out what the contextual element type is in place.
3666-
auto ALC = CS.TC.getProtocol(E->getLoc(),
3666+
auto ALC =
3667+
TypeChecker::getProtocol(CS.getASTContext(), E->getLoc(),
36673668
KnownProtocolKind::ExpressibleByArrayLiteral);
36683669
if (!ALC)
36693670
return visitExpr(E);
@@ -3713,8 +3714,9 @@ bool FailureDiagnosis::visitDictionaryExpr(DictionaryExpr *E) {
37133714
// surely initializing whatever is inside.
37143715
contextualType = contextualType->lookThroughAllOptionalTypes();
37153716

3716-
auto DLC = CS.TC.getProtocol(
3717-
E->getLoc(), KnownProtocolKind::ExpressibleByDictionaryLiteral);
3717+
auto DLC = TypeChecker::getProtocol(
3718+
CS.getASTContext(), E->getLoc(),
3719+
KnownProtocolKind::ExpressibleByDictionaryLiteral);
37183720
if (!DLC) return visitExpr(E);
37193721

37203722
// Validate the contextual type conforms to ExpressibleByDictionaryLiteral
@@ -3774,7 +3776,7 @@ bool FailureDiagnosis::visitObjectLiteralExpr(ObjectLiteralExpr *E) {
37743776
auto &TC = CS.getTypeChecker();
37753777

37763778
// Type check the argument first.
3777-
auto protocol = TC.getLiteralProtocol(E);
3779+
auto protocol = TypeChecker::getLiteralProtocol(CS.getASTContext(), E);
37783780
if (!protocol)
37793781
return false;
37803782
DeclName constrName = TC.getObjectLiteralConstructorName(E);

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2613,8 +2613,7 @@ bool ContextualFailure::tryProtocolConformanceFixIt(
26132613
// Let's build a list of protocols that the context does not conform to.
26142614
SmallVector<std::string, 8> missingProtoTypeStrings;
26152615
for (auto protocol : layout.getProtocols()) {
2616-
if (getTypeChecker()
2617-
.conformsToProtocol(FromType, protocol->getDecl(), getDC(),
2616+
if (TypeChecker::conformsToProtocol(FromType, protocol->getDecl(), getDC(),
26182617
ConformanceCheckFlags::InExpression)
26192618
.isInvalid()) {
26202619
missingProtoTypeStrings.push_back(protocol->getString());

lib/Sema/CSGen.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ namespace {
11501150
if (expr->getType())
11511151
return expr->getType();
11521152

1153-
auto protocol = CS.getTypeChecker().getLiteralProtocol(expr);
1153+
auto protocol = TypeChecker::getLiteralProtocol(CS.getASTContext(), expr);
11541154
if (!protocol)
11551155
return nullptr;
11561156

@@ -1167,9 +1167,9 @@ namespace {
11671167
visitInterpolatedStringLiteralExpr(InterpolatedStringLiteralExpr *expr) {
11681168
// Dig out the ExpressibleByStringInterpolation protocol.
11691169
auto &tc = CS.getTypeChecker();
1170-
auto interpolationProto
1171-
= tc.getProtocol(expr->getLoc(),
1172-
KnownProtocolKind::ExpressibleByStringInterpolation);
1170+
auto interpolationProto = TypeChecker::getProtocol(
1171+
CS.getASTContext(), expr->getLoc(),
1172+
KnownProtocolKind::ExpressibleByStringInterpolation);
11731173
if (!interpolationProto) {
11741174
tc.diagnose(expr->getStartLoc(), diag::interpolation_missing_proto);
11751175
return nullptr;
@@ -1240,7 +1240,7 @@ namespace {
12401240
return expr->getType();
12411241

12421242
auto &tc = CS.getTypeChecker();
1243-
auto protocol = tc.getLiteralProtocol(expr);
1243+
auto protocol = TypeChecker::getLiteralProtocol(CS.getASTContext(), expr);
12441244
if (!protocol) {
12451245
tc.diagnose(expr->getLoc(), diag::use_unknown_object_literal_protocol,
12461246
expr->getLiteralKindPlainName());
@@ -1763,9 +1763,9 @@ namespace {
17631763
// An array expression can be of a type T that conforms to the
17641764
// ExpressibleByArrayLiteral protocol.
17651765
auto &tc = CS.getTypeChecker();
1766-
ProtocolDecl *arrayProto
1767-
= tc.getProtocol(expr->getLoc(),
1768-
KnownProtocolKind::ExpressibleByArrayLiteral);
1766+
ProtocolDecl *arrayProto = TypeChecker::getProtocol(
1767+
CS.getASTContext(), expr->getLoc(),
1768+
KnownProtocolKind::ExpressibleByArrayLiteral);
17691769
if (!arrayProto) {
17701770
return Type();
17711771
}
@@ -1846,9 +1846,8 @@ namespace {
18461846
// ExpressibleByDictionaryLiteral protocol.
18471847
// FIXME: This isn't actually used for anything at the moment.
18481848
auto &tc = CS.getTypeChecker();
1849-
ProtocolDecl *dictionaryProto
1850-
= tc.getProtocol(expr->getLoc(),
1851-
KnownProtocolKind::ExpressibleByDictionaryLiteral);
1849+
ProtocolDecl *dictionaryProto = TypeChecker::getProtocol(
1850+
C, expr->getLoc(), KnownProtocolKind::ExpressibleByDictionaryLiteral);
18521851
if (!dictionaryProto) {
18531852
return Type();
18541853
}

lib/Sema/ConstraintSystem.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ void ConstraintSystem::assignFixedType(TypeVariableType *typeVar, Type type,
182182
if (!anchor)
183183
continue;
184184

185-
literalProtocol = TC.getLiteralProtocol(anchor);
185+
literalProtocol =
186+
TypeChecker::getLiteralProtocol(getASTContext(), anchor);
186187
if (literalProtocol)
187188
break;
188189
}
@@ -1781,8 +1782,9 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
17811782
// components.
17821783
auto verifyThatArgumentIsHashable = [&](unsigned index, Type argType,
17831784
ConstraintLocator *locator) {
1784-
if (auto *hashable = TC.getProtocol(choice.getDecl()->getLoc(),
1785-
KnownProtocolKind::Hashable)) {
1785+
if (auto *hashable = TypeChecker::getProtocol(
1786+
argType->getASTContext(), choice.getDecl()->getLoc(),
1787+
KnownProtocolKind::Hashable)) {
17861788
addConstraint(ConstraintKind::ConformsTo, argType,
17871789
hashable->getDeclaredType(),
17881790
getConstraintLocator(
@@ -1966,11 +1968,9 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
19661968
"subscript always has one arg");
19671969
auto argType = refFnType->getParams()[0].getPlainType();
19681970

1969-
auto &TC = getTypeChecker();
1970-
1971-
auto stringLiteral =
1972-
TC.getProtocol(choice.getDecl()->getLoc(),
1973-
KnownProtocolKind::ExpressibleByStringLiteral);
1971+
auto stringLiteral = TypeChecker::getProtocol(
1972+
getASTContext(), choice.getDecl()->getLoc(),
1973+
KnownProtocolKind::ExpressibleByStringLiteral);
19741974
if (!stringLiteral)
19751975
break;
19761976

@@ -3078,7 +3078,8 @@ bool constraints::hasAppliedSelf(ConstraintSystem &cs,
30783078

30793079
bool constraints::conformsToKnownProtocol(ConstraintSystem &cs, Type type,
30803080
KnownProtocolKind protocol) {
3081-
if (auto *proto = cs.TC.getProtocol(SourceLoc(), protocol))
3081+
if (auto *proto =
3082+
TypeChecker::getProtocol(cs.getASTContext(), SourceLoc(), protocol))
30823083
return (bool)TypeChecker::conformsToProtocol(
30833084
type, proto, cs.DC, ConformanceCheckFlags::InExpression);
30843085
return false;
@@ -3090,8 +3091,8 @@ Type constraints::isRawRepresentable(ConstraintSystem &cs, Type type) {
30903091
auto &TC = cs.TC;
30913092
auto *DC = cs.DC;
30923093

3093-
auto rawReprType =
3094-
TC.getProtocol(SourceLoc(), KnownProtocolKind::RawRepresentable);
3094+
auto rawReprType = TypeChecker::getProtocol(
3095+
cs.getASTContext(), SourceLoc(), KnownProtocolKind::RawRepresentable);
30953096
if (!rawReprType)
30963097
return Type();
30973098

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
19631963
if (!literal)
19641964
return nullptr;
19651965

1966-
auto *protocol = TC.getLiteralProtocol(literal);
1966+
auto *protocol = TypeChecker::getLiteralProtocol(TC.Context, literal);
19671967
if (!protocol)
19681968
return nullptr;
19691969

@@ -2914,8 +2914,8 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
29142914

29152915
// The expression type must conform to the Sequence.
29162916
auto &tc = cs.getTypeChecker();
2917-
ProtocolDecl *sequenceProto
2918-
= tc.getProtocol(Stmt->getForLoc(), KnownProtocolKind::Sequence);
2917+
ProtocolDecl *sequenceProto = TypeChecker::getProtocol(
2918+
cs.getASTContext(), Stmt->getForLoc(), KnownProtocolKind::Sequence);
29192919
if (!sequenceProto) {
29202920
return true;
29212921
}

0 commit comments

Comments
 (0)