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
+ for (auto result : results) {
34
+ if (auto nominal = dyn_cast<NominalTypeDecl>(result))
35
+ return nominal;
36
+
37
+ if (auto typealias = dyn_cast<TypeAliasDecl>(result))
38
+ return typealias;
39
+ }
40
+
41
+ return nullptr ;
42
+ }
43
+
22
44
void PrimitiveTypeMapping::initialize (ASTContext &ctx) {
23
45
assert (mappedTypeNames.empty () && " expected empty type map" );
46
+
47
+ auto addMappedType = [&](Identifier moduleName, Identifier typeName,
48
+ FullClangTypeInfo info) {
49
+ auto decl = findTypeInModuleByName (ctx, moduleName, typeName);
50
+ if (!decl)
51
+ return ;
52
+
53
+ // Always map a direct definition match. Either the nominal decl or the
54
+ // typealias itself.
55
+ mappedTypeNames[decl] = info;
56
+
57
+ // If the underlying type of a typealias doesn't have a type, set it here.
58
+ // This aims to reproduce the typealias behavior from BuiltinMappedTypes.
59
+ if (auto typealias = dyn_cast<TypeAliasDecl>(decl)) {
60
+ auto underlying = typealias->getDeclaredInterfaceType ()->getAnyNominal ();
61
+ if (underlying && !mappedTypeNames.contains (underlying))
62
+ mappedTypeNames[underlying] = info;
63
+ }
64
+ };
65
+
66
+ // Map stdlib types.
24
67
#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}
68
+ addMappedType (ctx.StdlibModuleName , \
69
+ ctx.getIdentifier (#SWIFT_NAME), \
70
+ {CLANG_REPR, std::optional<StringRef>(CLANG_REPR), \
71
+ std::optional<StringRef>(CLANG_REPR), NEEDS_NULLABILITY})
28
72
#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}
73
+ addMappedType (ctx.StdlibModuleName , \
74
+ ctx.getIdentifier (#SWIFT_NAME), \
75
+ {OBJC_REPR, std::optional<StringRef>(C_REPR), \
76
+ std::optional<StringRef>(C_REPR), NEEDS_NULLABILITY})
32
77
#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}
78
+ addMappedType (ctx.StdlibModuleName , \
79
+ ctx.getIdentifier (#SWIFT_NAME), \
80
+ {OBJC_REPR, std::optional<StringRef>(C_REPR), \
81
+ std::optional<StringRef>(CXX_REPR), NEEDS_NULLABILITY})
36
82
37
83
MAP (CBool, " bool" , false );
38
84
@@ -79,35 +125,37 @@ void PrimitiveTypeMapping::initialize(ASTContext &ctx) {
79
125
MAP (UnsafeRawPointer, " void const *" , true );
80
126
MAP (UnsafeMutableRawPointer, " void *" , true );
81
127
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 };
128
+ // Map other module types.
129
+
130
+ addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" ObjCBool" ),
131
+ {" BOOL" , std::nullopt, std::nullopt, false });
132
+ addMappedType (ctx.Id_ObjectiveC , ctx.getIdentifier (" Selector" ),
133
+ {" SEL" , std::nullopt, std::nullopt, true });
134
+ addMappedType (ctx.Id_ObjectiveC ,
135
+ ctx.getIdentifier (swift::getSwiftName (
136
+ KnownFoundationEntity::NSZone)),
137
+ {" struct _NSZone *" , std::nullopt, std::nullopt, true });
90
138
91
- mappedTypeNames[{ ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" )}] = {
92
- " Boolean" , std::nullopt, std::nullopt, false };
139
+ addMappedType ( ctx.Id_Darwin , ctx.getIdentifier (" DarwinBoolean" ),
140
+ { " Boolean" , std::nullopt, std::nullopt, false }) ;
93
141
94
- mappedTypeNames[{ ctx.Id_CoreGraphics , ctx.Id_CGFloat }] = {
95
- " CGFloat" , std::nullopt, std::nullopt, false };
142
+ addMappedType ( ctx.Id_CoreGraphics , ctx.Id_CGFloat ,
143
+ { " CGFloat" , std::nullopt, std::nullopt, false }) ;
96
144
97
- mappedTypeNames[{ ctx.Id_CoreFoundation , ctx.Id_CGFloat }] = {
98
- " CGFloat" , std::nullopt, std::nullopt, false };
145
+ addMappedType ( ctx.Id_CoreFoundation , ctx.Id_CGFloat ,
146
+ { " CGFloat" , std::nullopt, std::nullopt, false }) ;
99
147
100
148
// Use typedefs we set up for SIMD vector types.
101
149
#define MAP_SIMD_TYPE (BASENAME, _, __ ) \
102
150
StringRef simd2##BASENAME = " swift_" #BASENAME " 2" ; \
103
- mappedTypeNames[{ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" )}] = { \
104
- simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }; \
151
+ addMappedType ( ctx.Id_simd , ctx.getIdentifier (#BASENAME " 2" ), \
152
+ { simd2##BASENAME, simd2##BASENAME, simd2##BASENAME, false }); \
105
153
StringRef simd3##BASENAME = " swift_" #BASENAME " 3" ; \
106
- mappedTypeNames[{ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" )}] = { \
107
- simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }; \
154
+ addMappedType ( ctx.Id_simd , ctx.getIdentifier (#BASENAME " 3" ), \
155
+ { simd3##BASENAME, simd3##BASENAME, simd3##BASENAME, false }); \
108
156
StringRef simd4##BASENAME = " swift_" #BASENAME " 4" ; \
109
- mappedTypeNames[{ ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" )}] = { \
110
- simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false };
157
+ addMappedType ( ctx.Id_simd , ctx.getIdentifier (#BASENAME " 4" ), \
158
+ { simd4##BASENAME, simd4##BASENAME, simd4##BASENAME, false }) ;
111
159
#include " swift/ClangImporter/SIMDMappedTypes.def"
112
160
static_assert (SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4 ,
113
161
" must add or remove special name mappings if max number of "
@@ -119,9 +167,11 @@ PrimitiveTypeMapping::getMappedTypeInfoOrNull(const TypeDecl *typeDecl) {
119
167
if (mappedTypeNames.empty ())
120
168
initialize (typeDecl->getASTContext ());
121
169
122
- Identifier moduleName = typeDecl->getModuleContext ()->getName ();
123
- Identifier name = typeDecl->getName ();
124
- auto iter = mappedTypeNames.find ({moduleName, name});
170
+ auto nominal = dyn_cast<TypeDecl>(typeDecl);
171
+ if (!nominal)
172
+ return nullptr ;
173
+
174
+ auto iter = mappedTypeNames.find (nominal);
125
175
if (iter == mappedTypeNames.end ())
126
176
return nullptr ;
127
177
return &iter->second ;
0 commit comments