Skip to content

Commit 52fa6a1

Browse files
Merge pull request #82389 from adrian-prantl/153934495
Implement reflection support for Symbolic Extended Existential types.
2 parents 2ae485a + eabcf41 commit 52fa6a1

File tree

11 files changed

+403
-133
lines changed

11 files changed

+403
-133
lines changed

include/swift/Demangling/Demangle.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ class Node {
193193
};
194194

195195
using IndexType = uint64_t;
196+
using RemoteAddressType = std::pair<uint64_t, uint8_t>;
196197

197198
friend class NodeFactory;
198199

@@ -209,14 +210,20 @@ class Node {
209210
IndexType Index;
210211
NodePointer InlineChildren[2];
211212
NodeVector Children;
213+
RemoteAddressType RemoteAddress;
212214
};
213215

214216

215217
Kind NodeKind;
216218

217219
enum class PayloadKind : uint8_t {
218-
None = 0, OneChild = 1, TwoChildren = 2,
219-
Text, Index, ManyChildren
220+
None = 0,
221+
OneChild = 1,
222+
TwoChildren = 2,
223+
Text,
224+
Index,
225+
ManyChildren,
226+
RemoteAddress
220227
};
221228
PayloadKind NodePayloadKind;
222229

@@ -231,6 +238,10 @@ class Node {
231238
: NodeKind(k), NodePayloadKind(PayloadKind::Index) {
232239
Index = index;
233240
}
241+
Node(Kind k, uint64_t remoteAddress, uint8_t addressSpace)
242+
: NodeKind(k), NodePayloadKind(PayloadKind::RemoteAddress) {
243+
RemoteAddress = {remoteAddress, addressSpace};
244+
}
234245
Node(const Node &) = delete;
235246
Node &operator=(const Node &) = delete;
236247

@@ -284,6 +295,14 @@ class Node {
284295
return Index;
285296
}
286297

298+
bool hasRemoteAddress() const {
299+
return NodePayloadKind == PayloadKind::RemoteAddress;
300+
}
301+
std::pair<uint64_t, uint8_t> getRemoteAddress() const {
302+
assert(hasRemoteAddress());
303+
return RemoteAddress;
304+
}
305+
287306
using iterator = const NodePointer *;
288307

289308
size_t getNumChildren() const {

include/swift/Demangling/Demangler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ class NodeFactory {
249249
/// Creates a node of kind \p K with an \p Index payload.
250250
NodePointer createNode(Node::Kind K, Node::IndexType Index);
251251

252+
/// Creates a node of kind \p K with a \p RemoteAddress payload.
253+
///
254+
/// These nodes are created and consumed by the reflection library.
255+
NodePointer createNode(Node::Kind K, uint64_t RemoteAddress,
256+
uint8_t AddressSpace);
257+
252258
/// Creates a node of kind \p K with a \p Text payload.
253259
///
254260
/// The \p Text string must be already allocated with the Factory and therefore

include/swift/Demangling/TypeDecoder.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,43 @@ void decodeRequirement(
573573
}
574574
}
575575

576+
/// Extract the protocol and requirement nodes from a shape symbol.
577+
static inline std::pair<NodePointer, NodePointer>
578+
decodeShape(NodePointer Node) {
579+
if (!Node || Node->getKind() != Node::Kind::Global ||
580+
Node->getNumChildren() != 1)
581+
return {nullptr, nullptr};
582+
Node = Node->getChild(0);
583+
if (Node && (Node->getKind() == Node::Kind::Uniquable) &&
584+
Node->getNumChildren() == 1)
585+
Node = Node->getChild(0);
586+
if (!Node || Node->getKind() != Node::Kind::ExtendedExistentialTypeShape ||
587+
Node->getNumChildren() != 2)
588+
return {nullptr, nullptr};
589+
Node = Node->getChild(1);
590+
if (!Node || Node->getKind() != Node::Kind::Type ||
591+
Node->getNumChildren() != 1)
592+
return {nullptr, nullptr};
593+
Node = Node->getChild(0);
594+
if (!Node || Node->getKind() != Node::Kind::ConstrainedExistential ||
595+
Node->getNumChildren() != 2)
596+
return {nullptr, nullptr};
597+
NodePointer Requirements = Node->getChild(1);
598+
if (!Requirements || Requirements->getKind() !=
599+
Node::Kind::ConstrainedExistentialRequirementList)
600+
return {nullptr, nullptr};
601+
602+
Node = Node->getChild(0);
603+
if (!Node || Node->getKind() != Node::Kind::Type ||
604+
Node->getNumChildren() != 1)
605+
return {nullptr, nullptr};
606+
NodePointer Protocol = Node;
607+
if (!Protocol)
608+
return {nullptr, nullptr};
609+
610+
return {Protocol, Requirements};
611+
}
612+
576613
#define MAKE_NODE_TYPE_ERROR(Node, Fmt, ...) \
577614
TYPE_LOOKUP_ERROR_FMT("TypeDecoder.h:%u: Node kind %u \"%.*s\" - " Fmt, \
578615
__LINE__, (unsigned)Node->getKind(), \

0 commit comments

Comments
 (0)