| 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 | // DeclMutator.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Aaron B. Moss | 
|---|
| 10 | // Created On       : Fri Nov 27 14:44:00 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Thu Jun 22 13:49:00 2017 | 
|---|
| 13 | // Update Count     : 4 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "DeclMutator.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include "SynTree/Expression.h" | 
|---|
| 19 | #include "SynTree/Statement.h" | 
|---|
| 20 |  | 
|---|
| 21 | namespace GenPoly { | 
|---|
| 22 | DeclMutator::DeclMutator() : Mutator(), declsToAdd(1), declsToAddAfter(1) {} | 
|---|
| 23 |  | 
|---|
| 24 | DeclMutator::~DeclMutator() {} | 
|---|
| 25 |  | 
|---|
| 26 | void DeclMutator::mutateDeclarationList( std::list< Declaration* > &decls ) { | 
|---|
| 27 | for ( std::list< Declaration* >::iterator decl = decls.begin(); ; ++decl ) { | 
|---|
| 28 | // splice in new declarations after previous decl | 
|---|
| 29 | decls.splice( decl, declsToAddAfter.back() ); | 
|---|
| 30 |  | 
|---|
| 31 | if ( decl == decls.end() ) break; | 
|---|
| 32 |  | 
|---|
| 33 | // run mutator on declaration | 
|---|
| 34 | *decl = maybeMutate( *decl, *this ); | 
|---|
| 35 |  | 
|---|
| 36 | // splice in new declarations before current decl | 
|---|
| 37 | decls.splice( decl, declsToAdd.back() ); | 
|---|
| 38 | } | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | void DeclMutator::doBeginScope() { | 
|---|
| 42 | // add new decl lists for inside of scope | 
|---|
| 43 | declsToAdd.resize( declsToAdd.size()+1 ); | 
|---|
| 44 | declsToAddAfter.resize( declsToAddAfter.size()+1 ); | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 | void DeclMutator::doEndScope() { | 
|---|
| 48 | // splice any leftover declarations from this scope onto the containing scope | 
|---|
| 49 | std::vector< std::list< Declaration* > >::reverse_iterator back = declsToAdd.rbegin(); | 
|---|
| 50 | std::vector< std::list< Declaration* > >::reverse_iterator newBack = back + 1; | 
|---|
| 51 | newBack->splice( newBack->end(), *back ); | 
|---|
| 52 | declsToAdd.pop_back(); | 
|---|
| 53 |  | 
|---|
| 54 | back = declsToAddAfter.rbegin(); | 
|---|
| 55 | newBack = back + 1; | 
|---|
| 56 | newBack->splice( newBack->end(), *back ); | 
|---|
| 57 | declsToAddAfter.pop_back(); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | Statement* DeclMutator::mutateStatement( Statement *stmt ) { | 
|---|
| 61 | // shunt over to compound statement handling if applicable | 
|---|
| 62 | CompoundStmt *compoundStmt = dynamic_cast< CompoundStmt* >(stmt); | 
|---|
| 63 | if ( compoundStmt ) return mutate( compoundStmt ); | 
|---|
| 64 |  | 
|---|
| 65 | doBeginScope(); | 
|---|
| 66 |  | 
|---|
| 67 | // run mutator on statement | 
|---|
| 68 | stmt = maybeMutate( stmt, *this ); | 
|---|
| 69 | // return if no declarations to add | 
|---|
| 70 | if ( declsToAdd.back().empty() && declsToAddAfter.back().empty() ) { | 
|---|
| 71 | doEndScope(); | 
|---|
| 72 | return stmt; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | // otherwise add declarations to new compound statement | 
|---|
| 76 | CompoundStmt *compound = new CompoundStmt( noLabels ); | 
|---|
| 77 | for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) { | 
|---|
| 78 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); | 
|---|
| 79 | compound->get_kids().push_back( declStmt ); | 
|---|
| 80 | } | 
|---|
| 81 | declsToAdd.back().clear(); | 
|---|
| 82 |  | 
|---|
| 83 | // add mutated statement | 
|---|
| 84 | compound->get_kids().push_back( stmt ); | 
|---|
| 85 |  | 
|---|
| 86 | // add declarations after to new compound statement | 
|---|
| 87 | for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) { | 
|---|
| 88 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); | 
|---|
| 89 | compound->get_kids().push_back( declStmt ); | 
|---|
| 90 | } | 
|---|
| 91 | declsToAddAfter.back().clear(); | 
|---|
| 92 |  | 
|---|
| 93 | doEndScope(); | 
|---|
| 94 | return compound; | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void DeclMutator::mutateStatementList( std::list< Statement* > &stmts ) { | 
|---|
| 98 | doBeginScope(); | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | for ( std::list< Statement* >::iterator stmt = stmts.begin(); ; ++stmt ) { | 
|---|
| 102 | // add any new declarations after the previous statement | 
|---|
| 103 | for ( std::list< Declaration* >::iterator decl = declsToAddAfter.back().begin(); decl != declsToAddAfter.back().end(); ++decl ) { | 
|---|
| 104 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); | 
|---|
| 105 | stmts.insert( stmt, declStmt ); | 
|---|
| 106 | } | 
|---|
| 107 | declsToAddAfter.back().clear(); | 
|---|
| 108 |  | 
|---|
| 109 | if ( stmt == stmts.end() ) break; | 
|---|
| 110 |  | 
|---|
| 111 | // run mutator on statement | 
|---|
| 112 | *stmt = maybeMutate( *stmt, *this ); | 
|---|
| 113 |  | 
|---|
| 114 | // add any new declarations before the statement | 
|---|
| 115 | for ( std::list< Declaration* >::iterator decl = declsToAdd.back().begin(); decl != declsToAdd.back().end(); ++decl ) { | 
|---|
| 116 | DeclStmt *declStmt = new DeclStmt( noLabels, *decl ); | 
|---|
| 117 | stmts.insert( stmt, declStmt ); | 
|---|
| 118 | } | 
|---|
| 119 | declsToAdd.back().clear(); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | doEndScope(); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void DeclMutator::addDeclaration( Declaration *decl ) { | 
|---|
| 126 | declsToAdd.back().push_back( decl ); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void DeclMutator::addDeclarationAfter( Declaration *decl ) { | 
|---|
| 130 | declsToAddAfter.back().push_back( decl ); | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | CompoundStmt* DeclMutator::mutate(CompoundStmt *compoundStmt) { | 
|---|
| 134 | mutateStatementList( compoundStmt->get_kids() ); | 
|---|
| 135 | return compoundStmt; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | Statement* DeclMutator::mutate(IfStmt *ifStmt) { | 
|---|
| 139 | ifStmt->set_condition( maybeMutate( ifStmt->get_condition(), *this ) ); | 
|---|
| 140 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); | 
|---|
| 141 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); | 
|---|
| 142 | return ifStmt; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | Statement* DeclMutator::mutate(WhileStmt *whileStmt) { | 
|---|
| 146 | whileStmt->set_condition( maybeMutate( whileStmt->get_condition(), *this ) ); | 
|---|
| 147 | whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) ); | 
|---|
| 148 | return whileStmt; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | Statement* DeclMutator::mutate(ForStmt *forStmt) { | 
|---|
| 152 | mutateAll( forStmt->get_initialization(), *this ); | 
|---|
| 153 | forStmt->set_condition(  maybeMutate( forStmt->get_condition(), *this ) ); | 
|---|
| 154 | forStmt->set_increment(  maybeMutate( forStmt->get_increment(), *this ) ); | 
|---|
| 155 | forStmt->set_body(  mutateStatement( forStmt->get_body() ) ); | 
|---|
| 156 | return forStmt; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | Statement* DeclMutator::mutate(SwitchStmt *switchStmt) { | 
|---|
| 160 | switchStmt->set_condition( maybeMutate( switchStmt->get_condition(), *this ) ); | 
|---|
| 161 | mutateAll( switchStmt->get_statements(), *this ); | 
|---|
| 162 | return switchStmt; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | Statement* DeclMutator::mutate(CaseStmt *caseStmt) { | 
|---|
| 166 | caseStmt->set_condition( maybeMutate( caseStmt->get_condition(), *this ) ); | 
|---|
| 167 | mutateAll( caseStmt->get_statements(), *this ); | 
|---|
| 168 | return caseStmt; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | Statement* DeclMutator::mutate(TryStmt *tryStmt) { | 
|---|
| 172 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); | 
|---|
| 173 | mutateAll( tryStmt->get_catchers(), *this ); | 
|---|
| 174 | tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) ); | 
|---|
| 175 | return tryStmt; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 | Statement* DeclMutator::mutate(CatchStmt *catchStmt) { | 
|---|
| 179 | catchStmt->set_decl( maybeMutate( catchStmt->get_decl(), *this ) ); | 
|---|
| 180 | catchStmt->set_cond( maybeMutate( catchStmt->get_cond(), *this ) ); | 
|---|
| 181 | catchStmt->set_body( mutateStatement( catchStmt->get_body() ) ); | 
|---|
| 182 | return catchStmt; | 
|---|
| 183 | } | 
|---|
| 184 | }  // namespace GenPoly | 
|---|
| 185 |  | 
|---|
| 186 | // Local Variables: // | 
|---|
| 187 | // tab-width: 4 // | 
|---|
| 188 | // mode: c++ // | 
|---|
| 189 | // compile-command: "make install" // | 
|---|
| 190 | // End: // | 
|---|