Skip to content

Commit 7abd2af

Browse files
committed
Revert "Merge pull request swiftlang#41928 from hyp/i/ref-ptm"
This reverts commit e5bd9da, reversing changes made to b07b700.
1 parent b8b9226 commit 7abd2af

File tree

6 files changed

+114
-191
lines changed

6 files changed

+114
-191
lines changed

lib/PrintAsClang/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ add_swift_host_library(swiftPrintAsClang STATIC
33
CxxSynthesis.cpp
44
DeclAndTypePrinter.cpp
55
ModuleContentsWriter.cpp
6-
PrimitiveTypeMapping.cpp
76
PrintAsClang.cpp)
87
target_link_libraries(swiftPrintAsClang PRIVATE
98
swiftAST

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
#include "DeclAndTypePrinter.h"
1414
#include "CxxSynthesis.h"
15-
#include "PrimitiveTypeMapping.h"
1615

1716
#include "swift/AST/ASTContext.h"
1817
#include "swift/AST/ASTMangler.h"
@@ -1443,7 +1442,7 @@ class DeclAndTypePrinter::Implementation
14431442
/// Otherwise returns null.
14441443
const ClassDecl *getObjCBridgedClass(const NominalTypeDecl *nominal) {
14451444
// Print known types as their unbridged type.
1446-
if (owningPrinter.typeMapping.getKnownObjCTypeInfo(nominal))
1445+
if (getKnownTypeInfo(nominal))
14471446
return nullptr;
14481447

14491448
// Print imported bridgeable decls as their unbridged type.
@@ -1547,6 +1546,102 @@ class DeclAndTypePrinter::Implementation
15471546
return false;
15481547
}
15491548

1549+
/// If \p typeDecl is one of the standard library types used to map in Clang
1550+
/// primitives and basic types, return the info in \c specialNames containing
1551+
/// the Clang name and whether it can be nullable in C.
1552+
Optional<CTypeInfo> getKnownTypeInfo(const TypeDecl *typeDecl) {
1553+
auto &specialNames = owningPrinter.specialNames;
1554+
if (specialNames.empty()) {
1555+
ASTContext &ctx = getASTContext();
1556+
#define MAP(SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY) \
1557+
specialNames[{ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME)}] = \
1558+
{ CLANG_REPR, NEEDS_NULLABILITY }
1559+
1560+
MAP(CBool, "bool", false);
1561+
1562+
MAP(CChar, "char", false);
1563+
MAP(CWideChar, "wchar_t", false);
1564+
MAP(CChar16, "char16_t", false);
1565+
MAP(CChar32, "char32_t", false);
1566+
1567+
MAP(CSignedChar, "signed char", false);
1568+
MAP(CShort, "short", false);
1569+
MAP(CInt, "int", false);
1570+
MAP(CLong, "long", false);
1571+
MAP(CLongLong, "long long", false);
1572+
1573+
MAP(CUnsignedChar, "unsigned char", false);
1574+
MAP(CUnsignedShort, "unsigned short", false);
1575+
MAP(CUnsignedInt, "unsigned int", false);
1576+
MAP(CUnsignedLong, "unsigned long", false);
1577+
MAP(CUnsignedLongLong, "unsigned long long", false);
1578+
1579+
MAP(CFloat, "float", false);
1580+
MAP(CDouble, "double", false);
1581+
1582+
MAP(Int8, "int8_t", false);
1583+
MAP(Int16, "int16_t", false);
1584+
MAP(Int32, "int32_t", false);
1585+
MAP(Int64, "int64_t", false);
1586+
MAP(UInt8, "uint8_t", false);
1587+
MAP(UInt16, "uint16_t", false);
1588+
MAP(UInt32, "uint32_t", false);
1589+
MAP(UInt64, "uint64_t", false);
1590+
1591+
MAP(Float, "float", false);
1592+
MAP(Double, "double", false);
1593+
MAP(Float32, "float", false);
1594+
MAP(Float64, "double", false);
1595+
1596+
MAP(Int, "NSInteger", false);
1597+
MAP(UInt, "NSUInteger", false);
1598+
MAP(Bool, "BOOL", false);
1599+
1600+
MAP(OpaquePointer, "void *", true);
1601+
MAP(UnsafeRawPointer, "void const *", true);
1602+
MAP(UnsafeMutableRawPointer, "void *", true);
1603+
1604+
Identifier ID_ObjectiveC = ctx.Id_ObjectiveC;
1605+
specialNames[{ID_ObjectiveC, ctx.getIdentifier("ObjCBool")}]
1606+
= { "BOOL", false};
1607+
specialNames[{ID_ObjectiveC, ctx.getIdentifier("Selector")}]
1608+
= { "SEL", true };
1609+
specialNames[{ID_ObjectiveC,
1610+
ctx.getIdentifier(
1611+
ctx.getSwiftName(KnownFoundationEntity::NSZone))}]
1612+
= { "struct _NSZone *", true };
1613+
1614+
specialNames[{ctx.Id_Darwin, ctx.getIdentifier("DarwinBoolean")}]
1615+
= { "Boolean", false};
1616+
1617+
specialNames[{ctx.Id_CoreGraphics, ctx.Id_CGFloat}]
1618+
= { "CGFloat", false };
1619+
1620+
specialNames[{ctx.Id_CoreFoundation, ctx.Id_CGFloat}]
1621+
= { "CGFloat", false };
1622+
1623+
// Use typedefs we set up for SIMD vector types.
1624+
#define MAP_SIMD_TYPE(BASENAME, _, __) \
1625+
specialNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "2")}] \
1626+
= { "swift_" #BASENAME "2", false }; \
1627+
specialNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "3")}] \
1628+
= { "swift_" #BASENAME "3", false }; \
1629+
specialNames[{ctx.Id_simd, ctx.getIdentifier(#BASENAME "4")}] \
1630+
= { "swift_" #BASENAME "4", false };
1631+
#include "swift/ClangImporter/SIMDMappedTypes.def"
1632+
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
1633+
"must add or remove special name mappings if max number of "
1634+
"SIMD elements is changed");
1635+
}
1636+
1637+
Identifier moduleName = typeDecl->getModuleContext()->getName();
1638+
Identifier name = typeDecl->getName();
1639+
auto iter = specialNames.find({moduleName, name});
1640+
if (iter == specialNames.end())
1641+
return None;
1642+
return iter->second;
1643+
}
1644+
15501645
/// If \p typeDecl is one of the standard library types used to map in Clang
15511646
/// primitives and basic types, print out the appropriate spelling and
15521647
/// return true.
@@ -1555,8 +1650,7 @@ class DeclAndTypePrinter::Implementation
15551650
/// for interfacing with C and Objective-C.
15561651
bool printIfKnownSimpleType(const TypeDecl *typeDecl,
15571652
Optional<OptionalTypeKind> optionalKind) {
1558-
auto knownTypeInfo =
1559-
owningPrinter.typeMapping.getKnownObjCTypeInfo(typeDecl);
1653+
Optional<CTypeInfo> knownTypeInfo = getKnownTypeInfo(typeDecl);
15601654
if (!knownTypeInfo)
15611655
return false;
15621656
os << knownTypeInfo->name;

lib/PrintAsClang/DeclAndTypePrinter.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ namespace clang {
2525

2626
namespace swift {
2727

28-
class PrimitiveTypeMapping;
29-
3028
/// Responsible for printing a Swift Decl or Type in Objective-C, to be
3129
/// included in a Swift module's ObjC compatibility header.
3230
class DeclAndTypePrinter {
@@ -41,10 +39,22 @@ class DeclAndTypePrinter {
4139
raw_ostream &os;
4240
raw_ostream &prologueOS;
4341
const DelayedMemberSet &delayedMembers;
44-
PrimitiveTypeMapping &typeMapping;
4542
AccessLevel minRequiredAccess;
4643
OutputLanguageMode outputLang;
4744

45+
struct CTypeInfo {
46+
StringRef name;
47+
bool canBeNullable;
48+
};
49+
50+
/// A map from {Module, TypeName} pairs to {C name, C nullability} pairs.
51+
///
52+
/// This is populated on first use with a list of known Swift types that are
53+
/// translated directly by the ObjC printer instead of structurally, allowing
54+
/// it to do things like map 'Int' to 'NSInteger' and 'Float' to 'float'.
55+
/// In some sense it's the reverse of the ClangImporter's MappedTypes.def.
56+
llvm::DenseMap<std::pair<Identifier, Identifier>, CTypeInfo> specialNames;
57+
4858
/// The name 'CFTypeRef'.
4959
///
5060
/// Cached for convenience.
@@ -54,12 +64,10 @@ class DeclAndTypePrinter {
5464

5565
public:
5666
DeclAndTypePrinter(ModuleDecl &mod, raw_ostream &out, raw_ostream &prologueOS,
57-
DelayedMemberSet &delayed,
58-
PrimitiveTypeMapping &typeMapping, AccessLevel access,
67+
DelayedMemberSet &delayed, AccessLevel access,
5968
OutputLanguageMode outputLang)
6069
: M(mod), os(out), prologueOS(prologueOS), delayedMembers(delayed),
61-
typeMapping(typeMapping), minRequiredAccess(access),
62-
outputLang(outputLang) {}
70+
minRequiredAccess(access), outputLang(outputLang) {}
6371

6472
/// Returns true if \p VD should be included in a compatibility header for
6573
/// the options the printer was constructed with.

lib/PrintAsClang/ModuleContentsWriter.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "CxxSynthesis.h"
1616
#include "DeclAndTypePrinter.h"
1717
#include "OutputLanguageMode.h"
18-
#include "PrimitiveTypeMapping.h"
1918

2019
#include "swift/AST/ExistentialLayout.h"
2120
#include "swift/AST/Module.h"
@@ -124,7 +123,6 @@ class ModuleWriter {
124123
llvm::DenseMap<const TypeDecl *, std::pair<EmissionState, bool>> seenTypes;
125124
std::vector<const Decl *> declsToWrite;
126125
DelayedMemberSet delayedMembers;
127-
PrimitiveTypeMapping typeMapping;
128126
DeclAndTypePrinter printer;
129127
OutputLanguageMode outputLangMode;
130128

@@ -133,8 +131,7 @@ class ModuleWriter {
133131
llvm::SmallPtrSetImpl<ImportModuleTy> &imports, ModuleDecl &mod,
134132
AccessLevel access, OutputLanguageMode outputLang)
135133
: os(os), imports(imports), M(mod),
136-
printer(M, os, prologueOS, delayedMembers, typeMapping, access,
137-
outputLang),
134+
printer(M, os, prologueOS, delayedMembers, access, outputLang),
138135
outputLangMode(outputLang) {}
139136

140137
/// Returns true if we added the decl's module to the import set, false if

lib/PrintAsClang/PrimitiveTypeMapping.cpp

Lines changed: 0 additions & 112 deletions
This file was deleted.

lib/PrintAsClang/PrimitiveTypeMapping.h

Lines changed: 0 additions & 63 deletions
This file was deleted.

0 commit comments

Comments
 (0)