Skip to content

Commit 1b0d347

Browse files
committed
Common-out protocol name and type name reading code into the 'QualifiedContextNameReader'.
1 parent beb8ca9 commit 1b0d347

File tree

2 files changed

+91
-99
lines changed

2 files changed

+91
-99
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 87 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,86 @@ class TypeRefBuilder {
12411241
return llvm::None;
12421242
}
12431243

1244+
llvm::Optional<std::string>
1245+
readFullyQualifiedTypeName(uintptr_t typeDescriptorTarget) {
1246+
std::string typeName;
1247+
auto contextTypeDescriptorBytes = OpaqueByteReader(
1248+
remote::RemoteAddress(typeDescriptorTarget),
1249+
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
1250+
if (!contextTypeDescriptorBytes.get()) {
1251+
Error = "Failed to read context descriptor.";
1252+
return llvm::None;
1253+
}
1254+
const ExternalContextDescriptor<ObjCInteropKind, PointerSize>
1255+
*contextDescriptor =
1256+
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
1257+
contextTypeDescriptorBytes.get();
1258+
1259+
auto typeDescriptor =
1260+
dyn_cast<ExternalTypeContextDescriptor<ObjCInteropKind, PointerSize>>(
1261+
contextDescriptor);
1262+
if (!typeDescriptor) {
1263+
Error = "Unexpected type of context descriptor.";
1264+
return llvm::None;
1265+
}
1266+
1267+
auto optionalTypeName = readTypeNameFromTypeDescriptor(
1268+
typeDescriptor, typeDescriptorTarget);
1269+
if (!optionalTypeName.hasValue())
1270+
return llvm::None;
1271+
else
1272+
typeName = optionalTypeName.getValue();
1273+
1274+
std::vector<ContextNameInfo> contextNameChain;
1275+
contextNameChain.push_back(
1276+
ContextNameInfo{typeName, typeDescriptorTarget, false});
1277+
getParentContextChain(typeDescriptorTarget, contextDescriptor,
1278+
contextNameChain);
1279+
return constructFullyQualifiedNameFromContextChain(contextNameChain);
1280+
}
1281+
1282+
llvm::Optional<std::string>
1283+
readFullyQualifiedProtocolName(
1284+
uintptr_t protocolDescriptorTarget) {
1285+
llvm::Optional<std::string> protocolName;
1286+
// Set low bit indicates that this is an indirect
1287+
// reference
1288+
if (protocolDescriptorTarget & 0x1) {
1289+
auto adjustedProtocolDescriptorTarget = protocolDescriptorTarget & ~0x1;
1290+
if (auto symbol = OpaquePointerReader(
1291+
remote::RemoteAddress(adjustedProtocolDescriptorTarget),
1292+
PointerSize)) {
1293+
if (!symbol->getSymbol().empty()) {
1294+
Demangle::Context Ctx;
1295+
auto demangledRoot =
1296+
Ctx.demangleSymbolAsNode(symbol->getSymbol().str());
1297+
assert(demangledRoot->getKind() == Node::Kind::Global);
1298+
assert(demangledRoot->getChild(0)->getKind() ==
1299+
Node::Kind::ProtocolDescriptor);
1300+
protocolName =
1301+
nodeToString(demangledRoot->getChild(0)->getChild(0));
1302+
} else {
1303+
// This is an absolute address of a protocol descriptor
1304+
auto protocolDescriptorAddress = (uintptr_t)symbol->getOffset();
1305+
protocolName =
1306+
readFullyQualifiedProtocolNameFromProtocolDescriptor(
1307+
protocolDescriptorAddress);
1308+
}
1309+
} else {
1310+
Error = "Error reading external protocol address.";
1311+
return llvm::None;
1312+
}
1313+
} else {
1314+
// If this is a direct reference, get symbol name from the protocol
1315+
// descriptor.
1316+
protocolName =
1317+
readFullyQualifiedProtocolNameFromProtocolDescriptor(
1318+
protocolDescriptorTarget);
1319+
}
1320+
return protocolName;
1321+
}
1322+
1323+
private:
12441324
llvm::Optional<std::string>
12451325
readFullyQualifiedProtocolNameFromProtocolDescriptor(
12461326
uintptr_t protocolDescriptorAddress) {
@@ -1510,7 +1590,7 @@ class TypeRefBuilder {
15101590
OpaqueByteReader, OpaqueStringReader, OpaquePointerReader,
15111591
OpaqueDynamicSymbolResolver);
15121592
auto conformanceRequirementProtocolName =
1513-
nameReader.readFullyQualifiedProtocolNameFromProtocolDescriptor(
1593+
nameReader.readFullyQualifiedProtocolName(
15141594
protocolDescriptorAddress);
15151595
result.push_back(*conformanceRequirementProtocolName);
15161596
}
@@ -1608,41 +1688,12 @@ class TypeRefBuilder {
16081688
}
16091689
}
16101690

1611-
auto contextTypeDescriptorBytes = OpaqueByteReader(
1612-
remote::RemoteAddress(contextTypeDescriptorAddress),
1613-
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
1614-
if (!contextTypeDescriptorBytes.get()) {
1615-
Error = "Failed to read context descriptor.";
1616-
return llvm::None;
1617-
}
1618-
const ExternalContextDescriptor<ObjCInteropKind, PointerSize>
1619-
*contextDescriptor =
1620-
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
1621-
contextTypeDescriptorBytes.get();
1622-
1623-
auto typeDescriptor =
1624-
dyn_cast<ExternalTypeContextDescriptor<ObjCInteropKind, PointerSize>>(
1625-
contextDescriptor);
1626-
if (!typeDescriptor) {
1627-
Error = "Unexpected type of context descriptor.";
1628-
return llvm::None;
1629-
}
1630-
1631-
auto optionalTypeName = NameReader.readTypeNameFromTypeDescriptor(
1632-
typeDescriptor, contextTypeDescriptorAddress);
1633-
if (!optionalTypeName.hasValue())
1691+
auto fullyQualifiedName =
1692+
NameReader.readFullyQualifiedTypeName(contextTypeDescriptorAddress);
1693+
if (!fullyQualifiedName.hasValue())
16341694
return llvm::None;
16351695
else
1636-
typeName = optionalTypeName.getValue();
1637-
1638-
std::vector<ContextNameInfo> contextNameChain;
1639-
contextNameChain.push_back(
1640-
ContextNameInfo{typeName, contextTypeDescriptorAddress, false});
1641-
NameReader.getParentContextChain(contextTypeDescriptorAddress, contextDescriptor,
1642-
contextNameChain);
1643-
std::string fullyQualifiedName =
1644-
NameReader.constructFullyQualifiedNameFromContextChain(contextNameChain);
1645-
return std::make_pair(mangledTypeName, fullyQualifiedName);
1696+
return std::make_pair(mangledTypeName, *fullyQualifiedName);
16461697
}
16471698

16481699
/// Extract protocol name from a Conformance Descriptor
@@ -1670,67 +1721,8 @@ class TypeRefBuilder {
16701721
(const char *)protocolDescriptorFieldAddress,
16711722
(int32_t)*protocolDescriptorOffset);
16721723

1673-
auto constructFullyQualifiedProtocolName =
1674-
[&](uintptr_t protocolDescriptorAddress)
1675-
-> llvm::Optional<std::string> {
1676-
auto protocolName =
1677-
NameReader.readProtocolNameFromProtocolDescriptor(protocolDescriptorAddress);
1678-
1679-
// Read the protocol conformance descriptor itself
1680-
auto protocolContextDescriptorBytes = OpaqueByteReader(
1681-
remote::RemoteAddress(protocolDescriptorAddress),
1682-
sizeof(ExternalContextDescriptor<ObjCInteropKind, PointerSize>));
1683-
if (!protocolContextDescriptorBytes.get()) {
1684-
Error = "Failed to read context (protocol) descriptor.";
1685-
return llvm::None;
1686-
}
1687-
const ExternalContextDescriptor<ObjCInteropKind,
1688-
PointerSize> *protocolDescriptor =
1689-
(const ExternalContextDescriptor<ObjCInteropKind, PointerSize> *)
1690-
protocolContextDescriptorBytes.get();
1691-
1692-
std::vector<ContextNameInfo> contextNameChain;
1693-
contextNameChain.push_back(ContextNameInfo{
1694-
protocolName.getValue(), protocolDescriptorAddress, false});
1695-
NameReader.getParentContextChain(protocolDescriptorAddress, protocolDescriptor,
1696-
contextNameChain);
1697-
return NameReader.constructFullyQualifiedNameFromContextChain(contextNameChain);
1698-
};
1699-
1700-
// Set low bit indicates that this is an indirect
1701-
// reference
1702-
if (protocolDescriptorTarget & 0x1) {
1703-
auto adjustedProtocolDescriptorTarget = protocolDescriptorTarget & ~0x1;
1704-
if (auto symbol = OpaquePointerReader(
1705-
remote::RemoteAddress(adjustedProtocolDescriptorTarget),
1706-
PointerSize)) {
1707-
if (!symbol->getSymbol().empty()) {
1708-
Demangle::Context Ctx;
1709-
auto demangledRoot =
1710-
Ctx.demangleSymbolAsNode(symbol->getSymbol().str());
1711-
assert(demangledRoot->getKind() == Node::Kind::Global);
1712-
assert(demangledRoot->getChild(0)->getKind() ==
1713-
Node::Kind::ProtocolDescriptor);
1714-
protocolName =
1715-
nodeToString(demangledRoot->getChild(0)->getChild(0));
1716-
} else {
1717-
// This is an absolute address of a protocol descriptor
1718-
auto protocolDescriptorAddress = (uintptr_t)symbol->getOffset();
1719-
protocolName =
1720-
constructFullyQualifiedProtocolName(protocolDescriptorAddress);
1721-
}
1722-
} else {
1723-
Error = "Error reading external protocol address.";
1724-
return llvm::None;
1725-
}
1726-
} else {
1727-
// If this is a direct reference, get symbol name from the protocol
1728-
// descriptor.
1729-
protocolName =
1730-
constructFullyQualifiedProtocolName(protocolDescriptorTarget);
1731-
}
1732-
1733-
return protocolName;
1724+
return NameReader.readFullyQualifiedProtocolName(
1725+
protocolDescriptorTarget);
17341726
}
17351727

