Skip to content

Commit 71e72d8

Browse files
authored
Merge pull request #16062 from davezarzycki/nfc_simplify_TypeMatcher
[AST] NFC: De-boilerplate the TypeMatcher
2 parents 36b2c93 + 8a46a02 commit 71e72d8

File tree

1 file changed

+27
-92
lines changed

1 file changed

+27
-92
lines changed

include/swift/AST/TypeMatcher.h

Lines changed: 27 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -141,51 +141,33 @@ class TypeMatcher {
141141
return mismatch(firstTuple.getPointer(), secondType, sugaredFirstType);
142142
}
143143

144-
template<typename FirstReferenceStorageType>
145-
bool handleReferenceStorageType(
146-
CanTypeWrapper<FirstReferenceStorageType> firstStorage,
147-
Type secondType, Type sugaredFirstType) {
148-
if (auto secondStorage = secondType->getAs<FirstReferenceStorageType>()) {
149-
return this->visit(
150-
firstStorage.getReferentType(),
151-
secondStorage->getReferentType(),
152-
sugaredFirstType->getAs<FirstReferenceStorageType>()
153-
->getReferentType());
144+
bool visitReferenceStorageType(CanReferenceStorageType firstStorage,
145+
Type secondType, Type sugaredFirstType) {
146+
auto _secondStorage = secondType->getCanonicalType();
147+
if (firstStorage->getKind() == _secondStorage->getKind()) {
148+
auto secondStorage = cast<ReferenceStorageType>(_secondStorage);
149+
return this->visit(firstStorage.getReferentType(),
150+
secondStorage->getReferentType(),
151+
sugaredFirstType->castTo<ReferenceStorageType>()
152+
->getReferentType());
154153
}
155154

156155
return mismatch(firstStorage.getPointer(), secondType, sugaredFirstType);
157156
}
158157

159-
bool visitUnownedStorageType(CanUnownedStorageType firstStorage,
160-
Type secondType, Type sugaredFirstType) {
161-
return handleReferenceStorageType(firstStorage, secondType,
162-
sugaredFirstType);
163-
}
164-
165-
bool visitUnmanagedStorageType(CanUnmanagedStorageType firstStorage,
166-
Type secondType, Type sugaredFirstType) {
167-
return handleReferenceStorageType(firstStorage, secondType,
168-
sugaredFirstType);
169-
}
170-
171-
bool visitWeakStorageType(CanWeakStorageType firstStorage,
172-
Type secondType, Type sugaredFirstType) {
173-
return handleReferenceStorageType(firstStorage, secondType,
174-
sugaredFirstType);
175-
}
176-
177-
template<typename FirstNominalType>
178-
bool handleNominalType(CanTypeWrapper<FirstNominalType> firstNominal,
179-
Type secondType, Type sugaredFirstType) {
180-
if (auto secondNominal = secondType->getAs<FirstNominalType>()) {
158+
bool visitNominalType(CanNominalType firstNominal,
159+
Type secondType, Type sugaredFirstType) {
160+
auto _secondNominal = secondType->getCanonicalType();
161+
if (firstNominal->getKind() == _secondNominal->getKind()) {
162+
auto secondNominal = cast<NominalType>(_secondNominal);
181163
if (firstNominal->getDecl() != secondNominal->getDecl())
182164
return mismatch(firstNominal.getPointer(), secondNominal,
183165
sugaredFirstType);
184166

185167
if (firstNominal.getParent())
186168
return this->visit(firstNominal.getParent(),
187169
secondNominal->getParent(),
188-
sugaredFirstType->castTo<FirstNominalType>()
170+
sugaredFirstType->castTo<NominalType>()
189171
->getParent());
190172

191173
return true;
@@ -194,52 +176,20 @@ class TypeMatcher {
194176
return mismatch(firstNominal.getPointer(), secondType, sugaredFirstType);
195177
}
196178

197-
bool visitEnumType(CanEnumType firstEnum, Type secondType,
198-
Type sugaredFirstType) {
199-
return handleNominalType(firstEnum, secondType, sugaredFirstType);
200-
}
201-
202-
bool visitStructType(CanStructType firstStruct, Type secondType,
203-
Type sugaredFirstType) {
204-
return handleNominalType(firstStruct, secondType, sugaredFirstType);
205-
}
206-
207-
bool visitClassType(CanClassType firstClass, Type secondType,
208-
Type sugaredFirstType) {
209-
return handleNominalType(firstClass, secondType, sugaredFirstType);
210-
}
211-
212-
bool visitProtocolType(CanProtocolType firstProtocol, Type secondType,
213-
Type sugaredFirstType) {
214-
return handleNominalType(firstProtocol, secondType, sugaredFirstType);
215-
}
216-
217-
template<typename FirstMetatypeType>
218-
bool handleAnyMetatypeType(CanTypeWrapper<FirstMetatypeType> firstMeta,
219-
Type secondType, Type sugaredFirstType) {
220-
if (auto secondMeta = secondType->getAs<FirstMetatypeType>()) {
221-
if (firstMeta->getKind() != secondMeta->getKind())
222-
return mismatch(firstMeta.getPointer(), secondMeta, sugaredFirstType);
223-
179+
bool visitAnyMetatypeType(CanAnyMetatypeType firstMeta,
180+
Type secondType, Type sugaredFirstType) {
181+
auto _secondMeta = secondType->getCanonicalType();
182+
if (firstMeta->getKind() == _secondMeta->getKind()) {
183+
auto secondMeta = cast<AnyMetatypeType>(_secondMeta);
224184
return this->visit(firstMeta.getInstanceType(),
225185
secondMeta->getInstanceType(),
226-
sugaredFirstType->castTo<FirstMetatypeType>()
186+
sugaredFirstType->castTo<AnyMetatypeType>()
227187
->getInstanceType());
228188
}
229189

230190
return mismatch(firstMeta.getPointer(), secondType, sugaredFirstType);
231191
}
232192

233-
bool visitMetatypeType(CanMetatypeType firstMeta, Type secondType,
234-
Type sugaredFirstType) {
235-
return handleAnyMetatypeType(firstMeta, secondType, sugaredFirstType);
236-
}
237-
238-
bool visitExistentialMetatypeType(CanExistentialMetatypeType firstMeta,
239-
Type secondType, Type sugaredFirstType) {
240-
return handleAnyMetatypeType(firstMeta, secondType, sugaredFirstType);
241-
}
242-
243193
TRIVIAL_CASE(ModuleType)
244194
TRIVIAL_CASE(DynamicSelfType)
245195
TRIVIAL_CASE(ArchetypeType)
@@ -332,15 +282,15 @@ class TypeMatcher {
332282
return mismatch(firstUBGT.getPointer(), secondType, sugaredFirstType);
333283
}
334284

335-
template<typename FirstBoundGenericType>
336-
bool handleBoundGenericType(CanTypeWrapper<FirstBoundGenericType> firstBGT,
337-
Type secondType, Type sugaredFirstType) {
338-
if (auto secondBGT = secondType->getAs<FirstBoundGenericType>()) {
285+
bool visitBoundGenericType(CanBoundGenericType firstBGT,
286+
Type secondType, Type sugaredFirstType) {
287+
auto _secondBGT = secondType->getCanonicalType();
288+
if (firstBGT->getKind() == _secondBGT->getKind()) {
289+
auto secondBGT = cast<BoundGenericType>(_secondBGT);
339290
if (firstBGT->getDecl() != secondBGT->getDecl())
340291
return mismatch(firstBGT.getPointer(), secondBGT, sugaredFirstType);
341292

342-
auto sugaredFirstBGT
343-
= sugaredFirstType->castTo<FirstBoundGenericType>();
293+
auto sugaredFirstBGT = sugaredFirstType->castTo<BoundGenericType>();
344294
if (firstBGT->getParent() &&
345295
!this->visit(firstBGT.getParent(), secondBGT->getParent(),
346296
sugaredFirstBGT->getParent()))
@@ -360,21 +310,6 @@ class TypeMatcher {
360310
return mismatch(firstBGT.getPointer(), secondType, sugaredFirstType);
361311
}
362312

363-
bool visitBoundGenericClassType(CanBoundGenericClassType firstBGT,
364-
Type secondType, Type sugaredFirstType) {
365-
return handleBoundGenericType(firstBGT, secondType, sugaredFirstType);
366-
}
367-
368-
bool visitBoundGenericEnumType(CanBoundGenericEnumType firstBGT,
369-
Type secondType, Type sugaredFirstType) {
370-
return handleBoundGenericType(firstBGT, secondType, sugaredFirstType);
371-
}
372-
373-
bool visitBoundGenericStructType(CanBoundGenericStructType firstBGT,
374-
Type secondType, Type sugaredFirstType) {
375-
return handleBoundGenericType(firstBGT, secondType, sugaredFirstType);
376-
}
377-
378313
TRIVIAL_CASE(TypeVariableType)
379314

380315
#undef TRIVIAL_CASE

0 commit comments

Comments
 (0)