Skip to content

Commit

Permalink
Allow StmtVisitor visitation to be parameterized. NFC.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@310962 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rjmccall committed Aug 15, 2017
1 parent db361b5 commit 5db14d5
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions include/clang/AST/StmtVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ template <typename T> struct make_const_ptr { typedef const T *type; };
/// StmtVisitorBase - This class implements a simple visitor for Stmt
/// subclasses. Since Expr derives from Stmt, this also includes support for
/// visiting Exprs.
template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
template<template <typename> class Ptr, typename ImplClass, typename RetTy=void,
class... ParamTys>
class StmtVisitorBase {
public:

#define PTR(CLASS) typename Ptr<CLASS>::type
#define DISPATCH(NAME, CLASS) \
return static_cast<ImplClass*>(this)->Visit ## NAME(static_cast<PTR(CLASS)>(S))
return static_cast<ImplClass*>(this)->Visit ## NAME( \
static_cast<PTR(CLASS)>(S), std::forward<ParamTys>(P)...)

RetTy Visit(PTR(Stmt) S) {
RetTy Visit(PTR(Stmt) S, ParamTys... P) {

// If we have a binary expr, dispatch to the subcode of the binop. A smart
// optimizer (e.g. LLVM) will fold this comparison into the switch stmt
Expand Down Expand Up @@ -111,13 +113,13 @@ class StmtVisitorBase {
// If the implementation chooses not to implement a certain visit method, fall
// back on VisitExpr or whatever else is the superclass.
#define STMT(CLASS, PARENT) \
RetTy Visit ## CLASS(PTR(CLASS) S) { DISPATCH(PARENT, PARENT); }
RetTy Visit ## CLASS(PTR(CLASS) S, ParamTys... P) { DISPATCH(PARENT, PARENT); }
#include "clang/AST/StmtNodes.inc"

// If the implementation doesn't implement binary operator methods, fall back
// on VisitBinaryOperator.
#define BINOP_FALLBACK(NAME) \
RetTy VisitBin ## NAME(PTR(BinaryOperator) S) { \
RetTy VisitBin ## NAME(PTR(BinaryOperator) S, ParamTys... P) { \
DISPATCH(BinaryOperator, BinaryOperator); \
}
BINOP_FALLBACK(PtrMemD) BINOP_FALLBACK(PtrMemI)
Expand All @@ -137,7 +139,7 @@ class StmtVisitorBase {
// If the implementation doesn't implement compound assignment operator
// methods, fall back on VisitCompoundAssignOperator.
#define CAO_FALLBACK(NAME) \
RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S) { \
RetTy VisitBin ## NAME(PTR(CompoundAssignOperator) S, ParamTys... P) { \
DISPATCH(CompoundAssignOperator, CompoundAssignOperator); \
}
CAO_FALLBACK(MulAssign) CAO_FALLBACK(DivAssign) CAO_FALLBACK(RemAssign)
Expand All @@ -149,7 +151,7 @@ class StmtVisitorBase {
// If the implementation doesn't implement unary operator methods, fall back
// on VisitUnaryOperator.
#define UNARYOP_FALLBACK(NAME) \
RetTy VisitUnary ## NAME(PTR(UnaryOperator) S) { \
RetTy VisitUnary ## NAME(PTR(UnaryOperator) S, ParamTys... P) { \
DISPATCH(UnaryOperator, UnaryOperator); \
}
UNARYOP_FALLBACK(PostInc) UNARYOP_FALLBACK(PostDec)
Expand All @@ -163,7 +165,7 @@ class StmtVisitorBase {
#undef UNARYOP_FALLBACK

// Base case, ignore it. :)
RetTy VisitStmt(PTR(Stmt) Node) { return RetTy(); }
RetTy VisitStmt(PTR(Stmt) Node, ParamTys... P) { return RetTy(); }

#undef PTR
#undef DISPATCH
Expand All @@ -174,18 +176,18 @@ class StmtVisitorBase {
///
/// This class does not preserve constness of Stmt pointers (see also
/// ConstStmtVisitor).
template<typename ImplClass, typename RetTy=void>
template<typename ImplClass, typename RetTy=void, typename... ParamTys>
class StmtVisitor
: public StmtVisitorBase<make_ptr, ImplClass, RetTy> {};
: public StmtVisitorBase<make_ptr, ImplClass, RetTy, ParamTys...> {};

/// ConstStmtVisitor - This class implements a simple visitor for Stmt
/// subclasses. Since Expr derives from Stmt, this also includes support for
/// visiting Exprs.
///
/// This class preserves constness of Stmt pointers (see also StmtVisitor).
template<typename ImplClass, typename RetTy=void>
template<typename ImplClass, typename RetTy=void, typename... ParamTys>
class ConstStmtVisitor
: public StmtVisitorBase<make_const_ptr, ImplClass, RetTy> {};
: public StmtVisitorBase<make_const_ptr, ImplClass, RetTy, ParamTys...> {};

/// \brief This class implements a simple visitor for OMPClause
/// subclasses.
Expand Down

0 comments on commit 5db14d5

Please sign in to comment.