1313#include " PrimitiveTypeMapping.h"
1414#include " swift/AST/ASTContext.h"
1515#include " swift/AST/Decl.h"
16+ #include " swift/AST/Identifier.h"
1617#include " swift/AST/Module.h"
1718#include " swift/Basic/Assertions.h"
1819#include " swift/ClangImporter/ClangImporter.h"
20+ #include < optional>
1921
2022using namespace swift ;
2123
2224// / 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,
2526 Identifier typeName) {
2627 auto module = ctx.getLoadedModule (moduleName);
2728 if (!module )
@@ -69,18 +70,15 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
6970
7071 // Map stdlib types.
7172#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), \
7474 {CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
7575 std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY})
7676#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), \
7978 {OBJC_REPR, std::optional<StringRef>(C_REPR), \
8079 std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY})
8180#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), \
8482 {OBJC_REPR, std::optional<StringRef>(C_REPR), \
8583 std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY})
8684
@@ -140,10 +138,10 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
140138 {" BOOL" , std::nullopt , std::nullopt , false });
141139 addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" Selector" ),
142140 {" 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 });
147145
148146 addMappedType (ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" ),
149147 {" Boolean" , std::nullopt , std::nullopt , false });
@@ -157,17 +155,33 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
157155 // Use typedefs we set up for SIMD vector types.
158156#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
159157 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 ); \
163166 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 ); \
167175 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+
171185#include " swift/ClangImporter/SIMDMappedTypes.def"
172186 static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
173187 " must add or remove special name mappings if max number of "
@@ -192,15 +206,17 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
192206std::optional<PrimitiveTypeMapping::ClangTypeInfo>
193207PrimitiveTypeMapping::getKnownObjCTypeInfo (const TypeDecl *typeDecl) {
194208 if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl))
195- return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable };
209+ return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable ,
210+ typeInfo->simd };
196211 return std::nullopt ;
197212}
198213
199214std::optional<PrimitiveTypeMapping::ClangTypeInfo>
200215PrimitiveTypeMapping::getKnownCTypeInfo (const TypeDecl *typeDecl) {
201216 if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
202217 if (typeInfo->cName )
203- return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable };
218+ return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable ,
219+ typeInfo->simd };
204220 }
205221 return std::nullopt ;
206222}
@@ -209,7 +225,25 @@ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
209225PrimitiveTypeMapping::getKnownCxxTypeInfo (const TypeDecl *typeDecl) {
210226 if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
211227 if (typeInfo->cxxName )
212- return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable };
228+ return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable ,
229+ typeInfo->simd };
213230 }
214231 return std::nullopt ;
215232}
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