Skip to content

Commit c05e48b

Browse files
committed
Add CaptureDescriptor struct definition
For convenience when reading looking at heap closure metadata. Also move the capture typerefs above the metadata sources, since they're more likely to be accessed than generic metadata sources.
1 parent 6a51ae8 commit c05e48b

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

include/swift/Reflection/Records.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,63 @@ class BuiltinTypeDescriptorIterator
341341
}
342342
};
343343

344+
/// A key-value pair in a TypeRef -> MetadataSource map.
345+
struct GenericMetadataSource {
346+
using Key = RelativeDirectPointer<const char>;
347+
using Value = Key;
348+
349+
const Key MangledTypeName;
350+
const Value EncodedMetadataSource;
351+
};
352+
353+
/// Describes the layout of a heap closure.
354+
///
355+
/// For simplicity's sake and other reasons, this shouldn't contain
356+
/// architecture-specifically sized things like direct pointers, uintptr_t, etc.
357+
struct CaptureDescriptor {
358+
public:
359+
360+
/// The number of captures in the closure and the number of typerefs that
361+
/// immediately follow this struct.
362+
const uint32_t NumCaptures;
363+
364+
/// The number of sources of metadata available in the MetadataSourceMap
365+
/// directly following the list of capture's typerefs.
366+
const uint32_t NumMetadataSources;
367+
368+
/// The number of items in the NecessaryBindings structure at the head of
369+
/// the closure.
370+
const uint32_t NumBindings;
371+
372+
/// Get the key-value pair for the ith generic metadata source.
373+
const GenericMetadataSource &getGenericMetadataSource(size_t i) const {
374+
assert(i <= NumMetadataSources &&
375+
"Generic metadata source index out of range");
376+
auto Begin = getGenericMetadataSourceBuffer();
377+
return Begin[i];
378+
}
379+
380+
/// Get the typeref (encoded as a mangled type name) of the ith
381+
/// closure capture.
382+
const RelativeDirectPointer<const char> &
383+
getCaptureMangledTypeName(size_t i) const {
384+
assert(i <= NumCaptures && "Capture index out of range");
385+
auto Begin = getCaptureTypeRefBuffer();
386+
return Begin[i];
387+
}
388+
389+
private:
390+
const GenericMetadataSource *getGenericMetadataSourceBuffer() const {
391+
auto BeginTR = reinterpret_cast<const char *>(getCaptureTypeRefBuffer());
392+
auto EndTR = BeginTR + NumCaptures * sizeof(GenericMetadataSource);
393+
return reinterpret_cast<const GenericMetadataSource *>(EndTR );
394+
}
395+
396+
const RelativeDirectPointer<const char> *getCaptureTypeRefBuffer() const {
397+
return reinterpret_cast<const RelativeDirectPointer<const char> *>(this+1);
398+
}
399+
};
400+
344401
} // end namespace reflection
345402
} // end namespace swift
346403

lib/IRGen/GenReflection.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,11 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
636636
addConstantInt32(MetadataSources.size());
637637
addConstantInt32(Layout.getBindings().size());
638638

639+
// Now add typerefs of all of the captures.
640+
for (auto CaptureType : CaptureTypes) {
641+
addTypeRef(Callee.getModule().getSwiftModule(), CaptureType);
642+
}
643+
639644
// Add the pairs that make up the generic param -> metadata source map
640645
// to the struct.
641646
for (auto GenericAndSource : MetadataSources) {
@@ -644,11 +649,6 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
644649
addTypeRef(nullptr, GenericParam);
645650
addMetadataSource(Source);
646651
}
647-
648-
// Now add typerefs of all of the captures.
649-
for (auto CaptureType : CaptureTypes) {
650-
addTypeRef(Callee.getModule().getSwiftModule(), CaptureType);
651-
}
652652
}
653653

654654
llvm::GlobalVariable *emit() {

0 commit comments

Comments
 (0)