Skip to content

Commit 891d7b0

Browse files
committed
[interop] NFC, refactor printNullability to ClangSyntaxPrinter
This way it can be reused from ClangFunctionPrinter
1 parent 9d56de1 commit 891d7b0

File tree

3 files changed

+59
-58
lines changed

3 files changed

+59
-58
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,44 @@ void ClangSyntaxPrinter::printNamespace(
3535
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
3636
printNamespace([&](raw_ostream &os) { os << name; }, bodyPrinter);
3737
}
38+
39+
void ClangSyntaxPrinter::printNullability(
40+
Optional<OptionalTypeKind> kind, NullabilityPrintKind printKind) const {
41+
if (!kind)
42+
return;
43+
44+
switch (printKind) {
45+
case NullabilityPrintKind::ContextSensitive:
46+
switch (*kind) {
47+
case OTK_None:
48+
os << "nonnull";
49+
break;
50+
case OTK_Optional:
51+
os << "nullable";
52+
break;
53+
case OTK_ImplicitlyUnwrappedOptional:
54+
os << "null_unspecified";
55+
break;
56+
}
57+
break;
58+
case NullabilityPrintKind::After:
59+
os << ' ';
60+
LLVM_FALLTHROUGH;
61+
case NullabilityPrintKind::Before:
62+
switch (*kind) {
63+
case OTK_None:
64+
os << "_Nonnull";
65+
break;
66+
case OTK_Optional:
67+
os << "_Nullable";
68+
break;
69+
case OTK_ImplicitlyUnwrappedOptional:
70+
os << "_Null_unspecified";
71+
break;
72+
}
73+
break;
74+
}
75+
76+
if (printKind != NullabilityPrintKind::After)
77+
os << ' ';
78+
}

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
1515

1616
#include "swift/Basic/LLVM.h"
17+
#include "swift/ClangImporter/ClangImporter.h"
1718
#include "llvm/ADT/StringRef.h"
1819
#include "llvm/Support/raw_ostream.h"
1920

@@ -41,7 +42,18 @@ class ClangSyntaxPrinter {
4142
printNamespace(StringRef name,
4243
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;
4344

44-
private:
45+
/// Where nullability information should be printed.
46+
enum class NullabilityPrintKind {
47+
Before,
48+
After,
49+
ContextSensitive,
50+
};
51+
52+
void printNullability(
53+
Optional<OptionalTypeKind> kind,
54+
NullabilityPrintKind printKind = NullabilityPrintKind::After) const;
55+
56+
protected:
4557
raw_ostream &os;
4658
};
4759

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,14 @@ static bool looksLikeInitMethod(ObjCSelector selector) {
122122
}
123123

124124
class DeclAndTypePrinter::Implementation
125-
: private DeclVisitor<DeclAndTypePrinter::Implementation>,
126-
private TypeVisitor<DeclAndTypePrinter::Implementation, void,
127-
Optional<OptionalTypeKind>>
128-
{
125+
: private DeclVisitor<DeclAndTypePrinter::Implementation>,
126+
private TypeVisitor<DeclAndTypePrinter::Implementation, void,
127+
Optional<OptionalTypeKind>>,
128+
private ClangSyntaxPrinter {
129129
using PrinterImpl = Implementation;
130130
friend ASTVisitor;
131131
friend TypeVisitor;
132132

133-
// The output stream is accessible through 'owningPrinter',
134-
// but it makes the code simpler to have it here too.
135-
raw_ostream &os;
136133
DeclAndTypePrinter &owningPrinter;
137134
OutputLanguageMode outputLang;
138135

@@ -150,7 +147,7 @@ class DeclAndTypePrinter::Implementation
150147
public:
151148
explicit Implementation(raw_ostream &out, DeclAndTypePrinter &owner,
152149
OutputLanguageMode outputLang)
153-
: os(out), owningPrinter(owner), outputLang(outputLang) {}
150+
: ClangSyntaxPrinter(out), owningPrinter(owner), outputLang(outputLang) {}
154151

155152
void print(const Decl *D) {
156153
PrettyStackTraceDecl trace("printing", D);
@@ -1389,55 +1386,6 @@ class DeclAndTypePrinter::Implementation
13891386
TypeVisitor::visit(ty, optionalKind);
13901387
}
13911388

1392-
/// Where nullability information should be printed.
1393-
enum class NullabilityPrintKind {
1394-
Before,
1395-
After,
1396-
ContextSensitive,
1397-
};
1398-
1399-
void printNullability(Optional<OptionalTypeKind> kind,
1400-
NullabilityPrintKind printKind
1401-
= NullabilityPrintKind::After) {
1402-
if (!kind)
1403-
return;
1404-
1405-
switch (printKind) {
1406-
case NullabilityPrintKind::ContextSensitive:
1407-
switch (*kind) {
1408-
case OTK_None:
1409-
os << "nonnull";
1410-
break;
1411-
case OTK_Optional:
1412-
os << "nullable";
1413-
break;
1414-
case OTK_ImplicitlyUnwrappedOptional:
1415-
os << "null_unspecified";
1416-
break;
1417-
}
1418-
break;
1419-
case NullabilityPrintKind::After:
1420-
os << ' ';
1421-
LLVM_FALLTHROUGH;
1422-
case NullabilityPrintKind::Before:
1423-
switch (*kind) {
1424-
case OTK_None:
1425-
os << "_Nonnull";
1426-
break;
1427-
case OTK_Optional:
1428-
os << "_Nullable";
1429-
break;
1430-
case OTK_ImplicitlyUnwrappedOptional:
1431-
os << "_Null_unspecified";
1432-
break;
1433-
}
1434-
break;
1435-
}
1436-
1437-
if (printKind != NullabilityPrintKind::After)
1438-
os << ' ';
1439-
}
1440-
14411389
/// Determine whether this generic Swift nominal type maps to a
14421390
/// generic Objective-C class.
14431391
static bool hasGenericObjCType(const NominalTypeDecl *nominal) {

0 commit comments

Comments
 (0)