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 and ConstStmtVisitor interfaces. 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 22 namespace clang { 23 24 template <typename T> struct make_ptr { typedef T *type; }; 25 template <typename T> struct make_const_ptr { typedef const T *type; }; 26 27 /// StmtVisitorBase - This class implements a simple visitor for Stmt 28 /// subclasses. Since Expr derives from Stmt, this also includes support for 29 /// visiting Exprs. 30 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void> 31 class StmtVisitorBase { 32 public: 33 34 #define PTR(CLASS) typename Ptr<CLASS>::type 35 #define DISPATCH(NAME, CLASS) \ 36 return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<PTR(CLASS)>(S)) 37 Visit(PTR (Stmt)S)38 RetTy Visit(PTR(Stmt) S) { 39 40 // If we have a binary expr, dispatch to the subcode of the binop. A smart 41 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt 42 // below. 43 if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) { 44 switch (BinOp->getOpcode()) { 45 case BO_PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator); 46 case BO_PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator); 47 case BO_Mul: DISPATCH(BinMul, BinaryOperator); 48 case BO_Div: DISPATCH(BinDiv, BinaryOperator); 49 case BO_Rem: DISPATCH(BinRem, BinaryOperator); 50 case BO_Add: DISPATCH(BinAdd, BinaryOperator); 51 case BO_Sub: DISPATCH(BinSub, BinaryOperator); 52 case BO_Shl: DISPATCH(BinShl, BinaryOperator); 53 case BO_Shr: DISPATCH(BinShr, BinaryOperator); 54 55 case BO_LT: DISPATCH(BinLT, BinaryOperator); 56 case BO_GT: DISPATCH(BinGT, BinaryOperator); 57 case BO_LE: DISPATCH(BinLE, BinaryOperator); 58 case BO_GE: DISPATCH(BinGE, BinaryOperator); 59 case BO_EQ: DISPATCH(BinEQ, BinaryOperator); 60 case BO_NE: DISPATCH(BinNE, BinaryOperator); 61 62 case BO_And: DISPATCH(BinAnd, BinaryOperator); 63 case BO_Xor: DISPATCH(BinXor, BinaryOperator); 64 case BO_Or : DISPATCH(BinOr, BinaryOperator); 65 case BO_LAnd: DISPATCH(BinLAnd, BinaryOperator); 66 case BO_LOr : DISPATCH(BinLOr, BinaryOperator); 67 case BO_Assign: DISPATCH(BinAssign, BinaryOperator); 68 case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator); 69 case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator); 70 case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator); 71 case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator); 72 case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator); 73 case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator); 74 case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator); 75 case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator); 76 case BO_OrAssign: DISPATCH(BinOrAssign, CompoundAssignOperator); 77 case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator); 78 case BO_Comma: DISPATCH(BinComma, BinaryOperator); 79 } 80 } else if (PTR(UnaryOperator) UnOp = dyn_cast<UnaryOperator>(S)) { 81 switch (UnOp->getOpcode()) { 82 case UO_PostInc: DISPATCH(UnaryPostInc, UnaryOperator); 83 case UO_PostDec: DISPATCH(UnaryPostDec, UnaryOperator); 84 case UO_PreInc: DISPATCH(UnaryPreInc, UnaryOperator); 85 case UO_PreDec: DISPATCH(UnaryPreDec, UnaryOperator); 86 case UO_AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator); 87 case UO_Deref: DISPATCH(UnaryDeref, UnaryOperator); 88 case UO_Plus: DISPATCH(UnaryPlus, UnaryOperator); 89 case UO_Minus: DISPATCH(UnaryMinus, UnaryOperator); 90 case UO_Not: DISPATCH(UnaryNot, UnaryOperator); 91 case UO_LNot: DISPATCH(UnaryLNot, UnaryOperator); 92 case UO_Real: DISPATCH(UnaryReal, UnaryOperator); 93 case UO_Imag: DISPATCH(UnaryImag, UnaryOperator); 94 case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator); 95 } 96 } 97 98 // Top switch stmt: dispatch to VisitFooStmt for each FooStmt. 99 switch (S->getStmtClass()) { 100 default: llvm_unreachable("Unknown stmt kind!"); 101 #define ABSTRACT_STMT(STMT) 102 #define STMT(CLASS, PARENT) \ 103 case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS); 104 #include "clang/AST/StmtNodes.inc" 105 } 106 } 107 108 // If the implementation chooses not to implement a certain visit method, fall 109 // back on VisitExpr or whatever else is the superclass. 110 #define STMT(CLASS, PARENT) \ 111 RetTy Visit ## CLASS(PTR(CLASS) S) { DISPATCH(PARENT, PARENT); } 112 #include "clang/AST/StmtNodes.inc" 113 114 // If the implementation doesn't implement binary operator methods, fall back 115 // on VisitBinaryOperator. 116 #define BINOP_FALLBACK(NAME) \ 117 RetTy VisitBin ## NAME(PTR(BinaryOperator) S) { \ 118 DISPATCH(BinaryOperator, BinaryOperator); \ 119 } BINOP_FALLBACK(PtrMemI)120 BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI) 121 BINOP_FALLBACK(Mul) BINOP_FALLBACK(Div) BINOP_FALLBACK(Rem) 122 BINOP_FALLBACK(Add) BINOP_FALLBACK(Sub) BINOP_FALLBACK(Shl) 123 BINOP_FALLBACK(Shr) 124 125 BINOP_FALLBACK(LT) BINOP_FALLBACK(GT) BINOP_FALLBACK(LE) 126 BINOP_FALLBACK(GE) BINOP_FALLBACK(EQ) BINOP_FALLBACK(NE) 127 BINOP_FALLBACK(And) BINOP_FALLBACK(Xor) BINOP_FALLBACK(Or) 128 BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr) 129 130 BINOP_FALLBACK(Assign) 131 BINOP_FALLBACK(Comma) 132 #undef BINOP_FALLBACK 133 134 // If the implementation doesn't implement compound assignment operator 135 // methods, fall back on VisitCompoundAssignOperator. 136 #define CAO_FALLBACK(NAME) \ 137 RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S) { \ 138 DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \ 139 } 140 CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign) 141 CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign) 142 CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign) 143 CAO_FALLBACK(XorAssign) 144 #undef CAO_FALLBACK 145 146 // If the implementation doesn't implement unary operator methods, fall back 147 // on VisitUnaryOperator. 148 #define UNARYOP_FALLBACK(NAME) \ 149 RetTy VisitUnary ## NAME(PTR(UnaryOperator) S) { \ 150 DISPATCH(UnaryOperator, UnaryOperator); \ 151 } 152 UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec) 153 UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec) 154 UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref) 155 156 UNARYOP_FALLBACK(Plus) UNARYOP_FALLBACK(Minus) 157 UNARYOP_FALLBACK(Not) UNARYOP_FALLBACK(LNot) 158 UNARYOP_FALLBACK(Real) UNARYOP_FALLBACK(Imag) 159 UNARYOP_FALLBACK(Extension) 160 #undef UNARYOP_FALLBACK 161 162 // Base case, ignore it. :) 163 RetTy VisitStmt(PTR(Stmt) Node) { return RetTy(); } 164 165 #undef PTR 166 #undef DISPATCH 167 }; 168 169 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses. 170 /// Since Expr derives from Stmt, this also includes support for visiting Exprs. 171 /// 172 /// This class does not preserve constness of Stmt pointers (see also 173 /// ConstStmtVisitor). 174 template<typename ImplClass, typename RetTy=void> 175 class StmtVisitor 176 : public StmtVisitorBase<make_ptr, ImplClass, RetTy> {}; 177 178 /// ConstStmtVisitor - This class implements a simple visitor for Stmt 179 /// subclasses. Since Expr derives from Stmt, this also includes support for 180 /// visiting Exprs. 181 /// 182 /// This class preserves constness of Stmt pointers (see also StmtVisitor). 183 template<typename ImplClass, typename RetTy=void> 184 class ConstStmtVisitor 185 : public StmtVisitorBase<make_const_ptr, ImplClass, RetTy> {}; 186 187 } // end namespace clang 188 189 #endif 190