Skip to content

Commit 58c05e5

Browse files
authored
Merge pull request #26421 from xedin/arg-info-cleanup
[ConstraintSystem] Refactor argument information collection and use it in more places
2 parents 4db3938 + 45aec3c commit 58c05e5

File tree

4 files changed

+68
-107
lines changed

4 files changed

+68
-107
lines changed

lib/Sema/CSGen.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,8 +2490,7 @@ namespace {
24902490

24912491
SmallVector<Identifier, 4> scratch;
24922492
associateArgumentLabels(
2493-
fnExpr,
2494-
{expr->getArgumentLabels(scratch), expr->hasTrailingClosure()},
2493+
expr, {expr->getArgumentLabels(scratch), expr->hasTrailingClosure()},
24952494
/*labelsArePermanent=*/isa<CallExpr>(expr));
24962495

24972496
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(fnExpr)) {
@@ -3225,15 +3224,14 @@ namespace {
32253224
llvm_unreachable("unhandled operation");
32263225
}
32273226

3228-
void associateArgumentLabels(Expr *fn,
3229-
ConstraintSystem::ArgumentLabelState labels,
3227+
void associateArgumentLabels(Expr *expr,
3228+
ConstraintSystem::ArgumentInfo info,
32303229
bool labelsArePermanent = true) {
3231-
fn = getArgumentLabelTargetExpr(fn);
3232-
3230+
assert(expr);
32333231
// Record the labels.
32343232
if (!labelsArePermanent)
3235-
labels.Labels = CS.allocateCopy(labels.Labels);
3236-
CS.ArgumentLabels[CS.getConstraintLocator(fn)] = labels;
3233+
info.Labels = CS.allocateCopy(info.Labels);
3234+
CS.ArgumentInfos[CS.getArgumentInfoLocator(expr)] = info;
32373235
}
32383236
};
32393237

lib/Sema/CSSimplify.cpp

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -719,43 +719,34 @@ matchCallArguments(ArrayRef<AnyFunctionType::Param> args,
719719
static std::tuple<ValueDecl *, bool, ArrayRef<Identifier>, bool,
720720
ConstraintLocator *>
721721
getCalleeDeclAndArgs(ConstraintSystem &cs,
722-
ConstraintLocatorBuilder callLocator,
723-
SmallVectorImpl<Identifier> &argLabelsScratch) {
722+
ConstraintLocatorBuilder callBuilder) {
724723
ArrayRef<Identifier> argLabels;
725724
bool hasTrailingClosure = false;
725+
ConstraintLocator *targetLocator = nullptr;
726+
727+
auto *callLocator = cs.getConstraintLocator(callBuilder);
728+
auto *callExpr = callLocator->getAnchor();
726729

727730
// Break down the call.
728-
SmallVector<LocatorPathElt, 2> path;
729-
auto callExpr = callLocator.getLocatorParts(path);
730731
if (!callExpr)
731732
return std::make_tuple(nullptr, /*hasAppliedSelf=*/false, argLabels,
732-
hasTrailingClosure, nullptr);
733+
hasTrailingClosure, targetLocator);
733734

735+
auto path = callLocator->getPath();
734736
// Our remaining path can only be 'ApplyArgument'.
735737
if (!path.empty() &&
736738
!(path.size() <= 2 &&
737739
path.back().getKind() == ConstraintLocator::ApplyArgument))
738740
return std::make_tuple(nullptr, /*hasAppliedSelf=*/false, argLabels,
739-
hasTrailingClosure, nullptr);
740-
741-
// Dig out the callee.
742-
ConstraintLocator *targetLocator;
743-
if (auto call = dyn_cast<CallExpr>(callExpr)) {
744-
targetLocator = cs.getConstraintLocator(call->getDirectCallee());
745-
argLabels = call->getArgumentLabels();
746-
hasTrailingClosure = call->hasTrailingClosure();
747-
} else if (auto unresolved = dyn_cast<UnresolvedMemberExpr>(callExpr)) {
748-
targetLocator = cs.getConstraintLocator(callExpr);
749-
argLabels = unresolved->getArgumentLabels();
750-
hasTrailingClosure = unresolved->hasTrailingClosure();
751-
} else if (auto subscript = dyn_cast<SubscriptExpr>(callExpr)) {
752-
targetLocator = cs.getConstraintLocator(callExpr);
753-
argLabels = subscript->getArgumentLabels();
754-
hasTrailingClosure = subscript->hasTrailingClosure();
755-
} else if (auto dynSubscript = dyn_cast<DynamicSubscriptExpr>(callExpr)) {
756-
targetLocator = cs.getConstraintLocator(callExpr);
757-
argLabels = dynSubscript->getArgumentLabels();
758-
hasTrailingClosure = dynSubscript->hasTrailingClosure();
741+
hasTrailingClosure, targetLocator);
742+
743+
// Dig out the callee information.
744+
if (auto argInfo = cs.getArgumentInfo(callLocator)) {
745+
argLabels = argInfo->Labels;
746+
hasTrailingClosure = argInfo->HasTrailingClosure;
747+
targetLocator = cs.getConstraintLocator(
748+
isa<CallExpr>(callExpr) ? cast<CallExpr>(callExpr)->getDirectCallee()
749+
: callExpr);
759750
} else if (auto keyPath = dyn_cast<KeyPathExpr>(callExpr)) {
760751
if (path.size() != 2 ||
761752
path[0].getKind() != ConstraintLocator::KeyPathComponent ||
@@ -788,17 +779,9 @@ getCalleeDeclAndArgs(ConstraintSystem &cs,
788779
return std::make_tuple(nullptr, /*hasAppliedSelf=*/false, argLabels,
789780
hasTrailingClosure, nullptr);
790781
}
791-
792782
} else {
793-
if (auto apply = dyn_cast<ApplyExpr>(callExpr)) {
794-
argLabels = apply->getArgumentLabels(argLabelsScratch);
795-
assert(!apply->hasTrailingClosure());
796-
} else if (auto objectLiteral = dyn_cast<ObjectLiteralExpr>(callExpr)) {
797-
argLabels = objectLiteral->getArgumentLabels();
798-
hasTrailingClosure = objectLiteral->hasTrailingClosure();
799-
}
800783
return std::make_tuple(nullptr, /*hasAppliedSelf=*/false, argLabels,
801-
hasTrailingClosure, nullptr);
784+
hasTrailingClosure, targetLocator);
802785
}
803786

804787
// Find the overload choice corresponding to the callee locator.
@@ -941,12 +924,11 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
941924
ValueDecl *callee;
942925
bool hasAppliedSelf;
943926
ArrayRef<Identifier> argLabels;
944-
SmallVector<Identifier, 2> argLabelsScratch;
945927
bool hasTrailingClosure = false;
946928
ConstraintLocator *calleeLocator;
947929
std::tie(callee, hasAppliedSelf, argLabels, hasTrailingClosure,
948930
calleeLocator) =
949-
getCalleeDeclAndArgs(cs, locator, argLabelsScratch);
931+
getCalleeDeclAndArgs(cs, locator);
950932

951933
ParameterListInfo paramInfo(params, callee, hasAppliedSelf);
952934

@@ -3931,49 +3913,6 @@ ConstraintSystem::simplifyFunctionComponentConstraint(
39313913
return SolutionKind::Solved;
39323914
}
39333915

3934-
/// Retrieve the argument labels that are provided for a member
3935-
/// reference at the given locator.
3936-
static Optional<ConstraintSystem::ArgumentLabelState>
3937-
getArgumentLabels(ConstraintSystem &cs, ConstraintLocatorBuilder locator) {
3938-
SmallVector<LocatorPathElt, 2> parts;
3939-
Expr *anchor = locator.getLocatorParts(parts);
3940-
if (!anchor)
3941-
return None;
3942-
3943-
while (!parts.empty()) {
3944-
if (parts.back().getKind() == ConstraintLocator::Member ||
3945-
parts.back().getKind() == ConstraintLocator::SubscriptMember) {
3946-
parts.pop_back();
3947-
continue;
3948-
}
3949-
3950-
if (parts.back().getKind() == ConstraintLocator::ApplyFunction) {
3951-
if (auto applyExpr = dyn_cast<ApplyExpr>(anchor)) {
3952-
anchor = applyExpr->getSemanticFn();
3953-
}
3954-
parts.pop_back();
3955-
continue;
3956-
}
3957-
3958-
if (parts.back().getKind() == ConstraintLocator::ConstructorMember) {
3959-
parts.pop_back();
3960-
continue;
3961-
}
3962-
3963-
break;
3964-
}
3965-
3966-
if (!parts.empty())
3967-
return None;
3968-
3969-
anchor = getArgumentLabelTargetExpr(anchor);
3970-
auto known = cs.ArgumentLabels.find(cs.getConstraintLocator(anchor));
3971-
if (known == cs.ArgumentLabels.end())
3972-
return None;
3973-
3974-
return known->second;
3975-
}
3976-
39773916
/// Return true if the specified type or a super-class/super-protocol has the
39783917
/// @dynamicMemberLookup attribute on it. This implementation is not
39793918
/// particularly fast in the face of deep class hierarchies or lots of protocol
@@ -4194,10 +4133,8 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
41944133
// the argument labels into the name: we don't want to look for
41954134
// anything else, because the cost of the general search is so
41964135
// high.
4197-
if (auto argumentLabels =
4198-
getArgumentLabels(*this, ConstraintLocatorBuilder(memberLocator))) {
4199-
memberName = DeclName(TC.Context, memberName.getBaseName(),
4200-
argumentLabels->Labels);
4136+
if (auto info = getArgumentInfo(memberLocator)) {
4137+
memberName = DeclName(TC.Context, memberName.getBaseName(), info->Labels);
42014138
}
42024139
}
42034140

@@ -4473,8 +4410,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclName memberName,
44734410
if (::hasDynamicMemberLookupAttribute(instanceTy,
44744411
DynamicMemberLookupCache) &&
44754412
isValidKeyPathDynamicMemberLookup(subscript, TC)) {
4476-
auto info =
4477-
getArgumentLabels(*this, ConstraintLocatorBuilder(memberLocator));
4413+
auto info = getArgumentInfo(memberLocator);
44784414

44794415
if (!(info && info->Labels.size() == 1 &&
44804416
info->Labels[0] == getASTContext().Id_dynamicMember)) {
@@ -5906,7 +5842,7 @@ Type ConstraintSystem::simplifyAppliedOverloads(
59065842
return markFailure();
59075843
};
59085844

5909-
auto argumentInfo = getArgumentLabels(*this, locator);
5845+
auto argumentInfo = getArgumentInfo(getConstraintLocator(locator));
59105846

59115847
// Consider each of the constraints in the disjunction.
59125848
retry_after_fail:

lib/Sema/ConstraintSystem.cpp

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -435,23 +435,20 @@ ConstraintLocator *ConstraintSystem::getCalleeLocator(Expr *expr) {
435435
expr = fnExpr->getSemanticsProvidingExpr();
436436
}
437437

438-
auto *locator = getConstraintLocator(expr);
439-
if (auto *ude = dyn_cast<UnresolvedDotExpr>(expr)) {
440-
if (TC.getSelfForInitDelegationInConstructor(DC, ude)) {
441-
return getConstraintLocator(locator,
442-
ConstraintLocator::ConstructorMember);
443-
} else {
444-
return getConstraintLocator(locator, ConstraintLocator::Member);
445-
}
438+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(expr)) {
439+
return getConstraintLocator(
440+
expr, TC.getSelfForInitDelegationInConstructor(DC, UDE)
441+
? ConstraintLocator::ConstructorMember
442+
: ConstraintLocator::Member);
446443
}
447444

448445
if (isa<UnresolvedMemberExpr>(expr))
449-
return getConstraintLocator(locator, ConstraintLocator::UnresolvedMember);
446+
return getConstraintLocator(expr, ConstraintLocator::UnresolvedMember);
450447

451448
if (isa<MemberRefExpr>(expr))
452-
return getConstraintLocator(locator, ConstraintLocator::Member);
449+
return getConstraintLocator(expr, ConstraintLocator::Member);
453450

454-
return locator;
451+
return getConstraintLocator(expr);
455452
}
456453

457454
Type ConstraintSystem::openUnboundGenericType(UnboundGenericType *unbound,
@@ -2783,6 +2780,28 @@ void ConstraintSystem::generateConstraints(
27832780
}
27842781
}
27852782

2783+
ConstraintLocator *ConstraintSystem::getArgumentInfoLocator(Expr *anchor) {
2784+
if (!anchor)
2785+
return nullptr;
2786+
2787+
if (auto *apply = dyn_cast<ApplyExpr>(anchor)) {
2788+
auto *fnExpr = getArgumentLabelTargetExpr(apply->getFn());
2789+
return getConstraintLocator(fnExpr);
2790+
}
2791+
2792+
return getCalleeLocator(anchor);
2793+
}
2794+
2795+
Optional<ConstraintSystem::ArgumentInfo>
2796+
ConstraintSystem::getArgumentInfo(ConstraintLocator *locator) {
2797+
if (auto *infoLocator = getArgumentInfoLocator(locator->getAnchor())) {
2798+
auto known = ArgumentInfos.find(infoLocator);
2799+
if (known != ArgumentInfos.end())
2800+
return known->second;
2801+
}
2802+
return None;
2803+
}
2804+
27862805
bool constraints::isKnownKeyPathType(Type type) {
27872806
if (auto *BGT = type->getAs<BoundGenericType>())
27882807
return isKnownKeyPathDecl(type->getASTContext(), BGT->getDecl());

lib/Sema/ConstraintSystem.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,7 @@ class ConstraintSystem {
15481548
/// we're exploring.
15491549
SolverState *solverState = nullptr;
15501550

1551-
struct ArgumentLabelState {
1551+
struct ArgumentInfo {
15521552
ArrayRef<Identifier> Labels;
15531553
bool HasTrailingClosure;
15541554
};
@@ -1557,7 +1557,15 @@ class ConstraintSystem {
15571557
/// names (e.g., member references, normal name references, possible
15581558
/// constructions) to the argument labels provided in the call to
15591559
/// that locator.
1560-
llvm::DenseMap<ConstraintLocator *, ArgumentLabelState> ArgumentLabels;
1560+
llvm::DenseMap<ConstraintLocator *, ArgumentInfo> ArgumentInfos;
1561+
1562+
/// Form a locator with given anchor which then could be used
1563+
/// to retrieve argument information cached in the constraint system.
1564+
ConstraintLocator *getArgumentInfoLocator(Expr *anchor);
1565+
1566+
/// Retrieve the argument info that is associated with a member
1567+
/// reference at the given locator.
1568+
Optional<ArgumentInfo> getArgumentInfo(ConstraintLocator *locator);
15611569

15621570
ResolvedOverloadSetListItem *getResolvedOverloadSets() const {
15631571
return resolvedOverloadSets;

0 commit comments

Comments
 (0)