Skip to content

Commit b40c89d

Browse files
committed
[AST] Carry AST names through call expressions.
1 parent 3331dc4 commit b40c89d

File tree

6 files changed

+51
-51
lines changed

6 files changed

+51
-51
lines changed

include/swift/AST/Expr.h

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,21 @@ class TrailingCallArguments
696696
return { this->template getTrailingObjects<Identifier>(),
697697
asDerived().getNumArguments() };
698698
}
699+
700+
/// Retrieve the buffer containing the argument label locations.
701+
ArrayRef<SourceLoc> getArgumentLabelLocs() const {
702+
if (!asDerived().hasArgumentLabelLocs())
703+
return { };
704+
705+
return { this->template getTrailingObjects<SourceLoc>(),
706+
asDerived().getNumArguments() };
707+
}
708+
709+
/// Retrieve the location of the ith argument label.
710+
SourceLoc getArgumentLabelLoc(unsigned i) const {
711+
auto locs = getArgumentLabelLocs();
712+
return i < locs.size() ? locs[i] : SourceLoc();
713+
}
699714
};
700715

701716
/// ErrorExpr - Represents a semantically erroneous subexpression in the AST,
@@ -1656,14 +1671,6 @@ class UnresolvedMemberExpr final
16561671
bool implicit);
16571672

