Skip to content

ASTWalker: Offload TypeLoc #33283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions include/swift/AST/ASTWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ModuleDecl;
class Stmt;
class Pattern;
class TypeRepr;
class TypeLoc;
class ParameterList;
enum class AccessKind: unsigned char;

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

/// This method is called when first visiting a TypeLoc, before
/// walking into its TypeRepr children. If it returns false, the subtree is
/// skipped.
///
/// \param TL The TypeLoc to check.
virtual bool walkToTypeLocPre(TypeLoc &TL) { return true; }

/// This method is called after visiting the children of a TypeLoc.
/// If it returns false, the remaining traversal is terminated and returns
/// failure.
virtual bool walkToTypeLocPost(TypeLoc &TL) { return true; }


/// This method is called when first visiting a TypeRepr, before
/// walking into its children. If it returns false, the subtree is skipped.
///
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace swift {
class Expr;
enum class CheckedCastKind : unsigned;
class TypeExpr;
class TypeLoc;

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

TypeRepr *getTypeRepr() const { return PatTypeRepr; }

TypeLoc getTypeLoc() const;
SourceLoc getLoc() const;
SourceRange getSourceRange() const;

Expand Down
2 changes: 0 additions & 2 deletions include/swift/IDE/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,6 @@ class NameMatcher: public ASTWalker {
bool walkToDeclPost(Decl *D) override;
std::pair<bool, Stmt*> walkToStmtPre(Stmt *S) override;
Stmt* walkToStmtPost(Stmt *S) override;
bool walkToTypeLocPre(TypeLoc &TL) override;
bool walkToTypeLocPost(TypeLoc &TL) override;
bool walkToTypeReprPre(TypeRepr *T) override;
bool walkToTypeReprPost(TypeRepr *T) override;
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override;
Expand Down
4 changes: 3 additions & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,9 @@ void PrintAST::printTypedPattern(const TypedPattern *TP) {
if (auto decl = named->getDecl())
isIUO = decl->isImplicitlyUnwrappedOptional();

printTypeLocForImplicitlyUnwrappedOptional(TP->getTypeLoc(), isIUO);
const auto TyLoc = TypeLoc(TP->getTypeRepr(),
TP->hasType() ? TP->getType() : Type());
printTypeLocForImplicitlyUnwrappedOptional(TyLoc, isIUO);
}

/// Determines if we are required to print the name of a property declaration,
Expand Down
1 change: 0 additions & 1 deletion lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1781,7 +1781,6 @@ void ScopeCreator::forEachClosureIn(
return {false, P};
}
bool walkToDeclPre(Decl *D) override { return false; }
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
bool walkToParameterListPre(ParameterList *PL) override { return false; }
};
Expand Down
47 changes: 19 additions & 28 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
if (doIt(typeRepr))
return true;
for (auto &Inherit : ED->getInherited()) {
if (doIt(Inherit))
return true;
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}
if (visitTrailingRequirements(ED))
return true;
Expand Down Expand Up @@ -258,9 +259,10 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

bool visitAbstractTypeParamDecl(AbstractTypeParamDecl *TPD) {
for (auto Inherit: TPD->getInherited()) {
if (doIt(Inherit))
return true;
for (const auto &Inherit: TPD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(TyR))
return true;
}

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

bool WalkGenerics = visitGenericParamListIfNeeded(NTD);

for (auto &Inherit : NTD->getInherited()) {
if (doIt(Inherit))
return true;
for (const auto &Inherit : NTD->getInherited()) {
if (auto *const TyR = Inherit.getTypeRepr())
if (doIt(Inherit.getTypeRepr()))
return true;
}

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

visit(SD->getIndices());
if (doIt(SD->getElementTypeLoc()))
return true;
if (auto *const TyR = SD->getElementTypeLoc().getTypeRepr())
if (doIt(TyR))
return true;

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

if (auto *FD = dyn_cast<FuncDecl>(AFD))
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
if (!isa<AccessorDecl>(FD))
if (doIt(FD->getBodyResultTypeLoc()))
return true;
if (auto *const TyR = FD->getBodyResultTypeLoc().getTypeRepr())
if (doIt(TyR))
return true;
}

// Visit trailing requirements
if (WalkGenerics && visitTrailingRequirements(AFD))
Expand Down Expand Up @@ -1319,21 +1325,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
return false;
}

bool doIt(TypeLoc &TL) {
if (!Walker.walkToTypeLocPre(TL))
return false;

// No "visit" since TypeLocs are not a class hierarchy. Clients can do what
// they want in walkToTypeLocPre.

if (auto typerepr = TL.getTypeRepr())
if (doIt(typerepr))
return true;

// If we didn't bail out, do post-order visitation.
return !Walker.walkToTypeLocPost(TL);
}

/// Returns true on failure.
bool doIt(TypeRepr *T) {
// Do the pre-order visitation. If it returns false, we just
Expand Down
2 changes: 0 additions & 2 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ forEachImmediateChildExpr(llvm::function_ref<Expr *(Expr *)> callback) {
}
bool walkToDeclPre(Decl *D) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
};

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

this->walk(ChildWalker(callback));
Expand Down
10 changes: 0 additions & 10 deletions lib/AST/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ namespace {
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
return { false, S };
}
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
bool walkToParameterListPre(ParameterList *PL) override { return false; }
bool walkToDeclPre(Decl *D) override { return false; }
Expand Down Expand Up @@ -407,15 +406,6 @@ TypedPattern::TypedPattern(Pattern *pattern, TypeRepr *tr)
Bits.TypedPattern.IsPropagatedType = false;
}

