For example, I have a class:
template<typename T>
class Foo {
public:
T getBar();
private:
T bar_;
};
It is instantiated with:
using FooBarT = Foo<Bar>;
How do I get the CXXRecordDecl
with resolved fields and methods for Foo<bar>
?
I tried:
const auto *typeAliasDecl = llvm::dyn_cast<clang::TypeAliasDecl>(decl);
typeAliasDecl->getUnderlyingType()->getAsCXXRecordDecl()->dump();
The output I get is:
ClassTemplateSpecializationDecl 0x0000000 class Foo
`-TemplateArgument type 'Bar'
However, I want the CXXRecordDecl
with the fields and methods too so I can iterate through them. I've also tried:
for (const auto *contextDecl: typeAliasDecl->getUnderlyingType()->getUnqualifiedDesugaredType()->getAsCXXRecordDecl()->getDeclContext()->decls()) {
const auto *classTemplateDecl = llvm::dyn_cast<clang::ClassTemplateDecl>(contextDecl);
classTemplateDecl->dump();
}
The output:
ClassTemplateDecl Foo
|-TemplateTypeParmDecl 0x0000000 referenced typename depth 0 index 0 T
|-CXXRecordDecl class Foo definition
| ...
| |-FieldDecl 0x0000000 referenced bar_ 'T'
|-ClassTemplateSpecializationDecl 0x0000000 class Foo
`-TemplateArgument type 'Bar'
As you can see the CXXRecordDecl class Foo definition
has access to the FieldDecl
, but doesn't know about the type instantiation of bar_
, while the ClassTemplateSpecializationDecl
does.
I want the CXXRecordDecl
with the instantiated type for FieldDecl bar_