StmtVisitor.h revision 212904
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 interface.
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/StmtCXX.h"
20#include "clang/AST/StmtObjC.h"
21
22namespace clang {
23
24#define DISPATCH(NAME, CLASS) \
25  return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<CLASS*>(S))
26
27/// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
28/// Since Expr derives from Stmt, this also includes support for visiting Exprs.
29template<typename ImplClass, typename RetTy=void>
30class StmtVisitor {
31public:
32  RetTy Visit(Stmt *S) {
33
34    // If we have a binary expr, dispatch to the subcode of the binop.  A smart
35    // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
36    // below.
37    if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) {
38      switch (BinOp->getOpcode()) {
39      default: assert(0 && "Unknown binary operator!");
40      case BO_PtrMemD:   DISPATCH(BinPtrMemD,   BinaryOperator);
41      case BO_PtrMemI:   DISPATCH(BinPtrMemI,   BinaryOperator);
42      case BO_Mul:       DISPATCH(BinMul,       BinaryOperator);
43      case BO_Div:       DISPATCH(BinDiv,       BinaryOperator);
44      case BO_Rem:       DISPATCH(BinRem,       BinaryOperator);
45      case BO_Add:       DISPATCH(BinAdd,       BinaryOperator);
46      case BO_Sub:       DISPATCH(BinSub,       BinaryOperator);
47      case BO_Shl:       DISPATCH(BinShl,       BinaryOperator);
48      case BO_Shr:       DISPATCH(BinShr,       BinaryOperator);
49
50      case BO_LT:        DISPATCH(BinLT,        BinaryOperator);
51      case BO_GT:        DISPATCH(BinGT,        BinaryOperator);
52      case BO_LE:        DISPATCH(BinLE,        BinaryOperator);
53      case BO_GE:        DISPATCH(BinGE,        BinaryOperator);
54      case BO_EQ:        DISPATCH(BinEQ,        BinaryOperator);
55      case BO_NE:        DISPATCH(BinNE,        BinaryOperator);
56
57      case BO_And:       DISPATCH(BinAnd,       BinaryOperator);
58      case BO_Xor:       DISPATCH(BinXor,       BinaryOperator);
59      case BO_Or :       DISPATCH(BinOr,        BinaryOperator);
60      case BO_LAnd:      DISPATCH(BinLAnd,      BinaryOperator);
61      case BO_LOr :      DISPATCH(BinLOr,       BinaryOperator);
62      case BO_Assign:    DISPATCH(BinAssign,    BinaryOperator);
63      case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator);
64      case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator);
65      case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator);
66      case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator);
67      case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator);
68      case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator);
69      case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator);
70      case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator);
71      case BO_OrAssign:  DISPATCH(BinOrAssign,  CompoundAssignOperator);
72      case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator);
73      case BO_Comma:     DISPATCH(BinComma,     BinaryOperator);
74      }
75    } else if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(S)) {
76      switch (UnOp->getOpcode()) {
77      default: assert(0 && "Unknown unary operator!");
78      case UO_PostInc:   DISPATCH(UnaryPostInc,   UnaryOperator);
79      case UO_PostDec:   DISPATCH(UnaryPostDec,   UnaryOperator);
80      case UO_PreInc:    DISPATCH(UnaryPreInc,    UnaryOperator);
81      case UO_PreDec:    DISPATCH(UnaryPreDec,    UnaryOperator);
82      case UO_AddrOf:    DISPATCH(UnaryAddrOf,    UnaryOperator);
83      case UO_Deref:     DISPATCH(UnaryDeref,     UnaryOperator);
84      case UO_Plus:      DISPATCH(UnaryPlus,      UnaryOperator);
85      case UO_Minus:     DISPATCH(UnaryMinus,     UnaryOperator);
86      case UO_Not:       DISPATCH(UnaryNot,       UnaryOperator);
87      case UO_LNot:      DISPATCH(UnaryLNot,      UnaryOperator);
88      case UO_Real:      DISPATCH(UnaryReal,      UnaryOperator);
89      case UO_Imag:      DISPATCH(UnaryImag,      UnaryOperator);
90      case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator);
91      }
92    }
93
94    // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
95    switch (S->getStmtClass()) {
96    default: assert(0 && "Unknown stmt kind!");
97#define ABSTRACT_STMT(STMT)
98#define STMT(CLASS, PARENT)                              \
99    case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS);
100#include "clang/AST/StmtNodes.inc"
101    }
102  }
103
104  // If the implementation chooses not to implement a certain visit method, fall
105  // back on VisitExpr or whatever else is the superclass.
106#define STMT(CLASS, PARENT)                                   \
107  RetTy Visit ## CLASS(CLASS *S) { DISPATCH(PARENT, PARENT); }
108#include "clang/AST/StmtNodes.inc"
109
110  // If the implementation doesn't implement binary operator methods, fall back
111  // on VisitBinaryOperator.
112#define BINOP_FALLBACK(NAME) \
113  RetTy VisitBin ## NAME(BinaryOperator *S) { \
114    DISPATCH(BinaryOperator, BinaryOperator); \
115  }
116  BINOP_FALLBACK(PtrMemD)                    BINOP_FALLBACK(PtrMemI)
117  BINOP_FALLBACK(Mul)   BINOP_FALLBACK(Div)  BINOP_FALLBACK(Rem)
118  BINOP_FALLBACK(Add)   BINOP_FALLBACK(Sub)  BINOP_FALLBACK(Shl)
119  BINOP_FALLBACK(Shr)
120
121  BINOP_FALLBACK(LT)    BINOP_FALLBACK(GT)   BINOP_FALLBACK(LE)
122  BINOP_FALLBACK(GE)    BINOP_FALLBACK(EQ)   BINOP_FALLBACK(NE)
123  BINOP_FALLBACK(And)   BINOP_FALLBACK(Xor)  BINOP_FALLBACK(Or)
124  BINOP_FALLBACK(LAnd)  BINOP_FALLBACK(LOr)
125
126  BINOP_FALLBACK(Assign)
127  BINOP_FALLBACK(Comma)
128#undef BINOP_FALLBACK
129
130  // If the implementation doesn't implement compound assignment operator
131  // methods, fall back on VisitCompoundAssignOperator.
132#define CAO_FALLBACK(NAME) \
133  RetTy VisitBin ## NAME(CompoundAssignOperator *S) { \
134    DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
135  }
136  CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
137  CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
138  CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
139  CAO_FALLBACK(XorAssign)
140#undef CAO_FALLBACK
141
142  // If the implementation doesn't implement unary operator methods, fall back
143  // on VisitUnaryOperator.
144#define UNARYOP_FALLBACK(NAME) \
145  RetTy VisitUnary ## NAME(UnaryOperator *S) { \
146    DISPATCH(UnaryOperator, UnaryOperator);    \
147  }
148  UNARYOP_FALLBACK(PostInc)   UNARYOP_FALLBACK(PostDec)
149  UNARYOP_FALLBACK(PreInc)    UNARYOP_FALLBACK(PreDec)
150  UNARYOP_FALLBACK(AddrOf)    UNARYOP_FALLBACK(Deref)
151
152  UNARYOP_FALLBACK(Plus)      UNARYOP_FALLBACK(Minus)
153  UNARYOP_FALLBACK(Not)       UNARYOP_FALLBACK(LNot)
154  UNARYOP_FALLBACK(Real)      UNARYOP_FALLBACK(Imag)
155  UNARYOP_FALLBACK(Extension)
156#undef UNARYOP_FALLBACK
157
158  // Base case, ignore it. :)
159  RetTy VisitStmt(Stmt *Node) { return RetTy(); }
160};
161
162#undef DISPATCH
163
164}  // end namespace clang
165
166#endif
167