Subversion Repositories QNX 8.QNX8 LLVM/Clang compiler suite

Rev

Blame | Last modification | View Log | Download | RSS feed

  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>
  36. class StmtVisitorBase {
  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)
  127.   BINOP_FALLBACK(Mul)   BINOP_FALLBACK(Div)  BINOP_FALLBACK(Rem)
  128.   BINOP_FALLBACK(Add)   BINOP_FALLBACK(Sub)  BINOP_FALLBACK(Shl)
  129.   BINOP_FALLBACK(Shr)
  130.  
  131.   BINOP_FALLBACK(LT)    BINOP_FALLBACK(GT)   BINOP_FALLBACK(LE)
  132.   BINOP_FALLBACK(GE)    BINOP_FALLBACK(EQ)   BINOP_FALLBACK(NE)
  133.   BINOP_FALLBACK(Cmp)
  134.  
  135.   BINOP_FALLBACK(And)   BINOP_FALLBACK(Xor)  BINOP_FALLBACK(Or)
  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.  
  164.   UNARYOP_FALLBACK(Plus)      UNARYOP_FALLBACK(Minus)
  165.   UNARYOP_FALLBACK(Not)       UNARYOP_FALLBACK(LNot)
  166.   UNARYOP_FALLBACK(Real)      UNARYOP_FALLBACK(Imag)
  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>
  183. class StmtVisitor
  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
  199.