@@ -44,14 +44,30 @@ 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 (" metadata" );
69+ }
70+
5571static void
5672printCValueTypeStorageStruct (raw_ostream &os, const NominalTypeDecl *typeDecl,
5773 IRABIDetailsProvider::SizeAndAlignment layout) {
@@ -111,6 +127,18 @@ void ClangValueTypePrinter::printValueTypeDecl(
111127 typeMetadataFuncName);
112128 });
113129
130+ auto printEnumVWTableVariable = [&](StringRef metadataName = " metadata" ,
131+ StringRef vwTableName = " vwTable" ,
132+ StringRef enumVWTableName =
133+ " enumVWTable" ) {
134+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
135+ os, typeMetadataFuncName);
136+ os << " const auto *" << enumVWTableName << " = reinterpret_cast<" ;
137+ ClangSyntaxPrinter (os).printSwiftImplQualifier ();
138+ os << " EnumValueWitnessTable" ;
139+ os << " *>(" << vwTableName << " );\n " ;
140+ };
141+
114142 // Print out the C++ class itself.
115143 os << " class " ;
116144 ClangSyntaxPrinter (os).printBaseName (typeDecl);
@@ -121,11 +149,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
121149 os << " inline ~" ;
122150 printer.printBaseName (typeDecl);
123151 os << " () {\n " ;
124- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
125- << " ::" ;
126- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
127- os << " ;\n " ;
128- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
152+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
153+ os, typeMetadataFuncName);
129154 os << " vwTable->destroy(_getOpaquePointer(), metadata._0);\n " ;
130155 os << " }\n " ;
131156
@@ -134,11 +159,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
134159 os << " (const " ;
135160 printer.printBaseName (typeDecl);
136161 os << " &other) {\n " ;
137- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
138- << " ::" ;
139- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
140- os << " ;\n " ;
141- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
162+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
163+ os, typeMetadataFuncName);
142164 if (isOpaqueLayout) {
143165 os << " _storage = " ;
144166 printer.printSwiftImplQualifier ();
@@ -176,11 +198,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
176198 os << " _make() {" ;
177199 if (isOpaqueLayout) {
178200 os << " \n " ;
179- os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName ()
180- << " ::" ;
181- printer.printSwiftTypeMetadataAccessFunctionCall (typeMetadataFuncName);
182- os << " ;\n " ;
183- printer.printValueWitnessTableAccessSequenceFromTypeMetadata (" metadata" );
201+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
202+ os, typeMetadataFuncName);
184203 os << " return " ;
185204 printer.printBaseName (typeDecl);
186205 os << " (vwTable);\n }\n " ;
@@ -200,19 +219,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
200219 os << " .getOpaquePointer()" ;
201220 os << " ; }\n " ;
202221 os << " \n " ;
203- // Print out helper function for getting enum tag for enum type
222+ // Print out helper function for enums
204223 if (isa<EnumDecl>(typeDecl)) {
224+ os << " inline char * _Nonnull _destructiveProjectEnumData() {\n " ;
225+ printEnumVWTableVariable ();
226+ os << " enumVWTable->destructiveProjectEnumData(_getOpaquePointer(), "
227+ " metadata._0);\n " ;
228+ os << " return _getOpaquePointer();\n " ;
229+ os << " }\n " ;
205230 // FIXME: (tongjie) return type should be unsigned
206231 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 " ;
232+ printEnumVWTableVariable ();
216233 os << " return enumVWTable->getEnumTag(_getOpaquePointer(), "
217234 " metadata._0);\n " ;
218235 os << " }\n " ;
@@ -259,6 +276,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
259276 os << " callable(result._getOpaquePointer());\n " ;
260277 os << " return result;\n " ;
261278 os << " }\n " ;
279+ // Print out helper function for initializeWithTake
280+ // TODO: (tongjie) support opaque layout
281+ if (!isOpaqueLayout) {
282+ os << " static inline void initializeWithTake(char * _Nonnull "
283+ " destStorage, char * _Nonnull srcStorage) {\n " ;
284+ ClangValueTypePrinter::printValueWitnessTableAccessAsVariable (
285+ os, typeMetadataFuncName);
286+ os << " vwTable->initializeWithTake(destStorage, srcStorage, "
287+ " metadata._0);\n " ;
288+ os << " }\n " ;
289+ }
262290
263291 os << " };\n " ;
264292 });
0 commit comments