Skip to content

Commit 38029ba

Browse files
committed
[ABI] Store a ValueOwnership directly, not a manual encoding of it.
1 parent 0de8ae9 commit 38029ba

File tree

5 files changed

+47
-60
lines changed

5 files changed

+47
-60
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -644,32 +644,18 @@ using FunctionTypeFlags = TargetFunctionTypeFlags<size_t>;
644644

645645
template <typename int_type>
646646
class TargetParameterTypeFlags {
647-
enum : int_type {
648-
InOutMask = 1 << 0,
649-
SharedMask = 1 << 1,
650-
OwnedMask = 1 << 2,
651-
VariadicMask = 1 << 3,
652-
};
647+
enum : int_type { ValueOwnershipMask = 0x7F, VariadicMask = 0x80 };
653648
int_type Data;
654649

655650
constexpr TargetParameterTypeFlags(int_type Data) : Data(Data) {}
656651

657652
public:
658653
constexpr TargetParameterTypeFlags() : Data(0) {}
659654

660-
constexpr TargetParameterTypeFlags<int_type> withInOut(bool isInOut) const {
661-
return TargetParameterTypeFlags<int_type>((Data & ~InOutMask) |
662-
(isInOut ? InOutMask : 0));
663-
}
664-
665-
constexpr TargetParameterTypeFlags<int_type> withShared(bool isShared) const {
666-
return TargetParameterTypeFlags<int_type>((Data & ~SharedMask) |
667-
(isShared ? SharedMask : 0));
668-
}
669-
670-
constexpr TargetParameterTypeFlags<int_type> withOwned(bool isOwned) const {
671-
return TargetParameterTypeFlags<int_type>((Data & ~OwnedMask) |
672-
(isOwned ? OwnedMask : 0));
655+
constexpr TargetParameterTypeFlags<int_type>
656+
withValueOwnership(ValueOwnership ownership) const {
657+
return TargetParameterTypeFlags<int_type>((Data & ~ValueOwnershipMask) |
658+
(int_type)ownership);
673659
}
674660

675661
constexpr TargetParameterTypeFlags<int_type>
@@ -679,20 +665,10 @@ class TargetParameterTypeFlags {
679665
}
680666

681667
bool isNone() const { return Data == 0; }
682-
bool isInOut() const { return Data & InOutMask; }
683-
bool isShared() const { return Data & SharedMask; }
684-
bool isOwned() const { return Data & OwnedMask; }
685668
bool isVariadic() const { return Data & VariadicMask; }
686669

687670
ValueOwnership getValueOwnership() const {
688-
if (isInOut())
689-
return ValueOwnership::InOut;
690-
else if (isShared())
691-
return ValueOwnership::Shared;
692-
else if (isOwned())
693-
return ValueOwnership::Owned;
694-
695-
return ValueOwnership::Default;
671+
return (ValueOwnership)(Data & ValueOwnershipMask);
696672
}
697673

698674
int_type getIntValue() const { return Data; }

include/swift/Demangling/TypeDecoder.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class FunctionParam {
5656
void setType(BuiltType type) { Type = type; }
5757

5858
void setVariadic() { Flags = Flags.withVariadic(true); }
59-
void setShared() { Flags = Flags.withShared(true); }
60-
void setOwned() { Flags = Flags.withOwned(true); }
61-
void setInOut() { Flags = Flags.withInOut(true); }
59+
void setValueOwnership(ValueOwnership ownership) {
60+
Flags = Flags.withValueOwnership(ownership);
61+
}
6262
void setFlags(ParameterFlags flags) { Flags = flags; };
6363

6464
FunctionParam withLabel(StringRef label) const {
@@ -532,23 +532,23 @@ class TypeDecoder {
532532
[&](const Demangle::NodePointer &typeNode,
533533
FunctionParam<BuiltType> &param) -> bool {
534534
Demangle::NodePointer node = typeNode;
535-
switch (node->getKind()) {
536-
case NodeKind::InOut:
537-
param.setInOut();
535+
536+
auto setOwnership = [&](ValueOwnership ownership) {
537+
param.setValueOwnership(ownership);
538538
node = node->getFirstChild();
539539
hasParamFlags = true;
540+
};
541+
switch (node->getKind()) {
542+
case NodeKind::InOut:
543+
setOwnership(ValueOwnership::InOut);
540544
break;
541545

542546
case NodeKind::Shared:
543-
param.setShared();
544-
hasParamFlags = true;
545-
node = node->getFirstChild();
547+
setOwnership(ValueOwnership::Shared);
546548
break;
547549

548550
case NodeKind::Owned:
549-
param.setOwned();
550-
hasParamFlags = true;
551-
node = node->getFirstChild();
551+
setOwnership(ValueOwnership::Owned);
552552
break;
553553

554554
default:

lib/IRGen/GenMeta.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -838,11 +838,10 @@ namespace {
838838
auto param = params[index];
839839
auto flags = param.getParameterFlags();
840840

841-
auto parameterFlags = ParameterFlags()
842-
.withInOut(flags.isInOut())
843-
.withShared(flags.isShared())
844-
.withOwned(flags.isOwned())
845-
.withVariadic(flags.isVariadic());
841+
auto parameterFlags =
842+
ParameterFlags()
843+
.withValueOwnership(flags.getValueOwnership())
844+
.withVariadic(flags.isVariadic());
846845

847846
processor(index, getFunctionParameterRef(param), parameterFlags);
848847
}

lib/RemoteAST/RemoteAST.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,11 @@ class RemoteASTTypeBuilder {
350350

351351
auto label = Ctx.getIdentifier(param.getLabel());
352352
auto flags = param.getFlags();
353+
auto ownership = flags.getValueOwnership();
353354
auto parameterFlags = ParameterTypeFlags()
354-
.withInOut(flags.isInOut())
355-
.withShared(flags.isShared())
356-
.withOwned(flags.isOwned())
355+
.withInOut(ownership == ValueOwnership::InOut)
356+
.withShared(ownership == ValueOwnership::Shared)
357+
.withOwned(ownership == ValueOwnership::Owned)
357358
.withVariadic(flags.isVariadic());
358359

359360
funcParams.push_back(AnyFunctionType::Param(type, label, parameterFlags));

unittests/Reflection/TypeRef.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ TEST(TypeRefTest, UniqueFunctionTypeRef) {
148148

149149
// Test parameter with and without inout/shared/variadic and/or label.
150150
ParameterFlags paramFlags;
151-
auto inoutFlags = paramFlags.withInOut(true);
151+
auto inoutFlags = paramFlags.withValueOwnership(ValueOwnership::InOut);
152152
auto variadicFlags = paramFlags.withVariadic(true);
153-
auto sharedFlags = paramFlags.withShared(true);
153+
auto sharedFlags = paramFlags.withValueOwnership(ValueOwnership::Shared);
154+
auto ownedFlags = paramFlags.withValueOwnership(ValueOwnership::Owned);
154155

155156
auto F6 = Builder.createFunctionType({Param1.withFlags(inoutFlags)}, Result,
156157
FunctionTypeFlags());
@@ -170,17 +171,27 @@ TEST(TypeRefTest, UniqueFunctionTypeRef) {
170171
Result, FunctionTypeFlags());
171172
EXPECT_EQ(F8, F8_1);
172173

173-
auto F9 = Builder.createFunctionType({Param1}, Result, FunctionTypeFlags());
174-
auto F9_1 = Builder.createFunctionType({Param1.withLabel("foo")}, Result,
174+
auto F9 = Builder.createFunctionType({Param1.withFlags(ownedFlags)}, Result,
175+
FunctionTypeFlags());
176+
auto F9_1 = Builder.createFunctionType({Param1.withFlags(ownedFlags)},
177+
Result, FunctionTypeFlags());
178+
EXPECT_EQ(F9, F9_1);
179+
180+
auto F10 = Builder.createFunctionType({Param1}, Result, FunctionTypeFlags());
181+
auto F10_1 = Builder.createFunctionType({Param1.withLabel("foo")}, Result,
175182
FunctionTypeFlags());
176-
EXPECT_NE(F9, F9_1);
183+
EXPECT_NE(F10, F10_1);
177184

178185
EXPECT_NE(F6, F7);
179186
EXPECT_NE(F6, F8);
180187
EXPECT_NE(F6, F9);
188+
EXPECT_NE(F6, F10);
181189
EXPECT_NE(F7, F8);
182190
EXPECT_NE(F7, F9);
191+
EXPECT_NE(F7, F10);
183192
EXPECT_NE(F8, F9);
193+
EXPECT_NE(F8, F10);
194+
EXPECT_NE(F9, F10);
184195

185196
auto VoidVoid1 =
186197
Builder.createFunctionType(VoidParams, VoidResult, FunctionTypeFlags());
@@ -191,14 +202,14 @@ TEST(TypeRefTest, UniqueFunctionTypeRef) {
191202
EXPECT_NE(VoidVoid1, F1);
192203

193204
// Test escaping.
194-
auto F10 = Builder.createFunctionType(Parameters1, Result,
195-
FunctionTypeFlags().withEscaping(true));
196205
auto F11 = Builder.createFunctionType(Parameters1, Result,
197206
FunctionTypeFlags().withEscaping(true));
198-
auto F12 = Builder.createFunctionType(
207+
auto F12 = Builder.createFunctionType(Parameters1, Result,
208+
FunctionTypeFlags().withEscaping(true));
209+
auto F13 = Builder.createFunctionType(
199210
Parameters1, Result, FunctionTypeFlags().withEscaping(false));
200-
EXPECT_EQ(F10, F11);
201-
EXPECT_NE(F10, F12);
211+
EXPECT_EQ(F11, F12);
212+
EXPECT_NE(F11, F13);
202213
}
203214

204215
TEST(TypeRefTest, UniqueProtocolTypeRef) {

0 commit comments

Comments
 (0)