@@ -44,14 +44,30 @@ static void printCxxTypeName(raw_ostream &os, const NominalTypeDecl *type,
44
44
printer.printBaseName (type);
45
45
}
46
46
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) {
51
49
os << " _impl_" ;
52
50
ClangSyntaxPrinter (os).printBaseName (type);
53
51
}
54
52
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
+
55
71
static void
56
72
printCValueTypeStorageStruct (raw_ostream &os, const NominalTypeDecl *typeDecl,
57
73
IRABIDetailsProvider::SizeAndAlignment layout) {
@@ -111,6 +127,18 @@ void ClangValueTypePrinter::printValueTypeDecl(
111
127
typeMetadataFuncName);
112
128
});
113
129
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
+
114
142
// Print out the C++ class itself.
115
143
os << " class " ;
116
144
ClangSyntaxPrinter (os).printBaseName (typeDecl);
@@ -121,11 +149,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
121
149
os << " inline ~" ;
122
150
printer.printBaseName (typeDecl);
123
151
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);
129
154
os << " vwTable->destroy(_getOpaquePointer(), metadata._0);\n " ;
130
155
os << " }\n " ;
131
156
@@ -134,11 +159,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
134
159
os << " (const " ;
135
160
printer.printBaseName (typeDecl);
136
161
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);
142
164
if (isOpaqueLayout) {
143
165
os << " _storage = " ;
144
166
printer.printSwiftImplQualifier ();
@@ -176,11 +198,8 @@ void ClangValueTypePrinter::printValueTypeDecl(
176
198
os << " _make() {" ;
177
199
if (isOpaqueLayout) {
178
200
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);
184
203
os << " return " ;
185
204
printer.printBaseName (typeDecl);
186
205
os << " (vwTable);\n }\n " ;
@@ -200,19 +219,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
200
219
os << " .getOpaquePointer()" ;
201
220
os << " ; }\n " ;
202
221
os << " \n " ;
203
- // Print out helper function for getting enum tag for enum type
222
+ // Print out helper function for enums
204
223
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 " ;
205
230
// FIXME: (tongjie) return type should be unsigned
206
231
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 ();
216
233
os << " return enumVWTable->getEnumTag(_getOpaquePointer(), "
217
234
" metadata._0);\n " ;
218
235
os << " }\n " ;
@@ -259,6 +276,17 @@ void ClangValueTypePrinter::printValueTypeDecl(
259
276
os << " callable(result._getOpaquePointer());\n " ;
260
277
os << " return result;\n " ;
261
278
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
+ }
262
290
263
291
os << " };\n " ;
264
292
});
0 commit comments