Skip to content

Commit d933d5a

Browse files
authored
Merge pull request #3837 from DougGregor/se-0111-remove-function-labels
SE-0111 Work in progress: stripping argument labels from function references
2 parents d072016 + 202cf2e commit d933d5a

26 files changed

+802
-178
lines changed

include/swift/AST/DeclNameLoc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class DeclNameLoc {
7878
/// Whether the location information is invalid.
7979
bool isInvalid() const { return getBaseNameLoc().isInvalid(); }
8080

81+
/// Whether this was written as a compound name.
82+
bool isCompound() const { return NumArgumentLabels > 0; }
83+
8184
/// Retrieve the location of the base name.
8285
SourceLoc getBaseNameLoc() const {
8386
return getSourceLocs()[BaseNameIndex];

include/swift/AST/Expr.h

Lines changed: 121 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/AST/CaptureInfo.h"
2121
#include "swift/AST/ConcreteDeclRef.h"
2222
#include "swift/AST/DeclNameLoc.h"
23+
#include "swift/AST/FunctionRefKind.h"
2324
#include "swift/AST/ProtocolConformanceRef.h"
2425
#include "swift/AST/TypeAlignments.h"
2526
#include "swift/AST/TypeLoc.h"
@@ -53,7 +54,7 @@ namespace swift {
5354
class PatternBindingDecl;
5455
class ParameterList;
5556
class EnumElementDecl;
56-
57+
5758
enum class ExprKind : uint8_t {
5859
#define EXPR(Id, Parent) Id,
5960
#define EXPR_RANGE(Id, FirstId, LastId) \
@@ -114,7 +115,7 @@ enum class AccessSemantics : unsigned char {
114115
/// polymorphism is expected.
115116
Ordinary,
116117
};
117-
118+
118119
/// Expr - Base class for all expressions in swift.
119120
class alignas(8) Expr {
120121
Expr(const Expr&) = delete;
@@ -164,10 +165,21 @@ class alignas(8) Expr {
164165
friend class DeclRefExpr;
165166
unsigned : NumExprBits;
166167
unsigned Semantics : 2; // an AccessSemantics
168+
unsigned FunctionRefKind : 2;
167169
};
168-
enum { NumDeclRefExprBits = NumExprBits + 2 };
170+
enum { NumDeclRefExprBits = NumExprBits + 4 };
169171
static_assert(NumDeclRefExprBits <= 32, "fits in an unsigned");
170172

173+
class UnresolvedDeclRefExprBitfields {
174+
friend class UnresolvedDeclRefExpr;
175+
unsigned : NumExprBits;
176+
unsigned DeclRefKind : 2;
177+
unsigned IsSpecialized : 1;
178+
unsigned FunctionRefKind : 2;
179+
};
180+
enum { NumUnresolvedDeclRefExprBits = NumExprBits + 5 };
181+
static_assert(NumUnresolvedDeclRefExprBits <= 32, "fits in an unsigned");
182+
171183
class MemberRefExprBitfields {
172184
friend class MemberRefExpr;
173185
unsigned : NumExprBits;
@@ -193,6 +205,14 @@ class alignas(8) Expr {
193205
enum { NumTupleExprBits = NumExprBits + 3 };
194206
static_assert(NumTupleExprBits <= 32, "fits in an unsigned");
195207

208+
class UnresolvedDotExprBitfields {
209+
friend class UnresolvedDotExpr;
210+
unsigned : NumExprBits;
211+
unsigned FunctionRefKind : 2;
212+
};
213+
enum { NumUnresolvedDotExprExprBits = NumExprBits + 2 };
214+
static_assert(NumUnresolvedDotExprExprBits <= 32, "fits in an unsigned");
215+
196216
class SubscriptExprBitfields {
197217
friend class SubscriptExpr;
198218
unsigned : NumExprBits;
@@ -242,10 +262,19 @@ class alignas(8) Expr {
242262
class OverloadSetRefExprBitfields {
243263
friend class OverloadSetRefExpr;
244264
unsigned : NumExprBits;
265+
unsigned FunctionRefKind : 2;
245266
};
246-
enum { NumOverloadSetRefExprBits = NumExprBits };
267+
enum { NumOverloadSetRefExprBits = NumExprBits + 2};
247268
static_assert(NumOverloadSetRefExprBits <= 32, "fits in an unsigned");
248269

270+
class OverloadedDeclRefExprBitfields {
271+
friend class OverloadedDeclRefExpr;
272+
unsigned : NumOverloadSetRefExprBits;
273+
unsigned IsSpecialized : 1;
274+
};
275+
enum { NumOverloadedDeclRefExprBits = NumOverloadSetRefExprBits + 1 };
276+
static_assert(NumOverloadedDeclRefExprBits <= 32, "fits in an unsigned");
277+
249278
class BooleanLiteralExprBitfields {
250279
friend class BooleanLiteralExpr;
251280
unsigned : NumLiteralExprBits;
@@ -398,12 +427,15 @@ class alignas(8) Expr {
398427
NumberLiteralExprBitfields NumberLiteralExprBits;
399428
StringLiteralExprBitfields StringLiteralExprBits;
400429
DeclRefExprBitfields DeclRefExprBits;
430+
UnresolvedDeclRefExprBitfields UnresolvedDeclRefExprBits;
401431
TupleExprBitfields TupleExprBits;
402432
MemberRefExprBitfields MemberRefExprBits;
433+
UnresolvedDotExprBitfields UnresolvedDotExprBits;
403434
SubscriptExprBitfields SubscriptExprBits;
404435
DynamicSubscriptExprBitfields DynamicSubscriptExprBits;
405436
UnresolvedMemberExprBitfields UnresolvedMemberExprBits;
406437
OverloadSetRefExprBitfields OverloadSetRefExprBits;
438+
OverloadedDeclRefExprBitfields OverloadedDeclRefExprBits;
407439
BooleanLiteralExprBitfields BooleanLiteralExprBits;
408440
MagicIdentifierLiteralExprBitfields MagicIdentifierLiteralExprBits;
409441
ObjectLiteralExprBitfields ObjectLiteralExprBits;
@@ -1212,6 +1244,9 @@ class DeclRefExpr : public Expr {
12121244
Type Ty = Type())
12131245
: Expr(ExprKind::DeclRef, Implicit, Ty), DOrSpecialized(D), Loc(Loc) {
12141246
DeclRefExprBits.Semantics = (unsigned) semantics;
1247+
DeclRefExprBits.FunctionRefKind =
1248+
static_cast<unsigned>(Loc.isCompound() ? FunctionRefKind::Compound
1249+
: FunctionRefKind::Unapplied);
12151250
}
12161251

12171252
/// Retrieve the declaration to which this expression refers.
@@ -1254,6 +1289,16 @@ class DeclRefExpr : public Expr {
12541289
SourceLoc getLoc() const { return Loc.getBaseNameLoc(); }
12551290
DeclNameLoc getNameLoc() const { return Loc; }
12561291

1292+
/// Retrieve the kind of function reference.
1293+
FunctionRefKind getFunctionRefKind() const {
1294+
return static_cast<FunctionRefKind>(DeclRefExprBits.FunctionRefKind);
1295+
}
1296+
1297+
/// Set the kind of function reference.
1298+
void setFunctionRefKind(FunctionRefKind refKind) {
1299+
DeclRefExprBits.FunctionRefKind = static_cast<unsigned>(refKind);
1300+
}
1301+
12571302
static bool classof(const Expr *E) {
12581303
return E->getKind() == ExprKind::DeclRef;
12591304
}
@@ -1364,9 +1409,12 @@ class OverloadSetRefExpr : public Expr {
13641409
ArrayRef<ValueDecl*> Decls;
13651410

13661411
protected:
1367-
OverloadSetRefExpr(ExprKind Kind, ArrayRef<ValueDecl*> decls, bool Implicit,
1368-
Type Ty)
1369-
: Expr(Kind, Implicit, Ty), Decls(decls) {}
1412+
OverloadSetRefExpr(ExprKind Kind, ArrayRef<ValueDecl*> decls,
1413+
FunctionRefKind functionRefKind, bool Implicit, Type Ty)
1414+
: Expr(Kind, Implicit, Ty), Decls(decls) {
1415+
OverloadSetRefExprBits.FunctionRefKind =
1416+
static_cast<unsigned>(functionRefKind);
1417+
}
13701418

13711419
public:
13721420
ArrayRef<ValueDecl*> getDecls() const { return Decls; }
@@ -1380,6 +1428,17 @@ class OverloadSetRefExpr : public Expr {
13801428
/// concrete base object (which is not a metatype).
13811429
bool hasBaseObject() const;
13821430

1431+
/// Retrieve the kind of function reference.
1432+
FunctionRefKind getFunctionRefKind() const {
1433+
return static_cast<FunctionRefKind>(
1434+
OverloadSetRefExprBits.FunctionRefKind);
1435+
}
1436+
1437+
/// Set the kind of function reference.
1438+
void setFunctionRefKind(FunctionRefKind refKind) {
1439+
OverloadSetRefExprBits.FunctionRefKind = static_cast<unsigned>(refKind);
1440+
}
1441+
13831442
static bool classof(const Expr *E) {
13841443
return E->getKind() >= ExprKind::First_OverloadSetRefExpr &&
13851444
E->getKind() <= ExprKind::Last_OverloadSetRefExpr;
@@ -1388,26 +1447,30 @@ class OverloadSetRefExpr : public Expr {
13881447

13891448
/// OverloadedDeclRefExpr - A reference to an overloaded name that should
13901449
/// eventually be resolved (by overload resolution) to a value reference.
1391-
class OverloadedDeclRefExpr : public OverloadSetRefExpr {
1450+
class OverloadedDeclRefExpr final : public OverloadSetRefExpr {
13921451
DeclNameLoc Loc;
1393-
bool IsSpecialized = false;
13941452

13951453
public:
13961454
OverloadedDeclRefExpr(ArrayRef<ValueDecl*> Decls, DeclNameLoc Loc,
1455+
bool isSpecialized,
1456+
FunctionRefKind functionRefKind,
13971457
bool Implicit, Type Ty = Type())
1398-
: OverloadSetRefExpr(ExprKind::OverloadedDeclRef, Decls, Implicit, Ty),
1399-
Loc(Loc) { }
1458+
: OverloadSetRefExpr(ExprKind::OverloadedDeclRef, Decls, functionRefKind,
1459+
Implicit, Ty),
1460+
Loc(Loc) {
1461+
OverloadedDeclRefExprBits.IsSpecialized = isSpecialized;
1462+
}
14001463

14011464
DeclNameLoc getNameLoc() const { return Loc; }
14021465
SourceLoc getLoc() const { return Loc.getBaseNameLoc(); }
14031466
SourceRange getSourceRange() const { return Loc.getSourceRange(); }
14041467

1405-
void setSpecialized(bool specialized) { IsSpecialized = specialized; }
1406-
14071468
/// \brief Determine whether this declaration reference was immediately
14081469
/// specialized by <...>.
1409-
bool isSpecialized() const { return IsSpecialized; }
1410-
1470+
bool isSpecialized() const {
1471+
return OverloadedDeclRefExprBits.IsSpecialized;
1472+
}
1473+
14111474
static bool classof(const Expr *E) {
14121475
return E->getKind() == ExprKind::OverloadedDeclRef;
14131476
}
@@ -1421,29 +1484,48 @@ class OverloadedDeclRefExpr : public OverloadSetRefExpr {
14211484
class UnresolvedDeclRefExpr : public Expr {
14221485
DeclName Name;
14231486
DeclNameLoc Loc;
1424-
DeclRefKind RefKind;
1425-
bool IsSpecialized = false;
14261487

14271488
public:
14281489
UnresolvedDeclRefExpr(DeclName name, DeclRefKind refKind, DeclNameLoc loc)
1429-
: Expr(ExprKind::UnresolvedDeclRef, /*Implicit=*/loc.isInvalid()),
1430-
Name(name), Loc(loc), RefKind(refKind) {
1490+
: Expr(ExprKind::UnresolvedDeclRef, /*Implicit=*/loc.isInvalid()),
1491+
Name(name), Loc(loc) {
1492+
UnresolvedDeclRefExprBits.DeclRefKind = static_cast<unsigned>(refKind);
1493+
UnresolvedDeclRefExprBits.IsSpecialized = false;
1494+
UnresolvedDeclRefExprBits.FunctionRefKind =
1495+
static_cast<unsigned>(Loc.isCompound() ? FunctionRefKind::Compound
1496+
: FunctionRefKind::Unapplied);
14311497
}
14321498

14331499
bool hasName() const { return static_cast<bool>(Name); }
14341500
DeclName getName() const { return Name; }
1435-
DeclRefKind getRefKind() const { return RefKind; }
14361501

1437-
void setSpecialized(bool specialized) { IsSpecialized = specialized; }
1502+
DeclRefKind getRefKind() const {
1503+
return static_cast<DeclRefKind>(UnresolvedDeclRefExprBits.DeclRefKind);
1504+
}
1505+
1506+
void setSpecialized(bool specialized) {
1507+
UnresolvedDeclRefExprBits.IsSpecialized = specialized;
1508+
}
14381509

14391510
/// \brief Determine whether this declaration reference was immediately
14401511
/// specialized by <...>.
1441-
bool isSpecialized() const { return IsSpecialized; }
1512+
bool isSpecialized() const { return UnresolvedDeclRefExprBits.IsSpecialized; }
1513+
1514+
/// Retrieve the kind of function reference.
1515+
FunctionRefKind getFunctionRefKind() const {
1516+
return static_cast<FunctionRefKind>(
1517+
UnresolvedDeclRefExprBits.FunctionRefKind);
1518+
}
1519+
1520+
/// Set the kind of function reference.
1521+
void setFunctionRefKind(FunctionRefKind refKind) {
1522+
UnresolvedDeclRefExprBits.FunctionRefKind = static_cast<unsigned>(refKind);
1523+
}
14421524

14431525
DeclNameLoc getNameLoc() const { return Loc; }
14441526

14451527
SourceRange getSourceRange() const { return Loc.getSourceRange(); }
1446-
1528+
14471529
static bool classof(const Expr *E) {
14481530
return E->getKind() == ExprKind::UnresolvedDeclRef;
14491531
}
@@ -2246,8 +2328,12 @@ class UnresolvedDotExpr : public Expr {
22462328
public:
22472329
UnresolvedDotExpr(Expr *subexpr, SourceLoc dotloc, DeclName name,
22482330
DeclNameLoc nameloc, bool Implicit)
2249-
: Expr(ExprKind::UnresolvedDot, Implicit), SubExpr(subexpr), DotLoc(dotloc),
2250-
NameLoc(nameloc), Name(name) {}
2331+
: Expr(ExprKind::UnresolvedDot, Implicit), SubExpr(subexpr), DotLoc(dotloc),
2332+
NameLoc(nameloc), Name(name) {
2333+
UnresolvedDotExprBits.FunctionRefKind =
2334+
static_cast<unsigned>(NameLoc.isCompound() ? FunctionRefKind::Compound
2335+
: FunctionRefKind::Unapplied);
2336+
}
22512337

22522338
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
22532339

@@ -2266,6 +2352,16 @@ class UnresolvedDotExpr : public Expr {
22662352
DeclName getName() const { return Name; }
22672353
DeclNameLoc getNameLoc() const { return NameLoc; }
22682354

2355+
/// Retrieve the kind of function reference.
2356+
FunctionRefKind getFunctionRefKind() const {
2357+
return static_cast<FunctionRefKind>(UnresolvedDotExprBits.FunctionRefKind);
2358+
}
2359+
2360+
/// Set the kind of function reference.
2361+
void setFunctionRefKind(FunctionRefKind refKind) {
2362+
UnresolvedDotExprBits.FunctionRefKind = static_cast<unsigned>(refKind);
2363+
}
2364+
22692365
static bool classof(const Expr *E) {
22702366
return E->getKind() == ExprKind::UnresolvedDot;
22712367
}

include/swift/AST/FunctionRefKind.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===--- FunctionRefKind.h - Function reference kind ------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// This file defines the FunctionRefKind enum, which is used to describe how
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_AST_FUNCTION_REF_KIND_H
18+
#define SWIFT_AST_FUNCTION_REF_KIND_H
19+
20+
namespace swift {
21+
22+
/// Describes how a function is referenced within an expression node,
23+
/// which dictates whether argument labels are part of the resulting
24+
/// function type or not.
25+
///
26+
/// How a function is referenced comes down to how it was spelled in
27+
/// the source code, e.g., was it called in the source code and was it
28+
/// spelled as a compound name.
29+
enum class FunctionRefKind : unsigned {
30+
/// The function was referenced using a bare function name (e.g.,
31+
/// 'f') and not directly called.
32+
Unapplied,
33+
/// The function was referenced using a bare function name and was
34+
/// directly applied once, e.g., "f(a: 1, b: 2)".
35+
SingleApply,
36+
/// The function was referenced using a bare function name and was
37+
/// directly applied two or more times, e.g., "g(x)(y)".
38+
DoubleApply,
39+
/// The function was referenced using a compound function name,
40+
/// e.g., "f(a:b:)".
41+
Compound,
42+
};
43+
44+
/// Produce a string describing a function reference kind, for
45+
/// debugging purposes.
46+
StringRef getFunctionRefKindStr(FunctionRefKind refKind);
47+
48+
}
49+
50+
#endif // SWIFT_AST_FUNCTION_REF_KIND_H

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ namespace swift {
9595
/// was not compiled with -enable-testing.
9696
bool EnableTestableAttrRequiresTestableModule = true;
9797

98+
/// Whether to implement SE-0111, the removal of argument labels in types.
99+
bool SuppressArgumentLabelsInTypes = false;
100+
98101
///
99102
/// Flags for developers
100103
///

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def disable_testable_attr_requires_testable_module :
9494
Flag<["-"], "disable-testable-attr-requires-testable-module">,
9595
HelpText<"Disable checking of @testable">;
9696

97+
def suppress_argument_labels_in_types :
98+
Flag<["-"], "suppress-argument-labels-in-types">,
99+
HelpText<"SE-0111: Suppress argument labels in types">;
100+
97101
def enable_target_os_checking :
98102
Flag<["-"], "enable-target-os-checking">,
99103
HelpText<"Enable checking the target OS of serialized modules">;

0 commit comments

Comments
 (0)