| 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 | // ChooseMutator.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Rodolfo G. Esteves | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Rob Schluntz | 
|---|
| 12 | // Last Modified On : Wed Jun 03 15:30:20 2015 | 
|---|
| 13 | // Update Count     : 5 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include <list> | 
|---|
| 17 |  | 
|---|
| 18 | #include "SynTree/Statement.h" | 
|---|
| 19 | #include "ChooseMutator.h" | 
|---|
| 20 |  | 
|---|
| 21 | namespace ControlStruct { | 
|---|
| 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 | } | 
|---|
| 29 |  | 
|---|
| 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 | } | 
|---|
| 37 |  | 
|---|
| 38 | Statement *ChooseMutator::mutate( FallthruStmt *fallthruStmt ) { | 
|---|
| 39 | delete fallthruStmt; | 
|---|
| 40 | return new NullStmt(); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 | Statement* ChooseMutator::mutate( CaseStmt *caseStmt ) { | 
|---|
| 44 | std::list< Statement * > &stmts = caseStmt->get_statements(); | 
|---|
| 45 |  | 
|---|
| 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 |  | 
|---|
| 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 | 
|---|
| 61 |  | 
|---|
| 62 | mutateAll ( stmts, *this ); | 
|---|
| 63 | return caseStmt; | 
|---|
| 64 | } | 
|---|
| 65 | } // namespace ControlStruct | 
|---|
| 66 |  | 
|---|
| 67 | // Local Variables: // | 
|---|
| 68 | // tab-width: 4 // | 
|---|
| 69 | // mode: c++ // | 
|---|
| 70 | // compile-command: "make install" // | 
|---|
| 71 | // End: // | 
|---|