Skip to content

Commit b8b9226

Browse files
committed
Revert "Merge pull request swiftlang#41965 from hyp/i/cfuncprim"
This reverts commit fbbfb70, reversing changes made to cee0b4c.
1 parent 693a8bd commit b8b9226

17 files changed

+128
-627
lines changed

lib/PrintAsClang/CMakeLists.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11

22
add_swift_host_library(swiftPrintAsClang STATIC
3-
ClangSyntaxPrinter.cpp
3+
CxxSynthesis.cpp
44
DeclAndTypePrinter.cpp
55
ModuleContentsWriter.cpp
66
PrimitiveTypeMapping.cpp
7-
PrintAsClang.cpp
8-
PrintClangFunction.cpp)
7+
PrintAsClang.cpp)
98
target_link_libraries(swiftPrintAsClang PRIVATE
109
swiftAST
1110
swiftClangImporter
Lines changed: 4 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- ClangSyntaxPrinter.cpp - Printer for C and C++ code ----*- C++ -*-===//
1+
//===--- CxxSynthesis.cpp - Rules for synthesizing C++ code -----*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,15 +10,15 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ClangSyntaxPrinter.h"
13+
#include "CxxSynthesis.h"
1414

1515
using namespace swift;
1616
using namespace cxx_synthesis;
1717

1818
StringRef cxx_synthesis::getCxxImplNamespaceName() { return "_impl"; }
1919

2020
/// Print a C++ namespace declaration with the give name and body.
21-
void ClangSyntaxPrinter::printNamespace(
21+
void CxxPrinter::printNamespace(
2222
llvm::function_ref<void(raw_ostream &OS)> namePrinter,
2323
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const {
2424
os << "namespace ";
@@ -30,49 +30,8 @@ void ClangSyntaxPrinter::printNamespace(
3030
os << "\n\n";
3131
}
3232

33-
void ClangSyntaxPrinter::printNamespace(
33+
void CxxPrinter::printNamespace(
3434
StringRef name,
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-
}
Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- ClangSyntaxPrinter.h - Printer for C and C++ code ------*- C++ -*-===//
1+
//===--- CxxSynthesis.h - Rules for synthesizing C++ code -------*- C++ -*-===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,11 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#ifndef SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
14-
#define SWIFT_PRINTASCLANG_CLANGSYNTAXPRINTER_H
13+
#ifndef SWIFT_PRINTASCLANG_CXXSYNTHESIS_H
14+
#define SWIFT_PRINTASCLANG_CXXSYNTHESIS_H
1515

1616
#include "swift/Basic/LLVM.h"
17-
#include "swift/ClangImporter/ClangImporter.h"
1817
#include "llvm/ADT/StringRef.h"
1918
#include "llvm/Support/raw_ostream.h"
2019

@@ -27,11 +26,9 @@ namespace cxx_synthesis {
2726
/// module in C++.
2827
StringRef getCxxImplNamespaceName();
2928

30-
} // end namespace cxx_synthesis
31-
32-
class ClangSyntaxPrinter {
29+
class CxxPrinter {
3330
public:
34-
ClangSyntaxPrinter(raw_ostream &os) : os(os) {}
31+
CxxPrinter(raw_ostream &os) : os(os) {}
3532

3633
/// Print a C++ namespace declaration with the give name and body.
3734
void
@@ -42,21 +39,11 @@ class ClangSyntaxPrinter {
4239
printNamespace(StringRef name,
4340
llvm::function_ref<void(raw_ostream &OS)> bodyPrinter) const;
4441

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:
42+
private:
5743
raw_ostream &os;
5844
};
5945

46+
} // end namespace cxx_synthesis
6047
} // end namespace swift
6148

6249
#endif

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "DeclAndTypePrinter.h"
14-
#include "ClangSyntaxPrinter.h"
14+
#include "CxxSynthesis.h"
1515
#include "PrimitiveTypeMapping.h"
16-
#include "PrintClangFunction.h"
1716

1817
#include "swift/AST/ASTContext.h"
1918
#include "swift/AST/ASTMangler.h"
@@ -80,29 +79,6 @@ static bool isClangKeyword(Identifier name) {
8079
return isClangKeyword(name.str());
8180
}
8281

83-
bool DeclAndTypePrinter::isStrClangKeyword(StringRef name) {
84-
return ::isClangKeyword(name);
85-
}
86-
87-
// For a given Decl and Type, if the type is not an optional return
88-
// the type and OTK_None as the optionality. If the type is
89-
// optional, return the underlying object type, and an optionality
90-
// that is based on the type but overridden by the return value of
91-
// isImplicitlyUnwrappedOptional().
92-
std::pair<Type, OptionalTypeKind>
93-
DeclAndTypePrinter::getObjectTypeAndOptionality(const ValueDecl *D, Type ty) {
94-
OptionalTypeKind kind;
95-
if (auto objTy = ty->getReferenceStorageReferent()->getOptionalObjectType()) {
96-
kind = OTK_Optional;
97-
if (D->isImplicitlyUnwrappedOptional())
98-
kind = OTK_ImplicitlyUnwrappedOptional;
99-
100-
return {objTy, kind};
101-
}
102-
103-
return {ty, OTK_None};
104-
}
105-
10682
namespace {
10783
/// Whether the type being printed is in function param position.
10884
enum IsFunctionParam_t : bool {
@@ -122,14 +98,17 @@ static bool looksLikeInitMethod(ObjCSelector selector) {
12298
}
12399

124100
class DeclAndTypePrinter::Implementation
125-
: private DeclVisitor<DeclAndTypePrinter::Implementation>,
126-
private TypeVisitor<DeclAndTypePrinter::Implementation, void,
127-
Optional<OptionalTypeKind>>,
128-
private ClangSyntaxPrinter {
101+
: private DeclVisitor<DeclAndTypePrinter::Implementation>,
102+
private TypeVisitor<DeclAndTypePrinter::Implementation, void,
103+
Optional<OptionalTypeKind>>
104+
{
129105
using PrinterImpl = Implementation;
130106
friend ASTVisitor;
131107
friend TypeVisitor;
132108

109+
// The output stream is accessible through 'owningPrinter',
110+
// but it makes the code simpler to have it here too.
111+
raw_ostream &os;
133112
DeclAndTypePrinter &owningPrinter;
134113
OutputLanguageMode outputLang;
135114

@@ -147,7 +126,7 @@ class DeclAndTypePrinter::Implementation
147126
public:
148127
explicit Implementation(raw_ostream &out, DeclAndTypePrinter &owner,
149128
OutputLanguageMode outputLang)
150-
: ClangSyntaxPrinter(out), owningPrinter(owner), outputLang(outputLang) {}
129+
: os(out), owningPrinter(owner), outputLang(outputLang) {}
151130

152131
void print(const Decl *D) {
153132
PrettyStackTraceDecl trace("printing", D);
@@ -306,7 +285,17 @@ class DeclAndTypePrinter::Implementation
306285
// isImplicitlyUnwrappedOptional().
307286
static std::pair<Type, OptionalTypeKind>
308287
getObjectTypeAndOptionality(const ValueDecl *D, Type ty) {
309-
return DeclAndTypePrinter::getObjectTypeAndOptionality(D, ty);
288+
OptionalTypeKind kind;
289+
if (auto objTy =
290+
ty->getReferenceStorageReferent()->getOptionalObjectType()) {
291+
kind = OTK_Optional;
292+
if (D->isImplicitlyUnwrappedOptional())
293+
kind = OTK_ImplicitlyUnwrappedOptional;
294+
295+
return {objTy, kind};
296+
}
297+
298+
return {ty, OTK_None};
310299
}
311300

312301
// Ignore other declarations.
@@ -856,10 +845,7 @@ class DeclAndTypePrinter::Implementation
856845
FuncionSwiftABIInformation funcABI(FD, mangler);
857846

858847
os << "SWIFT_EXTERN ";
859-
860-
DeclAndTypeClangFunctionPrinter funcPrinter(os, owningPrinter.typeMapping);
861-
funcPrinter.printFunctionDeclAsCFunctionDecl(FD, funcABI.getSymbolName(),
862-
resultTy);
848+
printFunctionDeclAsCFunctionDecl(FD, funcABI.getSymbolName(), resultTy);
863849
// Swift functions can't throw exceptions, we can only
864850
// throw them from C++ when emitting C++ inline thunks for the Swift
865851
// functions.
@@ -1386,6 +1372,55 @@ class DeclAndTypePrinter::Implementation
13861372
TypeVisitor::visit(ty, optionalKind);
13871373
}
13881374

1375+
/// Where nullability information should be printed.
1376+
enum class NullabilityPrintKind {
1377+
Before,
1378+
After,
1379+
ContextSensitive,
1380+
};
1381+
1382+
void printNullability(Optional<OptionalTypeKind> kind,
1383+
NullabilityPrintKind printKind
1384+
= NullabilityPrintKind::After) {
1385+
if (!kind)
1386+
return;
1387+
1388+
switch (printKind) {
1389+
case NullabilityPrintKind::ContextSensitive:
1390+
switch (*kind) {
1391+
case OTK_None:
1392+
os << "nonnull";
1393+
break;
1394+
case OTK_Optional:
1395+
os << "nullable";
1396+
break;
1397+
case OTK_ImplicitlyUnwrappedOptional:
1398+
os << "null_unspecified";
1399+
break;
1400+
}
1401+
break;
1402+
case NullabilityPrintKind::After:
1403+
os << ' ';
1404+
LLVM_FALLTHROUGH;
1405+
case NullabilityPrintKind::Before:
1406+
switch (*kind) {
1407+
case OTK_None:
1408+
os << "_Nonnull";
1409+
break;
1410+
case OTK_Optional:
1411+
os << "_Nullable";
1412+
break;
1413+
case OTK_ImplicitlyUnwrappedOptional:
1414+
os << "_Null_unspecified";
1415+
break;
1416+
}
1417+
break;
1418+
}
1419+
1420+
if (printKind != NullabilityPrintKind::After)
1421+
os << ' ';
1422+
}
1423+
13891424
/// Determine whether this generic Swift nominal type maps to a
13901425
/// generic Objective-C class.
13911426
static bool hasGenericObjCType(const NominalTypeDecl *nominal) {

lib/PrintAsClang/DeclAndTypePrinter.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace clang {
2626
namespace swift {
2727

2828
class PrimitiveTypeMapping;
29-
class ValueDecl;
3029

3130
/// Responsible for printing a Swift Decl or Type in Objective-C, to be
3231
/// included in a Swift module's ObjC compatibility header.
@@ -89,12 +88,6 @@ class DeclAndTypePrinter {
8988
/// Returns the name of an <os/object.h> type minus the leading "OS_",
9089
/// or an empty string if \p decl is not an <os/object.h> type.
9190
static StringRef maybeGetOSObjectBaseName(const clang::NamedDecl *decl);
92-
93-
static std::pair<Type, OptionalTypeKind>
94-
getObjectTypeAndOptionality(const ValueDecl *D, Type ty);
95-
96-
/// Returns true if \p name matches a keyword in any Clang language mode.
97-
static bool isStrClangKeyword(StringRef name);
9891
};
9992

10093
} // end namespace swift

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#include "ModuleContentsWriter.h"
1414

15-
#include "ClangSyntaxPrinter.h"
15+
#include "CxxSynthesis.h"
1616
#include "DeclAndTypePrinter.h"
1717
#include "OutputLanguageMode.h"
1818
#include "PrimitiveTypeMapping.h"
@@ -642,34 +642,26 @@ swift::printModuleContentsAsObjC(raw_ostream &os,
642642
void swift::printModuleContentsAsCxx(
643643
raw_ostream &os, llvm::SmallPtrSetImpl<ImportModuleTy> &imports,
644644
ModuleDecl &M) {
645-
std::string moduleContentsBuf;
646-
llvm::raw_string_ostream moduleOS{moduleContentsBuf};
647-
std::string modulePrologueBuf;
648-
llvm::raw_string_ostream prologueOS{modulePrologueBuf};
649-
650-
ModuleWriter(moduleOS, prologueOS, imports, M, getRequiredAccess(M),
651-
OutputLanguageMode::Cxx)
652-
.write();
653-
654-
// FIXME: refactor.
655-
if (!prologueOS.str().empty()) {
656-
os << "#endif\n";
657-
os << "#ifdef __cplusplus\n";
658-
os << "namespace ";
659-
M.ValueDecl::getName().print(os);
660-
os << " {\n";
661-
os << "namespace " << cxx_synthesis::getCxxImplNamespaceName() << " {\n";
662-
os << "#endif\n\n";
663-
664-
os << prologueOS.str();
665-
666-
os << "\n#ifdef __cplusplus\n";
667-
os << "}\n";
668-
os << "}\n";
669-
}
670-
645+
using cxx_synthesis::CxxPrinter;
671646
// Construct a C++ namespace for the module.
672-
ClangSyntaxPrinter(os).printNamespace(
647+
CxxPrinter(os).printNamespace(
673648
[&](raw_ostream &os) { M.ValueDecl::getName().print(os); },
674-
[&](raw_ostream &os) { os << moduleOS.str(); });
649+
[&](raw_ostream &os) {
650+
std::string moduleContentsBuf;
651+
llvm::raw_string_ostream moduleOS{moduleContentsBuf};
652+
std::string modulePrologueBuf;
653+
llvm::raw_string_ostream prologueOS{modulePrologueBuf};
654+
655+
ModuleWriter(moduleOS, prologueOS, imports, M, getRequiredAccess(M),
656+
OutputLanguageMode::Cxx)
657+
.write();
658+
659+
// The module's prologue contains implementation details,
660+
// like extern "C" symbols for the referenced Swift functions.
661+
CxxPrinter(os).printNamespace(
662+
cxx_synthesis::getCxxImplNamespaceName(),
663+
[&](raw_ostream &os) { os << prologueOS.str(); });
664+
665+
os << moduleOS.str();
666+
});
675667
}

0 commit comments

Comments
 (0)