[51587aa] | 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 | // |
---|
[a08ba92] | 7 | // CaseRangeMutator.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[a08ba92] | 11 | // Last Modified By : Peter A. Buhr |
---|
[057b34f] | 12 | // Last Modified On : Sun Jul 31 00:14:16 2016 |
---|
| 13 | // Update Count : 29 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[51b7345] | 16 | #include <list> |
---|
| 17 | #include <cassert> |
---|
| 18 | #include <cstdlib> |
---|
| 19 | #include <iterator> |
---|
| 20 | |
---|
[d3b7937] | 21 | #include "Common/utility.h" |
---|
[51b7345] | 22 | |
---|
| 23 | #include "SynTree/Statement.h" |
---|
| 24 | #include "SynTree/Expression.h" |
---|
| 25 | #include "SynTree/Constant.h" |
---|
| 26 | #include "SynTree/Type.h" |
---|
| 27 | #include "CaseRangeMutator.h" |
---|
| 28 | |
---|
| 29 | namespace ControlStruct { |
---|
[a08ba92] | 30 | Statement *CaseRangeMutator::mutate( SwitchStmt *switchStmt ) { |
---|
| 31 | std::list< Statement * > &cases = switchStmt->get_branches(); |
---|
| 32 | |
---|
| 33 | // a `for' would be more natural... all this contortions are because `replace' invalidates the iterator |
---|
| 34 | std::list< Statement * >::iterator i = cases.begin(); |
---|
| 35 | while ( i != cases.end() ) { |
---|
[057b34f] | 36 | (*i)->acceptMutator( *this ); |
---|
[a08ba92] | 37 | |
---|
| 38 | if ( ! newCaseLabels.empty() ) { |
---|
| 39 | std::list< Statement * > newCases; |
---|
| 40 | |
---|
| 41 | // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) ); |
---|
| 42 | |
---|
[057b34f] | 43 | for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); j != newCaseLabels.end(); j++ ) { |
---|
| 44 | std::list< Label > emptyLabels; |
---|
| 45 | std::list< Statement * > emptyStmts; |
---|
[a08ba92] | 46 | newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) ); |
---|
| 47 | } // for |
---|
| 48 | |
---|
[057b34f] | 49 | if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * >( *i ) ) |
---|
[a08ba92] | 50 | if ( ! currentCase->get_statements().empty() ) { |
---|
[057b34f] | 51 | CaseStmt *lastCase = dynamic_cast< CaseStmt * >( newCases.back() ); |
---|
[a08ba92] | 52 | if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases |
---|
| 53 | // transfer the statement block ( if any ) to the new list: |
---|
| 54 | lastCase->set_statements( currentCase->get_statements() ); |
---|
| 55 | } // if |
---|
[057b34f] | 56 | std::list< Statement * >::iterator j = i; |
---|
| 57 | advance( j, 1 ); |
---|
| 58 | replace( cases, i, newCases ); |
---|
[a08ba92] | 59 | i = j; |
---|
| 60 | newCaseLabels.clear(); |
---|
| 61 | } else |
---|
| 62 | i++; |
---|
| 63 | } // while |
---|
| 64 | |
---|
| 65 | return switchStmt; |
---|
| 66 | } |
---|
[d9a0e76] | 67 | |
---|
[a08ba92] | 68 | Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) { |
---|
[057b34f] | 69 | if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) { // case list |
---|
[a08ba92] | 70 | assert( ! tcond->get_exprs().empty() ); |
---|
| 71 | for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) |
---|
[66d12f7] | 72 | newCaseLabels.push_back( *i ); // do I need to clone them? |
---|
[a08ba92] | 73 | } // if |
---|
| 74 | |
---|
| 75 | std::list< Statement * > &stmts = caseStmt->get_statements(); |
---|
[057b34f] | 76 | mutateAll( stmts, *this ); |
---|
[a08ba92] | 77 | |
---|
| 78 | return caseStmt; |
---|
| 79 | } |
---|
[51b7345] | 80 | } // namespace ControlStruct |
---|
[a08ba92] | 81 | |
---|
[51587aa] | 82 | // Local Variables: // |
---|
| 83 | // tab-width: 4 // |
---|
| 84 | // mode: c++ // |
---|
| 85 | // compile-command: "make install" // |
---|
| 86 | // End: // |
---|