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 #include "clang/AST/StmtOpenMP.h" 22 23 namespace clang { 24 25 template <typename T> struct make_ptr { typedef T *type; }; 26 template <typename T> struct make_const_ptr { typedef const T *type; }; 27 28 /// StmtVisitorBase - This class implements a simple visitor for Stmt 29 /// subclasses. Since Expr derives from Stmt, this also includes support for 30 /// visiting Exprs. 31 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void> 32 class StmtVisitorBase { 33 public: 34 35 #define PTR(CLASS) typename Ptr<CLASS>::type 36 #define DISPATCH(NAME, CLASS) \ 37 return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<PTR(CLASS)>(S)) 38 Visit(PTR (Stmt)S)39 RetTy Visit(PTR(Stmt) S) { 40 41 // If we have a binary expr, dispatch to the subcode of the binop. A smart 42 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt 43 // below. 44 if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) { 45 switch (BinOp->getOpcode()) { 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 case UO_PostInc: DISPATCH(UnaryPostInc, UnaryOperator); 84 case UO_PostDec: DISPATCH(UnaryPostDec, UnaryOperator); 85 case UO_PreInc: DISPATCH(UnaryPreInc, UnaryOperator); 86 case UO_PreDec: DISPATCH(UnaryPreDec, UnaryOperator); 87 case UO_AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator); 88 case UO_Deref: DISPATCH(UnaryDeref, UnaryOperator); 89 case UO_Plus: DISPATCH(UnaryPlus, UnaryOperator); 90 case UO_Minus: DISPATCH(UnaryMinus, UnaryOperator); 91 case UO_Not: DISPATCH(UnaryNot, UnaryOperator); 92 case UO_LNot: DISPATCH(UnaryLNot, UnaryOperator); 93 case UO_Real: DISPATCH(UnaryReal, UnaryOperator); 94 case UO_Imag: DISPATCH(UnaryImag, UnaryOperator); 95 case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator); 96 } 97 } 98 99 // Top switch stmt: dispatch to VisitFooStmt for each FooStmt. 100 switch (S->getStmtClass()) { 101 default: llvm_unreachable("Unknown stmt kind!"); 102 #define ABSTRACT_STMT(STMT) 103 #define STMT(CLASS, PARENT) \ 104 case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS); 105 #include "clang/AST/StmtNodes.inc" 106 } 107 } 108 109 // If the implementation chooses not to implement a certain visit method, fall 110 // back on VisitExpr or whatever else is the superclass. 111 #define STMT(CLASS, PARENT) \ 112 RetTy Visit ## CLASS(PTR(CLASS) S) { DISPATCH(PARENT, PARENT); } 113 #include "clang/AST/StmtNodes.inc" 114 115 // If the implementation doesn't implement binary operator methods, fall back 116 // on VisitBinaryOperator. 117 #define BINOP_FALLBACK(NAME) \ 118 RetTy VisitBin ## NAME(PTR(BinaryOperator) S) { \ 119 DISPATCH(BinaryOperator, BinaryOperator); \ 120 } BINOP_FALLBACK(PtrMemI)121 BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI) 122 BINOP_FALLBACK(Mul) BINOP_FALLBACK(Div) BINOP_FALLBACK(Rem) 123 BINOP_FALLBACK(Add) BINOP_FALLBACK(Sub) BINOP_FALLBACK(Shl) 124 BINOP_FALLBACK(Shr) 125 126 BINOP_FALLBACK(LT) BINOP_FALLBACK(GT) BINOP_FALLBACK(LE) 127 BINOP_FALLBACK(GE) BINOP_FALLBACK(EQ) BINOP_FALLBACK(NE) 128 BINOP_FALLBACK(And) BINOP_FALLBACK(Xor) BINOP_FALLBACK(Or) 129 BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr) 130 131 BINOP_FALLBACK(Assign) 132 BINOP_FALLBACK(Comma) 133 #undef BINOP_FALLBACK 134 135 // If the implementation doesn't implement compound assignment operator 136 // methods, fall back on VisitCompoundAssignOperator. 137 #define CAO_FALLBACK(NAME) \ 138 RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S) { \ 139 DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \ 140 } 141 CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign) 142 CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign) 143 CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign) 144 CAO_FALLBACK(XorAssign) 145 #undef CAO_FALLBACK 146 147 // If the implementation doesn't implement unary operator methods, fall back 148 // on VisitUnaryOperator. 149 #define UNARYOP_FALLBACK(NAME) \ 150 RetTy VisitUnary ## NAME(PTR(UnaryOperator) S) { \ 151 DISPATCH(UnaryOperator, UnaryOperator); \ 152 } 153 UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec) 154 UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec) 155 UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref) 156 157 UNARYOP_FALLBACK(Plus) UNARYOP_FALLBACK(Minus) 158 UNARYOP_FALLBACK(Not) UNARYOP_FALLBACK(LNot) 159 UNARYOP_FALLBACK(Real) UNARYOP_FALLBACK(Imag) 160 UNARYOP_FALLBACK(Extension) 161 #undef UNARYOP_FALLBACK 162 163 // Base case, ignore it. :) 164 RetTy VisitStmt(PTR(Stmt) Node) { return RetTy(); } 165 166 #undef PTR 167 #undef DISPATCH 168 }; 169 170 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses. 171 /// Since Expr derives from Stmt, this also includes support for visiting Exprs. 172 /// 173 /// This class does not preserve constness of Stmt pointers (see also 174 /// ConstStmtVisitor). 175 template<typename ImplClass, typename RetTy=void> 176 class StmtVisitor 177 : public StmtVisitorBase<make_ptr, ImplClass, RetTy> {}; 178 179 /// ConstStmtVisitor - This class implements a simple visitor for Stmt 180 /// subclasses. Since Expr derives from Stmt, this also includes support for 181 /// visiting Exprs. 182 /// 183 /// This class preserves constness of Stmt pointers (see also StmtVisitor). 184 template<typename ImplClass, typename RetTy=void> 185 class ConstStmtVisitor 186 : public StmtVisitorBase<make_const_ptr, ImplClass, RetTy> {}; 187 188 /// \brief This class implements a simple visitor for OMPClause 189 /// subclasses. 190 template<class ImplClass, template <typename> class Ptr, typename RetTy> 191 class OMPClauseVisitorBase { 192 public: 193 #define PTR(CLASS) typename Ptr<CLASS>::type 194 #define DISPATCH(CLASS) \ 195 return static_cast<ImplClass*>(this)->Visit##CLASS(static_cast<PTR(CLASS)>(S)) 196 197 #define OPENMP_CLAUSE(Name, Class) \ 198 RetTy Visit ## Class (PTR(Class) S) { DISPATCH(Class); } 199 #include "clang/Basic/OpenMPKinds.def" 200 Visit(PTR (OMPClause)S)201 RetTy Visit(PTR(OMPClause) S) { 202 // Top switch clause: visit each OMPClause. 203 switch (S->getClauseKind()) { 204 default: llvm_unreachable("Unknown clause kind!"); 205 #define OPENMP_CLAUSE(Name, Class) \ 206 case OMPC_ ## Name : return Visit ## Class(static_cast<PTR(Class)>(S)); 207 #include "clang/Basic/OpenMPKinds.def" 208 } 209 } 210 // Base case, ignore it. :) VisitOMPClause(PTR (OMPClause)Node)211 RetTy VisitOMPClause(PTR(OMPClause) Node) { return RetTy(); } 212 #undef PTR 213 #undef DISPATCH 214 }; 215 216 template<class ImplClass, typename RetTy = void> 217 class OMPClauseVisitor : 218 public OMPClauseVisitorBase <ImplClass, make_ptr, RetTy> {}; 219 template<class ImplClass, typename RetTy = void> 220 class ConstOMPClauseVisitor : 221 public OMPClauseVisitorBase <ImplClass, make_const_ptr, RetTy> {}; 222 223 } // end namespace clang 224 225 #endif 226