Skip to content

Wean AbstractFunctionDecl constructors off multiple parameter lists #18128

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 19 additions & 26 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,15 @@ class alignas(1 << DeclAlignInBits) Decl {
DefaultArgumentResilienceExpansion : 1
);

SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+5+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1+1,
/// \see AbstractFunctionDecl::BodyKind
BodyKind : 3,

/// Import as member status.
IAMStatus : 8,

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

/// Whether we are overridden later.
Overridden : 1,
Expand Down Expand Up @@ -5001,24 +5001,21 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {

AbstractFunctionDecl(DeclKind Kind, DeclContext *Parent, DeclName Name,
SourceLoc NameLoc, bool Throws, SourceLoc ThrowsLoc,
unsigned NumParameterLists,
bool HasImplicitSelfDecl,
GenericParamList *GenericParams)
: GenericContext(DeclContextKind::AbstractFunctionDecl, Parent),
ValueDecl(Kind, Parent, Name, NameLoc),
Body(nullptr), ThrowsLoc(ThrowsLoc) {
setBodyKind(BodyKind::None);
setGenericParams(GenericParams);
Bits.AbstractFunctionDecl.NumParameterLists = NumParameterLists;
Bits.AbstractFunctionDecl.HasImplicitSelfDecl = HasImplicitSelfDecl;
Bits.AbstractFunctionDecl.Overridden = false;
Bits.AbstractFunctionDecl.Throws = Throws;
Bits.AbstractFunctionDecl.NeedsNewVTableEntry = false;
Bits.AbstractFunctionDecl.HasComputedNeedsNewVTableEntry = false;
Bits.AbstractFunctionDecl.DefaultArgumentResilienceExpansion =
unsigned(ResilienceExpansion::Maximal);
Bits.AbstractFunctionDecl.Synthesized = false;

// Verify no bitfield truncation.
assert(Bits.AbstractFunctionDecl.NumParameterLists == NumParameterLists);
}

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

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

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

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

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,
Expand Down Expand Up @@ -5341,19 +5340,18 @@ class FuncDecl : public AbstractFunctionDecl {
SourceLoc FuncLoc,
DeclName Name, SourceLoc NameLoc,
bool Throws, SourceLoc ThrowsLoc,
unsigned NumParameterLists,
bool HasImplicitSelfDecl,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this boolean ever distinct from Parent->isTypeContext()?

GenericParamList *GenericParams, DeclContext *Parent)
: AbstractFunctionDecl(Kind, Parent,
Name, NameLoc,
Throws, ThrowsLoc,
NumParameterLists, GenericParams),
HasImplicitSelfDecl, GenericParams),
StaticLoc(StaticLoc), FuncLoc(FuncLoc) {
assert(!Name.getBaseName().isSpecial());

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

Bits.FuncDecl.HasDynamicSelf = false;
Bits.FuncDecl.ForcedStaticDispatch = false;
Expand All @@ -5367,7 +5365,7 @@ class FuncDecl : public AbstractFunctionDecl {
DeclName Name, SourceLoc NameLoc,
bool Throws, SourceLoc ThrowsLoc,
GenericParamList *GenericParams,
unsigned NumParameterLists,
bool HasImplicitSelfDecl,
DeclContext *Parent,
ClangNode ClangN);

Expand All @@ -5379,7 +5377,7 @@ class FuncDecl : public AbstractFunctionDecl {
DeclName Name, SourceLoc NameLoc,
bool Throws, SourceLoc ThrowsLoc,
GenericParamList *GenericParams,
unsigned NumParameterLists,
bool HasImplicitSelfDecl,
DeclContext *Parent);

static FuncDecl *create(ASTContext &Context, SourceLoc StaticLoc,
Expand All @@ -5388,7 +5386,8 @@ class FuncDecl : public AbstractFunctionDecl {
DeclName Name, SourceLoc NameLoc,
bool Throws, SourceLoc ThrowsLoc,
GenericParamList *GenericParams,
ArrayRef<ParameterList *> ParameterLists,
ParamDecl *SelfDecl,
ParameterList *ParameterList,
TypeLoc FnRetType, DeclContext *Parent,
ClangNode ClangN = ClangNode());

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

void setDeserializedSignature(ArrayRef<ParameterList *> ParameterLists,
TypeLoc FnRetType);

SourceLoc getStaticLoc() const { return StaticLoc; }
SourceLoc getFuncLoc() const { return FuncLoc; }

Expand Down Expand Up @@ -5594,7 +5590,7 @@ class AccessorDecl final : public FuncDecl {
SourceLoc staticLoc,
StaticSpellingKind staticSpelling,
bool throws, SourceLoc throwsLoc,
unsigned numParameterLists,
bool hasImplicitSelfDecl,
GenericParamList *genericParams,
DeclContext *parent,
ClangNode clangNode);
Expand All @@ -5610,7 +5606,7 @@ class AccessorDecl final : public FuncDecl {
StaticSpellingKind staticSpelling,
bool throws, SourceLoc throwsLoc,
GenericParamList *genericParams,
unsigned numParameterLists,
bool hasImplicitSelfDecl,
DeclContext *parent);

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

Expand Down Expand Up @@ -5923,8 +5920,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
GenericParamList *GenericParams,
DeclContext *Parent);

void setParameterLists(ParamDecl *selfParam, ParameterList *bodyParams);

SourceLoc getConstructorLoc() const { return getNameLoc(); }
SourceLoc getStartLoc() const { return getConstructorLoc(); }
SourceRange getSourceRange() const;
Expand Down Expand Up @@ -6114,8 +6109,6 @@ class DestructorDecl : public AbstractFunctionDecl {
DestructorDecl(SourceLoc DestructorLoc, ParamDecl *selfDecl,
DeclContext *Parent);

void setSelfDecl(ParamDecl *selfDecl);

MutableArrayRef<ParameterList *> getParameterLists() {
return { ParameterLists, 2 };
}
Expand Down
19 changes: 0 additions & 19 deletions include/swift/AST/ParameterList.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,6 @@ class alignas(ParamDecl *) ParameterList final :
static ParameterList *createWithoutLoc(ParamDecl *decl) {
return create(decl->getASTContext(), decl);
}

/// Create an implicit 'self' decl for a method in the specified decl context.
/// If 'static' is true, then this is self for a static method in the type.
///
/// Note that this decl is created, but it is returned with an incorrect
/// DeclContext that needs to be set correctly. This is automatically handled
/// when a function is created with this as part of its argument list.
///
static ParameterList *createUnboundSelf(SourceLoc loc, DeclContext *DC);

/// Create an implicit 'self' decl for a method in the specified decl context.
/// If 'static' is true, then this is self for a static method in the type.
///
/// Note that this decl is created, but it is returned with an incorrect
/// DeclContext that needs to be set correctly. This is automatically handled
/// when a function is created with this as part of its argument list.
static ParameterList *createSelf(SourceLoc loc, DeclContext *DC,
bool isStatic = false,
bool isInOut = false);

SourceLoc getLParenLoc() const { return LParenLoc; }
SourceLoc getRParenLoc() const { return RParenLoc; }
Expand Down
4 changes: 2 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1102,12 +1102,12 @@ class Parser {
DefaultArgumentInfo *defaultArgs = nullptr);

ParserStatus parseFunctionArguments(SmallVectorImpl<Identifier> &NamePieces,
SmallVectorImpl<ParameterList*> &BodyParams,
ParameterList *&BodyParams,
ParameterContextKind paramContext,
DefaultArgumentInfo &defaultArgs);
ParserStatus parseFunctionSignature(Identifier functionName,
DeclName &fullName,
SmallVectorImpl<ParameterList *> &bodyParams,
ParameterList *&bodyParams,
DefaultArgumentInfo &defaultArgs,
SourceLoc &throws,
bool &rethrows,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const uint16_t VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t VERSION_MINOR = 427; // Invalid conformances
const uint16_t VERSION_MINOR = 428; // Removing multiple parameter lists

using DeclIDField = BCFixed<31>;

Expand Down
11 changes: 11 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2828,6 +2828,17 @@ class Verifier : public ASTWalker {
abort();
}

// Check that type members have an interface type of the form
// (Self) -> (Args...) -> Result.
if (AFD->getImplicitSelfDecl()) {
if (!interfaceTy->castTo<AnyFunctionType>()
->getResult()->is<FunctionType>()) {
Out << "Interface type of method must return a function";
interfaceTy->dump(Out);
abort();
}
}

// Throwing @objc methods must have a foreign error convention.
if (AFD->isObjC() &&
static_cast<bool>(AFD->getForeignErrorConvention())
Expand Down
5 changes: 4 additions & 1 deletion lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ getBuiltinFunction(Identifier Id, ArrayRef<Type> argTypes, Type ResType,
Name, /*NameLoc=*/SourceLoc(),
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
/*GenericParams=*/nullptr,
/*SelfDecl=*/nullptr,
paramList,
TypeLoc::withoutLoc(ResType), DC);
FD->setInterfaceType(FnType);
Expand Down Expand Up @@ -242,7 +243,9 @@ getBuiltinGenericFunction(Identifier Id,
/*FuncLoc=*/SourceLoc(),
Name, /*NameLoc=*/SourceLoc(),
/*Throws=*/false, /*ThrowsLoc=*/SourceLoc(),
GenericParams, paramList,
GenericParams,
/*SelfDecl=*/nullptr,
paramList,
TypeLoc::withoutLoc(ResBodyType), DC);

func->setInterfaceType(InterfaceType);
Expand Down
Loading