Changeset 1bc9dcb for src


Ignore:
Timestamp:
Jun 16, 2017, 3:34:55 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
9d85038
Parents:
a724ac1 (diff), d33bc7c (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src
Files:
6 added
4 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.h

    ra724ac1 r1bc9dcb  
    5454        virtual void visit( BranchStmt *branchStmt ) override final;
    5555        virtual void visit( ReturnStmt *returnStmt ) override final;
     56        virtual void visit( ThrowStmt *throwStmt ) override final;
    5657        virtual void visit( TryStmt *tryStmt ) override final;
    5758        virtual void visit( CatchStmt *catchStmt ) override final;
     
    139140        virtual Statement* mutate( BranchStmt *branchStmt ) override final;
    140141        virtual Statement* mutate( ReturnStmt *returnStmt ) override final;
     142        virtual Statement* mutate( ThrowStmt *throwStmt ) override final;
    141143        virtual Statement* mutate( TryStmt *returnStmt ) override final;
    142144        virtual Statement* mutate( CatchStmt *catchStmt ) override final;
     
    230232        std::list< Statement* > *       get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); }
    231233        bool visit_children() { bool* skip = skip_children_impl(pass, 0); return ! (skip && *skip); }
    232 };
     234        void reset_visit() { bool* skip = skip_children_impl(pass, 0); if(skip) *skip = false; }
     235
     236        guard_value_impl init_guard() {
     237                guard_value_impl guard;
     238                auto at_cleanup = at_cleanup_impl(pass, 0);
     239                if( at_cleanup ) {
     240                        *at_cleanup = [&guard]( cleanup_func_t && func, void* val ) {
     241                                guard.push( std::move( func ), val );
     242                        };
     243                }
     244                return guard;
     245        }
     246};
     247
     248template<typename pass_type, typename T>
     249void GuardValue( pass_type * pass, T& val ) {
     250        pass->at_cleanup( [ val ]( void * newVal ) {
     251                * static_cast< T * >( newVal ) = val;
     252        }, static_cast< void * >( & val ) );
     253}
     254
     255class WithTypeSubstitution {
     256protected:
     257        WithTypeSubstitution() = default;
     258        ~WithTypeSubstitution() = default;
     259
     260public:
     261        TypeSubstitution * env;
     262};
     263
     264class WithStmtsToAdd {
     265protected:
     266        WithStmtsToAdd() = default;
     267        ~WithStmtsToAdd() = default;
     268
     269public:
     270        std::list< Statement* > stmtsToAddBefore;
     271        std::list< Statement* > stmtsToAddAfter;
     272};
     273
     274class WithShortCircuiting {
     275protected:
     276        WithShortCircuiting() = default;
     277        ~WithShortCircuiting() = default;
     278
     279public:
     280        bool skip_children;
     281};
     282
     283class WithScopes {
     284protected:
     285        WithScopes() = default;
     286        ~WithScopes() = default;
     287
     288public:
     289        at_cleanup_t at_cleanup;
     290
     291        template< typename T >
     292        void GuardValue( T& val ) {
     293                at_cleanup( [ val ]( void * newVal ) {
     294                        * static_cast< T * >( newVal ) = val;
     295                }, static_cast< void * >( & val ) );
     296        }
     297};
     298
    233299
    234300#include "PassVisitor.impl.h"
  • src/Common/PassVisitor.impl.h

    ra724ac1 r1bc9dcb  
    11#pragma once
    22
    3 #define VISIT_START( node )  \
    4         call_previsit( node ); \
    5         if( visit_children() ) { \
    6 
    7 #define VISIT_END( node )            \
    8         }                              \
    9         return call_postvisit( node ); \
    10 
    11 #define MUTATE_START( node )  \
    12         call_premutate( node ); \
    13         if( visit_children() ) { \
     3#define VISIT_START( node )                     \
     4        __attribute__((unused))                   \
     5        const auto & guard = init_guard();        \
     6        call_previsit( node );                    \
     7        if( visit_children() ) {                  \
     8                reset_visit();                      \
     9
     10#define VISIT_END( node )                       \
     11        }                                         \
     12        call_postvisit( node );                   \
     13
     14#define MUTATE_START( node )                    \
     15        __attribute__((unused))                   \
     16        const auto & guard = init_guard();        \
     17        call_premutate( node );                   \
     18        if( visit_children() ) {                  \
     19                reset_visit();                      \
    1420
    1521#define MUTATE_END( type, node )                \
     
    1824
    1925
    20 #define VISIT_BODY( node )    \
    21         VISIT_START( node );  \
    22         Visitor::visit( node ); \
    23         VISIT_END( node ); \
     26#define VISIT_BODY( node )        \
     27        VISIT_START( node );        \
     28        Visitor::visit( node );     \
     29        VISIT_END( node );          \
    2430
    2531
     
    389395
    390396//--------------------------------------------------------------------------
     397// ThrowStmt
     398
     399template< typename pass_type >
     400void PassVisitor< pass_type >::visit( ThrowStmt * node ) {
     401        VISIT_BODY( node );
     402}
     403
     404template< typename pass_type >
     405Statement * PassVisitor< pass_type >::mutate( ThrowStmt * node ) {
     406        MUTATE_BODY( Statement, node );
     407}
     408
     409//--------------------------------------------------------------------------
    391410// TryStmt
    392411template< typename pass_type >
  • src/Common/PassVisitor.proto.h

    ra724ac1 r1bc9dcb  
    11#pragma once
     2
     3typedef std::function<void( void * )> cleanup_func_t;
     4
     5class guard_value_impl {
     6public:
     7        guard_value_impl() = default;
     8
     9        ~guard_value_impl() {
     10                while( !cleanups.empty() ) {
     11                        auto& cleanup = cleanups.top();
     12                        cleanup.func( cleanup.val );
     13                        cleanups.pop();
     14                }
     15        }
     16
     17        void push( cleanup_func_t && func, void* val ) {
     18                cleanups.emplace( std::move(func), val );
     19        }
     20
     21private:
     22        struct cleanup_t {
     23                cleanup_func_t func;
     24                void * val;
     25
     26                cleanup_t( cleanup_func_t&& func, void * val ) : func(func), val(val) {}
     27        };
     28
     29        std::stack< cleanup_t > cleanups;
     30};
     31
     32typedef std::function< void( cleanup_func_t, void * ) > at_cleanup_t;
    233
    334//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
     
    73104#define FIELD_PTR( type, name )                                                                                                        \
    74105template<typename pass_type>                                                                                                           \
    75 static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; }  \
     106static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) -> decltype( &pass.name ) { return &pass.name; } \
    76107                                                                                                                                       \
    77108template<typename pass_type>                                                                                                           \
     
    82113FIELD_PTR( std::list< Statement* >, stmtsToAddAfter  )
    83114FIELD_PTR( bool, skip_children )
     115FIELD_PTR( at_cleanup_t, at_cleanup )
  • src/InitTweak/GenInit.cc

    ra724ac1 r1bc9dcb  
    1616#include <stack>
    1717#include <list>
     18
     19#include "InitTweak.h"
    1820#include "GenInit.h"
    19 #include "InitTweak.h"
     21
     22#include "Common/PassVisitor.h"
     23
    2024#include "SynTree/Declaration.h"
    21 #include "SynTree/Type.h"
    2225#include "SynTree/Expression.h"
    23 #include "SynTree/Statement.h"
    2426#include "SynTree/Initializer.h"
    2527#include "SynTree/Mutator.h"
     28#include "SynTree/Statement.h"
     29#include "SynTree/Type.h"
     30
    2631#include "SymTab/Autogen.h"
    2732#include "SymTab/Mangler.h"
     33
     34#include "GenPoly/DeclMutator.h"
    2835#include "GenPoly/PolyMutator.h"
    29 #include "GenPoly/DeclMutator.h"
    3036#include "GenPoly/ScopedSet.h"
     37
    3138#include "ResolvExpr/typeops.h"
    3239
     
    3744        }
    3845
    39         class ReturnFixer final : public GenPoly::PolyMutator {
     46        class ReturnFixer : public WithStmtsToAdd, public WithScopes {
    4047          public:
    4148                /// consistently allocates a temporary variable for the return value
     
    4451                static void makeReturnTemp( std::list< Declaration * > &translationUnit );
    4552
    46                 typedef GenPoly::PolyMutator Parent;
    47                 using Parent::mutate;
    48                 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
    49                 virtual Statement * mutate( ReturnStmt * returnStmt ) override;
     53                void premutate( FunctionDecl *functionDecl );
     54                void premutate( ReturnStmt * returnStmt );
    5055
    5156          protected:
     
    129134
    130135        void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
    131                 ReturnFixer fixer;
     136                PassVisitor<ReturnFixer> fixer;
    132137                mutateAll( translationUnit, fixer );
    133138        }
    134139
    135         Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
     140        void ReturnFixer::premutate( ReturnStmt *returnStmt ) {
    136141                std::list< DeclarationWithType * > & returnVals = ftype->get_returnVals();
    137142                assert( returnVals.size() == 0 || returnVals.size() == 1 );
     
    144149                        construct->get_args().push_back( new AddressExpr( new VariableExpr( returnVals.front() ) ) );
    145150                        construct->get_args().push_back( returnStmt->get_expr() );
    146                         stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
     151                        stmtsToAddBefore.push_back(new ExprStmt(noLabels, construct));
    147152
    148153                        // return the retVal object
    149154                        returnStmt->set_expr( new VariableExpr( returnVals.front() ) );
    150155                } // if
    151                 return returnStmt;
    152         }
    153 
    154         DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
    155                 ValueGuard< FunctionType * > oldFtype( ftype );
    156                 ValueGuard< std::string > oldFuncName( funcName );
     156        }
     157
     158        void ReturnFixer::premutate( FunctionDecl *functionDecl ) {
     159                GuardValue( ftype );
     160                GuardValue( funcName );
    157161
    158162                ftype = functionDecl->get_functionType();
    159163                funcName = functionDecl->get_name();
    160                 return Parent::mutate( functionDecl );
    161164        }
    162165
Note: See TracChangeset for help on using the changeset viewer.