Skip to content

Commit 7ca1392

Browse files
mikeashairspeedswift
authored andcommitted
[Reflection] Clean up the ReflectionContext additions.
Move the internal runtime structures into RuntimeInternals.h. Remove leftover methods. Comment the new methods. rdar://problem/55481578
1 parent 3a498ca commit 7ca1392

File tree

3 files changed

+69
-32
lines changed

3 files changed

+69
-32
lines changed

include/swift/Reflection/ReflectionContext.h

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/Remote/MemoryReader.h"
2929
#include "swift/Remote/MetadataReader.h"
3030
#include "swift/Reflection/Records.h"
31+
#include "swift/Reflection/RuntimeInternals.h"
3132
#include "swift/Reflection/TypeLowering.h"
3233
#include "swift/Reflection/TypeRef.h"
3334
#include "swift/Reflection/TypeRefBuilder.h"
@@ -772,16 +773,6 @@ class ReflectionContext
772773
}
773774
}
774775

775-
776-
777-
struct ConformanceNode {
778-
StoredPointer Left, Right;
779-
StoredPointer Type;
780-
StoredPointer Proto;
781-
StoredPointer Description;
782-
StoredSize FailureGeneration;
783-
};
784-
785776
StoredSignedPointer getTypeContextDescriptor(const TargetMetadata<Runtime> *Metadata) const {
786777
switch (Metadata->getKind()) {
787778
case MetadataKind::Class: {
@@ -803,19 +794,25 @@ class ReflectionContext
803794
}
804795
}
805796

797+
/// Iterate the protocol conformance cache tree rooted at NodePtr, calling
798+
/// Call with the type and protocol in each node.
806799
void iterateConformanceTree(StoredPointer NodePtr,
807800
std::function<void(StoredPointer Type, StoredPointer Proto)> Call) {
808801
if (!NodePtr)
809802
return;
810803
auto NodeBytes = getReader().readBytes(RemoteAddress(NodePtr), sizeof(Node));
811-
auto NodeData = reinterpret_cast<const ConformanceNode *>(NodeBytes.get());
804+
auto NodeData =
805+
reinterpret_cast<const ConformanceNode<Runtime> *>(NodeBytes.get());
812806
if (!NodeData)
813807
return;
814808
Call(NodeData->Type, NodeData->Proto);
815809
iterateConformanceTree(NodeData->Left, Call);
816810
iterateConformanceTree(NodeData->Right, Call);
817811
}
818812

813+
/// Iterate the protocol conformance cache in the target process, calling Call
814+
/// with the type and protocol of each conformance. Returns None on success,
815+
/// and a string describing the error on failure.
819816
llvm::Optional<std::string> iterateConformances(
820817
std::function<void(StoredPointer Type, StoredPointer Proto)> Call) {
821818
std::string ConformancesPointerName =
@@ -836,20 +833,10 @@ class ReflectionContext
836833
return llvm::None;
837834
}
838835

839-
void iterateModules(Demangle::NodePointer Ptr, std::function<void(llvm::StringRef)> Call) {
840-
if (Ptr->getKind() == Node::Kind::Module)
841-
Call(Ptr->getText());
842-
for (auto Child : *Ptr)
843-
iterateModules(Child, Call);
844-
}
845-
846-
struct MetadataAllocation {
847-
uint16_t Tag;
848-
StoredPointer Ptr;
849-
unsigned Size;
850-
};
851-
852-
StoredPointer allocationMetadataPointer(MetadataAllocation Allocation) {
836+
/// Fetch the metadata pointer from a metadata allocation, or 0 if this
837+
/// allocation's tag is not handled or an error occurred.
838+
StoredPointer allocationMetadataPointer(
839+
MetadataAllocation<Runtime> Allocation) {
853840
if (Allocation.Tag == GenericMetadataCacheTag) {
854841
struct GenericMetadataCacheEntry {
855842
StoredPointer Left, Right;
@@ -872,9 +859,12 @@ class ReflectionContext
872859
}
873860
return 0;
874861
}
875-
876-
llvm::Optional<std::string>
877-
iterateMetadataAllocations(std::function<void (MetadataAllocation)> Call) {
862+
863+
/// Iterate the metadata allocations in the target process, calling Call with
864+
/// each allocation found. Returns None on success, and a string describing
865+
/// the error on failure.
866+
llvm::Optional<std::string> iterateMetadataAllocations(
867+
std::function<void (MetadataAllocation<Runtime>)> Call) {
878868
std::string IterationEnabledName =
879869
"__swift_debug_metadataAllocationIterationEnabled";
880870
std::string AllocationPoolPointerName =
@@ -940,7 +930,7 @@ class ReflectionContext
940930
if (Header->Size == 0)
941931
break;
942932
auto RemoteAddr = PoolStart + Offset + sizeof(AllocationHeader);
943-
MetadataAllocation Allocation;
933+
MetadataAllocation<Runtime> Allocation;
944934
Allocation.Tag = Header->Tag;
945935
Allocation.Ptr = RemoteAddr;
946936
Allocation.Size = Header->Size;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===--- RuntimeInternals.h - Runtime Internal Structures -------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Runtime data structures that Reflection inspects externally.
14+
//
15+
// FIXME: Ideally the original definitions would be templatized on a Runtime
16+
// parameter and we could use the original definitions in both the runtime and
17+
// in Reflection.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#ifndef SWIFT_REFLECTION_RUNTIME_INTERNALS_H
22+
#define SWIFT_REFLECTION_RUNTIME_INTERNALS_H
23+
24+
namespace swift {
25+
26+
namespace reflection {
27+
28+
template <typename Runtime>
29+
struct ConformanceNode {
30+
typename Runtime::StoredPointer Left, Right;
31+
typename Runtime::StoredPointer Type;
32+
typename Runtime::StoredPointer Proto;
33+
typename Runtime::StoredPointer Description;
34+
typename Runtime::StoredSize FailureGeneration;
35+
};
36+
37+
template <typename Runtime>
38+
struct MetadataAllocation {
39+
uint16_t Tag;
40+
typename Runtime::StoredPointer Ptr;
41+
unsigned Size;
42+
};
43+
44+
} // end namespace reflection
45+
} // end namespace swift
46+
47+
#endif // SWIFT_REFLECTION_RUNTIME_INTERNALS_H

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ using namespace swift;
3333
using namespace swift::reflection;
3434
using namespace swift::remote;
3535

36-
using NativeReflectionContext = swift::reflection::ReflectionContext<
37-
External<RuntimeTarget<sizeof(uintptr_t)>>>;
36+
using Runtime = External<RuntimeTarget<sizeof(uintptr_t)>>;
37+
using NativeReflectionContext = swift::reflection::ReflectionContext<Runtime>;
3838

3939
struct SwiftReflectionContext {
4040
NativeReflectionContext *nativeContext;
@@ -629,7 +629,7 @@ swift_reflection_ptr_t swift_reflection_allocationMetadataPointer(
629629
SwiftReflectionContextRef ContextRef,
630630
swift_metadata_allocation_t Allocation) {
631631
auto Context = ContextRef->nativeContext;
632-
NativeReflectionContext::MetadataAllocation NativeAllocation;
632+
MetadataAllocation<Runtime> NativeAllocation;
633633
NativeAllocation.Tag = Allocation.Tag;
634634
NativeAllocation.Ptr = Allocation.Ptr;
635635
NativeAllocation.Size = Allocation.Size;

0 commit comments

Comments
 (0)