Skip to content

Commit efa8f86

Browse files
committed
[NFC] FuncDecl: Strip factory constructors out of TypeLocs
1 parent f8d30ec commit efa8f86

11 files changed

+111
-127
lines changed

include/swift/AST/Decl.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6244,15 +6244,24 @@ class FuncDecl : public AbstractFunctionDecl {
62446244
Type FnRetType, DeclContext *Parent);
62456245

62466246
static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
6247-
StaticSpellingKind StaticSpelling,
6248-
SourceLoc FuncLoc,
6249-
DeclName Name, SourceLoc NameLoc,
6250-
bool Async, SourceLoc AsyncLoc,
6251-
bool Throws, SourceLoc ThrowsLoc,
6247+
StaticSpellingKind StaticSpelling, SourceLoc FuncLoc,
6248+
DeclName Name, SourceLoc NameLoc, bool Async,
6249+
SourceLoc AsyncLoc, bool Throws, SourceLoc ThrowsLoc,
62526250
GenericParamList *GenericParams,
6253-
ParameterList *ParameterList,
6254-
TypeLoc FnRetType, DeclContext *Parent,
6255-
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);
62566265

62576266
bool isStatic() const;
62586267

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)

lib/AST/Decl.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7212,24 +7212,51 @@ FuncDecl *FuncDecl::createDeserialized(ASTContext &Context,
72127212
}
72137213

72147214
FuncDecl *FuncDecl::create(ASTContext &Context, SourceLoc StaticLoc,
7215-
StaticSpellingKind StaticSpelling,
7216-
SourceLoc FuncLoc,
7217-
DeclName Name, SourceLoc NameLoc,
7218-
bool Async, SourceLoc AsyncLoc,
7219-
bool Throws, SourceLoc ThrowsLoc,
7215+
StaticSpellingKind StaticSpelling, SourceLoc FuncLoc,
7216+
DeclName Name, SourceLoc NameLoc, bool Async,
7217+
SourceLoc AsyncLoc, bool Throws, SourceLoc ThrowsLoc,
72207218
GenericParamList *GenericParams,
7221-
ParameterList *BodyParams,
7222-
TypeLoc FnRetType, DeclContext *Parent,
7223-
ClangNode ClangN) {
7224-
auto *FD = FuncDecl::createImpl(
7225-
Context, StaticLoc, StaticSpelling, FuncLoc,
7226-
Name, NameLoc, Async, AsyncLoc, Throws, ThrowsLoc,
7227-
GenericParams, Parent, ClangN);
7219+
ParameterList *BodyParams, TypeRepr *ResultTyR,
7220+
DeclContext *Parent) {
7221+
auto *const FD = FuncDecl::createImpl(
7222+
Context, StaticLoc, StaticSpelling, FuncLoc, Name, NameLoc, Async,
7223+
AsyncLoc, Throws, ThrowsLoc, GenericParams, Parent, ClangNode());
72287224
FD->setParameters(BodyParams);
7229-
FD->FnRetType = FnRetType;
7225+
FD->FnRetType = TypeLoc(ResultTyR);
72307226
return FD;
72317227
}
7232-
7228+
7229+
FuncDecl *FuncDecl::createImplicit(ASTContext &Context,
7230+
StaticSpellingKind StaticSpelling,
7231+
DeclName Name, SourceLoc NameLoc, bool Async,
7232+
bool Throws, GenericParamList *GenericParams,
7233+
ParameterList *BodyParams, Type FnRetType,
7234+
DeclContext *Parent) {
7235+
assert(FnRetType);
7236+
auto *const FD = FuncDecl::createImpl(
7237+
Context, SourceLoc(), StaticSpelling, SourceLoc(), Name, NameLoc, Async,
7238+
SourceLoc(), Throws, SourceLoc(), GenericParams, Parent, ClangNode());
7239+
FD->setImplicit();
7240+
FD->setParameters(BodyParams);
7241+
FD->setResultInterfaceType(FnRetType);
7242+
return FD;
7243+
}
7244+
7245+
FuncDecl *FuncDecl::createImported(ASTContext &Context, SourceLoc FuncLoc,
7246+
DeclName Name, SourceLoc NameLoc,
7247+
bool Throws, ParameterList *BodyParams,
7248+
Type FnRetType, DeclContext *Parent,
7249+
ClangNode ClangN) {
7250+
assert(ClangN && FnRetType);
7251+
auto *const FD = FuncDecl::createImpl(
7252+
Context, SourceLoc(), StaticSpellingKind::None, FuncLoc, Name, NameLoc,
7253+
/*Async=*/false, SourceLoc(), Throws, SourceLoc(),
7254+
/*GenericParams=*/nullptr, Parent, ClangN);
7255+
FD->setParameters(BodyParams);
7256+
FD->setResultInterfaceType(FnRetType);
7257+
return FD;
7258+
}
7259+
72337260
OperatorDecl *FuncDecl::getOperatorDecl() const {
72347261
// Fast-path: Most functions are not operators.
72357262
if (!isOperator()) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,15 +176,8 @@ static FuncDecl *createFuncOrAccessor(ASTContext &ctx, SourceLoc funcLoc,
176176
bodyParams,
177177
resultTy, dc, clangNode);
178178
} else {
179-
TypeLoc resultTypeLoc = resultTy ? TypeLoc::withoutLoc(resultTy) : TypeLoc();
180-
return FuncDecl::create(ctx, /*StaticLoc=*/SourceLoc(),
181-
StaticSpellingKind::None,
182-
funcLoc, name, nameLoc,
183-
/*Async=*/false, /*AsyncLoc=*/SourceLoc(),
184-
throws, /*ThrowsLoc=*/SourceLoc(),
185-
/*GenericParams=*/nullptr,
186-
bodyParams,
187-
resultTypeLoc, dc, clangNode);
179+
return FuncDecl::createImported(ctx, funcLoc, name, nameLoc, throws,
180+
bodyParams, resultTy, dc, clangNode);
188181
}
189182
}
190183

lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -943,20 +943,12 @@ ParserResult<Stmt> Parser::parseStmtDefer() {
943943
// closure's DeclContext.
944944
auto params = ParameterList::createEmpty(Context);
945945
DeclName name(Context, Context.getIdentifier("$defer"), params);
946-
auto tempDecl
947-
= FuncDecl::create(Context,
948-
/*StaticLoc=*/ SourceLoc(),
949-
StaticSpellingKind::None,
950-
/*FuncLoc=*/ SourceLoc(),
951-
name,
952-
/*NameLoc=*/ PreviousLoc,
953-
/*Async=*/ false, /*AsyncLoc=*/ SourceLoc(),
954-
/*Throws=*/ false, /*ThrowsLoc=*/ SourceLoc(),
955-
/*generic params*/ nullptr,
956-
params,
957-
TypeLoc(),
958-
CurDeclContext);
959-
tempDecl->setImplicit();
946+
auto *const tempDecl = FuncDecl::createImplicit(
947+
Context, StaticSpellingKind::None, name, /*NameLoc=*/PreviousLoc,
948+
/*Async=*/false,
949+
/*Throws=*/false,
950+
/*GenericParams*/ nullptr, params, TupleType::getEmpty(Context),
951+
CurDeclContext);
960952
setLocalDiscriminator(tempDecl);
961953
ParserStatus Status;
962954
{

lib/Sema/DerivedConformanceAdditiveArithmetic.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,12 @@ static ValueDecl *deriveMathOperator(DerivedConformance &derived,
190190

191191
auto operatorId = C.getIdentifier(getMathOperatorName(op));
192192
DeclName operatorDeclName(C, operatorId, params);
193-
auto operatorDecl =
194-
FuncDecl::create(C, SourceLoc(), StaticSpellingKind::KeywordStatic,
195-
SourceLoc(), operatorDeclName, SourceLoc(),
196-
/*Async*/ false, SourceLoc(),
197-
/*Throws*/ false, SourceLoc(),
198-
/*GenericParams=*/nullptr, params,
199-
TypeLoc::withoutLoc(selfInterfaceType), parentDC);
200-
operatorDecl->setImplicit();
193+
auto *const operatorDecl = FuncDecl::createImplicit(
194+
C, StaticSpellingKind::KeywordStatic, operatorDeclName,
195+
/*NameLoc=*/SourceLoc(),
196+
/*Async=*/false,
197+
/*Throws=*/false,
198+
/*GenericParams=*/nullptr, params, selfInterfaceType, parentDC);
201199
auto bodySynthesizer = [](AbstractFunctionDecl *funcDecl,
202200
void *ctx) -> std::pair<BraceStmt *, bool> {
203201
auto op = (MathOperator) reinterpret_cast<intptr_t>(ctx);

lib/Sema/DerivedConformanceCodable.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -626,12 +626,11 @@ static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
626626

627627
// Func name: encode(to: Encoder)
628628
DeclName name(C, C.Id_encode, params);
629-
auto *encodeDecl = FuncDecl::create(
630-
C, SourceLoc(), StaticSpellingKind::None, SourceLoc(), name, SourceLoc(),
631-
/*Async*/ false, SourceLoc(),
632-
/*Throws=*/true, SourceLoc(), nullptr, params,
633-
TypeLoc::withoutLoc(returnType), conformanceDC);
634-
encodeDecl->setImplicit();
629+
auto *const encodeDecl = FuncDecl::createImplicit(
630+
C, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
631+
/*Async=*/false,
632+
/*Throws=*/true, /*GenericParams=*/nullptr, params, returnType,
633+
conformanceDC);
635634
encodeDecl->setSynthesized();
636635
encodeDecl->setBodySynthesizer(deriveBodyEncodable_encode);
637636

lib/Sema/DerivedConformanceComparable.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,17 +255,11 @@ deriveComparable_lt(
255255
}
256256

257257
DeclName name(C, generatedIdentifier, params);
258-
auto comparableDecl =
259-
FuncDecl::create(C, /*StaticLoc=*/SourceLoc(),
260-
StaticSpellingKind::KeywordStatic,
261-
/*FuncLoc=*/SourceLoc(), name, /*NameLoc=*/SourceLoc(),
262-
/*Async*/ false, SourceLoc(),
263-
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
264-
/*GenericParams=*/nullptr,
265-
params,
266-
TypeLoc::withoutLoc(boolTy),
267-
parentDC);
268-
comparableDecl->setImplicit();
258+
auto *const comparableDecl = FuncDecl::createImplicit(
259+
C, StaticSpellingKind::KeywordStatic, name, /*NameLoc=*/SourceLoc(),
260+
/*Async=*/false,
261+
/*Throws=*/false,
262+
/*GenericParams=*/nullptr, params, boolTy, parentDC);
269263
comparableDecl->setUserAccessible(false);
270264

271265
// Add the @_implements(Comparable, < (_:_:)) attribute

lib/Sema/DerivedConformanceDifferentiable.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,15 +544,13 @@ static ValueDecl *deriveDifferentiable_method(
544544
ParameterList *params = ParameterList::create(C, {param});
545545

546546
DeclName declName(C, methodName, params);
547-
auto *funcDecl = FuncDecl::create(C, SourceLoc(), StaticSpellingKind::None,
548-
SourceLoc(), declName, SourceLoc(),
549-
/*Async*/ false, SourceLoc(),
550-
/*Throws*/ false, SourceLoc(),
551-
/*GenericParams=*/nullptr, params,
552-
TypeLoc::withoutLoc(returnType), parentDC);
547+
auto *const funcDecl = FuncDecl::createImplicit(
548+
C, StaticSpellingKind::None, declName, /*NameLoc=*/SourceLoc(),
549+
/*Async=*/false,
550+
/*Throws=*/false,
551+
/*GenericParams=*/nullptr, params, returnType, parentDC);
553552
if (!nominal->getSelfClassDecl())
554553
funcDecl->setSelfAccessKind(SelfAccessKind::Mutating);
555-
funcDecl->setImplicit();
556554
funcDecl->setBodySynthesizer(bodySynthesizer.Fn, bodySynthesizer.Context);
557555

558556
funcDecl->setGenericSignature(parentDC->getGenericSignatureOfContext());

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -406,17 +406,11 @@ deriveEquatable_eq(
406406
}
407407

408408
DeclName name(C, generatedIdentifier, params);
409-
auto eqDecl =
410-
FuncDecl::create(C, /*StaticLoc=*/SourceLoc(),
411-
StaticSpellingKind::KeywordStatic,
412-
/*FuncLoc=*/SourceLoc(), name, /*NameLoc=*/SourceLoc(),
413-
/*Async*/ false, SourceLoc(),
414-
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
415-
/*GenericParams=*/nullptr,
416-
params,
417-
TypeLoc::withoutLoc(boolTy),
418-
parentDC);
419-
eqDecl->setImplicit();
409+
auto *const eqDecl = FuncDecl::createImplicit(
410+
C, StaticSpellingKind::KeywordStatic, name, /*NameLoc=*/SourceLoc(),
411+
/*Async=*/false,
412+
/*Throws=*/false,
413+
/*GenericParams=*/nullptr, params, boolTy, parentDC);
420414
eqDecl->setUserAccessible(false);
421415

422416
// Add the @_implements(Equatable, ==(_:_:)) attribute
@@ -549,15 +543,11 @@ deriveHashable_hashInto(
549543

550544
// Func name: hash(into: inout Hasher) -> ()
551545
DeclName name(C, C.Id_hash, params);
552-
auto *hashDecl = FuncDecl::create(C,
553-
SourceLoc(), StaticSpellingKind::None,
554-
SourceLoc(), name, SourceLoc(),
555-
/*Async*/ false, SourceLoc(),
556-
/*Throws=*/false, SourceLoc(),
557-
nullptr, params,
558-
TypeLoc::withoutLoc(returnType),
559-
parentDC);
560-
hashDecl->setImplicit();
546+
auto *const hashDecl = FuncDecl::createImplicit(
547+
C, StaticSpellingKind::None, name, /*NameLoc=*/SourceLoc(),
548+
/*Async=*/false,
549+
/*Throws=*/false,
550+
/*GenericParams=*/nullptr, params, returnType, parentDC);
561551
hashDecl->setBodySynthesizer(bodySynthesizer);
562552

563553
hashDecl->copyFormalAccessFrom(derived.Nominal);

0 commit comments

Comments
 (0)