Skip to content

Commit 2ad754d

Browse files
authored
Merge pull request #20152 from xedin/add-autoclosure-flag-to-param-decl
[AST/Sema] Associate `@autoclosure` flag with parameter declaration
2 parents c0c6052 + 7bd2688 commit 2ad754d

40 files changed

+447
-372
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ class ASTMangler : public Mangler {
229229
void appendAnyGenericType(const GenericTypeDecl *decl);
230230

231231
void appendFunction(AnyFunctionType *fn, bool isFunctionMangling = false);
232-
void appendFunctionType(AnyFunctionType *fn);
232+
void appendFunctionType(AnyFunctionType *fn, bool isAutoClosure = false);
233233

234234
void appendFunctionSignature(AnyFunctionType *fn);
235235

include/swift/AST/Decl.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4849,7 +4849,10 @@ class ParamDecl : public VarDecl {
48494849

48504850
/// The default value, if any, along with whether this is varargs.
48514851
llvm::PointerIntPair<StoredDefaultArgument *, 1> DefaultValueAndIsVariadic;
4852-
4852+
4853+
/// `@autoclosure` flag associated with this parameter.
4854+
bool IsAutoClosure = false;
4855+
48534856
public:
48544857
ParamDecl(VarDecl::Specifier specifier,
48554858
SourceLoc specifierLoc, SourceLoc argumentNameLoc,
@@ -4933,7 +4936,11 @@ class ParamDecl : public VarDecl {
49334936
/// Whether or not this parameter is varargs.
49344937
bool isVariadic() const { return DefaultValueAndIsVariadic.getInt(); }
49354938
void setVariadic(bool value = true) {DefaultValueAndIsVariadic.setInt(value);}
4936-
4939+
4940+
/// Whether or not this parameter is marked with `@autoclosure`.
4941+
bool isAutoClosure() const { return IsAutoClosure; }
4942+
void setAutoClosure(bool value = true) { IsAutoClosure = value; }
4943+
49374944
/// Remove the type of this varargs element designator, without the array
49384945
/// type wrapping it. A parameter like "Int..." will have formal parameter
49394946
/// type of "[Int]" and this returns "Int".
@@ -6835,16 +6842,8 @@ inline EnumElementDecl *EnumDecl::getUniqueElement(bool hasValue) const {
68356842
return result;
68366843
}
68376844

6838-
/// Determine the default argument kind and type for the given argument index
6839-
/// in this declaration, which must be a function or constructor.
6840-
///
6841-
/// \param Index The index of the argument for which we are querying the
6842-
/// default argument.
6843-
///
6844-
/// \returns the default argument kind and, if there is a default argument,
6845-
/// the type of the corresponding parameter.
6846-
std::pair<DefaultArgumentKind, Type>
6847-
getDefaultArgumentInfo(ValueDecl *source, unsigned Index);
6845+
/// Retrieve parameter declaration from the given source at given index.
6846+
const ParamDecl *getParameterAt(ValueDecl *source, unsigned index);
68486847

68496848
/// Display Decl subclasses.
68506849
void simple_display(llvm::raw_ostream &out, const Decl *decl);

include/swift/AST/Types.h

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ class alignas(1 << TypeAlignInBits) TypeBase {
276276
}
277277

278278
protected:
279-
enum { NumAFTExtInfoBits = 7 };
279+
enum { NumAFTExtInfoBits = 6 };
280280
enum { NumSILExtInfoBits = 6 };
281281
union { uint64_t OpaqueBits;
282282

@@ -1743,7 +1743,8 @@ class ParameterTypeFlags {
17431743

17441744
/// Create one from what's present in the parameter type
17451745
inline static ParameterTypeFlags
1746-
fromParameterType(Type paramTy, bool isVariadic, ValueOwnership ownership);
1746+
fromParameterType(Type paramTy, bool isVariadic, bool isAutoClosure,
1747+
ValueOwnership ownership);
17471748

17481749
bool isNone() const { return !value; }
17491750
bool isVariadic() const { return value.contains(Variadic); }
@@ -2835,15 +2836,14 @@ class AnyFunctionType : public TypeBase {
28352836
// If bits are added or removed, then TypeBase::AnyFunctionTypeBits
28362837
// and NumMaskBits must be updated, and they must match.
28372838
//
2838-
// |representation|isAutoClosure|noEscape|throws|
2839-
// | 0 .. 3 | 4 | 5 | 6 |
2839+
// |representation|noEscape|throws|
2840+
// | 0 .. 3 | 4 | 5 |
28402841
//
28412842
enum : unsigned {
28422843
RepresentationMask = 0xF << 0,
2843-
AutoClosureMask = 1 << 4,
2844-
NoEscapeMask = 1 << 5,
2845-
ThrowsMask = 1 << 6,
2846-
NumMaskBits = 7
2844+
NoEscapeMask = 1 << 4,
2845+
ThrowsMask = 1 << 5,
2846+
NumMaskBits = 6
28472847
};
28482848

28492849
unsigned Bits; // Naturally sized for speed.
@@ -2865,14 +2865,12 @@ class AnyFunctionType : public TypeBase {
28652865

28662866
// Constructor with no defaults.
28672867
ExtInfo(Representation Rep,
2868-
bool IsAutoClosure, bool IsNoEscape,
2868+
bool IsNoEscape,
28692869
bool Throws)
28702870
: ExtInfo(Rep, Throws) {
2871-
Bits |= (IsAutoClosure ? AutoClosureMask : 0);
28722871
Bits |= (IsNoEscape ? NoEscapeMask : 0);
28732872
}
28742873

2875-
bool isAutoClosure() const { return Bits & AutoClosureMask; }
28762874
bool isNoEscape() const { return Bits & NoEscapeMask; }
28772875
bool throws() const { return Bits & ThrowsMask; }
28782876
Representation getRepresentation() const {
@@ -2925,13 +2923,6 @@ class AnyFunctionType : public TypeBase {
29252923
| (unsigned)Rep);
29262924
}
29272925
LLVM_NODISCARD
2928-
ExtInfo withIsAutoClosure(bool IsAutoClosure = true) const {
2929-
if (IsAutoClosure)
2930-
return ExtInfo(Bits | AutoClosureMask);
2931-
else
2932-
return ExtInfo(Bits & ~AutoClosureMask);
2933-
}
2934-
LLVM_NODISCARD
29352926
ExtInfo withNoEscape(bool NoEscape = true) const {
29362927
if (NoEscape)
29372928
return ExtInfo(Bits | NoEscapeMask);
@@ -3028,12 +3019,6 @@ class AnyFunctionType : public TypeBase {
30283019
Representation getRepresentation() const {
30293020
return getExtInfo().getRepresentation();
30303021
}
3031-
3032-
/// \brief True if this type allows an implicit conversion from a function
3033-
/// argument expression of type T to a function of type () -> T.
3034-
bool isAutoClosure() const {
3035-
return getExtInfo().isAutoClosure();
3036-
}
30373022

30383023
/// \brief True if the parameter declaration it is attached to is guaranteed
30393024
/// to not persist the closure for longer than the duration of the call.
@@ -5325,9 +5310,8 @@ inline TupleTypeElt TupleTypeElt::getWithType(Type T) const {
53255310
/// Create one from what's present in the parameter decl and type
53265311
inline ParameterTypeFlags
53275312
ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic,
5313+
bool isAutoClosure,
53285314
ValueOwnership ownership) {
5329-
bool autoclosure = paramTy->is<AnyFunctionType>() &&
5330-
paramTy->castTo<AnyFunctionType>()->isAutoClosure();
53315315
bool escaping = paramTy->is<AnyFunctionType>() &&
53325316
!paramTy->castTo<AnyFunctionType>()->isNoEscape();
53335317
// FIXME(Remove InOut): The last caller that needs this is argument
@@ -5339,7 +5323,7 @@ ParameterTypeFlags::fromParameterType(Type paramTy, bool isVariadic,
53395323
ownership == ValueOwnership::InOut);
53405324
ownership = ValueOwnership::InOut;
53415325
}
5342-
return {isVariadic, autoclosure, escaping, ownership};
5326+
return {isVariadic, isAutoClosure, escaping, ownership};
53435327
}
53445328

53455329
inline const Type *BoundGenericType::getTrailingObjectsPointer() const {

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 465; // Last change: Remove owning addressors
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 466; // Last change: add isAutoClosure flag to param
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -737,7 +737,6 @@ namespace decls_block {
737737
FUNCTION_TYPE,
738738
TypeIDField, // output
739739
FunctionTypeRepresentationField, // representation
740-
BCFixed<1>, // auto-closure?
741740
BCFixed<1>, // noescape?
742741
BCFixed<1> // throws?
743742

@@ -1024,6 +1023,7 @@ namespace decls_block {
10241023
VarDeclSpecifierField, // specifier
10251024
TypeIDField, // interface type
10261025
BCFixed<1>, // isVariadic?
1026+
BCFixed<1>, // isAutoClosure?
10271027
DefaultArgumentField, // default argument kind
10281028
BCBlob // default argument text
10291029
>;

lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3722,7 +3722,7 @@ void AnyFunctionType::decomposeInput(
37223722
default:
37233723
result.emplace_back(type->getInOutObjectType(), Identifier(),
37243724
ParameterTypeFlags::fromParameterType(
3725-
type, false, ValueOwnership::Default));
3725+
type, false, false, ValueOwnership::Default));
37263726
return;
37273727
}
37283728
}

lib/AST/ASTDumper.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,9 @@ namespace {
982982
if (P->isVariadic())
983983
OS << " variadic";
984984

985+
if (P->isAutoClosure())
986+
OS << " autoclosure";
987+
985988
if (P->getDefaultArgumentKind() != DefaultArgumentKind::None)
986989
printField("default_arg",
987990
getDefaultArgumentKindString(P->getDefaultArgumentKind()));
@@ -3436,7 +3439,6 @@ namespace {
34363439
printField("representation",
34373440
getSILFunctionTypeRepresentationString(representation));
34383441

3439-
printFlag(T->isAutoClosure(), "autoclosure");
34403442
printFlag(!T->isNoEscape(), "escaping");
34413443
printFlag(T->throws(), "throws");
34423444

lib/AST/ASTMangler.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,7 +1693,7 @@ void ASTMangler::appendFunction(AnyFunctionType *fn, bool isFunctionMangling) {
16931693
}
16941694
}
16951695

1696-
void ASTMangler::appendFunctionType(AnyFunctionType *fn) {
1696+
void ASTMangler::appendFunctionType(AnyFunctionType *fn, bool isAutoClosure) {
16971697
assert((DWARFMangling || fn->isCanonical()) &&
16981698
"expecting canonical types when not mangling for the debugger");
16991699

@@ -1715,7 +1715,7 @@ void ASTMangler::appendFunctionType(AnyFunctionType *fn) {
17151715
case AnyFunctionType::Representation::Thin:
17161716
return appendOperator("Xf");
17171717
case AnyFunctionType::Representation::Swift:
1718-
if (fn->isAutoClosure()) {
1718+
if (isAutoClosure) {
17191719
if (fn->isNoEscape())
17201720
return appendOperator("XK");
17211721
else
@@ -1799,7 +1799,11 @@ void ASTMangler::appendTypeList(Type listTy) {
17991799

18001800
void ASTMangler::appendTypeListElement(Identifier name, Type elementType,
18011801
ParameterTypeFlags flags) {
1802-
appendType(elementType);
1802+
if (auto *fnType = elementType->getAs<FunctionType>())
1803+
appendFunctionType(fnType, flags.isAutoClosure());
1804+
else
1805+
appendType(elementType);
1806+
18031807
switch (flags.getValueOwnership()) {
18041808
case ValueOwnership::Default:
18051809
/* nothing */

lib/AST/Decl.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,7 +2040,6 @@ mapSignatureExtInfo(AnyFunctionType::ExtInfo info,
20402040
return AnyFunctionType::ExtInfo();
20412041
return AnyFunctionType::ExtInfo()
20422042
.withRepresentation(info.getRepresentation())
2043-
.withIsAutoClosure(info.isAutoClosure())
20442043
.withThrows(info.throws());
20452044
}
20462045

@@ -4802,7 +4801,8 @@ ParamDecl::ParamDecl(ParamDecl *PD, bool withTypes)
48024801
ArgumentName(PD->getArgumentName()),
48034802
ArgumentNameLoc(PD->getArgumentNameLoc()),
48044803
SpecifierLoc(PD->getSpecifierLoc()),
4805-
DefaultValueAndIsVariadic(nullptr, PD->DefaultValueAndIsVariadic.getInt()) {
4804+
DefaultValueAndIsVariadic(nullptr, PD->DefaultValueAndIsVariadic.getInt()),
4805+
IsAutoClosure(PD->isAutoClosure()) {
48064806
Bits.ParamDecl.IsTypeLocImplicit = PD->Bits.ParamDecl.IsTypeLocImplicit;
48074807
Bits.ParamDecl.defaultArgumentKind = PD->Bits.ParamDecl.defaultArgumentKind;
48084808
typeLoc = PD->getTypeLoc().clone(PD->getASTContext());
@@ -5107,17 +5107,15 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
51075107
return DeclName();
51085108
}
51095109

5110-
std::pair<DefaultArgumentKind, Type>
5111-
swift::getDefaultArgumentInfo(ValueDecl *source, unsigned Index) {
5110+
const ParamDecl *swift::getParameterAt(ValueDecl *source, unsigned index) {
51125111
const ParameterList *paramList;
51135112
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(source)) {
51145113
paramList = AFD->getParameters();
51155114
} else {
51165115
paramList = cast<EnumElementDecl>(source)->getParameterList();
51175116
}
51185117

5119-
auto param = paramList->get(Index);
5120-
return { param->getDefaultArgumentKind(), param->getInterfaceType() };
5118+
return paramList->get(index);
51215119
}
51225120

51235121
Type AbstractFunctionDecl::getMethodInterfaceType() const {

lib/AST/Parameter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ void ParameterList::getParams(
106106
type = ParamDecl::getVarargBaseTy(type);
107107

108108
auto label = P->getArgumentName();
109-
auto flags = ParameterTypeFlags::fromParameterType(type, P->isVariadic(),
109+
auto flags = ParameterTypeFlags::fromParameterType(type,
110+
P->isVariadic(),
111+
P->isAutoClosure(),
110112
P->getValueOwnership());
111113
params.emplace_back(type, label, flags);
112114
}

lib/IDE/CodeCompletion.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,7 +2172,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
21722172

21732173
Builder.addCallParameter(param->getArgumentName(), type,
21742174
param->isVariadic(), /*Outermost*/ true,
2175-
param->isInOut(), isIUO);
2175+
param->isInOut(), isIUO, param->isAutoClosure());
21762176
}
21772177
}
21782178

@@ -2270,12 +2270,12 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
22702270
auto isIUO =
22712271
PD->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
22722272
Builder.addCallParameter(argName, bodyName, ParamType,
2273-
Param.isVariadic(), /*TopLevel*/true,
2274-
Param.isInOut(), isIUO);
2275-
} else {
2276-
Builder.addCallParameter(Param.getLabel(), ParamType,
22772273
Param.isVariadic(), /*TopLevel*/ true,
2278-
Param.isInOut(), /*isIUO*/ false);
2274+
Param.isInOut(), isIUO, Param.isAutoClosure());
2275+
} else {
2276+
Builder.addCallParameter(
2277+
Param.getLabel(), ParamType, Param.isVariadic(), /*TopLevel*/ true,
2278+
Param.isInOut(), /*isIUO*/ false, Param.isAutoClosure());
22792279
}
22802280
modifiedBuilder = true;
22812281
NeedComma = true;
@@ -2490,7 +2490,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
24902490
Builder.addCallParameter(Ctx.Id_self, SelfParam.getPlainType(),
24912491
/*IsVarArg*/ false, /*TopLevel*/ true,
24922492
SelfParam.isInOut(),
2493-
/*isIUO*/ false);
2493+
/*isIUO*/ false, /*isAutoClosure*/ false);
24942494
Builder.addRightParen();
24952495
} else if (trivialTrailingClosure) {
24962496
Builder.addBraceStmtWithCursor(" { code }");
@@ -3336,7 +3336,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33363336
assert(RHSType && resultType);
33373337
builder.addCallParameter(Identifier(), Identifier(), RHSType,
33383338
/*IsVarArg*/ false, /*TopLevel*/ true,
3339-
/*IsInOut*/ false, /*isIUO*/ false);
3339+
/*IsInOut*/ false, /*isIUO*/ false,
3340+
/*isAutoClosure*/ false);
33403341
addTypeAnnotation(builder, resultType);
33413342
}
33423343

@@ -3359,7 +3360,8 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
33593360
builder.addWhitespace(" ");
33603361
if (RHSType)
33613362
builder.addCallParameter(Identifier(), Identifier(), RHSType, false, true,
3362-
/*IsInOut*/ false, /*isIUO*/ false);
3363+
/*IsInOut*/ false, /*isIUO*/ false,
3364+
/*isAutoClosure*/ false);
33633365
if (resultType)
33643366
addTypeAnnotation(builder, resultType);
33653367
}
@@ -3648,19 +3650,19 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36483650
builder.addLeftParen();
36493651
builder.addCallParameter(context.getIdentifier("red"), floatType, false,
36503652
true, /*IsInOut*/ false,
3651-
/*isIUO*/ false);
3653+
/*isIUO*/ false, /*isAutoClosure*/ false);
36523654
builder.addComma();
36533655
builder.addCallParameter(context.getIdentifier("green"), floatType, false,
3654-
true, /*IsInOut*/ false,
3655-
/*isIUO*/ false);
3656+
true, /*IsInOut*/ false, /*isIUO*/ false,
3657+
/*isAutoClosure*/ false);
36563658
builder.addComma();
36573659
builder.addCallParameter(context.getIdentifier("blue"), floatType, false,
3658-
true, /*IsInOut*/ false,
3659-
/*isIUO*/ false);
3660+
true, /*IsInOut*/ false, /*isIUO*/ false,
3661+
/*isAutoClosure*/ false);
36603662
builder.addComma();
36613663
builder.addCallParameter(context.getIdentifier("alpha"), floatType, false,
3662-
true, /*IsInOut*/ false,
3663-
/*isIUO*/ false);
3664+
true, /*IsInOut*/ false, /*isIUO*/ false,
3665+
/*isAutoClosure*/ false);
36643666
builder.addRightParen();
36653667
});
36663668

@@ -3670,7 +3672,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
36703672
builder.addLeftParen();
36713673
builder.addCallParameter(context.getIdentifier("resourceName"),
36723674
stringType, false, true, /*IsInOut*/ false,
3673-
/*isIUO*/ false);
3675+
/*isIUO*/ false, /*isAutoClosure*/ false);
36743676
builder.addRightParen();
36753677
});
36763678

0 commit comments

Comments
 (0)