たとえば、次のクラスがあります。
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_