| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // CopyParams.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Rob Schluntz | 
|---|
| 12 | // Last Modified On : Tue May 19 07:33:31 2015 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <cassert>                 // for assert | 
|---|
| 17 | #include <list>                    // for list, _List_iterator, _List_const_... | 
|---|
| 18 | #include <map>                     // for map, _Rb_tree_const_iterator, map<... | 
|---|
| 19 | #include <set>                     // for set, set<>::const_iterator | 
|---|
| 20 | #include <string>                  // for string, operator== | 
|---|
| 21 | #include <utility>                 // for pair | 
|---|
| 22 |  | 
|---|
| 23 | #include "Common/SemanticError.h"  // for SemanticError | 
|---|
| 24 | #include "Common/UniqueName.h"     // for UniqueName | 
|---|
| 25 | #include "SynTree/Declaration.h"   // for DeclarationWithType, TypeDecl, Fun... | 
|---|
| 26 | #include "SynTree/Expression.h"    // for VariableExpr, ApplicationExpr, Add... | 
|---|
| 27 | #include "SynTree/Label.h"         // for Label, noLabels | 
|---|
| 28 | #include "SynTree/Statement.h"     // for CompoundStmt, DeclStmt, ExprStmt | 
|---|
| 29 | #include "SynTree/SynTree.h"       // for UniqueId | 
|---|
| 30 | #include "SynTree/Type.h"          // for FunctionType, TypeInstType, Type | 
|---|
| 31 | #include "SynTree/Visitor.h"       // for acceptAll, Visitor | 
|---|
| 32 |  | 
|---|
| 33 | namespace GenPoly { | 
|---|
| 34 | class CopyParams : public Visitor { | 
|---|
| 35 | public: | 
|---|
| 36 | CopyParams(); | 
|---|
| 37 |  | 
|---|
| 38 | virtual void visit( FunctionDecl *funcDecl ); | 
|---|
| 39 | virtual void visit( AddressExpr *addrExpr ); | 
|---|
| 40 |  | 
|---|
| 41 | private: | 
|---|
| 42 | std::set< UniqueId > modVars; | 
|---|
| 43 | UniqueName namer; | 
|---|
| 44 | }; | 
|---|
| 45 |  | 
|---|
| 46 | /// creates local copies of polymorphic function parameters | 
|---|
| 47 | void copyParams( std::list< Declaration* > &translationUnit ) { | 
|---|
| 48 | CopyParams copier; | 
|---|
| 49 | acceptAll( translationUnit, copier ); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | CopyParams::CopyParams() : namer( "_cp" ) {} | 
|---|
| 53 |  | 
|---|
| 54 | void CopyParams::visit( FunctionDecl *funcDecl ) { | 
|---|
| 55 | if ( funcDecl->get_statements() ) { | 
|---|
| 56 | funcDecl->get_statements()->accept( *this ); | 
|---|
| 57 |  | 
|---|
| 58 | if ( ! modVars.empty() ) { | 
|---|
| 59 | std::map< std::string, DeclarationWithType* > assignOps; | 
|---|
| 60 | // xxx - this needs to use constructors, not assignment operators | 
|---|
| 61 | // assume the assignment operator is the first assert param after any "type" parameter | 
|---|
| 62 | for ( Type::ForallList::const_iterator tyVar = funcDecl->get_functionType()->get_forall().begin(); tyVar != funcDecl->get_functionType()->get_forall().end(); ++tyVar ) { | 
|---|
| 63 | if ( (*tyVar)->get_kind() == TypeDecl::Any ) { | 
|---|
| 64 | assert( !(*tyVar)->get_assertions().empty() ); | 
|---|
| 65 | assert( (*tyVar)->get_assertions().front()->get_name() == "?=?" ); | 
|---|
| 66 | assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front(); | 
|---|
| 67 | } // if | 
|---|
| 68 | } // for | 
|---|
| 69 | for ( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { | 
|---|
| 70 | std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() ); | 
|---|
| 71 | if ( var != modVars.end() ) { | 
|---|
| 72 | TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() ); | 
|---|
| 73 | assert( typeInst ); | 
|---|
| 74 | std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() ); | 
|---|
| 75 | if ( assignOp != assignOps.end() ) { | 
|---|
| 76 | DeclarationWithType *oldParam = *param; | 
|---|
| 77 | *param = (*param)->clone(); | 
|---|
| 78 | (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) ); | 
|---|
| 79 | ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) ); | 
|---|
| 80 | assign->get_args().push_back( new VariableExpr( oldParam ) ); | 
|---|
| 81 | assign->get_args().push_back( new VariableExpr( *param ) ); | 
|---|
| 82 | funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) ); | 
|---|
| 83 | funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) ); | 
|---|
| 84 | } // if | 
|---|
| 85 | modVars.erase( var ); | 
|---|
| 86 | } // if | 
|---|
| 87 | } // for | 
|---|
| 88 | } // if | 
|---|
| 89 | } // if | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic | 
|---|
| 93 | // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to | 
|---|
| 94 | // make sure that all of the correct cases are identified where copies are necessary. | 
|---|
| 95 | // | 
|---|
| 96 | // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy | 
|---|
| 97 | // every parameter of TypeInstType* when visiting the FunctionDecl. | 
|---|
| 98 | void CopyParams::visit( AddressExpr *addrExpr ) { | 
|---|
| 99 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) { | 
|---|
| 100 | if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) { | 
|---|
| 101 | modVars.insert( varExpr->get_var()->get_uniqueId() ); | 
|---|
| 102 | } // if | 
|---|
| 103 | } // if | 
|---|
| 104 | } | 
|---|
| 105 | } // namespace GenPoly | 
|---|
| 106 |  | 
|---|
| 107 | // Local Variables: // | 
|---|
| 108 | // tab-width: 4 // | 
|---|
| 109 | // mode: c++ // | 
|---|
| 110 | // compile-command: "make install" // | 
|---|
| 111 | // End: // | 
|---|