1 //===--- TypeVisitor.h - Visitor for Type 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 TypeVisitor interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_CLANG_AST_TYPEVISITOR_H 14 #define LLVM_CLANG_AST_TYPEVISITOR_H 15 16 #include "clang/AST/Type.h" 17 18 namespace clang { 19 20 #define DISPATCH(CLASS) \ 21 return static_cast<ImplClass*>(this)-> \ 22 Visit##CLASS(static_cast<const CLASS*>(T)) 23 24 /// An operation on a type. 25 /// 26 /// \tparam ImplClass Class implementing the operation. Must be inherited from 27 /// TypeVisitor. 28 /// \tparam RetTy %Type of result produced by the operation. 29 /// 30 /// The class implements polymorphic operation on an object of type derived 31 /// from Type. The operation is performed by calling method Visit. It then 32 /// dispatches the call to function \c VisitFooType, if actual argument type 33 /// is \c FooType. 34 /// 35 /// The class implements static polymorphism using Curiously Recurring 36 /// Template Pattern. It is designed to be a base class for some concrete 37 /// class: 38 /// 39 /// \code 40 /// class SomeVisitor : public TypeVisitor<SomeVisitor,sometype> { ... }; 41 /// ... 42 /// Type *atype = ... 43 /// ... 44 /// SomeVisitor avisitor; 45 /// sometype result = avisitor.Visit(atype); 46 /// \endcode 47 /// 48 /// Actual treatment is made by methods of the derived class, TypeVisitor only 49 /// dispatches call to the appropriate method. If the implementation class 50 /// \c ImplClass provides specific action for some type, say 51 /// \c ConstantArrayType, it should define method 52 /// <tt>VisitConstantArrayType(const ConstantArrayType*)</tt>. Otherwise 53 /// \c TypeVisitor dispatches call to the method that handles parent type. In 54 /// this example handlers are tried in the sequence: 55 /// 56 /// \li <tt>ImplClass::VisitConstantArrayType(const ConstantArrayType*)</tt> 57 /// \li <tt>ImplClass::VisitArrayType(const ArrayType*)</tt> 58 /// \li <tt>ImplClass::VisitType(const Type*)</tt> 59 /// \li <tt>TypeVisitor::VisitType(const Type*)</tt> 60 /// 61 /// The first function of this sequence that is defined will handle object of 62 /// type \c ConstantArrayType. 63 template<typename ImplClass, typename RetTy=void> 64 class TypeVisitor { 65 public: 66 67 /// Performs the operation associated with this visitor object. 68 RetTy Visit(const Type *T) { 69 // Top switch stmt: dispatch to VisitFooType for each FooType. 70 switch (T->getTypeClass()) { 71 #define ABSTRACT_TYPE(CLASS, PARENT) 72 #define TYPE(CLASS, PARENT) case Type::CLASS: DISPATCH(CLASS##Type); 73 #include "clang/AST/TypeNodes.inc" 74 } 75 llvm_unreachable("Unknown type class!"); 76 } 77 78 // If the implementation chooses not to implement a certain visit method, fall 79 // back on superclass. 80 #define TYPE(CLASS, PARENT) RetTy Visit##CLASS##Type(const CLASS##Type *T) { \ 81 DISPATCH(PARENT); \ 82 } 83 #include "clang/AST/TypeNodes.inc" 84 85 /// Method called if \c ImpClass doesn't provide specific handler 86 /// for some type class. 87 RetTy VisitType(const Type*) { return RetTy(); } 88 }; 89 90 #undef DISPATCH 91 92 } // end namespace clang 93 94 #endif 95