Skip to content

Commit 623a6ad

Browse files
committed
[AST] Add ValueDecl::getNumCurryLevels
1 parent 71eb43a commit 623a6ad

File tree

6 files changed

+23
-42
lines changed

6 files changed

+23
-42
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,6 +2704,9 @@ class ValueDecl : public Decl {
27042704
/// parameter lists, for example an enum element without associated values.
27052705
bool hasParameterList() const;
27062706

2707+
/// Returns the number of curry levels in the declaration's interface type.
2708+
unsigned getNumCurryLevels() const;
2709+
27072710
/// Get the decl for this value's opaque result type, if it has one.
27082711
OpaqueTypeDecl *getOpaqueResultTypeDecl() const;
27092712

@@ -7164,6 +7167,15 @@ inline bool ValueDecl::hasParameterList() const {
71647167
return isa<AbstractFunctionDecl>(this) || isa<SubscriptDecl>(this);
71657168
}
71667169

7170+
inline unsigned ValueDecl::getNumCurryLevels() const {
7171+
unsigned curryLevels = 0;
7172+
if (hasParameterList())
7173+
curryLevels++;
7174+
if (hasCurriedSelf())
7175+
curryLevels++;
7176+
return curryLevels;
7177+
}
7178+
71677179
inline bool Decl::isPotentiallyOverridable() const {
71687180
if (isa<VarDecl>(this) ||
71697181
isa<SubscriptDecl>(this) ||

lib/AST/Decl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,7 +2401,7 @@ CanType ValueDecl::getOverloadSignatureType() const {
24012401
/*topLevelFunction=*/true,
24022402
isMethod,
24032403
/*isInitializer=*/isa<ConstructorDecl>(afd),
2404-
isMethod ? 2 : 1)->getCanonicalType();
2404+
getNumCurryLevels())->getCanonicalType();
24052405
}
24062406

24072407
if (isa<AbstractStorageDecl>(this)) {
@@ -2417,7 +2417,7 @@ CanType ValueDecl::getOverloadSignatureType() const {
24172417
/*topLevelFunction=*/true,
24182418
/*isMethod=*/false,
24192419
/*isInitializer=*/false,
2420-
1)->getCanonicalType();
2420+
getNumCurryLevels())->getCanonicalType();
24212421
}
24222422

24232423
// We want to curry the default signature type with the 'self' type of the
@@ -2431,7 +2431,7 @@ CanType ValueDecl::getOverloadSignatureType() const {
24312431
if (isa<EnumElementDecl>(this)) {
24322432
auto mappedType = mapSignatureFunctionType(
24332433
getASTContext(), getInterfaceType(), /*topLevelFunction=*/false,
2434-
/*isMethod=*/false, /*isInitializer=*/false, /*curryLevels=*/0);
2434+
/*isMethod=*/false, /*isInitializer=*/false, getNumCurryLevels());
24352435
return mappedType->getCanonicalType();
24362436
}
24372437

lib/ParseSIL/ParseSIL.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,18 +2028,9 @@ bool SILParser::parseSILDeclRef(SILDeclRef &Member, bool FnTypeRequired) {
20282028
for (unsigned I = 0, E = values.size(); I < E; I++) {
20292029
auto *decl = values[I];
20302030

2031-
unsigned numArgumentLabels = 0;
2032-
if (auto *eed = dyn_cast<EnumElementDecl>(decl)) {
2033-
numArgumentLabels =
2034-
(eed->hasAssociatedValues() ? 2 : 1);
2035-
} else if (auto *afd = dyn_cast<AbstractFunctionDecl>(decl)) {
2036-
numArgumentLabels =
2037-
(decl->getDeclContext()->isTypeContext() ? 2 : 1);
2038-
}
2039-
20402031
auto lookupTy =
20412032
decl->getInterfaceType()
2042-
->removeArgumentLabels(numArgumentLabels);
2033+
->removeArgumentLabels(decl->getNumCurryLevels());
20432034
if (declTy == lookupTy->getCanonicalType()) {
20442035
TheDecl = decl;
20452036
// Update SILDeclRef to point to the right Decl.

lib/SIL/SILDeclRef.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,10 @@ unsigned SILDeclRef::getParameterListCount() const {
10161016

10171017
auto *vd = getDecl();
10181018

1019-
if (auto *func = dyn_cast<AbstractFunctionDecl>(vd)) {
1020-
return func->hasImplicitSelfDecl() ? 2 : 1;
1021-
} else if (auto *ed = dyn_cast<EnumElementDecl>(vd)) {
1022-
return ed->hasAssociatedValues() ? 2 : 1;
1019+
if (isa<AbstractFunctionDecl>(vd) || isa<EnumElementDecl>(vd)) {
1020+
// For functions and enum elements, the number of parameter lists is the
1021+
// same as in their interface type.
1022+
return vd->getNumCurryLevels();
10231023
} else if (isa<ClassDecl>(vd)) {
10241024
return 2;
10251025
} else if (isa<VarDecl>(vd)) {

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -877,29 +877,7 @@ void ConstraintSystem::recordOpenedTypes(
877877
static unsigned getNumRemovedArgumentLabels(TypeChecker &TC, ValueDecl *decl,
878878
bool isCurriedInstanceReference,
879879
FunctionRefKind functionRefKind) {
880-
unsigned numParameterLists;
881-
882-
// Enum elements with associated values have to be treated
883-
// as regular function values.
884-
//
885-
// enum E {
886-
// case foo(a: Int)
887-
// }
888-
// let bar: [Int] = []
889-
// bar.map(E.foo)
890-
//
891-
// `E.foo` has to act as a regular function type passed as a value.
892-
if (auto *EED = dyn_cast<EnumElementDecl>(decl)) {
893-
numParameterLists = EED->hasAssociatedValues() ? 2 : 1;
894-
895-
// Only applicable to functions. Nothing else should have argument labels in
896-
// the type.
897-
} else if (auto func = dyn_cast<AbstractFunctionDecl>(decl)) {
898-
numParameterLists = func->hasImplicitSelfDecl() ? 2 : 1;
899-
} else {
900-
return 0;
901-
}
902-
880+
unsigned numParameterLists = decl->getNumCurryLevels();
903881
switch (functionRefKind) {
904882
case FunctionRefKind::Unapplied:
905883
case FunctionRefKind::Compound:

lib/Sema/TypeCheckError.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class AbstractFunction {
5555
explicit AbstractFunction(AbstractFunctionDecl *fn)
5656
: TheKind(Kind::Function),
5757
IsRethrows(fn->getAttrs().hasAttribute<RethrowsAttr>()),
58-
ParamCount(fn->hasImplicitSelfDecl() ? 2 : 1) {
58+
ParamCount(fn->getNumCurryLevels()) {
5959
TheFunction = fn;
6060
}
6161

@@ -896,7 +896,7 @@ class Context {
896896
}
897897

898898
return Context(getKindForFunctionBody(
899-
D->getInterfaceType(), D->hasImplicitSelfDecl() ? 2 : 1));
899+
D->getInterfaceType(), D->getNumCurryLevels()));
900900
}
901901

902902
static Context forDeferBody() {

0 commit comments

Comments
 (0)