Skip to content

Finish killing multiple parameter lists #18139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

6 changes: 0 additions & 6 deletions include/swift/AST/AnyFunctionRef.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@ class AnyFunctionRef {
getCaptureInfo().getLocalCaptures(Result);
}

ArrayRef<ParameterList *> getParameterLists() const {
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
return AFD->getParameterLists();
return TheFunction.get<AbstractClosureExpr *>()->getParameterLists();
}

bool hasType() const {
if (auto *AFD = TheFunction.dyn_cast<AbstractFunctionDecl *>())
return AFD->hasInterfaceType();
Expand Down
148 changes: 46 additions & 102 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -4976,6 +4976,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {

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

private:
ParameterList *Params;

protected:
// If a function has a body at all, we have either a parsed body AST node or
// we have saved the end location of the unparsed body.
Expand Down Expand Up @@ -5192,48 +5195,32 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// depending on the function context.
bool argumentNameIsAPIByDefault() const;

/// \brief Returns the "natural" number of argument clauses taken by this
/// function. This value is one for free-standing functions, and two for
/// methods.
unsigned getNumParameterLists() const {
return Bits.AbstractFunctionDecl.HasImplicitSelfDecl ? 2 : 1;
}

/// \brief Returns the parameter pattern(s) for the function definition that
/// determine the parameter names bound in the function body.
///
/// The number of "top-level" elements in this pattern will match the number
/// of argument names in the compound name of the function or constructor.
MutableArrayRef<ParameterList *> getParameterLists();
ArrayRef<const ParameterList *> getParameterLists() const {
auto paramLists =
const_cast<AbstractFunctionDecl *>(this)->getParameterLists();
return ArrayRef<const ParameterList *>(paramLists.data(),paramLists.size());
}
ParameterList *getParameterList(unsigned i) {
return getParameterLists()[i];
}
const ParameterList *getParameterList(unsigned i) const {
return getParameterLists()[i];
}
/// Retrieve the function's parameter list, not including 'self' if present.
ParameterList *getParameters() {
return getParameterLists().back();
return Params;
}
const ParameterList *getParameters() const {
return getParameterLists().back();
return Params;
}

void setParameters(ParamDecl *SelfDecl,
ParameterList *Params);

/// \brief This method returns the implicit 'self' decl.
///
/// Note that some functions don't have an implicit 'self' decl, for example,
/// free functions. In this case nullptr is returned.
bool hasImplicitSelfDecl() const {
return Bits.AbstractFunctionDecl.HasImplicitSelfDecl;
}

ParamDecl **getImplicitSelfDeclStorage();

/// Retrieve the implicit 'self' parameter for methods, or nullptr for free
/// functions.
const ParamDecl *getImplicitSelfDecl() const {
return const_cast<AbstractFunctionDecl*>(this)->getImplicitSelfDecl();
}
ParamDecl *getImplicitSelfDecl();
ParamDecl *getImplicitSelfDecl() {
auto **selfDecl = getImplicitSelfDeclStorage();
return (selfDecl == nullptr ? nullptr : *selfDecl);
}

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

protected:
ParameterList **getParameterListBuffer(); // defined inline below
ParameterList * const *getParameterListBuffer() const {
return const_cast<FuncDecl*>(this)->getParameterListBuffer();
}

FuncDecl(DeclKind Kind,
SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
SourceLoc FuncLoc,
Expand Down Expand Up @@ -5426,30 +5408,7 @@ class FuncDecl : public AbstractFunctionDecl {
void setSelfAccessKind(SelfAccessKind mod) {
Bits.FuncDecl.SelfAccess = static_cast<unsigned>(mod);
}

/// \brief Returns the parameter lists(s) for the function definition.
///
/// The number of "top-level" elements will match the number of argument names
/// in the compound name of the function or constructor.
MutableArrayRef<ParameterList *> getParameterLists() {
return {getParameterListBuffer(), getNumParameterLists()};
}
ArrayRef<const ParameterList *> getParameterLists() const {
return {getParameterListBuffer(), getNumParameterLists()};
}
ParameterList *getParameterList(unsigned i) {
return getParameterLists()[i];
}
const ParameterList *getParameterList(unsigned i) const {
return getParameterLists()[i];
}
ParameterList *getParameters() {
return getParameterLists().back();
}
const ParameterList *getParameters() const {
return getParameterLists().back();
}


/// \returns true if this is non-mutating due to applying a 'mutating'
/// attribute. For example a "mutating set" accessor.
bool isExplicitNonMutating() const;
Expand Down Expand Up @@ -5504,7 +5463,9 @@ class FuncDecl : public AbstractFunctionDecl {
void getLocalCaptures(SmallVectorImpl<CapturedValue> &Result) const {
return getCaptureInfo().getLocalCaptures(Result);
}


ParamDecl **getImplicitSelfDeclStorage();

/// Get the supertype method this method overrides, if any.
FuncDecl *getOverriddenDecl() const {
return cast_or_null<FuncDecl>(AbstractFunctionDecl::getOverriddenDecl());
Expand Down Expand Up @@ -5672,14 +5633,6 @@ class AccessorDecl final : public FuncDecl {
}
};

inline ParameterList **FuncDecl::getParameterListBuffer() {
if (!isa<AccessorDecl>(this)) {
assert(getKind() == DeclKind::Func && "no new kinds of functions");
return reinterpret_cast<ParameterList**>(this+1);
}
return reinterpret_cast<ParameterList**>(static_cast<AccessorDecl*>(this)+1);
}

inline AccessorDecl *
AbstractStorageDecl::AccessorRecord::getAccessor(AccessorKind kind) const {
if (auto optIndex = AccessorIndices[unsigned(kind)]) {
Expand Down Expand Up @@ -5903,7 +5856,7 @@ class ConstructorDecl : public AbstractFunctionDecl {
/// The location of the '!' or '?' for a failable initializer.
SourceLoc FailabilityLoc;

ParameterList *ParameterLists[2];
ParamDecl *SelfDecl;

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

MutableArrayRef<ParameterList *> getParameterLists() {
return { ParameterLists, 2 };
}
ArrayRef<const ParameterList *> getParameterLists() const {
return AbstractFunctionDecl::getParameterLists();
}
ParameterList *getParameterList(unsigned i) {
return getParameterLists()[i];
}
const ParameterList *getParameterList(unsigned i) const {
return getParameterLists()[i];
}

/// Returns the normal parameters to the initializer, not including self.
ParameterList *getParameters() { return ParameterLists[1]; }

/// Returns the normal parameters to the initializer, not including self.
const ParameterList *getParameters() const { return ParameterLists[1]; }

ParamDecl **getImplicitSelfDeclStorage() { return &SelfDecl; }

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

public:
DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
DeclContext *Parent);

MutableArrayRef<ParameterList *> getParameterLists() {
return { ParameterLists, 2 };
}
ArrayRef<const ParameterList *> getParameterLists() const {
return { ParameterLists, 2 };
}

ParamDecl **getImplicitSelfDeclStorage() { return &SelfDecl; }

SourceLoc getDestructorLoc() const { return getNameLoc(); }
SourceLoc getStartLoc() const { return getDestructorLoc(); }
SourceRange getSourceRange() const;
Expand Down Expand Up @@ -6569,18 +6499,32 @@ inline bool AbstractStorageDecl::isStatic() const {
return false;
}

inline MutableArrayRef<ParameterList *>
AbstractFunctionDecl::getParameterLists() {
/// Constructors and destructors always have a 'self' parameter,
/// which is stored in an instance member. Functions only have a
/// 'self' if they are declared inside of a nominal type or extension,
/// in which case we tail-allocate storage for it.
inline ParamDecl **AbstractFunctionDecl::getImplicitSelfDeclStorage() {
switch (getKind()) {
default: llvm_unreachable("Unknown AbstractFunctionDecl!");
case DeclKind::Constructor:
return cast<ConstructorDecl>(this)->getParameterLists();
return cast<ConstructorDecl>(this)->getImplicitSelfDeclStorage();
case DeclKind::Destructor:
return cast<DestructorDecl>(this)->getParameterLists();
return cast<DestructorDecl>(this)->getImplicitSelfDeclStorage();
case DeclKind::Func:
case DeclKind::Accessor:
return cast<FuncDecl>(this)->getParameterLists();
return cast<FuncDecl>(this)->getImplicitSelfDeclStorage();
}
}

inline ParamDecl **FuncDecl::getImplicitSelfDeclStorage() {
if (!hasImplicitSelfDecl())
return nullptr;

if (!isa<AccessorDecl>(this)) {
assert(getKind() == DeclKind::Func && "no new kinds of functions");
return reinterpret_cast<ParamDecl **>(this+1);
}
return reinterpret_cast<ParamDecl **>(static_cast<AccessorDecl*>(this)+1);
}

inline DeclIterator &DeclIterator::operator++() {
Expand Down
8 changes: 0 additions & 8 deletions include/swift/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -3447,14 +3447,6 @@ class AbstractClosureExpr : public DeclContext, public Expr {
}
enum : unsigned { InvalidDiscriminator = 0xFFFF };

ArrayRef<ParameterList *> getParameterLists() {
return parameterList ? parameterList : ArrayRef<ParameterList *>();
}

ArrayRef<const ParameterList *> getParameterLists() const {
return parameterList ? parameterList : ArrayRef<const ParameterList *>();
}

/// \brief Retrieve the result type of this closure.
Type getResultType(llvm::function_ref<Type(const Expr *)> getType =
[](const Expr *E) -> Type {
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/Initializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class DefaultArgumentInitializer : public Initializer {
/// Change the parent of this context. This is necessary because
/// the function signature is parsed before the function
/// declaration/expression itself is built.
void changeFunction(DeclContext *parent, ArrayRef<ParameterList *> paramLists);
void changeFunction(DeclContext *parent, ParameterList *paramLists);

static bool classof(const DeclContext *DC) {
if (auto init = dyn_cast<Initializer>(DC))
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1009,10 +1009,10 @@ class Parser {

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

DefaultArgumentInfo(bool inTypeContext) {
NextIndex = inTypeContext ? 1 : 0;
DefaultArgumentInfo() {
NextIndex = 0;
HasDefaultArgument = false;
}
};
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2333,7 +2333,7 @@ void PrintAST::printFunctionParameters(AbstractFunctionDecl *AFD) {
auto curTy = AFD->hasInterfaceType() ? AFD->getInterfaceType() : nullptr;

// Skip over the implicit 'self'.
if (AFD->getImplicitSelfDecl()) {
if (AFD->hasImplicitSelfDecl()) {
if (curTy)
if (auto funTy = curTy->getAs<AnyFunctionType>())
curTy = funTy->getResult();
Expand Down
Loading