| [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 | // | 
|---|
| [51b73452] | 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" | 
|---|
| [51b73452] | 23 |  | 
|---|
|  | 24 | namespace GenPoly { | 
|---|
| [01aeade] | 25 | namespace { | 
|---|
|  | 26 | const std::list<Label> noLabels; | 
|---|
|  | 27 | } | 
|---|
|  | 28 |  | 
|---|
| [bfae637] | 29 | PolyMutator::PolyMutator() : scopeTyVars( (TypeDecl::Kind)-1 ), env( 0 ) {} | 
|---|
| [01aeade] | 30 |  | 
|---|
|  | 31 | void PolyMutator::mutateStatementList( std::list< Statement* > &statements ) { | 
|---|
|  | 32 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) { | 
|---|
|  | 33 | if ( ! stmtsToAddAfter.empty() ) { | 
|---|
|  | 34 | statements.splice( i, stmtsToAddAfter ); | 
|---|
|  | 35 | } // if | 
|---|
|  | 36 | *i = (*i)->acceptMutator( *this ); | 
|---|
|  | 37 | if ( ! stmtsToAdd.empty() ) { | 
|---|
|  | 38 | statements.splice( i, stmtsToAdd ); | 
|---|
|  | 39 | } // if | 
|---|
|  | 40 | } // for | 
|---|
|  | 41 | if ( ! stmtsToAddAfter.empty() ) { | 
|---|
|  | 42 | statements.splice( statements.end(), stmtsToAddAfter ); | 
|---|
|  | 43 | } // if | 
|---|
|  | 44 | } | 
|---|
|  | 45 |  | 
|---|
|  | 46 | Statement * PolyMutator::mutateStatement( Statement *stmt ) { | 
|---|
|  | 47 | Statement *newStmt = maybeMutate( stmt, *this ); | 
|---|
|  | 48 | if ( ! stmtsToAdd.empty() || ! stmtsToAddAfter.empty() ) { | 
|---|
|  | 49 | CompoundStmt *compound = new CompoundStmt( noLabels ); | 
|---|
|  | 50 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAdd ); | 
|---|
|  | 51 | compound->get_kids().push_back( newStmt ); | 
|---|
|  | 52 | compound->get_kids().splice( compound->get_kids().end(), stmtsToAddAfter ); | 
|---|
|  | 53 | // doEndScope(); | 
|---|
|  | 54 | return compound; | 
|---|
|  | 55 | } else { | 
|---|
|  | 56 | return newStmt; | 
|---|
|  | 57 | } | 
|---|
|  | 58 | } | 
|---|
|  | 59 |  | 
|---|
|  | 60 | Expression * PolyMutator::mutateExpression( Expression *expr ) { | 
|---|
|  | 61 | if ( expr ) { | 
|---|
|  | 62 | if ( expr->get_env() ) { | 
|---|
|  | 63 | env = expr->get_env(); | 
|---|
|  | 64 | } | 
|---|
| [540de412] | 65 | // xxx - should env be cloned (or moved) onto the result of the mutate? | 
|---|
| [01aeade] | 66 | return expr->acceptMutator( *this ); | 
|---|
|  | 67 | } else { | 
|---|
|  | 68 | return expr; | 
|---|
|  | 69 | } | 
|---|
|  | 70 | } | 
|---|
|  | 71 |  | 
|---|
|  | 72 | CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) { | 
|---|
|  | 73 | doBeginScope(); | 
|---|
|  | 74 | mutateStatementList( compoundStmt->get_kids() ); | 
|---|
|  | 75 | doEndScope(); | 
|---|
|  | 76 | return compoundStmt; | 
|---|
|  | 77 | } | 
|---|
|  | 78 |  | 
|---|
|  | 79 | Statement * PolyMutator::mutate(IfStmt *ifStmt) { | 
|---|
|  | 80 | ifStmt->set_thenPart(  mutateStatement( ifStmt->get_thenPart() ) ); | 
|---|
|  | 81 | ifStmt->set_elsePart(  mutateStatement( ifStmt->get_elsePart() ) ); | 
|---|
|  | 82 | ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) ); | 
|---|
|  | 83 | return ifStmt; | 
|---|
|  | 84 | } | 
|---|
|  | 85 |  | 
|---|
|  | 86 | Statement * PolyMutator::mutate(WhileStmt *whileStmt) { | 
|---|
|  | 87 | whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) ); | 
|---|
|  | 88 | whileStmt->set_condition(  mutateExpression( whileStmt->get_condition() ) ); | 
|---|
|  | 89 | return whileStmt; | 
|---|
|  | 90 | } | 
|---|
|  | 91 |  | 
|---|
|  | 92 | Statement * PolyMutator::mutate(ForStmt *forStmt) { | 
|---|
|  | 93 | forStmt->set_body(  mutateStatement( forStmt->get_body() ) ); | 
|---|
| [145f1fc] | 94 | mutateAll( forStmt->get_initialization(), *this ); | 
|---|
| [01aeade] | 95 | forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) ); | 
|---|
|  | 96 | forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) ); | 
|---|
|  | 97 | return forStmt; | 
|---|
|  | 98 | } | 
|---|
|  | 99 |  | 
|---|
|  | 100 | Statement * PolyMutator::mutate(SwitchStmt *switchStmt) { | 
|---|
| [8688ce1] | 101 | mutateStatementList( switchStmt->get_statements() ); | 
|---|
| [01aeade] | 102 | switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) ); | 
|---|
|  | 103 | return switchStmt; | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
|  | 106 | Statement * PolyMutator::mutate(CaseStmt *caseStmt) { | 
|---|
|  | 107 | mutateStatementList( caseStmt->get_statements() ); | 
|---|
|  | 108 | caseStmt->set_condition(  mutateExpression( caseStmt->get_condition() ) ); | 
|---|
|  | 109 | return caseStmt; | 
|---|
|  | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | Statement * PolyMutator::mutate(TryStmt *tryStmt) { | 
|---|
|  | 113 | tryStmt->set_block(  maybeMutate( tryStmt->get_block(), *this ) ); | 
|---|
|  | 114 | mutateAll( tryStmt->get_catchers(), *this ); | 
|---|
|  | 115 | return tryStmt; | 
|---|
|  | 116 | } | 
|---|
|  | 117 |  | 
|---|
|  | 118 | Statement * PolyMutator::mutate(CatchStmt *cathStmt) { | 
|---|
|  | 119 | cathStmt->set_body(  mutateStatement( cathStmt->get_body() ) ); | 
|---|
|  | 120 | cathStmt->set_decl(  maybeMutate( cathStmt->get_decl(), *this ) ); | 
|---|
|  | 121 | return cathStmt; | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | Statement * PolyMutator::mutate(ReturnStmt *retStmt) { | 
|---|
|  | 125 | retStmt->set_expr( mutateExpression( retStmt->get_expr() ) ); | 
|---|
|  | 126 | return retStmt; | 
|---|
|  | 127 | } | 
|---|
|  | 128 |  | 
|---|
|  | 129 | Statement * PolyMutator::mutate(ExprStmt *exprStmt) { | 
|---|
|  | 130 | exprStmt->set_expr( mutateExpression( exprStmt->get_expr() ) ); | 
|---|
|  | 131 | return exprStmt; | 
|---|
|  | 132 | } | 
|---|
|  | 133 |  | 
|---|
|  | 134 |  | 
|---|
|  | 135 | Expression * PolyMutator::mutate(UntypedExpr *untypedExpr) { | 
|---|
|  | 136 | for ( std::list< Expression* >::iterator i = untypedExpr->get_args().begin(); i != untypedExpr->get_args().end(); ++i ) { | 
|---|
|  | 137 | *i = mutateExpression( *i ); | 
|---|
|  | 138 | } // for | 
|---|
|  | 139 | return untypedExpr; | 
|---|
|  | 140 | } | 
|---|
| [540de412] | 141 |  | 
|---|
|  | 142 |  | 
|---|
| [09f800b] | 143 | Initializer *PolyMutator::mutate( SingleInit *singleInit ) { | 
|---|
|  | 144 | singleInit->set_value( mutateExpression( singleInit->get_value() ) ); | 
|---|
|  | 145 | return singleInit; | 
|---|
|  | 146 | } | 
|---|
|  | 147 |  | 
|---|
| [51b73452] | 148 | } // namespace GenPoly | 
|---|
| [01aeade] | 149 |  | 
|---|
| [51587aa] | 150 | // Local Variables: // | 
|---|
|  | 151 | // tab-width: 4 // | 
|---|
|  | 152 | // mode: c++ // | 
|---|
|  | 153 | // compile-command: "make install" // | 
|---|
|  | 154 | // End: // | 
|---|