[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 | // |
---|
[540de412] | 7 | // PolyMutator.cc -- |
---|
[51587aa] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Mon May 18 07:44:20 2015 |
---|
[25a8631] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thu Jun 22 13:47:00 2017 |
---|
| 13 | // Update Count : 17 |
---|
[51587aa] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "PolyMutator.h" |
---|
| 17 | #include "SynTree/Declaration.h" |
---|
| 18 | #include "SynTree/Type.h" |
---|
| 19 | #include "SynTree/Expression.h" |
---|
| 20 | #include "SynTree/Statement.h" |
---|
| 21 | #include "SynTree/Mutator.h" |
---|
[09f800b] | 22 | #include "SynTree/Initializer.h" |
---|
[51b7345] | 23 | |
---|
| 24 | namespace GenPoly { |
---|
[2c57025] | 25 | PolyMutator::PolyMutator() : scopeTyVars( TypeDecl::Data{} ), env( 0 ) {} |
---|
[01aeade] | 26 | |
---|
| 27 | void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) { |
---|
[dcd73d1] | 28 | SemanticError errors; |
---|
| 29 | |
---|
[01aeade] | 30 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { |
---|
| 31 | if ( ! stmtsToAddAfter.empty() ) { |
---|
| 32 | statements.splice( i, stmtsToAddAfter ); |
---|
| 33 | } // if |
---|
[dcd73d1] | 34 | try { |
---|
| 35 | *i = (*i)->acceptMutator( *this ); |
---|
| 36 | } catch ( SemanticError &e ) { |
---|
| 37 | errors.append( e ); |
---|
| 38 | } // try |
---|
[01aeade] | 39 | if ( ! stmtsToAdd.empty() ) { |
---|
| 40 | statements.splice( i, stmtsToAdd ); |
---|
| 41 | } // if |
---|
| 42 | } // for |
---|
| 43 | if ( ! stmtsToAddAfter.empty() ) { |
---|
| 44 | statements.splice( statements.end(), stmtsToAddAfter ); |
---|
| 45 | } // if |
---|
[dcd73d1] | 46 | if ( ! errors.isEmpty() ) { |
---|
| 47 | throw errors; |
---|
| 48 | } |
---|
[01aeade] | 49 | } |
---|
| 50 | |
---|
| 51 | Statement * PolyMutator::mutateStatement( Statement *stmt ) { |
---|
[45a4ea7] | 52 | // don't want statements from outer CompoundStmts to be added to this CompoundStmt |
---|
| 53 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
| 54 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
| 55 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
| 56 | stmtsToAdd.clear(); |
---|
| 57 | stmtsToAddAfter.clear(); |
---|
| 58 | |
---|
[01aeade] | 59 | Statement *newStmt = maybeMutate( stmt, *this ); |
---|
| 60 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { |
---|
| 61 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
| 62 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); |
---|
| 63 | compound->get_kids().push_back( newStmt ); |
---|
| 64 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); |
---|
| 65 | // doEndScope(); |
---|
| 66 | return compound; |
---|
| 67 | } else { |
---|
| 68 | return newStmt; |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | Expression * PolyMutator::mutateExpression( Expression *expr ) { |
---|
| 73 | if ( expr ) { |
---|
| 74 | if ( expr->get_env() ) { |
---|
| 75 | env = expr->get_env(); |
---|
| 76 | } |
---|
[540de412] | 77 | // xxx - should env be cloned (or moved) onto the result of the mutate? |
---|
[01aeade] | 78 | return expr->acceptMutator( *this ); |
---|
| 79 | } else { |
---|
| 80 | return expr; |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) { |
---|
| 85 | doBeginScope(); |
---|
| 86 | mutateStatementList( compoundStmt->get_kids() ); |
---|
| 87 | doEndScope(); |
---|
| 88 | return compoundStmt; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Statement * PolyMutator::mutate(IfStmt *ifStmt) { |
---|
[45a4ea7] | 92 | ifStmt->set_condition( mutateExpression( ifStmt->get_condition() ) ); |
---|
[01aeade] | 93 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); |
---|
| 94 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); |
---|
| 95 | return ifStmt; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | Statement * PolyMutator::mutate(WhileStmt *whileStmt) { |
---|
| 99 | whileStmt->set_condition( mutateExpression( whileStmt->get_condition() ) ); |
---|
[45a4ea7] | 100 | whileStmt->set_body( mutateStatement( whileStmt->get_body() ) ); |
---|
[01aeade] | 101 | return whileStmt; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | Statement * PolyMutator::mutate(ForStmt *forStmt) { |
---|
[145f1fc] | 105 | mutateAll( forStmt->get_initialization(), *this ); |
---|
[01aeade] | 106 | forStmt->set_condition( mutateExpression( forStmt->get_condition() ) ); |
---|
| 107 | forStmt->set_increment( mutateExpression( forStmt->get_increment() ) ); |
---|
[45a4ea7] | 108 | forStmt->set_body( mutateStatement( forStmt->get_body() ) ); |
---|
[01aeade] | 109 | return forStmt; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | Statement * PolyMutator::mutate(SwitchStmt *switchStmt) { |
---|
| 113 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
[45a4ea7] | 114 | mutateStatementList( switchStmt->get_statements() ); |
---|
[01aeade] | 115 | return switchStmt; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | Statement * PolyMutator::mutate(CaseStmt *caseStmt) { |
---|
| 119 | caseStmt->set_condition( mutateExpression( caseStmt->get_condition() ) ); |
---|
[45a4ea7] | 120 | mutateStatementList( caseStmt->get_statements() ); |
---|
[01aeade] | 121 | return caseStmt; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | Statement * PolyMutator::mutate(TryStmt *tryStmt) { |
---|
[25a8631] | 125 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
[01aeade] | 126 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
[25a8631] | 127 | tryStmt->set_finally( maybeMutate( tryStmt->get_finally(), *this ) ); |
---|
[01aeade] | 128 | return tryStmt; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { |
---|
[25a8631] | 132 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
| 133 | cathStmt->set_cond( maybeMutate( cathStmt->get_cond(), *this ) ); |
---|
| 134 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
[01aeade] | 135 | return cathStmt; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { |
---|
| 139 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
| 140 | return retStmt; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { |
---|
| 144 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
| 145 | return exprStmt; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { |
---|
| 150 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
| 151 | *i = mutateExpression( *i ); |
---|
| 152 | } // for |
---|
| 153 | return untypedExpr; |
---|
| 154 | } |
---|
[540de412] | 155 | |
---|
[d9fa60a] | 156 | Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) { |
---|
| 157 | // don't want statements from outer CompoundStmts to be added to this StmtExpr |
---|
| 158 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
| 159 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
[fc638d2] | 160 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
[d9fa60a] | 161 | |
---|
[fc638d2] | 162 | // xxx - not sure if this is needed, along with appropriate reset, but I don't think so... |
---|
| 163 | // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars ); |
---|
[d9fa60a] | 164 | |
---|
| 165 | stmtsToAdd.clear(); |
---|
| 166 | stmtsToAddAfter.clear(); |
---|
[fc638d2] | 167 | // scopeTyVars.clear(); |
---|
[d9fa60a] | 168 | |
---|
[fc638d2] | 169 | return Parent::mutate( stmtExpr ); |
---|
[d9fa60a] | 170 | } |
---|
[540de412] | 171 | |
---|
[09f800b] | 172 | Initializer *PolyMutator::mutate( SingleInit *singleInit ) { |
---|
| 173 | singleInit->set_value( mutateExpression( singleInit->get_value() ) ); |
---|
| 174 | return singleInit; |
---|
| 175 | } |
---|
[51b7345] | 176 | } // namespace GenPoly |
---|
[01aeade] | 177 | |
---|
[51587aa] | 178 | // Local Variables: // |
---|
| 179 | // tab-width: 4 // |
---|
| 180 | // mode: c++ // |
---|
| 181 | // compile-command: "make install" // |
---|
| 182 | // End: // |
---|