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