1193326Sed//===--- DeclVisitor.h - Visitor for Decl subclasses ------------*- C++ -*-===//
2193326Sed//
3193326Sed//                     The LLVM Compiler Infrastructure
4193326Sed//
5193326Sed// This file is distributed under the University of Illinois Open Source
6193326Sed// License. See LICENSE.TXT for details.
7193326Sed//
8193326Sed//===----------------------------------------------------------------------===//
9193326Sed//
10193326Sed//  This file defines the DeclVisitor interface.
11193326Sed//
12193326Sed//===----------------------------------------------------------------------===//
13193326Sed#ifndef LLVM_CLANG_AST_DECLVISITOR_H
14193326Sed#define LLVM_CLANG_AST_DECLVISITOR_H
15193326Sed
16193326Sed#include "clang/AST/Decl.h"
17193326Sed#include "clang/AST/DeclCXX.h"
18205219Srdivacky#include "clang/AST/DeclFriend.h"
19249423Sdim#include "clang/AST/DeclObjC.h"
20249423Sdim#include "clang/AST/DeclOpenMP.h"
21193326Sed#include "clang/AST/DeclTemplate.h"
22193326Sed
23193326Sednamespace clang {
24249423Sdimnamespace declvisitor {
25193326Sed
26249423Sdimtemplate <typename T> struct make_ptr       { typedef       T *type; };
27249423Sdimtemplate <typename T> struct make_const_ptr { typedef const T *type; };
28193326Sed
29193326Sed/// \brief A simple visitor class that helps create declaration visitors.
30249423Sdimtemplate<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
31249423Sdimclass Base {
32193326Sedpublic:
33249423Sdim
34249423Sdim#define PTR(CLASS) typename Ptr<CLASS>::type
35249423Sdim#define DISPATCH(NAME, CLASS) \
36249423Sdim  return static_cast<ImplClass*>(this)->Visit##NAME(static_cast<PTR(CLASS)>(D))
37249423Sdim
38249423Sdim  RetTy Visit(PTR(Decl) D) {
39193326Sed    switch (D->getKind()) {
40210299Sed#define DECL(DERIVED, BASE) \
41210299Sed      case Decl::DERIVED: DISPATCH(DERIVED##Decl, DERIVED##Decl);
42210299Sed#define ABSTRACT_DECL(DECL)
43210299Sed#include "clang/AST/DeclNodes.inc"
44193326Sed    }
45234353Sdim    llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
46193326Sed  }
47193326Sed
48193326Sed  // If the implementation chooses not to implement a certain visit
49193326Sed  // method, fall back to the parent.
50210299Sed#define DECL(DERIVED, BASE) \
51249423Sdim  RetTy Visit##DERIVED##Decl(PTR(DERIVED##Decl) D) { DISPATCH(BASE, BASE); }
52210299Sed#include "clang/AST/DeclNodes.inc"
53193326Sed
54249423Sdim  RetTy VisitDecl(PTR(Decl) D) { return RetTy(); }
55193326Sed
56249423Sdim#undef PTR
57193326Sed#undef DISPATCH
58249423Sdim};
59193326Sed
60249423Sdim} // end namespace declvisitor
61249423Sdim
62249423Sdim/// \brief A simple visitor class that helps create declaration visitors.
63249423Sdim///
64249423Sdim/// This class does not preserve constness of Decl pointers (see also
65249423Sdim/// ConstDeclVisitor).
66249423Sdimtemplate<typename ImplClass, typename RetTy=void>
67249423Sdimclass DeclVisitor
68249423Sdim : public declvisitor::Base<declvisitor::make_ptr, ImplClass, RetTy> {};
69249423Sdim
70249423Sdim/// \brief A simple visitor class that helps create declaration visitors.
71249423Sdim///
72249423Sdim/// This class preserves constness of Decl pointers (see also DeclVisitor).
73249423Sdimtemplate<typename ImplClass, typename RetTy=void>
74249423Sdimclass ConstDeclVisitor
75249423Sdim : public declvisitor::Base<declvisitor::make_const_ptr, ImplClass, RetTy> {};
76249423Sdim
77193326Sed}  // end namespace clang
78193326Sed
79193326Sed#endif // LLVM_CLANG_AST_DECLVISITOR_H
80