Skip to content

Commit 9d2b582

Browse files
authored
Merge pull request #18139 from slavapestov/finish-killing-multiple-parameter-lists
Finish killing multiple parameter lists
2 parents 183a1ae + 90cd772 commit 9d2b582

32 files changed

+225
-311
lines changed

include/swift/AST/AnyFunctionRef.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,6 @@ class AnyFunctionRef {
6262
getCaptureInfo().getLocalCaptures(Result);
6363
}
6464

65-
ArrayRef<ParameterList *> getParameterLists() const {
66-
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
67-
return AFD->getParameterLists();
68-
return TheFunction.get<AbstractClosureExpr *>()->getParameterLists();
69-
}
70-
7165
bool hasType() const {
7266
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
7367
return AFD->hasInterfaceType();

include/swift/AST/Decl.h

Lines changed: 46 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -4976,6 +4976,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
49764976

49774977
using BodySynthesizer = void (*)(AbstractFunctionDecl *);
49784978

4979+
private:
4980+
ParameterList *Params;
4981+
49794982
protected:
49804983
// If a function has a body at all, we have either a parsed body AST node or
49814984
// we have saved the end location of the unparsed body.
@@ -5192,48 +5195,32 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
51925195
/// depending on the function context.
51935196
bool argumentNameIsAPIByDefault() const;
51945197

5195-
/// \brief Returns the "natural" number of argument clauses taken by this
5196-
/// function. This value is one for free-standing functions, and two for
5197-
/// methods.
5198-
unsigned getNumParameterLists() const {
5199-
return Bits.AbstractFunctionDecl.HasImplicitSelfDecl ? 2 : 1;
5200-
}
5201-
5202-
/// \brief Returns the parameter pattern(s) for the function definition that
5203-
/// determine the parameter names bound in the function body.
5204-
///
5205-
/// The number of "top-level" elements in this pattern will match the number
5206-
/// of argument names in the compound name of the function or constructor.
5207-
MutableArrayRef<ParameterList *> getParameterLists();
5208-
ArrayRef<const ParameterList *> getParameterLists() const {
5209-
auto paramLists =
5210-
const_cast<AbstractFunctionDecl *>(this)->getParameterLists();
5211-
return ArrayRef<const ParameterList *>(paramLists.data(),paramLists.size());
5212-
}
5213-
ParameterList *getParameterList(unsigned i) {
5214-
return getParameterLists()[i];
5215-
}
5216-
const ParameterList *getParameterList(unsigned i) const {
5217-
return getParameterLists()[i];
5218-
}
5198+
/// Retrieve the function's parameter list, not including 'self' if present.
52195199
ParameterList *getParameters() {
5220-
return getParameterLists().back();
5200+
return Params;
52215201
}
52225202
const ParameterList *getParameters() const {
5223-
return getParameterLists().back();
5203+
return Params;
52245204
}
52255205

52265206
void setParameters(ParamDecl *SelfDecl,
52275207
ParameterList *Params);
52285208

5229-
/// \brief This method returns the implicit 'self' decl.
5230-
///
5231-
/// Note that some functions don't have an implicit 'self' decl, for example,
5232-
/// free functions. In this case nullptr is returned.
5209+
bool hasImplicitSelfDecl() const {
5210+
return Bits.AbstractFunctionDecl.HasImplicitSelfDecl;
5211+
}
5212+
5213+
ParamDecl **getImplicitSelfDeclStorage();
5214+
5215+
/// Retrieve the implicit 'self' parameter for methods, or nullptr for free
5216+
/// functions.
52335217
const ParamDecl *getImplicitSelfDecl() const {
52345218
return const_cast<AbstractFunctionDecl*>(this)->getImplicitSelfDecl();
52355219
}
5236-
ParamDecl *getImplicitSelfDecl();
5220+
ParamDecl *getImplicitSelfDecl() {
5221+
auto **selfDecl = getImplicitSelfDeclStorage();
5222+
return (selfDecl == nullptr ? nullptr : *selfDecl);
5223+
}
52375224

52385225
/// Retrieve the declaration that this method overrides, if any.
52395226
AbstractFunctionDecl *getOverriddenDecl() const {
@@ -5330,11 +5317,6 @@ class FuncDecl : public AbstractFunctionDecl {
53305317
OperatorDecl *Operator = nullptr;
53315318

53325319
protected:
5333-
ParameterList **getParameterListBuffer(); // defined inline below
5334-
ParameterList * const *getParameterListBuffer() const {
5335-
return const_cast<FuncDecl*>(this)->getParameterListBuffer();
5336-
}
5337-
53385320
FuncDecl(DeclKind Kind,
53395321
SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
53405322
SourceLoc FuncLoc,
@@ -5426,30 +5408,7 @@ class FuncDecl : public AbstractFunctionDecl {
54265408
void setSelfAccessKind(SelfAccessKind mod) {
54275409
Bits.FuncDecl.SelfAccess = static_cast<unsigned>(mod);
54285410
}
5429-
5430-
/// \brief Returns the parameter lists(s) for the function definition.
5431-
///
5432-
/// The number of "top-level" elements will match the number of argument names
5433-
/// in the compound name of the function or constructor.
5434-
MutableArrayRef<ParameterList *> getParameterLists() {
5435-
return {getParameterListBuffer(), getNumParameterLists()};
5436-
}
5437-
ArrayRef<const ParameterList *> getParameterLists() const {
5438-
return {getParameterListBuffer(), getNumParameterLists()};
5439-
}
5440-
ParameterList *getParameterList(unsigned i) {
5441-
return getParameterLists()[i];
5442-
}
5443-
const ParameterList *getParameterList(unsigned i) const {
5444-
return getParameterLists()[i];
5445-
}
5446-
ParameterList *getParameters() {
5447-
return getParameterLists().back();
5448-
}
5449-
const ParameterList *getParameters() const {
5450-
return getParameterLists().back();
5451-
}
5452-
5411+
54535412
/// \returns true if this is non-mutating due to applying a 'mutating'
54545413
/// attribute. For example a "mutating set" accessor.
54555414
bool isExplicitNonMutating() const;
@@ -5504,7 +5463,9 @@ class FuncDecl : public AbstractFunctionDecl {
55045463
void getLocalCaptures(SmallVectorImpl<CapturedValue> &Result) const {
55055464
return getCaptureInfo().getLocalCaptures(Result);
55065465
}
5507-
5466+
5467+
ParamDecl **getImplicitSelfDeclStorage();
5468+
55085469
/// Get the supertype method this method overrides, if any.
55095470
FuncDecl *getOverriddenDecl() const {
55105471
return cast_or_null<FuncDecl>(AbstractFunctionDecl::getOverriddenDecl());
@@ -5672,14 +5633,6 @@ class AccessorDecl final : public FuncDecl {
56725633
}
56735634
};
56745635

5675-
inline ParameterList **FuncDecl::getParameterListBuffer() {
5676-
if (!isa<AccessorDecl>(this)) {
5677-
assert(getKind() == DeclKind::Func && "no new kinds of functions");
5678-
return reinterpret_cast<ParameterList**>(this+1);
5679-
}
5680-
return reinterpret_cast<ParameterList**>(static_cast<AccessorDecl*>(this)+1);
5681-
}
5682-
56835636
inline AccessorDecl *
56845637
AbstractStorageDecl::AccessorRecord::getAccessor(AccessorKind kind) const {
56855638
if (auto optIndex = AccessorIndices[unsigned(kind)]) {
@@ -5903,7 +5856,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
59035856
/// The location of the '!' or '?' for a failable initializer.
59045857
SourceLoc FailabilityLoc;
59055858

5906-
ParameterList *ParameterLists[2];
5859+
ParamDecl *SelfDecl;
59075860

59085861
/// The interface type of the initializing constructor.
59095862
Type InitializerInterfaceType;
@@ -5939,25 +5892,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
59395892
Expr *getSuperInitCall() { return CallToSuperInit; }
59405893
void setSuperInitCall(Expr *CallExpr) { CallToSuperInit = CallExpr; }
59415894

5942-
MutableArrayRef<ParameterList *> getParameterLists() {
5943-
return { ParameterLists, 2 };
5944-
}
5945-
ArrayRef<const ParameterList *> getParameterLists() const {
5946-
return AbstractFunctionDecl::getParameterLists();
5947-
}
5948-
ParameterList *getParameterList(unsigned i) {
5949-
return getParameterLists()[i];
5950-
}
5951-
const ParameterList *getParameterList(unsigned i) const {
5952-
return getParameterLists()[i];
5953-
}
5954-
5955-
/// Returns the normal parameters to the initializer, not including self.
5956-
ParameterList *getParameters() { return ParameterLists[1]; }
5957-
5958-
/// Returns the normal parameters to the initializer, not including self.
5959-
const ParameterList *getParameters() const { return ParameterLists[1]; }
5960-
5895+
ParamDecl **getImplicitSelfDeclStorage() { return &SelfDecl; }
59615896

59625897
/// Specifies the kind of initialization call performed within the body
59635898
/// of the constructor, e.g., self.init or super.init.
@@ -6103,19 +6038,14 @@ class ConstructorDecl : public AbstractFunctionDecl {
61036038
/// }
61046039
/// \endcode
61056040
class DestructorDecl : public AbstractFunctionDecl {
6106-
ParameterList *ParameterLists[2];
6041+
ParamDecl *SelfDecl;
61076042

61086043
public:
61096044
DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
61106045
DeclContext *Parent);
61116046

6112-
MutableArrayRef<ParameterList *> getParameterLists() {
6113-
return { ParameterLists, 2 };
6114-
}
6115-
ArrayRef<const ParameterList *> getParameterLists() const {
6116-
return { ParameterLists, 2 };
6117-
}
6118-
6047+
ParamDecl **getImplicitSelfDeclStorage() { return &SelfDecl; }
6048+
61196049
SourceLoc getDestructorLoc() const { return getNameLoc(); }
61206050
SourceLoc getStartLoc() const { return getDestructorLoc(); }
61216051
SourceRange getSourceRange() const;
@@ -6569,18 +6499,32 @@ inline bool AbstractStorageDecl::isStatic() const {
65696499
return false;
65706500
}
65716501

6572-
inline MutableArrayRef<ParameterList *>
6573-
AbstractFunctionDecl::getParameterLists() {
6502+
/// Constructors and destructors always have a 'self' parameter,
6503+
/// which is stored in an instance member. Functions only have a
6504+
/// 'self' if they are declared inside of a nominal type or extension,
6505+
/// in which case we tail-allocate storage for it.
6506+
inline ParamDecl **AbstractFunctionDecl::getImplicitSelfDeclStorage() {
65746507
switch (getKind()) {
65756508
default: llvm_unreachable("Unknown AbstractFunctionDecl!");
65766509
case DeclKind::Constructor:
6577-
return cast<ConstructorDecl>(this)->getParameterLists();
6510+
return cast<ConstructorDecl>(this)->getImplicitSelfDeclStorage();
65786511
case DeclKind::Destructor:
6579-
return cast<DestructorDecl>(this)->getParameterLists();
6512+
return cast<DestructorDecl>(this)->getImplicitSelfDeclStorage();
65806513
case DeclKind::Func:
65816514
case DeclKind::Accessor:
6582-
return cast<FuncDecl>(this)->getParameterLists();
6515+
return cast<FuncDecl>(this)->getImplicitSelfDeclStorage();
6516+
}
6517+
}
6518+
6519+
inline ParamDecl **FuncDecl::getImplicitSelfDeclStorage() {
6520+
if (!hasImplicitSelfDecl())
6521+
return nullptr;
6522+
6523+
if (!isa<AccessorDecl>(this)) {
6524+
assert(getKind() == DeclKind::Func && "no new kinds of functions");
6525+
return reinterpret_cast<ParamDecl **>(this+1);
65836526
}
6527+
return reinterpret_cast<ParamDecl **>(static_cast<AccessorDecl*>(this)+1);
65846528
}
65856529

65866530
inline DeclIterator &DeclIterator::operator++() {

include/swift/AST/Expr.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,14 +3447,6 @@ class AbstractClosureExpr : public DeclContext, public Expr {
34473447
}
34483448
enum : unsigned { InvalidDiscriminator = 0xFFFF };
34493449

3450-
ArrayRef<ParameterList *> getParameterLists() {
3451-
return parameterList ? parameterList : ArrayRef<ParameterList *>();
3452-
}
3453-
3454-
ArrayRef<const ParameterList *> getParameterLists() const {
3455-
return parameterList ? parameterList : ArrayRef<const ParameterList *>();
3456-
}
3457-
34583450
/// \brief Retrieve the result type of this closure.
34593451
Type getResultType(llvm::function_ref<Type(const Expr *)> getType =
34603452
[](const Expr *E) -> Type {

include/swift/AST/Initializer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class DefaultArgumentInitializer : public Initializer {
167167
/// Change the parent of this context. This is necessary because
168168
/// the function signature is parsed before the function
169169
/// declaration/expression itself is built.
170-
void changeFunction(DeclContext *parent, ArrayRef<ParameterList *> paramLists);
170+
void changeFunction(DeclContext *parent, ParameterList *paramLists);
171171

172172
static bool classof(const DeclContext *DC) {
173173
if (auto init = dyn_cast<Initializer>(DC))

include/swift/Parse/Parser.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,10 +1009,10 @@ class Parser {
10091009

10101010
/// Set the parsed context for all the initializers to the given
10111011
/// function.
1012-
void setFunctionContext(DeclContext *DC, ArrayRef<ParameterList *> paramList);
1012+
void setFunctionContext(DeclContext *DC, ParameterList *paramList);
10131013

1014-
DefaultArgumentInfo(bool inTypeContext) {
1015-
NextIndex = inTypeContext ? 1 : 0;
1014+
DefaultArgumentInfo() {
1015+
NextIndex = 0;
10161016
HasDefaultArgument = false;
10171017
}
10181018
};

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,7 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
23332333
auto curTy = AFD->hasInterfaceType() ? AFD->getInterfaceType() : nullptr;
23342334

23352335
// Skip over the implicit 'self'.
2336-
if (AFD->getImplicitSelfDecl()) {
2336+
if (AFD->hasImplicitSelfDecl()) {
23372337
if (curTy)
23382338
if (auto funTy = curTy->getAs<AnyFunctionType>())
23392339
curTy = funTy->getResult();

0 commit comments

Comments
 (0)