13
13
#include " PrimitiveTypeMapping.h"
14
14
#include " swift/AST/ASTContext.h"
15
15
#include " swift/AST/Decl.h"
16
+ #include " swift/AST/Identifier.h"
16
17
#include " swift/AST/Module.h"
17
18
#include " swift/Basic/Assertions.h"
18
19
#include " swift/ClangImporter/ClangImporter.h"
20
+ #include < optional>
19
21
20
22
using namespace swift ;
21
23
22
24
// / Find the implementation of the named type in the named module if loaded.
23
- static TypeDecl *findTypeInModuleByName (ASTContext &ctx,
24
- Identifier moduleName,
25
+ static TypeDecl *findTypeInModuleByName (ASTContext &ctx, Identifier moduleName,
25
26
Identifier typeName) {
26
27
auto module = ctx.getLoadedModule (moduleName);
27
28
if (!module )
@@ -69,18 +70,15 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
69
70
70
71
// Map stdlib types.
71
72
#define MAP (SWIFT_NAME, CLANG_REPR, NEEDS_NULLABILITY ) \
72
- addMappedType (ctx.StdlibModuleName , \
73
- ctx.getIdentifier (#SWIFT_NAME), \
73
+ addMappedType (ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME), \
74
74
{CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
75
75
std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY})
76
76
#define MAP_C (SWIFT_NAME, OBJC_REPR, C_REPR, NEEDS_NULLABILITY ) \
77
- addMappedType (ctx.StdlibModuleName , \
78
- ctx.getIdentifier (#SWIFT_NAME), \
77
+ addMappedType (ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME), \
79
78
{OBJC_REPR, std::optional<StringRef>(C_REPR), \
80
79
std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY})
81
80
#define MAP_CXX (SWIFT_NAME, OBJC_REPR, C_REPR, CXX_REPR, NEEDS_NULLABILITY ) \
82
- addMappedType (ctx.StdlibModuleName , \
83
- ctx.getIdentifier (#SWIFT_NAME), \
81
+ addMappedType (ctx.StdlibModuleName , ctx.getIdentifier (#SWIFT_NAME), \
84
82
{OBJC_REPR, std::optional<StringRef>(C_REPR), \
85
83
std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY})
86
84
@@ -140,10 +138,10 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
140
138
{" BOOL" , std::nullopt , std::nullopt , false });
141
139
addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" Selector" ),
142
140
{" 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 });
141
+ addMappedType (
142
+ ctx.Id_ObjectiveC ,
143
+ ctx. getIdentifier ( swift::getSwiftName ( KnownFoundationEntity::NSZone)),
144
+ {" struct _NSZone *" , std::nullopt , std::nullopt , true });
147
145
148
146
addMappedType (ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" ),
149
147
{" Boolean" , std::nullopt , std::nullopt , false });
@@ -157,17 +155,33 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
157
155
// Use typedefs we set up for SIMD vector types.
158
156
#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
159
157
StringRef simd2##BASENAME = " swift_" #BASENAME " 2" ; \
160
- addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" ), \
161
- {simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }, \
162
- /* applyToUnderlying*/ false ); \
158
+ addMappedType ( \
159
+ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" ), \
160
+ {simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false , true }, \
161
+ /* applyToUnderlying*/ false ); \
162
+ addMappedType ( \
163
+ ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 2" ), \
164
+ {simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false , true }, \
165
+ /* applyToUnderlying*/ false ); \
163
166
StringRef simd3##BASENAME = " swift_" #BASENAME " 3" ; \
164
- addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" ), \
165
- {simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }, \
166
- /* applyToUnderlying*/ false ); \
167
+ addMappedType ( \
168
+ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" ), \
169
+ {simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false , true }, \
170
+ /* applyToUnderlying*/ false ); \
171
+ addMappedType ( \
172
+ ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 3" ), \
173
+ {simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false , true }, \
174
+ /* applyToUnderlying*/ false ); \
167
175
StringRef simd4##BASENAME = " swift_" #BASENAME " 4" ; \
168
- addMappedType (ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" ), \
169
- {simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false }, \
170
- /* applyToUnderlying*/ false );
176
+ addMappedType ( \
177
+ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" ), \
178
+ {simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false , true }, \
179
+ /* applyToUnderlying*/ false ); \
180
+ addMappedType ( \
181
+ ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 4" ), \
182
+ {simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false , true }, \
183
+ /* applyToUnderlying*/ false );
184
+
171
185
#include " swift/ClangImporter/SIMDMappedTypes.def"
172
186
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
173
187
" must add or remove special name mappings if max number of "
@@ -192,15 +206,17 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
192
206
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
193
207
PrimitiveTypeMapping::getKnownObjCTypeInfo (const TypeDecl *typeDecl) {
194
208
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl))
195
- return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable };
209
+ return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable ,
210
+ typeInfo->simd };
196
211
return std::nullopt ;
197
212
}
198
213
199
214
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
200
215
PrimitiveTypeMapping::getKnownCTypeInfo (const TypeDecl *typeDecl) {
201
216
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
202
217
if (typeInfo->cName )
203
- return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable };
218
+ return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable ,
219
+ typeInfo->simd };
204
220
}
205
221
return std::nullopt ;
206
222
}
@@ -209,7 +225,25 @@ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
209
225
PrimitiveTypeMapping::getKnownCxxTypeInfo (const TypeDecl *typeDecl) {
210
226
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
211
227
if (typeInfo->cxxName )
212
- return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable };
228
+ return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable ,
229
+ typeInfo->simd };
213
230
}
214
231
return std::nullopt ;
215
232
}
233
+
234
+ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
235
+ PrimitiveTypeMapping::getKnownSIMDTypeInfo (Type t, ASTContext &ctx) {
236
+ auto vecTy = t->getAs <BuiltinVectorType>();
237
+ if (!vecTy)
238
+ return std::nullopt ;
239
+
240
+ auto elemTy = vecTy->getElementType ();
241
+ auto numElems = vecTy->getNumElements ();
242
+
243
+ std::string elemTyName = elemTy.getString ();
244
+ // While the element type starts with an upper case, vector types start with
245
+ // lower case.
246
+ elemTyName[0 ] = std::tolower (elemTyName[0 ]);
247
+ Identifier swiftName = ctx.getIdentifier (" swift_" + elemTyName + std::to_string (numElems));
248
+ return ClangTypeInfo{swiftName.str (), false , true };
249
+ }
0 commit comments