[0dd3a2f] | 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 | // |
---|
[a5a71d0] | 7 | // XXX.cc -- |
---|
[0dd3a2f] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[a5a71d0] | 11 | // Last Modified By : Rob Schluntz |
---|
[d7903b1] | 12 | // Last Modified On : Mon May 02 15:19:17 2016 |
---|
[de62360d] | 13 | // Update Count : 3 |
---|
[0dd3a2f] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "Statement.h" |
---|
[d3b7937] | 17 | #include "Common/utility.h" |
---|
[51b7345] | 18 | #include <algorithm> |
---|
| 19 | #include <functional> |
---|
[a5a71d0] | 20 | #include "Expression.h" |
---|
| 21 | #include "Declaration.h" |
---|
[51b7345] | 22 | |
---|
[c11e31c] | 23 | using std::string; |
---|
| 24 | using std::endl; |
---|
[51b7345] | 25 | |
---|
[a5a71d0] | 26 | class VarExprReplacer : public Visitor { |
---|
| 27 | public: |
---|
| 28 | typedef std::map< DeclarationWithType *, DeclarationWithType * > DeclMap; |
---|
| 29 | private: |
---|
| 30 | const DeclMap & declMap; |
---|
| 31 | public: |
---|
| 32 | VarExprReplacer( const DeclMap & declMap ) : declMap( declMap ) {} |
---|
| 33 | |
---|
| 34 | // replace variable with new node from decl map |
---|
| 35 | virtual void visit( VariableExpr * varExpr ) { |
---|
| 36 | if ( declMap.count( varExpr->get_var() ) ) { |
---|
| 37 | varExpr->set_var( declMap.at( varExpr->get_var() ) ); |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | }; |
---|
| 41 | |
---|
| 42 | |
---|
[4bf5298] | 43 | CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) { |
---|
[51b7345] | 44 | } |
---|
| 45 | |
---|
[4bf5298] | 46 | CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) { |
---|
[0dd3a2f] | 47 | cloneAll( other.kids, kids ); |
---|
[a5a71d0] | 48 | |
---|
| 49 | // when cloning a compound statement, we may end up cloning declarations which |
---|
| 50 | // are referred to by VariableExprs throughout the block. Cloning a VariableExpr |
---|
| 51 | // does a shallow copy, so the VariableExpr will end up pointing to the original |
---|
| 52 | // declaration. If the original declaration is deleted, e.g. because the original |
---|
| 53 | // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case, |
---|
| 54 | // find all DeclarationWithType nodes (since a VariableExpr must point to a |
---|
| 55 | // DeclarationWithType) in the original CompoundStmt and map them to the cloned |
---|
| 56 | // node in the new CompoundStmt ('this'), then replace the Declarations referred to |
---|
| 57 | // by each VariableExpr according to the constructed map. Note that only the declarations |
---|
| 58 | // in the current level are collected into the map, because child CompoundStmts will |
---|
| 59 | // recursively execute this routine. There may be more efficient ways of doing |
---|
| 60 | // this. |
---|
| 61 | VarExprReplacer::DeclMap declMap; |
---|
| 62 | std::list< Statement * >::const_iterator origit = other.kids.begin(); |
---|
| 63 | for ( Statement * s : kids ) { |
---|
| 64 | assert( origit != other.kids.end() ); |
---|
| 65 | if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) { |
---|
| 66 | DeclStmt * origDeclStmt = dynamic_cast< DeclStmt * >( *origit ); |
---|
| 67 | assert( origDeclStmt ); |
---|
| 68 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) { |
---|
| 69 | DeclarationWithType * origdwt = dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ); |
---|
| 70 | assert( origdwt ); |
---|
| 71 | declMap[ origdwt ] = dwt; |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | if ( ! declMap.empty() ) { |
---|
| 76 | VarExprReplacer replacer( declMap ); |
---|
| 77 | accept( replacer ); |
---|
| 78 | } |
---|
[51b7345] | 79 | } |
---|
| 80 | |
---|
[4bf5298] | 81 | CompoundStmt::~CompoundStmt() { |
---|
[0dd3a2f] | 82 | deleteAll( kids ); |
---|
[51b7345] | 83 | } |
---|
| 84 | |
---|
[de62360d] | 85 | void CompoundStmt::print( std::ostream &os, int indent ) const { |
---|
[60089f4] | 86 | os << "CompoundStmt" << endl ; |
---|
[44b5ca0] | 87 | printAll( kids, os, indent + 2 ); |
---|
[51b7345] | 88 | } |
---|
| 89 | |
---|
[0dd3a2f] | 90 | // Local Variables: // |
---|
| 91 | // tab-width: 4 // |
---|
| 92 | // mode: c++ // |
---|
| 93 | // compile-command: "make install" // |
---|
| 94 | // End: // |
---|