Skip to content

Commit b5bc7bd

Browse files
authored
Merge pull request #13577 from davezarzycki/nfc_tail_alloc_TupleShuffleExpr
2 parents 3f0e702 + 1040d18 commit b5bc7bd

File tree

3 files changed

+93
-46
lines changed

3 files changed

+93
-46
lines changed

include/swift/AST/Expr.h

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,17 @@ class alignas(8) Expr {
287287

288288
SWIFT_INLINE_BITFIELD_EMPTY(ImplicitConversionExpr, Expr);
289289

290-
SWIFT_INLINE_BITFIELD(TupleShuffleExpr, ImplicitConversionExpr, 2,
291-
TypeImpact : 2
290+
SWIFT_INLINE_BITFIELD_FULL(TupleShuffleExpr, ImplicitConversionExpr, 2+16+16+16,
291+
TypeImpact : 2,
292+
: NumPadBits,
293+
NumCallerDefaultArgs : 16,
294+
/// This contains an entry for each element in the Expr type. Each element
295+
/// specifies which index from the SubExpr that the destination element gets.
296+
/// If the element value is DefaultInitialize, then the destination value
297+
/// gets the default initializer for that tuple element value.
298+
NumElementMappings : 16,
299+
/// The arguments that are packed into the variadic element.
300+
NumVariadicArgs : 16
292301
);
293302

294303
SWIFT_INLINE_BITFIELD(InOutToPointerExpr, ImplicitConversionExpr, 1,
@@ -2842,7 +2851,20 @@ class UnevaluatedInstanceExpr : public ImplicitConversionExpr {
28422851
/// If hasScalarSource() is true, the subexpression should be treated
28432852
/// as if it were implicitly injected into a single-element tuple
28442853
/// type. Otherwise, the subexpression is known to have a tuple type.
2845-
class TupleShuffleExpr : public ImplicitConversionExpr {
2854+
class TupleShuffleExpr final : public ImplicitConversionExpr,
2855+
private llvm::TrailingObjects<TupleShuffleExpr, Expr *, int, unsigned> {
2856+
friend TrailingObjects;
2857+
2858+
size_t numTrailingObjects(OverloadToken<Expr *>) const {
2859+
return Bits.TupleShuffleExpr.NumCallerDefaultArgs;
2860+
}
2861+
size_t numTrailingObjects(OverloadToken<int>) const {
2862+
return Bits.TupleShuffleExpr.NumElementMappings;
2863+
}
2864+
size_t numTrailingObjects(OverloadToken<unsigned>) const {
2865+
return Bits.TupleShuffleExpr.NumVariadicArgs;
2866+
}
2867+
28462868
public:
28472869
enum : int {
28482870
/// The element mapping value indicating that a field of the destination
@@ -2877,41 +2899,48 @@ class TupleShuffleExpr : public ImplicitConversionExpr {
28772899
};
28782900

28792901
private:
2880-
/// This contains an entry for each element in the Expr type. Each element
2881-
/// specifies which index from the SubExpr that the destination element gets.
2882-
/// If the element value is DefaultInitialize, then the destination value
2883-
/// gets the default initializer for that tuple element value.
2884-
ArrayRef<int> ElementMapping;
2885-
28862902
/// If we're doing a varargs shuffle, this is the array type to build.
28872903
Type VarargsArrayTy;
28882904

28892905
/// If there are any default arguments, the owning function
28902906
/// declaration.
28912907
ConcreteDeclRef DefaultArgsOwner;
28922908

2893-
/// The arguments that are packed into the variadic element.
2894-
ArrayRef<unsigned> VariadicArgs;
2895-
2896-
MutableArrayRef<Expr *> CallerDefaultArgs;
2897-
2898-
public:
2899-
TupleShuffleExpr(Expr *subExpr, ArrayRef<int> elementMapping,
2909+
TupleShuffleExpr(Expr *subExpr, ArrayRef<int> elementMapping,
29002910
TypeImpact typeImpact,
29012911
ConcreteDeclRef defaultArgsOwner,
29022912
ArrayRef<unsigned> VariadicArgs,
29032913
Type VarargsArrayTy,
2904-
MutableArrayRef<Expr *> CallerDefaultArgs,
2914+
ArrayRef<Expr *> CallerDefaultArgs,
29052915
Type ty)
29062916
: ImplicitConversionExpr(ExprKind::TupleShuffle, subExpr, ty),
2907-
ElementMapping(elementMapping), VarargsArrayTy(VarargsArrayTy),
2908-
DefaultArgsOwner(defaultArgsOwner), VariadicArgs(VariadicArgs),
2909-
CallerDefaultArgs(CallerDefaultArgs)
2910-
{
2917+
VarargsArrayTy(VarargsArrayTy), DefaultArgsOwner(defaultArgsOwner) {
29112918
Bits.TupleShuffleExpr.TypeImpact = typeImpact;
2919+
Bits.TupleShuffleExpr.NumCallerDefaultArgs = CallerDefaultArgs.size();
2920+
Bits.TupleShuffleExpr.NumElementMappings = elementMapping.size();
2921+
Bits.TupleShuffleExpr.NumVariadicArgs = VariadicArgs.size();
2922+
std::uninitialized_copy(CallerDefaultArgs.begin(), CallerDefaultArgs.end(),
2923+
getTrailingObjects<Expr*>());
2924+
std::uninitialized_copy(elementMapping.begin(), elementMapping.end(),
2925+
getTrailingObjects<int>());
2926+
std::uninitialized_copy(VariadicArgs.begin(), VariadicArgs.end(),
2927+
getTrailingObjects<unsigned>());
29122928
}
29132929

2914-
ArrayRef<int> getElementMapping() const { return ElementMapping; }
2930+
public:
2931+
static TupleShuffleExpr *create(ASTContext &ctx, Expr *subExpr,
2932+
ArrayRef<int> elementMapping,
2933+
TypeImpact typeImpact,
2934+
ConcreteDeclRef defaultArgsOwner,
2935+
ArrayRef<unsigned> VariadicArgs,
2936+
Type VarargsArrayTy,
2937+
ArrayRef<Expr *> CallerDefaultArgs,
2938+
Type ty);
2939+
2940+
ArrayRef<int> getElementMapping() const {
2941+
return {getTrailingObjects<int>(),
2942+
Bits.TupleShuffleExpr.NumElementMappings};
2943+
}
29152944

29162945
/// What is the type impact of this shuffle?
29172946
TypeImpact getTypeImpact() const {
@@ -2935,16 +2964,25 @@ class TupleShuffleExpr : public ImplicitConversionExpr {
29352964
}
29362965

29372966
/// Retrieve the argument indices for the variadic arguments.
2938-
ArrayRef<unsigned> getVariadicArgs() const { return VariadicArgs; }
2967+
ArrayRef<unsigned> getVariadicArgs() const {
2968+
return {getTrailingObjects<unsigned>(),
2969+
Bits.TupleShuffleExpr.NumVariadicArgs};
2970+
}
29392971

29402972
/// Retrieve the owner of the default arguments.
29412973
ConcreteDeclRef getDefaultArgsOwner() const { return DefaultArgsOwner; }
29422974

29432975
/// Retrieve the caller-defaulted arguments.
2944-
ArrayRef<Expr *> getCallerDefaultArgs() const { return CallerDefaultArgs; }
2976+
ArrayRef<Expr *> getCallerDefaultArgs() const {
2977+
return {getTrailingObjects<Expr*>(),
2978+
Bits.TupleShuffleExpr.NumCallerDefaultArgs};
2979+
}
29452980

29462981
/// Retrieve the caller-defaulted arguments.
2947-
MutableArrayRef<Expr *> getCallerDefaultArgs() { return CallerDefaultArgs; }
2982+
MutableArrayRef<Expr *> getCallerDefaultArgs() {
2983+
return {getTrailingObjects<Expr*>(),
2984+
Bits.TupleShuffleExpr.NumCallerDefaultArgs};
2985+
}
29482986

29492987
static bool classof(const Expr *E) {
29502988
return E->getKind() == ExprKind::TupleShuffle;

lib/AST/Expr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,24 @@ CaptureListExpr *CaptureListExpr::create(ASTContext &ctx,
13951395
return ::new(mem) CaptureListExpr(captureList, closureBody);
13961396
}
13971397

1398+
TupleShuffleExpr *TupleShuffleExpr::create(ASTContext &ctx,
1399+
Expr *subExpr,
1400+
ArrayRef<int> elementMapping,
1401+
TypeImpact typeImpact,
1402+
ConcreteDeclRef defaultArgsOwner,
1403+
ArrayRef<unsigned> VariadicArgs,
1404+
Type VarargsArrayTy,
1405+
ArrayRef<Expr *> CallerDefaultArgs,
1406+
Type ty) {
1407+
auto size = totalSizeToAlloc<Expr*, int, unsigned>(CallerDefaultArgs.size(),
1408+
elementMapping.size(),
1409+
VariadicArgs.size());
1410+
auto mem = ctx.Allocate(size, alignof(TupleShuffleExpr));
1411+
return ::new(mem) TupleShuffleExpr(subExpr, elementMapping, typeImpact,
1412+
defaultArgsOwner, VariadicArgs,
1413+
VarargsArrayTy, CallerDefaultArgs, ty);
1414+
}
1415+
13981416
SourceRange TupleExpr::getSourceRange() const {
13991417
SourceLoc start = SourceLoc();
14001418
SourceLoc end = SourceLoc();

lib/Sema/CSApply.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4975,16 +4975,14 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
49754975
}
49764976

49774977
// Create the tuple shuffle.
4978-
ArrayRef<int> mapping = tc.Context.AllocateCopy(sources);
4979-
auto callerDefaultArgsCopy = tc.Context.AllocateCopy(callerDefaultArgs);
49804978
return
4981-
cs.cacheType(new (tc.Context) TupleShuffleExpr(
4982-
expr, mapping,
4979+
cs.cacheType(TupleShuffleExpr::create(tc.Context,
4980+
expr, sources,
49834981
TupleShuffleExpr::TupleToTuple,
49844982
callee,
4985-
tc.Context.AllocateCopy(variadicArgs),
4983+
variadicArgs,
49864984
arrayType,
4987-
callerDefaultArgsCopy,
4985+
callerDefaultArgs,
49884986
toSugarType));
49894987
}
49904988

@@ -5073,13 +5071,13 @@ Expr *ExprRewriter::coerceScalarToTuple(Expr *expr, TupleType *toTuple,
50735071
Type destSugarTy = hasInit? toTuple
50745072
: TupleType::get(sugarFields, tc.Context);
50755073

5076-
return cs.cacheType(new (tc.Context) TupleShuffleExpr(expr,
5077-
tc.Context.AllocateCopy(elements),
5074+
return cs.cacheType(TupleShuffleExpr::create(tc.Context, expr,
5075+
elements,
50785076
TupleShuffleExpr::ScalarToTuple,
50795077
callee,
5080-
tc.Context.AllocateCopy(variadicArgs),
5078+
variadicArgs,
50815079
arrayType,
5082-
tc.Context.AllocateCopy(callerDefaultArgs),
5080+
callerDefaultArgs,
50835081
destSugarTy));
50845082
}
50855083

@@ -5704,17 +5702,10 @@ Expr *ExprRewriter::coerceCallArguments(
57045702
}
57055703

57065704
// Create the tuple shuffle.
5707-
ArrayRef<int> mapping = tc.Context.AllocateCopy(sources);
5708-
auto callerDefaultArgsCopy = tc.Context.AllocateCopy(callerDefaultArgs);
5709-
return
5710-
cs.cacheType(new (tc.Context) TupleShuffleExpr(
5711-
arg, mapping,
5712-
typeImpact,
5713-
callee,
5714-
tc.Context.AllocateCopy(variadicArgs),
5715-
sliceType,
5716-
callerDefaultArgsCopy,
5717-
paramType));
5705+
return cs.cacheType(TupleShuffleExpr::create(tc.Context, arg, sources,
5706+
typeImpact, callee, variadicArgs,
5707+
sliceType, callerDefaultArgs,
5708+
paramType));
57185709
}
57195710

57205711
static ClosureExpr *getClosureLiteralExpr(Expr *expr) {

0 commit comments

Comments
 (0)