@@ -44,14 +44,31 @@ static void printCxxTypeName(raw_ostream &os, const NominalTypeDecl *type,
4444 printer.printBaseName (type);
4545}
4646
47- // / Print out the C++ type name of the implementation class that provides hidden
48- // / access to the private class APIs.
49- static void printCxxImplClassName (raw_ostream &os,
50- const NominalTypeDecl *type) {
47+ void ClangValueTypePrinter::printCxxImplClassName (raw_ostream &os,
48+ const NominalTypeDecl *type) {
5149 os << " _impl_" ;
5250 ClangSyntaxPrinter (os).printBaseName (type);
5351}
5452
53+ void ClangValueTypePrinter::printMetadataAccessAsVariable (
54+ raw_ostream &os, StringRef metadataFuncName, int indent,
55+ StringRef varName) {
56+ ClangSyntaxPrinter printer (os);
57+ os << std::string (indent, ' ' ) << " auto " << varName << " = "
58+ << cxx_synthesis::getCxxImplNamespaceName () << " ::" ;
59+ printer.printSwiftTypeMetadataAccessFunctionCall (metadataFuncName);
60+ os << " ;\n " ;
61+ }
62+
63+ void ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
64+ raw_ostream &os, StringRef metadataFuncName, int indent,
65+ StringRef metadataVarName, StringRef vwTableVarName) {
66+ ClangSyntaxPrinter printer (os);
67+ printMetadataAccessAsVariable (os, metadataFuncName, indent, metadataVarName);
68+ printer.printValueWitnessTableAccessSequenceFromTypeMetadata (
69+ metadataVarName, vwTableVarName, indent);
70+ }
71+
5572static void
5673printCValueTypeStorageStruct (raw_ostream &os, const NominalTypeDecl *typeDecl,
5774 IRABIDetailsProvider::SizeAndAlignment layout) {
@@ -111,6 +128,18 @@ void ClangValueTypePrinter::printValueTypeDecl(
111128 typeMetadataFuncName);
112129 });
113130
131+ auto printEnumVWTableVariable = [&](StringRef metadataName = " metadata" ,
132+ StringRef vwTableName = " vwTable" ,
133+ StringRef enumVWTableName =
134+ " enumVWTable" ) {
135+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
136+ os, typeMetadataFuncName);
137+ os << " const auto *" << enumVWTableName << " = reinterpret_cast<" ;
138+ ClangSyntaxPrinter (os).printSwiftImplQualifier ();
139+ os << " EnumValueWitnessTable" ;
140+ os << " *>(" << vwTableName << " );\n " ;
141+ };
142+
114143 // Print out the C++ class itself.
115144 os << " class " ;
116145 ClangSyntaxPrinter (os).printBaseName (typeDecl);
@@ -121,11 +150,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
121150 os << " inline ~" ;
122151 printer.printBaseName (typeDecl);
123152 os << " () {\n " ;
124- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
125- << " ::" ;
126- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
127- os << " ;\n " ;
128- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
153+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
154+ os, typeMetadataFuncName);
129155 os << " vwTable->destroy(_getOpaquePointer(), metadata._0);\n " ;
130156 os << " }\n " ;
131157
@@ -134,11 +160,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
134160 os << " (const " ;
135161 printer.printBaseName (typeDecl);
136162 os << " &other) {\n " ;
137- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
138- << " ::" ;
139- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
140- os << " ;\n " ;
141- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
163+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
164+ os, typeMetadataFuncName);
142165 if (isOpaqueLayout) {
143166 os << " _storage = " ;
144167 printer.printSwiftImplQualifier ();
@@ -176,11 +199,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
176199 os << " _make() {" ;
177200 if (isOpaqueLayout) {
178201 os << " \n " ;
179- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
180- << " ::" ;
181- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
182- os << " ;\n " ;
183- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
202+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
203+ os, typeMetadataFuncName);
184204 os << " return " ;
185205 printer.printBaseName (typeDecl);
186206 os << " (vwTable);\n }\n " ;
@@ -200,19 +220,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
200220 os << " .getOpaquePointer()" ;
201221 os << " ; }\n " ;
202222 os << " \n " ;
203- // Print out helper function for getting enum tag for enum type
223+ // Print out helper function for enums
204224 if (isa<EnumDecl>(typeDecl)) {
225+ os << " inline char * _Nonnull _destructiveProjectEnumData() {\n " ;
226+ printEnumVWTableVariable ();
227+ os << " enumVWTable->destructiveProjectEnumData(_getOpaquePointer(), "
228+ " metadata._0);\n " ;
229+ os << " return _getOpaquePointer();\n " ;
230+ os << " }\n " ;
205231 // FIXME: (tongjie) return type should be unsigned
206232 os << " inline int _getEnumTag() const {\n " ;
207- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
208- << " ::" ;
209- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
210- os << " ;\n " ;
211- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
212- os << " const auto *enumVWTable = reinterpret_cast<" ;
213- ClangSyntaxPrinter (os).printSwiftImplQualifier ();
214- os << " EnumValueWitnessTable" ;
215- os << " *>(vwTable);\n " ;
233+ printEnumVWTableVariable ();
216234 os << " return enumVWTable->getEnumTag(_getOpaquePointer(), "
217235 " metadata._0);\n " ;
218236 os << " }\n " ;
@@ -259,6 +277,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
259277 os << " callable(result._getOpaquePointer());\n " ;
260278 os << " return result;\n " ;
261279 os << " }\n " ;
280+ // Print out helper function for initializeWithTake
281+ // TODO: (tongjie) support opaque layout
282+ if (!isOpaqueLayout) {
283+ os << " static inline void initializeWithTake(char * _Nonnull "
284+ " destStorage, char * _Nonnull srcStorage) {\n " ;
285+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
286+ os, typeMetadataFuncName);
287+ os << " vwTable->initializeWithTake(destStorage, srcStorage, "
288+ " metadata._0);\n " ;
289+ os << " }\n " ;
290+ }
262291
263292 os << " };\n " ;
264293 });
0 commit comments