| 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 | //
 | 
|---|
| 7 | // PolyMutator.cc -- 
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Rob Schluntz
 | 
|---|
| 12 | // Last Modified On : Fri Aug 14 15:28:50 2015
 | 
|---|
| 13 | // Update Count     : 11
 | 
|---|
| 14 | //
 | 
|---|
| 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"
 | 
|---|
| 22 | #include "SynTree/Initializer.h"
 | 
|---|
| 23 | 
 | 
|---|
| 24 | namespace GenPoly {
 | 
|---|
| 25 |         namespace {
 | 
|---|
| 26 |                 const std::list<Label> noLabels;
 | 
|---|
| 27 |         }
 | 
|---|
| 28 | 
 | 
|---|
| 29 |         PolyMutator::PolyMutator() : scopeTyVars( (TypeDecl::Kind)-1 ), env( 0 ) {}
 | 
|---|
| 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 |                         }
 | 
|---|
| 65 |                         return expr->acceptMutator( *this );
 | 
|---|
| 66 |                 } else {
 | 
|---|
| 67 |                         return expr;
 | 
|---|
| 68 |                 }
 | 
|---|
| 69 |         }
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         CompoundStmt * PolyMutator::mutate(CompoundStmt *compoundStmt) {
 | 
|---|
| 72 |                 doBeginScope();
 | 
|---|
| 73 |                 mutateStatementList( compoundStmt->get_kids() );
 | 
|---|
| 74 |                 doEndScope();
 | 
|---|
| 75 |                 return compoundStmt;
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         Statement * PolyMutator::mutate(IfStmt *ifStmt) {
 | 
|---|
| 79 |                 ifStmt->set_thenPart(  mutateStatement( ifStmt->get_thenPart() ) );
 | 
|---|
| 80 |                 ifStmt->set_elsePart(  mutateStatement( ifStmt->get_elsePart() ) );
 | 
|---|
| 81 |                 ifStmt->set_condition(  mutateExpression( ifStmt->get_condition() ) );
 | 
|---|
| 82 |                 return ifStmt;
 | 
|---|
| 83 |         }
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         Statement * PolyMutator::mutate(WhileStmt *whileStmt) {
 | 
|---|
| 86 |                 whileStmt->set_body(  mutateStatement( whileStmt->get_body() ) );
 | 
|---|
| 87 |                 whileStmt->set_condition(  mutateExpression( whileStmt->get_condition() ) );
 | 
|---|
| 88 |                 return whileStmt;
 | 
|---|
| 89 |         }
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         Statement * PolyMutator::mutate(ForStmt *forStmt) {
 | 
|---|
| 92 |                 forStmt->set_body(  mutateStatement( forStmt->get_body() ) );
 | 
|---|
| 93 |                 mutateAll( forStmt->get_initialization(), *this );
 | 
|---|
| 94 |                 forStmt->set_condition(  mutateExpression( forStmt->get_condition() ) );
 | 
|---|
| 95 |                 forStmt->set_increment(  mutateExpression( forStmt->get_increment() ) );
 | 
|---|
| 96 |                 return forStmt;
 | 
|---|
| 97 |         }
 | 
|---|
| 98 | 
 | 
|---|
| 99 |         Statement * PolyMutator::mutate(SwitchStmt *switchStmt) {
 | 
|---|
| 100 |                 mutateStatementList( switchStmt->get_branches() );
 | 
|---|
| 101 |                 switchStmt->set_condition( mutateExpression( switchStmt->get_condition() ) );
 | 
|---|
| 102 |                 return switchStmt;
 | 
|---|
| 103 |         }
 | 
|---|
| 104 | 
 | 
|---|
| 105 |         Statement * PolyMutator::mutate(ChooseStmt *switchStmt) {
 | 
|---|
| 106 |                 mutateStatementList( switchStmt->get_branches() );
 | 
|---|
| 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 |         }
 | 
|---|
| 146 |  
 | 
|---|
| 147 |  
 | 
|---|
| 148 |         Initializer *PolyMutator::mutate( SingleInit *singleInit ) {
 | 
|---|
| 149 |                 singleInit->set_value( mutateExpression( singleInit->get_value() ) );
 | 
|---|
| 150 |                 return singleInit;
 | 
|---|
| 151 |         }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | } // namespace GenPoly
 | 
|---|
| 154 | 
 | 
|---|
| 155 | // Local Variables: //
 | 
|---|
| 156 | // tab-width: 4 //
 | 
|---|
| 157 | // mode: c++ //
 | 
|---|
| 158 | // compile-command: "make install" //
 | 
|---|
| 159 | // End: //
 | 
|---|