Skip to content

Commit efca93e

Browse files
committed
Reflection: Add dumping of capture descriptors to swift-reflection-dump
The tests show that there's some round-tripping issue; I will investigate this next.
1 parent 71d2869 commit efca93e

File tree

6 files changed

+150
-2
lines changed

6 files changed

+150
-2
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define SWIFT_REFLECTION_TYPEREFBUILDER_H
2020

2121
#include "swift/Remote/MetadataReader.h"
22+
#include "swift/Reflection/MetadataSourceBuilder.h"
2223
#include "swift/Reflection/Records.h"
2324
#include "swift/Reflection/TypeLowering.h"
2425
#include "swift/Reflection/TypeRef.h"
@@ -80,6 +81,14 @@ struct ReflectionInfo {
8081
GenericSection reflstr;
8182
};
8283

84+
struct ClosureContextInfo {
85+
std::vector<const TypeRef *> CaptureTypes;
86+
std::vector<std::pair<const TypeRef *, const MetadataSource *>> MetadataSources;
87+
unsigned NumBindings = 0;
88+
89+
void dump(std::ostream &OS);
90+
};
91+
8392
/// An implementation of MetadataReader's BuilderType concept for
8493
/// building TypeRefs, and parsing field metadata from any images
8594
/// it has been made aware of.
@@ -105,6 +114,7 @@ class TypeRefBuilder {
105114
std::vector<std::unique_ptr<const TypeRef>> TypeRefPool;
106115

107116
TypeConverter TC;
117+
MetadataSourceBuilder MSB;
108118

109119
#define TYPEREF(Id, Parent) \
110120
std::unordered_map<TypeRefID, const Id##TypeRef *, \
@@ -275,6 +285,9 @@ class TypeRefBuilder {
275285
/// Get the primitive type lowering for a builtin type.
276286
const BuiltinTypeDescriptor *getBuiltinTypeInfo(const TypeRef *TR);
277287

288+
/// Get the unsubstituted capture types for a closure context.
289+
ClosureContextInfo getClosureContextInfo(const CaptureDescriptor &CD);
290+
278291
///
279292
/// Dumping typerefs, field declarations, associated types
280293
///
@@ -284,6 +297,7 @@ class TypeRefBuilder {
284297
void dumpFieldSection(std::ostream &OS);
285298
void dumpAssociatedTypeSection(std::ostream &OS);
286299
void dumpBuiltinTypeSection(std::ostream &OS);
300+
void dumpCaptureSection(std::ostream &OS);
287301
void dumpAllSections(std::ostream &OS);
288302
};
289303

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,45 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
152152
return nullptr;
153153
}
154154

155+
/// Get the unsubstituted capture types for a closure context.
156+
ClosureContextInfo
157+
TypeRefBuilder::getClosureContextInfo(const CaptureDescriptor &CD) {
158+
ClosureContextInfo Info;
159+
160+
for (auto i = CD.capture_begin(), e = CD.capture_end(); i != e; ++i) {
161+
const TypeRef *TR = nullptr;
162+
if (i->hasMangledTypeName()) {
163+
auto MangledName = i->getMangledTypeName();
164+
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
165+
TR = swift::remote::decodeMangledType(*this, DemangleTree);
166+
}
167+
Info.CaptureTypes.push_back(TR);
168+
}
169+
170+
for (auto i = CD.source_begin(), e = CD.source_end(); i != e; ++i) {
171+
const TypeRef *TR = nullptr;
172+
if (i->hasMangledTypeName()) {
173+
auto MangledName = i->getMangledTypeName();
174+
auto DemangleTree = Demangle::demangleTypeAsNode(MangledName);
175+
TR = swift::remote::decodeMangledType(*this, DemangleTree);
176+
}
177+
178+
const MetadataSource *MS = nullptr;
179+
if (i->hasMangledMetadataSource()) {
180+
auto MangledMetadataSource = i->getMangledMetadataSource();
181+
MS = MetadataSource::decode(MSB, MangledMetadataSource);
182+
}
183+
184+
Info.MetadataSources.push_back({TR, MS});
185+
}
186+
187+
Info.NumBindings = CD.NumBindings;
188+
189+
return Info;
190+
}
191+
155192
///
156-
/// Dumping typerefs, field declarations, associated types
193+
/// Dumping reflection metadata
157194
///
158195

159196
void
@@ -228,6 +265,36 @@ void TypeRefBuilder::dumpBuiltinTypeSection(std::ostream &OS) {
228265
}
229266
}
230267

