Skip to content

Commit 19104c0

Browse files
authored
Merge pull request #18128 from slavapestov/wean-func-ctors-off-multiple-parameter-lists
Wean AbstractFunctionDecl constructors off multiple parameter lists
2 parents dc7a51e + 5ce44bf commit 19104c0

21 files changed

+239
-325
lines changed

include/swift/AST/Decl.h

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -380,15 +380,15 @@ class alignas(1 << DeclAlignInBits) Decl {
380380
DefaultArgumentResilienceExpansion : 1
381381
);
382382

383-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+5+1+1+1+1+1+1,
383+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1+1,
384384
/// \see AbstractFunctionDecl::BodyKind
385385
BodyKind : 3,
386386

387387
/// Import as member status.
388388
IAMStatus : 8,
389389

390-
/// Number of curried parameter lists.
391-
NumParameterLists : 5,
390+
/// Whether the function has an implicit 'self' parameter.
391+
HasImplicitSelfDecl : 1,
392392

393393
/// Whether we are overridden later.
394394
Overridden : 1,
@@ -5001,24 +5001,21 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
50015001

50025002
AbstractFunctionDecl(DeclKind Kind, DeclContext *Parent, DeclName Name,
50035003
SourceLoc NameLoc, bool Throws, SourceLoc ThrowsLoc,
5004-
unsigned NumParameterLists,
5004+
bool HasImplicitSelfDecl,
50055005
GenericParamList *GenericParams)
50065006
: GenericContext(DeclContextKind::AbstractFunctionDecl, Parent),
50075007
ValueDecl(Kind, Parent, Name, NameLoc),
50085008
Body(nullptr), ThrowsLoc(ThrowsLoc) {
50095009
setBodyKind(BodyKind::None);
50105010
setGenericParams(GenericParams);
5011-
Bits.AbstractFunctionDecl.NumParameterLists = NumParameterLists;
5011+
Bits.AbstractFunctionDecl.HasImplicitSelfDecl = HasImplicitSelfDecl;
50125012
Bits.AbstractFunctionDecl.Overridden = false;
50135013
Bits.AbstractFunctionDecl.Throws = Throws;
50145014
Bits.AbstractFunctionDecl.NeedsNewVTableEntry = false;
50155015
Bits.AbstractFunctionDecl.HasComputedNeedsNewVTableEntry = false;
50165016
Bits.AbstractFunctionDecl.DefaultArgumentResilienceExpansion =
50175017
unsigned(ResilienceExpansion::Maximal);
50185018
Bits.AbstractFunctionDecl.Synthesized = false;
5019-
5020-
// Verify no bitfield truncation.
5021-
assert(Bits.AbstractFunctionDecl.NumParameterLists == NumParameterLists);
50225019
}
50235020

50245021
void setBodyKind(BodyKind K) {
@@ -5064,7 +5061,6 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
50645061
Bits.AbstractFunctionDecl.IAMStatus = newValue.getRawValue();
50655062
}
50665063

5067-
public:
50685064
/// Retrieve the location of the 'throws' keyword, if present.
50695065
SourceLoc getThrowsLoc() const { return ThrowsLoc; }
50705066

@@ -5200,7 +5196,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
52005196
/// function. This value is one for free-standing functions, and two for
52015197
/// methods.
52025198
unsigned getNumParameterLists() const {
5203-
return Bits.AbstractFunctionDecl.NumParameterLists;
5199+
return Bits.AbstractFunctionDecl.HasImplicitSelfDecl ? 2 : 1;
52045200
}
52055201

52065202
/// \brief Returns the parameter pattern(s) for the function definition that
@@ -5227,6 +5223,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
52275223
return getParameterLists().back();
52285224
}
52295225

