clang API Documentation

StmtVisitor.h
Go to the documentation of this file.
00001 //===--- StmtVisitor.h - Visitor for Stmt subclasses ------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the StmtVisitor and ConstStmtVisitor interfaces.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CLANG_AST_STMTVISITOR_H
00015 #define LLVM_CLANG_AST_STMTVISITOR_H
00016 
00017 #include "clang/AST/ExprCXX.h"
00018 #include "clang/AST/ExprObjC.h"
00019 #include "clang/AST/StmtCXX.h"
00020 #include "clang/AST/StmtObjC.h"
00021 
00022 namespace clang {
00023 
00024 template <typename T> struct make_ptr       { typedef       T *type; };
00025 template <typename T> struct make_const_ptr { typedef const T *type; };
00026 
00027 /// StmtVisitorBase - This class implements a simple visitor for Stmt
00028 /// subclasses. Since Expr derives from Stmt, this also includes support for
00029 /// visiting Exprs.
00030 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
00031 class StmtVisitorBase {
00032 public:
00033 
00034 #define PTR(CLASS) typename Ptr<CLASS>::type
00035 #define DISPATCH(NAME, CLASS) \
00036  return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<PTR(CLASS)>(S))
00037 
00038   RetTy Visit(PTR(Stmt) S) {
00039 
00040     // If we have a binary expr, dispatch to the subcode of the binop.  A smart
00041     // optimizer (e.g. LLVM) will fold this comparison into the switch stmt
00042     // below.
00043     if (PTR(BinaryOperator) BinOp = dyn_cast<BinaryOperator>(S)) {
00044       switch (BinOp->getOpcode()) {
00045       case BO_PtrMemD:   DISPATCH(BinPtrMemD,   BinaryOperator);
00046       case BO_PtrMemI:   DISPATCH(BinPtrMemI,   BinaryOperator);
00047       case BO_Mul:       DISPATCH(BinMul,       BinaryOperator);
00048       case BO_Div:       DISPATCH(BinDiv,       BinaryOperator);
00049       case BO_Rem:       DISPATCH(BinRem,       BinaryOperator);
00050       case BO_Add:       DISPATCH(BinAdd,       BinaryOperator);
00051       case BO_Sub:       DISPATCH(BinSub,       BinaryOperator);
00052       case BO_Shl:       DISPATCH(BinShl,       BinaryOperator);
00053       case BO_Shr:       DISPATCH(BinShr,       BinaryOperator);
00054 
00055       case BO_LT:        DISPATCH(BinLT,        BinaryOperator);
00056       case BO_GT:        DISPATCH(BinGT,        BinaryOperator);
00057       case BO_LE:        DISPATCH(BinLE,        BinaryOperator);
00058       case BO_GE:        DISPATCH(BinGE,        BinaryOperator);
00059       case BO_EQ:        DISPATCH(BinEQ,        BinaryOperator);
00060       case BO_NE:        DISPATCH(BinNE,        BinaryOperator);
00061 
00062       case BO_And:       DISPATCH(BinAnd,       BinaryOperator);
00063       case BO_Xor:       DISPATCH(BinXor,       BinaryOperator);
00064       case BO_Or :       DISPATCH(BinOr,        BinaryOperator);
00065       case BO_LAnd:      DISPATCH(BinLAnd,      BinaryOperator);
00066       case BO_LOr :      DISPATCH(BinLOr,       BinaryOperator);
00067       case BO_Assign:    DISPATCH(BinAssign,    BinaryOperator);
00068       case BO_MulAssign: DISPATCH(BinMulAssign, CompoundAssignOperator);
00069       case BO_DivAssign: DISPATCH(BinDivAssign, CompoundAssignOperator);
00070       case BO_RemAssign: DISPATCH(BinRemAssign, CompoundAssignOperator);
00071       case BO_AddAssign: DISPATCH(BinAddAssign, CompoundAssignOperator);
00072       case BO_SubAssign: DISPATCH(BinSubAssign, CompoundAssignOperator);
00073       case BO_ShlAssign: DISPATCH(BinShlAssign, CompoundAssignOperator);
00074       case BO_ShrAssign: DISPATCH(BinShrAssign, CompoundAssignOperator);
00075       case BO_AndAssign: DISPATCH(BinAndAssign, CompoundAssignOperator);
00076       case BO_OrAssign:  DISPATCH(BinOrAssign,  CompoundAssignOperator);
00077       case BO_XorAssign: DISPATCH(BinXorAssign, CompoundAssignOperator);
00078       case BO_Comma:     DISPATCH(BinComma,     BinaryOperator);
00079       }
00080     } else if (PTR(UnaryOperator) UnOp = dyn_cast<UnaryOperator>(S)) {
00081       switch (UnOp->getOpcode()) {
00082       case UO_PostInc:   DISPATCH(UnaryPostInc,   UnaryOperator);
00083       case UO_PostDec:   DISPATCH(UnaryPostDec,   UnaryOperator);
00084       case UO_PreInc:    DISPATCH(UnaryPreInc,    UnaryOperator);
00085       case UO_PreDec:    DISPATCH(UnaryPreDec,    UnaryOperator);
00086       case UO_AddrOf:    DISPATCH(UnaryAddrOf,    UnaryOperator);
00087       case UO_Deref:     DISPATCH(UnaryDeref,     UnaryOperator);
00088       case UO_Plus:      DISPATCH(UnaryPlus,      UnaryOperator);
00089       case UO_Minus:     DISPATCH(UnaryMinus,     UnaryOperator);
00090       case UO_Not:       DISPATCH(UnaryNot,       UnaryOperator);
00091       case UO_LNot:      DISPATCH(UnaryLNot,      UnaryOperator);
00092       case UO_Real:      DISPATCH(UnaryReal,      UnaryOperator);
00093       case UO_Imag:      DISPATCH(UnaryImag,      UnaryOperator);
00094       case UO_Extension: DISPATCH(UnaryExtension, UnaryOperator);
00095       }
00096     }
00097 
00098     // Top switch stmt: dispatch to VisitFooStmt for each FooStmt.
00099     switch (S->getStmtClass()) {
00100     default: llvm_unreachable("Unknown stmt kind!");
00101 #define ABSTRACT_STMT(STMT)
00102 #define STMT(CLASS, PARENT)                              \
00103     case Stmt::CLASS ## Class: DISPATCH(CLASS, CLASS);
00104 #include "clang/AST/StmtNodes.inc"
00105     }
00106   }
00107 
00108   // If the implementation chooses not to implement a certain visit method, fall
00109   // back on VisitExpr or whatever else is the superclass.
00110 #define STMT(CLASS, PARENT)                                   \
00111   RetTy Visit ## CLASS(PTR(CLASS) S) { DISPATCH(PARENT, PARENT); }
00112 #include "clang/AST/StmtNodes.inc"
00113 
00114   // If the implementation doesn't implement binary operator methods, fall back
00115   // on VisitBinaryOperator.
00116 #define BINOP_FALLBACK(NAME) \
00117   RetTy VisitBin ## NAME(PTR(BinaryOperator) S) { \
00118     DISPATCH(BinaryOperator, BinaryOperator); \
00119   }
00120   BINOP_FALLBACK(PtrMemD)                    BINOP_FALLBACK(PtrMemI)
00121   BINOP_FALLBACK(Mul)   BINOP_FALLBACK(Div)  BINOP_FALLBACK(Rem)
00122   BINOP_FALLBACK(Add)   BINOP_FALLBACK(Sub)  BINOP_FALLBACK(Shl)
00123   BINOP_FALLBACK(Shr)
00124 
00125   BINOP_FALLBACK(LT)    BINOP_FALLBACK(GT)   BINOP_FALLBACK(LE)
00126   BINOP_FALLBACK(GE)    BINOP_FALLBACK(EQ)   BINOP_FALLBACK(NE)
00127   BINOP_FALLBACK(And)   BINOP_FALLBACK(Xor)  BINOP_FALLBACK(Or)
00128   BINOP_FALLBACK(LAnd)  BINOP_FALLBACK(LOr)
00129 
00130   BINOP_FALLBACK(Assign)
00131   BINOP_FALLBACK(Comma)
00132 #undef BINOP_FALLBACK
00133 
00134   // If the implementation doesn't implement compound assignment operator
00135   // methods, fall back on VisitCompoundAssignOperator.
00136 #define CAO_FALLBACK(NAME) \
00137   RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S) { \
00138     DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
00139   }
00140   CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
00141   CAO_FALLBACK(AddAssign) CAO_FALLBACK(SubAssign) CAO_FALLBACK(ShlAssign)
00142   CAO_FALLBACK(ShrAssign) CAO_FALLBACK(AndAssign) CAO_FALLBACK(OrAssign)
00143   CAO_FALLBACK(XorAssign)
00144 #undef CAO_FALLBACK
00145 
00146   // If the implementation doesn't implement unary operator methods, fall back
00147   // on VisitUnaryOperator.
00148 #define UNARYOP_FALLBACK(NAME) \
00149   RetTy VisitUnary ## NAME(PTR(UnaryOperator) S) { \
00150     DISPATCH(UnaryOperator, UnaryOperator);    \
00151   }
00152   UNARYOP_FALLBACK(PostInc)   UNARYOP_FALLBACK(PostDec)
00153   UNARYOP_FALLBACK(PreInc)    UNARYOP_FALLBACK(PreDec)
00154   UNARYOP_FALLBACK(AddrOf)    UNARYOP_FALLBACK(Deref)
00155 
00156   UNARYOP_FALLBACK(Plus)      UNARYOP_FALLBACK(Minus)
00157   UNARYOP_FALLBACK(Not)       UNARYOP_FALLBACK(LNot)
00158   UNARYOP_FALLBACK(Real)      UNARYOP_FALLBACK(Imag)
00159   UNARYOP_FALLBACK(Extension)
00160 #undef UNARYOP_FALLBACK
00161 
00162   // Base case, ignore it. :)
00163   RetTy VisitStmt(PTR(Stmt) Node) { return RetTy(); }
00164 
00165 #undef PTR
00166 #undef DISPATCH
00167 };
00168 
00169 /// StmtVisitor - This class implements a simple visitor for Stmt subclasses.
00170 /// Since Expr derives from Stmt, this also includes support for visiting Exprs.
00171 ///
00172 /// This class does not preserve constness of Stmt pointers (see also
00173 /// ConstStmtVisitor).
00174 template<typename ImplClass, typename RetTy=void>
00175 class StmtVisitor
00176  : public StmtVisitorBase<make_ptr, ImplClass, RetTy> {};
00177 
00178 /// ConstStmtVisitor - This class implements a simple visitor for Stmt
00179 /// subclasses. Since Expr derives from Stmt, this also includes support for
00180 /// visiting Exprs.
00181 ///
00182 /// This class preserves constness of Stmt pointers (see also StmtVisitor).
00183 template<typename ImplClass, typename RetTy=void>
00184 class ConstStmtVisitor
00185  : public StmtVisitorBase<make_const_ptr, ImplClass, RetTy> {};
00186 
00187 }  // end namespace clang
00188 
00189 #endif