How to retrieve the inheritance hierarchy of a type?

Is there a way to use SwiftSyntax in a Swift macro to retrieve the inheritance hierarchy of a type? Currently, within a macro, I am aware that I can utilize classKeyword to determine whether it is a class or struct, and I can employ inheritedTypeCollection to acquire all directly inherited classes or protocols. However, I am curious about identifying the root class of a type.

For example:

I have a root class called "Person" and its subclass called "Student". Now, I have created a subclass of "Student" called "MaleStudent". How can I determine the root class of "MaleStudent" to be "Person"?

1 Like

SwiftSyntax is only about syntax. It doesn’t know anything about types and inheritance. Thus there is no way to retrieve the type hierarchy from a class declaration. It has only a very restricted view on your code - a single file or only a part of it.

To collect knowledge about types you need the whole context of the module to build as well as its dependencies. That’s not in the scope of SwiftSyntax, however.

6 Likes