Skip to content

Commit 3d0cece

Browse files
authored
Merge pull request #59364 from hyp/eng/cleanup-struct-printer
[interop][swiftToCxx] NFC, refactor out the code to print C++ impl class names and C s…
2 parents 6cc8359 + f8b870a commit 3d0cece

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ void ClangSyntaxPrinter::printIdentifier(StringRef name) {
4444
os << '_';
4545
}
4646

47+
void ClangSyntaxPrinter::printBaseName(const ValueDecl *decl) {
48+
assert(decl->getName().isSimpleName());
49+
printIdentifier(decl->getBaseIdentifier().str());
50+
}
51+
4752
void ClangSyntaxPrinter::printModuleNameCPrefix(const ModuleDecl &mod) {
4853
os << mod.getName().str() << '_';
4954
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class ClangSyntaxPrinter {
3939
/// trailing underscore.
4040
void printIdentifier(StringRef name);
4141

42+
/// Print the base name of the given declaration.
43+
void printBaseName(const ValueDecl *decl);
44+
4245
/// Print the C-style prefix for the given module name, that's used for
4346
/// C type names inside the module.
4447
void printModuleNameCPrefix(const ModuleDecl &mod);

lib/PrintAsClang/PrintClangValueType.cpp

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@ static void printCTypeName(raw_ostream &os, const NominalTypeDecl *type) {
3030
ClangSyntaxPrinter printer(os);
3131
printer.printModuleNameCPrefix(*type->getParentModule());
3232
// FIXME: add nested type qualifiers to fully disambiguate the name.
33-
printer.printIdentifier(type->getName().str());
33+
printer.printBaseName(type);
3434
}
3535

3636
/// Print out the C++ type name of a struct/enum declaration.
3737
static void printCxxTypeName(raw_ostream &os, const NominalTypeDecl *type) {
3838
// FIXME: Print namespace qualifiers for references from other modules.
3939
// FIXME: Print class qualifiers for nested class references.
40-
ClangSyntaxPrinter(os).printIdentifier(type->getName().str());
40+
ClangSyntaxPrinter(os).printBaseName(type);
41+
}
42+
43+
/// Print out the C++ type name of the implementation class that provides hidden
44+
/// access to the private class APIs.
45+
static void printCxxImplClassName(raw_ostream &os,
46+
const NominalTypeDecl *type) {
47+
os << "_impl_";
48+
ClangSyntaxPrinter(os).printBaseName(type);
4149
}
4250

4351
static void
@@ -68,28 +76,28 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
6876
// Print out a forward declaration of the "hidden" _impl class.
6977
printer.printNamespace(cxx_synthesis::getCxxImplNamespaceName(),
7078
[&](raw_ostream &os) {
71-
os << "class _impl_";
72-
printer.printIdentifier(SD->getName().str());
79+
os << "class ";
80+
printCxxImplClassName(os, SD);
7381
os << ";\n";
7482
});
7583

7684
// Print out the C++ class itself.
7785
os << "class ";
78-
ClangSyntaxPrinter(os).printIdentifier(SD->getName().str());
86+
ClangSyntaxPrinter(os).printBaseName(SD);
7987
os << " final {\n";
8088
// FIXME: Print the other members of the struct.
8189
os << "private:\n";
8290

8391
// Print out private default constructor.
8492
os << " inline ";
85-
printer.printIdentifier(SD->getName().str());
93+
printer.printBaseName(SD);
8694
os << "() {}\n";
8795
// Print out '_make' function which returns an unitialized instance for
8896
// passing to Swift.
8997
os << " static inline ";
90-
printer.printIdentifier(SD->getName().str());
98+
printer.printBaseName(SD);
9199
os << " _make() { return ";
92-
printer.printIdentifier(SD->getName().str());
100+
printer.printBaseName(SD);
93101
os << "(); }\n";
94102
// Print out the private accessors to the underlying Swift value storage.
95103
os << " inline const char * _Nonnull _getOpaquePointer() const { return "
@@ -101,17 +109,16 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
101109
os << " alignas(" << typeSizeAlign->alignment << ") ";
102110
os << "char _storage[" << typeSizeAlign->size << "];\n";
103111
// Wrap up the value type.
104-
os << " friend class " << cxx_synthesis::getCxxImplNamespaceName()
105-
<< "::_impl_";
106-
ClangSyntaxPrinter(os).printIdentifier(SD->getName().str());
112+
os << " friend class " << cxx_synthesis::getCxxImplNamespaceName() << "::";
113+
printCxxImplClassName(os, SD);
107114
os << ";\n";
108115
os << "};\n\n";
109116

110117
// Print out the "hidden" _impl class.
111118
printer.printNamespace(
112119
cxx_synthesis::getCxxImplNamespaceName(), [&](raw_ostream &os) {
113-
os << "class _impl_";
114-
printer.printIdentifier(SD->getName().str());
120+
os << "class ";
121+
printCxxImplClassName(os, SD);
115122
os << " {\n";
116123
os << "public:\n";
117124

@@ -140,22 +147,24 @@ void ClangValueTypePrinter::printStructDecl(const StructDecl *SD) {
140147
printCValueTypeStorageStruct(cPrologueOS, SD, *typeSizeAlign);
141148
}
142149

150+
/// Print the name of the C stub struct for passing/returning a value type
151+
/// directly to/from swiftcc function.
152+
static void printStubCTypeName(raw_ostream &os, const NominalTypeDecl *type) {
153+
os << "swift_interop_stub_";
154+
printCTypeName(os, type);
155+
}
156+
143157
/// Print out the C stub struct that's used to pass/return a value type directly
144158
/// to/from swiftcc function.
145159
static void
146160
printCStructStubForDirectPassing(raw_ostream &os, const NominalTypeDecl *SD,
147161
PrimitiveTypeMapping &typeMapping,
148162
SwiftToClangInteropContext &interopContext) {
149-
150-
auto printStubCTypeName = [&]() {
151-
os << "swift_interop_stub_";
152-
printCTypeName(os, SD);
153-
};
154163
// Print out a C stub for this value type.
155164
os << "// Stub struct to be used to pass/return values to/from Swift "
156165
"functions.\n";
157166
os << "struct ";
158-
printStubCTypeName();
167+
printStubCTypeName(os, SD);
159168
os << " {\n";
160169
llvm::SmallVector<std::pair<clang::CharUnits, clang::CharUnits>, 8> fields;
161170
interopContext.getIrABIDetails().enumerateDirectPassingRecordMembers(
@@ -177,7 +186,7 @@ printCStructStubForDirectPassing(raw_ostream &os, const NominalTypeDecl *SD,
177186
os << "static inline void swift_interop_returnDirect_";
178187
printCTypeName(os, SD);
179188
os << "(char * _Nonnull result, struct ";
180-
printStubCTypeName();
189+
printStubCTypeName(os, SD);
181190
os << " value";
182191
os << ") __attribute__((always_inline)) {\n";
183192
for (size_t i = 0; i < fields.size(); ++i) {
@@ -189,12 +198,12 @@ printCStructStubForDirectPassing(raw_ostream &os, const NominalTypeDecl *SD,
189198

190199
// Emit a stub that is used to pass value type directly to swiftcc function.
191200
os << "static inline struct ";
192-
printStubCTypeName();
201+
printStubCTypeName(os, SD);
193202
os << " swift_interop_passDirect_";
194203
printCTypeName(os, SD);
195204
os << "(const char * _Nonnull value) __attribute__((always_inline)) {\n";
196205
os << " struct ";
197-
printStubCTypeName();
206+
printStubCTypeName(os, SD);
198207
os << " result;\n";
199208
for (size_t i = 0; i < fields.size(); ++i) {
200209
os << " memcpy(&result._" << (i + 1) << ", value + "
@@ -206,8 +215,7 @@ printCStructStubForDirectPassing(raw_ostream &os, const NominalTypeDecl *SD,
206215
}
207216

208217
void ClangValueTypePrinter::printCStubTypeName(const NominalTypeDecl *type) {
209-
os << "swift_interop_stub_";
210-
printCTypeName(os, type);
218+
printStubCTypeName(os, type);
211219
// Ensure the stub is declared in the header.
212220
interopContext.runIfStubForDeclNotEmitted(type, [&]() {
213221
printCStructStubForDirectPassing(cPrologueOS, type, typeMapping,
@@ -240,8 +248,8 @@ void ClangValueTypePrinter::printParameterCxxToCUseScaffold(
240248
printCTypeName(os, type);
241249
os << '(';
242250
}
243-
os << cxx_synthesis::getCxxImplNamespaceName() << "::_impl_";
244-
ClangSyntaxPrinter(os).printIdentifier(type->getName().str());
251+
os << cxx_synthesis::getCxxImplNamespaceName() << "::";
252+
printCxxImplClassName(os, type);
245253
os << "::getOpaquePointer(";
246254
cxxParamPrinter();
247255
os << ')';
@@ -264,8 +272,8 @@ void ClangValueTypePrinter::printValueTypeIndirectReturnScaffold(
264272
const NominalTypeDecl *type,
265273
llvm::function_ref<void(StringRef)> bodyPrinter) {
266274
assert(isa<StructDecl>(type) || isa<EnumDecl>(type));
267-
os << " return " << cxx_synthesis::getCxxImplNamespaceName() << "::_impl_";
268-
ClangSyntaxPrinter(os).printIdentifier(type->getName().str());
275+
os << " return " << cxx_synthesis::getCxxImplNamespaceName() << "::";
276+
printCxxImplClassName(os, type);
269277
os << "::returnNewValue([&](void * _Nonnull result) {\n ";
270278
bodyPrinter("result");
271279
os << ";\n";
@@ -275,8 +283,8 @@ void ClangValueTypePrinter::printValueTypeIndirectReturnScaffold(
275283
void ClangValueTypePrinter::printValueTypeDirectReturnScaffold(
276284
const NominalTypeDecl *type, llvm::function_ref<void()> bodyPrinter) {
277285
assert(isa<StructDecl>(type) || isa<EnumDecl>(type));
278-
os << " return " << cxx_synthesis::getCxxImplNamespaceName() << "::_impl_";
279-
ClangSyntaxPrinter(os).printIdentifier(type->getName().str());
286+
os << " return " << cxx_synthesis::getCxxImplNamespaceName() << "::";
287+
printCxxImplClassName(os, type);
280288
os << "::returnNewValue([&](char * _Nonnull result) {\n";
281289
os << " ";
282290
os << cxx_synthesis::getCxxImplNamespaceName() << "::"

0 commit comments

Comments
 (0)