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