Skip to content

Commit 71d2869

Browse files
committed
Reflection: Plumb through capture descriptor section
1 parent ddc81e1 commit 71d2869

File tree

6 files changed

+202
-8
lines changed

6 files changed

+202
-8
lines changed

include/swift/Reflection/Records.h

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,184 @@ class BuiltinTypeDescriptorIterator
364364
}
365365
};
366366

367+
class CaptureTypeRecord {
368+
const RelativeDirectPointer<const char> MangledTypeName;
369+
370+
public:
371+
CaptureTypeRecord() = delete;
372+
373+
bool hasMangledTypeName() const {
374+
return MangledTypeName;
375+
}
376+
377+
std::string getMangledTypeName() const {
378+
return MangledTypeName.get();
379+
}
380+
};
381+
382+
struct CaptureTypeRecordIterator {
383+
const CaptureTypeRecord *Cur;
384+
const CaptureTypeRecord * const End;
385+
386+
CaptureTypeRecordIterator(const CaptureTypeRecord *Cur,
387+
const CaptureTypeRecord * const End)
388+
: Cur(Cur), End(End) {}
389+
390+
const CaptureTypeRecord &operator*() const {
391+
return *Cur;
392+
}
393+
394+
const CaptureTypeRecord *operator->() const {
395+
return Cur;
396+
}
397+
398+
CaptureTypeRecordIterator &operator++() {
399+
++Cur;
400+
return *this;
401+
}
402+
403+
bool operator==(const CaptureTypeRecordIterator &other) const {
404+
return Cur == other.Cur && End == other.End;
405+
}
406+
407+
bool operator!=(const CaptureTypeRecordIterator &other) const {
408+
return !(*this == other);
409+
}
410+
};
411+
412+
class MetadataSourceRecord {
413+
const RelativeDirectPointer<const char> MangledTypeName;
414+
const RelativeDirectPointer<const char> MangledMetadataSource;
415+
416+
public:
417+
MetadataSourceRecord() = delete;
418+
419+
bool hasMangledTypeName() const {
420+
return MangledTypeName;
421+
}
422+
423+
std::string getMangledTypeName() const {
424+
return MangledTypeName.get();
425+
}
426+
427+
bool hasMangledMetadataSource() const {
428+
return MangledMetadataSource;
429+
}
430+
431+
std::string getMangledMetadataSource() const {
432+
return MangledMetadataSource.get();
433+
}
434+
};
435+
436+
struct MetadataSourceRecordIterator {
437+
const MetadataSourceRecord *Cur;
438+
const MetadataSourceRecord * const End;
439+
440+
MetadataSourceRecordIterator(const MetadataSourceRecord *Cur,
441+
const MetadataSourceRecord * const End)
442+
: Cur(Cur), End(End) {}
443+
444+
const MetadataSourceRecord &operator*() const {
445+
return *Cur;
446+
}
447+
448+
const MetadataSourceRecord *operator->() const {
449+
return Cur;
450+
}
451+
452+
MetadataSourceRecordIterator &operator++() {
453+
++Cur;
454+
return *this;
455+
}
456+
457+
bool operator==(const MetadataSourceRecordIterator &other) const {
458+
return Cur == other.Cur && End == other.End;
459+
}
460+
461+
bool operator!=(const MetadataSourceRecordIterator &other) const {
462+
return !(*this == other);
463+
}
464+
};
465+
466+
// Capture descriptors describe the layout of a closure context
467+
// object. Unlike nominal types, the generic substitutions for a
468+
// closure context come from the object, and not the metadata.
469+
class CaptureDescriptor {
470+
const CaptureTypeRecord *getCaptureTypeRecordBuffer() const {
471+
return reinterpret_cast<const CaptureTypeRecord *>(this + 1);
472+
}
473+
474+
const MetadataSourceRecord *getMetadataSourceRecordBuffer() const {
475+
return reinterpret_cast<const MetadataSourceRecord *>(capture_end().End);
476+
}
477+
478+
public:
479+
uint32_t NumCaptureTypes;
480+
uint32_t NumMetadataSources;
481+
uint32_t NumBindings;
482+
483+
using const_iterator = FieldRecordIterator;
484+
485+
CaptureTypeRecordIterator capture_begin() const {
486+
auto Begin = getCaptureTypeRecordBuffer();
487+
auto End = Begin + NumCaptureTypes;
488+
return { Begin, End };
489+
}
490+
491+
CaptureTypeRecordIterator capture_end() const {
492+
auto Begin = getCaptureTypeRecordBuffer();
493+
auto End = Begin + NumCaptureTypes;
494+
return { End, End };
495+
}
496+
497+
MetadataSourceRecordIterator source_begin() const {
498+
auto Begin = getMetadataSourceRecordBuffer();
499+
auto End = Begin + NumMetadataSources;
500+
return { Begin, End };
501+
}
502+
503+
MetadataSourceRecordIterator source_end() const {
504+
auto Begin = getMetadataSourceRecordBuffer();
505+
auto End = Begin + NumMetadataSources;
506+
return { End, End };
507+
}
508+
};
509+
510+
class CaptureDescriptorIterator
511+
: public std::iterator<std::forward_iterator_tag, CaptureDescriptor> {
512+
public:
513+
const void *Cur;
514+
const void * const End;
515+
CaptureDescriptorIterator(const void *Cur, const void * const End)
516+
: Cur(Cur), End(End) {}
517+
518+
const CaptureDescriptor &operator*() const {
519+
return *reinterpret_cast<const CaptureDescriptor *>(Cur);
520+
}
521+
522+
const CaptureDescriptor *operator->() const {
523+
return reinterpret_cast<const CaptureDescriptor *>(Cur);
524+
}
525+
526+
CaptureDescriptorIterator &operator++() {
527+
const auto &CR = this->operator*();
528+
const void *Next = reinterpret_cast<const char *>(Cur)
529+
+ sizeof(CaptureDescriptor)
530+
+ CR.NumCaptureTypes * sizeof(CaptureTypeRecord)
531+
+ CR.NumMetadataSources * sizeof(MetadataSourceRecord);
532+
Cur = Next;
533+
return *this;
534+
}
535+
536+
bool operator==(CaptureDescriptorIterator const &other) const {
537+
return Cur == other.Cur && End == other.End;
538+
}
539+
540+
bool operator!=(CaptureDescriptorIterator const &other) const {
541+
return !(*this == other);
542+
}
543+
};
544+
367545
} // end namespace reflection
368546
} // end namespace swift
369547

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ class ReflectionSection {
6868
using FieldSection = ReflectionSection<FieldDescriptorIterator>;
6969
using AssociatedTypeSection = ReflectionSection<AssociatedTypeIterator>;
7070
using BuiltinTypeSection = ReflectionSection<BuiltinTypeDescriptorIterator>;
71+
using CaptureSection = ReflectionSection<CaptureDescriptorIterator>;
7172
using GenericSection = ReflectionSection<const void *>;
7273

7374
struct ReflectionInfo {
7475
FieldSection fieldmd;
7576
AssociatedTypeSection assocty;
7677
BuiltinTypeSection builtin;
78+
CaptureSection capture;
7779
GenericSection typeref;
7880
GenericSection reflstr;
7981
};

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct swift_reflection_info {
3838
swift_reflection_section_t fieldmd;
3939
swift_reflection_section_t builtin;
4040
swift_reflection_section_t assocty;
41+
swift_reflection_section_t capture;
4142
swift_reflection_section_t typeref;
4243
swift_reflection_section_t reflstr;
4344
} swift_reflection_info_t;

stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ internal struct ReflectionInfo : Sequence {
5454
internal let fieldmd: Section?
5555
internal let assocty: Section?
5656
internal let builtin: Section?
57+
internal let capture: Section?
5758
internal let typeref: Section?
5859
internal let reflstr: Section?
5960

@@ -62,6 +63,7 @@ internal struct ReflectionInfo : Sequence {
6263
fieldmd,
6364
assocty,
6465
builtin,
66+
capture,
6567
typeref,
6668
reflstr
6769
].makeIterator())
@@ -98,6 +100,7 @@ internal func getSectionInfo(_ name: String,
98100
/// - __swift3_fieldmd
99101
/// - __swift3_assocty
100102
/// - __swift3_builtin
103+
/// - __swift3_capture
101104
/// - __swift3_typeref
102105
/// - __swift3_reflstr (optional, may have been stripped out)
103106
///
@@ -111,14 +114,16 @@ internal func getReflectionInfoForImage(atIndex i: UInt32) -> ReflectionInfo? {
111114

112115
let imageName = _dyld_get_image_name(i)
113116
if let fieldmd = getSectionInfo("__swift3_fieldmd", header) {
114-
let assocty = getSectionInfo("__swift3_assocty", header)
115-
let builtin = getSectionInfo("__swift3_builtin", header)
116-
let typeref = getSectionInfo("__swift3_typeref", header)
117-
let reflstr = getSectionInfo("__swift3_reflstr", header)
117+
let assocty = getSectionInfo("__swift3_assocty", header)
118+
let builtin = getSectionInfo("__swift3_builtin", header)
119+
let capture = getSectionInfo("__swift3_capture", header)
120+
let typeref = getSectionInfo("__swift3_typeref", header)
121+
let reflstr = getSectionInfo("__swift3_reflstr", header)
118122
return ReflectionInfo(imageName: String(validatingUTF8: imageName)!,
119123
fieldmd: fieldmd,
120124
assocty: assocty,
121125
builtin: builtin,
126+
capture: capture,
122127
typeref: typeref,
123128
reflstr: reflstr)
124129
}

tools/swift-reflection-dump/swift-reflection-dump.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ static ReflectionInfo findReflectionInfo(const ObjectFile *objectFile) {
113113
objectFile, {"__swift3_assocty", ".swift3_assocty"});
114114
auto builtinTypeSection = findReflectionSection<BuiltinTypeSection>(
115115
objectFile, {"__swift3_builtin", ".swift3_builtin"});
116+
auto captureSection = findReflectionSection<CaptureSection>(
117+
objectFile, {"__swift3_capture", ".swift3_capture"});
116118
auto typeRefSection = findReflectionSection<GenericSection>(
117119
objectFile, {"__swift3_typeref", ".swift3_typeref"});
118120
auto reflectionStringsSection = findReflectionSection<GenericSection>(
@@ -122,6 +124,7 @@ static ReflectionInfo findReflectionInfo(const ObjectFile *objectFile) {
122124
fieldSection,
123125
associatedTypeSection,
124126
builtinTypeSection,
127+
captureSection,
125128
typeRefSection,
126129
reflectionStringsSection,
127130
};

tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct RemoteReflectionInfo {
4343
RemoteSection fieldmd;
4444
RemoteSection assocty;
4545
RemoteSection builtin;
46+
RemoteSection capture;
4647
RemoteSection typeref;
4748
RemoteSection reflstr;
4849
uintptr_t StartAddress;
@@ -91,25 +92,27 @@ uintptr_t getEndAddress(const RemoteSection Sections[], size_t Count) {
9192
RemoteReflectionInfo makeRemoteReflectionInfo(RemoteSection fieldmd,
9293
RemoteSection assocty,
9394
RemoteSection builtin,
95+
RemoteSection capture,
9496
RemoteSection typeref,
9597
RemoteSection reflstr) {
9698
RemoteReflectionInfo Info = {
9799
fieldmd,
98100
assocty,
99101
builtin,
102+
capture,
100103
typeref,
101104
reflstr,
102105
0,
103106
0
104107
};
105108

106-
const RemoteSection Sections[5] = {
107-
fieldmd, assocty, builtin, typeref, reflstr
109+
const RemoteSection Sections[6] = {
110+
fieldmd, assocty, builtin, capture, typeref, reflstr
108111
};
109112

110-
Info.StartAddress = getStartAddress(Sections, 5);
113+
Info.StartAddress = getStartAddress(Sections, 6);
111114

112-
uintptr_t EndAddress = getEndAddress(Sections, 5);
115+
uintptr_t EndAddress = getEndAddress(Sections, 6);
113116
Info.TotalSize = EndAddress - Info.StartAddress;
114117

115118
return Info;
@@ -256,6 +259,7 @@ PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
256259
makeRemoteSection(Reader),
257260
makeRemoteSection(Reader),
258261
makeRemoteSection(Reader),
262+
makeRemoteSection(Reader),
259263
makeRemoteSection(Reader));
260264
}
261265

@@ -277,6 +281,7 @@ PipeMemoryReader_receiveReflectionInfo(SwiftReflectionContextRef RC,
277281
makeLocalSection(Buffer, RemoteInfo.fieldmd, RemoteInfo),
278282
makeLocalSection(Buffer, RemoteInfo.assocty, RemoteInfo),
279283
makeLocalSection(Buffer, RemoteInfo.builtin, RemoteInfo),
284+
makeLocalSection(Buffer, RemoteInfo.capture, RemoteInfo),
280285
makeLocalSection(Buffer, RemoteInfo.typeref, RemoteInfo),
281286
makeLocalSection(Buffer, RemoteInfo.reflstr, RemoteInfo)
282287
};

0 commit comments

Comments
 (0)