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