Changeset 134322e
- Timestamp:
- Jun 1, 2017, 4:26:50 PM (7 years ago)
- 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:
- 676cc8c
- Parents:
- 3fb9a83
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/PassVisitor.h
r3fb9a83 r134322e 220 220 void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } 221 221 222 void visitStatementList( std::list< Statement* > &statements ); 222 223 void mutateStatementList( std::list< Statement* > &statements ); 224 225 Statement * visitStatement( Statement * stmt ); 223 226 Statement * mutateStatement( Statement * stmt ); 227 228 void visitExpression( Expression * expr ); 224 229 Expression * mutateExpression( Expression * expr ); 225 230 226 private: 227 TypeSubstitution * env; 228 229 std::list< Statement* > stmtsToAdd; 230 std::list< Statement* > stmtsToAddAfter; 231 232 TypeSubstitution ** get_env_ptr () { return env_impl ( pass, 0); } 233 std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } 234 std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } 231 235 }; 232 236 -
src/Common/PassVisitor.impl.h
r3fb9a83 r134322e 20 20 MUTATE_END( type, node ); \ 21 21 22 23 24 template<typename T> 25 static inline bool empty( T * ptr ) { 26 return !ptr || ptr->empty(); 27 } 28 29 typedef std::list< Statement * > StmtList_t; 30 31 template< typename pass_type > 32 void PassVisitor< pass_type >::visitStatementList( std::list< Statement * > & statements ) { 33 SemanticError errors; 34 35 StmtList_t* beforeStmts = get_beforeStmts(); 36 StmtList_t* afterStmts = get_afterStmts(); 37 38 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { 39 if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); } 40 try { 41 *i = (*i)->accept( *this ); 42 } catch ( SemanticError &e ) { 43 errors.append( e ); 44 } 45 if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); } 46 } 47 48 if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); } 49 if ( !errors.isEmpty() ) { throw errors; } 50 } 51 22 52 template< typename pass_type > 23 53 void PassVisitor< pass_type >::mutateStatementList( std::list< Statement * > & statements ) { 24 54 SemanticError errors; 25 55 56 StmtList_t* beforeStmts = get_beforeStmts(); 57 StmtList_t* afterStmts = get_afterStmts(); 58 26 59 for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { 27 if ( ! stmtsToAddAfter.empty() ) { 28 statements.splice( i, stmtsToAddAfter ); 29 } // if 60 if ( !empty( afterStmts ) ) { statements.splice( i, *afterStmts ); } 30 61 try { 31 62 *i = (*i)->acceptMutator( *this ); 32 63 } catch ( SemanticError &e ) { 33 64 errors.append( e ); 34 } // try 35 if ( ! stmtsToAdd.empty() ) { 36 statements.splice( i, stmtsToAdd ); 37 } // if 38 } // for 39 if ( ! stmtsToAddAfter.empty() ) { 40 statements.splice( statements.end(), stmtsToAddAfter ); 41 } // if 42 if ( ! errors.isEmpty() ) { 43 throw errors; 65 } 66 if ( !empty( beforeStmts ) ) { statements.splice( i, *beforeStmts ); } 44 67 } 68 69 if ( !empty( afterStmts ) ) { statements.splice( statements.end(), *afterStmts ); } 70 if ( !errors.isEmpty() ) { throw errors; } 71 } 72 73 template< typename pass_type > 74 Statement * PassVisitor< pass_type >::visitStatement( Statement * stmt ) { 75 // don't want statements from outer CompoundStmts to be added to this CompoundStmt 76 ValueGuardPtr< TypeSubstitution * > oldEnv ( get_env_ptr() ); 77 ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() ); 78 ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () ); 79 80 Statement *newStmt = maybeVisit( stmt, *this ); 81 82 StmtList_t* beforeStmts = get_beforeStmts(); 83 StmtList_t* afterStmts = get_afterStmts(); 84 85 if( empty(beforeStmts) && empty(afterStmts) ) { return newStmt; } 86 87 CompoundStmt *compound = new CompoundStmt( noLabels ); 88 if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); } 89 compound->get_kids().push_back( newStmt ); 90 if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); } 91 return compound; 45 92 } 46 93 … … 48 95 Statement * PassVisitor< pass_type >::mutateStatement( Statement * stmt ) { 49 96 // don't want statements from outer CompoundStmts to be added to this CompoundStmt 50 ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); 51 ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); 52 ValueGuard< TypeSubstitution * > oldEnv( env ); 53 set_env( env ); 54 55 stmtsToAdd.clear(); 56 stmtsToAddAfter.clear(); 97 ValueGuardPtr< TypeSubstitution * > oldEnv ( get_env_ptr() ); 98 ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() ); 99 ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () ); 57 100 58 101 Statement *newStmt = maybeMutate( stmt, *this ); 59 if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { 60 CompoundStmt *compound = new CompoundStmt( noLabels ); 61 compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); 62 compound->get_kids().push_back( newStmt ); 63 compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); 64 // doEndScope(); 65 return compound; 66 } else { 67 return newStmt; 102 103 StmtList_t* beforeStmts = get_beforeStmts(); 104 StmtList_t* afterStmts = get_afterStmts(); 105 106 if( empty(beforeStmts) && empty(afterStmts) ) { return newStmt; } 107 108 CompoundStmt *compound = new CompoundStmt( noLabels ); 109 if( !empty(beforeStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *beforeStmts ); } 110 compound->get_kids().push_back( newStmt ); 111 if( !empty(afterStmts) ) { compound->get_kids().splice( compound->get_kids().end(), *afterStmts ); } 112 return compound; 113 } 114 115 116 117 template< typename pass_type > 118 void PassVisitor< pass_type >::visitExpression( Expression * expr ) { 119 if( !expr ) return; 120 121 auto env_ptr = get_env_ptr(); 122 if ( env_ptr && expr->get_env() ) { 123 *env_ptr = expr->get_env(); 68 124 } 125 // xxx - should env be cloned (or moved) onto the result of the mutate? 126 expr->accept( *this ); 69 127 } 70 128 … … 73 131 if( !expr ) return nullptr; 74 132 75 if ( expr->get_env() ) {76 env = expr->get_env();77 set_env( env);133 auto env_ptr = get_env_ptr(); 134 if ( env_ptr && expr->get_env() ) { 135 *env_ptr = expr->get_env(); 78 136 } 79 137 // xxx - should env be cloned (or moved) onto the result of the mutate? … … 488 546 489 547 // don't want statements from outer CompoundStmts to be added to this StmtExpr 490 ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); 491 ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); 492 ValueGuard< TypeSubstitution * > oldEnv( env ); 493 set_env( env ); 494 495 // xxx - not sure if this is needed, along with appropriate reset, but I don't think so... 496 // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars ); 497 498 stmtsToAdd.clear(); 499 stmtsToAddAfter.clear(); 500 // scopeTyVars.clear(); 548 ValueGuardPtr< TypeSubstitution * > oldEnv ( get_env_ptr() ); 549 ValueGuardPtr< std::list< Statement* > > oldBeforeStmts( get_beforeStmts() ); 550 ValueGuardPtr< std::list< Statement* > > oldAfterStmts ( get_afterStmts () ); 501 551 502 552 Mutator::mutate( node ); -
src/Common/PassVisitor.proto.h
r3fb9a83 r134322e 70 70 static inline void end_scope_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) {} 71 71 72 // Env 73 template<typename pass_type> 74 static inline auto set_env_impl( pass_type& pass, TypeSubstitution * env, __attribute__((unused)) int unused ) ->decltype( pass.env, void() ) { 75 pass.env = env; 76 } 72 // Fields 73 #define FIELD_PTR( type, name ) \ 74 template<typename pass_type> \ 75 static inline auto name##_impl( pass_type& pass, __attribute__((unused)) int unused ) ->decltype( &pass.name ) { return &pass.name; } \ 76 \ 77 template<typename pass_type> \ 78 static inline type * name##_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) long unused ) { return nullptr;} \ 77 79 78 template<typename pass_type> 79 static inline void set_env_impl( __attribute__((unused)) pass_type& pass, __attribute__((unused)) TypeSubstitution * env, __attribute__((unused)) long unused ) {} 80 FIELD_PTR( TypeSubstitution *, env ) 81 FIELD_PTR( std::list< Statement* >, stmtsToAddBefore ) 82 FIELD_PTR( std::list< Statement* >, stmtsToAddAfter ) -
src/Common/utility.h
r3fb9a83 r134322e 244 244 ValueGuard(T& inRef) : old(inRef), ref(inRef) {} 245 245 ~ValueGuard() { ref = old; } 246 }; 247 248 template< typename T > 249 struct ValueGuardPtr { 250 T old; 251 T* ref; 252 253 ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {} 254 ~ValueGuardPtr() { if( ref ) *ref = old; } 255 }; 256 257 template< typename T > 258 struct ValueGuardPtr< std::list< T > > { 259 std::list< T > old; 260 std::list< T >* ref; 261 262 ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) { 263 if( ref ) { swap( *ref, old ); } 264 } 265 ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } } 246 266 }; 247 267 -
src/InitTweak/FixInit.cc
r3fb9a83 r134322e 20 20 #include <unordered_map> 21 21 #include <unordered_set> 22 22 23 #include "InitTweak.h" 23 24 #include "GenInit.h" 24 25 #include "FixInit.h" 25 26 #include "FixGlobalInit.h" 27 #include "CodeGen/GenType.h" // for warning/error messages 28 #include "Common/PassVisitor.h" 29 #include "GenPoly/DeclMutator.h" 30 #include "GenPoly/PolyMutator.h" 26 31 #include "ResolvExpr/Resolver.h" 27 32 #include "ResolvExpr/typeops.h" 33 #include "SymTab/Autogen.h" 34 #include "SymTab/Indexer.h" 35 #include "SynTree/AddStmtVisitor.h" 36 #include "SynTree/Attribute.h" 28 37 #include "SynTree/Declaration.h" 29 #include "SynTree/Type.h"30 38 #include "SynTree/Expression.h" 31 #include "SynTree/Attribute.h"32 #include "SynTree/Statement.h"33 39 #include "SynTree/Initializer.h" 34 40 #include "SynTree/Mutator.h" 35 #include "SymTab/Indexer.h" 36 #include "SymTab/Autogen.h" 37 #include "GenPoly/PolyMutator.h" 38 #include "GenPoly/DeclMutator.h" 39 #include "SynTree/AddStmtVisitor.h" 40 #include "CodeGen/GenType.h" // for warning/error messages 41 #include "SynTree/Statement.h" 42 #include "SynTree/Type.h" 41 43 #include "Tuples/Tuples.h" 42 44 … … 54 56 typedef std::unordered_map< int, int > UnqCount; 55 57 56 class InsertImplicitCalls final : public GenPoly::PolyMutator{58 class InsertImplicitCalls { 57 59 public: 58 60 /// wrap function application expressions as ImplicitCopyCtorExpr nodes so that it is easy to identify which … … 61 63 62 64 InsertImplicitCalls( EnvMap & envMap ) : envMap( envMap ) {} 63 typedef GenPoly::PolyMutator Parent; 64 using Parent::mutate; 65 virtual Expression * mutate( ApplicationExpr * appExpr ) override; 66 virtual Expression * mutate( StmtExpr * stmtExpr ) override; 65 66 Expression * postmutate( ApplicationExpr * appExpr ); 67 void premutate( StmtExpr * stmtExpr ); 67 68 68 69 // collects environments for relevant nodes 69 70 EnvMap & envMap; 71 TypeSubstitution * env; //Magically populated by the PassVisitor 70 72 }; 71 73 … … 300 302 namespace { 301 303 void InsertImplicitCalls::insert( std::list< Declaration * > & translationUnit, EnvMap & envMap ) { 302 InsertImplicitCallsinserter( envMap );304 PassVisitor<InsertImplicitCalls> inserter( envMap ); 303 305 mutateAll( translationUnit, inserter ); 304 306 } … … 350 352 } 351 353 352 Expression * InsertImplicitCalls::mutate( ApplicationExpr * appExpr ) { 353 appExpr = dynamic_cast< ApplicationExpr * >( Parent::mutate( appExpr ) ); 354 Expression * InsertImplicitCalls::postmutate( ApplicationExpr * appExpr ) { 354 355 assert( appExpr ); 355 356 … … 393 394 } 394 395 395 Expression * InsertImplicitCalls::mutate( StmtExpr * stmtExpr ) {396 void InsertImplicitCalls::premutate( StmtExpr * stmtExpr ) { 396 397 assert( env ); 397 398 envMap[stmtExpr] = env; 398 return Parent::mutate( stmtExpr );399 399 } 400 400
Note: See TracChangeset
for help on using the changeset viewer.