[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 |
---|
[4e06c1e] | 11 | // Last Modified By : Peter A. Buhr |
---|
[8688ce1] | 12 | // Last Modified On : Thu Aug 4 11:26:22 2016 |
---|
| 13 | // Update Count : 16 |
---|
[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) { |
---|
| 125 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
| 126 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
| 127 | return tryStmt; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { |
---|
| 131 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
| 132 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
| 133 | return cathStmt; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { |
---|
| 137 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
| 138 | return retStmt; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { |
---|
| 142 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
| 143 | return exprStmt; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { |
---|
| 148 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
| 149 | *i = mutateExpression( *i ); |
---|
| 150 | } // for |
---|
| 151 | return untypedExpr; |
---|
| 152 | } |
---|
[540de412] | 153 | |
---|
[d9fa60a] | 154 | Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) { |
---|
| 155 | // don't want statements from outer CompoundStmts to be added to this StmtExpr |
---|
| 156 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
| 157 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
[fc638d2] | 158 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
[d9fa60a] | 159 | |
---|
[fc638d2] | 160 | // xxx - not sure if this is needed, along with appropriate reset, but I don't think so... |
---|
| 161 | // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars ); |
---|
[d9fa60a] | 162 | |
---|
| 163 | stmtsToAdd.clear(); |
---|
| 164 | stmtsToAddAfter.clear(); |
---|
[fc638d2] | 165 | // scopeTyVars.clear(); |
---|
[d9fa60a] | 166 | |
---|
[fc638d2] | 167 | return Parent::mutate( stmtExpr ); |
---|
[d9fa60a] | 168 | } |
---|
[540de412] | 169 | |
---|
[09f800b] | 170 | Initializer *PolyMutator::mutate( SingleInit *singleInit ) { |
---|
| 171 | singleInit->set_value( mutateExpression( singleInit->get_value() ) ); |
---|
| 172 | return singleInit; |
---|
| 173 | } |
---|
[51b7345] | 174 | } // namespace GenPoly |
---|
[01aeade] | 175 | |
---|
[51587aa] | 176 | // Local Variables: // |
---|
| 177 | // tab-width: 4 // |
---|
| 178 | // mode: c++ // |
---|
| 179 | // compile-command: "make install" // |
---|
| 180 | // End: // |
---|