比如我有一个类:
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_
的实例化类型
最佳答案
仅供引用,您想要的 CXXRecordDecl 只是 AST 中的 ClassTemplateSpecializationDecl,因为 ClassTemplateSpecializationDecl 是 CXXRecordDecl 的子类。您真正想要的不是您已经拥有的 CXXRecordDecl,而是该 CXXRecordDecl 中的 FieldDecl。
您在 ClassTemplateSpecializationDecl 下没有 FieldDecl 的原因是您的模板实例化代码没有使用 bar_。试试下面的来源:
template<typename T>
class Foo {
public:
T getBar() { return bar_; };
private:
T bar_;
};
using FooBarT = Foo<int>;
void func() {
FooBarT().getBar();
}
然后 FieldDecl 将在 ClassTemplateSpecializationDecl 下:
| `-ClassTemplateSpecializationDecl 0x1fe7f2a9d80 <line:2:1, line:9:1> line:3:7 class Foo definition
...
| |-FieldDecl 0x1fe7f2aa3c8 <line:8:2, col:4> col:4 referenced bar_ 'int':'int'
关于c++ - 如何在 Clang 中获取模板类的实例化 CXXRecordDecl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813060/