2009-07-30 07:40:39 +08:00
|
|
|
//===--- ASTVisitor.h - Visitor for an ASTContext ---------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the ASTVisitor interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CLANG_INDEX_ASTVISITOR_H
|
|
|
|
#define LLVM_CLANG_INDEX_ASTVISITOR_H
|
|
|
|
|
|
|
|
#include "clang/AST/DeclVisitor.h"
|
|
|
|
#include "clang/AST/StmtVisitor.h"
|
2009-09-30 05:27:32 +08:00
|
|
|
#include "clang/AST/TypeLocVisitor.h"
|
2009-07-30 07:40:39 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
|
|
|
|
namespace idx {
|
|
|
|
|
|
|
|
/// \brief Traverses the full AST, both Decls and Stmts.
|
|
|
|
template<typename ImplClass>
|
|
|
|
class ASTVisitor : public DeclVisitor<ImplClass>,
|
2009-09-30 05:26:53 +08:00
|
|
|
public StmtVisitor<ImplClass>,
|
|
|
|
public TypeLocVisitor<ImplClass> {
|
2009-07-30 07:40:39 +08:00
|
|
|
public:
|
|
|
|
ASTVisitor() : CurrentDecl(0) { }
|
|
|
|
|
|
|
|
Decl *CurrentDecl;
|
|
|
|
|
|
|
|
typedef ASTVisitor<ImplClass> Base;
|
|
|
|
typedef DeclVisitor<ImplClass> BaseDeclVisitor;
|
|
|
|
typedef StmtVisitor<ImplClass> BaseStmtVisitor;
|
2009-09-30 05:26:53 +08:00
|
|
|
typedef TypeLocVisitor<ImplClass> BaseTypeLocVisitor;
|
2009-07-30 07:40:39 +08:00
|
|
|
|
|
|
|
using BaseStmtVisitor::Visit;
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// DeclVisitor
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Visit(Decl *D) {
|
|
|
|
Decl *PrevDecl = CurrentDecl;
|
|
|
|
CurrentDecl = D;
|
|
|
|
BaseDeclVisitor::Visit(D);
|
|
|
|
CurrentDecl = PrevDecl;
|
|
|
|
}
|
2009-09-30 05:26:53 +08:00
|
|
|
|
|
|
|
void VisitDeclaratorDecl(DeclaratorDecl *D) {
|
|
|
|
BaseDeclVisitor::VisitDeclaratorDecl(D);
|
|
|
|
if (DeclaratorInfo *DInfo = D->getDeclaratorInfo())
|
|
|
|
Visit(DInfo->getTypeLoc());
|
|
|
|
}
|
2009-07-30 07:40:39 +08:00
|
|
|
|
|
|
|
void VisitFunctionDecl(FunctionDecl *D) {
|
2009-09-30 05:26:35 +08:00
|
|
|
BaseDeclVisitor::VisitFunctionDecl(D);
|
2009-07-30 07:40:39 +08:00
|
|
|
if (D->isThisDeclarationADefinition())
|
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitObjCMethodDecl(ObjCMethodDecl *D) {
|
2009-09-30 05:26:35 +08:00
|
|
|
BaseDeclVisitor::VisitObjCMethodDecl(D);
|
2009-07-30 07:40:39 +08:00
|
|
|
if (D->getBody())
|
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitBlockDecl(BlockDecl *D) {
|
2009-09-30 05:26:35 +08:00
|
|
|
BaseDeclVisitor::VisitBlockDecl(D);
|
2009-07-30 07:40:39 +08:00
|
|
|
Visit(D->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitVarDecl(VarDecl *D) {
|
2009-09-30 05:26:35 +08:00
|
|
|
BaseDeclVisitor::VisitVarDecl(D);
|
2009-07-30 07:40:39 +08:00
|
|
|
if (Expr *Init = D->getInit())
|
|
|
|
Visit(Init);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDecl(Decl *D) {
|
|
|
|
if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (DeclContext *DC = dyn_cast<DeclContext>(D))
|
|
|
|
static_cast<ImplClass*>(this)->VisitDeclContext(DC);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitDeclContext(DeclContext *DC) {
|
|
|
|
for (DeclContext::decl_iterator
|
|
|
|
I = DC->decls_begin(), E = DC->decls_end(); I != E; ++I)
|
|
|
|
Visit(*I);
|
|
|
|
}
|
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// StmtVisitor
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void VisitDeclStmt(DeclStmt *Node) {
|
|
|
|
for (DeclStmt::decl_iterator
|
|
|
|
I = Node->decl_begin(), E = Node->decl_end(); I != E; ++I)
|
|
|
|
Visit(*I);
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:48 +08:00
|
|
|
void VisitBlockExpr(BlockExpr *Node) {
|
|
|
|
Visit(Node->getBlockDecl());
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:39 +08:00
|
|
|
void VisitStmt(Stmt *Node) {
|
|
|
|
for (Stmt::child_iterator
|
|
|
|
I = Node->child_begin(), E = Node->child_end(); I != E; ++I)
|
|
|
|
if (*I)
|
|
|
|
Visit(*I);
|
|
|
|
}
|
2009-09-30 05:26:53 +08:00
|
|
|
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// TypeLocVisitor
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void Visit(TypeLoc TL) {
|
|
|
|
for (; TL; TL = TL.getNextTypeLoc())
|
|
|
|
BaseTypeLocVisitor::Visit(TL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitArrayLoc(ArrayLoc TL) {
|
|
|
|
BaseTypeLocVisitor::VisitArrayLoc(TL);
|
|
|
|
if (TL.getSizeExpr())
|
|
|
|
Visit(TL.getSizeExpr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VisitFunctionLoc(FunctionLoc TL) {
|
|
|
|
BaseTypeLocVisitor::VisitFunctionLoc(TL);
|
|
|
|
for (unsigned i = 0; i != TL.getNumArgs(); ++i)
|
|
|
|
Visit(TL.getArg(i));
|
|
|
|
}
|
|
|
|
|
2009-07-30 07:40:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace idx
|
|
|
|
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
#endif
|