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