Skip to content

Commit c969f5f

Browse files
committed
Add a Utility to Strip Parens from TypeReprs
1 parent e6a1e23 commit c969f5f

File tree

4 files changed

+20
-14
lines changed

4 files changed

+20
-14
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
172172
/// `OpaqueReturnTypeRepr`s.
173173
CollectedOpaqueReprs collectOpaqueReturnTypeReprs();
174174

175+
/// Retrieve the type repr without any parentheses around it.
176+
///
177+
/// The use of this function must be restricted to contexts where
178+
/// user-written types are provided, and a syntactic analysis is appropriate.
179+
/// Most use cases should analyze the resolved \c Type instead and use
180+
/// \c Type::getCanonicalType() or \c Type::getWithoutParens().
181+
TypeRepr *getWithoutParens() const;
182+
175183
//*** Allocation Routines ************************************************/
176184

177185
void print(raw_ostream &OS, const PrintOptions &Opts = PrintOptions()) const;

lib/AST/TypeRepr.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ bool TypeRepr::hasOpaque() {
102102
findIf([](TypeRepr *ty) { return isa<OpaqueReturnTypeRepr>(ty); });
103103
}
104104

105+
TypeRepr *TypeRepr::getWithoutParens() const {
106+
auto *repr = const_cast<TypeRepr *>(this);
107+
while (auto *tupleRepr = dyn_cast<TupleTypeRepr>(repr)) {
108+
if (!tupleRepr->isParenType())
109+
break;
110+
repr = tupleRepr->getElementType(0);
111+
}
112+
return repr;
113+
}
114+
105115
CollectedOpaqueReprs TypeRepr::collectOpaqueReturnTypeReprs() {
106116
class Walker : public ASTWalker {
107117
CollectedOpaqueReprs &Reprs;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,15 +2061,9 @@ ParamSpecifierRequest::evaluate(Evaluator &evaluator,
20612061
assert(typeRepr != nullptr && "Should call setSpecifier() on "
20622062
"synthesized parameter declarations");
20632063

2064-
auto *nestedRepr = typeRepr;
2065-
20662064
// Look through parens here; other than parens, specifiers
20672065
// must appear at the top level of a parameter type.
2068-
while (auto *tupleRepr = dyn_cast<TupleTypeRepr>(nestedRepr)) {
2069-
if (!tupleRepr->isParenType())
2070-
break;
2071-
nestedRepr = tupleRepr->getElementType(0);
2072-
}
2066+
auto *nestedRepr = typeRepr->getWithoutParens();
20732067

20742068
if (auto isolated = dyn_cast<IsolatedTypeRepr>(nestedRepr))
20752069
nestedRepr = isolated->getBase();

lib/Sema/TypeCheckType.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2834,15 +2834,9 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
28342834

28352835
ValueOwnership ownership = ValueOwnership::Default;
28362836

2837-
auto *nestedRepr = eltTypeRepr;
2838-
28392837
// Look through parens here; other than parens, specifiers
28402838
// must appear at the top level of a parameter type.
2841-
while (auto *tupleRepr = dyn_cast<TupleTypeRepr>(nestedRepr)) {
2842-
if (!tupleRepr->isParenType())
2843-
break;
2844-
nestedRepr = tupleRepr->getElementType(0);
2845-
}
2839+
auto *nestedRepr = eltTypeRepr->getWithoutParens();
28462840

28472841
bool isolated = false;
28482842
bool compileTimeConst = false;

0 commit comments

Comments
 (0)