clang  6.0.0svn
StmtVisitor.h
Go to the documentation of this file.
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>
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 
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 
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)
131  BINOP_FALLBACK(Shr)
132 
136  BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr)
137 
138  BINOP_FALLBACK(Assign)
140 #undef BINOP_FALLBACK
141 
142  // If the implementation doesn't implement compound assignment operator
143  // methods, fall back on VisitCompoundAssignOperator.
144 #define CAO_FALLBACK(NAME) \
145  RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
146  DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
147  }
148  CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
149  CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
150  CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
151  CAO_FALLBACK(XorAssign)
152 #undef CAO_FALLBACK
153 
154  // If the implementation doesn't implement unary operator methods, fall back
155  // on VisitUnaryOperator.
156 #define UNARYOP_FALLBACK(NAME) \
157  RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
158  DISPATCH(UnaryOperator, UnaryOperator); \
159  }
160  UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
161  UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec)
162  UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref)
163 
167  UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait)
168 #undef UNARYOP_FALLBACK
169 
170  // Base case, ignore it. :)
171  RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }
172 
173 #undef PTR
174 #undef DISPATCH
175 };
176 
177 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
178 /// Since Expr derives from Stmt, this also includes support for visiting Exprs.
179 ///
180 /// This class does not preserve constness of Stmt pointers (see also
181 /// ConstStmtVisitor).
182 template<typename ImplClass, typename RetTy=void, typename... ParamTys>
184  : public StmtVisitorBase<make_ptr, ImplClass, RetTy, ParamTys...> {};
185 
186 /// ConstStmtVisitor - This class implements a simple visitor for Stmt
187 /// subclasses. Since Expr derives from Stmt, this also includes support for
188 /// visiting Exprs.
189 ///
190 /// This class preserves constness of Stmt pointers (see also StmtVisitor).
191 template<typename ImplClass, typename RetTy=void, typename... ParamTys>
193  : public StmtVisitorBase<make_const_ptr, ImplClass, RetTy, ParamTys...> {};
194 
195 /// \brief This class implements a simple visitor for OMPClause
196 /// subclasses.
197 template<class ImplClass, template <typename> class Ptr, typename RetTy>
199 public:
200 #define PTR(CLASS) typename Ptr<CLASS>::type
201 #define DISPATCH(CLASS) \
202  return static_cast<ImplClass*>(this)->Visit##CLASS(static_cast<PTR(CLASS)>(S))
203 
204 #define OPENMP_CLAUSE(Name, Class) \
205  RetTy Visit ## Class (PTR(Class) S) { DISPATCH(Class); }
206 #include "clang/Basic/OpenMPKinds.def"
207 
208  RetTy Visit(PTR(OMPClause) S) {
209  // Top switch clause: visit each OMPClause.
210  switch (S->getClauseKind()) {
211  default: llvm_unreachable("Unknown clause kind!");
212 #define OPENMP_CLAUSE(Name, Class) \
213  case OMPC_ ## Name : return Visit ## Class(static_cast<PTR(Class)>(S));
214 #include "clang/Basic/OpenMPKinds.def"
215  }
216  }
217  // Base case, ignore it. :)
218  RetTy VisitOMPClause(PTR(OMPClause) Node) { return RetTy(); }
219 #undef PTR
220 #undef DISPATCH
221 };
222 
223 template<class ImplClass, typename RetTy = void>
225  public OMPClauseVisitorBase <ImplClass, make_ptr, RetTy> {};
226 template<class ImplClass, typename RetTy = void>
228  public OMPClauseVisitorBase <ImplClass, make_const_ptr, RetTy> {};
229 
230 } // namespace clang
231 
232 #endif // LLVM_CLANG_AST_STMTVISITOR_H
#define CAO_FALLBACK(NAME)
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:192
Stmt - This represents one statement.
Definition: Stmt.h:66
This class implements a simple visitor for OMPClause subclasses.
Definition: StmtVisitor.h:198
StringRef P
Defines the Objective-C statement AST node classes.
Defines the clang::Expr interface and subclasses for C++ expressions.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2985
#define PTR(CLASS)
Definition: StmtVisitor.h:200
#define DISPATCH(NAME, CLASS)
Definition: StmtVisitor.h:201
RetTy Visit(PTR(OMPClause) S)
Definition: StmtVisitor.h:208
OpenMPClauseKind getClauseKind() const
Returns kind of OpenMP clause (private, shared, reduction, etc.).
Definition: OpenMPClause.h:79
const FunctionProtoType * T
UnaryOperator - This represents the unary-expression&#39;s (except sizeof and alignof), the postinc/postdec operators from postfix-expression, and various extensions.
Definition: Expr.h:1717
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:183
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:51
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:3191
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
StmtClass getStmtClass() const
Definition: Stmt.h:378
RetTy VisitOMPClause(PTR(OMPClause) Node)
Definition: StmtVisitor.h:218
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:39
This file defines OpenMP AST classes for executable directives and clauses.
#define UNARYOP_FALLBACK(NAME)
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:46
#define BINOP_FALLBACK(NAME)
Definition: StmtVisitor.h:124