| 1 | /*
|
|---|
| 2 | * This file is part of the Cforall project
|
|---|
| 3 | *
|
|---|
| 4 | * $Id: CopyParams.cc,v 1.3 2005/08/29 20:14:13 rcbilson Exp $
|
|---|
| 5 | *
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #include <set>
|
|---|
| 9 | #include <map>
|
|---|
| 10 | #include <cassert>
|
|---|
| 11 |
|
|---|
| 12 | #include "SynTree/Declaration.h"
|
|---|
| 13 | #include "SynTree/Type.h"
|
|---|
| 14 | #include "SynTree/Expression.h"
|
|---|
| 15 | #include "SynTree/Statement.h"
|
|---|
| 16 | #include "SynTree/Visitor.h"
|
|---|
| 17 | #include "UniqueName.h"
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | namespace GenPoly {
|
|---|
| 21 |
|
|---|
| 22 | class CopyParams : public Visitor
|
|---|
| 23 | {
|
|---|
| 24 | public:
|
|---|
| 25 | CopyParams();
|
|---|
| 26 |
|
|---|
| 27 | virtual void visit( FunctionDecl *funcDecl );
|
|---|
| 28 | virtual void visit( AddressExpr *addrExpr );
|
|---|
| 29 |
|
|---|
| 30 | private:
|
|---|
| 31 | std::set< UniqueId > modVars;
|
|---|
| 32 | UniqueName namer;
|
|---|
| 33 | };
|
|---|
| 34 |
|
|---|
| 35 | void
|
|---|
| 36 | copyParams( std::list< Declaration* > &translationUnit )
|
|---|
| 37 | {
|
|---|
| 38 | CopyParams copier;
|
|---|
| 39 | acceptAll( translationUnit, copier );
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | CopyParams::CopyParams()
|
|---|
| 43 | : namer( "_cp" )
|
|---|
| 44 | {
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | static const std::list< Label > noLabels;
|
|---|
| 48 |
|
|---|
| 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 | }
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | void
|
|---|
| 88 | 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 | } // namespace GenPoly
|
|---|