Misc. Swift C++ API reflection questions

I am sorry, there's a lot of Swift reflection questions I have here. Feel free to only answer a subset. Thank you in advance for your help.

  1. Looking at the Swift C++ APIs for reflection on Swift structs descriptors, I am having a hard time seeing if it's possible to find a way to access a StructDescriptor's methods (read: just methods, not ivars) via the C++ reflection APIs in __swift_reflection_version 3

    Just for reference, going after a ClassDescriptor's methods have been very straightforward as I'll use getMethodDescriptors found here: swift/Metadata.h at 6295cecfa77ac4f10a5569930bba26d1934972ff · apple/swift · GitHub I don't see a clear way to do the equivalent for a StructDescriptor

  2. Can you explain the use of FieldOffsetVectorOffset in StructDescriptor? I am having a hard time understanding it and how to practically apply it via the C++ APIs. I am aware of TrailingGenericContextObjects, but don't know if FieldOffsetVectorOffset applies to this or not or how it's used.

  3. swift/TypeMetadata.rst at main · apple/swift · GitHub has the following statement: "The value witness table pointer is at offset -1 from the metadata pointer, that is, the pointer-sized word immediately before the pointer's referenced address."

    How to I programmatically obtain the "value witness table" if I have a reference to a StructDescriptor*?. I thought the Fields.get() route might be the way to do it, but I am second guessing myself now...

  4. What's the point to the Mach-O section __swift5_typeref? I totally understand the __swift5_types, which I'd say equates to Objective-C's __DATA.__objc_classlist, but I don't get the purpose to __swift5_typeref, which seems like it's already taken care of by __swift5_types. I know there's more data packed into a __swift5_typeref index (6 bytes on 64bit) vs __swift5_types 4 bytes, but can't deduce the use of those extra 2 bytes.

Thank you. Sorry again for the question count. Y'all are awesome

I'm going to try and answer each of these. Note that I'm still learning the metadata myself.

As far as I can tell, a struct's methods are not emitted in the context descriptor (or anywhere afaict).

The field offset vector offset indicates the number of words from the metadata pointer to the vector of field offsets.

I don't believe you can access the value witness table from a context descriptor because you need the metadata to access a vwt.

This isn't limited to Mach-O, on Windows this is .sw5tyrf, and on Linux this is swift5_typeref. I believe the purpose of this section is for mangled type references to context descriptors. For example, some mangled names have symbolic references which reference a type context descriptor. These manglings go into the type reference section.

1 Like