Skip to content

Commit 2e92a5a

Browse files
authored
Merge pull request swiftlang#18988 from rjmccall/param-get-param-type
[NFC] Add a convenience getter for a Param's parameter type
2 parents 4ee219a + 768450d commit 2e92a5a

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

include/swift/AST/Types.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,14 @@ class YieldTypeFlags {
17991799
return YieldTypeFlags(isOwned ? value | Owned : value - Owned);
18001800
}
18011801

1802+
/// Return these flags interpreted as parameter flags.
1803+
ParameterTypeFlags asParamFlags() const {
1804+
return ParameterTypeFlags(/*variadic*/ false,
1805+
/*autoclosure*/ false,
1806+
/*escaping*/ false,
1807+
getValueOwnership());
1808+
}
1809+
18021810
bool operator ==(const YieldTypeFlags &other) const {
18031811
return value.toRaw() == other.value.toRaw();
18041812
}
@@ -2653,6 +2661,10 @@ class AnyFunctionType : public TypeBase {
26532661

26542662
Type getPlainType() const { return Ty; }
26552663

2664+
/// The type of the parameter. Adjusts for varargs, but not inout.
2665+
Type getParameterType(bool forCanonical = false,
2666+
ASTContext *ctx = nullptr) const;
2667+
26562668
bool hasLabel() const { return !Label.empty(); }
26572669
Identifier getLabel() const { return Label; }
26582670

@@ -2696,6 +2708,9 @@ class AnyFunctionType : public TypeBase {
26962708

26972709
CanType getType() const { return CanType(Param::getType()); }
26982710
CanType getPlainType() const { return CanType(Param::getPlainType()); }
2711+
CanType getParameterType() const {
2712+
return CanType(Param::getParameterType(/*forCanonical*/ true));
2713+
}
26992714
};
27002715

27012716
using CanParamArrayRef =
@@ -2719,6 +2734,13 @@ class AnyFunctionType : public TypeBase {
27192734

27202735
CanYield getCanonical() const;
27212736

2737+
/// There are a number of places where it's convenient to re-use
2738+
/// the call machinery, processing yields as if they were
2739+
/// parameters of a call. Return this reinterpreted as a parameter.
2740+
Param asParam() const {
2741+
return Param(getType(), Identifier(), getFlags().asParamFlags());
2742+
}
2743+
27222744
Yield subst(SubstitutionMap subs, SubstOptions options = None) const {
27232745
return Yield(getType().subst(subs, options), getFlags());
27242746
}
@@ -2738,6 +2760,7 @@ class AnyFunctionType : public TypeBase {
27382760
: Yield(type, flags) {}
27392761

27402762
CanType getType() const { return CanType(Yield::getType()); }
2763+
CanParam asParam() const { return CanParam::getFromParam(Yield::asParam());}
27412764

27422765
CanYield subst(SubstitutionMap subs, SubstOptions options = None) const {
27432766
return CanYield(getType().subst(subs, options)->getCanonicalType(),

lib/AST/ASTContext.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3563,19 +3563,27 @@ void AnyFunctionType::decomposeInput(
35633563
}
35643564
}
35653565

3566+
Type AnyFunctionType::Param::getParameterType(bool forCanonical,
3567+
ASTContext *ctx) const {
3568+
Type type = getPlainType();
3569+
if (isVariadic()) {
3570+
if (!ctx) ctx = &type->getASTContext();
3571+
auto arrayDecl = ctx->getArrayDecl();
3572+
if (!arrayDecl)
3573+
type = ErrorType::get(*ctx);
3574+
else if (forCanonical)
3575+
type = BoundGenericType::get(arrayDecl, Type(), {type});
3576+
else
3577+
type = ArraySliceType::get(type);
3578+
}
3579+
return type;
3580+
}
3581+
35663582
Type AnyFunctionType::composeInput(ASTContext &ctx, ArrayRef<Param> params,
35673583
bool canonicalVararg) {
35683584
SmallVector<TupleTypeElt, 4> elements;
35693585
for (const auto &param : params) {
3570-
Type eltType = param.getPlainType();
3571-
if (param.isVariadic()) {
3572-
if (!ctx.getArrayDecl())
3573-
eltType = ErrorType::get(ctx);
3574-
else if (canonicalVararg)
3575-
eltType = BoundGenericType::get(ctx.getArrayDecl(), Type(), {eltType});
3576-
else
3577-
eltType = ArraySliceType::get(eltType);
3578-
}
3586+
Type eltType = param.getParameterType(canonicalVararg, &ctx);
35793587
elements.emplace_back(eltType, param.getLabel(),
35803588
param.getParameterFlags());
35813589
}

lib/Sema/TypeCheckPattern.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,12 +1656,7 @@ bool TypeChecker::coerceParameterListToType(ParameterList *P, ClosureExpr *CE,
16561656
};
16571657

16581658
auto getType = [](const AnyFunctionType::Param &param) -> Type {
1659-
auto type = param.getPlainType();
1660-
1661-
if (param.isVariadic())
1662-
return ArraySliceType::get(type);
1663-
1664-
return type;
1659+
return param.getParameterType();
16651660
};
16661661

16671662
// If the closure is called with a single argument of tuple type

0 commit comments

Comments
 (0)