clang  14.0.0git
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/ExprConcepts.h"
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/ADT/STLExtras.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include <utility>
29 
30 namespace clang {
31 /// StmtVisitorBase - This class implements a simple visitor for Stmt
32 /// subclasses. Since Expr derives from Stmt, this also includes support for
33 /// visiting Exprs.
34 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void,
35  class... ParamTys>
37 public:
38 #define PTR(CLASS) typename Ptr<CLASS>::type
39 #define DISPATCH(NAME, CLASS) \
40  return static_cast<ImplClass*>(this)->Visit ## NAME( \
41  static_cast<PTR(CLASS)>(S), std::forward<ParamTys>(P)...)
42 
43  RetTy Visit(PTR(Stmt) S, ParamTys... P) {
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  case BO_Cmp: DISPATCH(BinCmp, BinaryOperator);
66 
67  case BO_And: DISPATCH(BinAnd, BinaryOperator);
68  case BO_Xor: DISPATCH(BinXor, BinaryOperator);
69  case BO_Or : DISPATCH(BinOr, BinaryOperator);
70  case BO_LAnd: DISPATCH(BinLAnd, BinaryOperator);
71  case BO_LOr : DISPATCH(BinLOr, BinaryOperator);
72  case BO_Assign: DISPATCH(BinAssign, BinaryOperator);
73  case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator);
74  case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator);
75  case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator);
76  case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator);
77  case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator);
78  case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator);
79  case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator);
80  case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator);
81  case BO_OrAssign: DISPATCH(BinOrAssign, CompoundAssignOperator);
82  case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator);
83  case BO_Comma: DISPATCH(BinComma, BinaryOperator);
84  }
85  } else if (PTR(UnaryOperator) UnOp = dyn_cast<UnaryOperator>(S)) {
86  switch (UnOp->getOpcode()) {
87  case UO_PostInc: DISPATCH(UnaryPostInc, UnaryOperator);
88  case UO_PostDec: DISPATCH(UnaryPostDec, UnaryOperator);
89  case UO_PreInc: DISPATCH(UnaryPreInc, UnaryOperator);
90  case UO_PreDec: DISPATCH(UnaryPreDec, UnaryOperator);
91  case UO_AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator);
92  case UO_Deref: DISPATCH(UnaryDeref, UnaryOperator);
93  case UO_Plus: DISPATCH(UnaryPlus, UnaryOperator);
94  case UO_Minus: DISPATCH(UnaryMinus, UnaryOperator);
95  case UO_Not: DISPATCH(UnaryNot, UnaryOperator);
96  case UO_LNot: DISPATCH(UnaryLNot, UnaryOperator);
97  case UO_Real: DISPATCH(UnaryReal, UnaryOperator);
98  case UO_Imag: DISPATCH(UnaryImag, UnaryOperator);
99  case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator);
100  case UO_Coawait: DISPATCH(UnaryCoawait, UnaryOperator);
101  }
102  }
103 
104  // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
105  switch (S->getStmtClass()) {
106  default: llvm_unreachable("Unknown stmt kind!");
107 #define ABSTRACT_STMT(STMT)
108 #define STMT(CLASS, PARENT) \
109  case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS);
110 #include "clang/AST/StmtNodes.inc"
111  }
112  }
113 
114  // If the implementation chooses not to implement a certain visit method, fall
115  // back on VisitExpr or whatever else is the superclass.
116 #define STMT(CLASS, PARENT) \
117  RetTy Visit ## CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); }
118 #include "clang/AST/StmtNodes.inc"
119 
120  // If the implementation doesn't implement binary operator methods, fall back
121  // on VisitBinaryOperator.
122 #define BINOP_FALLBACK(NAME) \
123  RetTy VisitBin ## NAME(PTR(BinaryOperator) S, ParamTys... P) { \
124  DISPATCH(BinaryOperator, BinaryOperator); \
125  }
126  BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI)
130 
133  BINOP_FALLBACK(Cmp)
134 
136  BINOP_FALLBACK(LAnd) BINOP_FALLBACK(LOr)
137 
138  BINOP_FALLBACK(Assign)
139  BINOP_FALLBACK(Comma)
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<std::add_pointer, ImplClass, RetTy, ParamTys...> {
185 };
186 
187 /// ConstStmtVisitor - This class implements a simple visitor for Stmt
188 /// subclasses. Since Expr derives from Stmt, this also includes support for
189 /// visiting Exprs.
190 ///
191 /// This class preserves constness of Stmt pointers (see also StmtVisitor).
192 template <typename ImplClass, typename RetTy = void, typename... ParamTys>
193 class ConstStmtVisitor : public StmtVisitorBase<llvm::make_const_ptr, ImplClass,
194  RetTy, ParamTys...> {};
195 
196 } // namespace clang
197 
198 #endif // LLVM_CLANG_AST_STMTVISITOR_H
clang::StmtVisitor
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:183
clang::interp::Shl
bool Shl(InterpState &S, CodePtr OpPC)
Definition: Interp.h:913
clang::interp::Add
bool Add(InterpState &S, CodePtr OpPC)
Definition: Interp.h:134
LT
ASTImporterLookupTable & LT
Definition: ASTImporterLookupTable.cpp:24
clang::UnaryOperator
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2157
ExprOpenMP.h
clang::interp::EQ
bool EQ(InterpState &S, CodePtr OpPC)
Definition: Interp.h:218
ExprConcepts.h
clang::interp::Mul
bool Mul(InterpState &S, CodePtr OpPC)
Definition: Interp.h:150
StmtOpenMP.h
Node
DynTypedNode Node
Definition: ASTMatchFinder.cpp:67
clang::BinaryOperator
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3803
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:239
clang::StmtVisitorBase::Visit
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:43
clang::StmtVisitorBase
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:36
ExprObjC.h
ExprCXX.h
StmtObjC.h
PTR
#define PTR(CLASS)
Definition: StmtVisitor.h:38
clang::prec::And
@ And
Definition: OperatorPrecedence.h:35
clang::StmtVisitorBase::BINOP_FALLBACK
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
clang::StmtVisitorBase::P
ParamTys P
Definition: StmtVisitor.h:171
LLVM.h
clang::CompoundAssignOperator
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4050
clang::ConstStmtVisitor
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:193
UNARYOP_FALLBACK
#define UNARYOP_FALLBACK(NAME)
clang::interp::GT
bool GT(InterpState &S, CodePtr OpPC)
Definition: Interp.h:247
clang::interp::NE
bool NE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:225
clang
Definition: CalledOnceCheck.h:17
clang::interp::GE
bool GE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:254
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
CAO_FALLBACK
#define CAO_FALLBACK(NAME)
DISPATCH
#define DISPATCH(NAME, CLASS)
Definition: StmtVisitor.h:39
Stmt.h
clang::interp::Shr
bool Shr(InterpState &S, CodePtr OpPC)
Definition: Interp.h:898
clang::interp::Sub
bool Sub(InterpState &S, CodePtr OpPC)
Definition: Interp.h:142
StmtCXX.h