Skip to content

Commit 352adc3

Browse files
committed
Remove Argument from UnresolvedMemberExpr
Instead, an expresison like `.foo()` is represented as an `UnresolvedMemberExpr` nested inside a `CallExpr`.
1 parent db33dfa commit 352adc3

20 files changed

+190
-457
lines changed

include/swift/AST/Expr.h

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,8 @@ class alignas(8) Expr {
249249
NumArgLabels : 16
250250
);
251251

252-
SWIFT_INLINE_BITFIELD_FULL(UnresolvedMemberExpr, Expr, 1+1+1+16,
253-
/// Whether the UnresolvedMemberExpr has arguments.
254-
HasArguments : 1,
255-
/// Whether the UnresolvedMemberExpr also has source locations for the
256-
/// argument label.
257-
HasArgLabelLocs : 1,
258-
/// Whether the last argument is a trailing closure.
259-
HasTrailingClosure : 1,
260-
: NumPadBits,
261-
/// # of argument labels stored after the UnresolvedMemberExpr.
262-
NumArgLabels : 16
252+
SWIFT_INLINE_BITFIELD_FULL(UnresolvedMemberExpr, Expr, 2,
253+
FunctionRefKind : 2
263254
);
264255

265256
SWIFT_INLINE_BITFIELD(OverloadSetRefExpr, Expr, 2,
@@ -1841,71 +1832,35 @@ class DynamicSubscriptExpr final
18411832
/// member, which is to be resolved with context sensitive type information into
18421833
/// bar.foo. These always have unresolved type.
18431834
class UnresolvedMemberExpr final
1844-
: public Expr,
1845-
public TrailingCallArguments<UnresolvedMemberExpr> {
1835+
: public Expr {
18461836
SourceLoc DotLoc;
18471837
DeclNameLoc NameLoc;
18481838
DeclNameRef Name;
1849-
Expr *Argument;
1850-
1851-
UnresolvedMemberExpr(SourceLoc dotLoc, DeclNameLoc nameLoc,
1852-
DeclNameRef name, Expr *argument,
1853-
ArrayRef<Identifier> argLabels,
1854-
ArrayRef<SourceLoc> argLabelLocs,
1855-
bool hasTrailingClosure,
1856-
bool implicit);
18571839

18581840
public:
1859-
/// Create a new unresolved member expression with no arguments.
1860-
static UnresolvedMemberExpr *create(ASTContext &ctx, SourceLoc dotLoc,
1861-
DeclNameLoc nameLoc, DeclNameRef name,
1862-
bool implicit);
1863-
1864-
/// Create a new unresolved member expression.
1865-
static UnresolvedMemberExpr *create(ASTContext &ctx, SourceLoc dotLoc,
1866-
DeclNameLoc nameLoc, DeclNameRef name,
1867-
SourceLoc lParenLoc,
1868-
ArrayRef<Expr *> args,
1869-
ArrayRef<Identifier> argLabels,
1870-
ArrayRef<SourceLoc> argLabelLocs,
1871-
SourceLoc rParenLoc,
1872-
ArrayRef<TrailingClosure> trailingClosures,
1873-
bool implicit);
1841+
UnresolvedMemberExpr(SourceLoc dotLoc, DeclNameLoc nameLoc, DeclNameRef name,
1842+
bool implicit)
1843+
: Expr(ExprKind::UnresolvedMember, implicit), DotLoc(dotLoc),
1844+
NameLoc(nameLoc), Name(name) {}
18741845

18751846
DeclNameRef getName() const { return Name; }
18761847
DeclNameLoc getNameLoc() const { return NameLoc; }
18771848
SourceLoc getDotLoc() const { return DotLoc; }
1878-
Expr *getArgument() const { return Argument; }
1879-
void setArgument(Expr *argument) { Argument = argument; }
18801849

1881-
/// Whether this reference has arguments.
1882-
bool hasArguments() const {
1883-
return Bits.UnresolvedMemberExpr.HasArguments;
1884-
}
1885-
1886-
unsigned getNumArguments() const {
1887-
return Bits.UnresolvedMemberExpr.NumArgLabels;
1888-
}
1889-
1890-
bool hasArgumentLabelLocs() const {
1891-
return Bits.UnresolvedMemberExpr.HasArgLabelLocs;
1892-
}
1850+
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
18931851

1894-
/// Whether this call with written with a trailing closure.
1895-
bool hasTrailingClosure() const {
1896-
return Bits.UnresolvedMemberExpr.HasTrailingClosure;
1897-
}
1852+
SourceLoc getStartLoc() const { return DotLoc; }
1853+
SourceLoc getEndLoc() const { return NameLoc.getSourceRange().End; }
18981854

1899-
/// Return the index of the unlabeled trailing closure argument.
1900-
Optional<unsigned> getUnlabeledTrailingClosureIndex() const {
1901-
return getArgument()->getUnlabeledTrailingClosureIndexOfPackedArgument();
1855+
/// Retrieve the kind of function reference.
1856+
FunctionRefKind getFunctionRefKind() const {
1857+
return static_cast<FunctionRefKind>(
1858+
Bits.UnresolvedMemberExpr.FunctionRefKind);
19021859
}
19031860

1904-
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }
1905-
1906-
SourceLoc getStartLoc() const { return DotLoc; }
1907-
SourceLoc getEndLoc() const {
1908-
return (Argument ? Argument->getEndLoc() : NameLoc.getSourceRange().End);
1861+
/// Set the kind of function reference.
1862+
void setFunctionRefKind(FunctionRefKind refKind) {
1863+
Bits.UnresolvedMemberExpr.FunctionRefKind = static_cast<unsigned>(refKind);
19091864
}
19101865

19111866
static bool classof(const Expr *E) {

include/swift/AST/Pattern.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,9 @@ class EnumElementPattern : public Pattern {
554554
bool hasUnresolvedOriginalExpr() const {
555555
return ElementDeclOrUnresolvedOriginalExpr.is<Expr*>();
556556
}
557+
void setUnresolvedOriginalExpr(Expr *e) {
558+
ElementDeclOrUnresolvedOriginalExpr = e;
559+
}
557560

558561
DeclNameLoc getNameLoc() const { return NameLoc; }
559562
SourceLoc getLoc() const { return NameLoc.getBaseNameLoc(); }

lib/AST/ASTDumper.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2108,11 +2108,8 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
21082108
void visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
21092109
printCommon(E, "unresolved_member_expr")
21102110
<< " name='" << E->getName() << "'";
2111-
printArgumentLabels(E->getArgumentLabels());
2112-
if (E->getArgument()) {
2113-
OS << '\n';
2114-
printRec(E->getArgument());
2115-
}
2111+
PrintWithColorRAII(OS, ExprModifierColor)
2112+
<< " function_ref=" << getFunctionRefKindStr(E->getFunctionRefKind());
21162113
PrintWithColorRAII(OS, ParenthesisColor) << ')';
21172114
}
21182115
void visitDotSelfExpr(DotSelfExpr *E) {

lib/AST/ASTWalker.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -504,17 +504,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
504504
Expr *visitOverloadedDeclRefExpr(OverloadedDeclRefExpr *E) { return E; }
505505
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *E) { return E; }
506506

507-
Expr *visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) {
508-
if (E->getArgument()) {
509-
if (auto arg = doIt(E->getArgument())) {
510-
E->setArgument(arg);
511-
return E;
512-
}
513-
514-
return nullptr;
515-
}
516-
return E;
517-
}
507+
Expr *visitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { return E; }
518508

519509
Expr *visitOpaqueValueExpr(OpaqueValueExpr *E) { return E; }
520510

lib/AST/Expr.cpp

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,61 +1608,6 @@ DynamicSubscriptExpr::create(ASTContext &ctx, Expr *base, Expr *index,
16081608
hasTrailingClosure, decl, implicit);
16091609
}
16101610

1611-
UnresolvedMemberExpr::UnresolvedMemberExpr(SourceLoc dotLoc,
1612-
DeclNameLoc nameLoc,
1613-
DeclNameRef name, Expr *argument,
1614-
ArrayRef<Identifier> argLabels,
1615-
ArrayRef<SourceLoc> argLabelLocs,
1616-
bool hasTrailingClosure,
1617-
bool implicit)
1618-
: Expr(ExprKind::UnresolvedMember, implicit),
1619-
DotLoc(dotLoc), NameLoc(nameLoc), Name(name), Argument(argument) {
1620-
Bits.UnresolvedMemberExpr.HasArguments = (argument != nullptr);
1621-
Bits.UnresolvedMemberExpr.NumArgLabels = argLabels.size();
1622-
Bits.UnresolvedMemberExpr.HasArgLabelLocs = !argLabelLocs.empty();
1623-
Bits.UnresolvedMemberExpr.HasTrailingClosure = hasTrailingClosure;
1624-
initializeCallArguments(argLabels, argLabelLocs);
1625-
}
1626-
1627-
UnresolvedMemberExpr *UnresolvedMemberExpr::create(ASTContext &ctx,
1628-
SourceLoc dotLoc,
1629-
DeclNameLoc nameLoc,
1630-
DeclNameRef name,
1631-
bool implicit) {
1632-
size_t size = totalSizeToAlloc({ }, { });
1633-
1634-
void *memory = ctx.Allocate(size, alignof(UnresolvedMemberExpr));
1635-
return new (memory) UnresolvedMemberExpr(dotLoc, nameLoc, name, nullptr,
1636-
{ }, { },
1637-
/*hasTrailingClosure=*/false,
1638-
implicit);
1639-
}
1640-
1641-
UnresolvedMemberExpr *
1642-
UnresolvedMemberExpr::create(ASTContext &ctx, SourceLoc dotLoc,
1643-
DeclNameLoc nameLoc, DeclNameRef name,
1644-
SourceLoc lParenLoc,
1645-
ArrayRef<Expr *> args,
1646-
ArrayRef<Identifier> argLabels,
1647-
ArrayRef<SourceLoc> argLabelLocs,
1648-
SourceLoc rParenLoc,
1649-
ArrayRef<TrailingClosure> trailingClosures,
1650-
bool implicit) {
1651-
SmallVector<Identifier, 4> argLabelsScratch;
1652-
SmallVector<SourceLoc, 4> argLabelLocsScratch;
1653-
Expr *arg = packSingleArgument(ctx, lParenLoc, args, argLabels, argLabelLocs,
1654-
rParenLoc, trailingClosures, implicit,
1655-
argLabelsScratch, argLabelLocsScratch);
1656-
1657-
size_t size = totalSizeToAlloc(argLabels, argLabelLocs);
1658-
1659-
void *memory = ctx.Allocate(size, alignof(UnresolvedMemberExpr));
1660-
return new (memory) UnresolvedMemberExpr(dotLoc, nameLoc, name, arg,
1661-
argLabels, argLabelLocs,
1662-
trailingClosures.size() == 1,
1663-
implicit);
1664-
}
1665-
16661611
ArrayRef<Identifier> ApplyExpr::getArgumentLabels(
16671612
SmallVectorImpl<Identifier> &scratch) const {
16681613
// Unary operators and 'self' applications have a single, unlabeled argument.

0 commit comments

Comments
 (0)