Skip to content

Commit 6700077

Browse files
Merge pull request swiftlang#33498 from AnthonyLatsis/type-locura-3
AST: Abstract away the use of TypeLoc for function and subscript result types
2 parents 713ca60 + efa8f86 commit 6700077

36 files changed

+381
-328
lines changed

include/swift/AST/Decl.h

Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5598,28 +5598,52 @@ enum class ObjCSubscriptKind {
55985598
/// signatures (indices and element type) are distinct.
55995599
///
56005600
class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
5601+
friend class ResultTypeRequest;
5602+
56015603
SourceLoc StaticLoc;
56025604
SourceLoc ArrowLoc;
56035605
SourceLoc EndLoc;
56045606
ParameterList *Indices;
56055607
TypeLoc ElementTy;
56065608

5607-
public:
5609+
void setElementInterfaceType(Type type);
5610+
56085611
SubscriptDecl(DeclName Name,
56095612
SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
56105613
SourceLoc SubscriptLoc, ParameterList *Indices,
5611-
SourceLoc ArrowLoc, TypeLoc ElementTy, DeclContext *Parent,
5614+
SourceLoc ArrowLoc, TypeRepr *ElementTyR, DeclContext *Parent,
56125615
GenericParamList *GenericParams)
56135616
: GenericContext(DeclContextKind::SubscriptDecl, Parent, GenericParams),
56145617
AbstractStorageDecl(DeclKind::Subscript,
56155618
StaticSpelling != StaticSpellingKind::None,
56165619
Parent, Name, SubscriptLoc,
56175620
/*will be overwritten*/ StorageIsNotMutable),
56185621
StaticLoc(StaticLoc), ArrowLoc(ArrowLoc),
5619-
Indices(nullptr), ElementTy(ElementTy) {
5622+
Indices(nullptr), ElementTy(ElementTyR) {
56205623
Bits.SubscriptDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);
56215624
setIndices(Indices);
56225625
}
5626+
5627+
public:
5628+
/// Factory function only for use by deserialization.
5629+
static SubscriptDecl *createDeserialized(ASTContext &Context, DeclName Name,
5630+
StaticSpellingKind StaticSpelling,
5631+
Type ElementTy, DeclContext *Parent,
5632+
GenericParamList *GenericParams);
5633+
5634+
static SubscriptDecl *create(ASTContext &Context, DeclName Name,
5635+
SourceLoc StaticLoc,
5636+
StaticSpellingKind StaticSpelling,
5637+
SourceLoc SubscriptLoc, ParameterList *Indices,
5638+
SourceLoc ArrowLoc, TypeRepr *ElementTyR,
5639+
DeclContext *Parent,
5640+
GenericParamList *GenericParams);
5641+
5642+
static SubscriptDecl *createImported(ASTContext &Context, DeclName Name,
5643+
SourceLoc SubscriptLoc,
5644+
ParameterList *Indices,
5645+
SourceLoc ArrowLoc, Type ElementTy,
5646+
DeclContext *Parent, ClangNode ClangN);
56235647

56245648
/// \returns the way 'static'/'class' was spelled in the source.
56255649
StaticSpellingKind getStaticSpelling() const {
@@ -5646,8 +5670,11 @@ class SubscriptDecl : public GenericContext, public AbstractStorageDecl {
56465670
/// Retrieve the type of the element referenced by a subscript
56475671
/// operation.
56485672
Type getElementInterfaceType() const;
5649-
TypeLoc &getElementTypeLoc() { return ElementTy; }
5650-
const TypeLoc &getElementTypeLoc() const { return ElementTy; }
5673+
5674+
TypeRepr *getElementTypeRepr() const { return ElementTy.getTypeRepr(); }
5675+
SourceRange getElementTypeSourceRange() const {
5676+
return ElementTy.getSourceRange();
5677+
}
56515678

56525679
/// Determine the kind of Objective-C subscripting this declaration
56535680
/// implies.
@@ -6146,6 +6173,7 @@ class FuncDecl : public AbstractFunctionDecl {
61466173
friend class AbstractFunctionDecl;
61476174
friend class SelfAccessKindRequest;
61486175
friend class IsStaticRequest;
6176+
friend class ResultTypeRequest;
61496177

61506178
SourceLoc StaticLoc; // Location of the 'static' token or invalid.
61516179
SourceLoc FuncLoc; // Location of the 'func' token.
@@ -6180,6 +6208,8 @@ class FuncDecl : public AbstractFunctionDecl {
61806208
Bits.FuncDecl.HasTopLevelLocalContextCaptures = false;
61816209
}
61826210

6211+
void setResultInterfaceType(Type type);
6212+
61836213
private:
61846214
static FuncDecl *createImpl(ASTContext &Context, SourceLoc StaticLoc,
61856215
StaticSpellingKind StaticSpelling,
@@ -6207,25 +6237,31 @@ class FuncDecl : public AbstractFunctionDecl {
62076237

62086238
public:
62096239
/// Factory function only for use by deserialization.
6210-
static FuncDecl *createDeserialized(ASTContext &Context, SourceLoc StaticLoc,
6240+
static FuncDecl *createDeserialized(ASTContext &Context,
62116241
StaticSpellingKind StaticSpelling,
6212-
SourceLoc FuncLoc,
6213-
DeclName Name, SourceLoc NameLoc,
6214-
bool Async, SourceLoc AsyncLoc,
6215-
bool Throws, SourceLoc ThrowsLoc,
6242+
DeclName Name, bool Async, bool Throws,
62166243
GenericParamList *GenericParams,
6217-
DeclContext *Parent);
6244+
Type FnRetType, DeclContext *Parent);
62186245

62196246
static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
6220-
StaticSpellingKind StaticSpelling,
6221-
SourceLoc FuncLoc,
6222-
DeclName Name, SourceLoc NameLoc,
6223-
bool Async, SourceLoc AsyncLoc,
6224-
bool Throws, SourceLoc ThrowsLoc,
6247+
StaticSpellingKind StaticSpelling, SourceLoc FuncLoc,
6248+
DeclName Name, SourceLoc NameLoc, bool Async,
6249+
SourceLoc AsyncLoc, bool Throws, SourceLoc ThrowsLoc,
62256250
GenericParamList *GenericParams,
6226-
ParameterList *ParameterList,
6227-
TypeLoc FnRetType, DeclContext *Parent,
6228-
ClangNode ClangN = ClangNode());
6251+
ParameterList *BodyParams, TypeRepr *ResultTyR,
6252+
DeclContext *Parent);
6253+
6254+
static FuncDecl *createImplicit(ASTContext &Context,
6255+
StaticSpellingKind StaticSpelling,
6256+
DeclName Name, SourceLoc NameLoc, bool Async,
6257+
bool Throws, GenericParamList *GenericParams,
6258+
ParameterList *BodyParams, Type FnRetType,
6259+
DeclContext *Parent);
6260+
6261+
static FuncDecl *createImported(ASTContext &Context, SourceLoc FuncLoc,
6262+
DeclName Name, SourceLoc NameLoc, bool Throws,
6263+
ParameterList *BodyParams, Type FnRetType,
6264+
DeclContext *Parent, ClangNode ClangN);
62296265

62306266
bool isStatic() const;
62316267

@@ -6269,8 +6305,10 @@ class FuncDecl : public AbstractFunctionDecl {
62696305
}
62706306
SourceRange getSourceRange() const;
62716307

6272-
TypeLoc &getBodyResultTypeLoc() { return FnRetType; }
6273-
const TypeLoc &getBodyResultTypeLoc() const { return FnRetType; }
6308+
TypeRepr *getResultTypeRepr() const { return FnRetType.getTypeRepr(); }
6309+
SourceRange getResultTypeSourceRange() const {
6310+
return FnRetType.getSourceRange();
6311+
}
62746312

62756313
/// Retrieve the result interface type of this function.
62766314
Type getResultInterfaceType() const;
@@ -6391,15 +6429,12 @@ class AccessorDecl final : public FuncDecl {
63916429

63926430
public:
63936431
static AccessorDecl *createDeserialized(ASTContext &ctx,
6394-
SourceLoc declLoc,
6395-
SourceLoc accessorKeywordLoc,
6396-
AccessorKind accessorKind,
6397-
AbstractStorageDecl *storage,
6398-
SourceLoc staticLoc,
6399-
StaticSpellingKind staticSpelling,
6400-
bool throws, SourceLoc throwsLoc,
6401-
GenericParamList *genericParams,
6402-
DeclContext *parent);
6432+
AccessorKind accessorKind,
6433+
AbstractStorageDecl *storage,
6434+
StaticSpellingKind staticSpelling,
6435+
bool throws,
6436+
GenericParamList *genericParams,
6437+
Type fnRetType, DeclContext *parent);
64036438

64046439
static AccessorDecl *create(ASTContext &ctx, SourceLoc declLoc,
64056440
SourceLoc accessorKeywordLoc,
@@ -6410,7 +6445,7 @@ class AccessorDecl final : public FuncDecl {
64106445
bool throws, SourceLoc throwsLoc,
64116446
GenericParamList *genericParams,
64126447
ParameterList *parameterList,
6413-
TypeLoc fnRetType, DeclContext *parent,
6448+
Type fnRetType, DeclContext *parent,
64146449
ClangNode clangNode = ClangNode());
64156450

64166451
SourceLoc getAccessorKeywordLoc() const { return 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

include/swift/AST/TypeLoc.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class alignas(1 << TypeReprAlignInBits) TypeLoc final {
6262

6363
bool isNull() const { return getType().isNull() && TyR == nullptr; }
6464

65-
void setInvalidType(ASTContext &C);
6665
void setType(Type Ty);
6766

6867
friend llvm::hash_code hash_value(const TypeLoc &owner) {

lib/AST/ASTContext.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,10 +3886,6 @@ CanType OpenedArchetypeType::getAny(Type existential) {
38863886
return OpenedArchetypeType::get(existential);
38873887
}
38883888

3889-
void TypeLoc::setInvalidType(ASTContext &C) {
3890-
Ty = ErrorType::get(C);
3891-
}
3892-
38933889
void SubstitutionMap::Storage::Profile(
38943890
llvm::FoldingSetNodeID &id,
38953891
GenericSignature genericSig,

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: 5 additions & 6 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
@@ -3166,9 +3166,8 @@ void PrintAST::visitSubscriptDecl(SubscriptDecl *decl) {
31663166
});
31673167
Printer << " -> ";
31683168

3169-
TypeLoc elementTy = decl->getElementTypeLoc();
3170-
if (!elementTy.getTypeRepr())
3171-
elementTy = TypeLoc::withoutLoc(decl->getElementInterfaceType());
3169+
TypeLoc elementTy(decl->getElementTypeRepr(),
3170+
decl->getElementInterfaceType());
31723171
Printer.printDeclResultTypePre(decl, elementTy);
31733172
Printer.callPrintStructurePre(PrintStructureKind::FunctionReturnType);
31743173

lib/AST/ASTWalker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
358358
bool WalkGenerics = visitGenericParamListIfNeeded(SD);
359359

360360
visit(SD->getIndices());
361-
if (auto *const TyR = SD->getElementTypeLoc().getTypeRepr())
361+
if (auto *const TyR = SD->getElementTypeRepr())
362362
if (doIt(TyR))
363363
return true;
364364

@@ -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/Builtins.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,10 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType) {
168168
auto *paramList = ParameterList::create(Context, params);
169169

170170
DeclName Name(Context, Id, paramList);
171-
auto FD = FuncDecl::create(Context, /*StaticLoc=*/SourceLoc(),
172-
StaticSpellingKind::None,
173-
/*FuncLoc=*/SourceLoc(),
174-
Name, /*NameLoc=*/SourceLoc(),
175-
/*Async-*/false, /*AsyncLoc=*/SourceLoc(),
176-
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
177-
/*GenericParams=*/nullptr,
178-
paramList,
179-
TypeLoc::withoutLoc(ResType), DC);
180-
FD->setImplicit();
171+
auto *const FD = FuncDecl::createImplicit(
172+
Context, StaticSpellingKind::None, Name, /*NameLoc=*/SourceLoc(),
173+
/*Async=*/false, /*Throws=*/false,
174+
/*GenericParams=*/nullptr, paramList, ResType, DC);
181175
FD->setAccess(AccessLevel::Public);
182176
return FD;
183177
}
@@ -214,17 +208,11 @@ getBuiltinGenericFunction(Identifier Id,
214208
auto *paramList = ParameterList::create(Context, params);
215209

216210
DeclName Name(Context, Id, paramList);
217-
auto func = FuncDecl::create(Context, /*StaticLoc=*/SourceLoc(),
218-
StaticSpellingKind::None,
219-
/*FuncLoc=*/SourceLoc(),
220-
Name, /*NameLoc=*/SourceLoc(),
221-
/*Async-*/false, /*AsyncLoc=*/SourceLoc(),
222-
/*Throws=*/ Rethrows, /*ThrowsLoc=*/SourceLoc(),
223-
GenericParams,
224-
paramList,
225-
TypeLoc::withoutLoc(ResType), DC);
226-
227-
func->setImplicit();
211+
auto *const func = FuncDecl::createImplicit(
212+
Context, StaticSpellingKind::None, Name, /*NameLoc=*/SourceLoc(),
213+
/*Async=*/false,
214+
/*Throws=*/Rethrows, GenericParams, paramList, ResType, DC);
215+
228216
func->setAccess(AccessLevel::Public);
229217
func->setGenericSignature(Sig);
230218
if (Rethrows)

0 commit comments

Comments
 (0)