例如,我有一堂课:
template<typename T>
class Foo {
public:
T getBar();
private:
T bar_;
};
它被实例化为:
using FooBarT = Foo<Bar>;
如何获得已CXXRecordDecl
解析的字段和方法Foo<bar>
?
我试过了:
const auto *typeAliasDecl = llvm::dyn_cast<clang::TypeAliasDecl>(decl);
typeAliasDecl->getUnderlyingType()->getAsCXXRecordDecl()->dump();
我得到的输出是:
ClassTemplateSpecializationDecl 0x0000000 class Foo
`-TemplateArgument type 'Bar'
但是,我也想要CXXRecordDecl
字段和方法,以便我可以遍历它们。我也试过:
for (const auto *contextDecl: typeAliasDecl->getUnderlyingType()->getUnqualifiedDesugaredType()->getAsCXXRecordDecl()->getDeclContext()->decls()) {
const auto *classTemplateDecl = llvm::dyn_cast<clang::ClassTemplateDecl>(contextDecl);
classTemplateDecl->dump();
}
输出:
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'
如您所见,CXXRecordDecl class Foo definition
可以访问FieldDecl
,但不知道 的类型实例化bar_
,而ClassTemplateSpecializationDecl
确实。
我想要CXXRecordDecl
实例化类型FieldDecl bar_