Skip to content

Commit 194c84e

Browse files
committed
[MetadataLookup] Properly setup Demangler symbolic ref resolver
1 parent 3323ae3 commit 194c84e

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ using namespace reflection;
4646
#include <objc/objc.h>
4747
#endif
4848

49+
/// Produce a Demangler value suitable for resolving runtime type metadata
50+
/// strings.
51+
static Demangler getDemanglerForRuntimeTypeResolution() {
52+
Demangler dem;
53+
// Resolve symbolic references to type contexts into the absolute address of
54+
// the type context descriptor, so that if we see a symbolic reference in the
55+
// mangled name we can immediately find the associated metadata.
56+
dem.setSymbolicReferenceResolver([&](int32_t offset,
57+
const void *base) -> NodePointer {
58+
auto absolute_addr = (uintptr_t)detail::applyRelativeOffset(base, offset);
59+
return dem.createNode(Node::Kind::SymbolicReference, absolute_addr);
60+
});
61+
return dem;
62+
}
63+
4964
template <typename T> struct DescriptorCacheEntry {
5065
private:
5166
std::string Name;
@@ -162,6 +177,14 @@ swift::_contextDescriptorMatchesMangling(const ContextDescriptor *context,
162177
node = node->getChild(0);
163178

164179
while (context) {
180+
// We can directly match symbolic references to the current context.
181+
if (node && node->getKind() == Demangle::Node::Kind::SymbolicReference) {
182+
if (equalContexts(context, reinterpret_cast<const ContextDescriptor *>(
183+
node->getIndex()))) {
184+
return true;
185+
}
186+
}
187+
165188
switch (context->getKind()) {
166189
case ContextDescriptorKind::Module: {
167190
auto module = cast<ModuleContextDescriptor>(context);
@@ -935,7 +958,7 @@ class DecodedMetadataBuilder {
935958
TypeInfo
936959
swift::_getTypeByMangledName(StringRef typeName,
937960
SubstGenericParameterFn substGenericParam) {
938-
Demangler demangler;
961+
auto demangler = getDemanglerForRuntimeTypeResolution();
939962
NodePointer node;
940963

941964
// Check whether this is the convenience syntax "ModuleName.ClassName".
@@ -950,16 +973,6 @@ swift::_getTypeByMangledName(StringRef typeName,
950973
return dotPos;
951974
};
952975

953-
// Resolve symbolic references to type contexts into the absolute address of
954-
// the type context descriptor, so that if we see a symbolic reference in the
955-
// mangled name we can immediately find the associated metadata.
956-
demangler.setSymbolicReferenceResolver(
957-
[&](int32_t offset, const void *base) -> NodePointer {
958-
auto absolute_addr = (uintptr_t)detail::applyRelativeOffset(base, offset);
959-
return demangler.createNode(Node::Kind::SymbolicReference,
960-
absolute_addr);
961-
});
962-
963976
auto dotPos = getDotPosForConvenienceSyntax();
964977
if (dotPos != llvm::StringRef::npos) {
965978
// Form a demangle tree for this class.
@@ -1081,7 +1094,7 @@ void swift::swift_getFieldAt(
10811094

10821095
};
10831096

1084-
Demangler dem;
1097+
auto dem = getDemanglerForRuntimeTypeResolution();
10851098
auto &cache = FieldCache.get();
10861099
auto isRequestedDescriptor = [&](const FieldDescriptor &descriptor) {
10871100
assert(descriptor.hasMangledTypeName());

0 commit comments

Comments
 (0)