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