Ignore:
Timestamp:
Jun 1, 2017, 11:00:46 AM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
296b2be, 4a58895
Parents:
7b15d7a
Message:

Added mutator capabilities to the pass visitor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    r7b15d7a rab904dc  
    11#pragma once
    22
     3#include "SynTree/Mutator.h"
    34#include "SynTree/Visitor.h"
    45
    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
     6#include "SynTree/Initializer.h"
     7#include "SynTree/Statement.h"
     8#include "SynTree/Type.h"
     9#include "SynTree/Declaration.h"
     10#include "SynTree/Expression.h"
     11#include "SynTree/Constant.h"
     12
     13//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     14// Deep magic (a.k.a template meta programming) to make the templated visitor work
     15// Basically the goal is to make 2 previsit_impl
    816// 1 - Use when a pass implements a valid previsit. This uses overloading which means the any overload of
    917//     'pass.previsit( node )' that compiles will be used for that node for that type
     
    1624//     The second implementation takes a long while the first takes an int. Since the caller always passes an literal 0
    1725//     the first implementation takes priority in regards to overloading.
    18 //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     26// Mutator functions work along the same principal
     27//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     28// Visit
    1929template<typename pass_type, typename node_type>
    2030static inline auto previsit_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.previsit( node ), void() ) {
     
    2333
    2434template<typename pass_type, typename node_type>
    25 static inline void previsit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {
    26         //Do nothing
    27 }
     35static inline void previsit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
    2836
    2937
     
    3442
    3543template<typename pass_type, typename node_type>
    36 static inline auto postvisit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {
    37         //Do nothing
    38 }
     44static inline void postvisit_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
     45
     46// Mutate
     47template<typename pass_type, typename node_type>
     48static inline auto premutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.premutate( node ), void() ) {
     49        return pass.premutate( node );
     50}
     51
     52template<typename pass_type, typename node_type>
     53static inline void premutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) {}
     54
     55
     56template<typename return_type, typename pass_type, typename node_type>
     57static inline auto postmutate_impl( pass_type& pass, node_type * node, __attribute__((unused)) int unused ) ->decltype( pass.postmutate( node ) ) {
     58        return pass.postmutate( node );
     59}
     60
     61template<typename return_type, typename pass_type, typename node_type>
     62static inline return_type postmutate_impl( __attribute__((unused)) pass_type& pass, node_type * node, __attribute__((unused)) long unused ) { return node; }
    3963
    4064//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    4468//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    4569template< typename pass_type >
    46 class PassVisitor final : public Visitor {
     70class PassVisitor final : public Visitor, public Mutator {
    4771public:
    4872        PassVisitor() = default;
     
    145169        virtual void visit( Constant *constant ) override final;
    146170
     171        virtual DeclarationWithType* mutate( ObjectDecl *objectDecl ) override final;
     172        virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ) override final;
     173        virtual Declaration* mutate( StructDecl *aggregateDecl ) override final;
     174        virtual Declaration* mutate( UnionDecl *aggregateDecl ) override final;
     175        virtual Declaration* mutate( EnumDecl *aggregateDecl ) override final;
     176        virtual Declaration* mutate( TraitDecl *aggregateDecl ) override final;
     177        virtual TypeDecl* mutate( TypeDecl *typeDecl ) override final;
     178        virtual Declaration* mutate( TypedefDecl *typeDecl ) override final;
     179        virtual AsmDecl* mutate( AsmDecl *asmDecl ) override final;
     180
     181        virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ) override final;
     182        virtual Statement* mutate( ExprStmt *exprStmt ) override final;
     183        virtual Statement* mutate( AsmStmt *asmStmt ) override final;
     184        virtual Statement* mutate( IfStmt *ifStmt ) override final;
     185        virtual Statement* mutate( WhileStmt *whileStmt ) override final;
     186        virtual Statement* mutate( ForStmt *forStmt ) override final;
     187        virtual Statement* mutate( SwitchStmt *switchStmt ) override final;
     188        virtual Statement* mutate( CaseStmt *caseStmt ) override final;
     189        virtual Statement* mutate( BranchStmt *branchStmt ) override final;
     190        virtual Statement* mutate( ReturnStmt *returnStmt ) override final;
     191        virtual Statement* mutate( TryStmt *returnStmt ) override final;
     192        virtual Statement* mutate( CatchStmt *catchStmt ) override final;
     193        virtual Statement* mutate( FinallyStmt *catchStmt ) override final;
     194        virtual NullStmt* mutate( NullStmt *nullStmt ) override final;
     195        virtual Statement* mutate( DeclStmt *declStmt ) override final;
     196        virtual Statement* mutate( ImplicitCtorDtorStmt *impCtorDtorStmt ) override final;
     197
     198        virtual Expression* mutate( ApplicationExpr *applicationExpr ) override final;
     199        virtual Expression* mutate( UntypedExpr *untypedExpr ) override final;
     200        virtual Expression* mutate( NameExpr *nameExpr ) override final;
     201        virtual Expression* mutate( AddressExpr *castExpr ) override final;
     202        virtual Expression* mutate( LabelAddressExpr *labAddressExpr ) override final;
     203        virtual Expression* mutate( CastExpr *castExpr ) override final;
     204        virtual Expression* mutate( UntypedMemberExpr *memberExpr ) override final;
     205        virtual Expression* mutate( MemberExpr *memberExpr ) override final;
     206        virtual Expression* mutate( VariableExpr *variableExpr ) override final;
     207        virtual Expression* mutate( ConstantExpr *constantExpr ) override final;
     208        virtual Expression* mutate( SizeofExpr *sizeofExpr ) override final;
     209        virtual Expression* mutate( AlignofExpr *alignofExpr ) override final;
     210        virtual Expression* mutate( UntypedOffsetofExpr *offsetofExpr ) override final;
     211        virtual Expression* mutate( OffsetofExpr *offsetofExpr ) override final;
     212        virtual Expression* mutate( OffsetPackExpr *offsetPackExpr ) override final;
     213        virtual Expression* mutate( AttrExpr *attrExpr ) override final;
     214        virtual Expression* mutate( LogicalExpr *logicalExpr ) override final;
     215        virtual Expression* mutate( ConditionalExpr *conditionalExpr ) override final;
     216        virtual Expression* mutate( CommaExpr *commaExpr ) override final;
     217        virtual Expression* mutate( TypeExpr *typeExpr ) override final;
     218        virtual Expression* mutate( AsmExpr *asmExpr ) override final;
     219        virtual Expression* mutate( ImplicitCopyCtorExpr *impCpCtorExpr ) override final;
     220        virtual Expression* mutate( ConstructorExpr *ctorExpr ) override final;
     221        virtual Expression* mutate( CompoundLiteralExpr *compLitExpr ) override final;
     222        virtual Expression* mutate( UntypedValofExpr *valofExpr ) override final;
     223        virtual Expression* mutate( RangeExpr *rangeExpr ) override final;
     224        virtual Expression* mutate( UntypedTupleExpr *tupleExpr ) override final;
     225        virtual Expression* mutate( TupleExpr *tupleExpr ) override final;
     226        virtual Expression* mutate( TupleIndexExpr *tupleExpr ) override final;
     227        virtual Expression* mutate( MemberTupleExpr *tupleExpr ) override final;
     228        virtual Expression* mutate( TupleAssignExpr *assignExpr ) override final;
     229        virtual Expression* mutate( StmtExpr * stmtExpr ) override final;
     230        virtual Expression* mutate( UniqueExpr * uniqueExpr ) override final;
     231
     232        virtual Type* mutate( VoidType *basicType ) override final;
     233        virtual Type* mutate( BasicType *basicType ) override final;
     234        virtual Type* mutate( PointerType *pointerType ) override final;
     235        virtual Type* mutate( ArrayType *arrayType ) override final;
     236        virtual Type* mutate( FunctionType *functionType ) override final;
     237        virtual Type* mutate( StructInstType *aggregateUseType ) override final;
     238        virtual Type* mutate( UnionInstType *aggregateUseType ) override final;
     239        virtual Type* mutate( EnumInstType *aggregateUseType ) override final;
     240        virtual Type* mutate( TraitInstType *aggregateUseType ) override final;
     241        virtual Type* mutate( TypeInstType *aggregateUseType ) override final;
     242        virtual Type* mutate( TupleType *tupleType ) override final;
     243        virtual Type* mutate( TypeofType *typeofType ) override final;
     244        virtual Type* mutate( AttrType *attrType ) override final;
     245        virtual Type* mutate( VarArgsType *varArgsType ) override final;
     246        virtual Type* mutate( ZeroType *zeroType ) override final;
     247        virtual Type* mutate( OneType *oneType ) override final;
     248
     249        virtual Initializer* mutate( SingleInit *singleInit ) override final;
     250        virtual Initializer* mutate( ListInit *listInit ) override final;
     251        virtual Initializer* mutate( ConstructorInit *ctorInit ) override final;
     252
     253        virtual Subrange *mutate( Subrange *subrange ) override final;
     254
     255        virtual Constant *mutate( Constant *constant ) override final;
     256
    147257private:
    148258        template<typename node_type>
     
    159269                postvisit_impl( pass, node, 0 );
    160270        }
     271
     272        template<typename node_type>
     273        auto call_premutate ( node_type * node )
     274                -> decltype( premutate_impl( pass, node, 0 ), void() )
     275        {
     276                premutate_impl( pass, node, 0 );
     277        }
     278
     279        template<typename return_type, typename node_type>
     280        auto call_postmutate ( node_type * node )
     281                -> decltype( postmutate_impl<return_type>( pass, node, 0 ) )
     282        {
     283                return postmutate_impl<return_type>( pass, node, 0 );
     284        }
    161285};
    162286
Note: See TracChangeset for help on using the changeset viewer.