[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 | // ChooseMutator.cc -- |
---|
[51587aa] | 8 | // |
---|
[843054c2] | 9 | // Author : Rodolfo G. Esteves |
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[4162aea9] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Wed Jun 03 15:30:20 2015 |
---|
| 13 | // Update Count : 5 |
---|
[51587aa] | 14 | // |
---|
[a08ba92] | 15 | |
---|
[51b7345] | 16 | #include <list> |
---|
| 17 | |
---|
| 18 | #include "SynTree/Statement.h" |
---|
| 19 | #include "ChooseMutator.h" |
---|
| 20 | |
---|
| 21 | namespace ControlStruct { |
---|
[a08ba92] | 22 | Statement *ChooseMutator::mutate( ChooseStmt *chooseStmt ) { |
---|
| 23 | bool enclosingChoose = insideChoose; |
---|
| 24 | insideChoose = true; |
---|
| 25 | mutateAll( chooseStmt->get_branches(), *this ); |
---|
| 26 | insideChoose = enclosingChoose; |
---|
| 27 | return new SwitchStmt( chooseStmt->get_labels(), chooseStmt->get_condition(), chooseStmt->get_branches() ); |
---|
| 28 | } |
---|
[51b7345] | 29 | |
---|
[a08ba92] | 30 | Statement *ChooseMutator::mutate( SwitchStmt *switchStmt ) { |
---|
| 31 | bool enclosingChoose = insideChoose; |
---|
| 32 | insideChoose = false; |
---|
| 33 | mutateAll( switchStmt->get_branches(), *this ); |
---|
| 34 | insideChoose = enclosingChoose; |
---|
| 35 | return switchStmt; |
---|
| 36 | } |
---|
[51b7345] | 37 | |
---|
[a08ba92] | 38 | Statement *ChooseMutator::mutate( FallthruStmt *fallthruStmt ) { |
---|
| 39 | delete fallthruStmt; |
---|
| 40 | return new NullStmt(); |
---|
| 41 | } |
---|
[51b7345] | 42 | |
---|
[a08ba92] | 43 | Statement* ChooseMutator::mutate( CaseStmt *caseStmt ) { |
---|
| 44 | std::list< Statement * > &stmts = caseStmt->get_statements(); |
---|
[d9a0e76] | 45 | |
---|
[4162aea9] | 46 | // the difference between switch and choose is that switch has an implicit fallthrough |
---|
| 47 | // to the next case, whereas choose has an implicit break at the end of the current case. |
---|
| 48 | // thus to transform a choose statement into a switch, we only need to insert breaks at the |
---|
| 49 | // end of any case that doesn't already end in a break and that doesn't end in a fallthru |
---|
| 50 | |
---|
[a08ba92] | 51 | if ( insideChoose ) { |
---|
| 52 | BranchStmt *posBrk; |
---|
| 53 | if ( (( posBrk = dynamic_cast< BranchStmt * > ( stmts.back() ) ) && |
---|
| 54 | ( posBrk->get_type() == BranchStmt::Break )) // last statement in the list is a (superfluous) 'break' |
---|
| 55 | || dynamic_cast< FallthruStmt * > ( stmts.back() ) ) |
---|
| 56 | ; |
---|
| 57 | else { |
---|
| 58 | stmts.push_back( new BranchStmt( std::list< Label >(), "", BranchStmt::Break ) ); |
---|
| 59 | } // if |
---|
| 60 | } // if |
---|
[d9a0e76] | 61 | |
---|
[a08ba92] | 62 | mutateAll ( stmts, *this ); |
---|
| 63 | return caseStmt; |
---|
| 64 | } |
---|
[51b7345] | 65 | } // namespace ControlStruct |
---|
[a08ba92] | 66 | |
---|
[51587aa] | 67 | // Local Variables: // |
---|
| 68 | // tab-width: 4 // |
---|
| 69 | // mode: c++ // |
---|
| 70 | // compile-command: "make install" // |
---|
| 71 | // End: // |
---|