Skip to content

Commit 78f0259

Browse files
Merge pull request swiftlang#33283 from AnthonyLatsis/type-locura-2
ASTWalker: Offload TypeLoc
2 parents 9f8ba0a + 17f12da commit 78f0259

16 files changed

+34
-83
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ class ModuleDecl;
2525
class Stmt;
2626
class Pattern;
2727
class TypeRepr;
28-
class TypeLoc;
2928
class ParameterList;
3029
enum class AccessKind: unsigned char;
3130

@@ -177,19 +176,6 @@ class ASTWalker {
177176
/// returns failure.
178177
virtual bool walkToDeclPost(Decl *D) { return true; }
179178

180-
/// This method is called when first visiting a TypeLoc, before
181-
/// walking into its TypeRepr children. If it returns false, the subtree is
182-
/// skipped.
183-
///
184-
/// \param TL The TypeLoc to check.
185-
virtual bool walkToTypeLocPre(TypeLoc &TL) { return true; }
186-
187-
/// This method is called after visiting the children of a TypeLoc.
188-
/// If it returns false, the remaining traversal is terminated and returns
189-
/// failure.
190-
virtual bool walkToTypeLocPost(TypeLoc &TL) { return true; }
191-
192-
193179
/// This method is called when first visiting a TypeRepr, before
194180
/// walking into its children. If it returns false, the subtree is skipped.
195181
///

include/swift/AST/Pattern.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace swift {
3434
class Expr;
3535
enum class CheckedCastKind : unsigned;
3636
class TypeExpr;
37-
class TypeLoc;
3837

3938
/// PatternKind - The classification of different kinds of
4039
/// value-matching pattern.
@@ -447,7 +446,6 @@ class TypedPattern : public Pattern {
447446

448447
TypeRepr *getTypeRepr() const { return PatTypeRepr; }
449448

450-
TypeLoc getTypeLoc() const;
451449
SourceLoc getLoc() const;
452450
SourceRange getSourceRange() const;
453451

include/swift/IDE/Utils.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ class NameMatcher: public ASTWalker {
280280
bool walkToDeclPost(Decl *D) override;
281281
std::pair<bool, Stmt*> walkToStmtPre(Stmt *S) override;
282282
Stmt* walkToStmtPost(Stmt *S) override;
283-
bool walkToTypeLocPre(TypeLoc &TL) override;
284-
bool walkToTypeLocPost(TypeLoc &TL) override;
285283
bool walkToTypeReprPre(TypeRepr *T) override;
286284
bool walkToTypeReprPost(TypeRepr *T) override;
287285
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override;

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1079,7 +1079,9 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
10791079
if (auto decl = named->getDecl())
10801080
isIUO = decl->isImplicitlyUnwrappedOptional();
10811081

1082-
printTypeLocForImplicitlyUnwrappedOptional(TP->getTypeLoc(), isIUO);
1082+
const auto TyLoc = TypeLoc(TP->getTypeRepr(),
1083+
TP->hasType() ? TP->getType() : Type());
1084+
printTypeLocForImplicitlyUnwrappedOptional(TyLoc, isIUO);
10831085
}
10841086

10851087
/// Determines if we are required to print the name of a property declaration,

lib/AST/ASTScopeCreation.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,6 @@ void ScopeCreator::forEachClosureIn(
17831783
return {false, P};
17841784
}
17851785
bool walkToDeclPre(Decl *D) override { return false; }
1786-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
17871786
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
17881787
bool walkToParameterListPre(ParameterList *PL) override { return false; }
17891788
};

lib/AST/ASTWalker.cpp

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
155155
if (doIt(typeRepr))
156156
return true;
157157
for (auto &Inherit : ED->getInherited()) {
158-
if (doIt(Inherit))
159-
return true;
158+
if (auto *const TyR = Inherit.getTypeRepr())
159+
if (doIt(TyR))
160+
return true;
160161
}
161162
if (visitTrailingRequirements(ED))
162163
return true;
@@ -258,9 +259,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
258259
}
259260

260261
bool visitAbstractTypeParamDecl(AbstractTypeParamDecl *TPD) {
261-
for (auto Inherit: TPD->getInherited()) {
262-
if (doIt(Inherit))
263-
return true;
262+
for (const auto &Inherit: TPD->getInherited()) {
263+
if (auto *const TyR = Inherit.getTypeRepr())
264+
if (doIt(TyR))
265+
return true;
264266
}
265267

266268
if (const auto ATD = dyn_cast<AssociatedTypeDecl>(TPD)) {
@@ -282,9 +284,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
282284

283285
bool WalkGenerics = visitGenericParamListIfNeeded(NTD);
284286

285-
for (auto &Inherit : NTD->getInherited()) {
286-
if (doIt(Inherit))
287-
return true;
287+
for (const auto &Inherit : NTD->getInherited()) {
288+
if (auto *const TyR = Inherit.getTypeRepr())
289+
if (doIt(Inherit.getTypeRepr()))
290+
return true;
288291
}
289292

290293
// Visit requirements
@@ -355,8 +358,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
355358
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
356359

357360
visit(SD->getIndices());
358-
if (doIt(SD->getElementTypeLoc()))
359-
return true;
361+
if (auto *const TyR = SD->getElementTypeLoc().getTypeRepr())
362+
if (doIt(TyR))
363+
return true;
360364

361365
// Visit trailing requirements
362366
if (WalkGenerics && visitTrailingRequirements(SD))
@@ -388,10 +392,12 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
388392
visit(PD);
389393
visit(AFD->getParameters());
390394

391-
if (auto *FD = dyn_cast<FuncDecl>(AFD))
395+
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
392396
if (!isa<AccessorDecl>(FD))
393-
if (doIt(FD->getBodyResultTypeLoc()))
394-
return true;
397+
if (auto *const TyR = FD->getBodyResultTypeLoc().getTypeRepr())
398+
if (doIt(TyR))
399+
return true;
400+
}
395401

396402
// Visit trailing requirements
397403
if (WalkGenerics && visitTrailingRequirements(AFD))
@@ -1320,21 +1326,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
13201326
return false;
13211327
}
13221328

1323-
bool doIt(TypeLoc &TL) {
1324-
if (!Walker.walkToTypeLocPre(TL))
1325-
return false;
1326-
1327-
// No "visit" since TypeLocs are not a class hierarchy. Clients can do what
1328-
// they want in walkToTypeLocPre.
1329-
1330-
if (auto typerepr = TL.getTypeRepr())
1331-
if (doIt(typerepr))
1332-
return true;
1333-
1334-
// If we didn't bail out, do post-order visitation.
1335-
return !Walker.walkToTypeLocPost(TL);
1336-
}
1337-
13381329
/// Returns true on failure.
13391330
bool doIt(TypeRepr *T) {
13401331
// Do the pre-order visitation. If it returns false, we just

lib/AST/Expr.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ forEachImmediateChildExpr(llvm::function_ref<Expr *(Expr *)> callback) {
423423
}
424424
bool walkToDeclPre(Decl *D) override { return false; }
425425
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
426-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
427426
};
428427

429428
this->walk(ChildWalker(callback, this));
@@ -453,7 +452,6 @@ void Expr::forEachChildExpr(llvm::function_ref<Expr *(Expr *)> callback) {
453452
}
454453
bool walkToDeclPre(Decl *D) override { return false; }
455454
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
456-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
457455
};
458456

459457
this->walk(ChildWalker(callback));

lib/AST/Pattern.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ namespace {
187187
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
188188
return { false, S };
189189
}
190-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
191190
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
192191
bool walkToParameterListPre(ParameterList *PL) override { return false; }
193192
bool walkToDeclPre(Decl *D) override { return false; }
@@ -407,15 +406,6 @@ TypedPattern::TypedPattern(Pattern *pattern, TypeRepr *tr)
407406
Bits.TypedPattern.IsPropagatedType = false;
408407
}
409408

410-
TypeLoc TypedPattern::getTypeLoc() const {
411-
TypeLoc loc = TypeLoc(PatTypeRepr);
412-
413-
if (hasType())
414-
loc.setType(getType());
415-
416-
return loc;
417-
}
418-
419409
SourceLoc TypedPattern::getLoc() const {
420410
if (SubPattern->isImplicit() && PatTypeRepr)
421411
return PatTypeRepr->getSourceRange().Start;

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ class ExprFinder : public ASTWalker {
177177
return {isInterstingRange(S), S};
178178
}
179179

180-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
181180
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
182181
};
183182
} // anonymous namespace

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,6 @@ Expr *NameMatcher::walkToExprPost(Expr *E) {
468468
return E;
469469
}
470470

