Skip to content

Commit 3301ae9

Browse files
[NFC] Simplify function type representation checks.
1 parent 66dfea1 commit 3301ae9

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,45 @@ enum class SILFunctionTypeRepresentation : uint8_t {
169169
Closure,
170170
};
171171

172+
/// Returns true if the function with this convention doesn't carry a context.
173+
constexpr bool
174+
isThinRepresentation(FunctionTypeRepresentation rep) {
175+
switch (rep) {
176+
case FunctionTypeRepresentation::Swift:
177+
case FunctionTypeRepresentation::Block:
178+
return false;
179+
case FunctionTypeRepresentation::Thin:
180+
case FunctionTypeRepresentation::CFunctionPointer:
181+
return true;
182+
}
183+
llvm_unreachable("Unhandled FunctionTypeRepresentation in switch.");
184+
}
185+
186+
/// Returns true if the function with this convention doesn't carry a context.
187+
constexpr bool
188+
isThinRepresentation(SILFunctionTypeRepresentation rep) {
189+
switch (rep) {
190+
case SILFunctionTypeRepresentation::Thick:
191+
case SILFunctionTypeRepresentation::Block:
192+
return false;
193+
case SILFunctionTypeRepresentation::Thin:
194+
case SILFunctionTypeRepresentation::Method:
195+
case SILFunctionTypeRepresentation::ObjCMethod:
196+
case SILFunctionTypeRepresentation::WitnessMethod:
197+
case SILFunctionTypeRepresentation::CFunctionPointer:
198+
case SILFunctionTypeRepresentation::Closure:
199+
return true;
200+
}
201+
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
202+
}
203+
204+
/// Returns true if the function with this convention carries a context.
205+
template <typename Repr>
206+
constexpr bool
207+
isThickRepresentation(Repr repr) {
208+
return !isThinRepresentation(repr);
209+
}
210+
172211
constexpr SILFunctionTypeRepresentation
173212
convertRepresentation(FunctionTypeRepresentation rep) {
174213
switch (rep) {
@@ -350,19 +389,7 @@ class ASTExtInfoBuilder {
350389

351390
/// True if the function representation carries context.
352391
constexpr bool hasContext() const {
353-
switch (getSILRepresentation()) {
354-
case SILFunctionTypeRepresentation::Thick:
355-
case SILFunctionTypeRepresentation::Block:
356-
return true;
357-
case SILFunctionTypeRepresentation::Thin:
358-
case SILFunctionTypeRepresentation::Method:
359-
case SILFunctionTypeRepresentation::ObjCMethod:
360-
case SILFunctionTypeRepresentation::WitnessMethod:
361-
case SILFunctionTypeRepresentation::CFunctionPointer:
362-
case SILFunctionTypeRepresentation::Closure:
363-
return false;
364-
}
365-
llvm_unreachable("Unhandled SILFunctionTypeRepresentation in switch.");
392+
return isThickRepresentation(getSILRepresentation());
366393
}
367394

368395
// Note that we don't have setters. That is by design, use

lib/Sema/CSSimplify.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,22 +1552,9 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
15521552
static bool
15531553
isSubtypeOf(FunctionTypeRepresentation potentialSubRepr,
15541554
FunctionTypeRepresentation potentialSuperRepr) {
1555-
auto isThin = [](FunctionTypeRepresentation rep) {
1556-
return rep == FunctionTypeRepresentation::CFunctionPointer ||
1557-
rep == FunctionTypeRepresentation::Thin;
1558-
};
1559-
1560-
// Allowing "thin" (c, thin) to "thin" conversions
1561-
if (isThin(potentialSubRepr) && isThin(potentialSuperRepr))
1562-
return true;
1563-
1564-
// Allowing all to "thick" (swift, block) conversions
1565-
// "thin" (c, thin) to "thick" or "thick" to "thick"
1566-
if (potentialSuperRepr == FunctionTypeRepresentation::Swift ||
1567-
potentialSuperRepr == FunctionTypeRepresentation::Block)
1568-
return true;
1569-
1570-
return potentialSubRepr == potentialSuperRepr;
1555+
return (potentialSubRepr == potentialSuperRepr)
1556+
|| isThinRepresentation(potentialSubRepr)
1557+
|| isThickRepresentation(potentialSuperRepr);
15711558
}
15721559

15731560
/// Returns true if `constraint rep1 rep2` is satisfied.

0 commit comments

Comments
 (0)