Skip to content

Commit c537cd2

Browse files
committed
[interop][SwiftToCxx] NFC, move swift::_impl::OpaqueStorage into _SwiftCxxInteroperability shims header
1 parent 330fc0b commit c537cd2

File tree

5 files changed

+50
-60
lines changed

5 files changed

+50
-60
lines changed

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
152152
if (isOpaqueLayout) {
153153
os << " _storage = ";
154154
printer.printSwiftImplQualifier();
155-
os << cxx_synthesis::getCxxOpaqueStorageClassName() << "(vwTable);\n";
155+
os << cxx_synthesis::getCxxOpaqueStorageClassName()
156+
<< "(vwTable->size, vwTable->getAlignment());\n";
156157
}
157158
os << " vwTable->initializeWithCopy(_getOpaquePointer(), const_cast<char "
158159
"*>(other._getOpaquePointer()), metadata._0);\n";
@@ -172,10 +173,12 @@ void ClangValueTypePrinter::printValueTypeDecl(
172173
// Print out private default constructor.
173174
os << " inline ";
174175
printer.printBaseName(typeDecl);
176+
// FIXME: make noexcept.
175177
if (isOpaqueLayout) {
176178
os << "(";
177179
printer.printSwiftImplQualifier();
178-
os << "ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable) {}\n";
180+
os << "ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable->size, "
181+
"vwTable->getAlignment()) {}\n";
179182
} else {
180183
os << "() {}\n";
181184
}

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ static void printValueWitnessTable(raw_ostream &os) {
111111
#define VOID_TYPE "void"
112112
#include "swift/ABI/ValueWitness.def"
113113

114+
membersOS << "\n constexpr size_t getAlignment() const { return (flags & "
115+
<< TargetValueWitnessFlags<uint64_t>::AlignmentMask << ") + 1; }\n";
114116
os << "\nstruct ValueWitnessTable {\n" << membersOS.str() << "};\n\n";
115117
membersOS.str().clear();
116118

@@ -154,43 +156,7 @@ static void printTypeMetadataResponseType(SwiftToClangInteropContext &ctx,
154156
funcSig.parameterTypes[0]);
155157
}
156158

157-
static void printSwiftResilientStorageClass(raw_ostream &os) {
158-
auto name = cxx_synthesis::getCxxOpaqueStorageClassName();
159-
static_assert(TargetValueWitnessFlags<uint64_t>::AlignmentMask ==
160-
TargetValueWitnessFlags<uint32_t>::AlignmentMask,
161-
"alignment mask doesn't match");
162-
os << "/// Container for an opaque Swift value, like resilient struct.\n";
163-
os << "class " << name << " {\n";
164-
os << "public:\n";
165-
os << " inline " << name << "() noexcept : storage(nullptr) { }\n";
166-
os << " inline " << name
167-
<< "(ValueWitnessTable * _Nonnull vwTable) noexcept : storage("
168-
"reinterpret_cast<char *>(opaqueAlloc(vwTable->size, (vwTable->flags &"
169-
<< TargetValueWitnessFlags<uint64_t>::AlignmentMask << ") + 1))) { }\n";
170-
os << " inline " << name << "(" << name
171-
<< "&& other) noexcept : storage(other.storage) { other.storage = "
172-
"nullptr; }\n";
173-
os << " inline " << name << "(const " << name << "&) noexcept = delete;\n";
174-
os << " inline ~" << name
175-
<< "() noexcept { if (storage) { opaqueFree(static_cast<char "
176-
"* _Nonnull>(storage)); } }\n";
177-
os << " void operator =(" << name
178-
<< "&& other) noexcept { auto temp = storage; storage = other.storage; "
179-
"other.storage = temp; }\n";
180-
os << " void operator =(const " << name << "&) noexcept = delete;\n";
181-
os << " inline char * _Nonnull getOpaquePointer() noexcept { return "
182-
"static_cast<char "
183-
"* _Nonnull>(storage); }\n";
184-
os << " inline const char * _Nonnull getOpaquePointer() const noexcept { "
185-
"return "
186-
"static_cast<char * _Nonnull>(storage); }\n";
187-
os << "private:\n";
188-
os << " char * _Nullable storage;\n";
189-
os << "};\n";
190-
}
191-
192159
void printCxxNaiveException(raw_ostream &os) {
193-
os << "\n";
194160
os << "/// Naive exception class that should be thrown\n";
195161
os << "class NaiveException {\n";
196162
os << "public:\n";
@@ -273,7 +239,6 @@ void swift::printSwiftToClangCoreScaffold(SwiftToClangInteropContext &ctx,
273239
/*isCForwardDefinition=*/true);
274240
});
275241
os << "\n";
276-
printSwiftResilientStorageClass(os);
277242
printCxxNaiveException(os);
278243
});
279244
os << "\n";