16581673
public:
1659-
/// Create a new unresolved member expression.
1660-
///
1661-
/// Note: do not add new callers for this entry point; use the entry points
1662-
/// that take separate arguments.
1663-
static UnresolvedMemberExpr *create(ASTContext &ctx, SourceLoc dotLoc,
1664-
DeclNameLoc nameLoc, DeclName name,
1665-
Expr *arg, bool implicit);
1666-
16671674
/// Create a new unresolved member expression with no arguments.
16681675
static UnresolvedMemberExpr *create(ASTContext &ctx, SourceLoc dotLoc,
16691676
DeclNameLoc nameLoc, DeclName name,
@@ -3535,9 +3542,11 @@ class CallExpr final : public ApplyExpr,
35353542
public:
35363543
/// Create a new call expression.
35373544
///
3538-
/// Note: prefer to use the second entry point, which separates out
3539-
/// arguments/labels/etc.
3545+
/// Note: prefer to use the entry points that separate out the arguments.
35403546
static CallExpr *create(ASTContext &ctx, Expr *fn, Expr *arg,
3547+
ArrayRef<Identifier> argLabels,
3548+
ArrayRef<SourceLoc> argLabelLocs,
3549+
bool hasTrailingClosure,
35413550
bool implicit, Type type = Type());
35423551

35433552
/// Create a new implicit call expression without any source-location

include/swift/Parse/Parser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ class Parser {
11911191
SourceLoc &rightLoc,
11921192
Expr *&trailingClosure);
11931193

1194-
ParserResult<Expr> parseTrailingClosure(SourceRange callee);
1194+
ParserResult<Expr> parseTrailingClosure(SourceRange calleeRange);
11951195

11961196
// NOTE: used only for legacy support for old object literal syntax.
11971197
// Will be removed in the future.

lib/AST/Expr.cpp

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,12 @@ static Expr *packSingleArgument(
11261126
return arg;
11271127
}
11281128

1129+
// Make sure we have argument labels.
1130+
if (argLabels.empty()) {
1131+
argLabelsScratch.assign(args.size(), Identifier());
1132+
argLabels = argLabelsScratch;
1133+
}
1134+
11291135
// Construct the argument tuple.
11301136
if (argLabels.empty() && !args.empty()) {
11311137
argLabelsScratch.assign(args.size(), Identifier());
@@ -1574,31 +1580,6 @@ UnresolvedMemberExpr::UnresolvedMemberExpr(SourceLoc dotLoc,
15741580
initializeCallArguments(argLabels, argLabelLocs, hasTrailingClosure);
15751581
}
15761582

1577-
UnresolvedMemberExpr *UnresolvedMemberExpr::create(ASTContext &ctx,
1578-
SourceLoc dotLoc,
1579-
DeclNameLoc nameLoc,
1580-
DeclName name,
1581-
Expr *arg, bool implicit) {
1582-
// Inspect the argument to dig out the argument labels, their location, and
1583-
// whether there is a trailing closure.
1584-
SmallVector<Identifier, 4> argLabelsScratch;
1585-
SmallVector<SourceLoc, 4> argLabelLocs;
1586-
bool hasTrailingClosure = false;
1587-
ArrayRef<Identifier> argLabels;
1588-
if (arg) {
1589-
argLabels = getArgumentLabelsFromArgument(arg, argLabelsScratch,
1590-
&argLabelLocs,
1591-
&hasTrailingClosure);
1592-
}
1593-
1594-
size_t size = totalSizeToAlloc(argLabels, argLabelLocs, hasTrailingClosure);
1595-
1596-
void *memory = ctx.Allocate(size, alignof(UnresolvedMemberExpr));
1597-
return new (memory) UnresolvedMemberExpr(dotLoc, nameLoc, name, arg,
1598-
argLabels, argLabelLocs,
1599-
hasTrailingClosure, implicit);
1600-
}
1601-
16021583
UnresolvedMemberExpr *UnresolvedMemberExpr::create(ASTContext &ctx,
16031584
SourceLoc dotLoc,
16041585
DeclNameLoc nameLoc,
@@ -1686,15 +1667,20 @@ CallExpr::CallExpr(Expr *fn, Expr *arg, bool Implicit,
16861667
}
16871668

16881669
CallExpr *CallExpr::create(ASTContext &ctx, Expr *fn, Expr *arg,
1670+
ArrayRef<Identifier> argLabels,
1671+
ArrayRef<SourceLoc> argLabelLocs,
1672+
bool hasTrailingClosure,
16891673
bool implicit, Type type) {
1690-
// Inspect the argument to dig out the argument labels, their location, and
1691-
// whether there is a trailing closure.
16921674
SmallVector<Identifier, 4> argLabelsScratch;
1693-
SmallVector<SourceLoc, 4> argLabelLocs;
1694-
bool hasTrailingClosure = false;
1695-
auto argLabels = getArgumentLabelsFromArgument(arg, argLabelsScratch,
1696-
&argLabelLocs,
1697-
&hasTrailingClosure);
1675+
SmallVector<SourceLoc, 4> argLabelLocsScratch;
1676+
if (argLabels.empty()) {
1677+
// Inspect the argument to dig out the argument labels, their location, and
1678+
// whether there is a trailing closure.
1679+
argLabels = getArgumentLabelsFromArgument(arg, argLabelsScratch,
1680+
&argLabelLocsScratch,
1681+
&hasTrailingClosure);
1682+
argLabelLocs = argLabelLocsScratch;
1683+
}
16981684

16991685
size_t size = totalSizeToAlloc(argLabels, argLabelLocs, hasTrailingClosure);
17001686

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1523,7 +1523,7 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
15231523
Result = makeParserResult(
15241524
ParserStatus(closure),
15251525
CallExpr::create(Context, Result.get(), SourceLoc(),
1526-
{ }, { Identifier() }, { }, SourceLoc(),
1526+
{ }, { }, { }, SourceLoc(),
15271527
closure.get(), /*implicit=*/false));
15281528

15291529
if (Result.hasCodeCompletion())

lib/Sema/CSApply.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,6 +2369,9 @@ namespace {
23692369
// If there was an argument, apply it.
23702370
if (auto arg = expr->getArgument()) {
23712371
ApplyExpr *apply = CallExpr::create(tc.Context, result, arg,
2372+
expr->getArgumentLabels(),
2373+
expr->getArgumentLabelLocs(),
2374+
expr->hasTrailingClosure(),
23722375
/*implicit=*/false);
23732376
result = finishApply(apply, Type(), cs.getConstraintLocator(expr));
23742377
}
@@ -6666,15 +6669,14 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
66666669
// FIXME: Standardize all callers to always provide all argument names,
66676670
// rather than hack around this.
66686671
CallExpr *call;
6672+
auto argLabels = witness->getFullName().getArgumentNames();
66696673
if (arguments.size() == 1 &&
66706674
(isVariadicWitness(witness) ||
6671-
argumentNamesMatch(arguments[0],
6672-
witness->getFullName().getArgumentNames()))) {
6673-
call = CallExpr::create(Context, unresolvedDot, arguments[0],
6675+
argumentNamesMatch(arguments[0], argLabels))) {
6676+
call = CallExpr::create(Context, unresolvedDot, arguments[0], { },
6677+
{ }, /*hasTrailingClosure=*/false,
66746678
/*implicit=*/true);
66756679
} else {
6676-
auto names = witness->getFullName().getArgumentNames();
6677-
66786680
// The tuple should have the source range enclosing its arguments unless
66796681
// they are invalid or there are no arguments.
66806682
SourceLoc TupleStartLoc = base->getStartLoc();
@@ -6690,7 +6692,7 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
66906692

66916693
call = CallExpr::create(Context, unresolvedDot,
66926694
TupleStartLoc,
6693-
arguments, names, { },
6695+
arguments, argLabels, { },
66946696
TupleEndLoc,
66956697
/*trailingClosure=*/nullptr,
66966698
/*implicit=*/true);

lib/Sema/CodeSynthesis.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,10 @@ swift::createDesignatedInitOverride(TypeChecker &tc,
21892189
return ctor;
21902190
}
21912191

2192-
Expr *superCall = CallExpr::create(ctx, ctorRef, ctorArgs, /*Implicit=*/true);
2192+
Expr *superCall =
2193+
CallExpr::create(ctx, ctorRef, ctorArgs,
2194+
superclassCtor->getFullName().getArgumentNames(), { },
2195+
/*hasTrailingClosure=*/false, /*implicit=*/true);
21932196
if (superclassCtor->hasThrows()) {
21942197
superCall = new (ctx) TryExpr(SourceLoc(), superCall, Type(),
21952198
/*Implicit=*/true);

0 commit comments

Comments
 (0)