Skip to content

Commit 19688cc

Browse files
committed
[AST] Add the skeleton for PackExpansionExpr.
1 parent be619c6 commit 19688cc

File tree

9 files changed

+90
-0
lines changed

9 files changed

+90
-0
lines changed

include/swift/AST/Expr.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,6 +3527,51 @@ class VarargExpansionExpr : public Expr {
35273527
}
35283528
};
35293529

3530+
/// A pack expansion expression is a pattern expression followed by
3531+
/// the expansion operator '...'. The pattern expression contains
3532+
/// references to parameter packs of length N, and the expansion
3533+
/// operator will repeat the pattern N times.
3534+
///
3535+
/// Pack expansion expressions are permitted in expression contexts
3536+
/// that naturally accept a comma-separated list of values, including
3537+
/// call argument lists, the elements of a tuple value, and the source
3538+
/// of a for-in loop.
3539+
class PackExpansionExpr: public Expr {
3540+
Expr *PatternExpr;
3541+
SourceLoc DotsLoc;
3542+
3543+
PackExpansionExpr(Expr *patternExpr,
3544+
SourceLoc dotsLoc,
3545+
bool implicit, Type type)
3546+
: Expr(ExprKind::PackExpansion, implicit, type),
3547+
PatternExpr(patternExpr), DotsLoc(dotsLoc) {}
3548+
3549+
public:
3550+
static PackExpansionExpr *create(ASTContext &ctx,
3551+
Expr *patternExpr,
3552+
SourceLoc dotsLoc,
3553+
bool implicit = false,
3554+
Type type = Type());
3555+
3556+
Expr *getPatternExpr() const { return PatternExpr; }
3557+
3558+
void setPatternExpr(Expr *patternExpr) {
3559+
PatternExpr = patternExpr;
3560+
}
3561+
3562+
SourceLoc getStartLoc() const {
3563+
return PatternExpr->getStartLoc();
3564+
}
3565+
3566+
SourceLoc getEndLoc() const {
3567+
return DotsLoc;
3568+
}
3569+
3570+
static bool classof(const Expr *E) {
3571+
return E->getKind() == ExprKind::PackExpansion;
3572+
}
3573+
};
3574+
35303575
/// SequenceExpr - A list of binary operations which has not yet been
35313576
/// folded into a tree. The operands all have even indices, while the
35323577
/// subexpressions with odd indices are all (potentially overloaded)

include/swift/AST/ExprNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ ABSTRACT_EXPR(AbstractClosure, Expr)
129129
EXPR_RANGE(AbstractClosure, Closure, AutoClosure)
130130
EXPR(InOut, Expr)
131131
EXPR(VarargExpansion, Expr)
132+
EXPR(PackExpansion, Expr)
132133
EXPR(DynamicType, Expr)
133134
EXPR(RebindSelfInConstructor, Expr)
134135
EXPR(OpaqueValue, Expr)

lib/AST/ASTDumper.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,12 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
24322432
PrintWithColorRAII(OS, ParenthesisColor) << ')';
24332433
}
24342434

2435+
void visitPackExpansionExpr(PackExpansionExpr *E) {
2436+
printCommon(E, "pack_expansion_expr") << "\n";
2437+
printRec(E->getPatternExpr());
2438+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
2439+
}
2440+
24352441
void visitForceTryExpr(ForceTryExpr *E) {
24362442
printCommon(E, "force_try_expr");
24372443
OS << '\n';

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4750,6 +4750,10 @@ void PrintAST::visitVarargExpansionExpr(VarargExpansionExpr *expr) {
47504750
visit(expr->getSubExpr());
47514751
}
47524752

4753+
void PrintAST::visitPackExpansionExpr(PackExpansionExpr *expr) {
4754+
visit(expr->getPatternExpr());
4755+
}
4756+
47534757
void PrintAST::visitArchetypeToSuperExpr(ArchetypeToSuperExpr *expr) {
47544758
}
47554759

lib/AST/ASTWalker.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,14 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
829829
return nullptr;
830830
}
831831

