source: src/Common/PassVisitor.h @ 5d88a0a

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 5d88a0a was 5d88a0a, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Some more documentation for PassVisitor?

  • Property mode set to 100644
File size: 8.4 KB
Line 
1#pragma once
2
3#include "SynTree/Visitor.h"
4
5//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6//Deep magic (a.k.a template meta programming) to make the templated visitor work
7//Basically the goal is to make 2 previsit_impl
8// 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
9//     'pass.previsit( node )' that compiles will be used for that node for that type
10//     This requires that this option only compile for passes that actually define an appropriate visit.
11//     SFINAE will make sure the compilation errors in this function don't halt the build.
12//     See http://en.cppreference.com/w/cpp/language/sfinae for details on SFINAE
13// 2 - Since the first implementation might not be specilizable, the second implementation exists and does nothing.
14//     This is needed only to eliminate the need for passes to specify any kind of handlers.
15//     The second implementation only works because it has a lower priority. This is due to the bogus last parameter.
16//     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
17//     the first implementation takes priority in regards to overloading.
18//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
19template<typename pass_type, typename node_type>
20static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.previsit( node ), void() ) {
21        pass.previsit( node );
22}
23
24template<typename pass_type, typename node_type>
25static inline void previsit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {
26        //Do nothing
27}
28
29
30template<typename pass_type, typename node_type>
31static inline auto postvisit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postvisit( node ), void() ) {
32        pass.postvisit( node );
33}
34
35template<typename pass_type, typename node_type>
36static inline auto postvisit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {
37        //Do nothing
38}
39
40//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
41// Templated visitor type
42// To use declare a PassVisitor< YOUR VISITOR TYPE >
43// The visitor type should specify the previsit/postvisit for types that are desired.
44//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
45template< typename pass_type >
46class PassVisitor final : public Visitor {
47public:
48        PassVisitor() = default;
49
50        template< typename... Args >
51        PassVisitor(Args &&... args) 
52                : pass( std::forward<Args>( args )... )
53        {}
54
55        virtual ~PassVisitor() = default;
56private:
57        pass_type pass;
58
59public:
60
61        virtual void visit( ObjectDecl *objectDecl ) override final;
62        virtual void visit( FunctionDecl *functionDecl ) override final;
63        virtual void visit( StructDecl *aggregateDecl ) override final;
64        virtual void visit( UnionDecl *aggregateDecl ) override final;
65        virtual void visit( EnumDecl *aggregateDecl ) override final;
66        virtual void visit( TraitDecl *aggregateDecl ) override final;
67        virtual void visit( TypeDecl *typeDecl ) override final;
68        virtual void visit( TypedefDecl *typeDecl ) override final;
69        virtual void visit( AsmDecl *asmDecl ) override final;
70
71        virtual void visit( CompoundStmt *compoundStmt ) override final;
72        virtual void visit( ExprStmt *exprStmt ) override final;
73        virtual void visit( AsmStmt *asmStmt ) override final;
74        virtual void visit( IfStmt *ifStmt ) override final;
75        virtual void visit( WhileStmt *whileStmt ) override final;
76        virtual void visit( ForStmt *forStmt ) override final;
77        virtual void visit( SwitchStmt *switchStmt ) override final;
78        virtual void visit( CaseStmt *caseStmt ) override final;
79        virtual void visit( BranchStmt *branchStmt ) override final;
80        virtual void visit( ReturnStmt *returnStmt ) override final;
81        virtual void visit( TryStmt *tryStmt ) override final;
82        virtual void visit( CatchStmt *catchStmt ) override final;
83        virtual void visit( FinallyStmt *finallyStmt ) override final;
84        virtual void visit( NullStmt *nullStmt ) override final;
85        virtual void visit( DeclStmt *declStmt ) override final;
86        virtual void visit( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final;
87
88        virtual void visit( ApplicationExpr *applicationExpr ) override final;
89        virtual void visit( UntypedExpr *untypedExpr ) override final;
90        virtual void visit( NameExpr *nameExpr ) override final;
91        virtual void visit( CastExpr *castExpr ) override final;
92        virtual void visit( AddressExpr *addressExpr ) override final;
93        virtual void visit( LabelAddressExpr *labAddressExpr ) override final;
94        virtual void visit( UntypedMemberExpr *memberExpr ) override final;
95        virtual void visit( MemberExpr *memberExpr ) override final;
96        virtual void visit( VariableExpr *variableExpr ) override final;
97        virtual void visit( ConstantExpr *constantExpr ) override final;
98        virtual void visit( SizeofExpr *sizeofExpr ) override final;
99        virtual void visit( AlignofExpr *alignofExpr ) override final;
100        virtual void visit( UntypedOffsetofExpr *offsetofExpr ) override final;
101        virtual void visit( OffsetofExpr *offsetofExpr ) override final;
102        virtual void visit( OffsetPackExpr *offsetPackExpr ) override final;
103        virtual void visit( AttrExpr *attrExpr ) override final;
104        virtual void visit( LogicalExpr *logicalExpr ) override final;
105        virtual void visit( ConditionalExpr *conditionalExpr ) override final;
106        virtual void visit( CommaExpr *commaExpr ) override final;
107        virtual void visit( TypeExpr *typeExpr ) override final;
108        virtual void visit( AsmExpr *asmExpr ) override final;
109        virtual void visit( ImplicitCopyCtorExpr *impCpCtorExpr ) override final;
110        virtual void visit( ConstructorExpr * ctorExpr ) override final;
111        virtual void visit( CompoundLiteralExpr *compLitExpr ) override final;
112        virtual void visit( UntypedValofExpr *valofExpr ) override final;
113        virtual void visit( RangeExpr *rangeExpr ) override final;
114        virtual void visit( UntypedTupleExpr *tupleExpr ) override final;
115        virtual void visit( TupleExpr *tupleExpr ) override final;
116        virtual void visit( TupleIndexExpr *tupleExpr ) override final;
117        virtual void visit( MemberTupleExpr *tupleExpr ) override final;
118        virtual void visit( TupleAssignExpr *assignExpr ) override final;
119        virtual void visit( StmtExpr * stmtExpr ) override final;
120        virtual void visit( UniqueExpr * uniqueExpr ) override final;
121
122        virtual void visit( VoidType *basicType ) override final;
123        virtual void visit( BasicType *basicType ) override final;
124        virtual void visit( PointerType *pointerType ) override final;
125        virtual void visit( ArrayType *arrayType ) override final;
126        virtual void visit( FunctionType *functionType ) override final;
127        virtual void visit( StructInstType *aggregateUseType ) override final;
128        virtual void visit( UnionInstType *aggregateUseType ) override final;
129        virtual void visit( EnumInstType *aggregateUseType ) override final;
130        virtual void visit( TraitInstType *aggregateUseType ) override final;
131        virtual void visit( TypeInstType *aggregateUseType ) override final;
132        virtual void visit( TupleType *tupleType ) override final;
133        virtual void visit( TypeofType *typeofType ) override final;
134        virtual void visit( AttrType *attrType ) override final;
135        virtual void visit( VarArgsType *varArgsType ) override final;
136        virtual void visit( ZeroType *zeroType ) override final;
137        virtual void visit( OneType *oneType ) override final;
138
139        virtual void visit( SingleInit *singleInit ) override final;
140        virtual void visit( ListInit *listInit ) override final;
141        virtual void visit( ConstructorInit *ctorInit ) override final;
142
143        virtual void visit( Subrange *subrange ) override final;
144
145        virtual void visit( Constant *constant ) override final;
146
147private:
148        template<typename node_type> 
149        auto call_previsit ( node_type * node ) 
150                -> decltype( previsit_impl ( pass, node, 0 ), void() ) 
151        { 
152                previsit_impl ( pass, node, 0 ); 
153        }
154
155        template<typename node_type> 
156        auto call_postvisit( node_type * node )
157                -> decltype( postvisit_impl( pass, node, 0 ), void() ) 
158        { 
159                postvisit_impl( pass, node, 0 ); 
160        }
161};
162
163#include "PassVisitor.impl.h"
Note: See TracBrowser for help on using the repository browser.