[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 ) { |
---|
| 52 | Statement *newStmt = maybeMutate( stmt, *this ); |
---|
| 53 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { |
---|
| 54 | CompoundStmt *compound = new CompoundStmt( noLabels ); |
---|
| 55 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); |
---|
| 56 | compound->get_kids().push_back( newStmt ); |
---|
| 57 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); |
---|
| 58 | // doEndScope(); |
---|
| 59 | return compound; |
---|
| 60 | } else { |
---|
| 61 | return newStmt; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | Expression * PolyMutator::mutateExpression( Expression *expr ) { |
---|
| 66 | if ( expr ) { |
---|
| 67 | if ( expr->get_env() ) { |
---|
| 68 | env = expr->get_env(); |
---|
| 69 | } |
---|
[540de412] | 70 | // xxx - should env be cloned (or moved) onto the result of the mutate? |
---|
[01aeade] | 71 | return expr->acceptMutator( *this ); |
---|
| 72 | } else { |
---|
| 73 | return expr; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) { |
---|
| 78 | doBeginScope(); |
---|
| 79 | mutateStatementList( compoundStmt->get_kids() ); |
---|
| 80 | doEndScope(); |
---|
| 81 | return compoundStmt; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | Statement * PolyMutator::mutate(IfStmt *ifStmt) { |
---|
| 85 | ifStmt->set_thenPart( mutateStatement( ifStmt->get_thenPart() ) ); |
---|
| 86 | ifStmt->set_elsePart( mutateStatement( ifStmt->get_elsePart() ) ); |
---|
| 87 | ifStmt->set_condition( mutateExpression( ifStmt->get_condition() ) ); |
---|
| 88 | return ifStmt; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | Statement * PolyMutator::mutate(WhileStmt *whileStmt) { |
---|
| 92 | whileStmt->set_body( mutateStatement( whileStmt->get_body() ) ); |
---|
| 93 | whileStmt->set_condition( mutateExpression( whileStmt->get_condition() ) ); |
---|
| 94 | return whileStmt; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | Statement * PolyMutator::mutate(ForStmt *forStmt) { |
---|
| 98 | forStmt->set_body( mutateStatement( forStmt->get_body() ) ); |
---|
[145f1fc] | 99 | mutateAll( forStmt->get_initialization(), *this ); |
---|
[01aeade] | 100 | forStmt->set_condition( mutateExpression( forStmt->get_condition() ) ); |
---|
| 101 | forStmt->set_increment( mutateExpression( forStmt->get_increment() ) ); |
---|
| 102 | return forStmt; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | Statement * PolyMutator::mutate(SwitchStmt *switchStmt) { |
---|
[8688ce1] | 106 | mutateStatementList( switchStmt->get_statements() ); |
---|
[01aeade] | 107 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); |
---|
| 108 | return switchStmt; |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | Statement * PolyMutator::mutate(CaseStmt *caseStmt) { |
---|
| 112 | mutateStatementList( caseStmt->get_statements() ); |
---|
| 113 | caseStmt->set_condition( mutateExpression( caseStmt->get_condition() ) ); |
---|
| 114 | return caseStmt; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | Statement * PolyMutator::mutate(TryStmt *tryStmt) { |
---|
| 118 | tryStmt->set_block( maybeMutate( tryStmt->get_block(), *this ) ); |
---|
| 119 | mutateAll( tryStmt->get_catchers(), *this ); |
---|
| 120 | return tryStmt; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { |
---|
| 124 | cathStmt->set_body( mutateStatement( cathStmt->get_body() ) ); |
---|
| 125 | cathStmt->set_decl( maybeMutate( cathStmt->get_decl(), *this ) ); |
---|
| 126 | return cathStmt; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { |
---|
| 130 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); |
---|
| 131 | return retStmt; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { |
---|
| 135 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); |
---|
| 136 | return exprStmt; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | |
---|
| 140 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { |
---|
| 141 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { |
---|
| 142 | *i = mutateExpression( *i ); |
---|
| 143 | } // for |
---|
| 144 | return untypedExpr; |
---|
| 145 | } |
---|
[540de412] | 146 | |
---|
[d9fa60a] | 147 | Expression *PolyMutator::mutate( StmtExpr * stmtExpr ) { |
---|
| 148 | // don't want statements from outer CompoundStmts to be added to this StmtExpr |
---|
| 149 | ValueGuard< std::list< Statement* > > oldStmtsToAdd( stmtsToAdd ); |
---|
| 150 | ValueGuard< std::list< Statement* > > oldStmtsToAddAfter( stmtsToAddAfter ); |
---|
[fc638d2] | 151 | ValueGuard< TypeSubstitution * > oldEnv( env ); |
---|
[d9fa60a] | 152 | |
---|
[fc638d2] | 153 | // xxx - not sure if this is needed, along with appropriate reset, but I don't think so... |
---|
| 154 | // ValueGuard< TyVarMap > oldScopeTyVars( scopeTyVars ); |
---|
[d9fa60a] | 155 | |
---|
| 156 | stmtsToAdd.clear(); |
---|
| 157 | stmtsToAddAfter.clear(); |
---|
[fc638d2] | 158 | // scopeTyVars.clear(); |
---|
[d9fa60a] | 159 | |
---|
[fc638d2] | 160 | return Parent::mutate( stmtExpr ); |
---|
[d9fa60a] | 161 | } |
---|
[540de412] | 162 | |
---|
[09f800b] | 163 | Initializer *PolyMutator::mutate( SingleInit *singleInit ) { |
---|
| 164 | singleInit->set_value( mutateExpression( singleInit->get_value() ) ); |
---|
| 165 | return singleInit; |
---|
| 166 | } |
---|
[51b7345] | 167 | } // namespace GenPoly |
---|
[01aeade] | 168 | |
---|
[51587aa] | 169 | // Local Variables: // |
---|
| 170 | // tab-width: 4 // |
---|
| 171 | // mode: c++ // |
---|
| 172 | // compile-command: "make install" // |
---|
| 173 | // End: // |
---|