268+
void ClosureContextInfo::dump(std::ostream &OS) {
269+
OS << "- Capture types:\n";
270+
for (auto *TR : CaptureTypes) {
271+
if (TR == nullptr)
272+
OS << "!!! Invalid typeref\n";
273+
else
274+
TR->dump(OS);
275+
}
276+
OS << "- Metadata sources:\n";
277+
for (auto MS : MetadataSources) {
278+
if (MS.first == nullptr)
279+
OS << "!!! Invalid typeref\n";
280+
else
281+
MS.first->dump(OS);
282+
if (MS.second == nullptr)
283+
OS << "!!! Invalid matadata source\n";
284+
else
285+
MS.second->dump(OS);
286+
}
287+
}
288+
289+
void TypeRefBuilder::dumpCaptureSection(std::ostream &OS) {
290+
for (const auto &sections : ReflectionInfos) {
291+
for (const auto &descriptor : sections.capture) {
292+
auto info = getClosureContextInfo(descriptor);
293+
info.dump(OS);
294+
}
295+
}
296+
}
297+
231298
void TypeRefBuilder::dumpAllSections(std::ostream &OS) {
232299
OS << "FIELDS:\n";
233300
OS << "=======\n";
@@ -241,4 +308,8 @@ void TypeRefBuilder::dumpAllSections(std::ostream &OS) {
241308
OS << "==============\n";
242309
dumpBuiltinTypeSection(OS);
243310
OS << '\n';
311+
OS << "CAPTURE DESCRIPTORS:\n";
312+
OS << "====================\n";
313+
dumpCaptureSection(OS);
314+
OS << '\n';
244315
}

test/Reflection/Inputs/Closures.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
public func makeSomeClosures<T, U>(t: T, x: Int, y: C1<U>)
3+
-> (() -> (), () -> (), () -> ()) {
4+
return ({ _ = t }, { _ = x }, { _ = y })
5+
}

test/Reflection/typeref_decoding.result.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,3 +657,25 @@ typealias Result = Swift.Int
657657
BUILTIN TYPES:
658658
==============
659659

660+
CAPTURE DESCRIPTORS:
661+
====================
662+
- Capture types:
663+
!!! Invalid typeref
664+
- Metadata sources:
665+
(generic_type_parameter depth=0 index=0)
666+
(closure_binding index=0)
667+
- Capture types:
668+
(struct Swift.Int)
669+
- Metadata sources:
670+
(generic_type_parameter depth=0 index=0)
671+
!!! Invalid matadata source
672+
- Capture types:
673+
(bound_generic_class TypesToReflect.C1
674+
(generic_type_parameter depth=0 index=1))
675+
- Metadata sources:
676+
(generic_type_parameter depth=0 index=0)
677+
(closure_binding index=0)
678+
(generic_type_parameter depth=0 index=1)
679+
(generic_argument index=0
680+
(reference_capture index=1))
681+
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// RUN: rm -rf %t && mkdir -p %t
2-
// RUN: %target-build-swift %S/Inputs/ConcreteTypes.swift %S/Inputs/GenericTypes.swift %S/Inputs/Protocols.swift %S/Inputs/Extensions.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -Xfrontend -enable-reflection-metadata -Xfrontend -enable-reflection-names -o %t/libTypesToReflect.%target-dylib-extension
2+
// RUN: %target-build-swift %S/Inputs/ConcreteTypes.swift %S/Inputs/GenericTypes.swift %S/Inputs/Protocols.swift %S/Inputs/Extensions.swift %S/Inputs/Closures.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -Xfrontend -enable-reflection-metadata -Xfrontend -enable-reflection-names -o %t/libTypesToReflect.%target-dylib-extension
33
// RUN: %target-swift-reflection-dump -binary-filename %t/libTypesToReflect.%target-dylib-extension > %t/typeref_decoding.txt
44
// RUN: diff -u %S/typeref_decoding.result.txt %t/typeref_decoding.txt

test/Reflection/typeref_decoding_objc.result.txt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,39 @@ ASSOCIATED TYPES:
4646
BUILTIN TYPES:
4747
==============
4848

49+
CAPTURE DESCRIPTORS:
50+
====================
51+
- Capture types:
52+
(struct Swift.StaticString)
53+
(struct Swift.StaticString)
54+
(struct Swift.UInt)
55+
(struct Swift.UInt)
56+
- Metadata sources:
57+
- Capture types:
58+
- Metadata sources:
59+
!!! Invalid typeref
60+
!!! Invalid matadata source
61+
- Capture types:
62+
(struct Swift.StaticString)
63+
(bound_generic_struct Swift.UnsafeBufferPointer
64+
(struct Swift.UInt8))
65+
(struct Swift.UInt)
66+
(struct Swift.UInt)
67+
- Metadata sources:
68+
- Capture types:
69+
- Metadata sources:
70+
!!! Invalid typeref
71+
!!! Invalid matadata source
72+
- Capture types:
73+
(bound_generic_struct Swift.UnsafeBufferPointer
74+
(struct Swift.UInt8))
75+
(bound_generic_struct Swift.UnsafeBufferPointer
76+
(struct Swift.UInt8))
77+
(struct Swift.UInt)
78+
(struct Swift.UInt)
79+
- Metadata sources:
80+
- Capture types:
81+
- Metadata sources:
82+
!!! Invalid typeref
83+
!!! Invalid matadata source
84+

0 commit comments

Comments
 (0)