Skip to content

Commit 698625f

Browse files
committed
Implement ReflectionContext::readInstanceStartFromTypeRef
Implement ReflectionContext::readInstanceStartFromTypeRef as an alternative way to finding out the start of an instance's field when reading the field's start from binary is not possible (for example, embedded Swift doesn't emit any reflection metadata on the binary).
1 parent 7b6f45c commit 698625f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

include/swift/Remote/MetadataReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ class MetadataReader {
617617

618618
/// Given a remote pointer to class metadata, attempt to discover its class
619619
/// instance size and whether fields should use the resilient layout strategy.
620-
llvm::Optional<unsigned> readInstanceStartAndAlignmentFromClassMetadata(
620+
llvm::Optional<unsigned> readInstanceStartFromClassMetadata(
621621
StoredPointer MetadataAddress) {
622622
auto meta = readMetadata(MetadataAddress);
623623
if (!meta || meta->getKind() != MetadataKind::Class)

include/swift/RemoteInspection/ReflectionContext.h

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ class ReflectionContext
908908
// Figure out where the stored properties of this class begin
909909
// by looking at the size of the superclass
910910
auto start =
911-
this->readInstanceStartAndAlignmentFromClassMetadata(MetadataAddress);
911+
this->readInstanceStartFromClassMetadata(MetadataAddress);
912912

913913
// Perform layout
914914
if (start)
@@ -1202,6 +1202,33 @@ class ReflectionContext
12021202
}
12031203
}
12041204

1205+
/// Given a typeref, attempt to discover its class instance size. For a
1206+
/// version of this function that performs the same job but starting out with
1207+
/// an instance pointer check
1208+
/// MetadataReader::readInstanceStartFromClassMetadata.
1209+
llvm::Optional<unsigned> readInstanceStartFromTypeRef(const TypeRef *TR) {
1210+
size_t isaAndRetainCountSize = sizeof(StoredSize) + sizeof(long long);
1211+
1212+
const TypeRef *superclass = getBuilder().lookupSuperclass(TR);
1213+
if (!superclass)
1214+
// If there is no superclass the stat of the instance's field is right
1215+
// after the isa and retain fields.
1216+
return isaAndRetainCountSize;
1217+
1218+
auto superclassStart = readInstanceStartFromTypeRef(superclass);
1219+
if (!superclassStart)
1220+
return llvm::None;
1221+
1222+
auto *superTI = getBuilder().getTypeConverter().getClassInstanceTypeInfo(
1223+
superclass, *superclassStart, nullptr);
1224+
if (!superTI)
1225+
return llvm::None;
1226+
1227+
// The start of the subclass's fields is right after the super class's ones.
1228+
size_t start = superTI->getSize();
1229+
return start;
1230+
}
1231+
12051232
const RecordTypeInfo *getRecordTypeInfo(const TypeRef *TR,
12061233
remote::TypeInfoProvider *ExternalTypeInfo) {
12071234
auto *TypeInfo = getTypeInfo(TR, ExternalTypeInfo);

0 commit comments

Comments
 (0)