Skip to content

Commit 21faa48

Browse files
committed
Remove FuncDecl::getBodyResultTypeLoc
1 parent c35d84a commit 21faa48

20 files changed

+119
-119
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6149,6 +6149,7 @@ class FuncDecl : public AbstractFunctionDecl {
61496149
friend class AbstractFunctionDecl;
61506150
friend class SelfAccessKindRequest;
61516151
friend class IsStaticRequest;
6152+
friend class ResultTypeRequest;
61526153

61536154
SourceLoc StaticLoc; // Location of the 'static' token or invalid.
61546155
SourceLoc FuncLoc; // Location of the 'func' token.
@@ -6183,6 +6184,8 @@ class FuncDecl : public AbstractFunctionDecl {
61836184
Bits.FuncDecl.HasTopLevelLocalContextCaptures = false;
61846185
}
61856186

6187+
void setResultInterfaceType(Type type);
6188+
61866189
private:
61876190
static FuncDecl *createImpl(ASTContext &Context, SourceLoc StaticLoc,
61886191
StaticSpellingKind StaticSpelling,
@@ -6210,14 +6213,11 @@ class FuncDecl : public AbstractFunctionDecl {
62106213

62116214
public:
62126215
/// Factory function only for use by deserialization.
6213-
static FuncDecl *createDeserialized(ASTContext &Context, SourceLoc StaticLoc,
6216+
static FuncDecl *createDeserialized(ASTContext &Context,
62146217
StaticSpellingKind StaticSpelling,
6215-
SourceLoc FuncLoc,
6216-
DeclName Name, SourceLoc NameLoc,
6217-
bool Async, SourceLoc AsyncLoc,
6218-
bool Throws, SourceLoc ThrowsLoc,
6218+
DeclName Name, bool Async, bool Throws,
62196219
GenericParamList *GenericParams,
6220-
DeclContext *Parent);
6220+
Type FnRetType, DeclContext *Parent);
62216221

62226222
static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
62236223
StaticSpellingKind StaticSpelling,
@@ -6272,7 +6272,6 @@ class FuncDecl : public AbstractFunctionDecl {
62726272
}
62736273
SourceRange getSourceRange() const;
62746274

6275-
TypeLoc &getBodyResultTypeLoc() { return FnRetType; }
62766275
TypeRepr *getResultTypeRepr() const { return FnRetType.getTypeRepr(); }
62776276
SourceRange getResultTypeSourceRange() const {
62786277
return FnRetType.getSourceRange();
@@ -6397,15 +6396,12 @@ class AccessorDecl final : public FuncDecl {
63976396

63986397
public:
63996398
static AccessorDecl *createDeserialized(ASTContext &ctx,
6400-
SourceLoc declLoc,
6401-
SourceLoc accessorKeywordLoc,
6402-
AccessorKind accessorKind,
6403-
AbstractStorageDecl *storage,
6404-
SourceLoc staticLoc,
6405-
StaticSpellingKind staticSpelling,
6406-
bool throws, SourceLoc throwsLoc,
6407-
GenericParamList *genericParams,
6408-
DeclContext *parent);
6399+
AccessorKind accessorKind,
6400+
AbstractStorageDecl *storage,
6401+
StaticSpellingKind staticSpelling,
6402+
bool throws,
6403+
GenericParamList *genericParams,
6404+
Type fnRetType, DeclContext *parent);
64096405

64106406
static AccessorDecl *create(ASTContext &ctx, SourceLoc declLoc,
64116407
SourceLoc accessorKeywordLoc,

include/swift/AST/TypeCheckRequests.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,8 +1449,6 @@ class ResultTypeRequest
14491449
private:
14501450
friend SimpleRequest;
14511451

1452-
TypeLoc &getResultTypeLoc() const;
1453-
14541452
// Evaluation.
14551453
Type evaluate(Evaluator &evaluator, ValueDecl *decl) const;
14561454

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,13 +1074,13 @@ namespace {
10741074
Indent -= 2;
10751075

10761076
if (auto FD = dyn_cast<FuncDecl>(D)) {
1077-
if (FD->getBodyResultTypeLoc().getTypeRepr()) {
1077+
if (FD->getResultTypeRepr()) {
10781078
OS << '\n';
10791079
Indent += 2;
10801080
OS.indent(Indent);
10811081
PrintWithColorRAII(OS, ParenthesisColor) << '(';
10821082
OS << "result\n";
1083-
printRec(FD->getBodyResultTypeLoc().getTypeRepr());
1083+
printRec(FD->getResultTypeRepr());
10841084
PrintWithColorRAII(OS, ParenthesisColor) << ')';
10851085
if (auto opaque = FD->getOpaqueResultTypeDecl()) {
10861086
OS << '\n';

lib/AST/ASTPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2948,8 +2948,8 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
29482948
if (Options.PrintOriginalSourceText && decl->getStartLoc().isValid()) {
29492949
SourceLoc StartLoc = decl->getStartLoc();
29502950
SourceLoc EndLoc;
2951-
if (!decl->getBodyResultTypeLoc().isNull()) {
2952-
EndLoc = decl->getBodyResultTypeLoc().getSourceRange().End;
2951+
if (decl->getResultTypeRepr()) {
2952+
EndLoc = decl->getResultTypeSourceRange().End;
29532953
} else {
29542954
EndLoc = decl->getSignatureSourceRange().End;
29552955
}
@@ -2986,7 +2986,7 @@ void PrintAST::visitFuncDecl(FuncDecl *decl) {
29862986

29872987
Type ResultTy = decl->getResultInterfaceType();
29882988
if (ResultTy && !ResultTy->isVoid()) {
2989-
TypeLoc ResultTyLoc = decl->getBodyResultTypeLoc();
2989+
TypeLoc ResultTyLoc(decl->getResultTypeRepr(), ResultTy);
29902990

29912991
// When printing a protocol requirement with types substituted for a
29922992
// conforming class, replace occurrences of the 'Self' generic parameter

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
394394

395395
if (auto *FD = dyn_cast<FuncDecl>(AFD)) {
396396
if (!isa<AccessorDecl>(FD))
397-
if (auto *const TyR = FD->getBodyResultTypeLoc().getTypeRepr())
397+
if (auto *const TyR = FD->getResultTypeRepr())
398398
if (doIt(TyR))
399399
return true;
400400
}

lib/AST/Decl.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7113,6 +7113,11 @@ void AbstractFunctionDecl::addDerivativeFunctionConfiguration(
71137113
DerivativeFunctionConfigs->insert(config);
71147114
}
71157115

7116+
void FuncDecl::setResultInterfaceType(Type type) {
7117+
getASTContext().evaluator.cacheOutput(ResultTypeRequest{this},
7118+
std::move(type));
7119+
}
7120+
71167121
FuncDecl *FuncDecl::createImpl(ASTContext &Context,
71177122
SourceLoc StaticLoc,
71187123
StaticSpellingKind StaticSpelling,
@@ -7142,18 +7147,17 @@ FuncDecl *FuncDecl::createImpl(ASTContext &Context,
71427147
}
71437148

71447149
FuncDecl *FuncDecl::createDeserialized(ASTContext &Context,
7145-
SourceLoc StaticLoc,
71467150
StaticSpellingKind StaticSpelling,
7147-
SourceLoc FuncLoc,
7148-
DeclName Name, SourceLoc NameLoc,
7149-
bool Async, SourceLoc AsyncLoc,
7150-
bool Throws, SourceLoc ThrowsLoc,
7151+
DeclName Name, bool Async, bool Throws,
71517152
GenericParamList *GenericParams,
7152-
DeclContext *Parent) {
7153-
return createImpl(Context, StaticLoc, StaticSpelling, FuncLoc,
7154-
Name, NameLoc, Async, AsyncLoc, Throws, ThrowsLoc,
7155-
GenericParams, Parent,
7156-
ClangNode());
7153+
Type FnRetType, DeclContext *Parent) {
7154+
assert(FnRetType && "Deserialized result type must not be null");
7155+
auto *const FD =
7156+
FuncDecl::createImpl(Context, SourceLoc(), StaticSpelling, SourceLoc(),
7157+
Name, SourceLoc(), Async, SourceLoc(), Throws,
7158+
SourceLoc(), GenericParams, Parent, ClangNode());
7159+
FD->setResultInterfaceType(FnRetType);
7160+
return FD;
71577161
}
71587162

71597163
FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
@@ -7171,7 +7175,7 @@ FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
71717175
Name, NameLoc, Async, AsyncLoc, Throws, ThrowsLoc,
71727176
GenericParams, Parent, ClangN);
71737177
FD->setParameters(BodyParams);
7174-
FD->getBodyResultTypeLoc() = FnRetType;
7178+
FD->FnRetType = FnRetType;
71757179
return FD;
71767180
}
71777181

@@ -7223,20 +7227,18 @@ AccessorDecl *AccessorDecl::createImpl(ASTContext &ctx,
72237227
return D;
72247228
}
72257229

7226-
AccessorDecl *AccessorDecl::createDeserialized(ASTContext &ctx,
7227-
SourceLoc declLoc,
7228-
SourceLoc accessorKeywordLoc,
7229-
AccessorKind accessorKind,
7230-
AbstractStorageDecl *storage,
7231-
SourceLoc staticLoc,
7232-
StaticSpellingKind staticSpelling,
7233-
bool throws, SourceLoc throwsLoc,
7234-
GenericParamList *genericParams,
7235-
DeclContext *parent) {
7236-
return createImpl(ctx, declLoc, accessorKeywordLoc, accessorKind,
7237-
storage, staticLoc, staticSpelling,
7238-
throws, throwsLoc, genericParams, parent,
7239-
ClangNode());
7230+
AccessorDecl *
7231+
AccessorDecl::createDeserialized(ASTContext &ctx, AccessorKind accessorKind,
7232+
AbstractStorageDecl *storage,
7233+
StaticSpellingKind staticSpelling,
7234+
bool throws, GenericParamList *genericParams,
7235+
Type fnRetType, DeclContext *parent) {
7236+
assert(fnRetType && "Deserialized result type must not be null");
7237+
auto *const D = AccessorDecl::createImpl(
7238+
ctx, SourceLoc(), SourceLoc(), accessorKind, storage, SourceLoc(),
7239+
staticSpelling, throws, SourceLoc(), genericParams, parent, ClangNode());
7240+
D->setResultInterfaceType(fnRetType);
7241+
return D;
72407242
}
72417243

72427244
AccessorDecl *AccessorDecl::create(ASTContext &ctx,
@@ -7257,7 +7259,7 @@ AccessorDecl *AccessorDecl::create(ASTContext &ctx,
72577259
staticLoc, staticSpelling, throws, throwsLoc,
72587260
genericParams, parent, clangNode);
72597261
D->setParameters(bodyParams);
7260-
D->getBodyResultTypeLoc() = TypeLoc::withoutLoc(fnRetType);
7262+
D->setResultInterfaceType(fnRetType);
72617263
return D;
72627264
}
72637265

lib/AST/TypeCheckRequests.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -935,23 +935,28 @@ void ParamSpecifierRequest::cacheResult(ParamSpecifier specifier) const {
935935
// ResultTypeRequest computation.
936936
//----------------------------------------------------------------------------//
937937

938-
TypeLoc &ResultTypeRequest::getResultTypeLoc() const {
939-
auto *decl = std::get<0>(getStorage());
940-
if (auto *funcDecl = dyn_cast<FuncDecl>(decl))
941-
return funcDecl->getBodyResultTypeLoc();
942-
auto *subscriptDecl = cast<SubscriptDecl>(decl);
943-
return subscriptDecl->getElementTypeLoc();
944-
}
945-
946938
Optional<Type> ResultTypeRequest::getCachedResult() const {
947-
if (auto type = getResultTypeLoc().getType())
948-
return type;
939+
Type type;
940+
auto *const decl = std::get<0>(getStorage());
941+
if (const auto *const funcDecl = dyn_cast<FuncDecl>(decl)) {
942+
type = funcDecl->FnRetType.getType();
943+
} else {
944+
type = cast<SubscriptDecl>(decl)->getElementTypeLoc().getType();
945+
}
949946

950-
return None;
947+
if (type.isNull())
948+
return None;
949+
950+
return type;
951951
}
952952

953953
void ResultTypeRequest::cacheResult(Type type) const {
954-
getResultTypeLoc().setType(type);
954+
auto *const decl = std::get<0>(getStorage());
955+
if (auto *const funcDecl = dyn_cast<FuncDecl>(decl)) {
956+
funcDecl->FnRetType.setType(type);
957+
} else {
958+
cast<SubscriptDecl>(decl)->getElementTypeLoc().setType(type);
959+
}
955960
}
956961

957962
//----------------------------------------------------------------------------//

lib/ClangImporter/ImportDecl.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,17 +4417,6 @@ namespace {
44174417
}
44184418
}
44194419

4420-
auto result = createFuncOrAccessor(Impl.SwiftContext,
4421-
/*funcLoc*/SourceLoc(),
4422-
accessorInfo,
4423-
importedName.getDeclName(),
4424-
/*nameLoc*/SourceLoc(),
4425-
bodyParams, Type(),
4426-
importedName.getErrorInfo().hasValue(),
4427-
dc, decl);
4428-
4429-
result->setAccess(getOverridableAccessLevel(dc));
4430-
44314420
auto resultTy = importedType.getType();
44324421
auto isIUO = importedType.isImplicitlyUnwrapped();
44334422

@@ -4451,8 +4440,16 @@ namespace {
44514440
}
44524441
}
44534442

4454-
// Record the return type.
4455-
result->getBodyResultTypeLoc().setType(resultTy);
4443+
auto result = createFuncOrAccessor(Impl.SwiftContext,
4444+
/*funcLoc*/SourceLoc(),
4445+
accessorInfo,
4446+
importedName.getDeclName(),
4447+
/*nameLoc*/SourceLoc(),
4448+
bodyParams, resultTy,
4449+
importedName.getErrorInfo().hasValue(),
4450+
dc, decl);
4451+
4452+
result->setAccess(getOverridableAccessLevel(dc));
44564453

44574454
// Optional methods in protocols.
44584455
if (decl->getImplementationControl() == clang::ObjCMethodDecl::Optional &&

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ bool ModelASTWalker::walkToDeclPre(Decl *D) {
905905
AFD->getSignatureSourceRange());
906906
if (FD) {
907907
SN.TypeRange = charSourceRangeFromSourceRange(SM,
908-
FD->getBodyResultTypeLoc().getSourceRange());
908+
FD->getResultTypeSourceRange());
909909
}
910910
pushStructureNode(SN, AFD);
911911
} else if (auto *NTD = dyn_cast<NominalTypeDecl>(D)) {

lib/Migrator/APIDiffMigratorPass.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
5858
FoundResult findChild(AbstractFunctionDecl *Parent) {
5959
auto NextIndex = consumeNext();
6060
if (!NextIndex) {
61-
if (auto Func = dyn_cast<FuncDecl>(Parent))
62-
return findChild(Func->getBodyResultTypeLoc());
63-
if (auto Init = dyn_cast<ConstructorDecl>(Parent)) {
61+
if (auto Func = dyn_cast<FuncDecl>(Parent)) {
62+
if (auto *const TyRepr = Func->getResultTypeRepr())
63+
return visit(TyRepr);
64+
} else if (auto Init = dyn_cast<ConstructorDecl>(Parent)) {
6465
SourceLoc End = Init->getFailabilityLoc();
6566
bool Optional = End.isValid();
6667
if (!Optional)
@@ -73,7 +74,7 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
7374

7475
for (auto *Param: *Parent->getParameters()) {
7576
if (!--NextIndex) {
76-
return findChild(Param->getTypeRepr());
77+
return visit(Param->getTypeRepr());
7778
}
7879
}
7980
llvm_unreachable("child index out of bounds");
@@ -100,12 +101,6 @@ class ChildIndexFinder : public TypeReprVisitor<ChildIndexFinder, FoundResult> {
100101
return false;
101102
}
102103

103-
FoundResult findChild(TypeLoc Loc) {
104-
if (!Loc.hasLocation())
105-
return {SourceRange(), false, false, false};
106-
return visit(Loc.getTypeRepr());
107-
}
108-
109104
public:
110105

111106
template<typename T>
@@ -1257,7 +1252,7 @@ struct APIDiffMigratorPass : public ASTMigratorPass, public SourceEntityWalker {
12571252
switch (DiffItem->DiffKind) {
12581253
case NodeAnnotation::GetterToProperty: {
12591254
auto FuncLoc = FD->getFuncLoc();
1260-
auto ReturnTyLoc = FD->getBodyResultTypeLoc().getSourceRange().Start;
1255+
auto ReturnTyLoc = FD->getResultTypeSourceRange().Start;
12611256
auto NameLoc = FD->getNameLoc();
12621257
if (FuncLoc.isInvalid() || ReturnTyLoc.isInvalid() || NameLoc.isInvalid())
12631258
break;

0 commit comments

Comments
 (0)