clang 20.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/ExprCXX.h"
18#include "clang/AST/ExprObjC.h"
20#include "clang/AST/Stmt.h"
21#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtObjC.h"
25#include "clang/Basic/LLVM.h"
26#include "llvm/ADT/STLExtras.h"
27#include "llvm/Support/Casting.h"
28#include "llvm/Support/ErrorHandling.h"
29#include <utility>
30
31namespace clang {
32/// StmtVisitorBase - This class implements a simple visitor for Stmt
33/// subclasses. Since Expr derives from Stmt, this also includes support for
34/// visiting Exprs.
35template<template <typename> class Ptr, typename ImplClass, typename RetTy=void,
36 class... ParamTys>
38public:
39#define PTR(CLASS) typename Ptr<CLASS>::type
40#define DISPATCH(NAME, CLASS) \
41 return static_cast<ImplClass*>(this)->Visit ## NAME( \
42 static_cast<PTR(CLASS)>(S), std::forward<ParamTys>(P)...)
43
44 RetTy Visit(PTR(Stmt) S, ParamTys... P) {
45 // If we have a binary expr, dispatch to the subcode of the binop. A smart
46 // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
47 // below.
48 if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) {
49 switch (BinOp->getOpcode()) {
50 case BO_PtrMemD: DISPATCH(BinPtrMemD, BinaryOperator);
51 case BO_PtrMemI: DISPATCH(BinPtrMemI, BinaryOperator);
52 case BO_Mul: DISPATCH(BinMul, BinaryOperator);
53 case BO_Div: DISPATCH(BinDiv, BinaryOperator);
54 case BO_Rem: DISPATCH(BinRem, BinaryOperator);
55 case BO_Add: DISPATCH(BinAdd, BinaryOperator);
56 case BO_Sub: DISPATCH(BinSub, BinaryOperator);
57 case BO_Shl: DISPATCH(BinShl, BinaryOperator);
58 case BO_Shr: DISPATCH(BinShr, BinaryOperator);
59
60 case BO_LT: DISPATCH(BinLT, BinaryOperator);
61 case BO_GT: DISPATCH(BinGT, BinaryOperator);
62 case BO_LE: DISPATCH(BinLE, BinaryOperator);
63 case BO_GE: DISPATCH(BinGE, BinaryOperator);
64 case BO_EQ: DISPATCH(BinEQ, BinaryOperator);
65 case BO_NE: DISPATCH(BinNE, BinaryOperator);
66 case BO_Cmp: DISPATCH(BinCmp, BinaryOperator);
67
68 case BO_And: DISPATCH(BinAnd, BinaryOperator);
69 case BO_Xor: DISPATCH(BinXor, BinaryOperator);
70 case BO_Or : DISPATCH(BinOr, BinaryOperator);
71 case BO_LAnd: DISPATCH(BinLAnd, BinaryOperator);
72 case BO_LOr : DISPATCH(BinLOr, BinaryOperator);
73 case BO_Assign: DISPATCH(BinAssign, BinaryOperator);
74 case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator);
75 case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator);
76 case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator);
77 case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator);
78 case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator);
79 case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator);
80 case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator);
81 case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator);
82 case BO_OrAssign: DISPATCH(BinOrAssign, CompoundAssignOperator);
83 case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator);
84 case BO_Comma: DISPATCH(BinComma, BinaryOperator);
85 }
86 } else if (PTR(UnaryOperator) UnOp = dyn_cast<UnaryOperator>(S)) {
87 switch (UnOp->getOpcode()) {
88 case UO_PostInc: DISPATCH(UnaryPostInc, UnaryOperator);
89 case UO_PostDec: DISPATCH(UnaryPostDec, UnaryOperator);
90 case UO_PreInc: DISPATCH(UnaryPreInc, UnaryOperator);
91 case UO_PreDec: DISPATCH(UnaryPreDec, UnaryOperator);
92 case UO_AddrOf: DISPATCH(UnaryAddrOf, UnaryOperator);
93 case UO_Deref: DISPATCH(UnaryDeref, UnaryOperator);
94 case UO_Plus: DISPATCH(UnaryPlus, UnaryOperator);
95 case UO_Minus: DISPATCH(UnaryMinus, UnaryOperator);
96 case UO_Not: DISPATCH(UnaryNot, UnaryOperator);
97 case UO_LNot: DISPATCH(UnaryLNot, UnaryOperator);
98 case UO_Real: DISPATCH(UnaryReal, UnaryOperator);
99 case UO_Imag: DISPATCH(UnaryImag, UnaryOperator);
100 case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator);
101 case UO_Coawait: DISPATCH(UnaryCoawait, UnaryOperator);
102 }
103 }
104
105 // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
106 switch (S->getStmtClass()) {
107 default: llvm_unreachable("Unknown stmt kind!");
108#define ABSTRACT_STMT(STMT)
109#define STMT(CLASS, PARENT) \
110 case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS);
111#include "clang/AST/StmtNodes.inc"
112 }
113 }
114
115 // If the implementation chooses not to implement a certain visit method, fall
116 // back on VisitExpr or whatever else is the superclass.
117#define STMT(CLASS, PARENT) \
118 RetTy Visit ## CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); }
119#include "clang/AST/StmtNodes.inc"
120
121 // If the implementation doesn't implement binary operator methods, fall back
122 // on VisitBinaryOperator.
123#define BINOP_FALLBACK(NAME) \
124 RetTy VisitBin ## NAME(PTR(BinaryOperator) S, ParamTys... P) { \
125 DISPATCH(BinaryOperator, BinaryOperator); \
126 }
130 BINOP_FALLBACK(Shr)
131
134 BINOP_FALLBACK(Cmp)
135
138
139 BINOP_FALLBACK(Assign)
140 BINOP_FALLBACK(Comma)
141#undef BINOP_FALLBACK
142
143 // If the implementation doesn't implement compound assignment operator
144 // methods, fall back on VisitCompoundAssignOperator.
145#define CAO_FALLBACK(NAME) \
146 RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
147 DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
148 }
149 CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
150 CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
151 CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
152 CAO_FALLBACK(XorAssign)
153#undef CAO_FALLBACK
154
155 // If the implementation doesn't implement unary operator methods, fall back
156 // on VisitUnaryOperator.
157#define UNARYOP_FALLBACK(NAME) \
158 RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
159 DISPATCH(UnaryOperator, UnaryOperator); \
160 }
161 UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
162 UNARYOP_FALLBACK(PreInc) UNARYOP_FALLBACK(PreDec)
164
168 UNARYOP_FALLBACK(Extension) UNARYOP_FALLBACK(Coawait)
169#undef UNARYOP_FALLBACK
170
171 // Base case, ignore it. :)
172 RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }
173
174#undef PTR
175#undef DISPATCH
176};
177
178/// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
179/// Since Expr derives from Stmt, this also includes support for visiting Exprs.
180///
181/// This class does not preserve constness of Stmt pointers (see also
182/// ConstStmtVisitor).
183template <typename ImplClass, typename RetTy = void, typename... ParamTys>
185 : public StmtVisitorBase<std::add_pointer, ImplClass, RetTy, ParamTys...> {
186};
187
188/// ConstStmtVisitor - This class implements a simple visitor for Stmt
189/// subclasses. Since Expr derives from Stmt, this also includes support for
190/// visiting Exprs.
191///
192/// This class preserves constness of Stmt pointers (see also StmtVisitor).
193template <typename ImplClass, typename RetTy = void, typename... ParamTys>
194class ConstStmtVisitor : public StmtVisitorBase<llvm::make_const_ptr, ImplClass,
195 RetTy, ParamTys...> {};
196
197} // namespace clang
198
199#endif // LLVM_CLANG_AST_STMTVISITOR_H
ASTImporterLookupTable & LT
DynTypedNode Node
#define DISPATCH(NAME)
Definition: AttrVisitor.h:28
#define PTR(CLASS)
Definition: AttrVisitor.h:27
Defines the clang::Expr interface and subclasses for C++ expressions.
Defines Expressions and AST nodes for C++2a concepts.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the Objective-C statement AST node classes.
This file defines OpenACC AST classes for statement-level contructs.
This file defines OpenMP AST classes for executable directives and clauses.
#define UNARYOP_FALLBACK(NAME)
#define BINOP_FALLBACK(NAME)
Definition: StmtVisitor.h:123
#define CAO_FALLBACK(NAME)
A builtin binary operation expression such as "x + y" or "x <= y".
Definition: Expr.h:3860
CompoundAssignOperator - For compound assignments (e.g.
Definition: Expr.h:4122
ConstStmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:195
StmtVisitorBase - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:37
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
RetTy Visit(PTR(Stmt) S, ParamTys... P)
Definition: StmtVisitor.h:44
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:185
Stmt - This represents one statement.
Definition: Stmt.h:84
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition: Expr.h:2188
The JSON file list parser is used to communicate input to InstallAPI.