Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    raff3af4 r5d88a0a  
    11#pragma once
    22
    3 #include <stack>
    4 
    5 #include "SynTree/Mutator.h"
    63#include "SynTree/Visitor.h"
    74
    8 #include "SynTree/Initializer.h"
    9 #include "SynTree/Statement.h"
    10 #include "SynTree/Type.h"
    11 #include "SynTree/Declaration.h"
    12 #include "SynTree/Expression.h"
    13 #include "SynTree/Constant.h"
     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}
    1423
    15 #include "PassVisitor.proto.h"
     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}
    1639
    1740//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    2144//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    2245template< typename pass_type >
    23 class PassVisitor final : public Visitor, public Mutator {
     46class PassVisitor final : public Visitor {
    2447public:
    2548        PassVisitor() = default;
    2649
    2750        template< typename... Args >
    28         PassVisitor(Args &&... args)
     51        PassVisitor(Args &&... args) 
    2952                : pass( std::forward<Args>( args )... )
    3053        {}
    3154
    3255        virtual ~PassVisitor() = default;
     56private:
     57        pass_type pass;
    3358
    34         pass_type pass;
     59public:
    3560
    3661        virtual void visit( ObjectDecl *objectDecl ) override final;
     
    120145        virtual void visit( Constant *constant ) override final;
    121146
    122         virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final;
    123         virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final;
    124         virtual Declaration* mutate( StructDecl *aggregateDecl ) override final;
    125         virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final;
    126         virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final;
    127         virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final;
    128         virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final;
    129         virtual Declaration* mutate( TypedefDecl *typeDecl ) override final;
    130         virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final;
     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        }
    131154
    132         virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final;
    133         virtual Statement* mutate( ExprStmt *exprStmt ) override final;
    134         virtual Statement* mutate( AsmStmt *asmStmt ) override final;
    135         virtual Statement* mutate( IfStmt *ifStmt ) override final;
    136         virtual Statement* mutate( WhileStmt *whileStmt ) override final;
    137         virtual Statement* mutate( ForStmt *forStmt ) override final;
    138         virtual Statement* mutate( SwitchStmt *switchStmt ) override final;
    139         virtual Statement* mutate( CaseStmt *caseStmt ) override final;
    140         virtual Statement* mutate( BranchStmt *branchStmt ) override final;
    141         virtual Statement* mutate( ReturnStmt *returnStmt ) override final;
    142         virtual Statement* mutate( TryStmt *returnStmt ) override final;
    143         virtual Statement* mutate( CatchStmt *catchStmt ) override final;
    144         virtual Statement* mutate( FinallyStmt *catchStmt ) override final;
    145         virtual NullStmt* mutate( NullStmt *nullStmt ) override final;
    146         virtual Statement* mutate( DeclStmt *declStmt ) override final;
    147         virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final;
    148 
    149         virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final;
    150         virtual Expression* mutate( UntypedExpr *untypedExpr ) override final;
    151         virtual Expression* mutate( NameExpr *nameExpr ) override final;
    152         virtual Expression* mutate( AddressExpr *castExpr ) override final;
    153         virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final;
    154         virtual Expression* mutate( CastExpr *castExpr ) override final;
    155         virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final;
    156         virtual Expression* mutate( MemberExpr *memberExpr ) override final;
    157         virtual Expression* mutate( VariableExpr *variableExpr ) override final;
    158         virtual Expression* mutate( ConstantExpr *constantExpr ) override final;
    159         virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final;
    160         virtual Expression* mutate( AlignofExpr *alignofExpr ) override final;
    161         virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final;
    162         virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final;
    163         virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final;
    164         virtual Expression* mutate( AttrExpr *attrExpr ) override final;
    165         virtual Expression* mutate( LogicalExpr *logicalExpr ) override final;
    166         virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final;
    167         virtual Expression* mutate( CommaExpr *commaExpr ) override final;
    168         virtual Expression* mutate( TypeExpr *typeExpr ) override final;
    169         virtual Expression* mutate( AsmExpr *asmExpr ) override final;
    170         virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final;
    171         virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final;
    172         virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final;
    173         virtual Expression* mutate( UntypedValofExpr *valofExpr ) override final;
    174         virtual Expression* mutate( RangeExpr *rangeExpr ) override final;
    175         virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final;
    176         virtual Expression* mutate( TupleExpr *tupleExpr ) override final;
    177         virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final;
    178         virtual Expression* mutate( MemberTupleExpr *tupleExpr ) override final;
    179         virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final;
    180         virtual Expression* mutate( StmtExpr * stmtExpr ) override final;
    181         virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final;
    182 
    183         virtual Type* mutate( VoidType *basicType ) override final;
    184         virtual Type* mutate( BasicType *basicType ) override final;
    185         virtual Type* mutate( PointerType *pointerType ) override final;
    186         virtual Type* mutate( ArrayType *arrayType ) override final;
    187         virtual Type* mutate( FunctionType *functionType ) override final;
    188         virtual Type* mutate( StructInstType *aggregateUseType ) override final;
    189         virtual Type* mutate( UnionInstType *aggregateUseType ) override final;
    190         virtual Type* mutate( EnumInstType *aggregateUseType ) override final;
    191         virtual Type* mutate( TraitInstType *aggregateUseType ) override final;
    192         virtual Type* mutate( TypeInstType *aggregateUseType ) override final;
    193         virtual Type* mutate( TupleType *tupleType ) override final;
    194         virtual Type* mutate( TypeofType *typeofType ) override final;
    195         virtual Type* mutate( AttrType *attrType ) override final;
    196         virtual Type* mutate( VarArgsType *varArgsType ) override final;
    197         virtual Type* mutate( ZeroType *zeroType ) override final;
    198         virtual Type* mutate( OneType *oneType ) override final;
    199 
    200         virtual Initializer* mutate( SingleInit *singleInit ) override final;
    201         virtual Initializer* mutate( ListInit *listInit ) override final;
    202         virtual Initializer* mutate( ConstructorInit *ctorInit ) override final;
    203 
    204         virtual Subrange *mutate( Subrange *subrange ) override final;
    205 
    206         virtual Constant *mutate( Constant *constant ) override final;
    207 
    208 private:
    209         template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); }
    210         template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); }
    211 
    212         template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); }
    213         template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); }
    214 
    215         void call_beginScope() { begin_scope_impl( pass, 0 ); }
    216         void call_endScope  () { end_scope_impl  ( pass, 0 ); }
    217 
    218         void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); }
    219 
    220         void visitStatementList( std::list< Statement* > &statements );
    221         void mutateStatementList( std::list< Statement* > &statements );
    222 
    223         Statement * visitStatement( Statement * stmt );
    224         Statement * mutateStatement( Statement * stmt );
    225 
    226         void visitExpression( Expression * expr );
    227         Expression * mutateExpression( Expression * expr );
    228 
    229 
    230         TypeSubstitution **             get_env_ptr    () { return env_impl             ( pass, 0); }
    231         std::list< Statement* > *       get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); }
    232         std::list< Statement* > *       get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); }
     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        }
    233161};
    234162
Note: See TracChangeset for help on using the changeset viewer.