17361728
/// Given the address of a conformance descriptor, attempt to read it.

test/Reflection/opaque_associated_type_requirements.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// RUN: %target-build-swift %s -parse-as-library -target %target-cpu-apple-macosx10.15 -I %t/includes -emit-module -emit-library -module-name AssociatedTypeRequirements -o %t/AssociatedTypeRequirements %t/includes/testModB.o
99

10-
// RUN: %target-swift-reflection-dump -binary-filename %t/AssociatedTypeRequirements -binary-filename %platform-module-dir/%target-library-name(swiftCore) | %FileCheck %s
10+
// RUN: %target-swift-reflection-dump -binary-filename %t/AssociatedTypeRequirements | %FileCheck %s
1111

1212
// CHECK: ASSOCIATED TYPES:
1313
// CHECK-NEXT: =============
@@ -17,9 +17,9 @@
1717
// CHECK-NEXT: (struct AssociatedTypeRequirements.Bar)
1818

1919
// CHECK: opaque type conformance requirements:
20-
// CHECK-DAG: AssociatedTypeRequirements.protoA
21-
// CHECK-DAG: AssociatedTypeRequirements.protoB
22-
// CHECK-DAG: testModB.testModBProtocol
20+
// CHECK-NEXT: AssociatedTypeRequirements.protoA
21+
// CHECK-NEXT: AssociatedTypeRequirements.protoB
22+
// CHECK-NEXT: testModB.testModBProtocol
2323

2424
import testModB
2525

0 commit comments

Comments
 (0)