StmtVisitor.h revision 198092
1//===--- StmtVisitor.h - Visitor for Stmt subclasses ------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the StmtVisitor interface. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_STMTVISITOR_H 15#define LLVM_CLANG_AST_STMTVISITOR_H 16 17#include "clang/AST/ExprCXX.h" 18#include "clang/AST/ExprObjC.h" 19#include "clang/AST/StmtCXX.h" 20#include "clang/AST/StmtObjC.h" 21 22namespace clang { 23 24#define DISPATCH(NAME, CLASS) \ 25 return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<CLASS*>(S)) 26 27/// StmtVisitor - This class implements a simple visitor for Stmt subclasses. 28/// Since Expr derives from Stmt, this also includes support for visiting Exprs. 29template<typename ImplClass, typename RetTy=void> 30class StmtVisitor { 31public: 32 RetTy Visit(Stmt *S) { 33 34 // If we have a binary expr, dispatch to the subcode of the binop. A smart 35 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt 36 // below. 37 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { 38 switch (BinOp->getOpcode()) { 39 default: assert(0 && "Unknown binary operator!"); 40 case BinaryOperator::PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator); 41 case BinaryOperator::PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator); 42 case BinaryOperator::Mul: DISPATCH(BinMul, BinaryOperator); 43 case BinaryOperator::Div: DISPATCH(BinDiv, BinaryOperator); 44 case BinaryOperator::Rem: DISPATCH(BinRem, BinaryOperator); 45 case BinaryOperator::Add: DISPATCH(BinAdd, BinaryOperator); 46 case BinaryOperator::Sub: DISPATCH(BinSub, BinaryOperator); 47 case BinaryOperator::Shl: DISPATCH(BinShl, BinaryOperator); 48 case BinaryOperator::Shr: DISPATCH(BinShr, BinaryOperator); 49 50 case BinaryOperator::LT: DISPATCH(BinLT, BinaryOperator); 51 case BinaryOperator::GT: DISPATCH(BinGT, BinaryOperator); 52 case BinaryOperator::LE: DISPATCH(BinLE, BinaryOperator); 53 case BinaryOperator::GE: DISPATCH(BinGE, BinaryOperator); 54 case BinaryOperator::EQ: DISPATCH(BinEQ, BinaryOperator); 55 case BinaryOperator::NE: DISPATCH(BinNE, BinaryOperator); 56 57 case BinaryOperator::And: DISPATCH(BinAnd, BinaryOperator); 58 case BinaryOperator::Xor: DISPATCH(BinXor, BinaryOperator); 59 case BinaryOperator::Or : DISPATCH(BinOr, BinaryOperator); 60 case BinaryOperator::LAnd: DISPATCH(BinLAnd, BinaryOperator); 61 case BinaryOperator::LOr : DISPATCH(BinLOr, BinaryOperator); 62 case BinaryOperator::Assign: DISPATCH(BinAssign, BinaryOperator); 63 case BinaryOperator::MulAssign: 64 DISPATCH(BinMulAssign, CompoundAssignOperator); 65 case BinaryOperator::DivAssign: 66 DISPATCH(BinDivAssign, CompoundAssignOperator); 67 case BinaryOperator::RemAssign: 68 DISPATCH(BinRemAssign, CompoundAssignOperator); 69 case BinaryOperator::AddAssign: 70 DISPATCH(BinAddAssign, CompoundAssignOperator); 71 case BinaryOperator::SubAssign: 72 DISPATCH(BinSubAssign, CompoundAssignOperator); 73 case BinaryOperator::ShlAssign: 74 DISPATCH(BinShlAssign, CompoundAssignOperator); 75 case BinaryOperator::ShrAssign: 76 DISPATCH(BinShrAssign, CompoundAssignOperator); 77 case BinaryOperator::AndAssign: 78 DISPATCH(BinAndAssign, CompoundAssignOperator); 79 case BinaryOperator::OrAssign: 80 DISPATCH(BinOrAssign, CompoundAssignOperator); 81 case BinaryOperator::XorAssign: 82 DISPATCH(BinXorAssign, CompoundAssignOperator); 83 case BinaryOperator::Comma: DISPATCH(BinComma, BinaryOperator); 84 } 85 } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) { 86 switch (UnOp->getOpcode()) { 87 default: assert(0 && "Unknown unary operator!"); 88 case UnaryOperator::PostInc: DISPATCH(UnaryPostInc, UnaryOperator); 89 case UnaryOperator::PostDec: DISPATCH(UnaryPostDec, UnaryOperator); 90 case UnaryOperator::PreInc: DISPATCH(UnaryPreInc, UnaryOperator); 91 case UnaryOperator::PreDec: DISPATCH(UnaryPreDec, UnaryOperator); 92 case UnaryOperator::AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator); 93 case UnaryOperator::Deref: DISPATCH(UnaryDeref, UnaryOperator); 94 case UnaryOperator::Plus: DISPATCH(UnaryPlus, UnaryOperator); 95 case UnaryOperator::Minus: DISPATCH(UnaryMinus, UnaryOperator); 96 case UnaryOperator::Not: DISPATCH(UnaryNot, UnaryOperator); 97 case UnaryOperator::LNot: DISPATCH(UnaryLNot, UnaryOperator); 98 case UnaryOperator::Real: DISPATCH(UnaryReal, UnaryOperator); 99 case UnaryOperator::Imag: DISPATCH(UnaryImag, UnaryOperator); 100 case UnaryOperator::Extension: DISPATCH(UnaryExtension, UnaryOperator); 101 case UnaryOperator::OffsetOf: DISPATCH(UnaryOffsetOf, UnaryOperator); 102 } 103 } 104 105 // Top switch stmt: dispatch to VisitFooStmt for each FooStmt. 106 switch (S->getStmtClass()) { 107 default: assert(0 && "Unknown stmt kind!"); 108#define STMT(CLASS, PARENT) \ 109 case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS); 110#include "clang/AST/StmtNodes.def" 111 } 112 } 113 114 // If the implementation chooses not to implement a certain visit method, fall 115 // back on VisitExpr or whatever else is the superclass. 116#define STMT(CLASS, PARENT) \ 117 RetTy Visit ## CLASS(CLASS *S) { DISPATCH(PARENT, PARENT); } 118#include "clang/AST/StmtNodes.def" 119 120 // If the implementation doesn't implement binary operator methods, fall back 121 // on VisitBinaryOperator. 122#define BINOP_FALLBACK(NAME) \ 123 RetTy VisitBin ## NAME(BinaryOperator *S) { \ 124 DISPATCH(BinaryOperator, BinaryOperator); \ 125 } 126 BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI) 127 BINOP_FALLBACK(Mul) BINOP_FALLBACK(Div) BINOP_FALLBACK(Rem) 128 BINOP_FALLBACK(Add) BINOP_FALLBACK(Sub) BINOP_FALLBACK(Shl) 129 BINOP_FALLBACK(Shr) 130 131 BINOP_FALLBACK(LT) BINOP_FALLBACK(GT) BINOP_FALLBACK(LE) 132 BINOP_FALLBACK(GE) BINOP_FALLBACK(EQ) BINOP_FALLBACK(NE) 133 BINOP_FALLBACK(And) BINOP_FALLBACK(Xor) BINOP_FALLBACK(Or) 134 BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr) 135 136 BINOP_FALLBACK(Assign) 137 BINOP_FALLBACK(Comma) 138#undef BINOP_FALLBACK 139 140 // If the implementation doesn't implement compound assignment operator 141 // methods, fall back on VisitCompoundAssignOperator. 142#define CAO_FALLBACK(NAME) \ 143 RetTy VisitBin ## NAME(CompoundAssignOperator *S) { \ 144 DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \ 145 } 146 CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign) 147 CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign) 148 CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign) 149 CAO_FALLBACK(XorAssign) 150#undef CAO_FALLBACK 151 152 // If the implementation doesn't implement unary operator methods, fall back 153 // on VisitUnaryOperator. 154#define UNARYOP_FALLBACK(NAME) \ 155 RetTy VisitUnary ## NAME(UnaryOperator *S) { \ 156 DISPATCH(UnaryOperator, UnaryOperator); \ 157 } 158 UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec) 159 UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec) 160 UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref) 161 162 UNARYOP_FALLBACK(Plus) UNARYOP_FALLBACK(Minus) 163 UNARYOP_FALLBACK(Not) UNARYOP_FALLBACK(LNot) 164 UNARYOP_FALLBACK(Real) UNARYOP_FALLBACK(Imag) 165 UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(OffsetOf) 166#undef UNARYOP_FALLBACK 167 168 // Base case, ignore it. :) 169 RetTy VisitStmt(Stmt *Node) { return RetTy(); } 170}; 171 172#undef DISPATCH 173 174} // end namespace clang 175 176#endif 177