14
14
#include " swift/AST/ASTContext.h"
15
15
#include " swift/AST/Decl.h"
16
16
#include " swift/AST/Module.h"
17
+ #include " swift/AST/Type.h"
18
+ #include " swift/AST/Types.h"
17
19
#include " swift/Basic/Assertions.h"
18
20
#include " swift/ClangImporter/ClangImporter.h"
21
+ #include < cctype>
22
+ #include < optional>
23
+ #include < string>
19
24
20
25
using namespace swift ;
21
26
@@ -101,13 +106,19 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
101
106
#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
102
107
StringRef simd2##BASENAME = " swift_" #BASENAME " 2" ; \
103
108
mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" )}] = { \
104
- simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }; \
109
+ simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false , true }; \
110
+ mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 2" )}] = { \
111
+ simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false , true }; \
105
112
StringRef simd3##BASENAME = " swift_" #BASENAME " 3" ; \
106
113
mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" )}] = { \
107
- simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }; \
114
+ simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false , true }; \
115
+ mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 3" )}] = { \
116
+ simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false , true }; \
108
117
StringRef simd4##BASENAME = " swift_" #BASENAME " 4" ; \
109
118
mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" )}] = { \
110
- simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false };
119
+ simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false , true }; \
120
+ mappedTypeNames[{ctx.Id_simd , ctx.getIdentifier (" simd_" #BASENAME " 4" )}] = { \
121
+ simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false , true };
111
122
#include " swift/ClangImporter/SIMDMappedTypes.def"
112
123
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
113
124
" must add or remove special name mappings if max number of "
@@ -130,15 +141,17 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
130
141
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
131
142
PrimitiveTypeMapping::getKnownObjCTypeInfo (const TypeDecl *typeDecl) {
132
143
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl))
133
- return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable };
144
+ return ClangTypeInfo{typeInfo->objcName , typeInfo->canBeNullable ,
145
+ typeInfo->simd };
134
146
return std::nullopt;
135
147
}
136
148
137
149
std::optional<PrimitiveTypeMapping::ClangTypeInfo>
138
150
PrimitiveTypeMapping::getKnownCTypeInfo (const TypeDecl *typeDecl) {
139
151
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
140
152
if (typeInfo->cName )
141
- return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable };
153
+ return ClangTypeInfo{*typeInfo->cName , typeInfo->canBeNullable ,
154
+ typeInfo->simd };
142
155
}
143
156
return std::nullopt;
144
157
}
@@ -147,7 +160,32 @@ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
147
160
PrimitiveTypeMapping::getKnownCxxTypeInfo (const TypeDecl *typeDecl) {
148
161
if (auto *typeInfo = getMappedTypeInfoOrNull (typeDecl)) {
149
162
if (typeInfo->cxxName )
150
- return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable };
163
+ return ClangTypeInfo{*typeInfo->cxxName , typeInfo->canBeNullable ,
164
+ typeInfo->simd };
151
165
}
152
166
return std::nullopt;
153
167
}
168
+
169
+ std::optional<PrimitiveTypeMapping::ClangTypeInfo>
170
+ PrimitiveTypeMapping::getKnownSIMDTypeInfo (Type t, ASTContext &ctx) {
171
+ auto vecTy = t->getAs <BuiltinVectorType>();
172
+ if (!vecTy)
173
+ return std::nullopt;
174
+
175
+ auto elemTy = vecTy->getElementType ();
176
+ auto numElems = vecTy->getNumElements ();
177
+
178
+ if (mappedTypeNames.empty ())
179
+ initialize (ctx);
180
+
181
+ Identifier moduleName = ctx.Id_simd ;
182
+ std::string elemTyName = elemTy.getString ();
183
+ // While the element type starts with an upper case, vector types start with
184
+ // lower case.
185
+ elemTyName[0 ] = std::tolower (elemTyName[0 ]);
186
+ Identifier name = ctx.getIdentifier (elemTyName + std::to_string (numElems));
187
+ auto iter = mappedTypeNames.find ({moduleName, name});
188
+ if (iter == mappedTypeNames.end ())
189
+ return std::nullopt;
190
+ return ClangTypeInfo{*iter->second .cName , false , true };
191
+ }
0 commit comments