clang API Documentation

DeclVisitor.h
Go to the documentation of this file.
00001 //===--- DeclVisitor.h - Visitor for Decl 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 DeclVisitor interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 #ifndef LLVM_CLANG_AST_DECLVISITOR_H
00014 #define LLVM_CLANG_AST_DECLVISITOR_H
00015 
00016 #include "clang/AST/Decl.h"
00017 #include "clang/AST/DeclCXX.h"
00018 #include "clang/AST/DeclFriend.h"
00019 #include "clang/AST/DeclObjC.h"
00020 #include "clang/AST/DeclOpenMP.h"
00021 #include "clang/AST/DeclTemplate.h"
00022 
00023 namespace clang {
00024 namespace declvisitor {
00025 
00026 template <typename T> struct make_ptr       { typedef       T *type; };
00027 template <typename T> struct make_const_ptr { typedef const T *type; };
00028 
00029 /// \brief A simple visitor class that helps create declaration visitors.
00030 template<template <typename> class Ptr, typename ImplClass, typename RetTy=void>
00031 class Base {
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)>(D))
00037 
00038   RetTy Visit(PTR(Decl) D) {
00039     switch (D->getKind()) {
00040 #define DECL(DERIVED, BASE) \
00041       case Decl::DERIVED: DISPATCH(DERIVED##Decl, DERIVED##Decl);
00042 #define ABSTRACT_DECL(DECL)
00043 #include "clang/AST/DeclNodes.inc"
00044     }
00045     llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
00046   }
00047 
00048   // If the implementation chooses not to implement a certain visit
00049   // method, fall back to the parent.
00050 #define DECL(DERIVED, BASE) \
00051   RetTy Visit##DERIVED##Decl(PTR(DERIVED##Decl) D) { DISPATCH(BASE, BASE); }
00052 #include "clang/AST/DeclNodes.inc"
00053 
00054   RetTy VisitDecl(PTR(Decl) D) { return RetTy(); }
00055 
00056 #undef PTR
00057 #undef DISPATCH
00058 };
00059 
00060 } // end namespace declvisitor
00061 
00062 /// \brief A simple visitor class that helps create declaration visitors.
00063 ///
00064 /// This class does not preserve constness of Decl pointers (see also
00065 /// ConstDeclVisitor).
00066 template<typename ImplClass, typename RetTy=void>
00067 class DeclVisitor
00068  : public declvisitor::Base<declvisitor::make_ptr, ImplClass, RetTy> {};
00069 
00070 /// \brief A simple visitor class that helps create declaration visitors.
00071 ///
00072 /// This class preserves constness of Decl pointers (see also DeclVisitor).
00073 template<typename ImplClass, typename RetTy=void>
00074 class ConstDeclVisitor
00075  : public declvisitor::Base<declvisitor::make_const_ptr, ImplClass, RetTy> {};
00076 
00077 }  // end namespace clang
00078 
00079 #endif // LLVM_CLANG_AST_DECLVISITOR_H