| 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 | //
|
|---|
| 7 | // CaseRangeMutator.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By :
|
|---|
| 12 | // Last Modified On : Sun Jul 31 12:16:28 2016
|
|---|
| 13 | // Update Count : 32
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <list>
|
|---|
| 17 | #include <cassert>
|
|---|
| 18 | #include <cstdlib>
|
|---|
| 19 | #include <iterator>
|
|---|
| 20 |
|
|---|
| 21 | #include "Common/utility.h"
|
|---|
| 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 {
|
|---|
| 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() ) {
|
|---|
| 36 | (*i)->acceptMutator( *this );
|
|---|
| 37 |
|
|---|
| 38 | if ( ! newCaseLabels.empty() ) {
|
|---|
| 39 | std::cout << "FRED" << std::endl;
|
|---|
| 40 | std::list< Statement * > newCases;
|
|---|
| 41 |
|
|---|
| 42 | // transform( newCaseLabels.begin(), newCaseLabels.end(), bnd1st( ptr_fun( ctor< CaseStmt, Label, Expression * > ) ) );
|
|---|
| 43 |
|
|---|
| 44 | for ( std::list< Expression * >::iterator j = newCaseLabels.begin(); j != newCaseLabels.end(); j++ ) {
|
|---|
| 45 | std::list< Label > emptyLabels;
|
|---|
| 46 | std::list< Statement * > emptyStmts;
|
|---|
| 47 | newCases.push_back( new CaseStmt( emptyLabels, *j, emptyStmts ) );
|
|---|
| 48 | } // for
|
|---|
| 49 |
|
|---|
| 50 | if ( CaseStmt *currentCase = dynamic_cast< CaseStmt * >( *i ) )
|
|---|
| 51 | if ( ! currentCase->get_statements().empty() ) {
|
|---|
| 52 | CaseStmt *lastCase = dynamic_cast< CaseStmt * >( newCases.back() );
|
|---|
| 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
|
|---|
| 57 | std::list< Statement * >::iterator j = i;
|
|---|
| 58 | advance( j, 1 );
|
|---|
| 59 | replace( cases, i, newCases );
|
|---|
| 60 | i = j;
|
|---|
| 61 | newCaseLabels.clear();
|
|---|
| 62 | } else
|
|---|
| 63 | i++;
|
|---|
| 64 | } // while
|
|---|
| 65 |
|
|---|
| 66 | return switchStmt;
|
|---|
| 67 | } // CaseRangeMutator::mutate
|
|---|
| 68 |
|
|---|
| 69 | Statement *CaseRangeMutator::mutate( CaseStmt *caseStmt ) {
|
|---|
| 70 | // case list, e.g., case 1, 3, 5:
|
|---|
| 71 | if ( TupleExpr *tcond = dynamic_cast< TupleExpr * >( caseStmt->get_condition() ) ) {
|
|---|
| 72 | assert( ! tcond->get_exprs().empty() );
|
|---|
| 73 | for ( std::list< Expression * >::iterator i = tcond->get_exprs().begin(); i != tcond->get_exprs().end(); i++ ) {
|
|---|
| 74 | newCaseLabels.push_back( *i ); // do I need to clone them?
|
|---|
| 75 | } // for
|
|---|
| 76 | } // if
|
|---|
| 77 |
|
|---|
| 78 | std::list< Statement * > &stmts = caseStmt->get_statements();
|
|---|
| 79 | mutateAll( stmts, *this );
|
|---|
| 80 |
|
|---|
| 81 | return caseStmt;
|
|---|
| 82 | } // CaseRangeMutator::mutate
|
|---|
| 83 | } // namespace ControlStruct
|
|---|
| 84 |
|
|---|
| 85 | // Local Variables: //
|
|---|
| 86 | // tab-width: 4 //
|
|---|
| 87 | // mode: c++ //
|
|---|
| 88 | // compile-command: "make install" //
|
|---|
| 89 | // End: //
|
|---|