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