| [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 | 
|---|
| [25a8631] | 11 | // Last Modified By : Andrew Beach | 
|---|
|  | 12 | // Last Modified On : Thu Jun 22 13:49:00 2017 | 
|---|
|  | 13 | // Update Count     : 4 | 
|---|
| [b0b958a] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #include "DeclMutator.h" | 
|---|
|  | 17 |  | 
|---|
|  | 18 | #include "SynTree/Expression.h" | 
|---|
|  | 19 | #include "SynTree/Statement.h" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | namespace GenPoly { | 
|---|
| [dbd8652] | 22 | DeclMutator::DeclMutator() : Mutator(), declsToAdd(1), declsToAddAfter(1) {} | 
|---|
| [b0b958a] | 23 |  | 
|---|
|  | 24 | DeclMutator::~DeclMutator() {} | 
|---|
| [8ca3a72] | 25 |  | 
|---|
| [b0b958a] | 26 | void DeclMutator::mutateDeclarationList( std::list< Declaration* > &decls ) { | 
|---|
| [dbd8652] | 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; | 
|---|
| [8ca3a72] | 32 |  | 
|---|
| [b0b958a] | 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() { | 
|---|
| [dbd8652] | 42 | // add new decl lists for inside of scope | 
|---|
| [b0b958a] | 43 | declsToAdd.resize( declsToAdd.size()+1 ); | 
|---|
| [dbd8652] | 44 | declsToAddAfter.resize( declsToAddAfter.size()+1 ); | 
|---|
| [b0b958a] | 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(); | 
|---|
| [8ca3a72] | 53 |  | 
|---|
| [dbd8652] | 54 | back = declsToAddAfter.rbegin(); | 
|---|
|  | 55 | newBack = back + 1; | 
|---|
|  | 56 | newBack->splice( newBack->end(), *back ); | 
|---|
|  | 57 | declsToAddAfter.pop_back(); | 
|---|
| [b0b958a] | 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 ); | 
|---|
| [8ca3a72] | 64 |  | 
|---|
| [b0b958a] | 65 | doBeginScope(); | 
|---|
| [8ca3a72] | 66 |  | 
|---|
| [b0b958a] | 67 | // run mutator on statement | 
|---|
|  | 68 | stmt = maybeMutate( stmt, *this ); | 
|---|
|  | 69 | // return if no declarations to add | 
|---|
| [dbd8652] | 70 | if ( declsToAdd.back().empty() && declsToAddAfter.back().empty() ) { | 
|---|
|  | 71 | doEndScope(); | 
|---|
|  | 72 | return stmt; | 
|---|
|  | 73 | } | 
|---|
| [b0b958a] | 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 |  | 
|---|
| [dbd8652] | 83 | // add mutated statement | 
|---|
| [b0b958a] | 84 | compound->get_kids().push_back( stmt ); | 
|---|
| [dbd8652] | 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(); | 
|---|
| [b0b958a] | 94 | return compound; | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
|  | 97 | void DeclMutator::mutateStatementList( std::list< Statement* > &stmts ) { | 
|---|
|  | 98 | doBeginScope(); | 
|---|
| [dbd8652] | 99 |  | 
|---|
| [8ca3a72] | 100 |  | 
|---|
| [dbd8652] | 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; | 
|---|
| [8ca3a72] | 110 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 121 |  | 
|---|
| [b0b958a] | 122 | doEndScope(); | 
|---|
|  | 123 | } | 
|---|
|  | 124 |  | 
|---|
|  | 125 | void DeclMutator::addDeclaration( Declaration *decl ) { | 
|---|
|  | 126 | declsToAdd.back().push_back( decl ); | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
| [dbd8652] | 129 | void DeclMutator::addDeclarationAfter( Declaration *decl ) { | 
|---|
|  | 130 | declsToAddAfter.back().push_back( decl ); | 
|---|
|  | 131 | } | 
|---|
|  | 132 |  | 
|---|
| [b0b958a] | 133 | CompoundStmt* DeclMutator::mutate(CompoundStmt *compoundStmt) { | 
|---|
|  | 134 | mutateStatementList( compoundStmt->get_kids() ); | 
|---|
|  | 135 | return compoundStmt; | 
|---|
|  | 136 | } | 
|---|
| [8ca3a72] | 137 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 144 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 150 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 158 |  | 
|---|
| [b0b958a] | 159 | Statement* DeclMutator::mutate(SwitchStmt *switchStmt) { | 
|---|
|  | 160 | switchStmt->set_condition( maybeMutate( switchStmt->get_condition(), *this ) ); | 
|---|
| [8688ce1] | 161 | mutateAll( switchStmt->get_statements(), *this ); | 
|---|
| [b0b958a] | 162 | return switchStmt; | 
|---|
|  | 163 | } | 
|---|
| [8ca3a72] | 164 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 170 |  | 
|---|
| [b0b958a] | 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 | } | 
|---|
| [8ca3a72] | 177 |  | 
|---|
| [b0b958a] | 178 | Statement* DeclMutator::mutate(CatchStmt *catchStmt) { | 
|---|
|  | 179 | catchStmt->set_decl( maybeMutate( catchStmt->get_decl(), *this ) ); | 
|---|
| [25a8631] | 180 | catchStmt->set_cond( maybeMutate( catchStmt->get_cond(), *this ) ); | 
|---|
| [b0b958a] | 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: // | 
|---|