Skip to content

Commit 327b989

Browse files
committed
[interop][SwiftToCxx] NFC, refactor printKnownCType method to ClangSyntaxPrinter for reuse
1 parent 447e524 commit 327b989

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "ClangSyntaxPrinter.h"
14+
#include "PrimitiveTypeMapping.h"
1415
#include "swift/ABI/MetadataValues.h"
1516
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/Decl.h"
@@ -424,3 +425,13 @@ void ClangSyntaxPrinter::printSymbolUSRAttribute(const ValueDecl *D) const {
424425
return;
425426
os << " SWIFT_SYMBOL(\"" << result << "\")";
426427
}
428+
429+
void ClangSyntaxPrinter::printKnownCType(
430+
Type t, PrimitiveTypeMapping &typeMapping) const {
431+
auto info =
432+
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
433+
assert(info.has_value() && "not a known type");
434+
os << info->name;
435+
if (info->canBeNullable)
436+
os << " _Null_unspecified";
437+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
#ifndef SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
1414
#define SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
1515

16-
#include "swift/IRGen/GenericRequirement.h"
16+
#include "swift/AST/Type.h"
1717
#include "swift/Basic/LLVM.h"
1818
#include "swift/ClangImporter/ClangImporter.h"
19+
#include "swift/IRGen/GenericRequirement.h"
1920
#include "llvm/ADT/StringRef.h"
2021
#include "llvm/Support/raw_ostream.h"
2122

@@ -25,6 +26,7 @@ class CanGenericSignature;
2526
class GenericTypeParamType;
2627
class ModuleDecl;
2728
class NominalTypeDecl;
29+
class PrimitiveTypeMapping;
2830

2931
namespace cxx_synthesis {
3032

@@ -225,6 +227,9 @@ class ClangSyntaxPrinter {
225227
/// on the generated declaration.
226228
void printSymbolUSRAttribute(const ValueDecl *D) const;
227229

230+
/// Print the given **known** type as a C type.
231+
void printKnownCType(Type t, PrimitiveTypeMapping &typeMapping) const;
232+
228233
protected:
229234
raw_ostream &os;
230235
};

lib/PrintAsClang/PrintSwiftToClangCoreScaffold.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,14 @@
2323

2424
using namespace swift;
2525

26-
static void printKnownCType(Type t, PrimitiveTypeMapping &typeMapping,
27-
raw_ostream &os) {
28-
auto info =
29-
typeMapping.getKnownCTypeInfo(t->getNominalOrBoundGenericNominal());
30-
assert(info.has_value() && "not a known type");
31-
os << info->name;
32-
if (info->canBeNullable)
33-
os << " _Null_unspecified";
34-
}
35-
3626
static void printKnownStruct(
3727
PrimitiveTypeMapping &typeMapping, raw_ostream &os, StringRef name,
3828
const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) {
3929
assert(typeRecord.getMembers().size() > 1);
4030
os << "struct " << name << " {\n";
4131
for (const auto &ty : llvm::enumerate(typeRecord.getMembers())) {
4232
os << " ";
43-
printKnownCType(ty.value(), typeMapping, os);
33+
ClangSyntaxPrinter(os).printKnownCType(ty.value(), typeMapping);
4434
os << " _" << ty.index() << ";\n";
4535
}
4636
os << "};\n";
@@ -51,7 +41,8 @@ static void printKnownTypedef(
5141
const IRABIDetailsProvider::TypeRecordABIRepresentation &typeRecord) {
5242
assert(typeRecord.getMembers().size() == 1);
5343
os << "typedef ";
54-
printKnownCType(typeRecord.getMembers()[0], typeMapping, os);
44+
ClangSyntaxPrinter(os).printKnownCType(typeRecord.getMembers()[0],
45+
typeMapping);
5546
os << " " << name << ";\n";
5647
}
5748

0 commit comments

Comments
 (0)