Skip to content

Commit 5a2316c

Browse files
committed
[NFC] AST: Define TypeRepr::isParenType()
1 parent 8493312 commit 5a2316c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

include/swift/AST/TypeRepr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ class alignas(1 << TypeReprAlignInBits) TypeRepr
184184
/// opaque return type reprs.
185185
bool hasOpaque();
186186

187+
/// Returns a Boolean value indicating whether this written type is
188+
/// parenthesized, that is, matches the following grammar: `'(' type ')'`.
189+
bool isParenType() const;
190+
187191
/// Retrieve the type repr without any parentheses around it.
188192
///
189193
/// The use of this function must be restricted to contexts where

lib/AST/TypeRepr.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,18 @@ bool TypeRepr::hasOpaque() {
109109
findIf([](TypeRepr *ty) { return isa<OpaqueReturnTypeRepr>(ty); });
110110
}
111111

112+
bool TypeRepr::isParenType() const {
113+
auto *tuple = dyn_cast<TupleTypeRepr>(this);
114+
return tuple && tuple->isParenType();
115+
}
116+
112117
TypeRepr *TypeRepr::getWithoutParens() const {
113-
auto *repr = const_cast<TypeRepr *>(this);
114-
while (auto *tupleRepr = dyn_cast<TupleTypeRepr>(repr)) {
115-
if (!tupleRepr->isParenType())
116-
break;
117-
repr = tupleRepr->getElementType(0);
118+
auto *result = this;
119+
while (result->isParenType()) {
120+
result = cast<TupleTypeRepr>(result)->getElementType(0);
118121
}
119-
return repr;
122+
123+
return const_cast<TypeRepr *>(result);
120124
}
121125

122126
bool TypeRepr::isSimpleUnqualifiedIdentifier(Identifier identifier) const {

0 commit comments

Comments
 (0)