471-
bool NameMatcher::walkToTypeLocPre(TypeLoc &TL) {
472-
if (isDone() || shouldSkip(TL.getSourceRange()))
473-
return false;
474-
return true;
475-
}
476-
477-
bool NameMatcher::walkToTypeLocPost(TypeLoc &TL) {
478-
return !isDone();
479-
}
480-
481471
bool NameMatcher::walkToTypeReprPre(TypeRepr *T) {
482472
if (isDone() || shouldSkip(T->getSourceRange()))
483473
return false;

lib/Parse/ParseStmt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2467,7 +2467,6 @@ struct FallthroughFinder : ASTWalker {
24672467
}
24682468

24692469
bool walkToDeclPre(Decl *d) override { return false; }
2470-
bool walkToTypeLocPre(TypeLoc &tl) override { return false; }
24712470
bool walkToTypeReprPre(TypeRepr *t) override { return false; }
24722471

24732472
static FallthroughStmt *findFallthrough(Stmt *s) {

lib/Sema/CSGen.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace {
158158
}
159159

160160
/// Ignore types.
161-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
161+
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
162162
};
163163

164164
/// Given a collection of "linked" expressions, analyzes them for
@@ -305,7 +305,7 @@ namespace {
305305
}
306306

307307
/// Ignore types.
308-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
308+
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
309309
};
310310