stdlib/public/SwiftShims/_SwiftCxxInteroperability.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,42 @@ inline void opaqueFree(void *_Nonnull p) noexcept {
5353
#endif
5454
}
5555

56+
/// Base class for a container for an opaque Swift value, like resilient struct.
57+
class OpaqueStorage {
58+
public:
59+
inline OpaqueStorage() noexcept : storage(nullptr) {}
60+
inline OpaqueStorage(size_t size, size_t alignment) noexcept
61+
: storage(reinterpret_cast<char *>(opaqueAlloc(size, alignment))) {}
62+
inline OpaqueStorage(OpaqueStorage &&other) noexcept
63+
: storage(other.storage) {
64+
other.storage = nullptr;
65+
}
66+
inline OpaqueStorage(const OpaqueStorage &) noexcept = delete;
67+
68+
inline ~OpaqueStorage() noexcept {
69+
if (storage) {
70+
opaqueFree(static_cast<char *_Nonnull>(storage));
71+
}
72+
}
73+
74+
void operator=(OpaqueStorage &&other) noexcept {
75+
auto temp = storage;
76+
storage = other.storage;
77+
other.storage = temp;
78+
}
79+
void operator=(const OpaqueStorage &) noexcept = delete;
80+
81+
inline char *_Nonnull getOpaquePointer() noexcept {
82+
return static_cast<char *_Nonnull>(storage);
83+
}
84+
inline const char *_Nonnull getOpaquePointer() const noexcept {
85+
return static_cast<char *_Nonnull>(storage);
86+
}
87+
88+
private:
89+
char *_Nullable storage;
90+
};
91+
5692
/// Base class for a Swift reference counted class value.
5793
class RefCountedClass {
5894
public:

test/Interop/SwiftToCxx/core/swift-impl-defs-in-cxx.swift

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
// CHECK-NEXT: size_t stride;
5252
// CHECK-NEXT: unsigned flags;
5353
// CHECK-NEXT: unsigned extraInhabitantCount;
54+
// CHECK-EMPTY:
55+
// CHECK-NEXT: constexpr size_t getAlignment() const { return (flags & 255) + 1; }
5456
// CHECK-NEXT: };
5557
// CHECK-EMPTY:
5658
// CHECK-NEXT: using EnumValueWitnessGetEnumTagTy = int(* __ptrauth_swift_value_witness_function_pointer(41909))(const void * _Nonnull, void * _Nonnull) SWIFT_NOEXCEPT_FUNCTION_PTR;
@@ -96,22 +98,6 @@
9698
// CHECK-NEXT: }
9799
// CHECK-NEXT: #endif
98100
// CHECK-EMPTY:
99-
// CHECK-NEXT: /// Container for an opaque Swift value, like resilient struct.
100-
// CHECK-NEXT: class OpaqueStorage {
101-
// CHECK-NEXT: public:
102-
// CHECK-NEXT: inline OpaqueStorage() noexcept : storage(nullptr) { }
103-
// CHECK-NEXT: inline OpaqueStorage(ValueWitnessTable * _Nonnull vwTable) noexcept : storage(reinterpret_cast<char *>(opaqueAlloc(vwTable->size, (vwTable->flags &255) + 1))) { }
104-
// CHECK-NEXT: inline OpaqueStorage(OpaqueStorage&& other) noexcept : storage(other.storage) { other.storage = nullptr; }
105-
// CHECK-NEXT: inline OpaqueStorage(const OpaqueStorage&) noexcept = delete;
106-
// CHECK-NEXT: inline ~OpaqueStorage() noexcept { if (storage) { opaqueFree(static_cast<char * _Nonnull>(storage)); } }
107-
// CHECK-NEXT: void operator =(OpaqueStorage&& other) noexcept { auto temp = storage; storage = other.storage; other.storage = temp; }
108-
// CHECK-NEXT: void operator =(const OpaqueStorage&) noexcept = delete;
109-
// CHECK-NEXT: inline char * _Nonnull getOpaquePointer() noexcept { return static_cast<char * _Nonnull>(storage); }
110-
// CHECK-NEXT: inline const char * _Nonnull getOpaquePointer() const noexcept { return static_cast<char * _Nonnull>(storage); }
111-
// CHECK-NEXT: private:
112-
// CHECK-NEXT: char * _Nullable storage;
113-
// CHECK-NEXT: };
114-
// CHECK-EMPTY:
115101
// CHECK-NEXT: /// Naive exception class that should be thrown
116102
// CHECK-NEXT: class NaiveException {
117103
// CHECK-NEXT: public:

test/Interop/SwiftToCxx/structs/resilient-struct-in-cxx.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public struct FirstSmallStruct {
3737
// CHECK-NEXT: #else
3838
// CHECK-NEXT: auto *vwTable = *vwTableAddr;
3939
// CHECK-NEXT: #endif
40-
// CHECK-NEXT: _storage = swift::_impl::OpaqueStorage(vwTable);
40+
// CHECK-NEXT: _storage = swift::_impl::OpaqueStorage(vwTable->size, vwTable->getAlignment());
4141
// CHECK-NEXT: vwTable->initializeWithCopy(_getOpaquePointer(), const_cast<char *>(other._getOpaquePointer()), metadata._0);
4242
// CHECK-NEXT: }
4343
// CHECK: private:
44-
// CHECK-NEXT: inline FirstSmallStruct(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable) {}
44+
// CHECK-NEXT: inline FirstSmallStruct(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable->size, vwTable->getAlignment()) {}
4545
// CHECK-NEXT: static inline FirstSmallStruct _make() {
4646
// CHECK-NEXT: auto metadata = _impl::$s7Structs16FirstSmallStructVMa(0);
4747
// CHECK-NEXT: auto *vwTableAddr = reinterpret_cast<swift::_impl::ValueWitnessTable **>(metadata._0) - 1;
@@ -103,11 +103,11 @@ public struct LargeStruct {
103103
// CHECK-NEXT: #else
104104
// CHECK-NEXT: auto *vwTable = *vwTableAddr;
105105
// CHECK-NEXT: #endif
106-
// CHECK-NEXT: _storage = swift::_impl::OpaqueStorage(vwTable);
106+
// CHECK-NEXT: _storage = swift::_impl::OpaqueStorage(vwTable->size, vwTable->getAlignment());
107107
// CHECK-NEXT: vwTable->initializeWithCopy(_getOpaquePointer(), const_cast<char *>(other._getOpaquePointer()), metadata._0);
108108
// CHECK-NEXT: }
109109
// CHECK: private:
110-
// CHECK-NEXT: inline LargeStruct(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable) {}
110+
// CHECK-NEXT: inline LargeStruct(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable->size, vwTable->getAlignment()) {}
111111
// CHECK-NEXT: static inline LargeStruct _make() {
112112
// CHECK-NEXT: auto metadata = _impl::$s7Structs11LargeStructVMa(0);
113113
// CHECK-NEXT: auto *vwTableAddr = reinterpret_cast<swift::_impl::ValueWitnessTable **>(metadata._0) - 1;
@@ -161,7 +161,7 @@ public struct StructWithRefCountStoredProp {
161161
}
162162
}
163163

164-
// CHECK: inline StructWithRefCountStoredProp(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable) {}
164+
// CHECK: inline StructWithRefCountStoredProp(swift::_impl::ValueWitnessTable * _Nonnull vwTable) : _storage(vwTable->size, vwTable->getAlignment()) {}
165165
// CHECK: inline void StructWithRefCountStoredProp::dump() const {
166166
// CHECK-NEXT: return _impl::$s7Structs28StructWithRefCountStoredPropV4dumpyyF(_getOpaquePointer());
167167
// CHECK-NEXT: }

0 commit comments

Comments
 (0)