clang  9.0.0svn
StmtVisitor.h
Go to the documentation of this file.
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/ExprObjC.h"
18 #include "clang/AST/ExprOpenMP.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/StmtCXX.h"
21 #include "clang/AST/StmtObjC.h"
22 #include "clang/AST/StmtOpenMP.h"
23 #include "clang/Basic/LLVM.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include <utility>
28 
29 namespace clang {
30 /// StmtVisitorBase - This class implements a simple visitor for Stmt
31 /// subclasses. Since Expr derives from Stmt, this also includes support for
32 /// visiting Exprs.
33 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void,
34  class... ParamTys>
36 public:
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  // If we have a binary expr, dispatch to the subcode of the binop. A smart
44  // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
45  // below.
46  if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) {
47  switch (BinOp->getOpcode()) {
48  case BO_PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator);
49  case BO_PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator);
50  case BO_Mul: DISPATCH(BinMul, BinaryOperator);
51  case BO_Div: DISPATCH(BinDiv, BinaryOperator);
52  case BO_Rem: DISPATCH(BinRem, BinaryOperator);
53  case BO_Add: DISPATCH(BinAdd, BinaryOperator);
54  case BO_Sub: DISPATCH(BinSub, BinaryOperator);
55  case BO_Shl: DISPATCH(BinShl, BinaryOperator);
56  case BO_Shr: DISPATCH(BinShr, BinaryOperator);
57 
58  case BO_LT: DISPATCH(BinLT, BinaryOperator);
59  case BO_GT: DISPATCH(BinGT, BinaryOperator);
60  case BO_LE: DISPATCH(BinLE, BinaryOperator);
61  case BO_GE: DISPATCH(BinGE, BinaryOperator);
62  case BO_EQ: DISPATCH(BinEQ, BinaryOperator);
63  case BO_NE: DISPATCH(BinNE, BinaryOperator);
64  case BO_Cmp: DISPATCH(BinCmp, 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 
132  BINOP_FALLBACK(Cmp)
133 
135  BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr)
136 
137  BINOP_FALLBACK(Assign)
139 #undef BINOP_FALLBACK
140 
141  // If the implementation doesn't implement compound assignment operator
142  // methods, fall back on VisitCompoundAssignOperator.
143 #define CAO_FALLBACK(NAME) \
144  RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
145  DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
146  }
147  CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
148  CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
149  CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
150  CAO_FALLBACK(XorAssign)
151 #undef CAO_FALLBACK
152 
153  // If the implementation doesn't implement unary operator methods, fall back
154  // on VisitUnaryOperator.
155 #define UNARYOP_FALLBACK(NAME) \
156  RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
157  DISPATCH(UnaryOperator, UnaryOperator); \
158  }
159  UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
160  UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec)
161  UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref)
162 
166  UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait)
167 #undef UNARYOP_FALLBACK
168 
169  // Base case, ignore it. :)
170  RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }
171 
172 #undef PTR
173 #undef DISPATCH
174 };
175 
176 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
177 /// Since Expr derives from Stmt, this also includes support for visiting Exprs.
178 ///
179 /// This class does not preserve constness of Stmt pointers (see also
180 /// ConstStmtVisitor).
181 template <typename ImplClass, typename RetTy = void, typename... ParamTys>
183  : public StmtVisitorBase<std::add_pointer, ImplClass, RetTy, ParamTys...> {
184 };
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>
192 class ConstStmtVisitor : public StmtVisitorBase<llvm::make_const_ptr, ImplClass,
193  RetTy, ParamTys...> {};
194 
195 } // namespace clang
196 
197 #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:65
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:3332
#define PTR(CLASS)
Definition: StmtVisitor.h:37
#define DISPATCH(NAME, CLASS)
Definition: StmtVisitor.h:38
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:1941
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:182
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:3552
ast_type_traits::DynTypedNode Node
Dataflow Directional Tag Classes.
StmtClass getStmtClass() const
Definition: Stmt.h:1059
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:35
This file defines OpenMP AST classes for executable directives and clauses.
#define UNARYOP_FALLBACK(NAME)
BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI) BINOP_FALLBACK(Mul) BINOP_FALLBACK(Div) BINOP_FALLBACK(Rem) BINOP_FALLBACK(Add) BINOP_FALLBACK(Sub) BINOP_FALLBACK(Shl) BINOP_FALLBACK(LT) BINOP_FALLBACK(GT) BINOP_FALLBACK(LE) BINOP_FALLBACK(GE) BINOP_FALLBACK(EQ) BINOP_FALLBACK(NE) BINOP_FALLBACK(And) BINOP_FALLBACK(Xor) BINOP_FALLBACK(Or) BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr) CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign) CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign) CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign) UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec) UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec) UNARYOP_FALLBACK(AddrOf) UNARYOP_FALLBACK(Deref) UNARYOP_FALLBACK(Plus) UNARYOP_FALLBACK(Minus) UNARYOP_FALLBACK(Not) UNARYOP_FALLBACK(LNot) UNARYOP_FALLBACK(Real) UNARYOP_FALLBACK(Imag) UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait) RetTy VisitStmt(PTR(Stmt) Node
ASTImporterLookupTable & LT
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:42