Skip to content

Commit 428c709

Browse files
committed
AST: Remove argument list-specific parts of TupleShuffleExpr
Before extending TupleShuffleExpr to represent all tuple conversions allowed by the constraint solver, remove the parts of TupleShuffleExpr that are no longer needed; this is support for default arguments, varargs, and scalar-to-tuple and tuple-to-scalar conversions.
1 parent d470e9d commit 428c709

File tree

7 files changed

+24
-253
lines changed

7 files changed

+24
-253
lines changed

include/swift/AST/Expr.h

Lines changed: 9 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,10 @@ class alignas(8) Expr {
290290

291291
SWIFT_INLINE_BITFIELD_EMPTY(ImplicitConversionExpr, Expr);
292292

293-
SWIFT_INLINE_BITFIELD_FULL(TupleShuffleExpr, ImplicitConversionExpr, 2+16+16+16,
294-
TypeImpact : 2,
295-
: NumPadBits,
296-
NumCallerDefaultArgs : 16,
293+
SWIFT_INLINE_BITFIELD_FULL(TupleShuffleExpr, ImplicitConversionExpr, 16,
297294
/// This contains an entry for each element in the Expr type. Each element
298295
/// specifies which index from the SubExpr that the destination element gets.
299-
/// If the element value is DefaultInitialize, then the destination value
300-
/// gets the default initializer for that tuple element value.
301-
NumElementMappings : 16,
302-
/// The arguments that are packed into the variadic element.
303-
NumVariadicArgs : 16
296+
NumElementMappings : 16
304297
);
305298

306299
SWIFT_INLINE_BITFIELD_FULL(ArgumentShuffleExpr, ImplicitConversionExpr, 2+16+16+16,
@@ -2969,141 +2962,31 @@ class UnevaluatedInstanceExpr : public ImplicitConversionExpr {
29692962

29702963
/// TupleShuffleExpr - This represents a permutation of a tuple value to a new
29712964
/// tuple type.
2972-
///
2973-
/// If hasScalarSource() is true, the subexpression should be treated
2974-
/// as if it were implicitly injected into a single-element tuple
2975-
/// type. Otherwise, the subexpression is known to have a tuple type.
29762965
class TupleShuffleExpr final : public ImplicitConversionExpr,
2977-
private llvm::TrailingObjects<TupleShuffleExpr, Expr *, int, unsigned> {
2966+
private llvm::TrailingObjects<TupleShuffleExpr, unsigned> {
29782967
friend TrailingObjects;
29792968

2980-
size_t numTrailingObjects(OverloadToken<Expr *>) const {
2981-
return Bits.TupleShuffleExpr.NumCallerDefaultArgs;
2982-
}
2983-
size_t numTrailingObjects(OverloadToken<int>) const {
2984-
return Bits.TupleShuffleExpr.NumElementMappings;
2985-
}
29862969
size_t numTrailingObjects(OverloadToken<unsigned>) const {
2987-
return Bits.TupleShuffleExpr.NumVariadicArgs;
2970+
return Bits.TupleShuffleExpr.NumElementMappings;
29882971
}
29892972

2990-
public:
2991-
enum : int {
2992-
/// The element mapping value indicating that a field of the destination
2993-
/// tuple should be default-initialized.
2994-
DefaultInitialize = -1,
2995-
/// The element mapping is part of the variadic field.
2996-
Variadic = -2,
2997-
/// The element mapping value indicating that the field of the
2998-
/// destination tuple should be default-initialized with an expression
2999-
/// provided by the caller.
3000-
/// FIXME: Yet another indication that TupleShuffleExpr uses the wrong
3001-
/// formulation.
3002-
CallerDefaultInitialize = -3
3003-
};
3004-
3005-
enum TypeImpact {
3006-
/// The source value is a tuple which is destructured and modified to
3007-
/// create the result, which is a tuple.
3008-
TupleToTuple,
3009-
3010-
/// The source value is a tuple which is destructured and modified to
3011-
/// create the result, which is a scalar because it has one element and
3012-
/// no labels.
3013-
TupleToScalar,
3014-
3015-
/// The source value is an individual value (possibly one with tuple
3016-
/// type) which is inserted into a particular position in the result,
3017-
/// which is a tuple.
3018-
ScalarToTuple
3019-
3020-
// (TupleShuffleExprs are never created for a scalar-to-scalar conversion.)
3021-
};
3022-
30232973
private:
3024-
/// If we're doing a varargs shuffle, this is the array type to build.
3025-
Type VarargsArrayTy;
3026-
3027-
/// If there are any default arguments, the owning function
3028-
/// declaration.
3029-
ConcreteDeclRef DefaultArgsOwner;
3030-
3031-
TupleShuffleExpr(Expr *subExpr, ArrayRef<int> elementMapping,
3032-
TypeImpact typeImpact,
3033-
ConcreteDeclRef defaultArgsOwner,
3034-
ArrayRef<unsigned> VariadicArgs,
3035-
Type VarargsArrayTy,
3036-
ArrayRef<Expr *> CallerDefaultArgs,
2974+
TupleShuffleExpr(Expr *subExpr, ArrayRef<unsigned> elementMapping,
30372975
Type ty)
3038-
: ImplicitConversionExpr(ExprKind::TupleShuffle, subExpr, ty),
3039-
VarargsArrayTy(VarargsArrayTy), DefaultArgsOwner(defaultArgsOwner) {
3040-
Bits.TupleShuffleExpr.TypeImpact = typeImpact;
3041-
Bits.TupleShuffleExpr.NumCallerDefaultArgs = CallerDefaultArgs.size();
2976+
: ImplicitConversionExpr(ExprKind::TupleShuffle, subExpr, ty) {
30422977
Bits.TupleShuffleExpr.NumElementMappings = elementMapping.size();
3043-
Bits.TupleShuffleExpr.NumVariadicArgs = VariadicArgs.size();
3044-
std::uninitialized_copy(CallerDefaultArgs.begin(), CallerDefaultArgs.end(),
3045-
getTrailingObjects<Expr*>());
30462978
std::uninitialized_copy(elementMapping.begin(), elementMapping.end(),
3047-
getTrailingObjects<int>());
3048-
std::uninitialized_copy(VariadicArgs.begin(), VariadicArgs.end(),
30492979
getTrailingObjects<unsigned>());
30502980
}
30512981

30522982
public:
30532983
static TupleShuffleExpr *create(ASTContext &ctx, Expr *subExpr,
3054-
ArrayRef<int> elementMapping,
3055-
TypeImpact typeImpact,
3056-
ConcreteDeclRef defaultArgsOwner,
3057-
ArrayRef<unsigned> VariadicArgs,
3058-
Type VarargsArrayTy,
3059-
ArrayRef<Expr *> CallerDefaultArgs,
2984+
ArrayRef<unsigned> elementMapping,
30602985
Type ty);
30612986

3062-
ArrayRef<int> getElementMapping() const {
3063-
return {getTrailingObjects<int>(),
3064-
static_cast<size_t>(Bits.TupleShuffleExpr.NumElementMappings)};
3065-
}
3066-
3067-
/// What is the type impact of this shuffle?
3068-
TypeImpact getTypeImpact() const {
3069-
return TypeImpact(Bits.TupleShuffleExpr.TypeImpact);
3070-
}
3071-
3072-
bool isSourceScalar() const {
3073-
return getTypeImpact() == ScalarToTuple;
3074-
}
3075-
3076-
bool isResultScalar() const {
3077-
return getTypeImpact() == TupleToScalar;
3078-
}
3079-
3080-
Type getVarargsArrayType() const {
3081-
assert(!VarargsArrayTy.isNull());
3082-
return VarargsArrayTy;
3083-
}
3084-
Type getVarargsArrayTypeOrNull() const {
3085-
return VarargsArrayTy;
3086-
}
3087-
3088-
/// Retrieve the argument indices for the variadic arguments.
3089-
ArrayRef<unsigned> getVariadicArgs() const {
2987+
ArrayRef<unsigned> getElementMapping() const {
30902988
return {getTrailingObjects<unsigned>(),
3091-
static_cast<size_t>(Bits.TupleShuffleExpr.NumVariadicArgs)};
3092-
}
3093-
3094-
/// Retrieve the owner of the default arguments.
3095-
ConcreteDeclRef getDefaultArgsOwner() const { return DefaultArgsOwner; }
3096-
3097-
/// Retrieve the caller-defaulted arguments.
3098-
ArrayRef<Expr *> getCallerDefaultArgs() const {
3099-
return {getTrailingObjects<Expr*>(),
3100-
static_cast<size_t>(Bits.TupleShuffleExpr.NumCallerDefaultArgs)};
3101-
}
3102-
3103-
/// Retrieve the caller-defaulted arguments.
3104-
MutableArrayRef<Expr *> getCallerDefaultArgs() {
3105-
return {getTrailingObjects<Expr*>(),
3106-
static_cast<size_t>(Bits.TupleShuffleExpr.NumCallerDefaultArgs)};
2989+
static_cast<size_t>(Bits.TupleShuffleExpr.NumElementMappings)};
31072990
}
31082991

31092992
static bool classof(const Expr *E) {

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,37 +2119,12 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
21192119
}
21202120
void visitTupleShuffleExpr(TupleShuffleExpr *E) {
21212121
printCommon(E, "tuple_shuffle_expr");
2122-
switch (E->getTypeImpact()) {
2123-
case TupleShuffleExpr::ScalarToTuple:
2124-
OS << " scalar_to_tuple";
2125-
break;
2126-
case TupleShuffleExpr::TupleToTuple:
2127-
OS << " tuple_to_tuple";
2128-
break;
2129-
case TupleShuffleExpr::TupleToScalar:
2130-
OS << " tuple_to_scalar";
2131-
break;
2132-
}
21332122
OS << " elements=[";
21342123
for (unsigned i = 0, e = E->getElementMapping().size(); i != e; ++i) {
21352124
if (i) OS << ", ";
21362125
OS << E->getElementMapping()[i];
21372126
}
2138-
OS << "]";
2139-
OS << " variadic_sources=[";
2140-
interleave(E->getVariadicArgs(),
2141-
[&](unsigned source) {
2142-
OS << source;
2143-
},
2144-
[&] { OS << ", "; });
2145-
OS << "]";
2146-
2147-
if (auto defaultArgsOwner = E->getDefaultArgsOwner()) {
2148-
OS << " default_args_owner=";
2149-
defaultArgsOwner.dump(OS);
2150-
}
2151-
2152-
OS << "\n";
2127+
OS << "]\n";
21532128
printRec(E->getSubExpr());
21542129
PrintWithColorRAII(OS, ParenthesisColor) << ')';
21552130
}

lib/AST/ASTVerifier.cpp

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1992,57 +1992,22 @@ class Verifier : public ASTWalker {
19921992
PrettyStackTraceExpr debugStack(Ctx, "verifying TupleShuffleExpr", E);
19931993

19941994
auto getSubElementType = [&](unsigned i) {
1995-
if (E->isSourceScalar()) {
1996-
assert(i == 0);
1997-
return E->getSubExpr()->getType();
1998-
} else {
1999-
return (E->getSubExpr()->getType()->castTo<TupleType>()
2000-
->getElementType(i));
2001-
}
1995+
return (E->getSubExpr()->getType()->castTo<TupleType>()
1996+
->getElementType(i));
20021997
};
20031998

20041999
/// Retrieve the ith element type from the resulting tuple type.
20052000
auto getOuterElementType = [&](unsigned i) -> Type {
2006-
if (E->isResultScalar()) {
2007-
assert(i == 0);
2008-
return E->getType()->getWithoutParens();
2009-
} else {
2010-
return E->getType()->castTo<TupleType>()->getElementType(i);
2011-
}
2001+
return E->getType()->castTo<TupleType>()->getElementType(i);
20122002
};
20132003

2014-
Type varargsType;
2015-
unsigned callerDefaultArgIndex = 0;
20162004
for (unsigned i = 0, e = E->getElementMapping().size(); i != e; ++i) {
20172005
int subElem = E->getElementMapping()[i];
2018-
if (subElem == TupleShuffleExpr::DefaultInitialize)
2019-
continue;
2020-
if (subElem == TupleShuffleExpr::Variadic) {
2021-
varargsType = (E->getType()->castTo<TupleType>()
2022-
->getElement(i).getVarargBaseTy());
2023-
break;
2024-
}
2025-
if (subElem == TupleShuffleExpr::CallerDefaultInitialize) {
2026-
auto init = E->getCallerDefaultArgs()[callerDefaultArgIndex++];
2027-
if (!getOuterElementType(i)->isEqual(init->getType())) {
2028-
Out << "Type mismatch in TupleShuffleExpr\n";
2029-
abort();
2030-
}
2031-
continue;
2032-
}
20332006
if (!getOuterElementType(i)->isEqual(getSubElementType(subElem))) {
20342007
Out << "Type mismatch in TupleShuffleExpr\n";
20352008
abort();
20362009
}
20372010
}
2038-
if (varargsType) {
2039-
for (auto sourceIdx : E->getVariadicArgs()) {
2040-
if (!getSubElementType(sourceIdx)->isEqual(varargsType)) {
2041-
Out << "Vararg type mismatch in TupleShuffleExpr\n";
2042-
abort();
2043-
}
2044-
}
2045-
}
20462011

20472012
verifyCheckedBase(E);
20482013
}

lib/AST/ASTWalker.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
646646
return nullptr;
647647
}
648648

649-
for (auto &defaultArg : E->getCallerDefaultArgs()) {
650-
if (Expr *newDefaultArg = doIt(defaultArg))
651-
defaultArg = newDefaultArg;
652-
else
653-
return nullptr;
654-
}
655-
656649
return E;
657650
}
658651

lib/AST/Expr.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,20 +1335,11 @@ CaptureListExpr *CaptureListExpr::create(ASTContext &ctx,
13351335

13361336
TupleShuffleExpr *TupleShuffleExpr::create(ASTContext &ctx,
13371337
Expr *subExpr,
1338-
ArrayRef<int> elementMapping,
1339-
TypeImpact typeImpact,
1340-
ConcreteDeclRef defaultArgsOwner,
1341-
ArrayRef<unsigned> VariadicArgs,
1342-
Type VarargsArrayTy,
1343-
ArrayRef<Expr *> CallerDefaultArgs,
1338+
ArrayRef<unsigned> elementMapping,
13441339
Type ty) {
1345-
auto size = totalSizeToAlloc<Expr*, int, unsigned>(CallerDefaultArgs.size(),
1346-
elementMapping.size(),
1347-
VariadicArgs.size());
1340+
auto size = totalSizeToAlloc<unsigned>(elementMapping.size());
13481341
auto mem = ctx.Allocate(size, alignof(TupleShuffleExpr));
1349-
return ::new(mem) TupleShuffleExpr(subExpr, elementMapping, typeImpact,
1350-
defaultArgsOwner, VariadicArgs,
1351-
VarargsArrayTy, CallerDefaultArgs, ty);
1342+
return ::new(mem) TupleShuffleExpr(subExpr, elementMapping, ty);
13521343
}
13531344

13541345
ArgumentShuffleExpr *ArgumentShuffleExpr::create(ASTContext &ctx,

lib/SILGen/SILGenExpr.cpp

Lines changed: 7 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2289,19 +2289,14 @@ static void emitTupleShuffleExprInto(RValueEmitter &emitter,
22892289
// Map outer initializations into a tuple of inner initializations:
22902290
// - fill out the initialization elements with null
22912291
TupleInitialization innerTupleInit;
2292-
if (E->isSourceScalar()) {
2293-
innerTupleInit.SubInitializations.push_back(nullptr);
2294-
} else {
2295-
CanTupleType innerTuple =
2296-
cast<TupleType>(E->getSubExpr()->getType()->getCanonicalType());
2297-
innerTupleInit.SubInitializations.resize(innerTuple->getNumElements());
2298-
}
2292+
2293+
CanTupleType innerTuple =
2294+
cast<TupleType>(E->getSubExpr()->getType()->getCanonicalType());
2295+
innerTupleInit.SubInitializations.resize(innerTuple->getNumElements());
22992296

23002297
// Map all the outer initializations to their appropriate targets.
23012298
for (unsigned outerIndex = 0; outerIndex != outerInits.size(); outerIndex++) {
23022299
auto innerMapping = E->getElementMapping()[outerIndex];
2303-
assert(innerMapping >= 0 &&
2304-
"non-argument tuple shuffle with default arguments or variadics?");
23052300
innerTupleInit.SubInitializations[innerMapping] =
23062301
std::move(outerInits[outerIndex]);
23072302
}
@@ -2313,22 +2308,13 @@ static void emitTupleShuffleExprInto(RValueEmitter &emitter,
23132308
#endif
23142309

23152310
// Emit the sub-expression into the tuple initialization we just built.
2316-
if (E->isSourceScalar()) {
2317-
emitter.SGF.emitExprInto(E->getSubExpr(),
2318-
innerTupleInit.SubInitializations[0].get());
2319-
} else {
2320-
emitter.SGF.emitExprInto(E->getSubExpr(), &innerTupleInit);
2321-
}
2311+
emitter.SGF.emitExprInto(E->getSubExpr(), &innerTupleInit);
23222312

23232313
outerTupleInit->finishInitialization(emitter.SGF);
23242314
}
23252315

23262316
RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
23272317
SGFContext C) {
2328-
// FIXME: Once we're no longer using this code path for enum element payloads,
2329-
// also assert that !E->isSourceScalar().
2330-
assert(!E->isResultScalar());
2331-
23322318
// If we're emitting into an initialization, we can try shuffling the
23332319
// elements of the initialization.
23342320
if (Initialization *I = C.getEmitInto()) {
@@ -2340,12 +2326,8 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
23402326

23412327
// Emit the sub-expression tuple and destructure it into elements.
23422328
SmallVector<RValue, 4> elements;
2343-
if (E->isSourceScalar()) {
2344-
elements.push_back(visit(E->getSubExpr()));
2345-
} else {
2346-
visit(E->getSubExpr()).extractElements(elements);
2347-
}
2348-
2329+
visit(E->getSubExpr()).extractElements(elements);
2330+
23492331
// Prepare a new tuple to hold the shuffled result.
23502332
RValue result(E->getType()->getCanonicalType());
23512333

@@ -2359,11 +2341,6 @@ RValue RValueEmitter::visitTupleShuffleExpr(TupleShuffleExpr *E,
23592341
"ran out of shuffle indexes before running out of fields?!");
23602342
int shuffleIndex = *shuffleIndexIterator++;
23612343

2362-
assert(shuffleIndex != TupleShuffleExpr::DefaultInitialize &&
2363-
shuffleIndex != TupleShuffleExpr::CallerDefaultInitialize &&
2364-
shuffleIndex != TupleShuffleExpr::Variadic &&
2365-
"Only argument tuples can have default initializers & varargs");
2366-
23672344
// Map from a different tuple element.
23682345
result.addElement(
23692346
std::move(elements[shuffleIndex]).ensurePlusOne(SGF, E));

0 commit comments

Comments
 (0)