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