311311
/// For a given expression, given information that is global to the

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4648,8 +4648,10 @@ SolutionApplicationTarget SolutionApplicationTarget::forInitialization(
46484648
if (auto *typedPattern = dyn_cast<TypedPattern>(pattern)) {
46494649
const Pattern *inner = typedPattern->getSemanticsProvidingPattern();
46504650
if (isa<NamedPattern>(inner) || isa<AnyPattern>(inner)) {
4651-
contextualType = typedPattern->getTypeLoc();
4652-
if (!contextualType.getType())
4651+
contextualType = TypeLoc(typedPattern->getTypeRepr());
4652+
if (typedPattern->hasType())
4653+
contextualType.setType(typedPattern->getType());
4654+
else
46534655
contextualType.setType(patternType);
46544656
}
46554657
}

lib/Sema/TypeCheckAccess.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ class AccessControlChecker : public AccessControlCheckerBase,
497497
if (!anyVar)
498498
return;
499499

500-
checkTypeAccess(TP->getTypeLoc(), anyVar, /*mayBeInferred*/true,
500+
checkTypeAccess(TP->hasType() ? TP->getType() : Type(),
501+
TP->getTypeRepr(), anyVar, /*mayBeInferred*/true,
501502
[&](AccessScope typeAccessScope,
502503
const TypeRepr *complainRepr,
503504
DowngradeToWarning downgradeToWarning) {
@@ -1117,7 +1118,8 @@ class UsableFromInlineChecker : public AccessControlCheckerBase,
11171118
return;
11181119

11191120
checkTypeAccess(
1120-
TP->getTypeLoc(),
1121+
TP->hasType() ? TP->getType() : Type(),
1122+
TP->getTypeRepr(),
11211123
fixedLayoutStructContext ? fixedLayoutStructContext : anyVar,
11221124
/*mayBeInferred*/ true,
11231125
[&](AccessScope typeAccessScope, const TypeRepr *complainRepr,
@@ -1835,7 +1837,8 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
18351837
if (shouldSkipChecking(anyVar))
18361838
return;
18371839

1838-
checkType(TP->getTypeLoc(), anyVar, getDiagnoser(anyVar));
1840+
checkType(TP->hasType() ? TP->getType() : Type(),
1841+
TP->getTypeRepr(), anyVar, getDiagnoser(anyVar));
18391842

18401843
// Check the property wrapper types.
18411844
for (auto attr : anyVar->getAttachedPropertyWrappers())

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,7 +2023,6 @@ class FunctionSyntacticDiagnosticWalker : public ASTWalker {
20232023
return {false, pattern};
20242024
}
20252025

2026-
bool walkToTypeLocPre(TypeLoc &typeLoc) override { return false; }
20272026
bool walkToTypeReprPre(TypeRepr *typeRepr) override { return false; }
20282027
bool walkToParameterListPre(ParameterList *params) override { return false; }
20292028
};
@@ -3913,7 +3912,6 @@ void swift::forEachExprInConstraintSystem(
39133912
}
39143913
bool walkToDeclPre(Decl *D) override { return false; }
39153914
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
3916-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
39173915
};
39183916

39193917
expr->walk(ChildWalker(callback));

lib/Serialization/SerializeDoc.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,6 @@ static void writeDeclCommentTable(
447447
return { false, E };
448448
}
449449

450-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
451450
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
452451
bool walkToParameterListPre(ParameterList *PL) override { return false; }
453452
};
@@ -685,7 +684,6 @@ struct BasicDeclLocsTableWriter : public ASTWalker {
685684

686685
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override { return { false, S };}
687686
std::pair<bool, Expr *> walkToExprPre(Expr *E) override { return { false, E };}
688-
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
689687
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
690688
bool walkToParameterListPre(ParameterList *PL) override { return false; }
691689

0 commit comments

Comments
 (0)