Skip to content

Commit ff37c4e

Browse files
committed
[interop][SwiftToCxx] NFC, extract value witness extraction printing into shared printer
1 parent 3533686 commit ff37c4e

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ void ClangSyntaxPrinter::printNullability(
127127
if (printKind != NullabilityPrintKind::After)
128128
os << ' ';
129129
}
130+
131+
void ClangSyntaxPrinter::printSwiftTypeMetadataAccessFunctionCall(
132+
StringRef name) {
133+
os << name << "(0)";
134+
}
135+
136+
void ClangSyntaxPrinter::printValueWitnessTableAccessFromTypeMetadata(
137+
StringRef metadataVariable) {
138+
os << "*(reinterpret_cast<";
139+
printSwiftImplQualifier();
140+
os << "ValueWitnessTable **>(" << metadataVariable << "._0) - 1)";
141+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ class ClangSyntaxPrinter {
7777
static bool isClangKeyword(StringRef name);
7878
static bool isClangKeyword(Identifier name);
7979

80+
/// Print the call expression to the Swift type metadata access function.
81+
void printSwiftTypeMetadataAccessFunctionCall(StringRef name);
82+
83+
/// Print the expression to access the value witness table pointer from the
84+
/// given type metadata variable.
85+
void printValueWitnessTableAccessFromTypeMetadata(StringRef metadataVariable);
86+
8087
protected:
8188
raw_ostream &os;
8289
};

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,12 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
115115
printer.printBaseName(SD);
116116
os << "() {\n";
117117
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";
118+
<< "::";
119+
printer.printSwiftTypeMetadataAccessFunctionCall(typeMetadataFuncName);
120+
os << ";\n";
121+
os << " auto *vwTable = ";
122+
printer.printValueWitnessTableAccessFromTypeMetadata("metadata");
123+
os << ";\n";
122124
os << " vwTable->destroy(_getOpaquePointer(), metadata._0);\n";
123125
os << " }\n";
124126

@@ -128,10 +130,12 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
128130
printer.printBaseName(SD);
129131
os << " &other) {\n";
130132
os << " auto metadata = " << cxx_synthesis::getCxxImplNamespaceName()
131-
<< "::" << typeMetadataFuncName << "(0);\n";
132-
os << " auto *vwTable = "
133-
"*(reinterpret_cast<swift::_impl::ValueWitnessTable **>(metadata._0) - "
134-
"1);\n";
133+
<< "::";
134+
printer.printSwiftTypeMetadataAccessFunctionCall(typeMetadataFuncName);
135+
os << ";\n";
136+
os << " auto *vwTable = ";
137+
printer.printValueWitnessTableAccessFromTypeMetadata("metadata");
138+
os << ";\n";
135139
os << " vwTable->initializeWithCopy(_getOpaquePointer(), const_cast<char "
136140
"*>(other._getOpaquePointer()), metadata._0);\n";
137141
os << " }\n";

0 commit comments

Comments
 (0)