16
16
17
17
#include " swift/Basic/LLVM.h"
18
18
#include " swift/Basic/Lazy.h"
19
+ #include " swift/Basic/Demangle.h"
19
20
#include " swift/Runtime/Concurrent.h"
20
21
#include " swift/Runtime/HeapObject.h"
21
22
#include " swift/Runtime/Metadata.h"
@@ -189,27 +190,46 @@ _searchTypeMetadataRecords(const TypeMetadataState &T,
189
190
}
190
191
191
192
static const Metadata *
192
- _typeByMangledName (const llvm::StringRef typeName) {
193
+ _classByName (const llvm::StringRef typeName) {
194
+
195
+ size_t DotPos = typeName.find (' .' );
196
+ if (DotPos == llvm::StringRef::npos)
197
+ return nullptr ;
198
+ if (typeName.find (' .' , DotPos + 1 ) != llvm::StringRef::npos)
199
+ return nullptr ;
200
+
201
+ using namespace Demangle ;
202
+
203
+ NodePointer ClassNd = NodeFactory::create (Node::Kind::Class);
204
+ NodePointer ModuleNd = NodeFactory::create (Node::Kind::Module,
205
+ typeName.substr (0 , DotPos));
206
+ NodePointer NameNd = NodeFactory::create (Node::Kind::Module,
207
+ typeName.substr (DotPos + 1 ));
208
+ ClassNd->addChildren (ModuleNd, NameNd);
209
+
210
+ std::string Mangled = mangleNode (ClassNd);
211
+ StringRef MangledName = Mangled;
212
+
193
213
const Metadata *foundMetadata = nullptr ;
194
214
auto &T = TypeMetadataRecords.get ();
195
215
196
216
// Look for an existing entry.
197
217
// Find the bucket for the metadata entry.
198
- if (auto Value = T.Cache .find (typeName ))
218
+ if (auto Value = T.Cache .find (MangledName ))
199
219
return Value->getMetadata ();
200
220
201
221
// Check type metadata records
202
222
T.SectionsToScanLock .withLock ([&] {
203
- foundMetadata = _searchTypeMetadataRecords (T, typeName );
223
+ foundMetadata = _searchTypeMetadataRecords (T, MangledName );
204
224
});
205
225
206
226
// Check protocol conformances table. Note that this has no support for
207
227
// resolving generic types yet.
208
228
if (!foundMetadata)
209
- foundMetadata = _searchConformancesByMangledTypeName (typeName );
229
+ foundMetadata = _searchConformancesByMangledTypeName (MangledName );
210
230
211
231
if (foundMetadata) {
212
- T.Cache .getOrInsert (typeName , foundMetadata);
232
+ T.Cache .getOrInsert (MangledName , foundMetadata);
213
233
}
214
234
215
235
#if SWIFT_OBJC_INTEROP
@@ -226,13 +246,16 @@ _typeByMangledName(const llvm::StringRef typeName) {
226
246
return foundMetadata;
227
247
}
228
248
229
- // / Return the type metadata for a given mangled name, used in the
230
- // / implementation of _typeByName(). The human readable name returned
231
- // / by swift_getTypeName() is non-unique, so we used mangled names
232
- // / internally.
249
+ // / Return the type metadata for a given name, used in the
250
+ // / implementation of _typeByName().
251
+ // /
252
+ // / Currently only top-level classes are supported.
253
+
254
+ // / \param typeName The name of a class in the form: <module>.<class>
255
+ // / \return Returns the metadata of the type, if found.
233
256
SWIFT_RUNTIME_EXPORT
234
257
const Metadata *
235
- swift_getTypeByMangledName (const char *typeName, size_t typeNameLength) {
258
+ swift_getTypeByName (const char *typeName, size_t typeNameLength) {
236
259
llvm::StringRef name (typeName, typeNameLength);
237
- return _typeByMangledName (name);
260
+ return _classByName (name);
238
261
}
0 commit comments