832+
Expr *visitPackExpansionExpr(PackExpansionExpr *E) {
833+
if (Expr *pattern = doIt(E->getPatternExpr())) {
834+
E->setPatternExpr(pattern);
835+
return E;
836+
}
837+
return nullptr;
838+
}
839+
832840
Expr *visitSequenceExpr(SequenceExpr *E) {
833841
for (unsigned i = 0, e = E->getNumElements(); i != e; ++i)
834842
if (Expr *Elt = doIt(E->getElement(i)))

lib/AST/Expr.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ ConcreteDeclRef Expr::getReferencedDecl(bool stopAtParenExpr) const {
389389
PASS_THROUGH_REFERENCE(InOut, getSubExpr);
390390

391391
NO_REFERENCE(VarargExpansion);
392+
NO_REFERENCE(PackExpansion);
392393
NO_REFERENCE(DynamicType);
393394

394395
PASS_THROUGH_REFERENCE(RebindSelfInConstructor, getSubExpr);
@@ -745,6 +746,7 @@ bool Expr::canAppendPostfixExpression(bool appendingPostfixOperator) const {
745746
case ExprKind::OpenExistential:
746747
case ExprKind::MakeTemporarilyEscapable:
747748
case ExprKind::VarargExpansion:
749+
case ExprKind::PackExpansion:
748750
return false;
749751

750752
case ExprKind::Call:
@@ -918,6 +920,7 @@ bool Expr::isValidParentOfTypeExpr(Expr *typeExpr) const {
918920
case ExprKind::AutoClosure:
919921
case ExprKind::InOut:
920922
case ExprKind::VarargExpansion:
923+
case ExprKind::PackExpansion:
921924
case ExprKind::DynamicType:
922925
case ExprKind::RebindSelfInConstructor:
923926
case ExprKind::OpaqueValue:
@@ -1235,6 +1238,14 @@ VarargExpansionExpr *VarargExpansionExpr::createArrayExpansion(ASTContext &ctx,
12351238
return new (ctx) VarargExpansionExpr(AE, /*implicit*/ true, AE->getType());
12361239
}
12371240

1241+
PackExpansionExpr *
1242+
PackExpansionExpr::create(ASTContext &ctx, Expr *patternExpr,
1243+
SourceLoc dotsLoc, bool implicit,
1244+
Type type) {
1245+
return new (ctx) PackExpansionExpr(patternExpr, dotsLoc,
1246+
implicit, type);
1247+
}
1248+
12381249
SequenceExpr *SequenceExpr::create(ASTContext &ctx, ArrayRef<Expr*> elements) {
12391250
assert(elements.size() & 1 && "even number of elements in sequence");
12401251
size_t bytes = totalSizeToAlloc<Expr *>(elements.size());

lib/SILGen/SILGenExpr.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ namespace {
447447
SGFContext C);
448448
RValue visitBridgeToObjCExpr(BridgeToObjCExpr *E, SGFContext C);
449449
RValue visitReifyPackExpr(ReifyPackExpr *E, SGFContext C);
450+
RValue visitPackExpansionExpr(PackExpansionExpr *E, SGFContext C);
450451
RValue visitBridgeFromObjCExpr(BridgeFromObjCExpr *E, SGFContext C);
451452
RValue visitConditionalBridgeFromObjCExpr(ConditionalBridgeFromObjCExpr *E,
452453
SGFContext C);
@@ -1517,6 +1518,12 @@ RValueEmitter::visitReifyPackExpr(ReifyPackExpr *E, SGFContext C) {
15171518
llvm_unreachable("Unimplemented!");
15181519
}
15191520

1521+
RValue
1522+
RValueEmitter::visitPackExpansionExpr(PackExpansionExpr *E,
1523+
SGFContext C) {
1524+
llvm_unreachable("not implemented for PackExpansionExpr");
1525+
}
1526+
15201527
RValue RValueEmitter::visitArchetypeToSuperExpr(ArchetypeToSuperExpr *E,
15211528
SGFContext C) {
15221529
ManagedValue archetype = SGF.emitRValueAsSingleValue(E->getSubExpr());

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3692,6 +3692,10 @@ namespace {
36923692
return expr;
36933693
}
36943694

3695+
Expr *visitPackExpansionExpr(PackExpansionExpr *expr) {
3696+
llvm_unreachable("not implemented for PackExpansionExpr");
3697+
}
3698+
36953699
Expr *visitDynamicTypeExpr(DynamicTypeExpr *expr) {
36963700
Expr *base = expr->getBase();
36973701
base = cs.coerceToRValue(base);

lib/Sema/CSGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2885,6 +2885,10 @@ namespace {
28852885
return variadicSeq;
28862886
}
28872887

2888+
Type visitPackExpansionExpr(PackExpansionExpr *expr) {
2889+
llvm_unreachable("not implemented for PackExpansionExpr");
2890+
}
2891+
28882892
Type visitDynamicTypeExpr(DynamicTypeExpr *expr) {
28892893
auto tv = CS.createTypeVariable(CS.getConstraintLocator(expr),
28902894
TVO_CanBindToNoEscape);

0 commit comments

Comments
 (0)