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/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... ParamTys>
35 public:
36 
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  // If we have a binary expr, dispatch to the subcode of the binop. A smart
45  // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
46  // below.
47  if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) {
48  switch (BinOp->getOpcode()) {
49  case BO_PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator);
50  case BO_PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator);
51  case BO_Mul: DISPATCH(BinMul, BinaryOperator);
52  case BO_Div: DISPATCH(BinDiv, BinaryOperator);
53  case BO_Rem: DISPATCH(BinRem, BinaryOperator);
54  case BO_Add: DISPATCH(BinAdd, BinaryOperator);
55  case BO_Sub: DISPATCH(BinSub, BinaryOperator);
56  case BO_Shl: DISPATCH(BinShl, BinaryOperator);
57  case BO_Shr: DISPATCH(BinShr, BinaryOperator);
58 
59  case BO_LT: DISPATCH(BinLT, BinaryOperator);
60  case BO_GT: DISPATCH(BinGT, BinaryOperator);
61  case BO_LE: DISPATCH(BinLE, BinaryOperator);
62  case BO_GE: DISPATCH(BinGE, BinaryOperator);
63  case BO_EQ: DISPATCH(BinEQ, BinaryOperator);
64  case BO_NE: DISPATCH(BinNE, 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  // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
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  // If the implementation chooses not to implement a certain visit method, fall
114  // back on VisitExpr or whatever else is the superclass.
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  // If the implementation doesn't implement binary operator methods, fall back
120  // on VisitBinaryOperator.
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)
128  BINOP_FALLBACK(Shr)
129 
133  BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr)
134 
135  BINOP_FALLBACK(Assign)
137 #undef BINOP_FALLBACK
138 
139  // If the implementation doesn't implement compound assignment operator
140  // methods, fall back on VisitCompoundAssignOperator.
141 #define CAO_FALLBACK(NAME) \
142  RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
143  DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
144  }
145  CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
146  CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
147  CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
148  CAO_FALLBACK(XorAssign)
149 #undef CAO_FALLBACK
150 
151  // If the implementation doesn't implement unary operator methods, fall back
152  // on VisitUnaryOperator.
153 #define UNARYOP_FALLBACK(NAME) \
154  RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
155  DISPATCH(UnaryOperator, UnaryOperator); \
156  }
157  UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
158  UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec)
159  UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref)
160 
164  UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait)
165 #undef UNARYOP_FALLBACK
166 
167  // Base case, ignore it. :)
168  RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }
169 
170 #undef PTR
171 #undef DISPATCH
172 };
173 
174 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
175 /// Since Expr derives from Stmt, this also includes support for visiting Exprs.
176 ///
177 /// This class does not preserve constness of Stmt pointers (see also
178 /// ConstStmtVisitor).
179 template<typename ImplClass, typename RetTy=void, typename... ParamTys>
181  : public StmtVisitorBase<make_ptr, ImplClass, RetTy, ParamTys...> {};
182 
183 /// ConstStmtVisitor - This class implements a simple visitor for Stmt
184 /// subclasses. Since Expr derives from Stmt, this also includes support for
185 /// visiting Exprs.
186 ///
187 /// This class preserves constness of Stmt pointers (see also StmtVisitor).
188 template<typename ImplClass, typename RetTy=void, typename... ParamTys>
190  : public StmtVisitorBase<make_const_ptr, ImplClass, RetTy, ParamTys...> {};
191 
192 /// \brief This class implements a simple visitor for OMPClause
193 /// subclasses.
194 template<class ImplClass, template <typename> class Ptr, typename RetTy>
196 public:
197 #define PTR(CLASS) typename Ptr<CLASS>::type
198 #define DISPATCH(CLASS) \
199  return static_cast<ImplClass*>(this)->Visit##CLASS(static_cast<PTR(CLASS)>(S))
200 
201 #define OPENMP_CLAUSE(Name, Class) \
202  RetTy Visit ## Class (PTR(Class) S) { DISPATCH(Class); }
203 #include "clang/Basic/OpenMPKinds.def"
204 
205  RetTy Visit(PTR(OMPClause) S) {
206  // Top switch clause: visit each OMPClause.
207  switch (S->getClauseKind()) {
208  default: llvm_unreachable("Unknown clause kind!");
209 #define OPENMP_CLAUSE(Name, Class) \
210  case OMPC_ ## Name : return Visit ## Class(static_cast<PTR(Class)>(S));
211 #include "clang/Basic/OpenMPKinds.def"
212  }
213  }
214  // Base case, ignore it. :)
215  RetTy VisitOMPClause(PTR(OMPClause) Node) { return RetTy(); }
216 #undef PTR
217 #undef DISPATCH
218 };
219 
220 template<class ImplClass, typename RetTy = void>
222  public OMPClauseVisitorBase <ImplClass, make_ptr, RetTy> {};
223 template<class ImplClass, typename RetTy = void>
225  public OMPClauseVisitorBase <ImplClass, make_const_ptr, RetTy> {};
226 
227 } // end namespace clang
228 
229 #endif
#define CAO_FALLBACK(NAME)
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:189
Stmt - This represents one statement.
Definition: Stmt.h:60
This class implements a simple visitor for OMPClause subclasses.
Definition: StmtVisitor.h:195
StringRef P
Defines the Objective-C statement AST node classes.
Defines the clang::Expr interface and subclasses for C++ expressions.
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:2986
#define PTR(CLASS)
Definition: StmtVisitor.h:197
#define DISPATCH(NAME, CLASS)
Definition: StmtVisitor.h:198
RetTy Visit(PTR(OMPClause) S)
Definition: StmtVisitor.h:205
OpenMPClauseKind getClauseKind() const
Returns kind of OpenMP clause (private, shared, reduction, etc.).
Definition: OpenMPClause.h:57
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:180
This is a basic class for representing single OpenMP clause.
Definition: OpenMPClause.h:33
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:3192
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
StmtClass getStmtClass() const
Definition: Stmt.h:361
RetTy VisitOMPClause(PTR(OMPClause) Node)
Definition: StmtVisitor.h:215
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:34
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:42
#define BINOP_FALLBACK(NAME)
Definition: StmtVisitor.h:121