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