| [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 | // | 
|---|
| [51b73452] | 15 |  | 
|---|
|  | 16 | #include "Statement.h" | 
|---|
| [d3b7937] | 17 | #include "Common/utility.h" | 
|---|
| [51b73452] | 18 | #include <algorithm> | 
|---|
|  | 19 | #include <functional> | 
|---|
| [a5a71d0] | 20 | #include "Expression.h" | 
|---|
|  | 21 | #include "Declaration.h" | 
|---|
| [23bb1b9] | 22 | #include "SynTree/VarExprReplacer.h" | 
|---|
| [51b73452] | 23 |  | 
|---|
| [c11e31c] | 24 | using std::string; | 
|---|
|  | 25 | using std::endl; | 
|---|
| [51b73452] | 26 |  | 
|---|
| [4bf5298] | 27 | CompoundStmt::CompoundStmt( std::list<Label> labels ) : Statement( labels ) { | 
|---|
| [51b73452] | 28 | } | 
|---|
|  | 29 |  | 
|---|
| [4bf5298] | 30 | CompoundStmt::CompoundStmt( const CompoundStmt &other ) : Statement( other ) { | 
|---|
| [0dd3a2f] | 31 | cloneAll( other.kids, kids ); | 
|---|
| [a5a71d0] | 32 |  | 
|---|
| [23bb1b9] | 33 | // when cloning a compound statement, we may end up cloning declarations which | 
|---|
|  | 34 | // are referred to by VariableExprs throughout the block. Cloning a VariableExpr | 
|---|
|  | 35 | // does a shallow copy, so the VariableExpr will end up pointing to the original | 
|---|
|  | 36 | // declaration. If the original declaration is deleted, e.g. because the original | 
|---|
|  | 37 | // CompoundStmt is deleted, then we have a dangling pointer. To avoid this case, | 
|---|
|  | 38 | // find all DeclarationWithType nodes (since a VariableExpr must point to a | 
|---|
|  | 39 | // DeclarationWithType) in the original CompoundStmt and map them to the cloned | 
|---|
|  | 40 | // node in the new CompoundStmt ('this'), then replace the Declarations referred to | 
|---|
|  | 41 | // by each VariableExpr according to the constructed map. Note that only the declarations | 
|---|
|  | 42 | // in the current level are collected into the map, because child CompoundStmts will | 
|---|
|  | 43 | // recursively execute this routine. There may be more efficient ways of doing | 
|---|
|  | 44 | // this. | 
|---|
|  | 45 | VarExprReplacer::DeclMap declMap; | 
|---|
|  | 46 | std::list< Statement * >::const_iterator origit = other.kids.begin(); | 
|---|
|  | 47 | for ( Statement * s : kids ) { | 
|---|
|  | 48 | assert( origit != other.kids.end() ); | 
|---|
|  | 49 | Statement * origStmt = *origit++; | 
|---|
|  | 50 | if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( s ) ) { | 
|---|
| [fc638d2] | 51 | DeclStmt * origDeclStmt = safe_dynamic_cast< DeclStmt * >( origStmt ); | 
|---|
| [23bb1b9] | 52 | if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * > ( declStmt->get_decl() ) ) { | 
|---|
| [fc638d2] | 53 | DeclarationWithType * origdwt = safe_dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ); | 
|---|
| [23bb1b9] | 54 | assert( dwt->get_name() == origdwt->get_name() ); | 
|---|
|  | 55 | declMap[ origdwt ] = dwt; | 
|---|
| [fc638d2] | 56 | } else assert( ! dynamic_cast< DeclarationWithType * > ( origDeclStmt->get_decl() ) ); | 
|---|
|  | 57 | } else assert( ! dynamic_cast< DeclStmt * > ( s ) ); | 
|---|
| [23bb1b9] | 58 | } | 
|---|
|  | 59 | if ( ! declMap.empty() ) { | 
|---|
|  | 60 | VarExprReplacer replacer( declMap ); | 
|---|
|  | 61 | accept( replacer ); | 
|---|
|  | 62 | } | 
|---|
| [51b73452] | 63 | } | 
|---|
|  | 64 |  | 
|---|
| [4bf5298] | 65 | CompoundStmt::~CompoundStmt() { | 
|---|
| [0dd3a2f] | 66 | deleteAll( kids ); | 
|---|
| [51b73452] | 67 | } | 
|---|
|  | 68 |  | 
|---|
| [de62360d] | 69 | void CompoundStmt::print( std::ostream &os, int indent ) const { | 
|---|
| [60089f4] | 70 | os << "CompoundStmt" << endl ; | 
|---|
| [44b5ca0] | 71 | printAll( kids, os, indent + 2 ); | 
|---|
| [51b73452] | 72 | } | 
|---|
|  | 73 |  | 
|---|
| [0dd3a2f] | 74 | // Local Variables: // | 
|---|
|  | 75 | // tab-width: 4 // | 
|---|
|  | 76 | // mode: c++ // | 
|---|
|  | 77 | // compile-command: "make install" // | 
|---|
|  | 78 | // End: // | 
|---|