@@ -22,7 +22,13 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
2222 assert (mappedTypeNames.empty () && " expected empty type map" );
2323#define MAP (SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY ) \
2424 mappedTypeNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = { \
25- CLANG_REPR, NEEDS_NULLABILITY}
25+ CLANG_REPR, \
26+ Optional<StringRef>(CLANG_REPR), \
27+ NEEDS_NULLABILITY \
28+ }
29+ #define MAP_C (SWIFT_NAME, OBJC_REPR, C_REPR, NEEDS_NULLABILITY ) \
30+ mappedTypeNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = { \
31+ OBJC_REPR, Optional<StringRef>(C_REPR), NEEDS_NULLABILITY}
2632
2733 MAP (CBool, " bool" , false );
2834
@@ -60,53 +66,74 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
6066 MAP (Float32, " float" , false );
6167 MAP (Float64, " double" , false );
6268
63- MAP (Int, " NSInteger" , false );
64- MAP (UInt, " NSUInteger" , false );
65- MAP (Bool, " BOOL" , false );
69+ MAP_C (Int, " NSInteger" , " ptrdiff_t " , false );
70+ MAP_C (UInt, " NSUInteger" , " size_t " , false );
71+ MAP_C (Bool, " BOOL" , " bool " , false );
6672
6773 MAP (OpaquePointer, " void *" , true );
6874 MAP (UnsafeRawPointer, " void const *" , true );
6975 MAP (UnsafeMutableRawPointer, " void *" , true );
7076
7177 Identifier ID_ObjectiveC = ctx.Id_ObjectiveC ;
72- mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" ObjCBool" )}] = {" BOOL " ,
73- false };
74- mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" Selector" )}] = {" SEL " ,
75- true };
78+ mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" ObjCBool" )}] = {
79+ " BOOL " , None, false };
80+ mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" Selector" )}] = {
81+ " SEL " , None, true };
7682 mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (ctx.getSwiftName (
7783 KnownFoundationEntity::NSZone))}] = {
78- " struct _NSZone *" , true };
84+ " struct _NSZone *" , None, true };
7985
8086 mappedTypeNames[{ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" )}] = {
81- " Boolean" , false };
87+ " Boolean" , None, false };
8288
83- mappedTypeNames[{ctx.Id_CoreGraphics , ctx.Id_CGFloat }] = {" CGFloat" , false };
89+ mappedTypeNames[{ctx.Id_CoreGraphics , ctx.Id_CGFloat }] = {" CGFloat" , None,
90+ false };
8491
85- mappedTypeNames[{ctx.Id_CoreFoundation , ctx.Id_CGFloat }] = {" CGFloat" , false };
92+ mappedTypeNames[{ctx.Id_CoreFoundation , ctx.Id_CGFloat }] = {" CGFloat" , None,
93+ false };
8694
8795 // Use typedefs we set up for SIMD vector types.
8896#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
8997 mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" )}] = { \
90- " swift_" #BASENAME " 2" , false }; \
98+ " swift_" #BASENAME " 2" , Optional<StringRef>(" swift_" #BASENAME " 2" ), \
99+ false }; \
91100 mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" )}] = { \
92- " swift_" #BASENAME " 3" , false }; \
101+ " swift_" #BASENAME " 3" , Optional<StringRef>(" swift_" #BASENAME " 3" ), \
102+ false }; \
93103 mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" )}] = { \
94- " swift_" #BASENAME " 4" , false };
104+ " swift_" #BASENAME " 4" , Optional<StringRef>(" swift_" #BASENAME " 4" ), \
105+ false };
95106#include " swift/ClangImporter/SIMDMappedTypes.def"
96107 static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
97108 " must add or remove special name mappings if max number of "
98109 " SIMD elements is changed" );
99110}
100111
101- Optional< PrimitiveTypeMapping::ObjCClangTypeInfo>
102- PrimitiveTypeMapping::getKnownObjCTypeInfo (const TypeDecl *typeDecl) {
112+ PrimitiveTypeMapping::ClangTypeInfo *
113+ PrimitiveTypeMapping::getMappedTypeInfoOrNull (const TypeDecl *typeDecl) {
103114 if (mappedTypeNames.empty ())
104115 initialize (typeDecl->getASTContext ());
105116
106117 Identifier moduleName = typeDecl->getModuleContext ()->getName ();
107118 Identifier name = typeDecl->getName ();
108119 auto iter = mappedTypeNames.find ({moduleName, name});
109120 if (iter == mappedTypeNames.end ())
110- return None;
111- return ObjCClangTypeInfo{iter->second .objcName , iter->second .canBeNullable };
121+ return nullptr ;
122+ return &iter->second ;
123+ }
124+
125+ Optional<PrimitiveTypeMapping::ObjCClangTypeInfo>
126+ PrimitiveTypeMapping::getKnownObjCTypeInfo (const TypeDecl *typeDecl) {
127+ if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl))
128+ return ObjCClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable };
129+ return None;
130+ }
131+
132+ Optional<PrimitiveTypeMapping::CClangTypeInfo>
133+ PrimitiveTypeMapping::getKnownCTypeInfo (const TypeDecl *typeDecl) {
134+ if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
135+ if (typeInfo->cName )
136+ return CClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable };
137+ }
138+ return None;
112139}
0 commit comments