source: src/GenPoly/CopyParams.cc @ 9dbf7c8

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9dbf7c8 was 9dbf7c8, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Modify CopyParams? to use DWT pointers rather than unique ids

  • Property mode set to 100644
File size: 4.6 KB
Line 
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
33namespace 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< DeclarationWithType * > 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->statements ) {
56                        funcDecl->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->type->forall.begin(); tyVar != funcDecl->type->forall.end(); ++tyVar ) {
63                                        if ( (*tyVar)->get_kind() == TypeDecl::Any ) {
64                                                assert( !(*tyVar)->assertions.empty() );
65                                                assert( (*tyVar)->assertions.front()->name == "?=?" );
66                                                assignOps[ (*tyVar)->name ] = (*tyVar)->assertions.front();
67                                        } // if
68                                } // for
69                                for ( std::list< DeclarationWithType* >::iterator param = funcDecl->type->parameters.begin(); param != funcDecl->type->parameters.end(); ++param ) {
70                                        std::set< DeclarationWithType * >::const_iterator var = modVars.find( *param );
71                                        if ( var != modVars.end() ) {
72                                                TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() );
73                                                std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->name );
74                                                if ( assignOp != assignOps.end() ) {
75                                                        DeclarationWithType *oldParam = *param;
76                                                        *param = (*param)->clone();
77                                                        (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) );
78                                                        ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) );
79                                                        assign->args.push_back( new VariableExpr( oldParam ) );
80                                                        assign->args.push_back( new VariableExpr( *param ) );
81                                                        funcDecl->statements->push_front( new ExprStmt( noLabels, assign ) );
82                                                        funcDecl->statements->push_front( new DeclStmt( noLabels, oldParam ) );
83                                                } // if
84                                                modVars.erase( var );
85                                        } // if
86                                } // for
87                        } // if
88                } // if
89        }
90
91        // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic
92        // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to
93        // make sure that all of the correct cases are identified where copies are necessary.
94        //
95        // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy
96        // every parameter of TypeInstType* when visiting the FunctionDecl.
97        void CopyParams::visit( AddressExpr *addrExpr ) {
98                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->arg ) ) {
99                        if ( dynamic_cast< TypeInstType* >( varExpr->var->get_type() ) ) {
100                                modVars.insert( varExpr->var );
101                        } // if
102                } // if
103        }
104} // namespace GenPoly
105
106// Local Variables: //
107// tab-width: 4 //
108// mode: c++ //
109// compile-command: "make install" //
110// End: //
Note: See TracBrowser for help on using the repository browser.