Skip to content

Commit b2a27a5

Browse files
committed
[PrintAsObjC] Clarify the 'specialNames' map in DeclAndTypePrinter
And comment the other cached members a little better.
1 parent dbbbbe5 commit b2a27a5

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

lib/PrintAsObjC/DeclAndTypePrinter.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,17 +1443,15 @@ class DeclAndTypePrinter::Implementation
14431443
}
14441444

14451445
/// If \p typeDecl is one of the standard library types used to map in Clang
1446-
/// primitives and basic types, return the address of the info in
1447-
/// \c specialNames containing the Clang name and whether it can be optional.
1448-
///
1449-
/// Returns null if the name is not one of these known types.
1450-
const NameAndOptional *getKnownTypeInfo(const TypeDecl *typeDecl) {
1446+
/// primitives and basic types, return the info in \c specialNames containing
1447+
/// the Clang name and whether it can be nullable in C.
1448+
Optional<CTypeInfo> getKnownTypeInfo(const TypeDecl *typeDecl) {
14511449
auto &specialNames = owningPrinter.specialNames;
14521450
if (specialNames.empty()) {
14531451
ASTContext &ctx = getASTContext();
14541452
#define MAP(SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY) \
14551453
specialNames[{ctx.StdlibModuleName, ctx.getIdentifier(#SWIFT_NAME)}] = \
1456-
{ CLANG_REPR, NEEDS_NULLABILITY}
1454+
{ CLANG_REPR, NEEDS_NULLABILITY }
14571455

14581456
MAP(CBool, "bool", false);
14591457

@@ -1534,8 +1532,8 @@ class DeclAndTypePrinter::Implementation
15341532
Identifier name = typeDecl->getName();
15351533
auto iter = specialNames.find({moduleName, name});
15361534
if (iter == specialNames.end())
1537-
return nullptr;
1538-
return &iter->second;
1535+
return None;
1536+
return iter->second;
15391537
}
15401538

15411539
/// If \p typeDecl is one of the standard library types used to map in Clang
@@ -1546,11 +1544,11 @@ class DeclAndTypePrinter::Implementation
15461544
/// for interfacing with C and Objective-C.
15471545
bool printIfKnownSimpleType(const TypeDecl *typeDecl,
15481546
Optional<OptionalTypeKind> optionalKind) {
1549-
auto *knownTypeInfo = getKnownTypeInfo(typeDecl);
1547+
Optional<CTypeInfo> knownTypeInfo = getKnownTypeInfo(typeDecl);
15501548
if (!knownTypeInfo)
15511549
return false;
1552-
os << knownTypeInfo->first;
1553-
if (knownTypeInfo->second)
1550+
os << knownTypeInfo->name;
1551+
if (knownTypeInfo->canBeNullable)
15541552
printNullability(optionalKind);
15551553
return true;
15561554
}

lib/PrintAsObjC/DeclAndTypePrinter.h

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,28 @@ class DeclAndTypePrinter {
3838
const DelayedMemberSet &delayedMembers;
3939
AccessLevel minRequiredAccess;
4040

41-
using NameAndOptional = std::pair<StringRef, bool>;
42-
llvm::DenseMap<std::pair<Identifier, Identifier>, NameAndOptional>
43-
specialNames;
41+
struct CTypeInfo {
42+
StringRef name;
43+
bool canBeNullable;
44+
};
4445

45-
// Cached for convenience.
46+
/// A map from {Module, TypeName} pairs to {C name, C nullability} pairs.
47+
///
48+
/// This is populated on first use with a list of known Swift types that are
49+
/// translated directly by the ObjC printer instead of structurally, allowing
50+
/// it to do things like map 'Int' to 'NSInteger' and 'Float' to 'float'.
51+
/// In some sense it's the reverse of the ClangImporter's MappedTypes.def.
52+
llvm::DenseMap<std::pair<Identifier, Identifier>, CTypeInfo> specialNames;
53+
54+
/// The name 'CFTypeRef'.
55+
///
56+
/// Cached for convenience.
4657
Identifier ID_CFTypeRef;
58+
59+
/// The protocol type 'NSCopying', or a null type if Foundation has not been
60+
/// imported.
61+
///
62+
/// Cached for convenience.
4763
Optional<Type> NSCopyingType;
4864

4965
Implementation getImpl();

0 commit comments

Comments
 (0)