TypeLoc TypedPattern::getTypeLoc() const {
TypeLoc loc = TypeLoc(PatTypeRepr);

if (hasType())
loc.setType(getType());

return loc;
}

SourceLoc TypedPattern::getLoc() const {
if (SubPattern->isImplicit() && PatTypeRepr)
return PatTypeRepr->getSourceRange().Start;
Expand Down
1 change: 0 additions & 1 deletion lib/IDE/ExprContextAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ class ExprFinder : public ASTWalker {
return {isInterstingRange(S), S};
}

bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
};
} // anonymous namespace
Expand Down
10 changes: 0 additions & 10 deletions lib/IDE/SwiftSourceDocInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,6 @@ Expr *NameMatcher::walkToExprPost(Expr *E) {
return E;
}

bool NameMatcher::walkToTypeLocPre(TypeLoc &TL) {
if (isDone() || shouldSkip(TL.getSourceRange()))
return false;
return true;
}

bool NameMatcher::walkToTypeLocPost(TypeLoc &TL) {
return !isDone();
}

bool NameMatcher::walkToTypeReprPre(TypeRepr *T) {
if (isDone() || shouldSkip(T->getSourceRange()))
return false;
Expand Down
1 change: 0 additions & 1 deletion lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2467,7 +2467,6 @@ struct FallthroughFinder : ASTWalker {
}

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

static FallthroughStmt *findFallthrough(Stmt *s) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ namespace {
}

/// Ignore types.
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
};

/// Given a collection of "linked" expressions, analyzes them for
Expand Down Expand Up @@ -305,7 +305,7 @@ namespace {
}

/// Ignore types.
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
};

/// For a given expression, given information that is global to the
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4639,8 +4639,10 @@ SolutionApplicationTarget SolutionApplicationTarget::forInitialization(
if (auto *typedPattern = dyn_cast<TypedPattern>(pattern)) {
const Pattern *inner = typedPattern->getSemanticsProvidingPattern();
if (isa<NamedPattern>(inner) || isa<AnyPattern>(inner)) {
contextualType = typedPattern->getTypeLoc();
if (!contextualType.getType())
contextualType = TypeLoc(typedPattern->getTypeRepr());
if (typedPattern->hasType())
contextualType.setType(typedPattern->getType());
else
contextualType.setType(patternType);
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Sema/TypeCheckAccess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,8 @@ class AccessControlChecker : public AccessControlCheckerBase,
if (!anyVar)
return;

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

checkTypeAccess(
TP->getTypeLoc(),
TP->hasType() ? TP->getType() : Type(),
TP->getTypeRepr(),
fixedLayoutStructContext ? fixedLayoutStructContext : anyVar,
/*mayBeInferred*/ true,
[&](AccessScope typeAccessScope, const TypeRepr *complainRepr,
Expand Down Expand Up @@ -1835,7 +1837,8 @@ class ExportabilityChecker : public DeclVisitor<ExportabilityChecker> {
if (shouldSkipChecking(anyVar))
return;

checkType(TP->getTypeLoc(), anyVar, getDiagnoser(anyVar));
checkType(TP->hasType() ? TP->getType() : Type(),
TP->getTypeRepr(), anyVar, getDiagnoser(anyVar));

// Check the property wrapper types.
for (auto attr : anyVar->getAttachedPropertyWrappers())
Expand Down
2 changes: 0 additions & 2 deletions lib/Sema/TypeCheckConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,6 @@ class FunctionSyntacticDiagnosticWalker : public ASTWalker {
return {false, pattern};
}

bool walkToTypeLocPre(TypeLoc &typeLoc) override { return false; }
bool walkToTypeReprPre(TypeRepr *typeRepr) override { return false; }
bool walkToParameterListPre(ParameterList *params) override { return false; }
};
Expand Down Expand Up @@ -4004,7 +4003,6 @@ void swift::forEachExprInConstraintSystem(
}
bool walkToDeclPre(Decl *D) override { return false; }
bool walkToTypeReprPre(TypeRepr *T) override { return false; }
bool walkToTypeLocPre(TypeLoc &TL) override { return false; }
};

expr->walk(ChildWalker(callback));
Expand Down
2 changes: 0 additions & 2 deletions lib/Serialization/SerializeDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,6 @@ static void writeDeclCommentTable(
return { false, E };
}

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

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

Expand Down