Skip to content

Commit a12c160

Browse files
committed
Sema: Replace calls to OverloadChoice constructor with two overloads of getDecl()
1 parent b181fcf commit a12c160

File tree

5 files changed

+49
-29
lines changed

5 files changed

+49
-29
lines changed

include/swift/Sema/OverloadChoice.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,20 +126,19 @@ class OverloadChoice {
126126
/// FIXME: This needs three bits. Can we pack them somewhere?
127127
FunctionRefInfo TheFunctionRefInfo = FunctionRefInfo::unappliedBaseName();
128128

129-
public:
130-
OverloadChoice() : BaseAndDeclKind(nullptr, 0), DeclOrKind() {}
131-
132129
OverloadChoice(Type base, ValueDecl *value,
133130
FunctionRefInfo functionRefInfo)
134131
: BaseAndDeclKind(base, 0),
135132
TheFunctionRefInfo(functionRefInfo) {
136-
assert(!base || !base->hasTypeParameter());
137133
assert((reinterpret_cast<uintptr_t>(value) & (uintptr_t)0x03) == 0 &&
138134
"Badly aligned decl");
139135

140136
DeclOrKind = value;
141137
}
142138

139+
public:
140+
OverloadChoice() : BaseAndDeclKind(nullptr, 0), DeclOrKind() {}
141+
143142
OverloadChoice(Type base, OverloadChoiceKind kind)
144143
: BaseAndDeclKind(base, 0), DeclOrKind(uint32_t(kind)) {
145144
assert(base && "Must have a base type for overload choice");
@@ -162,6 +161,22 @@ class OverloadChoice {
162161
BaseAndDeclKind.getInt() == 0 && DeclOrKind.isNull();
163162
}
164163

164+
/// Retrieve an overload choice for a declaration that was found via
165+
/// unqualified lookup.
166+
static OverloadChoice getDecl(ValueDecl *value,
167+
FunctionRefInfo functionRefInfo) {
168+
return OverloadChoice(Type(), value, functionRefInfo);
169+
}
170+
171+
172+
/// Retrieve an overload choice for a declaration that was found via
173+
/// qualified lookup.
174+
static OverloadChoice getDecl(Type base, ValueDecl *value,
175+
FunctionRefInfo functionRefInfo) {
176+
ASSERT(!base->hasTypeParameter());
177+
return OverloadChoice(base, value, functionRefInfo);
178+
}
179+
165180
/// Retrieve an overload choice for a declaration that was found via
166181
/// dynamic lookup.
167182
static OverloadChoice getDeclViaDynamic(Type base, ValueDecl *value,

lib/Sema/CSDiagnostics.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2465,9 +2465,10 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
24652465
const auto &declRef = SE->getDecl();
24662466
if (auto *subscript =
24672467
dyn_cast_or_null<SubscriptDecl>(declRef.getDecl())) {
2468-
if (isImmutable(subscript))
2469-
return {expr, OverloadChoice(getType(SE->getBase()), subscript,
2470-
FunctionRefInfo::doubleBaseNameApply())};
2468+
if (isImmutable(subscript)) {
2469+
return {expr, OverloadChoice::getDecl(getType(SE->getBase()), subscript,
2470+
FunctionRefInfo::doubleBaseNameApply())};
2471+
}
24712472
}
24722473
}
24732474

@@ -2522,8 +2523,8 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
25222523
// If the member isn't settable, then it is the problem: return it.
25232524
if (auto member = dyn_cast<AbstractStorageDecl>(MRE->getMember().getDecl()))
25242525
if (isImmutable(member))
2525-
return {expr, OverloadChoice(getType(MRE->getBase()), member,
2526-
FunctionRefInfo::singleBaseNameApply())};
2526+
return {expr, OverloadChoice::getDecl(getType(MRE->getBase()), member,
2527+
FunctionRefInfo::singleBaseNameApply())};
25272528

25282529
// If we weren't able to resolve a member or if it is mutable, then the
25292530
// problem must be with the base, recurse.
@@ -2543,8 +2544,8 @@ AssignmentFailure::resolveImmutableBase(Expr *expr) const {
25432544
}
25442545

25452546
if (auto *DRE = dyn_cast<DeclRefExpr>(expr))
2546-
return {expr, OverloadChoice(Type(), DRE->getDecl(),
2547-
FunctionRefInfo::unappliedBaseName())};
2547+
return {expr, OverloadChoice::getDecl(DRE->getDecl(),
2548+
FunctionRefInfo::unappliedBaseName())};
25482549

25492550
// Look through x!
25502551
if (auto *FVE = dyn_cast<ForceValueExpr>(expr))

lib/Sema/CSGen.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ namespace {
10201020
options);
10211021
SmallVector<OverloadChoice, 4> outerChoices;
10221022
for (auto decl : outerAlternatives) {
1023-
outerChoices.push_back(OverloadChoice(Type(), decl, functionRefInfo));
1023+
outerChoices.push_back(OverloadChoice::getDecl(decl, functionRefInfo));
10241024
}
10251025
CS.addValueMemberConstraint(
10261026
baseTy, name, tv, CurDC, functionRefInfo, outerChoices,
@@ -1044,8 +1044,8 @@ namespace {
10441044
auto tv = CS.createTypeVariable(memberLocator,
10451045
TVO_CanBindToLValue | TVO_CanBindToNoEscape);
10461046

1047-
OverloadChoice choice =
1048-
OverloadChoice(CS.getType(base), decl, functionRefInfo);
1047+
auto choice =
1048+
OverloadChoice::getDecl(CS.getType(base), decl, functionRefInfo);
10491049

10501050
auto locator = CS.getConstraintLocator(expr, ConstraintLocator::Member);
10511051
CS.addBindOverloadConstraint(tv, choice, locator, CurDC);
@@ -1162,7 +1162,7 @@ namespace {
11621162
// a known subscript here. This might be cleaner if we split off a new
11631163
// UnresolvedSubscriptExpr from SubscriptExpr.
11641164
if (auto decl = declOrNull) {
1165-
OverloadChoice choice = OverloadChoice(
1165+
auto choice = OverloadChoice::getDecl(
11661166
baseTy, decl, FunctionRefInfo::doubleBaseNameApply());
11671167
CS.addBindOverloadConstraint(memberTy, choice, memberLocator,
11681168
CurDC);
@@ -1657,8 +1657,8 @@ namespace {
16571657
// resolve it. This records the overload for use later.
16581658
auto tv = CS.createTypeVariable(locator, options);
16591659

1660-
OverloadChoice choice =
1661-
OverloadChoice(Type(), E->getDecl(), E->getFunctionRefInfo());
1660+
auto choice =
1661+
OverloadChoice::getDecl(E->getDecl(), E->getFunctionRefInfo());
16621662
CS.addBindOverloadConstraint(tv, choice, locator, CurDC);
16631663
return tv;
16641664
}
@@ -1775,8 +1775,8 @@ namespace {
17751775
if (decls[i]->isInvalid())
17761776
continue;
17771777

1778-
OverloadChoice choice =
1779-
OverloadChoice(Type(), decls[i], expr->getFunctionRefInfo());
1778+
auto choice =
1779+
OverloadChoice::getDecl(decls[i], expr->getFunctionRefInfo());
17801780
choices.push_back(choice);
17811781
}
17821782

@@ -4120,7 +4120,7 @@ namespace {
41204120
// logic.
41214121
if (result->isInvalid())
41224122
continue;
4123-
OverloadChoice choice = OverloadChoice(Type(), result, functionRefInfo);
4123+
auto choice = OverloadChoice::getDecl(result, functionRefInfo);
41244124
choices.push_back(choice);
41254125
}
41264126

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10491,8 +10491,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
1049110491
auto choice =
1049210492
instanceTy->isAnyObject()
1049310493
? candidate
10494-
: OverloadChoice(instanceTy, decl,
10495-
FunctionRefInfo::singleBaseNameApply());
10494+
: OverloadChoice::getDecl(instanceTy, decl,
10495+
FunctionRefInfo::singleBaseNameApply());
1049610496

1049710497
const bool invalidMethodRef = isa<FuncDecl>(decl) && !hasInstanceMethods;
1049810498
const bool invalidMemberRef = !isa<FuncDecl>(decl) && !hasInstanceMembers;
@@ -10727,7 +10727,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
1072710727
}
1072810728
}
1072910729

10730-
return OverloadChoice(baseTy, cand, functionRefInfo);
10730+
return OverloadChoice::getDecl(baseTy, cand, functionRefInfo);
1073110731
};
1073210732

1073310733
// Add all results from this lookup.
@@ -11866,7 +11866,8 @@ ConstraintSystem::simplifyValueWitnessConstraint(
1186611866
if (!witness)
1186711867
return fail();
1186811868

11869-
auto choice = OverloadChoice(resolvedBaseType, witness.getDecl(), functionRefInfo);
11869+
auto choice = OverloadChoice::getDecl(
11870+
resolvedBaseType, witness.getDecl(), functionRefInfo);
1187011871
addBindOverloadConstraint(memberType, choice, getConstraintLocator(locator),
1187111872
useDC);
1187211873
return SolutionKind::Solved;
@@ -14110,8 +14111,8 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
1411014111
SmallVector<OverloadChoice, 4> choices;
1411114112
for (auto candidate : candidates) {
1411214113
if (candidate->isInvalid()) continue;
14113-
choices.push_back(OverloadChoice(type2, candidate,
14114-
FunctionRefInfo::singleBaseNameApply()));
14114+
choices.push_back(OverloadChoice::getDecl(type2, candidate,
14115+
FunctionRefInfo::singleBaseNameApply()));
1411514116
}
1411614117

1411714118
if (choices.empty()) {

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
11701170
// Open up the type of the requirement.
11711171
reqLocator =
11721172
cs->getConstraintLocator(req, ConstraintLocator::ProtocolRequirement);
1173-
OverloadChoice reqChoice(selfTy, req, FunctionRefInfo::doubleBaseNameApply());
1173+
auto reqChoice = OverloadChoice::getDecl(
1174+
selfTy, req, FunctionRefInfo::doubleBaseNameApply());
11741175
auto reqTypeInfo =
11751176
cs->getTypeOfMemberReference(reqChoice, dc, reqLocator,
11761177
/*preparedOverload=*/nullptr);
@@ -1206,12 +1207,14 @@ swift::matchWitness(WitnessChecker::RequirementEnvironmentCache &reqEnvCache,
12061207
DeclReferenceType openWitnessTypeInfo;
12071208

12081209
if (witness->getDeclContext()->isTypeContext()) {
1209-
OverloadChoice witnessChoice(selfTy, witness, FunctionRefInfo::doubleBaseNameApply());
1210+
auto witnessChoice = OverloadChoice::getDecl(
1211+
selfTy, witness, FunctionRefInfo::doubleBaseNameApply());
12101212
openWitnessTypeInfo =
12111213
cs->getTypeOfMemberReference(witnessChoice, dc, witnessLocator,
12121214
/*preparedOverload=*/nullptr);
12131215
} else {
1214-
OverloadChoice witnessChoice(Type(), witness, FunctionRefInfo::doubleBaseNameApply());
1216+
auto witnessChoice = OverloadChoice::getDecl(
1217+
witness, FunctionRefInfo::doubleBaseNameApply());
12151218
openWitnessTypeInfo =
12161219
cs->getTypeOfReference(
12171220
witnessChoice, /*useDC=*/nullptr, witnessLocator,

0 commit comments

Comments
 (0)