5226+
void setParameters(ParamDecl *SelfDecl,
5227+
ParameterList *Params);
5228+
52305229
/// \brief This method returns the implicit 'self' decl.
52315230
///
52325231
/// Note that some functions don't have an implicit 'self' decl, for example,
@@ -5341,19 +5340,18 @@ class FuncDecl : public AbstractFunctionDecl {
53415340
SourceLoc FuncLoc,
53425341
DeclName Name, SourceLoc NameLoc,
53435342
bool Throws, SourceLoc ThrowsLoc,
5344-
unsigned NumParameterLists,
5343+
bool HasImplicitSelfDecl,
53455344
GenericParamList *GenericParams, DeclContext *Parent)
53465345
: AbstractFunctionDecl(Kind, Parent,
53475346
Name, NameLoc,
53485347
Throws, ThrowsLoc,
5349-
NumParameterLists, GenericParams),
5348+
HasImplicitSelfDecl, GenericParams),
53505349
StaticLoc(StaticLoc), FuncLoc(FuncLoc) {
53515350
assert(!Name.getBaseName().isSpecial());
53525351

53535352
Bits.FuncDecl.IsStatic =
53545353
StaticLoc.isValid() || StaticSpelling != StaticSpellingKind::None;
53555354
Bits.FuncDecl.StaticSpelling = static_cast<unsigned>(StaticSpelling);
5356-
assert(NumParameterLists > 0 && "Must have at least an empty tuple arg");
53575355

53585356
Bits.FuncDecl.HasDynamicSelf = false;
53595357
Bits.FuncDecl.ForcedStaticDispatch = false;
@@ -5367,7 +5365,7 @@ class FuncDecl : public AbstractFunctionDecl {
53675365
DeclName Name, SourceLoc NameLoc,
53685366
bool Throws, SourceLoc ThrowsLoc,
53695367
GenericParamList *GenericParams,
5370-
unsigned NumParameterLists,
5368+
bool HasImplicitSelfDecl,
53715369
DeclContext *Parent,
53725370
ClangNode ClangN);
53735371

@@ -5379,7 +5377,7 @@ class FuncDecl : public AbstractFunctionDecl {
53795377
DeclName Name, SourceLoc NameLoc,
53805378
bool Throws, SourceLoc ThrowsLoc,
53815379
GenericParamList *GenericParams,
5382-
unsigned NumParameterLists,
5380+
bool HasImplicitSelfDecl,
53835381
DeclContext *Parent);
53845382

53855383
static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
@@ -5388,7 +5386,8 @@ class FuncDecl : public AbstractFunctionDecl {
53885386
DeclName Name, SourceLoc NameLoc,
53895387
bool Throws, SourceLoc ThrowsLoc,
53905388
GenericParamList *GenericParams,
5391-
ArrayRef<ParameterList *> ParameterLists,
5389+
ParamDecl *SelfDecl,
5390+
ParameterList *ParameterList,
53925391
TypeLoc FnRetType, DeclContext *Parent,
53935392
ClangNode ClangN = ClangNode());
53945393

@@ -5455,9 +5454,6 @@ class FuncDecl : public AbstractFunctionDecl {
54555454
/// attribute. For example a "mutating set" accessor.
54565455
bool isExplicitNonMutating() const;
54575456

5458-
void setDeserializedSignature(ArrayRef<ParameterList *> ParameterLists,
5459-
TypeLoc FnRetType);
5460-
54615457
SourceLoc getStaticLoc() const { return StaticLoc; }
54625458
SourceLoc getFuncLoc() const { return FuncLoc; }
54635459

@@ -5594,7 +5590,7 @@ class AccessorDecl final : public FuncDecl {
55945590
SourceLoc staticLoc,
55955591
StaticSpellingKind staticSpelling,
55965592
bool throws, SourceLoc throwsLoc,
5597-
unsigned numParameterLists,
5593+
bool hasImplicitSelfDecl,
55985594
GenericParamList *genericParams,
55995595
DeclContext *parent,
56005596
ClangNode clangNode);
@@ -5610,7 +5606,7 @@ class AccessorDecl final : public FuncDecl {
56105606
StaticSpellingKind staticSpelling,
56115607
bool throws, SourceLoc throwsLoc,
56125608
GenericParamList *genericParams,
5613-
unsigned numParameterLists,
5609+
bool hasImplicitSelfDecl,
56145610
DeclContext *parent);
56155611

56165612
static AccessorDecl *create(ASTContext &ctx, SourceLoc declLoc,
@@ -5622,7 +5618,8 @@ class AccessorDecl final : public FuncDecl {
56225618
StaticSpellingKind staticSpelling,
56235619
bool throws, SourceLoc throwsLoc,
56245620
GenericParamList *genericParams,
5625-
ArrayRef<ParameterList *> parameterLists,
5621+
ParamDecl *selfDecl,
5622+
ParameterList *parameterList,
56265623
TypeLoc fnRetType, DeclContext *parent,
56275624
ClangNode clangNode = ClangNode());
56285625

@@ -5923,8 +5920,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
59235920
GenericParamList *GenericParams,
59245921
DeclContext *Parent);
59255922

5926-
void setParameterLists(ParamDecl *selfParam, ParameterList *bodyParams);
5927-
59285923
SourceLoc getConstructorLoc() const { return getNameLoc(); }
59295924
SourceLoc getStartLoc() const { return getConstructorLoc(); }
59305925
SourceRange getSourceRange() const;
@@ -6114,8 +6109,6 @@ class DestructorDecl : public AbstractFunctionDecl {
61146109
DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
61156110
DeclContext *Parent);
61166111

6117-
void setSelfDecl(ParamDecl *selfDecl);
6118-
61196112
MutableArrayRef<ParameterList *> getParameterLists() {
61206113
return { ParameterLists, 2 };
61216114
}

include/swift/AST/ParameterList.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,6 @@ class alignas(ParamDecl *) ParameterList final :
6565
static ParameterList *createWithoutLoc(ParamDecl *decl) {
6666
return create(decl->getASTContext(), decl);
6767
}
68-
69-
/// Create an implicit 'self' decl for a method in the specified decl context.
70-
/// If 'static' is true, then this is self for a static method in the type.
71-
///
72-
/// Note that this decl is created, but it is returned with an incorrect
73-
/// DeclContext that needs to be set correctly. This is automatically handled
74-
/// when a function is created with this as part of its argument list.
75-
///
76-
static ParameterList *createUnboundSelf(SourceLoc loc, DeclContext *DC);
77-
78-
/// Create an implicit 'self' decl for a method in the specified decl context.
79-
/// If 'static' is true, then this is self for a static method in the type.
80-
///
81-
/// Note that this decl is created, but it is returned with an incorrect
82-
/// DeclContext that needs to be set correctly. This is automatically handled
83-
/// when a function is created with this as part of its argument list.
84-
static ParameterList *createSelf(SourceLoc loc, DeclContext *DC,
85-
bool isStatic = false,
86-
bool isInOut = false);
8768

8869
SourceLoc getLParenLoc() const { return LParenLoc; }
8970
SourceLoc getRParenLoc() const { return RParenLoc; }

include/swift/Parse/Parser.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,12 +1102,12 @@ class Parser {
11021102
DefaultArgumentInfo *defaultArgs = nullptr);
11031103

11041104
ParserStatus parseFunctionArguments(SmallVectorImpl<Identifier> &NamePieces,
1105-
SmallVectorImpl<ParameterList*> &BodyParams,
1105+
ParameterList *&BodyParams,
11061106
ParameterContextKind paramContext,
11071107
DefaultArgumentInfo &defaultArgs);
11081108
ParserStatus parseFunctionSignature(Identifier functionName,
11091109
DeclName &fullName,
1110-
SmallVectorImpl<ParameterList *> &bodyParams,
1110+
ParameterList *&bodyParams,
11111111
DefaultArgumentInfo &defaultArgs,
11121112
SourceLoc &throws,
11131113
bool &rethrows,

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t VERSION_MINOR = 427; // Invalid conformances
58+
const uint16_t VERSION_MINOR = 428; // Removing multiple parameter lists
5959

6060
using DeclIDField = BCFixed<31>;
6161

lib/AST/ASTVerifier.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,6 +2828,17 @@ class Verifier : public ASTWalker {
28282828
abort();
28292829
}
28302830

2831+
// Check that type members have an interface type of the form
2832+
// (Self) -> (Args...) -> Result.
2833+
if (AFD->getImplicitSelfDecl()) {
2834+
if (!interfaceTy->castTo<AnyFunctionType>()
2835+
->getResult()->is<FunctionType>()) {
2836+
Out << "Interface type of method must return a function";
2837+
interfaceTy->dump(Out);
2838+
abort();
2839+
}
2840+
}
2841+
28312842
// Throwing @objc methods must have a foreign error convention.
28322843
if (AFD->isObjC() &&
28332844
static_cast<bool>(AFD->getForeignErrorConvention())

lib/AST/Builtins.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
178178
Name, /*NameLoc=*/SourceLoc(),
179179
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
180180
/*GenericParams=*/nullptr,
181+
/*SelfDecl=*/nullptr,
181182
paramList,
182183
TypeLoc::withoutLoc(ResType), DC);
183184
FD->setInterfaceType(FnType);
@@ -242,7 +243,9 @@ getBuiltinGenericFunction(Identifier Id,
242243
/*FuncLoc=*/SourceLoc(),
243244
Name, /*NameLoc=*/SourceLoc(),
244245
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
245-
GenericParams, paramList,
246+
GenericParams,
247+
/*SelfDecl=*/nullptr,
248+
paramList,
246249
TypeLoc::withoutLoc(ResBodyType), DC);
247250

248251
func->setInterfaceType(InterfaceType);

0 commit comments

Comments
 (0)