Changeset 17cd4eb for translator/GenPoly
- Timestamp:
- Jan 7, 2015, 6:04:42 PM (10 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:
- 0b8cd722
- Parents:
- d9a0e76
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
translator/GenPoly/CopyParams.cc
rd9a0e76 r17cd4eb 1 /*2 * This file is part of the Cforall project3 *4 * $Id: CopyParams.cc,v 1.3 2005/08/29 20:14:13 rcbilson Exp $5 *6 */7 8 1 #include <set> 9 2 #include <map> … … 19 12 20 13 namespace GenPoly { 14 class CopyParams : public Visitor { 15 public: 16 CopyParams(); 17 18 virtual void visit( FunctionDecl *funcDecl ); 19 virtual void visit( AddressExpr *addrExpr ); 21 20 22 class CopyParams : public Visitor 23 { 24 public: 25 CopyParams(); 26 27 virtual void visit( FunctionDecl *funcDecl ); 28 virtual void visit( AddressExpr *addrExpr ); 21 private: 22 std::set< UniqueId > modVars; 23 UniqueName namer; 24 }; 29 25 30 private: 31 std::set< UniqueId > modVars;32 UniqueName namer;33 }; 26 void copyParams( std::list< Declaration* > &translationUnit ) { 27 CopyParams copier; 28 acceptAll( translationUnit, copier ); 29 } 34 30 35 void 36 copyParams( std::list< Declaration* > &translationUnit ) 37 { 38 CopyParams copier; 39 acceptAll( translationUnit, copier ); 40 } 31 CopyParams::CopyParams() : namer( "_cp" ) {} 41 32 42 CopyParams::CopyParams() 43 : namer( "_cp" ) 44 { 45 } 33 static const std::list< Label > noLabels; 46 34 47 static const std::list< Label > noLabels; 35 void CopyParams::visit( FunctionDecl *funcDecl ) { 36 if ( funcDecl->get_statements() ) { 37 funcDecl->get_statements()->accept( *this ); 38 39 if ( ! modVars.empty() ) { 40 std::map< std::string, DeclarationWithType* > assignOps; 41 // assume the assignment operator is the first assert param after any "type" parameter 42 for ( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 43 if ( (*tyVar)->get_kind() == TypeDecl::Any ) { 44 assert( !(*tyVar)->get_assertions().empty() ); 45 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 46 } // if 47 } // for 48 for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 49 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 50 if ( var != modVars.end() ) { 51 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 52 assert( typeInst ); 53 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 54 if ( assignOp != assignOps.end() ) { 55 DeclarationWithType *oldParam = *param; 56 *param = (*param)->clone(); 57 (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); 58 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 59 assign->get_args().push_back( new VariableExpr( oldParam ) ); 60 assign->get_args().push_back( new VariableExpr( *param ) ); 61 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 62 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 63 } // if 64 modVars.erase( var ); 65 } // if 66 } // for 67 } // if 68 } // if 69 } 48 70 49 void 50 CopyParams::visit( FunctionDecl *funcDecl ) 51 { 52 if( funcDecl->get_statements() ) { 53 funcDecl->get_statements()->accept( *this ); 54 55 if( !modVars.empty() ) { 56 std::map< std::string, DeclarationWithType* > assignOps; 57 // assume that the assignment operator is the first assert param after any "type" parameter 58 for( std::list< TypeDecl* >::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { 59 if( (*tyVar)->get_kind() == TypeDecl::Any ) { 60 assert( !(*tyVar)->get_assertions().empty() ); 61 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); 62 } 63 } 64 for( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { 65 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); 66 if( var != modVars.end() ) { 67 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); 68 assert( typeInst ); 69 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); 70 if( assignOp != assignOps.end() ) { 71 DeclarationWithType *oldParam = *param; 72 *param = (*param)->clone(); 73 (*param)->set_name( namer.newName() ); 74 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); 75 assign->get_args().push_back( new VariableExpr( oldParam ) ); 76 assign->get_args().push_back( new VariableExpr( *param ) ); 77 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); 78 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); 79 } 80 modVars.erase( var ); 81 } 82 } 71 // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic 72 // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to 73 // make sure that all of the correct cases are identified where copies are necessary. 74 // 75 // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy 76 // every parameter of TypeInstType* when visiting the FunctionDecl. 77 void CopyParams::visit( AddressExpr *addrExpr ) { 78 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) { 79 if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) { 80 modVars.insert( varExpr->get_var()->get_uniqueId() ); 81 } // if 82 } // if 83 83 } 84 }85 }86 87 void88 CopyParams::visit( AddressExpr *addrExpr )89 {90 if( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {91 if( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {92 modVars.insert( varExpr->get_var()->get_uniqueId() );93 }94 }95 }96 97 84 } // namespace GenPoly
Note: See TracChangeset
for help on using the changeset viewer.