[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
|
---|
[08061589] | 11 | // Last Modified By :
|
---|
| 12 | // Last Modified On : Sun Jul 31 12:16:28 2016
|
---|
| 13 | // Update Count : 32
|
---|
[51587aa] | 14 | //
|
---|
[a08ba92] | 15 |
|
---|
[51b73452] | 16 | #include <list>
|
---|
| 17 | #include <cassert>
|
---|
| 18 | #include <cstdlib>
|
---|
| 19 | #include <iterator>
|
---|
| 20 |
|
---|
[d3b7937] | 21 | #include "Common/utility.h"
|
---|
[51b73452] | 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() ) {
|
---|
[08061589] | 39 | std::cout << "FRED" << std::endl;
|
---|
[a08ba92] | 40 | std::list< Statement * > newCases;
|
---|
| 41 |
|
---|
| 42 | // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
|
---|
| 43 |
|
---|
[057b34f] | 44 | for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); j != newCaseLabels.end(); j++ ) {
|
---|
| 45 | std::list< Label > emptyLabels;
|
---|
| 46 | std::list< Statement * > emptyStmts;
|
---|
[a08ba92] | 47 | newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
|
---|
| 48 | } // for
|
---|
| 49 |
|
---|
[057b34f] | 50 | if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * >( *i ) )
|
---|
[a08ba92] | 51 | if ( ! currentCase->get_statements().empty() ) {
|
---|
[057b34f] | 52 | CaseStmt *lastCase = dynamic_cast< CaseStmt * >( newCases.back() );
|
---|
[a08ba92] | 53 | if ( lastCase == 0 ) { throw ( 0 ); /* FIXME */ } // something is very wrong, as I just made these, and they were all cases
|
---|
| 54 | // transfer the statement block ( if any ) to the new list:
|
---|
| 55 | lastCase->set_statements( currentCase->get_statements() );
|
---|
| 56 | } // if
|
---|
[057b34f] | 57 | std::list< Statement * >::iterator j = i;
|
---|
| 58 | advance( j, 1 );
|
---|
| 59 | replace( cases, i, newCases );
|
---|
[a08ba92] | 60 | i = j;
|
---|
| 61 | newCaseLabels.clear();
|
---|
| 62 | } else
|
---|
| 63 | i++;
|
---|
| 64 | } // while
|
---|
| 65 |
|
---|
| 66 | return switchStmt;
|
---|
[08061589] | 67 | } // CaseRangeMutator::mutate
|
---|
[d9a0e76] | 68 |
|
---|
[a08ba92] | 69 | Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) {
|
---|
[08061589] | 70 | // case list, e.g., case 1, 3, 5:
|
---|
| 71 | if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
|
---|
[a08ba92] | 72 | assert( ! tcond->get_exprs().empty() );
|
---|
[08061589] | 73 | for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) {
|
---|
[66d12f7] | 74 | newCaseLabels.push_back( *i ); // do I need to clone them?
|
---|
[08061589] | 75 | } // for
|
---|
[a08ba92] | 76 | } // if
|
---|
| 77 |
|
---|
| 78 | std::list< Statement * > &stmts = caseStmt->get_statements();
|
---|
[057b34f] | 79 | mutateAll( stmts, *this );
|
---|
[a08ba92] | 80 |
|
---|
| 81 | return caseStmt;
|
---|
[08061589] | 82 | } // CaseRangeMutator::mutate
|
---|
[51b73452] | 83 | } // namespace ControlStruct
|
---|
[a08ba92] | 84 |
|
---|
[51587aa] | 85 | // Local Variables: //
|
---|
| 86 | // tab-width: 4 //
|
---|
| 87 | // mode: c++ //
|
---|
| 88 | // compile-command: "make install" //
|
---|
| 89 | // End: //
|
---|