clang的AST源码分析
QualType类
重点关注函数:
cpp
/// Return true if this QualType doesn't point to a type yet.
bool isNull() const {
return Value.getPointer().isNull();
}
被RecursiveASTVisitor调用
cpp
template <typename Derived>
bool RecursiveASTVisitor<Derived>::TraverseType(QualType T) {
if (T.isNull())
return true;
switch (T->getTypeClass()) {
#define ABSTRACT_TYPE(CLASS, BASE)
#define TYPE(CLASS, BASE) \
case Type::CLASS: \
return getDerived().Traverse##CLASS##Type( \
static_cast<CLASS##Type *>(const_cast<Type *>(T.getTypePtr())));
#include "clang/AST/TypeNodes.inc"
}
return true;
}
RecursiveASTVisitor的模版类DeducedTypeVisitor
cpp
namespace {
/// Computes the deduced type at a given location by visiting the relevant
/// nodes. We use this to display the actual type when hovering over an "auto"
/// keyword or "decltype()" expression.
/// FIXME: This could have been a lot simpler by visiting AutoTypeLocs but it
/// seems that the AutoTypeLocs that can be visited along with their AutoType do
/// not have the deduced type set. Instead, we have to go to the appropriate
/// DeclaratorDecl/FunctionDecl and work our back to the AutoType that does have
/// a deduced type set. The AST should be improved to simplify this scenario.
class DeducedTypeVisitor : public RecursiveASTVisitor<DeducedTypeVisitor> {
SourceLocation SearchedLocation;
public:
DeducedTypeVisitor(SourceLocation SearchedLocation)
: SearchedLocation(SearchedLocation) {}
// Handle auto initializers:
//- auto i = 1;
//- decltype(auto) i = 1;
//- auto& i = 1;
//- auto* i = &a;
bool VisitDeclaratorDecl(DeclaratorDecl *D) {
if (!D->getTypeSourceInfo() ||
D->getTypeSourceInfo()->getTypeLoc().getBeginLoc() != SearchedLocation)
return true;
if (auto *AT = D->getType()->getContainedAutoType()) {
DeducedType = AT->desugar();
}
return true;
}
// Handle auto return types:
//- auto foo() {}
//- auto& foo() {}
//- auto foo() -> int {}
//- auto foo() -> decltype(1+1) {}
//- operator auto() const { return 10; }
bool VisitFunctionDecl(FunctionDecl *D) {
if (!D->getTypeSourceInfo())
return true;
// Loc of auto in return type (c++14).
auto CurLoc = D->getReturnTypeSourceRange().getBegin();
// Loc of "auto" in operator auto()
if (CurLoc.isInvalid() && isa<CXXConversionDecl>(D))
CurLoc = D->getTypeSourceInfo()->getTypeLoc().getBeginLoc();
// Loc of "auto" in function with trailing return type (c++11).
if (CurLoc.isInvalid())
CurLoc = D->getSourceRange().getBegin();
if (CurLoc != SearchedLocation)
return true;
const AutoType *AT = D->getReturnType()->getContainedAutoType();
if (AT && !AT->getDeducedType().isNull()) {
DeducedType = AT->getDeducedType();
} else if (auto *DT = dyn_cast<DecltypeType>(D->getReturnType())) {
// auto in a trailing return type just points to a DecltypeType and
// getContainedAutoType does not unwrap it.
if (!DT->getUnderlyingType().isNull())
DeducedType = DT->getUnderlyingType();
} else if (!D->getReturnType().isNull()) {
DeducedType = D->getReturnType();
}
return true;
}
// Handle non-auto decltype, e.g.:
// - auto foo() -> decltype(expr) {}
// - decltype(expr);
bool VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
if (TL.getBeginLoc() != SearchedLocation)
return true;
// A DecltypeType's underlying type can be another DecltypeType! E.g.
// int I = 0;
// decltype(I) J = I;
// decltype(J) K = J;
const DecltypeType *DT = dyn_cast<DecltypeType>(TL.getTypePtr());
while (DT && !DT->getUnderlyingType().isNull()) {
DeducedType = DT->getUnderlyingType();
DT = dyn_cast<DecltypeType>(DeducedType.getTypePtr());
}
return true;
}
QualType DeducedType;
};
} // namespace
注意getDeducedType
cpp
llvm::Optional<QualType> getDeducedType(ASTContext &ASTCtx,
SourceLocation Loc) {
if (!Loc.isValid())
return {};
DeducedTypeVisitor V(Loc);
V.TraverseAST(ASTCtx);
if (V.DeducedType.isNull())
return llvm::None;
return V.DeducedType;
}
MatchASTVisitor
cpp
bool MatchASTVisitor::TraverseType(QualType TypeNode) {
match(TypeNode);
return RecursiveASTVisitor<MatchASTVisitor>::TraverseType(TypeNode);
}