1 //===--- TypeVisitor.h - Visitor for Type subclasses ------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the TypeVisitor interface.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_TYPEVISITOR_H
14 #define LLVM_CLANG_AST_TYPEVISITOR_H
16 #include "clang/AST/Type.h"
20 #define DISPATCH(CLASS) \
21 return static_cast<ImplClass*>(this)-> \
22 Visit##CLASS(static_cast<const CLASS*>(T))
24 /// An operation on a type.
26 /// \tparam ImplClass Class implementing the operation. Must be inherited from
28 /// \tparam RetTy %Type of result produced by the operation.
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
35 /// The class implements static polymorphism using Curiously Recurring
36 /// Template Pattern. It is designed to be a base class for some concrete
40 /// class SomeVisitor : public TypeVisitor<SomeVisitor,sometype> { ... };
44 /// SomeVisitor avisitor;
45 /// sometype result = avisitor.Visit(atype);
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:
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>
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>
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.def"
75 llvm_unreachable("Unknown type class!");
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) { \
83 #include "clang/AST/TypeNodes.def"
85 /// Method called if \c ImpClass doesn't provide specific handler
86 /// for some type class.
87 RetTy VisitType(const Type*) { return RetTy(); }
92 } // end namespace clang