【发布时间】:2015-04-27 16:37:06
【问题描述】:
我正在使用 Clang 的 API 为我正在编写的工具解析以下 sn-p:
namespace NS
{
struct X
{
};
}
struct Y
{
NS::X foo();
};
I use a (type that derives from) RecursiveASTVisitor to visit the AST。当VisitCXXRecordDecl()函数被调用时,我可以得到一个指向声明的Type对象的指针:
bool VisitCXXRecordDecl(CXXRecordDecl* const decl)
{
auto declared_type = decl->getTypeForDecl();
// ...
}
同样,当VisitCXXMethodDecl() 函数被调用时,我可以得到一个指向函数返回Type 的指针,如下所示:
bool VisitCXXMethodDecl(CXXMethodDecl* const func)
{
auto return_type = func->getReturnType().getTypePtr();
// ...
}
令我惊讶的是,上面两个函数中的变量declared_type 和return_type 并没有指向同一个Type 对象。
现在我确实知道了规范类型,事实上,如果我在VisitCXXMethodDecl() 中编写以下内容,我将获得指向同一Type 对象declared_type 指向的指针:
auto canonical_type = return_type->getCanonicalTypeInternal().getTypePtr();
// Now `canonical_type` holds a pointer to the same Type object as the
// `declared_type` variable inside VisitCXXRecordDecl().
但是,我认为只有在涉及类型别名时,类型才具有与自身不同的规范类型(至少这是我从 the Doxygen docs 和 the CFE Internals Manual 收集到的)。我不确定这里创建两个Type对象的原因是什么,这让我相信我没有理解Type对象和规范类型在Clang的设计中的作用。
【问题讨论】:
-
clang 可能试图记住该类型拼写为 NS::X,而不是例如 X(这要归功于 using 指令)。为什么不更仔细地研究非规范类型(可能在调试器中)以查看它存储了哪些信息?
标签: c++ types clang abstract-syntax-tree