| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | #ifndef LLVM_CLANG_AST_STMTVISITOR_H |
| 14 | #define LLVM_CLANG_AST_STMTVISITOR_H |
| 15 | |
| 16 | #include "clang/AST/ExprCXX.h" |
| 17 | #include "clang/AST/ExprObjC.h" |
| 18 | #include "clang/AST/ExprOpenMP.h" |
| 19 | #include "clang/AST/Stmt.h" |
| 20 | #include "clang/AST/StmtCXX.h" |
| 21 | #include "clang/AST/StmtObjC.h" |
| 22 | #include "clang/AST/StmtOpenMP.h" |
| 23 | #include "clang/Basic/LLVM.h" |
| 24 | #include "llvm/ADT/STLExtras.h" |
| 25 | #include "llvm/Support/Casting.h" |
| 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include <utility> |
| 28 | |
| 29 | namespace clang { |
| 30 | |
| 31 | |
| 32 | |
| 33 | template<template <typename> class Ptr, typename ImplClass, typename RetTy=void, |
| 34 | class... ParamTys> |
| 35 | class StmtVisitorBase { |
| 36 | public: |
| 37 | #define PTR(CLASS) typename Ptr<CLASS>::type |
| 38 | #define DISPATCH(NAME, CLASS) \ |
| 39 | return static_cast<ImplClass*>(this)->Visit ## NAME( \ |
| 40 | static_cast<PTR(CLASS)>(S), std::forward<ParamTys>(P)...) |
| 41 | |
| 42 | RetTy Visit(PTR(Stmt) S, ParamTys... P) { |
| 43 | |
| 44 | |
| 45 | |
| 46 | if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) { |
| 47 | switch (BinOp->getOpcode()) { |
| 48 | case BO_PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator); |
| 49 | case BO_PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator); |
| 50 | case BO_Mul: DISPATCH(BinMul, BinaryOperator); |
| 51 | case BO_Div: DISPATCH(BinDiv, BinaryOperator); |
| 52 | case BO_Rem: DISPATCH(BinRem, BinaryOperator); |
| 53 | case BO_Add: DISPATCH(BinAdd, BinaryOperator); |
| 54 | case BO_Sub: DISPATCH(BinSub, BinaryOperator); |
| 55 | case BO_Shl: DISPATCH(BinShl, BinaryOperator); |
| 56 | case BO_Shr: DISPATCH(BinShr, BinaryOperator); |
| 57 | |
| 58 | case BO_LT: DISPATCH(BinLT, BinaryOperator); |
| 59 | case BO_GT: DISPATCH(BinGT, BinaryOperator); |
| 60 | case BO_LE: DISPATCH(BinLE, BinaryOperator); |
| 61 | case BO_GE: DISPATCH(BinGE, BinaryOperator); |
| 62 | case BO_EQ: DISPATCH(BinEQ, BinaryOperator); |
| 63 | case BO_NE: DISPATCH(BinNE, BinaryOperator); |
| 64 | case BO_Cmp: DISPATCH(BinCmp, BinaryOperator); |
| 65 | |
| 66 | case BO_And: DISPATCH(BinAnd, BinaryOperator); |
| 67 | case BO_Xor: DISPATCH(BinXor, BinaryOperator); |
| 68 | case BO_Or : DISPATCH(BinOr, BinaryOperator); |
| 69 | case BO_LAnd: DISPATCH(BinLAnd, BinaryOperator); |
| 70 | case BO_LOr : DISPATCH(BinLOr, BinaryOperator); |
| 71 | case BO_Assign: DISPATCH(BinAssign, BinaryOperator); |
| 72 | case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator); |
| 73 | case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator); |
| 74 | case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator); |
| 75 | case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator); |
| 76 | case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator); |
| 77 | case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator); |
| 78 | case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator); |
| 79 | case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator); |
| 80 | case BO_OrAssign: DISPATCH(BinOrAssign, CompoundAssignOperator); |
| 81 | case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator); |
| 82 | case BO_Comma: DISPATCH(BinComma, BinaryOperator); |
| 83 | } |
| 84 | } else if (PTR(UnaryOperator) UnOp = dyn_cast<UnaryOperator>(S)) { |
| 85 | switch (UnOp->getOpcode()) { |
| 86 | case UO_PostInc: DISPATCH(UnaryPostInc, UnaryOperator); |
| 87 | case UO_PostDec: DISPATCH(UnaryPostDec, UnaryOperator); |
| 88 | case UO_PreInc: DISPATCH(UnaryPreInc, UnaryOperator); |
| 89 | case UO_PreDec: DISPATCH(UnaryPreDec, UnaryOperator); |
| 90 | case UO_AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator); |
| 91 | case UO_Deref: DISPATCH(UnaryDeref, UnaryOperator); |
| 92 | case UO_Plus: DISPATCH(UnaryPlus, UnaryOperator); |
| 93 | case UO_Minus: DISPATCH(UnaryMinus, UnaryOperator); |
| 94 | case UO_Not: DISPATCH(UnaryNot, UnaryOperator); |
| 95 | case UO_LNot: DISPATCH(UnaryLNot, UnaryOperator); |
| 96 | case UO_Real: DISPATCH(UnaryReal, UnaryOperator); |
| 97 | case UO_Imag: DISPATCH(UnaryImag, UnaryOperator); |
| 98 | case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator); |
| 99 | case UO_Coawait: DISPATCH(UnaryCoawait, UnaryOperator); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | |
| 104 | switch (S->getStmtClass()) { |
| 105 | default: llvm_unreachable("Unknown stmt kind!"); |
| 106 | #define ABSTRACT_STMT(STMT) |
| 107 | #define STMT(CLASS, PARENT) \ |
| 108 | case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS); |
| 109 | #include "clang/AST/StmtNodes.inc" |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | #define STMT(CLASS, PARENT) \ |
| 116 | RetTy Visit ## CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); } |
| 117 | #include "clang/AST/StmtNodes.inc" |
| 118 | |
| 119 | |
| 120 | |
| 121 | #define BINOP_FALLBACK(NAME) \ |
| 122 | RetTy VisitBin ## NAME(PTR(BinaryOperator) S, ParamTys... P) { \ |
| 123 | DISPATCH(BinaryOperator, BinaryOperator); \ |
| 124 | } |
| 125 | BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI) |
| 126 | BINOP_FALLBACK(Mul) BINOP_FALLBACK(Div) BINOP_FALLBACK(Rem) |
| 127 | BINOP_FALLBACK(Add) BINOP_FALLBACK(Sub) BINOP_FALLBACK(Shl) |
| 128 | BINOP_FALLBACK(Shr) |
| 129 | |
| 130 | BINOP_FALLBACK(LT) BINOP_FALLBACK(GT) BINOP_FALLBACK(LE) |
| 131 | BINOP_FALLBACK(GE) BINOP_FALLBACK(EQ) BINOP_FALLBACK(NE) |
| 132 | BINOP_FALLBACK(Cmp) |
| 133 | |
| 134 | BINOP_FALLBACK(And) BINOP_FALLBACK(Xor) BINOP_FALLBACK(Or) |
| 135 | BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr) |
| 136 | |
| 137 | BINOP_FALLBACK(Assign) |
| 138 | BINOP_FALLBACK(Comma) |
| 139 | #undef BINOP_FALLBACK |
| 140 | |
| 141 | |
| 142 | |
| 143 | #define CAO_FALLBACK(NAME) \ |
| 144 | RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \ |
| 145 | DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \ |
| 146 | } |
| 147 | CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign) |
| 148 | CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign) |
| 149 | CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign) |
| 150 | CAO_FALLBACK(XorAssign) |
| 151 | #undef CAO_FALLBACK |
| 152 | |
| 153 | |
| 154 | |
| 155 | #define UNARYOP_FALLBACK(NAME) \ |
| 156 | RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \ |
| 157 | DISPATCH(UnaryOperator, UnaryOperator); \ |
| 158 | } |
| 159 | UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec) |
| 160 | UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec) |
| 161 | UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref) |
| 162 | |
| 163 | UNARYOP_FALLBACK(Plus) UNARYOP_FALLBACK(Minus) |
| 164 | UNARYOP_FALLBACK(Not) UNARYOP_FALLBACK(LNot) |
| 165 | UNARYOP_FALLBACK(Real) UNARYOP_FALLBACK(Imag) |
| 166 | UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait) |
| 167 | #undef UNARYOP_FALLBACK |
| 168 | |
| 169 | |
| 170 | RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); } |
| 171 | |
| 172 | #undef PTR |
| 173 | #undef DISPATCH |
| 174 | }; |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | template <typename ImplClass, typename RetTy = void, typename... ParamTys> |
| 182 | class StmtVisitor |
| 183 | : public StmtVisitorBase<std::add_pointer, ImplClass, RetTy, ParamTys...> { |
| 184 | }; |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | template <typename ImplClass, typename RetTy = void, typename... ParamTys> |
| 192 | class ConstStmtVisitor : public StmtVisitorBase<llvm::make_const_ptr, ImplClass, |
| 193 | RetTy, ParamTys...> {}; |
| 194 | |
| 195 | } |
| 196 | |
| 197 | #endif |
| 198 | |