source: src/GenPoly/CopyParams.cc@ f1ee72e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since f1ee72e was 6943f051, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fix implicit type assertion ordering - assignment must come first

  • Property mode set to 100644
File size: 4.0 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 <set>
17#include <map>
18#include <cassert>
19
20#include "SynTree/Declaration.h"
21#include "SynTree/Type.h"
22#include "SynTree/Expression.h"
23#include "SynTree/Statement.h"
24#include "SynTree/Visitor.h"
25#include "Common/UniqueName.h"
26
27namespace GenPoly {
28 class CopyParams : public Visitor {
29 public:
30 CopyParams();
31
32 virtual void visit( FunctionDecl *funcDecl );
33 virtual void visit( AddressExpr *addrExpr );
34
35 private:
36 std::set< UniqueId > modVars;
37 UniqueName namer;
38 };
39
40 void copyParams( std::list< Declaration* > &translationUnit ) {
41 CopyParams copier;
42 acceptAll( translationUnit, copier );
43 }
44
45 CopyParams::CopyParams() : namer( "_cp" ) {}
46
47 static const std::list< Label > noLabels;
48
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 assert( (*tyVar)->get_assertions().front()->get_name() == "?=?" );
60 assignOps[ (*tyVar)->get_name() ] = (*tyVar)->get_assertions().front();
61 } // if
62 } // for
63 for ( std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); param != funcDecl->get_functionType()->get_parameters().end(); ++param ) {
64 std::set< UniqueId >::const_iterator var = modVars.find( (*param)->get_uniqueId() );
65 if ( var != modVars.end() ) {
66 TypeInstType *typeInst = dynamic_cast< TypeInstType* >( (*param)->get_type() );
67 assert( typeInst );
68 std::map< std::string, DeclarationWithType* >::const_iterator assignOp = assignOps.find( typeInst->get_name() );
69 if ( assignOp != assignOps.end() ) {
70 DeclarationWithType *oldParam = *param;
71 *param = (*param)->clone();
72 (*param)->set_mangleName( namer.newName( (*param)->get_mangleName() ) );
73 ApplicationExpr *assign = new ApplicationExpr( new VariableExpr( assignOp->second ) );
74 assign->get_args().push_back( new VariableExpr( oldParam ) );
75 assign->get_args().push_back( new VariableExpr( *param ) );
76 funcDecl->get_statements()->get_kids().push_front( new ExprStmt( noLabels, assign ) );
77 funcDecl->get_statements()->get_kids().push_front( new DeclStmt( noLabels, oldParam ) );
78 } // if
79 modVars.erase( var );
80 } // if
81 } // for
82 } // if
83 } // if
84 }
85
86 // this test is insufficient because it is possible for values to be modified by being passed to other polymorphic
87 // routines (e.g., assignment operators) without having their addresses explicitly taken. Some thought is needed to
88 // make sure that all of the correct cases are identified where copies are necessary.
89 //
90 // As a temporary measure, for correctness at the expense of performance, ignore the modVars list entirely and copy
91 // every parameter of TypeInstType* when visiting the FunctionDecl.
92 void CopyParams::visit( AddressExpr *addrExpr ) {
93 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( addrExpr->get_arg() ) ) {
94 if ( dynamic_cast< TypeInstType* >( varExpr->get_var()->get_type() ) ) {
95 modVars.insert( varExpr->get_var()->get_uniqueId() );
96 } // if
97 } // if
98 }
99} // namespace GenPoly
100
101// Local Variables: //
102// tab-width: 4 //
103// mode: c++ //
104// compile-command: "make install" //
105// End: //
Note: See TracBrowser for help on using the repository browser.