Changeset 01aeade for translator/GenPoly/CopyParams.cc
- Timestamp:
- May 19, 2015, 7:57:09 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- a08ba92
- Parents:
- 51587aa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/GenPoly/CopyParams.cc
r51587aa r01aeade 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // XXX.cc --7 // CopyParams.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : 13 // Update Count : 011 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 19 07:33:31 2015 13 // Update Count : 1 14 14 // 15 15 16 #include <set> 16 17 #include <map> … … 24 25 #include "UniqueName.h" 25 26 27 namespace GenPoly { 28 class CopyParams : public Visitor { 29 public: 30 CopyParams(); 31 32 virtual void visit( FunctionDecl *funcDecl ); 33 virtual void visit( AddressExpr *addrExpr ); 26 34 27 namespace GenPoly { 28 class CopyParams : public Visitor { 29 public: 30 CopyParams(); 31 32 virtual void visit( FunctionDecl *funcDecl ); 33 virtual void visit( AddressExpr *addrExpr ); 35 private: 36 std::set< UniqueId > modVars; 37 UniqueName namer; 38 }; 34 39 35 private: 36 std::set< UniqueId > modVars;37 UniqueName namer;38 }; 40 void copyParams( std::list< Declaration* > &translationUnit ) { 41 CopyParams copier; 42 acceptAll( translationUnit, copier ); 43 } 39 44 40 void copyParams( std::list< Declaration* > &translationUnit ) { 41 CopyParams copier; 42 acceptAll( translationUnit, copier ); 43 } 45 CopyParams::CopyParams() : namer( "_cp" ) {} 44 46 45 CopyParams::CopyParams() : namer( "_cp" ) {} 47 static const std::list< Label > noLabels; 46 48 47 static const std::list< Label > noLabels; 49 void CopyParams::visit( FunctionDecl *funcDecl ) { 50 if ( funcDecl->get_statements() ) { 51 funcDecl->get_statements()->accept( *this ); 52 53 if ( ! modVars.empty() ) { 54 std::map< std::string, DeclarationWithType* > assignOps; 55 // assume the assignment operator is the first assert param after any "type" parameter 56 for ( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 57 if ( (*tyVar)->get_kind() == TypeDecl::Any ) { 58 assert( !(*tyVar)->get_assertions().empty() ); 59 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 60 } // if 61 } // for 62 for ( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 63 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 64 if ( var != modVars.end() ) { 65 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 66 assert( typeInst ); 67 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 68 if ( assignOp != assignOps.end() ) { 69 DeclarationWithType *oldParam = *param; 70 *param = (*param)->clone(); 71 (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); 72 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 73 assign->get_args().push_back( new VariableExpr( oldParam ) ); 74 assign->get_args().push_back( new VariableExpr( *param ) ); 75 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 76 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 77 } // if 78 modVars.erase( var ); 79 } // if 80 } // for 81 } // if 82 } // if 83 } 48 84 49 void CopyParams::visit( FunctionDecl *funcDecl ) { 50 if ( funcDecl->get_statements() ) { 51 funcDecl->get_statements()->accept( *this ); 52 53 if ( ! modVars.empty() ) { 54 std::map< std::string, DeclarationWithType* > assignOps; 55 // assume the assignment operator is the first assert param after any "type" parameter 56 for ( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 57 if ( (*tyVar)->get_kind() == TypeDecl::Any ) { 58 assert( !(*tyVar)->get_assertions().empty() ); 59 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 60 } // if 61 } // for 62 for ( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 63 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 64 if ( var != modVars.end() ) { 65 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 66 assert( typeInst ); 67 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 68 if ( assignOp != assignOps.end() ) { 69 DeclarationWithType *oldParam = *param; 70 *param = (*param)->clone(); 71 (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); 72 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 73 assign->get_args().push_back( new VariableExpr( oldParam ) ); 74 assign->get_args().push_back( new VariableExpr( *param ) ); 75 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 76 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 85 // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic 86 // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to 87 // make sure that all of the correct cases are identified where copies are necessary. 88 // 89 // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy 90 // every parameter of TypeInstType* when visiting the FunctionDecl. 91 void CopyParams::visit( AddressExpr *addrExpr ) { 92 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) { 93 if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) { 94 modVars.insert( varExpr->get_var()->get_uniqueId() ); 77 95 } // if 78 modVars.erase( var ); 79 } // if 80 } // for 81 } // if 82 } // if 83 } 96 } // if 97 } 98 } // namespace GenPoly 84 99 85 // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic86 // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to87 // make sure that all of the correct cases are identified where copies are necessary.88 //89 // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy90 // every parameter of TypeInstType* when visiting the FunctionDecl.91 void CopyParams::visit( AddressExpr *addrExpr ) {92 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {93 if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {94 modVars.insert( varExpr->get_var()->get_uniqueId() );95 } // if96 } // if97 }98 } // namespace GenPoly99 100 // Local Variables: // 100 101 // tab-width: 4 //
Note: See TracChangeset
for help on using the changeset viewer.