19
19
20
20
using namespace swift ;
21
21
22
+ // / Find the implementation of the named type in the named module if loaded.
23
+ static TypeDecl *findTypeInModuleByName (ASTContext &ctx,
24
+ Identifier moduleName,
25
+ Identifier typeName) {
26
+ auto module = ctx.getLoadedModule (moduleName);
27
+ if (!module )
28
+ return nullptr ;
29
+
30
+ // Find all of the declarations with this name in the Swift module.
31
+ SmallVector<ValueDecl *, 1 > results;
32
+ module ->lookupValue (typeName, NLKind::UnqualifiedLookup, results);
33
+ assert (results.size () <= 1 &&
34
+ " Expected at most one match for a primitive type" );
35
+ for (auto result : results) {
36
+ if (auto nominal = dyn_cast<NominalTypeDecl>(result))
37
+ return nominal;
38
+
39
+ if (auto typealias = dyn_cast<TypeAliasDecl>(result))
40
+ return typealias;
41
+ }
42
+
43
+ return nullptr ;
44
+ }
45
+
22
46
void PrimitiveTypeMapping::initialize (ASTContext &ctx) {
23
47
assert (mappedTypeNames.empty () && " expected empty type map" );
48
+
49
+ auto addMappedType = [&](Identifier moduleName, Identifier typeName,
50
+ FullClangTypeInfo info,
51
+ bool applyToUnderlying = true ) {
52
+ auto decl = findTypeInModuleByName (ctx, moduleName, typeName);
53
+ if (!decl)
54
+ return ;
55
+
56
+ // Always map a direct definition match. Either the nominal decl or the
57
+ // typealias itself.
58
+ mappedTypeNames[decl] = info;
59
+
60
+ // If the underlying type of a typealias doesn't have a type, set it here.
61
+ // This aims to reproduce the typealias behavior from BuiltinMappedTypes.
62
+ auto typealias = dyn_cast<TypeAliasDecl>(decl);
63
+ if (applyToUnderlying && typealias) {
64
+ auto underlying = typealias->getDeclaredInterfaceType ()->getAnyNominal ();
65
+ if (underlying && !mappedTypeNames.contains (underlying))
66
+ mappedTypeNames[underlying] = info;
67
+ }
68
+ };
69
+
70
+ // Map stdlib types.
24
71
#define MAP (SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY ) \
25
- mappedTypeNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = { \
26
- CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
27
- std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY}
72
+ addMappedType (ctx.StdlibModuleName , \
73
+ ctx.getIdentifier (#SWIFT_NAME), \
74
+ {CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
75
+ std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY})
28
76
#define MAP_C (SWIFT_NAME, OBJC_REPR, C_REPR, NEEDS_NULLABILITY ) \
29
- mappedTypeNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = { \
30
- OBJC_REPR, std::optional<StringRef>(C_REPR), \
31
- std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY}
77
+ addMappedType (ctx.StdlibModuleName , \
78
+ ctx.getIdentifier (#SWIFT_NAME), \
79
+ {OBJC_REPR, std::optional<StringRef>(C_REPR), \
80
+ std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY})
32
81
#define MAP_CXX (SWIFT_NAME, OBJC_REPR, C_REPR, CXX_REPR, NEEDS_NULLABILITY ) \
33
- mappedTypeNames[{ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME)}] = { \
34
- OBJC_REPR, std::optional<StringRef>(C_REPR), \
35
- std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY}
82
+ addMappedType (ctx.StdlibModuleName , \
83
+ ctx.getIdentifier (#SWIFT_NAME), \
84
+ {OBJC_REPR, std::optional<StringRef>(C_REPR), \
85
+ std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY})
36
86
37
87
MAP (CBool, " bool" , false );
38
88
39
89
MAP (CChar, " char" , false );
40
- MAP (CWideChar, " wchar_t" , false );
41
90
MAP (CChar8, " char8_t" , false );
42
91
MAP (CChar16, " char16_t" , false );
43
92
MAP (CChar32, " char32_t" , false );
44
93
94
+ // Set after CChar32 to prefer char32_t for the shared underlying
95
+ // Unicode.Scalar. char32_t is stable across platforms.
96
+ MAP (CWideChar, " wchar_t" , false );
97
+
45
98
MAP (CSignedChar, " signed char" , false );
46
99
MAP (CShort, " short" , false );
47
100
MAP (CInt, " int" , false );
@@ -71,6 +124,8 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
71
124
MAP (Float32, " float" , false );
72
125
MAP (Float64, " double" , false );
73
126
127
+ MAP (Float16, " _Float16" , false );
128
+
74
129
MAP_CXX (Int, " NSInteger" , " ptrdiff_t" , " swift::Int" , false );
75
130
MAP_CXX (UInt, " NSUInteger" , " size_t" , " swift::UInt" , false );
76
131
MAP_C (Bool, " BOOL" , " bool" , false );
@@ -79,35 +134,40 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
79
134
MAP (UnsafeRawPointer, " void const *" , true );
80
135
MAP (UnsafeMutableRawPointer, " void *" , true );
81
136
82
- Identifier ID_ObjectiveC = ctx.Id_ObjectiveC ;
83
- mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" ObjCBool" )}] = {
84
- " BOOL" , std::nullopt, std::nullopt, false };
85
- mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (" Selector" )}] = {
86
- " SEL" , std::nullopt, std::nullopt, true };
87
- mappedTypeNames[{ID_ObjectiveC, ctx.getIdentifier (swift::getSwiftName (
88
- KnownFoundationEntity::NSZone))}] = {
89
- " struct _NSZone *" , std::nullopt, std::nullopt, true };
137
+ // Map other module types.
90
138
91
- mappedTypeNames[{ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" )}] = {
92
- " Boolean" , std::nullopt, std::nullopt, false };
139
+ addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" ObjCBool" ),
140
+ {" BOOL" , std::nullopt, std::nullopt, false });
141
+ addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" Selector" ),
142
+ {" SEL" , std::nullopt, std::nullopt, true });
143
+ addMappedType (ctx.Id_ObjectiveC ,
144
+ ctx.getIdentifier (swift::getSwiftName (
145
+ KnownFoundationEntity::NSZone)),
146
+ {" struct _NSZone *" , std::nullopt, std::nullopt, true });
93
147
94
- mappedTypeNames[{ ctx.Id_CoreGraphics , ctx.Id_CGFloat }] = {
95
- " CGFloat " , std::nullopt, std::nullopt, false };
148
+ addMappedType ( ctx.Id_Darwin , ctx.getIdentifier ( " DarwinBoolean " ),
149
+ { " Boolean " , std::nullopt, std::nullopt, false }) ;
96
150
97
- mappedTypeNames[{ctx.Id_CoreFoundation , ctx.Id_CGFloat }] = {
98
- " CGFloat" , std::nullopt, std::nullopt, false };
151
+ addMappedType (ctx.Id_CoreGraphics , ctx.Id_CGFloat ,
152
+ {" CGFloat" , std::nullopt, std::nullopt, false });
153
+
154
+ addMappedType (ctx.Id_CoreFoundation , ctx.Id_CGFloat ,
155
+ {" CGFloat" , std::nullopt, std::nullopt, false });
99
156
100
157
// Use typedefs we set up for SIMD vector types.
101
158
#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
102
159
StringRef simd2##BASENAME = " swift_" #BASENAME " 2" ; \
103
- mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" )}] = { \
104
- simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }; \
160
+ addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" ), \
161
+ {simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }, \
162
+ /* applyToUnderlying*/ false ); \
105
163
StringRef simd3##BASENAME = " swift_" #BASENAME " 3" ; \
106
- mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" )}] = { \
107
- simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }; \
164
+ addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" ), \
165
+ {simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }, \
166
+ /* applyToUnderlying*/ false ); \
108
167
StringRef simd4##BASENAME = " swift_" #BASENAME " 4" ; \
109
- mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" )}] = { \
110
- simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false };
168
+ addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" ), \
169
+ {simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false }, \
170
+ /* applyToUnderlying*/ false );
111
171
#include " swift/ClangImporter/SIMDMappedTypes.def"
112
172
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
113
173
" must add or remove special name mappings if max number of "
@@ -119,9 +179,11 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
119
179
if (mappedTypeNames.empty ())
120
180
initialize (typeDecl->getASTContext ());
121
181
122
- Identifier moduleName = typeDecl->getModuleContext ()->getName ();
123
- Identifier name = typeDecl->getName ();
124
- auto iter = mappedTypeNames.find ({moduleName, name});
182
+ auto nominal = dyn_cast<TypeDecl>(typeDecl);
183
+ if (!nominal)
184
+ return nullptr ;
185
+
186
+ auto iter = mappedTypeNames.find (nominal);
125
187
if (iter == mappedTypeNames.end ())
126
188
return nullptr ;
127
189
return &iter->second ;
0 commit comments