C++ AST question - casting GenericTypeDecl to ClassDecl

I'm not very comfortable with C++, and I'm trying to take a stab at resolving an open issue with symbol graph generation for the Documentation generation system. I would appreciate any advice in pointing me in the right direction.

I think the question I have it related to the type GenericTypeDecl within swiftSymbolGraphGen. In the issue, @QuietMisdreavus has the advice of converting TD, which is a pointer to a type of GenericTypeDecl to the type ClassDecl, in order to interrogate it to determine if the relevant declaration is an actor or distributed actor.

I'm stumped on how to mechanically do this transformation in C++. I naively tried a C-based cast (TypeDecl), but that was a no-go with the compiler. If I'm reading the types correctly (not guaranteed), GenericTypeDecl is derived from TypeDecl, so it seems like there should be a path to interpret it as TypeDecl.

How do you make this kind of conversion in C++?

Or would anyone be willing to point me at relevant reading on where I could learn?

1 Like

i look forward to when i can finally get rid of this hack

I think you make the conversion using dyn_cast.

2 Likes

You use cast<> or dyn_cast<>, which are template functions defined in the LLVM library. In this code, you already know it's a Class (because you switch on the kind) so cast<> is what you want:

auto *CD = cast<ClassDecl>(TD);
if (CD->isActor()) ...

Also see page 84-90 of https://download.swift.org/docs/assets/generics.pdf.

3 Likes

Thank you!