@@ -61,16 +61,13 @@ printCValueTypeStorageStruct(raw_ostream &os, const NominalTypeDecl *typeDecl,
6161}
6262
6363void printCTypeMetadataTypeFunction (raw_ostream &os,
64- const NominalTypeDecl *typeDecl) {
64+ const NominalTypeDecl *typeDecl,
65+ StringRef typeMetadataFuncName) {
6566 os << " // Type metadata accessor for " << typeDecl->getNameStr () << " \n " ;
66- auto entity = irgen::LinkEntity::forTypeMetadataAccessFunction (
67- typeDecl->getDeclaredType ()->getCanonicalType ());
6867 os << " SWIFT_EXTERN " ;
6968 ClangSyntaxPrinter printer (os);
7069 printer.printSwiftImplQualifier ();
71- os << " MetadataResponseTy " ;
72- entity.mangle (os);
73- os << ' (' ;
70+ os << " MetadataResponseTy " << typeMetadataFuncName << ' (' ;
7471 printer.printSwiftImplQualifier ();
7572 os << " MetadataRequestTy)" ;
7673 os << " SWIFT_NOEXCEPT SWIFT_CALL;\n\n " ;
@@ -90,6 +87,10 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
9087
9188 ClangSyntaxPrinter printer (os);
9289
90+ auto typeMetadataFunc = irgen::LinkEntity::forTypeMetadataAccessFunction (
91+ SD->getDeclaredType ()->getCanonicalType ());
92+ std::string typeMetadataFuncName = typeMetadataFunc.mangleAsString ();
93+
9394 // Print out a forward declaration of the "hidden" _impl class.
9495 printer.printNamespace (cxx_synthesis::getCxxImplNamespaceName (),
9596 [&](raw_ostream &os) {
@@ -99,13 +100,41 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
99100
100101 // Print out special functions, like functions that
101102 // access type metadata.
102- printCTypeMetadataTypeFunction (os, SD);
103+ printCTypeMetadataTypeFunction (os, SD,
104+ typeMetadataFuncName);
103105 });
104106
105107 // Print out the C++ class itself.
106108 os << " class " ;
107109 ClangSyntaxPrinter (os).printBaseName (SD);
108110 os << " final {\n " ;
111+ os << " public:\n " ;
112+
113+ // Print out the destructor.
114+ os << " inline ~" ;
115+ printer.printBaseName (SD);
116+ os << " () {\n " ;
117+ os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
118+ << " ::" << typeMetadataFuncName << " (0);\n " ;
119+ os << " auto *vwTable = "
120+ " *(reinterpret_cast<swift::_impl::ValueWitnessTable **>(metadata._0) - "
121+ " 1);\n " ;
122+ os << " vwTable->destroy(_getOpaquePointer(), metadata._0);\n " ;
123+ os << " }\n " ;
124+
125+ os << " inline " ;
126+ printer.printBaseName (SD);
127+ os << " (const " ;
128+ printer.printBaseName (SD);
129+ os << " &) = default;\n " ;
130+
131+ // FIXME: the move constructor should be hidden somehow.
132+ os << " inline " ;
133+ printer.printBaseName (SD);
134+ os << " (" ;
135+ printer.printBaseName (SD);
136+ os << " &&) = default;\n " ;
137+
109138 // FIXME: Print the other members of the struct.
110139 os << " private:\n " ;
111140
0 commit comments